2024-07-09 15:39:40 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-09-09 03:16:54 +09:00
|
|
|
"encoding/json"
|
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"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
2024-09-04 01:20:12 +09:00
|
|
|
|
2024-09-16 20:31:15 +09:00
|
|
|
"git.ophivana.moe/cat/fortify/internal/final"
|
|
|
|
|
2024-09-09 03:16:54 +09:00
|
|
|
"git.ophivana.moe/cat/fortify/dbus"
|
2024-09-04 01:20:12 +09:00
|
|
|
"git.ophivana.moe/cat/fortify/internal/app"
|
2024-09-12 21:07:05 +09:00
|
|
|
"git.ophivana.moe/cat/fortify/internal/verbose"
|
2024-07-09 15:39:40 +09:00
|
|
|
)
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
var (
|
|
|
|
Version = "impure"
|
2024-09-09 03:16:54 +09:00
|
|
|
|
|
|
|
a *app.App
|
2024-09-09 21:19:12 +09:00
|
|
|
|
|
|
|
dbusSession *dbus.Config
|
|
|
|
dbusSystem *dbus.Config
|
2024-09-12 20:53:33 +09:00
|
|
|
|
|
|
|
launchOptionText string
|
2024-09-04 01:20:12 +09:00
|
|
|
)
|
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-09 15:39:40 +09:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2024-09-12 21:07:05 +09:00
|
|
|
verbose.Set(flagVerbose)
|
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()
|
|
|
|
|
2024-09-12 20:53:33 +09:00
|
|
|
a = app.New(userName, flag.Args(), launchOptionText)
|
2024-09-16 20:31:15 +09:00
|
|
|
final.Prepare(*a.User, a.UID(), a.RunDir())
|
2024-09-04 01:20:12 +09:00
|
|
|
|
2024-09-09 03:16:54 +09:00
|
|
|
// parse D-Bus config file if applicable
|
|
|
|
if mustDBus {
|
2024-09-09 21:19:12 +09:00
|
|
|
if dbusConfigSession == "builtin" {
|
|
|
|
dbusSession = dbus.NewConfig(dbusID, true, mpris)
|
2024-09-09 03:16:54 +09:00
|
|
|
} else {
|
2024-09-09 21:19:12 +09:00
|
|
|
if f, err := os.Open(dbusConfigSession); err != nil {
|
2024-09-16 20:31:15 +09:00
|
|
|
final.Fatal("Error opening D-Bus proxy config file:", err)
|
2024-09-09 21:19:12 +09:00
|
|
|
} else {
|
|
|
|
if err = json.NewDecoder(f).Decode(&dbusSession); err != nil {
|
2024-09-16 20:31:15 +09:00
|
|
|
final.Fatal("Error parsing D-Bus proxy config file:", err)
|
2024-09-09 21:19:12 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// system bus proxy is optional
|
|
|
|
if dbusConfigSystem != "nil" {
|
|
|
|
if f, err := os.Open(dbusConfigSystem); err != nil {
|
2024-09-16 20:31:15 +09:00
|
|
|
final.Fatal("Error opening D-Bus proxy config file:", err)
|
2024-09-09 03:16:54 +09:00
|
|
|
} else {
|
2024-09-09 21:19:12 +09:00
|
|
|
if err = json.NewDecoder(f).Decode(&dbusSystem); err != nil {
|
2024-09-16 20:31:15 +09:00
|
|
|
final.Fatal("Error parsing D-Bus proxy config file:", err)
|
2024-09-09 03:16:54 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// ensure RunDir (e.g. `/run/user/%d/fortify`)
|
2024-09-16 20:31:15 +09:00
|
|
|
a.EnsureRunDir()
|
2024-07-15 23:29:21 +09:00
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
// state query command early exit
|
2024-09-16 20:31:15 +09:00
|
|
|
tryState()
|
2024-09-04 01:20:12 +09:00
|
|
|
|
|
|
|
// ensure Share (e.g. `/tmp/fortify.%d`)
|
2024-09-16 20:31:15 +09:00
|
|
|
a.EnsureShare()
|
2024-09-12 20:53:33 +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 {
|
2024-09-12 21:07:05 +09:00
|
|
|
if verbose.Get() {
|
2024-07-15 23:29:21 +09:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, fs.ErrPermission):
|
2024-09-12 21:07:05 +09:00
|
|
|
fmt.Printf("User %s home directory %s is not accessible\n", a.Username, a.HomeDir)
|
2024-07-15 23:29:21 +09:00
|
|
|
case errors.Is(err, fs.ErrNotExist):
|
2024-09-12 21:07:05 +09:00
|
|
|
fmt.Printf("User %s home directory %s does not exis\n", a.Username, a.HomeDir)
|
2024-07-15 23:29:21 +09:00
|
|
|
default:
|
2024-09-12 21:07:05 +09:00
|
|
|
fmt.Printf("Error stat user %s home directory %s: %s\n", 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`)
|
2024-09-16 20:31:15 +09:00
|
|
|
a.EnsureRuntime()
|
2024-07-15 23:29:21 +09:00
|
|
|
|
2024-09-08 02:24:01 +09:00
|
|
|
if mustWayland {
|
|
|
|
a.ShareWayland()
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
|
2024-09-08 02:24:01 +09:00
|
|
|
if mustX {
|
|
|
|
a.ShareX()
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
|
2024-09-08 02:24:01 +09:00
|
|
|
if mustDBus {
|
2024-09-09 21:19:12 +09:00
|
|
|
a.ShareDBus(dbusSession, dbusSystem, dbusVerbose)
|
2024-07-15 23:29:21 +09:00
|
|
|
}
|
|
|
|
|
2024-09-08 02:24:01 +09:00
|
|
|
if mustPulse {
|
|
|
|
a.SharePulse()
|
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
|
|
|
}
|