2023-07-17 03:23:26 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-02-04 01:58:04 +01:00
|
|
|
"fmt"
|
2024-02-21 06:42:10 +01:00
|
|
|
"os"
|
2024-02-04 01:58:04 +01:00
|
|
|
"syscall"
|
2023-07-17 03:23:26 +02:00
|
|
|
|
|
|
|
"github.com/awnumar/memguard"
|
|
|
|
"github.com/quexten/goldwarden/agent"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var daemonizeCmd = &cobra.Command{
|
|
|
|
Use: "daemonize",
|
|
|
|
Short: "Starts the agent as a daemon",
|
|
|
|
Long: `Starts the agent as a daemon. The agent will run in the background and will
|
|
|
|
run in the background until it is stopped.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-08-21 18:37:34 +02:00
|
|
|
websocketDisabled := runtimeConfig.WebsocketDisabled
|
|
|
|
sshDisabled := runtimeConfig.DisableSSHAgent
|
|
|
|
|
2023-08-21 13:52:06 +02:00
|
|
|
if websocketDisabled {
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Websocket disabled")
|
2023-08-21 13:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if sshDisabled {
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("SSH agent disabled")
|
2023-08-21 13:52:06 +02:00
|
|
|
}
|
|
|
|
|
2024-02-04 01:58:04 +01:00
|
|
|
cleanup := func() {
|
|
|
|
fmt.Println("removing sockets and exiting")
|
|
|
|
fmt.Println("unlinking", runtimeConfig.GoldwardenSocketPath)
|
|
|
|
err := syscall.Unlink(runtimeConfig.GoldwardenSocketPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println("unlinking", runtimeConfig.SSHAgentSocketPath)
|
|
|
|
err = syscall.Unlink(runtimeConfig.SSHAgentSocketPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println("memguard wiping memory and exiting")
|
2023-07-17 03:23:26 +02:00
|
|
|
memguard.SafeExit(0)
|
2024-02-04 01:58:04 +01:00
|
|
|
}
|
|
|
|
|
2024-02-21 06:42:10 +01:00
|
|
|
home, _ := os.UserHomeDir()
|
|
|
|
_, err := os.Stat("/.flatpak-info")
|
|
|
|
isFlatpak := err == nil
|
|
|
|
if runtimeConfig.GoldwardenSocketPath == "" {
|
|
|
|
if isFlatpak {
|
2024-02-23 05:57:46 +01:00
|
|
|
fmt.Println("Socket path is empty, overwriting with flatpak path.")
|
2024-02-21 06:42:10 +01:00
|
|
|
runtimeConfig.GoldwardenSocketPath = home + "/.var/app/com.quexten.Goldwarden/data/goldwarden.sock"
|
2024-02-23 05:57:46 +01:00
|
|
|
} else {
|
|
|
|
fmt.Println("Socket path is empty, overwriting with default path.")
|
|
|
|
runtimeConfig.GoldwardenSocketPath = home + "/.goldwarden.sock"
|
2024-02-21 06:42:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if runtimeConfig.SSHAgentSocketPath == "" {
|
|
|
|
if isFlatpak {
|
2024-02-23 05:57:46 +01:00
|
|
|
fmt.Println("SSH Agent socket path is empty, overwriting with flatpak path.")
|
2024-02-21 06:42:10 +01:00
|
|
|
runtimeConfig.SSHAgentSocketPath = home + "/.var/app/com.quexten.Goldwarden/data/ssh-auth-sock"
|
2024-02-23 05:57:46 +01:00
|
|
|
} else {
|
|
|
|
fmt.Println("SSH Agent socket path is empty, overwriting with default path.")
|
|
|
|
runtimeConfig.SSHAgentSocketPath = home + "/.goldwarden-ssh-agent.sock"
|
2024-02-21 06:42:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = agent.StartUnixAgent(runtimeConfig.GoldwardenSocketPath, runtimeConfig)
|
2023-07-17 03:23:26 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-02-04 01:58:04 +01:00
|
|
|
cleanup()
|
2023-07-17 03:23:26 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(daemonizeCmd)
|
|
|
|
}
|