Annotation of loncom/interface/resetpw.pm, revision 1.27

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow access to password changing via a token sent to user's e-mail. 
                      3: #
1.27    ! raeburn     4: # $Id: resetpw.pm,v 1.26 2010/11/29 19:15:14 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;
                     58: use Apache::lonlocal;
                     59: use LONCAPA;
                     60: 
                     61: sub handler {
                     62:     my $r = shift;
                     63:     &Apache::loncommon::content_type($r,'text/html');
                     64:     $r->send_http_header;
                     65:     if ($r->header_only) {
                     66:         return OK;
                     67:     }
                     68:     my $contact_name = &mt('LON-CAPA helpdesk');
1.27    ! raeburn    69:     my $origmail =  $r->dir_config('lonSupportEMail');
1.1       raeburn    70:     my $server = $r->dir_config('lonHostID');
1.19      raeburn    71:     my $defdom = &Apache::lonnet::default_login_domain();
1.27    ! raeburn    72:     my $contacts =
        !            73:         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
        !            74:                                                  $defdom,$origmail);
        !            75:     my ($contact_email) = split(',',$contacts);
1.26      raeburn    76:     my $handle = &Apache::lonnet::check_for_valid_session($r);
                     77:     my $lonidsdir=$r->dir_config('lonIDsDir');
                     78:     if ($handle ne '') {
                     79:         if ($handle=~/^publicuser\_/) {
                     80:             unlink($r->dir_config('lonIDsDir')."/$handle.id");
                     81:         } else {
                     82:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                     83:         }
                     84:     }
1.1       raeburn    85:     &Apache::lonacc::get_posted_cgi($r);
                     86:     &Apache::lonlocal::get_language_handle($r);
                     87:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
                     88:     
                     89:     my @emailtypes = ('permanentemail','critnotification','notification');
1.2       albertel   90:     my $uname = &unescape($env{'form.uname'});
1.1       raeburn    91:     my $udom = $env{'form.udom'};
                     92:     my $token = $env{'form.token'};
1.26      raeburn    93:     my $brcrum = [];
                     94:     my $bread_crumbs_component = 'Forgotten Password';
                     95:     if ($token) {
                     96:         push (@{$brcrum},
                     97:             {href => '/adm/resetpw',
                     98:              text => 'Update Password'});
                     99: 
                    100:         $bread_crumbs_component = 'Reset Password'; 
                    101:     } else {
                    102:         push (@{$brcrum},
                    103:             {href => '/adm/resetpw',
                    104:              text => 'Account Information'});
                    105:         if ($uname && $udom) {
                    106:             push (@{$brcrum},
                    107:                 {href => '/adm/resetpw',
                    108:                  text => 'Result'});
                    109:         }
                    110:     }
                    111:     my $args = {bread_crumbs => $brcrum,
                    112:                 bread_crumbs_component => $bread_crumbs_component};
                    113:     $r->print(&Apache::loncommon::start_page('Reset password','',$args));
1.11      bisitz    114:     $r->print('<h3>'.&mt('Reset forgotten LON-CAPA password').'</h3>');
1.1       raeburn   115:     my $output;
                    116:     if ($token) {
                    117:         $output = &reset_passwd($r,$token,$contact_name,$contact_email);
                    118:     } elsif ($uname && $udom) {
1.6       albertel  119:         my $domdesc = &Apache::lonnet::domain($udom,'description');
1.1       raeburn   120:         my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
                    121:         if ($authtype =~ /^internal/) {
1.3       raeburn   122:             my $useremail = $env{'form.useremail'};
                    123:             if ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                    124:                 $output = &invalid_state('baduseremail',$domdesc,
                    125:                                          $contact_name,$contact_email);
                    126:             } else {
                    127:                 my %userinfo = 
1.4       albertel  128: 		    &Apache::lonnet::get('environment',\@emailtypes,
                    129: 					 $udom,$uname);
1.18      raeburn   130:                 my @allemails;
1.3       raeburn   131:                 foreach my $type (@emailtypes) {
1.18      raeburn   132:                     my $email = $userinfo{$type};
                    133:                     my @items;
                    134:                     if ($email =~ /,/) {
                    135:                         @items = split(',',$userinfo{$type});
                    136:                     } else {
                    137:                         @items = ($email);
                    138:                     }
                    139:                     foreach my $item (@items) {
                    140:                         if ($item =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                    141:                             unless(grep(/^\Q$item\E$/,@allemails)) { 
                    142:                                 push(@allemails,$item);
                    143:                             }
                    144:                         }
1.3       raeburn   145:                     }
                    146:                 }
1.18      raeburn   147:                 if (@allemails > 0) {
                    148:                     if (grep(/^\Q$useremail\E$/,@allemails)) {
                    149:                         $output = &send_token($uname,$udom,$useremail,$server,
1.3       raeburn   150:                                               $domdesc,$contact_name,
                    151:                                               $contact_email);
                    152:                     } else {
                    153:                         $output = &invalid_state('mismatch',$domdesc,
                    154:                                                  $contact_name,
                    155:                                                  $contact_email);
                    156:                     }
                    157:                 } else {
                    158:                     $output = &invalid_state('missing',$domdesc,
                    159:                                              $contact_name,$contact_email);
1.1       raeburn   160:                 }
                    161:             }
                    162:         } elsif ($authtype =~ /^(krb|unix|local)/) { 
                    163:             $output = &invalid_state('authentication',$domdesc,
                    164:                                      $contact_name,$contact_email);
                    165:         } else {
                    166:             $output = &invalid_state('invalid',$domdesc,
                    167:                                      $contact_name,$contact_email);
                    168:         }
                    169:     } else {
                    170:         $output = &get_uname($defdom);
                    171:     }
                    172:     $r->print($output);
                    173:     $r->print(&Apache::loncommon::end_page());
                    174:     return OK;
                    175: }
                    176: 
                    177: sub get_uname {
                    178:     my ($defdom) = @_;
                    179:     my %lt = &Apache::lonlocal::texthash(
                    180:                                          unam => 'username',
                    181:                                          udom => 'domain',
1.12      schafran  182:                                          uemail => 'E-mail address in LON-CAPA',
1.1       raeburn   183:                                          proc => 'Proceed');
                    184: 
1.23      bisitz    185:     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.');
1.8       bisitz    186:     $msg .= '<br /><br />'.&mt('Three conditions must be met:')
                    187:            .'<ul><li>'.&mt('An e-mail address must have previously been associated with your LON-CAPA username.').'</li>'
                    188:            .'<li>'.&mt('You must be able to access e-mail sent to that address.').'</li>'
                    189:            .'<li>'.&mt('Your LON-CAPA account must be of a type for which LON-CAPA can reset a password.')
                    190:            .'</ul>';
1.26      raeburn   191:     $msg .= '<form name="forgotpw" method="post" action="/adm/resetpw">'.
                    192:             &Apache::lonhtmlcommon::start_pick_box(). 
                    193:             &Apache::lonhtmlcommon::row_title('LON-CAPA '.$lt{'unam'}).
                    194:             '<input type="text" name="uname" size="20" />'.
                    195:             &Apache::lonhtmlcommon::row_closure(1).
                    196:             &Apache::lonhtmlcommon::row_title('LON-CAPA '.$lt{'udom'}).
                    197:             &Apache::loncommon::select_dom_form($defdom,'udom').
                    198:             &Apache::lonhtmlcommon::row_closure(1).
                    199:             &Apache::lonhtmlcommon::row_title($lt{'uemail'}).
                    200:             '<input type="text" name="useremail" size="30" />'.
                    201:             &Apache::lonhtmlcommon::end_pick_box().
                    202:             '<br /><br /><input type="submit" name="resetter" value="'.$lt{'proc'}.'" /></form>';
1.1       raeburn   203:     return $msg;
                    204: }
                    205: 
                    206: sub send_token {
                    207:     my ($uname,$udom,$email,$server,$domdesc,$contact_name,
                    208:         $contact_email) = @_;
1.11      bisitz    209:     my $msg = &mt('Thank you for your request to reset the password for your LON-CAPA account.').'<br /><br />';
1.1       raeburn   210: 
                    211:     my $now = time;
                    212:     my $temppasswd = &create_passwd();
1.2       albertel  213:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    214: 		'time'       => $now,
                    215: 		'domain'     => $udom,
                    216: 		'username'   => $uname,
                    217: 		'email'      => $email,
                    218: 		'temppasswd' => $temppasswd);
1.1       raeburn   219: 
1.3       raeburn   220:     my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
                    221:     if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2       albertel  222:         my $esc_token = &escape($token);
1.15      raeburn   223:         my $showtime = &Apache::lonlocal::locallocaltime(time);
                    224:         my $reseturl = &Apache::lonnet::absolute_url().'/adm/resetpw?token='.$esc_token;
1.26      raeburn   225:         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   226:         my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
                    227:                                 $contact_email);
                    228:         if ($result eq 'ok') {
1.13      schafran  229:             $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.<br /><br />The link included in the message will be valid for the next <b>two</b> hours.");
1.1       raeburn   230:         } else {
1.7       albertel  231:             $msg .= &mt("An error occurred when sending a message to the e-mail address associated with your LON-CAPA account. Please contact the [_1] ([_2]) for assistance.",$contact_name,$contact_email);
1.1       raeburn   232:         }
                    233:     } else {
1.4       albertel  234:         $msg .= &mt("An error occurred creating a token required for the password reset process. Please contact the [_1] ([_2]) for assistance.",$contact_name,$contact_email);
1.1       raeburn   235:     }
                    236:     return $msg;
                    237: }
                    238: 
                    239: sub send_mail {
                    240:     my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
                    241:     my $outcome;
                    242:     my $requestmail = "To: $email\n".
                    243:                       "From: $contact_name <$contact_email>\n".
                    244:                       "Subject: ".&mt('Your LON-CAPA account')."\n".
1.25      bisitz    245:                       "Content-type: text/plain\;charset=UTF-8\n".
1.1       raeburn   246:                       "\n\n".$mailmsg."\n\n".
                    247:                       &mt('[_1] LON-CAPA support team',$domdesc)."\n".
                    248:                       "$contact_email\n";
                    249:     if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
                    250:         print MAIL $requestmail;
                    251:         close(MAIL);
                    252:         $outcome = 'ok';
                    253:     } else {
                    254:         $outcome = 'fail';
                    255:     }
                    256:     return $outcome;
                    257: }
                    258: 
                    259: sub invalid_state {
                    260:     my ($error,$domdesc,$contact_name,$contact_email) = @_;
                    261:     my $msg;
                    262:     if ($error eq 'invalid') {
1.26      raeburn   263:         $msg = '<p class="LC_warning">'.&mt('The username you provided was not verified as a valid username in the LON-CAPA system for the [_1] domain.',$domdesc)
                    264:               .'</p>'.&mt('Please [_1]go back[_2] and try again.','<a href="javascript:history.go(-1)"><u>','</u></a>');
1.1       raeburn   265:     } else {
1.3       raeburn   266:         if ($error eq 'baduseremail') {
                    267:             $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
                    268:         } elsif ($error eq 'mismatch') {
                    269:             $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.');  
                    270:         } elsif ($error eq 'missing') {
1.1       raeburn   271:             $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
                    272:         } elsif ($error eq 'authentication') {
                    273:             $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
                    274:         }
1.26      raeburn   275:         $msg = '<p class="LC_warning">'.$msg.'</p>';
1.1       raeburn   276:         if ($contact_email ne '') {
                    277:             my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
1.8       bisitz    278:             $msg .= '<br /> '.&mt('You may wish to contact the [_1]LON-CAPA helpdesk[_2] for the [_3] domain.'
                    279:                                  ,'<a href="/adm/helpdesk?origurl='.$escuri.'">','</a>',$domdesc);
1.1       raeburn   280:         } else {
1.8       bisitz    281:             $msg .= '<br /> '.&mt('You may wish to send an e-mail to the server administrator: [_1] for the [_2] domain.',$Apache::lonnet::perlvar{'AdminEmail'},$domdesc);
1.1       raeburn   282:         }
                    283:     }
                    284:     return $msg;
                    285: }
                    286: 
                    287: sub reset_passwd {
                    288:     my ($r,$token,$contact_name,$contact_email) = @_;
                    289:     my $msg;
                    290:     my %data = &Apache::lonnet::tmpget($token);
                    291:     my $now = time;
                    292:     if (keys(%data) == 0) {
1.17      bisitz    293:         $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.'
                    294:                   ,'<a href="/adm/resetpw">','</a>');
1.1       raeburn   295:         return $msg;
                    296:     }
                    297:     if (($data{'time'} =~ /^\d+$/) && 
                    298:         ($data{'username'} ne '') && 
                    299:         ($data{'domain'} ne '') && 
1.3       raeburn   300:         ($data{'email'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && 
1.1       raeburn   301:         ($data{'temppasswd'} =~/^\w+$/)) {
1.9       raeburn   302:         my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.1       raeburn   303:         if ($now - $data{'time'} < 7200) {
                    304:             if ($env{'form.action'} eq 'verify_and_change_pass') {
1.22      raeburn   305:                 unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'}) && ($env{'form.email'} eq $data{'email'})) {
                    306:                     $msg = &generic_failure_msg($contact_name,$contact_email);
                    307:                     return $msg;
                    308:                 }
1.1       raeburn   309:                 my $change_failed = 
1.2       albertel  310: 		    &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1       raeburn   311:                 if (!$change_failed) {
                    312:                     my $delete = &Apache::lonnet::tmpdel($token);
1.9       raeburn   313:                     my $now = &Apache::lonlocal::locallocaltime(time);
1.1       raeburn   314:                     my $domdesc = 
1.6       albertel  315: 			&Apache::lonnet::domain($data{'domain'},'description');
1.1       raeburn   316:                     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";
                    317:                     my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
                    318:                                             $contact_name,$contact_email);
                    319:                     if ($result eq 'ok') {
                    320:                         $msg .= &mt('An e-mail confirming setting of the password for your LON-CAPA account has been sent to [_1].',$data{'email'});
                    321:                     } else {
                    322:                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming setting of your new password.',$data{'email'});
                    323:                     }
1.17      bisitz    324:                     $msg .= '<br /><br />'
                    325:                            .'<a href="/adm/login">'.&mt('Go to the login page').'</a>.';
1.20      raeburn   326:                 } elsif ($change_failed eq 'invalid_client') {
                    327:                     my $homeserver = &Apache::lonnet::homeserver($data{'username'},$data{'domain'});
                    328:                     if ($homeserver eq 'no_host') {
                    329:                         $msg .= &generic_failure_msg($contact_name,$contact_email);
                    330:                     } else {
                    331:                         my $protocol = $Apache::lonnet::protocol{$homeserver};
                    332:                         $protocol = 'http' if ($protocol ne 'https');
                    333:                         my $url = $protocol.'://'.&Apache::lonnet::hostname($homeserver).
                    334:                                   '/adm/resetpw';
                    335:                         my ($opentag,$closetag);
                    336:                         if ($url) {
                    337:                            $opentag = '<a href="'.$url.'">';
                    338:                            $closetag = '</a>';
                    339:                         }
                    340:                         $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);
                    341:                     }
1.1       raeburn   342:                 } else {
1.20      raeburn   343:                     $msg .= &generic_failure_msg($contact_name,$contact_email);
1.1       raeburn   344:                 }
                    345:             } else {
1.13      schafran  346:                 $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.1       raeburn   347:                 $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. The new password must contain at least 7 characters.').' '.&mt('Your new password will be sent to the LON-CAPA server in an encrypted form.').'<br />');
                    348:                 &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
                    349:             }
                    350:         } else {
1.17      bisitz    351:             $msg = &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.'
                    352:                       ,'<a href="/adm/resetpw">','</a>');
1.1       raeburn   353:         }
                    354:     } else {
1.17      bisitz    355:         $msg .= &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.'
                    356:                    ,'<a href="/adm/resetpw">','</a>');
1.1       raeburn   357:     }
                    358:     return $msg;
                    359: }
                    360: 
1.20      raeburn   361: sub generic_failure_msg {
                    362:     my ($contact_name,$contact_email) = @_;
                    363:     return &mt('A problem occurred when attempting to reset the password for your account. Please contact the [_1] - ([_2]) for assistance.',
                    364:               $contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>');
                    365: }
                    366: 
1.1       raeburn   367: sub create_passwd {
                    368:     my $passwd = '';
1.2       albertel  369:     my @letts = ("a".."z");
1.1       raeburn   370:     for (my $i=0; $i<8; $i++) {
1.2       albertel  371:         my $lettnum = int(rand(2));
1.1       raeburn   372:         my $item = '';
                    373:         if ($lettnum) {
1.2       albertel  374:             $item = $letts[int(rand(26))];
                    375:             my $uppercase = int(rand(2));
1.1       raeburn   376:             if ($uppercase) {
                    377:                 $item =~ tr/a-z/A-Z/;
                    378:             }
                    379:         } else {
1.2       albertel  380:             $item = int(rand(10));
1.1       raeburn   381:         }
                    382:         $passwd .= $item;
                    383:     }
                    384:     return ($passwd);
                    385: }
                    386: 
                    387: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>