2024-07-09 15:39:40 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2024-07-15 23:29:21 +09:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-10-13 00:09:14 +09:00
|
|
|
"syscall"
|
2024-09-04 01:20:12 +09:00
|
|
|
|
2024-09-17 13:48:42 +09:00
|
|
|
"git.ophivana.moe/cat/fortify/internal"
|
2024-09-04 01:20:12 +09:00
|
|
|
"git.ophivana.moe/cat/fortify/internal/app"
|
2024-10-14 02:27:02 +09:00
|
|
|
init0 "git.ophivana.moe/cat/fortify/internal/init"
|
2024-10-11 02:01:03 +09:00
|
|
|
"git.ophivana.moe/cat/fortify/internal/shim"
|
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 (
|
2024-10-12 01:28:22 +09:00
|
|
|
flagVerbose bool
|
2024-09-04 01:20:12 +09:00
|
|
|
)
|
2024-07-09 15:39:40 +09:00
|
|
|
|
2024-10-12 01:28:22 +09:00
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&flagVerbose, "v", false, "Verbose output")
|
2024-07-16 22:07:40 +09:00
|
|
|
}
|
|
|
|
|
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-10-13 00:09:14 +09:00
|
|
|
// linux/sched/coredump.h
|
|
|
|
if _, _, errno := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_SET_DUMPABLE, 0, 0); errno != 0 {
|
|
|
|
fmt.Printf("fortify: cannot set SUID_DUMP_DISABLE: %s", errno.Error())
|
|
|
|
}
|
|
|
|
|
2024-09-22 00:29:36 +09:00
|
|
|
if internal.SdBootedV {
|
|
|
|
verbose.Println("system booted with systemd as init system")
|
|
|
|
}
|
|
|
|
|
2024-10-14 02:27:02 +09:00
|
|
|
// shim/init early exit
|
|
|
|
init0.Try()
|
2024-10-13 12:09:38 +09:00
|
|
|
shim.Try()
|
2024-07-15 23:29:21 +09:00
|
|
|
|
2024-10-13 20:06:47 +09:00
|
|
|
// root check
|
|
|
|
if os.Getuid() == 0 {
|
|
|
|
fmt.Println("fortify: this program must not run as root")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-10-12 19:46:40 +09:00
|
|
|
// version/license/template command early exit
|
2024-09-04 01:20:12 +09:00
|
|
|
tryVersion()
|
|
|
|
tryLicense()
|
2024-10-12 19:46:40 +09:00
|
|
|
tryTemplate()
|
2024-09-04 01:20:12 +09:00
|
|
|
|
2024-09-22 00:29:36 +09:00
|
|
|
// state query command early exit
|
|
|
|
tryState()
|
|
|
|
|
|
|
|
// invoke app
|
|
|
|
r := 1
|
|
|
|
a := app.New()
|
2024-10-12 01:51:06 +09:00
|
|
|
if err := a.Seal(loadConfig()); err != nil {
|
2024-09-22 00:29:36 +09:00
|
|
|
logBaseError(err, "fortify: cannot seal app:")
|
|
|
|
} else if err = a.Start(); err != nil {
|
|
|
|
logBaseError(err, "fortify: cannot start app:")
|
|
|
|
} else if r, err = a.Wait(); err != nil {
|
2024-10-12 02:11:43 +09:00
|
|
|
if r < 1 {
|
|
|
|
r = 1
|
2024-09-09 03:16:54 +09:00
|
|
|
}
|
2024-10-12 02:11:43 +09:00
|
|
|
logWaitError(err)
|
2024-09-09 03:16:54 +09:00
|
|
|
}
|
2024-09-22 00:29:36 +09:00
|
|
|
if err := a.WaitErr(); err != nil {
|
|
|
|
fmt.Println("fortify: inner wait failed:", err)
|
|
|
|
}
|
|
|
|
os.Exit(r)
|
|
|
|
}
|