| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
/* |
|---|
| 6 |
* Basic behaviour: |
|---|
| 7 |
* |
|---|
| 8 |
* Only if a user exists in LDAP authentication will be processed in this |
|---|
| 9 |
* module. In all other cases it will fall back to the internal method. |
|---|
| 10 |
* |
|---|
| 11 |
* To enable, set $CFG->auth = 'ldap' in config.php |
|---|
| 12 |
* |
|---|
| 13 |
* Configuration parameters in config.php: |
|---|
| 14 |
* |
|---|
| 15 |
* // LDAP host |
|---|
| 16 |
* $CFG->ldap_host = 'localhost'; |
|---|
| 17 |
* // LDAP port |
|---|
| 18 |
* $CFG->ldap_port = 389; |
|---|
| 19 |
* // Base DN |
|---|
| 20 |
* $CFG->ldap_basedn = 'dc=curverider,dc=co,dc=uk'; |
|---|
| 21 |
* // Bind as |
|---|
| 22 |
* $CFG->ldap_bind_dn = 'cn=admin,dc=curverider,dc=co,dc=uk'; |
|---|
| 23 |
* // Password for non anonymous bind |
|---|
| 24 |
* $CFG->ldap_bind_pwd = 'secret'; |
|---|
| 25 |
* // Protocol version |
|---|
| 26 |
* $CFG->ldap_protocol_version = 3; |
|---|
| 27 |
* // Filter for username, common are cn or uid |
|---|
| 28 |
* $CFG->ldap_filter_attr = 'uid'; |
|---|
| 29 |
* // Search attibutes (can be bassed as array or comma seperated string) |
|---|
| 30 |
* $CFG->ldap_search_attr = array('dn', 'ou', 'mail'); |
|---|
| 31 |
* // Create user, relies on the givenname, sn, and email attributes for now |
|---|
| 32 |
* $CFG->ldap_user_create = true; |
|---|
| 33 |
* // Fallback option, try internal authentication if everything fails |
|---|
| 34 |
* $CFG->ldap_internal_fallback = true |
|---|
| 35 |
*/ |
|---|
| 36 |
|
|---|
| 37 |
function ldap_authenticate_user_login($username, $password) { |
|---|
| 38 |
global $CFG, $messages; |
|---|
| 39 |
|
|---|
| 40 |
if (!function_exists(ldap_connect)) { |
|---|
| 41 |
$messages[] = 'No PHP LDAP module available, please contact the system administrator.'; |
|---|
| 42 |
return false; |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
if (!$CFG->ldap_host) { |
|---|
| 47 |
|
|---|
| 48 |
require_once($CFG->dirroot . 'auth/internal/lib.php'); |
|---|
| 49 |
return internal_authenticate_user_login($username, $password); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
if (!$CFG->ldap_port) { |
|---|
| 54 |
$CFG->ldap_port = 389; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
if (!$CFG->ldap_filter_attr) { |
|---|
| 59 |
$CFG->ldap_filter_attr = 'uid'; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
if (!$CFG->ldap_search_attr) { |
|---|
| 64 |
$CFG->ldap_search_attr = array('dn'); |
|---|
| 65 |
} |
|---|
| 66 |
else |
|---|
| 67 |
{ |
|---|
| 68 |
if (!is_array($CFG->ldap_search_attr)) |
|---|
| 69 |
{ |
|---|
| 70 |
|
|---|
| 71 |
$CFG->ldap_search_attr = explode(',', $CFG->ldap_search_attr); |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
$ds = @ldap_connect($CFG->ldap_host, $CFG->ldap_port); |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
$version = 3; |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
if ($CFG->ldap_protocol_version) { |
|---|
| 83 |
$version = $CFG->ldap_protocol_version; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
@ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $version); |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
$ldapbind = null; |
|---|
| 90 |
|
|---|
| 91 |
if ($ds) { |
|---|
| 92 |
if ($CFG->ldap_bind_dn != '') { |
|---|
| 93 |
$ldapbind = @ldap_bind($ds, $CFG->ldap_bind_dn, $CFG->ldap_bind_pwd); |
|---|
| 94 |
} else { |
|---|
| 95 |
|
|---|
| 96 |
$ldapbind = @ldap_bind($ds); |
|---|
| 97 |
} |
|---|
| 98 |
} else { |
|---|
| 99 |
|
|---|
| 100 |
$messages[] = 'Unable to bind to the LDAP server, please contact your system administrator. Error: '.ldap_error($ds); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
if ($ldapbind) { |
|---|
| 105 |
|
|---|
| 106 |
$sr = @ldap_search($ds, $CFG->ldap_basedn, $CFG->ldap_filter_attr ."=". $username, $CFG->ldap_search_attr); |
|---|
| 107 |
|
|---|
| 108 |
if ($sr) { |
|---|
| 109 |
$entry = ldap_get_entries($ds, $sr); |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
if ($entry) { |
|---|
| 113 |
if ($entry[0]) { |
|---|
| 114 |
|
|---|
| 115 |
if (@ldap_bind($ds, $entry[0]['dn'], $password) ) { |
|---|
| 116 |
|
|---|
| 117 |
//$messages[] = "Succesfull LDAP login for ".$entry[0]['dn']; |
|---|
| 118 |
|
|---|
| 119 |
// If we need to create the user |
|---|
| 120 |
if ($CFG->ldap_user_create == true) { |
|---|
| 121 |
|
|---|
| 122 |
if (!preg_match("/^[A-Za-z0-9]{3,12}$/",$username)) { |
|---|
| 123 |
$messages[] = __gettext("Error! Your username must contain letters and numbers only, cannot be blank, and must be between 3 and 12 characters in length."); |
|---|
| 124 |
} else { |
|---|
| 125 |
|
|---|
| 126 |
$username = strtolower($username); |
|---|
| 127 |
if (record_exists('users','username',$username)) { |
|---|
| 128 |
$messages[] = sprintf(__gettext("The username %s is already taken by another user. You will need to pick a different one."), $username); |
|---|
| 129 |
} else { |
|---|
| 130 |
|
|---|
| 131 |
$user = new StdClass; |
|---|
| 132 |
$user->email = $entry[0]["mail"][0]; |
|---|
| 133 |
$user->name = $entry[0]["givenname"][0]; |
|---|
| 134 |
$user->name = $user->name . " " . $entry[0]["sn"][0]; |
|---|
| 135 |
$user->username = $username; |
|---|
| 136 |
$user->password = md5($password); |
|---|
| 137 |
$user->user_type = 'person'; |
|---|
| 138 |
$user->owner = -1; |
|---|
| 139 |
|
|---|
| 140 |
$user_id = insert_record('users',$user); |
|---|
| 141 |
|
|---|
| 142 |
if (!empty($user_id)) { |
|---|
| 143 |
$rssresult = run("weblogs:rss:publish", array($uid, false)); |
|---|
| 144 |
$rssresult = run("files:rss:publish", array($uid, false)); |
|---|
| 145 |
$rssresult = run("profile:rss:publish", array($uid, false)); |
|---|
| 146 |
|
|---|
| 147 |
} else { |
|---|
| 148 |
|
|---|
| 149 |
$messages[] = sprintf(__gettext("User addition %d failed: Unknown reason, please contact you system administrator."), $username); |
|---|
| 150 |
} |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
ldap_close($ds); |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
return get_record_select('users',"username = ? AND active = ? AND user_type = ? ", |
|---|
| 160 |
array($username,'yes','person')); |
|---|
| 161 |
} else { |
|---|
| 162 |
|
|---|
| 163 |
$messages[] = 'Invalid credentials. LDAP error: '.ldap_error($ds); |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
ldap_close($ds); |
|---|
| 167 |
|
|---|
| 168 |
return false; |
|---|
| 169 |
} |
|---|
| 170 |
} else { |
|---|
| 171 |
|
|---|
| 172 |
ldap_close($ds); |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
if ($CFG->ldap_internal_fallback && $CFG->ldap_internal_fallback == true) { |
|---|
| 176 |
require_once($CFG->dirroot . 'auth/internal/lib.php'); |
|---|
| 177 |
|
|---|
| 178 |
return internal_authenticate_user_login($username, $password); |
|---|
| 179 |
} |
|---|
| 180 |
else |
|---|
| 181 |
{ |
|---|
| 182 |
return false; |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|
| 185 |
} |
|---|
| 186 |
} else { |
|---|
| 187 |
$messages[] = 'Unable to setup an LDAP connection, please contact your system administrator. LDAP error: '.ldap_error($ds); |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
ldap_close($ds); |
|---|
| 191 |
|
|---|
| 192 |
return false; |
|---|
| 193 |
} |
|---|
| 194 |
} else { |
|---|
| 195 |
$messages[] = 'Unable to bind to the LDAP server with your credentials, please contact your system administrator. LDAP error: '.ldap_error($ds); |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
ldap_close($ds); |
|---|
| 199 |
|
|---|
| 200 |
return false; |
|---|
| 201 |
} |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
function ldap_create_user($user) |
|---|
| 205 |
{ |
|---|
| 206 |
} |
|---|
| 207 |
?> |
|---|
| 208 |
|
|---|