2001-07-11 05:29:21 +00: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>
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Module Initialization
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
# Make it harder for us to do dangerous things in Perl.
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
# Bundle the functions in this file together into the "Token" package.
|
|
|
|
package Token;
|
|
|
|
|
2002-08-29 09:25:54 +00:00
|
|
|
use Bugzilla::Config;
|
|
|
|
|
2002-05-03 06:37:47 +00:00
|
|
|
use Date::Format;
|
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
# This module requires that its caller have said "require CGI.pl" to import
|
|
|
|
# relevant functions from that script and its companion globals.pl.
|
|
|
|
|
2002-05-03 06:37:47 +00:00
|
|
|
################################################################################
|
|
|
|
# Constants
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
# The maximum number of days a token will remain valid.
|
|
|
|
my $maxtokenage = 3;
|
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
################################################################################
|
|
|
|
# Functions
|
|
|
|
################################################################################
|
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
sub IssueEmailChangeToken {
|
|
|
|
my ($userid, $old_email, $new_email) = @_;
|
|
|
|
|
2002-05-03 06:37:47 +00:00
|
|
|
my $token_ts = time();
|
|
|
|
my $issuedate = time2str("%Y-%m-%d %H:%M", $token_ts);
|
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
# Generate a unique token and insert it into the tokens table.
|
|
|
|
# We have to lock the tokens table before generating the token,
|
|
|
|
# since the database must be queried for token uniqueness.
|
|
|
|
&::SendSQL("LOCK TABLES tokens WRITE");
|
|
|
|
my $token = GenerateUniqueToken();
|
|
|
|
my $quotedtoken = &::SqlQuote($token);
|
|
|
|
my $quoted_emails = &::SqlQuote($old_email . ":" . $new_email);
|
|
|
|
&::SendSQL("INSERT INTO tokens ( userid , issuedate , token ,
|
|
|
|
tokentype , eventdata )
|
2002-05-03 06:37:47 +00:00
|
|
|
VALUES ( $userid , '$issuedate' , $quotedtoken ,
|
2002-04-01 22:52:40 +00:00
|
|
|
'emailold' , $quoted_emails )");
|
|
|
|
my $newtoken = GenerateUniqueToken();
|
|
|
|
$quotedtoken = &::SqlQuote($newtoken);
|
|
|
|
&::SendSQL("INSERT INTO tokens ( userid , issuedate , token ,
|
|
|
|
tokentype , eventdata )
|
2002-05-03 06:37:47 +00:00
|
|
|
VALUES ( $userid , '$issuedate' , $quotedtoken ,
|
2002-04-01 22:52:40 +00:00
|
|
|
'emailnew' , $quoted_emails )");
|
|
|
|
&::SendSQL("UNLOCK TABLES");
|
|
|
|
|
|
|
|
# Mail the user the token along with instructions for using it.
|
|
|
|
|
|
|
|
my $template = $::template;
|
|
|
|
my $vars = $::vars;
|
|
|
|
|
2002-08-29 09:25:54 +00:00
|
|
|
$vars->{'oldemailaddress'} = $old_email . Param('emailsuffix');
|
|
|
|
$vars->{'newemailaddress'} = $new_email . Param('emailsuffix');
|
2002-05-03 06:37:47 +00:00
|
|
|
|
|
|
|
$vars->{'max_token_age'} = $maxtokenage;
|
|
|
|
$vars->{'token_ts'} = $token_ts;
|
2002-04-01 22:52:40 +00:00
|
|
|
|
2002-04-24 18:27:43 +00:00
|
|
|
$vars->{'token'} = $token;
|
2002-08-29 09:25:54 +00:00
|
|
|
$vars->{'emailaddress'} = $old_email . Param('emailsuffix');
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
my $message;
|
2002-04-24 07:24:50 +00:00
|
|
|
$template->process("account/email/change-old.txt.tmpl", $vars, \$message)
|
|
|
|
|| &::ThrowTemplateError($template->error());
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
open SENDMAIL, "|/usr/lib/sendmail -t -i";
|
|
|
|
print SENDMAIL $message;
|
|
|
|
close SENDMAIL;
|
|
|
|
|
2002-04-24 18:27:43 +00:00
|
|
|
$vars->{'token'} = $newtoken;
|
2002-08-29 09:25:54 +00:00
|
|
|
$vars->{'emailaddress'} = $new_email . Param('emailsuffix');
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
$message = "";
|
2002-04-24 07:24:50 +00:00
|
|
|
$template->process("account/email/change-new.txt.tmpl", $vars, \$message)
|
|
|
|
|| &::ThrowTemplateError($template->error());
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
open SENDMAIL, "|/usr/lib/sendmail -t -i";
|
|
|
|
print SENDMAIL $message;
|
|
|
|
close SENDMAIL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
sub IssuePasswordToken {
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
my ($loginname) = @_;
|
|
|
|
|
|
|
|
# Retrieve the user's ID from the database.
|
|
|
|
my $quotedloginname = &::SqlQuote($loginname);
|
|
|
|
&::SendSQL("SELECT userid FROM profiles WHERE login_name = $quotedloginname");
|
|
|
|
my ($userid) = &::FetchSQLData();
|
|
|
|
|
2002-05-03 06:37:47 +00:00
|
|
|
my $token_ts = time();
|
|
|
|
my $issuedate = time2str("%Y-%m-%d %H:%M", $token_ts);
|
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
# Generate a unique token and insert it into the tokens table.
|
|
|
|
# We have to lock the tokens table before generating the token,
|
|
|
|
# since the database must be queried for token uniqueness.
|
2001-08-17 08:38:54 +00:00
|
|
|
&::SendSQL("LOCK TABLES tokens WRITE");
|
2001-07-11 05:29:21 +00:00
|
|
|
my $token = GenerateUniqueToken();
|
|
|
|
my $quotedtoken = &::SqlQuote($token);
|
|
|
|
my $quotedipaddr = &::SqlQuote($::ENV{'REMOTE_ADDR'});
|
|
|
|
&::SendSQL("INSERT INTO tokens ( userid , issuedate , token , tokentype , eventdata )
|
2002-05-03 06:37:47 +00:00
|
|
|
VALUES ( $userid , '$issuedate' , $quotedtoken , 'password' , $quotedipaddr )");
|
2001-07-11 05:29:21 +00:00
|
|
|
&::SendSQL("UNLOCK TABLES");
|
|
|
|
|
|
|
|
# Mail the user the token along with instructions for using it.
|
2002-04-26 06:01:30 +00:00
|
|
|
|
|
|
|
my $template = $::template;
|
|
|
|
my $vars = $::vars;
|
|
|
|
|
|
|
|
$vars->{'token'} = $token;
|
2002-08-29 09:25:54 +00:00
|
|
|
$vars->{'emailaddress'} = $loginname . Param('emailsuffix');
|
2002-04-26 06:01:30 +00:00
|
|
|
|
2002-05-03 06:37:47 +00:00
|
|
|
$vars->{'max_token_age'} = $maxtokenage;
|
|
|
|
$vars->{'token_ts'} = $token_ts;
|
|
|
|
|
2002-04-26 06:01:30 +00:00
|
|
|
my $message = "";
|
2002-04-26 06:46:16 +00:00
|
|
|
$template->process("account/password/forgotten-password.txt.tmpl",
|
|
|
|
$vars, \$message)
|
2002-04-26 06:01:30 +00:00
|
|
|
|| &::ThrowTemplateError($template->error());
|
|
|
|
|
|
|
|
open SENDMAIL, "|/usr/lib/sendmail -t -i";
|
|
|
|
print SENDMAIL $message;
|
|
|
|
close SENDMAIL;
|
2001-07-11 05:29:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
sub CleanTokenTable {
|
|
|
|
&::SendSQL("LOCK TABLES tokens WRITE");
|
|
|
|
&::SendSQL("DELETE FROM tokens
|
2002-05-03 06:37:47 +00:00
|
|
|
WHERE TO_DAYS(NOW()) - TO_DAYS(issuedate) >= " . $maxtokenage);
|
2002-04-01 22:52:40 +00:00
|
|
|
&::SendSQL("UNLOCK TABLES");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
sub GenerateUniqueToken {
|
|
|
|
# Generates a unique random token. Uses &GenerateRandomPassword
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
my $token;
|
|
|
|
my $duplicate = 1;
|
|
|
|
my $tries = 0;
|
|
|
|
while ($duplicate) {
|
|
|
|
|
|
|
|
++$tries;
|
|
|
|
if ($tries > 100) {
|
2002-09-30 07:22:44 +00:00
|
|
|
&::ThrowCodeError("token_generation_error");
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$token = &::GenerateRandomPassword();
|
|
|
|
&::SendSQL("SELECT userid FROM tokens WHERE token = " . &::SqlQuote($token));
|
|
|
|
$duplicate = &::FetchSQLData();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) = @_;
|
|
|
|
|
|
|
|
# Quote the token for inclusion in SQL statements.
|
|
|
|
my $quotedtoken = &::SqlQuote($token);
|
|
|
|
|
|
|
|
# Get information about the token being cancelled.
|
|
|
|
&::SendSQL("SELECT issuedate , tokentype , eventdata , login_name , realname
|
|
|
|
FROM tokens, profiles
|
|
|
|
WHERE tokens.userid = profiles.userid
|
|
|
|
AND token = $quotedtoken");
|
|
|
|
my ($issuedate, $tokentype, $eventdata, $loginname, $realname) = &::FetchSQLData();
|
|
|
|
|
|
|
|
# Get the email address of the Bugzilla maintainer.
|
2002-08-29 09:25:54 +00:00
|
|
|
my $maintainer = Param('maintainer');
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
my $template = $::template;
|
|
|
|
my $vars = $::vars;
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2003-03-14 05:43:38 +00:00
|
|
|
$vars->{'emailaddress'} = $loginname . Param('emailsuffix');
|
2002-04-01 22:52:40 +00:00
|
|
|
$vars->{'maintainer'} = $maintainer;
|
|
|
|
$vars->{'remoteaddress'} = $::ENV{'REMOTE_ADDR'};
|
2002-04-24 18:27:43 +00:00
|
|
|
$vars->{'token'} = $token;
|
2002-04-01 22:52:40 +00:00
|
|
|
$vars->{'tokentype'} = $tokentype;
|
|
|
|
$vars->{'issuedate'} = $issuedate;
|
|
|
|
$vars->{'eventdata'} = $eventdata;
|
|
|
|
$vars->{'cancelaction'} = $cancelaction;
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
# Notify the user via email about the cancellation.
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
my $message;
|
2002-04-24 07:24:50 +00:00
|
|
|
$template->process("account/cancel-token.txt.tmpl", $vars, \$message)
|
|
|
|
|| &::ThrowTemplateError($template->error());
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
open SENDMAIL, "|/usr/lib/sendmail -t -i";
|
|
|
|
print SENDMAIL $message;
|
2001-07-11 05:29:21 +00:00
|
|
|
close SENDMAIL;
|
|
|
|
|
|
|
|
# Delete the token from the database.
|
2001-08-17 08:38:54 +00:00
|
|
|
&::SendSQL("LOCK TABLES tokens WRITE");
|
2001-07-11 05:29:21 +00:00
|
|
|
&::SendSQL("DELETE FROM tokens WHERE token = $quotedtoken");
|
|
|
|
&::SendSQL("UNLOCK TABLES");
|
|
|
|
}
|
|
|
|
|
2003-03-22 04:47:35 +00:00
|
|
|
sub DeletePasswordTokens {
|
|
|
|
my ($userid, $reason) = @_;
|
|
|
|
|
|
|
|
my $dbh = Bugzilla->dbh;
|
|
|
|
my $sth = $dbh->prepare("SELECT token " .
|
|
|
|
"FROM tokens " .
|
|
|
|
"WHERE userid=? AND tokentype='password'");
|
|
|
|
$sth->execute($userid);
|
|
|
|
while (my $token = $sth->fetchrow_array) {
|
|
|
|
Token::Cancel($token, "user_logged_in");
|
|
|
|
}
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
sub HasEmailChangeToken {
|
|
|
|
# Returns an email change token if the user has one.
|
|
|
|
|
|
|
|
my ($userid) = @_;
|
|
|
|
|
2002-07-09 02:16:56 +00:00
|
|
|
&::SendSQL("SELECT token FROM tokens WHERE userid = $userid " .
|
|
|
|
"AND (tokentype = 'emailnew' OR tokentype = 'emailold') " .
|
|
|
|
"LIMIT 1");
|
2002-04-01 22:52:40 +00:00
|
|
|
my ($token) = &::FetchSQLData();
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
1;
|