Skip to content

A free open source IT asset/license management system

Added SASL incomatible warning

snipe199e17fde024
diff --git a/app/Models/Ldap.php b/app/Models/Ldap.php
index 59cb6706ce..6c3f532896 100644
--- a/app/Models/Ldap.php
+++ b/app/Models/Ldap.php
@@ -262,19 +262,50 @@ class Ldap extends Model
* Livewire component state, whatever) matches the auto-detect
* condition that routes bindAdminToLdap through SASL EXTERNAL:
* client cert + key are populated AND both bind DN and bind
- * password are blank.
+ * password are blank AND the current PHP build has SASL support.
*
* Runtime, LDAP troubleshooter, and LDAP Livewire wizard all
* evaluate the same.
*/
public static function shouldUseSaslExternal(object $config): bool
{
- return !empty($config->ldap_client_tls_cert)
+ return self::saslExternalAvailable()
+ && !empty($config->ldap_client_tls_cert)
&& !empty($config->ldap_client_tls_key)
&& empty($config->ldap_uname)
&& empty($config->ldap_pword);
}
+ /**
+ * True when the current PHP build was compiled with SASL support and
+ * ldap_sasl_bind() is callable. Wizard uses this to warn admins whose
+ * config would otherwise trigger SASL EXTERNAL but whose runtime
+ * cannot execute the bind.
+ *
+ * The optional test override is a seam for phpunit. The LdapTest
+ * suite mocks ldap_sasl_bind() at the namespace level to exercise
+ * the SASL branch on a CI PHP that was built without SASL, and
+ * function_exists() sits behind a language construct that php-mock
+ * can't reliably intercept. Test setUp calls setSaslExternalOverride(true)
+ * to force the check, tearDown resets to null.
+ */
+ public static function saslExternalAvailable(): bool
+ {
+ return self::$saslExternalOverride ?? function_exists('ldap_sasl_bind');
+ }
+
+ /**
+ * Test seam. Real callers never touch this. LdapTest uses it to
+ * force saslExternalAvailable() true/false regardless of what the
+ * CI PHP build actually supports. Pass null to reset.
+ */
+ public static function setSaslExternalOverride(?bool $available): void
+ {
+ self::$saslExternalOverride = $available;
+ }
+
+ private static ?bool $saslExternalOverride = null;
+
/**
* Binds/authenticates an admin to LDAP for LDAP searching/syncing.
* Modifies state on the passed-in $connection. Throws on any bind
@@ -284,10 +315,10 @@ class Ldap extends Model
* return value.
*
* @throws Exception on any bind failure
+ *
* @since [v3.0]
*
* @author [A. Gianotto] [<[email protected]>]
- *
*/
public static function bindAdminToLdap($connection): void
{
diff --git a/resources/lang/en-US/admin/settings/general.php b/resources/lang/en-US/admin/settings/general.php
index 89eb496057..dda7175883 100644
--- a/resources/lang/en-US/admin/settings/general.php
+++ b/resources/lang/en-US/admin/settings/general.php
@@ -107,6 +107,7 @@ return [
'ldap_uname_help' => 'The distinguished name (DN) or User Principal Name (UPN) of an admin service account that can search your directory. For Active Directory this is usually a UPN like <code>[email protected]</code>. For OpenLDAP and most other directories this is a full DN like <code>cn=admin,dc=example,dc=com</code>. Enter the entire path including the base DN portion, not just the account name.',
'ldap_pword_help' => 'Leave blank to keep the previously-saved password.',
'sasl_external_step2_hint' => 'A client TLS certificate is configured on the previous step. Leave the Bind DN/UPN and Bind Password fields blank to authenticate with the certificate (SASL EXTERNAL). Fill them in only if the server requires cert-plus-bind together.',
+ 'sasl_external_unavailable_hint' => 'A client TLS certificate is configured, but this PHP build was compiled without SASL support, so certificate-only authentication cannot be used on this server. Fill in the Bind DN/UPN and Bind Password below to authenticate with simple bind instead. Mutual TLS still applies at the transport layer, the cert and key just aren\'t used as the auth identity. For Google Workspace admins, a service account username and password can be generated in the Google Admin console.',
'bind' => [
'pword_decrypt_failed' => 'Could not decrypt the stored bind password. This usually means the app key changed since the password was saved. Enter the password again to fix.',
'rejected' => 'The LDAP server rejected the bind (:error). Double-check the username and password.',
diff --git a/resources/views/livewire/ldap-settings.blade.php b/resources/views/livewire/ldap-settings.blade.php
index df8ddecc79..a4fbd6822f 100644
--- a/resources/views/livewire/ldap-settings.blade.php
+++ b/resources/views/livewire/ldap-settings.blade.php
@@ -469,14 +469,19 @@
alongside. That was the friction that pushed us
to combine what used to be two separate steps. --}}
- {{-- Only renders when a client cert AND key are
- populated on step 1, which is the only case
- where leaving the bind fields blank does
- something meaningful. --}}
+ {{-- Only renders when a client cert AND key are populated on step 1.
+ When runtime lacks SASL support (via ldap_sasl_bind) the hint would actively
+ mislead admins into leaving bind fields blank for an auth path that can't execute. --}}
@if ($ldap_client_tls_cert !== '' && $ldap_client_tls_key !== '')
- <x-alert type="info" icon="tip">
- {{ trans('admin/settings/general.ldap_wizard.sasl_external_step2_hint') }}
- </x-alert>
+ @if (\App\Models\Ldap::saslExternalAvailable())
+ <x-alert type="info" icon="tip">
+ {{ trans('admin/settings/general.ldap_wizard.sasl_external_step2_hint') }}
+ </x-alert>
+ @else
+ <x-alert type="warning" icon="warning">
+ {!! trans('admin/settings/general.ldap_wizard.sasl_external_unavailable_hint') !!}
+ </x-alert>
+ @endif
@endif
<!-- Base Bind DN, placed first so users compose their
diff --git a/tests/Unit/LdapTest.php b/tests/Unit/LdapTest.php
index d48b142653..7f0b5fdbdf 100644
--- a/tests/Unit/LdapTest.php
+++ b/tests/Unit/LdapTest.php
@@ -94,6 +94,29 @@ class LdapTest extends TestCase
$this->assertNull(Ldap::bindAdminToLdap('dummy'));
}
+ /**
+ * Simulate a PHP build that has SASL support compiled in. Herd's
+ * default macOS PHP and some Docker images don't, and the fix for
+ * the "no SASL, hard crash" case gates shouldUseSaslExternal() on
+ * function_exists('ldap_sasl_bind'). The mock has to intercept that
+ * check so the SASL branch fires regardless of the CI PHP build.
+ */
+ private function mockSaslExternalAvailable(): void
+ {
+ // The Ldap model gates shouldUseSaslExternal() on
+ // function_exists('ldap_sasl_bind'), which php-mock can't
+ // reliably intercept in this namespace. Use the class's test
+ // seam instead to force availability on regardless of what
+ // the CI PHP build actually supports.
+ Ldap::setSaslExternalOverride(true);
+ }
+
+ protected function tearDown(): void
+ {
+ Ldap::setSaslExternalOverride(null);
+ parent::tearDown();
+ }
+
public function test_sasl_external_bind_when_cert_and_key_present_without_credentials()
{
// GH #19518: SASL EXTERNAL bind (auth via client TLS cert)
@@ -108,6 +131,7 @@ class LdapTest extends TestCase
'ldap_pword' => '',
]);
+ $this->mockSaslExternalAvailable();
$this->getFunctionMock('App\\Models', 'ldap_sasl_bind')
->expects($this->once())
->with('dummy', null, null, 'EXTERNAL')
@@ -128,6 +152,7 @@ class LdapTest extends TestCase
'ldap_pword' => '',
]);
+ $this->mockSaslExternalAvailable();
$this->getFunctionMock('App\\Models', 'ldap_sasl_bind')
->expects($this->once())
->willReturn(false);
@@ -143,6 +168,31 @@ class LdapTest extends TestCase
$this->assertNull(Ldap::bindAdminToLdap('dummy'));
}
+ public function test_sasl_external_falls_back_to_simple_bind_when_php_lacks_sasl_support()
+ {
+ // Guard against a PHP build compiled without SASL support (Herd's
+ // default, some Docker images). shouldUseSaslExternal() has to
+ // return false when ldap_sasl_bind is unavailable so the runtime
+ // doesn't hit a "Call to undefined function ldap_sasl_bind()"
+ // fatal. The wizard's warning banner covers the discoverability
+ // side. This test covers the runtime-guard side.
+ $this->settings->enableLdap();
+ $this->settings->set([
+ 'ldap_client_tls_cert' => 'CERT PEM',
+ 'ldap_client_tls_key' => 'KEY PEM',
+ 'ldap_uname' => '',
+ 'ldap_pword' => '',
+ ]);
+
+ Ldap::setSaslExternalOverride(false);
+
+ $this->getFunctionMock('App\\Models', 'ldap_sasl_bind')
+ ->expects($this->never());
+
+ $this->assertFalse(Ldap::shouldUseSaslExternal(Setting::getSettings()));
+ $this->assertFalse(Ldap::saslExternalAvailable());
+ }
+
public function test_simple_bind_still_used_when_credentials_are_present()
{
// Regression guard: cert + key populated but the user has ALSO