Debian Codegraph @

jetring-review

raw
1 #!/usr/bin/perl
2 # Given a keyring and a changeset, shows what gpg would do if it
3 # applied the changeset to the keyring. The keyring is not modified.
4 use warnings;
5 use strict;
6 use File::Temp qw(tempdir);
7 use Cwd q{abs_path};
8
9 my @allowed_actions=qw(import edit-key delete-key);
10 my @gpgopts=qw(--command-fd 0 --no-auto-check-trustdb --options /dev/null
11 --no-default-keyring);
12
13 my $diff=0;
14 if (@ARGV && $ARGV[0] eq '-d') {
15 $diff=1;
16 shift;
17 }
18 my $keyring=shift || usage();
19 my $changeset=shift || usage();
20
21 # avoid gnupg touching ~/.gnupg
22 $ENV{GNUPGHOME}=tempdir("jetring.XXXXXXXXXX", TMPDIR => 1, CLEANUP => 1);
23
24 $keyring=abs_path($keyring); # gpg works better with an absolute path
25
26 my $testring=$keyring.".tmp.$$";
27 if (-e $testring) {
28 die "$testring exists";
29 }
30 system("cp", $keyring, $testring) == 0 || die "copy failed";
31 push @gpgopts, "--keyring", $testring;
32
33 sub END {
34 if (defined $testring) {
35 unlink $testring;
36 unlink $testring."~";# gpg backup file
37 unlink $testring.".cache"; # generated by jetring-diff
38 }
39 }
40
41 my %fields;
42 my $field;
43 open(CHANGESET, "<", $changeset) || die "$changeset: $!";
44 while (<CHANGESET>) {
45 chomp;
46 if ($_ eq "-----BEGIN PGP SIGNED MESSAGE-----") {
47 <CHANGESET> for 1..2;
48 next;
49 }
50 elsif ($_ eq "-----BEGIN PGP SIGNATURE-----") {
51 last;
52 }
53 if (/^([^\s]+):(?:\s+(.*))?/) {
54 $field=lc $1;
55 if (defined $2) {
56 $fields{$field}=$2;
57 }
58 else {
59 $fields{$field}='';
60 }
61 }
62 elsif (/^\s+(.*)/ && defined $field) {
63 $fields{$field}.="\n" if length $fields{$field};
64 $fields{$field}.=$1;
65 }
66 elsif ($_ eq "") {
67 process() if defined $field;
68 %fields=();
69 $field=undef;
70 }
71 else {
72 die "parse error on line $. of $changeset";
73 }
74 }
75 close CHANGESET;
76 process() if defined $field;
77
78 if ($diff) {
79 system("jetring-diff", $keyring, $testring) == 0 ||
80 die "jetring-diff exited nonzero";
81 }
82
83 sub process {
84 if (! exists $fields{action}) {
85 die "$changeset missing action field";
86 }
87 my @action=split(' ', $fields{action});
88 my $command=shift @action;
89 if (! grep { $_ eq $command } @allowed_actions) {
90 die "$changeset contains disallowed action \"$command\"";
91 }
92 if (! exists $fields{data}) {
93 die "$changeset missing data field";
94 }
95
96 print "gpg --$command @action\n";
97 my $pid = open(GPG, "|-");
98 $SIG{PIPE} = 'IGNORE';
99 if (! $pid) {
100 exec("gpg", @gpgopts, "--$command", @action) ||
101 die("failed to run gpg");
102 }
103 $|=1;
104 GPG->autoflush(1);
105 foreach my $line (split("\n", $fields{data})) {
106 print ">> $line\n" if $command ne 'import';
107 print GPG "$line\n" || die "failed talking to gpg";
108 sleep 1 if $command ne 'import'; # makes output clearer
109 }
110 close GPG || die "gpg exited nonzero";
111 print "gpg operation complete\n\n";
112 }
113
114 sub usage {
115 die "Usage: jetring-review [-d] keyring changeset\n";
116 }