token.cgi 10.8 KB
Newer Older
1
#!/usr/bin/perl -T
2 3 4
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
#
6 7
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
8

9
use 5.10.1;
10
use strict;
11 12
use warnings;

13
use lib qw(. lib);
14

15
use Bugzilla;
16
use Bugzilla::Constants;
17
use Bugzilla::Util;
18 19 20
use Bugzilla::Error;
use Bugzilla::Token;
use Bugzilla::User;
21

22
use Date::Format;
23 24
use Date::Parse;

25 26 27
local our $cgi = Bugzilla->cgi;
local our $template = Bugzilla->template;
local our $vars = {};
28

29 30 31
my $action = $cgi->param('a');
my $token = $cgi->param('t');

32
Bugzilla->login(LOGIN_OPTIONAL);
33 34 35

# Throw an error if the form does not contain an "action" field specifying
# what the user wants to do.
36
$action || ThrowUserError('unknown_action');
37

38 39
Bugzilla::Token::CleanTokenTable();
my ($user_id, $date, $data, $tokentype) = Bugzilla::Token::GetTokenData($token);
40

41 42 43 44
# Requesting a new password is the single action which doesn't require a token.
# XXX Ideally, these checks should be done inside the subroutines themselves.
unless ($action eq 'reqpw') {
    $tokentype || ThrowUserError("token_does_not_exist");
45

46
    # Make sure the token is the correct type for the action being taken.
47 48
    # The { user_error => 'wrong_token_for_*' } trick is to make 012throwables.t happy.
    my $error = {};
49
    if (grep($action eq $_ , qw(cfmpw cxlpw chgpw)) && $tokentype ne 'password') {
50
        $error = { user_error => 'wrong_token_for_changing_passwd' };
51
    }
52 53 54
    elsif ($action eq 'cxlem'
           && ($tokentype ne 'emailold' && $tokentype ne 'emailnew'))
    {
55
        $error = { user_error => 'wrong_token_for_cancelling_email_change' };
56 57
    }
    elsif (grep($action eq $_ , qw(cfmem chgem)) && $tokentype ne 'emailnew') {
58
        $error = { user_error => 'wrong_token_for_confirming_email_change' };
59 60 61 62
    }
    elsif ($action =~ /^(request|confirm|cancel)_new_account$/
           && $tokentype ne 'account')
    {
63
        $error = { user_error => 'wrong_token_for_creating_account' };
64
    }
65

66 67 68
    if (my $user_error = $error->{user_error}) {
        Bugzilla::Token::Cancel($token, $user_error);
        ThrowUserError($user_error);
69
    }
70 71
}

72
if ($action eq 'reqpw') {
73 74 75
    requestChangePassword();
}
elsif ($action eq 'cfmpw') {
76
    confirmChangePassword($token);
77 78
}
elsif ($action eq 'cxlpw') {
79
    cancelChangePassword($token);
80 81 82 83 84
}
elsif ($action eq 'chgpw') {
    changePassword($user_id, $token);
}
elsif ($action eq 'cfmem') {
85
    confirmChangeEmail($token);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}
elsif ($action eq 'cxlem') {
    cancelChangeEmail($user_id, $data, $tokentype, $token);
}
elsif ($action eq 'chgem') {
    changeEmail($user_id, $data, $token);
}
elsif ($action eq 'request_new_account') {
    request_create_account($date, $data, $token);
}
elsif ($action eq 'confirm_new_account') {
    confirm_create_account($data, $token);
}
elsif ($action eq 'cancel_new_account') {
    cancel_create_account($data, $token);
}
else {
103
    ThrowUserError('unknown_action', {action => $action});
104 105 106 107 108 109 110 111
}

exit;

################################################################################
# Functions
################################################################################

112 113 114
# If the user is requesting a password change, make sure they submitted
# their login name and it exists in the database, and that the DB module is in
# the list of allowed verification methods.
115
sub requestChangePassword {
116 117 118 119
    # check verification methods
    Bugzilla->user->authorizer->can_change_password
      || ThrowUserError("password_change_requests_not_allowed");

120 121 122 123 124
    # Check the hash token to make sure this user actually submitted
    # the forgotten password form.
    my $token = $cgi->param('token');
    check_hash_token($token, ['reqpw']);

125 126 127
    my $login_name = $cgi->param('loginname')
      or ThrowUserError("login_needed_for_password_change");

128
    check_email_syntax($login_name);
129
    my $user = new Bugzilla::User({ name => $login_name });
130 131

    # Make sure the user account is active.
132
    if ($user && !$user->is_enabled) {
133 134 135 136
        ThrowUserError('account_disabled',
                       {disabled_reason => get_text('account_disabled', {account => $login_name})});
    }

137
    Bugzilla::Token::IssuePasswordToken($user) if $user;
138

139
    $vars->{'message'} = "password_change_request";
140
    $vars->{'login_name'} = $login_name;
141

142
    print $cgi->header();
143
    $template->process("global/message.html.tmpl", $vars)
144
      || ThrowTemplateError($template->error());
145 146 147
}

sub confirmChangePassword {
148 149 150
    my $token = shift;
    $vars->{'token'} = $token;

151
    print $cgi->header();
152 153
    $template->process("account/password/set-forgotten-password.html.tmpl", $vars)
      || ThrowTemplateError($template->error());
154 155
}

156 157
sub cancelChangePassword {
    my $token = shift;
158
    $vars->{'message'} = "password_change_canceled";
159
    Bugzilla::Token::Cancel($token, $vars->{'message'});
160

161
    print $cgi->header();
162
    $template->process("global/message.html.tmpl", $vars)
163
      || ThrowTemplateError($template->error());
164 165
}

166 167
# If the user is changing their password, make sure they submitted a new
# password and that the new password is valid.
168
sub changePassword {
169
    my ($user_id, $token) = @_;
170
    my $dbh = Bugzilla->dbh;
171 172 173 174

    my $password = $cgi->param('password');
    (defined $password && defined $cgi->param('matchpassword'))
      || ThrowUserError("require_new_password");
175

176 177 178
    validate_password($password, $cgi->param('matchpassword'));
    # Make sure that these never show up in the UI under any circumstances.
    $cgi->delete('password', 'matchpassword');
179

180 181 182 183
    my $user = Bugzilla::User->check({ id => $user_id });
    $user->set_password($password);
    $user->update();
    delete_token($token);
184 185
    $dbh->do(q{DELETE FROM tokens WHERE userid = ?
               AND tokentype = 'password'}, undef, $user_id);
186

187
    Bugzilla->logout_user_by_id($user_id);
188

189
    $vars->{'message'} = "password_changed";
190

191
    print $cgi->header();
192
    $template->process("global/message.html.tmpl", $vars)
193
      || ThrowTemplateError($template->error());
194 195
}

196
sub confirmChangeEmail {
197 198
    my $token = shift;
    $vars->{'token'} = $token;
199

200
    print $cgi->header();
201 202
    $template->process("account/email/confirm.html.tmpl", $vars)
      || ThrowTemplateError($template->error());
203 204 205
}

sub changeEmail {
206
    my ($userid, $eventdata, $token) = @_;
207
    my $dbh = Bugzilla->dbh;
208 209
    my ($old_email, $new_email) = split(/:/,$eventdata);

210 211 212 213 214 215 216 217 218 219
    $dbh->bz_start_transaction();
    
    my $user = Bugzilla::User->check({ id => $userid });
    my $realpassword = $user->cryptpassword;
    my $cgipassword  = $cgi->param('password');

    # Make sure the user who wants to change the email address
    # is the real account owner.
    if (bz_crypt($cgipassword, $realpassword) ne $realpassword) {
        ThrowUserError("old_password_incorrect");
220
    }
221

222 223
    # The new email address should be available as this was 
    # confirmed initially so cancel token if it is not still available
224
    if (! is_available_username($new_email,$old_email)) {
225
        $vars->{'email'} = $new_email; # Needed for Bugzilla::Token::Cancel's mail
226
        Bugzilla::Token::Cancel($token, "account_exists", $vars);
227
        ThrowUserError("account_exists", { email => $new_email } );
228 229
    } 

230 231 232
    # Update the user's login name in the profiles table.
    $user->set_login($new_email);
    $user->update({ keep_session => 1, keep_tokens => 1 });
233
    delete_token($token);
234 235
    $dbh->do(q{DELETE FROM tokens WHERE userid = ?
               AND tokentype = 'emailnew'}, undef, $userid);
236

237 238
    $dbh->bz_commit_transaction();

239
    # Return HTTP response headers.
240
    print $cgi->header();
241 242

    # Let the user know their email address has been changed.
243
    $vars->{'message'} = "login_changed";
244 245

    $template->process("global/message.html.tmpl", $vars)
246
      || ThrowTemplateError($template->error());
247 248 249
}

sub cancelChangeEmail {
250
    my ($userid, $eventdata, $tokentype, $token) = @_;
251 252
    my $dbh = Bugzilla->dbh;

bbaetz%acm.org's avatar
bbaetz%acm.org committed
253
    $dbh->bz_start_transaction();
254

255 256
    my ($old_email, $new_email) = split(/:/,$eventdata);

257
    if ($tokentype eq "emailold") {
258
        $vars->{'message'} = "emailold_change_canceled";
259
        my $user = Bugzilla::User->check({ id => $userid });
260 261

        # check to see if it has been altered
262 263
        if ($user->login ne $old_email) {
            $user->set_login($old_email);
264
            $user->update({ keep_tokens => 1 });
265

266
            $vars->{'message'} = "email_change_canceled_reinstated";
267 268 269
        } 
    } 
    else {
270
        $vars->{'message'} = 'email_change_canceled'
271 272 273 274
     }

    $vars->{'old_email'} = $old_email;
    $vars->{'new_email'} = $new_email;
275
    Bugzilla::Token::Cancel($token, $vars->{'message'}, $vars);
276

277 278 279
    $dbh->do(q{DELETE FROM tokens WHERE userid = ?
               AND tokentype = 'emailold' OR tokentype = 'emailnew'},
             undef, $userid);
280

281 282
    $dbh->bz_commit_transaction();

283
    # Return HTTP response headers.
284
    print $cgi->header();
285

286
    $template->process("global/message.html.tmpl", $vars)
287
      || ThrowTemplateError($template->error());
288
}
289

290
sub request_create_account {
291
    my ($date, $login_name, $token) = @_;
292

293 294
    Bugzilla->user->check_account_creation_enabled;

295
    $vars->{'token'} = $token;
296
    $vars->{'email'} = $login_name . Bugzilla->params->{'emailsuffix'};
297
    $vars->{'expiration_ts'} = ctime(str2time($date) + MAX_TOKEN_AGE * 86400);
298 299 300 301 302 303 304

    print $cgi->header();
    $template->process('account/email/confirm-new.html.tmpl', $vars)
      || ThrowTemplateError($template->error());
}

sub confirm_create_account {
305
    my ($login_name, $token) = @_;
306

307 308
    Bugzilla->user->check_account_creation_enabled;

309 310
    my $password = $cgi->param('passwd1') || '';
    validate_password($password, $cgi->param('passwd2') || '');
311 312
    # Make sure that these never show up anywhere in the UI.
    $cgi->delete('passwd1', 'passwd2');
313 314 315

    my $otheruser = Bugzilla::User->create({
        login_name => $login_name, 
316
        realname   => scalar $cgi->param('realname'),
317
        cryptpassword => $password});
318 319

    # Now delete this token.
320
    delete_token($token);
321

322
    # Let the user know that their user account has been successfully created.
323 324
    $vars->{'message'} = 'account_created';
    $vars->{'otheruser'} = $otheruser;
325

326
    # Log in the new user using credentials they just gave.
327 328 329
    $cgi->param('Bugzilla_login', $otheruser->login);
    $cgi->param('Bugzilla_password', $password);
    Bugzilla->login(LOGIN_OPTIONAL);
330 331 332

    print $cgi->header();

333
    $template->process('index.html.tmpl', $vars)
334 335 336 337
      || ThrowTemplateError($template->error());
}

sub cancel_create_account {
338
    my ($login_name, $token) = @_;
339

340
    $vars->{'message'} = 'account_creation_canceled';
341
    $vars->{'account'} = $login_name;
342
    Bugzilla::Token::Cancel($token, $vars->{'message'});
343 344 345 346 347

    print $cgi->header();
    $template->process('global/message.html.tmpl', $vars)
      || ThrowTemplateError($template->error());
}