MediaWiki 1.41.2
wrapOldPasswords.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Wrap all passwords of a certain type in a new layered type. '
39 . 'The script runs in dry-run mode by default (use --update to update rows)' );
40 $this->addOption( 'type',
41 'Password type to wrap passwords in (must inherit LayeredParameterizedPassword)', true, true );
42 $this->addOption( 'verbose', 'Enables verbose output', false, false, 'v' );
43 $this->addOption( 'update', 'Actually wrap passwords', false, false, 'u' );
44 $this->setBatchSize( 3 );
45 }
46
47 public function execute() {
48 $passwordFactory = $this->getServiceContainer()->getPasswordFactory();
49
50 $typeInfo = $passwordFactory->getTypes();
51 $layeredType = $this->getOption( 'type' );
52
53 // Check that type exists and is a layered type
54 if ( !isset( $typeInfo[$layeredType] ) ) {
55 $this->fatalError( 'Undefined password type' );
56 }
57
58 $passObj = $passwordFactory->newFromType( $layeredType );
59 if ( !$passObj instanceof LayeredParameterizedPassword ) {
60 $this->fatalError( 'Layered parameterized password type must be used.' );
61 }
62
63 // Extract the first layer type
64 $typeConfig = $typeInfo[$layeredType];
65 $firstType = $typeConfig['types'][0];
66
67 $update = $this->hasOption( 'update' );
68
69 // Get a list of password types that are applicable
70 $dbw = $this->getDB( DB_PRIMARY );
71 $typeCond = 'user_password' . $dbw->buildLike( ":$firstType:", $dbw->anyString() );
72
73 $count = 0;
74 $minUserId = 0;
75 while ( true ) {
76 if ( $update ) {
77 $this->beginTransaction( $dbw, __METHOD__ );
78 }
79
80 $start = microtime( true );
81 $res = $dbw->newSelectQueryBuilder()
82 ->select( [ 'user_id', 'user_name', 'user_password' ] )
83 ->lockInShareMode()
84 ->from( 'user' )
85 ->where( [ 'user_id > ' . $dbw->addQuotes( $minUserId ), $typeCond ] )
86 ->orderBy( 'user_id' )
87 ->limit( $this->getBatchSize() )
88 ->caller( __METHOD__ )->fetchResultSet();
89
90 if ( $res->numRows() === 0 ) {
91 if ( $update ) {
92 $this->commitTransaction( $dbw, __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->update( 'user',
120 [ 'user_password' => $layeredPassword->toString() ],
121 [ 'user_id' => $row->user_id ],
122 __METHOD__
123 );
124 }
125
126 $minUserId = $row->user_id;
127 }
128
129 if ( $update ) {
130 $this->commitTransaction( $dbw, __METHOD__ );
131
132 // Clear memcached so old passwords are wiped out
133 foreach ( $updateUsers as $user ) {
134 $user->clearSharedCache( 'refresh' );
135 }
136 }
137
138 $this->output( "Last id processed: $minUserId; Actually updated: $count...\n" );
139 $delta = microtime( true ) - $start;
140 $this->output( sprintf(
141 "%4d passwords wrapped in %6.2fms (%6.2fms each)\n",
142 $res->numRows(),
143 $delta * 1000.0,
144 ( $delta / $res->numRows() ) * 1000.0
145 ) );
146 }
147
148 if ( $update ) {
149 $this->output( "$count users rows updated.\n" );
150 } else {
151 $this->output( "$count user rows found using old password formats. "
152 . "Run script again with --update to update these rows.\n" );
153 }
154 }
155}
156
157$maintClass = WrapOldPasswords::class;
158require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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:98
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.
const DB_PRIMARY
Definition defines.php:28