aboutsummaryrefslogtreecommitdiff
path: root/write.go
blob: e4c7de112f820cecd53ad6173512b493413538fe (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package main

import (
	"encoding/json"
	"fmt"
	"strings"

	ldap "bottin/ldapserver"

	consul "github.com/hashicorp/consul/api"
	message "github.com/lor00x/goldap/message"
)

// Generic item modification function --------

func (server *Server) putAttributes(dn string, attrs Entry) error {
	prefix, err := dnToConsul(dn)
	if err != nil {
		return err
	}

	for k, valuesNC := range attrs {
		path := prefix + "/attribute=" + k

		// Trim spaces and remove empty values
		values := []string{}
		for _, v := range valuesNC {
			vv := strings.TrimSpace(v)
			if len(vv) > 0 {
				values = append(values, vv)
			}
		}

		// If we have zero values, delete associated k/v pair
		// Otherwise, write new values
		if len(values) == 0 {
			_, err := server.kv.Delete(path, nil)
			if err != nil {
				return err
			}
		} else {
			json, err := json.MarshalIndent(values, "", "  ")
			if err != nil {
				return err
			}
			pair := &consul.KVPair{Key: path, Value: json}
			_, err = server.kv.Put(pair, nil)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

// Add request ------------------------

func (server *Server) handleAdd(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {
	state := s.(*State)
	r := m.GetAddRequest()

	code, err := server.handleAddInternal(state, &r)

	res := ldap.NewResponse(code)
	if err != nil {
		res.SetDiagnosticMessage(err.Error())
	}
	if code == ldap.LDAPResultSuccess {
		server.logger.Printf("Successfully added %s", string(r.Entry()))
	} else {
		server.logger.Printf("Failed to add %s (%s)", string(r.Entry()), err)
	}
	w.Write(message.AddResponse(res))
}

func (server *Server) handleAddInternal(state *State, r *message.AddRequest) (int, error) {
	dn, err := server.checkDN(string(r.Entry()), false)
	if err != nil {
		return ldap.LDAPResultInvalidDNSyntax, err
	}

	dnSplit, err := parseDN(dn)
	if err != nil {
		return ldap.LDAPResultInvalidDNSyntax, err
	}

	// Check permissions
	attrListStr := []string{}
	for _, attribute := range r.Attributes() {
		attrListStr = append(attrListStr, string(attribute.Type_()))
	}
	if !server.config.Acl.Check(&state.login, "add", dn, attrListStr) {
		return ldap.LDAPResultInsufficientAccessRights, nil
	}

	// Check that object does not already exist
	exists, err := server.objectExists(dn)
	if err != nil {
		return ldap.LDAPResultOperationsError, err
	}
	if exists {
		return ldap.LDAPResultEntryAlreadyExists, nil
	}

	// Check that parent object exists
	parentDn := unparseDN(dnSplit[1:])
	parentExists, err := server.objectExists(parentDn)
	if err != nil {
		return ldap.LDAPResultOperationsError, err
	}
	if !parentExists {
		return ldap.LDAPResultNoSuchObject, fmt.Errorf(
			"Parent object %s does not exist", parentDn)
	}

	// If adding a group, track of who the members will be so that their memberOf field can be updated later
	members := []string{}

	// Check attributes
	entry := Entry{}
	for _, attribute := range r.Attributes() {
		key := string(attribute.Type_())
		vals_str := []string{}
		for _, val := range attribute.Vals() {
			vals_str = append(vals_str, string(val))
		}

		// Fail if they are trying to write memberOf, we manage this ourselves
		err = checkRestrictedAttr(key)
		if err != nil {
			return ldap.LDAPResultObjectClassViolation, err
		}
		if strings.EqualFold(key, ATTR_MEMBER) {
			// If they are writing a member list, we have to check they are adding valid members
			// Also, rewrite member list to use canonical DN syntax (no spaces, all lowercase)
			for _, member := range vals_str {
				member_canonical, err := server.checkDN(member, false)
				if err != nil {
					return ldap.LDAPResultInvalidDNSyntax, err
				}
				exists, err = server.objectExists(member_canonical)
				if err != nil {
					return ldap.LDAPResultOperationsError, err
				}
				if !exists {
					return ldap.LDAPResultNoSuchObject, fmt.Errorf(
						"Cannot add %s to members, it does not exist!",
						member_canonical)
				}
				members = append(members, member_canonical)
			}
			entry[key] = members
		} else {
			if prev, ok := entry[key]; ok {
				entry[key] = append(prev, vals_str...)
			} else {
				entry[key] = vals_str
			}
		}
	}

	// Ensure object has at least one objectclass value
	hasObjectClass := false
	for k := range entry {
		if strings.EqualFold(k, ATTR_OBJECTCLASS) {
			hasObjectClass = true
			break
		}
	}
	if !hasObjectClass {
		entry[ATTR_OBJECTCLASS] = []string{"top"}
	}

	// Write system attributes
	entry[ATTR_CREATORSNAME] = []string{state.login.user}
	entry[ATTR_CREATETIMESTAMP] = []string{genTimestamp()}
	entry[ATTR_ENTRYUUID] = []string{genUuid()}
	entry[dnSplit[0].Type] = []string{dnSplit[0].Value}

	// Add our item in the DB
	err = server.putAttributes(dn, entry)
	if err != nil {
		return ldap.LDAPResultOperationsError, err
	}
	// ~~ from this point on, our operation succeeded ~~
	// ~~ future errors cause inconsistencies in the DB and are logged ~~

	// If our item has a member list, add it to all of its member's memberOf attribute
	for _, member := range members {
		server.memberOfAdd(member, dn)
	}

	return ldap.LDAPResultSuccess, nil
}

// Delete request ------------------------

func (server *Server) handleDelete(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {
	state := s.(*State)
	r := m.GetDeleteRequest()

	code, err := server.handleDeleteInternal(state, &r)

	res := ldap.NewResponse(code)
	if err != nil {
		res.SetDiagnosticMessage(err.Error())
	}
	if code == ldap.LDAPResultSuccess {
		server.logger.Printf("Successfully deleted %s", string(r))
	} else {
		server.logger.Printf("Failed to delete %s (%s)", string(r), err)
	}
	w.Write(message.DelResponse(res))
}

func (server *Server) handleDeleteInternal(state *State, r *message.DelRequest) (int, error) {
	dn, err := server.checkDN(string(*r), false)
	if err != nil {
		return ldap.LDAPResultInvalidDNSyntax, err
	}

	// Check for delete permission
	if !server.config.Acl.Check(&state.login, "delete", dn, []string{}) {
		return ldap.LDAPResultInsufficientAccessRights, nil
	}

	// Check that this LDAP entry exists and has no children
	path, err := dnToConsul(dn)
	if err != nil {
		return ldap.LDAPResultInvalidDNSyntax, err
	}

	items, _, err := server.kv.List(path+"/", &server.readOpts)
	if err != nil {
		return ldap.LDAPResultOperationsError, err
	}

	if len(items) == 0 {
		return ldap.LDAPResultNoSuchObject, fmt.Errorf("Not found: %s", dn)
	}
	for _, item := range items {
		itemDN, _, err := consulToDN(item.Key)
		if err != nil {
			continue
		}
		if itemDN != dn {
			return ldap.LDAPResultNotAllowedOnNonLeaf, fmt.Errorf(
				"Cannot delete %s as it has children", dn)
		}
	}

	// Retrieve group membership before we delete everything
	memberOf, err := server.getAttribute(dn, ATTR_MEMBEROF)
	if err != nil {
		return ldap.LDAPResultOperationsError, err
	}
	memberList, err := server.getAttribute(dn, ATTR_MEMBER)
	if err != nil {
		return ldap.LDAPResultOperationsError, err
	}

	// Delete the LDAP entry
	_, err = server.kv.DeleteTree(path+"/", nil)
	if err != nil {
		return ldap.LDAPResultOperationsError, err
	}
	// ~~ from this point on, our operation succeeded ~~
	// ~~ future errors cause inconsistencies in the DB and are logged ~~

	// Delete it from the member list of all the groups it was a member of
	if memberOf != nil {
		for _, group := range memberOf {
			groupMembers, err := server.getAttribute(group, ATTR_MEMBER)
			if err != nil {
				server.logger.Warnf("Could not remove %s from members of %s: %s", dn, group, err)
				continue
			}

			newMembers := []string{}
			for _, memb := range groupMembers {
				if memb != dn {
					newMembers = append(newMembers, memb)
				}
			}

			err = server.putAttributes(group, Entry{
				ATTR_MEMBER: newMembers,
			})
			if err != nil {
				server.logger.Warnf("Could not remove %s from members of %s: %s", dn, group, err)
			}
		}
	}

	// Delete it from all of its member's memberOf info
	for _, member := range memberList {
		server.memberOfRemove(member, dn)
	}

	return ldap.LDAPResultSuccess, nil
}

// Modify request ------------------------

func (server *Server) handleModify(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {
	state := s.(*State)
	r := m.GetModifyRequest()

	code, err := server.handleModifyInternal(state, &r)

	res := ldap.NewResponse(code)
	if err != nil {
		res.SetDiagnosticMessage(err.Error())
	}
	if code == ldap.LDAPResultSuccess {
		server.logger.Printf("Successfully modified %s", string(r.Object()))
	} else {
		server.logger.Printf("Failed to modifiy %s (%s)", string(r.Object()), err)
	}
	w.Write(message.ModifyResponse(res))
}

func (server *Server) handleModifyInternal(state *State, r *message.ModifyRequest) (int, error) {
	dn, err := server.checkDN(string(r.Object()), false)
	if err != nil {
		return ldap.LDAPResultInvalidDNSyntax, err
	}

	dnSplit, err := parseDN(dn)
	if err != nil {
		return ldap.LDAPResultInvalidDNSyntax, err
	}

	// First permission check with no particular attributes
	if !server.config.Acl.Check(&state.login, "modify", dn, []string{}) &&
		!server.config.Acl.Check(&state.login, "modifyAdd", dn, []string{}) {
		return ldap.LDAPResultInsufficientAccessRights, nil
	}

	// Retrieve previous values (by the way, check object exists)
	path, err := dnToConsul(dn)
	if err != nil {
		return ldap.LDAPResultInvalidDNSyntax, err
	}

	items, _, err := server.kv.List(path+"/attribute=", &server.readOpts)
	if err != nil {
		return ldap.LDAPResultOperationsError, err
	}

	if len(items) == 0 {
		return ldap.LDAPResultNoSuchObject, fmt.Errorf("Not found: %s", dn)
	}

	prevEntry := Entry{}
	for _, item := range items {
		itemDN, attr, err := consulToDN(item.Key)
		if err != nil {
			continue
		}
		if itemDN != dn {
			server.logger.Fatal("itemDN != dn in handleModifyInternal")
		}
		vals, err := parseValue(item.Value)
		if err != nil {
			return ldap.LDAPResultOperationsError, err
		}
		prevEntry[attr] = vals
	}

	// Keep track of group members added/deleted
	addMembers, delMembers := []string{}, []string{}

	// Produce new entry values to be saved
	entry := Entry{}

	for _, change := range r.Changes() {
		attr := string(change.Modification().Type_())
		changeValues := []string{}
		for _, v := range change.Modification().Vals() {
			changeValues = append(changeValues, string(v))
		}

		// If we already had an attribute with this name before,
		// make sure we are using the same lowercase/uppercase
		for prevAttr := range prevEntry {
			if strings.EqualFold(attr, prevAttr) {
				attr = prevAttr
				break
			}
		}

		// Check that this attribute is not system-managed thus restricted
		err = checkRestrictedAttr(attr)
		if err != nil {
			return ldap.LDAPResultObjectClassViolation, err
		}
		if strings.EqualFold(attr, dnSplit[0].Type) {
			return ldap.LDAPResultObjectClassViolation, fmt.Errorf("%s may not be changed as it is part of object path", attr)
		}

		// Check for permission to modify this attribute
		if !(server.config.Acl.Check(&state.login, "modify", dn, []string{attr}) ||
			(change.Operation() == ldap.ModifyRequestChangeOperationAdd &&
				server.config.Acl.Check(&state.login, "modifyAdd", dn, []string{attr}))) {
			return ldap.LDAPResultInsufficientAccessRights, nil
		}

		// If we are changing ATTR_MEMBER, rewrite all values to canonical form
		if strings.EqualFold(attr, ATTR_MEMBER) {
			for i := range changeValues {
				canonical_val, err := server.checkDN(changeValues[i], false)
				if err != nil {
					return ldap.LDAPResultInvalidDNSyntax, err
				}
				changeValues[i] = canonical_val
			}
		}

		// If we don't yet have a new value for this attr,
		// but one existed before, initialize entry[attr] to the old value
		// so that later on what we do is simply modify entry[attr] in place
		// (this allows to handle sequences of several changes on the same attr)
		if _, ok := entry[attr]; !ok {
			if _, ok := prevEntry[attr]; ok {
				entry[attr] = prevEntry[attr]
			}
		}

		// Apply effective modification on entry[attr]
		if change.Operation() == ldap.ModifyRequestChangeOperationAdd {
			for _, val := range changeValues {
				if !listContains(entry[attr], val) {
					entry[attr] = append(entry[attr], val)
					if strings.EqualFold(attr, ATTR_MEMBER) {
						addMembers = append(addMembers, val)
					}
				}
			}
		} else if change.Operation() == ldap.ModifyRequestChangeOperationDelete {
			if len(changeValues) == 0 {
				// Delete everything
				if strings.EqualFold(attr, ATTR_MEMBER) {
					delMembers = append(delMembers, entry[attr]...)
				}
				entry[attr] = []string{}
			} else {
				// Delete only those specified
				newList := []string{}
				for _, prevVal := range entry[attr] {
					if !listContains(changeValues, prevVal) {
						newList = append(newList, prevVal)
					} else {
						if strings.EqualFold(attr, ATTR_MEMBER) {
							delMembers = append(delMembers, prevVal)
						}
					}
				}
				entry[attr] = newList
			}
		} else if change.Operation() == ldap.ModifyRequestChangeOperationReplace {
			if strings.EqualFold(attr, ATTR_MEMBER) {
				for _, newMem := range changeValues {
					if !listContains(entry[attr], newMem) {
						addMembers = append(addMembers, newMem)
					}
				}
				for _, prevMem := range entry[attr] {
					if !listContains(changeValues, prevMem) {
						delMembers = append(delMembers, prevMem)
					}
				}
			}
			entry[attr] = changeValues
		}
	}

	// Check that added members actually exist
	for i := range addMembers {
		exists, err := server.objectExists(addMembers[i])
		if err != nil {
			return ldap.LDAPResultOperationsError, err
		}
		if !exists {
			return ldap.LDAPResultNoSuchObject, fmt.Errorf(
				"Cannot add member %s, it does not exist", addMembers[i])
		}
	}

	for k, v := range entry {
		if strings.EqualFold(k, ATTR_OBJECTCLASS) && len(v) == 0 {
			return ldap.LDAPResultInsufficientAccessRights, fmt.Errorf(
				"Cannot remove all objectclass values")
		}
	}

	// Now, the modification has been processed and accepted and we want to commit it
	entry[ATTR_MODIFIERSNAME] = []string{state.login.user}
	entry[ATTR_MODIFYTIMESTAMP] = []string{genTimestamp()}

	// Save the edited values
	err = server.putAttributes(dn, entry)
	if err != nil {
		return ldap.LDAPResultOperationsError, err
	}
	// ~~ from this point on, our operation succeeded ~~
	// ~~ future errors cause inconsistencies in the DB and are logged ~~

	// Update memberOf for added members and deleted members
	for _, addMem := range addMembers {
		server.memberOfAdd(addMem, dn)
	}

	for _, delMem := range delMembers {
		server.memberOfRemove(delMem, dn)
	}

	return ldap.LDAPResultSuccess, nil
}