]>
git.sthu.org Git - pgp-tools.git/blob - caff/pgp-clean
3 # pgp-clean -- remove all non-self signatures from key
6 # Copyright (c) 2004, 2005 Peter Palfrader <peter@palfrader.org>
7 # Copyright (c) 2006 Christoph Berg <cb@df7cb.de>
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
19 # 3. The name of the author may not be used to endorse or promote products
20 # derived from this software without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 pgp-clean -- remove all non-self signatures from key
43 =item B<pgp-clean> [B<-s>] I<keyid> [I<keyid> ...]
49 B<pgp-clean> takes a list of keyids on the command line and outputs an
50 ascii-armored keyring on stdout for each key with all signatures except
51 self-signatures stripped. Its use is to reduce the size of keys sent out after
52 signing (e.g. with B<caff>).
58 =item B<-s> B<--export-subkeys>
60 Do not remove subkeys. (Pruned by default.)
72 =item $HOME/.gnupg/pubring.gpg - default GnuPG keyring
82 Peter Palfrader <peter@palfrader.org>
84 This manpage was written in POD by Christoph Berg <cb@df7cb.de>.
92 use File
::Temp
qw{tempdir
};
98 my $REVISION = '$Rev$';
99 my ($REVISION_NUMER) = $REVISION =~ /(\d+)/;
100 my $VERSION = "0.0.0.$REVISION_NUMER";
108 print STDERR
"[NOTICE] $line\n";
112 print STDERR
"[INFO] $line\n";
116 #print STDERR "[DEBUG] $line\n";
120 #print STDERR "[trace] $line\n";
124 #print STDERR "[trace2] $line\n";
129 stdin
=> IO
::Handle
->new(),
130 stdout
=> IO
::Handle
->new(),
131 stderr
=> IO
::Handle
->new(),
132 status
=> IO
::Handle
->new() );
133 my $handles = GnuPG
::Handles
->new( %fds );
134 return ($fds{'stdin'}, $fds{'stdout'}, $fds{'stderr'}, $fds{'status'}, $handles);
137 sub readwrite_gpg
($$$$$%) {
138 my ($in, $inputfd, $stdoutfd, $stderrfd, $statusfd, %options) = @_;
140 trace
("Entering readwrite_gpg.");
142 my ($first_line, $dummy) = split /\n/, $in;
143 debug
("readwrite_gpg sends ".(defined $first_line ?
$first_line : "<nothing>"));
145 local $INPUT_RECORD_SEPARATOR = undef;
146 my $sout = IO
::Select
->new();
147 my $sin = IO
::Select
->new();
150 trace
("input is $inputfd; output is $stdoutfd; err is $stderrfd; status is ".(defined $statusfd ?
$statusfd : 'undef').".");
152 $inputfd->blocking(0);
153 $stdoutfd->blocking(0);
154 $statusfd->blocking(0) if defined $statusfd;
155 $stderrfd->blocking(0);
156 $sout->add($stdoutfd);
157 $sout->add($stderrfd);
158 $sout->add($statusfd) if defined $statusfd;
161 my ($stdout, $stderr, $status) = ("", "", "");
162 my $exitwhenstatusmatches = $options{'exitwhenstatusmatches'};
163 trace
("doing stuff until we find $exitwhenstatusmatches") if defined $exitwhenstatusmatches;
165 my $readwrote_stuff_this_time = 0;
166 my $do_not_wait_on_select = 0;
167 my ($readyr, $readyw, $written);
168 while ($sout->count() > 0 || (defined($sin) && ($sin->count() > 0))) {
169 if (defined $exitwhenstatusmatches) {
170 if ($status =~ /$exitwhenstatusmatches/m) {
171 trace
("readwrite_gpg found match on $exitwhenstatusmatches");
172 if ($readwrote_stuff_this_time) {
173 trace
("read/write some more\n");
174 $do_not_wait_on_select = 1;
176 trace
("that's it in our while loop.\n");
182 $readwrote_stuff_this_time = 0;
183 trace
("select waiting for ".($sout->count())." fds.");
184 ($readyr, $readyw, undef) = IO
::Select
::select($sout, $sin, undef, $do_not_wait_on_select ?
0 : 1);
185 trace
("ready: write: ".(defined $readyw ?
scalar @
$readyw : 0 )."; read: ".(defined $readyr ?
scalar @
$readyr : 0));
186 for my $wfd (@
$readyw) {
187 $readwrote_stuff_this_time = 1;
188 if (length($in) != $offset) {
189 trace
("writing to $wfd.");
190 $written = $wfd->syswrite($in, length($in) - $offset, $offset);
193 if ($offset == length($in)) {
194 trace
("writing to $wfd done.");
195 unless ($options{'nocloseinput'}) {
197 trace
("$wfd closed.");
204 next unless (defined(@
$readyr)); # Wait some more.
206 for my $rfd (@
$readyr) {
207 $readwrote_stuff_this_time = 1;
209 trace
("reading from $rfd done.");
214 trace
("reading from $rfd.");
215 if ($rfd == $stdoutfd) {
217 trace2
("stdout is now $stdout\n================");
220 if (defined $statusfd && $rfd == $statusfd) {
222 trace2
("status is now $status\n================");
225 if ($rfd == $stderrfd) {
227 trace2
("stderr is now $stderr\n================");
232 trace
("readwrite_gpg done.");
233 return ($stdout, $stderr, $status);
237 my ($gnupghome, $keyid) = @_;
239 my $gpg = GnuPG
::Interface
->new();
240 my %confighash = ( armor
=> 1 );
241 $confighash{'homedir'}=$gnupghome if (defined $gnupghome);
242 $gpg->options->hash_init( %confighash );
243 $gpg->options->meta_interactive( 0 );
244 my ($inputfd, $stdoutfd, $stderrfd, $statusfd, $handles) = make_gpg_fds
();
245 my $pid = $gpg->export_keys(handles
=> $handles, command_args
=> [ $keyid ]);
246 my ($stdout, $stderr, $status) = readwrite_gpg
('', $inputfd, $stdoutfd, $stderrfd, $statusfd);
256 my $KEYEDIT_PROMPT = '^\[GNUPG:\] GET_LINE keyedit.prompt';
257 my $KEYEDIT_DELUID_PROMPT = '^\[GNUPG:\] GET_BOOL keyedit.remove.uid.okay';
258 my $KEYEDIT_DELSIG_PROMPT = '^\[GNUPG:\] GET_BOOL keyedit.delsig';
259 my $KEYEDIT_KEYEDIT_OR_DELSIG_PROMPT = '^\[GNUPG:\] (GET_BOOL keyedit.delsig|GET_LINE keyedit.prompt)';
260 my $KEYEDIT_DELSUBKEY_PROMPT = '^\[GNUPG:\] GET_BOOL keyedit.remove.subkey';
269 print $fd "pgp-clean $VERSION - (c) 2004, 2005, 2006 Peter Palfrader et al.\n";
273 my ($fd, $exitcode) = @_;
275 print $fd "Usage: $PROGRAM_NAME [-s] <keyid> [<keyid> ...]\n";
276 print $fd "-s --export-subkeys do not remove subkeys\n";
280 Getopt
::Long
::config
('bundling');
282 '-h' => \
$params->{'help'},
283 '--help' => \
$params->{'help'},
284 '-V' => \
$params->{'version'},
285 '--version' => \
$params->{'version'},
286 '-s' => \
$params->{'export-subkeys'},
287 '--export-subkeys' => \
$params->{'export-subkeys'},
291 if ($params->{'help'}) {
294 if ($params->{'version'}) {
298 usage
(\
*STDERR
, 1) unless scalar @ARGV >= 1;
301 for my $keyid (@ARGV) {
303 unless ($keyid =~ /^[A-Za-z0-9]{8}([A-Za-z0-9]{8})?$/) {
304 print STDERR
"$keyid is not a keyid.\n";
307 push @KEYIDS, uc($keyid);
316 for my $keyid (@KEYIDS) {
319 my $gpg = GnuPG
::Interface
->new();
320 $gpg->options->meta_interactive( 0 );
321 my ($inputfd, $stdoutfd, $stderrfd, $statusfd, $handles) = make_gpg_fds
();
322 $gpg->options->hash_init( 'extra_args' => [ '--with-colons', '--fixed-list-mode' ] );
323 my $pid = $gpg->list_public_keys(handles
=> $handles, command_args
=> [ $keyid ]);
324 my ($stdout, $stderr, $status) = readwrite_gpg
('', $inputfd, $stdoutfd, $stderrfd, $statusfd);
327 warn ("No data from gpg for list-key $keyid\n");
330 my $keyinfo = $stdout;
331 my @publine = grep { /^pub/ } (split /\n/, $stdout);
332 my ($dummy1, $dummy2, $dummy3, $dummy4, $longkeyid, $dummy6, $dummy7, $dummy8, $dummy9, $dummy10, $dummy11, $flags) = split /:/, pop @publine;
333 my $can_encrypt = $flags =~ /E/;
334 unless (defined $longkeyid) {
335 warn ("Didn't find public keyid in edit dialog of key $keyid.\n");
341 my $asciikey = export_key
(undef, $keyid);
342 if ($asciikey eq '') {
343 warn ("No data from gpg for export $keyid\n");
349 my $this_uid_text = '';
351 debug
("Doing key $keyid, uid $uid_number");
353 # import into temporary gpghome
354 ###############################
355 my $tempdir = tempdir
( "caff-$keyid-XXXXX", DIR
=> '/tmp/', CLEANUP
=> 1);
356 $gpg = GnuPG
::Interface
->new();
357 $gpg->options->hash_init( 'homedir' => $tempdir );
358 $gpg->options->meta_interactive( 0 );
359 ($inputfd, $stdoutfd, $stderrfd, $statusfd, $handles) = make_gpg_fds
();
360 $pid = $gpg->import_keys(handles
=> $handles);
361 ($stdout, $stderr, $status) = readwrite_gpg
($asciikey, $inputfd, $stdoutfd, $stderrfd, $statusfd);
364 if ($status !~ /^\[GNUPG:\] IMPORT_OK/m) {
365 warn ("Could not import $keyid into temporary gnupg.\n");
371 $gpg = GnuPG
::Interface
->new();
372 $gpg->options->hash_init(
373 'homedir' => $tempdir,
374 'extra_args' => [ '--with-colons', '--fixed-list-mode', '--command-fd=0', '--no-tty' ] );
375 ($inputfd, $stdoutfd, $stderrfd, $statusfd, $handles) = make_gpg_fds
();
376 $pid = $gpg->wrap_call(
377 commands
=> [ '--edit' ],
378 command_args
=> [ $keyid ],
379 handles
=> $handles );
381 debug
("Starting edit session");
382 ($stdout, $stderr, $status) = readwrite_gpg
('', $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches
=> $KEYEDIT_PROMPT, nocloseinput
=> 1);
386 my $number_of_subkeys = 0;
391 debug
("Parsing stdout output.");
392 for my $line (split /\n/, $stdout) {
393 debug
("Checking line $line");
394 my ($type, $dummy2, $dummy3, $dummy4, $dummy5, $dummy6, $dummy7, $dummy8, $dummy9, $uidtext) = split /:/, $line;
395 if ($type eq 'sub') {
396 $number_of_subkeys++;
398 next unless ($type eq 'uid' || $type eq 'uat');
399 debug
("line is interesting.");
401 readwrite_gpg
("$i\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches
=> $KEYEDIT_PROMPT, nocloseinput
=> 1);
404 debug
("Parsing stdout output done.");
408 if (!$params->{'export-subkeys'} and $number_of_subkeys > 0) {
409 for (my $i=1; $i<=$number_of_subkeys; $i++) {
410 readwrite_gpg
("key $i\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches
=> $KEYEDIT_PROMPT, nocloseinput
=> 1);
412 readwrite_gpg
("delkey\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches
=> $KEYEDIT_DELSUBKEY_PROMPT, nocloseinput
=> 1);
413 readwrite_gpg
("yes\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches
=> $KEYEDIT_PROMPT, nocloseinput
=> 1);
418 my $signed_by_me = 0;
419 ($stdout, $stderr, $status) =
420 readwrite_gpg
("delsig\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches
=> $KEYEDIT_DELSIG_PROMPT, nocloseinput
=> 1);
422 while($status =~ /$KEYEDIT_DELSIG_PROMPT/m) {
423 # sig:?::17:EA2199412477CAF8:1058095214:::::13x:
424 my @sigline = grep { /^sig/ } (split /\n/, $stdout);
425 $stdout =~ s/\n/\\n/g;
426 notice
("[sigremoval] why are there ".(scalar @sigline)." siglines in that part of the dialog!? got: $stdout") if scalar @sigline >= 2; # XXX
427 my $line = pop @sigline;
429 if (defined $line) { # only if we found a sig here - we never remove revocation packets for instance
430 debug
("[sigremoval] doing line $line.");
431 my ($dummy1, $dummy2, $dummy3, $dummy4, $signer, $created, $dummy7, $dummy8, $dummy9) = split /:/, $line;
432 if ($signer eq $longkeyid) {
433 debug
("[sigremoval] selfsig ($signer).");
436 debug
("[sigremoval] not interested in that sig ($signer).");
440 debug
("[sigremoval] no sig line here, only got: ".$stdout);
442 ($stdout, $stderr, $status) =
443 readwrite_gpg
($answer."\n", $inputfd, $stdoutfd, $stderrfd, $statusfd, exitwhenstatusmatches
=> $KEYEDIT_KEYEDIT_OR_DELSIG_PROMPT, nocloseinput
=> 1);
445 readwrite_gpg
("save\n", $inputfd, $stdoutfd, $stderrfd, $statusfd);
448 $asciikey = export_key
($tempdir, $longkeyid);
449 if ($asciikey eq '') {
450 warn ("No data from gpg for export $longkeyid\n");