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