Annotation of loncom/interface/resetpw.pm, revision 1.37
1.1 raeburn 1: # The LearningOnline Network
2: # Allow access to password changing via a token sent to user's e-mail.
3: #
1.37 ! raeburn 4: # $Id: resetpw.pm,v 1.36 2013/08/17 00:34:29 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');
1.27 raeburn 69: my $origmail = $r->dir_config('lonSupportEMail');
1.1 raeburn 70: my $server = $r->dir_config('lonHostID');
1.19 raeburn 71: my $defdom = &Apache::lonnet::default_login_domain();
1.27 raeburn 72: my $contacts =
73: &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
74: $defdom,$origmail);
75: my ($contact_email) = split(',',$contacts);
1.26 raeburn 76: my $handle = &Apache::lonnet::check_for_valid_session($r);
77: my $lonidsdir=$r->dir_config('lonIDsDir');
78: if ($handle ne '') {
79: if ($handle=~/^publicuser\_/) {
80: unlink($r->dir_config('lonIDsDir')."/$handle.id");
81: } else {
82: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
83: }
84: }
1.1 raeburn 85: &Apache::lonacc::get_posted_cgi($r);
86: &Apache::lonlocal::get_language_handle($r);
87: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
88:
89: my @emailtypes = ('permanentemail','critnotification','notification');
1.2 albertel 90: my $uname = &unescape($env{'form.uname'});
1.1 raeburn 91: my $udom = $env{'form.udom'};
92: my $token = $env{'form.token'};
1.26 raeburn 93: my $brcrum = [];
94: if ($token) {
95: push (@{$brcrum},
96: {href => '/adm/resetpw',
97: text => 'Update Password'});
98: } else {
99: push (@{$brcrum},
100: {href => '/adm/resetpw',
101: text => 'Account Information'});
102: if ($uname && $udom) {
103: push (@{$brcrum},
104: {href => '/adm/resetpw',
105: text => 'Result'});
106: }
107: }
1.33 bisitz 108: my $args = {bread_crumbs => $brcrum};
1.26 raeburn 109: $r->print(&Apache::loncommon::start_page('Reset password','',$args));
1.33 bisitz 110: $r->print('<h2>'.&mt('Reset forgotten LON-CAPA password').'</h2>');
1.1 raeburn 111: my $output;
112: if ($token) {
113: $output = &reset_passwd($r,$token,$contact_name,$contact_email);
114: } elsif ($uname && $udom) {
1.6 albertel 115: my $domdesc = &Apache::lonnet::domain($udom,'description');
1.1 raeburn 116: my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
117: if ($authtype =~ /^internal/) {
1.3 raeburn 118: my $useremail = $env{'form.useremail'};
1.37 ! raeburn 119: my ($blocked,$blocktext) =
! 120: &Apache::loncommon::blocking_status('passwd',$uname,$udom);
! 121: if ($blocked) {
! 122: $output = '<p class="LC_warning">'.$blocktext.'</p>'
! 123: .&display_actions($contact_email,$domdesc);
! 124: } elsif ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
! 125: $output = &invalid_state('baduseremail',$domdesc,
! 126: $contact_name,$contact_email);
1.3 raeburn 127: } else {
128: my %userinfo =
1.4 albertel 129: &Apache::lonnet::get('environment',\@emailtypes,
130: $udom,$uname);
1.18 raeburn 131: my @allemails;
1.3 raeburn 132: foreach my $type (@emailtypes) {
1.18 raeburn 133: my $email = $userinfo{$type};
134: my @items;
135: if ($email =~ /,/) {
136: @items = split(',',$userinfo{$type});
137: } else {
138: @items = ($email);
139: }
140: foreach my $item (@items) {
141: if ($item =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
142: unless(grep(/^\Q$item\E$/,@allemails)) {
143: push(@allemails,$item);
144: }
145: }
1.3 raeburn 146: }
147: }
1.18 raeburn 148: if (@allemails > 0) {
149: if (grep(/^\Q$useremail\E$/,@allemails)) {
150: $output = &send_token($uname,$udom,$useremail,$server,
1.3 raeburn 151: $domdesc,$contact_name,
152: $contact_email);
153: } else {
154: $output = &invalid_state('mismatch',$domdesc,
155: $contact_name,
156: $contact_email);
157: }
158: } else {
159: $output = &invalid_state('missing',$domdesc,
160: $contact_name,$contact_email);
1.1 raeburn 161: }
162: }
163: } elsif ($authtype =~ /^(krb|unix|local)/) {
164: $output = &invalid_state('authentication',$domdesc,
165: $contact_name,$contact_email);
166: } else {
167: $output = &invalid_state('invalid',$domdesc,
168: $contact_name,$contact_email);
169: }
170: } else {
171: $output = &get_uname($defdom);
172: }
173: $r->print($output);
174: $r->print(&Apache::loncommon::end_page());
175: return OK;
176: }
177:
178: sub get_uname {
179: my ($defdom) = @_;
180: my %lt = &Apache::lonlocal::texthash(
1.32 bisitz 181: unam => 'LON-CAPA username',
182: udom => 'LON-CAPA domain',
1.12 schafran 183: uemail => 'E-mail address in LON-CAPA',
1.1 raeburn 184: proc => 'Proceed');
185:
1.23 bisitz 186: my $msg = &mt('If you use the same account for other campus services besides LON-CAPA, (e.g., e-mail, course registration, etc.), a separate centrally managed mechanism likely exists to reset a password. However, if your account is used for just LON-CAPA access you will probably be able to reset a password from this page.');
1.8 bisitz 187: $msg .= '<br /><br />'.&mt('Three conditions must be met:')
188: .'<ul><li>'.&mt('An e-mail address must have previously been associated with your LON-CAPA username.').'</li>'
189: .'<li>'.&mt('You must be able to access e-mail sent to that address.').'</li>'
190: .'<li>'.&mt('Your LON-CAPA account must be of a type for which LON-CAPA can reset a password.')
191: .'</ul>';
1.26 raeburn 192: $msg .= '<form name="forgotpw" method="post" action="/adm/resetpw">'.
193: &Apache::lonhtmlcommon::start_pick_box().
1.32 bisitz 194: &Apache::lonhtmlcommon::row_title($lt{'unam'}).
1.26 raeburn 195: '<input type="text" name="uname" size="20" />'.
196: &Apache::lonhtmlcommon::row_closure(1).
1.32 bisitz 197: &Apache::lonhtmlcommon::row_title($lt{'udom'}).
1.26 raeburn 198: &Apache::loncommon::select_dom_form($defdom,'udom').
199: &Apache::lonhtmlcommon::row_closure(1).
200: &Apache::lonhtmlcommon::row_title($lt{'uemail'}).
201: '<input type="text" name="useremail" size="30" />'.
202: &Apache::lonhtmlcommon::end_pick_box().
203: '<br /><br /><input type="submit" name="resetter" value="'.$lt{'proc'}.'" /></form>';
1.1 raeburn 204: return $msg;
205: }
206:
207: sub send_token {
208: my ($uname,$udom,$email,$server,$domdesc,$contact_name,
209: $contact_email) = @_;
1.29 bisitz 210: my $msg =
211: '<p class="LC_info">'
212: .&mt('Thank you for your request to reset the password for your LON-CAPA account.')
213: .'</p>';
1.1 raeburn 214:
215: my $now = time;
216: my $temppasswd = &create_passwd();
1.2 albertel 217: my %info = ('ip' => $ENV{'REMOTE_ADDR'},
218: 'time' => $now,
219: 'domain' => $udom,
220: 'username' => $uname,
221: 'email' => $email,
222: 'temppasswd' => $temppasswd);
1.1 raeburn 223:
1.3 raeburn 224: my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
225: if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2 albertel 226: my $esc_token = &escape($token);
1.15 raeburn 227: my $showtime = &Apache::lonlocal::locallocaltime(time);
228: my $reseturl = &Apache::lonnet::absolute_url().'/adm/resetpw?token='.$esc_token;
1.26 raeburn 229: 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 230: my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
231: $contact_email);
232: if ($result eq 'ok') {
1.34 bisitz 233: $msg .=
234: &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.')
235: .'<br /><br />'
236: .&mt('The link included in the message will be valid for the next [_1]two[_2] hours.','<b>','</b>');
1.1 raeburn 237: } else {
1.28 bisitz 238: $msg .=
1.29 bisitz 239: '<p class="LC_error">'
240: .&mt('An error occurred when sending a message to the e-mail address'
241: .' associated with your LON-CAPA account.')
242: .'</p>'
243: .&display_actions($contact_email,$domdesc);
1.1 raeburn 244: }
245: } else {
1.28 bisitz 246: $msg .=
1.29 bisitz 247: '<p class="LC_error">'
248: .&mt('An error occurred creating a token required for the'
249: .' password reset process.')
250: .'</p>'
251: .&display_actions($contact_email,$domdesc);
1.1 raeburn 252: }
253: return $msg;
254: }
255:
256: sub send_mail {
257: my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
258: my $outcome;
259: my $requestmail = "To: $email\n".
260: "From: $contact_name <$contact_email>\n".
261: "Subject: ".&mt('Your LON-CAPA account')."\n".
1.25 bisitz 262: "Content-type: text/plain\;charset=UTF-8\n".
1.1 raeburn 263: "\n\n".$mailmsg."\n\n".
264: &mt('[_1] LON-CAPA support team',$domdesc)."\n".
265: "$contact_email\n";
266: if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
267: print MAIL $requestmail;
268: close(MAIL);
269: $outcome = 'ok';
270: } else {
271: $outcome = 'fail';
272: }
273: return $outcome;
274: }
275:
276: sub invalid_state {
277: my ($error,$domdesc,$contact_name,$contact_email) = @_;
278: my $msg;
279: if ($error eq 'invalid') {
1.29 bisitz 280: $msg =
281: '<p class="LC_warning">'
282: .&mt('The username you provided was not verified as a valid username'
283: .' in the LON-CAPA system for the [_1] domain.','<i>'.$domdesc.'</i>')
284: .'</p>';
285: $msg .= &display_actions($contact_email,$domdesc);
1.1 raeburn 286: } else {
1.3 raeburn 287: if ($error eq 'baduseremail') {
288: $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
289: } elsif ($error eq 'mismatch') {
290: $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.');
291: } elsif ($error eq 'missing') {
1.1 raeburn 292: $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
293: } elsif ($error eq 'authentication') {
294: $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
295: }
1.29 bisitz 296: $msg = '<p class="LC_warning">'.$msg.'</p>'
297: .&display_actions($contact_email,$domdesc);
1.1 raeburn 298: }
299: return $msg;
300: }
301:
302: sub reset_passwd {
303: my ($r,$token,$contact_name,$contact_email) = @_;
304: my $msg;
305: my %data = &Apache::lonnet::tmpget($token);
306: my $now = time;
307: if (keys(%data) == 0) {
1.17 bisitz 308: $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.'
309: ,'<a href="/adm/resetpw">','</a>');
1.1 raeburn 310: return $msg;
311: }
312: if (($data{'time'} =~ /^\d+$/) &&
313: ($data{'username'} ne '') &&
314: ($data{'domain'} ne '') &&
1.3 raeburn 315: ($data{'email'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) &&
1.1 raeburn 316: ($data{'temppasswd'} =~/^\w+$/)) {
1.9 raeburn 317: my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.37 ! raeburn 318: my ($blocked,$blocktext) =
! 319: &Apache::loncommon::blocking_status('passwd',$data{'username'},$data{'domain'});
! 320: if ($blocked) {
! 321: $msg = '<p class="LC_warning">'.$blocktext.'</p>';
! 322: return $msg;
! 323: } elsif ($now - $data{'time'} < 7200) {
1.1 raeburn 324: if ($env{'form.action'} eq 'verify_and_change_pass') {
1.22 raeburn 325: unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'}) && ($env{'form.email'} eq $data{'email'})) {
326: $msg = &generic_failure_msg($contact_name,$contact_email);
327: return $msg;
328: }
1.1 raeburn 329: my $change_failed =
1.2 albertel 330: &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1 raeburn 331: if (!$change_failed) {
332: my $delete = &Apache::lonnet::tmpdel($token);
1.9 raeburn 333: my $now = &Apache::lonlocal::locallocaltime(time);
1.1 raeburn 334: my $domdesc =
1.6 albertel 335: &Apache::lonnet::domain($data{'domain'},'description');
1.1 raeburn 336: 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";
337: my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
338: $contact_name,$contact_email);
1.33 bisitz 339: my $confirm_msg;
1.1 raeburn 340: if ($result eq 'ok') {
1.33 bisitz 341: $confirm_msg =
342: &Apache::lonhtmlcommon::confirm_success(
343: &mt('An e-mail confirming setting of the password'
344: .' for your LON-CAPA account has been sent to [_1].'
345: ,'<span class="LC_filename">'.$data{'email'}.'</span>'));
1.1 raeburn 346: } else {
1.33 bisitz 347: $confirm_msg =
348: &Apache::lonhtmlcommon::confirm_success(
349: &mt('An error occurred when sending e-mail to [_1]'
350: .' confirming setting of your new password.'
351: ,'<span class="LC_filename">'.$data{'email'}.'</span>'),1);
1.1 raeburn 352: }
1.33 bisitz 353: $msg .=
354: &Apache::loncommon::confirmwrapper($confirm_msg)
355: .&Apache::lonhtmlcommon::actionbox([
356: '<a href="/adm/login">'.&mt('Go to the login page').'</a>']);
1.20 raeburn 357: } elsif ($change_failed eq 'invalid_client') {
358: my $homeserver = &Apache::lonnet::homeserver($data{'username'},$data{'domain'});
359: if ($homeserver eq 'no_host') {
360: $msg .= &generic_failure_msg($contact_name,$contact_email);
361: } else {
362: my $protocol = $Apache::lonnet::protocol{$homeserver};
363: $protocol = 'http' if ($protocol ne 'https');
364: my $url = $protocol.'://'.&Apache::lonnet::hostname($homeserver).
365: '/adm/resetpw';
366: my ($opentag,$closetag);
367: if ($url) {
368: $opentag = '<a href="'.$url.'">';
369: $closetag = '</a>';
370: }
1.28 bisitz 371: $msg .=
372: '<p class="LC_warning">'
373: .&mt('A problem occurred when attempting to reset'
374: .' the password for your account.'
375: .' Please try again from your [_1]home server[_2].'
376: ,$opentag,$closetag)
377: .'</p>';
1.20 raeburn 378: }
1.1 raeburn 379: } else {
1.20 raeburn 380: $msg .= &generic_failure_msg($contact_name,$contact_email);
1.1 raeburn 381: }
382: } else {
1.13 schafran 383: $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 /><br />');
1.1 raeburn 384: $r->print(&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. The new password must contain at least 7 characters.').' '.&mt('Your new password will be sent to the LON-CAPA server in an encrypted form.').'<br />');
385: &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
386: }
387: } else {
1.28 bisitz 388: $msg =
389: '<p class="LC_warning">'
390: .&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.'
391: ,'<a href="/adm/resetpw">','</a>')
392: .'</p>';
1.1 raeburn 393: }
394: } else {
1.28 bisitz 395: $msg .=
396: '<p class="LC_warning">'
397: .&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.'
398: ,'<a href="/adm/resetpw">','</a>')
399: .'</p>';
1.1 raeburn 400: }
401: return $msg;
402: }
403:
1.20 raeburn 404: sub generic_failure_msg {
405: my ($contact_name,$contact_email) = @_;
1.28 bisitz 406: return
1.29 bisitz 407: '<p class="LC_error">'
408: .&mt('A problem occurred when attempting to reset the password for your account.')
409: .'<br />'
1.36 raeburn 410: .&mt('Please contact the [_1] ([_2]) for assistance.',
1.28 bisitz 411: $contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>')
412: .'</p>';
1.20 raeburn 413: }
414:
1.1 raeburn 415: sub create_passwd {
416: my $passwd = '';
1.2 albertel 417: my @letts = ("a".."z");
1.1 raeburn 418: for (my $i=0; $i<8; $i++) {
1.2 albertel 419: my $lettnum = int(rand(2));
1.1 raeburn 420: my $item = '';
421: if ($lettnum) {
1.2 albertel 422: $item = $letts[int(rand(26))];
423: my $uppercase = int(rand(2));
1.1 raeburn 424: if ($uppercase) {
425: $item =~ tr/a-z/A-Z/;
426: }
427: } else {
1.2 albertel 428: $item = int(rand(10));
1.1 raeburn 429: }
430: $passwd .= $item;
431: }
432: return ($passwd);
433: }
434:
1.29 bisitz 435: sub display_actions {
436: my ($contact_email, $domdesc) = @_;
437: my @msg = (&mt('[_1]Go back[_2] and try again',
438: '<a href="javascript:history.go(-1)">','</a>'));
439: my $msg2 = '';
440: if ($contact_email ne '') {
1.30 raeburn 441: my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
442: push(@msg, &mt('Contact the [_1]LON-CAPA helpdesk[_2] for the institution: [_3]',
443: '<a href="/adm/helpdesk?origurl='.$escuri.'">',
444: '</a>','<i>'.$domdesc.'</i>'));
445: } else {
446: $msg2 =
447: '<p>'
448: .&mt('You may wish to send an e-mail to the'
449: .' server administrator: [_1] for the [_2] domain.',
1.31 raeburn 450: '<i>'.$Apache::lonnet::perlvar{'AdmEMail'}.'</i>',
1.30 raeburn 451: '<i>'.$domdesc.'</i>')
452: .'</p>';
453: }
1.29 bisitz 454:
455: return &Apache::lonhtmlcommon::actionbox(\@msg).$msg2;
456:
457: }
458:
1.1 raeburn 459: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>