model.go 16.7 KB
Newer Older
ale's avatar
ale committed
1
2
3
4
5
6
7
package backend

import (
	"context"
	"strings"

	ldaputil "git.autistici.org/ai3/go-common/ldap"
8
	"github.com/tstranex/u2f"
ale's avatar
ale committed
9
10
11
12
13
	"gopkg.in/ldap.v2"

	"git.autistici.org/ai3/accountserver"
)

14
15
16
17
const (
	// Names of some well-known LDAP attributes.
	totpSecretLDAPAttr        = "totpSecret"
	preferredLanguageLDAPAttr = "preferredLanguage"
ale's avatar
ale committed
18
19
	recoveryHintLDAPAttr      = "recoverQuestion"
	recoveryResponseLDAPAttr  = "recoverAnswer"
20
21
22
23
	aspLDAPAttr               = "appSpecificPassword"
	storagePublicKeyLDAPAttr  = "storagePublicKey"
	storagePrivateKeyLDAPAttr = "storageEncryptedSecretKey"
	passwordLDAPAttr          = "userPassword"
24
	u2fRegistrationsLDAPAttr  = "u2fRegistration"
25
26
)

27
// backend is the interface to an LDAP-backed user database.
ale's avatar
ale committed
28
29
30
31
32
//
// We keep a set of LDAP queries for each resource type, each having a
// "resource" query to return a specific resource belonging to a user,
// and a "presence" query that checks for existence of a resource for
// all users.
33
type backend struct {
ale's avatar
ale committed
34
	conn                ldapConn
ale's avatar
ale committed
35
	baseDN              string
36
37
	userQuery           *queryTemplate
	userResourceQueries []*queryTemplate
ale's avatar
ale committed
38
	resources           *resourceRegistry
ale's avatar
ale committed
39
40
}

ale's avatar
ale committed
41
42
// backendTX holds the business logic (that runs within a single
// transaction).
43
44
45
46
47
type backendTX struct {
	*ldapTX
	backend *backend
}

ale's avatar
ale committed
48
49
const ldapPoolSize = 20

50
51
52
53
54
55
56
func (b *backend) NewTransaction() (accountserver.TX, error) {
	return &backendTX{
		ldapTX:  newLDAPTX(b.conn),
		backend: b,
	}, nil
}

ale's avatar
ale committed
57
58
// NewLDAPBackend initializes an LDAPBackend object with the given LDAP
// connection pool.
59
func NewLDAPBackend(uri, bindDN, bindPw, base string) (accountserver.Backend, error) {
ale's avatar
ale committed
60
61
62
63
	pool, err := ldaputil.NewConnectionPool(uri, bindDN, bindPw, ldapPoolSize)
	if err != nil {
		return nil, err
	}
64
65
	return newLDAPBackendWithConn(pool, base)
}
ale's avatar
ale committed
66

67
func newLDAPBackendWithConn(conn ldapConn, base string) (*backend, error) {
ale's avatar
ale committed
68
69
70
71
72
73
74
75
	rsrc := newResourceRegistry()
	rsrc.register(accountserver.ResourceTypeEmail, &emailResourceHandler{baseDN: base})
	rsrc.register(accountserver.ResourceTypeMailingList, &mailingListResourceHandler{baseDN: base})
	rsrc.register(accountserver.ResourceTypeDAV, &webdavResourceHandler{baseDN: base})
	rsrc.register(accountserver.ResourceTypeWebsite, &websiteResourceHandler{baseDN: base})
	rsrc.register(accountserver.ResourceTypeDomain, &domainResourceHandler{baseDN: base})
	rsrc.register(accountserver.ResourceTypeDatabase, &databaseResourceHandler{baseDN: base})

76
	return &backend{
ale's avatar
ale committed
77
78
		conn:   conn,
		baseDN: base,
79
80
81
82
83
84
		userQuery: &queryTemplate{
			Base:   joinDN("uid=${user}", "ou=People", base),
			Filter: "(objectClass=*)",
			Scope:  ldap.ScopeBaseObject,
		},
		userResourceQueries: []*queryTemplate{
ale's avatar
ale committed
85
			// Find all resources that are children of the main uid object.
86
87
88
89
			&queryTemplate{
				Base:  joinDN("uid=${user}", "ou=People", base),
				Scope: ldap.ScopeWholeSubtree,
			},
ale's avatar
ale committed
90
			// Find mailing lists, which are nested under a different root.
91
92
			&queryTemplate{
				Base:   joinDN("ou=Lists", base),
ale's avatar
ale committed
93
				Filter: "(&(objectClass=mailingList)(listOwner=${user}))",
94
95
				Scope:  ldap.ScopeSingleLevel,
			},
ale's avatar
ale committed
96
		},
ale's avatar
ale committed
97
		resources: rsrc,
ale's avatar
ale committed
98
	}, nil
ale's avatar
ale committed
99
100
101
}

func newUser(entry *ldap.Entry) (*accountserver.User, error) {
102
103
104
105
106
107
108
109
	// Note that some user-level attributes related to
	// authentication are stored on the uid= object, while others
	// are on the email= object. We set the latter in the GetUser
	// function later.
	//
	// The case of password recovery attributes is more complex:
	// the current schema has those on email=, but we'd like to
	// move them to uid=, so we currently have to support both.
ale's avatar
ale committed
110
	user := &accountserver.User{
111
112
113
114
		Name:                 entry.GetAttributeValue("uid"),
		Lang:                 entry.GetAttributeValue(preferredLanguageLDAPAttr),
		PasswordRecoveryHint: entry.GetAttributeValue(recoveryHintLDAPAttr),
		U2FRegistrations:     decodeU2FRegistrations(entry.GetAttributeValues(u2fRegistrationsLDAPAttr)),
ale's avatar
ale committed
115
	}
116
117
118
119
120
121

	// The user has 2FA enabled if it has a TOTP secret or U2F keys.
	if (entry.GetAttributeValue(totpSecretLDAPAttr) != "") || (len(user.U2FRegistrations) > 0) {
		user.Has2FA = true
	}

ale's avatar
ale committed
122
123
124
125
126
127
	if user.Lang == "" {
		user.Lang = "en"
	}
	return user, nil
}

ale's avatar
ale committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
func userToLDAP(user *accountserver.User) (attrs []ldap.PartialAttribute) {
	// Most attributes are read-only and have specialized methods to set them.
	attrs = append(attrs, []ldap.PartialAttribute{
		{Type: "objectClass", Vals: []string{"top", "person", "posixAccount", "shadowAccount", "organizationalPerson", "inetOrgPerson", "totpAccount"}},
		{Type: "uid", Vals: s2l(user.Name)},
		{Type: "cn", Vals: s2l(user.Name)},
		{Type: "givenName", Vals: []string{"Private"}},
		{Type: "sn", Vals: []string{"Private"}},
		{Type: "gecos", Vals: s2l(user.Name)},
		{Type: "loginShell", Vals: []string{"/bin/false"}},
		{Type: "homeDirectory", Vals: []string{"/var/empty"}},
		{Type: "shadowLastChange", Vals: []string{"12345"}},
		{Type: "shadowWarning", Vals: []string{"7"}},
		{Type: "shadowMax", Vals: []string{"99999"}},
		{Type: preferredLanguageLDAPAttr, Vals: s2l(user.Lang)},
143
		{Type: u2fRegistrationsLDAPAttr, Vals: encodeU2FRegistrations(user.U2FRegistrations)},
ale's avatar
ale committed
144
145
146
147
	}...)
	return
}

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
func decodeU2FRegistration(enc string) (*u2f.Registration, error) {
	var reg u2f.Registration
	if err := reg.UnmarshalBinary([]byte(enc)); err != nil {
		return nil, err
	}
	return &reg, nil
}

func encodeU2FRegistration(r *u2f.Registration) string {
	b, _ := r.MarshalBinary()
	return string(b)
}

func decodeU2FRegistrations(encRegs []string) []*u2f.Registration {
	var out []*u2f.Registration
	for _, enc := range encRegs {
		if r, err := decodeU2FRegistration(enc); err == nil {
			out = append(out, r)
		}
	}
	return out
}

func encodeU2FRegistrations(regs []*u2f.Registration) []string {
	var out []string
	for _, r := range regs {
		out = append(out, encodeU2FRegistration(r))
	}
	return out
}

ale's avatar
ale committed
179
180
func (tx *backendTX) getUserDN(user *accountserver.User) string {
	return joinDN("uid="+user.Name, "ou=People", tx.backend.baseDN)
ale's avatar
ale committed
181
182
}

ale's avatar
ale committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// CreateUser creates a new user.
func (tx *backendTX) CreateUser(ctx context.Context, user *accountserver.User) error {
	dn := tx.getUserDN(user)

	tx.create(dn)
	for _, attr := range userToLDAP(user) {
		tx.setAttr(dn, attr.Type, attr.Vals...)
	}

	// Create all resources.
	for _, r := range user.Resources {
		if err := tx.CreateResource(ctx, r); err != nil {
			return err
		}
	}

	return nil
}

202
203
204
205
206
207
208
209
210
// UpdateUser updates values for the user only (not the resources).
func (tx *backendTX) UpdateUser(ctx context.Context, user *accountserver.User) error {
	dn := tx.getUserDN(user)
	for _, attr := range userToLDAP(user) {
		tx.setAttr(dn, attr.Type, attr.Vals...)
	}
	return nil
}

ale's avatar
ale committed
211
// GetUser returns a user.
212
func (tx *backendTX) GetUser(ctx context.Context, username string) (*accountserver.User, error) {
ale's avatar
ale committed
213
214
	// First of all, find the main user object, and just that one.
	vars := map[string]string{"user": username}
215
	result, err := tx.search(ctx, tx.backend.userQuery.query(vars))
ale's avatar
ale committed
216
217
218
219
220
221
	if err != nil {
		if ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject) {
			return nil, nil
		}
		return nil, err
	}
222
223
224
	if len(result.Entries) == 0 {
		return nil, nil
	}
ale's avatar
ale committed
225
226
227
228
229
230
231
232
233
234

	user, err := newUser(result.Entries[0])
	if err != nil {
		return nil, err
	}

	// Now run the resource queries, and accumulate results on the User
	// object we just created.
	// TODO: parallelize.
	// TODO: add support for non-LDAP resource queries.
235
236
	for _, tpl := range tx.backend.userResourceQueries {
		result, err = tx.search(ctx, tpl.query(vars))
ale's avatar
ale committed
237
238
239
240
241
242
243
244
245
		if err != nil {
			continue
		}

		for _, entry := range result.Entries {
			// Some user-level attributes are actually stored on the email
			// object, a shortcoming of the legacy A/I database model. Set
			// them on the main User object.
			if isObjectClass(entry, "virtualMailUser") {
246
247
248
				if s := entry.GetAttributeValue(recoveryHintLDAPAttr); s != "" {
					user.PasswordRecoveryHint = s
				}
249
250
				user.AppSpecificPasswords = getASPInfo(decodeAppSpecificPasswords(entry.GetAttributeValues(aspLDAPAttr)))
				user.HasEncryptionKeys = (entry.GetAttributeValue(storagePublicKeyLDAPAttr) != "")
ale's avatar
ale committed
251
			}
ale's avatar
ale committed
252

ale's avatar
ale committed
253
			// Parse the resource and add it to the User.
ale's avatar
ale committed
254
			if r, err := tx.backend.resources.FromLDAP(entry); err == nil {
ale's avatar
ale committed
255
256
257
258
259
260
261
262
263
264
				user.Resources = append(user.Resources, r)
			}
		}
	}

	groupWebResourcesByHomedir(user.Resources)

	return user, nil
}

265
func (tx *backendTX) SetUserPassword(ctx context.Context, user *accountserver.User, encryptedPassword string) error {
266
267
268
269
270
271
	dn := tx.getUserDN(user)
	tx.setAttr(dn, passwordLDAPAttr, encryptedPassword)
	for _, r := range user.GetResourcesByType(accountserver.ResourceTypeEmail) {
		dn, _ = tx.backend.resources.GetDN(r.ID)
		tx.setAttr(dn, passwordLDAPAttr, encryptedPassword)
	}
272
	return nil
ale's avatar
ale committed
273
274
}

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
func (tx *backendTX) GetUserEncryptedPassword(ctx context.Context, user *accountserver.User) string {
	values := tx.readAttributeValues(ctx, tx.getUserDN(user), passwordLDAPAttr)
	if len(values) > 0 {
		// Remove legacy LDAP encryption prefix.
		return strings.TrimPrefix(values[0], "{crypt}")
	}
	return ""
}

func (tx *backendTX) GetUserRecoveryEncryptedPassword(ctx context.Context, user *accountserver.User) string {
	values := tx.readAttributeValues(ctx, tx.getUserDN(user), recoveryResponseLDAPAttr)
	if len(values) > 0 {
		// Remove legacy LDAP encryption prefix.
		return strings.TrimPrefix(values[0], "{crypt}")
	}
	return ""
}

ale's avatar
ale committed
293
func (tx *backendTX) SetPasswordRecoveryHint(ctx context.Context, user *accountserver.User, hint, response string) error {
294
295
	// Write the password recovery attributes on the uid= object,
	// as per the new schema.
ale's avatar
ale committed
296
297
298
299
300
301
	dn := tx.getUserDN(user)
	tx.setAttr(dn, recoveryHintLDAPAttr, hint)
	tx.setAttr(dn, recoveryResponseLDAPAttr, response)
	return nil
}

302
func (tx *backendTX) GetUserEncryptionKeys(ctx context.Context, user *accountserver.User) ([]*accountserver.UserEncryptionKey, error) {
303
304
305
	r := user.GetSingleResourceByType(accountserver.ResourceTypeEmail)
	dn, _ := tx.backend.resources.GetDN(r.ID)
	rawKeys := tx.readAttributeValues(ctx, dn, storagePrivateKeyLDAPAttr)
306
	return decodeUserEncryptionKeys(rawKeys), nil
ale's avatar
ale committed
307
308
}

309
func (tx *backendTX) SetUserEncryptionKeys(ctx context.Context, user *accountserver.User, keys []*accountserver.UserEncryptionKey) error {
310
	encKeys := encodeUserEncryptionKeys(keys)
311
312
313
314
	for _, r := range user.GetResourcesByType(accountserver.ResourceTypeEmail) {
		dn, _ := tx.backend.resources.GetDN(r.ID)
		tx.setAttr(dn, storagePrivateKeyLDAPAttr, encKeys...)
	}
315
	return nil
ale's avatar
ale committed
316
317
}

318
func (tx *backendTX) SetUserEncryptionPublicKey(ctx context.Context, user *accountserver.User, pub []byte) error {
319
320
321
322
	for _, r := range user.GetResourcesByType(accountserver.ResourceTypeEmail) {
		dn, _ := tx.backend.resources.GetDN(r.ID)
		tx.setAttr(dn, storagePublicKeyLDAPAttr, string(pub))
	}
323
324
325
326
327
328
329
330
331
	return nil
}

func excludeASPFromList(asps []*appSpecificPassword, id string) []*appSpecificPassword {
	var out []*appSpecificPassword
	for _, asp := range asps {
		if asp.ID != id {
			out = append(out, asp)
		}
332
	}
333
	return out
334
335
}

336
337
func (tx *backendTX) setASPOnResource(ctx context.Context, r *accountserver.Resource, info *accountserver.AppSpecificPasswordInfo, encryptedPassword string) {
	dn, _ := tx.backend.resources.GetDN(r.ID)
ale's avatar
ale committed
338

339
	// Obtain the full list of ASPs from the backend and replace/append the new one.
340
	asps := decodeAppSpecificPasswords(tx.readAttributeValues(ctx, dn, aspLDAPAttr))
341
342
	asps = append(excludeASPFromList(asps, info.ID), newAppSpecificPassword(*info, encryptedPassword))
	outASPs := encodeAppSpecificPasswords(asps)
343
	tx.setAttr(dn, aspLDAPAttr, outASPs...)
ale's avatar
ale committed
344
345
}

346
347
348
func (tx *backendTX) SetApplicationSpecificPassword(ctx context.Context, user *accountserver.User, info *accountserver.AppSpecificPasswordInfo, encryptedPassword string) error {
	for _, r := range user.GetResourcesByType(accountserver.ResourceTypeEmail) {
		tx.setASPOnResource(ctx, r, info, encryptedPassword)
ale's avatar
ale committed
349
	}
350
351
	return nil
}
ale's avatar
ale committed
352

353
354
355
func (tx *backendTX) deleteASPOnResource(ctx context.Context, r *accountserver.Resource, id string) {
	dn, _ := tx.backend.resources.GetDN(r.ID)
	asps := decodeAppSpecificPasswords(tx.readAttributeValues(ctx, dn, aspLDAPAttr))
356
357
	asps = excludeASPFromList(asps, id)
	outASPs := encodeAppSpecificPasswords(asps)
358
359
	tx.setAttr(dn, aspLDAPAttr, outASPs...)
}
ale's avatar
ale committed
360

361
362
363
364
func (tx *backendTX) DeleteApplicationSpecificPassword(ctx context.Context, user *accountserver.User, id string) error {
	for _, r := range user.GetResourcesByType(accountserver.ResourceTypeEmail) {
		tx.deleteASPOnResource(ctx, r, id)
	}
365
	return nil
ale's avatar
ale committed
366
367
}

368
func (tx *backendTX) SetUserTOTPSecret(ctx context.Context, user *accountserver.User, secret string) error {
369
	tx.setAttr(tx.getUserDN(user), totpSecretLDAPAttr, secret)
370
	return nil
ale's avatar
ale committed
371
372
}

373
func (tx *backendTX) DeleteUserTOTPSecret(ctx context.Context, user *accountserver.User) error {
374
	tx.setAttr(tx.getUserDN(user), totpSecretLDAPAttr)
375
	return nil
ale's avatar
ale committed
376
377
}

378
func (tx *backendTX) SetResourcePassword(ctx context.Context, r *accountserver.Resource, encryptedPassword string) error {
ale's avatar
ale committed
379
	dn, _ := tx.backend.resources.GetDN(r.ID)
380
	tx.setAttr(dn, passwordLDAPAttr, encryptedPassword)
381
	return nil
ale's avatar
ale committed
382
383
}

ale's avatar
ale committed
384
func (tx *backendTX) hasResource(ctx context.Context, resourceType, resourceName string) (bool, error) {
385
	tpl, err := tx.backend.resources.SearchQuery(resourceType)
ale's avatar
ale committed
386
387
	if err != nil {
		return false, err
ale's avatar
ale committed
388
389
390
	}

	// Make a quick LDAP search that only fetches the DN attribute.
391
392
	tpl.Attrs = []string{"dn"}
	result, err := tx.search(ctx, tpl.query(map[string]string{
ale's avatar
ale committed
393
394
		"resource": resourceName,
		"type":     resourceType,
395
	}))
ale's avatar
ale committed
396
397
398
399
400
401
402
403
404
405
406
407
408
	if err != nil {
		if ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject) {
			return false, nil
		}
		return false, err
	}
	if len(result.Entries) == 0 {
		return false, nil
	}
	return true, nil
}

// HasAnyResource returns true if any of the specified resources exists.
ale's avatar
ale committed
409
410
411
func (tx *backendTX) HasAnyResource(ctx context.Context, resourceIDs []accountserver.FindResourceRequest) (bool, error) {
	for _, req := range resourceIDs {
		has, err := tx.hasResource(ctx, req.Type, req.Name)
ale's avatar
ale committed
412
413
414
415
416
417
418
		if err != nil || has {
			return has, err
		}
	}
	return false, nil
}

419
// GetResource returns a ResourceWrapper, as part of a read-modify-update transaction.
420
func (tx *backendTX) GetResource(ctx context.Context, rsrcID accountserver.ResourceID) (*accountserver.Resource, error) {
ale's avatar
ale committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
	// From the resource ID we can obtain the DN, and fetch it
	// straight from LDAP without even doing a real search.
	dn, err := tx.backend.resources.GetDN(rsrcID)
	if err != nil {
		return nil, err
	}

	// This is just a 'point' search.
	req := ldap.NewSearchRequest(
		dn,
		ldap.ScopeBaseObject,
		ldap.NeverDerefAliases,
		0,
		0,
		false,
		"(objectClass=*)",
		nil,
		nil,
	)

	result, err := tx.search(ctx, req)
442
443
444
445
446
447
448
	if err != nil {
		if ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject) {
			return nil, nil
		}
		return nil, err
	}

ale's avatar
ale committed
449
450
	// We know the resource type so we don't have to guess.
	return tx.backend.resources.FromLDAPWithType(rsrcID.Type(), result.Entries[0])
451
452
}

ale's avatar
ale committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// CreateResource creates a new LDAP-backed resource object.
func (tx *backendTX) CreateResource(ctx context.Context, r *accountserver.Resource) error {
	dn, err := tx.backend.resources.GetDN(r.ID)
	if err != nil {
		return err
	}

	tx.create(dn)
	for _, attr := range tx.backend.resources.ToLDAP(r) {
		tx.setAttr(dn, attr.Type, attr.Vals...)
	}

	return nil
}

468
// UpdateResource updates a LDAP-backed resource that was obtained by a previous GetResource call.
469
func (tx *backendTX) UpdateResource(ctx context.Context, r *accountserver.Resource) error {
ale's avatar
ale committed
470
471
472
473
	dn, err := tx.backend.resources.GetDN(r.ID)
	if err != nil {
		return err
	}
474

475
476
	// We can simply dump all attribute/value pairs and let the
	// code in ldapTX do the work of finding the differences.
ale's avatar
ale committed
477
	for _, attr := range tx.backend.resources.ToLDAP(r) {
478
		tx.setAttr(dn, attr.Type, attr.Vals...)
479
480
	}

481
	return nil
482
483
}

ale's avatar
ale committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
var siteRoot = "/home/users/investici.org/"

// The hosting directory for a website is the path component immediately after
// siteRoot. This works also for sites with nested documentRoots.
func getHostingDir(path string) string {
	path = strings.TrimPrefix(path, siteRoot)
	if i := strings.Index(path, "/"); i > 0 {
		return path[:i]
	}
	return path
}

// This is a very specific function meant to address a peculiar characteristic
// of the A/I legacy data model, where DAV accounts and websites do not have an
// explicit relation.
func groupWebResourcesByHomedir(resources []*accountserver.Resource) {
	// Set the group name to be the 'hostingDir' for sites and DAV
	// accounts. Keep a reference of websites by ID so we can later fix the
	// group for databases too, via their ParentID.
	webs := make(map[string]*accountserver.Resource)
	for _, r := range resources {
ale's avatar
ale committed
505
		switch r.ID.Type() {
506
		case accountserver.ResourceTypeWebsite, accountserver.ResourceTypeDomain:
ale's avatar
ale committed
507
			r.Group = getHostingDir(r.Website.DocumentRoot)
508
			webs[r.ID.String()] = r
ale's avatar
ale committed
509
510
511
512
513
514
		case accountserver.ResourceTypeDAV:
			r.Group = getHostingDir(r.DAV.Homedir)
		}
	}
	// Fix databases in a second pass.
	for _, r := range resources {
ale's avatar
ale committed
515
		if r.ID.Type() == accountserver.ResourceTypeDatabase && !r.ParentID.Empty() {
516
			r.Group = webs[r.ParentID.String()].Group
ale's avatar
ale committed
517
518
519
		}
	}
}