Skip to content

Commit 71f6511

Browse files
authored
don't prepend sudo to command if user is superuser (#110)
1 parent b3ecf95 commit 71f6511

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

internal/script/script.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func RunScripts(myTarget target.Target, scripts []ScriptDefinition, ignoreScript
107107
continue
108108
}
109109
if script.Superuser && !canElevate {
110-
slog.Info("skipping script because it requires superuser privileges and the target cannot elevate privileges", slog.String("script", script.Name))
110+
slog.Info("skipping script because it requires superuser privileges and the user cannot elevate privileges on target", slog.String("script", script.Name))
111111
continue
112112
}
113113
if script.Sequential {
@@ -163,8 +163,12 @@ func RunScripts(myTarget target.Target, scripts []ScriptDefinition, ignoreScript
163163
// instigates a known bug in the terminal that corrupts the tty settings:
164164
// https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1043320
165165
var cmd *exec.Cmd
166-
if needsElevatedPrivileges {
167-
// run master script with sudo, "-S" to read password from stdin
166+
if needsElevatedPrivileges && !canElevate {
167+
// this shouldn't happen because we already filtered out the scripts that require elevated privileges if the user cannot elevate privileges on the target
168+
err = fmt.Errorf("master script requires elevated privileges but the user cannot elevate privileges on target")
169+
return nil, err
170+
} else if needsElevatedPrivileges && !myTarget.IsSuperUser() {
171+
// run master script with sudo, "-S" to read password from stdin. Note: password won't be asked for if password-less sudo is configured.
168172
cmd = exec.Command("sudo", "-S", "bash", path.Join(myTarget.GetTempDirectory(), masterScriptName))
169173
} else {
170174
cmd = exec.Command("bash", path.Join(myTarget.GetTempDirectory(), masterScriptName))

internal/target/target.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ type Target interface {
3535
// It returns true if the user can elevate privileges, false otherwise.
3636
CanElevatePrivileges() bool
3737

38+
// IsSuperUser checks if the current user is a superuser.
39+
// It returns true if the user is a superuser, false otherwise.
40+
IsSuperUser() bool
41+
3842
// GetArchitecture returns the architecture of the target system.
3943
// It returns a string representing the architecture and any error that occurred.
4044
GetArchitecture() (arch string, err error)
@@ -370,7 +374,7 @@ func (t *LocalTarget) CanElevatePrivileges() bool {
370374
if t.canElevate != 0 {
371375
return t.canElevate == 1
372376
}
373-
if os.Geteuid() == 0 {
377+
if t.IsSuperUser() {
374378
t.canElevate = 1
375379
return true // user is root
376380
}
@@ -406,7 +410,7 @@ func (t *RemoteTarget) CanElevatePrivileges() bool {
406410
if t.canElevate != 0 {
407411
return t.canElevate == 1
408412
}
409-
if t.user == "root" {
413+
if t.IsSuperUser() {
410414
t.canElevate = 1
411415
return true
412416
}
@@ -420,6 +424,16 @@ func (t *RemoteTarget) CanElevatePrivileges() bool {
420424
return false
421425
}
422426

427+
// IsSuperUser checks if the current user is a superuser.
428+
// It returns true if the user is a superuser, false otherwise.
429+
func (t *LocalTarget) IsSuperUser() bool {
430+
return os.Geteuid() == 0
431+
}
432+
433+
func (t *RemoteTarget) IsSuperUser() bool {
434+
return t.user == "root"
435+
}
436+
423437
// InstallLkms installs the specified LKMs (Loadable Kernel Modules) on the target.
424438
// It returns the list of installed LKMs and any error encountered during the installation process.
425439
func (t *LocalTarget) InstallLkms(lkms []string) (installedLkms []string, err error) {

0 commit comments

Comments
 (0)