From 088ed806aea61e4afc60bb54c97051cebf149277 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 aa1bcb54..37320836 100644 --- a/core/cmd/dms/main.go +++ b/core/cmd/dms/main.go @@ -31,7 +31,7 @@ func init() { func main() { clipboard.MaybeServeAndExit() - 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 dcde6e8e..1cb6dd16 100644 --- a/core/cmd/dms/main_distro.go +++ b/core/cmd/dms/main_distro.go @@ -28,7 +28,7 @@ func init() { func main() { clipboard.MaybeServeAndExit() - 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()