Annotation of loncom/interface/resetpw.pm, revision 1.23.2.1
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.1! raeburn 4: # $Id: resetpw.pm,v 1.23 2010/02/19 15:41:40 bisitz 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.1 raeburn 72: &Apache::lonacc::get_posted_cgi($r);
73: &Apache::lonlocal::get_language_handle($r);
74: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
75:
76: my @emailtypes = ('permanentemail','critnotification','notification');
1.2 albertel 77: my $uname = &unescape($env{'form.uname'});
1.1 raeburn 78: my $udom = $env{'form.udom'};
79: my $token = $env{'form.token'};
1.10 raeburn 80: my $start_page =
81: &Apache::loncommon::start_page('Reset password','',
82: {
83: 'no_inline_link' => 1,});
84: $r->print($start_page);
1.11 bisitz 85: $r->print('<h3>'.&mt('Reset forgotten LON-CAPA password').'</h3>');
1.1 raeburn 86: my $output;
87: if ($token) {
88: $output = &reset_passwd($r,$token,$contact_name,$contact_email);
89: } elsif ($uname && $udom) {
1.6 albertel 90: my $domdesc = &Apache::lonnet::domain($udom,'description');
1.1 raeburn 91: my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
92: if ($authtype =~ /^internal/) {
1.3 raeburn 93: my $useremail = $env{'form.useremail'};
94: if ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
95: $output = &invalid_state('baduseremail',$domdesc,
96: $contact_name,$contact_email);
97: } else {
98: my %userinfo =
1.4 albertel 99: &Apache::lonnet::get('environment',\@emailtypes,
100: $udom,$uname);
1.18 raeburn 101: my @allemails;
1.3 raeburn 102: foreach my $type (@emailtypes) {
1.18 raeburn 103: my $email = $userinfo{$type};
104: my @items;
105: if ($email =~ /,/) {
106: @items = split(',',$userinfo{$type});
107: } else {
108: @items = ($email);
109: }
110: foreach my $item (@items) {
111: if ($item =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
112: unless(grep(/^\Q$item\E$/,@allemails)) {
113: push(@allemails,$item);
114: }
115: }
1.3 raeburn 116: }
117: }
1.18 raeburn 118: if (@allemails > 0) {
119: if (grep(/^\Q$useremail\E$/,@allemails)) {
120: $output = &send_token($uname,$udom,$useremail,$server,
1.3 raeburn 121: $domdesc,$contact_name,
122: $contact_email);
123: } else {
124: $output = &invalid_state('mismatch',$domdesc,
125: $contact_name,
126: $contact_email);
127: }
128: } else {
129: $output = &invalid_state('missing',$domdesc,
130: $contact_name,$contact_email);
1.1 raeburn 131: }
132: }
133: } elsif ($authtype =~ /^(krb|unix|local)/) {
134: $output = &invalid_state('authentication',$domdesc,
135: $contact_name,$contact_email);
136: } else {
137: $output = &invalid_state('invalid',$domdesc,
138: $contact_name,$contact_email);
139: }
140: } else {
141: $output = &get_uname($defdom);
142: }
143: $r->print($output);
144: $r->print(&Apache::loncommon::end_page());
145: return OK;
146: }
147:
148: sub get_uname {
149: my ($defdom) = @_;
150: my %lt = &Apache::lonlocal::texthash(
151: unam => 'username',
152: udom => 'domain',
1.12 schafran 153: uemail => 'E-mail address in LON-CAPA',
1.1 raeburn 154: proc => 'Proceed');
155:
1.23 bisitz 156: 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 157: $msg .= '<br /><br />'.&mt('Three conditions must be met:')
158: .'<ul><li>'.&mt('An e-mail address must have previously been associated with your LON-CAPA username.').'</li>'
159: .'<li>'.&mt('You must be able to access e-mail sent to that address.').'</li>'
160: .'<li>'.&mt('Your LON-CAPA account must be of a type for which LON-CAPA can reset a password.')
161: .'</ul>';
1.1 raeburn 162: $msg .= qq|
163: <form name="forgotpw" method="post">
164: <table>
165: <tr><td>
166: <tr><td align="left">LON-CAPA $lt{'unam'}: </td>
1.16 bisitz 167: <td><input type="text" name="uname" size="15" /></td></tr>
1.1 raeburn 168: <tr><td align="left">LON-CAPA $lt{'udom'}: </td>
169: <td>|;
170: $msg .= &Apache::loncommon::select_dom_form($defdom,'udom');
171: $msg .= qq|</td></tr>
1.3 raeburn 172: <tr><td align="left">$lt{'uemail'}: </td>
1.16 bisitz 173: <td><input type="text" name="useremail" size="30" /></td></tr>
1.1 raeburn 174: <tr><td colspan="2" align="left"><br />
1.16 bisitz 175: <input type="submit" value="$lt{'proc'}" /></td></tr>
1.1 raeburn 176: </table>
1.2 albertel 177: </form>
1.1 raeburn 178: |;
179: return $msg;
180: }
181:
182: sub send_token {
183: my ($uname,$udom,$email,$server,$domdesc,$contact_name,
184: $contact_email) = @_;
1.11 bisitz 185: my $msg = &mt('Thank you for your request to reset the password for your LON-CAPA account.').'<br /><br />';
1.1 raeburn 186:
187: my $now = time;
188: my $temppasswd = &create_passwd();
1.2 albertel 189: my %info = ('ip' => $ENV{'REMOTE_ADDR'},
190: 'time' => $now,
191: 'domain' => $udom,
192: 'username' => $uname,
193: 'email' => $email,
194: 'temppasswd' => $temppasswd);
1.1 raeburn 195:
1.3 raeburn 196: my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
197: if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2 albertel 198: my $esc_token = &escape($token);
1.15 raeburn 199: my $showtime = &Apache::lonlocal::locallocaltime(time);
200: my $reseturl = &Apache::lonnet::absolute_url().'/adm/resetpw?token='.$esc_token;
201: my $mailmsg = &mt('A request was submitted on [_1] for reset of the password for your LON-CAPA account.',$showtime).' '.&mt('To complete this process please open a web browser and enter the following URL in the address/location box: [_1]',$reseturl);
1.1 raeburn 202: my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
203: $contact_email);
204: if ($result eq 'ok') {
1.13 schafran 205: $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 />The link included in the message will be valid for the next <b>two</b> hours.");
1.1 raeburn 206: } else {
1.7 albertel 207: $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 208: }
209: } else {
1.4 albertel 210: $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 211: }
212: return $msg;
213: }
214:
215: sub send_mail {
216: my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
217: my $outcome;
218: my $requestmail = "To: $email\n".
219: "From: $contact_name <$contact_email>\n".
220: "Subject: ".&mt('Your LON-CAPA account')."\n".
1.23.2.1! raeburn 221: "Content-type: text/plain\;charset=UTF-8\n".
1.1 raeburn 222: "\n\n".$mailmsg."\n\n".
223: &mt('[_1] LON-CAPA support team',$domdesc)."\n".
224: "$contact_email\n";
225: if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
226: print MAIL $requestmail;
227: close(MAIL);
228: $outcome = 'ok';
229: } else {
230: $outcome = 'fail';
231: }
232: return $outcome;
233: }
234:
235: sub invalid_state {
236: my ($error,$domdesc,$contact_name,$contact_email) = @_;
237: my $msg;
238: if ($error eq 'invalid') {
1.8 bisitz 239: $msg = &mt('The username you provided was not verified as a valid username in the LON-CAPA system for the [_1] domain.',$domdesc)
240: .' '.&mt('Please [_1]go back[_2] and try again.','<a href="javascript:history.go(-1)"><u>','</u></a>');
1.1 raeburn 241: } else {
1.3 raeburn 242: if ($error eq 'baduseremail') {
243: $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
244: } elsif ($error eq 'mismatch') {
245: $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.');
246: } elsif ($error eq 'missing') {
1.1 raeburn 247: $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
248: } elsif ($error eq 'authentication') {
249: $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
250: }
251: if ($contact_email ne '') {
252: my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
1.8 bisitz 253: $msg .= '<br /> '.&mt('You may wish to contact the [_1]LON-CAPA helpdesk[_2] for the [_3] domain.'
254: ,'<a href="/adm/helpdesk?origurl='.$escuri.'">','</a>',$domdesc);
1.1 raeburn 255: } else {
1.8 bisitz 256: $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 257: }
258: }
259: return $msg;
260: }
261:
262: sub reset_passwd {
263: my ($r,$token,$contact_name,$contact_email) = @_;
264: my $msg;
265: my %data = &Apache::lonnet::tmpget($token);
266: my $now = time;
267: if (keys(%data) == 0) {
1.17 bisitz 268: $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.'
269: ,'<a href="/adm/resetpw">','</a>');
1.1 raeburn 270: return $msg;
271: }
272: if (($data{'time'} =~ /^\d+$/) &&
273: ($data{'username'} ne '') &&
274: ($data{'domain'} ne '') &&
1.3 raeburn 275: ($data{'email'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) &&
1.1 raeburn 276: ($data{'temppasswd'} =~/^\w+$/)) {
1.9 raeburn 277: my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.1 raeburn 278: if ($now - $data{'time'} < 7200) {
279: if ($env{'form.action'} eq 'verify_and_change_pass') {
1.22 raeburn 280: unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'}) && ($env{'form.email'} eq $data{'email'})) {
281: $msg = &generic_failure_msg($contact_name,$contact_email);
282: return $msg;
283: }
1.1 raeburn 284: my $change_failed =
1.2 albertel 285: &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1 raeburn 286: if (!$change_failed) {
287: my $delete = &Apache::lonnet::tmpdel($token);
1.9 raeburn 288: my $now = &Apache::lonlocal::locallocaltime(time);
1.1 raeburn 289: my $domdesc =
1.6 albertel 290: &Apache::lonnet::domain($data{'domain'},'description');
1.1 raeburn 291: 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";
292: my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
293: $contact_name,$contact_email);
294: if ($result eq 'ok') {
295: $msg .= &mt('An e-mail confirming setting of the password for your LON-CAPA account has been sent to [_1].',$data{'email'});
296: } else {
297: $msg .= &mt('An error occurred when sending e-mail to [_1] confirming setting of your new password.',$data{'email'});
298: }
1.17 bisitz 299: $msg .= '<br /><br />'
300: .'<a href="/adm/login">'.&mt('Go to the login page').'</a>.';
1.20 raeburn 301: } elsif ($change_failed eq 'invalid_client') {
302: my $homeserver = &Apache::lonnet::homeserver($data{'username'},$data{'domain'});
303: if ($homeserver eq 'no_host') {
304: $msg .= &generic_failure_msg($contact_name,$contact_email);
305: } else {
306: my $protocol = $Apache::lonnet::protocol{$homeserver};
307: $protocol = 'http' if ($protocol ne 'https');
308: my $url = $protocol.'://'.&Apache::lonnet::hostname($homeserver).
309: '/adm/resetpw';
310: my ($opentag,$closetag);
311: if ($url) {
312: $opentag = '<a href="'.$url.'">';
313: $closetag = '</a>';
314: }
315: $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);
316: }
1.1 raeburn 317: } else {
1.20 raeburn 318: $msg .= &generic_failure_msg($contact_name,$contact_email);
1.1 raeburn 319: }
320: } else {
1.13 schafran 321: $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 322: $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 />');
323: &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
324: }
325: } else {
1.17 bisitz 326: $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.'
327: ,'<a href="/adm/resetpw">','</a>');
1.1 raeburn 328: }
329: } else {
1.17 bisitz 330: $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.'
331: ,'<a href="/adm/resetpw">','</a>');
1.1 raeburn 332: }
333: return $msg;
334: }
335:
1.20 raeburn 336: sub generic_failure_msg {
337: my ($contact_name,$contact_email) = @_;
338: return &mt('A problem occurred when attempting to reset the password for your account. Please contact the [_1] - ([_2]) for assistance.',
339: $contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>');
340: }
341:
1.1 raeburn 342: sub create_passwd {
343: my $passwd = '';
1.2 albertel 344: my @letts = ("a".."z");
1.1 raeburn 345: for (my $i=0; $i<8; $i++) {
1.2 albertel 346: my $lettnum = int(rand(2));
1.1 raeburn 347: my $item = '';
348: if ($lettnum) {
1.2 albertel 349: $item = $letts[int(rand(26))];
350: my $uppercase = int(rand(2));
1.1 raeburn 351: if ($uppercase) {
352: $item =~ tr/a-z/A-Z/;
353: }
354: } else {
1.2 albertel 355: $item = int(rand(10));
1.1 raeburn 356: }
357: $passwd .= $item;
358: }
359: return ($passwd);
360: }
361:
362: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>