Commit 834383f3 authored by jan.koester's avatar jan.koester
Browse files

dirImportNative: merge native users that already exist in the directory

A native user whose name already exists as a directory user was skipped and
kept. The native login fallback then accepted its old password and created a
session for the NATIVE uid, which has none of the directory user's groups
(proxyplus: relay denied, groups=[] for noreply). Each such login also
counted a failed dirAuthenticate in LoginGuard.

Now the native sha512 hash is stored in the directory user's @webHash (the
directory password keeps working) and the native record is removed after
the group pass. admin.local is excluded, as is a directory user that already
has a different @webHash.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 67314fcc
Loading
Loading
Loading
Loading
Loading
+33 −1
Changes for plugin/ad/ad.cpp: 33 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -1011,7 +1011,39 @@ bool ActiveDirectory::dirImportNative(const std::string &domain, int &migrated,
            std::string sam = udat.getUsername();
            if (sam.empty()) continue;
            dsdb::Dn dn = users.child("CN", sam);
            if (d->dir->bySamAccountName(sam) || d->dir->byDn(dn)) {
            auto existingUser = d->dir->bySamAccountName(sam);
            if (!existingUser) existingUser = d->dir->byDn(dn);
            if (existingUser) {
                /* Same name already a directory user: merge instead of leaving a
                 * second account behind. Otherwise api.cpp's native fallback logs
                 * the old password in as the NATIVE uid -- a session without any
                 * of the directory user's groups (seen: proxyplus relay denied,
                 * groups=[]). The native sha512 hash goes to @webHash (the
                 * directory password keeps working too), the native record is
                 * dropped after the group pass. admin.local is left alone: its
                 * SuperAdmin is created natively on purpose (setup wizard). */
                std::string nativeHash;
                try { nativeHash = udat.getPwHash(); } catch (...) {}
                std::string dirHash = existingUser->str("@webHash");
                bool mergeable = !dsdb::iequals(domain, ADMIN_DOMAIN) && existingUser->isClass("user") &&
                                 !nativeHash.empty() && (dirHash.empty() || dirHash == nativeHash);
                if (mergeable) {
                    dsdb::AttrMap a = existingUser->attrs;
                    a["@webHash"] = {nativeHash};
                    const uuid::uuid dirGuid = existingUser->objectGuid;   /* entry may move on modify */
                    try {
                        d->dir->modify(dirGuid, a, nullptr, true);
                        migratedUids.push_back(uid);
                        ++migrated;
                        std::cerr << "[AD] dirImportNative: native user '" << sam << "' (" << uid.c_str()
                                  << ") merged into directory user " << dirGuid.c_str()
                                  << std::endl;
                        continue;
                    } catch (std::exception &e) {
                        std::cerr << "[AD] dirImportNative: merging native user '" << sam
                                  << "' failed: " << e.what() << std::endl;
                    }
                }
                if (skipped) skipped->push_back(sam);
                continue;
            }