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

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

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