-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.jdap.php
226 lines (214 loc) · 8.31 KB
/
class.jdap.php
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
include_once "class.smbhash.php";
class jdap {
/*
* The class always returns an array with at least the following keys:
* [error] : the error code (0 = success)
* [errormsg]: the error message
* [usermsf] : a user friendly feedback message
*/
private $config = Array();
private $msg = Array();
function __construct($configarr, $msgarr) {
$this->config = $configarr;
$this->msg = $msgarr;
}
function __destruct() {
unset($this->config);
}
public function doRun() {
$result = Array();
//if no user/pass is given there is no use to continue
if(!(array_key_exists("Username", $this->config) && array_key_exists("Password", $this->config))) {
$result["usermsg"] = $this->msg['failedLogin'];
$result["error"] = -1;
$result["errmsg"] = "No username or password given";
return $result;
}
// connect to ldap server
$ldapconn = ldap_connect($this->config['host'], $this->config['port']);
$ldaprdn = $this->findUser($ldapconn, $this->config["Username"]);
if (ldap_errno($ldapconn) == 0) {
//If ldap connection is succesful we start the action
if(array_key_exists("jdapLogIn", $this->config)) {
if ($this->logIn($ldapconn, $ldaprdn, $this->config['Password'])) {
//$result = getAttributes($ldapconn, $ldaprdn);
if(array_key_exists("jdapAttributes", $this->config)) {
$result = $this->getAttributes($ldapconn, $ldaprdn, $this->config["jdapAttributes"]);
}
$result["usermsg"] = $this->msg['login'];
}
else {
$result["usermsg"] = $this->msg['failedLogin'];
}
}
//Change password action
elseif(array_key_exists("jdapUpdatePassword", $this->config)) {
if ($this->logIn($ldapconn, $ldaprdn, $this->config['Password'])) {
if ($this->updatePassword($ldapconn, $ldaprdn, $this->config["jdapUpdatePassword"])) {
$result["usermsg"] = $this->msg['failedPw'];
}
}
else {
$result["usermsg"] = $this->msg['failedCred'];
}
}
//Change attributes action
elseif(array_key_exists("jdapModifyAttributes", $this->config)) {
if ($this->logIn($ldapconn, $ldaprdn, $this->config['Password'])) {
if ($this->modifyAttributes($ldapconn, $ldaprdn, $this->config["jdapModifyAttributes"])) {
$result["usermsg"] = $this->msg['failedModify'];
}
}
else {
$result["usermsg"] = $this->msg['failedCred'];
}
}
else {
$result["usermsg"] = $this->msg['nop'];
}
}
else {
echo "Could not connect to LDAP server: " . ldap_error($ldapconn);
$result["usermsg"] = $this->msg['connectFailed'];
}
$result["error"] = ldap_errno($ldapconn);
$result["errmsg"] = ldap_error($ldapconn);
if ($result["usermsg"] == "" && $result["error"] == 0) {
$result["usermsg"] = $this->msg['success'];
}
elseif ($result["usermsg"] == "") {
$result["usermsg"] = $this->msg['failed'];
}
ldap_unbind($ldapconn);
return $result;
}
private function logIn(&$ldapcn, $udn, $pass) {
/*
* Simple login function
*/
$ldapbind = @ldap_bind($ldapcn, $udn, $pass);
if (ldap_errno($ldapcn) == 0) {
return true;
}
else {
return false;
}
}
private function findUser(&$ldapcn, $user) {
/*
* return DN of first user found
*/
$filter = "(&(objectClass=person)(".$this->config['user_attribute']."=".$user."))";
$ldapbind = @ldap_bind($ldapcn, $this->config["searchuser"], $this->config["searchpass"]);
$res = ldap_search($ldapcn, $this->config['basedn'], $filter, array("dn"));
if (res) {
$entryid = ldap_first_entry($ldapcn, $res);
if($entryid) {
return ldap_get_dn($ldapcn, $entryid);
}
else {
return ldap_errno($ldapcn);
}
}
else {
return ldap_errno($ldapcn);
}
}
private function getAttributes(&$ldapcn, $udn, $attr = array()) {
/*
* return requested attribubtes or all if none given
* Errorcode on fail!
*/
$res = ldap_search($ldapcn, $udn, "(objectClass=person)", $attr);
if (res) {
$entry = ldap_first_entry($ldapcn, $res);
return ldap_get_attributes($ldapcn, $entry);
}
else {
return ldap_errno($ldapcn);
}
}
private function modifyAttributes(&$ldapcn, $udn, $attr) {
@ldap_mod_replace($ldapcn, $udn, $attr);
return ldap_errno($ldapcn);
}
private function updatePassword(&$ldapcn, $udn, $passwd) {
error_log("PasSSP: changing pw for ".$udn);
$modattr = Array();
//Create Samba Hashes
if ($this->config['sambapasswords']) {
$smbHasher = new smbHash();
$modattr['sambaLMPassword'] = $smbHasher->lmhash($passwd);
$modattr['sambaNTPassword'] = $smbHasher->nthash($passwd);
$modattr['sambaPwdLastSet'] = $newData['sambaPwdCanChange'] = time();
$modattr['sambaPwdMustChange'] = '2147483647';
}
$modattr["userPassword"] = $this->encryptPassword($passwd);
return $this->modifyAttributes($ldapcn, $udn, $modattr);
}
function encryptPassword($password) {
/*
* funcions to encrypt the password for ldap
* based on code from egroupware.org
*/
$type = strtolower($this->config['password_encryption']);
$salt = '';
switch($type) {
case 'des':
$salt = self::randomstring(2);
$_password = crypt($password, $salt);
$e_password = '{crypt}'.$_password;
break;
case 'blowfish_crypt':
if(@defined('CRYPT_BLOWFISH') && CRYPT_BLOWFISH == 1) {
$salt = '$2$' . self::randomstring(13);
$e_password = '{crypt}'.crypt($password,$salt);
break;
}
self::$error = 'no blowfish crypt';
break;
case 'md5_crypt':
if(@defined('CRYPT_MD5') && CRYPT_MD5 == 1) {
$salt = '$1$' . self::randomstring(9);
$e_password = '{crypt}'.crypt($password,$salt);
break;
}
self::$error = 'no md5 crypt';
break;
case 'ext_crypt':
if(@defined('CRYPT_EXT_DES') && CRYPT_EXT_DES == 1) {
$salt = self::randomstring(9);
$e_password = '{crypt}'.crypt($password,$salt);
break;
}
self::$error = 'no ext crypt';
break;
case 'md5':
/* New method taken from the openldap-software list as recommended by
* Kervin L. Pierre" <[email protected]>
*/
$e_password = '{md5}' . base64_encode(pack("H*",md5($password)));
break;
case 'smd5':
$salt = self::randomstring(8);
$hash = md5($password . $salt,true);
$e_password = '{SMD5}' . base64_encode($hash . $salt);
break;
case 'sha':
$e_password = '{SHA}' . base64_encode(sha1($password,true));
break;
case 'ssha':
$salt = self::randomstring(8);
$hash = sha1($password . $salt,true);
$e_password = '{SSHA}' . base64_encode($hash . $salt);
break;
default:
// if plain no type is prepended
$e_password =$password;
break;
}
return $e_password;
}
}
?>