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 = '2.1 ($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 my (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) = localtime ($created);
109 $currentKey = { 'bits' => $bits,
110 'type' => (defined $ALGOS{$algo} ?
$ALGOS{$algo} : '#'.$algo),
112 'year' => $year+1900,
115 'revoked' => $revoked,
118 $keys{ $keyid } = $currentKey;
119 } elsif (defined $currentKey && $type eq 'uid') {
120 my ($type, $name) = split /:/;
121 if ($currentKey->{'revoked'} eq 'r') {
122 $name .= ' [REVOKED]';
124 push @
{ $currentKey->{'uid'} }, $name;
133 # returns the number of columns of the terminal
136 return $DEFAULTCOLS unless (defined ($pid = open(KID
, "-|")));
143 return (defined $cols) ?
$cols : $DEFAULTCOLS;
146 # returns the number of lines of the terminal
149 return $DEFAULTROWS unless (defined ($pid = open(KID
, "-|")));
156 return (defined $rows) ?
$rows : $DEFAULTROWS;
159 # sets MAX_UID_FIELD_LEN, DIALOGSIZE, and WHIPTAILSIZE
161 my $COLS = &getCols
();
162 my $ROWS = &getRows
();
163 $MAX_UID_FIELD_LEN = $COLS - 27;
164 @DIALOGSIZE = ($ROWS-7, $COLS-7, $ROWS-14);
165 @WHIPTAILSIZE = ($ROWS-7, $COLS-7, $ROWS-14);
168 sub prepareForDialog
{
172 for my $keyid (keys %$keys) {
173 for (@
{ $keys->{$keyid}->{'uid'} }) {
175 $keys->{$keyid}->{'keyid'},
176 length() <= $MAX_UID_FIELD_LEN ?
$_ : substr($_, 0, $MAX_UID_FIELD_LEN-2) . '..',
179 push @keyargs, '-'x8
, '-'x40
, 'off';
192 for my $keyid (keys %$keys) {
193 push @lines, sprintf( "%s%s/%s %s-%s-%s\n",
194 $keys->{$keyid}->{'bits'},
195 $keys->{$keyid}->{'type'},
196 $keys->{$keyid}->{'keyid'},
197 $keys->{$keyid}->{'year'},
198 $keys->{$keyid}->{'month'},
199 $keys->{$keyid}->{'day'} );
200 push @lines, map { ' 'x26
. $_ . "\n" } @
{ $keys->{$keyid}->{'uid'} };
210 # open(SAVEOUT, ">&STDOUT") || die ("Cannot save STDOUT: $!\n");
211 # open(SAVEIN , "<&STDIN" ) || die ("Cannot save STDIN: $!\n");
213 my $pid = open3
( '<&STDIN', '>&STDOUT', \
*ERRFH
, @
$args);
216 my @keys = grep { !$unique{$_}++ }
217 grep { /^[0-9A-Fa-f]{8}$/ }
218 map { s/\s//g; $_ } <ERRFH
>;
221 # open(STDOUT, ">&SAVEOUT") || die "Cannot restore STDOUT: $!\n";
222 # open(STDIN , "<&SAVEIN") || die "Cannot restore STDIN: $!\n";
231 my $frontend = $options->{'frontend'};
232 $frontend = 'dialog' unless (defined $frontend);
234 if ($frontend eq 'dialog') {
235 unless (-x
$Dialog) {
236 warn("Dialog ($Dialog) not executeable/installed. Falling back to Whiptail\n");
237 $frontend = 'whiptail';
240 if ($frontend eq 'whiptail') {
241 unless (-x
$Whiptail ) {
242 warn("Whiptail ($Whiptail) not executeable/installed. Falling back to plain\n");
247 if ( $frontend eq 'dialog' ) {
259 push @ARGS, @
{&prepareForDialog
($keys)};
260 return &callDialog
( \
@ARGS );
261 } elsif ( $frontend eq 'whiptail' ) {
274 push @ARGS, @
{&prepareForDialog
($keys)};
275 return &callDialog
( \
@ARGS );
277 print for (@
{ &prepareForTXT
( $keys ) });
278 if ($keyserverWasSetOnCmdLine) {
279 printf ("Now run gpg --keyserver %s --recv-keys <key ids>\n", $options->{'keyserver'});
281 print ("Now run gpg --recv-keys <key ids>\n");
284 ## If no frontend was selected, or selected frontend was plain,
285 ## exit successfully, otherwise with an exitcode != 0
286 exit (defined $options->{'frontend'} &&
287 $options->{'frontend'} ne "" &&
288 $options->{'frontend'} ne "plain");
297 if ($options->{'keyserver'}) {
298 push @args, '--keyserver='.$options->{'keyserver'};
300 push @args, '--recv-keys';
301 for my $keyid (@
$keyids) {
303 my ($cleanid) = $keyid =~ /^([a-zA-Z0-9]{8})$/;
304 warn ("keyid '$keyid' has unexpected format - skipping\n"), next
305 unless defined $cleanid;
306 push @args, $cleanid;
309 print "Calling GnuPG...\n";
310 exec (@args) || die "can't exec gnupg: $!\n"; # won't return
315 my $errorcode = shift;
317 Syntax
: keylookup
[options
] <searchstring
>
320 --keyserver
=<keyserver
> Select keyserver
321 --frontend
=<frontend
> One of whiptail
, dialog
or plain
322 --importall Import all matched
keys
323 --help
print this message
331 print "keylookup $version\nWritten by Christian Kurz and Peter Palfrader.\n";
336 GetOptions
( \
%options,
343 &version
(0) if ($options{'version'});
344 &usage
(0) if ($options{'help'} || ( scalar(@ARGV) == 0));
346 ## Take all additional arguments to the program as a search target,
347 ## escape the string for use in URLs.
348 $options{'search'} = join ' ', @ARGV;
349 $options{'search'} =~ s
/ ( [^A
-Za
-z0
-9] )
350 / '%' . unpack("H2", $1)
352 my $keys = getHits
( \
%options );
355 if ($options{'importall'}) {
356 my @allkeys = keys %$keys;
359 $keyids = selectKeys
($keys, \
%options); # won't return if no interactive frontend
361 &importKeys
($keyids, \
%options) if (scalar @
$keyids); # won't return