aboutsummaryrefslogtreecommitdiffhomepage
path: root/config.go
blob: aadf87ff4e325bffd775d1d74bb89c8045ed3add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"os"

	"git.ophivana.moe/cat/fortify/dbus"
	"git.ophivana.moe/cat/fortify/internal"
	"git.ophivana.moe/cat/fortify/internal/app"
	"git.ophivana.moe/cat/fortify/internal/system"
)

var (
	printTemplate bool

	confPath string

	dbusConfigSession string
	dbusConfigSystem  string
	dbusID            string
	mpris             bool
	dbusVerbose       bool

	userName    string
	enablements [system.ELen]bool

	launchMethodText string
)

func init() {
	flag.BoolVar(&printTemplate, "template", false, "Print a full config template and exit")

	// config file, disables every other flag here
	flag.StringVar(&confPath, "c", "nil", "Path to full app configuration, or \"nil\" to configure from flags")

	flag.StringVar(&dbusConfigSession, "dbus-config", "builtin", "Path to D-Bus proxy config file, or \"builtin\" for defaults")
	flag.StringVar(&dbusConfigSystem, "dbus-system", "nil", "Path to system D-Bus proxy config file, or \"nil\" to disable")
	flag.StringVar(&dbusID, "dbus-id", "", "D-Bus ID of application, leave empty to disable own paths, has no effect if custom config is available")
	flag.BoolVar(&mpris, "mpris", false, "Allow owning MPRIS D-Bus path, has no effect if custom config is available")
	flag.BoolVar(&dbusVerbose, "dbus-log", false, "Force logging in the D-Bus proxy")

	flag.StringVar(&userName, "u", "chronos", "Passwd name of user to run as")
	flag.BoolVar(&enablements[system.EWayland], "wayland", false, "Share Wayland socket")
	flag.BoolVar(&enablements[system.EX11], "X", false, "Share X11 socket and allow connection")
	flag.BoolVar(&enablements[system.EDBus], "dbus", false, "Proxy D-Bus connection")
	flag.BoolVar(&enablements[system.EPulse], "pulse", false, "Share PulseAudio socket and cookie")
}

func init() {
	methodHelpString := "Method of launching the child process, can be one of \"sudo\""
	if internal.SdBootedV {
		methodHelpString += ", \"systemd\""
	}

	flag.StringVar(&launchMethodText, "method", "sudo", methodHelpString)
}

func tryTemplate() {
	if printTemplate {
		if s, err := json.MarshalIndent(app.Template(), "", "  "); err != nil {
			fatalf("cannot generate template: %v", err)
			panic("unreachable")
		} else {
			fmt.Println(string(s))
		}
		os.Exit(0)
	}
}

func loadConfig() *app.Config {
	if confPath == "nil" {
		// config from flags
		return configFromFlags()
	} else {
		// config from file
		c := new(app.Config)
		if f, err := os.Open(confPath); err != nil {
			fatalf("cannot access config file '%s': %s\n", confPath, err)
			panic("unreachable")
		} else if err = json.NewDecoder(f).Decode(&c); err != nil {
			fatalf("cannot parse config file '%s': %s\n", confPath, err)
			panic("unreachable")
		} else {
			return c
		}
	}
}

func configFromFlags() (config *app.Config) {
	// initialise config from flags
	config = &app.Config{
		ID:      dbusID,
		User:    userName,
		Command: flag.Args(),
		Method:  launchMethodText,
	}

	// enablements from flags
	for i := system.Enablement(0); i < system.Enablement(system.ELen); i++ {
		if enablements[i] {
			config.Confinement.Enablements.Set(i)
		}
	}

	// parse D-Bus config file from flags if applicable
	if enablements[system.EDBus] {
		if dbusConfigSession == "builtin" {
			config.Confinement.SessionBus = dbus.NewConfig(dbusID, true, mpris)
		} else {
			if c, err := dbus.NewConfigFromFile(dbusConfigSession); err != nil {
				fatalf("cannot load session bus proxy config from %q: %s\n", dbusConfigSession, err)
			} else {
				config.Confinement.SessionBus = c
			}
		}

		// system bus proxy is optional
		if dbusConfigSystem != "nil" {
			if c, err := dbus.NewConfigFromFile(dbusConfigSystem); err != nil {
				fatalf("cannot load system bus proxy config from %q: %s\n", dbusConfigSystem, err)
			} else {
				config.Confinement.SystemBus = c
			}
		}

		// override log from configuration
		if dbusVerbose {
			config.Confinement.SessionBus.Log = true
			config.Confinement.SystemBus.Log = true
		}
	}

	return
}