| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# Copyright (c) 2007 Anthony Towns |
| 4 |
# GNU GPL; v2 or later |
| 5 |
# Gives an overview of what changed between two keyrings |
| 6 |
|
| 7 |
use strict; |
| 8 |
use Cwd q{abs_path}; |
| 9 |
use File::Temp qw(tempdir); |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
|
| 13 |
if (@ARGV != 2) { |
| 14 |
die "usage: jetring-diff keyring1.gpg keyring2.gpg\n"; |
| 15 |
} |
| 16 |
|
| 17 |
# avoid gnupg touching ~/.gnupg |
| 18 |
$ENV{GNUPGHOME}=tempdir("jetring.XXXXXXXXXX", TMPDIR => 1, CLEANUP => 1); |
| 19 |
|
| 20 |
my $l = parse_keyring(shift); |
| 21 |
my $r = parse_keyring(shift); |
| 22 |
|
| 23 |
foreach my $id (sort keys %{$l}) { |
| 24 |
if (not exists $r->{$id}) { |
| 25 |
summary("-", @{$l->{$id}}); |
| 26 |
} |
| 27 |
else { |
| 28 |
my $diff=0; |
| 29 |
my @out; |
| 30 |
|
| 31 |
my %rpackets = map { comparable($_->{'details'}) => $_ } |
| 32 |
@{$r->{$id}}; |
| 33 |
my %lpackets = map { comparable($_->{'details'}) => 1 } |
| 34 |
@{$l->{$id}}; |
| 35 |
|
| 36 |
foreach my $packet (@{$l->{$id}}) { |
| 37 |
if (defined($rpackets{comparable($packet->{'details'})})) { |
| 38 |
push @out, " ".outformat($packet->{'details'}); |
| 39 |
push @out, comparesigs(\$diff, $packet->{'sigs'}, |
| 40 |
$rpackets{comparable($packet->{'details'})}->{'sigs'}); |
| 41 |
} else { |
| 42 |
push @out, "-".outformat($packet->{'details'}); |
| 43 |
$diff = 1; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
foreach my $packet (@{$r->{$id}}) { |
| 48 |
if (! $lpackets{comparable($packet->{'details'})}) { |
| 49 |
push @out, "+".outformat($packet->{'details'}); |
| 50 |
$diff = 1; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
print @out if $diff; |
| 55 |
} |
| 56 |
} |
| 57 |
foreach my $id (sort keys %{$r}) { |
| 58 |
if (not exists $l->{$id}) { |
| 59 |
summary("+", @{$r->{$id}}); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
sub parse_keyring { |
| 64 |
my $k=shift; |
| 65 |
|
| 66 |
$k=abs_path($k); # annoying gpg.. |
| 67 |
my $cache=$k.".cache"; |
| 68 |
|
| 69 |
my $cached=0; |
| 70 |
my $kmtime=(stat($k))[9]; |
| 71 |
if (-e $cache) { |
| 72 |
my $cmtime=(stat($cache))[9]; |
| 73 |
if ($kmtime == $cmtime) { |
| 74 |
open(DUMP, $cache) || die "$cache: $!"; |
| 75 |
$cached=1; |
| 76 |
} |
| 77 |
} |
| 78 |
if (! $cached) { |
| 79 |
open(DUMP, "gpg --options /dev/null --no-default-keyring --no-auto-check-trustdb --keyring $k --list-sigs --fixed-list-mode --with-colons |") |
| 80 |
or die "couldn't dump keyring $k: $!"; |
| 81 |
if (! open(CACHE, ">$cache")) { |
| 82 |
print STDERR "warning: cannot write cache $cache\n"; |
| 83 |
$cache=undef; |
| 84 |
} |
| 85 |
} |
| 86 |
my %keys; |
| 87 |
my $id; |
| 88 |
my $packet; |
| 89 |
while (<DUMP>) { |
| 90 |
if (! $cached && defined $cache) { |
| 91 |
print CACHE $_; |
| 92 |
} |
| 93 |
chomp; |
| 94 |
|
| 95 |
my @fields=split(":", $_); |
| 96 |
$fields[5]="-"; # ignore creation date, varies |
| 97 |
next if $fields[0] eq 'tru'; |
| 98 |
if ($fields[0] ne 'sig' && $fields[0] ne 'rev') { |
| 99 |
# sig and rev fields tag along with the preceding packet, |
| 100 |
# any other fields need their own packet. If there is a |
| 101 |
# previous packet, push it and create a new one. |
| 102 |
if (defined($packet)) { |
| 103 |
push @{$keys{$id}}, $packet; |
| 104 |
undef $packet; |
| 105 |
} |
| 106 |
$packet->{'details'} = \@fields; |
| 107 |
|
| 108 |
# If this is the start of a new public key, save the id for |
| 109 |
# all the subsequent packets |
| 110 |
if ($fields[0] eq 'pub') { |
| 111 |
$id=$fields[4]; |
| 112 |
} |
| 113 |
} else { |
| 114 |
# This is a signature or revocation, it needs a key and |
| 115 |
# packet to go with |
| 116 |
if (! defined $id or !defined($packet)) { |
| 117 |
die "parse error: $_"; |
| 118 |
next; |
| 119 |
} |
| 120 |
push @{$packet->{'sigs'}}, \@fields; |
| 121 |
} |
| 122 |
} |
| 123 |
# Push any last packet still hanging around |
| 124 |
push @{$keys{$id}}, $packet; |
| 125 |
close DUMP; |
| 126 |
|
| 127 |
if (defined $cache) { |
| 128 |
close CACHE; |
| 129 |
utime($kmtime, $kmtime, $cache) || |
| 130 |
print STDERR "warning: failed setting cache time: $!"; |
| 131 |
} |
| 132 |
|
| 133 |
return \%keys; |
| 134 |
} |
| 135 |
|
| 136 |
sub summary { |
| 137 |
my $prefix=shift; |
| 138 |
|
| 139 |
foreach my $record (@_) { |
| 140 |
if (ref $record eq 'HASH') { |
| 141 |
summary($prefix, $record->{$_}) |
| 142 |
foreach reverse sort keys %$record; |
| 143 |
} |
| 144 |
else { |
| 145 |
if ($record->[0] eq 'pub' || $record->[0] eq 'uid') { |
| 146 |
print "$prefix".outformat($record); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
sub outformat { |
| 153 |
return join(":", @{shift()})."\n"; |
| 154 |
} |
| 155 |
|
| 156 |
sub comparable { |
| 157 |
my @record=@{shift()}; |
| 158 |
if ($record[0] eq 'sig') { |
| 159 |
# Displayed user ids for sigs vary, so compare different |
| 160 |
# ones the same. The user-id is what matters. |
| 161 |
$record[9]=""; |
| 162 |
} |
| 163 |
return join(":", @record); |
| 164 |
} |
| 165 |
|
| 166 |
sub comparesigs { |
| 167 |
my $diff = shift; |
| 168 |
my $l = shift; |
| 169 |
my $r = shift; |
| 170 |
my %lseen = map { comparable($_) => 1 } @{$l}; |
| 171 |
my %rseen = map { comparable($_) => 1 } @{$r}; |
| 172 |
my @out; |
| 173 |
|
| 174 |
foreach my $record (@{$l}) { |
| 175 |
if (! $rseen{comparable($record)}) { |
| 176 |
push @out, "-".outformat($record); |
| 177 |
${$diff} = 1; |
| 178 |
} |
| 179 |
} |
| 180 |
foreach my $record (@{$r}) { |
| 181 |
if (! $lseen{comparable($record)}) { |
| 182 |
push @out, "+".outformat($record); |
| 183 |
${$diff} = 1; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return @out; |
| 188 |
} |