MediaWiki master
resetAuthenticationThrottle.php
Go to the documentation of this file.
1<?php
27use Wikimedia\IPUtils;
28
29require_once __DIR__ . '/Maintenance.php';
30
38
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Reset login/signup throttling for a specified user and/or IP. '
42 . "\n\n"
43 . 'When resetting signup or temp account, provide the IP. When resetting login (or both), provide '
44 . 'both username (as entered in login screen) and IP. An easy way to obtain them is '
45 . "the 'throttler' log channel." );
46 $this->addOption( 'login', 'Reset login throttle' );
47 $this->addOption( 'signup', 'Reset account creation throttle' );
48 $this->addOption( 'tempaccount', 'Reset temp account creation throttle' );
49 $this->addOption( 'tempaccountnameacquisition', 'Reset temp account name acquisition throttle' );
50 $this->addOption( 'user', 'Username to reset', false, true );
51 $this->addOption( 'ip', 'IP to reset', false, true );
52 }
53
54 public function execute() {
55 $forLogin = (bool)$this->getOption( 'login' );
56 $forSignup = (bool)$this->getOption( 'signup' );
57 $forTempAccount = (bool)$this->getOption( 'tempaccount' );
58 $forTempAccountNameAcquisition = (bool)$this->getOption( 'tempaccountnameacquisition' );
59 $username = $this->getOption( 'user' );
60 $ip = $this->getOption( 'ip' );
61
62 if ( !$forLogin && !$forSignup && !$forTempAccount && !$forTempAccountNameAcquisition ) {
63 $this->fatalError(
64 'At least one of --login, --signup, --tempaccount, or --tempaccountnameacquisition is required!'
65 );
66 } elseif ( $forLogin && ( $ip === null || $username === null ) ) {
67 $this->fatalError( '--user and --ip are both required when using --login!' );
68 } elseif ( $forSignup && $ip === null ) {
69 $this->fatalError( '--ip is required when using --signup!' );
70 } elseif ( $forTempAccount && $ip === null ) {
71 $this->fatalError( '--ip is required when using --tempaccount!' );
72 } elseif ( $forTempAccountNameAcquisition && $ip === null ) {
73 $this->fatalError( '--ip is required when using --tempaccountnameacquisition!' );
74 } elseif ( $ip !== null && !IPUtils::isValid( $ip ) ) {
75 $this->fatalError( "Not a valid IP: $ip" );
76 }
77
78 if ( $forLogin ) {
79 $this->clearLoginThrottle( $username, $ip );
80 }
81 if ( $forSignup ) {
82 $this->clearSignupThrottle( $ip );
83 }
84 if ( $forTempAccount ) {
86 }
87 if ( $forTempAccountNameAcquisition ) {
89 }
90
91 LoggerFactory::getInstance( 'throttler' )->info( 'Manually cleared {type} throttle', [
92 'type' => implode( ' and ', array_filter( [
93 $forLogin ? 'login' : null,
94 $forSignup ? 'signup' : null,
95 $forTempAccount ? 'tempaccount' : null,
96 $forTempAccountNameAcquisition ? 'tempaccountnameacquisition' : null,
97 ] ) ),
98 'username' => $username,
99 'ipKey' => $ip,
100 ] );
101 }
102
107 protected function clearLoginThrottle( $rawUsername, $ip ) {
108 $this->output( 'Clearing login throttle...' );
109
110 $passwordAttemptThrottle = $this->getConfig()->get( MainConfigNames::PasswordAttemptThrottle );
111 if ( !$passwordAttemptThrottle ) {
112 $this->output( "none set\n" );
113 return;
114 }
115
116 $throttler = new Throttler( $passwordAttemptThrottle, [
117 'type' => 'password',
118 'cache' => ObjectCache::getLocalClusterInstance(),
119 ] );
120 if ( $rawUsername !== null ) {
121 $usernames = $this->getServiceContainer()->getAuthManager()
122 ->normalizeUsername( $rawUsername );
123 if ( !$usernames ) {
124 $this->fatalError( "Not a valid username: $rawUsername" );
125 }
126 } else {
127 $usernames = [ null ];
128 }
129 foreach ( $usernames as $username ) {
130 $throttler->clear( $username, $ip );
131 }
132
133 $botPasswordThrottler = new Throttler( $passwordAttemptThrottle, [
134 'type' => 'botpassword',
135 'cache' => ObjectCache::getLocalClusterInstance(),
136 ] );
137 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
138 $botPasswordThrottler->clear( $username, $ip );
139
140 $this->output( "done\n" );
141 }
142
146 protected function clearSignupThrottle( $ip ) {
147 $this->output( 'Clearing signup throttle...' );
148
149 $accountCreationThrottle = $this->getConfig()->get( MainConfigNames::AccountCreationThrottle );
150 if ( !is_array( $accountCreationThrottle ) ) {
151 $accountCreationThrottle = [ [
152 'count' => $accountCreationThrottle,
153 'seconds' => 86400,
154 ] ];
155 }
156 if ( !$accountCreationThrottle ) {
157 $this->output( "none set\n" );
158 return;
159 }
160 $throttler = new Throttler( $accountCreationThrottle, [
161 'type' => 'acctcreate',
162 'cache' => ObjectCache::getLocalClusterInstance(),
163 ] );
164
165 $throttler->clear( null, $ip );
166
167 $this->output( "done\n" );
168 }
169
170 protected function clearTempAccountCreationThrottle( string $ip ): void {
171 $this->output( 'Clearing temp account creation throttle...' );
172
173 $tempAccountCreationThrottle = $this->getConfig()->get( MainConfigNames::TempAccountCreationThrottle );
174 if ( !$tempAccountCreationThrottle ) {
175 $this->output( "none set\n" );
176 return;
177 }
178 $throttler = new Throttler( $tempAccountCreationThrottle, [
179 'type' => 'tempacctcreate',
180 'cache' => ObjectCache::getLocalClusterInstance(),
181 ] );
182
183 $throttler->clear( null, $ip );
184
185 $this->output( "done\n" );
186 }
187
188 protected function clearTempAccountNameAcquisitionThrottle( string $ip ): void {
189 $this->output( 'Clearing temp account name acquisition throttle...' );
190
191 $tempAccountNameAcquisitionThrottle = $this->getConfig()->get(
192 MainConfigNames::TempAccountNameAcquisitionThrottle
193 );
194 if ( !$tempAccountNameAcquisitionThrottle ) {
195 $this->output( "none set\n" );
196 return;
197 }
198 $throttler = new Throttler( $tempAccountNameAcquisitionThrottle, [
199 'type' => 'tempacctnameacquisition',
200 'cache' => ObjectCache::getLocalClusterInstance(),
201 ] );
202
203 $throttler->clear( null, $ip );
204
205 $this->output( "done\n" );
206 }
207
208}
209
210$maintClass = ResetAuthenticationThrottle::class;
211require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Create PSR-3 logger objects.
A class containing constants representing the names of configuration variables.
Reset login/signup throttling for a specified user and/or IP.