Skip to content

Commit 151179e

Browse files
committed
Add LDAP URI builder function for proper protocol and port handling
1 parent 967b1e0 commit 151179e

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

mailscanner/conf.php.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ define('DB_DSN', DB_TYPE . '://' . DB_USER . ':' . DB_PASS . '@' . DB_HOST . ':'
7272

7373
// LDAP settings for authentication
7474
define('USE_LDAP', false);
75-
define('LDAP_HOST', 'server.example.com');
76-
define('LDAP_PORT', '389');
75+
define('LDAP_HOST', 'ldap://server.example.com'); // Host or LDAP Uri, use ldaps:// for SSL connection
76+
define('LDAP_PORT', '389'); // LDAP port number
7777
define('LDAP_DN', 'DC=example,DC=com');
7878
define('LDAP_USER', '[email protected]'); // If no email set: cn=admin,dc=example,dc=com
7979
define('LDAP_PASS', 'secret');

mailscanner/functions.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2944,6 +2944,35 @@ function address_filter_sql($addresses, $type)
29442944
return $sqladdr;
29452945
}
29462946

2947+
/**
2948+
* Constructs an LDAP URI using the given host and port.
2949+
*
2950+
* If the host doesn't already include a protocol, it will be prefixed with "ldaps://" when the port is "636",
2951+
* otherwise with "ldap://". If the URI doesn't already contain a port, the port is appended.
2952+
*
2953+
* @param string $host The LDAP host.
2954+
* @param int|string $port The LDAP port.
2955+
* @return string The constructed LDAP URI.
2956+
*/
2957+
function ldap_build_uri($host, $port) {
2958+
// Convert the port to a string immediately
2959+
$portStr = (string)$port;
2960+
2961+
// If the host doesn't already start with "ldap://" or "ldaps://", prepend the appropriate protocol
2962+
if (stripos($host, 'ldap://') !== 0 && stripos($host, 'ldaps://') !== 0) {
2963+
$protocol = ($portStr === '636') ? 'ldaps://' : 'ldap://';
2964+
$host = $protocol . $host;
2965+
}
2966+
2967+
// Use parse_url to check if the URI already includes a port
2968+
$parsedUrl = parse_url($host);
2969+
if ($portStr && !isset($parsedUrl['port'])) {
2970+
$host .= ':' . $portStr;
2971+
}
2972+
2973+
return $host;
2974+
}
2975+
29472976
/**
29482977
* @param string $username
29492978
* @param string $password
@@ -2954,7 +2983,8 @@ function ldap_authenticate($username, $password)
29542983
{
29552984
$username = ldap_escape(strtolower($username), '', LDAP_ESCAPE_DN);
29562985
if ('' !== $username && '' !== $password) {
2957-
$ds = ldap_connect(LDAP_HOST, LDAP_PORT) or exit(__('ldpaauth103') . ' ' . LDAP_HOST);
2986+
$ldap_uri = ldap_build_uri(LDAP_HOST, LDAP_PORT);
2987+
$ds = ldap_connect($ldap_uri) or exit(__('ldpaauth103') . ' ' . $ldap_uri);
29582988

29592989
$ldap_protocol_version = 3;
29602990
if (defined('LDAP_PROTOCOL_VERSION')) {
@@ -3172,8 +3202,9 @@ function ldap_get_conf_var($entry)
31723202
// Translate MailScanner.conf vars to internal
31733203
$entry = translate_etoi($entry);
31743204

3175-
$lh = ldap_connect(LDAP_HOST, LDAP_PORT)
3176-
or exit(__('ldapgetconfvar103') . ' ' . LDAP_HOST . "\n");
3205+
$ldap_uri = ldap_build_uri(LDAP_HOST, LDAP_PORT);
3206+
$lh = ldap_connect($ldap_uri)
3207+
or exit(__('ldapgetconfvar103') . ' ' . $ldap_uri . "\n");
31773208

31783209
@ldap_bind($lh)
31793210
or exit(__('ldapgetconfvar203') . "\n");
@@ -3212,8 +3243,9 @@ function ldap_get_conf_truefalse($entry)
32123243
// Translate MailScanner.conf vars to internal
32133244
$entry = translate_etoi($entry);
32143245

3215-
$lh = ldap_connect(LDAP_HOST, LDAP_PORT)
3216-
or exit(__('ldapgetconfvar103') . ' ' . LDAP_HOST . "\n");
3246+
$ldap_uri = ldap_build_uri(LDAP_HOST, LDAP_PORT);
3247+
$lh = ldap_connect($ldap_uri)
3248+
or exit(__('ldapgetconfvar103') . ' ' . $ldap_uri . "\n");
32173249

32183250
@ldap_bind($lh)
32193251
or exit(__('ldapgetconfvar203') . "\n");

tools/LDAP/ldaptest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
// $verbose = true;
4242

4343
echo 'Test connection to server' . PHP_EOL;
44-
$ds = ldap_connect(LDAP_HOST, LDAP_PORT) or exit('Connection to server failed');
44+
$ldap_uri = ldap_build_uri(LDAP_HOST, LDAP_PORT);
45+
$ds = ldap_connect($ldap_uri) or exit(sprintf('Connection to server "%s" failed', $ldap_uri));
4546

4647
$ldap_protocol_version = 3;
4748
if (defined('LDAP_PROTOCOL_VERSION')) {

0 commit comments

Comments
 (0)