From 87e9f8152f15e7a0baf63891d4c0aa96da5d58fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Tue, 30 Jul 2013 09:08:37 +0200 Subject: [PATCH] setuid/gid implemented --- tlsserver.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tlsserver.go b/tlsserver.go index 3022f0e..9d3806d 100644 --- a/tlsserver.go +++ b/tlsserver.go @@ -8,12 +8,15 @@ import ( "fmt" "os" "os/exec" + "syscall" ) var ( cfile = flag.String("cert", "cert.pem", "Certificate file in PEM format") kfile = flag.String("key", "key.pem", "Key file in PEM format") port = flag.Int("port", 1234, "Port to bind to") + uid = flag.Int("uid", -1, "UID to run under") + gid = flag.Int("gid", -1, "GID to run under") args []string nargs int ) @@ -50,6 +53,25 @@ func main() { } defer sock.Close() + // set uid/gid + if *gid >= 0 { + err := syscall.Setgid(*gid) + if err != nil { + fmt.Println("Couldn't setgid to", *gid, ":", err) + os.Exit(4) + } + } + + if *uid >= 0 { + err := syscall.Setuid(*uid) + if err != nil { + fmt.Println("Couldn't setuid to", *uid, ":", err) + os.Exit(4) + } + } + + + // accept-loop for { conn, err := sock.Accept()