-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenv.go
94 lines (89 loc) · 1.66 KB
/
env.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
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"os"
"strconv"
"strings"
"io/ioutil"
"net/url"
)
const (
nslcdConf = "/etc/nslcd.conf"
defaultFilter = "(&(objectClass=posixAccount)(uid=%s))"
)
func getNslcdConfPath() string {
conf := os.Getenv("NSLCD_CONF")
if conf == "" {
conf = nslcdConf
}
return conf
}
func (l *ldapEnv) loadNslcdConf() error {
conf := getNslcdConfPath()
b, err := ioutil.ReadFile(conf)
if err != nil {
l.host = "localhost"
l.port = 389
l.base = "dc=example,dc=org"
l.filter = defaultFilter
l.tls = false
l.skip = false
l.debug = false
l.binddn = ""
l.bindpw = ""
}
for _, s := range strings.Split(string(b), "\n") {
v := strings.Split(s, " ")
switch {
case v[0] == "uri":
u, err := url.Parse(v[1])
if err != nil {
return err
}
if u.Scheme == "ldaps" {
l.tls = true
} else {
l.tls = false
}
if strings.Contains(u.Host, ":") {
h := strings.Split(u.Host, ":")
l.host = h[0]
p, err := strconv.Atoi(h[1])
if err != nil {
return err
}
l.port = p
} else {
l.host = u.Host
if l.tls {
l.port = 636
if isAddr(l.host) {
l.skip = true
}
} else {
l.port = 389
}
}
case v[0] == "base":
l.base = v[1]
case v[0] == "binddn":
l.binddn = v[1]
case v[0] == "bindpw":
l.bindpw = v[1]
case v[0] == "pam_authz_search":
if strings.Contains(v[1], "$username") {
l.filter = strings.Replace(v[1], "$username", "%s", 1)
} else {
l.filter = v[1]
}
case v[0] == "tls_reqcert":
if v[1] == "never" || v[1] == "allow" {
l.skip = true
}
default:
if l.filter == "" {
l.filter = defaultFilter
}
}
}
return nil
}