1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Arthur Schiwon <[email protected]> |
4
|
|
|
* @author Bart Visscher <[email protected]> |
5
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
6
|
|
|
* @author Lukas Reschke <[email protected]> |
7
|
|
|
* @author Lyonel Vincent <[email protected]> |
8
|
|
|
* @author Morris Jobke <[email protected]> |
9
|
|
|
* @author Robin Appelman <[email protected]> |
10
|
|
|
* @author Robin McCorkell <[email protected]> |
11
|
|
|
* @author Scrutinizer Auto-Fixer <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
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\lib; |
31
|
|
|
|
32
|
|
|
use OC\ServerNotAvailableException; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* magic properties (incomplete) |
36
|
|
|
* responsible for LDAP connections in context with the provided configuration |
37
|
|
|
* |
38
|
|
|
* @property string ldapUserFilter |
39
|
|
|
* @property string ldapUserDisplayName |
40
|
|
|
* @property string ldapUserDisplayName2 |
41
|
|
|
* @property boolean hasPagedResultSupport |
42
|
|
|
* @property string[] ldapBaseUsers |
43
|
|
|
* @property int|string ldapPagingSize holds an integer |
44
|
|
|
* @property bool|mixed|void ldapGroupMemberAssocAttr |
45
|
|
|
*/ |
46
|
|
|
class Connection extends LDAPUtility { |
47
|
|
|
private $ldapConnectionRes = null; |
48
|
|
|
private $configPrefix; |
49
|
|
|
private $configID; |
50
|
|
|
private $configured = false; |
51
|
|
|
private $hasPagedResultSupport = true; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var bool runtime flag that indicates whether supported primary groups are available |
55
|
|
|
*/ |
56
|
|
|
public $hasPrimaryGroups = true; |
57
|
|
|
|
58
|
|
|
//cache handler |
59
|
|
|
protected $cache; |
60
|
|
|
|
61
|
|
|
/** @var Configuration settings handler **/ |
62
|
|
|
protected $configuration; |
63
|
|
|
|
64
|
|
|
protected $doNotValidate = false; |
65
|
|
|
|
66
|
|
|
protected $ignoreValidation = false; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Constructor |
70
|
|
|
* @param ILDAPWrapper $ldap |
71
|
|
|
* @param string $configPrefix a string with the prefix for the configkey column (appconfig table) |
72
|
|
|
* @param string|null $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections |
73
|
|
|
*/ |
74
|
|
|
public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') { |
75
|
|
|
parent::__construct($ldap); |
76
|
|
|
$this->configPrefix = $configPrefix; |
77
|
100 |
|
$this->configID = $configID; |
78
|
100 |
|
$this->configuration = new Configuration($configPrefix, |
79
|
100 |
|
!is_null($configID)); |
80
|
100 |
|
$memcache = \OC::$server->getMemCacheFactory(); |
81
|
100 |
|
if($memcache->isAvailable()) { |
82
|
100 |
|
$this->cache = $memcache->create(); |
83
|
100 |
|
} |
84
|
100 |
|
$this->hasPagedResultSupport = |
85
|
100 |
|
$this->ldap->hasPagedResultSupport(); |
86
|
100 |
|
$helper = new Helper(); |
87
|
100 |
|
$this->doNotValidate = !in_array($this->configPrefix, |
88
|
100 |
|
$helper->getServerConfigurationPrefixes()); |
89
|
100 |
|
} |
90
|
100 |
|
|
91
|
100 |
|
public function __destruct() { |
92
|
100 |
|
if($this->ldap->isResource($this->ldapConnectionRes)) { |
93
|
|
|
@$this->ldap->unbind($this->ldapConnectionRes); |
|
|
|
|
94
|
9 |
|
}; |
95
|
9 |
|
} |
96
|
9 |
|
|
97
|
|
|
/** |
98
|
9 |
|
* defines behaviour when the instance is cloned |
99
|
9 |
|
*/ |
100
|
|
|
public function __clone() { |
101
|
|
|
$this->configuration = new Configuration($this->configPrefix, |
102
|
|
|
!is_null($this->configID)); |
103
|
|
|
$this->ldapConnectionRes = null; |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
1 |
|
* @param string $name |
108
|
1 |
|
* @return bool|mixed|void |
109
|
1 |
|
*/ |
110
|
1 |
|
public function __get($name) { |
111
|
|
|
if(!$this->configured) { |
112
|
|
|
$this->readConfiguration(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if($name === 'hasPagedResultSupport') { |
116
|
3 |
|
return $this->hasPagedResultSupport; |
117
|
3 |
|
} |
118
|
3 |
|
|
119
|
3 |
|
return $this->configuration->$name; |
120
|
|
|
} |
121
|
3 |
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $name |
124
|
|
|
* @param mixed $value |
125
|
3 |
|
*/ |
126
|
|
|
public function __set($name, $value) { |
127
|
|
|
$this->doNotValidate = false; |
128
|
|
|
$before = $this->configuration->$name; |
129
|
|
|
$this->configuration->$name = $value; |
130
|
|
|
$after = $this->configuration->$name; |
131
|
|
|
if($before !== $after) { |
132
|
|
|
if(!empty($this->configID)) { |
133
|
|
|
$this->configuration->saveConfiguration(); |
134
|
|
|
} |
135
|
|
|
$this->validateConfiguration(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* sets whether the result of the configuration validation shall |
141
|
|
|
* be ignored when establishing the connection. Used by the Wizard |
142
|
|
|
* in early configuration state. |
143
|
|
|
* @param bool $state |
144
|
|
|
*/ |
145
|
|
|
public function setIgnoreValidation($state) { |
146
|
|
|
$this->ignoreValidation = (bool)$state; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* initializes the LDAP backend |
151
|
|
|
* @param bool $force read the config settings no matter what |
152
|
|
|
*/ |
153
|
|
|
public function init($force = false) { |
154
|
|
|
$this->readConfiguration($force); |
155
|
|
|
$this->establishConnection(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns the LDAP handler |
160
|
|
|
*/ |
161
|
|
|
public function getConnectionResource() { |
162
|
|
|
if(!$this->ldapConnectionRes) { |
163
|
|
|
$this->init(); |
164
|
|
|
} else if(!$this->ldap->isResource($this->ldapConnectionRes)) { |
165
|
|
|
$this->ldapConnectionRes = null; |
166
|
|
|
$this->establishConnection(); |
167
|
|
|
} |
168
|
|
|
if(is_null($this->ldapConnectionRes)) { |
169
|
|
|
\OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server ' . $this->configuration->ldapHost, \OCP\Util::ERROR); |
|
|
|
|
170
|
|
|
throw new ServerNotAvailableException('Connection to LDAP server could not be established'); |
171
|
|
|
} |
172
|
|
|
return $this->ldapConnectionRes; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* resets the connection resource |
177
|
|
|
*/ |
178
|
|
|
public function resetConnectionResource() { |
179
|
|
|
if(!is_null($this->ldapConnectionRes)) { |
180
|
|
|
@$this->ldap->unbind($this->ldapConnectionRes); |
|
|
|
|
181
|
|
|
$this->ldapConnectionRes = null; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string|null $key |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
private function getCacheKey($key) { |
190
|
|
|
$prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-'; |
191
|
|
|
if(is_null($key)) { |
192
|
|
|
return $prefix; |
193
|
|
|
} |
194
|
|
|
return $prefix.md5($key); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $key |
199
|
|
|
* @return mixed|null |
200
|
|
|
*/ |
201
|
|
|
public function getFromCache($key) { |
202
|
|
|
if(!$this->configured) { |
203
|
|
|
$this->readConfiguration(); |
204
|
|
|
} |
205
|
|
|
if(is_null($this->cache) || !$this->configuration->ldapCacheTTL) { |
|
|
|
|
206
|
|
|
return null; |
207
|
|
|
} |
208
|
|
|
$key = $this->getCacheKey($key); |
209
|
|
|
|
210
|
|
|
return json_decode(base64_decode($this->cache->get($key)), true); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $key |
215
|
|
|
* @param mixed $value |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function writeToCache($key, $value) { |
220
|
|
|
if(!$this->configured) { |
221
|
|
|
$this->readConfiguration(); |
222
|
|
|
} |
223
|
|
|
if(is_null($this->cache) |
224
|
|
|
|| !$this->configuration->ldapCacheTTL |
|
|
|
|
225
|
|
|
|| !$this->configuration->ldapConfigurationActive) { |
|
|
|
|
226
|
|
|
return null; |
227
|
|
|
} |
228
|
|
|
$key = $this->getCacheKey($key); |
229
|
|
|
$value = base64_encode(json_encode($value)); |
230
|
|
|
$this->cache->set($key, $value, $this->configuration->ldapCacheTTL); |
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function clearCache() { |
234
|
|
|
if(!is_null($this->cache)) { |
235
|
|
|
$this->cache->clear($this->getCacheKey(null)); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Caches the general LDAP configuration. |
241
|
|
|
* @param bool $force optional. true, if the re-read should be forced. defaults |
242
|
|
|
* to false. |
243
|
|
|
* @return null |
244
|
|
|
*/ |
245
|
|
|
private function readConfiguration($force = false) { |
246
|
|
|
if((!$this->configured || $force) && !is_null($this->configID)) { |
247
|
|
|
$this->configuration->readConfiguration(); |
248
|
|
|
$this->configured = $this->validateConfiguration(); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* set LDAP configuration with values delivered by an array, not read from configuration |
254
|
|
|
* @param array $config array that holds the config parameters in an associated array |
255
|
|
|
* @param array &$setParameters optional; array where the set fields will be given to |
256
|
|
|
* @return boolean true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters |
257
|
|
|
*/ |
258
|
|
|
public function setConfiguration($config, &$setParameters = null) { |
259
|
|
|
if(is_null($setParameters)) { |
260
|
3 |
|
$setParameters = array(); |
261
|
3 |
|
} |
262
|
|
|
$this->doNotValidate = false; |
263
|
|
|
$this->configuration->setConfiguration($config, $setParameters); |
264
|
|
|
if(count($setParameters) > 0) { |
265
|
3 |
|
$this->configured = $this->validateConfiguration(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
return $this->configured; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
2 |
|
* saves the current Configuration in the database and empties the |
274
|
2 |
|
* cache |
275
|
2 |
|
* @return null |
276
|
2 |
|
*/ |
277
|
2 |
|
public function saveConfiguration() { |
278
|
2 |
|
$this->configuration->saveConfiguration(); |
279
|
2 |
|
$this->clearCache(); |
280
|
2 |
|
} |
281
|
2 |
|
|
282
|
|
|
/** |
283
|
|
|
* get the current LDAP configuration |
284
|
2 |
|
* @return array |
285
|
|
|
*/ |
286
|
|
|
public function getConfiguration() { |
287
|
|
|
$this->readConfiguration(); |
288
|
|
|
$config = $this->configuration->getConfiguration(); |
289
|
|
|
$cta = $this->configuration->getConfigTranslationArray(); |
290
|
|
|
$result = array(); |
291
|
|
|
foreach($cta as $dbkey => $configkey) { |
292
|
|
|
switch($configkey) { |
293
|
|
|
case 'homeFolderNamingRule': |
294
|
|
|
if(strpos($config[$configkey], 'attr:') === 0) { |
295
|
|
|
$result[$dbkey] = substr($config[$configkey], 5); |
296
|
|
|
} else { |
297
|
|
|
$result[$dbkey] = ''; |
298
|
|
|
} |
299
|
|
|
break; |
300
|
|
|
case 'ldapBase': |
301
|
|
|
case 'ldapBaseUsers': |
302
|
|
|
case 'ldapBaseGroups': |
303
|
|
|
case 'ldapAttributesForUserSearch': |
304
|
|
|
case 'ldapAttributesForGroupSearch': |
305
|
|
|
if(is_array($config[$configkey])) { |
306
|
|
|
$result[$dbkey] = implode("\n", $config[$configkey]); |
307
|
|
|
break; |
308
|
|
|
} //else follows default |
309
|
|
|
default: |
310
|
|
|
$result[$dbkey] = $config[$configkey]; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
return $result; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
private function doSoftValidation() { |
317
|
|
|
//if User or Group Base are not set, take over Base DN setting |
318
|
|
|
foreach(array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) { |
319
|
|
|
$val = $this->configuration->$keyBase; |
320
|
|
|
if(empty($val)) { |
321
|
|
|
$obj = strpos('Users', $keyBase) !== false ? 'Users' : 'Groups'; |
322
|
|
|
\OCP\Util::writeLog('user_ldap', |
323
|
|
|
'Base tree for '.$obj. |
324
|
|
|
' is empty, using Base DN', |
325
|
|
|
\OCP\Util::INFO); |
326
|
|
|
$this->configuration->$keyBase = $this->configuration->ldapBase; |
|
|
|
|
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
foreach(array('ldapExpertUUIDUserAttr' => 'ldapUuidUserAttribute', |
331
|
2 |
|
'ldapExpertUUIDGroupAttr' => 'ldapUuidGroupAttribute') |
332
|
|
|
as $expertSetting => $effectiveSetting) { |
333
|
2 |
|
$uuidOverride = $this->configuration->$expertSetting; |
334
|
2 |
|
if(!empty($uuidOverride)) { |
335
|
2 |
|
$this->configuration->$effectiveSetting = $uuidOverride; |
336
|
2 |
|
} else { |
337
|
2 |
|
$uuidAttributes = array('auto', 'entryuuid', 'nsuniqueid', |
338
|
2 |
|
'objectguid', 'guid'); |
339
|
2 |
|
if(!in_array($this->configuration->$effectiveSetting, |
340
|
2 |
|
$uuidAttributes) |
341
|
2 |
|
&& (!is_null($this->configID))) { |
342
|
2 |
|
$this->configuration->$effectiveSetting = 'auto'; |
343
|
2 |
|
$this->configuration->saveConfiguration(); |
344
|
|
|
\OCP\Util::writeLog('user_ldap', |
345
|
2 |
|
'Illegal value for the '. |
346
|
2 |
|
$effectiveSetting.', '.'reset to '. |
347
|
2 |
|
'autodetect.', \OCP\Util::INFO); |
348
|
2 |
|
} |
349
|
2 |
|
|
350
|
|
|
} |
351
|
|
|
} |
352
|
2 |
|
|
353
|
2 |
|
$backupPort = $this->configuration->ldapBackupPort; |
|
|
|
|
354
|
2 |
|
if(empty($backupPort)) { |
355
|
2 |
|
$this->configuration->backupPort = $this->configuration->ldapPort; |
|
|
|
|
356
|
2 |
|
} |
357
|
|
|
|
358
|
|
|
//make sure empty search attributes are saved as simple, empty array |
359
|
|
|
$saKeys = array('ldapAttributesForUserSearch', |
360
|
|
|
'ldapAttributesForGroupSearch'); |
361
|
|
|
foreach($saKeys as $key) { |
362
|
|
|
$val = $this->configuration->$key; |
363
|
|
|
if(is_array($val) && count($val) === 1 && empty($val[0])) { |
364
|
|
|
$this->configuration->$key = array(); |
365
|
|
|
} |
366
|
2 |
|
} |
367
|
|
|
|
368
|
2 |
|
if((stripos($this->configuration->ldapHost, 'ldaps://') === 0) |
|
|
|
|
369
|
2 |
|
&& $this->configuration->ldapTLS) { |
|
|
|
|
370
|
2 |
|
$this->configuration->ldapTLS = false; |
|
|
|
|
371
|
2 |
|
\OCP\Util::writeLog('user_ldap', |
372
|
|
|
'LDAPS (already using secure connection) and '. |
373
|
|
|
'TLS do not work together. Switched off TLS.', |
374
|
2 |
|
\OCP\Util::INFO); |
375
|
2 |
|
} |
376
|
2 |
|
} |
377
|
2 |
|
|
378
|
2 |
|
/** |
379
|
|
|
* @return bool |
380
|
|
|
*/ |
381
|
2 |
|
private function doCriticalValidation() { |
382
|
|
|
$configurationOK = true; |
383
|
2 |
|
$errorStr = 'Configuration Error (prefix '. |
384
|
2 |
|
strval($this->configPrefix).'): '; |
385
|
|
|
|
386
|
|
|
//options that shall not be empty |
387
|
|
|
$options = array('ldapHost', 'ldapPort', 'ldapUserDisplayName', |
388
|
|
|
'ldapGroupDisplayName', 'ldapLoginFilter'); |
389
|
|
|
foreach($options as $key) { |
390
|
|
|
$val = $this->configuration->$key; |
391
|
2 |
|
if(empty($val)) { |
392
|
|
|
switch($key) { |
393
|
|
|
case 'ldapHost': |
394
|
|
|
$subj = 'LDAP Host'; |
395
|
|
|
break; |
396
|
2 |
|
case 'ldapPort': |
397
|
2 |
|
$subj = 'LDAP Port'; |
398
|
|
|
break; |
399
|
2 |
|
case 'ldapUserDisplayName': |
400
|
|
|
$subj = 'LDAP User Display Name'; |
401
|
|
|
break; |
402
|
2 |
|
case 'ldapGroupDisplayName': |
403
|
2 |
|
$subj = 'LDAP Group Display Name'; |
404
|
2 |
|
break; |
405
|
2 |
|
case 'ldapLoginFilter': |
406
|
2 |
|
$subj = 'LDAP Login Filter'; |
407
|
|
|
break; |
408
|
2 |
|
default: |
409
|
2 |
|
$subj = $key; |
410
|
2 |
|
break; |
411
|
2 |
|
} |
412
|
2 |
|
$configurationOK = false; |
413
|
2 |
|
\OCP\Util::writeLog('user_ldap', |
414
|
2 |
|
$errorStr.'No '.$subj.' given!', |
415
|
2 |
|
\OCP\Util::WARN); |
416
|
2 |
|
} |
417
|
2 |
|
} |
418
|
2 |
|
|
419
|
2 |
|
//combinations |
420
|
2 |
|
$agent = $this->configuration->ldapAgentName; |
|
|
|
|
421
|
2 |
|
$pwd = $this->configuration->ldapAgentPassword; |
|
|
|
|
422
|
2 |
|
if((empty($agent) && !empty($pwd)) || (!empty($agent) && empty($pwd))) { |
423
|
|
|
\OCP\Util::writeLog('user_ldap', |
424
|
|
|
$errorStr.'either no password is given for the'. |
425
|
|
|
'user agent or a password is given, but not an'. |
426
|
|
|
'LDAP agent.', |
427
|
2 |
|
\OCP\Util::WARN); |
428
|
2 |
|
$configurationOK = false; |
429
|
2 |
|
} |
430
|
2 |
|
|
431
|
2 |
|
$base = $this->configuration->ldapBase; |
|
|
|
|
432
|
2 |
|
$baseUsers = $this->configuration->ldapBaseUsers; |
|
|
|
|
433
|
|
|
$baseGroups = $this->configuration->ldapBaseGroups; |
|
|
|
|
434
|
|
|
|
435
|
2 |
|
if(empty($base) && empty($baseUsers) && empty($baseGroups)) { |
436
|
2 |
|
\OCP\Util::writeLog('user_ldap', |
437
|
2 |
|
$errorStr.'Not a single Base DN given.', |
438
|
|
|
\OCP\Util::WARN); |
439
|
|
|
$configurationOK = false; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
if(mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
|
|
|
443
|
|
|
=== false) { |
444
|
|
|
\OCP\Util::writeLog('user_ldap', |
445
|
|
|
$errorStr.'login filter does not contain %uid '. |
446
|
2 |
|
'place holder.', |
447
|
2 |
|
\OCP\Util::WARN); |
448
|
2 |
|
$configurationOK = false; |
449
|
|
|
} |
450
|
2 |
|
|
451
|
2 |
|
return $configurationOK; |
452
|
2 |
|
} |
453
|
2 |
|
|
454
|
2 |
|
/** |
455
|
2 |
|
* Validates the user specified configuration |
456
|
|
|
* @return bool true if configuration seems OK, false otherwise |
457
|
2 |
|
*/ |
458
|
2 |
|
private function validateConfiguration() { |
459
|
2 |
|
|
460
|
2 |
|
if($this->doNotValidate) { |
461
|
2 |
|
//don't do a validation if it is a new configuration with pure |
462
|
2 |
|
//default values. Will be allowed on changes via __set or |
463
|
2 |
|
//setConfiguration |
464
|
2 |
|
return false; |
465
|
|
|
} |
466
|
2 |
|
|
467
|
|
|
// first step: "soft" checks: settings that are not really |
468
|
|
|
// necessary, but advisable. If left empty, give an info message |
469
|
|
|
$this->doSoftValidation(); |
470
|
|
|
|
471
|
|
|
//second step: critical checks. If left empty or filled wrong, mark as |
472
|
|
|
//not configured and give a warning. |
473
|
2 |
|
return $this->doCriticalValidation(); |
474
|
|
|
} |
475
|
2 |
|
|
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Connects and Binds to LDAP |
479
|
|
|
*/ |
480
|
|
|
private function establishConnection() { |
481
|
|
|
if(!$this->configuration->ldapConfigurationActive) { |
|
|
|
|
482
|
|
|
return null; |
483
|
|
|
} |
484
|
2 |
|
static $phpLDAPinstalled = true; |
485
|
|
|
if(!$phpLDAPinstalled) { |
486
|
|
|
return false; |
487
|
|
|
} |
488
|
2 |
|
if(!$this->ignoreValidation && !$this->configured) { |
489
|
|
|
\OCP\Util::writeLog('user_ldap', |
490
|
|
|
'Configuration is invalid, cannot connect', |
491
|
|
|
\OCP\Util::WARN); |
492
|
|
|
return false; |
493
|
|
|
} |
494
|
|
|
if(!$this->ldapConnectionRes) { |
495
|
|
|
if(!$this->ldap->areLDAPFunctionsAvailable()) { |
496
|
|
|
$phpLDAPinstalled = false; |
497
|
|
|
\OCP\Util::writeLog('user_ldap', |
498
|
|
|
'function ldap_connect is not available. Make '. |
499
|
|
|
'sure that the PHP ldap module is installed.', |
500
|
|
|
\OCP\Util::ERROR); |
501
|
|
|
|
502
|
|
|
return false; |
503
|
|
|
} |
504
|
|
|
if($this->configuration->turnOffCertCheck) { |
|
|
|
|
505
|
|
|
if(putenv('LDAPTLS_REQCERT=never')) { |
506
|
|
|
\OCP\Util::writeLog('user_ldap', |
507
|
|
|
'Turned off SSL certificate validation successfully.', |
508
|
|
|
\OCP\Util::DEBUG); |
509
|
|
|
} else { |
510
|
|
|
\OCP\Util::writeLog('user_ldap', |
511
|
|
|
'Could not turn off SSL certificate validation.', |
512
|
|
|
\OCP\Util::WARN); |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
$bindStatus = false; |
517
|
|
|
$error = -1; |
518
|
|
|
try { |
519
|
|
|
if (!$this->configuration->ldapOverrideMainServer |
|
|
|
|
520
|
|
|
&& !$this->getFromCache('overrideMainServer') |
521
|
|
|
) { |
522
|
|
|
$this->doConnect($this->configuration->ldapHost, |
|
|
|
|
523
|
|
|
$this->configuration->ldapPort); |
|
|
|
|
524
|
|
|
$bindStatus = $this->bind(); |
525
|
|
|
$error = $this->ldap->isResource($this->ldapConnectionRes) ? |
|
|
|
|
526
|
|
|
$this->ldap->errno($this->ldapConnectionRes) : -1; |
|
|
|
|
527
|
|
|
} |
528
|
|
|
if($bindStatus === true) { |
529
|
|
|
return $bindStatus; |
530
|
|
|
} |
531
|
|
|
} catch (\OC\ServerNotAvailableException $e) { |
532
|
|
|
if(trim($this->configuration->ldapBackupHost) === "") { |
|
|
|
|
533
|
|
|
throw $e; |
534
|
|
|
} |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
//if LDAP server is not reachable, try the Backup (Replica!) Server |
538
|
|
|
if( $error !== 0 |
539
|
|
|
|| $this->configuration->ldapOverrideMainServer |
|
|
|
|
540
|
|
|
|| $this->getFromCache('overrideMainServer')) |
541
|
|
|
{ |
542
|
|
|
$this->doConnect($this->configuration->ldapBackupHost, |
|
|
|
|
543
|
|
|
$this->configuration->ldapBackupPort); |
|
|
|
|
544
|
|
|
$bindStatus = $this->bind(); |
545
|
|
|
if($bindStatus && $error === -1 && !$this->getFromCache('overrideMainServer')) { |
546
|
|
|
//when bind to backup server succeeded and failed to main server, |
547
|
|
|
//skip contacting him until next cache refresh |
548
|
|
|
$this->writeToCache('overrideMainServer', true); |
549
|
|
|
} |
550
|
|
|
} |
551
|
|
|
return $bindStatus; |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* @param string $host |
557
|
|
|
* @param string $port |
558
|
|
|
* @return false|void |
559
|
|
|
*/ |
560
|
|
|
private function doConnect($host, $port) { |
561
|
|
|
if(empty($host)) { |
562
|
|
|
return false; |
563
|
|
|
} |
564
|
|
|
$this->ldapConnectionRes = $this->ldap->connect($host, $port); |
565
|
|
|
if($this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) { |
566
|
|
|
if($this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) { |
567
|
|
|
if($this->configuration->ldapTLS) { |
|
|
|
|
568
|
|
|
$this->ldap->startTls($this->ldapConnectionRes); |
569
|
|
|
} |
570
|
|
|
} |
571
|
|
|
} |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Binds to LDAP |
576
|
|
|
*/ |
577
|
|
|
public function bind() { |
578
|
|
|
static $getConnectionResourceAttempt = false; |
579
|
|
|
if(!$this->configuration->ldapConfigurationActive) { |
|
|
|
|
580
|
|
|
return false; |
581
|
|
|
} |
582
|
|
|
if($getConnectionResourceAttempt) { |
583
|
|
|
$getConnectionResourceAttempt = false; |
584
|
|
|
return false; |
585
|
|
|
} |
586
|
|
|
$getConnectionResourceAttempt = true; |
587
|
|
|
$cr = $this->getConnectionResource(); |
588
|
|
|
$getConnectionResourceAttempt = false; |
589
|
|
|
if(!$this->ldap->isResource($cr)) { |
590
|
|
|
return false; |
591
|
|
|
} |
592
|
|
|
$ldapLogin = @$this->ldap->bind($cr, |
593
|
|
|
$this->configuration->ldapAgentName, |
|
|
|
|
594
|
|
|
$this->configuration->ldapAgentPassword); |
|
|
|
|
595
|
|
|
if(!$ldapLogin) { |
596
|
|
|
\OCP\Util::writeLog('user_ldap', |
597
|
|
|
'Bind failed: ' . $this->ldap->errno($cr) . ': ' . $this->ldap->error($cr), |
598
|
|
|
\OCP\Util::WARN); |
599
|
|
|
$this->ldapConnectionRes = null; |
600
|
|
|
return false; |
601
|
|
|
} |
602
|
|
|
return true; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
} |
606
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: