| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Maintenance\Maintenance; |
| 25 |
use MediaWiki\Password\LayeredParameterizedPassword; |
| 26 |
use MediaWiki\Password\ParameterizedPassword; |
| 27 |
use MediaWiki\User\User; |
| 28 |
use Wikimedia\Rdbms\IExpression; |
| 29 |
use Wikimedia\Rdbms\LikeValue; |
| 30 |
|
| 31 |
|
| 32 |
require_once __DIR__ . '/Maintenance.php'; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class WrapOldPasswords extends Maintenance { |
| 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 |
|
| 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 |
|
| 71 |
$typeConfig = $typeInfo[$layeredType]; |
| 72 |
$firstType = $typeConfig['types'][0]; |
| 73 |
|
| 74 |
$update = $this->hasOption( 'update' ); |
| 75 |
|
| 76 |
|
| 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 |
|
| 110 |
|
| 111 |
$updateUsers = []; |
| 112 |
foreach ( $res as $row ) { |
| 113 |
$user = User::newFromId( $row->user_id ); |
| 114 |
|
| 115 |
$password = $passwordFactory->newFromCiphertext( $row->user_password ); |
| 116 |
'@phan-var ParameterizedPassword $password'; |
| 117 |
|
| 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 |
|
| 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 |
|
| 172 |
$maintClass = WrapOldPasswords::class; |
| 173 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 174 |
|