Provide ability to drop privileges on start
git-svn-id: file:///srv/svn/repo/marisa/trunk@28 d6811dac-2434-b64a-9ddc-f563ab233461
This commit is contained in:
29
partage.go
29
partage.go
@@ -7,9 +7,11 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"time"
|
"time"
|
||||||
"path"
|
"path"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"strconv"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"html/template"
|
"html/template"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -32,6 +34,8 @@ type metadata struct {
|
|||||||
|
|
||||||
var conf struct {
|
var conf struct {
|
||||||
bind string
|
bind string
|
||||||
|
user string
|
||||||
|
group string
|
||||||
baseuri string
|
baseuri string
|
||||||
filepath string
|
filepath string
|
||||||
metapath string
|
metapath string
|
||||||
@@ -218,6 +222,8 @@ func uploader(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.StringVar(&conf.bind, "bind", "0.0.0.0:8080", "Address to bind to (default: 0.0.0.0:8080)")
|
flag.StringVar(&conf.bind, "bind", "0.0.0.0:8080", "Address to bind to (default: 0.0.0.0:8080)")
|
||||||
|
flag.StringVar(&conf.user, "user", "", "User to drop privileges to on startup (default: current user)")
|
||||||
|
flag.StringVar(&conf.group, "group", "", "Group to drop privileges to on startup (default: user's group)")
|
||||||
flag.StringVar(&conf.baseuri, "baseuri", "http://127.0.0.1:8080", "Base URI to use for links (default: http://127.0.0.1:8080)")
|
flag.StringVar(&conf.baseuri, "baseuri", "http://127.0.0.1:8080", "Base URI to use for links (default: http://127.0.0.1:8080)")
|
||||||
flag.StringVar(&conf.filepath, "filepath", "./files", "Path to save files to (default: ./files)")
|
flag.StringVar(&conf.filepath, "filepath", "./files", "Path to save files to (default: ./files)")
|
||||||
flag.StringVar(&conf.metapath, "metapath", "./meta", "Path to save metadata to (default: ./meta)")
|
flag.StringVar(&conf.metapath, "metapath", "./meta", "Path to save metadata to (default: ./meta)")
|
||||||
@@ -235,6 +241,29 @@ func main() {
|
|||||||
syscall.Chroot(conf.chroot)
|
syscall.Chroot(conf.chroot)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if conf.user != "" {
|
||||||
|
u, err := user.Lookup(conf.user)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uid, _ := strconv.Atoi(u.Uid)
|
||||||
|
gid, _ := strconv.Atoi(u.Gid)
|
||||||
|
|
||||||
|
if conf.group != "" {
|
||||||
|
g, err := user.LookupGroup(conf.group)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
gid, _ = strconv.Atoi(g.Gid)
|
||||||
|
}
|
||||||
|
|
||||||
|
syscall.Setuid(uid)
|
||||||
|
syscall.Setgid(gid)
|
||||||
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", uploader)
|
http.HandleFunc("/", uploader)
|
||||||
http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath))))
|
http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath))))
|
||||||
http.ListenAndServe(conf.bind, nil)
|
http.ListenAndServe(conf.bind, nil)
|
||||||
|
|||||||
Reference in New Issue
Block a user