Annotation of loncom/interface/resetpw.pm, revision 1.46
1.1 raeburn 1: # The LearningOnline Network
2: # Allow access to password changing via a token sent to user's e-mail.
3: #
1.46 ! raeburn 4: # $Id: resetpw.pm,v 1.45 2019/04/26 20:22:18 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 {
642: unless ((lc($env{'form.uname'}) eq lc($data{'username'})) && (lc($env{'form.udom'}) eq lc($data{'domain'}))) {
643: $invalidinfo = "||$env{'form.uname'}|| ||$env{'form.udom'}|| ";
644: }
645: }
646: } else {
647: $env{'form.uname'} = $data{'username'};
648: $env{'form.udom'} = $data{'domain'};
649: }
650: if ($formfields{'email'}) {
651: $env{'form.email'} =~ s/^\s+|\s+$//g;
652: if ($needscase) {
653: unless ($env{'form.email'} eq $data{'email'}) {
654: $invalidinfo .= "||$env{'form.email'}||";
655: }
656: } else {
657: unless (lc($env{'form.email'}) eq lc($data{'email'})) {
658: $invalidinfo = "||$env{'form.email'}||";
659: }
660: }
661: }
662: if ($invalidinfo) {
663: &Apache::lonnet::logthis("Forgot Password -- token data: ||$data{'username'}|| ||$data{'domain'}|| ||$data{'email'}|| differs from form: $invalidinfo");
1.39 raeburn 664: $r->print(&generic_failure_msg($contact_name,$contact_email));
1.44 raeburn 665: unless ($formfields{'username'}) {
666: delete($env{'form.uname'});
667: delete($env{'form.udom'});
668: }
1.39 raeburn 669: return;
1.22 raeburn 670: }
1.44 raeburn 671: my $change_failed =
1.2 albertel 672: &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1 raeburn 673: if (!$change_failed) {
674: my $delete = &Apache::lonnet::tmpdel($token);
1.9 raeburn 675: my $now = &Apache::lonlocal::locallocaltime(time);
1.1 raeburn 676: my $domdesc =
1.6 albertel 677: &Apache::lonnet::domain($data{'domain'},'description');
1.1 raeburn 678: 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";
679: my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
680: $contact_name,$contact_email);
1.33 bisitz 681: my $confirm_msg;
1.1 raeburn 682: if ($result eq 'ok') {
1.33 bisitz 683: $confirm_msg =
684: &Apache::lonhtmlcommon::confirm_success(
685: &mt('An e-mail confirming setting of the password'
686: .' for your LON-CAPA account has been sent to [_1].'
687: ,'<span class="LC_filename">'.$data{'email'}.'</span>'));
1.1 raeburn 688: } else {
1.33 bisitz 689: $confirm_msg =
690: &Apache::lonhtmlcommon::confirm_success(
691: &mt('An error occurred when sending e-mail to [_1]'
692: .' confirming setting of your new password.'
693: ,'<span class="LC_filename">'.$data{'email'}.'</span>'),1);
1.1 raeburn 694: }
1.39 raeburn 695: $r->print(
1.33 bisitz 696: &Apache::loncommon::confirmwrapper($confirm_msg)
697: .&Apache::lonhtmlcommon::actionbox([
1.39 raeburn 698: '<a href="/adm/login">'.&mt('Go to the login page').'</a>'])
699: );
1.20 raeburn 700: } elsif ($change_failed eq 'invalid_client') {
701: my $homeserver = &Apache::lonnet::homeserver($data{'username'},$data{'domain'});
702: if ($homeserver eq 'no_host') {
1.39 raeburn 703: $r->print(&generic_failure_msg($contact_name,$contact_email));
1.20 raeburn 704: } else {
1.42 raeburn 705: my $hostname = &Apache::lonnet::hostname($homeserver);
1.20 raeburn 706: my $protocol = $Apache::lonnet::protocol{$homeserver};
707: $protocol = 'http' if ($protocol ne 'https');
1.42 raeburn 708: my $url = $protocol.'://'.$hostname.'/adm/resetpw';
1.20 raeburn 709: my ($opentag,$closetag);
710: if ($url) {
711: $opentag = '<a href="'.$url.'">';
712: $closetag = '</a>';
713: }
1.39 raeburn 714: $r->print(
1.28 bisitz 715: '<p class="LC_warning">'
716: .&mt('A problem occurred when attempting to reset'
717: .' the password for your account.'
718: .' Please try again from your [_1]home server[_2].'
719: ,$opentag,$closetag)
1.39 raeburn 720: .'</p>'
721: );
1.20 raeburn 722: }
1.45 raeburn 723: } elsif (($change_failed eq 'prioruse') && ($passwdconf->{'numsaved'})) {
724: my $domdesc =
725: &Apache::lonnet::domain($data{'domain'},'description');
726: $r->print(
727: '<p class="LC_warning">'
728: .&mt('Please enter a password that you have not used recently.')
729: .'</p>'
1.46 ! raeburn 730: .&display_actions($contact_email,$domdesc,$token)
1.45 raeburn 731: );
1.1 raeburn 732: } else {
1.39 raeburn 733: $r->print(&generic_failure_msg($contact_name,$contact_email));
1.1 raeburn 734: }
1.44 raeburn 735: unless ($formfields{'username'}) {
736: delete($env{'form.uname'});
737: delete($env{'form.udom'});
738: }
1.1 raeburn 739: } else {
1.13 schafran 740: $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 741: if (keys(%formfields)) {
742: if (($formfields{'username'}) && ($formfields{'email'})) {
743: $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.'));
744: } elsif ($formfields{'username'}) {
745: $r->print(&mt('Please enter the username and domain of the LON-CAPA account for which you are setting a password.'));
746: } elsif ($formfields{'email'}) {
747: $r->print(&mt('Please enter the e-mail address associated with the LON-CAPA account for which you are setting a password.'));
748: }
749: if ($needscase) {
750: $r->print(' '.&mt('User data entered must match LON-CAPA account information (including case).'));
751: }
752: $r->print(' ');
753: }
754: if (ref($passwdconf->{chars}) eq 'ARRAY') {
755: my %rules;
756: map { $rules{$_} = 1; } @{$passwdconf->{chars}};
757: my %rulenames = &Apache::lonlocal::texthash(
758: uc => 'At least one upper case letter',
759: lc => 'At least one lower case letter',
760: num => 'At least one number',
761: spec => 'At least one non-alphanumeric',
762: );
763: $r->print(&mt('The new password must satisfy the following:').'<ul>');
764: foreach my $poss ('uc','lc','num','spec') {
765: if ($rules{$poss}) {
766: $r->print('<li>'.$rulenames{$poss}.'</li>');
767: }
768: }
769: $r->print('</ul>');
770: } else {
771: $r->print(&mt('The new password must contain at least 7 characters.').' ');
772: }
773: $r->print(&mt('Your new password will be sent to the LON-CAPA server in an encrypted form.').'<br />');
774: &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token,$timelimit,\%formfields);
1.1 raeburn 775: }
776: } else {
1.39 raeburn 777: $r->print(
1.28 bisitz 778: '<p class="LC_warning">'
779: .&mt('Sorry, the token generated when you requested a password reset has expired. 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.'
780: ,'<a href="/adm/resetpw">','</a>')
1.39 raeburn 781: .'</p>'
782: );
1.1 raeburn 783: }
784: } else {
1.39 raeburn 785: $r->print(
1.28 bisitz 786: '<p class="LC_warning">'
787: .&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.'
788: ,'<a href="/adm/resetpw">','</a>')
1.39 raeburn 789: .'</p>'
790: );
1.1 raeburn 791: }
1.39 raeburn 792: return;
1.1 raeburn 793: }
794:
1.20 raeburn 795: sub generic_failure_msg {
796: my ($contact_name,$contact_email) = @_;
1.28 bisitz 797: return
1.29 bisitz 798: '<p class="LC_error">'
799: .&mt('A problem occurred when attempting to reset the password for your account.')
800: .'<br />'
1.36 raeburn 801: .&mt('Please contact the [_1] ([_2]) for assistance.',
1.28 bisitz 802: $contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>')
803: .'</p>';
1.20 raeburn 804: }
805:
1.1 raeburn 806: sub create_passwd {
807: my $passwd = '';
1.2 albertel 808: my @letts = ("a".."z");
1.1 raeburn 809: for (my $i=0; $i<8; $i++) {
1.2 albertel 810: my $lettnum = int(rand(2));
1.1 raeburn 811: my $item = '';
812: if ($lettnum) {
1.2 albertel 813: $item = $letts[int(rand(26))];
814: my $uppercase = int(rand(2));
1.1 raeburn 815: if ($uppercase) {
816: $item =~ tr/a-z/A-Z/;
817: }
818: } else {
1.2 albertel 819: $item = int(rand(10));
1.1 raeburn 820: }
821: $passwd .= $item;
822: }
823: return ($passwd);
824: }
825:
1.29 bisitz 826: sub display_actions {
1.46 ! raeburn 827: my ($contact_email,$domdesc,$token) = @_;
! 828: my $url = '/adm/resetpw';
! 829: if ($token) {
! 830: $url .= '?token='.&escape($token);
! 831: }
1.29 bisitz 832: my @msg = (&mt('[_1]Go back[_2] and try again',
1.46 ! raeburn 833: '<a href="'.$url.'">','</a>'));
1.29 bisitz 834: my $msg2 = '';
835: if ($contact_email ne '') {
1.30 raeburn 836: my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
837: push(@msg, &mt('Contact the [_1]LON-CAPA helpdesk[_2] for the institution: [_3]',
838: '<a href="/adm/helpdesk?origurl='.$escuri.'">',
839: '</a>','<i>'.$domdesc.'</i>'));
840: } else {
841: $msg2 =
842: '<p>'
843: .&mt('You may wish to send an e-mail to the'
844: .' server administrator: [_1] for the [_2] domain.',
1.31 raeburn 845: '<i>'.$Apache::lonnet::perlvar{'AdmEMail'}.'</i>',
1.30 raeburn 846: '<i>'.$domdesc.'</i>')
847: .'</p>';
848: }
1.29 bisitz 849: return &Apache::lonhtmlcommon::actionbox(\@msg).$msg2;
1.44 raeburn 850: }
1.29 bisitz 851:
1.44 raeburn 852: sub reset_requires {
853: my ($username,$domain,$passwdconf) = @_;
854: my (%fields,$needscase);
855: return ($needscase,%fields) unless (ref($passwdconf) eq 'HASH');
856: my (%postlink,%resetcase);
857: if (ref($passwdconf->{resetpostlink}) eq 'HASH') {
858: %postlink = %{$passwdconf->{resetpostlink}};
859: }
860: if (ref($passwdconf->{resetcase}) eq 'ARRAY') {
861: map { $resetcase{$_} = 1; } (@{$passwdconf->{resetcase}});
862: } else {
863: $needscase = 1;
864: }
865: my %userenv =
866: &Apache::lonnet::get('environment',['inststatus'],$domain,$username);
867: my @inststatuses;
868: if ($userenv{'inststatus'} ne '') {
869: @inststatuses = split(/:/,$userenv{'inststatus'});
870: } else {
871: @inststatuses = ('default');
872: }
873: foreach my $status (@inststatuses) {
874: if (ref($postlink{$status}) eq 'ARRAY') {
875: map { $fields{$_} = 1; } (@{$postlink{$status}});
876: } else {
877: map { $fields{$_} = 1; } ('username','email');
878: }
879: if ($resetcase{$status}) {
880: $needscase = 1;
881: }
882: }
883: return ($needscase,%fields);
1.29 bisitz 884: }
885:
1.1 raeburn 886: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>