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