Skip to content

Commit

Permalink
fix: use md5 algo and drop virtual key usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ccailly committed Dec 18, 2023
1 parent 282446a commit 9e037be
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
72 changes: 72 additions & 0 deletions install/migrations/update_10.0.11_to_10.0.12.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2023 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

/**
* Update from 10.0.11 to 10.0.12
*
* @return bool for success (will die for most error)
**/
function update10011to10012()
{
/**
* @var \DBmysql $DB
* @var \Migration $migration
*/
global $DB, $migration;

$updateresult = true;
$ADDTODISPLAYPREF = [];
$DELFROMDISPLAYPREF = [];
$update_dir = __DIR__ . '/update_10.0.11_to_10.0.12/';

//TRANS: %s is the number of new version
$migration->displayTitle(sprintf(__('Update to %s'), '10.0.12'));
$migration->setVersion('10.0.12');

$update_scripts = scandir($update_dir);
foreach ($update_scripts as $update_script) {
if (preg_match('/\.php$/', $update_script) !== 1) {
continue;
}
require $update_dir . $update_script;
}

// ************ Keep it at the end **************
$migration->updateDisplayPrefs($ADDTODISPLAYPREF, $DELFROMDISPLAYPREF);

$migration->executeMigration();

return $updateresult;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,24 @@
*/

/**
* @var \DBmysql $DB
* @var \Migration $migration
*/

// Add user_dn_hash field
$migration->addField('glpi_users', 'user_dn_hash', 'varchar(255) GENERATED ALWAYS AS (sha2(`user_dn`,256)) VIRTUAL', [
$migration->addField('glpi_users', 'user_dn_hash', 'varchar(255)', [
'after' => 'user_dn',
]);

$migration->addPostQuery($DB->buildUpdate(
'glpi_users',
[
'user_dn_hash' => new \QueryExpression('MD5(`user_dn`)'),
],
[
'user_dn' => ['!=', null]
]
));

// Add user_dn_hash index
$migration->addKey('glpi_users', 'user_dn_hash');
2 changes: 1 addition & 1 deletion install/mysql/glpi-empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7610,7 +7610,7 @@ CREATE TABLE `glpi_users` (
`password_forget_token` char(40) DEFAULT NULL,
`password_forget_token_date` timestamp NULL DEFAULT NULL,
`user_dn` text,
`user_dn_hash` varchar(255) GENERATED ALWAYS AS (sha2(`user_dn`,256)) VIRTUAL,
`user_dn_hash` varchar(255),
`registration_number` varchar(255) DEFAULT NULL,
`show_count_on_tabs` tinyint DEFAULT NULL,
`refresh_views` int DEFAULT NULL,
Expand Down
20 changes: 17 additions & 3 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,14 @@ public function getFromDBbySyncField($value)
/**
* Retrieve a user from the database using it's dn.
*
* @since 0.84
*
* @param string $user_dn dn of the user
*
* @return boolean
*/
public function getFromDBbyDn($user_dn)
{
return $this->getFromDBByCrit([
'user_dn_hash' => hash('sha256', $user_dn)
'user_dn_hash' => md5($user_dn)
]);
}

Expand Down Expand Up @@ -892,6 +890,14 @@ public function post_addItem()
$right->add($affectation);
}
}

// Hash user_dn if set
if (isset($this->input['user_dn'])) {
$this->update([
'id' => $this->fields['id'],
'user_dn_hash' => md5($this->input['user_dn'])
]);
}
}


Expand Down Expand Up @@ -1187,6 +1193,14 @@ public function post_updateItem($history = true)
true
);
}

// Hash user_dn if is updated
if (in_array('user_dn', $this->updates)) {
$this->update([
'id' => $this->fields['id'],
'user_dn_hash' => md5($this->fields['user_dn'])
]);
}
}


Expand Down

0 comments on commit 9e037be

Please sign in to comment.