-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLDAPUserManager.php
460 lines (415 loc) · 13.3 KB
/
LDAPUserManager.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
/**
* @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br)
*
* @author Alan Tygel <[email protected]>
* @author Vinicius Brand <[email protected]>
* @author Daniel Tygel <[email protected]>
* @author Arthur Schiwon <[email protected]>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\LdapWriteSupport;
use Exception;
use OC\HintException;
use OC\ServerNotAvailableException;
use OC\User\Backend;
use OC_User;
use OCA\LdapWriteSupport\AppInfo\Application;
use OCA\LdapWriteSupport\Service\Configuration;
use OCA\User_LDAP\Exceptions\ConstraintViolationException;
use OCA\User_LDAP\ILDAPUserPlugin;
use OCA\User_LDAP\IUserLDAP;
use OCP\IImage;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\LDAP\IDeletionFlagSupport;
use OCP\LDAP\ILDAPProvider;
use Psr\Log\LoggerInterface;
class LDAPUserManager implements ILDAPUserPlugin {
/** @var ILDAPProvider */
private $ldapProvider;
/** @var IUserSession */
private $userSession;
/** @var IUserManager */
private $userManager;
/** @var LDAPConnect */
private $ldapConnect;
/** @var Configuration */
private $configuration;
/** @var IL10N */
private $l10n;
/** @var LoggerInterface */
private $logger;
public function __construct(IUserManager $userManager, IUserSession $userSession, LDAPConnect $ldapConnect, ILDAPProvider $LDAPProvider, Configuration $configuration, IL10N $l10n, LoggerInterface $logger) {
$this->userManager = $userManager;
$this->userSession = $userSession;
$this->ldapConnect = $ldapConnect;
$this->ldapProvider = $LDAPProvider;
$this->configuration = $configuration;
$this->l10n = $l10n;
$this->logger = $logger;
$this->userManager->listen('\OC\User', 'changeUser', [$this, 'changeUserHook']);
$this->makeLdapBackendFirst();
}
/**
* Check if plugin implements actions
*
* @param int $actions bitwise-or'ed actions
* @return boolean
*
* Returns the supported actions as int to be
* compared with OC_USER_BACKEND_CREATE_USER etc.
*/
public function respondToActions() {
$setPassword = function_exists('ldap_exop_passwd') && !$this->ldapConnect->hasPasswordPolicy()
? Backend::SET_PASSWORD
: 0;
return Backend::SET_DISPLAYNAME |
Backend::PROVIDE_AVATAR |
Backend::CREATE_USER |
$setPassword;
}
/**
*
* @param string $uid user ID of the user
* @param string $displayName new user's display name
* @return string
* @throws HintException
* @throws ServerNotAvailableException
*/
public function setDisplayName($uid, $displayName) {
trigger_error(__METHOD__);
$this->logger->error(__METHOD__, ['app' => 'ldap_write_support']);
$userDN = $this->getUserDN($uid);
$connection = $this->ldapProvider->getLDAPConnection($uid);
try {
$displayNameField = $this->ldapProvider->getLDAPDisplayNameField($uid);
// The LDAP backend supports a second display name field, but it is
// not exposed at this time. So it is just ignored for now.
} catch (Exception $e) {
throw new HintException(
'Corresponding LDAP User not found',
$this->l10n->t('Could not find related LDAP entry')
);
}
if (!is_resource($connection) && !is_object($connection)) {
$this->logger->debug('LDAP resource not available', ['app' => 'ldap_write_support']);
throw new ServerNotAvailableException('LDAP server is not available');
}
try {
if (ldap_mod_replace($connection, $userDN, [$displayNameField => $displayName])) {
return $displayName;
}
trigger_error(print_r(['conn' => $connection,
'user' => $userDN,
'dpyField' => $displayNameField,
'dpy' => $displayName], true));
throw new HintException('Failed to set display name');
} catch (ConstraintViolationException $e) {
throw new HintException(
$e->getMessage(),
$this->l10n->t('DisplayName change rejected'),
$e->getCode()
);
}
}
/**
* checks whether the user is allowed to change his avatar in Nextcloud
*
* @param string $uid the Nextcloud user name
* @return boolean either the user can or cannot
*/
public function canChangeAvatar($uid) {
return $this->configuration->hasAvatarPermission();
}
/**
* Saves NC user avatar to LDAP
*
* @param IUser $user
*/
public function changeAvatar($user): void {
try {
$userDN = $this->getUserDN($user->getUID());
} catch (Exception $e) {
return;
}
/** @var IImage $avatar */
$avatar = $user->getAvatarImage(-1);
if ($avatar) {
$data = $avatar->data();
$connection = $this->ldapProvider->getLDAPConnection($user->getUID());
ldap_mod_replace($connection, $userDN, ['jpegphoto' => $data]);
}
}
/**
* Saves NC user email to LDAP
*
* @param IUser $user
* @throws Exception
*/
public function changeEmail(IUser $user, string $newEmail): void {
try {
$userDN = $this->getUserDN($user->getUID());
} catch (Exception $e) {
return;
}
$emailField = $this->ldapProvider->getLDAPEmailField($user->getUID());
$connection = $this->ldapProvider->getLDAPConnection($user->getUID());
ldap_mod_replace($connection, $userDN, [$emailField => $newEmail]);
}
/**
* Create a new user in LDAP Backend
*
* @param string $username The username of the user to create
* @param string $password The password of the new user
* @return bool|string the created user of false
* @throws Exception
*/
public function createUser($username, $password) {
$adminUser = $this->userSession->getUser();
$requireActorFromLDAP = $this->configuration->isLdapActorRequired();
if ($requireActorFromLDAP && !$adminUser instanceof IUser) {
throw new Exception('Acting user is not from LDAP');
}
try {
// $adminUser can be null, for example when using the registration app,
// throw an Exception to fallback on using the global LDAP connection.
if ($adminUser === null) {
throw new Exception('No admin user available');
}
$connection = $this->ldapProvider->getLDAPConnection($adminUser->getUID());
// TODO: what about multiple bases?
$base = $this->ldapProvider->getLDAPBaseUsers($adminUser->getUID());
$displayNameAttribute = $this->ldapProvider->getLDAPDisplayNameField($adminUser->getUID());
} catch (Exception $e) {
if ($requireActorFromLDAP) {
if ($this->configuration->isPreventFallback()) {
throw new \Exception('Acting admin is not from LDAP', 0, $e);
}
return false;
}
$connection = $this->ldapConnect->getLDAPConnection();
$base = $this->ldapConnect->getLDAPBaseUsers()[0];
$displayNameAttribute = $this->ldapConnect->getDisplayNameAttribute();
}
[$newUserDN, $newUserEntry] = $this->buildNewEntry($username, $password, $base);
$newUserDN = $this->ldapProvider->sanitizeDN([$newUserDN])[0];
$this->ensureAttribute($newUserEntry, $displayNameAttribute, $username);
$ret = ldap_add($connection, $newUserDN, $newUserEntry);
$message = 'Create LDAP user \'{username}\' ({dn})';
$logMethod = 'info';
if($ret === false) {
$message = 'Unable to create LDAP user \'{username}\' ({dn})';
$logMethod = 'error';
}
$this->logger->$logMethod($message, [
'app' => Application::APP_ID,
'username' => $username,
'dn' => $newUserDN,
]);
if (!$ret && $this->configuration->isPreventFallback()) {
throw new \Exception('Cannot create user: ' . ldap_error($connection), ldap_errno($connection));
}
// Set password through ldap password exop, if supported
if ($this->respondToActions() & Backend::SET_PASSWORD) {
try {
$ret = ldap_exop_passwd($connection, $newUserDN, '', $password);
if ($ret === false) {
$message = 'ldap_exop_passwd failed, falling back to ldap_mod_replace to to set password for new user';
$this->logger->log(ILogger::DEBUG, $message, [
'app' => Application::APP_ID,
]);
// Fallback to `userPassword` in case the server does not support exop_passwd
$ret = ldap_mod_replace($connection, $newUserDN, ['userPassword' => $password]);
if ($ret === false) {
$message = 'Failed to set password for new user {dn}';
$this->logger->log(ILogger::ERROR, $message, [
'app' => Application::APP_ID,
'dn' => $newUserDN,
]);
}
}
} catch (\Exception $e) {
$this->logger->logException($e, ['app' => Application::APP_ID]);
}
}
return $ret ? $newUserDN : false;
}
public function ensureAttribute(array &$ldif, string $attribute, string $fallbackValue): void {
$lowerCasedLDIF = array_change_key_case($ldif, CASE_LOWER);
if(!isset($lowerCasedLDIF[strtolower($attribute)])) {
$ldif[$attribute] = $fallbackValue;
}
}
public function buildNewEntry($username, $password, $base): array {
// Make sure the parameters don't fool the following algorithm
if (strpos($username, PHP_EOL) !== false) {
throw new Exception('Username contains a new line');
}
if (strpos($password, PHP_EOL) !== false) {
throw new Exception('Password contains a new line');
}
if (strpos($base, PHP_EOL) !== false) {
throw new Exception('Base DN contains a new line');
}
$ldif = $this->configuration->getUserTemplate();
$ldif = str_replace('{UID}', $username, $ldif);
$ldif = str_replace('{PWD}', $password, $ldif);
$ldif = str_replace('{BASE}', $base, $ldif);
$entry = [];
$lines = explode(PHP_EOL, $ldif);
foreach ($lines as $line) {
$split = explode(':', $line, 2);
$key = trim($split[0]);
$value = trim($split[1]);
if (!isset($entry[$key])) {
$entry[$key] = $value;
} else if (is_array($entry[$key])) {
$entry[$key][] = $value;
} else {
$entry[$key] = [$entry[$key], $value];
}
}
$dn = $entry['dn'];
unset($entry['dn']);
return [$dn, $entry];
}
/**
* @param $uid
* @return bool
*/
public function deleteUser($uid): bool {
$connection = $this->ldapProvider->getLDAPConnection($uid);
$userDN = $this->getUserDN($uid);
$user = $this->userManager->get($uid);
if ($res = ldap_delete($connection, $userDN)) {
$message = "Delete LDAP user (isDeleted): " . $uid;
$this->logger->notice($message, ['app' => Application::APP_ID]);
if (
$this->ldapProvider instanceof IDeletionFlagSupport
&& $user instanceof IUser
) {
$this->ldapProvider->flagRecord($uid);
} else {
$this->logger->warning(
'Could not run delete process on {uid}',
['app' => Application::APP_ID, 'uid' => $uid]
);
}
} else {
$errno = ldap_errno($connection);
if ($errno === 0x20) { #LDAP_NO_SUCH_OBJECT
$message = "Delete LDAP user {uid}: object not found. Is already deleted? Assuming YES";
$res = true;
} else {
$message = "Unable to delete LDAP user {uid}";
}
$this->logger->notice($message, ['app' => Application::APP_ID, 'uid' => $uid]);
}
ldap_close($connection);
return $res;
}
/**
* Set password
*
* @param string $uid The username
* @param string $password The new password
* @return bool
*
* Change the password of a user
*/
public function setPassword($uid, $password) {
if(!function_exists('ldap_exop_passwd')) {
// since PHP 7.2 – respondToActions checked this already, this
// method should not be called. Double check due to public scope.
// This method can be removed when Nextcloud 16 compat is dropped.
return false;
}
try {
$cr = $this->ldapProvider->getLDAPConnection($uid);
$userDN = $this->getUserDN($uid);
return ldap_exop_passwd($cr, $userDN, '', $password);
} catch (\Exception $e) {
$this->logger->logException($e, ['app' => Application::APP_ID]);
}
return false;
}
/**
* get the user's home directory
*
* @param string $uid the username
* @return boolean
*/
public function getHome($uid) {
// Not implemented
return false;
}
/**
* get display name of the user
*
* @param string $uid user ID of the user
* @return string display name
*/
public function getDisplayName($uid) {
// Not implemented
return false;
}
/**
* Count the number of users
*
* @return int|bool
*/
public function countUsers() {
// Not implemented
return false;
}
public function makeLdapBackendFirst(): void {
$backends = $this->userManager->getBackends();
$otherBackends = [];
$this->userManager->clearBackends();
foreach ($backends as $backend) {
if ($backend instanceof IUserLDAP) {
OC_User::useBackend($backend);
} else {
$otherBackends[] = $backend;
}
}
#insert other backends: database, etc
foreach ($otherBackends as $backend) {
OC_User::useBackend($backend);
}
}
/**
* @throws Exception
*/
public function changeUserHook(IUser $user, string $feature, $attr1, $attr2): void {
switch ($feature) {
case 'avatar':
$this->changeAvatar($user);
break;
case 'eMailAddress':
//attr1 = new email ; attr2 = old email
$this->changeEmail($user, $attr1);
break;
}
}
private function getUserDN($uid): string {
return $this->ldapProvider->getUserDN($uid);
}
}