1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package hackers_git
import (
"fmt"
yaml "gopkg.in/yaml.v2"
"io/ioutil"
"os"
)
func filename2uid(filename string) int32 {
// TODO
return 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
}
file, err := os.Open(filename)
if err != nil {
return
}
contents, err := ioutil.ReadAll(file)
if err != nil {
return
}
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
}
func load_user_password(filename string) string {
// TODO
return "!"
}
|