A free open source IT asset/license management system
Active Directory: Fixed #19999 - switch to bitwise for activation check
snipe54d3a66ccd5a
diff --git a/app/Console/Commands/LdapSync.php b/app/Console/Commands/LdapSync.phpindex cdfe725432..0c3f41de71 100644--- a/app/Console/Commands/LdapSync.php+++ b/app/Console/Commands/LdapSync.php@@ -394,9 +394,9 @@ class LdapSync extends Command}// Sync activated state for Active Directory.- if (! empty($ldap_map['activated'])) { // IF we have an 'active' flag set....- // ....then *most* things that are truthy will activate the user. Anything falsey will deactivate them.- // (Specifically, we don't handle a value of '0.0' correctly)+ if (!empty($ldap_map['activated'])) {+ // IF we have an 'active' flag set, then *most* things that are truthy will activate the user.+ // Anything falsey will deactivate them. (Specifically, we don't handle a value of '0.0' correctly)$raw_value = @$results[$i][$ldap_map['activated']][0];$filter_var = filter_var($raw_value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);@@ -410,42 +410,10 @@ class LdapSync extends Command}} elseif (array_key_exists('useraccountcontrol', $results[$i])) {- // ....otherwise, (ie if no 'active' LDAP flag is defined), IF the UAC setting exists,- // ....then use the UAC setting on the account to determine can-log-in vs. cannot-log-in-- /* The following is _probably_ the correct logic, but we can't use it because- some users may have been dependent upon the previous behavior, and this- could cause additional access to be available to users they don't want- to allow to log in.-- $useraccountcontrol = $results[$i]['useraccountcontrol'][0];- if(- // based on MS docs at: https://support.microsoft.com/en-us/help/305144/how-to-use-useraccountcontrol-to-manipulate-user-account-properties- ($useraccountcontrol & 0x200) && // is a NORMAL_ACCOUNT- !($useraccountcontrol & 0x02) && // *and* _not_ ACCOUNTDISABLE- !($useraccountcontrol & 0x10) // *and* _not_ LOCKOUT- ) {- $user->activated = 1;- } else {- $user->activated = 0;- } */- $enabled_accounts = [- '512', // 0x200 NORMAL_ACCOUNT- '544', // 0x220 NORMAL_ACCOUNT, PASSWD_NOTREQD- '66048', // 0x10200 NORMAL_ACCOUNT, DONT_EXPIRE_PASSWORD- '66080', // 0x10220 NORMAL_ACCOUNT, PASSWD_NOTREQD, DONT_EXPIRE_PASSWORD- '262656', // 0x40200 NORMAL_ACCOUNT, SMARTCARD_REQUIRED- '262688', // 0x40220 NORMAL_ACCOUNT, PASSWD_NOTREQD, SMARTCARD_REQUIRED- '328192', // 0x50200 NORMAL_ACCOUNT, SMARTCARD_REQUIRED, DONT_EXPIRE_PASSWORD- '328224', // 0x50220 NORMAL_ACCOUNT, PASSWD_NOT_REQD, SMARTCARD_REQUIRED, DONT_EXPIRE_PASSWORD- '4194816', // 0x400200 NORMAL_ACCOUNT, DONT_REQ_PREAUTH- '4260352', // 0x410200 NORMAL_ACCOUNT, DONT_EXPIRE_PASSWORD, DONT_REQ_PREAUTH- '1049088', // 0x100200 NORMAL_ACCOUNT, NOT_DELEGATED- '1114624', // 0x110200 NORMAL_ACCOUNT, DONT_EXPIRE_PASSWORD, NOT_DELEGATED,- ];- $user->activated = (in_array($results[$i]['useraccountcontrol'][0], $enabled_accounts)) ? 1 : 0;+ // No explicit ldap_active_flag is configured, but the LDAP+ // row carries useraccountcontrol which implies AD.+ $user->activated = self::isActivatedByUserAccountControl((int) $results[$i]['useraccountcontrol'][0]) ? 1 : 0;- // If we're not using AD, and there isn't an activated flag set, activate all users} /* implied 'else' here - leave the $user->activated flag alone. Newly-created accounts will be active.already-existing accounts will be however the administrator has set them */@@ -587,4 +555,21 @@ class LdapSync extends Commandreturn User::verifyExactUsernameMatch($user, $username);}++ /**+ * Translate an Active Directory userAccountControl integer into+ * a boolean "is this account activated" per Microsoft's documented+ * semantics. See https://learn.microsoft.com/windows/win32/adschema/a-useraccountcontrol.+ *+ * A user is activated when the UAC value has the NORMAL_ACCOUNT+ * bit set AND the ACCOUNTDISABLED and LOCKOUT bits unset.+ */+ public static function isActivatedByUserAccountControl(int $useraccountcontrol): bool+ {+ $isNormalAccount = ($useraccountcontrol & 0x200) !== 0;+ $isDisabled = ($useraccountcontrol & 0x2) !== 0;+ $isLockedOut = ($useraccountcontrol & 0x10) !== 0;++ return $isNormalAccount && !$isDisabled && !$isLockedOut;+ }}diff --git a/tests/Unit/LdapSyncUserAccountControlTest.php b/tests/Unit/LdapSyncUserAccountControlTest.phpnew file mode 100644index 0000000000..9607f2839c--- /dev/null+++ b/tests/Unit/LdapSyncUserAccountControlTest.php@@ -0,0 +1,71 @@+<?php++namespace Tests\Unit;++use App\Console\Commands\LdapSync;+use PHPUnit\Framework\Attributes\DataProvider;+use PHPUnit\Framework\TestCase;++/**+ * Regression and conformance coverage for the Active Directory+ * userAccountControl → activated translation (#19999). The old code+ * used a hardcoded whitelist of 12 integer values which silently+ * deactivated legitimate users whose flag combination fell outside+ * that list. The current implementation does a bitwise check per+ * Microsoft's documented semantics.+ *+ * Reference flag bits:+ * NORMAL_ACCOUNT 0x200 (= 512)+ * ACCOUNTDISABLED 0x2 (= 2)+ * LOCKOUT 0x10 (= 16)+ * PASSWD_NOTREQD 0x20+ * SMARTCARD_REQUIRED 0x40000 (= 262144)+ * DONT_EXPIRE_PASSWORD 0x10000 (= 65536)+ * NOT_DELEGATED 0x100000 (= 1048576)+ * DONT_REQ_PREAUTH 0x400000 (= 4194304)+ */+class LdapSyncUserAccountControlTest extends TestCase+{+ public static function activatedCases(): array+ {+ return [+ 'plain NORMAL_ACCOUNT (0x200)' => [0x200, true],+ 'NORMAL_ACCOUNT | DONT_EXPIRE_PASSWORD' => [0x10200, true],+ 'NORMAL_ACCOUNT | SMARTCARD_REQUIRED' => [0x40200, true],+ 'NORMAL_ACCOUNT | NOT_DELEGATED' => [0x100200, true],+ 'issue #19999: NORMAL_ACCOUNT | SMARTCARD_REQUIRED | NOT_DELEGATED' => [0x140200, true],+ 'NORMAL_ACCOUNT | DONT_EXPIRE_PASSWORD | NOT_DELEGATED | SMARTCARD_REQUIRED' => [0x150200, true],+ 'NORMAL_ACCOUNT | PASSWD_NOTREQD' => [0x220, true],+ ];+ }++ public static function deactivatedCases(): array+ {+ return [+ 'zero (no bits set at all)' => [0, false],+ 'ACCOUNTDISABLED alone' => [0x2, false],+ 'NORMAL_ACCOUNT | ACCOUNTDISABLED' => [0x202, false],+ 'NORMAL_ACCOUNT | DONT_EXPIRE_PASSWORD | ACCOUNTDISABLED' => [0x10202, false],+ 'NORMAL_ACCOUNT | LOCKOUT' => [0x210, false],+ 'NORMAL_ACCOUNT | ACCOUNTDISABLED | LOCKOUT' => [0x212, false],+ // Service accounts (WORKSTATION_TRUST_ACCOUNT = 0x1000) are+ // not NORMAL_ACCOUNT and therefore not activated as users.+ 'WORKSTATION_TRUST_ACCOUNT (service account, not a user)' => [0x1000, false],+ // SMARTCARD_REQUIRED alone without NORMAL_ACCOUNT is not a+ // normal user either and must not activate.+ 'SMARTCARD_REQUIRED without NORMAL_ACCOUNT' => [0x40000, false],+ ];+ }++ #[DataProvider('activatedCases')]+ public function test_activated_cases(int $uac, bool $expected): void+ {+ $this->assertSame($expected, LdapSync::isActivatedByUserAccountControl($uac));+ }++ #[DataProvider('deactivatedCases')]+ public function test_deactivated_cases(int $uac, bool $expected): void+ {+ $this->assertSame($expected, LdapSync::isActivatedByUserAccountControl($uac));+ }+}