Fix search, get etc
authorkristianf <devnull@localhost>
Mon, 1 Dec 2014 22:20:07 +0000 (23:20 +0100)
committerkristianf <devnull@localhost>
Mon, 1 Dec 2014 22:20:07 +0000 (23:20 +0100)
hkp_search.php

index 17d61e3..e8d57dc 100755 (executable)
@@ -4,7 +4,7 @@ ini_set("date.timezone", "Europe/Oslo");
 $stdin = fopen('php://stdin', 'r');
 $str = stream_get_contents($stdin);
 $in = preg_split("/\n/", $str);
-
+file_put_contents("/var/tmp/ldaptest.txt", $str);
 $data[] = array();
 
 
@@ -29,6 +29,13 @@ class hkp
                return $f;
        }
 
+       public function get($search)
+       {
+               $url = "http://{$this->hkp_host}:{$this->hkp_port}/pks/lookup?op=get&exact=on&options=mr&search=0x{$search}";
+               $f = file_get_contents($url);
+               return $f;
+       }
+
        public function map_algoid_to_type($algoid)
        {
                switch($algoid)
@@ -36,13 +43,13 @@ class hkp
                        case "1":
                                return "RSA";
                        case "17":
-                               return "DSS";
+                               return "DSA";
                        case "19":
                                return "ECC";
                }
        }
 
-       public function print_pgp_key($search)
+       public function print_pgp_key_search($search)
        {
                $vindex = $this->vindex($search);
                //var_dump($vindex);
@@ -71,6 +78,14 @@ class hkp
                }
                echo "\n";
        }
+
+       public function print_pgp_key($search)
+       {
+               echo "dn: ou=PGP Keys,dc=sumptuouscapital,dc=com\n"; 
+               echo "objectClass: pgpKeyInfo\n";
+               echo "pgpCertID: ${search}\n";
+               echo "pgpKey:: ".base64_encode($this->get($search))."\n";
+       }
 }
 
 function printServerInfo($base)
@@ -87,18 +102,39 @@ function printServerInfo($base)
 
 function printPgpKey($filter)
 {
-       $filter = preg_match("/\(pgpUserID=\*?(.+?)\*?\)/", $filter, $matches); 
-       if(count($matches) > 1)
+       $isfound = false; 
+
+       // Search
+       preg_match("/\(pgpUserID=\*?(.+?)\*?\)/", $filter, $matches); 
+       if(count($matches) >= 1)
        {
+               $isfound = true; 
                $search = $matches[1];
                $hkp = new hkp;         
-               $hkp->print_pgp_key($search);
+               $hkp->print_pgp_key_search($search);
                echo "\n"; 
                echo "RESULT\n";
        }
-       else
+
+       // Get specific key     
+       if(!$isfound)
+       {
+               preg_match("/pgpCertID=([0-9A-Fa-f]+)/", $filter, $matches); 
+               if(count($matches) >= 1)
+               {
+                       $isfound = true; 
+                       $search = $matches[1];
+                       $hkp = new hkp;         
+                       $hkp->print_pgp_key($search);
+                       echo "\n"; 
+                       echo "RESULT\n";
+               }
+       }
+
+       // Still not found? error
+       if($isfound === false)
        {
-               echo "DEBUG: No matches found";
+               echo "DEBUG: No matches found\n";
                exit(1); 
        }
 }
@@ -129,6 +165,5 @@ else
        exit(1); 
 }
 
-//file_put_contents("/var/tmp/ldaptest.txt", $str);
 fclose($stdin);
 ?>