Annotation of loncom/interface/resetpw.pm, revision 1.51
1.1 raeburn 1: # The LearningOnline Network
2: # Allow access to password changing via a token sent to user's e-mail.
3: #
1.51 ! raeburn 4: # $Id: resetpw.pm,v 1.50 2021/11/30 15:55:37 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;
1.44 raeburn 58: use Apache::lonpreferences;
1.1 raeburn 59: use Apache::lonlocal;
60: use LONCAPA;
1.38 raeburn 61: use HTML::Entities;
1.1 raeburn 62:
63: sub handler {
64: my $r = shift;
65: &Apache::loncommon::content_type($r,'text/html');
66: $r->send_http_header;
67: if ($r->header_only) {
68: return OK;
69: }
70: my $contact_name = &mt('LON-CAPA helpdesk');
1.27 raeburn 71: my $origmail = $r->dir_config('lonSupportEMail');
1.1 raeburn 72: my $server = $r->dir_config('lonHostID');
1.19 raeburn 73: my $defdom = &Apache::lonnet::default_login_domain();
1.27 raeburn 74: my $contacts =
75: &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
76: $defdom,$origmail);
77: my ($contact_email) = split(',',$contacts);
1.26 raeburn 78: my $handle = &Apache::lonnet::check_for_valid_session($r);
79: my $lonidsdir=$r->dir_config('lonIDsDir');
80: if ($handle ne '') {
81: if ($handle=~/^publicuser\_/) {
82: unlink($r->dir_config('lonIDsDir')."/$handle.id");
83: } else {
84: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
85: }
86: }
1.1 raeburn 87: &Apache::lonacc::get_posted_cgi($r);
88: &Apache::lonlocal::get_language_handle($r);
89: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
90:
91: my @emailtypes = ('permanentemail','critnotification','notification');
1.38 raeburn 92: my $uname = $env{'form.uname'};
93: $uname =~ s/^\s+|\s+$//g;
94: $uname = &LONCAPA::clean_username($uname);
95: my $udom = &LONCAPA::clean_domain($env{'form.udom'});
1.44 raeburn 96: my ($domdesc,$otherinst,$lookup);
1.38 raeburn 97: if ($udom) {
98: $domdesc = &Apache::lonnet::domain($udom,'description');
99: if ($domdesc) {
1.40 raeburn 100: $otherinst = 1;
101: my @ids=&Apache::lonnet::current_machine_ids();
1.38 raeburn 102: my %servers = &Apache::lonnet::internet_dom_servers($udom);
1.44 raeburn 103: foreach my $hostid (keys(%servers)) {
104: if (grep(/^\Q$hostid\E$/,@ids)) {
1.40 raeburn 105: $otherinst = 0;
106: last;
107: }
1.38 raeburn 108: }
109: }
110: }
1.44 raeburn 111: my $dom_in_effect = $defdom;
112: if (($udom ne '') && ($domdesc ne '')) {
113: unless ($otherinst) {
114: $dom_in_effect = $udom;
115: }
116: }
117: my %passwdconf = &Apache::lonnet::get_passwdconf($dom_in_effect);
1.50 raeburn 118: my $clientip = &Apache::lonnet::get_requestor_ip($r);
1.1 raeburn 119: my $token = $env{'form.token'};
1.44 raeburn 120: my $useremail = $env{'form.useremail'};
121: if (($udom ne '') && (!$otherinst) && (!$token)) {
122: if ($uname ne '') {
123: my $uhome = &Apache::lonnet::homeserver($uname,$udom);
124: if ($uhome eq 'no_host') {
125: my %srch = (srchby => 'uname_ci',
126: srchdomain => $udom,
127: srchterm => $uname,
128: srchtype => 'exact');
129: my %srch_results = &Apache::lonnet::usersearch(\%srch);
130: if (keys(%srch_results) > 1) {
131: $lookup = 'nonunique';
132: if ($useremail =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
133: foreach my $key (keys(%srch_results)) {
134: if (ref($srch_results{$key}) eq 'HASH') {
135: if ($srch_results{$key}{permanentemail} =~ /^\Q$useremail\E$/i) {
136: ($uname) = split(/:/,$key);
137: undef($lookup);
138: last;
139: }
140: }
141: }
142: }
143: } elsif (keys(%srch_results) == 1) {
144: my $match = (keys(%srch_results))[0];
145: ($uname) = split(/:/,$match);
146: } else {
147: $lookup = 'nomatch';
148: }
149: }
150: }
151: if (($lookup eq 'nomatch') || ($uname eq '')) {
152: if (($useremail =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) &&
153: ($passwdconf{'resetprelink'} eq 'either')) {
154: my %srch = (srchby => 'email',
155: srchdomain => $udom,
156: srchterm => $useremail,
157: srchtype => 'exact');
158: my %srch_results = &Apache::lonnet::usersearch(\%srch);
159: if (keys(%srch_results) > 1) {
160: $lookup = 'nonunique';
161: } elsif (keys(%srch_results) == 1) {
162: my $match = (keys(%srch_results))[0];
163: ($uname) = split(/:/,$match);
164: undef($lookup);
165: } else {
166: $lookup = 'nomatch';
167: }
168: }
169: }
170: }
1.26 raeburn 171: my $brcrum = [];
172: if ($token) {
173: push (@{$brcrum},
174: {href => '/adm/resetpw',
175: text => 'Update Password'});
176: } else {
177: push (@{$brcrum},
178: {href => '/adm/resetpw',
179: text => 'Account Information'});
180: if ($uname && $udom) {
181: push (@{$brcrum},
182: {href => '/adm/resetpw',
183: text => 'Result'});
184: }
185: }
1.33 bisitz 186: my $args = {bread_crumbs => $brcrum};
1.38 raeburn 187: my $js;
188: unless ($token || $otherinst || ($uname && $udom)) {
189: my (@intdoms,@instdoms);
190: my $internet_names = &Apache::lonnet::get_internet_names($server);
191: if (ref($internet_names) eq 'ARRAY') {
192: @intdoms = @{$internet_names};
193: }
194: if (@intdoms) {
195: my %iphost = &Apache::lonnet::get_iphost();
196: foreach my $ip (keys(%iphost)) {
197: if (ref($iphost{$ip}) eq 'ARRAY') {
198: foreach my $id (@{$iphost{$ip}}) {
199: my $location = &Apache::lonnet::internet_dom($id);
200: if ($location) {
201: if (grep(/^\Q$location\E$/,@intdoms)) {
202: my $dom = &Apache::lonnet::host_domain($id);
203: unless (grep(/^\Q$dom\E/,@instdoms)) {
204: push(@instdoms,$dom);
205: }
206: }
207: }
208: }
209: }
210: }
211: }
212: my $instdomstr;
213: if (@instdoms > 0) {
214: $instdomstr = "'".join("','",@instdoms)."'";
215: }
216: my %js_lt = &Apache::lonlocal::texthash(
217: thdo => 'The domain you have selected is for another institution.',
218: yowi => 'You will be switched to the Forgot Password utility at that institution.',
219: unam => 'You must enter a username.',
1.44 raeburn 220: mail => 'You must enter an e-mail address.',
221: eith => 'Enter a username and/or an e-mail address.',
1.38 raeburn 222: );
223: &js_escape(\%js_lt);
224: $js = <<"END";
225: function verifyDomain(caller,form) {
226: var redirect = 1;
227: var instdoms = new Array($instdomstr);
228: if (instdoms.length > 0) {
229: for (var i=0; i<instdoms.length; i++) {
230: if (caller.options[caller.selectedIndex].value == instdoms[i]) {
231: redirect = 0;
232: break;
233: }
234: }
235: }
1.44 raeburn 236: if (redirect == 0) {
237: if (caller.options[caller.selectedIndex].value != '$dom_in_effect') {
238: document.forgotpw.uname.value = '';
239: document.forgotpw.useremail.value = '';
240: form.submit();
241: }
242: }
1.38 raeburn 243: if (redirect == 1) {
244: if (confirm('$js_lt{thdo}\\n$js_lt{yowi}')) {
245: form.submit();
1.44 raeburn 246: } else {
247: for (var i=0; i<caller.options.length; i++) {
248: if (caller.option[i].value == '$dom_in_effect') {
249: caller.selectedIndex = i;
250: break;
251: }
252: }
1.38 raeburn 253: }
254: }
255: return;
256: }
257:
1.44 raeburn 258: END
259: if ($passwdconf{resetprelink} eq 'either') {
260: $js .= <<"END";
261: function validInfo() {
262: if ((document.forgotpw.uname.value == '') &&
263: (document.forgotpw.useremail.value == '')) {
264: alert("$js_lt{'eith'}");
265: return false;
266: }
267: return true;
268: }
269: END
270: } else {
271: $js .= <<"END";
272:
1.38 raeburn 273: function validInfo() {
274: if (document.forgotpw.uname.value == '') {
275: alert("$js_lt{'unam'}");
276: return false;
277: }
278: if (document.forgotpw.useremail.value == '') {
279: alert("$js_lt{'mail'}");
280: return false;
281: }
282: return true;
283: }
284: END
1.44 raeburn 285: }
286: }
287: $js = &Apache::lonhtmlcommon::scripttag($js);
288: if (($passwdconf{'captcha'} eq 'recaptcha') && ($passwdconf{'recaptchaversion'} >=2)) {
289: $js.= "\n".'<script src="https://www.google.com/recaptcha/api.js"></script>'."\n";
1.38 raeburn 290: }
291: my $header = &Apache::loncommon::start_page('Reset password',$js,$args).
292: '<h2>'.&mt('Reset forgotten LON-CAPA password').'</h2>';
1.1 raeburn 293: my $output;
294: if ($token) {
1.39 raeburn 295: $r->print($header);
1.50 raeburn 296: &reset_passwd($r,$token,$contact_name,$contact_email,$clientip,\%passwdconf);
1.39 raeburn 297: $r->print(&Apache::loncommon::end_page());
298: return OK;
1.38 raeburn 299: } elsif ($udom) {
300: if (!$domdesc) {
301: $output = &invalid_state('baddomain',$domdesc,
302: $contact_name,$contact_email);
303: } elsif ($otherinst) {
1.49 raeburn 304: ($header,$output) = &homeserver_redirect($r,$uname,$udom,$domdesc,$brcrum);
1.44 raeburn 305: } elsif (($uname) || ($useremail)) {
306: my $earlyout;
307: unless ($passwdconf{'captcha'} eq 'unused') {
308: my ($captcha_chk,$captcha_error) =
1.46 raeburn 309: &Apache::loncommon::captcha_response('passwords',$server,$dom_in_effect);
1.44 raeburn 310: if ($captcha_chk != 1) {
311: my $error = 'captcha';
312: if ($passwdconf{'captcha'} eq 'recaptcha') {
313: $error = 'recaptcha';
314: }
315: $output = &invalid_state($error,$domdesc,
1.37 raeburn 316: $contact_name,$contact_email);
1.44 raeburn 317: $earlyout = 1;
318: }
319: }
320: unless ($earlyout) {
321: if ($lookup) {
322: $output = &invalid_state($lookup,$domdesc,
323: $contact_name,$contact_email);
324: $earlyout = 1;
325: }
326: }
327: unless ($earlyout) {
328: my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
329: if ($authtype =~ /^internal/) {
330: my ($blocked,$blocktext) =
1.51 ! raeburn 331: &Apache::loncommon::blocking_status('passwd',$clientip,$uname,$udom);
1.44 raeburn 332: if ($blocked) {
333: $output = '<p class="LC_warning">'.$blocktext.'</p>'
334: .&display_actions($contact_email,$domdesc);
335: } elsif (($passwdconf{'resetprelink'} ne 'either') &&
336: ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/)) {
337: $output = &invalid_state('baduseremail',$domdesc,
338: $contact_name,$contact_email);
339: } else {
340: my %userinfo =
341: &Apache::lonnet::get('environment',\@emailtypes,
342: $udom,$uname);
343: my @allemails;
344: foreach my $type (@emailtypes) {
345: if (ref($passwdconf{resetemail}) eq 'ARRAY') {
346: if ($type eq 'permanentemail') {
347: next unless (grep(/^permanent$/,@{$passwdconf{resetemail}}));
348: } elsif ($type eq 'critnotification') {
349: next unless (grep(/^critical$/,@{$passwdconf{resetemail}}));
350: } elsif ($type eq 'notification') {
351: next unless (grep(/^notify$/,@{$passwdconf{resetemail}}));
352: }
353: }
354: my $email = $userinfo{$type};
355: my @items;
356: if ($email =~ /,/) {
357: @items = split(',',$userinfo{$type});
358: } else {
359: @items = ($email);
360: }
361: foreach my $item (@items) {
362: if ($item =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
1.46 raeburn 363: unless (grep(/^\Q$item\E$/i,@allemails)) {
1.44 raeburn 364: push(@allemails,$item);
365: }
366: }
367: }
1.38 raeburn 368: }
1.44 raeburn 369: if (@allemails > 0) {
370: my ($sendto,$warning,$timelimit);
371: my $timelimit = 2;
372: if ($passwdconf{'resetlink'} =~ /^\d+(|\.\d*)$/) {
373: $timelimit = $passwdconf{'resetlink'};
374: }
375: if ($passwdconf{'resetprelink'} eq 'either') {
376: if ($useremail ne '') {
377: if (grep(/^\Q$useremail\E$/i,@allemails)) {
378: $sendto = $useremail;
379: } else {
380: $warning = &mt('The e-mail address you entered did not match the expected e-mail address.');
381: }
382: } elsif (@allemails > 1) {
383: $warning = &mt('More than one e-mail address is associated with your username, and one has been selected to receive the message sent by LON-CAPA.');
384: }
385: unless ($sendto) {
386: $sendto = $allemails[0];
387: }
388: } else {
389: if (grep(/^\Q$useremail\E$/i,@allemails)) {
390: $sendto = $useremail;
391: } else {
392: $output = &invalid_state('mismatch',$domdesc,
393: $contact_name,
394: $contact_email);
1.38 raeburn 395: }
1.18 raeburn 396: }
1.44 raeburn 397: if ($sendto) {
398: $output = &send_token($uname,$udom,$sendto,$server,
399: $domdesc,$contact_name,
400: $contact_email,$timelimit,$warning);
401: }
1.38 raeburn 402: } else {
1.44 raeburn 403: $output = &invalid_state('missing',$domdesc,
404: $contact_name,$contact_email);
1.38 raeburn 405: }
1.3 raeburn 406: }
1.44 raeburn 407: } elsif ($authtype =~ /^(krb|unix|local)/) {
408: $output = &invalid_state('authentication',$domdesc,
409: $contact_name,$contact_email);
410: } else {
411: $output = &invalid_state('invalid',$domdesc,
412: $contact_name,$contact_email);
1.1 raeburn 413: }
414: }
415: } else {
1.44 raeburn 416: $output = &get_uname($server,$dom_in_effect,\%passwdconf);
1.1 raeburn 417: }
418: } else {
1.44 raeburn 419: $output = &get_uname($server,$defdom,\%passwdconf);
1.1 raeburn 420: }
1.38 raeburn 421: $r->print($header.$output);
1.1 raeburn 422: $r->print(&Apache::loncommon::end_page());
423: return OK;
424: }
425:
426: sub get_uname {
1.44 raeburn 427: my ($server,$defdom,$passwdconf) = @_;
428: return unless (ref($passwdconf) eq 'HASH');
1.1 raeburn 429: my %lt = &Apache::lonlocal::texthash(
1.44 raeburn 430: unam => 'LON-CAPA username',
431: udom => 'LON-CAPA domain',
432: uemail => 'E-mail address in LON-CAPA',
433: vali => 'Validation',
434: proc => 'Proceed');
435: my $msg;
436: unless ($passwdconf->{'resetremove'}) {
437: $msg = '<p>'.&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.').'</p>';
438: }
439: if ($passwdconf->{'resetcustom'} eq "/res/$defdom/$defdom-domainconfig/customtext/resetpw/resetpw.html") {
440: my $contents = &Apache::lonnet::getfile(&Apache::lonnet::filelocation('',$passwdconf->{'resetcustom'}));
441: unless ($contents eq '-1') {
442: $msg .= $contents;
443: }
444: }
445: $msg .= '<p>'.&mt('Three conditions must be met:')
1.8 bisitz 446: .'<ul><li>'.&mt('An e-mail address must have previously been associated with your LON-CAPA username.').'</li>'
447: .'<li>'.&mt('You must be able to access e-mail sent to that address.').'</li>'
448: .'<li>'.&mt('Your LON-CAPA account must be of a type for which LON-CAPA can reset a password.')
1.44 raeburn 449: .'</ul></p>';
1.38 raeburn 450: my $onchange = 'javascript:verifyDomain(this,this.form);';
451: $msg .= '<form name="forgotpw" method="post" action="/adm/resetpw" onsubmit="return validInfo();">'.
1.44 raeburn 452: &Apache::lonhtmlcommon::start_pick_box().
1.32 bisitz 453: &Apache::lonhtmlcommon::row_title($lt{'udom'}).
1.38 raeburn 454: &Apache::loncommon::select_dom_form($defdom,'udom',undef,undef,$onchange).
1.26 raeburn 455: &Apache::lonhtmlcommon::row_closure(1).
1.44 raeburn 456: &Apache::lonhtmlcommon::row_title($lt{'unam'}).
457: '<input type="text" name="uname" size="20" autocapitalize="off" autocorrect="off" />'.
458: &Apache::lonhtmlcommon::row_closure(1).
1.26 raeburn 459: &Apache::lonhtmlcommon::row_title($lt{'uemail'}).
1.44 raeburn 460: '<input type="text" name="useremail" size="30" autocapitalize="off" autocorrect="off" />'.
461: &Apache::lonhtmlcommon::row_closure(1);
462: unless ($passwdconf->{'captcha'} eq 'notused') {
463: my ($captcha_form,$captcha_error,$captcha,$recaptcha_version) =
464: &Apache::loncommon::captcha_display('passwords',$server,$defdom);
465: if ($captcha_form) {
466: $msg .= &Apache::lonhtmlcommon::row_title($lt{'vali'}).
467: $captcha_form."\n".
468: &Apache::lonhtmlcommon::row_closure(1);
469: }
470: }
471: $msg .= &Apache::lonhtmlcommon::end_pick_box().
1.26 raeburn 472: '<br /><br /><input type="submit" name="resetter" value="'.$lt{'proc'}.'" /></form>';
1.1 raeburn 473: return $msg;
474: }
475:
476: sub send_token {
477: my ($uname,$udom,$email,$server,$domdesc,$contact_name,
1.44 raeburn 478: $contact_email,$timelimit,$warning) = @_;
1.29 bisitz 479: my $msg =
480: '<p class="LC_info">'
481: .&mt('Thank you for your request to reset the password for your LON-CAPA account.')
482: .'</p>';
1.1 raeburn 483:
484: my $now = time;
485: my $temppasswd = &create_passwd();
1.48 raeburn 486: my $ip = &Apache::lonnet::get_requestor_ip();
487: my %info = ('ip' => $ip,
1.2 albertel 488: 'time' => $now,
489: 'domain' => $udom,
490: 'username' => $uname,
491: 'email' => $email,
492: 'temppasswd' => $temppasswd);
1.1 raeburn 493:
1.3 raeburn 494: my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
495: if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2 albertel 496: my $esc_token = &escape($token);
1.15 raeburn 497: my $showtime = &Apache::lonlocal::locallocaltime(time);
498: my $reseturl = &Apache::lonnet::absolute_url().'/adm/resetpw?token='.$esc_token;
1.26 raeburn 499: 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 500: my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
501: $contact_email);
502: if ($result eq 'ok') {
1.34 bisitz 503: $msg .=
504: &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.')
505: .'<br /><br />'
1.44 raeburn 506: .&mt('The link included in the message will be valid for the next [_1][quant,_2,hour][_3].','<b>',$timelimit,'</b>');
1.1 raeburn 507: } else {
1.28 bisitz 508: $msg .=
1.29 bisitz 509: '<p class="LC_error">'
510: .&mt('An error occurred when sending a message to the e-mail address'
511: .' associated with your LON-CAPA account.')
512: .'</p>'
513: .&display_actions($contact_email,$domdesc);
1.1 raeburn 514: }
515: } else {
1.28 bisitz 516: $msg .=
1.29 bisitz 517: '<p class="LC_error">'
518: .&mt('An error occurred creating a token required for the'
519: .' password reset process.')
520: .'</p>'
521: .&display_actions($contact_email,$domdesc);
1.1 raeburn 522: }
523: return $msg;
524: }
525:
526: sub send_mail {
527: my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
528: my $outcome;
529: my $requestmail = "To: $email\n".
530: "From: $contact_name <$contact_email>\n".
531: "Subject: ".&mt('Your LON-CAPA account')."\n".
1.25 bisitz 532: "Content-type: text/plain\;charset=UTF-8\n".
1.1 raeburn 533: "\n\n".$mailmsg."\n\n".
534: &mt('[_1] LON-CAPA support team',$domdesc)."\n".
535: "$contact_email\n";
536: if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
537: print MAIL $requestmail;
538: close(MAIL);
539: $outcome = 'ok';
540: } else {
541: $outcome = 'fail';
542: }
543: return $outcome;
544: }
545:
546: sub invalid_state {
547: my ($error,$domdesc,$contact_name,$contact_email) = @_;
548: my $msg;
549: if ($error eq 'invalid') {
1.29 bisitz 550: $msg =
551: '<p class="LC_warning">'
552: .&mt('The username you provided was not verified as a valid username'
553: .' in the LON-CAPA system for the [_1] domain.','<i>'.$domdesc.'</i>')
554: .'</p>';
555: $msg .= &display_actions($contact_email,$domdesc);
1.1 raeburn 556: } else {
1.44 raeburn 557: if ($error eq 'captcha') {
558: $msg = &mt('Validation of the code you entered failed');
559: } elsif ($error eq 'recaptcha') {
560: $msg = &mt('Validation of human, not robot, failed');
561: } elsif ($error eq 'nonunique') {
562: $msg = &mt('More than one username was identified from the information you provided; try providing both a username and e-mail address');
563: } elsif ($error eq 'nomatch') {
564: $msg = &mt('A valid user could not be identified from the username and/or e-mail address you provided');
565: } elsif ($error eq 'baduseremail') {
1.3 raeburn 566: $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
567: } elsif ($error eq 'mismatch') {
568: $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.');
569: } elsif ($error eq 'missing') {
1.1 raeburn 570: $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
571: } elsif ($error eq 'authentication') {
572: $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
1.38 raeburn 573: } elsif ($error eq 'baddomain') {
574: $msg = &mt('The domain you provided was not verified as a valid domain in the LON-CAPA system.');
1.1 raeburn 575: }
1.29 bisitz 576: $msg = '<p class="LC_warning">'.$msg.'</p>'
577: .&display_actions($contact_email,$domdesc);
1.1 raeburn 578: }
579: return $msg;
580: }
581:
1.38 raeburn 582: sub homeserver_redirect {
1.49 raeburn 583: my ($r,$uname,$udom,$domdesc,$brcrum) = @_;
1.42 raeburn 584: my $uhome;
1.43 raeburn 585: if (($uname ne '') && ($udom ne '')) {
1.42 raeburn 586: $uhome = &Apache::lonnet::homeserver($uname,$udom);
587: }
588: if (($uhome eq 'no_host') || ($uhome eq '')) {
1.38 raeburn 589: $uhome = &Apache::lonnet::domain($udom,'primary');
590: }
1.42 raeburn 591: my $hostname = &Apache::lonnet::hostname($uhome);
1.38 raeburn 592: my $protocol = $Apache::lonnet::protocol{$uhome};
593: $protocol = 'http' if ($protocol ne 'https');
1.49 raeburn 594: my $alias = &Apache::lonnet::use_proxy_alias($r,$uhome);
595: $hostname = $alias if ($alias ne '');
1.42 raeburn 596: my $url = $protocol.'://'.$hostname.'/adm/resetpw';
1.38 raeburn 597: # Breadcrumbs
598: my $start_page = &Apache::loncommon::start_page('Switching Server',undef,
599: {'redirect' => [0,$url],
600: 'bread_crumbs' => $brcrum,});
601: my $output = '<p>'.&mt('This LON-CAPA server belongs to a different domain.').' '.
602: &mt('You are being switched to your domain ([_1]), to use the "Forgot Password" tool.',$domdesc).
603: '</p>';
604: return ($start_page,$output);
605: }
606:
1.1 raeburn 607: sub reset_passwd {
1.50 raeburn 608: my ($r,$token,$contact_name,$contact_email,$clientip,$passwdconf) = @_;
1.44 raeburn 609: return unless (ref($passwdconf) eq 'HASH');
1.1 raeburn 610: my %data = &Apache::lonnet::tmpget($token);
611: my $now = time;
612: if (keys(%data) == 0) {
1.39 raeburn 613: $r->print(&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.'
614: ,'<a href="/adm/resetpw">','</a>')
615: );
616: return;
1.1 raeburn 617: }
618: if (($data{'time'} =~ /^\d+$/) &&
619: ($data{'username'} ne '') &&
620: ($data{'domain'} ne '') &&
1.3 raeburn 621: ($data{'email'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) &&
1.1 raeburn 622: ($data{'temppasswd'} =~/^\w+$/)) {
1.44 raeburn 623: my $timelimit = 7200;
624: if ($passwdconf->{resetlink} =~ /^\d+(|\.\d*)$/) {
625: $timelimit = 3600 * $passwdconf->{resetlink};
626: }
1.9 raeburn 627: my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.37 raeburn 628: my ($blocked,$blocktext) =
1.50 raeburn 629: &Apache::loncommon::blocking_status('passwd',$clientip,$data{'username'},$data{'domain'});
1.37 raeburn 630: if ($blocked) {
1.39 raeburn 631: $r->print('<p class="LC_warning">'.$blocktext.'</p>');
632: return;
1.44 raeburn 633: } elsif ($now - $data{'time'} < $timelimit) {
634: my ($needscase,%formfields) = &reset_requires($data{'username'},$data{'domain'},
635: $passwdconf);
1.1 raeburn 636: if ($env{'form.action'} eq 'verify_and_change_pass') {
1.44 raeburn 637: my $invalidinfo;
638: if ($formfields{'username'}) {
639: $env{'form.uname'} =~ s/^\s+|\s+$//g;
640: $env{'form.udom'} =~ s/^\s+|\s+$//g;
641: if ($needscase) {
642: unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'})) {
643: $invalidinfo = "||$env{'form.uname'}|| ||$env{'form.udom'}|| ";
644: }
645: } else {
1.47 raeburn 646: if ((lc($env{'form.uname'}) eq lc($data{'username'})) && (lc($env{'form.udom'}) eq lc($data{'domain'}))) {
647: $env{'form.uname'} = $data{'username'};
648: } else {
1.44 raeburn 649: $invalidinfo = "||$env{'form.uname'}|| ||$env{'form.udom'}|| ";
650: }
651: }
652: } else {
653: $env{'form.uname'} = $data{'username'};
654: $env{'form.udom'} = $data{'domain'};
655: }
656: if ($formfields{'email'}) {
657: $env{'form.email'} =~ s/^\s+|\s+$//g;
658: if ($needscase) {
659: unless ($env{'form.email'} eq $data{'email'}) {
660: $invalidinfo .= "||$env{'form.email'}||";
661: }
662: } else {
663: unless (lc($env{'form.email'}) eq lc($data{'email'})) {
664: $invalidinfo = "||$env{'form.email'}||";
665: }
666: }
667: }
668: if ($invalidinfo) {
669: &Apache::lonnet::logthis("Forgot Password -- token data: ||$data{'username'}|| ||$data{'domain'}|| ||$data{'email'}|| differs from form: $invalidinfo");
1.47 raeburn 670: my $retry;
671: $r->print(
672: '<p class="LC_warning">'
673: .&mt('A problem occurred when attempting to reset'
674: .' the password for your account.').'</p>');
675: if (($formfields{'username'}) && ($formfields{'email'})) {
676: if ($needscase) {
677: $r->print('<p>'
678: .&mt('Please verify you entered the correct username and e-mail address, '
679: .'including the correct lower and/or upper case letters')
680: .'</p>');
681: } else {
682: $r->print('<p>'
683: .&mt('Please verify you entered the correct username and e-mail address.')
684: .'</p>');
685: }
686: $retry = 1;
687: } elsif ($formfields{'username'}) {
688: if ($needscase) {
689: $r->print('<p>'
690: .&mt('Please verify you entered the correct username, '
691: .'including the correct lower and/or upper case letters')
692: .'</p>');
693: } else {
694: $r->print('<p>'
695: .&mt('Please verify you entered the correct username.')
696: .'</p>');
697: }
698: $retry = 1;
699: } elsif ($formfields{'email'}) {
700: if ($needscase) {
701: $r->print('<p>'
702: .&mt('Please verify you entered the correct e-mail address, '
703: .'including the correct lower and/or upper case letters')
704: .'</p>');
705: } else {
706: $r->print('<p>'
707: .&mt('Please verify you entered the correct e-mail address.')
708: .'</p>');
709: }
710: $retry = 1;
711: }
712: if ($retry) {
713: &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token,$timelimit,\%formfields);
714: } else {
715: $r->print(&generic_failure_msg($contact_name,$contact_email));
716: }
1.44 raeburn 717: unless ($formfields{'username'}) {
718: delete($env{'form.uname'});
719: delete($env{'form.udom'});
720: }
1.39 raeburn 721: return;
1.22 raeburn 722: }
1.44 raeburn 723: my $change_failed =
1.47 raeburn 724: &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token,$timelimit,\%formfields);
1.1 raeburn 725: if (!$change_failed) {
726: my $delete = &Apache::lonnet::tmpdel($token);
1.9 raeburn 727: my $now = &Apache::lonlocal::locallocaltime(time);
1.1 raeburn 728: my $domdesc =
1.6 albertel 729: &Apache::lonnet::domain($data{'domain'},'description');
1.48 raeburn 730: my $ip = &Apache::lonnet::get_requestor_ip();
731: 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,$ip,$contact_name,$contact_email)."\n";
1.1 raeburn 732: my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
733: $contact_name,$contact_email);
1.33 bisitz 734: my $confirm_msg;
1.1 raeburn 735: if ($result eq 'ok') {
1.33 bisitz 736: $confirm_msg =
737: &Apache::lonhtmlcommon::confirm_success(
738: &mt('An e-mail confirming setting of the password'
739: .' for your LON-CAPA account has been sent to [_1].'
740: ,'<span class="LC_filename">'.$data{'email'}.'</span>'));
1.1 raeburn 741: } else {
1.33 bisitz 742: $confirm_msg =
743: &Apache::lonhtmlcommon::confirm_success(
744: &mt('An error occurred when sending e-mail to [_1]'
745: .' confirming setting of your new password.'
746: ,'<span class="LC_filename">'.$data{'email'}.'</span>'),1);
1.1 raeburn 747: }
1.39 raeburn 748: $r->print(
1.33 bisitz 749: &Apache::loncommon::confirmwrapper($confirm_msg)
750: .&Apache::lonhtmlcommon::actionbox([
1.39 raeburn 751: '<a href="/adm/login">'.&mt('Go to the login page').'</a>'])
752: );
1.20 raeburn 753: } elsif ($change_failed eq 'invalid_client') {
754: my $homeserver = &Apache::lonnet::homeserver($data{'username'},$data{'domain'});
755: if ($homeserver eq 'no_host') {
1.39 raeburn 756: $r->print(&generic_failure_msg($contact_name,$contact_email));
1.20 raeburn 757: } else {
1.42 raeburn 758: my $hostname = &Apache::lonnet::hostname($homeserver);
1.20 raeburn 759: my $protocol = $Apache::lonnet::protocol{$homeserver};
760: $protocol = 'http' if ($protocol ne 'https');
1.49 raeburn 761: my $alias = &Apache::lonnet::use_proxy_alias($r,$homeserver);
762: $hostname = $alias if ($alias ne '');
1.42 raeburn 763: my $url = $protocol.'://'.$hostname.'/adm/resetpw';
1.20 raeburn 764: my ($opentag,$closetag);
765: if ($url) {
766: $opentag = '<a href="'.$url.'">';
767: $closetag = '</a>';
768: }
1.39 raeburn 769: $r->print(
1.28 bisitz 770: '<p class="LC_warning">'
771: .&mt('A problem occurred when attempting to reset'
772: .' the password for your account.'
773: .' Please try again from your [_1]home server[_2].'
774: ,$opentag,$closetag)
1.39 raeburn 775: .'</p>'
776: );
1.20 raeburn 777: }
1.45 raeburn 778: } elsif (($change_failed eq 'prioruse') && ($passwdconf->{'numsaved'})) {
779: my $domdesc =
780: &Apache::lonnet::domain($data{'domain'},'description');
781: $r->print(
782: '<p class="LC_warning">'
783: .&mt('Please enter a password that you have not used recently.')
784: .'</p>'
1.46 raeburn 785: .&display_actions($contact_email,$domdesc,$token)
1.45 raeburn 786: );
1.47 raeburn 787: } elsif (($change_failed eq 'internalerror') || ($change_failed eq 'missingtemp') ||
788: ($change_failed eq 'error')) {
1.39 raeburn 789: $r->print(&generic_failure_msg($contact_name,$contact_email));
1.1 raeburn 790: }
1.44 raeburn 791: unless ($formfields{'username'}) {
792: delete($env{'form.uname'});
793: delete($env{'form.udom'});
794: }
1.1 raeburn 795: } else {
1.13 schafran 796: $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.44 raeburn 797: if (keys(%formfields)) {
798: if (($formfields{'username'}) && ($formfields{'email'})) {
799: $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.'));
800: } elsif ($formfields{'username'}) {
801: $r->print(&mt('Please enter the username and domain of the LON-CAPA account for which you are setting a password.'));
802: } elsif ($formfields{'email'}) {
803: $r->print(&mt('Please enter the e-mail address associated with the LON-CAPA account for which you are setting a password.'));
804: }
805: if ($needscase) {
806: $r->print(' '.&mt('User data entered must match LON-CAPA account information (including case).'));
807: }
1.47 raeburn 808: $r->print('<br />');
809: }
810: my ($min,$max,$minrule,$maxrule);
811: if ($passwdconf->{min}) {
812: $min = $passwdconf->{min};
813: } else {
814: $min = $Apache::lonnet::passwdmin;
815: }
816: if ($min) {
817: $minrule = &mt('Minimum password length: [_1]',$min);
818: }
819: if ($passwdconf->{max}) {
820: $max = $passwdconf->{max};
821: $maxrule = &mt('Maximum password length: [_1]',$max);
1.44 raeburn 822: }
823: if (ref($passwdconf->{chars}) eq 'ARRAY') {
824: my %rules;
825: map { $rules{$_} = 1; } @{$passwdconf->{chars}};
826: my %rulenames = &Apache::lonlocal::texthash(
827: uc => 'At least one upper case letter',
828: lc => 'At least one lower case letter',
829: num => 'At least one number',
830: spec => 'At least one non-alphanumeric',
831: );
832: $r->print(&mt('The new password must satisfy the following:').'<ul>');
833: foreach my $poss ('uc','lc','num','spec') {
834: if ($rules{$poss}) {
835: $r->print('<li>'.$rulenames{$poss}.'</li>');
836: }
837: }
1.47 raeburn 838: if ($min) {
839: $r->print('<li>'.$minrule.'</li>');
840: }
841: if ($max) {
842: $r->print('<li>'.$maxrule.'</li>');
843: }
1.44 raeburn 844: $r->print('</ul>');
845: } else {
1.47 raeburn 846: if ($min && $max) {
847: $r->print(&mt('The new password must satisfy the following:').'<ul>'."\n".
848: '<li>'.$minrule.'</li>'."\n".
849: '<li>'.$maxrule.'</li>'."\n".
850: '</ul>'."\n");
851: } elsif ($min) {
852: $r->print($minrule.'<br />');
853: } elsif ($max) {
854: $r->print($maxrule.'<br />');
855: }
1.44 raeburn 856: }
857: $r->print(&mt('Your new password will be sent to the LON-CAPA server in an encrypted form.').'<br />');
858: &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token,$timelimit,\%formfields);
1.1 raeburn 859: }
860: } else {
1.39 raeburn 861: $r->print(
1.28 bisitz 862: '<p class="LC_warning">'
1.47 raeburn 863: .&mt('Sorry, the token generated when you requested a password reset has expired.').'<br />'
864: .&mt('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.'
1.28 bisitz 865: ,'<a href="/adm/resetpw">','</a>')
1.39 raeburn 866: .'</p>'
867: );
1.1 raeburn 868: }
869: } else {
1.39 raeburn 870: $r->print(
1.28 bisitz 871: '<p class="LC_warning">'
872: .&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.'
873: ,'<a href="/adm/resetpw">','</a>')
1.39 raeburn 874: .'</p>'
875: );
1.1 raeburn 876: }
1.39 raeburn 877: return;
1.1 raeburn 878: }
879:
1.20 raeburn 880: sub generic_failure_msg {
881: my ($contact_name,$contact_email) = @_;
1.28 bisitz 882: return
1.29 bisitz 883: '<p class="LC_error">'
884: .&mt('A problem occurred when attempting to reset the password for your account.')
885: .'<br />'
1.36 raeburn 886: .&mt('Please contact the [_1] ([_2]) for assistance.',
1.28 bisitz 887: $contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>')
888: .'</p>';
1.20 raeburn 889: }
890:
1.1 raeburn 891: sub create_passwd {
892: my $passwd = '';
1.2 albertel 893: my @letts = ("a".."z");
1.1 raeburn 894: for (my $i=0; $i<8; $i++) {
1.2 albertel 895: my $lettnum = int(rand(2));
1.1 raeburn 896: my $item = '';
897: if ($lettnum) {
1.2 albertel 898: $item = $letts[int(rand(26))];
899: my $uppercase = int(rand(2));
1.1 raeburn 900: if ($uppercase) {
901: $item =~ tr/a-z/A-Z/;
902: }
903: } else {
1.2 albertel 904: $item = int(rand(10));
1.1 raeburn 905: }
906: $passwd .= $item;
907: }
908: return ($passwd);
909: }
910:
1.29 bisitz 911: sub display_actions {
1.46 raeburn 912: my ($contact_email,$domdesc,$token) = @_;
913: my $url = '/adm/resetpw';
914: if ($token) {
915: $url .= '?token='.&escape($token);
916: }
1.29 bisitz 917: my @msg = (&mt('[_1]Go back[_2] and try again',
1.46 raeburn 918: '<a href="'.$url.'">','</a>'));
1.29 bisitz 919: my $msg2 = '';
920: if ($contact_email ne '') {
1.30 raeburn 921: my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
922: push(@msg, &mt('Contact the [_1]LON-CAPA helpdesk[_2] for the institution: [_3]',
923: '<a href="/adm/helpdesk?origurl='.$escuri.'">',
924: '</a>','<i>'.$domdesc.'</i>'));
925: } else {
926: $msg2 =
927: '<p>'
928: .&mt('You may wish to send an e-mail to the'
929: .' server administrator: [_1] for the [_2] domain.',
1.31 raeburn 930: '<i>'.$Apache::lonnet::perlvar{'AdmEMail'}.'</i>',
1.30 raeburn 931: '<i>'.$domdesc.'</i>')
932: .'</p>';
933: }
1.29 bisitz 934: return &Apache::lonhtmlcommon::actionbox(\@msg).$msg2;
1.44 raeburn 935: }
1.29 bisitz 936:
1.44 raeburn 937: sub reset_requires {
938: my ($username,$domain,$passwdconf) = @_;
939: my (%fields,$needscase);
940: return ($needscase,%fields) unless (ref($passwdconf) eq 'HASH');
941: my (%postlink,%resetcase);
942: if (ref($passwdconf->{resetpostlink}) eq 'HASH') {
943: %postlink = %{$passwdconf->{resetpostlink}};
944: }
945: if (ref($passwdconf->{resetcase}) eq 'ARRAY') {
946: map { $resetcase{$_} = 1; } (@{$passwdconf->{resetcase}});
947: } else {
948: $needscase = 1;
949: }
950: my %userenv =
951: &Apache::lonnet::get('environment',['inststatus'],$domain,$username);
952: my @inststatuses;
953: if ($userenv{'inststatus'} ne '') {
954: @inststatuses = split(/:/,$userenv{'inststatus'});
955: } else {
956: @inststatuses = ('default');
957: }
958: foreach my $status (@inststatuses) {
959: if (ref($postlink{$status}) eq 'ARRAY') {
960: map { $fields{$_} = 1; } (@{$postlink{$status}});
961: } else {
962: map { $fields{$_} = 1; } ('username','email');
963: }
964: if ($resetcase{$status}) {
965: $needscase = 1;
966: }
967: }
968: return ($needscase,%fields);
1.29 bisitz 969: }
970:
1.1 raeburn 971: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>