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