File:  [LON-CAPA] / loncom / interface / lonpreferences.pm
Revision 1.17: download - view: text, annotated - select for diffs
Tue Dec 3 19:57:26 2002 UTC (21 years, 6 months ago) by matthew
Branches: MAIN
CVS tags: version_0_6_2, version_0_6, HEAD
Trivial changes to make it shut the hell up.

    1: # The LearningOnline Network
    2: # Preferences
    3: #
    4: # $Id: lonpreferences.pm,v 1.17 2002/12/03 19:57:26 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: #         Anonymous Discussion Name Change Subroutines         #
  104: ################################################################
  105: sub screennamechanger {
  106:     my $r = shift;
  107:     my $user       = $ENV{'user.name'};
  108:     my $domain     = $ENV{'user.domain'};
  109:     my %userenv = &Apache::lonnet::get
  110:         ('environment',['screenname','nickname']);
  111:     my $screenname=$userenv{'screenname'};
  112:     my $nickname=$userenv{'nickname'};
  113:     my $bodytag=&Apache::loncommon::bodytag(
  114:               'Change Your Nickname and Anonymous Screen Name');
  115:     $r->print(<<ENDSCREEN);
  116: <html>
  117: $bodytag
  118: 
  119: <form name="server" action="/adm/preferences" method="post">
  120: <input type="hidden" name="action" value="verify_and_change_screenname" />
  121: <br />New screenname (shown if you post anonymously):
  122: <input type="text" size="20" value="$screenname" name="screenname" />
  123: <br />New nickname (shown if you post non-anonymously):
  124: <input type="text" size="20" value="$nickname" name="nickname" />
  125: <input type="submit" value="Change" />
  126: </form>
  127: </body>
  128: </html>
  129: ENDSCREEN
  130: }
  131: 
  132: sub verify_and_change_screenname {
  133:     my $r = shift;
  134:     my $user       = $ENV{'user.name'};
  135:     my $domain     = $ENV{'user.domain'};
  136: # Screenname
  137:     my $newscreen  = $ENV{'form.screenname'};
  138:     $newscreen=~s/[^ \w]//g;
  139:     my $message='';
  140:     if ($newscreen) {
  141:         &Apache::lonnet::put('environment',{'screenname' => $newscreen});
  142:         &Apache::lonnet::appenv('environment.screenname' => $newscreen);
  143:         $message='Set new screenname to '.$newscreen;
  144:     } else {
  145:         &Apache::lonnet::del('environment',['screenname']);
  146:         &Apache::lonnet::delenv('environment\.screenname');
  147:         $message='Reset screenname';
  148:     }
  149: # Nickname
  150:     $message.='<br />';
  151:     $newscreen  = $ENV{'form.nickname'};
  152:     $newscreen=~s/[^ \w]//g;
  153:     if ($newscreen) {
  154:         &Apache::lonnet::put('environment',{'nickname' => $newscreen});
  155:         &Apache::lonnet::appenv('environment.nickname' => $newscreen);
  156:         $message.='Set new nickname to '.$newscreen;
  157:     } else {
  158:         &Apache::lonnet::del('environment',['nickname']);
  159:         &Apache::lonnet::delenv('environment\.nickname');
  160:         $message.='Reset nickname';
  161:     }
  162: 
  163:     my $bodytag=&Apache::loncommon::bodytag(
  164:                     'Change Your Nickname and Anonymous Screen Name');
  165:     $r->print(<<ENDVCSCREEN);
  166: <html>
  167: $bodytag
  168: </p>
  169: $message
  170: </body></html>
  171: ENDVCSCREEN
  172: }
  173: 
  174: ################################################################
  175: #         Message Forward                                      #
  176: ################################################################
  177: 
  178: sub msgforwardchanger {
  179:     my $r = shift;
  180:     my $user       = $ENV{'user.name'};
  181:     my $domain     = $ENV{'user.domain'};
  182:     my %userenv = &Apache::lonnet::get('environment',['msgforward']);
  183:     my $msgforward=$userenv{'msgforward'};
  184:     my $bodytag=&Apache::loncommon::bodytag(
  185:                                          'Change Your Message Forwarding');
  186:     $r->print(<<ENDMSG);
  187: <html>
  188: $bodytag
  189: 
  190: <form name="server" action="/adm/preferences" method="post">
  191: <input type="hidden" name="action" value="verify_and_change_msgforward" />
  192: New Forwarding Address(es) (<tt>user:domain,user:domain,...</tt>):
  193: <input type="text" size="40" value="$msgforward" name="msgforward" />
  194: <input type="submit" value="Change" />
  195: </form>
  196: </body>
  197: </html>
  198: ENDMSG
  199: }
  200: 
  201: sub verify_and_change_msgforward {
  202:     my $r = shift;
  203:     my $user       = $ENV{'user.name'};
  204:     my $domain     = $ENV{'user.domain'};
  205:     my $newscreen  = '';
  206:     my $message='';
  207:     foreach (split(/\,/,$ENV{'form.msgforward'})) {
  208: 	my ($msuser,$msdomain)=split(/[\@\:]/,$_);
  209:         $msuser=~s/\W//g;
  210:         $msdomain=~s/\W//g;
  211:         if (($msuser) && ($msdomain)) {
  212: 	    if (&Apache::lonnet::homeserver($msuser,$msdomain) ne 'no_host') {
  213:                $newscreen.=$msuser.':'.$msdomain.',';
  214: 	   } else {
  215:                $message.='No such user: '.$msuser.':'.$msdomain.'<br>';
  216:            }
  217:         }
  218:     }
  219:     $newscreen=~s/\,$//;
  220:     if ($newscreen) {
  221:         &Apache::lonnet::put('environment',{'msgforward' => $newscreen});
  222:         &Apache::lonnet::appenv('environment.msgforward' => $newscreen);
  223:         $message.='Set new message forwarding to '.$newscreen;
  224:     } else {
  225:         &Apache::lonnet::del('environment',['msgforward']);
  226:         &Apache::lonnet::delenv('environment\.msgforward');
  227:         $message.='Reset message forwarding';
  228:     }
  229:     my $bodytag=&Apache::loncommon::bodytag(
  230:                                          'Change Your Message Forwarding');
  231:     $r->print(<<ENDVCMSG);
  232: <html>
  233: $bodytag
  234: </p>
  235: $message
  236: </body></html>
  237: ENDVCMSG
  238: }
  239: 
  240: ######################################################
  241: #            password handler subroutines            #
  242: ######################################################
  243: sub passwordchanger {
  244:     # This function is a bit of a mess....
  245:     # Passwords are encrypted using londes.js (DES encryption)
  246:     my $r = shift;
  247:     my $errormessage = shift;
  248:     $errormessage = ($errormessage || '');
  249:     my $user       = $ENV{'user.name'};
  250:     my $domain     = $ENV{'user.domain'};
  251:     my $homeserver = $ENV{'user.home'};
  252:     my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
  253:     # Check for authentication types that allow changing of the password.
  254:     return if ($currentauth !~ /^(unix|internal):/);
  255:     #
  256:     # Generate keys
  257:     my ($lkey_cpass ,$ukey_cpass ) = &des_keys();
  258:     my ($lkey_npass1,$ukey_npass1) = &des_keys();
  259:     my ($lkey_npass2,$ukey_npass2) = &des_keys();
  260:     # Store the keys in the log files
  261:     my $lonhost = $r->dir_config('lonHostID');
  262:     my $logtoken=Apache::lonnet::reply('tmpput:'
  263: 				       .$ukey_cpass  . $lkey_cpass .'&'
  264: 				       .$ukey_npass1 . $lkey_npass1.'&'
  265: 				       .$ukey_npass2 . $lkey_npass2,
  266: 				       $lonhost);
  267:     # Hexify the keys for output as javascript variables
  268:     $ukey_cpass = hex($ukey_cpass);
  269:     $lkey_cpass = hex($lkey_cpass);
  270:     $ukey_npass1= hex($ukey_npass1);
  271:     $lkey_npass1= hex($lkey_npass1);
  272:     $ukey_npass2= hex($ukey_npass2);
  273:     $lkey_npass2= hex($lkey_npass2);
  274:     # Output javascript to deal with passwords
  275:     # Output DES javascript
  276:     $r->print("<html><head>");
  277:     {
  278: 	my $include = $r->dir_config('lonIncludes');
  279: 	my $jsh=Apache::File->new($include."/londes.js");
  280: 	$r->print(<$jsh>);
  281:     }
  282:     my $bodytag=&Apache::loncommon::bodytag('Change Password','',
  283:                                          'onLoad="init();"');
  284:     $r->print(<<ENDFORM);
  285: </head>
  286: $bodytag
  287: 
  288: <script language="JavaScript">
  289: 
  290:     function send() {
  291:         uextkey=this.document.client.elements.ukey_cpass.value;
  292:         lextkey=this.document.client.elements.lkey_cpass.value;
  293:         initkeys();
  294: 
  295:         this.document.server.elements.currentpass.value
  296:             =crypted(this.document.client.elements.currentpass.value);
  297: 
  298:         uextkey=this.document.client.elements.ukey_npass1.value;
  299:         lextkey=this.document.client.elements.lkey_npass1.value;
  300:         initkeys();
  301:         this.document.server.elements.newpass_1.value
  302:             =crypted(this.document.client.elements.newpass_1.value);
  303: 
  304:         uextkey=this.document.client.elements.ukey_npass2.value;
  305:         lextkey=this.document.client.elements.lkey_npass2.value;
  306:         initkeys();
  307:         this.document.server.elements.newpass_2.value
  308:             =crypted(this.document.client.elements.newpass_2.value);
  309: 
  310:         this.document.server.submit();
  311:     }
  312: 
  313: </script>
  314: $errormessage
  315: 
  316: <p>
  317: <!-- We seperate the forms into 'server' and 'client' in order to
  318:      ensure that unencrypted passwords will not be sent out by a
  319:      crappy browser -->
  320: 
  321: <form name="server" action="/adm/preferences" method="post">
  322: <input type="hidden" name="logtoken"    value="$logtoken" />
  323: <input type="hidden" name="action"      value="verify_and_change_pass" />
  324: <input type="hidden" name="currentpass" value="" />
  325: <input type="hidden" name="newpass_1"   value="" />
  326: <input type="hidden" name="newpass_2"   value="" />
  327: </form>
  328: 
  329: <form name="client" >
  330: <table>
  331: <tr><td align="right"> Current password:                      </td>
  332:     <td><input type="password" name="currentpass" size="10"/> </td></tr>
  333: <tr><td align="right"> New password:                          </td>
  334:     <td><input type="password" name="newpass_1" size="10"  /> </td></tr>
  335: <tr><td align="right"> Confirm password:                      </td>
  336:     <td><input type="password" name="newpass_2" size="10"  /> </td></tr>
  337: <tr><td colspan="2" align="center">
  338:     <input type="button" value="Change Password" onClick="send();">
  339: </table>
  340: <input type="hidden" name="ukey_cpass"  value="$ukey_cpass" />
  341: <input type="hidden" name="lkey_cpass"  value="$lkey_cpass" />
  342: <input type="hidden" name="ukey_npass1" value="$ukey_npass1" />
  343: <input type="hidden" name="lkey_npass1" value="$lkey_npass1" />
  344: <input type="hidden" name="ukey_npass2" value="$ukey_npass2" />
  345: <input type="hidden" name="lkey_npass2" value="$lkey_npass2" />
  346: </form>
  347: </p>
  348: ENDFORM
  349:     #
  350:     return;
  351: }
  352: 
  353: sub verify_and_change_password {
  354:     my $r = shift;
  355:     my $user       = $ENV{'user.name'};
  356:     my $domain     = $ENV{'user.domain'};
  357:     my $homeserver = $ENV{'user.home'};
  358:     my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
  359:     # Check for authentication types that allow changing of the password.
  360:     return if ($currentauth !~ /^(unix|internal):/);
  361:     #
  362:     $r->print(<<ENDHEADER);
  363: <html>
  364: <head>
  365: <title>LON-CAPA Preferences:  Change password for $user</title>
  366: </head>
  367: ENDHEADER
  368:     #
  369:     my $currentpass = $ENV{'form.currentpass'}; 
  370:     my $newpass1    = $ENV{'form.newpass_1'}; 
  371:     my $newpass2    = $ENV{'form.newpass_2'};
  372:     my $logtoken    = $ENV{'form.logtoken'};
  373:     # Check for empty data 
  374:     unless (defined($currentpass) && 
  375: 	    defined($newpass1)    && 
  376: 	    defined($newpass2)    ){
  377: 	&passwordchanger($r,"<p>\n<font color='#ff0000'>ERROR</font>".
  378: 			 "Password data was blank.\n</p>");
  379: 	return;
  380:     }
  381:     # Get the keys
  382:     my $lonhost = $r->dir_config('lonHostID');
  383:     my $tmpinfo = Apache::lonnet::reply('tmpget:'.$logtoken,$lonhost);
  384:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
  385:         # I do not a have a better idea about how to handle this
  386: 	$r->print(<<ENDERROR);
  387: <p>
  388: <font color="#ff0000">ERROR:</font> Unable to retrieve stored token for
  389: password decryption.  Please log out and try again.
  390: </p>
  391: ENDERROR
  392:         # Probably should log an error here
  393:         return;
  394:     }
  395:     my ($ckey,$n1key,$n2key)=split(/&/,$tmpinfo);
  396:     # 
  397:     $currentpass = &des_decrypt($ckey ,$currentpass);
  398:     $newpass1    = &des_decrypt($n1key,$newpass1);
  399:     $newpass2    = &des_decrypt($n2key,$newpass2);
  400:     # 
  401:     if ($newpass1 ne $newpass2) {
  402: 	&passwordchanger($r,
  403: 			 '<font color="#ff0000">ERROR:</font>'.
  404: 			 'The new passwords you entered do not match.  '.
  405: 			 'Please try again.');
  406: 	return;
  407:     }
  408:     if (length($newpass1) < 7) {
  409: 	&passwordchanger($r,
  410: 			 '<font color="#ff0000">ERROR:</font>'.
  411: 			 'Passwords must be a minimum of 7 characters long.  '.
  412: 			 'Please try again.');
  413: 	return;
  414:     }
  415:     #
  416:     # Check for bad characters
  417:     my $badpassword = 0;
  418:     foreach (split(//,$newpass1)) {
  419: 	$badpassword = 1 if ((ord($_)<32)||(ord($_)>126));
  420:     }
  421:     if ($badpassword) {
  422: 	# I can't figure out how to enter bad characters on my browser.
  423: 	&passwordchanger($r,<<ENDERROR);
  424: <font color="#ff0000">ERROR:</font>
  425: The password you entered contained illegal characters.<br />
  426: Valid characters are: space and <br />
  427: <pre>
  428: !&quot;\#$%&amp;\'()*+,-./0123456789:;&lt;=&gt;?\@
  429: ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_\`abcdefghijklmnopqrstuvwxyz{|}~
  430: </pre>
  431: ENDERROR
  432:     }
  433:     # 
  434:     # Change the password (finally)
  435:     my $result = &Apache::lonnet::changepass
  436: 	($user,$domain,$currentpass,$newpass1,$homeserver);
  437:     # Inform the user the password has (not?) been changed
  438:     if ($result =~ /^ok$/) {
  439: 	$r->print(<<"ENDTEXT");
  440: <h2>The password for $user was successfully changed</h2>
  441: ENDTEXT
  442:     } else {
  443: 	# error error: run in circles, scream and shout
  444:         $r->print(<<ENDERROR);
  445: <h2><font color="#ff0000">The password for $user was not changed</font></h2>
  446: Please make sure your old password was entered correctly.
  447: ENDERROR
  448:     }
  449:     return;
  450: }
  451: 
  452: ######################################################
  453: #            other handler subroutines               #
  454: ######################################################
  455: 
  456: ################################################################
  457: #                          Main handler                        #
  458: ################################################################
  459: sub handler {
  460:     my $r = shift;
  461:     my $user = $ENV{'user.name'};
  462:     my $domain = $ENV{'user.domain'};
  463:     $r->content_type('text/html');
  464:     # Some pages contain DES keys and should not be cached.
  465:     &Apache::loncommon::no_cache($r);
  466:     $r->send_http_header;
  467:     return OK if $r->header_only;
  468:     #
  469:     if ($ENV{'form.action'} eq 'changepass') {
  470: 	&passwordchanger($r);
  471:     } elsif ($ENV{'form.action'} eq 'verify_and_change_pass') {
  472: 	&verify_and_change_password($r);
  473:     } elsif ($ENV{'form.action'} eq 'changescreenname') {
  474:         &screennamechanger($r);
  475:     } elsif ($ENV{'form.action'} eq 'verify_and_change_screenname') {
  476:         &verify_and_change_screenname($r);
  477:     } elsif ($ENV{'form.action'} eq 'changemsgforward') {
  478:         &msgforwardchanger($r);
  479:     } elsif ($ENV{'form.action'} eq 'verify_and_change_msgforward') {
  480:         &verify_and_change_msgforward($r);
  481:     } elsif ($ENV{'form.action'} eq 'debugtoggle') {
  482: 	if ($ENV{'user.name'} eq 'albertel' ) {
  483: 	    if ($ENV{'user.debug'}) {
  484: 		&Apache::lonnet::delenv('user\.debug');
  485: 	    } else {
  486: 		&Apache::lonnet::appenv('user.debug' => 1);
  487: 	    }
  488: 	}
  489:     } else {
  490: 	$r->print(<<ENDHEADER);
  491: <html>
  492: <head>
  493: <title>LON-CAPA Preferences</title>
  494: </head>
  495: ENDHEADER
  496:         $r->print(&Apache::loncommon::bodytag('Change Your Preferences'));
  497: 	# Determine current authentication method
  498: 	my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
  499: 	if ($currentauth =~ /^(unix|internal):/) {
  500: 	    $r->print(<<ENDPASSWORDFORM);
  501: <form name="client" action="/adm/preferences" method="post">
  502: <input type="hidden" name="action" value="changepass" />
  503: <input type="submit" value="Change password" />
  504: </form>
  505: ENDPASSWORDFORM
  506:         }
  507: # Change screen name
  508: 	    $r->print(<<ENDSCREENNAMEFORM);
  509: <form name="client" action="/adm/preferences" method="post">
  510: <input type="hidden" name="action" value="changescreenname" />
  511: <input type="submit" 
  512: value="Change nickname and anonymous discussion screen name" />
  513: </form>
  514: ENDSCREENNAMEFORM
  515: 	    $r->print(<<ENDMSGFORWARDFORM);
  516: <form name="client" action="/adm/preferences" method="post">
  517: <input type="hidden" name="action" value="changemsgforward" />
  518: <input type="submit" value="Change message forwarding address" />
  519: </form>
  520: ENDMSGFORWARDFORM
  521: # The "about me" page
  522: 	my $aboutmeaction=
  523: 	    '/adm/'.$ENV{'user.domain'}.'/'.$ENV{'user.name'}.'/aboutme';
  524: 	$r->print(<<ENDABOUTME);
  525: <form name="client" action="$aboutmeaction" method="post">
  526: <input type="hidden" name="action" value="changescreenname" />
  527: <input type="submit" value="Edit the 'About Me' Personal Information Screen" />
  528: </form>
  529: ENDABOUTME
  530: 	if ($ENV{'user.name'} eq 'albertel') {
  531: 	    $r->print(<<ENDDEBUG);
  532: <form name="client" action="/adm/preferences" method="post">
  533: <input type="hidden" name="action" value="debugtoggle" />
  534: <input type="submit" value="Toggle Debug" />
  535: Current Debug status is -$ENV{'user.debug'}-.
  536: </form>
  537: ENDDEBUG
  538: 	}
  539: 	# Other preference setting code should be added here
  540:     }
  541:     $r->print(<<ENDFOOTER);
  542: </body>
  543: </html>
  544: ENDFOOTER
  545:     return OK;
  546: }
  547: 
  548: 1;
  549: __END__

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