2024-07-09 15:39:40 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-15 23:29:21 +09:00
|
|
|
"errors"
|
2024-07-09 15:39:40 +09:00
|
|
|
"flag"
|
2024-07-15 23:29:21 +09:00
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
2024-09-04 01:20:12 +09:00
|
|
|
|
|
|
|
"git.ophivana.moe/cat/fortify/internal/acl"
|
|
|
|
"git.ophivana.moe/cat/fortify/internal/app"
|
|
|
|
"git.ophivana.moe/cat/fortify/internal/state"
|
|
|
|
"git.ophivana.moe/cat/fortify/internal/system"
|
|
|
|
"git.ophivana.moe/cat/fortify/internal/util"
|
|
|
|
"git.ophivana.moe/cat/fortify/internal/xcb"
|
2024-07-09 15:39:40 +09:00
|
|
|
)
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
var (
|
|
|
|
Version = "impure"
|
|
|
|
a *app.App
|
|
|
|
)
|
2024-07-09 15:39:40 +09:00
|
|
|
|
2024-07-16 22:07:40 +09:00
|
|
|
func tryVersion() {
|
|
|
|
if printVersion {
|
|
|
|
fmt.Println(Version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-15 23:29:21 +09:00
|
|
|
const (
|
2024-09-04 01:20:12 +09:00
|
|
|
term = "TERM"
|
|
|
|
display = "DISPLAY"
|
2024-07-15 23:29:21 +09:00
|
|
|
|
|
|
|
// https://manpages.debian.org/experimental/libwayland-doc/wl_display_connect.3.en.html
|
|
|
|
waylandDisplay = "WAYLAND_DISPLAY"
|
|
|
|
)
|
|
|
|
|
2024-07-09 15:39:40 +09:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2024-07-15 23:29:21 +09:00
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// launcher payload early exit
|
|
|
|
app.Early(printVersion)
|
2024-07-15 23:29:21 +09:00
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// version/license command early exit
|
|
|
|
tryVersion()
|
|
|
|
tryLicense()
|
|
|
|
|
|
|
|
system.Retrieve(flagVerbose)
|
|
|
|
a = app.New(userName, flag.Args())
|
|
|
|
state.Set(*a.User, a.Command(), a.UID())
|
|
|
|
|
|
|
|
// ensure RunDir (e.g. `/run/user/%d/fortify`)
|
|
|
|
if err := os.Mkdir(system.V.RunDir, 0700); err != nil && !errors.Is(err, fs.ErrExist) {
|
|
|
|
state.Fatal("Error creating runtime directory:", err)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// state query command early exit
|
|
|
|
state.Early()
|
|
|
|
|
|
|
|
// ensure Share (e.g. `/tmp/fortify.%d`)
|
|
|
|
// acl is unnecessary as this directory is world executable
|
|
|
|
if err := os.Mkdir(system.V.Share, 0701); err != nil && !errors.Is(err, fs.ErrExist) {
|
|
|
|
state.Fatal("Error creating shared directory:", err)
|
|
|
|
}
|
2024-07-16 14:19:43 +09:00
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// warn about target user home directory ownership
|
|
|
|
if stat, err := os.Stat(a.HomeDir); err != nil {
|
|
|
|
if system.V.Verbose {
|
2024-07-15 23:29:21 +09:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, fs.ErrPermission):
|
2024-09-04 01:20:12 +09:00
|
|
|
fmt.Printf("User %s home directory %s is not accessible", a.Username, a.HomeDir)
|
2024-07-15 23:29:21 +09:00
|
|
|
case errors.Is(err, fs.ErrNotExist):
|
2024-09-04 01:20:12 +09:00
|
|
|
fmt.Printf("User %s home directory %s does not exist", a.Username, a.HomeDir)
|
2024-07-15 23:29:21 +09:00
|
|
|
default:
|
2024-09-04 01:20:12 +09:00
|
|
|
fmt.Printf("Error stat user %s home directory %s: %s", a.Username, a.HomeDir, err)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// FreeBSD: not cross-platform
|
2024-09-04 01:20:12 +09:00
|
|
|
if u := strconv.Itoa(int(stat.Sys().(*syscall.Stat_t).Uid)); u != a.Uid {
|
|
|
|
fmt.Printf("User %s home directory %s has incorrect ownership (expected UID %s, found %s)", a.Username, a.HomeDir, a.Uid, u)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// ensure runtime directory ACL (e.g. `/run/user/%d`)
|
|
|
|
if s, err := os.Stat(system.V.Runtime); err != nil {
|
2024-07-15 23:29:21 +09:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.Fatal("Runtime directory does not exist")
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
2024-09-04 01:20:12 +09:00
|
|
|
state.Fatal("Error accessing runtime directory:", err)
|
2024-07-15 23:29:21 +09:00
|
|
|
} else if !s.IsDir() {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.Fatal(fmt.Sprintf("Path '%s' is not a directory", system.V.Runtime))
|
2024-07-15 23:29:21 +09:00
|
|
|
} else {
|
2024-09-04 01:20:12 +09:00
|
|
|
if err = acl.UpdatePerm(system.V.Runtime, a.UID(), acl.Execute); err != nil {
|
|
|
|
state.Fatal("Error preparing runtime dir:", err)
|
2024-07-16 14:19:43 +09:00
|
|
|
} else {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.RegisterRevertPath(system.V.Runtime)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
2024-09-04 01:20:12 +09:00
|
|
|
if system.V.Verbose {
|
|
|
|
fmt.Printf("Runtime data dir '%s' configured\n", system.V.Runtime)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// ensure Wayland socket ACL (e.g. `/run/user/%d/wayland-%d`)
|
2024-07-15 23:29:21 +09:00
|
|
|
if w, ok := os.LookupEnv(waylandDisplay); !ok {
|
2024-09-04 01:20:12 +09:00
|
|
|
if system.V.Verbose {
|
2024-07-15 23:29:21 +09:00
|
|
|
fmt.Println("Wayland: WAYLAND_DISPLAY not set, skipping")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// add environment variable for new process
|
2024-09-04 01:20:12 +09:00
|
|
|
wp := path.Join(system.V.Runtime, w)
|
|
|
|
a.AppendEnv(waylandDisplay, wp)
|
|
|
|
if err := acl.UpdatePerm(wp, a.UID(), acl.Read, acl.Write, acl.Execute); err != nil {
|
|
|
|
state.Fatal(fmt.Sprintf("Error preparing Wayland '%s':", w), err)
|
2024-07-16 14:19:43 +09:00
|
|
|
} else {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.RegisterRevertPath(wp)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
2024-09-04 01:20:12 +09:00
|
|
|
if system.V.Verbose {
|
2024-07-15 23:29:21 +09:00
|
|
|
fmt.Printf("Wayland socket '%s' configured\n", w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// discovery X11 and grant user permission via the `ChangeHosts` command
|
2024-07-15 23:29:21 +09:00
|
|
|
if d, ok := os.LookupEnv(display); !ok {
|
2024-09-04 01:20:12 +09:00
|
|
|
if system.V.Verbose {
|
2024-07-15 23:29:21 +09:00
|
|
|
fmt.Println("X11: DISPLAY not set, skipping")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// add environment variable for new process
|
2024-09-04 01:20:12 +09:00
|
|
|
a.AppendEnv(display, d)
|
2024-07-16 14:19:43 +09:00
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
if system.V.Verbose {
|
|
|
|
fmt.Printf("X11: Adding XHost entry SI:localuser:%s to display '%s'\n", a.Username, d)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
2024-09-04 01:20:12 +09:00
|
|
|
if err := xcb.ChangeHosts(xcb.HostModeInsert, xcb.FamilyServerInterpreted, "localuser\x00"+a.Username); err != nil {
|
|
|
|
state.Fatal(fmt.Sprintf("Error adding XHost entry to '%s':", d), err)
|
2024-07-16 14:19:43 +09:00
|
|
|
} else {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.XcbActionComplete()
|
2024-07-16 14:19:43 +09:00
|
|
|
}
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// ensure PulseAudio directory ACL (e.g. `/run/user/%d/pulse`)
|
|
|
|
pulse := path.Join(system.V.Runtime, "pulse")
|
2024-07-15 23:29:21 +09:00
|
|
|
pulseS := path.Join(pulse, "native")
|
|
|
|
if s, err := os.Stat(pulse); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.Fatal("Error accessing PulseAudio directory:", err)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
if mustPulse {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.Fatal("PulseAudio is unavailable")
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
2024-09-04 01:20:12 +09:00
|
|
|
if system.V.Verbose {
|
2024-07-15 23:29:21 +09:00
|
|
|
fmt.Printf("PulseAudio dir '%s' not found, skipping\n", pulse)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// add environment variable for new process
|
2024-09-04 01:20:12 +09:00
|
|
|
a.AppendEnv(util.PulseServer, "unix:"+pulseS)
|
|
|
|
if err = acl.UpdatePerm(pulse, a.UID(), acl.Execute); err != nil {
|
|
|
|
state.Fatal("Error preparing PulseAudio:", err)
|
2024-07-16 14:19:43 +09:00
|
|
|
} else {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.RegisterRevertPath(pulse)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// ensure PulseAudio socket permission (e.g. `/run/user/%d/pulse/native`)
|
2024-07-15 23:29:21 +09:00
|
|
|
if s, err = os.Stat(pulseS); err != nil {
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.Fatal("PulseAudio directory found but socket does not exist")
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
2024-09-04 01:20:12 +09:00
|
|
|
state.Fatal("Error accessing PulseAudio socket:", err)
|
2024-07-15 23:29:21 +09:00
|
|
|
} else {
|
|
|
|
if m := s.Mode(); m&0o006 != 0o006 {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.Fatal(fmt.Sprintf("Unexpected permissions on '%s':", pulseS), m)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish current user's pulse-cookie for target user
|
2024-09-04 01:20:12 +09:00
|
|
|
pulseCookieSource := util.DiscoverPulseCookie()
|
|
|
|
pulseCookieFinal := path.Join(system.V.Share, "pulse-cookie")
|
|
|
|
a.AppendEnv(util.PulseCookie, pulseCookieFinal)
|
|
|
|
if system.V.Verbose {
|
2024-07-15 23:29:21 +09:00
|
|
|
fmt.Printf("Publishing PulseAudio cookie '%s' to '%s'\n", pulseCookieSource, pulseCookieFinal)
|
|
|
|
}
|
2024-09-04 01:20:12 +09:00
|
|
|
if err = util.CopyFile(pulseCookieFinal, pulseCookieSource); err != nil {
|
|
|
|
state.Fatal("Error copying PulseAudio cookie:", err)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
2024-09-04 01:20:12 +09:00
|
|
|
if err = acl.UpdatePerm(pulseCookieFinal, a.UID(), acl.Read); err != nil {
|
|
|
|
state.Fatal("Error publishing PulseAudio cookie:", err)
|
2024-07-16 14:19:43 +09:00
|
|
|
} else {
|
2024-09-04 01:20:12 +09:00
|
|
|
state.RegisterRevertPath(pulseCookieFinal)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
if system.V.Verbose {
|
2024-07-15 23:29:21 +09:00
|
|
|
fmt.Printf("PulseAudio dir '%s' configured\n", pulse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass $TERM to launcher
|
|
|
|
if t, ok := os.LookupEnv(term); ok {
|
2024-09-04 01:20:12 +09:00
|
|
|
a.AppendEnv(term, t)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
a.Run()
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|