Move parsing and privilege drop out of main()
git-svn-id: file:///srv/svn/repo/marisa/trunk@34 d6811dac-2434-b64a-9ddc-f563ab233461
This commit is contained in:
102
partage.go
102
partage.go
@@ -242,11 +242,54 @@ func uploader(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseconfig(file string) error {
|
||||||
|
cfg, err := ini.Load(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
conf.bind = cfg.Section("").Key("bind").String()
|
||||||
|
conf.user = cfg.Section("").Key("user").String()
|
||||||
|
conf.group = cfg.Section("").Key("group").String()
|
||||||
|
conf.baseuri = cfg.Section("").Key("baseuri").String()
|
||||||
|
conf.filepath = cfg.Section("").Key("filepath").String()
|
||||||
|
conf.metapath = cfg.Section("").Key("metapath").String()
|
||||||
|
conf.filectx = cfg.Section("").Key("filectx").String()
|
||||||
|
conf.metactx = cfg.Section("").Key("metactx").String()
|
||||||
|
conf.rootdir = cfg.Section("").Key("rootdir").String()
|
||||||
|
conf.chroot = cfg.Section("").Key("chroot").String()
|
||||||
|
conf.tmplpath = cfg.Section("").Key("tmplpath").String()
|
||||||
|
conf.maxsize, _ = cfg.Section("").Key("maxsize").Int64()
|
||||||
|
conf.expiry, _ = cfg.Section("").Key("expiry").Int64()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func dropprivilege(username string, groupname string) error {
|
||||||
|
u, err := user.Lookup(username)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
uid, _ := strconv.Atoi(u.Uid)
|
||||||
|
gid, _ := strconv.Atoi(u.Gid)
|
||||||
|
|
||||||
|
if conf.group != "" {
|
||||||
|
g, err := user.LookupGroup(groupname)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
gid, _ = strconv.Atoi(g.Gid)
|
||||||
|
}
|
||||||
|
|
||||||
|
syscall.Setuid(uid)
|
||||||
|
syscall.Setgid(gid)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var file string
|
var configfile string
|
||||||
flag.StringVar(&file, "f", "", "Configuration file")
|
|
||||||
flag.BoolVar(&verbose, "v", false, "Verbose logging")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
/* default values */
|
/* default values */
|
||||||
conf.bind = "0.0.0.0:8080"
|
conf.bind = "0.0.0.0:8080"
|
||||||
@@ -260,30 +303,15 @@ func main() {
|
|||||||
conf.maxsize = 34359738368
|
conf.maxsize = 34359738368
|
||||||
conf.expiry = 86400
|
conf.expiry = 86400
|
||||||
|
|
||||||
if file != "" {
|
flag.StringVar(&configfile, "f", "", "Configuration file")
|
||||||
|
flag.BoolVar(&verbose, "v", false, "Verbose logging")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if configfile != "" {
|
||||||
if verbose {
|
if verbose {
|
||||||
log.Printf("Reading configuration %s", file)
|
log.Printf("Reading configuration %s", configfile)
|
||||||
}
|
}
|
||||||
|
parseconfig(configfile)
|
||||||
cfg, err := ini.Load(file)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
conf.bind = cfg.Section("").Key("bind").String()
|
|
||||||
conf.user = cfg.Section("").Key("user").String()
|
|
||||||
conf.group = cfg.Section("").Key("group").String()
|
|
||||||
conf.baseuri = cfg.Section("").Key("baseuri").String()
|
|
||||||
conf.filepath = cfg.Section("").Key("filepath").String()
|
|
||||||
conf.metapath = cfg.Section("").Key("metapath").String()
|
|
||||||
conf.filectx = cfg.Section("").Key("filectx").String()
|
|
||||||
conf.metactx = cfg.Section("").Key("metactx").String()
|
|
||||||
conf.rootdir = cfg.Section("").Key("rootdir").String()
|
|
||||||
conf.chroot = cfg.Section("").Key("chroot").String()
|
|
||||||
conf.tmplpath = cfg.Section("").Key("tmplpath").String()
|
|
||||||
conf.maxsize, _ = cfg.Section("").Key("maxsize").Int64()
|
|
||||||
conf.expiry, _ = cfg.Section("").Key("expiry").Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.chroot != "" {
|
if conf.chroot != "" {
|
||||||
@@ -294,30 +322,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if conf.user != "" {
|
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
if verbose {
|
if verbose {
|
||||||
log.Printf("Dropping privileges to %s", conf.user)
|
log.Printf("Dropping privileges to %s", conf.user)
|
||||||
}
|
}
|
||||||
|
dropprivilege(conf.user, conf.group)
|
||||||
syscall.Setuid(uid)
|
|
||||||
syscall.Setgid(gid)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", uploader)
|
http.HandleFunc("/", uploader)
|
||||||
|
|||||||
Reference in New Issue
Block a user