2024-09-08 02:24:01 +09:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.ophivana.moe/cat/fortify/internal/state"
|
|
|
|
"git.ophivana.moe/cat/fortify/internal/system"
|
|
|
|
"git.ophivana.moe/cat/fortify/internal/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
term = "TERM"
|
|
|
|
sudoAskPass = "SUDO_ASKPASS"
|
|
|
|
)
|
|
|
|
const (
|
2024-09-12 20:53:33 +09:00
|
|
|
LaunchMethodSudo uint8 = iota
|
|
|
|
LaunchMethodBwrap
|
2024-09-08 02:24:01 +09:00
|
|
|
LaunchMethodMachineCtl
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a *App) Run() {
|
|
|
|
// pass $TERM to launcher
|
|
|
|
if t, ok := os.LookupEnv(term); ok {
|
|
|
|
a.AppendEnv(term, t)
|
|
|
|
}
|
|
|
|
|
2024-09-12 20:53:33 +09:00
|
|
|
var commandBuilder func() (args []string)
|
|
|
|
|
|
|
|
switch a.launchOption {
|
|
|
|
case LaunchMethodSudo:
|
|
|
|
commandBuilder = a.commandBuilderSudo
|
|
|
|
case LaunchMethodBwrap:
|
|
|
|
commandBuilder = a.commandBuilderBwrap
|
|
|
|
case LaunchMethodMachineCtl:
|
|
|
|
commandBuilder = a.commandBuilderMachineCtl
|
|
|
|
default:
|
|
|
|
panic("unreachable")
|
2024-09-08 02:24:01 +09:00
|
|
|
}
|
|
|
|
|
2024-09-12 20:53:33 +09:00
|
|
|
cmd := exec.Command(a.toolPath, commandBuilder()...)
|
2024-09-09 22:24:58 +09:00
|
|
|
cmd.Env = []string{}
|
2024-09-08 02:24:01 +09:00
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Dir = system.V.RunDir
|
|
|
|
|
|
|
|
if system.V.Verbose {
|
|
|
|
fmt.Println("Executing:", cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
state.Fatal("Error starting process:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.RegisterEnablement(a.enablements)
|
|
|
|
|
|
|
|
if err := state.SaveProcess(a.Uid, cmd); err != nil {
|
|
|
|
// process already started, shouldn't be fatal
|
|
|
|
fmt.Println("Error registering process:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var r int
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
var exitError *exec.ExitError
|
|
|
|
if !errors.As(err, &exitError) {
|
|
|
|
state.Fatal("Error running process:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if system.V.Verbose {
|
|
|
|
fmt.Println("Process exited with exit code", r)
|
|
|
|
}
|
|
|
|
state.BeforeExit()
|
|
|
|
os.Exit(r)
|
|
|
|
}
|
|
|
|
|
2024-09-09 00:32:17 +09:00
|
|
|
func (a *App) commandBuilderSudo() (args []string) {
|
2024-09-08 02:24:01 +09:00
|
|
|
args = make([]string, 0, 4+len(a.env)+len(a.command))
|
|
|
|
|
|
|
|
// -Hiu $USER
|
|
|
|
args = append(args, "-Hiu", a.Username)
|
|
|
|
|
|
|
|
// -A?
|
|
|
|
if _, ok := os.LookupEnv(sudoAskPass); ok {
|
|
|
|
if system.V.Verbose {
|
|
|
|
fmt.Printf("%s set, adding askpass flag\n", sudoAskPass)
|
|
|
|
}
|
|
|
|
args = append(args, "-A")
|
|
|
|
}
|
|
|
|
|
|
|
|
// environ
|
|
|
|
args = append(args, a.env...)
|
|
|
|
|
|
|
|
// -- $@
|
|
|
|
args = append(args, "--")
|
|
|
|
args = append(args, a.command...)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-12 20:53:33 +09:00
|
|
|
func (a *App) commandBuilderBwrap() (args []string) {
|
|
|
|
// TODO: build bwrap command
|
|
|
|
state.Fatal("bwrap")
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2024-09-09 00:32:17 +09:00
|
|
|
func (a *App) commandBuilderMachineCtl() (args []string) {
|
2024-09-08 02:24:01 +09:00
|
|
|
args = make([]string, 0, 9+len(a.env))
|
|
|
|
|
|
|
|
// shell --uid=$USER
|
|
|
|
args = append(args, "shell", "--uid="+a.Username)
|
|
|
|
|
|
|
|
// --quiet
|
|
|
|
if !system.V.Verbose {
|
|
|
|
args = append(args, "--quiet")
|
|
|
|
}
|
|
|
|
|
|
|
|
// environ
|
|
|
|
envQ := make([]string, len(a.env)+1)
|
|
|
|
for i, e := range a.env {
|
|
|
|
envQ[i] = "-E" + e
|
|
|
|
}
|
|
|
|
envQ[len(a.env)] = "-E" + a.launcherPayloadEnv()
|
|
|
|
args = append(args, envQ...)
|
|
|
|
|
|
|
|
// -- .host
|
|
|
|
args = append(args, "--", ".host")
|
|
|
|
|
|
|
|
// /bin/sh -c
|
|
|
|
if sh, ok := util.Which("sh"); !ok {
|
|
|
|
state.Fatal("Did not find 'sh' in PATH")
|
|
|
|
} else {
|
|
|
|
args = append(args, sh, "-c")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.command) == 0 { // execute shell if command is not provided
|
|
|
|
a.command = []string{"$SHELL"}
|
|
|
|
}
|
|
|
|
|
|
|
|
innerCommand := strings.Builder{}
|
|
|
|
|
2024-09-09 00:32:17 +09:00
|
|
|
innerCommand.WriteString("dbus-update-activation-environment --systemd")
|
|
|
|
for _, e := range a.env {
|
|
|
|
innerCommand.WriteString(" " + strings.SplitN(e, "=", 2)[0])
|
2024-09-08 02:24:01 +09:00
|
|
|
}
|
2024-09-09 00:32:17 +09:00
|
|
|
innerCommand.WriteString("; ")
|
2024-09-08 02:24:01 +09:00
|
|
|
|
|
|
|
if executable, err := os.Executable(); err != nil {
|
|
|
|
state.Fatal("Error reading executable path:", err)
|
|
|
|
} else {
|
2024-09-09 03:16:54 +09:00
|
|
|
if a.enablements.Has(state.EnableDBus) {
|
2024-09-09 21:19:12 +09:00
|
|
|
innerCommand.WriteString(dbusSessionBusAddress + "=" + "'" + dbusAddress[0] + "' ")
|
|
|
|
if dbusSystem {
|
|
|
|
innerCommand.WriteString(dbusSystemBusAddress + "=" + "'" + dbusAddress[1] + "' ")
|
|
|
|
}
|
2024-09-09 03:16:54 +09:00
|
|
|
}
|
2024-09-08 02:24:01 +09:00
|
|
|
innerCommand.WriteString("exec " + executable + " -V")
|
|
|
|
}
|
|
|
|
args = append(args, innerCommand.String())
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|