Annotation of loncom/interface/resetpw.pm, revision 1.7
1.1 raeburn 1: # The LearningOnline Network
2: # Allow access to password changing via a token sent to user's e-mail.
3: #
4: # Copyright Michigan State University Board of Trustees
5: #
6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
7: #
8: # LON-CAPA is free software; you can redistribute it and/or modify
9: # it under the terms of the GNU General Public License as published by
10: # the Free Software Foundation; either version 2 of the License, or
11: # (at your option) any later version.
12: #
13: # LON-CAPA is distributed in the hope that it will be useful,
14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: # GNU General Public License for more details.
17: #
18: # You should have received a copy of the GNU General Public License
19: # along with LON-CAPA; if not, write to the Free Software
20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21: #
22: # /home/httpd/html/adm/gpl.txt
23: #
24: # http://www.lon-capa.org/
25: #
26: #
27: package Apache::resetpw;
28:
29: use strict;
30: use Apache::Constants qw(:common);
31: use Apache::lonacc;
32: use Apache::lonnet;
33: use Apache::loncommon;
34: use Apache::lonlocal;
35: use LONCAPA;
36:
37: sub handler {
38: my $r = shift;
39: &Apache::loncommon::content_type($r,'text/html');
40: $r->send_http_header;
41: if ($r->header_only) {
42: return OK;
43: }
44: my $start_page =
45: &Apache::loncommon::start_page('Reset password','',
46: {
47: 'no_inline_link' => 1,});
48: $r->print($start_page);
49: my $contact_name = &mt('LON-CAPA helpdesk');
50: my $contact_email = $r->dir_config('lonSupportEMail');
51: my $server = $r->dir_config('lonHostID');
52: my $defdom = $r->dir_config('lonDefDomain');
53: &Apache::lonacc::get_posted_cgi($r);
54: &Apache::lonlocal::get_language_handle($r);
55: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
56:
57: my @emailtypes = ('permanentemail','critnotification','notification');
1.2 albertel 58: my $uname = &unescape($env{'form.uname'});
1.1 raeburn 59: my $udom = $env{'form.udom'};
60: my $token = $env{'form.token'};
61: $r->print(&mt('<h3>Reset forgotten LON-CAPA password</h3>'));
62: my $output;
63: if ($token) {
64: $output = &reset_passwd($r,$token,$contact_name,$contact_email);
65: } elsif ($uname && $udom) {
1.6 albertel 66: my $domdesc = &Apache::lonnet::domain($udom,'description');
1.1 raeburn 67: my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
68: if ($authtype =~ /^internal/) {
1.3 raeburn 69: my $useremail = $env{'form.useremail'};
70: if ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
71: $output = &invalid_state('baduseremail',$domdesc,
72: $contact_name,$contact_email);
73: } else {
74: my %userinfo =
1.4 albertel 75: &Apache::lonnet::get('environment',\@emailtypes,
76: $udom,$uname);
1.3 raeburn 77: my $email = '';
78: my $emailtarget;
79: foreach my $type (@emailtypes) {
80: $email = $userinfo{$type};
81: if ($email =~ /[^\@]+\@[^\@]+/) {
82: $emailtarget = $type;
83: last;
84: }
85: }
86: if ($email =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
87: if ($useremail eq $email) {
88: $output = &send_token($uname,$udom,$email,$server,
89: $domdesc,$contact_name,
90: $contact_email);
91: } else {
92: $output = &invalid_state('mismatch',$domdesc,
93: $contact_name,
94: $contact_email);
95: }
96: } else {
97: $output = &invalid_state('missing',$domdesc,
98: $contact_name,$contact_email);
1.1 raeburn 99: }
100: }
101: } elsif ($authtype =~ /^(krb|unix|local)/) {
102: $output = &invalid_state('authentication',$domdesc,
103: $contact_name,$contact_email);
104: } else {
105: $output = &invalid_state('invalid',$domdesc,
106: $contact_name,$contact_email);
107: }
108: } else {
109: $output = &get_uname($defdom);
110: }
111: $r->print($output);
112: $r->print(&Apache::loncommon::end_page());
113: return OK;
114: }
115:
116: sub get_uname {
117: my ($defdom) = @_;
118: my %lt = &Apache::lonlocal::texthash(
119: unam => 'username',
120: udom => 'domain',
1.3 raeburn 121: uemail => 'Email address in LON-CAPA',
1.1 raeburn 122: proc => 'Proceed');
123:
124: 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.');
125: $msg .= '<br /><br />'.&mt('Three conditions must be met:<ul><li>An e-mail address must have previously been associated with your LON-CAPA username.</li><li>You must be able to access e-mail sent to that address.</li><li>Your account must be of a type for which LON-CAPA can reset a password.</ul>');
126: $msg .= qq|
127: <form name="forgotpw" method="post">
128: <table>
129: <tr><td>
130: <tr><td align="left">LON-CAPA $lt{'unam'}: </td>
1.5 raeburn 131: <td><input type="text" name="uname" size="15" /> </td></tr>
1.1 raeburn 132: <tr><td align="left">LON-CAPA $lt{'udom'}: </td>
133: <td>|;
134: $msg .= &Apache::loncommon::select_dom_form($defdom,'udom');
135: $msg .= qq|</td></tr>
1.3 raeburn 136: <tr><td align="left">$lt{'uemail'}: </td>
1.5 raeburn 137: <td><input type="text" name="useremail" size="30" /></td></tr>
1.1 raeburn 138: <tr><td colspan="2" align="left"><br />
139: <input type="button" value="$lt{'proc'}" onClick="document.forgotpw.submit()"></td></tr>
140: </table>
1.2 albertel 141: </form>
1.1 raeburn 142: |;
143: return $msg;
144: }
145:
146: sub send_token {
147: my ($uname,$udom,$email,$server,$domdesc,$contact_name,
148: $contact_email) = @_;
149: my $msg = &mt('Thank you for your request to reset the password for your
150: LON-CAPA account.').'<br /><br />';
151:
152: my $now = time;
153: my $temppasswd = &create_passwd();
1.2 albertel 154: my %info = ('ip' => $ENV{'REMOTE_ADDR'},
155: 'time' => $now,
156: 'domain' => $udom,
157: 'username' => $uname,
158: 'email' => $email,
159: 'temppasswd' => $temppasswd);
1.1 raeburn 160:
1.3 raeburn 161: my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
162: if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2 albertel 163: my $esc_token = &escape($token);
1.1 raeburn 164: my $mailmsg = "A request was submitted on ".localtime(time)." for a reset of the ".
165: "password for your LON-CAPA account.".
166: "To complete this process please open a web browser and enter the following ".
167: "URL in the address/location box: ".&Apache::lonnet::absolute_url()."/adm/resetpw?token=$esc_token";
168: my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
169: $contact_email);
170: if ($result eq 'ok') {
1.3 raeburn 171: $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 172: } else {
1.7 ! albertel 173: $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 174: }
175: } else {
1.4 albertel 176: $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 177: }
178: return $msg;
179: }
180:
181: sub send_mail {
182: my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
183: my $outcome;
184: my $requestmail = "To: $email\n".
185: "From: $contact_name <$contact_email>\n".
186: "Subject: ".&mt('Your LON-CAPA account')."\n".
187: "\n\n".$mailmsg."\n\n".
188: &mt('[_1] LON-CAPA support team',$domdesc)."\n".
189: "$contact_email\n";
190: if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
191: print MAIL $requestmail;
192: close(MAIL);
193: $outcome = 'ok';
194: } else {
195: $outcome = 'fail';
196: }
197: return $outcome;
198: }
199:
200: sub invalid_state {
201: my ($error,$domdesc,$contact_name,$contact_email) = @_;
202: my $msg;
203: if ($error eq 'invalid') {
204: $msg = &mt('The username you provided was not verified as a valid username in the LON-CAPA system for the [_1] domain.',$domdesc).&mt(' Please <a href="javascript:history.go(-1)"><u>go back</u></a> and try again.');
205: } else {
1.3 raeburn 206: if ($error eq 'baduseremail') {
207: $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
208: } elsif ($error eq 'mismatch') {
209: $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.');
210: } elsif ($error eq 'missing') {
1.1 raeburn 211: $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
212: } elsif ($error eq 'authentication') {
213: $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
214: }
215: if ($contact_email ne '') {
216: my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
1.3 raeburn 217: $msg .= '<br />'.&mt(' You may wish to contact the <a href="/adm/helpdesk?origurl=[_1]">LON-CAPA helpdesk</a> for the [_2] domain.',$escuri,$domdesc);
1.1 raeburn 218: } else {
1.4 albertel 219: $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 220: }
221: }
222: return $msg;
223: }
224:
225: sub reset_passwd {
226: my ($r,$token,$contact_name,$contact_email) = @_;
227: my $msg;
228: my %data = &Apache::lonnet::tmpget($token);
229: my $now = time;
230: if (keys(%data) == 0) {
231: $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.');
232: return $msg;
233: }
234: if (($data{'time'} =~ /^\d+$/) &&
235: ($data{'username'} ne '') &&
236: ($data{'domain'} ne '') &&
1.3 raeburn 237: ($data{'email'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) &&
1.1 raeburn 238: ($data{'temppasswd'} =~/^\w+$/)) {
239: my $reqtime = localtime($data{'time'});
240: if ($now - $data{'time'} < 7200) {
241: if ($env{'form.action'} eq 'verify_and_change_pass') {
242: my $change_failed =
1.2 albertel 243: &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1 raeburn 244: if (!$change_failed) {
245: my $delete = &Apache::lonnet::tmpdel($token);
246: my $now = localtime(time);
247: my $domdesc =
1.6 albertel 248: &Apache::lonnet::domain($data{'domain'},'description');
1.1 raeburn 249: 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";
250: my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
251: $contact_name,$contact_email);
252: if ($result eq 'ok') {
253: $msg .= &mt('An e-mail confirming setting of the password for your LON-CAPA account has been sent to [_1].',$data{'email'});
254: } else {
255: $msg .= &mt('An error occurred when sending e-mail to [_1] confirming setting of your new password.',$data{'email'});
256: }
1.4 albertel 257: $msg .= '<br /><br />'.&mt('<a href="/adm/login">Go to the login page</a>.');
1.1 raeburn 258: } else {
259: $msg .= &mt('A problem occurred when attempting to reset the password for your account. Please contact the [_1] - (<a href="mailto:[_2]">[_2]</a>) for assistance.',$contact_name,$contact_email);
260: }
261: } else {
262: $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 />');
263: $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 />');
264: &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
265: }
266: } else {
267: $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.');
268: }
269: } else {
270: $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.');
271: }
272: return $msg;
273: }
274:
275: sub create_passwd {
276: my $passwd = '';
1.2 albertel 277: my @letts = ("a".."z");
1.1 raeburn 278: for (my $i=0; $i<8; $i++) {
1.2 albertel 279: my $lettnum = int(rand(2));
1.1 raeburn 280: my $item = '';
281: if ($lettnum) {
1.2 albertel 282: $item = $letts[int(rand(26))];
283: my $uppercase = int(rand(2));
1.1 raeburn 284: if ($uppercase) {
285: $item =~ tr/a-z/A-Z/;
286: }
287: } else {
1.2 albertel 288: $item = int(rand(10));
1.1 raeburn 289: }
290: $passwd .= $item;
291: }
292: return ($passwd);
293: }
294:
295: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>