2021-07-24 10:16:34 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-07-24 10:16:34 +00:00
|
|
|
|
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-12-10 08:14:24 +00:00
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2021-11-10 05:13:16 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 06:29:02 +00:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2023-02-08 06:44:42 +00:00
|
|
|
auth_module "code.gitea.io/gitea/modules/auth"
|
2023-08-30 06:55:25 +00:00
|
|
|
"code.gitea.io/gitea/modules/container"
|
2021-07-24 10:16:34 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-02-04 13:29:09 +00:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2023-02-08 06:44:42 +00:00
|
|
|
source_service "code.gitea.io/gitea/services/auth/source"
|
2021-11-22 15:21:55 +00:00
|
|
|
user_service "code.gitea.io/gitea/services/user"
|
2021-07-24 10:16:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 13:12:35 +00:00
|
|
|
log.Trace("Doing: SyncExternalUsers[%s]", source.authSource.Name)
|
2021-07-24 10:16:34 +00:00
|
|
|
|
|
|
|
isAttributeSSHPublicKeySet := len(strings.TrimSpace(source.AttributeSSHPublicKey)) > 0
|
|
|
|
var sshKeysNeedUpdate bool
|
|
|
|
|
|
|
|
// Find all users with this login type - FIXME: Should this be an iterator?
|
2023-09-14 17:09:32 +00:00
|
|
|
users, err := user_model.GetUsersBySource(ctx, source.authSource)
|
2021-07-24 10:16:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("SyncExternalUsers: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-01-02 13:12:35 +00:00
|
|
|
log.Warn("SyncExternalUsers: Cancelled before update of %s", source.authSource.Name)
|
|
|
|
return db.ErrCancelledf("Before update of %s", source.authSource.Name)
|
2021-07-24 10:16:34 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2023-06-20 03:04:13 +00:00
|
|
|
usernameUsers := make(map[string]*user_model.User, len(users))
|
|
|
|
mailUsers := make(map[string]*user_model.User, len(users))
|
2023-08-30 06:55:25 +00:00
|
|
|
keepActiveUsers := make(container.Set[int64])
|
2023-06-20 03:04:13 +00:00
|
|
|
|
|
|
|
for _, u := range users {
|
|
|
|
usernameUsers[u.LowerName] = u
|
|
|
|
mailUsers[strings.ToLower(u.Email)] = u
|
|
|
|
}
|
2021-09-15 09:28:37 +00:00
|
|
|
|
2021-07-24 10:16:34 +00:00
|
|
|
sr, err := source.SearchEntries()
|
|
|
|
if err != nil {
|
2022-01-02 13:12:35 +00:00
|
|
|
log.Error("SyncExternalUsers LDAP source failure [%s], skipped", source.authSource.Name)
|
2021-07-24 10:16:34 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-03-29 06:29:02 +00:00
|
|
|
orgCache := make(map[string]*organization.Organization)
|
|
|
|
teamCache := make(map[string]*organization.Team)
|
2021-09-15 09:28:37 +00:00
|
|
|
|
2023-02-08 06:44:42 +00:00
|
|
|
groupTeamMapping, err := auth_module.UnmarshalGroupTeamMapping(source.GroupTeamMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-24 10:16:34 +00:00
|
|
|
for _, su := range sr {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-01-02 13:12:35 +00:00
|
|
|
log.Warn("SyncExternalUsers: Cancelled at update of %s before completed update of users", source.authSource.Name)
|
2021-07-24 10:16:34 +00:00
|
|
|
// Rewrite authorized_keys file if LDAP Public SSH Key attribute is set and any key was added or removed
|
|
|
|
if sshKeysNeedUpdate {
|
2023-09-25 13:17:37 +00:00
|
|
|
err = asymkey_model.RewriteAllPublicKeys(ctx)
|
2021-07-24 10:16:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("RewriteAllPublicKeys: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-02 13:12:35 +00:00
|
|
|
return db.ErrCancelledf("During update of %s before completed update of users", source.authSource.Name)
|
2021-07-24 10:16:34 +00:00
|
|
|
default:
|
|
|
|
}
|
2023-06-20 03:04:13 +00:00
|
|
|
if len(su.Username) == 0 && len(su.Mail) == 0 {
|
2021-07-24 10:16:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-20 03:04:13 +00:00
|
|
|
var usr *user_model.User
|
|
|
|
if len(su.Username) > 0 {
|
|
|
|
usr = usernameUsers[su.LowerName]
|
|
|
|
}
|
|
|
|
if usr == nil && len(su.Mail) > 0 {
|
|
|
|
usr = mailUsers[strings.ToLower(su.Mail)]
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 03:04:13 +00:00
|
|
|
if usr != nil {
|
2023-08-30 06:55:25 +00:00
|
|
|
keepActiveUsers.Add(usr.ID)
|
2023-06-20 03:04:13 +00:00
|
|
|
} else if len(su.Username) == 0 {
|
|
|
|
// we cannot create the user if su.Username is empty
|
|
|
|
continue
|
2021-09-15 09:28:37 +00:00
|
|
|
}
|
2023-06-20 03:04:13 +00:00
|
|
|
|
|
|
|
if len(su.Mail) == 0 {
|
2023-06-22 08:16:12 +00:00
|
|
|
su.Mail = fmt.Sprintf("%s@localhost.local", su.Username)
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fullName := composeFullName(su.Name, su.Surname, su.Username)
|
|
|
|
// If no existing user found, create one
|
|
|
|
if usr == nil {
|
2022-01-02 13:12:35 +00:00
|
|
|
log.Trace("SyncExternalUsers[%s]: Creating user %s", source.authSource.Name, su.Username)
|
2021-07-24 10:16:34 +00:00
|
|
|
|
2021-11-24 09:49:20 +00:00
|
|
|
usr = &user_model.User{
|
2022-04-29 19:38:11 +00:00
|
|
|
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{
|
2024-02-23 02:18:33 +00:00
|
|
|
IsRestricted: optional.Some(su.IsRestricted),
|
|
|
|
IsActive: optional.Some(true),
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 17:09:32 +00:00
|
|
|
err = user_model.CreateUser(ctx, usr, overwriteDefault)
|
2021-07-24 10:16:34 +00:00
|
|
|
if err != nil {
|
2022-01-02 13:12:35 +00:00
|
|
|
log.Error("SyncExternalUsers[%s]: Error creating user %s: %v", source.authSource.Name, su.Username, err)
|
2021-09-27 02:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && isAttributeSSHPublicKeySet {
|
2022-01-02 13:12:35 +00:00
|
|
|
log.Trace("SyncExternalUsers[%s]: Adding LDAP Public SSH Keys for user %s", source.authSource.Name, usr.Name)
|
2023-10-11 04:24:07 +00:00
|
|
|
if asymkey_model.AddPublicKeysBySource(ctx, usr, source.authSource, su.SSHPublicKey) {
|
2021-07-24 10:16:34 +00:00
|
|
|
sshKeysNeedUpdate = true
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 02:39:36 +00:00
|
|
|
|
|
|
|
if err == nil && len(source.AttributeAvatar) > 0 {
|
2023-10-11 04:24:07 +00:00
|
|
|
_ = user_service.UploadAvatar(ctx, usr, su.Avatar)
|
2021-09-27 02:39:36 +00:00
|
|
|
}
|
2021-07-24 10:16:34 +00:00
|
|
|
} else if updateExisting {
|
|
|
|
// Synchronize SSH Public Key if that attribute is set
|
2023-10-11 04:24:07 +00:00
|
|
|
if isAttributeSSHPublicKeySet && asymkey_model.SynchronizePublicKeys(ctx, usr, source.authSource, su.SSHPublicKey) {
|
2021-07-24 10:16:34 +00:00
|
|
|
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 13:12:35 +00:00
|
|
|
log.Trace("SyncExternalUsers[%s]: Updating user %s", source.authSource.Name, usr.Name)
|
2021-07-24 10:16:34 +00:00
|
|
|
|
2024-02-04 13:29:09 +00:00
|
|
|
opts := &user_service.UpdateOptions{
|
|
|
|
FullName: optional.Some(fullName),
|
|
|
|
IsActive: optional.Some(true),
|
|
|
|
}
|
|
|
|
if source.AdminFilter != "" {
|
|
|
|
opts.IsAdmin = optional.Some(su.IsAdmin)
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
|
|
|
// Change existing restricted flag only if RestrictedFilter option is set
|
2024-02-04 13:29:09 +00:00
|
|
|
if !su.IsAdmin && source.RestrictedFilter != "" {
|
|
|
|
opts.IsRestricted = optional.Some(su.IsRestricted)
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 13:29:09 +00:00
|
|
|
if err := user_service.UpdateUser(ctx, usr, opts); err != nil {
|
2022-01-02 13:12:35 +00:00
|
|
|
log.Error("SyncExternalUsers[%s]: Error updating user %s: %v", source.authSource.Name, usr.Name, err)
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
2024-02-04 13:29:09 +00:00
|
|
|
|
|
|
|
if err := user_service.ReplacePrimaryEmailAddress(ctx, usr, su.Mail); err != nil {
|
|
|
|
log.Error("SyncExternalUsers[%s]: Error updating user %s primary email %s: %v", source.authSource.Name, usr.Name, su.Mail, err)
|
|
|
|
}
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
2021-09-27 02:39:36 +00:00
|
|
|
|
|
|
|
if usr.IsUploadAvatarChanged(su.Avatar) {
|
|
|
|
if err == nil && len(source.AttributeAvatar) > 0 {
|
2023-10-11 04:24:07 +00:00
|
|
|
_ = user_service.UploadAvatar(ctx, usr, su.Avatar)
|
2021-09-27 02:39:36 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
2022-02-11 14:24:58 +00:00
|
|
|
// Synchronize LDAP groups with organization and team memberships
|
|
|
|
if source.GroupsEnabled && (source.GroupTeamMap != "" || source.GroupTeamMapRemoval) {
|
2023-02-08 06:44:42 +00:00
|
|
|
if err := source_service.SyncGroupsToTeamsCached(ctx, usr, su.Groups, groupTeamMapping, source.GroupTeamMapRemoval, orgCache, teamCache); err != nil {
|
|
|
|
log.Error("SyncGroupsToTeamsCached: %v", err)
|
|
|
|
}
|
2022-02-11 14:24:58 +00:00
|
|
|
}
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rewrite authorized_keys file if LDAP Public SSH Key attribute is set and any key was added or removed
|
|
|
|
if sshKeysNeedUpdate {
|
2023-09-25 13:17:37 +00:00
|
|
|
err = asymkey_model.RewriteAllPublicKeys(ctx)
|
2021-07-24 10:16:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("RewriteAllPublicKeys: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-01-02 13:12:35 +00:00
|
|
|
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 10:16:34 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deactivate users not present in LDAP
|
|
|
|
if updateExisting {
|
2023-06-20 03:04:13 +00:00
|
|
|
for _, usr := range users {
|
2023-08-30 06:55:25 +00:00
|
|
|
if keepActiveUsers.Contains(usr.ID) {
|
2023-06-20 03:04:13 +00:00
|
|
|
continue
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 03:04:13 +00:00
|
|
|
log.Trace("SyncExternalUsers[%s]: Deactivating user %s", source.authSource.Name, usr.Name)
|
|
|
|
|
2024-02-04 13:29:09 +00:00
|
|
|
opts := &user_service.UpdateOptions{
|
|
|
|
IsActive: optional.Some(false),
|
|
|
|
}
|
|
|
|
if err := user_service.UpdateUser(ctx, usr, opts); err != nil {
|
2023-06-20 03:04:13 +00:00
|
|
|
log.Error("SyncExternalUsers[%s]: Error deactivating user %s: %v", source.authSource.Name, usr.Name, err)
|
2021-07-24 10:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|