forked from xoe-labs/coredns-ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
247 lines (204 loc) · 5.45 KB
/
setup.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package ldap
import (
"fmt"
"strconv"
"sync"
"time"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/prometheus/client_golang/prometheus"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/upstream"
)
const pluginName = "ldap"
// Define log to be a logger with the plugin name in it.
// nolint: gochecknoglobals
var log = clog.NewWithPlugin(pluginName)
// init registers this plugin.
// nolint: gochecknoinits
func init() { plugin.Register(pluginName, setup) }
// setup is the function that gets called when the config parser see the token "ldap". Setup is responsible
// for parsing any extra options the ldap plugin may have. The first token this function sees is "ldap".
func setup(c *caddy.Controller) error {
// parse corefile config
l, err := ldapParse(c)
if err != nil {
return plugin.Error(pluginName, err)
}
err = l.InitClient()
if err != nil {
return plugin.Error(pluginName, err)
}
// add prometheus metrics on startup
c.OnStartup(func() error {
// add plugin-global metric once
once.Do(func() {
prometheus.MustRegister(requestCount)
})
return nil
})
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
l.Next = next
return l
})
return nil
}
// nolint: gochecknoglobals
var once sync.Once
func ldapParse(c *caddy.Controller) (*Ldap, error) {
var (
ldap *Ldap
err error
)
i := 0
for c.Next() {
if i > 0 {
return nil, plugin.ErrOnce
}
i++
ldap, err = ParseStanza(c)
if err != nil {
return ldap, err
}
}
return ldap, nil
}
// ParseStanza parses a ldap stanza.
// nolint: funlen, gocognit, gocyclo
func ParseStanza(c *caddy.Controller) (*Ldap, error) {
zoneNames := c.RemainingArgs()
if len(zoneNames) != 0 {
for i := 0; i < len(zoneNames); i++ {
zoneNames[i] = plugin.Host(zoneNames[i]).Normalize()
}
} else {
zoneNames = make([]string, len(c.ServerBlockKeys))
for i := 0; i < len(zoneNames); i++ {
zoneNames[i] = plugin.Host(c.ServerBlockKeys[i]).Normalize()
}
}
ldap := New(zoneNames)
ldap.Upstream = upstream.New()
for c.NextBlock() {
switch c.Val() {
// RFC 4516 URL
case "ldap_url":
if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.ldapURL = c.Val()
case "paging_limit":
if !c.NextArg() {
return nil, c.ArgErr()
}
pagingLimit, err := strconv.ParseUint(c.Val(), 10, 0)
if err != nil {
return nil, c.Errf("paging_limit: %w", err)
}
ldap.pagingLimit = uint32(pagingLimit)
case "base_dn":
if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.SearchRequest.BaseDN = c.Val() // ou=ae-dir
case "filter":
if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.SearchRequest.Filter = c.Val() // (objectClass=aeNwDevice)
case "attributes":
c.Next()
for c.NextBlock() {
switch c.Val() {
case "fqdn":
if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.SearchRequest.Attributes = append(ldap.SearchRequest.Attributes, c.Val())
ldap.FqdnAttr = c.Val() // aeFqdn
case "ip4":
if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.SearchRequest.Attributes = append(ldap.SearchRequest.Attributes, c.Val())
ldap.Ip4Attr = c.Val() // ipHostNumber
case "ip6":
if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.SearchRequest.Attributes = append(ldap.SearchRequest.Attributes, c.Val())
ldap.Ip6Attr = c.Val() // ipHostNumber
default:
return nil, c.Errf("unknown attributes property '%s'", c.Val())
}
}
continue
case "username":
if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.username = c.Val()
case "password":
if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.password = c.Val()
case "sasl":
ldap.sasl = true
case "ttl":
if !c.NextArg() {
return nil, c.ArgErr()
}
ttl, err := time.ParseDuration(c.Val())
if err != nil {
return nil, c.Errf("ttl: %w", err)
}
ldap.ttl = ttl
case "sync_interval":
if !c.NextArg() {
return nil, c.ArgErr()
}
syncInterval, err := time.ParseDuration(c.Val())
if err != nil {
return nil, c.Errf("sync_interval: %w", err)
}
ldap.syncInterval = syncInterval
case "fallthrough":
ldap.Fall.SetZonesFromArgs(c.RemainingArgs())
default:
return nil, c.Errf("unknown property '%s'", c.Val())
}
}
// validate non-default ldap values ...
if ldap.ldapURL == "" {
return nil, c.Err("ldap_url cannot be empty")
}
if ldap.SearchRequest.BaseDN == "" {
return nil, c.Err("base_dn cannot be empty")
}
if ldap.SearchRequest.Filter == "" {
return nil, c.Err("filter cannot be empty")
}
if ldap.FqdnAttr == "" {
return nil, c.Err("fqdn attribute cannot be empty")
}
if ldap.Ip4Attr == "" {
return nil, c.Err("ip4 attribute cannot be empty")
}
// if only one of password and username set
if (ldap.username == "") != (ldap.password == "") {
return nil, c.Err("if not using sasl, both, username and password must be set")
}
// if both username/password and sasl are set
if ldap.username != "" && ldap.sasl {
fmt.Printf("666 %#v\t%#v", ldap.username, ldap.sasl)
return nil, c.Err("cannot use sasl and username based authentication at the same time")
}
// if neither username/password nor sasl are set
if ldap.username == "" && !ldap.sasl {
return nil, c.Err("authenticate either via username/password or sasl")
}
return ldap, nil
}