This file is indexed.

/usr/bin/keylookup is in signing-party 2.2-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/perl -w

# Copyright (c) 2000, 2002 Christian Kurz <shorty@debian.org>,
# Copyright (c) 2000, 2002, 2005 Peter Palfrader <peter@palfrader.org>
#
# $Id: keylookup 764 2015-01-22 22:48:30Z guilhem-guest $
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2, or (at your option)
#   any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software Foundation,
#   Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
#
# Keylookup is part of pgp-tools:
#           http://pgp-tools.alioth.debian.org/
#           svn://anonscm.debian.org/pgp-tools/trunk
#           http://anonscm.debian.org/viewvc/pgp-tools/trunk/

delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
$|=1; # Always flush buffers


use strict;
use IO::Socket;
use IPC::Open3;
use Getopt::Long;

my $version = '3.0 ($Id: keylookup 764 2015-01-22 22:48:30Z guilhem-guest $)';

# Strings to use in the dialog|whiptail frontend
my $TITLE = 'Import Keys';
my $BACKTITLE = 'KeyLookup $Revision: 764 $';
my $INSTRUCTION = 'Select keys to import:';
#
my @TPUTCOL=('tput', 'cols');
my @TPUTROW=('tput', 'lines');
my $DEFAULTCOLS = 80;
my $DEFAULTROWS = 25;
# Size of the dialog boxes, will be set in calcDialogSize;
my $MAX_UID_FIELD_LEN;
my @DIALOGSIZE;
my @WHIPTAILSIZE;


# Was the keyserver overriden|given on the command line?
# This is used to find out wheter we need to instruct the user
# to give the keyserver option to GnuPG.
my $keyserverWasSetOnCmdLine = 0;


# Maps algorithm numbers to algorithm types as defined in RFC 2400.
my %ALGOS = (
		 1 => 'R',         # RSA
		 2 => 'r',         # RSA encrypt only  (deprecated)
		 3 => 's',         # RSA sign only (deprecated)
		 16 => 'g',        # ElGamal encrypt only
		 20 => 'g',        # ElGamal sign and encrypt (all OpenPGP implementations cryptographically broken, do not use.  no longer part of OpenPGP)
		 17 => 'D'         # DSA
            );


# getHits receives all options as a parameter, calls fetchIT to
# query a keyserver, processes the output from the keyserver and
# stores it in a datastructure for later use.
sub getHits($) {
	my $options = shift;

	my $pid = open(KID, '-|');
	defined ($pid) or die ("Cannot fork: $!\n");
	unless ($pid) {
		close (STDIN);
		open (STDIN, "/dev/null") || die ("Cannot open /dev/null as stdin: $!\n");

		# child
		my @ops = ($ENV{GNUPGBIN} // 'gpg');
		if ($options->{'keyserver'}) {
			push @ops, '--keyserver='.$options->{'keyserver'};
		};
		push @ops, '--command-fd=0';
		push @ops, '--batch';
		push @ops, '--no-tty';
		push @ops, '--with-colons';
		push @ops, '--fixed-list-mode';
		push @ops, '--search';
		push @ops, @{$options->{'search'}};
		exec(@ops);
		die ("Cannot exec GnuPG: $!\n");
	};
	my %keys;
	my $currentKey;

	while (<KID>) {
		chomp;
		if ( $_ eq "" ) { next; }
		my ($type, undef) = split /:/;
		if ($type eq 'pub') {
			my ($type, $keyid, $algo, $bits, $created, undef, $revoked) = split /:/;
			$currentKey = { 'bits' => $bits,
					'type' => (defined $ALGOS{$algo} ? $ALGOS{$algo} : '#'.$algo),
					'keyid' => $keyid,
					'created' => $created,
					'revoked' => $revoked,
					'uid' => []
				      };
			$keys{ $keyid } = $currentKey;
		} elsif (defined $currentKey && $type eq 'uid') {
			my ($type, $name) = split /:/;
			if ($currentKey->{'revoked'} eq 'r') {
				$name .= ' [REVOKED]';
			};
			push @{ $currentKey->{'uid'} }, $name;
		};
	};
	close KID;
	waitpid $pid, 0;

	return \%keys;
};

# returns the number of columns of the terminal
sub getCols {
	my $pid;
	return $DEFAULTCOLS unless (defined ($pid = open(KID, "-|")));
	unless ($pid) {
		exec (@TPUTCOL);
	};
	my $cols = <KID>;
	close KID;
	wait;
	return (defined $cols) ? $cols : $DEFAULTCOLS;
};

# returns the number of lines of the terminal
sub getRows {
	my $pid;
	return $DEFAULTROWS unless (defined ($pid = open(KID, "-|")));
	unless ($pid) {
		exec (@TPUTROW);
	};
	my $rows = <KID>;
	close KID;
	wait;
	return (defined $rows) ? $rows : $DEFAULTROWS;
};

# sets MAX_UID_FIELD_LEN, DIALOGSIZE, and WHIPTAILSIZE
sub calcDialogSize {
	my $COLS = &getCols();
	my $ROWS = &getRows();
	$MAX_UID_FIELD_LEN = $COLS - 27;
	@DIALOGSIZE = ($ROWS-7, $COLS-7, $ROWS-14);
	@WHIPTAILSIZE = ($ROWS-7, $COLS-7, $ROWS-14);
}

sub prepareForDialog {
	my $keys = shift;
	my @keyargs = ();

	for my $keyid (sort {- ($keys->{$a}->{'created'} <=> $keys->{$b}->{'created'})} keys %$keys) {
		for (@{ $keys->{$keyid}->{'uid'} }) {
			push @keyargs,
				$keys->{$keyid}->{'keyid'},
				length() <= $MAX_UID_FIELD_LEN ? $_ : substr($_, 0, $MAX_UID_FIELD_LEN-2) . '..',
				'off';
		};
		my (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) = localtime ($keys->{$keyid}->{'created'});
		push @keyargs, $keys->{$keyid}->{'keyid'}, sprintf( "[created: %s-%s-%s]", $year+1900, $mon+1, $mday ), 'off';
		push @keyargs, '-'x8, '-'x40, 'off';
	};
	pop @keyargs;
	pop @keyargs;
	pop @keyargs;

	return \@keyargs;
};

sub prepareForTXT {
	my $keys = shift;
	my @lines = ();

	for my $keyid (sort {- ($keys->{$a}->{'created'} <=> $keys->{$b}->{'created'})} keys %$keys) {
		my (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) = localtime ($keys->{$keyid}->{'created'});
		push @lines, sprintf( "%s%s/%s %s-%s-%s\n",
		                      $keys->{$keyid}->{'bits'},
		                      $keys->{$keyid}->{'type'},
		                      $keys->{$keyid}->{'keyid'},
		                      $year+1900, $mon+1, $mday );
		push @lines, map { ' 'x26 . $_ . "\n" } @{ $keys->{$keyid}->{'uid'} };
		push @lines, "\n";
	};

	return \@lines;
};

sub callDialog {
	my $args = shift;

	# open(SAVEOUT, ">&STDOUT") || die ("Cannot save STDOUT: $!\n");
	# open(SAVEIN , "<&STDIN" ) || die ("Cannot save STDIN: $!\n");

	my $pid = open3( '<&STDIN', '>&STDOUT', \*ERRFH, @$args);
	
	my %unique;
	my @keys = grep { !$unique{$_}++ }
		# get the keyID; can be 8, 16 or 40 nibbles
		grep /^((([a-zA-Z0-9]{24})?[a-zA-Z0-9]{8})?[a-zA-Z0-9]{8})$/, map { s/\s//g; $_ } <ERRFH>;
	wait;

	# open(STDOUT, ">&SAVEOUT") || die "Cannot restore STDOUT: $!\n";
	# open(STDIN , "<&SAVEIN")  || die "Cannot restore STDIN: $!\n";

	return \@keys;
};

sub selectKeys {
	my $keys = shift;
	my $options = shift;

	my $frontend = $options->{'frontend'};
	$frontend = 'dialog' unless (defined $frontend);

	if ($frontend eq 'dialog') {
		unless (`which dialog` && $? == 0) {
			warn("Dialog not executeable/installed. Falling back to Whiptail\n");
			$frontend = 'whiptail';
		}
	};
	if ($frontend eq 'whiptail') {
		unless (`which whiptail` && $? == 0 ) {
			warn("Whiptail not executeable/installed. Falling back to plain\n");
			$frontend = 'plain';
		}
	};

	if ( $frontend eq 'dialog' ) {
		calcDialogSize;
		my @ARGS = (
			'dialog',
			'--backtitle',
			$BACKTITLE,
			'--separate-output',
			'--title',
			$TITLE,
			'--checklist',
			$INSTRUCTION,
			@DIALOGSIZE);
		push @ARGS, @{&prepareForDialog($keys)};
		return &callDialog( \@ARGS );
	} elsif ( $frontend eq 'whiptail' ) {
		calcDialogSize;
		my @ARGS = (
			'whiptail',
			'--backtitle',
			$BACKTITLE,
			'--separate-output',
			'--title',
			$TITLE,
			'--checklist',
			$INSTRUCTION,
			@WHIPTAILSIZE,
			'--');
		push @ARGS, @{&prepareForDialog($keys)};
		return &callDialog( \@ARGS );
	} else {
		print for (@{ &prepareForTXT( $keys ) });
		if ($keyserverWasSetOnCmdLine) {
			printf ("Now run gpg --keyserver %s --recv-keys <key ids>\n", $options->{'keyserver'});
		} else {
			print ("Now run gpg --recv-keys <key ids>\n");
		};

		## If no frontend was selected, or selected frontend was plain,
		## exit successfully, otherwise with an exitcode != 0
		exit (defined $options->{'frontend'} &&
		      $options->{'frontend'} ne "" &&
		      $options->{'frontend'} ne "plain");
	};
};

sub importKeys {
	my $keyids = shift;
	my $options = shift;

	my @args = ($ENV{GNUPGBIN} // 'gpg');
	if ($options->{'keyserver'}) {
		push @args, '--keyserver='.$options->{'keyserver'};
	};
	push @args,  '--recv-keys';
	for my $keyid (@$keyids) {
		# untaint keyids
		my ($cleanid) = $keyid =~ /^((([a-zA-Z0-9]{24})?[a-zA-Z0-9]{8})?[a-zA-Z0-9]{8})$/;
		warn ("keyid '$keyid' has unexpected format - skipping\n"), next
			unless defined $cleanid;
		push @args, $cleanid;
	}

	print "Calling GnuPG...\n";
	exec (@args) || die "can't exec gnupg: $!\n"; # won't return
};


sub usage {
	my $errorcode = shift;
	print << 'EOF'
Syntax: keylookup [options] <searchstring>

Options:
	--keyserver=<keyserver>	Select keyserver
	--frontend=<frontend>	One of whiptail, dialog or plain
	--importall		Import all matched keys
	--help			print this message

EOF
;
	exit($errorcode);
};

sub version {
	print "keylookup $version\nWritten by Christian Kurz and Peter Palfrader.\n";
	exit(0);
};

	my %options;
	GetOptions( \%options,
		'keyserver=s',
		'frontend=s',
		'importall',
		'version',
		'help') or
		&usage(1);
	&version(0) if ($options{'version'});
	&usage(0) if ($options{'help'} || ( scalar(@ARGV) == 0));

	## Take all additional arguments to the program as a search target,
	## escape the string for use in URLs.
	$options{'search'} = \@ARGV;
	my $keys = getHits( \%options );
	my $keyids;

	if (scalar keys %$keys == 0) {
		print "GnuPG did not find any keys matching your search string.\n";
		exit 0;
	};
	if ($options{'importall'}) {
		my @allkeys = keys %$keys;
		$keyids = \@allkeys;
	} else {
		$keyids = selectKeys($keys, \%options); # won't return if no interactive frontend
	};
	&importKeys($keyids, \%options) if (scalar @$keyids);	# won't return