File:  [LON-CAPA] / loncom / interface / lonpreferences.pm
Revision 1.6: download - view: text, annotated - select for diffs
Sat Mar 30 23:27:00 2002 UTC (22 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
Sets environment 'screenname'

    1: # The LearningOnline Network
    2: # Preferences
    3: #
    4: # $Id: lonpreferences.pm,v 1.6 2002/03/30 23:27:00 www 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: sub screennamechanger {
  102:     my $r = shift;
  103:     my $errormessage = shift;
  104:     $errormessage = ($errormessage || '');
  105:     my $user       = $ENV{'user.name'};
  106:     my $domain     = $ENV{'user.domain'};
  107:     my $homeserver = $ENV{'user.home'};
  108:     my %userenv = &Apache::lonnet::get('environment',['screenname']);
  109:     my $screenname=$userenv{'screenname'};
  110:     $r->print(<<ENDSCREEN);
  111: <html>
  112: <body bgcolor="#FFFFFF">
  113: <h1>Preferences for $user</h1>
  114: <h3>$user is a member of domain $domain</h3>
  115: $errormessage
  116: <p>
  117: Change anonymous discussion screen name for $user
  118: </p>
  119: 
  120: <form name="server" action="/adm/preferences" method="post">
  121: <input type="hidden" name="action" value="verify_and_change_screenname" />
  122: New screenname:
  123: <input type="text" size="20" value="$screenname" name="screenname" />
  124: <input type="submit" value="Change" />
  125: </form>
  126: </body>
  127: </html>
  128: ENDSCREEN
  129: }
  130: 
  131: sub verify_and_change_screenname {
  132:     my $r = shift;
  133:     my $user       = $ENV{'user.name'};
  134:     my $domain     = $ENV{'user.domain'};
  135:     my $homeserver = $ENV{'user.home'};
  136:     my $newscreen  = $ENV{'form.screenname'};
  137:     $newscreen=~s/\W//g;
  138:     my $message='';
  139:     if ($newscreen) {
  140: 	my %tmp;
  141:         $tmp{'screenname'}=$newscreen;
  142:         &Apache::lonnet::put('environment',\%tmp);
  143:         $message='Set new screenname to '.$newscreen;
  144:     } else {
  145:         &Apache::lonnet::del('environment',['screenname']);
  146:         $message='Reset screenname';
  147:     }
  148:     $r->print(<<ENDVCSCREEN);
  149: <html>
  150: <body bgcolor="#FFFFFF">
  151: <h1>Preferences for $user</h1>
  152: <h3>$user is a member of domain $domain</h3>
  153: <p>
  154: Change anonymous discussion screen name for $user
  155: </p>
  156: $message
  157: </body></html>
  158: ENDVCSCREEN
  159: }
  160: 
  161: ######################################################
  162: #            password handler subroutines            #
  163: ######################################################
  164: sub passwordchanger {
  165:     # This function is a bit of a mess....
  166:     # Passwords are encrypted using londes.js (DES encryption)
  167:     my $r = shift;
  168:     my $errormessage = shift;
  169:     $errormessage = ($errormessage || '');
  170:     my $user       = $ENV{'user.name'};
  171:     my $domain     = $ENV{'user.domain'};
  172:     my $homeserver = $ENV{'user.home'};
  173:     my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
  174:     # Check for authentication types that allow changing of the password.
  175:     return if ($currentauth !~ /^(unix|internal):/);
  176:     #
  177:     # Generate keys
  178:     my ($lkey_cpass ,$ukey_cpass ) = &des_keys();
  179:     my ($lkey_npass1,$ukey_npass1) = &des_keys();
  180:     my ($lkey_npass2,$ukey_npass2) = &des_keys();
  181:     # Store the keys in the log files
  182:     my $lonhost = $r->dir_config('lonHostID');
  183:     my $logtoken=Apache::lonnet::reply('tmpput:'
  184: 				       .$ukey_cpass  . $lkey_cpass .'&'
  185: 				       .$ukey_npass1 . $lkey_npass1.'&'
  186: 				       .$ukey_npass2 . $lkey_npass2,
  187: 				       $lonhost);
  188:     # Hexify the keys for output as javascript variables
  189:     $ukey_cpass = hex($ukey_cpass);
  190:     $lkey_cpass = hex($lkey_cpass);
  191:     $ukey_npass1= hex($ukey_npass1);
  192:     $lkey_npass1= hex($lkey_npass1);
  193:     $ukey_npass2= hex($ukey_npass2);
  194:     $lkey_npass2= hex($lkey_npass2);
  195:     # Output javascript to deal with passwords
  196:     # Output DES javascript
  197:     {
  198: 	my $include = $r->dir_config('lonIncludes');
  199: 	my $jsh=Apache::File->new($include."/londes.js");
  200: 	$r->print(<$jsh>);
  201:     }
  202:     $r->print(<<ENDFORM);
  203: 
  204: <body bgcolor="#FFFFFF" onLoad="init();">
  205: 
  206: <script language="JavaScript">
  207: 
  208:     function send() {
  209:         uextkey=this.document.client.elements.ukey_cpass.value;
  210:         lextkey=this.document.client.elements.lkey_cpass.value;
  211:         initkeys();
  212: 
  213:         this.document.server.elements.currentpass.value
  214:             =crypted(this.document.client.elements.currentpass.value);
  215: 
  216:         uextkey=this.document.client.elements.ukey_npass1.value;
  217:         lextkey=this.document.client.elements.lkey_npass1.value;
  218:         initkeys();
  219:         this.document.server.elements.newpass_1.value
  220:             =crypted(this.document.client.elements.newpass_1.value);
  221: 
  222:         uextkey=this.document.client.elements.ukey_npass2.value;
  223:         lextkey=this.document.client.elements.lkey_npass2.value;
  224:         initkeys();
  225:         this.document.server.elements.newpass_2.value
  226:             =crypted(this.document.client.elements.newpass_2.value);
  227: 
  228:         this.document.server.submit();
  229:     }
  230: 
  231: </script>
  232: <h1>Preferences for $user</h1>
  233: <h3>$user is a member of domain $domain</h3>
  234: $errormessage
  235: <p>
  236: Change password for $user
  237: </p>
  238: <p>
  239: <!-- We seperate the forms into 'server' and 'client' in order to
  240:      ensure that unencrypted passwords will not be sent out by a
  241:      crappy browser -->
  242: 
  243: <form name="server" action="/adm/preferences" method="post">
  244: <input type="hidden" name="logtoken"    value="$logtoken" />
  245: <input type="hidden" name="action"      value="verify_and_change_pass" />
  246: <input type="hidden" name="currentpass" value="" />
  247: <input type="hidden" name="newpass_1"   value="" />
  248: <input type="hidden" name="newpass_2"   value="" />
  249: </form>
  250: 
  251: <form name="client" >
  252: <table>
  253: <tr><td align="right"> Current password:                      </td>
  254:     <td><input type="password" name="currentpass" size="10"/> </td></tr>
  255: <tr><td align="right"> New password:                          </td>
  256:     <td><input type="password" name="newpass_1" size="10"  /> </td></tr>
  257: <tr><td align="right"> Confirm password:                      </td>
  258:     <td><input type="password" name="newpass_2" size="10"  /> </td></tr>
  259: <tr><td colspan="2" align="center">
  260:     <input type="button" value="Change Password" onClick="send();">
  261: </table>
  262: <input type="hidden" name="ukey_cpass"  value="$ukey_cpass" />
  263: <input type="hidden" name="lkey_cpass"  value="$lkey_cpass" />
  264: <input type="hidden" name="ukey_npass1" value="$ukey_npass1" />
  265: <input type="hidden" name="lkey_npass1" value="$lkey_npass1" />
  266: <input type="hidden" name="ukey_npass2" value="$ukey_npass2" />
  267: <input type="hidden" name="lkey_npass2" value="$lkey_npass2" />
  268: </form>
  269: </p>
  270: ENDFORM
  271:     #
  272:     return;
  273: }
  274: 
  275: sub verify_and_change_password {
  276:     my $r = shift;
  277:     my $user       = $ENV{'user.name'};
  278:     my $domain     = $ENV{'user.domain'};
  279:     my $homeserver = $ENV{'user.home'};
  280:     my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
  281:     # Check for authentication types that allow changing of the password.
  282:     return if ($currentauth !~ /^(unix|internal):/);
  283:     #
  284:     $r->print(<<ENDHEADER);
  285: <html>
  286: <head>
  287: <title>LON-CAPA Preferences:  Change password for $user</title>
  288: </head>
  289: ENDHEADER
  290:     #
  291:     my $currentpass = $ENV{'form.currentpass'}; 
  292:     my $newpass1    = $ENV{'form.newpass_1'}; 
  293:     my $newpass2    = $ENV{'form.newpass_2'};
  294:     my $logtoken    = $ENV{'form.logtoken'};
  295:     # Check for empty data 
  296:     unless (defined($currentpass) && 
  297: 	    defined($newpass1)    && 
  298: 	    defined($newpass2)    ){
  299: 	&passwordchanger($r,"<p>\n<font color='#ff0000'>ERROR</font>".
  300: 			 "Password data was blank.\n</p>");
  301: 	return;
  302:     }
  303:     # Get the keys
  304:     my $lonhost = $r->dir_config('lonHostID');
  305:     my $tmpinfo = Apache::lonnet::reply('tmpget:'.$logtoken,$lonhost);
  306:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
  307:         # I do not a have a better idea about how to handle this
  308: 	$r->print(<<ENDERROR);
  309: <p>
  310: <font color="#ff0000">ERROR:</font> Unable to retrieve stored token for
  311: password decryption.  Please log out and try again.
  312: </p>
  313: ENDERROR
  314:         # Probably should log an error here
  315:         return;
  316:     }
  317:     my ($ckey,$n1key,$n2key)=split(/&/,$tmpinfo);
  318:     # 
  319:     my $currentpass = &des_decrypt($ckey ,$currentpass);
  320:     my $newpass1    = &des_decrypt($n1key,$newpass1);
  321:     my $newpass2    = &des_decrypt($n2key,$newpass2);
  322:     # 
  323:     if ($newpass1 ne $newpass2) {
  324: 	&passwordchanger($r,
  325: 			 '<font color="#ff0000">ERROR:</font>'.
  326: 			 'The new passwords you entered do not match.  '.
  327: 			 'Please try again.');
  328: 	return;
  329:     }
  330:     if (length($newpass1) < 7) {
  331: 	&passwordchanger($r,
  332: 			 '<font color="#ff0000">ERROR:</font>'.
  333: 			 'Passwords must be a minimum of 7 characters long.  '.
  334: 			 'Please try again.');
  335: 	return;
  336:     }
  337:     #
  338:     # Check for bad characters
  339:     my $badpassword = 0;
  340:     foreach (split(//,$newpass1)) {
  341: 	$badpassword = 1 if ((ord($_)<32)||(ord($_)>126));
  342:     }
  343:     if ($badpassword) {
  344: 	# I can't figure out how to enter bad characters on my browser.
  345: 	&passwordchanger($r,<<ENDERROR);
  346: <font color="#ff0000">ERROR:</font>
  347: The password you entered contained illegal characters.<br />
  348: Valid characters are: space and <br />
  349: <pre>
  350: !&quot;\#$%&amp;\'()*+,-./0123456789:;&lt;=&gt;?\@
  351: ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_\`abcdefghijklmnopqrstuvwxyz{|}~
  352: </pre>
  353: ENDERROR
  354:     }
  355:     # 
  356:     # Change the password (finally)
  357:     my $result = &Apache::lonnet::changepass
  358: 	($user,$domain,$currentpass,$newpass1,$homeserver);
  359:     # Inform the user the password has (not?) been changed
  360:     if ($result =~ /^ok$/) {
  361: 	$r->print(<<"ENDTEXT");
  362: <h2>Password for $user was successfully changed</h2>
  363: ENDTEXT
  364:     } else {
  365: 	# error error: run in circles, scream and shout
  366:         $r->print(<<ENDERROR);
  367: <h2><font color="#ff0000">Password for $user was not changed</font></h2>
  368: There was an internal error when attempting to change your password.
  369: Please contact your instructor or the domain coordinator.
  370: ENDERROR
  371:     }
  372:     return;
  373: }
  374: 
  375: ######################################################
  376: #            other handler subroutines               #
  377: ######################################################
  378: 
  379: 
  380: ################################################################
  381: #                          Main handler                        #
  382: ################################################################
  383: sub handler {
  384:     my $r = shift;
  385:     my $user = $ENV{'user.name'};
  386:     my $domain = $ENV{'user.domain'};
  387:     $r->content_type('text/html');
  388:     # Some pages contain DES keys and should not be cached.
  389:     &Apache::loncommon::no_cache($r);
  390:     $r->send_http_header;
  391:     return OK if $r->header_only;
  392:     # Spit out the header
  393:     if ($ENV{'form.action'} eq 'changepass') {
  394: 	&passwordchanger($r);
  395:     } elsif ($ENV{'form.action'} eq 'verify_and_change_pass') {
  396: 	&verify_and_change_password($r);
  397:     } elsif ($ENV{'form.action'} eq 'changescreenname') {
  398:         &screennamechanger($r);
  399:     } elsif ($ENV{'form.action'} eq 'verify_and_change_screenname') {
  400:         &verify_and_change_screenname($r);
  401:     } else {
  402: 	$r->print(<<ENDHEADER);
  403: <html>
  404: <head>
  405: <title>LON-CAPA Preferences</title>
  406: </head>
  407: <body bgcolor="#FFFFFF" >
  408: <h1>Preferences for $user</h1>
  409: <h3>$user is a member of domain $domain</h3>
  410: ENDHEADER
  411: 	# Determine current authentication method
  412: 	my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
  413: 	if ($currentauth =~ /^(unix|internal):/) {
  414: 	    $r->print(<<ENDPASSWORDFORM);
  415: <form name="client" action="/adm/preferences" method="post">
  416: <input type="hidden" name="action" value="changepass">
  417: <input type="submit" value="Change password">
  418: </form>
  419: ENDPASSWORDFORM
  420: # Change screen name
  421: 	    $r->print(<<ENDSCREENNAMEFORM);
  422: <form name="client" action="/adm/preferences" method="post">
  423: <input type="hidden" name="action" value="changescreenname">
  424: <input type="submit" value="Change anonymous discussion screen name">
  425: </form>
  426: ENDSCREENNAMEFORM
  427:             # Other preference setting code should be added here
  428: 	}
  429:     }
  430:     $r->print(<<ENDFOOTER);
  431: </body>
  432: </html>
  433: ENDFOOTER
  434:     return OK;
  435: } 
  436: 
  437: 1;
  438: __END__

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