summaryrefslogtreecommitdiffstats
path: root/model/ldap/ldap.go
blob: 5f116f0fba28a16436185f6b9939d3d6e79f122b (plain)
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
package ldapauth

import (
	"crypto/tls"
	"fmt"
	"strconv"
	"time"
	"net/http"

	"github.com/mattermost/mattermost-server/app"
	"github.com/mattermost/mattermost-server/einterfaces"
	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/store"
	"gopkg.in/ldap.v2"
)

func init() {
	app.RegisterLdapInterface(NewLdapInterface)
}


type LdapConnection struct {
	Settings *model.LdapSettings
	Ldap *ldap.Conn
}

func NewLdapConnection(settings *model.LdapSettings) (*LdapConnection, *model.AppError) {
	c := new(LdapConnection)
	c.Settings = settings

	addr := fmt.Sprintf("%s:%d", *c.Settings.LdapServer, *c.Settings.LdapPort)
	tlsConfig := &tls.Config{InsecureSkipVerify: *c.Settings.SkipCertificateVerification}

	if *c.Settings.ConnectionSecurity == "TLS" {
		ldapConn, err := ldap.DialTLS("tcp", addr, tlsConfig)
		if err != nil {
			return nil, model.NewAppError("NewLdapConnection", "ent.ldap.do_login.unable_to_connect.app_error", nil, err.Error(), http.StatusBadRequest)
		}

		c.Ldap = ldapConn
	} else {
		ldapConn, err := ldap.Dial("tcp", addr)
		if err != nil {
			return nil, model.NewAppError("NewLdapConnection", "ent.ldap.do_login.unable_to_connect.app_error", nil, err.Error(), http.StatusBadRequest)
		}

		if *c.Settings.ConnectionSecurity == "STARTTLS" {
			if err := ldapConn.StartTLS(tlsConfig); err != nil {
				return nil, model.NewAppError("NewLdapConnection", "ent.ldap.do_login.unable_to_connect.app_error", nil, err.Error(), http.StatusBadRequest)
			}
		}

		c.Ldap = ldapConn
	}

	c.Ldap.SetTimeout(time.Duration(*c.Settings.QueryTimeout) * time.Second)

	if len(*c.Settings.BindUsername) > 0 {
		if err := c.Ldap.Bind(*c.Settings.BindUsername, *c.Settings.BindPassword); err != nil {
			return nil, model.NewAppError("NewLdapConnection", "ent.ldap.do_login.bind_admin_user.app_error", nil, err.Error(), http.StatusBadRequest)
		}
	}

	return c, nil
}

func (c LdapConnection) Close() {
	c.Ldap.Close()
}

func (c LdapConnection) Search(filter string, attributes []string) (map[string]string, *model.AppError) {
	searchRequest := ldap.NewSearchRequest(
		*c.Settings.BaseDN,
		ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
		filter, attributes,
		nil,
	)

	result, err := c.Ldap.Search(searchRequest)
	if err != nil {
		return nil, model.NewAppError("Search", "ent.ldap.do_login.search_ldap_server.app_error", nil, err.Error(), http.StatusBadRequest)
	}

	if len(result.Entries) == 0 {
		return nil, model.NewAppError("Search", "ent.ldap.do_login.user_not_registered.app_error", nil, "filter="+filter, http.StatusUnauthorized)
	} else if len(result.Entries) > 1 {
		return nil, model.NewAppError("Search", "ent.ldap.do_login.matched_to_many_users.app_error", nil, "filter="+filter, http.StatusBadRequest)
	}

	values := map[string]string{}
	for _, attribute := range attributes {
		values[attribute] = result.Entries[0].GetAttributeValue(attribute)
	}

	return values, nil
}

func (c LdapConnection) FindAuthData(id string) (string, *model.AppError) {
	values, err := c.GetUserAttributes(id, []string{*c.Settings.IdAttribute})
	if err != nil {
		return "", err
	}

	authData := values[*c.Settings.IdAttribute]
	if len(authData) == 0 {
		return "", model.NewAppError("FindUser", "ent.ldap.do_login.search_ldap_server.app_error", nil, "userId="+id, http.StatusBadRequest)
	}

	return authData, nil
}

func addAttribute(attributes []string, attr string) []string {
	if len(attr) == 0 {
		return attributes
	}

	for _, attribute := range attributes {
		if attribute == attr {
			return attributes
		}
	}

	return append(attributes, attr)
}

func (c LdapConnection) FindUser(id string) (*model.User, *model.AppError) {
	attributes := []string{*c.Settings.IdAttribute, *c.Settings.UsernameAttribute, *c.Settings.EmailAttribute}
	attributes = addAttribute(attributes, *c.Settings.FirstNameAttribute)
	attributes = addAttribute(attributes, *c.Settings.LastNameAttribute)
	attributes = addAttribute(attributes, *c.Settings.NicknameAttribute)
	attributes = addAttribute(attributes, *c.Settings.PositionAttribute)

	attrs, err := c.GetUserAttributes(id, attributes)
	if err != nil {
		return nil, err
	}

	user := &model.User{}
	user.Username = attrs[*c.Settings.UsernameAttribute]
	user.Email = attrs[*c.Settings.EmailAttribute]
	authData := attrs[*c.Settings.IdAttribute]
	user.AuthData = &authData

	/* optional user attributes */
	if len(*c.Settings.FirstNameAttribute) > 0 {
		user.FirstName = attrs[*c.Settings.FirstNameAttribute]
	}

	if len(*c.Settings.LastNameAttribute) > 0 {
		user.LastName = attrs[*c.Settings.LastNameAttribute]
	}

	if len(*c.Settings.NicknameAttribute) > 0 {
		user.Nickname = attrs[*c.Settings.NicknameAttribute]
	}

	if len(*c.Settings.PositionAttribute) > 0 {
		user.Position = attrs[*c.Settings.PositionAttribute]
	}

	return user, nil
}

func (c LdapConnection) GetUserAttributes(id string, attributes []string) (map[string]string, *model.AppError) {
	loginIdAttribute := *c.Settings.LoginIdAttribute
	if len(loginIdAttribute) == 0 {
		loginIdAttribute = *c.Settings.UsernameAttribute
	}

	filter := fmt.Sprintf("(%s=%s)", loginIdAttribute, ldap.EscapeFilter(id))
	if len(*c.Settings.UserFilter) > 0 {
		filter = fmt.Sprintf("(&%s%s)", filter, *c.Settings.UserFilter)
	}

	values, err := c.Search(filter, attributes)
	if err != nil {
		return nil, err
	}

	return values, nil
}

func (c LdapConnection) CheckPassword(authData string, password string) *model.AppError {
	if err := c.Ldap.Bind(authData, password); err != nil {
		return model.NewAppError("CheckPasswordAuthData", "ent.ldap.do_login.invalid_password.app_error", nil, "auth_data="+authData, http.StatusUnauthorized)
	}

	return nil
}

type LdapInterface struct {
	App *app.App;
}

func NewLdapInterface(app *app.App) einterfaces.LdapInterface {
	return LdapInterface{App: app}
}

func (i LdapInterface) NewLdapConnection() (*LdapConnection, *model.AppError) {
	settings := &i.App.GetConfig().LdapSettings
	return NewLdapConnection(settings)
}

func (i LdapInterface) DoLogin(id string, password string) (*model.User, *model.AppError) {
	c, err := i.NewLdapConnection()
	if err != nil {
		return nil, err
	}
	defer c.Close()

	ldapUser, err := c.FindUser(id)
	if err != nil {
		return nil, err
	}

	if err := c.CheckPassword(*ldapUser.AuthData, password); err != nil {
		return nil, err
	}

	return i.GetLdapUser(ldapUser)
}

func (i LdapInterface) GetUser(id string) (*model.User, *model.AppError) {
	c, err := i.NewLdapConnection()
	if err != nil {
		return nil, err
	}
	defer c.Close()

	ldapUser, err := c.FindUser(id)
	if err != nil {
		return nil, err
	}

	return i.GetLdapUser(ldapUser)
}

func (i LdapInterface) GetUserAttributes(id string, attributes []string) (map[string]string, *model.AppError) {
	c, err := i.NewLdapConnection()
	if err != nil {
		return nil, err
	}
	defer c.Close()

	return c.GetUserAttributes(id, attributes)
}

func (i LdapInterface) CheckPassword(id string, password string) *model.AppError {
	c, err := i.NewLdapConnection()
	if err != nil {
		return err
	}
	defer c.Close()

	authData, err := c.FindAuthData(id)
	if err != nil {
		return err
	}

	return c.CheckPassword(authData, password)
}

func (i LdapInterface) CheckPasswordAuthData(authData string, password string) *model.AppError {
	c, err := i.NewLdapConnection()
	if err != nil {
		return err
	}
	defer c.Close()

	return c.CheckPassword(authData, password)
}

func (i LdapInterface) SwitchToLdap(userId, ldapId, ldapPassword string) *model.AppError {
	c, err := i.NewLdapConnection()
	if err != nil {
		return err
	}
	defer c.Close()

	authData, err := c.FindAuthData(ldapId)
	if err != nil {
		return err
	}

	if err := c.CheckPassword(authData, ldapPassword); err != nil {
		return err
	}

	if res := <-i.App.Srv.Store.User().UpdateAuthData(userId, model.USER_AUTH_SERVICE_LDAP, &authData, "", false); res.Err != nil {
		return res.Err
	}

	return nil
}

func (i LdapInterface) ValidateFilter(filter string) *model.AppError {
	if _, err := ldap.CompileFilter(filter); err != nil {
		return model.NewAppError("ValidateFilter", "ent.ldap.validate_filter.app_error", nil, err.Error(), http.StatusBadRequest)
	}

	return nil
}

func (i LdapInterface) RunTest() *model.AppError {
	c, err := i.NewLdapConnection()
	if err != nil {
		return err
	}
	defer c.Close()

	return nil
}

func (i LdapInterface) MigrateIDAttribute(toAttribute string) error {
	// not required for login
	// TODO
	return nil
}

func (i LdapInterface) StartSynchronizeJob(waitForJobToFinish bool) (*model.Job, *model.AppError) {
	// not required for login
	// TODO
	return nil, nil
}

func (i LdapInterface) GetAllLdapUsers() ([]*model.User, *model.AppError) {
	// not used (maybe used by synchronization job)
	// TODO
	return nil, nil
}

func (i LdapInterface) CreateLdapUser(user *model.User) (*model.User, *model.AppError) {
	found := true
	count := 0
	for found {
		if found = i.App.IsUsernameTaken(user.Username); found {
			user.Username = user.Username + strconv.Itoa(count)
			count++
		}
	}

	user.AuthService = model.USER_AUTH_SERVICE_LDAP
	user.EmailVerified = true

	ruser, err := i.App.CreateUser(user)
	if err != nil {
		return nil, model.NewAppError("CreateUser", "ent.ldap.create_fail", nil, err.Error(), http.StatusBadRequest)
	}

	return ruser, nil
}

func (i LdapInterface) UpdateLdapUserAttrs(user *model.User, ldapUser *model.User) *model.AppError {
	userAttrsChanged := false

	if ldapUser.Username != user.Username {
		if existingUser, _ := i.App.GetUserByUsername(ldapUser.Username); existingUser == nil {
			user.Username = ldapUser.Username
			userAttrsChanged = true
		}
	}

	if ldapUser.GetFullName() != user.GetFullName() {
		user.FirstName = ldapUser.FirstName
		user.LastName = ldapUser.LastName
		userAttrsChanged = true
	}

	if ldapUser.Nickname != user.Nickname {
		user.Nickname = ldapUser.Nickname
		userAttrsChanged = true
	}

	if ldapUser.Position != user.Position {
		user.Position = ldapUser.Position
		userAttrsChanged = true
	}

	if ldapUser.Email != user.Email {
		if existingUser, _ := i.App.GetUserByEmail(ldapUser.Email); existingUser == nil {
			user.Email = ldapUser.Email
			userAttrsChanged = true
		}
	}

	if userAttrsChanged {
		result := <-i.App.Srv.Store.User().Update(user, true)
		if result.Err != nil {
			return result.Err
		}

		user = result.Data.([2]*model.User)[0]
		i.App.InvalidateCacheForUser(user.Id)
	}

	return nil
}

func (i LdapInterface) GetLdapUser(ldapUser *model.User) (*model.User, *model.AppError) {
	user, err := i.App.GetUserByAuth(ldapUser.AuthData, model.USER_AUTH_SERVICE_LDAP)
	if err != nil {
		if err.Id == store.MISSING_AUTH_ACCOUNT_ERROR {
			return i.CreateLdapUser(ldapUser)
		}
		return nil, err
	}

	if err := i.UpdateLdapUserAttrs(user, ldapUser); err != nil {
		return nil, err
	}

	return user, nil
}