moodle/moodle · critical · moodle_exception

auth_ldap_noconnect_all

auth_ldap_noconnect_all

Error message

auth_ldap_noconnect_all

What it means

ldap_connect() failed to establish ANY usable connection to the configured LDAP server(s) (bind failed or ldap_connect returned false), so the plugin throws auth_ldap_noconnect_all. All LDAP operations (login, user lookup, create, sync, password expiry) funnel through this method, so any of them surfaces this error when the directory is unreachable or credentials are wrong.

Solutions

  1. Verify the LDAP server is reachable: from the web server run `ldapsearch -x -H ldap://host:389 -D 'binddn' -w pass` or test port with `nc -vz host 389`
  2. Check $debuginfo embedded in the exception — it contains the precise bind/connection failure reason
  3. Confirm the php-ldap extension is installed and enabled (php -m | grep ldap) and matches your PHP version
  4. Validate host URLs, bind DN/password, start_tls and ldaps settings in the auth_ldap config; for ldaps, ensure CA certs are trusted (LDAPTLS_CACERT)
  5. Check for duplicate server URLs that cause retries to exhaust before this throw

Example fix

// before
$CFG->auth_ldap_host_url = 'ldap://olddc.example.com'; // host decommissioned
// after
$CFG->auth_ldap_host_url = 'ldap://dc1.example.com ldap://dc2.example.com';
Defensive patterns

Strategy: try-catch

Validate before calling

$hosts = preg_split('/\s+/', trim(get_config('auth_ldap','host_url') ?? ''));
foreach ($hosts as $host) {
    $parts = parse_url(str_contains($host,'://') ? $host : 'ldap://'.$host);
    $port = $parts['port'] ?? (str_starts_with($parts['scheme'] ?? '', 'ldaps') ? 636 : 389);
    $c = @fsockopen($parts['host'], $port, $errno, $errstr, 3);
    if (!$c) { debugging("LDAP unreachable: $host ($errstr)"); continue; }
    fclose($c);
}
if (!function_exists('ldap_connect')) { debugging('php-ldap extension missing'); }

Type guard

function ldapExtensionAvailable(): bool {
    return extension_loaded('ldap') && function_exists('ldap_connect');
}

Try / catch

try {
    $conn = $auth->ldap_connect();
} catch (moodle_exception $e) {
    if ($e->errorcode === 'auth_ldap_noconnect_all') {
        error_log('LDAP connect failed: ' . $e->debuginfo);
        throw new availability_exception('ldap unavailable'); // degrade gracefully
    }
    throw $e;
}

Prevention

When it happens

Trigger: Any call that needs the directory — user_login, get_userinfo, user_create, password_expire, sync_users_update_callback, user_activate — when: server host/port wrong, LDAP PHP extension not installed, bind DN/password invalid, TLS/start_tls handshake fails, or network/firewall blocks the port.

Common situations: LDAP server down or moved; wrong 'host URL' setting (ldaps:// vs ldap:// and port 636 vs 389); bind user password rotated; missing php-ldap extension after a PHP upgrade; certificate verification failure on ldaps; firewall between web server and DC.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/14fd77799ee7b3d7. Report an issue: GitHub.

Appendix: source

Thrown at public/auth/ldap/auth.php:2025

        // and can drain the TCP/IP ressources on the server if we
        // are syncing a lot of users (as we try to open a new connection
        // to get the user details). This is the least invasive way
        // to reuse existing connections without greater code surgery.
        if(!empty($this->ldapconnection)) {
            $this->ldapconns++;
            return $this->ldapconnection;
        }

        if($ldapconnection = ldap_connect_moodle($this->config->host_url, $this->config->ldap_version,
                                                 $this->config->user_type, $this->config->bind_dn,
                                                 $this->config->bind_pw, $this->config->opt_deref,
                                                 $debuginfo, $this->config->start_tls)) {
            $this->ldapconns = 1;
            $this->ldapconnection = $ldapconnection;
            return $ldapconnection;
        }

        throw new \moodle_exception('auth_ldap_noconnect_all', 'auth_ldap', '', $debuginfo);
    }

    /**
     * Disconnects from a LDAP server
     *
     * @param force boolean Forces closing the real connection to the LDAP server, ignoring any
     *                      cached connections. This is needed when we've used paged results
     *                      and want to use normal results again.
     */
    function ldap_close($force=false) {
        $this->ldapconns--;
        if (($this->ldapconns == 0) || ($force)) {
            $this->ldapconns = 0;
            @ldap_close($this->ldapconnection);
            unset($this->ldapconnection);
        }
    }

View on GitHub (pinned to 48ea8c3322)