misc fixes; added log function to login to help diagnose errors
[mirror/userdir-ldap-cgi.git] / Util.pm
diff --git a/Util.pm b/Util.pm
index 4cbec32..1127c63 100644 (file)
--- a/Util.pm
+++ b/Util.pm
@@ -2,13 +2,19 @@
 package Util;
 
 use strict;
-use Crypt::Blowfish;
 
 my $blocksize = 8; # A blowfish block is 8 bytes
 my $configfile = "/etc/userdir-ldap/userdir-ldap.conf";
+#my $configfile = "/home/randolph/html/debian/perl/userdir-ldap.conf";
 
 my %config = &ReadConfigFile;
 
+my $hascryptix = 1;
+eval 'use Crypt::Blowfish';
+if ($@) {
+  $hascryptix = undef;
+}
+
 sub CreateKey {
   my $keysize = shift;
   my $input;
@@ -46,8 +52,8 @@ sub Encrypt {
 
   $input .= " " x ($blocksize - (length($input) % $blocksize)) if (length($input % $blocksize));
 
-  for ($pos = 0; $pos < length($input); $pos += $blocksize) {
-    $output .= unpack("H16", $cipher->encrypt(substr($input, $pos, $blocksize)));
+  for ($pos = 0; $pos < length($input); $pos += $blocksize) {    
+    $output .= unpack("H16", $cipher->encrypt(substr($input, $pos, $blocksize))) if ($hascryptix);
   }
   return $output;
 }
@@ -63,7 +69,7 @@ sub Decrypt {
 
   for ($pos = 0; $pos < length($input); $pos += $blocksize*2) {
     $portion = pack("H16", substr($input, $pos, $blocksize*2));
-    $output .= $cipher->decrypt($portion);
+    $output .= $cipher->decrypt($portion) if ($hascryptix);
   }
     
   $output =~ s/ +$//;
@@ -114,27 +120,27 @@ sub ReadPasswordFromFile {
 }
 
 sub CheckAuthToken {
-  my ($id, $hrkey) = split(/:/, shift, 2);
+  my ($id, $hrkey) = split(/,/, shift, 2);
   return undef if (!$id || !$hrkey);
   my $key = pack("H".(length($hrkey)), $hrkey);
   my $cipher = new Crypt::Blowfish $key;
   my $r = ReadPasswordFromFile($id, $cipher);
   if ($r) {
-    UpdateAuthToken("$id:$hrkey", $r);
+    UpdateAuthToken("$id,$hrkey", $r);
   } else {    
-    ClearAuthToken("$id:$hrkey")
+    ClearAuthToken("$id,$hrkey")
   }
   return $r;
 }
 
 sub ClearAuthToken {
-  my ($id, $hrkey) = split(/:/, shift, 2);
+  my ($id, $hrkey) = split(/,/, shift, 2);
   $id =~ y/\//_/; # switch / to _
   unlink "$config{authtokenpath}/$id" || &HTMLError("Error removing authtoken: $!");
 }
 
 sub UpdateAuthToken {
-  my ($id, $hrkey) = split(/:/, shift, 2);
+  my ($id, $hrkey) = split(/,/, shift, 2);
   my $password = shift;
   my $key = pack("H".(length($hrkey)), $hrkey);
   $id =~ y/\//_/; # switch / to _
@@ -169,7 +175,7 @@ sub FormatFingerPrint {
 
 sub FetchKey {
   my $fingerprint = shift;
-  my ($out, $keyringparam);
+  my ($out, $keyringparam) = undef;
   
   foreach (split(/:/, $config{keyrings})) {
     $keyringparam .= "--keyring $_ ";
@@ -179,10 +185,10 @@ sub FetchKey {
   $fingerprint = "0x".$fingerprint;
 
   $/ = undef; # just suck it up ....
-  open(FP, "$config{gpg} $keyringparam --list-sigs --fingerprint $fingerprint|");
+  open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --list-sigs --fingerprint $fingerprint|");
   $out = <FP>;
   close FP;
-  open(FP, "$config{gpg} $keyringparam --export -a $fingerprint|");
+  open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --export -a $fingerprint|");
   $out .= <FP>;
   close FP;
   $/ = "\n";
@@ -257,10 +263,10 @@ sub ReadConfigFile {
       # Chop off any trailing comments
       s/#.*//;
       ($attr, $setting) = split(/=/, $_, 2);
-      $setting =~ s/"//g;
+      $setting =~ s/"//g; #"
       $setting =~ s/;$//;
-      $attr =~ s/^ +//; $attr =~ s/ +$//;
-      $setting =~ s/^ +//; $setting =~ s/ +$//;      
+      $attr =~ s/^\s+//; $attr =~ s/\s+$//;
+      $setting =~ s/^\s+//; $setting =~ s/\s+$//;      
       $config{$attr} = $setting;
     }
   }