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

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 $start_page =
                     45:         &Apache::loncommon::start_page('Reset password','',
                     46:                                            {
                     47:                                              'no_inline_link'   => 1,});
                     48:     $r->print($start_page);
                     49:     my $contact_name = &mt('LON-CAPA helpdesk');
                     50:     my $contact_email =  $r->dir_config('lonSupportEMail');
                     51:     my $server = $r->dir_config('lonHostID');
                     52:     my $defdom = $r->dir_config('lonDefDomain');
                     53:     &Apache::lonacc::get_posted_cgi($r);
                     54:     &Apache::lonlocal::get_language_handle($r);
                     55:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
                     56:     
                     57:     my @emailtypes = ('permanentemail','critnotification','notification');
1.2       albertel   58:     my $uname = &unescape($env{'form.uname'});
1.1       raeburn    59:     my $udom = $env{'form.udom'};
                     60:     my $token = $env{'form.token'};
1.8       bisitz     61:     $r->print(&mt('<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.3       raeburn   121:                                          uemail => 'Email 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) = @_;
                    153:     my $msg = &mt('Thank you for your request to reset the password for your
                    154:         LON-CAPA account.').'<br /><br />';
                    155: 
                    156:     my $now = time;
                    157:     my $temppasswd = &create_passwd();
1.2       albertel  158:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    159: 		'time'       => $now,
                    160: 		'domain'     => $udom,
                    161: 		'username'   => $uname,
                    162: 		'email'      => $email,
                    163: 		'temppasswd' => $temppasswd);
1.1       raeburn   164: 
1.3       raeburn   165:     my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
                    166:     if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2       albertel  167:         my $esc_token = &escape($token);
1.9     ! raeburn   168:         my $mailmsg = "A request was submitted on ".&Apache::lonlocal::locallocaltime(time)." for a reset of the ".
1.1       raeburn   169:              "password for your LON-CAPA account.".
                    170:              "To complete this process please open a web browser and enter the following ".
                    171:              "URL in the address/location box: ".&Apache::lonnet::absolute_url()."/adm/resetpw?token=$esc_token";
                    172:         my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
                    173:                                 $contact_email);
                    174:         if ($result eq 'ok') {
1.3       raeburn   175:             $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   176:         } else {
1.7       albertel  177:             $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   178:         }
                    179:     } else {
1.4       albertel  180:         $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   181:     }
                    182:     return $msg;
                    183: }
                    184: 
                    185: sub send_mail {
                    186:     my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
                    187:     my $outcome;
                    188:     my $requestmail = "To: $email\n".
                    189:                       "From: $contact_name <$contact_email>\n".
                    190:                       "Subject: ".&mt('Your LON-CAPA account')."\n".
                    191:                       "\n\n".$mailmsg."\n\n".
                    192:                       &mt('[_1] LON-CAPA support team',$domdesc)."\n".
                    193:                       "$contact_email\n";
                    194:     if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
                    195:         print MAIL $requestmail;
                    196:         close(MAIL);
                    197:         $outcome = 'ok';
                    198:     } else {
                    199:         $outcome = 'fail';
                    200:     }
                    201:     return $outcome;
                    202: }
                    203: 
                    204: sub invalid_state {
                    205:     my ($error,$domdesc,$contact_name,$contact_email) = @_;
                    206:     my $msg;
                    207:     if ($error eq 'invalid') {
1.8       bisitz    208:         $msg = &mt('The username you provided was not verified as a valid username in the LON-CAPA system for the [_1] domain.',$domdesc)
                    209:               .' '.&mt('Please [_1]go back[_2] and try again.','<a href="javascript:history.go(-1)"><u>','</u></a>');
1.1       raeburn   210:     } else {
1.3       raeburn   211:         if ($error eq 'baduseremail') {
                    212:             $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
                    213:         } elsif ($error eq 'mismatch') {
                    214:             $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.');  
                    215:         } elsif ($error eq 'missing') {
1.1       raeburn   216:             $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
                    217:         } elsif ($error eq 'authentication') {
                    218:             $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
                    219:         }
                    220:         if ($contact_email ne '') {
                    221:             my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
1.8       bisitz    222:             $msg .= '<br /> '.&mt('You may wish to contact the [_1]LON-CAPA helpdesk[_2] for the [_3] domain.'
                    223:                                  ,'<a href="/adm/helpdesk?origurl='.$escuri.'">','</a>',$domdesc);
1.1       raeburn   224:         } else {
1.8       bisitz    225:             $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   226:         }
                    227:     }
                    228:     return $msg;
                    229: }
                    230: 
                    231: sub reset_passwd {
                    232:     my ($r,$token,$contact_name,$contact_email) = @_;
                    233:     my $msg;
                    234:     my %data = &Apache::lonnet::tmpget($token);
                    235:     my $now = time;
                    236:     if (keys(%data) == 0) {
                    237:         $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.');
                    238:         return $msg;
                    239:     }
                    240:     if (($data{'time'} =~ /^\d+$/) && 
                    241:         ($data{'username'} ne '') && 
                    242:         ($data{'domain'} ne '') && 
1.3       raeburn   243:         ($data{'email'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && 
1.1       raeburn   244:         ($data{'temppasswd'} =~/^\w+$/)) {
1.9     ! raeburn   245:         my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.1       raeburn   246:         if ($now - $data{'time'} < 7200) {
                    247:             if ($env{'form.action'} eq 'verify_and_change_pass') {
                    248:                 my $change_failed = 
1.2       albertel  249: 		    &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1       raeburn   250:                 if (!$change_failed) {
                    251:                     my $delete = &Apache::lonnet::tmpdel($token);
1.9     ! raeburn   252:                     my $now = &Apache::lonlocal::locallocaltime(time);
1.1       raeburn   253:                     my $domdesc = 
1.6       albertel  254: 			&Apache::lonnet::domain($data{'domain'},'description');
1.1       raeburn   255:                     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";
                    256:                     my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
                    257:                                             $contact_name,$contact_email);
                    258:                     if ($result eq 'ok') {
                    259:                         $msg .= &mt('An e-mail confirming setting of the password for your LON-CAPA account has been sent to [_1].',$data{'email'});
                    260:                     } else {
                    261:                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming setting of your new password.',$data{'email'});
                    262:                     }
1.4       albertel  263:                     $msg .= '<br /><br />'.&mt('<a href="/adm/login">Go to the login page</a>.');
1.1       raeburn   264:                 } else {
                    265:                     $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);
                    266:                 }
                    267:             } else {
                    268:                 $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 />');
                    269:                 $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 />');
                    270:                 &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
                    271:             }
                    272:         } else {
                    273:             $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.');
                    274:         }
                    275:     } else {
                    276:         $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.');
                    277:     }
                    278:     return $msg;
                    279: }
                    280: 
                    281: sub create_passwd {
                    282:     my $passwd = '';
1.2       albertel  283:     my @letts = ("a".."z");
1.1       raeburn   284:     for (my $i=0; $i<8; $i++) {
1.2       albertel  285:         my $lettnum = int(rand(2));
1.1       raeburn   286:         my $item = '';
                    287:         if ($lettnum) {
1.2       albertel  288:             $item = $letts[int(rand(26))];
                    289:             my $uppercase = int(rand(2));
1.1       raeburn   290:             if ($uppercase) {
                    291:                 $item =~ tr/a-z/A-Z/;
                    292:             }
                    293:         } else {
1.2       albertel  294:             $item = int(rand(10));
1.1       raeburn   295:         }
                    296:         $passwd .= $item;
                    297:     }
                    298:     return ($passwd);
                    299: }
                    300: 
                    301: 1;

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