Annotation of loncom/interface/lonpreferences.pm, revision 1.8

1.1       www         1: # The LearningOnline Network
                      2: # Preferences
                      3: #
1.8     ! matthew     4: # $Id: lonpreferences.pm,v 1.7 2002/04/03 18:47:39 www Exp $
1.2       albertel    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: #
1.1       www        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: #
1.3       matthew    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:  
1.1       www        45: package Apache::lonpreferences;
                     46: 
                     47: use strict;
                     48: use Apache::Constants qw(:common);
1.3       matthew    49: use Apache::File;
                     50: use Crypt::DES;
                     51: use DynaLoader; # for Crypt::DES version
1.4       matthew    52: use Apache::loncommon();
1.3       matthew    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 {
1.4       matthew    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.
1.3       matthew    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))));
1.4       matthew    94:     $plaintext=substr($plaintext,1,ord(substr($plaintext,0,1)) );
1.3       matthew    95:     return $plaintext;
                     96: }
                     97: 
1.4       matthew    98: ################################################################
                     99: #                       Handler subroutines                    #
                    100: ################################################################
1.5       www       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'};
1.6       www       108:     my %userenv = &Apache::lonnet::get('environment',['screenname']);
                    109:     my $screenname=$userenv{'screenname'};
1.5       www       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>
1.6       www       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>
1.5       www       126: </body>
                    127: </html>
                    128: ENDSCREEN
                    129: }
1.6       www       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) {
1.7       www       140:         &Apache::lonnet::put('environment',{'screenname' => $newscreen});
                    141:         &Apache::lonnet::appenv('environment.screenname' => $newscreen);
1.6       www       142:         $message='Set new screenname to '.$newscreen;
                    143:     } else {
                    144:         &Apache::lonnet::del('environment',['screenname']);
1.7       www       145:         &Apache::lonnet::delenv('environment\.screenname');
1.6       www       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: 
1.4       matthew   161: ######################################################
                    162: #            password handler subroutines            #
                    163: ######################################################
1.3       matthew   164: sub passwordchanger {
1.4       matthew   165:     # This function is a bit of a mess....
1.3       matthew   166:     # Passwords are encrypted using londes.js (DES encryption)
                    167:     my $r = shift;
1.4       matthew   168:     my $errormessage = shift;
                    169:     $errormessage = ($errormessage || '');
1.3       matthew   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();
1.4       matthew   181:     # Store the keys in the log files
1.3       matthew   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);
1.4       matthew   188:     # Hexify the keys for output as javascript variables
1.3       matthew   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
1.4       matthew   196:     # Output DES javascript
1.3       matthew   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();">
1.1       www       205: 
1.3       matthew   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>
1.4       matthew   234: $errormessage
1.3       matthew   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="" />
1.4       matthew   247: <input type="hidden" name="newpass_1"   value="" />
                    248: <input type="hidden" name="newpass_2"   value="" />
1.3       matthew   249: </form>
                    250: 
                    251: <form name="client" >
                    252: <table>
1.4       matthew   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>
1.3       matthew   259: <tr><td colspan="2" align="center">
                    260:     <input type="button" value="Change Password" onClick="send();">
                    261: </table>
1.4       matthew   262: <input type="hidden" name="ukey_cpass"  value="$ukey_cpass" />
                    263: <input type="hidden" name="lkey_cpass"  value="$lkey_cpass" />
1.3       matthew   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);
1.4       matthew   281:     # Check for authentication types that allow changing of the password.
                    282:     return if ($currentauth !~ /^(unix|internal):/);
1.3       matthew   283:     #
1.4       matthew   284:     $r->print(<<ENDHEADER);
                    285: <html>
                    286: <head>
                    287: <title>LON-CAPA Preferences:  Change password for $user</title>
                    288: </head>
                    289: ENDHEADER
1.3       matthew   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 
1.4       matthew   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>");
1.3       matthew   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')) {
1.4       matthew   307:         # I do not a have a better idea about how to handle this
1.3       matthew   308: 	$r->print(<<ENDERROR);
                    309: <p>
                    310: <font color="#ff0000">ERROR:</font> Unable to retrieve stored token for
1.4       matthew   311: password decryption.  Please log out and try again.
1.3       matthew   312: </p>
                    313: ENDERROR
1.4       matthew   314:         # Probably should log an error here
1.3       matthew   315:         return;
                    316:     }
                    317:     my ($ckey,$n1key,$n2key)=split(/&/,$tmpinfo);
1.4       matthew   318:     # 
1.3       matthew   319:     my $currentpass = &des_decrypt($ckey ,$currentpass);
                    320:     my $newpass1    = &des_decrypt($n1key,$newpass1);
                    321:     my $newpass2    = &des_decrypt($n2key,$newpass2);
1.4       matthew   322:     # 
1.3       matthew   323:     if ($newpass1 ne $newpass2) {
1.4       matthew   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.');
1.3       matthew   335: 	return;
                    336:     }
1.4       matthew   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>
1.8     ! matthew   368: Please make sure your old password was entered correctly.
1.4       matthew   369: ENDERROR
                    370:     }
                    371:     return;
1.3       matthew   372: }
                    373: 
1.4       matthew   374: ######################################################
                    375: #            other handler subroutines               #
                    376: ######################################################
                    377: 
                    378: 
1.3       matthew   379: ################################################################
                    380: #                          Main handler                        #
                    381: ################################################################
1.1       www       382: sub handler {
                    383:     my $r = shift;
1.3       matthew   384:     my $user = $ENV{'user.name'};
                    385:     my $domain = $ENV{'user.domain'};
1.1       www       386:     $r->content_type('text/html');
1.4       matthew   387:     # Some pages contain DES keys and should not be cached.
                    388:     &Apache::loncommon::no_cache($r);
1.1       www       389:     $r->send_http_header;
                    390:     return OK if $r->header_only;
1.3       matthew   391:     # Spit out the header
                    392:     if ($ENV{'form.action'} eq 'changepass') {
                    393: 	&passwordchanger($r);
                    394:     } elsif ($ENV{'form.action'} eq 'verify_and_change_pass') {
                    395: 	&verify_and_change_password($r);
1.5       www       396:     } elsif ($ENV{'form.action'} eq 'changescreenname') {
                    397:         &screennamechanger($r);
1.6       www       398:     } elsif ($ENV{'form.action'} eq 'verify_and_change_screenname') {
                    399:         &verify_and_change_screenname($r);
1.3       matthew   400:     } else {
                    401: 	$r->print(<<ENDHEADER);
1.1       www       402: <html>
                    403: <head>
1.4       matthew   404: <title>LON-CAPA Preferences</title>
1.1       www       405: </head>
1.3       matthew   406: <body bgcolor="#FFFFFF" >
                    407: <h1>Preferences for $user</h1>
                    408: <h3>$user is a member of domain $domain</h3>
                    409: ENDHEADER
                    410: 	# Determine current authentication method
                    411: 	my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
                    412: 	if ($currentauth =~ /^(unix|internal):/) {
1.4       matthew   413: 	    $r->print(<<ENDPASSWORDFORM);
                    414: <form name="client" action="/adm/preferences" method="post">
                    415: <input type="hidden" name="action" value="changepass">
                    416: <input type="submit" value="Change password">
                    417: </form>
                    418: ENDPASSWORDFORM
1.5       www       419: # Change screen name
                    420: 	    $r->print(<<ENDSCREENNAMEFORM);
                    421: <form name="client" action="/adm/preferences" method="post">
                    422: <input type="hidden" name="action" value="changescreenname">
                    423: <input type="submit" value="Change anonymous discussion screen name">
                    424: </form>
                    425: ENDSCREENNAMEFORM
1.4       matthew   426:             # Other preference setting code should be added here
1.3       matthew   427: 	}
                    428:     }
                    429:     $r->print(<<ENDFOOTER);
1.1       www       430: </body>
                    431: </html>
1.3       matthew   432: ENDFOOTER
1.1       www       433:     return OK;
                    434: } 
                    435: 
                    436: 1;
                    437: __END__

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