2023-09-12 02:22:48 +03:00
|
|
|
//go:build linux || freebsd
|
|
|
|
|
2023-08-03 01:42:31 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2023-09-12 02:22:48 +03:00
|
|
|
"github.com/quexten/goldwarden/agent/systemauth/biometrics"
|
2023-08-03 01:42:31 +03:00
|
|
|
"github.com/quexten/goldwarden/browserbiometrics"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupPolkit() {
|
2023-08-04 01:44:15 +03:00
|
|
|
file, err := os.Create("/tmp/goldwarden-policy")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-09-12 02:22:48 +03:00
|
|
|
_, err = file.WriteString(biometrics.POLICY)
|
2023-08-04 01:44:15 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = file.Close()
|
2023-08-03 01:42:31 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
command := exec.Command("pkexec", "mv", "/tmp/goldwarden-policy", "/usr/share/polkit-1/actions/com.quexten.goldwarden.policy")
|
|
|
|
err = command.Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-09-19 16:12:42 +03:00
|
|
|
command2 := exec.Command("pkexec", "chown", "root:root", "/usr/share/polkit-1/actions/com.quexten.goldwarden.policy")
|
|
|
|
err = command2.Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-12-21 14:56:48 +03:00
|
|
|
command3 := exec.Command("sudo", "chcon", "system_u:object_r:usr_t:s0", "/usr/share/polkit-1/actions/com.quexten.goldwarden.policy")
|
|
|
|
err = command3.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("failed setting selinux context")
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Println("Set selinux context successfully")
|
|
|
|
fmt.Println("Might require a reboot to take effect!")
|
|
|
|
}
|
|
|
|
|
2023-08-04 01:44:15 +03:00
|
|
|
fmt.Println("Polkit setup successfully")
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsPolkitSetup() bool {
|
|
|
|
_, err := os.Stat("/usr/share/polkit-1/actions/com.quexten.goldwarden.policy")
|
|
|
|
return !os.IsNotExist(err)
|
2023-08-03 01:42:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var polkitCmd = &cobra.Command{
|
|
|
|
Use: "polkit",
|
|
|
|
Short: "Sets up polkit",
|
|
|
|
Long: "Sets up polkit",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
setupPolkit()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const SYSTEMD_SERVICE = `[Unit]
|
|
|
|
Description="Goldwarden daemon"
|
2024-01-10 18:48:01 +03:00
|
|
|
After=graphical-session.target
|
2023-08-03 01:42:31 +03:00
|
|
|
|
|
|
|
[Service]
|
|
|
|
ExecStart=BINARY_PATH daemonize
|
|
|
|
|
|
|
|
[Install]
|
2023-09-12 01:24:47 +03:00
|
|
|
WantedBy=graphical-session.target`
|
2023-08-03 01:42:31 +03:00
|
|
|
|
|
|
|
func setupSystemd() {
|
2023-08-04 01:45:13 +03:00
|
|
|
file, err := os.Create("/tmp/goldwarden.service")
|
2023-08-03 01:42:31 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file.WriteString(strings.ReplaceAll(SYSTEMD_SERVICE, "BINARY_PATH", path))
|
|
|
|
file.Close()
|
|
|
|
|
2023-09-11 23:47:37 +03:00
|
|
|
userDirectory := os.Getenv("HOME")
|
2023-12-16 08:40:00 +03:00
|
|
|
//ensure user systemd dir exists
|
|
|
|
command0 := exec.Command("mkdir", "-p", userDirectory+"/.config/systemd/user/")
|
|
|
|
err = command0.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("failed creating systemd user dir")
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-09-19 16:12:42 +03:00
|
|
|
command := exec.Command("mv", "/tmp/goldwarden.service", userDirectory+"/.config/systemd/user/goldwarden.service")
|
2023-08-03 01:42:31 +03:00
|
|
|
err = command.Run()
|
|
|
|
if err != nil {
|
2023-12-16 08:40:00 +03:00
|
|
|
fmt.Println("failed moving goldwarden service file to systemd dir")
|
|
|
|
fmt.Println(err.Error())
|
2023-08-03 01:42:31 +03:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-08-04 01:44:15 +03:00
|
|
|
command2 := exec.Command("systemctl", "--now", "--user", "enable", "goldwarden.service")
|
2023-09-19 16:12:42 +03:00
|
|
|
command2.Stdout = os.Stdout
|
|
|
|
command2.Stderr = os.Stderr
|
2023-08-04 01:44:15 +03:00
|
|
|
err = command2.Run()
|
|
|
|
if err != nil {
|
2023-12-16 08:40:00 +03:00
|
|
|
fmt.Println("failed enabling systemd service")
|
2023-08-04 01:44:15 +03:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Systemd setup successfully")
|
2023-08-03 01:42:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var systemdCmd = &cobra.Command{
|
|
|
|
Use: "systemd",
|
|
|
|
Short: "Sets up systemd autostart",
|
|
|
|
Long: "Sets up systemd autostart",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
setupSystemd()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var browserbiometricsCmd = &cobra.Command{
|
|
|
|
Use: "browserbiometrics",
|
|
|
|
Short: "Sets up browser biometrics",
|
|
|
|
Long: "Sets up browser biometrics",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
err := browserbiometrics.DetectAndInstallBrowsers()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error: " + err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Println("Done.")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var setupCmd = &cobra.Command{
|
|
|
|
Use: "setup",
|
|
|
|
Short: "Sets up Goldwarden integrations",
|
|
|
|
Long: "Sets up Goldwarden integrations",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
cmd.Help()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(setupCmd)
|
|
|
|
setupCmd.AddCommand(polkitCmd)
|
|
|
|
setupCmd.AddCommand(systemdCmd)
|
|
|
|
setupCmd.AddCommand(browserbiometricsCmd)
|
|
|
|
}
|