convert gpg keyring processing to less nesting and abort earlier if possible

This commit is contained in:
Johannes 'josch' Schauer 2020-01-22 00:29:38 +01:00
parent c26ec4d6fc
commit ae15fe3d9f
Signed by: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -3183,10 +3183,16 @@ sub main() {
) { ) {
$keyring $keyring
= '/usr/share/keyrings/debian-archive-keyring.gpg'; = '/usr/share/keyrings/debian-archive-keyring.gpg';
} else {
last;
} }
# we can only check if we need the signed-by entry if we u # we can only check if we need the signed-by entry if we u
# automatically chosen keyring exists # automatically chosen keyring exists
if (defined $keyring && -e $keyring) { if (!defined $keyring || !-e $keyring) {
last;
}
# we can only check key material if gpg is installed # we can only check key material if gpg is installed
my $gpghome = tempdir( my $gpghome = tempdir(
"mmdebstrap.gpghome.XXXXXXXXXXXX", "mmdebstrap.gpghome.XXXXXXXXXXXX",
@ -3209,14 +3215,32 @@ sub main() {
} }
# we only want to check if the gpg command exists # we only want to check if the gpg command exists
close $fh; close $fh;
if ($? == 0 && defined $ret && !defined $message) { if ($? != 0 || !defined $ret || defined $message) {
info "gpg --version failed: cannot determine the right"
. " signed-by value";
last;
}
# find all the fingerprints of the keys apt currently # find all the fingerprints of the keys apt currently
# knows about # knows about
my @keyringopts = ();
opendir my $dh, "$options->{apttrustedparts}"
or error "cannot read $options->{apttrustedparts}";
while (my $filename = readdir $dh) {
if ($filename !~ /\.(asc|gpg)$/) {
next;
}
push @keyringopts, '--keyring',
"$options->{apttrustedparts}/$filename";
}
if (-e $options->{apttrusted}) {
push @keyringopts, '--keyring', $options->{apttrusted};
}
my @aptfingerprints = (); my @aptfingerprints = ();
my $collect_fingerprints = sub { if (scalar @keyringopts == 0) {
my $filename = shift; $signedby = " [signed-by=\"$keyring\"]";
open my $fh, '-|', @gpgcmd, '--keyring', last;
$filename, '--with-colons', }
open my $fh, '-|', @gpgcmd, @keyringopts, '--with-colons',
'--list-keys' // error "failed to fork(): $!"; '--list-keys' // error "failed to fork(): $!";
while (my $line = <$fh>) { while (my $line = <$fh>) {
if ($line !~ /^fpr:::::::::([^:]+):/) { if ($line !~ /^fpr:::::::::([^:]+):/) {
@ -3225,32 +3249,26 @@ sub main() {
push @aptfingerprints, $1; push @aptfingerprints, $1;
} }
close $fh; close $fh;
}; if ($? != 0) {
opendir my $dh, "$options->{apttrustedparts}" error "gpg failed";
or error "cannot read $options->{apttrustedparts}";
while (my $filename = readdir $dh) {
if ($filename !~ /\.(asc|gpg)$/) {
next;
} }
$collect_fingerprints->( if (scalar @aptfingerprints == 0) {
"$options->{apttrustedparts}/$filename"); $signedby = " [signed-by=\"$keyring\"]";
last;
} }
if (-e $options->{apttrusted}) { # check if all fingerprints from the keyring that we guessed
$collect_fingerprints->($options->{apttrusted}); # are known by apt and only add signed-by option if that's not
} # the case
# check if all fingerprints from the keyring that we
# guessed are known by apt and only add signed-by
# option if that's not the case
my @suitefingerprints = (); my @suitefingerprints = ();
open my $suitefh, '-|', @gpgcmd, '--keyring', open my $suitefh, '-|', @gpgcmd, '--keyring', $keyring,
$keyring, '--with-colons', '--with-colons',
'--list-keys' // error "failed to fork(): $!"; '--list-keys' // error "failed to fork(): $!";
while (my $line = <$suitefh>) { while (my $line = <$suitefh>) {
if ($line !~ /^fpr:::::::::([^:]+):/) { if ($line !~ /^fpr:::::::::([^:]+):/) {
next; next;
} }
# if this fingerprint is not known by apt, then we # if this fingerprint is not known by apt, then we need to
# need to add the signed-by option # add the signed-by option
if (none { $_ eq $1 } @aptfingerprints) { if (none { $_ eq $1 } @aptfingerprints) {
$signedby = " [signed-by=\"$keyring\"]"; $signedby = " [signed-by=\"$keyring\"]";
last; last;
@ -3260,22 +3278,6 @@ sub main() {
if ($? != 0) { if ($? != 0) {
error "gpg failed"; error "gpg failed";
} }
} else {
info "gpg --version failed: cannot determine the right"
. " signed-by value";
}
remove_tree($gpghome, { error => \my $err });
if (@$err) {
for my $diag (@$err) {
my ($file, $message) = %$diag;
if ($file eq '') {
warning "general error: $message";
} else {
warning "problem unlinking $file: $message";
}
}
}
}
} }
if (scalar @ARGV > 0) { if (scalar @ARGV > 0) {
for my $arg (@ARGV) { for my $arg (@ARGV) {