This file is indexed.

/usr/bin/pgp-clean 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/perl -w

# pgp-clean  --  remove all non-self signatures from key
# $Id: pgp-clean 764 2015-01-22 22:48:30Z guilhem-guest $
#
# Copyright (c) 2004, 2005 Peter Palfrader <peter@palfrader.org>
# Copyright (c) 2006 Christoph Berg <cb@df7cb.de>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=pod

=head1 NAME

pgp-clean -- remove all non-self signatures from key

=head1 SYNOPSIS

=over

=item B<pgp-clean> [B<-s>] I<keyid> [I<keyid> ...]

=back

=head1 DESCRIPTION

B<pgp-clean> takes a list of keyids on the command line and outputs an
ascii-armored keyring on stdout for each key with all signatures except
self-signatures stripped.  Its use is to reduce the size of keys sent out after
signing (e.g. with B<caff>).

=head1 OPTIONS

=over

=item B<-s> B<--export-subkeys>

Do not remove subkeys. (Pruned by default.)

=item I<keyid>

Use this key.

=back

=head1 ENVIRONMENT

=over

=item I<HOME>

The default home directory.

=item I<GNUPGBIN>

The gpg binary.  Default: C<"gpg">.

=item I<GNUPGHOME>

The default working directory for gpg.  Default: C<$HOME/.gnupg>.

=back

=head1 FILES

=over

=item $HOME/.gnupg/pubring.gpg  -  default GnuPG keyring

=back

=head1 SEE ALSO

caff(1), gpg(1).

=head1 AUTHOR

Peter Palfrader <peter@palfrader.org>

This manpage was written in POD by Christoph Berg <cb@df7cb.de>.

=cut

use strict;
use IO::Handle;
use English '-no_match_vars';
use File::Path;
use File::Temp qw{tempdir};
use Fcntl;
use IO::Select;
use Getopt::Long;
use GnuPG::Interface;

my $REVISION = '$Rev: 764 $';
my ($REVISION_NUMER) = $REVISION =~ /(\d+)/;
my $VERSION = "0.0.0.$REVISION_NUMER";

###########
# functions
###########

sub notice($) {
	my ($line) = @_;
	print STDERR "[NOTICE] $line\n";
};
sub info($) {
	my ($line) = @_;
	print STDERR "[INFO] $line\n";
};
sub debug($) {
	my ($line) = @_;
	#print STDERR "[DEBUG] $line\n";
};
sub trace($) {
	my ($line) = @_;
	#print STDERR "[trace] $line\n";
};
sub trace2($) {
	my ($line) = @_;
	#print STDERR "[trace2] $line\n";
};

sub make_gpg_fds() {
	my %fds = (
		stdin => IO::Handle->new(),
		stdout => IO::Handle->new(),
		stderr => IO::Handle->new(),
		status => IO::Handle->new() );
	my $handles = GnuPG::Handles->new( %fds );
	return ($fds{'stdin'}, $fds{'stdout'}, $fds{'stderr'}, $fds{'status'}, $handles);
};

sub readwrite_gpg($$$$$%) {
	my ($in, $inputfd, $stdoutfd, $stderrfd, $statusfd, %options) = @_;

	trace("Entering readwrite_gpg.");

	my ($first_line, $dummy) = split /\n/, $in;
	debug("readwrite_gpg sends ".(defined $first_line ? $first_line : "<nothing>"));

	local $INPUT_RECORD_SEPARATOR = undef;
	my $sout = IO::Select->new();
	my $sin = IO::Select->new();
	my $offset = 0;

	trace("input is $inputfd; output is $stdoutfd; err is $stderrfd; status is ".(defined $statusfd ? $statusfd : 'undef').".");

	$inputfd->blocking(0);
	$stdoutfd->blocking(0);
	$statusfd->blocking(0) if defined $statusfd;
	$stderrfd->blocking(0);
	$sout->add($stdoutfd);
	$sout->add($stderrfd);
	$sout->add($statusfd) if defined $statusfd;
	$sin->add($inputfd);

	my ($stdout, $stderr, $status) = ("", "", "");
	my $exitwhenstatusmatches = $options{'exitwhenstatusmatches'};
	trace("doing stuff until we find $exitwhenstatusmatches") if defined $exitwhenstatusmatches;

	my $readwrote_stuff_this_time = 0;
	my $do_not_wait_on_select = 0;
	my ($readyr, $readyw, $written);
	while ($sout->count() > 0 || (defined($sin) && ($sin->count() > 0))) {
		if (defined $exitwhenstatusmatches) {
			if ($status =~ /$exitwhenstatusmatches/m) {
				trace("readwrite_gpg found match on $exitwhenstatusmatches");
				if ($readwrote_stuff_this_time) {
					trace("read/write some more\n");
					$do_not_wait_on_select = 1;
				} else {
					trace("that's it in our while loop.\n");
					last;
				}
			};
		};

		$readwrote_stuff_this_time = 0;
		trace("select waiting for ".($sout->count())." fds.");
		($readyr, $readyw, undef) = IO::Select::select($sout, $sin, undef, $do_not_wait_on_select ? 0 : 1);
		trace("ready: write: ".(defined $readyw ? scalar @$readyw : 0 )."; read: ".(defined $readyr ? scalar @$readyr : 0));
		for my $wfd (@$readyw) {
			$readwrote_stuff_this_time = 1;
			if (length($in) != $offset) {
				trace("writing to $wfd.");
				$written = $wfd->syswrite($in, length($in) - $offset, $offset);
				$offset += $written;
			};
			if ($offset == length($in)) {
				trace("writing to $wfd done.");
				unless ($options{'nocloseinput'}) {
					close $wfd;
					trace("$wfd closed.");
				};
				$sin->remove($wfd);
				$sin = undef;
			}
		}

		next unless defined $readyr and @$readyr; # Wait some more.

		for my $rfd (@$readyr) {
			$readwrote_stuff_this_time = 1;
			if ($rfd->eof) {
				trace("reading from $rfd done.");
				$sout->remove($rfd);
				close($rfd);
				next;
			}
			trace("reading from $rfd.");
			if ($rfd == $stdoutfd) {
				$stdout .= <$rfd>;
				trace2("stdout is now $stdout\n================");
				next;
			}
			if (defined $statusfd && $rfd == $statusfd) {
				$status .= <$rfd>;
				trace2("status is now $status\n================");
				next;
			}
			if ($rfd == $stderrfd) {
				$stderr .= <$rfd>;
				trace2("stderr is now $stderr\n================");
				next;
			}
		}
	}
	trace("readwrite_gpg done.");
	return ($stdout, $stderr, $status);
};

sub export_key($$) {
	my ($gnupghome, $keyid) = @_;

	my $gpg = GnuPG::Interface->new();
	$gpg->call( $ENV{GNUPGBIN} ) if defined $ENV{GNUPGBIN};
	my %confighash = ( armor => 1 );
	$confighash{'homedir'}=$gnupghome if (defined $gnupghome);
	$gpg->options->hash_init( %confighash );
	$gpg->options->meta_interactive( 0 );
	my ($inputfd, $stdoutfd, $stderrfd, $statusfd, $handles) = make_gpg_fds();
	my $pid = $gpg->export_keys(handles => $handles, command_args => [ $keyid ]);
	my ($stdout, $stderr, $status) = readwrite_gpg('', $inputfd, $stdoutfd, $stderrfd, $statusfd);
	waitpid $pid, 0;

	return $stdout;
};

##################
# global variables
##################

my $KEYEDIT_PROMPT = '^\[GNUPG:\] GET_LINE keyedit.prompt';
my $KEYEDIT_DELUID_PROMPT = '^\[GNUPG:\] GET_BOOL keyedit.remove.uid.okay';
my $KEYEDIT_DELSIG_PROMPT = '^\[GNUPG:\] GET_BOOL keyedit.delsig';
my $KEYEDIT_KEYEDIT_OR_DELSIG_PROMPT = '^\[GNUPG:\] (GET_BOOL keyedit.delsig|GET_LINE keyedit.prompt)';
my $KEYEDIT_DELSUBKEY_PROMPT = '^\[GNUPG:\] GET_BOOL keyedit.remove.subkey';
my $params;

###################
# argument handling
###################

sub version($) {
	my ($fd) = @_;
	print $fd "pgp-clean $VERSION - (c) 2004, 2005, 2006 Peter Palfrader et al.\n";
};

sub usage($$) {
	my ($fd, $exitcode) = @_;
	version($fd);
	print $fd "Usage: $PROGRAM_NAME [-s] <keyid> [<keyid> ...]\n";
	print $fd "-s --export-subkeys  do not remove subkeys\n";
	exit $exitcode;
};

Getopt::Long::config('bundling');
if (!GetOptions (
	'-h'               =>  \$params->{'help'},
	'--help'           =>  \$params->{'help'},
	'-V'               =>  \$params->{'version'},
	'--version'        =>  \$params->{'version'},
	'-s'               =>  \$params->{'export-subkeys'},
	'--export-subkeys' =>  \$params->{'export-subkeys'},
	)) {
	usage(\*STDERR, 1);
};
if ($params->{'help'}) {
	usage(\*STDOUT, 0);
};
if ($params->{'version'}) {
	version(\*STDOUT);
	exit(0);
};
usage(\*STDERR, 1) unless scalar @ARGV >= 1;

my @KEYIDS;
for my $keyid (@ARGV) {
	$keyid =~ s/^0x//i;
	unless ($keyid =~ /^[A-Za-z0-9]{8}([A-Za-z0-9]{8})?$/) {
		print STDERR "$keyid is not a keyid.\n";
		usage(\*STDERR, 1);
	};
	push @KEYIDS, uc($keyid);
};



##################
# export and prune
##################
KEYS:
for my $keyid (@KEYIDS) {
	# get key listing
	#################
	my $gpg = GnuPG::Interface->new();
	$gpg->call( $ENV{GNUPGBIN} ) if defined $ENV{GNUPGBIN};
	$gpg->options->meta_interactive( 0 );
	my ($inputfd, $stdoutfd, $stderrfd, $statusfd, $handles) = make_gpg_fds();
	$gpg->options->hash_init( 'extra_args' => [ '--with-colons', '--fixed-list-mode' ] );
	my $pid = $gpg->list_public_keys(handles => $handles, command_args => [ $keyid ]);
	my ($stdout, $stderr, $status) = readwrite_gpg('', $inputfd, $stdoutfd, $stderrfd, $statusfd);
	waitpid $pid, 0;
	if ($stdout eq '') {
		warn ("No data from gpg for list-key $keyid\n");
		next;
	};
	my $keyinfo = $stdout;
	my @publine = grep /^pub/, (split /\n/, $stdout);
	my ($dummy1, $dummy2, $dummy3, $dummy4, $longkeyid, $dummy6, $dummy7, $dummy8, $dummy9, $dummy10, $dummy11, $flags) = split /:/, pop @publine;
	my $can_encrypt = $flags =~ /E/;
	unless (defined $longkeyid) {
		warn ("Didn't find public keyid in edit dialog of key $keyid.\n");
		next;
	};

	# export the key
	################
	my $asciikey = export_key(undef, $keyid);
	if ($asciikey eq '') {
		warn ("No data from gpg for export $keyid\n");
		next;
	};

	my @UIDS;
	my $uid_number = 0;
	my $this_uid_text = '';
	$uid_number++;
	debug("Doing key $keyid, uid $uid_number");

	# import into temporary gpghome
	###############################
	my $tempdir = tempdir( "caff-$keyid-XXXXX", DIR => '/tmp/', CLEANUP => 1);
	$gpg = GnuPG::Interface->new();
	$gpg->call( $ENV{GNUPGBIN} ) if defined $ENV{GNUPGBIN};
	$gpg->options->hash_init( 'homedir' => $tempdir );
	$gpg->options->meta_interactive( 0 );
	($inputfd, $stdoutfd, $stderrfd, $statusfd, $handles) = make_gpg_fds();
	$pid = $gpg->import_keys(handles => $handles);
	($stdout, $stderr, $status) = readwrite_gpg($asciikey, $inputfd, $stdoutfd, $stderrfd, $statusfd);
	waitpid $pid, 0;

	if ($status !~ /^\[GNUPG:\] IMPORT_OK/m) {
		warn ("Could not import $keyid into temporary gnupg.\n");
		next;
	};

	# prune it
	##########
	$gpg = GnuPG::Interface->new();
	$gpg->call( $ENV{GNUPGBIN} ) if defined $ENV{GNUPGBIN};
	$gpg->options->hash_init(
		'homedir' => $tempdir,
		'extra_args' => [ '--with-colons', '--fixed-list-mode', '--command-fd=0', '--no-tty' ] );
	($inputfd, $stdoutfd, $stderrfd, $statusfd, $handles) = make_gpg_fds();
	$pid = $gpg->wrap_call(
		commands     => [ '--edit-key' ],
		command_args => [ $keyid ],
		handles      => $handles );

	debug("Starting edit session");
	($stdout, $stderr, $status) = readwrite_gpg('', $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches => $KEYEDIT_PROMPT, nocloseinput => 1);

	# mark all uids
	###################
	my $number_of_subkeys = 0;
	my $i = 1;
	my $have_one = 0;
	my $is_uat = 0;
	my $delete_some = 0;
	debug("Parsing stdout output.");
	for my $line (split /\n/, $stdout) {
		debug("Checking line $line");
		my ($type, $dummy2, $dummy3, $dummy4, $dummy5, $dummy6, $dummy7, $dummy8, $dummy9, $uidtext) = split /:/, $line;
		if ($type eq 'sub') {
			$number_of_subkeys++;
		};
		next unless ($type eq 'uid' || $type eq 'uat');
		debug("line is interesting.");
		debug("mark uid.");
		readwrite_gpg("$i\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches => $KEYEDIT_PROMPT, nocloseinput => 1);
		$i++;
	};
	debug("Parsing stdout output done.");

	# delete subkeys
	################
	if (!$params->{'export-subkeys'} and $number_of_subkeys > 0) {
		for (my $i=1; $i<=$number_of_subkeys; $i++) {
			readwrite_gpg("key $i\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches => $KEYEDIT_PROMPT, nocloseinput => 1);
		};
		readwrite_gpg("delkey\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches => $KEYEDIT_DELSUBKEY_PROMPT, nocloseinput => 1);
		readwrite_gpg("yes\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches => $KEYEDIT_PROMPT, nocloseinput => 1);
	};

	# delete signatures
	###################
	my $signed_by_me = 0;
	($stdout, $stderr, $status) =
		readwrite_gpg("delsig\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches => $KEYEDIT_DELSIG_PROMPT, nocloseinput => 1);

	while($status =~ /$KEYEDIT_DELSIG_PROMPT/m) {
		# sig:?::17:EA2199412477CAF8:1058095214:::::13x:
		my @sigline = grep /^sig/, (split /\n/, $stdout);
		$stdout =~ s/\n/\\n/g;
		notice("[sigremoval] why are there ".(scalar @sigline)." siglines in that part of the dialog!? got: $stdout") if scalar @sigline >= 2; # XXX
		my $line = pop @sigline;
		my $answer = "no";
		if (defined $line) { # only if we found a sig here - we never remove revocation packets for instance
			debug("[sigremoval] doing line $line.");
			my ($dummy1, $dummy2, $dummy3, $dummy4, $signer, $created, $dummy7, $dummy8, $dummy9) = split /:/, $line;
			if ($signer eq $longkeyid) {
				debug("[sigremoval] selfsig ($signer).");
				$answer = "no";
			} else {
				debug("[sigremoval] not interested in that sig ($signer).");
				$answer = "yes";
			};
		} else {
			debug("[sigremoval] no sig line here, only got: ".$stdout);
		};
		($stdout, $stderr, $status) =
			readwrite_gpg($answer."\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches => $KEYEDIT_KEYEDIT_OR_DELSIG_PROMPT, nocloseinput => 1);
	};
	readwrite_gpg("save\n", $inputfd, $stdoutfd, $stderrfd, $statusfd);
	waitpid $pid, 0;

	$asciikey = export_key($tempdir, $longkeyid);
	if ($asciikey eq '') {
		warn ("No data from gpg for export $longkeyid\n");
		next;
	};


	print $asciikey;
}