tProvide ability to listen on unix sockets - partage - File upload system
 (HTM) git clone git://git.z3bra.org/partage.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit d33c6923d41cd16ef7ffd4549c727f1807349681
 (DIR) parent 0dd0507f6726df84532eab8003408a1ff98e7ac6
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Tue, 19 Oct 2021 20:09:49 +0200
       
       Provide ability to listen on unix sockets
       
       Diffstat:
         M partage.go                          |      22 +++++++++++++++++++++-
       
       1 file changed, 21 insertions(+), 1 deletion(-)
       ---
 (DIR) diff --git a/partage.go b/partage.go
       t@@ -8,6 +8,7 @@ import (
                "io"
                "io/ioutil"
                "log"
       +        "net"
                "net/http"
                "os"
                "os/user"
       t@@ -286,7 +287,9 @@ func usergroupids(username string, groupname string) (int, int, error) {
        }
        
        func main() {
       +        var err error
                var configfile string
       +        var listener net.Listener
        
                /* default values */
                conf.bind = "0.0.0.0:8080"
       t@@ -318,6 +321,18 @@ func main() {
                        syscall.Chroot(conf.chroot)
                }
        
       +        if conf.bind[0] == '/' {
       +                listener, err = net.Listen("unix", conf.bind)
       +                if err != nil {
       +                        log.Fatal(err)
       +                }
       +        } else {
       +                listener, err = net.Listen("tcp", conf.bind)
       +                if err != nil {
       +                        log.Fatal(err)
       +                }
       +        }
       +
                if conf.user != "" {
                        if verbose {
                                log.Printf("Dropping privileges to %s", conf.user)
       t@@ -326,6 +341,11 @@ func main() {
                        if err != nil {
                                log.Fatal(err)
                        }
       +
       +                if listener.Addr().Network() == "unix" {
       +                        os.Chown(conf.bind, uid, gid)
       +                }
       +
                        syscall.Setuid(uid)
                        syscall.Setgid(gid)
                }
       t@@ -337,5 +357,5 @@ func main() {
                        log.Printf("Listening on %s", conf.bind)
                }
        
       -        http.ListenAndServe(conf.bind, nil)
       +        http.Serve(listener, nil)
        }