fix stupid think-o that caused keytrans adduserid to misbehave if another key came...
[monkeysphere.git] / src / share / keytrans
index 56ddde2ad4cecccbb0f8904d5e8bf9c24be3d47b..255a271c2cefd7f2dd446d5b69e9a275f8116ce5 100755 (executable)
@@ -722,6 +722,7 @@ sub findkey {
   my $foundfprstr = Crypt::OpenSSL::Bignum->new_from_bin($foundfpr)->to_hex();
   # left-pad with 0's to bring up to full 40-char (160-bit) fingerprint:
   $foundfprstr = sprintf("%040s", $foundfprstr);
+  my $matched = 0;
 
   # is this a match?
   if ((!defined($data->{target}->{fpr})) ||
@@ -731,6 +732,7 @@ sub findkey {
     }
     $data->{key} = { 'rsa' => $pubkey,
                     'timestamp' => $key_timestamp };
+    $matched = 1;
   }
 
   if ($tag != $packet_types->{seckey} &&
@@ -740,7 +742,7 @@ sub findkey {
     }
     return;
   }
-  if (!defined($data->{key})) {
+  if (!$matched) {
     # we don't think the public part of this key matches
     if ($readbytes < $packetlen) {
       read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
@@ -797,7 +799,9 @@ sub openpgp2rsa {
     $fpr = uc($fpr);
   }
 
-  my $data = { 'fpr' => $fpr};
+  my $data = { target => { fpr => $fpr,
+                        },
+              };
   my $subs = { $packet_types->{pubkey} => \&findkey,
               $packet_types->{pub_subkey} => \&findkey,
               $packet_types->{seckey} => \&findkey,
@@ -808,6 +812,40 @@ sub openpgp2rsa {
   return $data->{key}->{rsa};
 }
 
+sub findkeyfprs {
+  my $data = shift;
+  my $instr = shift;
+  my $tag = shift;
+  my $packetlen = shift;
+
+  findkey($data, $instr, $tag, $packetlen);
+  if (defined($data->{key})) {
+    if (defined($data->{key}->{rsa}) && defined($data->{key}->{timestamp})) {
+      $data->{keys}->{fingerprint($data->{key}->{rsa}, $data->{key}->{timestamp})} = $data->{key};
+    } else {
+      die "should have found some key here";
+    }
+    undef($data->{key});
+  }
+};
+
+sub getallprimarykeys {
+  my $instr = shift;
+
+  my $subs = { $packet_types->{pubkey} => \&findkeyfprs,
+               $packet_types->{seckey} => \&findkeyfprs,
+            };
+  my $data = {target => { } };
+
+  packetwalk($instr, $subs, $data);
+
+  if (defined $data->{keys}) {
+    return $data->{keys};
+  } else {
+    return {};
+  }
+}
+
 sub adduserid {
   my $instr = shift;
   my $fpr = shift;
@@ -1084,7 +1122,7 @@ for (basename($0)) {
        open($instream,'-');
        binmode($instream, ":bytes");
 
-       my $revcert = revokeuserid($instream, $fpr, $uid, $ENV{KEYTRANS_REVUID_TIMESTAMP});
+       my $revcert = revokeuserid($instream, $fpr, $uid, $ENV{PEM2OPENPGP_TIMESTAMP});
 
        print $revcert;
       } elsif (/^adduserid$/) {
@@ -1100,8 +1138,14 @@ for (basename($0)) {
                               });
 
        print $newuid;
+      } elsif (/^listfprs$/) {
+        my $instream;
+       open($instream,'-');
+       binmode($instream, ":bytes");
+        my $keys = getallprimarykeys($instream);
+        printf("%s\n", join("\n", map { uc(unpack('H*', $_)) } keys(%{$keys})));
       } else {
-       die "Unrecognized subcomand.  keytrans subcommands are not a stable interface!\n";
+       die "Unrecognized subcommand.  keytrans subcommands are not a stable interface!\n";
       }
     }
   }