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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow access to password changing via a token sent to user's e-mail. 
                      3: #
                      4: # Copyright Michigan State University Board of Trustees
                      5: #
                      6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      7: #
                      8: # LON-CAPA is free software; you can redistribute it and/or modify
                      9: # it under the terms of the GNU General Public License as published by
                     10: # the Free Software Foundation; either version 2 of the License, or
                     11: # (at your option) any later version.
                     12: #
                     13: # LON-CAPA is distributed in the hope that it will be useful,
                     14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16: # GNU General Public License for more details.
                     17: #
                     18: # You should have received a copy of the GNU General Public License
                     19: # along with LON-CAPA; if not, write to the Free Software
                     20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     21: #
                     22: # /home/httpd/html/adm/gpl.txt
                     23: #
                     24: # http://www.lon-capa.org/
                     25: #
                     26: #
                     27: package Apache::resetpw;
                     28: 
                     29: use strict;
                     30: use Apache::Constants qw(:common);
                     31: use Apache::lonacc;
                     32: use Apache::lonnet;
                     33: use Apache::loncommon;
                     34: use Apache::lonlocal;
                     35: use LONCAPA;
                     36: 
                     37: sub handler {
                     38:     my $r = shift;
                     39:     &Apache::loncommon::content_type($r,'text/html');
                     40:     $r->send_http_header;
                     41:     if ($r->header_only) {
                     42:         return OK;
                     43:     }
                     44:     my $contact_name = &mt('LON-CAPA helpdesk');
                     45:     my $contact_email =  $r->dir_config('lonSupportEMail');
                     46:     my $server = $r->dir_config('lonHostID');
                     47:     my $defdom = $r->dir_config('lonDefDomain');
                     48:     &Apache::lonacc::get_posted_cgi($r);
                     49:     &Apache::lonlocal::get_language_handle($r);
                     50:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
                     51:     
                     52:     my @emailtypes = ('permanentemail','critnotification','notification');
1.2       albertel   53:     my $uname = &unescape($env{'form.uname'});
1.1       raeburn    54:     my $udom = $env{'form.udom'};
                     55:     my $token = $env{'form.token'};
1.10      raeburn    56:     my $start_page =
                     57:         &Apache::loncommon::start_page('Reset password','',
                     58:                                            {
                     59:                                              'no_inline_link'   => 1,});
                     60:     $r->print($start_page);
1.11      bisitz     61:     $r->print('<h3>'.&mt('Reset forgotten LON-CAPA password').'</h3>');
1.1       raeburn    62:     my $output;
                     63:     if ($token) {
                     64:         $output = &reset_passwd($r,$token,$contact_name,$contact_email);
                     65:     } elsif ($uname && $udom) {
1.6       albertel   66:         my $domdesc = &Apache::lonnet::domain($udom,'description');
1.1       raeburn    67:         my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
                     68:         if ($authtype =~ /^internal/) {
1.3       raeburn    69:             my $useremail = $env{'form.useremail'};
                     70:             if ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                     71:                 $output = &invalid_state('baduseremail',$domdesc,
                     72:                                          $contact_name,$contact_email);
                     73:             } else {
                     74:                 my %userinfo = 
1.4       albertel   75: 		    &Apache::lonnet::get('environment',\@emailtypes,
                     76: 					 $udom,$uname);
1.3       raeburn    77:                 my $email = '';
                     78:                 my $emailtarget;
                     79:                 foreach my $type (@emailtypes) {
                     80:                     $email = $userinfo{$type};
                     81:                     if ($email =~ /[^\@]+\@[^\@]+/) {
                     82:                         $emailtarget = $type; 
                     83:                         last;
                     84:                     }
                     85:                 }
                     86:                 if ($email =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                     87:                     if ($useremail eq $email) {
                     88:                         $output = &send_token($uname,$udom,$email,$server,
                     89:                                               $domdesc,$contact_name,
                     90:                                               $contact_email);
                     91:                     } else {
                     92:                         $output = &invalid_state('mismatch',$domdesc,
                     93:                                                  $contact_name,
                     94:                                                  $contact_email);
                     95:                     }
                     96:                 } else {
                     97:                     $output = &invalid_state('missing',$domdesc,
                     98:                                              $contact_name,$contact_email);
1.1       raeburn    99:                 }
                    100:             }
                    101:         } elsif ($authtype =~ /^(krb|unix|local)/) { 
                    102:             $output = &invalid_state('authentication',$domdesc,
                    103:                                      $contact_name,$contact_email);
                    104:         } else {
                    105:             $output = &invalid_state('invalid',$domdesc,
                    106:                                      $contact_name,$contact_email);
                    107:         }
                    108:     } else {
                    109:         $output = &get_uname($defdom);
                    110:     }
                    111:     $r->print($output);
                    112:     $r->print(&Apache::loncommon::end_page());
                    113:     return OK;
                    114: }
                    115: 
                    116: sub get_uname {
                    117:     my ($defdom) = @_;
                    118:     my %lt = &Apache::lonlocal::texthash(
                    119:                                          unam => 'username',
                    120:                                          udom => 'domain',
1.12    ! schafran  121:                                          uemail => 'E-mail address in LON-CAPA',
1.1       raeburn   122:                                          proc => 'Proceed');
                    123: 
                    124:     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    125:     $msg .= '<br /><br />'.&mt('Three conditions must be met:')
                    126:            .'<ul><li>'.&mt('An e-mail address must have previously been associated with your LON-CAPA username.').'</li>'
                    127:            .'<li>'.&mt('You must be able to access e-mail sent to that address.').'</li>'
                    128:            .'<li>'.&mt('Your LON-CAPA account must be of a type for which LON-CAPA can reset a password.')
                    129:            .'</ul>';
1.1       raeburn   130:     $msg .= qq|
                    131: <form name="forgotpw" method="post">
                    132: <table>
                    133: <tr><td>
                    134: <tr><td align="left">LON-CAPA $lt{'unam'}:                      </td>
1.5       raeburn   135:     <td><input type="text" name="uname" size="15"  /> </td></tr>
1.1       raeburn   136: <tr><td align="left">LON-CAPA $lt{'udom'}:                      </td>
                    137:     <td>|;
                    138:     $msg .= &Apache::loncommon::select_dom_form($defdom,'udom');
                    139:     $msg .= qq|</td></tr>
1.3       raeburn   140: <tr><td align="left">$lt{'uemail'}:                             </td>
1.5       raeburn   141:     <td><input type="text" name="useremail" size="30"  /></td></tr>
1.1       raeburn   142: <tr><td colspan="2" align="left"><br />
                    143:     <input type="button" value="$lt{'proc'}" onClick="document.forgotpw.submit()"></td></tr>
                    144: </table>
1.2       albertel  145: </form>
1.1       raeburn   146: |;
                    147:     return $msg;
                    148: }
                    149: 
                    150: sub send_token {
                    151:     my ($uname,$udom,$email,$server,$domdesc,$contact_name,
                    152:         $contact_email) = @_;
1.11      bisitz    153:     my $msg = &mt('Thank you for your request to reset the password for your LON-CAPA account.').'<br /><br />';
1.1       raeburn   154: 
                    155:     my $now = time;
                    156:     my $temppasswd = &create_passwd();
1.2       albertel  157:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    158: 		'time'       => $now,
                    159: 		'domain'     => $udom,
                    160: 		'username'   => $uname,
                    161: 		'email'      => $email,
                    162: 		'temppasswd' => $temppasswd);
1.1       raeburn   163: 
1.3       raeburn   164:     my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
                    165:     if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2       albertel  166:         my $esc_token = &escape($token);
1.9       raeburn   167:         my $mailmsg = "A request was submitted on ".&Apache::lonlocal::locallocaltime(time)." for a reset of the ".
1.1       raeburn   168:              "password for your LON-CAPA account.".
                    169:              "To complete this process please open a web browser and enter the following ".
                    170:              "URL in the address/location box: ".&Apache::lonnet::absolute_url()."/adm/resetpw?token=$esc_token";
                    171:         my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
                    172:                                 $contact_email);
                    173:         if ($result eq 'ok') {
1.3       raeburn   174:             $msg .= &mt("An e-mail message 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   175:         } else {
1.7       albertel  176:             $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   177:         }
                    178:     } else {
1.4       albertel  179:         $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   180:     }
                    181:     return $msg;
                    182: }
                    183: 
                    184: sub send_mail {
                    185:     my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
                    186:     my $outcome;
                    187:     my $requestmail = "To: $email\n".
                    188:                       "From: $contact_name <$contact_email>\n".
                    189:                       "Subject: ".&mt('Your LON-CAPA account')."\n".
                    190:                       "\n\n".$mailmsg."\n\n".
                    191:                       &mt('[_1] LON-CAPA support team',$domdesc)."\n".
                    192:                       "$contact_email\n";
                    193:     if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
                    194:         print MAIL $requestmail;
                    195:         close(MAIL);
                    196:         $outcome = 'ok';
                    197:     } else {
                    198:         $outcome = 'fail';
                    199:     }
                    200:     return $outcome;
                    201: }
                    202: 
                    203: sub invalid_state {
                    204:     my ($error,$domdesc,$contact_name,$contact_email) = @_;
                    205:     my $msg;
                    206:     if ($error eq 'invalid') {
1.8       bisitz    207:         $msg = &mt('The username you provided was not verified as a valid username in the LON-CAPA system for the [_1] domain.',$domdesc)
                    208:               .' '.&mt('Please [_1]go back[_2] and try again.','<a href="javascript:history.go(-1)"><u>','</u></a>');
1.1       raeburn   209:     } else {
1.3       raeburn   210:         if ($error eq 'baduseremail') {
                    211:             $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
                    212:         } elsif ($error eq 'mismatch') {
                    213:             $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.');  
                    214:         } elsif ($error eq 'missing') {
1.1       raeburn   215:             $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
                    216:         } elsif ($error eq 'authentication') {
                    217:             $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
                    218:         }
                    219:         if ($contact_email ne '') {
                    220:             my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
1.8       bisitz    221:             $msg .= '<br /> '.&mt('You may wish to contact the [_1]LON-CAPA helpdesk[_2] for the [_3] domain.'
                    222:                                  ,'<a href="/adm/helpdesk?origurl='.$escuri.'">','</a>',$domdesc);
1.1       raeburn   223:         } else {
1.8       bisitz    224:             $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   225:         }
                    226:     }
                    227:     return $msg;
                    228: }
                    229: 
                    230: sub reset_passwd {
                    231:     my ($r,$token,$contact_name,$contact_email) = @_;
                    232:     my $msg;
                    233:     my %data = &Apache::lonnet::tmpget($token);
                    234:     my $now = time;
                    235:     if (keys(%data) == 0) {
                    236:         $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 <a href="/adm/resetpw">new request</a> 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.');
                    237:         return $msg;
                    238:     }
                    239:     if (($data{'time'} =~ /^\d+$/) && 
                    240:         ($data{'username'} ne '') && 
                    241:         ($data{'domain'} ne '') && 
1.3       raeburn   242:         ($data{'email'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && 
1.1       raeburn   243:         ($data{'temppasswd'} =~/^\w+$/)) {
1.9       raeburn   244:         my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.1       raeburn   245:         if ($now - $data{'time'} < 7200) {
                    246:             if ($env{'form.action'} eq 'verify_and_change_pass') {
                    247:                 my $change_failed = 
1.2       albertel  248: 		    &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1       raeburn   249:                 if (!$change_failed) {
                    250:                     my $delete = &Apache::lonnet::tmpdel($token);
1.9       raeburn   251:                     my $now = &Apache::lonlocal::locallocaltime(time);
1.1       raeburn   252:                     my $domdesc = 
1.6       albertel  253: 			&Apache::lonnet::domain($data{'domain'},'description');
1.1       raeburn   254:                     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";
                    255:                     my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
                    256:                                             $contact_name,$contact_email);
                    257:                     if ($result eq 'ok') {
                    258:                         $msg .= &mt('An e-mail confirming setting of the password for your LON-CAPA account has been sent to [_1].',$data{'email'});
                    259:                     } else {
                    260:                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming setting of your new password.',$data{'email'});
                    261:                     }
1.4       albertel  262:                     $msg .= '<br /><br />'.&mt('<a href="/adm/login">Go to the login page</a>.');
1.1       raeburn   263:                 } else {
                    264:                     $msg .= &mt('A problem occurred when attempting to reset the password for your account.  Please contact the [_1] - (<a href="mailto:[_2]">[_2]</a>) for assistance.',$contact_name,$contact_email);
                    265:                 }
                    266:             } else {
                    267:                 $r->print(&mt('The token included in an email sent to you [_1] has been verified, so you may now proceed to reset the password for your LON-CAPA account.',$reqtime).'<br /><br />');
                    268:                 $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 />');
                    269:                 &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
                    270:             }
                    271:         } else {
                    272:             $msg = &mt('Sorry, the token generated when you requested a password reset has expired. Please submit a <a href="/adm/resetpw">new request</a>, 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.');
                    273:         }
                    274:     } else {
                    275:         $msg .= &mt('Sorry, the URL generated when you requested reset of your password contained incomplete information. Please submit a <a href="/adm/resetpw">new request</a> for a password reset, and use the new URL that will be sent to your e-mail account to complete the process.');
                    276:     }
                    277:     return $msg;
                    278: }
                    279: 
                    280: sub create_passwd {
                    281:     my $passwd = '';
1.2       albertel  282:     my @letts = ("a".."z");
1.1       raeburn   283:     for (my $i=0; $i<8; $i++) {
1.2       albertel  284:         my $lettnum = int(rand(2));
1.1       raeburn   285:         my $item = '';
                    286:         if ($lettnum) {
1.2       albertel  287:             $item = $letts[int(rand(26))];
                    288:             my $uppercase = int(rand(2));
1.1       raeburn   289:             if ($uppercase) {
                    290:                 $item =~ tr/a-z/A-Z/;
                    291:             }
                    292:         } else {
1.2       albertel  293:             $item = int(rand(10));
1.1       raeburn   294:         }
                    295:         $passwd .= $item;
                    296:     }
                    297:     return ($passwd);
                    298: }
                    299: 
                    300: 1;

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