2003-03-27 00:07:02 +00:00
|
|
|
#!/usr/bin/perl -wT
|
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>
|
|
|
|
|
|
|
|
############################################################################
|
|
|
|
# Script Initialization
|
|
|
|
############################################################################
|
|
|
|
|
|
|
|
# Make it harder for us to do dangerous things in Perl.
|
|
|
|
use strict;
|
|
|
|
|
2002-01-20 01:44:52 +00:00
|
|
|
use lib qw(.);
|
|
|
|
|
2002-03-15 23:23:12 +00:00
|
|
|
use vars qw($template $vars);
|
|
|
|
|
2003-05-05 01:15:38 +00:00
|
|
|
use Bugzilla;
|
2004-03-27 03:51:44 +00:00
|
|
|
use Bugzilla::Constants;
|
2005-01-31 19:26:01 +00:00
|
|
|
use Bugzilla::Auth;
|
2004-03-27 03:51:44 +00:00
|
|
|
|
2004-02-29 14:19:27 +00:00
|
|
|
my $cgi = Bugzilla->cgi;
|
2003-05-05 01:15:38 +00:00
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
# Include the Bugzilla CGI and general utility library.
|
|
|
|
require "CGI.pl";
|
|
|
|
|
2004-03-27 03:51:44 +00:00
|
|
|
Bugzilla->login(LOGIN_OPTIONAL);
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2004-03-18 03:57:05 +00:00
|
|
|
# Use the "Bugzilla::Token" module that contains functions for doing various
|
2001-07-11 05:29:21 +00:00
|
|
|
# token-related tasks.
|
2004-03-18 03:57:05 +00:00
|
|
|
use Bugzilla::Token;
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2003-06-03 09:48:15 +00:00
|
|
|
use Bugzilla::User;
|
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
################################################################################
|
|
|
|
# Data Validation / Security Authorization
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
# Throw an error if the form does not contain an "action" field specifying
|
|
|
|
# what the user wants to do.
|
2004-02-29 14:19:27 +00:00
|
|
|
$cgi->param('a') || ThrowCodeError("unknown_action");
|
2001-07-11 05:29:21 +00:00
|
|
|
|
|
|
|
# Assign the action to a global variable.
|
2004-02-29 14:19:27 +00:00
|
|
|
$::action = $cgi->param('a');
|
2001-07-11 05:29:21 +00:00
|
|
|
|
|
|
|
# If a token was submitted, make sure it is a valid token that exists in the
|
|
|
|
# database and is the correct type for the action being taken.
|
2004-02-29 14:19:27 +00:00
|
|
|
if ($cgi->param('t')) {
|
2001-07-11 05:29:21 +00:00
|
|
|
# Assign the token and its SQL quoted equivalent to global variables.
|
2004-02-29 14:19:27 +00:00
|
|
|
$::token = $cgi->param('t');
|
2001-07-11 05:29:21 +00:00
|
|
|
$::quotedtoken = SqlQuote($::token);
|
|
|
|
|
|
|
|
# Make sure the token contains only valid characters in the right amount.
|
|
|
|
my $validationerror = ValidatePassword($::token);
|
|
|
|
if ($validationerror) {
|
2002-09-30 07:22:44 +00:00
|
|
|
ThrowUserError("token_invalid");
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
|
2004-03-18 03:57:05 +00:00
|
|
|
Bugzilla::Token::CleanTokenTable();
|
2002-04-01 22:52:40 +00:00
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
# Make sure the token exists in the database.
|
|
|
|
SendSQL( "SELECT tokentype FROM tokens WHERE token = $::quotedtoken" );
|
2002-09-30 07:22:44 +00:00
|
|
|
(my $tokentype = FetchSQLData()) || ThrowUserError("token_inexistent");
|
2001-07-11 05:29:21 +00:00
|
|
|
|
|
|
|
# Make sure the token is the correct type for the action being taken.
|
|
|
|
if ( grep($::action eq $_ , qw(cfmpw cxlpw chgpw)) && $tokentype ne 'password' ) {
|
2004-03-18 03:57:05 +00:00
|
|
|
Bugzilla::Token::Cancel($::token, "wrong_token_for_changing_passwd");
|
2002-09-30 07:22:44 +00:00
|
|
|
ThrowUserError("wrong_token_for_changing_passwd");
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
2002-04-01 22:52:40 +00:00
|
|
|
if ( ($::action eq 'cxlem')
|
|
|
|
&& (($tokentype ne 'emailold') && ($tokentype ne 'emailnew')) ) {
|
2004-03-18 03:57:05 +00:00
|
|
|
Bugzilla::Token::Cancel($::token, "wrong_token_for_cancelling_email_change");
|
2002-09-30 07:22:44 +00:00
|
|
|
ThrowUserError("wrong_token_for_cancelling_email_change");
|
2002-04-01 22:52:40 +00:00
|
|
|
}
|
|
|
|
if ( grep($::action eq $_ , qw(cfmem chgem))
|
|
|
|
&& ($tokentype ne 'emailnew') ) {
|
2004-03-18 03:57:05 +00:00
|
|
|
Bugzilla::Token::Cancel($::token, "wrong_token_for_confirming_email_change");
|
2002-09-30 07:22:44 +00:00
|
|
|
ThrowUserError("wrong_token_for_confirming_email_change");
|
2002-04-01 22:52:40 +00:00
|
|
|
}
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
2004-07-20 22:41:22 +00:00
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
# If the user is requesting a password change, make sure they submitted
|
2004-07-20 22:41:22 +00:00
|
|
|
# their login name and it exists in the database, and that the DB module is in
|
|
|
|
# the list of allowed verification methids.
|
2001-07-11 05:29:21 +00:00
|
|
|
if ( $::action eq 'reqpw' ) {
|
2004-02-29 14:19:27 +00:00
|
|
|
defined $cgi->param('loginname')
|
2002-09-30 07:22:44 +00:00
|
|
|
|| ThrowUserError("login_needed_for_password_change");
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2004-07-20 22:41:22 +00:00
|
|
|
# check verification methods
|
|
|
|
unless (Bugzilla::Auth->has_db) {
|
|
|
|
ThrowUserError("password_change_requests_not_allowed");
|
|
|
|
}
|
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
# Make sure the login name looks like an email address. This function
|
|
|
|
# displays its own error and stops execution if the login name looks wrong.
|
2004-02-29 14:19:27 +00:00
|
|
|
CheckEmailSyntax($cgi->param('loginname'));
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2004-02-29 14:19:27 +00:00
|
|
|
my $quotedloginname = SqlQuote($cgi->param('loginname'));
|
2001-07-11 05:29:21 +00:00
|
|
|
SendSQL("SELECT userid FROM profiles WHERE login_name = $quotedloginname");
|
|
|
|
FetchSQLData()
|
2002-09-30 07:22:44 +00:00
|
|
|
|| ThrowUserError("account_inexistent");
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# If the user is changing their password, make sure they submitted a new
|
|
|
|
# password and that the new password is valid.
|
|
|
|
if ( $::action eq 'chgpw' ) {
|
2004-02-29 14:19:27 +00:00
|
|
|
defined $cgi->param('password')
|
|
|
|
&& defined $cgi->param('matchpassword')
|
2002-09-30 07:22:44 +00:00
|
|
|
|| ThrowUserError("require_new_password");
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2004-02-29 14:19:27 +00:00
|
|
|
ValidatePassword($cgi->param('password'), $cgi->param('matchpassword'));
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Main Body Execution
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
# All calls to this script should contain an "action" variable whose value
|
|
|
|
# determines what the user wants to do. The code below checks the value of
|
|
|
|
# that variable and runs the appropriate code.
|
|
|
|
|
|
|
|
if ($::action eq 'reqpw') {
|
|
|
|
requestChangePassword();
|
|
|
|
} elsif ($::action eq 'cfmpw') {
|
|
|
|
confirmChangePassword();
|
|
|
|
} elsif ($::action eq 'cxlpw') {
|
|
|
|
cancelChangePassword();
|
|
|
|
} elsif ($::action eq 'chgpw') {
|
|
|
|
changePassword();
|
2002-04-01 22:52:40 +00:00
|
|
|
} elsif ($::action eq 'cfmem') {
|
|
|
|
confirmChangeEmail();
|
|
|
|
} elsif ($::action eq 'cxlem') {
|
|
|
|
cancelChangeEmail();
|
|
|
|
} elsif ($::action eq 'chgem') {
|
|
|
|
changeEmail();
|
2001-07-11 05:29:21 +00:00
|
|
|
} else {
|
|
|
|
# If the action that the user wants to take (specified in the "a" form field)
|
|
|
|
# is none of the above listed actions, display an error telling the user
|
|
|
|
# that we do not understand what they would like to do.
|
2003-09-14 06:05:23 +00:00
|
|
|
ThrowCodeError("unknown_action", { action => $::action });
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Functions
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
sub requestChangePassword {
|
2004-03-18 09:01:35 +00:00
|
|
|
Bugzilla::Token::IssuePasswordToken($cgi->param('loginname'));
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2002-08-10 08:06:58 +00:00
|
|
|
$vars->{'message'} = "password_change_request";
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2004-02-29 14:19:27 +00:00
|
|
|
print $cgi->header();
|
2002-03-15 23:23:12 +00:00
|
|
|
$template->process("global/message.html.tmpl", $vars)
|
2002-04-24 07:24:50 +00:00
|
|
|
|| ThrowTemplateError($template->error());
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub confirmChangePassword {
|
2002-03-15 23:23:12 +00:00
|
|
|
$vars->{'token'} = $::token;
|
|
|
|
|
2004-02-29 14:19:27 +00:00
|
|
|
print $cgi->header();
|
2002-04-24 07:24:50 +00:00
|
|
|
$template->process("account/password/set-forgotten-password.html.tmpl", $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
2002-03-15 23:23:12 +00:00
|
|
|
sub cancelChangePassword {
|
2004-03-18 16:14:55 +00:00
|
|
|
$vars->{'message'} = "password_change_cancelled";
|
2004-03-18 03:57:05 +00:00
|
|
|
Bugzilla::Token::Cancel($::token, $vars->{'message'});
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2004-02-29 14:19:27 +00:00
|
|
|
print $cgi->header();
|
2002-03-15 23:23:12 +00:00
|
|
|
$template->process("global/message.html.tmpl", $vars)
|
2002-04-24 07:24:50 +00:00
|
|
|
|| ThrowTemplateError($template->error());
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub changePassword {
|
2005-02-17 21:57:27 +00:00
|
|
|
my $dbh = Bugzilla->dbh;
|
|
|
|
|
2001-07-11 05:29:21 +00:00
|
|
|
# Quote the password and token for inclusion into SQL statements.
|
2005-01-31 19:26:01 +00:00
|
|
|
my $cryptedpassword = bz_crypt($cgi->param('password'));
|
2001-07-11 05:29:21 +00:00
|
|
|
my $quotedpassword = SqlQuote($cryptedpassword);
|
|
|
|
|
|
|
|
# Get the user's ID from the tokens table.
|
|
|
|
SendSQL("SELECT userid FROM tokens WHERE token = $::quotedtoken");
|
|
|
|
my $userid = FetchSQLData();
|
|
|
|
|
|
|
|
# Update the user's password in the profiles table and delete the token
|
|
|
|
# from the tokens table.
|
2005-02-17 21:57:27 +00:00
|
|
|
$dbh->bz_lock_tables('profiles WRITE', 'tokens WRITE');
|
2001-07-11 05:29:21 +00:00
|
|
|
SendSQL("UPDATE profiles
|
|
|
|
SET cryptpassword = $quotedpassword
|
|
|
|
WHERE userid = $userid");
|
|
|
|
SendSQL("DELETE FROM tokens WHERE token = $::quotedtoken");
|
2005-02-17 21:57:27 +00:00
|
|
|
$dbh->bz_unlock_tables();
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2004-03-27 01:28:29 +00:00
|
|
|
Bugzilla->logout_user_by_id($userid);
|
2002-02-04 12:23:05 +00:00
|
|
|
|
2002-08-10 08:06:58 +00:00
|
|
|
$vars->{'message'} = "password_changed";
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2004-02-29 14:19:27 +00:00
|
|
|
print $cgi->header();
|
2002-03-15 23:23:12 +00:00
|
|
|
$template->process("global/message.html.tmpl", $vars)
|
2002-04-24 07:24:50 +00:00
|
|
|
|| ThrowTemplateError($template->error());
|
2001-07-11 05:29:21 +00:00
|
|
|
}
|
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
sub confirmChangeEmail {
|
|
|
|
# Return HTTP response headers.
|
2004-02-29 14:19:27 +00:00
|
|
|
print $cgi->header();
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
$vars->{'token'} = $::token;
|
|
|
|
|
2002-04-24 07:24:50 +00:00
|
|
|
$template->process("account/email/confirm.html.tmpl", $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
2002-04-01 22:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub changeEmail {
|
2005-02-17 21:57:27 +00:00
|
|
|
my $dbh = Bugzilla->dbh;
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
# Get the user's ID from the tokens table.
|
|
|
|
SendSQL("SELECT userid, eventdata FROM tokens
|
|
|
|
WHERE token = $::quotedtoken");
|
|
|
|
my ($userid, $eventdata) = FetchSQLData();
|
|
|
|
my ($old_email, $new_email) = split(/:/,$eventdata);
|
|
|
|
my $quotednewemail = SqlQuote($new_email);
|
|
|
|
|
|
|
|
# Check the user entered the correct old email address
|
2004-02-29 14:19:27 +00:00
|
|
|
if(lc($cgi->param('email')) ne lc($old_email)) {
|
2002-09-30 07:22:44 +00:00
|
|
|
ThrowUserError("email_confirmation_failed");
|
2002-04-01 22:52:40 +00:00
|
|
|
}
|
|
|
|
# The new email address should be available as this was
|
|
|
|
# confirmed initially so cancel token if it is not still available
|
2005-02-09 06:42:43 +00:00
|
|
|
if (! is_available_username($new_email,$old_email)) {
|
2004-03-18 09:01:35 +00:00
|
|
|
$vars->{'email'} = $new_email; # Needed for Bugzilla::Token::Cancel's mail
|
|
|
|
Bugzilla::Token::Cancel($::token,"account_exists");
|
2003-04-02 12:35:07 +00:00
|
|
|
ThrowUserError("account_exists", { email => $new_email } );
|
2002-04-01 22:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Update the user's login name in the profiles table and delete the token
|
|
|
|
# from the tokens table.
|
2005-02-17 21:57:27 +00:00
|
|
|
$dbh->bz_lock_tables('profiles WRITE', 'tokens WRITE');
|
2002-04-01 22:52:40 +00:00
|
|
|
SendSQL("UPDATE profiles
|
|
|
|
SET login_name = $quotednewemail
|
|
|
|
WHERE userid = $userid");
|
|
|
|
SendSQL("DELETE FROM tokens WHERE token = $::quotedtoken");
|
|
|
|
SendSQL("DELETE FROM tokens WHERE userid = $userid
|
|
|
|
AND tokentype = 'emailnew'");
|
2005-02-17 21:57:27 +00:00
|
|
|
$dbh->bz_unlock_tables();
|
2003-06-03 09:48:15 +00:00
|
|
|
|
|
|
|
# The email address has been changed, so we need to rederive the groups
|
|
|
|
my $user = new Bugzilla::User($userid);
|
|
|
|
$user->derive_groups;
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
# Return HTTP response headers.
|
2004-02-29 14:19:27 +00:00
|
|
|
print $cgi->header();
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
# Let the user know their email address has been changed.
|
|
|
|
|
2002-08-10 08:06:58 +00:00
|
|
|
$vars->{'message'} = "login_changed";
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
$template->process("global/message.html.tmpl", $vars)
|
2002-04-24 07:24:50 +00:00
|
|
|
|| ThrowTemplateError($template->error());
|
2002-04-01 22:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub cancelChangeEmail {
|
2005-02-17 21:57:27 +00:00
|
|
|
my $dbh = Bugzilla->dbh;
|
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
# Get the user's ID from the tokens table.
|
|
|
|
SendSQL("SELECT userid, tokentype, eventdata FROM tokens
|
|
|
|
WHERE token = $::quotedtoken");
|
|
|
|
my ($userid, $tokentype, $eventdata) = FetchSQLData();
|
|
|
|
my ($old_email, $new_email) = split(/:/,$eventdata);
|
|
|
|
|
|
|
|
if($tokentype eq "emailold") {
|
2002-09-30 07:22:44 +00:00
|
|
|
$vars->{'message'} = "emailold_change_cancelled";
|
2002-04-01 22:52:40 +00:00
|
|
|
|
|
|
|
SendSQL("SELECT login_name FROM profiles WHERE userid = $userid");
|
|
|
|
my $actualemail = FetchSQLData();
|
|
|
|
|
|
|
|
# check to see if it has been altered
|
|
|
|
if($actualemail ne $old_email) {
|
|
|
|
my $quotedoldemail = SqlQuote($old_email);
|
|
|
|
|
2005-02-17 21:57:27 +00:00
|
|
|
$dbh->bz_lock_tables('profiles WRITE');
|
2002-04-01 22:52:40 +00:00
|
|
|
SendSQL("UPDATE profiles
|
|
|
|
SET login_name = $quotedoldemail
|
|
|
|
WHERE userid = $userid");
|
2005-02-17 21:57:27 +00:00
|
|
|
$dbh->bz_unlock_tables();
|
2003-06-03 09:48:15 +00:00
|
|
|
|
|
|
|
# email has changed, so rederive groups
|
|
|
|
# Note that this is done _after_ the tables are unlocked
|
|
|
|
# This is sort of a race condition (given the lack of transactions)
|
|
|
|
# but the user had access to it just now, so it's not a security
|
|
|
|
# issue
|
|
|
|
|
|
|
|
my $user = new Bugzilla::User($userid);
|
|
|
|
$user->derive_groups;
|
|
|
|
|
2002-09-30 07:22:44 +00:00
|
|
|
$vars->{'message'} = "email_change_cancelled_reinstated";
|
2002-04-01 22:52:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2002-09-30 07:22:44 +00:00
|
|
|
$vars->{'message'} = 'email_change_cancelled'
|
|
|
|
}
|
|
|
|
|
|
|
|
$vars->{'old_email'} = $old_email;
|
|
|
|
$vars->{'new_email'} = $new_email;
|
2004-03-18 03:57:05 +00:00
|
|
|
Bugzilla::Token::Cancel($::token, $vars->{'message'});
|
2002-04-01 22:52:40 +00:00
|
|
|
|
2005-02-17 21:57:27 +00:00
|
|
|
$dbh->bz_lock_tables('tokens WRITE');
|
2002-04-01 22:52:40 +00:00
|
|
|
SendSQL("DELETE FROM tokens
|
|
|
|
WHERE userid = $userid
|
|
|
|
AND tokentype = 'emailold' OR tokentype = 'emailnew'");
|
2005-02-17 21:57:27 +00:00
|
|
|
$dbh->bz_unlock_tables();
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
# Return HTTP response headers.
|
2004-02-29 14:19:27 +00:00
|
|
|
print $cgi->header();
|
2001-07-11 05:29:21 +00:00
|
|
|
|
2002-04-01 22:52:40 +00:00
|
|
|
$template->process("global/message.html.tmpl", $vars)
|
2002-04-24 07:24:50 +00:00
|
|
|
|| ThrowTemplateError($template->error());
|
2002-04-01 22:52:40 +00:00
|
|
|
}
|
2001-07-11 05:29:21 +00:00
|
|
|
|