1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
<?php
/**
* Helper for deleting unused local passwords.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Maintenance
*/
namespace MediaWiki\Maintenance;
use MediaWiki\Password\InvalidPassword;
use MediaWiki\Password\PasswordFactory;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\IExpression;
use Wikimedia\Rdbms\LikeValue;
use Wikimedia\Rdbms\RawSQLValue;
// @codeCoverageIgnoreStart
require_once __DIR__ . '/../Maintenance.php';
// @codeCoverageIgnoreEnd
/**
* Delete unused local passwords.
*
* Mainly intended to be used as a base class by authentication extensions to provide maintenance
* scripts which allow deleting local passwords for users who have another way of logging in.
* Such scripts would customize how to locate users who have other login methods and don't need
* local login anymore.
* Make sure to set LocalPasswordPrimaryAuthenticationProvider to loginOnly => true or disable it
* completely before running this, otherwise it might recreate passwords.
*
* This class can also be used directly to just delete all local passwords, or those for a specific
* user. Deleting all passwords is useful when the wiki has used local password login in the past
* but it has been disabled.
*/
class DeleteLocalPasswords extends Maintenance {
/** @var string|null User to run on, or null for all. */
protected $user;
/** @var int Number of deleted passwords. */
protected $total;
public function __construct() {
parent::__construct();
$this->addDescription( "Deletes local password for users." );
$this->setBatchSize( 1000 );
$this->addOption( 'user', 'If specified, only checks the given user', false, true );
$this->addOption( 'delete', 'Really delete. To prevent accidents, you must provide this flag.' );
$this->addOption( 'prefix', "Instead of deleting, make passwords invalid by prefixing with "
. "':null:'. Make sure PasswordConfig has a 'null' entry. This is meant for testing before "
. "hard delete." );
$this->addOption( 'unprefix', 'Instead of deleting, undo the effect of --prefix.' );
}
protected function initialize() {
if (
(int)$this->hasOption( 'delete' ) + (int)$this->hasOption( 'prefix' )
+ (int)$this->hasOption( 'unprefix' ) !== 1
) {
$this->fatalError( "Exactly one of the 'delete', 'prefix', 'unprefix' options must be used\n" );
}
if ( $this->hasOption( 'prefix' ) || $this->hasOption( 'unprefix' ) ) {
$passwordHashTypes = $this->getServiceContainer()->getPasswordFactory()->getTypes();
if (
!isset( $passwordHashTypes['null'] )
|| $passwordHashTypes['null']['class'] !== InvalidPassword::class
) {
$this->fatalError(
<<<'ERROR'
'null' password entry missing. To use password prefixing, add
$wgPasswordConfig['null'] = [ 'class' => InvalidPassword::class ];
to your configuration (and remove once the passwords were deleted).
ERROR
);
}
}
$user = $this->getOption( 'user', false );
if ( $user !== false ) {
$userNameUtils = $this->getServiceContainer()->getUserNameUtils();
$this->user = $userNameUtils->getCanonical( $user );
if ( $this->user === false ) {
$this->fatalError( "Invalid user name\n" );
}
}
}
public function execute() {
$this->initialize();
foreach ( $this->getUserBatches() as $userBatch ) {
$this->processUsers( $userBatch, $this->getUserDB() );
}
$this->output( "done. (wrote $this->total rows)\n" );
}
/**
* Get the primary DB handle for the current user batch. This is provided for the benefit
* of authentication extensions which subclass this and work with wiki farms.
* @return IDatabase
*/
protected function getUserDB() {
return $this->getPrimaryDB();
}
protected function processUsers( array $userBatch, IDatabase $dbw ) {
if ( !$userBatch ) {
return;
}
if ( $this->getOption( 'delete' ) ) {
$dbw->newUpdateQueryBuilder()
->update( 'user' )
->set( [ 'user_password' => PasswordFactory::newInvalidPassword()->toString() ] )
->where( [ 'user_name' => $userBatch ] )
->caller( __METHOD__ )->execute();
} elseif ( $this->getOption( 'prefix' ) ) {
$dbw->newUpdateQueryBuilder()
->update( 'user' )
->set( [
'user_password' => new RawSQLValue(
$dbw->buildConcat( [ $dbw->addQuotes( ':null:' ), 'user_password' ] )
)
] )
->where( [
$dbw->expr( 'user_password', IExpression::NOT_LIKE, new LikeValue( ':null:', $dbw->anyString() ) ),
$dbw->expr( 'user_password', '!=', PasswordFactory::newInvalidPassword()->toString() ),
$dbw->expr( 'user_password', '!=', null ),
'user_name' => $userBatch,
] )
->caller( __METHOD__ )->execute();
} elseif ( $this->getOption( 'unprefix' ) ) {
$dbw->newUpdateQueryBuilder()
->update( 'user' )
->set( [
'user_password' => new RawSQLValue(
$dbw->buildSubString( 'user_password', strlen( ':null:' ) + 1 )
)
] )
->where( [
$dbw->expr( 'user_password', IExpression::LIKE, new LikeValue( ':null:', $dbw->anyString() ) ),
'user_name' => $userBatch,
] )
->caller( __METHOD__ )->execute();
}
$this->total += $dbw->affectedRows();
$this->waitForReplication();
}
/**
* This method iterates through the requested users and returns their names in batches of
* self::$mBatchSize.
*
* Subclasses should reimplement this and locate users who use the specific authentication
* method. The default implementation just iterates through all users. Extensions that work
* with wikifarm should also update self::getUserDB() as necessary.
* @return \Generator
*/
protected function getUserBatches() {
if ( $this->user !== null ) {
$this->output( "\t ... querying '$this->user'\n" );
yield [ [ $this->user ] ];
return;
}
$lastUsername = '';
$dbw = $this->getPrimaryDB();
do {
$this->output( "\t ... querying from '$lastUsername'\n" );
$users = $dbw->newSelectQueryBuilder()
->select( 'user_name' )
->from( 'user' )
->where( $dbw->expr( 'user_name', '>', $lastUsername ) )
->orderBy( 'user_name ASC' )
->limit( $this->getBatchSize() )
->caller( __METHOD__ )->fetchFieldValues();
if ( $users ) {
yield $users;
$lastUsername = end( $users );
}
} while ( count( $users ) === $this->getBatchSize() );
}
}
/** @deprecated class alias since 1.43 */
class_alias( DeleteLocalPasswords::class, 'DeleteLocalPasswords' );
|