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