diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2015-08-29 23:20:53 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2015-08-29 23:20:53 -0600 |
commit | 2206136023f3e548c3f02e393a85c6e911b61ef3 (patch) | |
tree | 0f6e11c109ac8093be7e5c77faafe2e50c7d2c20 /src/nshd/hackers_git/hackers_parse.go | |
parent | 3bca4faef65f9ba97abad49505e10c675894c559 (diff) |
roll sleeves up, do actual yaml the hard way
Diffstat (limited to 'src/nshd/hackers_git/hackers_parse.go')
-rw-r--r-- | src/nshd/hackers_git/hackers_parse.go | 89 |
1 files changed, 69 insertions, 20 deletions
diff --git a/src/nshd/hackers_git/hackers_parse.go b/src/nshd/hackers_git/hackers_parse.go index 5152b55..8f93432 100644 --- a/src/nshd/hackers_git/hackers_parse.go +++ b/src/nshd/hackers_git/hackers_parse.go @@ -4,25 +4,20 @@ import ( "fmt" yaml "gopkg.in/yaml.v2" "io/ioutil" - "nslcd_proto" "os" ) -type yaml_user struct { - username string - fullname string - shell string - groups []string -} - func filename2uid(filename string) int32 { // TODO return 0 } -func load_user_yaml(filename string) (ret nslcd_proto.Passwd, err error) { - ret.UID = filename2uid(filename) - if ret.UID < 0 { +var usersGid = name2gid("users") + +func load_user_yaml(filename string) (ret user, err error) { + ret.passwd.UID = filename2uid(filename) + + if ret.passwd.UID < 0 { err = fmt.Errorf("Invalid user filename: %q", filename) return } @@ -34,15 +29,69 @@ func load_user_yaml(filename string) (ret nslcd_proto.Passwd, err error) { if err != nil { return } - var user yaml_user - err = yaml.Unmarshal(contents, &user) - - ret.Name = user.username - ret.Password = "x" - ret.GID = name2gid("users") - ret.GECOS = user.fullname - ret.HomeDir = "/home/" + ret.Name - ret.Shell = user.shell + var _data interface{} + err = yaml.Unmarshal(contents, &_data) + if err != nil { + return + } + data, isMap := _data.(map[interface{}]interface{}) + errs := []string{} + if !isMap { + errs = append(errs, "root node is not a map") + } else { + if iface, isSet := data["username"]; !isSet { + errs = append(errs, "\"username\" is not set") + } else if str, isTyp := iface.(string); !isTyp { + errs = append(errs, "\"username\" is not a string") + } else { + ret.passwd.Name = str + ret.passwd.HomeDir = "/home/" + str + } + + if iface, isSet := data["fullname"]; !isSet { + errs = append(errs, "\"fullname\" is not set") + } else if str, isTyp := iface.(string); !isTyp { + errs = append(errs, "\"fullname\" is not a string") + } else { + ret.passwd.GECOS = str + } + + if iface, isSet := data["shell"]; !isSet { + errs = append(errs, "\"shell\" is not set") + } else if str, isTyp := iface.(string); !isTyp { + errs = append(errs, "\"shell\" is not a string") + } else { + ret.passwd.Shell = str + } + + if iface, isSet := data["groups"]; !isSet { + errs = append(errs, "\"groups\" is not set") + } else if ary, isTyp := iface.([]interface{}); !isTyp { + errs = append(errs, "\"groups\" is not an array") + } else { + groups := make([]string, len(ary)) + e := false + for i, iface := range ary { + if str, isTyp := iface.(string); !isTyp { + errs = append(errs, "\"group\" item is not an array") + e = true + break + } else { + groups[i] = str + } + } + if !e { + ret.groups = groups + } + } + } + if len(errs) > 0 { + err = &yaml.TypeError{errs} + } + + ret.passwd.Password = "x" + ret.passwd.GID = usersGid + ret.pwhash = "!" return } |