diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2015-08-26 20:25:52 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2015-08-26 20:25:52 -0600 |
commit | 13bb2e14fcdd260d060b7240357d4a8a80002114 (patch) | |
tree | 8bd49d988aa597c98578833b0b82b952b203c98b /src/nshd | |
parent | a6ac8f680062069b2821214f5b74cc96673ee4ca (diff) |
work
Diffstat (limited to 'src/nshd')
-rw-r--r-- | src/nshd/main.go | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/nshd/main.go b/src/nshd/main.go new file mode 100644 index 0000000..45b7b33 --- /dev/null +++ b/src/nshd/main.go @@ -0,0 +1,90 @@ +package main + +import ( + "os" + "fmt" + "net" + "nslcd_proto" + sd "sd_daemon" + "sd_daemon/logger" + _ "inotify" + "syscall" +) + +func get_socket() (socket net.Listener, err error) { + socket = nil + err = nil + fds := sd.ListenFds(true); + if (fds == nil) { + err = fmt.Errorf("failed to aquire sockets from systemd") + return + } + if (len(fds) != 1) { + err = fmt.Errorf("wrong number of sockets from systemd: expected %d but got %d", 1, len(fds)) + return + } + socket, err = net.FileListener(fds[0]) + fds[0].Close() + return +} + +func getpeercred(conn *net.UnixConn) (cred *syscall.Ucred, err error) { + cred = nil + file, err := conn.File() + if err != nil { + return + } + defer file.Close() + cred, err = syscall.GetsockoptUcred(int(file.Fd()), syscall.SOL_SOCKET, syscall.SO_PEERCRED) + return +} + +func handler(conn *net.UnixConn, backend nslcd_proto.Backend) { + cred, err := getpeercred(conn) + if err != nil { + logger.Debug("connection from unknown client") + } else { + logger.Debug("connection from pid=%v uid=%v gid=%v", + cred.Pid, cred.Uid, cred.Gid) + } + err = nslcd_proto.HandleRequest(backend, conn, conn, *cred) + if err != nil { + logger.Debug("Error while handling request: %v", err) + } + + +} + +func main() { + var err error = nil + var socket net.Listener = nil + defer func() { + if socket != nil { + socket.Close() + } + }() + + socket, err = get_socket() + if err != nil { + logger.Err("%s", err.Error()) + sd.Notify(false, "STOPPING=1") + os.Exit(1) + } + + sd.Notify(false, "READY=1") + + for { + conn, err := socket.Accept() + if err != nil { + logger.Notice("%s", err.Error()) + } + go handler(conn.(*net.UnixConn), nil) + } + + sd.Notify(false, "STOPPING=1") + os.Exit(0) +} + + +// Initialize() error { func +// } |