File:  [LON-CAPA] / loncom / interface / lonpreferences.pm
Revision 1.4: download - view: text, annotated - select for diffs
Tue Feb 19 21:50:40 2002 UTC (22 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Users are now able to change passwords if they are internally or filesystem
authenticated.

    1: # The LearningOnline Network
    2: # Preferences
    3: #
    4: # $Id: lonpreferences.pm,v 1.4 2002/02/19 21:50:40 matthew Exp $
    5: #
    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: # (Internal Server Error Handler
   29: #
   30: # (Login Screen
   31: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
   32: # 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
   33: #
   34: # 3/1/1 Gerd Kortemeyer)
   35: #
   36: # 3/1 Gerd Kortemeyer
   37: #
   38: # 2/13/02 2/14 2/15 Matthew Hall
   39: #
   40: # This package uses the "londes.js" javascript code. 
   41: #
   42: # TODOs that have to be completed:
   43: #    interface with lonnet to change the password
   44:  
   45: package Apache::lonpreferences;
   46: 
   47: use strict;
   48: use Apache::Constants qw(:common);
   49: use Apache::File;
   50: use Crypt::DES;
   51: use DynaLoader; # for Crypt::DES version
   52: use Apache::loncommon();
   53: 
   54: #
   55: # Write lonnet::passwd to do the call below.
   56: # Use:
   57: #   my $answer=reply("encrypt:passwd:$udom:$uname:$upass",$tryserver);
   58: #
   59: ##################################################
   60: #          password associated functions         #
   61: ##################################################
   62: sub des_keys {
   63:     # Make a new key for DES encryption.
   64:     # Each key has two parts which are returned seperately.
   65:     # Please note:  Each key must be passed through the &hex function
   66:     # before it is output to the web browser.  The hex versions cannot
   67:     # be used to decrypt.
   68:     my @hexstr=('0','1','2','3','4','5','6','7',
   69:                 '8','9','a','b','c','d','e','f');
   70:     my $lkey='';
   71:     for (0..7) {
   72:         $lkey.=$hexstr[rand(15)];
   73:     }
   74:     my $ukey='';
   75:     for (0..7) {
   76:         $ukey.=$hexstr[rand(15)];
   77:     }
   78:     return ($lkey,$ukey);
   79: }
   80: 
   81: sub des_decrypt {
   82:     my ($key,$cyphertext) = @_;
   83:     my $keybin=pack("H16",$key);
   84:     my $cypher;
   85:     if ($Crypt::DES::VERSION>=2.03) {
   86:         $cypher=new Crypt::DES $keybin;
   87:     } else {
   88:         $cypher=new DES $keybin;
   89:     }
   90:     my $plaintext=
   91: 	$cypher->decrypt(unpack("a8",pack("H16",substr($cyphertext,0,16))));
   92:     $plaintext.=
   93: 	$cypher->decrypt(unpack("a8",pack("H16",substr($cyphertext,16,16))));
   94:     $plaintext=substr($plaintext,1,ord(substr($plaintext,0,1)) );
   95:     return $plaintext;
   96: }
   97: 
   98: ################################################################
   99: #                       Handler subroutines                    #
  100: ################################################################
  101: 
  102: ######################################################
  103: #            password handler subroutines            #
  104: ######################################################
  105: sub passwordchanger {
  106:     # This function is a bit of a mess....
  107:     # Passwords are encrypted using londes.js (DES encryption)
  108:     my $r = shift;
  109:     my $errormessage = shift;
  110:     $errormessage = ($errormessage || '');
  111:     my $user       = $ENV{'user.name'};
  112:     my $domain     = $ENV{'user.domain'};
  113:     my $homeserver = $ENV{'user.home'};
  114:     my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
  115:     # Check for authentication types that allow changing of the password.
  116:     return if ($currentauth !~ /^(unix|internal):/);
  117:     #
  118:     # Generate keys
  119:     my ($lkey_cpass ,$ukey_cpass ) = &des_keys();
  120:     my ($lkey_npass1,$ukey_npass1) = &des_keys();
  121:     my ($lkey_npass2,$ukey_npass2) = &des_keys();
  122:     # Store the keys in the log files
  123:     my $lonhost = $r->dir_config('lonHostID');
  124:     my $logtoken=Apache::lonnet::reply('tmpput:'
  125: 				       .$ukey_cpass  . $lkey_cpass .'&'
  126: 				       .$ukey_npass1 . $lkey_npass1.'&'
  127: 				       .$ukey_npass2 . $lkey_npass2,
  128: 				       $lonhost);
  129:     # Hexify the keys for output as javascript variables
  130:     $ukey_cpass = hex($ukey_cpass);
  131:     $lkey_cpass = hex($lkey_cpass);
  132:     $ukey_npass1= hex($ukey_npass1);
  133:     $lkey_npass1= hex($lkey_npass1);
  134:     $ukey_npass2= hex($ukey_npass2);
  135:     $lkey_npass2= hex($lkey_npass2);
  136:     # Output javascript to deal with passwords
  137:     # Output DES javascript
  138:     {
  139: 	my $include = $r->dir_config('lonIncludes');
  140: 	my $jsh=Apache::File->new($include."/londes.js");
  141: 	$r->print(<$jsh>);
  142:     }
  143:     $r->print(<<ENDFORM);
  144: 
  145: <body bgcolor="#FFFFFF" onLoad="init();">
  146: 
  147: <script language="JavaScript">
  148: 
  149:     function send() {
  150:         uextkey=this.document.client.elements.ukey_cpass.value;
  151:         lextkey=this.document.client.elements.lkey_cpass.value;
  152:         initkeys();
  153: 
  154:         this.document.server.elements.currentpass.value
  155:             =crypted(this.document.client.elements.currentpass.value);
  156: 
  157:         uextkey=this.document.client.elements.ukey_npass1.value;
  158:         lextkey=this.document.client.elements.lkey_npass1.value;
  159:         initkeys();
  160:         this.document.server.elements.newpass_1.value
  161:             =crypted(this.document.client.elements.newpass_1.value);
  162: 
  163:         uextkey=this.document.client.elements.ukey_npass2.value;
  164:         lextkey=this.document.client.elements.lkey_npass2.value;
  165:         initkeys();
  166:         this.document.server.elements.newpass_2.value
  167:             =crypted(this.document.client.elements.newpass_2.value);
  168: 
  169:         this.document.server.submit();
  170:     }
  171: 
  172: </script>
  173: <h1>Preferences for $user</h1>
  174: <h3>$user is a member of domain $domain</h3>
  175: $errormessage
  176: <p>
  177: Change password for $user
  178: </p>
  179: <p>
  180: <!-- We seperate the forms into 'server' and 'client' in order to
  181:      ensure that unencrypted passwords will not be sent out by a
  182:      crappy browser -->
  183: 
  184: <form name="server" action="/adm/preferences" method="post">
  185: <input type="hidden" name="logtoken"    value="$logtoken" />
  186: <input type="hidden" name="action"      value="verify_and_change_pass" />
  187: <input type="hidden" name="currentpass" value="" />
  188: <input type="hidden" name="newpass_1"   value="" />
  189: <input type="hidden" name="newpass_2"   value="" />
  190: </form>
  191: 
  192: <form name="client" >
  193: <table>
  194: <tr><td align="right"> Current password:                      </td>
  195:     <td><input type="password" name="currentpass" size="10"/> </td></tr>
  196: <tr><td align="right"> New password:                          </td>
  197:     <td><input type="password" name="newpass_1" size="10"  /> </td></tr>
  198: <tr><td align="right"> Confirm password:                      </td>
  199:     <td><input type="password" name="newpass_2" size="10"  /> </td></tr>
  200: <tr><td colspan="2" align="center">
  201:     <input type="button" value="Change Password" onClick="send();">
  202: </table>
  203: <input type="hidden" name="ukey_cpass"  value="$ukey_cpass" />
  204: <input type="hidden" name="lkey_cpass"  value="$lkey_cpass" />
  205: <input type="hidden" name="ukey_npass1" value="$ukey_npass1" />
  206: <input type="hidden" name="lkey_npass1" value="$lkey_npass1" />
  207: <input type="hidden" name="ukey_npass2" value="$ukey_npass2" />
  208: <input type="hidden" name="lkey_npass2" value="$lkey_npass2" />
  209: </form>
  210: </p>
  211: ENDFORM
  212:     #
  213:     return;
  214: }
  215: 
  216: sub verify_and_change_password {
  217:     my $r = shift;
  218:     my $user       = $ENV{'user.name'};
  219:     my $domain     = $ENV{'user.domain'};
  220:     my $homeserver = $ENV{'user.home'};
  221:     my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
  222:     # Check for authentication types that allow changing of the password.
  223:     return if ($currentauth !~ /^(unix|internal):/);
  224:     #
  225:     $r->print(<<ENDHEADER);
  226: <html>
  227: <head>
  228: <title>LON-CAPA Preferences:  Change password for $user</title>
  229: </head>
  230: ENDHEADER
  231:     #
  232:     my $currentpass = $ENV{'form.currentpass'}; 
  233:     my $newpass1    = $ENV{'form.newpass_1'}; 
  234:     my $newpass2    = $ENV{'form.newpass_2'};
  235:     my $logtoken    = $ENV{'form.logtoken'};
  236:     # Check for empty data 
  237:     unless (defined($currentpass) && 
  238: 	    defined($newpass1)    && 
  239: 	    defined($newpass2)    ){
  240: 	&passwordchanger($r,"<p>\n<font color='#ff0000'>ERROR</font>".
  241: 			 "Password data was blank.\n</p>");
  242: 	return;
  243:     }
  244:     # Get the keys
  245:     my $lonhost = $r->dir_config('lonHostID');
  246:     my $tmpinfo = Apache::lonnet::reply('tmpget:'.$logtoken,$lonhost);
  247:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
  248:         # I do not a have a better idea about how to handle this
  249: 	$r->print(<<ENDERROR);
  250: <p>
  251: <font color="#ff0000">ERROR:</font> Unable to retrieve stored token for
  252: password decryption.  Please log out and try again.
  253: </p>
  254: ENDERROR
  255:         # Probably should log an error here
  256:         return;
  257:     }
  258:     my ($ckey,$n1key,$n2key)=split(/&/,$tmpinfo);
  259:     # 
  260:     my $currentpass = &des_decrypt($ckey ,$currentpass);
  261:     my $newpass1    = &des_decrypt($n1key,$newpass1);
  262:     my $newpass2    = &des_decrypt($n2key,$newpass2);
  263:     # 
  264:     if ($newpass1 ne $newpass2) {
  265: 	&passwordchanger($r,
  266: 			 '<font color="#ff0000">ERROR:</font>'.
  267: 			 'The new passwords you entered do not match.  '.
  268: 			 'Please try again.');
  269: 	return;
  270:     }
  271:     if (length($newpass1) < 7) {
  272: 	&passwordchanger($r,
  273: 			 '<font color="#ff0000">ERROR:</font>'.
  274: 			 'Passwords must be a minimum of 7 characters long.  '.
  275: 			 'Please try again.');
  276: 	return;
  277:     }
  278:     #
  279:     # Check for bad characters
  280:     my $badpassword = 0;
  281:     foreach (split(//,$newpass1)) {
  282: 	$badpassword = 1 if ((ord($_)<32)||(ord($_)>126));
  283:     }
  284:     if ($badpassword) {
  285: 	# I can't figure out how to enter bad characters on my browser.
  286: 	&passwordchanger($r,<<ENDERROR);
  287: <font color="#ff0000">ERROR:</font>
  288: The password you entered contained illegal characters.<br />
  289: Valid characters are: space and <br />
  290: <pre>
  291: !&quot;\#$%&amp;\'()*+,-./0123456789:;&lt;=&gt;?\@
  292: ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_\`abcdefghijklmnopqrstuvwxyz{|}~
  293: </pre>
  294: ENDERROR
  295:     }
  296:     # 
  297:     # Change the password (finally)
  298:     my $result = &Apache::lonnet::changepass
  299: 	($user,$domain,$currentpass,$newpass1,$homeserver);
  300:     # Inform the user the password has (not?) been changed
  301:     if ($result =~ /^ok$/) {
  302: 	$r->print(<<"ENDTEXT");
  303: <h2>Password for $user was successfully changed</h2>
  304: ENDTEXT
  305:     } else {
  306: 	# error error: run in circles, scream and shout
  307:         $r->print(<<ENDERROR);
  308: <h2><font color="#ff0000">Password for $user was not changed</font></h2>
  309: There was an internal error when attempting to change your password.
  310: Please contact your instructor or the domain coordinator.
  311: ENDERROR
  312:     }
  313:     return;
  314: }
  315: 
  316: ######################################################
  317: #            other handler subroutines               #
  318: ######################################################
  319: 
  320: 
  321: ################################################################
  322: #                          Main handler                        #
  323: ################################################################
  324: sub handler {
  325:     my $r = shift;
  326:     my $user = $ENV{'user.name'};
  327:     my $domain = $ENV{'user.domain'};
  328:     $r->content_type('text/html');
  329:     # Some pages contain DES keys and should not be cached.
  330:     &Apache::loncommon::no_cache($r);
  331:     $r->send_http_header;
  332:     return OK if $r->header_only;
  333:     # Spit out the header
  334:     if ($ENV{'form.action'} eq 'changepass') {
  335: 	&passwordchanger($r);
  336:     } elsif ($ENV{'form.action'} eq 'verify_and_change_pass') {
  337: 	&verify_and_change_password($r);
  338:     } else {
  339: 	$r->print(<<ENDHEADER);
  340: <html>
  341: <head>
  342: <title>LON-CAPA Preferences</title>
  343: </head>
  344: <body bgcolor="#FFFFFF" >
  345: <h1>Preferences for $user</h1>
  346: <h3>$user is a member of domain $domain</h3>
  347: ENDHEADER
  348: 	# Determine current authentication method
  349: 	my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
  350: 	if ($currentauth =~ /^(unix|internal):/) {
  351: 	    $r->print(<<ENDPASSWORDFORM);
  352: <form name="client" action="/adm/preferences" method="post">
  353: <input type="hidden" name="action" value="changepass">
  354: <input type="submit" value="Change password">
  355: </form>
  356: ENDPASSWORDFORM
  357:             # Other preference setting code should be added here
  358: 	}
  359:     }
  360:     $r->print(<<ENDFOOTER);
  361: </body>
  362: </html>
  363: ENDFOOTER
  364:     return OK;
  365: } 
  366: 
  367: 1;
  368: __END__

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