Annotation of loncom/interface/resetpw.pm, revision 1.47
1.1 raeburn 1: # The LearningOnline Network
2: # Allow access to password changing via a token sent to user's e-mail.
3: #
1.47 ! raeburn 4: # $Id: resetpw.pm,v 1.46 2019/08/30 00:09:39 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) {
303: ($header,$output) = &homeserver_redirect($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.2 albertel 485: my %info = ('ip' => $ENV{'REMOTE_ADDR'},
486: 'time' => $now,
487: 'domain' => $udom,
488: 'username' => $uname,
489: 'email' => $email,
490: 'temppasswd' => $temppasswd);
1.1 raeburn 491:
1.3 raeburn 492: my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
493: if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2 albertel 494: my $esc_token = &escape($token);
1.15 raeburn 495: my $showtime = &Apache::lonlocal::locallocaltime(time);
496: my $reseturl = &Apache::lonnet::absolute_url().'/adm/resetpw?token='.$esc_token;
1.26 raeburn 497: 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 498: my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
499: $contact_email);
500: if ($result eq 'ok') {
1.34 bisitz 501: $msg .=
502: &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.')
503: .'<br /><br />'
1.44 raeburn 504: .&mt('The link included in the message will be valid for the next [_1][quant,_2,hour][_3].','<b>',$timelimit,'</b>');
1.1 raeburn 505: } else {
1.28 bisitz 506: $msg .=
1.29 bisitz 507: '<p class="LC_error">'
508: .&mt('An error occurred when sending a message to the e-mail address'
509: .' associated with your LON-CAPA account.')
510: .'</p>'
511: .&display_actions($contact_email,$domdesc);
1.1 raeburn 512: }
513: } else {
1.28 bisitz 514: $msg .=
1.29 bisitz 515: '<p class="LC_error">'
516: .&mt('An error occurred creating a token required for the'
517: .' password reset process.')
518: .'</p>'
519: .&display_actions($contact_email,$domdesc);
1.1 raeburn 520: }
521: return $msg;
522: }
523:
524: sub send_mail {
525: my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
526: my $outcome;
527: my $requestmail = "To: $email\n".
528: "From: $contact_name <$contact_email>\n".
529: "Subject: ".&mt('Your LON-CAPA account')."\n".
1.25 bisitz 530: "Content-type: text/plain\;charset=UTF-8\n".
1.1 raeburn 531: "\n\n".$mailmsg."\n\n".
532: &mt('[_1] LON-CAPA support team',$domdesc)."\n".
533: "$contact_email\n";
534: if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
535: print MAIL $requestmail;
536: close(MAIL);
537: $outcome = 'ok';
538: } else {
539: $outcome = 'fail';
540: }
541: return $outcome;
542: }
543:
544: sub invalid_state {
545: my ($error,$domdesc,$contact_name,$contact_email) = @_;
546: my $msg;
547: if ($error eq 'invalid') {
1.29 bisitz 548: $msg =
549: '<p class="LC_warning">'
550: .&mt('The username you provided was not verified as a valid username'
551: .' in the LON-CAPA system for the [_1] domain.','<i>'.$domdesc.'</i>')
552: .'</p>';
553: $msg .= &display_actions($contact_email,$domdesc);
1.1 raeburn 554: } else {
1.44 raeburn 555: if ($error eq 'captcha') {
556: $msg = &mt('Validation of the code you entered failed');
557: } elsif ($error eq 'recaptcha') {
558: $msg = &mt('Validation of human, not robot, failed');
559: } elsif ($error eq 'nonunique') {
560: $msg = &mt('More than one username was identified from the information you provided; try providing both a username and e-mail address');
561: } elsif ($error eq 'nomatch') {
562: $msg = &mt('A valid user could not be identified from the username and/or e-mail address you provided');
563: } elsif ($error eq 'baduseremail') {
1.3 raeburn 564: $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
565: } elsif ($error eq 'mismatch') {
566: $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.');
567: } elsif ($error eq 'missing') {
1.1 raeburn 568: $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
569: } elsif ($error eq 'authentication') {
570: $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
1.38 raeburn 571: } elsif ($error eq 'baddomain') {
572: $msg = &mt('The domain you provided was not verified as a valid domain in the LON-CAPA system.');
1.1 raeburn 573: }
1.29 bisitz 574: $msg = '<p class="LC_warning">'.$msg.'</p>'
575: .&display_actions($contact_email,$domdesc);
1.1 raeburn 576: }
577: return $msg;
578: }
579:
1.38 raeburn 580: sub homeserver_redirect {
581: my ($uname,$udom,$domdesc,$brcrum) = @_;
1.42 raeburn 582: my $uhome;
1.43 raeburn 583: if (($uname ne '') && ($udom ne '')) {
1.42 raeburn 584: $uhome = &Apache::lonnet::homeserver($uname,$udom);
585: }
586: if (($uhome eq 'no_host') || ($uhome eq '')) {
1.38 raeburn 587: $uhome = &Apache::lonnet::domain($udom,'primary');
588: }
1.42 raeburn 589: my $hostname = &Apache::lonnet::hostname($uhome);
1.38 raeburn 590: my $protocol = $Apache::lonnet::protocol{$uhome};
591: $protocol = 'http' if ($protocol ne 'https');
1.42 raeburn 592: my $url = $protocol.'://'.$hostname.'/adm/resetpw';
1.38 raeburn 593: # Breadcrumbs
594: my $start_page = &Apache::loncommon::start_page('Switching Server',undef,
595: {'redirect' => [0,$url],
596: 'bread_crumbs' => $brcrum,});
597: my $output = '<p>'.&mt('This LON-CAPA server belongs to a different domain.').' '.
598: &mt('You are being switched to your domain ([_1]), to use the "Forgot Password" tool.',$domdesc).
599: '</p>';
600: return ($start_page,$output);
601: }
602:
1.1 raeburn 603: sub reset_passwd {
1.44 raeburn 604: my ($r,$token,$contact_name,$contact_email,$passwdconf) = @_;
605: return unless (ref($passwdconf) eq 'HASH');
1.1 raeburn 606: my %data = &Apache::lonnet::tmpget($token);
607: my $now = time;
608: if (keys(%data) == 0) {
1.39 raeburn 609: $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.'
610: ,'<a href="/adm/resetpw">','</a>')
611: );
612: return;
1.1 raeburn 613: }
614: if (($data{'time'} =~ /^\d+$/) &&
615: ($data{'username'} ne '') &&
616: ($data{'domain'} ne '') &&
1.3 raeburn 617: ($data{'email'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) &&
1.1 raeburn 618: ($data{'temppasswd'} =~/^\w+$/)) {
1.44 raeburn 619: my $timelimit = 7200;
620: if ($passwdconf->{resetlink} =~ /^\d+(|\.\d*)$/) {
621: $timelimit = 3600 * $passwdconf->{resetlink};
622: }
1.9 raeburn 623: my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.37 raeburn 624: my ($blocked,$blocktext) =
625: &Apache::loncommon::blocking_status('passwd',$data{'username'},$data{'domain'});
626: if ($blocked) {
1.39 raeburn 627: $r->print('<p class="LC_warning">'.$blocktext.'</p>');
628: return;
1.44 raeburn 629: } elsif ($now - $data{'time'} < $timelimit) {
630: my ($needscase,%formfields) = &reset_requires($data{'username'},$data{'domain'},
631: $passwdconf);
1.1 raeburn 632: if ($env{'form.action'} eq 'verify_and_change_pass') {
1.44 raeburn 633: my $invalidinfo;
634: if ($formfields{'username'}) {
635: $env{'form.uname'} =~ s/^\s+|\s+$//g;
636: $env{'form.udom'} =~ s/^\s+|\s+$//g;
637: if ($needscase) {
638: unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'})) {
639: $invalidinfo = "||$env{'form.uname'}|| ||$env{'form.udom'}|| ";
640: }
641: } else {
1.47 ! raeburn 642: if ((lc($env{'form.uname'}) eq lc($data{'username'})) && (lc($env{'form.udom'}) eq lc($data{'domain'}))) {
! 643: $env{'form.uname'} = $data{'username'};
! 644: } else {
1.44 raeburn 645: $invalidinfo = "||$env{'form.uname'}|| ||$env{'form.udom'}|| ";
646: }
647: }
648: } else {
649: $env{'form.uname'} = $data{'username'};
650: $env{'form.udom'} = $data{'domain'};
651: }
652: if ($formfields{'email'}) {
653: $env{'form.email'} =~ s/^\s+|\s+$//g;
654: if ($needscase) {
655: unless ($env{'form.email'} eq $data{'email'}) {
656: $invalidinfo .= "||$env{'form.email'}||";
657: }
658: } else {
659: unless (lc($env{'form.email'}) eq lc($data{'email'})) {
660: $invalidinfo = "||$env{'form.email'}||";
661: }
662: }
663: }
664: if ($invalidinfo) {
665: &Apache::lonnet::logthis("Forgot Password -- token data: ||$data{'username'}|| ||$data{'domain'}|| ||$data{'email'}|| differs from form: $invalidinfo");
1.47 ! raeburn 666: my $retry;
! 667: $r->print(
! 668: '<p class="LC_warning">'
! 669: .&mt('A problem occurred when attempting to reset'
! 670: .' the password for your account.').'</p>');
! 671: if (($formfields{'username'}) && ($formfields{'email'})) {
! 672: if ($needscase) {
! 673: $r->print('<p>'
! 674: .&mt('Please verify you entered the correct username and e-mail address, '
! 675: .'including the correct lower and/or upper case letters')
! 676: .'</p>');
! 677: } else {
! 678: $r->print('<p>'
! 679: .&mt('Please verify you entered the correct username and e-mail address.')
! 680: .'</p>');
! 681: }
! 682: $retry = 1;
! 683: } elsif ($formfields{'username'}) {
! 684: if ($needscase) {
! 685: $r->print('<p>'
! 686: .&mt('Please verify you entered the correct username, '
! 687: .'including the correct lower and/or upper case letters')
! 688: .'</p>');
! 689: } else {
! 690: $r->print('<p>'
! 691: .&mt('Please verify you entered the correct username.')
! 692: .'</p>');
! 693: }
! 694: $retry = 1;
! 695: } elsif ($formfields{'email'}) {
! 696: if ($needscase) {
! 697: $r->print('<p>'
! 698: .&mt('Please verify you entered the correct e-mail address, '
! 699: .'including the correct lower and/or upper case letters')
! 700: .'</p>');
! 701: } else {
! 702: $r->print('<p>'
! 703: .&mt('Please verify you entered the correct e-mail address.')
! 704: .'</p>');
! 705: }
! 706: $retry = 1;
! 707: }
! 708: if ($retry) {
! 709: &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token,$timelimit,\%formfields);
! 710: } else {
! 711: $r->print(&generic_failure_msg($contact_name,$contact_email));
! 712: }
1.44 raeburn 713: unless ($formfields{'username'}) {
714: delete($env{'form.uname'});
715: delete($env{'form.udom'});
716: }
1.39 raeburn 717: return;
1.22 raeburn 718: }
1.44 raeburn 719: my $change_failed =
1.47 ! raeburn 720: &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token,$timelimit,\%formfields);
1.1 raeburn 721: if (!$change_failed) {
722: my $delete = &Apache::lonnet::tmpdel($token);
1.9 raeburn 723: my $now = &Apache::lonlocal::locallocaltime(time);
1.1 raeburn 724: my $domdesc =
1.6 albertel 725: &Apache::lonnet::domain($data{'domain'},'description');
1.1 raeburn 726: 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";
727: my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
728: $contact_name,$contact_email);
1.33 bisitz 729: my $confirm_msg;
1.1 raeburn 730: if ($result eq 'ok') {
1.33 bisitz 731: $confirm_msg =
732: &Apache::lonhtmlcommon::confirm_success(
733: &mt('An e-mail confirming setting of the password'
734: .' for your LON-CAPA account has been sent to [_1].'
735: ,'<span class="LC_filename">'.$data{'email'}.'</span>'));
1.1 raeburn 736: } else {
1.33 bisitz 737: $confirm_msg =
738: &Apache::lonhtmlcommon::confirm_success(
739: &mt('An error occurred when sending e-mail to [_1]'
740: .' confirming setting of your new password.'
741: ,'<span class="LC_filename">'.$data{'email'}.'</span>'),1);
1.1 raeburn 742: }
1.39 raeburn 743: $r->print(
1.33 bisitz 744: &Apache::loncommon::confirmwrapper($confirm_msg)
745: .&Apache::lonhtmlcommon::actionbox([
1.39 raeburn 746: '<a href="/adm/login">'.&mt('Go to the login page').'</a>'])
747: );
1.20 raeburn 748: } elsif ($change_failed eq 'invalid_client') {
749: my $homeserver = &Apache::lonnet::homeserver($data{'username'},$data{'domain'});
750: if ($homeserver eq 'no_host') {
1.39 raeburn 751: $r->print(&generic_failure_msg($contact_name,$contact_email));
1.20 raeburn 752: } else {
1.42 raeburn 753: my $hostname = &Apache::lonnet::hostname($homeserver);
1.20 raeburn 754: my $protocol = $Apache::lonnet::protocol{$homeserver};
755: $protocol = 'http' if ($protocol ne 'https');
1.42 raeburn 756: my $url = $protocol.'://'.$hostname.'/adm/resetpw';
1.20 raeburn 757: my ($opentag,$closetag);
758: if ($url) {
759: $opentag = '<a href="'.$url.'">';
760: $closetag = '</a>';
761: }
1.39 raeburn 762: $r->print(
1.28 bisitz 763: '<p class="LC_warning">'
764: .&mt('A problem occurred when attempting to reset'
765: .' the password for your account.'
766: .' Please try again from your [_1]home server[_2].'
767: ,$opentag,$closetag)
1.39 raeburn 768: .'</p>'
769: );
1.20 raeburn 770: }
1.45 raeburn 771: } elsif (($change_failed eq 'prioruse') && ($passwdconf->{'numsaved'})) {
772: my $domdesc =
773: &Apache::lonnet::domain($data{'domain'},'description');
774: $r->print(
775: '<p class="LC_warning">'
776: .&mt('Please enter a password that you have not used recently.')
777: .'</p>'
1.46 raeburn 778: .&display_actions($contact_email,$domdesc,$token)
1.45 raeburn 779: );
1.47 ! raeburn 780: } elsif (($change_failed eq 'internalerror') || ($change_failed eq 'missingtemp') ||
! 781: ($change_failed eq 'error')) {
1.39 raeburn 782: $r->print(&generic_failure_msg($contact_name,$contact_email));
1.1 raeburn 783: }
1.44 raeburn 784: unless ($formfields{'username'}) {
785: delete($env{'form.uname'});
786: delete($env{'form.udom'});
787: }
1.1 raeburn 788: } else {
1.13 schafran 789: $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 790: if (keys(%formfields)) {
791: if (($formfields{'username'}) && ($formfields{'email'})) {
792: $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.'));
793: } elsif ($formfields{'username'}) {
794: $r->print(&mt('Please enter the username and domain of the LON-CAPA account for which you are setting a password.'));
795: } elsif ($formfields{'email'}) {
796: $r->print(&mt('Please enter the e-mail address associated with the LON-CAPA account for which you are setting a password.'));
797: }
798: if ($needscase) {
799: $r->print(' '.&mt('User data entered must match LON-CAPA account information (including case).'));
800: }
1.47 ! raeburn 801: $r->print('<br />');
! 802: }
! 803: my ($min,$max,$minrule,$maxrule);
! 804: if ($passwdconf->{min}) {
! 805: $min = $passwdconf->{min};
! 806: } else {
! 807: $min = $Apache::lonnet::passwdmin;
! 808: }
! 809: if ($min) {
! 810: $minrule = &mt('Minimum password length: [_1]',$min);
! 811: }
! 812: if ($passwdconf->{max}) {
! 813: $max = $passwdconf->{max};
! 814: $maxrule = &mt('Maximum password length: [_1]',$max);
1.44 raeburn 815: }
816: if (ref($passwdconf->{chars}) eq 'ARRAY') {
817: my %rules;
818: map { $rules{$_} = 1; } @{$passwdconf->{chars}};
819: my %rulenames = &Apache::lonlocal::texthash(
820: uc => 'At least one upper case letter',
821: lc => 'At least one lower case letter',
822: num => 'At least one number',
823: spec => 'At least one non-alphanumeric',
824: );
825: $r->print(&mt('The new password must satisfy the following:').'<ul>');
826: foreach my $poss ('uc','lc','num','spec') {
827: if ($rules{$poss}) {
828: $r->print('<li>'.$rulenames{$poss}.'</li>');
829: }
830: }
1.47 ! raeburn 831: if ($min) {
! 832: $r->print('<li>'.$minrule.'</li>');
! 833: }
! 834: if ($max) {
! 835: $r->print('<li>'.$maxrule.'</li>');
! 836: }
1.44 raeburn 837: $r->print('</ul>');
838: } else {
1.47 ! raeburn 839: if ($min && $max) {
! 840: $r->print(&mt('The new password must satisfy the following:').'<ul>'."\n".
! 841: '<li>'.$minrule.'</li>'."\n".
! 842: '<li>'.$maxrule.'</li>'."\n".
! 843: '</ul>'."\n");
! 844: } elsif ($min) {
! 845: $r->print($minrule.'<br />');
! 846: } elsif ($max) {
! 847: $r->print($maxrule.'<br />');
! 848: }
1.44 raeburn 849: }
850: $r->print(&mt('Your new password will be sent to the LON-CAPA server in an encrypted form.').'<br />');
851: &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token,$timelimit,\%formfields);
1.1 raeburn 852: }
853: } else {
1.39 raeburn 854: $r->print(
1.28 bisitz 855: '<p class="LC_warning">'
1.47 ! raeburn 856: .&mt('Sorry, the token generated when you requested a password reset has expired.').'<br />'
! 857: .&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 858: ,'<a href="/adm/resetpw">','</a>')
1.39 raeburn 859: .'</p>'
860: );
1.1 raeburn 861: }
862: } else {
1.39 raeburn 863: $r->print(
1.28 bisitz 864: '<p class="LC_warning">'
865: .&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.'
866: ,'<a href="/adm/resetpw">','</a>')
1.39 raeburn 867: .'</p>'
868: );
1.1 raeburn 869: }
1.39 raeburn 870: return;
1.1 raeburn 871: }
872:
1.20 raeburn 873: sub generic_failure_msg {
874: my ($contact_name,$contact_email) = @_;
1.28 bisitz 875: return
1.29 bisitz 876: '<p class="LC_error">'
877: .&mt('A problem occurred when attempting to reset the password for your account.')
878: .'<br />'
1.36 raeburn 879: .&mt('Please contact the [_1] ([_2]) for assistance.',
1.28 bisitz 880: $contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>')
881: .'</p>';
1.20 raeburn 882: }
883:
1.1 raeburn 884: sub create_passwd {
885: my $passwd = '';
1.2 albertel 886: my @letts = ("a".."z");
1.1 raeburn 887: for (my $i=0; $i<8; $i++) {
1.2 albertel 888: my $lettnum = int(rand(2));
1.1 raeburn 889: my $item = '';
890: if ($lettnum) {
1.2 albertel 891: $item = $letts[int(rand(26))];
892: my $uppercase = int(rand(2));
1.1 raeburn 893: if ($uppercase) {
894: $item =~ tr/a-z/A-Z/;
895: }
896: } else {
1.2 albertel 897: $item = int(rand(10));
1.1 raeburn 898: }
899: $passwd .= $item;
900: }
901: return ($passwd);
902: }
903:
1.29 bisitz 904: sub display_actions {
1.46 raeburn 905: my ($contact_email,$domdesc,$token) = @_;
906: my $url = '/adm/resetpw';
907: if ($token) {
908: $url .= '?token='.&escape($token);
909: }
1.29 bisitz 910: my @msg = (&mt('[_1]Go back[_2] and try again',
1.46 raeburn 911: '<a href="'.$url.'">','</a>'));
1.29 bisitz 912: my $msg2 = '';
913: if ($contact_email ne '') {
1.30 raeburn 914: my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
915: push(@msg, &mt('Contact the [_1]LON-CAPA helpdesk[_2] for the institution: [_3]',
916: '<a href="/adm/helpdesk?origurl='.$escuri.'">',
917: '</a>','<i>'.$domdesc.'</i>'));
918: } else {
919: $msg2 =
920: '<p>'
921: .&mt('You may wish to send an e-mail to the'
922: .' server administrator: [_1] for the [_2] domain.',
1.31 raeburn 923: '<i>'.$Apache::lonnet::perlvar{'AdmEMail'}.'</i>',
1.30 raeburn 924: '<i>'.$domdesc.'</i>')
925: .'</p>';
926: }
1.29 bisitz 927: return &Apache::lonhtmlcommon::actionbox(\@msg).$msg2;
1.44 raeburn 928: }
1.29 bisitz 929:
1.44 raeburn 930: sub reset_requires {
931: my ($username,$domain,$passwdconf) = @_;
932: my (%fields,$needscase);
933: return ($needscase,%fields) unless (ref($passwdconf) eq 'HASH');
934: my (%postlink,%resetcase);
935: if (ref($passwdconf->{resetpostlink}) eq 'HASH') {
936: %postlink = %{$passwdconf->{resetpostlink}};
937: }
938: if (ref($passwdconf->{resetcase}) eq 'ARRAY') {
939: map { $resetcase{$_} = 1; } (@{$passwdconf->{resetcase}});
940: } else {
941: $needscase = 1;
942: }
943: my %userenv =
944: &Apache::lonnet::get('environment',['inststatus'],$domain,$username);
945: my @inststatuses;
946: if ($userenv{'inststatus'} ne '') {
947: @inststatuses = split(/:/,$userenv{'inststatus'});
948: } else {
949: @inststatuses = ('default');
950: }
951: foreach my $status (@inststatuses) {
952: if (ref($postlink{$status}) eq 'ARRAY') {
953: map { $fields{$_} = 1; } (@{$postlink{$status}});
954: } else {
955: map { $fields{$_} = 1; } ('username','email');
956: }
957: if ($resetcase{$status}) {
958: $needscase = 1;
959: }
960: }
961: return ($needscase,%fields);
1.29 bisitz 962: }
963:
1.1 raeburn 964: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>