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

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

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