|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Aaron Wood <[email protected]> |
|
6
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
7
|
|
|
* @author blizzz <[email protected]> |
|
8
|
|
|
* @author Christoph Wurst <[email protected]> |
|
9
|
|
|
* @author Joas Schilling <[email protected]> |
|
10
|
|
|
* @author Morris Jobke <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @license AGPL-3.0 |
|
13
|
|
|
* |
|
14
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
16
|
|
|
* as published by the Free Software Foundation. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, |
|
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21
|
|
|
* GNU Affero General Public License for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace OCA\User_LDAP\Mapping; |
|
29
|
|
|
|
|
30
|
|
|
use Doctrine\DBAL\Exception; |
|
31
|
|
|
use OC\DB\QueryBuilder\QueryBuilder; |
|
32
|
|
|
use OCP\DB\IPreparedStatement; |
|
33
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Class AbstractMapping |
|
37
|
|
|
* |
|
38
|
|
|
* @package OCA\User_LDAP\Mapping |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract class AbstractMapping { |
|
41
|
|
|
/** |
|
42
|
|
|
* @var \OCP\IDBConnection $dbc |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $dbc; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* returns the DB table name which holds the mappings |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
abstract protected function getTableName(bool $includePrefix = true); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param \OCP\IDBConnection $dbc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(\OCP\IDBConnection $dbc) { |
|
57
|
|
|
$this->dbc = $dbc; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** @var array caches Names (value) by DN (key) */ |
|
61
|
|
|
protected $cache = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* checks whether a provided string represents an existing table col |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $col |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public function isColNameValid($col) { |
|
70
|
|
|
switch ($col) { |
|
71
|
|
|
case 'ldap_dn': |
|
72
|
|
|
case 'owncloud_name': |
|
73
|
|
|
case 'directory_uuid': |
|
74
|
|
|
return true; |
|
75
|
|
|
default: |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Gets the value of one column based on a provided value of another column |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $fetchCol |
|
84
|
|
|
* @param string $compareCol |
|
85
|
|
|
* @param string $search |
|
86
|
|
|
* @return string|false |
|
87
|
|
|
* @throws \Exception |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getXbyY($fetchCol, $compareCol, $search) { |
|
90
|
|
|
if (!$this->isColNameValid($fetchCol)) { |
|
91
|
|
|
//this is used internally only, but we don't want to risk |
|
92
|
|
|
//having SQL injection at all. |
|
93
|
|
|
throw new \Exception('Invalid Column Name'); |
|
94
|
|
|
} |
|
95
|
|
|
$query = $this->dbc->prepare(' |
|
96
|
|
|
SELECT `' . $fetchCol . '` |
|
97
|
|
|
FROM `' . $this->getTableName() . '` |
|
98
|
|
|
WHERE `' . $compareCol . '` = ? |
|
99
|
|
|
'); |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
|
|
$res = $query->execute([$search]); |
|
103
|
|
|
$data = $res->fetchOne(); |
|
104
|
|
|
$res->closeCursor(); |
|
105
|
|
|
return $data; |
|
106
|
|
|
} catch (Exception $e) { |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Performs a DELETE or UPDATE query to the database. |
|
113
|
|
|
* |
|
114
|
|
|
* @param IPreparedStatement $statement |
|
115
|
|
|
* @param array $parameters |
|
116
|
|
|
* @return bool true if at least one row was modified, false otherwise |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function modify(IPreparedStatement $statement, $parameters) { |
|
119
|
|
|
try { |
|
120
|
|
|
$result = $statement->execute($parameters); |
|
121
|
|
|
$updated = $result->rowCount() > 0; |
|
122
|
|
|
$result->closeCursor(); |
|
123
|
|
|
return $updated; |
|
124
|
|
|
} catch (Exception $e) { |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Gets the LDAP DN based on the provided name. |
|
131
|
|
|
* Replaces Access::ocname2dn |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $name |
|
134
|
|
|
* @return string|false |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getDNByName($name) { |
|
137
|
|
|
$dn = array_search($name, $this->cache); |
|
138
|
|
|
if ($dn === false && ($dn = $this->getXbyY('ldap_dn', 'owncloud_name', $name)) !== false) { |
|
139
|
|
|
$this->cache[$dn] = $name; |
|
140
|
|
|
} |
|
141
|
|
|
return $dn; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Updates the DN based on the given UUID |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $fdn |
|
148
|
|
|
* @param string $uuid |
|
149
|
|
|
* @return bool |
|
150
|
|
|
*/ |
|
151
|
|
|
public function setDNbyUUID($fdn, $uuid) { |
|
152
|
|
|
$oldDn = $this->getDnByUUID($uuid); |
|
153
|
|
|
$statement = $this->dbc->prepare(' |
|
154
|
|
|
UPDATE `' . $this->getTableName() . '` |
|
155
|
|
|
SET `ldap_dn` = ? |
|
156
|
|
|
WHERE `directory_uuid` = ? |
|
157
|
|
|
'); |
|
158
|
|
|
|
|
159
|
|
|
$r = $this->modify($statement, [$fdn, $uuid]); |
|
160
|
|
|
|
|
161
|
|
|
if ($r && is_string($oldDn) && isset($this->cache[$oldDn])) { |
|
162
|
|
|
$this->cache[$fdn] = $this->cache[$oldDn]; |
|
163
|
|
|
unset($this->cache[$oldDn]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $r; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Updates the UUID based on the given DN |
|
171
|
|
|
* |
|
172
|
|
|
* required by Migration/UUIDFix |
|
173
|
|
|
* |
|
174
|
|
|
* @param $uuid |
|
175
|
|
|
* @param $fdn |
|
176
|
|
|
* @return bool |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setUUIDbyDN($uuid, $fdn) { |
|
179
|
|
|
$statement = $this->dbc->prepare(' |
|
180
|
|
|
UPDATE `' . $this->getTableName() . '` |
|
181
|
|
|
SET `directory_uuid` = ? |
|
182
|
|
|
WHERE `ldap_dn` = ? |
|
183
|
|
|
'); |
|
184
|
|
|
|
|
185
|
|
|
unset($this->cache[$fdn]); |
|
186
|
|
|
|
|
187
|
|
|
return $this->modify($statement, [$uuid, $fdn]); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Gets the name based on the provided LDAP DN. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $fdn |
|
194
|
|
|
* @return string|false |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getNameByDN($fdn) { |
|
197
|
|
|
if (!isset($this->cache[$fdn])) { |
|
198
|
|
|
$this->cache[$fdn] = $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); |
|
199
|
|
|
} |
|
200
|
|
|
return $this->cache[$fdn]; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
protected function prepareListOfIdsQuery(array $dnList): IQueryBuilder { |
|
204
|
|
|
$qb = $this->dbc->getQueryBuilder(); |
|
205
|
|
|
$qb->select('owncloud_name', 'ldap_dn') |
|
206
|
|
|
->from($this->getTableName(false)) |
|
207
|
|
|
->where($qb->expr()->in('ldap_dn', $qb->createNamedParameter($dnList, QueryBuilder::PARAM_STR_ARRAY))); |
|
208
|
|
|
return $qb; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
protected function collectResultsFromListOfIdsQuery(IQueryBuilder $qb, array &$results): void { |
|
212
|
|
|
$stmt = $qb->execute(); |
|
213
|
|
|
while ($entry = $stmt->fetch(\Doctrine\DBAL\FetchMode::ASSOCIATIVE)) { |
|
214
|
|
|
$results[$entry['ldap_dn']] = $entry['owncloud_name']; |
|
215
|
|
|
$this->cache[$entry['ldap_dn']] = $entry['owncloud_name']; |
|
216
|
|
|
} |
|
217
|
|
|
$stmt->closeCursor(); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function getListOfIdsByDn(array $fdns): array { |
|
221
|
|
|
$totalDBParamLimit = 65000; |
|
222
|
|
|
$sliceSize = 1000; |
|
223
|
|
|
$maxSlices = $totalDBParamLimit / $sliceSize; |
|
224
|
|
|
$results = []; |
|
225
|
|
|
|
|
226
|
|
|
$slice = 1; |
|
227
|
|
|
$fdnsSlice = count($fdns) > $sliceSize ? array_slice($fdns, 0, $sliceSize) : $fdns; |
|
228
|
|
|
$qb = $this->prepareListOfIdsQuery($fdnsSlice); |
|
229
|
|
|
|
|
230
|
|
|
while (isset($fdnsSlice[999])) { |
|
231
|
|
|
// Oracle does not allow more than 1000 values in the IN list, |
|
232
|
|
|
// but allows slicing |
|
233
|
|
|
$slice++; |
|
234
|
|
|
$fdnsSlice = array_slice($fdns, $sliceSize * ($slice - 1), $sliceSize); |
|
235
|
|
|
|
|
236
|
|
|
/** @see https://github.com/vimeo/psalm/issues/4995 */ |
|
237
|
|
|
/** @psalm-suppress TypeDoesNotContainType */ |
|
238
|
|
|
if (!isset($qb)) { |
|
239
|
|
|
$qb = $this->prepareListOfIdsQuery($fdnsSlice); |
|
240
|
|
|
continue; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
if (!empty($fdnsSlice)) { |
|
244
|
|
|
$qb->orWhere($qb->expr()->in('ldap_dn', $qb->createNamedParameter($fdnsSlice, QueryBuilder::PARAM_STR_ARRAY))); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
if ($slice % $maxSlices === 0) { |
|
248
|
|
|
$this->collectResultsFromListOfIdsQuery($qb, $results); |
|
249
|
|
|
unset($qb); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
if (isset($qb)) { |
|
254
|
|
|
$this->collectResultsFromListOfIdsQuery($qb, $results); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return $results; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Searches mapped names by the giving string in the name column |
|
262
|
|
|
* |
|
263
|
|
|
* @param string $search |
|
264
|
|
|
* @param string $prefixMatch |
|
265
|
|
|
* @param string $postfixMatch |
|
266
|
|
|
* @return string[] |
|
267
|
|
|
*/ |
|
268
|
|
|
public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
|
269
|
|
|
$statement = $this->dbc->prepare(' |
|
270
|
|
|
SELECT `owncloud_name` |
|
271
|
|
|
FROM `' . $this->getTableName() . '` |
|
272
|
|
|
WHERE `owncloud_name` LIKE ? |
|
273
|
|
|
'); |
|
274
|
|
|
|
|
275
|
|
|
try { |
|
276
|
|
|
$res = $statement->execute([$prefixMatch . $this->dbc->escapeLikeParameter($search) . $postfixMatch]); |
|
277
|
|
|
} catch (Exception $e) { |
|
278
|
|
|
return []; |
|
279
|
|
|
} |
|
280
|
|
|
$names = []; |
|
281
|
|
|
while ($row = $res->fetch()) { |
|
282
|
|
|
$names[] = $row['owncloud_name']; |
|
283
|
|
|
} |
|
284
|
|
|
return $names; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Gets the name based on the provided LDAP UUID. |
|
289
|
|
|
* |
|
290
|
|
|
* @param string $uuid |
|
291
|
|
|
* @return string|false |
|
292
|
|
|
*/ |
|
293
|
|
|
public function getNameByUUID($uuid) { |
|
294
|
|
|
return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
public function getDnByUUID($uuid) { |
|
298
|
|
|
return $this->getXbyY('ldap_dn', 'directory_uuid', $uuid); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Gets the UUID based on the provided LDAP DN |
|
303
|
|
|
* |
|
304
|
|
|
* @param string $dn |
|
305
|
|
|
* @return false|string |
|
306
|
|
|
* @throws \Exception |
|
307
|
|
|
*/ |
|
308
|
|
|
public function getUUIDByDN($dn) { |
|
309
|
|
|
return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* gets a piece of the mapping list |
|
314
|
|
|
* |
|
315
|
|
|
* @param int $offset |
|
316
|
|
|
* @param int $limit |
|
317
|
|
|
* @return array |
|
318
|
|
|
*/ |
|
319
|
|
|
public function getList($offset = null, $limit = null) { |
|
320
|
|
|
$query = $this->dbc->prepare(' |
|
321
|
|
|
SELECT |
|
322
|
|
|
`ldap_dn` AS `dn`, |
|
323
|
|
|
`owncloud_name` AS `name`, |
|
324
|
|
|
`directory_uuid` AS `uuid` |
|
325
|
|
|
FROM `' . $this->getTableName() . '`', |
|
326
|
|
|
$limit, |
|
327
|
|
|
$offset |
|
328
|
|
|
); |
|
329
|
|
|
|
|
330
|
|
|
$query->execute(); |
|
331
|
|
|
return $query->fetchAll(); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* attempts to map the given entry |
|
336
|
|
|
* |
|
337
|
|
|
* @param string $fdn fully distinguished name (from LDAP) |
|
338
|
|
|
* @param string $name |
|
339
|
|
|
* @param string $uuid a unique identifier as used in LDAP |
|
340
|
|
|
* @return bool |
|
341
|
|
|
*/ |
|
342
|
|
|
public function map($fdn, $name, $uuid) { |
|
343
|
|
|
if (mb_strlen($fdn) > 255) { |
|
344
|
|
|
\OC::$server->getLogger()->error( |
|
345
|
|
|
'Cannot map, because the DN exceeds 255 characters: {dn}', |
|
346
|
|
|
[ |
|
347
|
|
|
'app' => 'user_ldap', |
|
348
|
|
|
'dn' => $fdn, |
|
349
|
|
|
] |
|
350
|
|
|
); |
|
351
|
|
|
return false; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
$row = [ |
|
355
|
|
|
'ldap_dn' => $fdn, |
|
356
|
|
|
'owncloud_name' => $name, |
|
357
|
|
|
'directory_uuid' => $uuid |
|
358
|
|
|
]; |
|
359
|
|
|
|
|
360
|
|
|
try { |
|
361
|
|
|
$result = $this->dbc->insertIfNotExist($this->getTableName(), $row); |
|
362
|
|
|
if ((bool)$result === true) { |
|
363
|
|
|
$this->cache[$fdn] = $name; |
|
364
|
|
|
} |
|
365
|
|
|
// insertIfNotExist returns values as int |
|
366
|
|
|
return (bool)$result; |
|
367
|
|
|
} catch (\Exception $e) { |
|
368
|
|
|
return false; |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* removes a mapping based on the owncloud_name of the entry |
|
374
|
|
|
* |
|
375
|
|
|
* @param string $name |
|
376
|
|
|
* @return bool |
|
377
|
|
|
*/ |
|
378
|
|
|
public function unmap($name) { |
|
379
|
|
|
$statement = $this->dbc->prepare(' |
|
380
|
|
|
DELETE FROM `' . $this->getTableName() . '` |
|
381
|
|
|
WHERE `owncloud_name` = ?'); |
|
382
|
|
|
|
|
383
|
|
|
return $this->modify($statement, [$name]); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
/** |
|
387
|
|
|
* Truncates the mapping table |
|
388
|
|
|
* |
|
389
|
|
|
* @return bool |
|
390
|
|
|
*/ |
|
391
|
|
|
public function clear() { |
|
392
|
|
|
$sql = $this->dbc |
|
393
|
|
|
->getDatabasePlatform() |
|
394
|
|
|
->getTruncateTableSQL('`' . $this->getTableName() . '`'); |
|
395
|
|
|
try { |
|
396
|
|
|
$this->dbc->executeQuery($sql); |
|
397
|
|
|
|
|
398
|
|
|
return true; |
|
399
|
|
|
} catch (Exception $e) { |
|
400
|
|
|
return false; |
|
401
|
|
|
} |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* clears the mapping table one by one and executing a callback with |
|
406
|
|
|
* each row's id (=owncloud_name col) |
|
407
|
|
|
* |
|
408
|
|
|
* @param callable $preCallback |
|
409
|
|
|
* @param callable $postCallback |
|
410
|
|
|
* @return bool true on success, false when at least one row was not |
|
411
|
|
|
* deleted |
|
412
|
|
|
*/ |
|
413
|
|
|
public function clearCb(callable $preCallback, callable $postCallback): bool { |
|
414
|
|
|
$picker = $this->dbc->getQueryBuilder(); |
|
415
|
|
|
$picker->select('owncloud_name') |
|
416
|
|
|
->from($this->getTableName()); |
|
417
|
|
|
$cursor = $picker->execute(); |
|
418
|
|
|
$result = true; |
|
419
|
|
|
while ($id = $cursor->fetchOne()) { |
|
420
|
|
|
$preCallback($id); |
|
421
|
|
|
if ($isUnmapped = $this->unmap($id)) { |
|
422
|
|
|
$postCallback($id); |
|
423
|
|
|
} |
|
424
|
|
|
$result &= $isUnmapped; |
|
425
|
|
|
} |
|
426
|
|
|
$cursor->closeCursor(); |
|
427
|
|
|
return $result; |
|
|
|
|
|
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* returns the number of entries in the mappings table |
|
432
|
|
|
* |
|
433
|
|
|
* @return int |
|
434
|
|
|
*/ |
|
435
|
|
|
public function count() { |
|
436
|
|
|
$qb = $this->dbc->getQueryBuilder(); |
|
437
|
|
|
$query = $qb->select($qb->func()->count('ldap_dn')) |
|
438
|
|
|
->from($this->getTableName()); |
|
439
|
|
|
$res = $query->execute(); |
|
440
|
|
|
$count = $res->fetchOne(); |
|
441
|
|
|
$res->closeCursor(); |
|
442
|
|
|
return (int)$count; |
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
|