--- loncom/lonlocal.pm 2004/06/01 09:53:44 1.4 +++ loncom/lonlocal.pm 2004/06/17 09:27:23 1.5 @@ -1,5 +1,5 @@ # -# $Id: lonlocal.pm,v 1.4 2004/06/01 09:53:44 foxr Exp $ +# $Id: lonlocal.pm,v 1.5 2004/06/17 09:27:23 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -56,6 +56,15 @@ my $fileindex = 0; # Per process lonc u my $lastError; # Reason for last failure. +# Debugging: + +my $DEBUG = 1; + +sub Debug { + my $msg = shift; + print STDERR "$msg\n"; +} + # Initialization $perlvar = LONCAPA::Configuration::read_conf('loncapa.conf'); @@ -63,7 +72,7 @@ $perlvar = LONCAPA::Configuration::read_ #------------------------------------------------------------------------ # -# Name BuildKey +# Name CreateCipherKey # Description: Create an encryption key. # Returns: The key. # @@ -73,13 +82,15 @@ sub CreateCipherKey { my $binaryKey; my $cipherkey; - # we'll use the output of /dev/random to produce our key. + # we'll use the output of /dev/urandom to produce our key. # On a system with decent entropy, this ought to be much more # random than all the playing that used to be done to get a key. - # + # On a system with not so decent entropy we'll still get an ok key. + # My concern with /dev/random is that we may block for an indefinite + # time period...where for us decent keys are probably good enough. $keylength = IDEA::keysize(); - open(RANDOM, "{lonCertificateDirectory}; - my $Filename = $CertificateDir.$pathsep.".$fileindex.".$PID; + my $Filename = $CertificateDir.$pathsep.".$fileindex.".$$; # If this file already exists, this is a recoverable error... we just # delete the earlier incarnation of the file. @@ -134,7 +145,7 @@ sub CreateKeyFile { # the file is created with the appropriate locked down permissions. if(! sysopen(KEYFILE, $Filename, O_CREAT | O_EXCL | O_WRONLY, 0600)) { - $lastError = "Creation of key file failed ".$ERRNO; + $lastError = "Creation of key file failed ".$!; return undef; } # Create the key, write it to the file and close the file: @@ -143,7 +154,7 @@ sub CreateKeyFile { print KEYFILE "$key\n"; close KEYFILE; - return \($key, $Filename); + return ($key, $Filename); } @@ -165,13 +176,17 @@ sub CreateKeyFile { # sub ReadKeyFile { my $Filename = shift; + Debug("ReadKeyFile: $Filename"); + if(! open(KEYFILE, "<$Filename")) { + Debug(" Open of $Filename failed\n"); $lastError = "Key file open failed"; return undef } my $key = ; - chomp; + chomp($key); + Debug(" Read key: $key"); close KEYFILE; unlink $Filename; # @@ -179,6 +194,7 @@ sub ReadKeyFile { # permissions: # if(-e $Filename) { + Debug("File did not get deleted"); $lastError = "Key file still exists after unlink"; return undef; } @@ -189,9 +205,13 @@ sub ReadKeyFile { # replacing our file... of course if they read this comment they'll # be too smart to put an incorrectly sized file # - if(length($key) != IDEA::keysize*2) { + my $keylen = length($key); + my $rightlen= IDEA::keysize()*2; + if($keylen != $rightlen) { + Debug("Key is incorrect length is $keylen sb $rightlen"); $lastError = "Key file has incorrect length"; return undef; } + Debug("Returning key: $key to caller"); return $key; }