Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions ldap/src/Managers/LDAPManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,96 @@ function () {
}
return \FOG\Db\SchemaReconciler::applyConstraints('ldap');
},
// 29 - the five columns a 1.5 table never had (forum 18300).
// CREATE TABLE IF NOT EXISTS left a 1.5 table as it was, and
// save() names every declared column, so each add or edit of a
// server failed with "Unknown column 'lsGroupNamAttr'".
function () {
return $this->_addMissing15Columns();
},
];
}
/**
* Adds the LDAPServers columns that a table created by 1.5 lacks, and
* backfills the existing rows to what 1.5 did without the column.
*
* A column that is already present is left alone, rows and all.
*
* @return bool|string true, or the database error
*/
private function _addMissing15Columns()
{
$columns = [
// 1.5 hard-coded (name=%s) as the group-name filter.
'lsGroupNamAttr' => [
'definition' => "VARCHAR(255) NOT NULL DEFAULT ''",
'backfill' => "UPDATE `LDAPServers` SET `lsGroupNamAttr` = 'name'",
],
// 1.5 had no display-name feature.
'lsDisplayNameEnabled' => [
'definition' => 'TINYINT(1) NOT NULL DEFAULT 0',
'backfill' => null,
],
'lsDisplayNameAttr' => [
'definition' => "VARCHAR(255) NOT NULL DEFAULT ''",
'backfill' => null,
],
// 1.5 chose LDAPS by port, with this exact list.
'lsIsLDAPs' => [
'definition' => 'TINYINT(1) NOT NULL DEFAULT 0',
'backfill' => 'UPDATE `LDAPServers` SET `lsIsLDAPs` = 1 '
. 'WHERE `lsPort` IN (636, 686, 3269, 7636)',
],
// 1.5 had no API sign-in through LDAP; keep it off.
'lsAllowAPI' => [
'definition' => 'TINYINT(1) NOT NULL DEFAULT 0',
'backfill' => null,
],
];
$rows = self::$DB->query(
"SELECT `COLUMN_NAME` AS `c` "
. "FROM `information_schema`.`COLUMNS` "
. "WHERE `TABLE_SCHEMA` = DATABASE() "
. "AND LOWER(`TABLE_NAME`) = 'ldapservers'"
)->fetch(\PDO::FETCH_ASSOC, 'fetch_all')->get();
if (self::$DB->error) {
// A failed read must not record the step as applied: it would
// never run again.
return self::$DB->error;
}
$have = array_map(
function ($row) {
return strtolower((string)($row['c'] ?? ''));
},
(array)$rows
);
if (!count($have)) {
// Step 0 creates the table, so this is not expected. Change
// nothing rather than guess.
return true;
}
foreach ($columns as $column => $spec) {
if (in_array(strtolower($column), $have, true)) {
continue;
}
$statements = [
sprintf(
'ALTER TABLE `LDAPServers` ADD COLUMN `%s` %s',
$column,
$spec['definition']
),
];
if ($spec['backfill']) {
$statements[] = $spec['backfill'];
}
foreach ($statements as $sql) {
self::$DB->query($sql);
if (self::$DB->error) {
return self::$DB->error;
}
}
}
return true;
}
/**
* Installs the plugin database non-destructively (create-if-absent +
Expand Down
131 changes: 131 additions & 0 deletions tests/ldap-upgrade-from-15-columns.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* Every LDAPServers column that 1.5 never created reaches an upgraded table.
*
* THE BUG THIS EXISTS FOR (forum topic 18300). A server upgraded from 1.5
* could not add or edit an LDAP server: "Add LDAP server failed!", with the
* database answering "Unknown column 'lsGroupNamAttr'". Every released 1.5.10
* LDAPServers table stops at lsEnableNestedGroup. createSql() declares the
* 1.6 columns, but CREATE TABLE IF NOT EXISTS is a no-op against a table that
* exists, and only the nested-group and TLS columns had an ADD COLUMN step.
* Reads hid it, because getColumns() drops an absent column. save() names
* every declared column, so every write failed.
*
* WHAT IS CHECKED. Each column in createSql() that is not in the pinned 1.5
* list must be added by a later step: an "ADD COLUMN `x`" in schema(), or an
* entry in _addMissing15Columns() that schema() calls.
*
* PINNED BY HAND: the 1.5 list is LDAPServers as install() created it in
* fogproject tag 1.5.10.2534, the last 1.5 release. This repository is fetched
* on its own (bin/fetch-plugins.sh), so it cannot read a fogproject checkout.
* 1.5 is closed, so the list cannot move.
*
* Usage: php tests/ldap-upgrade-from-15-columns.test.php
* Exit status 0 = pass, 1 = fail.
*
* PHP version 7.4+
*
* @category Tests
* @package FOGProject
* @author Tom Elliott <tommygunsster@gmail.com>
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link https://fogproject.org
*/

$root = dirname(__DIR__);
$file = $root . '/ldap/src/Managers/LDAPManager.php';
$failures = [];

// LDAPServers as 1.5.10.2534's LDAPManager::install() created it.
$released15 = [
'lsID', 'lsName', 'lsDesc', 'lsCreatedBy', 'lsAddress', 'lsCreatedTime',
'lsUserSearchDN', 'lsPort', 'lsUserNamAttr', 'lsGrpMemberAttr',
'lsAdminGroup', 'lsUserGroup', 'lsSearchScope', 'lsBindDN', 'lsBindPwd',
'lsGrpSearchDN', 'lsUseGroupMatch', 'lsUserFilter', 'lsEnableNestedGroup',
];

/**
* Lifts one method's body out of the source by matching braces.
*
* @param string $src the source, comments already stripped
* @param string $name the method name
*
* @return string
*/
function luMethodBody($src, $name)
{
$at = strpos($src, 'function ' . $name . '(');
if (false === $at) {
return '';
}
$open = strpos($src, '{', $at);
$depth = 0;
for ($i = $open, $n = strlen($src); $i < $n; $i++) {
if ($src[$i] === '{') {
$depth++;
}
if ($src[$i] === '}' && --$depth === 0) {
return substr($src, $open, $i - $open + 1);
}
}

return '';
}

// Strip comments, so a column named only in prose counts for nothing.
$src = '';
foreach (token_get_all(file_get_contents($file)) as $token) {
if (is_array($token) && in_array($token[0], [T_COMMENT, T_DOC_COMMENT], true)) {
continue;
}
$src .= is_array($token) ? $token[1] : $token;
}

// createSql()'s column list: the first [...] after the "true," argument.
$create = luMethodBody($src, 'createSql');
if (!preg_match('/true\s*,\s*\[(.*?)\]/s', $create, $m)) {
fwrite(STDERR, "FAIL: could not read the column list in createSql()\n");
exit(1);
}
preg_match_all("/'(ls[A-Za-z]+)'/", $m[1], $mm);
$declared = $mm[1];
if (count($declared) < count($released15)) {
fwrite(STDERR, "FAIL: createSql() lists fewer columns than 1.5 had\n");
exit(1);
}

$schema = luMethodBody($src, 'schema');
$helper = luMethodBody($src, '_addMissing15Columns');
$helperRuns = $helper !== ''
&& false !== strpos($schema, '_addMissing15Columns()');

$new = array_diff($declared, $released15);
foreach ($new as $column) {
$byAlter = (bool)preg_match(
'/ADD COLUMN `' . preg_quote($column, '/') . '`/',
$schema
);
$byHelper = $helperRuns && (bool)preg_match(
"/'" . preg_quote($column, '/') . "'\s*=>/",
$helper
);
if (!$byAlter && !$byHelper) {
$failures[] = sprintf(
'%s is in createSql() but no schema() step adds it to a table '
. 'created by 1.5, so save() fails there with Unknown column',
$column
);
}
}

if ($failures) {
foreach ($failures as $f) {
fwrite(STDERR, "FAIL: $f\n");
}
exit(1);
}
printf(
"PASS: %d LDAPServers columns added since 1.5 all reach an upgraded table\n",
count($new)
);
exit(0);
Loading