-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
83 lines (74 loc) · 1.61 KB
/
config.go
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
package main
import (
"os"
"os/user"
"strconv"
"gopkg.in/yaml.v2"
)
type Config struct {
TrustedUser string `yaml:"trusted_user,omitempty"`
TrustedUserId uint32
AllowedGroup string `yaml:"allowed_group,omitempty"`
AllowedGroupId uint32
SocketPath string `yaml:"socket_path,omitempty"`
}
func NewDefaultConfig() *Config {
cfg := &Config{
TrustedUser: "0",
AllowedGroup: "0",
SocketPath: "/var/run/go-keyholder.sock",
}
return cfg
}
func (c *Config) ReadFile(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
dec := yaml.NewDecoder(f)
if err = dec.Decode(c); err != nil {
return err
}
if c.TrustedUser != "" {
if uid, err := strconv.ParseUint(c.TrustedUser, 10, 32); err == nil {
user, err := user.LookupId(c.TrustedUser)
if err != nil {
return err
}
c.TrustedUser = user.Name
c.TrustedUserId = uint32(uid)
} else {
user, err := user.Lookup(c.TrustedUser)
if err != nil {
return err
}
uid, err := strconv.ParseUint(user.Uid, 10, 32)
if err != nil {
return err
}
c.TrustedUserId = uint32(uid)
}
}
if c.AllowedGroup != "" {
if gid, err := strconv.ParseUint(c.AllowedGroup, 10, 32); err == nil {
group, err := user.LookupGroupId(c.AllowedGroup)
if err != nil {
return err
}
c.AllowedGroup = group.Name
c.AllowedGroupId = uint32(gid)
} else {
group, err := user.LookupGroup(c.AllowedGroup)
if err != nil {
return err
}
gid, err := strconv.ParseUint(group.Gid, 10, 32)
if err != nil {
return err
}
c.AllowedGroupId = uint32(gid)
}
}
return nil
}