setuid/gid implemented

This commit is contained in:
Özgür Kesim 2013-07-30 09:08:37 +02:00
parent 34bbc146bb
commit 87e9f8152f

View File

@ -8,12 +8,15 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"syscall"
) )
var ( var (
cfile = flag.String("cert", "cert.pem", "Certificate file in PEM format") cfile = flag.String("cert", "cert.pem", "Certificate file in PEM format")
kfile = flag.String("key", "key.pem", "Key file in PEM format") kfile = flag.String("key", "key.pem", "Key file in PEM format")
port = flag.Int("port", 1234, "Port to bind to") 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 args []string
nargs int nargs int
) )
@ -50,6 +53,25 @@ func main() {
} }
defer sock.Close() 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 // accept-loop
for { for {
conn, err := sock.Accept() conn, err := sock.Accept()