summaryrefslogtreecommitdiff
path: root/src/web/helpers/ldap.php
blob: f3697cc6a6f06d8c122854411e92788b51f4391e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php /* Copyright (c) 2024 Freya Murphy */

function ldap_auth($auth_username, $auth_password) {
	$url = getenv("LDAP_URL");
	$bind = getenv("LDAP_BIND_DN");
	$password = getenv("LDAP_BIND_PASSWORD");
	$bound = getenv("LDAP_BASE_DN");
	$filter = getenv("LDAP_FILTER");
	$uid = getenv("LDAP_UID");

	$conn = @ldap_connect($url);
	if (!$conn) {
		return NULL;
	}
	ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);

	$bind_conn = @ldap_bind($conn, $bind, $password);
	if (!$bind_conn) {
		return NULL;
	}

	$search = @ldap_search($conn, $bound, $filter);

	$info = @ldap_get_entries($conn, $search);
	$user = NULL;
	for ($i=0; $i<$info['count']; $i++) {
		$user = $info[$i];
		if (!array_key_exists($uid, $user))
			continue;
		if ($user[$uid][0] == $auth_username)
			break;
	}

	if ($user == NULL) {
		return FALSE;
	}

	$succ = @ldap_bind($conn, $user['dn'], $auth_password);
	return !!$succ;
}