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

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'};
                     61:     $r->print(&mt('<h3>Reset forgotten LON-CAPA password</h3>'));
                     62:     my $output;
                     63:     if ($token) {
                     64:         $output = &reset_passwd($r,$token,$contact_name,$contact_email);
                     65:     } elsif ($uname && $udom) {
                     66:         my $domdesc = $Apache::lonnet::domaindescription{$udom};
                     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.1       raeburn    75:             &Apache::lonnet::get('environment',\@emailtypes,$udom,$uname);
1.3     ! raeburn    76:                 my $email = '';
        !            77:                 my $emailtarget;
        !            78:                 foreach my $type (@emailtypes) {
        !            79:                     $email = $userinfo{$type};
        !            80:                     if ($email =~ /[^\@]+\@[^\@]+/) {
        !            81:                         $emailtarget = $type; 
        !            82:                         last;
        !            83:                     }
        !            84:                 }
        !            85:                 if ($email =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
        !            86:                     if ($useremail eq $email) {
        !            87:                         $output = &send_token($uname,$udom,$email,$server,
        !            88:                                               $domdesc,$contact_name,
        !            89:                                               $contact_email);
        !            90:                     } else {
        !            91:                         $output = &invalid_state('mismatch',$domdesc,
        !            92:                                                  $contact_name,
        !            93:                                                  $contact_email);
        !            94:                     }
        !            95:                 } else {
        !            96:                     $output = &invalid_state('missing',$domdesc,
        !            97:                                              $contact_name,$contact_email);
1.1       raeburn    98:                 }
                     99:             }
                    100:         } elsif ($authtype =~ /^(krb|unix|local)/) { 
                    101:             $output = &invalid_state('authentication',$domdesc,
                    102:                                      $contact_name,$contact_email);
                    103:         } else {
                    104:             $output = &invalid_state('invalid',$domdesc,
                    105:                                      $contact_name,$contact_email);
                    106:         }
                    107:     } else {
                    108:         $output = &get_uname($defdom);
                    109:     }
                    110:     $r->print($output);
                    111:     $r->print(&Apache::loncommon::end_page());
                    112:     return OK;
                    113: }
                    114: 
                    115: sub get_uname {
                    116:     my ($defdom) = @_;
                    117:     my %lt = &Apache::lonlocal::texthash(
                    118:                                          unam => 'username',
                    119:                                          udom => 'domain',
1.3     ! raeburn   120:                                          uemail => 'Email address in LON-CAPA',
1.1       raeburn   121:                                          proc => 'Proceed');
                    122: 
                    123:     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.');
                    124:     $msg .= '<br /><br />'.&mt('Three conditions must be met:<ul><li>An e-mail address must have previously been associated with your LON-CAPA username.</li><li>You must be able to access e-mail sent to that address.</li><li>Your account must be of a type for which LON-CAPA can reset a password.</ul>');
                    125:     $msg .= qq|
                    126: <form name="forgotpw" method="post">
                    127: <table>
                    128: <tr><td>
                    129: <tr><td align="left">LON-CAPA $lt{'unam'}:                      </td>
                    130:     <td><input type="text" name="uname" size="10"  /> </td></tr>
                    131: <tr><td align="left">LON-CAPA $lt{'udom'}:                      </td>
                    132:     <td>|;
                    133:     $msg .= &Apache::loncommon::select_dom_form($defdom,'udom');
                    134:     $msg .= qq|</td></tr>
1.3     ! raeburn   135: <tr><td align="left">$lt{'uemail'}:                             </td>
        !           136:     <td><input type="text" name="useremail" size="20"  /></td></tr>
1.1       raeburn   137: <tr><td colspan="2" align="left"><br />
                    138:     <input type="button" value="$lt{'proc'}" onClick="document.forgotpw.submit()"></td></tr>
                    139: </table>
1.2       albertel  140: </form>
1.1       raeburn   141: |;
                    142:     return $msg;
                    143: }
                    144: 
                    145: sub send_token {
                    146:     my ($uname,$udom,$email,$server,$domdesc,$contact_name,
                    147:         $contact_email) = @_;
                    148:     my $msg = &mt('Thank you for your request to reset the password for your
                    149:         LON-CAPA account.').'<br /><br />';
                    150: 
                    151:     my $now = time;
                    152:     my $temppasswd = &create_passwd();
1.2       albertel  153:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    154: 		'time'       => $now,
                    155: 		'domain'     => $udom,
                    156: 		'username'   => $uname,
                    157: 		'email'      => $email,
                    158: 		'temppasswd' => $temppasswd);
1.1       raeburn   159: 
1.3     ! raeburn   160:     my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
        !           161:     if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2       albertel  162:         my $esc_token = &escape($token);
1.1       raeburn   163:         my $mailmsg = "A request was submitted on ".localtime(time)." for a reset of the ".
                    164:              "password for your LON-CAPA account.".
                    165:              "To complete this process please open a web browser and enter the following ".
                    166:              "URL in the address/location box: ".&Apache::lonnet::absolute_url()."/adm/resetpw?token=$esc_token";
                    167:         my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
                    168:                                 $contact_email);
                    169:         if ($result eq 'ok') {
1.3     ! raeburn   170:             $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   171:         } else {
1.3     ! raeburn   172:             $msg .= ("An error occurred when sending a message to the e-mail address associated with your LON-CAPA account. Please contact the $contact_name ($contact_email) for assistance.");
1.1       raeburn   173:         }
                    174:     } else {
                    175:         $msg .= &mt("An error occurred creating a token required for the password reset process. Please contact the $contact_name ($contact_email) for assistance.");
                    176:     }
                    177:     return $msg;
                    178: }
                    179: 
                    180: sub send_mail {
                    181:     my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
                    182:     my $outcome;
                    183:     my $requestmail = "To: $email\n".
                    184:                       "From: $contact_name <$contact_email>\n".
                    185:                       "Subject: ".&mt('Your LON-CAPA account')."\n".
                    186:                       "\n\n".$mailmsg."\n\n".
                    187:                       &mt('[_1] LON-CAPA support team',$domdesc)."\n".
                    188:                       "$contact_email\n";
                    189:     if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
                    190:         print MAIL $requestmail;
                    191:         close(MAIL);
                    192:         $outcome = 'ok';
                    193:     } else {
                    194:         $outcome = 'fail';
                    195:     }
                    196:     return $outcome;
                    197: }
                    198: 
                    199: sub invalid_state {
                    200:     my ($error,$domdesc,$contact_name,$contact_email) = @_;
                    201:     my $msg;
                    202:     if ($error eq 'invalid') {
                    203:         $msg = &mt('The username you provided was not verified as a valid username in the LON-CAPA system for the [_1] domain.',$domdesc).&mt(' Please <a href="javascript:history.go(-1)"><u>go back</u></a> and try again.');
                    204:     } else {
1.3     ! raeburn   205:         if ($error eq 'baduseremail') {
        !           206:             $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
        !           207:         } elsif ($error eq 'mismatch') {
        !           208:             $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.');  
        !           209:         } elsif ($error eq 'missing') {
1.1       raeburn   210:             $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
                    211:         } elsif ($error eq 'authentication') {
                    212:             $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
                    213:         }
                    214:         if ($contact_email ne '') {
                    215:             my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
1.3     ! raeburn   216:             $msg .= '<br />'.&mt(' You may wish to contact the <a href="/adm/helpdesk?origurl=[_1]">LON-CAPA helpdesk</a> for the [_2] domain.',$escuri,$domdesc);
1.1       raeburn   217:         } else {
1.3     ! raeburn   218:             $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   219:         }
                    220:     }
                    221:     return $msg;
                    222: }
                    223: 
                    224: sub reset_passwd {
                    225:     my ($r,$token,$contact_name,$contact_email) = @_;
                    226:     my $msg;
                    227:     my %data = &Apache::lonnet::tmpget($token);
                    228:     my $now = time;
                    229:     if (keys(%data) == 0) {
                    230:         $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.');
                    231:         return $msg;
                    232:     }
                    233:     if (($data{'time'} =~ /^\d+$/) && 
                    234:         ($data{'username'} ne '') && 
                    235:         ($data{'domain'} ne '') && 
1.3     ! raeburn   236:         ($data{'email'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && 
1.1       raeburn   237:         ($data{'temppasswd'} =~/^\w+$/)) {
                    238:         my $reqtime = localtime($data{'time'});
                    239:         if ($now - $data{'time'} < 7200) {
                    240:             if ($env{'form.action'} eq 'verify_and_change_pass') {
                    241:                 my $change_failed = 
1.2       albertel  242: 		    &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1       raeburn   243:                 if (!$change_failed) {
                    244:                     my $delete = &Apache::lonnet::tmpdel($token);
                    245:                     my $now = localtime(time);
                    246:                     my $domdesc = 
                    247:                         $Apache::lonnet::domaindescription{$data{'domain'}};
                    248:                     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";
                    249:                     my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
                    250:                                             $contact_name,$contact_email);
                    251:                     if ($result eq 'ok') {
                    252:                         $msg .= &mt('An e-mail confirming setting of the password for your LON-CAPA account has been sent to [_1].',$data{'email'});
                    253:                     } else {
                    254:                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming setting of your new password.',$data{'email'});
                    255:                     }
                    256:                     $msg .= '<br /<br />'.&mt('<a href="/adm/login">Go to the login page</a>.');
                    257:                 } else {
                    258:                     $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);
                    259:                 }
                    260:             } else {
                    261:                 $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 />');
                    262:                 $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 />');
                    263:                 &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
                    264:             }
                    265:         } else {
                    266:             $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.');
                    267:         }
                    268:     } else {
                    269:         $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.');
                    270:     }
                    271:     return $msg;
                    272: }
                    273: 
                    274: sub create_passwd {
                    275:     my $passwd = '';
1.2       albertel  276:     my @letts = ("a".."z");
1.1       raeburn   277:     for (my $i=0; $i<8; $i++) {
1.2       albertel  278:         my $lettnum = int(rand(2));
1.1       raeburn   279:         my $item = '';
                    280:         if ($lettnum) {
1.2       albertel  281:             $item = $letts[int(rand(26))];
                    282:             my $uppercase = int(rand(2));
1.1       raeburn   283:             if ($uppercase) {
                    284:                 $item =~ tr/a-z/A-Z/;
                    285:             }
                    286:         } else {
1.2       albertel  287:             $item = int(rand(10));
1.1       raeburn   288:         }
                    289:         $passwd .= $item;
                    290:     }
                    291:     return ($passwd);
                    292: }
                    293: 
                    294: 1;

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