summaryrefslogtreecommitdiffstats
path: root/scripts/gpg-diff
blob: 29f45ca098fef312c8ef6be03e623998aa6dc9e9 (plain)
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
#!/usr/bin/perl -w

# Copyright (c) 2007 Anthony Towns
# GNU GPL; v2 or later
# Gives an overview of what changed between two keyrings

# Take from jetring-diff and modified to be suitable for git.
# Copyright (c) 2007 Jonathan McDowell <noodles@earth.li>

use strict;
use Cwd q{abs_path};
use File::Temp qw(tempdir);
use warnings;
use strict;

if (@ARGV != 2 and @ARGV != 7) {
	die "usage: gpg-diff old.gpg new.gpg | path old.gpg old-hex old-mode ".
			"new.gpg new-hex new-mode\n";
}

# avoid gnupg touching ~/.gnupg
$ENV{GNUPGHOME}=tempdir("jetring.XXXXXXXXXX", TMPDIR => 1, CLEANUP => 1);

my ($l, $r);

if (@ARGV == 7) {
	# Print a diff style header
	print "gpg-diff a/$ARGV[0] b/$ARGV[4]\n";
	print "--- a/$ARGV[0]\n";
	print "+++ b/$ARGV[4]\n";
	print "\n";

	if ($ARGV[4] eq '/dev/null') {
		print "Key deleted\n";
		exit 0;
	}

	$l = parse_keyring($ARGV[1]);
	$r = parse_keyring($ARGV[4]);
} else {
	$l = parse_keyring(shift);
	$r = parse_keyring(shift);
}

foreach my $id (sort keys %{$l}) {
	if (not exists $r->{$id}) {
		summary("-", @{$l->{$id}});
	}
	else {
		my $diff=0;
		my @out;

		my %rpackets = map { comparable($_->{'details'}) => $_ }
				@{$r->{$id}};
		my %lpackets = map { comparable($_->{'details'}) => 1 }
				@{$l->{$id}};

		foreach my $packet (@{$l->{$id}}) {
			if (defined($rpackets{comparable($packet->{'details'})})) {
				push @out, " ".outformat($packet->{'details'});
				push @out, comparesigs(\$diff, $packet->{'sigs'},
						$rpackets{comparable($packet->{'details'})}->{'sigs'});
			} else {
				push @out, "-".outformat($packet->{'details'});
				$diff = 1;
			}
		}

		foreach my $packet (@{$r->{$id}}) {
			if (! $lpackets{comparable($packet->{'details'})}) {
				push @out, "+".outformat($packet->{'details'});
				$diff = 1;
			}
		}

		print @out if $diff;
	}
}
foreach my $id (sort keys %{$r}) {
	if (not exists $l->{$id}) {
		summary("+", @{$r->{$id}});
	}
}

sub parse_keyring {
	my $k=shift;

	$k=abs_path($k); # annoying gpg..
	my $cache=$k.".cache";

	my $cached=0;
	my $kmtime=(stat($k))[9];
	if (-e $cache) {
		my $cmtime=(stat($cache))[9];
		if ($kmtime == $cmtime) {
			open(DUMP, $cache) || die "$cache: $!";
			$cached=1;
		}
	}
	if (! $cached) {
		open(DUMP, "gpg --options /dev/null --no-default-keyring ".
			"--no-auto-check-trustdb --keyring $k --list-sigs ".
			"--fixed-list-mode --with-colons -q |") 
			or die "couldn't dump keyring $k: $!";
# Disable caching for the moment
#		if (! open(CACHE, ">$cache")) {
#			print STDERR "warning: cannot write cache $cache\n";
			$cache=undef;
#		}
	}
	my %keys;
	my $id;
	my $packet;
	while (<DUMP>) {
		if (! $cached && defined $cache) {
			print CACHE $_;
		}
		chomp;

		my @fields=split(":", $_);
		$fields[5]="-"; # ignore creation date, varies
		next if $fields[0] eq 'tru';
		if ($fields[0] eq 'pub') {
			$id=$fields[4];
		}
		if ($fields[0] ne 'sig' && $fields[0] ne 'rev') {
			if (defined($packet)) {
				push @{$keys{$id}}, $packet;
				undef $packet;
			}
			$packet->{'details'} = \@fields;
		} else {
			if (! defined $id or !defined($packet)) {
				die "parse error: $_";
				next;
			}
			push @{$packet->{'sigs'}}, \@fields;
		}
	}
	push @{$keys{$id}}, $packet;
	close DUMP;

	if (defined $cache) {
		close CACHE;
		utime($kmtime, $kmtime, $cache) ||
			print STDERR "warning: failed setting cache time: $!";
	}

	return \%keys;
}

sub summary {
	my $prefix=shift;

	foreach my $record (@_) {
		if (ref $record eq 'HASH') {
			summary($prefix, $record->{$_})
				foreach reverse sort keys %$record;
		}
		else {
			if ($record->[0] eq 'pub' || $record->[0] eq 'uid') {
				print "$prefix".outformat($record);
			}
		}
	}
}

sub outformat {
	return join(":", @{shift()})."\n";
}

sub comparable {
	my @record=@{shift()};
	if ($record[0] eq 'sig') {
		# Displayed user ids for sigs vary, so compare different
		# ones the same. The user-id is what matters.
		$record[9]="";
	}
	return join(":", @record);
}

sub comparesigs {
	my $diff = shift;
	my $l = shift;
	my $r = shift;
	my %lseen = map { comparable($_) => 1 } @{$l};
	my %rseen = map { comparable($_) => 1 } @{$r};
	my @out;

	foreach my $record (@{$l}) {
		if (! $rseen{comparable($record)}) {
			push @out, "-".outformat($record);
			${$diff} = 1;
		}
	}
	foreach my $record (@{$r}) {
		if (! $lseen{comparable($record)}) {
			push @out, "+".outformat($record);
			${$diff} = 1;
		}
	}

	return @out;
}