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

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 lib '/home/httpd/lib/perl/';
        !            36: use LONCAPA;
        !            37: 
        !            38: sub handler {
        !            39:     my $r = shift;
        !            40:     &Apache::loncommon::content_type($r,'text/html');
        !            41:     $r->send_http_header;
        !            42:     if ($r->header_only) {
        !            43:         return OK;
        !            44:     }
        !            45:     my $start_page =
        !            46:         &Apache::loncommon::start_page('Reset password','',
        !            47:                                            {
        !            48:                                              'no_inline_link'   => 1,});
        !            49:     $r->print($start_page);
        !            50:     my $contact_name = &mt('LON-CAPA helpdesk');
        !            51:     my $contact_email =  $r->dir_config('lonSupportEMail');
        !            52:     my $server = $r->dir_config('lonHostID');
        !            53:     my $defdom = $r->dir_config('lonDefDomain');
        !            54:     &Apache::lonacc::get_posted_cgi($r);
        !            55:     &Apache::lonlocal::get_language_handle($r);
        !            56:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
        !            57:     
        !            58:     my @emailtypes = ('permanentemail','critnotification','notification');
        !            59:     my $uname = $env{'form.uname'};
        !            60:     my $udom = $env{'form.udom'};
        !            61:     $uname =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
        !            62:     my $token = $env{'form.token'};
        !            63:     $r->print(&mt('<h3>Reset forgotten LON-CAPA password</h3>'));
        !            64:     my $output;
        !            65:     if ($token) {
        !            66:         $output = &reset_passwd($r,$token,$contact_name,$contact_email);
        !            67:     } elsif ($uname && $udom) {
        !            68:         my $domdesc = $Apache::lonnet::domaindescription{$udom};
        !            69:         my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
        !            70:         if ($authtype =~ /^internal/) {
        !            71:             my %userinfo = 
        !            72:             &Apache::lonnet::get('environment',\@emailtypes,$udom,$uname);
        !            73:             my $email = '';
        !            74:             my $emailtarget;
        !            75:             foreach my $type (@emailtypes) {
        !            76:                 $email = $userinfo{$type};
        !            77:                 if ($email =~ /[^\@]+\@[^\@]+/) {
        !            78:                     $emailtarget = $type; 
        !            79:                     last;
        !            80:                 }
        !            81:             }
        !            82:             if ($email =~ /[^\@]+\@[^\@]+/) {
        !            83:                 $output = &send_token($uname,$udom,$email,$server,$domdesc,
        !            84:                                       $contact_name,$contact_email);
        !            85:             } else {
        !            86:                 $output = &invalid_state('missing',$domdesc,
        !            87:                                          $contact_name,$contact_email);
        !            88:             }
        !            89:         } elsif ($authtype =~ /^(krb|unix|local)/) { 
        !            90:             $output = &invalid_state('authentication',$domdesc,
        !            91:                                      $contact_name,$contact_email);
        !            92:         } else {
        !            93:             $output = &invalid_state('invalid',$domdesc,
        !            94:                                      $contact_name,$contact_email);
        !            95:         }
        !            96:     } else {
        !            97:         $output = &get_uname($defdom);
        !            98:     }
        !            99:     $r->print($output);
        !           100:     $r->print(&Apache::loncommon::end_page());
        !           101:     return OK;
        !           102: }
        !           103: 
        !           104: sub get_uname {
        !           105:     my ($defdom) = @_;
        !           106:     my %lt = &Apache::lonlocal::texthash(
        !           107:                                          unam => 'username',
        !           108:                                          udom => 'domain',
        !           109:                                          proc => 'Proceed');
        !           110: 
        !           111:     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.');
        !           112:     $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>');
        !           113:     $msg .= qq|
        !           114: <form name="forgotpw" method="post">
        !           115: <table>
        !           116: <tr><td>
        !           117: <tr><td align="left">LON-CAPA $lt{'unam'}:                      </td>
        !           118:     <td><input type="text" name="uname" size="10"  /> </td></tr>
        !           119: <tr><td align="left">LON-CAPA $lt{'udom'}:                      </td>
        !           120:     <td>|;
        !           121:     $msg .= &Apache::loncommon::select_dom_form($defdom,'udom');
        !           122:     $msg .= qq|</td></tr>
        !           123: <tr><td colspan="2" align="left"><br />
        !           124:     <input type="button" value="$lt{'proc'}" onClick="document.forgotpw.submit()"></td></tr>
        !           125: </table>
        !           126: |;
        !           127:     return $msg;
        !           128: }
        !           129: 
        !           130: sub send_token {
        !           131:     my ($uname,$udom,$email,$server,$domdesc,$contact_name,
        !           132:         $contact_email) = @_;
        !           133: #    my $token = MD5->hexhash(MD5->hexhash(time.{}.rand().$$));
        !           134:     my $msg = &mt('Thank you for your request to reset the password for your
        !           135:         LON-CAPA account.').'<br /><br />';
        !           136: 
        !           137:     my $now = time;
        !           138:     my $temppasswd = &create_passwd();
        !           139:     my %info = ('ip'  => $ENV{'REMOTE_ADDR'},
        !           140:               'time'     => $now,
        !           141:               'domain'   => $udom,
        !           142:               'username' => $uname,
        !           143:               'email'    => $email,
        !           144:               'temppasswd' => $temppasswd);
        !           145: 
        !           146:     my $token = &Apache::lonnet::tmpput(\%info,$server);
        !           147:     if ($token !~ /^error/) {
        !           148:         my $esc_token = $token;
        !           149:         $esc_token =~ s/(\W)/"%".unpack('H2',$1)/eg;
        !           150:         my $mailmsg = "A request was submitted on ".localtime(time)." for a reset of the ".
        !           151:              "password for your LON-CAPA account.".
        !           152:              "To complete this process please open a web browser and enter the following ".
        !           153:              "URL in the address/location box: ".&Apache::lonnet::absolute_url()."/adm/resetpw?token=$esc_token";
        !           154:         my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
        !           155:                                 $contact_email);
        !           156:         if ($result eq 'ok') {
        !           157:             $msg .= &mt("An e-mail message sent to <b>$email</b> 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.");
        !           158:         } else {
        !           159:             $msg .= ("An error occurred when sending e-mail to $email with information about the reset process. Please contact the $contact_name ($contact_email) for assistance.");
        !           160:         }
        !           161:     } else {
        !           162:         $msg .= &mt("An error occurred creating a token required for the password reset process. Please contact the $contact_name ($contact_email) for assistance.");
        !           163:     }
        !           164:     return $msg;
        !           165: }
        !           166: 
        !           167: sub send_mail {
        !           168:     my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
        !           169:     my $outcome;
        !           170:     my $requestmail = "To: $email\n".
        !           171:                       "From: $contact_name <$contact_email>\n".
        !           172:                       "Subject: ".&mt('Your LON-CAPA account')."\n".
        !           173:                       "\n\n".$mailmsg."\n\n".
        !           174:                       &mt('[_1] LON-CAPA support team',$domdesc)."\n".
        !           175:                       "$contact_email\n";
        !           176:     if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
        !           177:         print MAIL $requestmail;
        !           178:         close(MAIL);
        !           179:         $outcome = 'ok';
        !           180:     } else {
        !           181:         $outcome = 'fail';
        !           182:     }
        !           183:     return $outcome;
        !           184: }
        !           185: 
        !           186: sub invalid_state {
        !           187:     my ($error,$domdesc,$contact_name,$contact_email) = @_;
        !           188:     my $msg;
        !           189:     if ($error eq 'invalid') {
        !           190:         $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.');
        !           191:     } else {
        !           192:         if ($error eq 'missing') {
        !           193:             $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
        !           194:         } elsif ($error eq 'authentication') {
        !           195:             $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
        !           196:         }
        !           197:         if ($contact_email ne '') {
        !           198:             my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
        !           199:             $msg .= &mt(' You may wish to contact the <a href="/adm/helpdesk?origurl=[_1]">LON-CAPA helpdesk</a> for the [_2] domain.',$escuri,$domdesc);
        !           200:         } else {
        !           201:             $msg .= &mt(' You may wish to send an e-mail to the server administrator: [_1] for the [_2] domain.',Apache::lonnet::perlvar{'AdminEmail'},$domdesc);
        !           202:         }
        !           203:     }
        !           204:     return $msg;
        !           205: }
        !           206: 
        !           207: sub reset_passwd {
        !           208:     my ($r,$token,$contact_name,$contact_email) = @_;
        !           209:     my $msg;
        !           210:     my %data = &Apache::lonnet::tmpget($token);
        !           211:     my $now = time;
        !           212:     if (keys(%data) == 0) {
        !           213:         $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.');
        !           214:         return $msg;
        !           215:     }
        !           216:     if (($data{'time'} =~ /^\d+$/) && 
        !           217:         ($data{'username'} ne '') && 
        !           218:         ($data{'domain'} ne '') && 
        !           219:         ($data{'email'}  =~ /^[^\@]+\@[^\@]+$/) && 
        !           220:         ($data{'temppasswd'} =~/^\w+$/)) {
        !           221:         my $reqtime = localtime($data{'time'});
        !           222:         if ($now - $data{'time'} < 7200) {
        !           223:             if ($env{'form.action'} eq 'verify_and_change_pass') {
        !           224:                 my $change_failed = 
        !           225:   &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
        !           226:                 if (!$change_failed) {
        !           227:                     my $delete = &Apache::lonnet::tmpdel($token);
        !           228:                     my $now = localtime(time);
        !           229:                     my $domdesc = 
        !           230:                         $Apache::lonnet::domaindescription{$data{'domain'}};
        !           231:                     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";
        !           232:                     my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
        !           233:                                             $contact_name,$contact_email);
        !           234:                     if ($result eq 'ok') {
        !           235:                         $msg .= &mt('An e-mail confirming setting of the password for your LON-CAPA account has been sent to [_1].',$data{'email'});
        !           236:                     } else {
        !           237:                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming setting of your new password.',$data{'email'});
        !           238:                     }
        !           239:                     $msg .= '<br /<br />'.&mt('<a href="/adm/login">Go to the login page</a>.');
        !           240:                 } else {
        !           241:                     $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);
        !           242:                 }
        !           243:             } else {
        !           244:                 $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 />');
        !           245:                 $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 />');
        !           246:                 &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
        !           247:             }
        !           248:         } else {
        !           249:             $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.');
        !           250:         }
        !           251:     } else {
        !           252:         $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.');
        !           253:     }
        !           254:     return $msg;
        !           255: }
        !           256: 
        !           257: sub create_passwd {
        !           258:     my $passwd = '';
        !           259:     my @letts = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
        !           260:     for (my $i=0; $i<8; $i++) {
        !           261:         my $lettnum = int (rand 2);
        !           262:         my $item = '';
        !           263:         if ($lettnum) {
        !           264:             $item = $letts[int( rand(26) )];
        !           265:             my $uppercase = int(rand 2);
        !           266:             if ($uppercase) {
        !           267:                 $item =~ tr/a-z/A-Z/;
        !           268:             }
        !           269:         } else {
        !           270:             $item = int( rand(10) );
        !           271:         }
        !           272:         $passwd .= $item;
        !           273:     }
        !           274:     return ($passwd);
        !           275: }
        !           276: 
        !           277: 1;

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