Changeset 28 in code


Ignore:
Timestamp:
Oct 19, 2021, 7:42:00 AM (4 years ago)
Author:
dev
Message:

Provide ability to drop privileges on start

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/partage.go

    r27 r28  
    88        "net/http"
    99        "os"
     10        "os/user"
    1011        "time"
    1112        "path"
    1213        "syscall"
     14        "strconv"
    1315        "path/filepath"
    1416        "html/template"
     
    3335var conf struct {
    3436        bind     string
     37        user     string
     38        group    string
    3539        baseuri  string
    3640        filepath string
     
    219223func main() {
    220224        flag.StringVar(&conf.bind,        "bind",        "0.0.0.0:8080", "Address to bind to (default: 0.0.0.0:8080)")
     225        flag.StringVar(&conf.user,        "user",        "", "User to drop privileges to on startup (default: current user)")
     226        flag.StringVar(&conf.group,       "group",       "", "Group to drop privileges to on startup (default: user's group)")
    221227        flag.StringVar(&conf.baseuri,     "baseuri",     "http://127.0.0.1:8080", "Base URI to use for links (default: http://127.0.0.1:8080)")
    222228        flag.StringVar(&conf.filepath,    "filepath",    "./files", "Path to save files to (default: ./files)")
     
    236242        }
    237243
     244        if conf.user != "" {
     245                u, err := user.Lookup(conf.user)
     246                if err != nil {
     247                        fmt.Println(err)
     248                        return
     249                }
     250
     251                uid, _ := strconv.Atoi(u.Uid)
     252                gid, _ := strconv.Atoi(u.Gid)
     253
     254                if conf.group != "" {
     255                        g, err := user.LookupGroup(conf.group)
     256                        if err != nil {
     257                                fmt.Println(err)
     258                                return
     259                        }
     260                        gid, _ = strconv.Atoi(g.Gid)
     261                }
     262
     263                syscall.Setuid(uid)
     264                syscall.Setgid(gid)
     265        }
     266
    238267        http.HandleFunc("/", uploader)
    239268        http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath))))
Note: See TracChangeset for help on using the changeset viewer.