Commit 65e2e0e8 authored by lpsolit%gmail.com's avatar lpsolit%gmail.com

Bug 300551: Eliminate deprecated Bugzilla::DB routines from User.pm and Token.pm…

Bug 300551: Eliminate deprecated Bugzilla::DB routines from User.pm and Token.pm - Patch by Frédéric Buclin <LpSolit@gmail.com> r=wicked a=justdave
parent f4ec7d28
...@@ -88,36 +88,32 @@ sub IssueEmailChangeToken { ...@@ -88,36 +88,32 @@ sub IssueEmailChangeToken {
Bugzilla::BugMail::MessageToMTA($message); Bugzilla::BugMail::MessageToMTA($message);
} }
# Generates a random token, adds it to the tokens table, and sends it
# to the user with instructions for using it to change their password.
sub IssuePasswordToken { sub IssuePasswordToken {
# Generates a random token, adds it to the tokens table, and sends it my $loginname = shift;
# to the user with instructions for using it to change their password.
my ($loginname) = @_;
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
my $template = Bugzilla->template;
my $vars = {};
# Retrieve the user's ID from the database. # Retrieve the user's ID from the database.
my $quotedloginname = &::SqlQuote($loginname); trick_taint($loginname);
&::SendSQL("SELECT profiles.userid, tokens.issuedate FROM profiles my ($userid, $too_soon) =
LEFT JOIN tokens $dbh->selectrow_array('SELECT profiles.userid, tokens.issuedate
ON tokens.userid = profiles.userid FROM profiles
AND tokens.tokentype = 'password' LEFT JOIN tokens
AND tokens.issuedate > NOW() - " . ON tokens.userid = profiles.userid
$dbh->sql_interval(10, 'MINUTE') . " AND tokens.tokentype = ?
WHERE " . $dbh->sql_istrcmp('login_name', $quotedloginname)); AND tokens.issuedate > NOW() - ' .
my ($userid, $toosoon) = &::FetchSQLData(); $dbh->sql_interval(10, 'MINUTE') . '
WHERE ' . $dbh->sql_istrcmp('login_name', '?'),
if ($toosoon) { undef, ('password', $loginname));
ThrowUserError('too_soon_for_new_token');
}; ThrowUserError('too_soon_for_new_token') if $too_soon;
my ($token, $token_ts) = _create_token($userid, 'password', $::ENV{'REMOTE_ADDR'}); my ($token, $token_ts) = _create_token($userid, 'password', $::ENV{'REMOTE_ADDR'});
# Mail the user the token along with instructions for using it. # Mail the user the token along with instructions for using it.
my $template = Bugzilla->template;
my $vars = {};
$vars->{'token'} = $token; $vars->{'token'} = $token;
$vars->{'emailaddress'} = $loginname . Param('emailsuffix'); $vars->{'emailaddress'} = $loginname . Param('emailsuffix');
...@@ -143,9 +139,10 @@ sub IssueSessionToken { ...@@ -143,9 +139,10 @@ sub IssueSessionToken {
sub CleanTokenTable { sub CleanTokenTable {
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
$dbh->bz_lock_tables('tokens WRITE'); $dbh->bz_lock_tables('tokens WRITE');
&::SendSQL("DELETE FROM tokens WHERE " . $dbh->do('DELETE FROM tokens
$dbh->sql_to_days('NOW()') . " - " . WHERE ' . $dbh->sql_to_days('NOW()') . ' - ' .
$dbh->sql_to_days('issuedate') . " >= " . $maxtokenage); $dbh->sql_to_days('issuedate') . ' >= ?',
undef, $maxtokenage);
$dbh->bz_unlock_tables(); $dbh->bz_unlock_tables();
} }
...@@ -154,9 +151,8 @@ sub GenerateUniqueToken { ...@@ -154,9 +151,8 @@ sub GenerateUniqueToken {
# for the tokens themselves and checks uniqueness by searching for # for the tokens themselves and checks uniqueness by searching for
# the token in the "tokens" table. Gives up if it can't come up # the token in the "tokens" table. Gives up if it can't come up
# with a token after about one hundred tries. # with a token after about one hundred tries.
my ($table, $column) = @_; my ($table, $column) = @_;
my $token; my $token;
my $duplicate = 1; my $duplicate = 1;
my $tries = 0; my $tries = 0;
...@@ -175,30 +171,27 @@ sub GenerateUniqueToken { ...@@ -175,30 +171,27 @@ sub GenerateUniqueToken {
$sth->execute($token); $sth->execute($token);
$duplicate = $sth->fetchrow_array; $duplicate = $sth->fetchrow_array;
} }
return $token; return $token;
} }
# Cancels a previously issued token and notifies the system administrator.
# This should only happen when the user accidentally makes a token request
# or when a malicious hacker makes a token request on behalf of a user.
sub Cancel { sub Cancel {
# Cancels a previously issued token and notifies the system administrator.
# This should only happen when the user accidentally makes a token request
# or when a malicious hacker makes a token request on behalf of a user.
my ($token, $cancelaction, $vars) = @_; my ($token, $cancelaction, $vars) = @_;
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
$vars ||= {}; $vars ||= {};
# Quote the token for inclusion in SQL statements.
my $quotedtoken = &::SqlQuote($token);
# Get information about the token being cancelled. # Get information about the token being cancelled.
&::SendSQL("SELECT " . $dbh->sql_date_format('issuedate') . ", trick_taint($token);
tokentype , eventdata , login_name , realname my ($issuedate, $tokentype, $eventdata, $loginname, $realname) =
FROM tokens, profiles $dbh->selectrow_array('SELECT ' . $dbh->sql_date_format('issuedate') . ',
WHERE tokens.userid = profiles.userid tokentype , eventdata , login_name , realname
AND token = $quotedtoken"); FROM tokens
my ($issuedate, $tokentype, $eventdata, $loginname, $realname) = &::FetchSQLData(); INNER JOIN profiles
ON tokens.userid = profiles.userid
WHERE token = ?',
undef, $token);
# Get the email address of the Bugzilla maintainer. # Get the email address of the Bugzilla maintainer.
my $maintainer = Param('maintainer'); my $maintainer = Param('maintainer');
...@@ -228,53 +221,53 @@ sub Cancel { ...@@ -228,53 +221,53 @@ sub Cancel {
sub DeletePasswordTokens { sub DeletePasswordTokens {
my ($userid, $reason) = @_; my ($userid, $reason) = @_;
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
my $sth = $dbh->prepare("SELECT token " .
"FROM tokens " . detaint_natural($userid);
"WHERE userid=? AND tokentype='password'"); my $tokens = $dbh->selectcol_arrayref('SELECT token FROM tokens
$sth->execute($userid); WHERE userid = ? AND tokentype = ?',
while (my $token = $sth->fetchrow_array) { undef, ($userid, 'password'));
foreach my $token (@$tokens) {
Bugzilla::Token::Cancel($token, $reason); Bugzilla::Token::Cancel($token, $reason);
} }
} }
# Returns an email change token if the user has one.
sub HasEmailChangeToken { sub HasEmailChangeToken {
# Returns an email change token if the user has one. my $userid = shift;
my ($userid) = @_;
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
&::SendSQL("SELECT token FROM tokens WHERE userid = $userid " .
"AND (tokentype = 'emailnew' OR tokentype = 'emailold') " . my $token = $dbh->selectrow_array('SELECT token FROM tokens
$dbh->sql_limit(1)); WHERE userid = ?
my ($token) = &::FetchSQLData(); AND (tokentype = ? OR tokentype = ?) ' .
$dbh->sql_limit(1),
undef, ($userid, 'emailnew', 'emailold'));
return $token; return $token;
} }
# Returns the userid, issuedate and eventdata for the specified token
sub GetTokenData { sub GetTokenData {
# Returns the userid, issuedate and eventdata for the specified token
my ($token) = @_; my ($token) = @_;
my $dbh = Bugzilla->dbh;
return unless defined $token; return unless defined $token;
trick_taint($token); trick_taint($token);
my $dbh = Bugzilla->dbh;
return $dbh->selectrow_array( return $dbh->selectrow_array(
"SELECT userid, " . $dbh->sql_date_format('issuedate') . ", eventdata "SELECT userid, " . $dbh->sql_date_format('issuedate') . ", eventdata
FROM tokens FROM tokens
WHERE token = ?", undef, $token); WHERE token = ?", undef, $token);
} }
# Deletes specified token
sub DeleteToken { sub DeleteToken {
# Deletes specified token
my ($token) = @_; my ($token) = @_;
my $dbh = Bugzilla->dbh;
return unless defined $token; return unless defined $token;
trick_taint($token); trick_taint($token);
my $dbh = Bugzilla->dbh;
$dbh->bz_lock_tables('tokens WRITE'); $dbh->bz_lock_tables('tokens WRITE');
$dbh->do("DELETE FROM tokens WHERE token = ?", undef, $token); $dbh->do("DELETE FROM tokens WHERE token = ?", undef, $token);
$dbh->bz_unlock_tables(); $dbh->bz_unlock_tables();
...@@ -284,16 +277,16 @@ sub DeleteToken { ...@@ -284,16 +277,16 @@ sub DeleteToken {
# Internal Functions # Internal Functions
################################################################################ ################################################################################
# Generates a unique token and inserts it into the database
# Returns the token and the token timestamp
sub _create_token { sub _create_token {
# Generates a unique token and inserts it into the database
# Returns the token and the token timestamp
my ($userid, $tokentype, $eventdata) = @_; my ($userid, $tokentype, $eventdata) = @_;
my $dbh = Bugzilla->dbh;
detaint_natural($userid); detaint_natural($userid);
trick_taint($tokentype); trick_taint($tokentype);
trick_taint($eventdata); trick_taint($eventdata);
my $dbh = Bugzilla->dbh;
$dbh->bz_lock_tables('tokens WRITE'); $dbh->bz_lock_tables('tokens WRITE');
my $token = GenerateUniqueToken(); my $token = GenerateUniqueToken();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment