2021-07-24 15:46:34 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-09-15 14:58:37 +05:30
|
|
|
"sort"
|
2021-07-24 15:46:34 +05:30
|
|
|
"strings"
|
|
|
|
|
2021-12-10 13:44:24 +05:30
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2021-11-10 10:43:16 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 15:46:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-04-30 01:08:11 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-11-22 20:51:55 +05:30
|
|
|
user_service "code.gitea.io/gitea/services/user"
|
2021-07-24 15:46:34 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Sync causes this ldap source to synchronize its users with the db
|
|
|
|
func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Trace("Doing: SyncExternalUsers[%s]", source.authSource.Name)
|
2021-07-24 15:46:34 +05:30
|
|
|
|
2021-09-15 14:58:37 +05:30
|
|
|
var existingUsers []int
|
2021-07-24 15:46:34 +05:30
|
|
|
isAttributeSSHPublicKeySet := len(strings.TrimSpace(source.AttributeSSHPublicKey)) > 0
|
|
|
|
var sshKeysNeedUpdate bool
|
|
|
|
|
|
|
|
// Find all users with this login type - FIXME: Should this be an iterator?
|
2022-01-02 18:42:35 +05:30
|
|
|
users, err := user_model.GetUsersBySource(source.authSource)
|
2021-07-24 15:46:34 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("SyncExternalUsers: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Warn("SyncExternalUsers: Cancelled before update of %s", source.authSource.Name)
|
|
|
|
return db.ErrCancelledf("Before update of %s", source.authSource.Name)
|
2021-07-24 15:46:34 +05:30
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-09-15 14:58:37 +05:30
|
|
|
sort.Slice(users, func(i, j int) bool {
|
|
|
|
return users[i].LowerName < users[j].LowerName
|
|
|
|
})
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
sr, err := source.SearchEntries()
|
|
|
|
if err != nil {
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Error("SyncExternalUsers LDAP source failure [%s], skipped", source.authSource.Name)
|
2021-07-24 15:46:34 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sr) == 0 {
|
|
|
|
if !source.AllowDeactivateAll {
|
|
|
|
log.Error("LDAP search found no entries but did not report an error. Refusing to deactivate all users")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Warn("LDAP search found no entries but did not report an error. All users will be deactivated as per settings")
|
|
|
|
}
|
|
|
|
|
2021-09-15 14:58:37 +05:30
|
|
|
sort.Slice(sr, func(i, j int) bool {
|
|
|
|
return sr[i].LowerName < sr[j].LowerName
|
|
|
|
})
|
|
|
|
|
|
|
|
userPos := 0
|
2022-03-29 11:59:02 +05:30
|
|
|
orgCache := make(map[string]*organization.Organization)
|
|
|
|
teamCache := make(map[string]*organization.Team)
|
2021-09-15 14:58:37 +05:30
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
for _, su := range sr {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Warn("SyncExternalUsers: Cancelled at update of %s before completed update of users", source.authSource.Name)
|
2021-07-24 15:46:34 +05:30
|
|
|
// Rewrite authorized_keys file if LDAP Public SSH Key attribute is set and any key was added or removed
|
|
|
|
if sshKeysNeedUpdate {
|
2021-12-10 13:44:24 +05:30
|
|
|
err = asymkey_model.RewriteAllPublicKeys()
|
2021-07-24 15:46:34 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("RewriteAllPublicKeys: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-02 18:42:35 +05:30
|
|
|
return db.ErrCancelledf("During update of %s before completed update of users", source.authSource.Name)
|
2021-07-24 15:46:34 +05:30
|
|
|
default:
|
|
|
|
}
|
|
|
|
if len(su.Username) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(su.Mail) == 0 {
|
|
|
|
su.Mail = fmt.Sprintf("%s@localhost", su.Username)
|
|
|
|
}
|
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
var usr *user_model.User
|
2021-09-15 14:58:37 +05:30
|
|
|
for userPos < len(users) && users[userPos].LowerName < su.LowerName {
|
|
|
|
userPos++
|
|
|
|
}
|
|
|
|
if userPos < len(users) && users[userPos].LowerName == su.LowerName {
|
|
|
|
usr = users[userPos]
|
|
|
|
existingUsers = append(existingUsers, userPos)
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fullName := composeFullName(su.Name, su.Surname, su.Username)
|
|
|
|
// If no existing user found, create one
|
|
|
|
if usr == nil {
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Trace("SyncExternalUsers[%s]: Creating user %s", source.authSource.Name, su.Username)
|
2021-07-24 15:46:34 +05:30
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
usr = &user_model.User{
|
2022-04-30 01:08:11 +05:30
|
|
|
LowerName: su.LowerName,
|
|
|
|
Name: su.Username,
|
|
|
|
FullName: fullName,
|
|
|
|
LoginType: source.authSource.Type,
|
|
|
|
LoginSource: source.authSource.ID,
|
|
|
|
LoginName: su.Username,
|
|
|
|
Email: su.Mail,
|
|
|
|
IsAdmin: su.IsAdmin,
|
|
|
|
}
|
|
|
|
overwriteDefault := &user_model.CreateUserOverwriteOptions{
|
|
|
|
IsRestricted: util.OptionalBoolOf(su.IsRestricted),
|
|
|
|
IsActive: util.OptionalBoolTrue,
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
2022-04-30 01:08:11 +05:30
|
|
|
err = user_model.CreateUser(usr, overwriteDefault)
|
2021-07-24 15:46:34 +05:30
|
|
|
if err != nil {
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Error("SyncExternalUsers[%s]: Error creating user %s: %v", source.authSource.Name, su.Username, err)
|
2021-09-27 08:09:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && isAttributeSSHPublicKeySet {
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Trace("SyncExternalUsers[%s]: Adding LDAP Public SSH Keys for user %s", source.authSource.Name, usr.Name)
|
|
|
|
if asymkey_model.AddPublicKeysBySource(usr, source.authSource, su.SSHPublicKey) {
|
2021-07-24 15:46:34 +05:30
|
|
|
sshKeysNeedUpdate = true
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 08:09:36 +05:30
|
|
|
|
|
|
|
if err == nil && len(source.AttributeAvatar) > 0 {
|
2021-11-22 20:51:55 +05:30
|
|
|
_ = user_service.UploadAvatar(usr, su.Avatar)
|
2021-09-27 08:09:36 +05:30
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
} else if updateExisting {
|
|
|
|
// Synchronize SSH Public Key if that attribute is set
|
2022-01-02 18:42:35 +05:30
|
|
|
if isAttributeSSHPublicKeySet && asymkey_model.SynchronizePublicKeys(usr, source.authSource, su.SSHPublicKey) {
|
2021-07-24 15:46:34 +05:30
|
|
|
sshKeysNeedUpdate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user data has changed
|
|
|
|
if (len(source.AdminFilter) > 0 && usr.IsAdmin != su.IsAdmin) ||
|
|
|
|
(len(source.RestrictedFilter) > 0 && usr.IsRestricted != su.IsRestricted) ||
|
|
|
|
!strings.EqualFold(usr.Email, su.Mail) ||
|
|
|
|
usr.FullName != fullName ||
|
|
|
|
!usr.IsActive {
|
|
|
|
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Trace("SyncExternalUsers[%s]: Updating user %s", source.authSource.Name, usr.Name)
|
2021-07-24 15:46:34 +05:30
|
|
|
|
|
|
|
usr.FullName = fullName
|
2022-02-24 11:43:59 +05:30
|
|
|
emailChanged := usr.Email != su.Mail
|
2021-07-24 15:46:34 +05:30
|
|
|
usr.Email = su.Mail
|
|
|
|
// Change existing admin flag only if AdminFilter option is set
|
|
|
|
if len(source.AdminFilter) > 0 {
|
|
|
|
usr.IsAdmin = su.IsAdmin
|
|
|
|
}
|
|
|
|
// Change existing restricted flag only if RestrictedFilter option is set
|
|
|
|
if !usr.IsAdmin && len(source.RestrictedFilter) > 0 {
|
|
|
|
usr.IsRestricted = su.IsRestricted
|
|
|
|
}
|
|
|
|
usr.IsActive = true
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
err = user_model.UpdateUser(ctx, usr, emailChanged, "full_name", "email", "is_admin", "is_restricted", "is_active")
|
2021-07-24 15:46:34 +05:30
|
|
|
if err != nil {
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Error("SyncExternalUsers[%s]: Error updating user %s: %v", source.authSource.Name, usr.Name, err)
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
}
|
2021-09-27 08:09:36 +05:30
|
|
|
|
|
|
|
if usr.IsUploadAvatarChanged(su.Avatar) {
|
|
|
|
if err == nil && len(source.AttributeAvatar) > 0 {
|
2021-11-22 20:51:55 +05:30
|
|
|
_ = user_service.UploadAvatar(usr, su.Avatar)
|
2021-09-27 08:09:36 +05:30
|
|
|
}
|
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
2022-02-11 19:54:58 +05:30
|
|
|
// Synchronize LDAP groups with organization and team memberships
|
|
|
|
if source.GroupsEnabled && (source.GroupTeamMap != "" || source.GroupTeamMapRemoval) {
|
|
|
|
source.SyncLdapGroupsToTeams(usr, su.LdapTeamAdd, su.LdapTeamRemove, orgCache, teamCache)
|
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Rewrite authorized_keys file if LDAP Public SSH Key attribute is set and any key was added or removed
|
|
|
|
if sshKeysNeedUpdate {
|
2021-12-10 13:44:24 +05:30
|
|
|
err = asymkey_model.RewriteAllPublicKeys()
|
2021-07-24 15:46:34 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("RewriteAllPublicKeys: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Warn("SyncExternalUsers: Cancelled during update of %s before delete users", source.authSource.Name)
|
|
|
|
return db.ErrCancelledf("During update of %s before delete users", source.authSource.Name)
|
2021-07-24 15:46:34 +05:30
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deactivate users not present in LDAP
|
|
|
|
if updateExisting {
|
2021-09-15 14:58:37 +05:30
|
|
|
existPos := 0
|
|
|
|
for i, usr := range users {
|
|
|
|
for existPos < len(existingUsers) && i > existingUsers[existPos] {
|
|
|
|
existPos++
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
2021-09-15 14:58:37 +05:30
|
|
|
if usr.IsActive && (existPos >= len(existingUsers) || i < existingUsers[existPos]) {
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Trace("SyncExternalUsers[%s]: Deactivating user %s", source.authSource.Name, usr.Name)
|
2021-07-24 15:46:34 +05:30
|
|
|
|
|
|
|
usr.IsActive = false
|
2022-03-22 20:52:54 +05:30
|
|
|
err = user_model.UpdateUserCols(ctx, usr, "is_active")
|
2021-07-24 15:46:34 +05:30
|
|
|
if err != nil {
|
2022-01-02 18:42:35 +05:30
|
|
|
log.Error("SyncExternalUsers[%s]: Error deactivating user %s: %v", source.authSource.Name, usr.Name, err)
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|