From 43c52dd754088a115f6f0800f2eb20ecee9c9598 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 7 Feb 2022 20:36:41 +0400 Subject: [PATCH] Now program must try to run as a user set in a config file. init loggers from new config options. --- cmd/dwelling-upload/main.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/cmd/dwelling-upload/main.go b/cmd/dwelling-upload/main.go index 8659d31..be19c88 100644 --- a/cmd/dwelling-upload/main.go +++ b/cmd/dwelling-upload/main.go @@ -10,6 +10,8 @@ import ( "net/http" "os" "os/signal" + "os/user" + "strconv" "syscall" ) @@ -23,19 +25,36 @@ func main() { log.Fatalln(err) } - logErr, err := logging.NewLogger(config.LogDir + "/error.log") + // Substitute user to the one in config + dwu, err := user.Lookup(config.User) + if err != nil { + log.Fatalln(err) + } + + dwuid, err := strconv.Atoi(dwu.Uid) + if err != nil { + log.Fatalln(err) + } + + if os.Getuid() != dwuid { + if err := syscall.Setuid(dwuid); err != nil { + log.Fatalln(err) + } + } + + logErr, err := logging.NewLogger(config.Log.Error) if err != nil { log.Fatalln("failed to open error logger:", err) } defer logErr.Close() - logUpload, err := logging.NewLogger(config.LogDir + "/upload.log") + logUpload, err := logging.NewLogger(config.Log.Upload) if err != nil { log.Fatalln("failed to open upload logger:", err) } defer logUpload.Close() - logDownload, err := logging.NewLogger(config.LogDir + "/download.log") + logDownload, err := logging.NewLogger(config.Log.Download) if err != nil { log.Fatalln("failed to open download logger:", err) }