From 96ddc11f470aca96894eeb540e30f57bb587f227 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 25 Apr 2023 18:53:23 -0400 Subject: [PATCH] fix(server): add ssh commands for admin keys --- server/cmd/cmd.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/server/cmd/cmd.go b/server/cmd/cmd.go index 436f5ea8526c7ec0809898e6f28aa41f53a6b0a6..333861ca3cbd223a562c6d299acca19bc0c89325 100644 --- a/server/cmd/cmd.go +++ b/server/cmd/cmd.go @@ -141,8 +141,9 @@ func rootCommand(cfg *config.Config, s ssh.Session) *cobra.Command { ) user, _ := cfg.Backend.UserByPublicKey(s.PublicKey()) - if user != nil { - if user.IsAdmin() { + isAdmin := isPublicKeyAdmin(cfg, s.PublicKey()) || (user != nil && user.IsAdmin()) + if user != nil || isAdmin { + if isAdmin { rootCmd.AddCommand( settingsCommand(), userCommand(), @@ -180,14 +181,21 @@ func checkIfReadable(cmd *cobra.Command, args []string) error { return nil } -func checkIfAdmin(cmd *cobra.Command, _ []string) error { - cfg, s := fromContext(cmd) - ak := backend.MarshalAuthorizedKey(s.PublicKey()) +func isPublicKeyAdmin(cfg *config.Config, pk ssh.PublicKey) bool { for _, k := range cfg.InitialAdminKeys { - if k == ak { - return nil + pk2, _, err := backend.ParseAuthorizedKey(k) + if err == nil && backend.KeysEqual(pk, pk2) { + return true } } + return false +} + +func checkIfAdmin(cmd *cobra.Command, _ []string) error { + cfg, s := fromContext(cmd) + if isPublicKeyAdmin(cfg, s.PublicKey()) { + return nil + } user, _ := cfg.Backend.UserByPublicKey(s.PublicKey()) if user == nil {