{"record":{"id":"14fd77799ee7b3d7","repo":"moodle/moodle","slug":"auth-ldap-noconnect-all","errorCode":"auth_ldap_noconnect_all","errorMessage":"auth_ldap_noconnect_all","messagePattern":"auth_ldap_noconnect_all","errorType":"exception","errorClass":"moodle_exception","httpStatus":null,"severity":"critical","filePath":"public/auth/ldap/auth.php","lineNumber":2025,"sourceCode":"        // and can drain the TCP/IP ressources on the server if we\n        // are syncing a lot of users (as we try to open a new connection\n        // to get the user details). This is the least invasive way\n        // to reuse existing connections without greater code surgery.\n        if(!empty($this->ldapconnection)) {\n            $this->ldapconns++;\n            return $this->ldapconnection;\n        }\n\n        if($ldapconnection = ldap_connect_moodle($this->config->host_url, $this->config->ldap_version,\n                                                 $this->config->user_type, $this->config->bind_dn,\n                                                 $this->config->bind_pw, $this->config->opt_deref,\n                                                 $debuginfo, $this->config->start_tls)) {\n            $this->ldapconns = 1;\n            $this->ldapconnection = $ldapconnection;\n            return $ldapconnection;\n        }\n\n        throw new \\moodle_exception('auth_ldap_noconnect_all', 'auth_ldap', '', $debuginfo);\n    }\n\n    /**\n     * Disconnects from a LDAP server\n     *\n     * @param force boolean Forces closing the real connection to the LDAP server, ignoring any\n     *                      cached connections. This is needed when we've used paged results\n     *                      and want to use normal results again.\n     */\n    function ldap_close($force=false) {\n        $this->ldapconns--;\n        if (($this->ldapconns == 0) || ($force)) {\n            $this->ldapconns = 0;\n            @ldap_close($this->ldapconnection);\n            unset($this->ldapconnection);\n        }\n    }\n","sourceCodeStart":2007,"sourceCodeEnd":2043,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/auth/ldap/auth.php#L2007-L2043","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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`","Check $debuginfo embedded in the exception — it contains the precise bind/connection failure reason","Confirm the php-ldap extension is installed and enabled (php -m | grep ldap) and matches your PHP version","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)","Check for duplicate server URLs that cause retries to exhaust before this throw"],"exampleFix":"// before\n$CFG->auth_ldap_host_url = 'ldap://olddc.example.com'; // host decommissioned\n// after\n$CFG->auth_ldap_host_url = 'ldap://dc1.example.com ldap://dc2.example.com';","handlingStrategy":"try-catch","validationCode":"$hosts = preg_split('/\\s+/', trim(get_config('auth_ldap','host_url') ?? ''));\nforeach ($hosts as $host) {\n    $parts = parse_url(str_contains($host,'://') ? $host : 'ldap://'.$host);\n    $port = $parts['port'] ?? (str_starts_with($parts['scheme'] ?? '', 'ldaps') ? 636 : 389);\n    $c = @fsockopen($parts['host'], $port, $errno, $errstr, 3);\n    if (!$c) { debugging(\"LDAP unreachable: $host ($errstr)\"); continue; }\n    fclose($c);\n}\nif (!function_exists('ldap_connect')) { debugging('php-ldap extension missing'); }","typeGuard":"function ldapExtensionAvailable(): bool {\n    return extension_loaded('ldap') && function_exists('ldap_connect');\n}","tryCatchPattern":"try {\n    $conn = $auth->ldap_connect();\n} catch (moodle_exception $e) {\n    if ($e->errorcode === 'auth_ldap_noconnect_all') {\n        error_log('LDAP connect failed: ' . $e->debuginfo);\n        throw new availability_exception('ldap unavailable'); // degrade gracefully\n    }\n    throw $e;\n}","preventionTips":["Configure multiple host URLs for failover","Monitor directory reachability from web-server cron","After PHP upgrades, verify php-ldap is reinstalled/enabled","Check $e->debuginfo first — it names the exact bind/TLS failure","Use ldaps with trusted CA certs; test with ldapsearch from the web host"],"tags":["ldap","network","connection","moodle","bind"],"backgroundTag":"connection-refused","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}