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

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

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