// _MSet sets multiple values named by the keys in the given filename. func _MSet(filename string, keys, values []string) error { if len(keys) != len(values) { return fmt.Errorf("number of keys is different to number of values") } // Check if the key is already used. conf, err := shconf.ParseFile(filename) if err != nil { if err != os.ErrNotExist { return err } println("ErrNotExist") //TODO: remove } var buf bytes.Buffer for i, key := range keys { if _, err = conf.Get(key); err != nil { continue // TODO: log key already set. } buf.WriteString(key) buf.Write(conf.Separator()) buf.WriteString(values[i]) buf.WriteByte('\n') } return file.Append(filename, buf.Bytes()) }
// Detect returns the Linux distribution. func Detect() (Distro, error) { var id string var err error if _, err = os.Stat("/etc/os-release"); !os.IsNotExist(err) { cfg, err := shconf.ParseFile("/etc/os-release") if err != nil { return 0, err } if id, err = cfg.Get("ID"); err != nil { return 0, err } if v, found := idToDistro[id]; found { return v, nil } } else if _, err = os.Stat("/etc/centos-release"); !os.IsNotExist(err) { return CentOS, nil } else if _, err = os.Stat("/etc/pclinuxos-release"); !os.IsNotExist(err) { return PCLinuxOS, nil } panic("Linux distribution unsopported") }
// _Set sets the value named by the key in the given filename. func _Set(filename, key, value string) error { // Check if the key is already used. conf, err := shconf.ParseFile(filename) if err != nil { if err != os.ErrNotExist { return err } println("ErrNotExist") //TODO: remove } else { if _, err = conf.Get(key); err != shconf.ErrKey { panic("OPS") } } return file.AppendString(filename, key+string(conf.Separator())+value) }
// init sets the configuration data. func (c *configData) init() error { _conf_login := &conf_login{} cmdutil.SetPrefix("\n* ", "") cfg, err := shconf.ParseFile(_CONF_LOGIN) if err != nil { return err } else { if err = cfg.Unmarshal(_conf_login); err != nil { return err } if _DEBUG { cmdutil.Println(_CONF_LOGIN) reflectutil.PrintStruct(_conf_login) } if _conf_login.PASS_MAX_DAYS == 0 { _conf_login.PASS_MAX_DAYS = 99999 } if _conf_login.PASS_WARN_AGE == 0 { _conf_login.PASS_WARN_AGE = 7 } } cfg, err = shconf.ParseFile(_CONF_USERADD) if err != nil { return err } else { _conf_useradd := &conf_useradd{} if err = cfg.Unmarshal(_conf_useradd); err != nil { return err } if _DEBUG { cmdutil.Println(_CONF_USERADD) reflectutil.PrintStruct(_conf_useradd) } if _conf_useradd.HOME == "" { _conf_useradd.HOME = "/home" } if _conf_useradd.SHELL == "" { _conf_useradd.SHELL = "/bin/sh" } config.useradd = *_conf_useradd } // Optional files found, err := exist(_CONF_ADDUSER) // Based in Debian. if found { cfg, err := shconf.ParseFile(_CONF_ADDUSER) if err != nil { return err } _conf_adduser := &conf_adduser{} if err = cfg.Unmarshal(_conf_adduser); err != nil { return err } if _DEBUG { cmdutil.Println(_CONF_ADDUSER) reflectutil.PrintStruct(_conf_adduser) } if _conf_login.SYS_UID_MIN == 0 || _conf_login.SYS_UID_MAX == 0 || _conf_login.SYS_GID_MIN == 0 || _conf_login.SYS_GID_MAX == 0 || _conf_login.UID_MIN == 0 || _conf_login.UID_MAX == 0 || _conf_login.GID_MIN == 0 || _conf_login.GID_MAX == 0 { _conf_login.SYS_UID_MIN = _conf_adduser.FIRST_SYSTEM_UID _conf_login.SYS_UID_MAX = _conf_adduser.LAST_SYSTEM_UID _conf_login.SYS_GID_MIN = _conf_adduser.FIRST_SYSTEM_GID _conf_login.SYS_GID_MAX = _conf_adduser.LAST_SYSTEM_GID _conf_login.UID_MIN = _conf_adduser.FIRST_UID _conf_login.UID_MAX = _conf_adduser.LAST_UID _conf_login.GID_MIN = _conf_adduser.FIRST_GID _conf_login.GID_MAX = _conf_adduser.LAST_GID } } else if err != nil { return err } else if found, err = exist(_CONF_LIBUSER); found { // Based in Red Hat. cfg, err := shconf.ParseFile(_CONF_LIBUSER) if err != nil { return err } _conf_libuser := &conf_libuser{} if err = cfg.Unmarshal(_conf_libuser); err != nil { return err } if _DEBUG { cmdutil.Println(_CONF_LIBUSER) reflectutil.PrintStruct(_conf_libuser) } if _conf_libuser.login_defs != _CONF_LOGIN { _conf_login.ENCRYPT_METHOD = _conf_libuser.crypt_style _conf_login.SHA_CRYPT_MIN_ROUNDS = _conf_libuser.hash_rounds_min _conf_login.SHA_CRYPT_MAX_ROUNDS = _conf_libuser.hash_rounds_max } } else if err != nil { return err } /*else if found, err = exist(_CONF_PASSWD); found { cfg, err := shconf.ParseFile(_CONF_PASSWD) if err != nil { return err } _conf_passwd := &conf_passwd{} if err = cfg.Unmarshal(_conf_passwd); err != nil { return err } if _DEBUG { cmdutil.Println(_CONF_PASSWD) reflectutil.PrintStruct(_conf_passwd) } if _conf_passwd.CRYPT != "" { _conf_login.ENCRYPT_METHOD = _conf_passwd.CRYPT } } else if err != nil { return err }*/ switch strings.ToUpper(_conf_login.ENCRYPT_METHOD) { case "MD5": c.crypter = crypt.New(crypt.MD5) case "SHA256": c.crypter = crypt.New(crypt.SHA256) case "SHA512": c.crypter = crypt.New(crypt.SHA512) case "": if c.crypter, err = lookupCrypter(); err != nil { return err } default: return fmt.Errorf("user: requested cryp function is unavailable: %s", c.login.ENCRYPT_METHOD) } if _conf_login.SYS_UID_MIN == 0 || _conf_login.SYS_UID_MAX == 0 || _conf_login.SYS_GID_MIN == 0 || _conf_login.SYS_GID_MAX == 0 || _conf_login.UID_MIN == 0 || _conf_login.UID_MAX == 0 || _conf_login.GID_MIN == 0 || _conf_login.GID_MAX == 0 { _conf_login.SYS_UID_MIN = 100 _conf_login.SYS_UID_MAX = 999 _conf_login.SYS_GID_MIN = 100 _conf_login.SYS_GID_MAX = 999 _conf_login.UID_MIN = 1000 _conf_login.UID_MAX = 29999 _conf_login.GID_MIN = 1000 _conf_login.GID_MAX = 29999 } config.login = *_conf_login return nil }