@@ -68,6 +68,7 @@ public function __construct(\OCP\IDBConnection $dbc) {
6868 public function isColNameValid ($ col ) {
6969 switch ($ col ) {
7070 case 'ldap_dn ' :
71+ case 'ldap_dn_hash ' :
7172 case 'owncloud_name ' :
7273 case 'directory_uuid ' :
7374 return true ;
@@ -151,11 +152,11 @@ public function setDNbyUUID($fdn, $uuid) {
151152 $ oldDn = $ this ->getDnByUUID ($ uuid );
152153 $ statement = $ this ->dbc ->prepare ('
153154 UPDATE ` ' . $ this ->getTableName () . '`
154- SET `ldap_dn` = ?
155+ SET `ldap_dn_hash` = ?, ` ldap_dn` = ?
155156 WHERE `directory_uuid` = ?
156157 ' );
157158
158- $ r = $ this ->modify ($ statement , [$ fdn , $ uuid ]);
159+ $ r = $ this ->modify ($ statement , [$ this -> getDNHash ( $ fdn ), $ fdn , $ uuid ]);
159160
160161 if ($ r && is_string ($ oldDn ) && isset ($ this ->cache [$ oldDn ])) {
161162 $ this ->cache [$ fdn ] = $ this ->cache [$ oldDn ];
@@ -178,12 +179,24 @@ public function setUUIDbyDN($uuid, $fdn) {
178179 $ statement = $ this ->dbc ->prepare ('
179180 UPDATE ` ' . $ this ->getTableName () . '`
180181 SET `directory_uuid` = ?
181- WHERE `ldap_dn ` = ?
182+ WHERE `ldap_dn_hash ` = ?
182183 ' );
183184
184185 unset($ this ->cache [$ fdn ]);
185186
186- return $ this ->modify ($ statement , [$ uuid , $ fdn ]);
187+ return $ this ->modify ($ statement , [$ uuid , $ this ->getDNHash ($ fdn )]);
188+ }
189+
190+ /**
191+ * Get the hash to store in database column ldap_dn_hash for a given dn
192+ */
193+ protected function getDNHash (string $ fdn ): string {
194+ $ hash = hash ('sha256 ' , $ fdn , false );
195+ if (is_string ($ hash )) {
196+ return $ hash ;
197+ } else {
198+ throw new \RuntimeException ('hash function did not return a string ' );
199+ }
187200 }
188201
189202 /**
@@ -194,16 +207,19 @@ public function setUUIDbyDN($uuid, $fdn) {
194207 */
195208 public function getNameByDN ($ fdn ) {
196209 if (!isset ($ this ->cache [$ fdn ])) {
197- $ this ->cache [$ fdn ] = $ this ->getXbyY ('owncloud_name ' , 'ldap_dn ' , $ fdn );
210+ $ this ->cache [$ fdn ] = $ this ->getXbyY ('owncloud_name ' , 'ldap_dn_hash ' , $ this -> getDNHash ( $ fdn) );
198211 }
199212 return $ this ->cache [$ fdn ];
200213 }
201214
202- protected function prepareListOfIdsQuery (array $ dnList ): IQueryBuilder {
215+ /**
216+ * @param array<string> $hashList
217+ */
218+ protected function prepareListOfIdsQuery (array $ hashList ): IQueryBuilder {
203219 $ qb = $ this ->dbc ->getQueryBuilder ();
204- $ qb ->select ('owncloud_name ' , 'ldap_dn ' )
220+ $ qb ->select ('owncloud_name ' , 'ldap_dn_hash ' , ' ldap_dn ' )
205221 ->from ($ this ->getTableName (false ))
206- ->where ($ qb ->expr ()->in ('ldap_dn ' , $ qb ->createNamedParameter ($ dnList , QueryBuilder::PARAM_STR_ARRAY )));
222+ ->where ($ qb ->expr ()->in ('ldap_dn_hash ' , $ qb ->createNamedParameter ($ hashList , QueryBuilder::PARAM_STR_ARRAY )));
207223 return $ qb ;
208224 }
209225
@@ -216,13 +232,18 @@ protected function collectResultsFromListOfIdsQuery(IQueryBuilder $qb, array &$r
216232 $ stmt ->closeCursor ();
217233 }
218234
235+ /**
236+ * @param array<string> $fdns
237+ * @return array<string,string>
238+ */
219239 public function getListOfIdsByDn (array $ fdns ): array {
220240 $ totalDBParamLimit = 65000 ;
221241 $ sliceSize = 1000 ;
222242 $ maxSlices = $ totalDBParamLimit / $ sliceSize ;
223243 $ results = [];
224244
225245 $ slice = 1 ;
246+ $ fdns = array_map ([$ this , 'getDNHash ' ], $ fdns );
226247 $ fdnsSlice = count ($ fdns ) > $ sliceSize ? array_slice ($ fdns , 0 , $ sliceSize ) : $ fdns ;
227248 $ qb = $ this ->prepareListOfIdsQuery ($ fdnsSlice );
228249
@@ -240,7 +261,7 @@ public function getListOfIdsByDn(array $fdns): array {
240261 }
241262
242263 if (!empty ($ fdnsSlice )) {
243- $ qb ->orWhere ($ qb ->expr ()->in ('ldap_dn ' , $ qb ->createNamedParameter ($ fdnsSlice , QueryBuilder::PARAM_STR_ARRAY )));
264+ $ qb ->orWhere ($ qb ->expr ()->in ('ldap_dn_hash ' , $ qb ->createNamedParameter ($ fdnsSlice , QueryBuilder::PARAM_STR_ARRAY )));
244265 }
245266
246267 if ($ slice % $ maxSlices === 0 ) {
@@ -305,7 +326,7 @@ public function getDnByUUID($uuid) {
305326 * @throws \Exception
306327 */
307328 public function getUUIDByDN ($ dn ) {
308- return $ this ->getXbyY ('directory_uuid ' , 'ldap_dn ' , $ dn );
329+ return $ this ->getXbyY ('directory_uuid ' , 'ldap_dn_hash ' , $ this -> getDNHash ( $ dn ) );
309330 }
310331
311332 /**
@@ -339,9 +360,9 @@ public function getList($offset = null, $limit = null) {
339360 * @return bool
340361 */
341362 public function map ($ fdn , $ name , $ uuid ) {
342- if (mb_strlen ($ fdn ) > 255 ) {
363+ if (mb_strlen ($ fdn ) > 4096 ) {
343364 \OC ::$ server ->getLogger ()->error (
344- 'Cannot map, because the DN exceeds 255 characters: {dn} ' ,
365+ 'Cannot map, because the DN exceeds 4096 characters: {dn} ' ,
345366 [
346367 'app ' => 'user_ldap ' ,
347368 'dn ' => $ fdn ,
@@ -351,6 +372,7 @@ public function map($fdn, $name, $uuid) {
351372 }
352373
353374 $ row = [
375+ 'ldap_dn_hash ' => $ this ->getDNHash ($ fdn ),
354376 'ldap_dn ' => $ fdn ,
355377 'owncloud_name ' => $ name ,
356378 'directory_uuid ' => $ uuid
@@ -438,7 +460,7 @@ public function clearCb(callable $preCallback, callable $postCallback): bool {
438460 */
439461 public function count () {
440462 $ qb = $ this ->dbc ->getQueryBuilder ();
441- $ query = $ qb ->select ($ qb ->func ()->count ('ldap_dn ' ))
463+ $ query = $ qb ->select ($ qb ->func ()->count ('ldap_dn_hash ' ))
442464 ->from ($ this ->getTableName ());
443465 $ res = $ query ->execute ();
444466 $ count = $ res ->fetchOne ();
0 commit comments