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