Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WrapOldPasswords
0.00% covered (danger)
0.00%
0 / 72
0.00% covered (danger)
0.00%
0 / 2
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2/**
3 * Maintenance script to wrap all old-style passwords in a layered type
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\User\User;
25use Wikimedia\Rdbms\IExpression;
26use Wikimedia\Rdbms\LikeValue;
27
28require_once __DIR__ . '/Maintenance.php';
29
30/**
31 * Maintenance script to wrap all passwords of a certain type in a specified layered
32 * type that wraps around the old type.
33 *
34 * @since 1.24
35 * @ingroup Maintenance
36 */
37class WrapOldPasswords extends Maintenance {
38    public function __construct() {
39        parent::__construct();
40        $this->addDescription( 'Wrap all passwords of a certain type in a new layered type. '
41                    . 'The script runs in dry-run mode by default (use --update to update rows)' );
42        $this->addOption( 'type',
43            'Password type to wrap passwords in (must inherit LayeredParameterizedPassword)', true, true );
44        $this->addOption( 'verbose', 'Enables verbose output', false, false, 'v' );
45        $this->addOption( 'update', 'Actually wrap passwords', false, false, 'u' );
46        $this->setBatchSize( 100 );
47    }
48
49    public function execute() {
50        $passwordFactory = $this->getServiceContainer()->getPasswordFactory();
51
52        $typeInfo = $passwordFactory->getTypes();
53        $layeredType = $this->getOption( 'type' );
54
55        // Check that type exists and is a layered type
56        if ( !isset( $typeInfo[$layeredType] ) ) {
57            $this->fatalError( 'Undefined password type' );
58        }
59
60        $passObj = $passwordFactory->newFromType( $layeredType );
61        if ( !$passObj instanceof LayeredParameterizedPassword ) {
62            $this->fatalError( 'Layered parameterized password type must be used.' );
63        }
64
65        // Extract the first layer type
66        $typeConfig = $typeInfo[$layeredType];
67        $firstType = $typeConfig['types'][0];
68
69        $update = $this->hasOption( 'update' );
70
71        // Get a list of password types that are applicable
72        $dbw = $this->getPrimaryDB();
73
74        $count = 0;
75        $minUserId = 0;
76        do {
77            if ( $update ) {
78                $this->beginTransaction( $dbw, __METHOD__ );
79            }
80
81            $res = $dbw->newSelectQueryBuilder()
82                ->select( [ 'user_id', 'user_name', 'user_password' ] )
83                ->lockInShareMode()
84                ->from( 'user' )
85                ->where( [
86                    $dbw->expr( 'user_id', '>', $minUserId ),
87                    $dbw->expr(
88                        'user_password',
89                        IExpression::LIKE,
90                        new LikeValue( ":$firstType:", $dbw->anyString() )
91                    ),
92                ] )
93                ->orderBy( 'user_id' )
94                ->limit( $this->getBatchSize() )
95                ->caller( __METHOD__ )->fetchResultSet();
96
97            /** @var User[] $updateUsers */
98            $updateUsers = [];
99            foreach ( $res as $row ) {
100                $user = User::newFromId( $row->user_id );
101                /** @var ParameterizedPassword $password */
102                $password = $passwordFactory->newFromCiphertext( $row->user_password );
103                '@phan-var ParameterizedPassword $password';
104                /** @var LayeredParameterizedPassword $layeredPassword */
105                $layeredPassword = $passwordFactory->newFromType( $layeredType );
106                '@phan-var LayeredParameterizedPassword $layeredPassword';
107                $layeredPassword->partialCrypt( $password );
108
109                if ( $this->hasOption( 'verbose' ) ) {
110                    $this->output(
111                        "Updating password for user {$row->user_name} ({$row->user_id}) from " .
112                        "type {$password->getType()} to {$layeredPassword->getType()}.\n"
113                    );
114                }
115
116                $count++;
117                if ( $update ) {
118                    $updateUsers[] = $user;
119                    $dbw->newUpdateQueryBuilder()
120                        ->update( 'user' )
121                        ->set( [ 'user_password' => $layeredPassword->toString() ] )
122                        ->where( [ 'user_id' => $row->user_id ] )
123                        ->caller( __METHOD__ )
124                        ->execute();
125                }
126
127                $minUserId = $row->user_id;
128            }
129
130            if ( $update ) {
131                $this->commitTransaction( $dbw, __METHOD__ );
132                $this->waitForReplication();
133
134                // Clear memcached so old passwords are wiped out
135                foreach ( $updateUsers as $user ) {
136                    $user->clearSharedCache( 'refresh' );
137                }
138            }
139        } while ( $res->numRows() );
140
141        if ( $update ) {
142            $this->output( "$count users rows updated.\n" );
143        } else {
144            $this->output( "$count user rows found using old password formats. "
145                    . "Run script again with --update to update these rows.\n" );
146        }
147    }
148}
149
150$maintClass = WrapOldPasswords::class;
151require_once RUN_MAINTENANCE_IF_MAIN;