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