Issues (1963)

html/inc/ldap.inc (1 issue)

Severity
1
<?php
2
3
// This file is part of BOINC.
4
// http://boinc.berkeley.edu
5
// Copyright (C) 2014 University of California
6
//
7
// BOINC is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License
9
// as published by the Free Software Foundation,
10
// either version 3 of the License, or (at your option) any later version.
11
//
12
// BOINC is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
// See the GNU Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
19
20
// LDAP authentication.
21
// returns (user, error_msg) where user is an object with
22
// $user->name
23
// $user->email_addr
24
//
25
// your project.inc must define LDAP_HOST and LDAP_BASE_DN
26
//
27
function ldap_auth($uid, $passwd) {
28
    $ad = @ldap_connect(LDAP_HOST);
29
    if (!$ad) {
0 ignored issues
show
$ad is of type false|resource, thus it always evaluated to false.
Loading history...
30
        return array(null, "Can't connect to LDAP server");
31
    }
32
    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
33
    ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);
34
    $rn = "uid=$uid,".LDAP_BASE_DN;
35
    $r = @ldap_bind($ad, $rn, $passwd);
36
    if (!$r) {
37
        return array(null, "Invalid credentials");
38
    }
39
    $x = ldap_search($ad, $rn, "(objectclass=*)");
40
    $x = ldap_get_entries($ad, $x);
41
42
    $user = new StdClass;
43
    $x = $x[0];
44
    $name = $x["cn"];
45
    $user->name = $name[0];
46
    $email = $x["mail"];
47
    $user->email_addr = $email[0];
48
    return array($user, null);
49
}
50
51
// for LDAP-authenticated users, we store LDAP:uid in user.email_addr
52
//
53
function ldap_email_string($uid) {
54
    return "LDAP:$uid";
55
}
56
57
function is_ldap_email($x) {
58
    return (substr($x, 0, 5) == "LDAP:");
59
}
60
61
function ldap_email_to_uid($x) {
62
    return substr($x, 5);
63
}
64
65
function example_usage() {
66
    list($user, $error_msg) = ldap_auth("davea", "xxx");
67
    if ($error_msg) {
68
        echo "error: $error_msg\n";
69
    } else {
70
        echo "name: $user->name; email: $user->email_addr\n";
71
    }
72
}
73
74
?>
75