MediaWiki master
wrapOldPasswords.php
Go to the documentation of this file.
1<?php
29
30// @codeCoverageIgnoreStart
31require_once __DIR__ . '/Maintenance.php';
32// @codeCoverageIgnoreEnd
33
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription( 'Wrap all passwords of a certain type in a new layered type. '
45 . 'The script runs in dry-run mode by default (use --update to update rows)' );
46 $this->addOption( 'type',
47 'Password type to wrap passwords in (must inherit LayeredParameterizedPassword)', true, true );
48 $this->addOption( 'verbose', 'Enables verbose output', false, false, 'v' );
49 $this->addOption( 'update', 'Actually wrap passwords', false, false, 'u' );
50 $this->setBatchSize( 3 );
51 }
52
53 public function execute() {
54 $passwordFactory = $this->getServiceContainer()->getPasswordFactory();
55
56 $typeInfo = $passwordFactory->getTypes();
57 $layeredType = $this->getOption( 'type' );
58
59 // Check that type exists and is a layered type
60 if ( !isset( $typeInfo[$layeredType] ) ) {
61 $this->fatalError( 'Undefined password type' );
62 }
63
64 $passObj = $passwordFactory->newFromType( $layeredType );
65 if ( !$passObj instanceof LayeredParameterizedPassword ) {
66 $this->fatalError( 'Layered parameterized password type must be used.' );
67 }
68
69 // Extract the first layer type
70 $typeConfig = $typeInfo[$layeredType];
71 $firstType = $typeConfig['types'][0];
72
73 $update = $this->hasOption( 'update' );
74
75 // Get a list of password types that are applicable
76 $dbw = $this->getPrimaryDB();
77
78 $count = 0;
79 $minUserId = 0;
80 while ( true ) {
81 if ( $update ) {
82 $this->beginTransaction( $dbw, __METHOD__ );
83 }
84
85 $start = microtime( true );
86 $res = $dbw->newSelectQueryBuilder()
87 ->select( [ 'user_id', 'user_name', 'user_password' ] )
88 ->lockInShareMode()
89 ->from( 'user' )
90 ->where( [
91 $dbw->expr( 'user_id', '>', $minUserId ),
92 $dbw->expr(
93 'user_password',
94 IExpression::LIKE,
95 new LikeValue( ":$firstType:", $dbw->anyString() )
96 ),
97 ] )
98 ->orderBy( 'user_id' )
99 ->limit( $this->getBatchSize() )
100 ->caller( __METHOD__ )->fetchResultSet();
101
102 if ( $res->numRows() === 0 ) {
103 if ( $update ) {
104 $this->commitTransaction( $dbw, __METHOD__ );
105 }
106 break;
107 }
108
110 $updateUsers = [];
111 foreach ( $res as $row ) {
112 $user = User::newFromId( $row->user_id );
114 $password = $passwordFactory->newFromCiphertext( $row->user_password );
115 '@phan-var ParameterizedPassword $password';
117 $layeredPassword = $passwordFactory->newFromType( $layeredType );
118 '@phan-var LayeredParameterizedPassword $layeredPassword';
119 $layeredPassword->partialCrypt( $password );
120
121 if ( $this->hasOption( 'verbose' ) ) {
122 $this->output(
123 "Updating password for user {$row->user_name} ({$row->user_id}) from " .
124 "type {$password->getType()} to {$layeredPassword->getType()}.\n"
125 );
126 }
127
128 $count++;
129 if ( $update ) {
130 $updateUsers[] = $user;
131 $dbw->newUpdateQueryBuilder()
132 ->update( 'user' )
133 ->set( [ 'user_password' => $layeredPassword->toString() ] )
134 ->where( [ 'user_id' => $row->user_id ] )
135 ->caller( __METHOD__ )
136 ->execute();
137 }
138
139 $minUserId = $row->user_id;
140 }
141
142 if ( $update ) {
143 $this->commitTransaction( $dbw, __METHOD__ );
144
145 // Clear memcached so old passwords are wiped out
146 foreach ( $updateUsers as $user ) {
147 $user->clearSharedCache( 'refresh' );
148 }
149 }
150
151 $this->output( "Last id processed: $minUserId; Actually updated: $count...\n" );
152 $delta = microtime( true ) - $start;
153 $this->output( sprintf(
154 "%4d passwords wrapped in %6.2fms (%6.2fms each)\n",
155 $res->numRows(),
156 $delta * 1000.0,
157 ( $delta / $res->numRows() ) * 1000.0
158 ) );
159 }
160
161 if ( $update ) {
162 $this->output( "$count users rows updated.\n" );
163 } else {
164 $this->output( "$count user rows found using old password formats. "
165 . "Run script again with --update to update these rows.\n" );
166 }
167 }
168}
169
170// @codeCoverageIgnoreStart
171$maintClass = WrapOldPasswords::class;
172require_once RUN_MAINTENANCE_IF_MAIN;
173// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
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.
internal since 1.36
Definition User.php:93
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...
execute()
Do the actual work.
__construct()
Default constructor.