3 # (c) 2000, 2002 Christian Kurz <shorty@debian.org>,
4 # Peter Palfrader <peter@palfrader.org>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software Foundation,
20 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 # Keylookup homepage: http://www.palfrader.org/keylookup/
23 # CVS, BTS: http://savannah.gnu.org/projects/keylookup/
25 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV', 'PATH'};
26 $|=1; # Always flush buffers
34 my $version = '3.0 ($Id$)';
36 # Where to find GnuPG's options file.
37 # Full path to the dialog and whiptail executeable.
38 my $Dialog = '/usr/bin/dialog';
39 my $Whiptail = '/usr/bin/whiptail';
41 # Strings to use in the dialog|whiptail frontend
42 my $TITLE = 'Import Keys';
43 my $BACKTITLE = 'KeyLookup $Revision$';
44 my $INSTRUCTION = 'Select keys to import:';
46 my @TPUTCOL=('tput', 'cols');
47 my @TPUTROW=('tput', 'lines');
50 # Size of the dialog boxes, will be set in calcDialogSize;
51 my $MAX_UID_FIELD_LEN;
56 # Was the keyserver overriden|given on the command line?
57 # This is used to find out wheter we need to instruct the user
58 # to give the keyserver option to GnuPG.
59 my $keyserverWasSetOnCmdLine = 0;
62 # Maps algorithm numbers to algorithm types as defined in RFC 2400.
65 2 => 'r', # RSA encrypt only (deprecated)
66 3 => 's', # RSA sign only (deprecated)
67 16 => 'g', # ElGamal encrypt only
68 20 => 'g', # ElGamal sign and encrypt (all OpenPGP implementations cryptographically broken, do not use. no longer part of OpenPGP)
73 # getHits receives all options as a parameter, calls fetchIT to
74 # query a keyserver, processes the output from the keyserver and
75 # stores it in a datastructure for later use.
79 my $pid = open(KID
, '-|');
80 defined ($pid) or die ("Cannot fork: $!\n");
83 open (STDIN
, "/dev/null") || die ("Cannot open /dev/null as stdin: $!\n");
87 if ($options->{'keyserver'}) {
88 push @ops, '--keyserver='.$options->{'keyserver'};
90 push @ops, '--command-fd=0';
92 push @ops, '--no-tty';
93 push @ops, '--with-colons';
94 push @ops, '--fixed-list-mode';
95 push @ops, '--search';
96 push @ops, $options->{'search'};
98 die ("Cannot exec GnuPG: $!\n");
105 my ($type, undef) = split /:/;
106 if ($type eq 'pub') {
107 my ($type, $keyid, $algo, $bits, $created, undef, $revoked) = split /:/;
108 $currentKey = { 'bits' => $bits,
109 'type' => (defined $ALGOS{$algo} ?
$ALGOS{$algo} : '#'.$algo),
111 'created' => $created,
112 'revoked' => $revoked,
115 $keys{ $keyid } = $currentKey;
116 } elsif (defined $currentKey && $type eq 'uid') {
117 my ($type, $name) = split /:/;
118 if ($currentKey->{'revoked'} eq 'r') {
119 $name .= ' [REVOKED]';
121 push @
{ $currentKey->{'uid'} }, $name;
130 # returns the number of columns of the terminal
133 return $DEFAULTCOLS unless (defined ($pid = open(KID
, "-|")));
140 return (defined $cols) ?
$cols : $DEFAULTCOLS;
143 # returns the number of lines of the terminal
146 return $DEFAULTROWS unless (defined ($pid = open(KID
, "-|")));
153 return (defined $rows) ?
$rows : $DEFAULTROWS;
156 # sets MAX_UID_FIELD_LEN, DIALOGSIZE, and WHIPTAILSIZE
158 my $COLS = &getCols
();
159 my $ROWS = &getRows
();
160 $MAX_UID_FIELD_LEN = $COLS - 27;
161 @DIALOGSIZE = ($ROWS-7, $COLS-7, $ROWS-14);
162 @WHIPTAILSIZE = ($ROWS-7, $COLS-7, $ROWS-14);
165 sub prepareForDialog
{
169 for my $keyid (sort {- ($keys->{$a}->{'created'} <=> $keys->{$b}->{'created'})} keys %$keys) {
170 for (@
{ $keys->{$keyid}->{'uid'} }) {
172 $keys->{$keyid}->{'keyid'},
173 length() <= $MAX_UID_FIELD_LEN ?
$_ : substr($_, 0, $MAX_UID_FIELD_LEN-2) . '..',
176 my (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) = localtime ($keys->{$keyid}->{'created'});
177 push @keyargs, $keys->{$keyid}->{'keyid'}, sprintf( "[created: %s-%s-%s]", $year+1900, $mon+1, $mday ), 'off';
178 push @keyargs, '-'x8
, '-'x40
, 'off';
191 for my $keyid (sort {- ($keys->{$a}->{'created'} <=> $keys->{$b}->{'created'})} keys %$keys) {
192 my (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) = localtime ($keys->{$keyid}->{'created'});
193 push @lines, sprintf( "%s%s/%s %s-%s-%s\n",
194 $keys->{$keyid}->{'bits'},
195 $keys->{$keyid}->{'type'},
196 $keys->{$keyid}->{'keyid'},
197 $year+1900, $mon+1, $mday );
198 push @lines, map { ' 'x26
. $_ . "\n" } @
{ $keys->{$keyid}->{'uid'} };
208 # open(SAVEOUT, ">&STDOUT") || die ("Cannot save STDOUT: $!\n");
209 # open(SAVEIN , "<&STDIN" ) || die ("Cannot save STDIN: $!\n");
211 my $pid = open3
( '<&STDIN', '>&STDOUT', \
*ERRFH
, @
$args);
214 my @keys = grep { !$unique{$_}++ }
215 grep { /^[0-9A-Fa-f]{8}$/ }
216 map { s/\s//g; $_ } <ERRFH
>;
219 # open(STDOUT, ">&SAVEOUT") || die "Cannot restore STDOUT: $!\n";
220 # open(STDIN , "<&SAVEIN") || die "Cannot restore STDIN: $!\n";
229 my $frontend = $options->{'frontend'};
230 $frontend = 'dialog' unless (defined $frontend);
232 if ($frontend eq 'dialog') {
233 unless (-x
$Dialog) {
234 warn("Dialog ($Dialog) not executeable/installed. Falling back to Whiptail\n");
235 $frontend = 'whiptail';
238 if ($frontend eq 'whiptail') {
239 unless (-x
$Whiptail ) {
240 warn("Whiptail ($Whiptail) not executeable/installed. Falling back to plain\n");
245 if ( $frontend eq 'dialog' ) {
257 push @ARGS, @
{&prepareForDialog
($keys)};
258 return &callDialog
( \
@ARGS );
259 } elsif ( $frontend eq 'whiptail' ) {
272 push @ARGS, @
{&prepareForDialog
($keys)};
273 return &callDialog
( \
@ARGS );
275 print for (@
{ &prepareForTXT
( $keys ) });
276 if ($keyserverWasSetOnCmdLine) {
277 printf ("Now run gpg --keyserver %s --recv-keys <key ids>\n", $options->{'keyserver'});
279 print ("Now run gpg --recv-keys <key ids>\n");
282 ## If no frontend was selected, or selected frontend was plain,
283 ## exit successfully, otherwise with an exitcode != 0
284 exit (defined $options->{'frontend'} &&
285 $options->{'frontend'} ne "" &&
286 $options->{'frontend'} ne "plain");
295 if ($options->{'keyserver'}) {
296 push @args, '--keyserver='.$options->{'keyserver'};
298 push @args, '--recv-keys';
299 for my $keyid (@
$keyids) {
301 my ($cleanid) = $keyid =~ /^([a-zA-Z0-9]{8})$/;
302 warn ("keyid '$keyid' has unexpected format - skipping\n"), next
303 unless defined $cleanid;
304 push @args, $cleanid;
307 print "Calling GnuPG...\n";
308 exec (@args) || die "can't exec gnupg: $!\n"; # won't return
313 my $errorcode = shift;
315 Syntax
: keylookup
[options
] <searchstring
>
318 --keyserver
=<keyserver
> Select keyserver
319 --frontend
=<frontend
> One of whiptail
, dialog
or plain
320 --importall Import all matched
keys
321 --help
print this message
329 print "keylookup $version\nWritten by Christian Kurz and Peter Palfrader.\n";
334 GetOptions
( \
%options,
341 &version
(0) if ($options{'version'});
342 &usage
(0) if ($options{'help'} || ( scalar(@ARGV) == 0));
344 ## Take all additional arguments to the program as a search target,
345 ## escape the string for use in URLs.
346 $options{'search'} = join ' ', @ARGV;
347 $options{'search'} =~ s
/ ( [^A
-Za
-z0
-9] )
348 / '%' . unpack("H2", $1)
350 my $keys = getHits
( \
%options );
353 if ($options{'importall'}) {
354 my @allkeys = keys %$keys;
357 $keyids = selectKeys
($keys, \
%options); # won't return if no interactive frontend
359 &importKeys
($keyids, \
%options) if (scalar @
$keyids); # won't return