MediaWiki master
wrapOldPasswords.php
Go to the documentation of this file.
1<?php
9
13use MediaWiki\User\User;
16use Wikimedia\Timestamp\ConvertibleTimestamp;
17
18// @codeCoverageIgnoreStart
19require_once __DIR__ . '/Maintenance.php';
20// @codeCoverageIgnoreEnd
21
30 public function __construct() {
31 parent::__construct();
32 $this->addDescription( 'Wrap all passwords of a certain type in a new layered type. '
33 . 'The script runs in dry-run mode by default (use --update to update rows)' );
34 $this->addOption( 'type',
35 'Password type to wrap passwords in (must inherit LayeredParameterizedPassword)', true, true );
36 $this->addOption( 'verbose', 'Enables verbose output', false, false, 'v' );
37 $this->addOption( 'update', 'Actually wrap passwords', false, false, 'u' );
38 $this->setBatchSize( 3 );
39 }
40
41 public function execute() {
42 $passwordFactory = $this->getServiceContainer()->getPasswordFactory();
43
44 $typeInfo = $passwordFactory->getTypes();
45 $layeredType = $this->getOption( 'type' );
46
47 // Check that type exists and is a layered type
48 if ( !isset( $typeInfo[$layeredType] ) ) {
49 $this->fatalError( 'Undefined password type' );
50 }
51
52 $passObj = $passwordFactory->newFromType( $layeredType );
53 if ( !$passObj instanceof LayeredParameterizedPassword ) {
54 $this->fatalError( 'Layered parameterized password type must be used.' );
55 }
56
57 // Extract the first layer type
58 $typeConfig = $typeInfo[$layeredType];
59 $firstType = $typeConfig['types'][0];
60
61 $update = $this->hasOption( 'update' );
62
63 // Get a list of password types that are applicable
64 $dbw = $this->getPrimaryDB();
65
66 $count = 0;
67 $minUserId = 0;
68 while ( true ) {
69 if ( $update ) {
70 $this->beginTransactionRound( __METHOD__ );
71 }
72
73 $start = ConvertibleTimestamp::hrtime();
74 $res = $dbw->newSelectQueryBuilder()
75 ->select( [ 'user_id', 'user_name', 'user_password' ] )
76 ->lockInShareMode()
77 ->from( 'user' )
78 ->where( [
79 $dbw->expr( 'user_id', '>', $minUserId ),
80 $dbw->expr(
81 'user_password',
83 new LikeValue( ":$firstType:", $dbw->anyString() )
84 ),
85 ] )
86 ->orderBy( 'user_id' )
87 ->limit( $this->getBatchSize() )
88 ->caller( __METHOD__ )->fetchResultSet();
89
90 if ( $res->numRows() === 0 ) {
91 if ( $update ) {
92 $this->commitTransactionRound( __METHOD__ );
93 }
94 break;
95 }
96
98 $updateUsers = [];
99 foreach ( $res as $row ) {
100 $user = User::newFromId( $row->user_id );
102 $password = $passwordFactory->newFromCiphertext( $row->user_password );
103 '@phan-var ParameterizedPassword $password';
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->commitTransactionRound( __METHOD__ );
132
133 // Clear memcached so old passwords are wiped out
134 foreach ( $updateUsers as $user ) {
135 $user->clearSharedCache( 'refresh' );
136 }
137 }
138
139 $this->output( "Last id processed: $minUserId; Actually updated: $count...\n" );
140 $delta = ( ConvertibleTimestamp::hrtime() - $start ) / 1e9;
141 $this->output( sprintf(
142 "%4d passwords wrapped in %6.2fms (%6.2fms each)\n",
143 $res->numRows(),
144 $delta * 1000.0,
145 ( $delta / $res->numRows() ) * 1000.0
146 ) );
147 }
148
149 if ( $update ) {
150 $this->output( "$count users rows updated.\n" );
151 } else {
152 $this->output( "$count user rows found using old password formats. "
153 . "Run script again with --update to update these rows.\n" );
154 }
155 }
156}
157
158// @codeCoverageIgnoreStart
159$maintClass = WrapOldPasswords::class;
160require_once RUN_MAINTENANCE_IF_MAIN;
161// @codeCoverageIgnoreEnd
$maintClass
This password hash type layers one or more parameterized password types on top of each other.
Helper class for password hash types that have a delimited set of parameters inside the hash.
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:636
Content of like value.
Definition LikeValue.php:14
Maintenance script to wrap all passwords of a certain type in a specified layered type that wraps aro...