--- loncom/interface/resetpw.pm 2013/02/05 15:45:18 1.31 +++ loncom/interface/resetpw.pm 2020/12/18 15:23:02 1.48 @@ -1,7 +1,7 @@ # The LearningOnline Network # Allow access to password changing via a token sent to user's e-mail. # -# $Id: resetpw.pm,v 1.31 2013/02/05 15:45:18 raeburn Exp $ +# $Id: resetpw.pm,v 1.48 2020/12/18 15:23:02 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -55,8 +55,10 @@ use Apache::Constants qw(:common); use Apache::lonacc; use Apache::lonnet; use Apache::loncommon; +use Apache::lonpreferences; use Apache::lonlocal; use LONCAPA; +use HTML::Entities; sub handler { my $r = shift; @@ -87,17 +89,89 @@ sub handler { &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']); my @emailtypes = ('permanentemail','critnotification','notification'); - my $uname = &unescape($env{'form.uname'}); - my $udom = $env{'form.udom'}; + my $uname = $env{'form.uname'}; + $uname =~ s/^\s+|\s+$//g; + $uname = &LONCAPA::clean_username($uname); + my $udom = &LONCAPA::clean_domain($env{'form.udom'}); + my ($domdesc,$otherinst,$lookup); + if ($udom) { + $domdesc = &Apache::lonnet::domain($udom,'description'); + if ($domdesc) { + $otherinst = 1; + my @ids=&Apache::lonnet::current_machine_ids(); + my %servers = &Apache::lonnet::internet_dom_servers($udom); + foreach my $hostid (keys(%servers)) { + if (grep(/^\Q$hostid\E$/,@ids)) { + $otherinst = 0; + last; + } + } + } + } + my $dom_in_effect = $defdom; + if (($udom ne '') && ($domdesc ne '')) { + unless ($otherinst) { + $dom_in_effect = $udom; + } + } + my %passwdconf = &Apache::lonnet::get_passwdconf($dom_in_effect); my $token = $env{'form.token'}; + my $useremail = $env{'form.useremail'}; + if (($udom ne '') && (!$otherinst) && (!$token)) { + if ($uname ne '') { + my $uhome = &Apache::lonnet::homeserver($uname,$udom); + if ($uhome eq 'no_host') { + my %srch = (srchby => 'uname_ci', + srchdomain => $udom, + srchterm => $uname, + srchtype => 'exact'); + my %srch_results = &Apache::lonnet::usersearch(\%srch); + if (keys(%srch_results) > 1) { + $lookup = 'nonunique'; + if ($useremail =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) { + foreach my $key (keys(%srch_results)) { + if (ref($srch_results{$key}) eq 'HASH') { + if ($srch_results{$key}{permanentemail} =~ /^\Q$useremail\E$/i) { + ($uname) = split(/:/,$key); + undef($lookup); + last; + } + } + } + } + } elsif (keys(%srch_results) == 1) { + my $match = (keys(%srch_results))[0]; + ($uname) = split(/:/,$match); + } else { + $lookup = 'nomatch'; + } + } + } + if (($lookup eq 'nomatch') || ($uname eq '')) { + if (($useremail =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && + ($passwdconf{'resetprelink'} eq 'either')) { + my %srch = (srchby => 'email', + srchdomain => $udom, + srchterm => $useremail, + srchtype => 'exact'); + my %srch_results = &Apache::lonnet::usersearch(\%srch); + if (keys(%srch_results) > 1) { + $lookup = 'nonunique'; + } elsif (keys(%srch_results) == 1) { + my $match = (keys(%srch_results))[0]; + ($uname) = split(/:/,$match); + undef($lookup); + } else { + $lookup = 'nomatch'; + } + } + } + } my $brcrum = []; - my $bread_crumbs_component = 'Forgotten Password'; if ($token) { push (@{$brcrum}, {href => '/adm/resetpw', text => 'Update Password'}); - - $bread_crumbs_component = 'Reset Password'; } else { push (@{$brcrum}, {href => '/adm/resetpw', @@ -108,104 +182,299 @@ sub handler { text => 'Result'}); } } - my $args = {bread_crumbs => $brcrum, - bread_crumbs_component => $bread_crumbs_component}; - $r->print(&Apache::loncommon::start_page('Reset password','',$args)); - $r->print('
'.$blocktext.'
' + .&display_actions($contact_email,$domdesc); + } elsif (($passwdconf{'resetprelink'} ne 'either') && + ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/)) { + $output = &invalid_state('baduseremail',$domdesc, + $contact_name,$contact_email); } else { - $output = &invalid_state('mismatch',$domdesc, - $contact_name, - $contact_email); + my %userinfo = + &Apache::lonnet::get('environment',\@emailtypes, + $udom,$uname); + my @allemails; + foreach my $type (@emailtypes) { + if (ref($passwdconf{resetemail}) eq 'ARRAY') { + if ($type eq 'permanentemail') { + next unless (grep(/^permanent$/,@{$passwdconf{resetemail}})); + } elsif ($type eq 'critnotification') { + next unless (grep(/^critical$/,@{$passwdconf{resetemail}})); + } elsif ($type eq 'notification') { + next unless (grep(/^notify$/,@{$passwdconf{resetemail}})); + } + } + my $email = $userinfo{$type}; + my @items; + if ($email =~ /,/) { + @items = split(',',$userinfo{$type}); + } else { + @items = ($email); + } + foreach my $item (@items) { + if ($item =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) { + unless (grep(/^\Q$item\E$/i,@allemails)) { + push(@allemails,$item); + } + } + } + } + if (@allemails > 0) { + my ($sendto,$warning,$timelimit); + my $timelimit = 2; + if ($passwdconf{'resetlink'} =~ /^\d+(|\.\d*)$/) { + $timelimit = $passwdconf{'resetlink'}; + } + if ($passwdconf{'resetprelink'} eq 'either') { + if ($useremail ne '') { + if (grep(/^\Q$useremail\E$/i,@allemails)) { + $sendto = $useremail; + } else { + $warning = &mt('The e-mail address you entered did not match the expected e-mail address.'); + } + } elsif (@allemails > 1) { + $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.'); + } + unless ($sendto) { + $sendto = $allemails[0]; + } + } else { + if (grep(/^\Q$useremail\E$/i,@allemails)) { + $sendto = $useremail; + } else { + $output = &invalid_state('mismatch',$domdesc, + $contact_name, + $contact_email); + } + } + if ($sendto) { + $output = &send_token($uname,$udom,$sendto,$server, + $domdesc,$contact_name, + $contact_email,$timelimit,$warning); + } + } else { + $output = &invalid_state('missing',$domdesc, + $contact_name,$contact_email); + } } + } elsif ($authtype =~ /^(krb|unix|local)/) { + $output = &invalid_state('authentication',$domdesc, + $contact_name,$contact_email); } else { - $output = &invalid_state('missing',$domdesc, + $output = &invalid_state('invalid',$domdesc, $contact_name,$contact_email); } } - } elsif ($authtype =~ /^(krb|unix|local)/) { - $output = &invalid_state('authentication',$domdesc, - $contact_name,$contact_email); } else { - $output = &invalid_state('invalid',$domdesc, - $contact_name,$contact_email); + $output = &get_uname($server,$dom_in_effect,\%passwdconf); } } else { - $output = &get_uname($defdom); + $output = &get_uname($server,$defdom,\%passwdconf); } - $r->print($output); + $r->print($header.$output); $r->print(&Apache::loncommon::end_page()); return OK; } sub get_uname { - my ($defdom) = @_; + my ($server,$defdom,$passwdconf) = @_; + return unless (ref($passwdconf) eq 'HASH'); my %lt = &Apache::lonlocal::texthash( - unam => 'username', - udom => 'domain', - uemail => 'E-mail address in LON-CAPA', - proc => 'Proceed'); - - my $msg = &mt('If you use the same account for other campus services besides LON-CAPA, (e.g., e-mail, course registration, etc.), a separate centrally managed mechanism likely exists to reset a password. However, if your account is used for just LON-CAPA access you will probably be able to reset a password from this page.'); - $msg .= ''.&mt('If you use the same account for other campus services besides LON-CAPA, (e.g., e-mail, course registration, etc.), a separate centrally managed mechanism likely exists to reset a password. However, if your account is used for just LON-CAPA access you will probably be able to reset a password from this page.').'
'; + } + if ($passwdconf->{'resetcustom'} eq "/res/$defdom/$defdom-domainconfig/customtext/resetpw/resetpw.html") { + my $contents = &Apache::lonnet::getfile(&Apache::lonnet::filelocation('',$passwdconf->{'resetcustom'})); + unless ($contents eq '-1') { + $msg .= $contents; + } + } + $msg .= ''.&mt('Three conditions must be met:') .'
'
.&mt('Thank you for your request to reset the password for your LON-CAPA account.')
@@ -213,7 +482,8 @@ sub send_token {
my $now = time;
my $temppasswd = &create_passwd();
- my %info = ('ip' => $ENV{'REMOTE_ADDR'},
+ my $ip = &Apache::lonnet::get_requestor_ip();
+ my %info = ('ip' => $ip,
'time' => $now,
'domain' => $udom,
'username' => $uname,
@@ -229,7 +499,10 @@ sub send_token {
my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
$contact_email);
if ($result eq 'ok') {
- $msg .= &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.
The link included in the message will be valid for the next two hours.");
+ $msg .=
+ &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.')
+ .'
'
+ .&mt('The link included in the message will be valid for the next [_1][quant,_2,hour][_3].','',$timelimit,'');
} else {
$msg .=
'
' @@ -237,7 +510,6 @@ sub send_token { .' associated with your LON-CAPA account.') .'
' .&display_actions($contact_email,$domdesc); - } } else { $msg .= @@ -281,7 +553,15 @@ sub invalid_state { .''; $msg .= &display_actions($contact_email,$domdesc); } else { - if ($error eq 'baduseremail') { + if ($error eq 'captcha') { + $msg = &mt('Validation of the code you entered failed'); + } elsif ($error eq 'recaptcha') { + $msg = &mt('Validation of human, not robot, failed'); + } elsif ($error eq 'nonunique') { + $msg = &mt('More than one username was identified from the information you provided; try providing both a username and e-mail address'); + } elsif ($error eq 'nomatch') { + $msg = &mt('A valid user could not be identified from the username and/or e-mail address you provided'); + } elsif ($error eq 'baduseremail') { $msg = &mt('The e-mail address you provided does not appear to be a valid address.'); } elsif ($error eq 'mismatch') { $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.'); @@ -289,6 +569,8 @@ sub invalid_state { $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.'); } elsif ($error eq 'authentication') { $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.'); + } elsif ($error eq 'baddomain') { + $msg = &mt('The domain you provided was not verified as a valid domain in the LON-CAPA system.'); } $msg = ''.$msg.'
' .&display_actions($contact_email,$domdesc); @@ -296,100 +578,298 @@ sub invalid_state { return $msg; } +sub homeserver_redirect { + my ($uname,$udom,$domdesc,$brcrum) = @_; + my $uhome; + if (($uname ne '') && ($udom ne '')) { + $uhome = &Apache::lonnet::homeserver($uname,$udom); + } + if (($uhome eq 'no_host') || ($uhome eq '')) { + $uhome = &Apache::lonnet::domain($udom,'primary'); + } + my $hostname = &Apache::lonnet::hostname($uhome); + my $protocol = $Apache::lonnet::protocol{$uhome}; + $protocol = 'http' if ($protocol ne 'https'); + my $url = $protocol.'://'.$hostname.'/adm/resetpw'; + # Breadcrumbs + my $start_page = &Apache::loncommon::start_page('Switching Server',undef, + {'redirect' => [0,$url], + 'bread_crumbs' => $brcrum,}); + my $output = ''.&mt('This LON-CAPA server belongs to a different domain.').' '. + &mt('You are being switched to your domain ([_1]), to use the "Forgot Password" tool.',$domdesc). + '
'; + return ($start_page,$output); +} + sub reset_passwd { - my ($r,$token,$contact_name,$contact_email) = @_; - my $msg; + my ($r,$token,$contact_name,$contact_email,$passwdconf) = @_; + return unless (ref($passwdconf) eq 'HASH'); my %data = &Apache::lonnet::tmpget($token); my $now = time; if (keys(%data) == 0) { - $msg = &mt('Sorry, the URL you provided to complete the reset of your password was invalid. Either the token included in the URL has been deleted or the URL you provided was invalid. Please submit a [_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.' - ,'',''); - return $msg; + $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.' + ,'','') + ); + return; } if (($data{'time'} =~ /^\d+$/) && ($data{'username'} ne '') && ($data{'domain'} ne '') && ($data{'email'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && ($data{'temppasswd'} =~/^\w+$/)) { + my $timelimit = 7200; + if ($passwdconf->{resetlink} =~ /^\d+(|\.\d*)$/) { + $timelimit = 3600 * $passwdconf->{resetlink}; + } my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'}); - if ($now - $data{'time'} < 7200) { + my ($blocked,$blocktext) = + &Apache::loncommon::blocking_status('passwd',$data{'username'},$data{'domain'}); + if ($blocked) { + $r->print(''.$blocktext.'
'); + return; + } elsif ($now - $data{'time'} < $timelimit) { + my ($needscase,%formfields) = &reset_requires($data{'username'},$data{'domain'}, + $passwdconf); if ($env{'form.action'} eq 'verify_and_change_pass') { - unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'}) && ($env{'form.email'} eq $data{'email'})) { - $msg = &generic_failure_msg($contact_name,$contact_email); - return $msg; + my $invalidinfo; + if ($formfields{'username'}) { + $env{'form.uname'} =~ s/^\s+|\s+$//g; + $env{'form.udom'} =~ s/^\s+|\s+$//g; + if ($needscase) { + unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'})) { + $invalidinfo = "||$env{'form.uname'}|| ||$env{'form.udom'}|| "; + } + } else { + if ((lc($env{'form.uname'}) eq lc($data{'username'})) && (lc($env{'form.udom'}) eq lc($data{'domain'}))) { + $env{'form.uname'} = $data{'username'}; + } else { + $invalidinfo = "||$env{'form.uname'}|| ||$env{'form.udom'}|| "; + } + } + } else { + $env{'form.uname'} = $data{'username'}; + $env{'form.udom'} = $data{'domain'}; } - my $change_failed = - &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token); + if ($formfields{'email'}) { + $env{'form.email'} =~ s/^\s+|\s+$//g; + if ($needscase) { + unless ($env{'form.email'} eq $data{'email'}) { + $invalidinfo .= "||$env{'form.email'}||"; + } + } else { + unless (lc($env{'form.email'}) eq lc($data{'email'})) { + $invalidinfo = "||$env{'form.email'}||"; + } + } + } + if ($invalidinfo) { + &Apache::lonnet::logthis("Forgot Password -- token data: ||$data{'username'}|| ||$data{'domain'}|| ||$data{'email'}|| differs from form: $invalidinfo"); + my $retry; + $r->print( + '' + .&mt('A problem occurred when attempting to reset' + .' the password for your account.').'
'); + if (($formfields{'username'}) && ($formfields{'email'})) { + if ($needscase) { + $r->print('' + .&mt('Please verify you entered the correct username and e-mail address, ' + .'including the correct lower and/or upper case letters') + .'
'); + } else { + $r->print('' + .&mt('Please verify you entered the correct username and e-mail address.') + .'
'); + } + $retry = 1; + } elsif ($formfields{'username'}) { + if ($needscase) { + $r->print('' + .&mt('Please verify you entered the correct username, ' + .'including the correct lower and/or upper case letters') + .'
'); + } else { + $r->print('' + .&mt('Please verify you entered the correct username.') + .'
'); + } + $retry = 1; + } elsif ($formfields{'email'}) { + if ($needscase) { + $r->print('' + .&mt('Please verify you entered the correct e-mail address, ' + .'including the correct lower and/or upper case letters') + .'
'); + } else { + $r->print('' + .&mt('Please verify you entered the correct e-mail address.') + .'
'); + } + $retry = 1; + } + if ($retry) { + &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token,$timelimit,\%formfields); + } else { + $r->print(&generic_failure_msg($contact_name,$contact_email)); + } + unless ($formfields{'username'}) { + delete($env{'form.uname'}); + delete($env{'form.udom'}); + } + return; + } + my $change_failed = + &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token,$timelimit,\%formfields); if (!$change_failed) { my $delete = &Apache::lonnet::tmpdel($token); my $now = &Apache::lonlocal::locallocaltime(time); my $domdesc = &Apache::lonnet::domain($data{'domain'},'description'); - 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"; + my $ip = &Apache::lonnet::get_requestor_ip(); + 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"; my $result = &send_mail($domdesc,$data{'email'},$mailmsg, $contact_name,$contact_email); + my $confirm_msg; if ($result eq 'ok') { - $msg .= - '' - .&mt('An e-mail confirming setting of the password' - .' for your LON-CAPA account has been sent to [_1].' - ,$data{'email'}) - .'
'; + $confirm_msg = + &Apache::lonhtmlcommon::confirm_success( + &mt('An e-mail confirming setting of the password' + .' for your LON-CAPA account has been sent to [_1].' + ,''.$data{'email'}.'')); } else { - $msg .= - '' - .&mt('An error occurred when sending e-mail to [_1]' - .' confirming setting of your new password.' - ,$data{'email'}) - .'
'; + $confirm_msg = + &Apache::lonhtmlcommon::confirm_success( + &mt('An error occurred when sending e-mail to [_1]' + .' confirming setting of your new password.' + ,''.$data{'email'}.''),1); } - $msg .= '' .&mt('A problem occurred when attempting to reset' .' the password for your account.' .' Please try again from your [_1]home server[_2].' ,$opentag,$closetag) - .'
'; + .'' + ); } - } else { - $msg .= &generic_failure_msg($contact_name,$contact_email); + } elsif (($change_failed eq 'prioruse') && ($passwdconf->{'numsaved'})) { + my $domdesc = + &Apache::lonnet::domain($data{'domain'},'description'); + $r->print( + '' + .&mt('Please enter a password that you have not used recently.') + .'
' + .&display_actions($contact_email,$domdesc,$token) + ); + } elsif (($change_failed eq 'internalerror') || ($change_failed eq 'missingtemp') || + ($change_failed eq 'error')) { + $r->print(&generic_failure_msg($contact_name,$contact_email)); + } + unless ($formfields{'username'}) { + delete($env{'form.uname'}); + delete($env{'form.udom'}); } } else { $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).''
- .&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.'
+ .&mt('Sorry, the token generated when you requested a password reset has expired.').'
'
+ .&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.'
,'','')
- .'
' .&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.' ,'','') - .'
'; + .'' + ); } - return $msg; + return; } sub generic_failure_msg { @@ -398,7 +878,7 @@ sub generic_failure_msg { ''
.&mt('A problem occurred when attempting to reset the password for your account.')
.'
'
- .&mt('Please contact the [_1] - ([_2]) for assistance.',
+ .&mt('Please contact the [_1] ([_2]) for assistance.',
$contact_name,''.$contact_email.'')
.'