#!/usr/bin/perl -w # $Id: pks-list-keyids,v 1.7 2005/03/25 19:40:10 jason Exp jason $ # This takes pksclient index, gpg, or pgpring output on stdin and prints # all keyids encountered. keyids are sorted with duplicates removed if -d # is given. -n disables the printing of keyids, producing only statistics. # NB: Using pgpring or gpg --with-colons output produces 64 bit # (long/full) keyids. For now, use sed 's/^........//' for short keyids. use Getopt::Std; require "flush.pl"; $skip_output = 0; $remove_dupes = 0; %opts = (); getopts ('nd', \%opts); $skip_output = 1 if (defined ($opts{'n'})); # no output - don't print keyids $remove_dupes = 1 if (defined ($opts{'d'})); # detect and remove duplicates $line = 0; $count = 0; $dupes = 0; while () { $line++; if (!/^...:/) { # no :? must be pksclient/gpg output # only pub and sig lines have keyids if ((/^pub\s+(\w+)\/(\w+)/) || (/^sig......\s+(\w+)/) || (/^rev. ....\s+(\w+)/)) { $keyid = $1; # sig $keyid = $2 if defined ($2); # pub } } elsif ((/^pub:(\w+):(\w*):(\w*):(\w*):/) || # pgpring/gpg --with-colons (/^sig:(\w*):(\w*):(\w*):(\w*):/) || # output... (/^rev:(\w*):(\w*):(\w*):(\w*):/)) { $keyid = $4; } else { next if /^uid/; next if /^fpr/; next if /^sub/; next if /^tru/; next if /^ /; die "$0: unrecognized format: $_"; } next if (!defined ($keyid)); if ($remove_dupes) { # check for duplicate keyids... if (exists($keyids{$keyid})) { $dupes++; } else { # save keyid $keyids{$keyid} = $keyid; } } else { print $keyid, "\n" if (! $skip_output); } $count++; } if (! $skip_output && $remove_dupes) { foreach $keyid (sort(keys(%keyids))) { print "$keyid\n"; } } flush (STDOUT); $unique = $count - $dupes; print STDERR "\n"; print STDERR "info: processed $line line(s)\n"; print STDERR "info: found $count keyid(s)"; print STDERR ", $dupes duplicate keyid(s), $unique unique" if ($remove_dupes); print STDERR "\n"; $runtime = time() - $^T; print STDERR "info: completed in $runtime second(s)\n\n"; # try to print some process status information... times() isn't enough. # for more data, use time(1) from the shell. # FIXME: make this optional and/or work on systems with different ps args? if (open (PS, "ps -uxwp $$|")) { while () { print STDERR "info: ps: $_"; } print STDERR "\n"; }