MediaWiki master
wrapOldPasswords.php
Go to the documentation of this file.
1<?php
30
31// @codeCoverageIgnoreStart
32require_once __DIR__ . '/Maintenance.php';
33// @codeCoverageIgnoreEnd
34
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
111 $updateUsers = [];
112 foreach ( $res as $row ) {
113 $user = User::newFromId( $row->user_id );
115 $password = $passwordFactory->newFromCiphertext( $row->user_password );
116 '@phan-var ParameterizedPassword $password';
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;
173require_once RUN_MAINTENANCE_IF_MAIN;
174// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DB servers to catch up.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB handle.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
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.
User class for the MediaWiki software.
Definition User.php:119
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.