File: jetring-diff

package info (click to toggle)
jetring 0.20
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 464 kB
  • ctags: 26
  • sloc: perl: 462; sh: 171; makefile: 25
file content (188 lines) | stat: -rwxr-xr-x 4,238 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl -w

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

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

if (@ARGV != 2) {
	die "usage: jetring-diff keyring1.gpg keyring2.gpg\n";
}

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

my $l = parse_keyring(shift);
my $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 |") 
			or die "couldn't dump keyring $k: $!";
		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] ne 'sig' && $fields[0] ne 'rev') {
			# sig and rev fields tag along with the preceding packet,
			# any other fields need their own packet. If there is a
			# previous packet, push it and create a new one.
			if (defined($packet)) {
				push @{$keys{$id}}, $packet;
				undef $packet;
			}
			$packet->{'details'} = \@fields;
			
			# If this is the start of a new public key, save the id for
			# all the subsequent packets
			if ($fields[0] eq 'pub') {
				$id=$fields[4];
			}
		} else {
			# This is a signature or revocation, it needs a key and
			# packet to go with
			if (! defined $id or !defined($packet)) {
				die "parse error: $_";
				next;
			}
			push @{$packet->{'sigs'}}, \@fields;
		}
	}
	# Push any last packet still hanging around
	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;
}