summaryrefslogtreecommitdiff
path: root/privdrop.go
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-09-28 08:08:48 -0600
committertjpcc <tjp@ctrl-c.club>2023-10-09 08:47:37 -0600
commit6e1c25af361dde4c063eccbf769e966df4b65f23 (patch)
treed28044cf2db246555deda8db395f2f0a7e786590 /privdrop.go
parentb4f45f7c654e87bda6d5e7effb6ac5b5feb29ce0 (diff)
config file refactor
Diffstat (limited to 'privdrop.go')
-rw-r--r--privdrop.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/privdrop.go b/privdrop.go
new file mode 100644
index 0000000..c866430
--- /dev/null
+++ b/privdrop.go
@@ -0,0 +1,34 @@
+package main
+
+import (
+ "errors"
+ "fmt"
+ "os/user"
+ "strconv"
+ "syscall"
+)
+
+func privdrop(config *Configuration) error {
+ if config.SystemUser == nil {
+ return nil
+ }
+
+ current, err := user.Current()
+ if err != nil {
+ return fmt.Errorf("looking up current user: %w", err)
+ }
+ if current.Uid != "0" {
+ return errors.New("'systemuser' directive requires running as root user")
+ }
+
+ uid, err := strconv.Atoi(config.SystemUser.Uid)
+ if err != nil {
+ return errors.New("invalid 'systemuser' directive")
+ }
+
+ if err := syscall.Setuid(uid); err != nil {
+ return fmt.Errorf("setuid: %w", err)
+ }
+
+ return nil
+}