cmd/stepd: Add privelege dropping
This commit is contained in:
parent
d24cf1cdc1
commit
f8bbf7f3fc
@ -6,7 +6,11 @@ import "log"
|
|||||||
import "time"
|
import "time"
|
||||||
import "slices"
|
import "slices"
|
||||||
import "errors"
|
import "errors"
|
||||||
|
import "syscall"
|
||||||
|
import "os/user"
|
||||||
import "context"
|
import "context"
|
||||||
|
import "strings"
|
||||||
|
import "strconv"
|
||||||
import "net/http"
|
import "net/http"
|
||||||
import "unicode/utf8"
|
import "unicode/utf8"
|
||||||
import "path/filepath"
|
import "path/filepath"
|
||||||
@ -27,6 +31,10 @@ func main () {
|
|||||||
'p', "pid-file",
|
'p', "pid-file",
|
||||||
"Write the PID to the specified file",
|
"Write the PID to the specified file",
|
||||||
"", cli.ValString)
|
"", cli.ValString)
|
||||||
|
flagUser := cli.NewInputFlag (
|
||||||
|
'u', "user",
|
||||||
|
"The user:group to run as",
|
||||||
|
"", cli.ValString)
|
||||||
flagLogDirectory := cli.NewInputFlag (
|
flagLogDirectory := cli.NewInputFlag (
|
||||||
'l', "log-directory",
|
'l', "log-directory",
|
||||||
"Write logs to the specified directory",
|
"Write logs to the specified directory",
|
||||||
@ -52,6 +60,7 @@ func main () {
|
|||||||
cmd := cli.New (
|
cmd := cli.New (
|
||||||
"Run an HTTP server that automaticaly executes STEP files",
|
"Run an HTTP server that automaticaly executes STEP files",
|
||||||
flagPidFile,
|
flagPidFile,
|
||||||
|
flagUser,
|
||||||
flagLogDirectory,
|
flagLogDirectory,
|
||||||
flagHTTPAddress,
|
flagHTTPAddress,
|
||||||
flagHTTPErrorDocument,
|
flagHTTPErrorDocument,
|
||||||
@ -98,11 +107,15 @@ func main () {
|
|||||||
if err != nil { log.Fatalln("XXX", err) }
|
if err != nil { log.Fatalln("XXX", err) }
|
||||||
pidFile := daemon.PidFile(pidFileAbs)
|
pidFile := daemon.PidFile(pidFileAbs)
|
||||||
err = pidFile.Start()
|
err = pidFile.Start()
|
||||||
if err != nil { log.Println("!!! could not write pid:", err) }
|
if err != nil { log.Fatalln("XXX could not write pid:", err) }
|
||||||
defer func () {
|
}
|
||||||
err := pidFile.Close()
|
|
||||||
if err != nil { log.Println("!!! could not delete pidfile:", err) }
|
// drop privelege
|
||||||
} ()
|
if flagUser.Value != "" {
|
||||||
|
log.Println("... dropping privelege to", flagUser.Value)
|
||||||
|
user, group, _ := strings.Cut(flagUser.Value, ":")
|
||||||
|
err := dropPrivelege(user, group)
|
||||||
|
if err != nil { log.Fatalln("XXX could not drop privelege:", err) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// the single argument is for the directory to serve. we actually cd
|
// the single argument is for the directory to serve. we actually cd
|
||||||
@ -259,3 +272,23 @@ func logProviders (providers []step.Provider) {
|
|||||||
}
|
}
|
||||||
line()
|
line()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dropPrivelege (usr, group string) error {
|
||||||
|
if group != "" {
|
||||||
|
groupInfo, err := user.LookupGroup(group)
|
||||||
|
if err != nil { return err }
|
||||||
|
gid, err := strconv.Atoi(groupInfo.Gid)
|
||||||
|
if err != nil { return err }
|
||||||
|
err = syscall.Setgid(gid)
|
||||||
|
if err != nil { return err }
|
||||||
|
}
|
||||||
|
if usr != "" {
|
||||||
|
usrInfo, err := user.Lookup(usr)
|
||||||
|
if err != nil { return err }
|
||||||
|
uid, err := strconv.Atoi(usrInfo.Uid)
|
||||||
|
if err != nil { return err }
|
||||||
|
err = syscall.Setuid(uid)
|
||||||
|
if err != nil { return err }
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user