Skip to content

Commit

Permalink
Merge pull request #35756 from nextcloud/backport/31683/stable23
Browse files Browse the repository at this point in the history
[stable23] limit ldap_dn columns to 4000
  • Loading branch information
blizzz authored Jan 17, 2023
2 parents 01cfab5 + 167a542 commit 2cf8353
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 8 deletions.
1 change: 1 addition & 0 deletions apps/user_ldap/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154717' => $baseDir . '/../lib/Migration/Version1130Date20220110154717.php',
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154718' => $baseDir . '/../lib/Migration/Version1130Date20220110154718.php',
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154719' => $baseDir . '/../lib/Migration/Version1130Date20220110154719.php',
'OCA\\User_LDAP\\Migration\\Version1141Date20220323143801' => $baseDir . '/../lib/Migration/Version1141Date20220323143801.php',
'OCA\\User_LDAP\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
'OCA\\User_LDAP\\PagedResults\\IAdapter' => $baseDir . '/../lib/PagedResults/IAdapter.php',
'OCA\\User_LDAP\\PagedResults\\Php73' => $baseDir . '/../lib/PagedResults/Php73.php',
Expand Down
1 change: 1 addition & 0 deletions apps/user_ldap/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class ComposerStaticInitUser_LDAP
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154717' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154717.php',
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154718' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154718.php',
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154719' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154719.php',
'OCA\\User_LDAP\\Migration\\Version1141Date20220323143801' => __DIR__ . '/..' . '/../lib/Migration/Version1141Date20220323143801.php',
'OCA\\User_LDAP\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
'OCA\\User_LDAP\\PagedResults\\IAdapter' => __DIR__ . '/..' . '/../lib/PagedResults/IAdapter.php',
'OCA\\User_LDAP\\PagedResults\\Php73' => __DIR__ . '/..' . '/../lib/PagedResults/Php73.php',
Expand Down
4 changes: 2 additions & 2 deletions apps/user_ldap/lib/Mapping/AbstractMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ public function getList(int $offset = 0, int $limit = null, bool $invalidatedOnl
* @return bool
*/
public function map($fdn, $name, $uuid) {
if (mb_strlen($fdn) > 4096) {
if (mb_strlen($fdn) > 4000) {
\OC::$server->getLogger()->error(
'Cannot map, because the DN exceeds 4096 characters: {dn}',
'Cannot map, because the DN exceeds 4000 characters: {dn}',
[
'app' => 'user_ldap',
'dn' => $fdn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table = $schema->createTable('ldap_user_mapping');
$table->addColumn('ldap_dn', Types::STRING, [
'notnull' => true,
'length' => 255,
'length' => 4000,
'default' => '',
]);
$table->addColumn('owncloud_name', Types::STRING, [
Expand All @@ -73,7 +73,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table = $schema->createTable('ldap_group_mapping');
$table->addColumn('ldap_dn', Types::STRING, [
'notnull' => true,
'length' => 255,
'length' => 4000,
'default' => '',
]);
$table->addColumn('owncloud_name', Types::STRING, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
}
$column = $table->getColumn('ldap_dn');
if ($tableName === 'ldap_user_mapping') {
if ($column->getLength() < 4096) {
$column->setLength(4096);
if ($column->getLength() < 4000) {
$column->setLength(4000);
$changeSchema = true;
}

Expand All @@ -117,7 +117,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table2 = $schema->createTable('ldap_group_mapping_backup');
$table2->addColumn('ldap_dn', Types::STRING, [
'notnull' => true,
'length' => 4096,
'length' => 4000,
'default' => '',
]);
$table2->addColumn('owncloud_name', Types::STRING, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table = $schema->createTable('ldap_group_mapping');
$table->addColumn('ldap_dn', Types::STRING, [
'notnull' => true,
'length' => 4096,
'length' => 4000,
'default' => '',
]);
$table->addColumn('owncloud_name', Types::STRING, [
Expand Down
113 changes: 113 additions & 0 deletions apps/user_ldap/lib/Migration/Version1141Date20220323143801.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2022 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\User_LDAP\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version1141Date20220323143801 extends SimpleMigrationStep {
/** @var IDBConnection */
private $dbc;

public function __construct(IDBConnection $dbc) {
$this->dbc = $dbc;
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) {
$qb = $this->dbc->getQueryBuilder();
$qb->select('ldap_dn')
->from($tableName)
->where($qb->expr()->gt($qb->func()->octetLength('ldap_dn'), '4000', IQueryBuilder::PARAM_INT));

$dnsTooLong = [];
$result = $qb->executeQuery();
while (($dn = $result->fetchOne()) !== false) {
$dnsTooLong[] = $dn;
}
$result->closeCursor();
$this->shortenDNs($dnsTooLong, $tableName);
}
}

protected function shortenDNs(array $dns, string $table): void {
$qb = $this->dbc->getQueryBuilder();
$qb->update($table)
->set('ldap_dn', $qb->createParameter('shortenedDn'))
->where($qb->expr()->eq('ldap_dn', $qb->createParameter('originalDn')));

$pageSize = 1000;
$page = 0;
do {
$subset = array_slice($dns, $page * $pageSize, $pageSize);
try {
$this->dbc->beginTransaction();
foreach ($subset as $dn) {
$shortenedDN = mb_substr($dn, 0, 4000);
$qb->setParameter('shortenedDn', $shortenedDN);
$qb->setParameter('originalDn', $dn);
$qb->executeStatement();
}
$this->dbc->commit();
} catch (\Throwable $t) {
$this->dbc->rollBack();
throw $t;
}
$page++;
} while (count($subset) === $pageSize);
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) {
$table = $schema->getTable($tableName);
$column = $table->getColumn('ldap_dn');
if ($column->getLength() > 4000) {
$column->setLength(4000);
}
}

return $schema;
}
}

0 comments on commit 2cf8353

Please sign in to comment.