aboutsummaryrefslogtreecommitdiff
path: root/ldapserver/route.go
blob: ec09f7df11c1a1f353cb8c65c7b3051984d712b9 (plain) (blame)
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
248
249
250
251
252
253
254
255
256
package ldapserver

import (
	"strings"

	ldap "github.com/vjeantet/goldap/message"
)

// Constant to LDAP Request protocol Type names
const (
	SEARCH   = "SearchRequest"
	BIND     = "BindRequest"
	COMPARE  = "CompareRequest"
	ADD      = "AddRequest"
	MODIFY   = "ModifyRequest"
	DELETE   = "DelRequest"
	EXTENDED = "ExtendedRequest"
	ABANDON  = "AbandonRequest"
)

// HandlerFunc type is an adapter to allow the use of
// ordinary functions as LDAP handlers.  If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler object that calls f.
type HandlerFunc func(UserState, ResponseWriter, *Message)

// RouteMux manages all routes
type RouteMux struct {
	routes        []*route
	notFoundRoute *route
}

type route struct {
	label       string
	operation   string
	handler     HandlerFunc
	exoName     string
	sBasedn     string
	uBasedn     bool
	sFilter     string
	uFilter     bool
	sScope      int
	uScope      bool
	sAuthChoice string
	uAuthChoice bool
}

// Match return true when the *Message matches the route
// conditions
func (r *route) Match(m *Message) bool {
	if m.ProtocolOpName() != r.operation {
		return false
	}

	switch v := m.ProtocolOp().(type) {
	case ldap.BindRequest:
		if r.uAuthChoice == true {
			if strings.ToLower(v.AuthenticationChoice()) != r.sAuthChoice {
				return false
			}
		}
		return true

	case ldap.ExtendedRequest:
		if string(v.RequestName()) != r.exoName {
			return false
		}
		return true

	case ldap.SearchRequest:
		if r.uBasedn == true {
			if strings.ToLower(string(v.BaseObject())) != r.sBasedn {
				return false
			}
		}

		if r.uFilter == true {
			if strings.ToLower(v.FilterString()) != r.sFilter {
				return false
			}
		}

		if r.uScope == true {
			if int(v.Scope()) != r.sScope {
				return false
			}
		}
		return true
	}
	return true
}

func (r *route) Label(label string) *route {
	r.label = label
	return r
}

func (r *route) BaseDn(dn string) *route {
	r.sBasedn = strings.ToLower(dn)
	r.uBasedn = true
	return r
}

func (r *route) AuthenticationChoice(choice string) *route {
	r.sAuthChoice = strings.ToLower(choice)
	r.uAuthChoice = true
	return r
}

func (r *route) Filter(pattern string) *route {
	r.sFilter = strings.ToLower(pattern)
	r.uFilter = true
	return r
}

func (r *route) Scope(scope int) *route {
	r.sScope = scope
	r.uScope = true
	return r
}

func (r *route) RequestName(name ldap.LDAPOID) *route {
	r.exoName = string(name)
	return r
}

// NewRouteMux returns a new *RouteMux
// RouteMux implements ldapserver.Handler
func NewRouteMux() *RouteMux {
	return &RouteMux{}
}

// Handler interface used to serve a LDAP Request message
type Handler interface {
	ServeLDAP(s UserState, w ResponseWriter, r *Message)
}

// ServeLDAP dispatches the request to the handler whose
// pattern most closely matches the request request Message.
func (h *RouteMux) ServeLDAP(s UserState, w ResponseWriter, r *Message) {

	//find a matching Route
	for _, route := range h.routes {

		//if the route don't match, skip it
		if route.Match(r) == false {
			continue
		}

		if route.label != "" {
			if DEBUG {
				Logger.Printf("")
				Logger.Printf(" ROUTE MATCH ; %s", route.label)
				Logger.Printf("")
			}
		}

		route.handler(s, w, r)
		return
	}

	// Catch a AbandonRequest not handled by user
	switch v := r.ProtocolOp().(type) {
	case ldap.AbandonRequest:
		// retreive the request to abandon, and send a abort signal to it
		if requestToAbandon, ok := r.Client.GetMessageByID(int(v)); ok {
			requestToAbandon.Abandon()
		}
	}

	if h.notFoundRoute != nil {
		h.notFoundRoute.handler(s, w, r)
	} else {
		res := NewResponse(LDAPResultUnwillingToPerform)
		res.SetDiagnosticMessage("Operation not implemented by server")
		w.Write(res)
	}
}

// Adds a new Route to the Handler
func (h *RouteMux) addRoute(r *route) {
	//and finally append to the list of Routes
	//create the Route
	h.routes = append(h.routes, r)
}

func (h *RouteMux) NotFound(handler HandlerFunc) *route {
	route := &route{}
	route.handler = handler
	h.notFoundRoute = route
	return route
}

func (h *RouteMux) Bind(handler HandlerFunc) *route {
	route := &route{}
	route.operation = BIND
	route.handler = handler
	h.addRoute(route)
	return route
}

func (h *RouteMux) Search(handler HandlerFunc) *route {
	route := &route{}
	route.operation = SEARCH
	route.handler = handler
	h.addRoute(route)
	return route
}

func (h *RouteMux) Add(handler HandlerFunc) *route {
	route := &route{}
	route.operation = ADD
	route.handler = handler
	h.addRoute(route)
	return route
}

func (h *RouteMux) Delete(handler HandlerFunc) *route {
	route := &route{}
	route.operation = DELETE
	route.handler = handler
	h.addRoute(route)
	return route
}

func (h *RouteMux) Modify(handler HandlerFunc) *route {
	route := &route{}
	route.operation = MODIFY
	route.handler = handler
	h.addRoute(route)
	return route
}

func (h *RouteMux) Compare(handler HandlerFunc) *route {
	route := &route{}
	route.operation = COMPARE
	route.handler = handler
	h.addRoute(route)
	return route
}

func (h *RouteMux) Extended(handler HandlerFunc) *route {
	route := &route{}
	route.operation = EXTENDED
	route.handler = handler
	h.addRoute(route)
	return route
}

func (h *RouteMux) Abandon(handler HandlerFunc) *route {
	route := &route{}
	route.operation = ABANDON
	route.handler = handler
	h.addRoute(route)
	return route
}