| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# SPDX-FileCopyrightText: 2001,2002 Progeny Linux Systems, Inc. |
| 4 |
# Authors: John Goerzen, Branden Robinson |
| 5 |
# SPDX-FileCopyrightText: 2009 Peter Pentchev <[email protected]> |
| 6 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 7 |
|
| 8 |
use strict; |
| 9 |
|
| 10 |
use File::Basename; |
| 11 |
use Getopt::Long; |
| 12 |
use IO::File; |
| 13 |
|
| 14 |
use Debian::debsigs::debsigsmain; |
| 15 |
use Debian::debsigs::forktools qw/forkreader/; |
| 16 |
|
| 17 |
sub syntax($); |
| 18 |
sub version(); |
| 19 |
|
| 20 |
my ($showhelp, $showversion); |
| 21 |
Getopt::Long::Configure('no_ignore_case'); |
| 22 |
GetOptions ('help|h' => \$showhelp, |
| 23 |
'version|V' => \$showversion); |
| 24 |
|
| 25 |
version() if $showversion; |
| 26 |
syntax(0) if $showhelp; |
| 27 |
exit(0) if $showhelp || $showversion; |
| 28 |
|
| 29 |
syntax(1) unless (@ARGV && $ARGV[0] =~ /\.changes$/); |
| 30 |
|
| 31 |
my $changesfile = $ARGV[0]; |
| 32 |
my $changespath = (fileparse($changesfile))[1]; |
| 33 |
my $newchangesfile = "$changesfile.debsigs-signchanges.$$"; |
| 34 |
my $ADDSIG = $ENV{DEBSIGSSIG} ? $ENV{DEBSIGSSIG} : 'maint'; |
| 35 |
my $DEBSIGSOPTIONS = $ENV{DEBSIGSOPTIONS} ? $ENV{DEBSIGSOPTIONS} : ''; |
| 36 |
open(CHANGES, "<$changesfile") or die "Couldn't open $changesfile: $!"; |
| 37 |
open(NEWCHANGES, ">$newchangesfile") or |
| 38 |
die "Couldn't open $newchangesfile: $!"; |
| 39 |
|
| 40 |
# Look for the Files section. |
| 41 |
|
| 42 |
my ($line, $cline, $sigtype, $defkeyname); |
| 43 |
my @changes; |
| 44 |
my %debs; |
| 45 |
|
| 46 |
while (defined($line = <CHANGES>)) { |
| 47 |
$cline = $line; |
| 48 |
chomp $cline; |
| 49 |
|
| 50 |
if ($sigtype && $line =~ /^\S/) { |
| 51 |
undef $sigtype; |
| 52 |
} |
| 53 |
if (!defined($sigtype)) { |
| 54 |
push @changes, $line; |
| 55 |
|
| 56 |
# A default key? |
| 57 |
if ($cline =~ /^Changed-By:\s+(.*)$/) { |
| 58 |
$defkeyname = $1; |
| 59 |
} elsif ($cline =~ /^Maintainer:\s+(.*)$/ && !defined($defkeyname)) { |
| 60 |
$defkeyname = $1; |
| 61 |
} |
| 62 |
|
| 63 |
# A new section? |
| 64 |
if ($cline =~ /Files:/) { |
| 65 |
$sigtype = 'md5'; |
| 66 |
} elsif ($cline =~ /^Checksums-(\w+):/) { |
| 67 |
$sigtype = $1; |
| 68 |
} |
| 69 |
} else { |
| 70 |
if ($cline !~ /\s(\S+\.deb)$/) { |
| 71 |
push @changes, $line; |
| 72 |
next; |
| 73 |
} |
| 74 |
|
| 75 |
$debs{$1}{$sigtype} = $line; |
| 76 |
push @changes, {'t' => 'sig', 'sigtype' => $sigtype, 'file' => $1}; |
| 77 |
} |
| 78 |
} |
| 79 |
close CHANGES; |
| 80 |
|
| 81 |
# Do we need to determine the default key? |
| 82 |
if ($defkeyname && $DEBSIGSOPTIONS !~ /(^|\s)--?(k|default-key)/) { |
| 83 |
my ($defkey); |
| 84 |
eval { |
| 85 |
my ($fd, $pid) = forkreader(undef, |
| 86 |
'gpg', '--list-secret-keys', '--with-colons', $defkeyname); |
| 87 |
die("Couldn't exec gpg: $!\n") unless defined($fd); |
| 88 |
my $keyline; |
| 89 |
while ($keyline = <$fd>) { |
| 90 |
if ($keyline =~ /^sec:[^:]*:[^:]*:[^:]*:([^:]+):/) { |
| 91 |
die("More than one secret key present - $defkey, $1\n") if $defkey; |
| 92 |
$defkey = $1; |
| 93 |
} |
| 94 |
} |
| 95 |
close($fd) or die("Couldn't close the gpg reader: $!\n"); |
| 96 |
die("No secret key found\n") unless $defkey; |
| 97 |
}; |
| 98 |
die("Couldn't determine the secret key for '$defkeyname': $@") if $@; |
| 99 |
$DEBSIGSOPTIONS .= " --default-key='$defkey'"; |
| 100 |
} |
| 101 |
|
| 102 |
# Sign each .deb file and recalculate the checksums |
| 103 |
my $file; |
| 104 |
foreach $file (keys %debs) { |
| 105 |
my ($fname); |
| 106 |
|
| 107 |
print "Changespath is '$changespath'\n"; |
| 108 |
$fname = "$changespath/$file"; |
| 109 |
|
| 110 |
print "Signing $file...\n"; |
| 111 |
system("debsigs $DEBSIGSOPTIONS --sign=$ADDSIG $fname") && |
| 112 |
die "Couldn't sign $fname\n"; |
| 113 |
|
| 114 |
foreach $sigtype (keys %{$debs{$file}}) { |
| 115 |
my ($progname, $csum, $size, $section, $priority, $nfile); |
| 116 |
my @el; |
| 117 |
|
| 118 |
$cline = $debs{$file}{$sigtype}; |
| 119 |
chomp $cline; |
| 120 |
if ($sigtype eq 'md5') { |
| 121 |
($csum, $size, $section, $priority, $nfile) = split(' ', $cline); |
| 122 |
} else { |
| 123 |
($csum, $size, $nfile) = split(' ', $cline); |
| 124 |
} |
| 125 |
|
| 126 |
# Recalculate the checksum; $sigtype has already been validated |
| 127 |
$progname = lc($sigtype).'sum'; |
| 128 |
$csum = `$progname $fname`; |
| 129 |
if (!defined($csum)) { |
| 130 |
die("$sigtype checksum for $file: $progname: $!\n"); |
| 131 |
} |
| 132 |
chomp $csum; |
| 133 |
@el = split /\s+/, $csum; |
| 134 |
if ($#el != 1 || $el[1] ne $fname || $el[0] !~ /^([0-9a-f]+)$/) { |
| 135 |
die("$sigtype checksum for $file: bad $progname output: $csum\n"); |
| 136 |
} |
| 137 |
$csum = $el[0]; |
| 138 |
|
| 139 |
# Store it with all the other fields |
| 140 |
$size = (stat($fname))[7]; |
| 141 |
if ($sigtype eq 'md5') { |
| 142 |
$line = " $csum $size $section $priority $file\n"; |
| 143 |
} else { |
| 144 |
$line = " $csum $size $file\n"; |
| 145 |
} |
| 146 |
$debs{$file}{$sigtype} = $line; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
# Output the changes file |
| 151 |
foreach (@changes) { |
| 152 |
if (ref($_) eq '') { |
| 153 |
print NEWCHANGES; |
| 154 |
next; |
| 155 |
} elsif (ref($_) ne 'HASH') { |
| 156 |
die("INTERNAL ERROR: invalid \@changes ref '".ref($_)."'\n"); |
| 157 |
} elsif (!defined($_->{'t'})) { |
| 158 |
die("INTERNAL ERROR: \@changes ref without a type\n"); |
| 159 |
} elsif ($_->{'t'} eq 'sig') { |
| 160 |
if (!defined($_->{'file'})) { |
| 161 |
die("INTERNAL ERROR: \@changes sig without a sigtype\n"); |
| 162 |
} elsif (!defined($_->{'file'})) { |
| 163 |
die("INTERNAL ERROR: \@changes sig $_->{sigtype} for no file\n"); |
| 164 |
} |
| 165 |
$line = $debs{$_->{'file'}}{$_->{'sigtype'}}; |
| 166 |
if (!defined($line)) { |
| 167 |
die("INTERNAL ERROR: \@changes sig $_->{sigtype} nofile $_->{file}\n"); |
| 168 |
} |
| 169 |
print NEWCHANGES $line; |
| 170 |
} else { |
| 171 |
die("INTERNAL ERROR: \@changes ref of unknown type '$_->{t}'\n"); |
| 172 |
} |
| 173 |
} |
| 174 |
close NEWCHANGES; |
| 175 |
|
| 176 |
rename($newchangesfile, $changesfile) or |
| 177 |
die "Couldn't rename $newchangesfile to $changesfile: $!"; |
| 178 |
|
| 179 |
sub syntax($) { |
| 180 |
my ($err) = @_; |
| 181 |
my $s = "Usage: debsigs-signchanges file\n"; |
| 182 |
|
| 183 |
if ($err) { |
| 184 |
print STDERR "$s"; |
| 185 |
exit(1); |
| 186 |
} else { |
| 187 |
print "$s"; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
sub version() { |
| 192 |
print "debsigs-signchanges $Debian::debsigs::debsigsmain::VERSION\n"; |
| 193 |
} |
| 194 |
|
| 195 |
__END__ |
| 196 |
|
| 197 |
=head1 NAME |
| 198 |
|
| 199 |
debsigs-signchanges - cryptographically sign Debian packages based on |
| 200 |
package changes file |
| 201 |
|
| 202 |
=head1 SYNOPSIS |
| 203 |
|
| 204 |
B<debsigs-signchanges> I<file> |
| 205 |
|
| 206 |
=head1 DESCRIPTION |
| 207 |
|
| 208 |
I<debsigs-signchanges> reads the Debian package changes file specified |
| 209 |
as an operand and runs I<debsigs>(1) on each package listed within. It |
| 210 |
then updates the changes file to reflect the new cryptographic hash and |
| 211 |
size of the signed Debian packages. |
| 212 |
|
| 213 |
=head1 OPERANDS |
| 214 |
|
| 215 |
I<debsigs-signchanges> takes one operand: the name of a Debian package |
| 216 |
changes file to scan and update. |
| 217 |
|
| 218 |
=head1 AUTHORS |
| 219 |
|
| 220 |
=over 5 |
| 221 |
|
| 222 |
=item John Goerzen <[email protected]> |
| 223 |
|
| 224 |
=item Branden Robinson <[email protected]> |
| 225 |
|
| 226 |
=back |
| 227 |
|
| 228 |
=head1 SEE ALSO |
| 229 |
|
| 230 |
debsigs(1), debsig-verify(1), gpg(1) |
| 231 |
|
| 232 |
=cut |
| 233 |
|
| 234 |
# vim:set et ai sts=2 sw=2 tw=72: |