Annotation of loncom/interface/resetpw.pm, revision 1.22.2.3
1.1 raeburn 1: # The LearningOnline Network
2: # Allow access to password changing via a token sent to user's e-mail.
3: #
1.22.2.3! raeburn 4: # $Id: resetpw.pm,v 1.22.2.2 2010/09/19 15:04:40 raeburn Exp $
1.14 bisitz 5: #
1.1 raeburn 6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: #
1.21 raeburn 29:
30: =pod
31:
32: =head1 NAME
33:
1.22 raeburn 34: Apache::resetpw: reset user password.
1.21 raeburn 35:
36: =head1 SYNOPSIS
37:
38: Handles resetting of forgotten passwords.
39:
40: This is part of the LearningOnline Network with CAPA project
41: described at http://www.lon-capa.org.
42:
43: =head1 OVERVIEW
44:
45: A user with an e-mail address associated with his/her LON-CAPA username
1.22 raeburn 46: can reset a forgotten password, using a link sent to the e-mail address
1.21 raeburn 47: if the authentication type for the account is "internal".
48:
49: =cut
50:
1.1 raeburn 51: package Apache::resetpw;
52:
53: use strict;
54: use Apache::Constants qw(:common);
55: use Apache::lonacc;
56: use Apache::lonnet;
57: use Apache::loncommon;
58: use Apache::lonlocal;
59: use LONCAPA;
60:
61: sub handler {
62: my $r = shift;
63: &Apache::loncommon::content_type($r,'text/html');
64: $r->send_http_header;
65: if ($r->header_only) {
66: return OK;
67: }
68: my $contact_name = &mt('LON-CAPA helpdesk');
69: my $contact_email = $r->dir_config('lonSupportEMail');
70: my $server = $r->dir_config('lonHostID');
1.19 raeburn 71: my $defdom = &Apache::lonnet::default_login_domain();
1.22.2.3! raeburn 72: my $handle = &Apache::lonnet::check_for_valid_session($r);
! 73: my $lonidsdir=$r->dir_config('lonIDsDir');
! 74: if ($handle ne '') {
! 75: if ($handle=~/^publicuser\_/) {
! 76: unlink($r->dir_config('lonIDsDir')."/$handle.id");
! 77: } else {
! 78: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
! 79: }
! 80: }
1.1 raeburn 81: &Apache::lonacc::get_posted_cgi($r);
82: &Apache::lonlocal::get_language_handle($r);
83: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
84:
85: my @emailtypes = ('permanentemail','critnotification','notification');
1.2 albertel 86: my $uname = &unescape($env{'form.uname'});
1.22.2.2 raeburn 87: my $useremail = $env{'form.useremail'};
1.1 raeburn 88: my $udom = $env{'form.udom'};
89: my $token = $env{'form.token'};
1.22.2.2 raeburn 90: my $case_change;
1.22.2.3! raeburn 91: my $brcrum = [];
! 92: my $bread_crumbs_component = 'Forgotten Password';
! 93: if ($token) {
! 94: push (@{$brcrum},
! 95: {href => '/adm/resetpw',
! 96: text => 'Update Password'});
! 97:
! 98: $bread_crumbs_component = 'Reset Password';
! 99: } else {
! 100: push (@{$brcrum},
! 101: {href => '/adm/resetpw',
! 102: text => 'Account Information'});
! 103: if ($uname && $udom) {
! 104: push (@{$brcrum},
! 105: {href => '/adm/resetpw',
! 106: text => 'Result'});
! 107: }
! 108: }
! 109: my $args = {no_inline_link => 1,
! 110: bread_crumbs => $brcrum,
! 111: bread_crumbs_component => $bread_crumbs_component};
! 112: $r->print(&Apache::loncommon::start_page('Reset password','',$args));
1.11 bisitz 113: $r->print('<h3>'.&mt('Reset forgotten LON-CAPA password').'</h3>');
1.1 raeburn 114: my $output;
115: if ($token) {
116: $output = &reset_passwd($r,$token,$contact_name,$contact_email);
117: } elsif ($uname && $udom) {
1.6 albertel 118: my $domdesc = &Apache::lonnet::domain($udom,'description');
1.22.2.2 raeburn 119: my $homeserver = &Apache::lonnet::homeserver($uname,$udom);
120: if ($homeserver eq 'no_host') {
121: my $lc_uname = lc($uname);
122: if ($lc_uname ne $uname) {
123: $homeserver = &Apache::lonnet::homeserver($lc_uname,$udom);
124: unless ($homeserver eq 'no_host') {
125: $uname = $lc_uname;
126: $useremail = lc($env{'form.useremail'});
127: $case_change = 1;
128: }
129: }
130: }
1.1 raeburn 131: my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
132: if ($authtype =~ /^internal/) {
1.3 raeburn 133: if ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
134: $output = &invalid_state('baduseremail',$domdesc,
135: $contact_name,$contact_email);
136: } else {
137: my %userinfo =
1.4 albertel 138: &Apache::lonnet::get('environment',\@emailtypes,
139: $udom,$uname);
1.18 raeburn 140: my @allemails;
1.3 raeburn 141: foreach my $type (@emailtypes) {
1.18 raeburn 142: my $email = $userinfo{$type};
143: my @items;
144: if ($email =~ /,/) {
145: @items = split(',',$userinfo{$type});
146: } else {
147: @items = ($email);
148: }
149: foreach my $item (@items) {
150: if ($item =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
1.22.2.2 raeburn 151: if ($case_change) {
152: my $lcitem = lc($item);
153: unless(grep(/^\Q$lcitem\E$/,@allemails)) {
154: push(@allemails,$lcitem);
155: }
156: } else {
157: unless(grep(/^\Q$item\E$/,@allemails)) {
158: push(@allemails,$item);
159: }
1.18 raeburn 160: }
161: }
1.3 raeburn 162: }
163: }
1.18 raeburn 164: if (@allemails > 0) {
165: if (grep(/^\Q$useremail\E$/,@allemails)) {
166: $output = &send_token($uname,$udom,$useremail,$server,
1.3 raeburn 167: $domdesc,$contact_name,
168: $contact_email);
169: } else {
170: $output = &invalid_state('mismatch',$domdesc,
171: $contact_name,
172: $contact_email);
173: }
174: } else {
175: $output = &invalid_state('missing',$domdesc,
176: $contact_name,$contact_email);
1.1 raeburn 177: }
178: }
179: } elsif ($authtype =~ /^(krb|unix|local)/) {
180: $output = &invalid_state('authentication',$domdesc,
181: $contact_name,$contact_email);
182: } else {
183: $output = &invalid_state('invalid',$domdesc,
184: $contact_name,$contact_email);
185: }
186: } else {
187: $output = &get_uname($defdom);
188: }
189: $r->print($output);
190: $r->print(&Apache::loncommon::end_page());
191: return OK;
192: }
193:
194: sub get_uname {
195: my ($defdom) = @_;
196: my %lt = &Apache::lonlocal::texthash(
1.22.2.1 raeburn 197: unam => 'LON-CAPA username',
198: udom => 'LON-CAPA domain',
1.12 schafran 199: uemail => 'E-mail address in LON-CAPA',
1.1 raeburn 200: proc => 'Proceed');
201:
1.22.2.2 raeburn 202: my $msg = '<br />'.&mt('To be able to reset a forgotten password:')
1.8 bisitz 203: .'<ul><li>'.&mt('An e-mail address must have previously been associated with your LON-CAPA username.').'</li>'
1.22.2.2 raeburn 204: .'<li>'.&mt('You must be able to access e-mail sent to the e-mail address associated with your WebCenter account.').'</li></ul><br />'
205: .&mt('In most cases the GCI WebCenter username is the same as your e-mail address, in which case you will enter the same information twice. ').'<br />';
1.22.2.3! raeburn 206: $msg .= '<form name="forgotpw" method="post" action="/adm/resetpw">'.
! 207: &Apache::lonhtmlcommon::start_pick_box().
! 208: &Apache::lonhtmlcommon::row_title('LON-CAPA '.$lt{'unam'}).
! 209: '<input type="text" name="uname" size="30" />'.
! 210: '<input type="hidden" name="udom" value="'.$defdom.'" />'.
! 211: &Apache::lonhtmlcommon::row_closure(1).
! 212: &Apache::lonhtmlcommon::row_title($lt{'uemail'}).
! 213: '<input type="text" name="useremail" size="30" />'.
! 214: &Apache::lonhtmlcommon::end_pick_box().
! 215: '<br /><br /><input type="submit" name="resetter" value="'.$lt{'proc'}.'" /></form>'."\n";
1.1 raeburn 216: return $msg;
217: }
218:
219: sub send_token {
220: my ($uname,$udom,$email,$server,$domdesc,$contact_name,
221: $contact_email) = @_;
1.11 bisitz 222: my $msg = &mt('Thank you for your request to reset the password for your LON-CAPA account.').'<br /><br />';
1.1 raeburn 223:
224: my $now = time;
225: my $temppasswd = &create_passwd();
1.2 albertel 226: my %info = ('ip' => $ENV{'REMOTE_ADDR'},
227: 'time' => $now,
228: 'domain' => $udom,
229: 'username' => $uname,
230: 'email' => $email,
231: 'temppasswd' => $temppasswd);
1.1 raeburn 232:
1.3 raeburn 233: my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
234: if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2 albertel 235: my $esc_token = &escape($token);
1.15 raeburn 236: my $showtime = &Apache::lonlocal::locallocaltime(time);
237: my $reseturl = &Apache::lonnet::absolute_url().'/adm/resetpw?token='.$esc_token;
1.22.2.3! raeburn 238: my $mailmsg = &mt('A request was submitted on [_1] for reset of the password for your LON-CAPA account.',$showtime)." \n".&mt('To complete this process please open a web browser and enter the following URL in the address/location box: [_1]',"\n\n".$reseturl);
1.1 raeburn 239: my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
240: $contact_email);
241: if ($result eq 'ok') {
1.22.2.2 raeburn 242: $msg .= &mt('An e-mail sent to the e-mail address associated with your LON-CAPA account includes the web address for the link you should use to complete the reset process.').'<br /><br />'.&mt('The link included in the message will be valid for the next [_1]two[_2] hours.','<b>','</b>');
1.1 raeburn 243: } else {
1.22.2.2 raeburn 244: $msg .= &mt('An error occurred when sending a message to the e-mail address associated with your LON-CAPA account. Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1 raeburn 245: }
246: } else {
1.22.2.2 raeburn 247: $msg .= &mt('An error occurred creating a token required for the password reset process. Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1 raeburn 248: }
249: return $msg;
250: }
251:
252: sub send_mail {
253: my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
254: my $outcome;
255: my $requestmail = "To: $email\n".
256: "From: $contact_name <$contact_email>\n".
257: "Subject: ".&mt('Your LON-CAPA account')."\n".
258: "\n\n".$mailmsg."\n\n".
259: &mt('[_1] LON-CAPA support team',$domdesc)."\n".
260: "$contact_email\n";
261: if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
262: print MAIL $requestmail;
263: close(MAIL);
264: $outcome = 'ok';
265: } else {
266: $outcome = 'fail';
267: }
268: return $outcome;
269: }
270:
271: sub invalid_state {
272: my ($error,$domdesc,$contact_name,$contact_email) = @_;
273: my $msg;
274: if ($error eq 'invalid') {
1.22.2.3! raeburn 275: $msg = '<p class="LC_warning">'.&mt('The username you provided was not verified as a valid username in the LON-CAPA system for the [_1] domain.',$domdesc)
! 276: .'</p>'.&mt('Please [_1]go back[_2] and try again.','<a href="javascript:history.go(-1)"><u>','</u></a>');
1.1 raeburn 277: } else {
1.3 raeburn 278: if ($error eq 'baduseremail') {
279: $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
280: } elsif ($error eq 'mismatch') {
281: $msg = &mt('The e-mail address you provided does not match the address recorded in the LON-CAPA system for the username and domain you provided.');
282: } elsif ($error eq 'missing') {
1.1 raeburn 283: $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
284: } elsif ($error eq 'authentication') {
285: $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
286: }
1.22.2.3! raeburn 287: $msg = '<p class="LC_warning">'.$msg.'</p>';
1.1 raeburn 288: if ($contact_email ne '') {
289: my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
1.8 bisitz 290: $msg .= '<br /> '.&mt('You may wish to contact the [_1]LON-CAPA helpdesk[_2] for the [_3] domain.'
291: ,'<a href="/adm/helpdesk?origurl='.$escuri.'">','</a>',$domdesc);
1.1 raeburn 292: } else {
1.8 bisitz 293: $msg .= '<br /> '.&mt('You may wish to send an e-mail to the server administrator: [_1] for the [_2] domain.',$Apache::lonnet::perlvar{'AdminEmail'},$domdesc);
1.1 raeburn 294: }
295: }
296: return $msg;
297: }
298:
299: sub reset_passwd {
300: my ($r,$token,$contact_name,$contact_email) = @_;
301: my $msg;
302: my %data = &Apache::lonnet::tmpget($token);
303: my $now = time;
304: if (keys(%data) == 0) {
1.17 bisitz 305: $msg = &mt('Sorry, the URL you provided to complete the reset of your password was invalid. Either the token included in the URL has been deleted or the URL you provided was invalid. Please submit a [_1]new request[_2] for a password reset, and follow the link to the new URL included in the e-mail that will be sent to you, to allow you to enter a new password.'
306: ,'<a href="/adm/resetpw">','</a>');
1.1 raeburn 307: return $msg;
308: }
309: if (($data{'time'} =~ /^\d+$/) &&
310: ($data{'username'} ne '') &&
311: ($data{'domain'} ne '') &&
1.3 raeburn 312: ($data{'email'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) &&
1.1 raeburn 313: ($data{'temppasswd'} =~/^\w+$/)) {
1.9 raeburn 314: my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.1 raeburn 315: if ($now - $data{'time'} < 7200) {
316: if ($env{'form.action'} eq 'verify_and_change_pass') {
1.22.2.2 raeburn 317: my $homeserver = &Apache::lonnet::homeserver($env{'form.uname'},$env{'form.udom'});
318: if ($homeserver eq 'no_host') {
319: my $lc_uname = lc($env{'form.uname'});
320: if ($lc_uname ne $env{'form.uname'}) {
321: $homeserver = &Apache::lonnet::homeserver($lc_uname,$env{'form.udom'});
322: unless ($homeserver eq 'no_host') {
323: if ($env{'form.uname'} eq $env{'form.email'}) {
324: $env{'form.email'} = $lc_uname;
325: }
326: $env{'form.uname'} = $lc_uname;
327: }
328: }
329: }
1.22 raeburn 330: unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'}) && ($env{'form.email'} eq $data{'email'})) {
331: $msg = &generic_failure_msg($contact_name,$contact_email);
332: return $msg;
333: }
1.1 raeburn 334: my $change_failed =
1.2 albertel 335: &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1 raeburn 336: if (!$change_failed) {
337: my $delete = &Apache::lonnet::tmpdel($token);
1.9 raeburn 338: my $now = &Apache::lonlocal::locallocaltime(time);
1.1 raeburn 339: my $domdesc =
1.6 albertel 340: &Apache::lonnet::domain($data{'domain'},'description');
1.1 raeburn 341: my $mailmsg = &mt('The password for your LON-CAPA account in the [_1] domain was changed [_2] from IP address: [_3]. If you did not perform this change or authorize it, please contact the [_4] ([_5]).',$domdesc,$now,$ENV{'REMOTE_ADDR'},$contact_name,$contact_email)."\n";
342: my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
343: $contact_name,$contact_email);
344: if ($result eq 'ok') {
345: $msg .= &mt('An e-mail confirming setting of the password for your LON-CAPA account has been sent to [_1].',$data{'email'});
346: } else {
347: $msg .= &mt('An error occurred when sending e-mail to [_1] confirming setting of your new password.',$data{'email'});
348: }
1.17 bisitz 349: $msg .= '<br /><br />'
350: .'<a href="/adm/login">'.&mt('Go to the login page').'</a>.';
1.20 raeburn 351: } elsif ($change_failed eq 'invalid_client') {
352: my $homeserver = &Apache::lonnet::homeserver($data{'username'},$data{'domain'});
353: if ($homeserver eq 'no_host') {
354: $msg .= &generic_failure_msg($contact_name,$contact_email);
355: } else {
356: my $protocol = $Apache::lonnet::protocol{$homeserver};
357: $protocol = 'http' if ($protocol ne 'https');
358: my $url = $protocol.'://'.&Apache::lonnet::hostname($homeserver).
359: '/adm/resetpw';
360: my ($opentag,$closetag);
361: if ($url) {
362: $opentag = '<a href="'.$url.'">';
363: $closetag = '</a>';
364: }
365: $msg .= &mt('A problem occurred when attempting to reset the password for your account. Please try again from your [_1]home server[_2].',$opentag,$closetag);
366: }
1.1 raeburn 367: } else {
1.20 raeburn 368: $msg .= &generic_failure_msg($contact_name,$contact_email);
1.1 raeburn 369: }
370: } else {
1.22.2.2 raeburn 371: $r->print(&mt('The token included in an e-mail sent to you [_1] has been verified, so you may now proceed to reset the password for your LON-CAPA account.',$reqtime).'<br />'.
372: '<p>'.&mt('Please enter the username and domain of the LON-CAPA account, and the associated e-mail address, for which you are setting a password.').'<br />'.
373: &mt('In most cases the GCI WebCenter username is the same as your e-mail address, in which case you will enter the same information twice.').'</p>'.
374: '<p>'.&mt('The new password must contain at least 7 characters.').' '.
375: &mt('Your new password will be sent to the LON-CAPA server in an encrypted form.').'<br /></p>');
1.1 raeburn 376: &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
377: }
378: } else {
1.17 bisitz 379: $msg = &mt('Sorry, the token generated when you requested a password reset has expired. Please submit a [_1]new request[_2], and follow the link to the web page included in the new e-mail that will be sent to you, to allow you to enter a new password.'
380: ,'<a href="/adm/resetpw">','</a>');
1.1 raeburn 381: }
382: } else {
1.17 bisitz 383: $msg .= &mt('Sorry, the URL generated when you requested reset of your password contained incomplete information. Please submit a [_1]new request[_2] for a password reset, and use the new URL that will be sent to your e-mail account to complete the process.'
384: ,'<a href="/adm/resetpw">','</a>');
1.1 raeburn 385: }
386: return $msg;
387: }
388:
1.20 raeburn 389: sub generic_failure_msg {
390: my ($contact_name,$contact_email) = @_;
391: return &mt('A problem occurred when attempting to reset the password for your account. Please contact the [_1] - ([_2]) for assistance.',
392: $contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>');
393: }
394:
1.1 raeburn 395: sub create_passwd {
396: my $passwd = '';
1.2 albertel 397: my @letts = ("a".."z");
1.1 raeburn 398: for (my $i=0; $i<8; $i++) {
1.2 albertel 399: my $lettnum = int(rand(2));
1.1 raeburn 400: my $item = '';
401: if ($lettnum) {
1.2 albertel 402: $item = $letts[int(rand(26))];
403: my $uppercase = int(rand(2));
1.1 raeburn 404: if ($uppercase) {
405: $item =~ tr/a-z/A-Z/;
406: }
407: } else {
1.2 albertel 408: $item = int(rand(10));
1.1 raeburn 409: }
410: $passwd .= $item;
411: }
412: return ($passwd);
413: }
414:
415: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>