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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow access to password changing via a token sent to user's e-mail. 
                      3: #
1.17.10.1! gci         4: # $Id: resetpw.pm,v 1.17 2009/04/29 16:57:29 bisitz 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: #
                     29: package Apache::resetpw;
                     30: 
                     31: use strict;
                     32: use Apache::Constants qw(:common);
                     33: use Apache::lonacc;
                     34: use Apache::lonnet;
                     35: use Apache::loncommon;
                     36: use Apache::lonlocal;
                     37: use LONCAPA;
                     38: 
                     39: sub handler {
                     40:     my $r = shift;
                     41:     &Apache::loncommon::content_type($r,'text/html');
                     42:     $r->send_http_header;
                     43:     if ($r->header_only) {
                     44:         return OK;
                     45:     }
                     46:     my $contact_name = &mt('LON-CAPA helpdesk');
                     47:     my $contact_email =  $r->dir_config('lonSupportEMail');
                     48:     my $server = $r->dir_config('lonHostID');
                     49:     my $defdom = $r->dir_config('lonDefDomain');
                     50:     &Apache::lonacc::get_posted_cgi($r);
                     51:     &Apache::lonlocal::get_language_handle($r);
                     52:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
                     53:     
                     54:     my @emailtypes = ('permanentemail','critnotification','notification');
1.2       albertel   55:     my $uname = &unescape($env{'form.uname'});
1.1       raeburn    56:     my $udom = $env{'form.udom'};
                     57:     my $token = $env{'form.token'};
1.10      raeburn    58:     my $start_page =
                     59:         &Apache::loncommon::start_page('Reset password','',
                     60:                                            {
                     61:                                              'no_inline_link'   => 1,});
                     62:     $r->print($start_page);
1.11      bisitz     63:     $r->print('<h3>'.&mt('Reset forgotten LON-CAPA password').'</h3>');
1.1       raeburn    64:     my $output;
                     65:     if ($token) {
                     66:         $output = &reset_passwd($r,$token,$contact_name,$contact_email);
                     67:     } elsif ($uname && $udom) {
1.6       albertel   68:         my $domdesc = &Apache::lonnet::domain($udom,'description');
1.1       raeburn    69:         my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
                     70:         if ($authtype =~ /^internal/) {
1.3       raeburn    71:             my $useremail = $env{'form.useremail'};
                     72:             if ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                     73:                 $output = &invalid_state('baduseremail',$domdesc,
                     74:                                          $contact_name,$contact_email);
                     75:             } else {
                     76:                 my %userinfo = 
1.4       albertel   77: 		    &Apache::lonnet::get('environment',\@emailtypes,
                     78: 					 $udom,$uname);
1.3       raeburn    79:                 my $email = '';
                     80:                 my $emailtarget;
                     81:                 foreach my $type (@emailtypes) {
                     82:                     $email = $userinfo{$type};
                     83:                     if ($email =~ /[^\@]+\@[^\@]+/) {
                     84:                         $emailtarget = $type; 
                     85:                         last;
                     86:                     }
                     87:                 }
                     88:                 if ($email =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                     89:                     if ($useremail eq $email) {
                     90:                         $output = &send_token($uname,$udom,$email,$server,
                     91:                                               $domdesc,$contact_name,
                     92:                                               $contact_email);
                     93:                     } else {
                     94:                         $output = &invalid_state('mismatch',$domdesc,
                     95:                                                  $contact_name,
                     96:                                                  $contact_email);
                     97:                     }
                     98:                 } else {
                     99:                     $output = &invalid_state('missing',$domdesc,
                    100:                                              $contact_name,$contact_email);
1.1       raeburn   101:                 }
                    102:             }
                    103:         } elsif ($authtype =~ /^(krb|unix|local)/) { 
                    104:             $output = &invalid_state('authentication',$domdesc,
                    105:                                      $contact_name,$contact_email);
                    106:         } else {
                    107:             $output = &invalid_state('invalid',$domdesc,
                    108:                                      $contact_name,$contact_email);
                    109:         }
                    110:     } else {
                    111:         $output = &get_uname($defdom);
                    112:     }
                    113:     $r->print($output);
                    114:     $r->print(&Apache::loncommon::end_page());
                    115:     return OK;
                    116: }
                    117: 
                    118: sub get_uname {
                    119:     my ($defdom) = @_;
                    120:     my %lt = &Apache::lonlocal::texthash(
                    121:                                          unam => 'username',
                    122:                                          udom => 'domain',
1.12      schafran  123:                                          uemail => 'E-mail address in LON-CAPA',
1.1       raeburn   124:                                          proc => 'Proceed');
                    125: 
                    126:     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    127:     $msg .= '<br /><br />'.&mt('Three conditions must be met:')
                    128:            .'<ul><li>'.&mt('An e-mail address must have previously been associated with your LON-CAPA username.').'</li>'
                    129:            .'<li>'.&mt('You must be able to access e-mail sent to that address.').'</li>'
                    130:            .'<li>'.&mt('Your LON-CAPA account must be of a type for which LON-CAPA can reset a password.')
                    131:            .'</ul>';
1.1       raeburn   132:     $msg .= qq|
                    133: <form name="forgotpw" method="post">
                    134: <table>
                    135: <tr><td>
1.17.10.1! gci       136: <tr><td align="left">GCI WebCenter $lt{'unam'}:                      </td>
1.16      bisitz    137:     <td><input type="text" name="uname" size="15" /></td></tr>
1.17.10.1! gci       138: <tr><td align="left">GCI WebCenter $lt{'udom'}:                      </td>
1.1       raeburn   139:     <td>|;
                    140:     $msg .= &Apache::loncommon::select_dom_form($defdom,'udom');
                    141:     $msg .= qq|</td></tr>
1.3       raeburn   142: <tr><td align="left">$lt{'uemail'}:                             </td>
1.16      bisitz    143:     <td><input type="text" name="useremail" size="30" /></td></tr>
1.1       raeburn   144: <tr><td colspan="2" align="left"><br />
1.16      bisitz    145:     <input type="submit" value="$lt{'proc'}" /></td></tr>
1.1       raeburn   146: </table>
1.2       albertel  147: </form>
1.1       raeburn   148: |;
                    149:     return $msg;
                    150: }
                    151: 
                    152: sub send_token {
                    153:     my ($uname,$udom,$email,$server,$domdesc,$contact_name,
                    154:         $contact_email) = @_;
1.11      bisitz    155:     my $msg = &mt('Thank you for your request to reset the password for your LON-CAPA account.').'<br /><br />';
1.1       raeburn   156: 
                    157:     my $now = time;
                    158:     my $temppasswd = &create_passwd();
1.2       albertel  159:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    160: 		'time'       => $now,
                    161: 		'domain'     => $udom,
                    162: 		'username'   => $uname,
                    163: 		'email'      => $email,
                    164: 		'temppasswd' => $temppasswd);
1.1       raeburn   165: 
1.3       raeburn   166:     my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
                    167:     if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2       albertel  168:         my $esc_token = &escape($token);
1.15      raeburn   169:         my $showtime = &Apache::lonlocal::locallocaltime(time);
                    170:         my $reseturl = &Apache::lonnet::absolute_url().'/adm/resetpw?token='.$esc_token;
                    171:         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   172:         my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
                    173:                                 $contact_email);
                    174:         if ($result eq 'ok') {
1.13      schafran  175:             $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   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) {
1.17      bisitz    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 [_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.'
                    238:                   ,'<a href="/adm/resetpw">','</a>');
1.1       raeburn   239:         return $msg;
                    240:     }
                    241:     if (($data{'time'} =~ /^\d+$/) && 
                    242:         ($data{'username'} ne '') && 
                    243:         ($data{'domain'} ne '') && 
1.3       raeburn   244:         ($data{'email'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && 
1.1       raeburn   245:         ($data{'temppasswd'} =~/^\w+$/)) {
1.9       raeburn   246:         my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.1       raeburn   247:         if ($now - $data{'time'} < 7200) {
                    248:             if ($env{'form.action'} eq 'verify_and_change_pass') {
                    249:                 my $change_failed = 
1.2       albertel  250: 		    &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1       raeburn   251:                 if (!$change_failed) {
                    252:                     my $delete = &Apache::lonnet::tmpdel($token);
1.9       raeburn   253:                     my $now = &Apache::lonlocal::locallocaltime(time);
1.1       raeburn   254:                     my $domdesc = 
1.6       albertel  255: 			&Apache::lonnet::domain($data{'domain'},'description');
1.1       raeburn   256:                     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";
                    257:                     my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
                    258:                                             $contact_name,$contact_email);
                    259:                     if ($result eq 'ok') {
                    260:                         $msg .= &mt('An e-mail confirming setting of the password for your LON-CAPA account has been sent to [_1].',$data{'email'});
                    261:                     } else {
                    262:                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming setting of your new password.',$data{'email'});
                    263:                     }
1.17      bisitz    264:                     $msg .= '<br /><br />'
                    265:                            .'<a href="/adm/login">'.&mt('Go to the login page').'</a>.';
1.1       raeburn   266:                 } else {
1.17      bisitz    267:                     $msg .= &mt('A problem occurred when attempting to reset the password for your account. Please contact the [_1] - ([_2]) for assistance.'
                    268:                                ,$contact_name
                    269:                                ,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>');
1.1       raeburn   270:                 }
                    271:             } else {
1.13      schafran  272:                 $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   273:                 $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 />');
                    274:                 &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
                    275:             }
                    276:         } else {
1.17      bisitz    277:             $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.'
                    278:                       ,'<a href="/adm/resetpw">','</a>');
1.1       raeburn   279:         }
                    280:     } else {
1.17      bisitz    281:         $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.'
                    282:                    ,'<a href="/adm/resetpw">','</a>');
1.1       raeburn   283:     }
                    284:     return $msg;
                    285: }
                    286: 
                    287: sub create_passwd {
                    288:     my $passwd = '';
1.2       albertel  289:     my @letts = ("a".."z");
1.1       raeburn   290:     for (my $i=0; $i<8; $i++) {
1.2       albertel  291:         my $lettnum = int(rand(2));
1.1       raeburn   292:         my $item = '';
                    293:         if ($lettnum) {
1.2       albertel  294:             $item = $letts[int(rand(26))];
                    295:             my $uppercase = int(rand(2));
1.1       raeburn   296:             if ($uppercase) {
                    297:                 $item =~ tr/a-z/A-Z/;
                    298:             }
                    299:         } else {
1.2       albertel  300:             $item = int(rand(10));
1.1       raeburn   301:         }
                    302:         $passwd .= $item;
                    303:     }
                    304:     return ($passwd);
                    305: }
                    306: 
                    307: 1;

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