|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package authdaemon |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "os/signal" |
| 11 | + "syscall" |
| 12 | + |
| 13 | + "github.com/fosrl/cli/internal/logger" |
| 14 | + authdaemonpkg "github.com/fosrl/newt/authdaemon" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + defaultPort = 22123 |
| 20 | + defaultPrincipalsPath = "/var/run/auth-daemon/principals" |
| 21 | + defaultCACertPath = "/etc/ssh/ca.pem" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + errPresharedKeyRequired = errors.New("pre-shared-key is required") |
| 26 | + errRootRequired = errors.New("auth-daemon must be run as root (use sudo)") |
| 27 | +) |
| 28 | + |
| 29 | +func AuthDaemonCmd() *cobra.Command { |
| 30 | + opts := struct { |
| 31 | + PreSharedKey string |
| 32 | + Port int |
| 33 | + PrincipalsFile string |
| 34 | + CACertPath string |
| 35 | + GenerateRandomPassword bool |
| 36 | + }{} |
| 37 | + |
| 38 | + cmd := &cobra.Command{ |
| 39 | + Use: "auth-daemon", |
| 40 | + Short: "Start the auth daemon", |
| 41 | + Long: "Start the auth daemon for remote SSH authentication", |
| 42 | + PreRunE: func(c *cobra.Command, args []string) error { |
| 43 | + if opts.PreSharedKey == "" { |
| 44 | + return errPresharedKeyRequired |
| 45 | + } |
| 46 | + if os.Geteuid() != 0 { |
| 47 | + return errRootRequired |
| 48 | + } |
| 49 | + return nil |
| 50 | + }, |
| 51 | + Run: func(c *cobra.Command, args []string) { |
| 52 | + runAuthDaemon(opts) |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + cmd.Flags().StringVar(&opts.PreSharedKey, "pre-shared-key", "", "Preshared key required for all requests to the auth daemon (required)") |
| 57 | + cmd.MarkFlagRequired("pre-shared-key") |
| 58 | + cmd.Flags().IntVar(&opts.Port, "port", defaultPort, "TCP listen port for the HTTPS server") |
| 59 | + cmd.Flags().StringVar(&opts.PrincipalsFile, "principals-file", defaultPrincipalsPath, "Path to the principals file") |
| 60 | + cmd.Flags().StringVar(&opts.CACertPath, "ca-cert-path", defaultCACertPath, "Path to the CA certificate file") |
| 61 | + cmd.Flags().BoolVar(&opts.GenerateRandomPassword, "generate-random-password", false, "Generate a random password for authenticated users") |
| 62 | + |
| 63 | + cmd.AddCommand(PrincipalsCmd()) |
| 64 | + |
| 65 | + return cmd |
| 66 | +} |
| 67 | + |
| 68 | +// PrincipalsCmd returns the "principals" subcommand for use as AuthorizedPrincipalsCommand in sshd_config. |
| 69 | +func PrincipalsCmd() *cobra.Command { |
| 70 | + opts := struct { |
| 71 | + PrincipalsFile string |
| 72 | + Username string |
| 73 | + }{} |
| 74 | + |
| 75 | + cmd := &cobra.Command{ |
| 76 | + Use: "principals", |
| 77 | + Short: "Output principals for a username (for AuthorizedPrincipalsCommand in sshd_config)", |
| 78 | + Long: "Read the principals file and print principals that match the given username, one per line. Configure in sshd_config with AuthorizedPrincipalsCommand and %u for the username.", |
| 79 | + PreRunE: func(c *cobra.Command, args []string) error { |
| 80 | + if opts.Username == "" { |
| 81 | + return errors.New("username is required") |
| 82 | + } |
| 83 | + return nil |
| 84 | + }, |
| 85 | + Run: func(c *cobra.Command, args []string) { |
| 86 | + path := opts.PrincipalsFile |
| 87 | + if path == "" { |
| 88 | + path = defaultPrincipalsPath |
| 89 | + } |
| 90 | + runPrincipals(path, opts.Username) |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + cmd.Flags().StringVar(&opts.PrincipalsFile, "principals-file", defaultPrincipalsPath, "Path to the principals file written by the auth daemon") |
| 95 | + cmd.Flags().StringVar(&opts.Username, "username", "", "Username to look up (e.g. from sshd %u)") |
| 96 | + cmd.MarkFlagRequired("username") |
| 97 | + |
| 98 | + return cmd |
| 99 | +} |
| 100 | + |
| 101 | +func runPrincipals(principalsPath, username string) { |
| 102 | + list, err := authdaemonpkg.GetPrincipals(principalsPath, username) |
| 103 | + if err != nil { |
| 104 | + logger.Error("%v", err) |
| 105 | + os.Exit(1) |
| 106 | + } |
| 107 | + if len(list) == 0 { |
| 108 | + fmt.Println("") |
| 109 | + return |
| 110 | + } |
| 111 | + for _, principal := range list { |
| 112 | + fmt.Println(principal) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func runAuthDaemon(opts struct { |
| 117 | + PreSharedKey string |
| 118 | + Port int |
| 119 | + PrincipalsFile string |
| 120 | + CACertPath string |
| 121 | + GenerateRandomPassword bool |
| 122 | +}) { |
| 123 | + cfg := authdaemonpkg.Config{ |
| 124 | + Port: opts.Port, |
| 125 | + PresharedKey: opts.PreSharedKey, |
| 126 | + PrincipalsFilePath: opts.PrincipalsFile, |
| 127 | + CACertPath: opts.CACertPath, |
| 128 | + Force: true, |
| 129 | + GenerateRandomPassword: opts.GenerateRandomPassword, |
| 130 | + } |
| 131 | + |
| 132 | + srv, err := authdaemonpkg.NewServer(cfg) |
| 133 | + if err != nil { |
| 134 | + logger.Error("%v", err) |
| 135 | + os.Exit(1) |
| 136 | + } |
| 137 | + |
| 138 | + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 139 | + defer stop() |
| 140 | + |
| 141 | + if err := srv.Run(ctx); err != nil { |
| 142 | + logger.Error("%v", err) |
| 143 | + os.Exit(1) |
| 144 | + } |
| 145 | +} |
0 commit comments