3 # (c) 2000, 2002 Christian Kurz <shorty@debian.org>,
4 # Peter Palfrader <peter@palfrader.org>
6 # $Id: keylookup,v 1.34 2002/09/19 03:35:10 weasel Exp $
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 = '2.1 ($Id: keylookup,v 1.34 2002/09/19 03:35:10 weasel Exp $)';
36 # The port to use for keyservers unless given otherwise.
38 # The default proxy port which is used unless the port is explicitly given
39 # in the http_proxy environment variable.
42 # Name of the GnuPG binary. The executeable must the in the PATH. This may
43 # be overriden using the --gnupg= switch.
45 # Where to find GnuPG's options file.
46 my $GNUPGOPTIONS=(defined $ENV{GNUPGHOME
} ?
$ENV{GNUPGHOME
} : $ENV{'HOME'}.'/.gnupg' ) . '/options';
47 # Full path to the dialog and whiptail executeable.
48 my $Dialog = '/usr/bin/dialog';
49 my $Whiptail = '/usr/bin/whiptail';
51 # Strings to use in the dialog|whiptail frontend
52 my $TITLE = 'Import Keys';
53 my $BACKTITLE = 'KeyLookup $Revision: 1.34 $';
54 my $INSTRUCTION = 'Select keys to import:';
56 my @TPUTCOL=('tput', 'cols');
57 my @TPUTROW=('tput', 'lines');
60 # Size of the dialog boxes, will be set in calcDialogSize;
61 my $MAX_UID_FIELD_LEN;
66 # Was the keyserver overriden|given on the command line?
67 # This is used to find out wheter we need to instruct the user
68 # to give the keyserver option to GnuPG.
69 my $keyserverWasSetOnCmdLine = 0;
72 # Queries a remote keyserver (using a proxy if requested) and returns
73 # the keyservers anser. No module is used deliberatly to make this
74 # script run on as many systems as possible.
79 my $requestURL = shift;
80 my $honorproxy = shift;
85 if ($honorproxy && defined $ENV{'http_proxy'}) {
89 if ( $ENV{'http_proxy'} =~ m
#^http://(\S+):(\d+)/?$# ) {
92 } elsif ( $ENV{'http_proxy'} =~ m
#^http://(\S+)/?$# ) {
94 $proxyport = $PROXY_PORT;
96 die ("Unkown http_proxy format");
99 $remote = IO
::Socket
::INET
->new(
101 PeerAddr
=> $proxyserver,
102 PeerPort
=> $proxyport
103 ) || die ("Cannot connect to prox: $!");
105 my $URL=sprintf("http://%s:%s%s/", $server, $port, $requestURL);
106 printf $remote "GET %s HTTP/1.1\n\n", $URL;
108 $remote = IO
::Socket
::INET
->new(
112 ) || die ("Cannot connect to keysever: $!");
114 printf $remote "GET %s HTTP/1.1\nHost: %s\n\n\n", $requestURL, $server;
127 # getHits receives all options as a parameter, calls fetchIT to
128 # query a keyserver, processes the output from the keyserver and
129 # stores it in a datastructure for later use.
133 die ("$0: No keyserver given!\n") unless (defined $options->{'keyserver'});
135 my $result = fetchIt
(
136 $options->{'keyserver'},
138 sprintf("/pks/lookup?op=index&search=%s", $options->{'search'}),
139 $options->{'honor-http-proxy'}
142 $result =~ s/<.*?>//g;
143 $result =~ s/>/>/g;
144 $result =~ s/</</g;
145 $result =~ s/"/"/g;
146 $result =~ s/&/&/g;
148 my @lines = split (/\r?\n/, $result);
160 m
, ^pub
# pub at the start of the line
163 ([A
-Za
-z
]*) # optional type (R or D usually) D
165 ([0-9A
-Za
-z
]+) # keyid 94C09C7F
167 (\d
+)/(\d+)/(\d
+) # date 1999/11/10
169 (.*) # primary user id Peter Palfrader
171 warn ("Unexpected format: $_\n"), next;
173 $currentKey = { 'bits' => $1,
181 $keys{ $3 } = $currentKey;
182 } elsif (defined $currentKey) {
184 push @
{ $currentKey->{'uid'} }, $_;
191 # returns the number of columns of the terminal
194 return $DEFAULTCOLS unless (defined ($pid = open(KID
, "-|")));
201 return (defined $cols) ?
$cols : $DEFAULTCOLS;
204 # returns the number of lines of the terminal
207 return $DEFAULTROWS unless (defined ($pid = open(KID
, "-|")));
214 return (defined $rows) ?
$rows : $DEFAULTROWS;
217 # sets MAX_UID_FIELD_LEN, DIALOGSIZE, and WHIPTAILSIZE
219 my $COLS = &getCols
();
220 my $ROWS = &getRows
();
221 $MAX_UID_FIELD_LEN = $COLS - 27;
222 @DIALOGSIZE = ($ROWS-7, $COLS-7, $ROWS-14);
223 @WHIPTAILSIZE = ($ROWS-7, $COLS-7, $ROWS-14);
226 sub prepareForDialog
{
230 for my $keyid (keys %$keys) {
231 for (@
{ $keys->{$keyid}->{'uid'} }) {
233 $keys->{$keyid}->{'keyid'},
234 length() <= $MAX_UID_FIELD_LEN ?
$_ : substr($_, 0, $MAX_UID_FIELD_LEN-2) . '..',
237 push @keyargs, '-'x8
, '-'x40
, 'off';
250 for my $keyid (keys %$keys) {
251 push @lines, sprintf( "%s%s/%s %s-%s-%s\n",
252 $keys->{$keyid}->{'bits'},
253 $keys->{$keyid}->{'type'},
254 $keys->{$keyid}->{'keyid'},
255 $keys->{$keyid}->{'year'},
256 $keys->{$keyid}->{'month'},
257 $keys->{$keyid}->{'day'} );
258 push @lines, map { ' 'x26
. $_ . "\n" } @
{ $keys->{$keyid}->{'uid'} };
268 # open(SAVEOUT, ">&STDOUT") || die ("Cannot save STDOUT: $!\n");
269 # open(SAVEIN , "<&STDIN" ) || die ("Cannot save STDIN: $!\n");
271 my $pid = open3
( '<&STDIN', '>&STDOUT', \
*ERRFH
, @
$args);
274 my @keys = grep { !$unique{$_}++ }
275 grep { /^[0-9A-Fa-f]{8}$/ }
276 map { s/\s//g; $_ } <ERRFH
>;
279 # open(STDOUT, ">&SAVEOUT") || die "Cannot restore STDOUT: $!\n";
280 # open(STDIN , "<&SAVEIN") || die "Cannot restore STDIN: $!\n";
289 my $frontend = $options->{'frontend'};
290 $frontend = 'dialog' unless (defined $frontend);
292 if ($frontend eq 'dialog') {
293 unless (-x
$Dialog) {
294 warn("Dialog ($Dialog) not executeable/installed. Falling back to Whiptail\n");
295 $frontend = 'whiptail';
298 if ($frontend eq 'whiptail') {
299 unless (-x
$Whiptail ) {
300 warn("Whiptail ($Whiptail) not executeable/installed. Falling back to plain\n");
305 if ( $frontend eq 'dialog' ) {
317 push @ARGS, @
{&prepareForDialog
($keys)};
318 return &callDialog
( \
@ARGS );
319 } elsif ( $frontend eq 'whiptail' ) {
332 push @ARGS, @
{&prepareForDialog
($keys)};
333 return &callDialog
( \
@ARGS );
335 print for (@
{ &prepareForTXT
( $keys ) });
336 if ($keyserverWasSetOnCmdLine) {
337 printf ("Now run gpg --keyserver %s --recv-keys <key ids>\n", $options->{'keyserver'});
339 print ("Now run gpg --recv-keys <key ids>\n");
342 ## If no frontend was selected, or selected frontend was plain,
343 ## exit successfully, otherwise with an exitcode != 0
344 exit (defined $options->{'frontend'} &&
345 $options->{'frontend'} ne "" &&
346 $options->{'frontend'} ne "plain");
354 my @args = ( $options->{'gnupg'},
356 $options->{'keyserver'},
358 for my $keyid (@
$keyids) {
360 my ($cleanid) = $keyid =~ /^([a-zA-Z0-9]{8})$/;
361 warn ("keyid '$keyid' has unexpected format - skipping\n"), next
362 unless defined $cleanid;
363 push @args, $cleanid;
366 print "Calling GnuPG...\n";
367 exec (@args) || die "can't exec gnupg: $!\n"; # won't return
372 my $errorcode = shift;
374 Syntax
: keylookup
[options
] <searchstring
>
377 --keyserver
=<keyserver
> Select keyserver
378 --port
=<port
> Use a non standard port
379 --frontend
=<frontend
> One of whiptail
, dialog
or plain
380 --importall Import all matched
keys
381 --gnupg
=<gnupg
> use this gnupg binary
382 --honor
-http
-proxy honor the http_proxy environment varibale
383 --help
print this message
391 print "keylookup $version\nWritten by Christian Kurz and Peter Palfrader.\n";
396 GetOptions
( \
%options,
406 &version
(0) if ($options{'version'});
407 &usage
(0) if ($options{'help'} || ( scalar(@ARGV) == 0));
409 ## If the keyserver was not given on the command line, lurk into
410 ## GnuPG's config file, it might be defined there.
411 $keyserverWasSetOnCmdLine = defined $options{'keyserver'};
412 unless (defined $options{'keyserver'} &&
413 defined $options{'honor-http-proxy'} ) {
414 if ( open(GNUPGOPTIONS
, $GNUPGOPTIONS) ) {
415 my $keyserver = $options{'keyserver'};
416 while (<GNUPGOPTIONS
>) {
417 $options{'keyserver'} = $1 if (/^\s*keyserver\s+(\S+?)[#\s]/i && ! defined $keyserver);
418 $options{'honor-http-proxy'} = 1 if /^\s*(keyserver-options\s+)honor-http-proxy\s/i;
420 close(GNUPGOPTIONS
) || warn("Cannot close $GNUPGOPTIONS: $!\n");
422 warn ("Cannot open $GNUPGOPTIONS: $!\n");
425 $options{'port'} = $PORT unless (defined $options{'port'});
426 $options{'gnupg'} = $GnuPG unless (defined $options{'gnupg'});
429 $options{'keyserver'} =~ /(.*)/;
430 $options{'keyserver'} = $1;
432 ## Take all additional arguments to the program as a search target,
433 ## escape the string for use in URLs.
434 $options{'search'} = join ' ', @ARGV;
435 $options{'search'} =~ s
/ ( [^A
-Za
-z0
-9] )
436 / '%' . unpack("H2", $1)
438 my $keys = getHits
( \
%options );
441 if ($options{'importall'}) {
442 my @allkeys = keys %$keys;
445 $keyids = selectKeys
($keys, \
%options); # won't return if no interactive frontend
447 &importKeys
($keyids, \
%options) if (scalar @
$keyids); # won't return