From 9798d78300d402178896f6ee1c370baed490158a Mon Sep 17 00:00:00 2001 From: bbedward Date: Mon, 6 Apr 2026 18:19:17 -0400 Subject: [PATCH] core: allow RO commands to run as root --- core/cmd/dms/main.go | 2 +- core/cmd/dms/main_distro.go | 2 +- core/cmd/dms/utils.go | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/core/cmd/dms/main.go b/core/cmd/dms/main.go index d6fe9b8d..9de4df49 100644 --- a/core/cmd/dms/main.go +++ b/core/cmd/dms/main.go @@ -28,7 +28,7 @@ func init() { } func main() { - if os.Geteuid() == 0 { + if os.Geteuid() == 0 && !isReadOnlyCommand(os.Args) { log.Fatal("This program should not be run as root. Exiting.") } diff --git a/core/cmd/dms/main_distro.go b/core/cmd/dms/main_distro.go index 339bf40a..e6ea92d9 100644 --- a/core/cmd/dms/main_distro.go +++ b/core/cmd/dms/main_distro.go @@ -25,7 +25,7 @@ func init() { } func main() { - if os.Geteuid() == 0 { + if os.Geteuid() == 0 && !isReadOnlyCommand(os.Args) { log.Fatal("This program should not be run as root. Exiting.") } diff --git a/core/cmd/dms/utils.go b/core/cmd/dms/utils.go index d5b3b046..9459e41c 100644 --- a/core/cmd/dms/utils.go +++ b/core/cmd/dms/utils.go @@ -7,6 +7,22 @@ import ( "strings" ) +// isReadOnlyCommand returns true if the CLI args indicate a command that is +// safe to run as root (e.g. shell completion, help). +func isReadOnlyCommand(args []string) bool { + for _, arg := range args[1:] { + if strings.HasPrefix(arg, "-") { + continue + } + switch arg { + case "completion", "help", "__complete": + return true + } + return false + } + return false +} + func isArchPackageInstalled(packageName string) bool { cmd := exec.Command("pacman", "-Q", packageName) err := cmd.Run()