-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
197 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package utils | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/user" | ||
"strconv" | ||
"syscall" | ||
) | ||
|
||
func IsRoot() bool { | ||
return os.Geteuid() == 0 | ||
} | ||
|
||
func DropPrivileges(newUid, newGid string) error { | ||
if newUid == "" { | ||
return errors.New("user option is empty, cant drop privileges") | ||
} | ||
|
||
if newGid == "" { | ||
return errors.New("group option is unset, cant drop privileges") | ||
} | ||
|
||
gid, err := strconv.Atoi(newGid) | ||
if err != nil { | ||
g, err := user.LookupGroup(newGid) | ||
if err != nil { | ||
return err | ||
} | ||
gid, err = strconv.Atoi(g.Gid) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err = syscall.Setgid(gid); err != nil { | ||
return err | ||
} | ||
|
||
uid, err := strconv.Atoi(newUid) | ||
if err != nil { | ||
u, err := user.Lookup(newUid) | ||
if err != nil { | ||
return err | ||
} | ||
uid, err = strconv.Atoi(u.Uid) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if err = syscall.Setuid(uid); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package utils | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
func IsRoot() bool { | ||
return false | ||
} | ||
|
||
func DropPrivileges(newUid, newGid string) error { | ||
return errors.New("cant drop privileges in windows") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package utils | ||
|
||
import ( | ||
"os/user" | ||
"strings" | ||
) | ||
|
||
// ExpandTilde replaces ~ with the homedir of the current user | ||
func ExpandTilde(path string) (string, error) { | ||
u, err := user.Current() | ||
if err != nil { | ||
return "", err | ||
} | ||
return strings.Replace(path, "~", u.HomeDir, 1), nil | ||
} |