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