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