|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alexander Bergolth <[email protected]> |
|
6
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
7
|
|
|
* @author Joas Schilling <[email protected]> |
|
8
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
|
9
|
|
|
* @author Lukas Reschke <[email protected]> |
|
10
|
|
|
* @author Morris Jobke <[email protected]> |
|
11
|
|
|
* @author Robin McCorkell <[email protected]> |
|
12
|
|
|
* @author Roger Szabo <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
* @license AGPL-3.0 |
|
15
|
|
|
* |
|
16
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
17
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* as published by the Free Software Foundation. |
|
19
|
|
|
* |
|
20
|
|
|
* This program is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU Affero General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
namespace OCA\User_LDAP; |
|
31
|
|
|
|
|
32
|
|
|
use OC\ServerNotAvailableException; |
|
33
|
|
|
use OCA\User_LDAP\Exceptions\ConstraintViolationException; |
|
34
|
|
|
|
|
35
|
|
|
class LDAP implements ILDAPWrapper { |
|
36
|
|
|
protected $curFunc = ''; |
|
37
|
|
|
protected $curArgs = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param resource $link |
|
41
|
|
|
* @param string $dn |
|
42
|
|
|
* @param string $password |
|
43
|
|
|
* @return bool|mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
public function bind($link, $dn, $password) { |
|
46
|
|
|
return $this->invokeLDAPMethod('bind', $link, $dn, $password); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $host |
|
51
|
|
|
* @param string $port |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public function connect($host, $port) { |
|
55
|
|
|
if(strpos($host, '://') === false) { |
|
56
|
|
|
$host = 'ldap://' . $host; |
|
57
|
|
|
} |
|
58
|
|
|
if(strpos($host, ':', strpos($host, '://') + 1) === false) { |
|
59
|
|
|
//ldap_connect ignores port parameter when URLs are passed |
|
60
|
|
|
$host .= ':' . $port; |
|
61
|
|
|
} |
|
62
|
|
|
return $this->invokeLDAPMethod('connect', $host); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param resource $link |
|
67
|
|
|
* @param resource $result |
|
68
|
|
|
* @param string $cookie |
|
69
|
|
|
* @return bool|LDAP |
|
70
|
|
|
*/ |
|
71
|
|
|
public function controlPagedResultResponse($link, $result, &$cookie) { |
|
72
|
|
|
$this->preFunctionCall('ldap_control_paged_result_response', |
|
73
|
|
|
array($link, $result, $cookie)); |
|
74
|
|
|
$result = ldap_control_paged_result_response($link, $result, $cookie); |
|
75
|
|
|
$this->postFunctionCall(); |
|
76
|
|
|
|
|
77
|
|
|
return $result; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param LDAP $link |
|
82
|
|
|
* @param int $pageSize |
|
83
|
|
|
* @param bool $isCritical |
|
84
|
|
|
* @param string $cookie |
|
85
|
|
|
* @return mixed|true |
|
86
|
|
|
*/ |
|
87
|
|
|
public function controlPagedResult($link, $pageSize, $isCritical, $cookie) { |
|
88
|
|
|
return $this->invokeLDAPMethod('control_paged_result', $link, $pageSize, |
|
89
|
|
|
$isCritical, $cookie); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param LDAP $link |
|
94
|
|
|
* @param LDAP $result |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
|
|
public function countEntries($link, $result) { |
|
98
|
|
|
return $this->invokeLDAPMethod('count_entries', $link, $result); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param LDAP $link |
|
103
|
|
|
* @return integer |
|
104
|
|
|
*/ |
|
105
|
|
|
public function errno($link) { |
|
106
|
|
|
return $this->invokeLDAPMethod('errno', $link); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param LDAP $link |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function error($link) { |
|
114
|
|
|
return $this->invokeLDAPMethod('error', $link); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Splits DN into its component parts |
|
119
|
|
|
* @param string $dn |
|
120
|
|
|
* @param int @withAttrib |
|
|
|
|
|
|
121
|
|
|
* @return array|false |
|
122
|
|
|
* @link http://www.php.net/manual/en/function.ldap-explode-dn.php |
|
123
|
|
|
*/ |
|
124
|
|
|
public function explodeDN($dn, $withAttrib) { |
|
125
|
|
|
return $this->invokeLDAPMethod('explode_dn', $dn, $withAttrib); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param LDAP $link |
|
130
|
|
|
* @param LDAP $result |
|
131
|
|
|
* @return mixed |
|
132
|
|
|
*/ |
|
133
|
|
|
public function firstEntry($link, $result) { |
|
134
|
|
|
return $this->invokeLDAPMethod('first_entry', $link, $result); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param LDAP $link |
|
139
|
|
|
* @param LDAP $result |
|
140
|
|
|
* @return array|mixed |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getAttributes($link, $result) { |
|
143
|
|
|
return $this->invokeLDAPMethod('get_attributes', $link, $result); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param LDAP $link |
|
148
|
|
|
* @param LDAP $result |
|
149
|
|
|
* @return mixed|string |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getDN($link, $result) { |
|
152
|
|
|
return $this->invokeLDAPMethod('get_dn', $link, $result); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param LDAP $link |
|
157
|
|
|
* @param LDAP $result |
|
158
|
|
|
* @return array|mixed |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getEntries($link, $result) { |
|
161
|
|
|
return $this->invokeLDAPMethod('get_entries', $link, $result); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param LDAP $link |
|
166
|
|
|
* @param resource $result |
|
167
|
|
|
* @return mixed |
|
168
|
|
|
*/ |
|
169
|
|
|
public function nextEntry($link, $result) { |
|
170
|
|
|
return $this->invokeLDAPMethod('next_entry', $link, $result); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param LDAP $link |
|
175
|
|
|
* @param string $baseDN |
|
176
|
|
|
* @param string $filter |
|
177
|
|
|
* @param array $attr |
|
178
|
|
|
* @return mixed |
|
179
|
|
|
*/ |
|
180
|
|
|
public function read($link, $baseDN, $filter, $attr) { |
|
181
|
|
|
return $this->invokeLDAPMethod('read', $link, $baseDN, $filter, $attr); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param LDAP $link |
|
186
|
|
|
* @param string $baseDN |
|
187
|
|
|
* @param string $filter |
|
188
|
|
|
* @param array $attr |
|
189
|
|
|
* @param int $attrsOnly |
|
190
|
|
|
* @param int $limit |
|
191
|
|
|
* @return mixed |
|
192
|
|
|
*/ |
|
193
|
|
|
public function search($link, $baseDN, $filter, $attr, $attrsOnly = 0, $limit = 0) { |
|
194
|
|
|
return $this->invokeLDAPMethod('search', $link, $baseDN, $filter, $attr, $attrsOnly, $limit); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param LDAP $link |
|
199
|
|
|
* @param string $userDN |
|
200
|
|
|
* @param string $password |
|
201
|
|
|
* @return bool |
|
202
|
|
|
*/ |
|
203
|
|
|
public function modReplace($link, $userDN, $password) { |
|
204
|
|
|
return $this->invokeLDAPMethod('mod_replace', $link, $userDN, array('userPassword' => $password)); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param LDAP $link |
|
209
|
|
|
* @param string $option |
|
210
|
|
|
* @param int $value |
|
211
|
|
|
* @return bool|mixed |
|
212
|
|
|
*/ |
|
213
|
|
|
public function setOption($link, $option, $value) { |
|
214
|
|
|
return $this->invokeLDAPMethod('set_option', $link, $option, $value); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param LDAP $link |
|
219
|
|
|
* @return mixed|true |
|
220
|
|
|
*/ |
|
221
|
|
|
public function startTls($link) { |
|
222
|
|
|
return $this->invokeLDAPMethod('start_tls', $link); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param resource $link |
|
227
|
|
|
* @return bool|mixed |
|
228
|
|
|
*/ |
|
229
|
|
|
public function unbind($link) { |
|
230
|
|
|
return $this->invokeLDAPMethod('unbind', $link); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Checks whether the server supports LDAP |
|
235
|
|
|
* @return boolean if it the case, false otherwise |
|
236
|
|
|
* */ |
|
237
|
|
|
public function areLDAPFunctionsAvailable() { |
|
238
|
|
|
return function_exists('ldap_connect'); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Checks whether the submitted parameter is a resource |
|
243
|
|
|
* @param Resource $resource the resource variable to check |
|
244
|
|
|
* @return bool true if it is a resource, false otherwise |
|
245
|
|
|
*/ |
|
246
|
|
|
public function isResource($resource) { |
|
247
|
|
|
return is_resource($resource); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Checks whether the return value from LDAP is wrong or not. |
|
252
|
|
|
* |
|
253
|
|
|
* When using ldap_search we provide an array, in case multiple bases are |
|
254
|
|
|
* configured. Thus, we need to check the array elements. |
|
255
|
|
|
* |
|
256
|
|
|
* @param $result |
|
257
|
|
|
* @return bool |
|
258
|
|
|
*/ |
|
259
|
|
|
protected function isResultFalse($result) { |
|
260
|
|
|
if($result === false) { |
|
261
|
|
|
return true; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
if($this->curFunc === 'ldap_search' && is_array($result)) { |
|
265
|
|
|
foreach ($result as $singleResult) { |
|
266
|
|
|
if($singleResult === false) { |
|
267
|
|
|
return true; |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return false; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @return mixed |
|
277
|
|
|
*/ |
|
278
|
|
|
protected function invokeLDAPMethod() { |
|
279
|
|
|
$arguments = func_get_args(); |
|
280
|
|
|
$func = 'ldap_' . array_shift($arguments); |
|
281
|
|
|
if(function_exists($func)) { |
|
282
|
|
|
$this->preFunctionCall($func, $arguments); |
|
283
|
|
|
$result = call_user_func_array($func, $arguments); |
|
284
|
|
|
if ($this->isResultFalse($result)) { |
|
285
|
|
|
$this->postFunctionCall(); |
|
286
|
|
|
} |
|
287
|
|
|
return $result; |
|
288
|
|
|
} |
|
289
|
|
|
return null; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @param string $functionName |
|
294
|
|
|
* @param array $args |
|
295
|
|
|
*/ |
|
296
|
|
|
private function preFunctionCall($functionName, $args) { |
|
297
|
|
|
$this->curFunc = $functionName; |
|
298
|
|
|
$this->curArgs = $args; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Analyzes the returned LDAP error and acts accordingly if not 0 |
|
303
|
|
|
* |
|
304
|
|
|
* @param resource $resource the LDAP Connection resource |
|
305
|
|
|
* @throws ConstraintViolationException |
|
306
|
|
|
* @throws ServerNotAvailableException |
|
307
|
|
|
* @throws \Exception |
|
308
|
|
|
*/ |
|
309
|
|
|
private function processLDAPError($resource) { |
|
310
|
|
|
$errorCode = ldap_errno($resource); |
|
311
|
|
|
if($errorCode === 0) { |
|
312
|
|
|
return; |
|
313
|
|
|
} |
|
314
|
|
|
$errorMsg = ldap_error($resource); |
|
315
|
|
|
|
|
316
|
|
|
if($this->curFunc === 'ldap_get_entries' |
|
317
|
|
|
&& $errorCode === -4) { |
|
318
|
|
|
} else if ($errorCode === 32) { |
|
319
|
|
|
//for now |
|
320
|
|
|
} else if ($errorCode === 10) { |
|
321
|
|
|
//referrals, we switch them off, but then there is AD :) |
|
322
|
|
|
} else if ($errorCode === -1) { |
|
323
|
|
|
throw new ServerNotAvailableException('Lost connection to LDAP server.'); |
|
324
|
|
|
} else if ($errorCode === 52) { |
|
325
|
|
|
throw new ServerNotAvailableException('LDAP server is shutting down.'); |
|
326
|
|
|
} else if ($errorCode === 48) { |
|
327
|
|
|
throw new \Exception('LDAP authentication method rejected', $errorCode); |
|
328
|
|
|
} else if ($errorCode === 1) { |
|
329
|
|
|
throw new \Exception('LDAP Operations error', $errorCode); |
|
330
|
|
|
} else if ($errorCode === 19) { |
|
331
|
|
|
ldap_get_option($this->curArgs[0], LDAP_OPT_ERROR_STRING, $extended_error); |
|
332
|
|
|
throw new ConstraintViolationException(!empty($extended_error)?$extended_error:$errorMsg, $errorCode); |
|
333
|
|
|
} else { |
|
334
|
|
|
\OC::$server->getLogger()->debug('LDAP error {message} ({code}) after calling {func}', [ |
|
335
|
|
|
'app' => 'user_ldap', |
|
336
|
|
|
'message' => $errorMsg, |
|
337
|
|
|
'code' => $errorCode, |
|
338
|
|
|
'func' => $this->curFunc, |
|
339
|
|
|
]); |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Called after an ldap method is run to act on LDAP error if necessary |
|
345
|
|
|
*/ |
|
346
|
|
|
private function postFunctionCall() { |
|
347
|
|
|
if($this->isResource($this->curArgs[0])) { |
|
348
|
|
|
$resource = $this->curArgs[0]; |
|
349
|
|
|
} else if( |
|
350
|
|
|
$this->curFunc === 'ldap_search' |
|
351
|
|
|
&& is_array($this->curArgs[0]) |
|
352
|
|
|
&& $this->isResource($this->curArgs[0][0]) |
|
353
|
|
|
) { |
|
354
|
|
|
// we use always the same LDAP connection resource, is enough to |
|
355
|
|
|
// take the first one. |
|
356
|
|
|
$resource = $this->curArgs[0][0]; |
|
357
|
|
|
} else { |
|
358
|
|
|
return; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
$this->processLDAPError($resource); |
|
362
|
|
|
|
|
363
|
|
|
$this->curFunc = ''; |
|
364
|
|
|
$this->curArgs = []; |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
|