Changeset 34 in code


Ignore:
Timestamp:
Oct 19, 2021, 4:04:07 PM (4 years ago)
Author:
dev
Message:

Move parsing and privilege drop out of main()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/partage.go

    r32 r34  
    243243}
    244244
     245func parseconfig(file string) error {
     246        cfg, err := ini.Load(file)
     247        if err != nil {
     248                return err
     249        }
     250
     251        conf.bind = cfg.Section("").Key("bind").String()
     252        conf.user = cfg.Section("").Key("user").String()
     253        conf.group = cfg.Section("").Key("group").String()
     254        conf.baseuri = cfg.Section("").Key("baseuri").String()
     255        conf.filepath = cfg.Section("").Key("filepath").String()
     256        conf.metapath = cfg.Section("").Key("metapath").String()
     257        conf.filectx = cfg.Section("").Key("filectx").String()
     258        conf.metactx = cfg.Section("").Key("metactx").String()
     259        conf.rootdir = cfg.Section("").Key("rootdir").String()
     260        conf.chroot = cfg.Section("").Key("chroot").String()
     261        conf.tmplpath = cfg.Section("").Key("tmplpath").String()
     262        conf.maxsize, _ = cfg.Section("").Key("maxsize").Int64()
     263        conf.expiry, _ = cfg.Section("").Key("expiry").Int64()
     264
     265        return nil
     266}
     267
     268func dropprivilege(username string, groupname string) error {
     269        u, err := user.Lookup(username)
     270        if err != nil {
     271                return err
     272        }
     273
     274        uid, _ := strconv.Atoi(u.Uid)
     275        gid, _ := strconv.Atoi(u.Gid)
     276
     277        if conf.group != "" {
     278                g, err := user.LookupGroup(groupname)
     279                if err != nil {
     280                        return err
     281                }
     282                gid, _ = strconv.Atoi(g.Gid)
     283        }
     284
     285        syscall.Setuid(uid)
     286        syscall.Setgid(gid)
     287
     288        return nil
     289}
     290
    245291func main() {
    246         var file string
    247         flag.StringVar(&file, "f", "", "Configuration file")
    248         flag.BoolVar(&verbose, "v", false, "Verbose logging")
    249         flag.Parse()
     292        var configfile string
    250293
    251294        /* default values */
     
    261304        conf.expiry = 86400
    262305
    263         if file != "" {
     306        flag.StringVar(&configfile, "f", "", "Configuration file")
     307        flag.BoolVar(&verbose, "v", false, "Verbose logging")
     308        flag.Parse()
     309
     310        if configfile != "" {
    264311                if verbose {
    265                         log.Printf("Reading configuration %s", file)
    266                 }
    267 
    268                 cfg, err := ini.Load(file)
    269                 if err != nil {
    270                         fmt.Println(err)
    271                         return
    272                 }
    273 
    274                 conf.bind = cfg.Section("").Key("bind").String()
    275                 conf.user = cfg.Section("").Key("user").String()
    276                 conf.group = cfg.Section("").Key("group").String()
    277                 conf.baseuri = cfg.Section("").Key("baseuri").String()
    278                 conf.filepath = cfg.Section("").Key("filepath").String()
    279                 conf.metapath = cfg.Section("").Key("metapath").String()
    280                 conf.filectx = cfg.Section("").Key("filectx").String()
    281                 conf.metactx = cfg.Section("").Key("metactx").String()
    282                 conf.rootdir = cfg.Section("").Key("rootdir").String()
    283                 conf.chroot = cfg.Section("").Key("chroot").String()
    284                 conf.tmplpath = cfg.Section("").Key("tmplpath").String()
    285                 conf.maxsize, _ = cfg.Section("").Key("maxsize").Int64()
    286                 conf.expiry, _ = cfg.Section("").Key("expiry").Int64()
     312                        log.Printf("Reading configuration %s", configfile)
     313                }
     314                parseconfig(configfile)
    287315        }
    288316
     
    295323
    296324        if conf.user != "" {
    297                 u, err := user.Lookup(conf.user)
    298                 if err != nil {
    299                         fmt.Println(err)
    300                         return
    301                 }
    302 
    303                 uid, _ := strconv.Atoi(u.Uid)
    304                 gid, _ := strconv.Atoi(u.Gid)
    305 
    306                 if conf.group != "" {
    307                         g, err := user.LookupGroup(conf.group)
    308                         if err != nil {
    309                                 fmt.Println(err)
    310                                 return
    311                         }
    312                         gid, _ = strconv.Atoi(g.Gid)
    313                 }
    314 
    315325                if verbose {
    316326                        log.Printf("Dropping privileges to %s", conf.user)
    317327                }
    318 
    319                 syscall.Setuid(uid)
    320                 syscall.Setgid(gid)
     328                dropprivilege(conf.user, conf.group)
    321329        }
    322330
Note: See TracChangeset for help on using the changeset viewer.