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