3 * Maintenance script to wrap all old-style passwords in a layered type
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
26 use MediaWiki\MediaWikiServices
;
29 * Maintenance script to wrap all passwords of a certain type in a specified layered
30 * type that wraps around the old type.
33 * @ingroup Maintenance
35 class WrapOldPasswords
extends Maintenance
{
36 public function __construct() {
37 parent
::__construct();
38 $this->addDescription( 'Wrap all passwords of a certain type in a new layered type. '
39 . 'The script runs in dry-run mode by default (use --update to update rows)' );
40 $this->addOption( 'type',
41 'Password type to wrap passwords in (must inherit LayeredParameterizedPassword)', true, true );
42 $this->addOption( 'verbose', 'Enables verbose output', false, false, 'v' );
43 $this->addOption( 'update', 'Actually wrap passwords', false, false, 'u' );
44 $this->setBatchSize( 100 );
47 public function execute() {
48 $passwordFactory = MediaWikiServices
::getInstance()->getPasswordFactory();
50 $typeInfo = $passwordFactory->getTypes();
51 $layeredType = $this->getOption( 'type' );
53 // Check that type exists and is a layered type
54 if ( !isset( $typeInfo[$layeredType] ) ) {
55 $this->fatalError( 'Undefined password type' );
58 $passObj = $passwordFactory->newFromType( $layeredType );
59 if ( !$passObj instanceof LayeredParameterizedPassword
) {
60 $this->fatalError( 'Layered parameterized password type must be used.' );
63 // Extract the first layer type
64 $typeConfig = $typeInfo[$layeredType];
65 $firstType = $typeConfig['types'][0];
67 $update = $this->hasOption( 'update' );
69 // Get a list of password types that are applicable
70 $dbw = $this->getDB( DB_MASTER
);
71 $typeCond = 'user_password' . $dbw->buildLike( ":$firstType:", $dbw->anyString() );
75 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
78 $this->beginTransaction( $dbw, __METHOD__
);
81 $res = $dbw->select( 'user',
82 [ 'user_id', 'user_name', 'user_password' ],
84 'user_id > ' . $dbw->addQuotes( $minUserId ),
89 'ORDER BY' => 'user_id',
90 'LIMIT' => $this->getBatchSize(),
95 /** @var User[] $updateUsers */
97 foreach ( $res as $row ) {
98 $user = User
::newFromId( $row->user_id
);
99 /** @var ParameterizedPassword $password */
100 $password = $passwordFactory->newFromCiphertext( $row->user_password
);
101 '@phan-var ParameterizedPassword $password';
102 /** @var LayeredParameterizedPassword $layeredPassword */
103 $layeredPassword = $passwordFactory->newFromType( $layeredType );
104 '@phan-var LayeredParameterizedPassword $layeredPassword';
105 $layeredPassword->partialCrypt( $password );
107 if ( $this->hasOption( 'verbose' ) ) {
109 "Updating password for user {$row->user_name} ({$row->user_id}) from " .
110 "type {$password->getType()} to {$layeredPassword->getType()}.\n"
116 $updateUsers[] = $user;
117 $dbw->update( 'user',
118 [ 'user_password' => $layeredPassword->toString() ],
119 [ 'user_id' => $row->user_id
],
124 $minUserId = $row->user_id
;
128 $this->commitTransaction( $dbw, __METHOD__
);
129 $lbFactory->waitForReplication();
131 // Clear memcached so old passwords are wiped out
132 foreach ( $updateUsers as $user ) {
133 $user->clearSharedCache( 'refresh' );
136 } while ( $res->numRows() );
139 $this->output( "$count users rows updated." );
141 $this->output( "$count user rows found using old password formats. "
142 . "Run script again with --update to update these rows" );
147 $maintClass = WrapOldPasswords
::class;
148 require_once RUN_MAINTENANCE_IF_MAIN
;