2001-07-11 09:29:21 +04:00
|
|
|
# -*- Mode: perl; indent-tabs-mode: nil -*-
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public
|
|
|
|
# License Version 1.1 (the "License"); you may not use this file
|
|
|
|
# except in compliance with the License. You may obtain a copy of
|
|
|
|
# the License at http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS
|
|
|
|
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
# implied. See the License for the specific language governing
|
|
|
|
# rights and limitations under the License.
|
|
|
|
#
|
|
|
|
# The Original Code is the Bugzilla Bug Tracking System.
|
|
|
|
#
|
|
|
|
# The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
# Corporation. Portions created by Netscape are
|
|
|
|
# Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
# Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s): Myk Melez <myk@mozilla.org>
|
2006-10-15 02:02:13 +04:00
|
|
|
# Frédéric Buclin <LpSolit@gmail.com>
|
2001-07-11 09:29:21 +04:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Module Initialization
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
# Make it harder for us to do dangerous things in Perl.
|
|
|
|
use strict;
|
|
|
|
|
2004-03-18 12:01:35 +03:00
|
|
|
# Bundle the functions in this file together into the "Bugzilla::Token" package.
|
2004-03-18 06:57:05 +03:00
|
|
|
package Bugzilla::Token;
|
2001-07-11 09:29:21 +04:00
|
|
|
|
2006-08-19 22:12:00 +04:00
|
|
|
use Bugzilla::Constants;
|
2003-09-14 10:05:23 +04:00
|
|
|
use Bugzilla::Error;
|
2006-06-02 14:50:15 +04:00
|
|
|
use Bugzilla::Mailer;
|
2005-07-01 05:44:19 +04:00
|
|
|
use Bugzilla::Util;
|
2002-08-29 13:25:54 +04:00
|
|
|
|
2002-05-03 10:37:47 +04:00
|
|
|
use Date::Format;
|
2005-07-01 05:44:19 +04:00
|
|
|
use Date::Parse;
|
2006-10-15 02:02:13 +04:00
|
|
|
use File::Basename;
|
|
|
|
|
|
|
|
use base qw(Exporter);
|
|
|
|
|
|
|
|
@Bugzilla::Token::EXPORT = qw(issue_session_token check_token_data delete_token);
|
2002-05-03 10:37:47 +04:00
|
|
|
|
|
|
|
################################################################################
|
2006-08-19 22:12:00 +04:00
|
|
|
# Public Functions
|
2002-05-03 10:37:47 +04:00
|
|
|
################################################################################
|
|
|
|
|
2006-08-19 22:12:00 +04:00
|
|
|
# Creates and sends a token to create a new user account.
|
|
|
|
# It assumes that the login has the correct format and is not already in use.
|
|
|
|
sub issue_new_user_account_token {
|
|
|
|
my $login_name = shift;
|
|
|
|
my $dbh = Bugzilla->dbh;
|
|
|
|
my $template = Bugzilla->template;
|
|
|
|
my $vars = {};
|
2002-05-03 10:37:47 +04:00
|
|
|
|
2006-08-19 22:12:00 +04:00
|
|
|
# Is there already a pending request for this login name? If yes, do not throw
|
|
|
|
# an error because the user may have lost his email with the token inside.
|
|
|
|
# But to prevent using this way to mailbomb an email address, make sure
|
|
|
|
# the last request is at least 10 minutes old before sending a new email.
|
|
|
|
|
|
|
|
my $pending_requests =
|
|
|
|
$dbh->selectrow_array('SELECT COUNT(*)
|
|
|
|
FROM tokens
|
|
|
|
WHERE tokentype = ?
|
|
|
|
AND ' . $dbh->sql_istrcmp('eventdata', '?') . '
|
|
|
|
AND issuedate > NOW() - ' . $dbh->sql_interval(10, 'MINUTE'),
|
|
|
|
undef, ('account', $login_name));
|
|
|
|
|
|
|
|
ThrowUserError('too_soon_for_new_token', {'type' => 'account'}) if $pending_requests;
|
|
|
|
|
|
|
|
my ($token, $token_ts) = _create_token(undef, 'account', $login_name);
|
|
|
|
|
|
|
|
$vars->{'email'} = $login_name . Bugzilla->params->{'emailsuffix'};
|
|
|
|
$vars->{'token_ts'} = $token_ts;
|
|
|
|
$vars->{'token'} = $token;
|
|
|
|
|
|
|
|
my $message;
|
|
|
|
$template->process('account/email/request-new.txt.tmpl', $vars, \$message)
|
|
|
|
|| ThrowTemplateError($template->error());
|
|
|
|
|
|
|
|
MessageToMTA($message);
|
|
|
|
}
|
2001-07-11 09:29:21 +04:00
|
|
|
|
2002-04-02 02:52:40 +04:00
|
|
|
sub IssueEmailChangeToken {
|
|
|
|
my ($userid, $old_email, $new_email) = @_;
|
2006-07-04 01:26:22 +04:00
|
|
|
my $email_suffix = Bugzilla->params->{'emailsuffix'};
|
2002-04-02 02:52:40 +04:00
|
|
|
|
2005-07-01 05:44:19 +04:00
|
|
|
my ($token, $token_ts) = _create_token($userid, 'emailold', $old_email . ":" . $new_email);
|
2002-05-03 10:37:47 +04:00
|
|
|
|
2005-07-01 05:44:19 +04:00
|
|
|
my $newtoken = _create_token($userid, 'emailnew', $old_email . ":" . $new_email);
|
2002-04-02 02:52:40 +04:00
|
|
|
|
|
|
|
# Mail the user the token along with instructions for using it.
|
|
|
|
|
2005-10-25 03:11:56 +04:00
|
|
|
my $template = Bugzilla->template;
|
|
|
|
my $vars = {};
|
2002-04-02 02:52:40 +04:00
|
|
|
|
2006-07-04 01:26:22 +04:00
|
|
|
$vars->{'oldemailaddress'} = $old_email . $email_suffix;
|
|
|
|
$vars->{'newemailaddress'} = $new_email . $email_suffix;
|
2002-05-03 10:37:47 +04:00
|
|
|
|
2006-07-14 01:55:43 +04:00
|
|
|
$vars->{'max_token_age'} = MAX_TOKEN_AGE;
|
2002-05-03 10:37:47 +04:00
|
|
|
$vars->{'token_ts'} = $token_ts;
|
2002-04-02 02:52:40 +04:00
|
|
|
|
2002-04-24 22:27:43 +04:00
|
|
|
$vars->{'token'} = $token;
|
2006-07-04 01:26:22 +04:00
|
|
|
$vars->{'emailaddress'} = $old_email . $email_suffix;
|
2002-04-02 02:52:40 +04:00
|
|
|
|
|
|
|
my $message;
|
2002-04-24 11:24:50 +04:00
|
|
|
$template->process("account/email/change-old.txt.tmpl", $vars, \$message)
|
2003-09-14 10:05:23 +04:00
|
|
|
|| ThrowTemplateError($template->error());
|
2002-04-02 02:52:40 +04:00
|
|
|
|
2006-06-02 14:50:15 +04:00
|
|
|
MessageToMTA($message);
|
2002-04-02 02:52:40 +04:00
|
|
|
|
2002-04-24 22:27:43 +04:00
|
|
|
$vars->{'token'} = $newtoken;
|
2006-07-04 01:26:22 +04:00
|
|
|
$vars->{'emailaddress'} = $new_email . $email_suffix;
|
2002-04-02 02:52:40 +04:00
|
|
|
|
|
|
|
$message = "";
|
2002-04-24 11:24:50 +04:00
|
|
|
$template->process("account/email/change-new.txt.tmpl", $vars, \$message)
|
2003-09-14 10:05:23 +04:00
|
|
|
|| ThrowTemplateError($template->error());
|
2002-04-02 02:52:40 +04:00
|
|
|
|
2006-06-02 14:50:15 +04:00
|
|
|
MessageToMTA($message);
|
2002-04-02 02:52:40 +04:00
|
|
|
}
|
|
|
|
|
2006-03-09 21:19:54 +03:00
|
|
|
# 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.
|
2001-07-11 09:29:21 +04:00
|
|
|
sub IssuePasswordToken {
|
2006-03-09 21:19:54 +03:00
|
|
|
my $loginname = shift;
|
2005-02-18 00:57:27 +03:00
|
|
|
my $dbh = Bugzilla->dbh;
|
2006-03-09 21:19:54 +03:00
|
|
|
my $template = Bugzilla->template;
|
|
|
|
my $vars = {};
|
2005-02-18 00:57:27 +03:00
|
|
|
|
2001-07-11 09:29:21 +04:00
|
|
|
# Retrieve the user's ID from the database.
|
2006-03-09 21:19:54 +03:00
|
|
|
trick_taint($loginname);
|
|
|
|
my ($userid, $too_soon) =
|
|
|
|
$dbh->selectrow_array('SELECT profiles.userid, tokens.issuedate
|
|
|
|
FROM profiles
|
|
|
|
LEFT JOIN tokens
|
|
|
|
ON tokens.userid = profiles.userid
|
|
|
|
AND tokens.tokentype = ?
|
|
|
|
AND tokens.issuedate > NOW() - ' .
|
|
|
|
$dbh->sql_interval(10, 'MINUTE') . '
|
|
|
|
WHERE ' . $dbh->sql_istrcmp('login_name', '?'),
|
|
|
|
undef, ('password', $loginname));
|
|
|
|
|
2006-08-19 22:12:00 +04:00
|
|
|
ThrowUserError('too_soon_for_new_token', {'type' => 'password'}) if $too_soon;
|
2001-07-11 09:29:21 +04:00
|
|
|
|
2005-07-01 05:44:19 +04:00
|
|
|
my ($token, $token_ts) = _create_token($userid, 'password', $::ENV{'REMOTE_ADDR'});
|
2001-07-11 09:29:21 +04:00
|
|
|
|
|
|
|
# Mail the user the token along with instructions for using it.
|
2002-04-26 10:01:30 +04:00
|
|
|
$vars->{'token'} = $token;
|
2006-07-04 01:26:22 +04:00
|
|
|
$vars->{'emailaddress'} = $loginname . Bugzilla->params->{'emailsuffix'};
|
2002-04-26 10:01:30 +04:00
|
|
|
|
2006-07-14 01:55:43 +04:00
|
|
|
$vars->{'max_token_age'} = MAX_TOKEN_AGE;
|
2002-05-03 10:37:47 +04:00
|
|
|
$vars->{'token_ts'} = $token_ts;
|
|
|
|
|
2002-04-26 10:01:30 +04:00
|
|
|
my $message = "";
|
2002-04-26 10:46:16 +04:00
|
|
|
$template->process("account/password/forgotten-password.txt.tmpl",
|
|
|
|
$vars, \$message)
|
2003-09-14 10:05:23 +04:00
|
|
|
|| ThrowTemplateError($template->error());
|
2002-04-26 10:01:30 +04:00
|
|
|
|
2006-06-02 14:50:15 +04:00
|
|
|
MessageToMTA($message);
|
2001-07-11 09:29:21 +04:00
|
|
|
}
|
|
|
|
|
2006-10-15 02:02:13 +04:00
|
|
|
sub issue_session_token {
|
2005-07-01 05:44:19 +04:00
|
|
|
# Generates a random token, adds it to the tokens table, and returns
|
|
|
|
# the token to the caller.
|
|
|
|
|
|
|
|
my $data = shift;
|
|
|
|
return _create_token(Bugzilla->user->id, 'session', $data);
|
|
|
|
}
|
2001-07-11 09:29:21 +04:00
|
|
|
|
2002-04-02 02:52:40 +04:00
|
|
|
sub CleanTokenTable {
|
2005-02-18 00:57:27 +03:00
|
|
|
my $dbh = Bugzilla->dbh;
|
|
|
|
$dbh->bz_lock_tables('tokens WRITE');
|
2006-03-09 21:19:54 +03:00
|
|
|
$dbh->do('DELETE FROM tokens
|
|
|
|
WHERE ' . $dbh->sql_to_days('NOW()') . ' - ' .
|
|
|
|
$dbh->sql_to_days('issuedate') . ' >= ?',
|
2006-07-14 01:55:43 +04:00
|
|
|
undef, MAX_TOKEN_AGE);
|
2005-02-18 00:57:27 +03:00
|
|
|
$dbh->bz_unlock_tables();
|
2002-04-02 02:52:40 +04:00
|
|
|
}
|
|
|
|
|
2001-07-11 09:29:21 +04:00
|
|
|
sub GenerateUniqueToken {
|
2005-09-02 01:39:21 +04:00
|
|
|
# Generates a unique random token. Uses generate_random_password
|
2001-07-11 09:29:21 +04:00
|
|
|
# for the tokens themselves and checks uniqueness by searching for
|
|
|
|
# the token in the "tokens" table. Gives up if it can't come up
|
|
|
|
# with a token after about one hundred tries.
|
2006-01-03 17:45:22 +03:00
|
|
|
my ($table, $column) = @_;
|
2006-03-09 21:19:54 +03:00
|
|
|
|
2001-07-11 09:29:21 +04:00
|
|
|
my $token;
|
|
|
|
my $duplicate = 1;
|
|
|
|
my $tries = 0;
|
2006-01-03 17:45:22 +03:00
|
|
|
$table ||= "tokens";
|
|
|
|
$column ||= "token";
|
2001-07-11 09:29:21 +04:00
|
|
|
|
2005-07-01 05:44:19 +04:00
|
|
|
my $dbh = Bugzilla->dbh;
|
2006-01-03 17:45:22 +03:00
|
|
|
my $sth = $dbh->prepare("SELECT userid FROM $table WHERE $column = ?");
|
2005-07-01 05:44:19 +04:00
|
|
|
|
|
|
|
while ($duplicate) {
|
2001-07-11 09:29:21 +04:00
|
|
|
++$tries;
|
|
|
|
if ($tries > 100) {
|
2003-09-14 10:05:23 +04:00
|
|
|
ThrowCodeError("token_generation_error");
|
2001-07-11 09:29:21 +04:00
|
|
|
}
|
2005-09-02 01:39:21 +04:00
|
|
|
$token = generate_random_password();
|
2005-07-01 05:44:19 +04:00
|
|
|
$sth->execute($token);
|
|
|
|
$duplicate = $sth->fetchrow_array;
|
2001-07-11 09:29:21 +04:00
|
|
|
}
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
2006-03-09 21:19:54 +03:00
|
|
|
# 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.
|
2001-07-11 09:29:21 +04:00
|
|
|
sub Cancel {
|
2005-10-25 03:11:56 +04:00
|
|
|
my ($token, $cancelaction, $vars) = @_;
|
2005-02-18 00:57:27 +03:00
|
|
|
my $dbh = Bugzilla->dbh;
|
2006-08-19 22:12:00 +04:00
|
|
|
my $template = Bugzilla->template;
|
2005-10-25 03:11:56 +04:00
|
|
|
$vars ||= {};
|
2005-02-18 00:57:27 +03:00
|
|
|
|
2001-07-11 09:29:21 +04:00
|
|
|
# Get information about the token being cancelled.
|
2006-03-09 21:19:54 +03:00
|
|
|
trick_taint($token);
|
2006-08-19 22:12:00 +04:00
|
|
|
my ($issuedate, $tokentype, $eventdata, $loginname) =
|
2006-03-09 21:19:54 +03:00
|
|
|
$dbh->selectrow_array('SELECT ' . $dbh->sql_date_format('issuedate') . ',
|
2006-08-19 22:12:00 +04:00
|
|
|
tokentype, eventdata, login_name
|
2006-03-09 21:19:54 +03:00
|
|
|
FROM tokens
|
2006-08-19 22:12:00 +04:00
|
|
|
LEFT JOIN profiles
|
2006-03-09 21:19:54 +03:00
|
|
|
ON tokens.userid = profiles.userid
|
|
|
|
WHERE token = ?',
|
|
|
|
undef, $token);
|
2001-07-11 09:29:21 +04:00
|
|
|
|
2006-08-19 22:12:00 +04:00
|
|
|
# If we are cancelling the creation of a new user account, then there
|
|
|
|
# is no entry in the 'profiles' table.
|
|
|
|
$loginname ||= $eventdata;
|
2006-07-04 01:26:22 +04:00
|
|
|
$vars->{'emailaddress'} = $loginname . Bugzilla->params->{'emailsuffix'};
|
2006-08-19 22:12:00 +04:00
|
|
|
$vars->{'maintainer'} = Bugzilla->params->{'maintainer'};
|
2002-04-02 02:52:40 +04:00
|
|
|
$vars->{'remoteaddress'} = $::ENV{'REMOTE_ADDR'};
|
2002-04-24 22:27:43 +04:00
|
|
|
$vars->{'token'} = $token;
|
2002-04-02 02:52:40 +04:00
|
|
|
$vars->{'tokentype'} = $tokentype;
|
|
|
|
$vars->{'issuedate'} = $issuedate;
|
|
|
|
$vars->{'eventdata'} = $eventdata;
|
|
|
|
$vars->{'cancelaction'} = $cancelaction;
|
2001-07-11 09:29:21 +04:00
|
|
|
|
2002-04-02 02:52:40 +04:00
|
|
|
# Notify the user via email about the cancellation.
|
2001-07-11 09:29:21 +04:00
|
|
|
|
2002-04-02 02:52:40 +04:00
|
|
|
my $message;
|
2002-04-24 11:24:50 +04:00
|
|
|
$template->process("account/cancel-token.txt.tmpl", $vars, \$message)
|
2003-09-14 10:05:23 +04:00
|
|
|
|| ThrowTemplateError($template->error());
|
2002-04-02 02:52:40 +04:00
|
|
|
|
2006-06-02 14:50:15 +04:00
|
|
|
MessageToMTA($message);
|
2001-07-11 09:29:21 +04:00
|
|
|
|
|
|
|
# Delete the token from the database.
|
2006-10-15 02:02:13 +04:00
|
|
|
delete_token($token);
|
2001-07-11 09:29:21 +04:00
|
|
|
}
|
|
|
|
|
2003-03-22 07:47:35 +03:00
|
|
|
sub DeletePasswordTokens {
|
|
|
|
my ($userid, $reason) = @_;
|
|
|
|
my $dbh = Bugzilla->dbh;
|
2006-03-09 21:19:54 +03:00
|
|
|
|
|
|
|
detaint_natural($userid);
|
|
|
|
my $tokens = $dbh->selectcol_arrayref('SELECT token FROM tokens
|
|
|
|
WHERE userid = ? AND tokentype = ?',
|
|
|
|
undef, ($userid, 'password'));
|
|
|
|
|
|
|
|
foreach my $token (@$tokens) {
|
2004-03-18 12:01:35 +03:00
|
|
|
Bugzilla::Token::Cancel($token, $reason);
|
2003-03-22 07:47:35 +03:00
|
|
|
}
|
2001-07-11 09:29:21 +04:00
|
|
|
}
|
|
|
|
|
2006-03-09 21:19:54 +03:00
|
|
|
# Returns an email change token if the user has one.
|
2002-04-02 02:52:40 +04:00
|
|
|
sub HasEmailChangeToken {
|
2006-03-09 21:19:54 +03:00
|
|
|
my $userid = shift;
|
2005-02-18 19:14:27 +03:00
|
|
|
my $dbh = Bugzilla->dbh;
|
2006-03-09 21:19:54 +03:00
|
|
|
|
|
|
|
my $token = $dbh->selectrow_array('SELECT token FROM tokens
|
|
|
|
WHERE userid = ?
|
|
|
|
AND (tokentype = ? OR tokentype = ?) ' .
|
|
|
|
$dbh->sql_limit(1),
|
|
|
|
undef, ($userid, 'emailnew', 'emailold'));
|
2002-04-02 02:52:40 +04:00
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
2006-03-09 21:19:54 +03:00
|
|
|
# Returns the userid, issuedate and eventdata for the specified token
|
2005-08-13 16:27:04 +04:00
|
|
|
sub GetTokenData {
|
2005-07-01 05:44:19 +04:00
|
|
|
my ($token) = @_;
|
2006-03-09 21:19:54 +03:00
|
|
|
my $dbh = Bugzilla->dbh;
|
|
|
|
|
2005-07-01 05:44:19 +04:00
|
|
|
return unless defined $token;
|
2006-10-15 02:02:13 +04:00
|
|
|
$token = clean_text($token);
|
2005-07-01 05:44:19 +04:00
|
|
|
trick_taint($token);
|
2006-03-09 21:19:54 +03:00
|
|
|
|
2005-07-01 05:44:19 +04:00
|
|
|
return $dbh->selectrow_array(
|
|
|
|
"SELECT userid, " . $dbh->sql_date_format('issuedate') . ", eventdata
|
|
|
|
FROM tokens
|
|
|
|
WHERE token = ?", undef, $token);
|
|
|
|
}
|
|
|
|
|
2006-03-09 21:19:54 +03:00
|
|
|
# Deletes specified token
|
2006-10-15 02:02:13 +04:00
|
|
|
sub delete_token {
|
2005-07-01 05:44:19 +04:00
|
|
|
my ($token) = @_;
|
2006-03-09 21:19:54 +03:00
|
|
|
my $dbh = Bugzilla->dbh;
|
|
|
|
|
2005-07-01 05:44:19 +04:00
|
|
|
return unless defined $token;
|
|
|
|
trick_taint($token);
|
|
|
|
|
|
|
|
$dbh->bz_lock_tables('tokens WRITE');
|
|
|
|
$dbh->do("DELETE FROM tokens WHERE token = ?", undef, $token);
|
|
|
|
$dbh->bz_unlock_tables();
|
|
|
|
}
|
|
|
|
|
2006-10-15 02:02:13 +04:00
|
|
|
# Given a token, makes sure it comes from the currently logged in user
|
|
|
|
# and match the expected event. Returns 1 on success, else displays a warning.
|
|
|
|
# Note: this routine must not be called while tables are locked as it will try
|
|
|
|
# to lock some tables itself, see CleanTokenTable().
|
|
|
|
sub check_token_data {
|
|
|
|
my ($token, $expected_action) = @_;
|
|
|
|
my $user = Bugzilla->user;
|
|
|
|
my $template = Bugzilla->template;
|
|
|
|
my $cgi = Bugzilla->cgi;
|
|
|
|
|
|
|
|
my ($creator_id, $date, $token_action) = GetTokenData($token);
|
|
|
|
unless ($creator_id
|
|
|
|
&& $creator_id == $user->id
|
|
|
|
&& $token_action eq $expected_action)
|
|
|
|
{
|
|
|
|
# Something is going wrong. Ask confirmation before processing.
|
|
|
|
# It is possible that someone tried to trick an administrator.
|
|
|
|
# In this case, we want to know his name!
|
|
|
|
require Bugzilla::User;
|
|
|
|
|
|
|
|
my $vars = {};
|
|
|
|
$vars->{'abuser'} = Bugzilla::User->new($creator_id)->identity;
|
|
|
|
$vars->{'token_action'} = $token_action;
|
|
|
|
$vars->{'expected_action'} = $expected_action;
|
|
|
|
$vars->{'script_name'} = basename($0);
|
|
|
|
|
|
|
|
# Now is a good time to remove old tokens from the DB.
|
|
|
|
CleanTokenTable();
|
|
|
|
|
|
|
|
# If no token was found, create a valid token for the given action.
|
|
|
|
unless ($creator_id) {
|
|
|
|
$token = issue_session_token($expected_action);
|
|
|
|
$cgi->param('token', $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
print $cgi->header();
|
|
|
|
|
|
|
|
$template->process('admin/confirm-action.html.tmpl', $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-07-01 05:44:19 +04:00
|
|
|
################################################################################
|
|
|
|
# Internal Functions
|
|
|
|
################################################################################
|
|
|
|
|
2006-03-09 21:19:54 +03:00
|
|
|
# Generates a unique token and inserts it into the database
|
|
|
|
# Returns the token and the token timestamp
|
2005-08-13 16:27:04 +04:00
|
|
|
sub _create_token {
|
2005-07-01 05:44:19 +04:00
|
|
|
my ($userid, $tokentype, $eventdata) = @_;
|
2006-03-09 21:19:54 +03:00
|
|
|
my $dbh = Bugzilla->dbh;
|
2005-07-01 05:44:19 +04:00
|
|
|
|
2006-08-26 00:14:58 +04:00
|
|
|
detaint_natural($userid) if defined $userid;
|
2005-07-01 05:44:19 +04:00
|
|
|
trick_taint($tokentype);
|
|
|
|
trick_taint($eventdata);
|
|
|
|
|
|
|
|
$dbh->bz_lock_tables('tokens WRITE');
|
|
|
|
|
|
|
|
my $token = GenerateUniqueToken();
|
|
|
|
|
|
|
|
$dbh->do("INSERT INTO tokens (userid, issuedate, token, tokentype, eventdata)
|
|
|
|
VALUES (?, NOW(), ?, ?, ?)", undef, ($userid, $token, $tokentype, $eventdata));
|
|
|
|
|
|
|
|
$dbh->bz_unlock_tables();
|
|
|
|
|
|
|
|
if (wantarray) {
|
|
|
|
my (undef, $token_ts, undef) = GetTokenData($token);
|
|
|
|
$token_ts = str2time($token_ts);
|
|
|
|
return ($token, $token_ts);
|
|
|
|
} else {
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
}
|
2002-04-02 02:52:40 +04:00
|
|
|
|
2001-07-11 09:29:21 +04:00
|
|
|
1;
|
2006-11-20 21:59:12 +03:00
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
Bugzilla::Token - Provides different routines to manage tokens.
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
use Bugzilla::Token;
|
|
|
|
|
|
|
|
Bugzilla::Token::issue_new_user_account_token($login_name);
|
|
|
|
Bugzilla::Token::IssueEmailChangeToken($user_id, $old_email, $new_email);
|
|
|
|
Bugzilla::Token::IssuePasswordToken($login_name);
|
|
|
|
Bugzilla::Token::DeletePasswordTokens($user_id, $reason);
|
|
|
|
Bugzilla::Token::Cancel($token, $cancelaction, $vars);
|
|
|
|
|
|
|
|
Bugzilla::Token::CleanTokenTable();
|
|
|
|
|
|
|
|
my $token = issue_session_token($event);
|
|
|
|
check_token_data($token, $event)
|
|
|
|
delete_token($token);
|
|
|
|
|
|
|
|
my $token = Bugzilla::Token::GenerateUniqueToken($table, $column);
|
|
|
|
my $token = Bugzilla::Token::HasEmailChangeToken($user_id);
|
|
|
|
my ($token, $date, $data) = Bugzilla::Token::GetTokenData($token);
|
|
|
|
|
|
|
|
=head1 SUBROUTINES
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
|
|
|
=item C<issue_new_user_account_token($login_name)>
|
|
|
|
|
|
|
|
Description: Creates and sends a token per email to the email address
|
|
|
|
requesting a new user account. It doesn't check whether
|
|
|
|
the user account already exists. The user will have to
|
|
|
|
use this token to confirm the creation of his user account.
|
|
|
|
|
|
|
|
Params: $login_name - The new login name requested by the user.
|
|
|
|
|
|
|
|
Returns: Nothing. It throws an error if the same user made the same
|
|
|
|
request in the last few minutes.
|
|
|
|
|
|
|
|
=item C<sub IssueEmailChangeToken($user_id, $old_email, $new_email)>
|
|
|
|
|
|
|
|
Description: Sends two distinct tokens per email to the old and new email
|
|
|
|
addresses to confirm the email address change for the given
|
|
|
|
user ID. These tokens remain valid for the next MAX_TOKEN_AGE days.
|
|
|
|
|
|
|
|
Params: $user_id - The user ID of the user account requesting a new
|
|
|
|
email address.
|
|
|
|
$old_email - The current (old) email address of the user.
|
|
|
|
$new_email - The new email address of the user.
|
|
|
|
|
|
|
|
Returns: Nothing.
|
|
|
|
|
|
|
|
=item C<IssuePasswordToken($login_name)>
|
|
|
|
|
|
|
|
Description: Sends a token per email to the given login name. This token
|
|
|
|
can be used to change the password (e.g. in case the user
|
|
|
|
cannot remember his password and wishes to enter a new one).
|
|
|
|
|
|
|
|
Params: $login_name - The login name of the user requesting a new password.
|
|
|
|
|
|
|
|
Returns: Nothing. It throws an error if the same user made the same
|
|
|
|
request in the last few minutes.
|
|
|
|
|
|
|
|
=item C<CleanTokenTable()>
|
|
|
|
|
|
|
|
Description: Removes all tokens older than MAX_TOKEN_AGE days from the DB.
|
|
|
|
This means that these tokens will now be considered as invalid.
|
|
|
|
|
|
|
|
Params: None.
|
|
|
|
|
|
|
|
Returns: Nothing.
|
|
|
|
|
|
|
|
=item C<GenerateUniqueToken($table, $column)>
|
|
|
|
|
|
|
|
Description: Generates and returns a unique token. This token is unique
|
|
|
|
in the $column of the $table. This token is NOT stored in the DB.
|
|
|
|
|
|
|
|
Params: $table (optional): The table to look at (default: tokens).
|
|
|
|
$column (optional): The column to look at for uniqueness (default: token).
|
|
|
|
|
|
|
|
Returns: A token which is unique in $column.
|
|
|
|
|
|
|
|
=item C<Cancel($token, $cancelaction, $vars)>
|
|
|
|
|
|
|
|
Description: Invalidates an existing token, generally when the token is used
|
|
|
|
for an action which is not the one expected. An email is sent
|
|
|
|
to the user who originally requested this token to inform him
|
|
|
|
that this token has been invalidated (e.g. because an hacker
|
2006-11-20 22:05:49 +03:00
|
|
|
tried to use this token for some malicious action).
|
2006-11-20 21:59:12 +03:00
|
|
|
|
|
|
|
Params: $token: The token to invalidate.
|
|
|
|
$cancelaction: The reason why this token is invalidated.
|
|
|
|
$vars: Some additional information about this action.
|
|
|
|
|
|
|
|
Returns: Nothing.
|
|
|
|
|
|
|
|
=item C<DeletePasswordTokens($user_id, $reason)>
|
|
|
|
|
|
|
|
Description: Cancels all password tokens for the given user. Emails are sent
|
2006-11-20 22:05:49 +03:00
|
|
|
to the user to inform him about this action.
|
2006-11-20 21:59:12 +03:00
|
|
|
|
|
|
|
Params: $user_id: The user ID of the user account whose password tokens
|
|
|
|
are cancelled.
|
|
|
|
$reason: The reason why these tokens are cancelled.
|
|
|
|
|
|
|
|
Returns: Nothing.
|
|
|
|
|
|
|
|
=item C<HasEmailChangeToken($user_id)>
|
|
|
|
|
|
|
|
Description: Returns any existing token currently used for an email change
|
|
|
|
for the given user.
|
|
|
|
|
|
|
|
Params: $user_id - A user ID.
|
|
|
|
|
|
|
|
Returns: A token if it exists, else undef.
|
|
|
|
|
|
|
|
=item C<GetTokenData($token)>
|
|
|
|
|
|
|
|
Description: Returns all stored data for the given token.
|
|
|
|
|
|
|
|
Params: $token - A valid token.
|
|
|
|
|
|
|
|
Returns: The user ID, the date and time when the token was created and
|
|
|
|
the (event)data stored with that token.
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
=head2 Security related routines
|
|
|
|
|
|
|
|
The following routines have been written to be used together as described below,
|
2006-11-20 22:05:49 +03:00
|
|
|
although they can be used separately.
|
2006-11-20 21:59:12 +03:00
|
|
|
|
|
|
|
=over
|
|
|
|
|
|
|
|
=item C<issue_session_token($event)>
|
|
|
|
|
|
|
|
Description: Creates and returns a token used internally.
|
|
|
|
|
|
|
|
Params: $event - The event which needs to be stored in the DB for future
|
|
|
|
reference/checks.
|
|
|
|
|
|
|
|
Returns: A unique token.
|
|
|
|
|
|
|
|
=item C<check_token_data($token, $event)>
|
|
|
|
|
|
|
|
Description: Makes sure the $token has been created by the currently logged in
|
|
|
|
user and to be used for the given $event. If this token is used for
|
|
|
|
an unexpected action (i.e. $event doesn't match the information stored
|
|
|
|
with the token), a warning is displayed asking whether the user really
|
|
|
|
wants to continue. On success, it returns 1.
|
|
|
|
This is the routine to use for security checks, combined with
|
|
|
|
issue_session_token() and delete_token() as follows:
|
|
|
|
|
|
|
|
1. First, create a token for some coming action.
|
|
|
|
my $token = issue_session_token($action);
|
|
|
|
2. Some time later, it's time to make sure that the expected action
|
|
|
|
is going to be executed, and by the expected user.
|
|
|
|
check_token_data($token, $action);
|
|
|
|
3. The check has been done and we no longer need this token.
|
|
|
|
delete_token($token);
|
|
|
|
|
|
|
|
Params: $token - The token used for security checks.
|
|
|
|
$event - The expected event to be run.
|
|
|
|
|
|
|
|
Returns: 1 on success, else a warning is thrown.
|
|
|
|
|
|
|
|
=item C<delete_token($token)>
|
|
|
|
|
|
|
|
Description: Deletes the specified token. No notification is sent.
|
|
|
|
|
|
|
|
Params: $token - The token to delete.
|
|
|
|
|
|
|
|
Returns: Nothing.
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
=cut
|