Modify notify_login_failures() for better scale. MDL-11661

This commit is contained in:
stronk7 2007-10-09 00:11:07 +00:00
parent e072450699
commit 4b3f69957e

View file

@ -5827,6 +5827,10 @@ function moodle_needs_upgrading() {
/** /**
* Notify admin users or admin user of any failed logins (since last notification). * Notify admin users or admin user of any failed logins (since last notification).
* *
* Note that this function must be only executed from the cron script
* It uses the cache_flags system to store temporary records, deleting them
* by name before finishing
*
* @uses $CFG * @uses $CFG
* @uses $db * @uses $db
* @uses HOURSECS * @uses HOURSECS
@ -5852,67 +5856,94 @@ function notify_login_failures() {
$CFG->notifyloginthreshold = 10; // default to something sensible. $CFG->notifyloginthreshold = 10; // default to something sensible.
} }
$notifyipsrs = $db->Execute('SELECT ip FROM '. $CFG->prefix .'log WHERE time > '. $CFG->lastnotifyfailure .' /// Get all the IPs with more than notifyloginthreshold failures since lastnotifyfailure
AND module=\'login\' AND action=\'error\' GROUP BY ip HAVING count(*) > '. $CFG->notifyloginthreshold); /// and insert them into the cache_flags temp table
$iprs = get_recordset_sql("SELECT ip, count(*)
FROM {$CFG->prefix}log
WHERE module = 'login'
AND action = 'error'
AND time > $CFG->lastnotifyfailure
GROUP BY ip
HAVING count(*) >= $CFG->notifyloginthreshold");
while ($iprec = rs_fetch_next_record($iprs)) {
if (!empty($iprec->ip)) {
set_cache_flag('login_failure_by_ip', $iprec->ip, '1', 0);
}
}
rs_close($iprs);
$notifyusersrs = $db->Execute('SELECT info FROM '. $CFG->prefix .'log WHERE time > '. $CFG->lastnotifyfailure .' /// Get all the INFOs with more than notifyloginthreshold failures since lastnotifyfailure
AND module=\'login\' AND action=\'error\' GROUP BY info HAVING count(*) > '. $CFG->notifyloginthreshold); /// and insert them into the cache_flags temp table
$infors = get_recordset_sql("SELECT info, count(*)
FROM {$CFG->prefix}log
WHERE module = 'login'
AND action = 'error'
AND time > $CFG->lastnotifyfailure
GROUP BY info
HAVING count(*) >= $CFG->notifyloginthreshold");
while ($inforec = rs_fetch_next_record($infors)) {
if (!empty($inforec->info)) {
set_cache_flag('login_failure_by_info', $inforec->info, '1', 0);
}
}
rs_close($infors);
if ($notifyipsrs) { /// Now, select all the login error logged records belonging to the ips and infos
$ipstr = ''; /// since lastnotifyfailure, that we have stored in the cache_flags table
while ($row = rs_fetch_next_record($notifyipsrs)) { $logsrs = get_recordset_sql("SELECT l.*, u.firstname, u.lastname
$ipstr .= "'". $row->ip ."',"; FROM {$CFG->prefix}log l
} JOIN {$CFG->prefix}cache_flags cf ON (l.ip = cf.name)
rs_close($notifyipsrs); LEFT JOIN {$CFG->prefix}user u ON (l.userid = u.id)
$ipstr = substr($ipstr,0,strlen($ipstr)-1); WHERE l.module = 'login'
} AND l.action = 'error'
if ($notifyusersrs) { AND l.time > $CFG->lastnotifyfailure
$userstr = ''; AND cf.flagtype = 'login_failure_by_ip'
while ($row = rs_fetch_next_record($notifyusersrs)) { UNION ALL
$userstr .= "'". $row->info ."',"; SELECT l.*, u.firstname, u.lastname
} FROM {$CFG->prefix}log l
rs_close($notifyusersrs); JOIN {$CFG->prefix}cache_flags cf ON (l.info = cf.name)
$userstr = substr($userstr,0,strlen($userstr)-1); LEFT JOIN {$CFG->prefix}user u ON (l.userid = u.id)
} WHERE l.module = 'login'
AND l.action = 'error'
AND l.time > $CFG->lastnotifyfailure
AND cf.flagtype = 'login_failure_by_info'
ORDER BY time DESC");
if (strlen($userstr) > 0 || strlen($ipstr) > 0) { /// Init some variables
$count = 0; $count = 0;
$logs = get_logs('time > '. $CFG->lastnotifyfailure .' AND module=\'login\' AND action=\'error\' ' $messages = '';
.((strlen($ipstr) > 0 && strlen($userstr) > 0) ? ' AND ( ip IN ('. $ipstr .') OR info IN ('. $userstr .') ) ' /// Iterate over the logs recordset
: ((strlen($ipstr) != 0) ? ' AND ip IN ('. $ipstr .') ' : ' AND info IN ('. $userstr .') ')), 'l.time DESC', '', '', $count); while ($log = rs_fetch_next_record($logsrs)) {
$log->time = userdate($log->time);
$messages .= get_string('notifyloginfailuresmessage','',$log)."\n";
$count++;
}
rs_close($logsrs);
// if we haven't run in the last hour and we have something useful to report and we are actually supposed to be reporting to somebody /// If we haven't run in the last hour and
if (is_array($recip) and count($recip) > 0 and ((time() - HOURSECS) > $CFG->lastnotifyfailure) /// we have something useful to report and we
and is_array($logs) and count($logs) > 0) { /// are actually supposed to be reporting to somebody
if ((time() - HOURSECS) > $CFG->lastnotifyfailure && $count > 0 && is_array($recip) && count($recip) > 0) {
$message = '';
$site = get_site(); $site = get_site();
$subject = get_string('notifyloginfailuressubject', '', format_string($site->fullname)); $subject = get_string('notifyloginfailuressubject', '', format_string($site->fullname));
$message .= get_string('notifyloginfailuresmessagestart', '', $CFG->wwwroot) /// Calculate the complete body of notification (start + messages + end)
.(($CFG->lastnotifyfailure != 0) ? '('.userdate($CFG->lastnotifyfailure).')' : '')."\n\n"; $body = get_string('notifyloginfailuresmessagestart', '', $CFG->wwwroot) .
foreach ($logs as $log) { (($CFG->lastnotifyfailure != 0) ? '('.userdate($CFG->lastnotifyfailure).')' : '')."\n\n" .
$log->time = userdate($log->time); $messages .
$message .= get_string('notifyloginfailuresmessage','',$log)."\n"; "\n\n".get_string('notifyloginfailuresmessageend','',$CFG->wwwroot)."\n\n";
}
$message .= "\n\n".get_string('notifyloginfailuresmessageend','',$CFG->wwwroot)."\n\n"; /// For each destination, send mail
foreach ($recip as $admin) { foreach ($recip as $admin) {
mtrace('Emailing '. $admin->username .' about '. count($logs) .' failed login attempts'); mtrace('Emailing '. $admin->username .' about '. $count .' failed login attempts');
email_to_user($admin,get_admin(),$subject,$message); email_to_user($admin,get_admin(), $subject, $body);
}
$conf = new object();
$conf->name = 'lastnotifyfailure';
$conf->value = time();
if ($current = get_record('config', 'name', 'lastnotifyfailure')) {
$conf->id = $current->id;
if (! update_record('config', $conf)) {
mtrace('Could not update last notify time');
} }
} else if (! insert_record('config', $conf)) { /// Update lastnotifyfailure with current time
mtrace('Could not set last notify time'); set_config('lastnotifyfailure', time());
}
}
} }
/// Finally, delete all the temp records we have created in cache_flags
delete_records_select('cache_flags', "flagtype IN ('login_failure_by_ip', 'login_failure_by_info')");
} }
/** /**