/usr/bin/dscverify is in devscripts 2.17.6+deb9u2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | #!/usr/bin/perl
# This program takes .changes or .dsc files as arguments and verifies
# that they're properly signed by a Debian developer, and that the local
# copies of the files mentioned in them match the MD5 sums given.
# Copyright 1998 Roderick Schertler <roderick@argon.org>
# Modifications copyright 1999,2000,2002 Julian Gilbey <jdg@debian.org>
# Drastically simplified to match katie's signature checking Feb 2002
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
use 5.004; # correct pipe close behavior
use strict;
use warnings;
use Cwd;
use Fcntl;
use Digest::MD5;
use Dpkg::IPC;
use File::HomeDir;
use File::Spec;
use File::Temp;
use File::Basename;
use POSIX qw(:errno_h);
use Getopt::Long qw(:config bundling permute no_getopt_compat);
use List::Util qw(first);
my $progname = basename $0;
my $modified_conf_msg;
my $Exit = 0;
my $start_dir = cwd;
my $verify_sigs = 1;
my $use_default_keyrings = 1;
my $verbose = 0;
my $havegpg = first { !system('sh', '-c', "command -v $_ >/dev/null 2>&1") } qw(gpg2 gpg);
sub usage {
print <<"EOF";
Usage: $progname [options] changes-or-buildinfo-dsc-file ...
Options: --help Display this message
--version Display version and copyright information
--keyring <keyring>
Add <keyring> to the list of keyrings used
--no-default-keyrings
Do not check against the default keyrings
--nosigcheck, --no-sig-check, -u
Do not verify the GPG signature
--no-conf, --noconf
Do not read the devscripts config file
--verbose
Do not suppress GPG output.
Default settings modified by devscripts configuration files:
$modified_conf_msg
EOF
}
my $version = <<"EOF";
This is $progname, from the Debian devscripts package, version 2.17.6+deb9u2
This code is copyright 1998 Roderick Schertler <roderick\@argon.org>
Modifications are copyright 1999, 2000, 2002 Julian Gilbey <jdg\@debian.org>
This program comes with ABSOLUTELY NO WARRANTY.
You are free to redistribute this code under the terms of the
GNU General Public License, version 2 or later.
EOF
sub xwarndie_mess {
my @mess = ("$progname: ", @_);
$mess[$#mess] =~ s/:$/: $!\n/; # XXX loses if it's really /:\n/
return @mess;
}
sub xwarn {
warn xwarndie_mess @_;
$Exit ||= 1;
}
sub xdie {
die xwarndie_mess @_;
}
sub get_rings {
my @rings = @_;
my @keyrings = qw(/usr/share/keyrings/debian-keyring.gpg
/usr/share/keyrings/debian-maintainers.gpg);
$ENV{HOME} = File::HomeDir->my_home;
if (defined $ENV{HOME} && -r "$ENV{HOME}/.gnupg/trustedkeys.gpg") {
unshift(@keyrings, "$ENV{HOME}/.gnupg/trustedkeys.gpg");
}
unshift(@keyrings, '/org/keyring.debian.org/keyrings/debian-keyring.gpg');
if (system('dpkg-vendor', '--derives-from', 'Ubuntu') == 0) {
unshift(@keyrings, qw(/usr/share/keyrings/ubuntu-master-keyring.gpg
/usr/share/keyrings/ubuntu-archive-keyring.gpg));
}
for (@keyrings) {
push @rings, $_ if -r;
}
return @rings if @rings;
xdie "can't find any system keyrings\n";
}
sub check_signature($\@;\$) {
my ($file, $rings, $outref) = @_;
my $fh = eval { File::Temp->new() }
or xdie "unable to open status file for gpg: $@\n";
# Allow the status file descriptor to pass on to the child process
my $flags = fcntl($fh, F_GETFD, 0);
fcntl($fh, F_SETFD, $flags & ~FD_CLOEXEC);
my $fd = fileno $fh;
my @cmd;
push @cmd, $havegpg, "--status-fd", $fd,
qw(--batch --no-options --no-default-keyring --always-trust);
foreach (@$rings) { push @cmd, '--keyring'; push @cmd, $_; }
my ($out, $err) = ('', '');
eval {
spawn(exec => \@cmd,
from_file => $file,
to_string => \$out,
error_to_string => \$err,
wait_child => 1);
};
if ($@) {
print $out if ($verbose);
return $err || $@;
}
print $err if ($verbose);
seek($fh, 0, SEEK_SET);
my $status;
$status .= $_ while <$fh>;
close $fh;
if ($status !~ m/^\[GNUPG:\] VALIDSIG/m) {
return $out;
}
if (defined $outref) {
$$outref = $out;
}
return '';
}
sub process_file {
my ($file, @rings) = @_;
my ($filedir, $filebase);
my $sigcheck;
print "$file:\n";
# Move to the directory in which the file appears to live
chdir $start_dir or xdie "can't chdir to original directory!\n";
if ($file =~ m-(.*)/([^/]+)-) {
$filedir = $1;
$filebase = $2;
unless (chdir $filedir) {
xwarn "can't chdir $filedir:";
return;
}
} else {
$filebase = $file;
}
my $out;
if ($verify_sigs) {
$sigcheck = check_signature $filebase, @rings, $out;
if ($sigcheck) {
xwarn "$file failed signature check:\n$sigcheck";
return;
} else {
print " Good signature found\n";
}
}
else {
if (!open SIGNED, '<', $filebase) {
xwarn "can't open $file:";
return;
}
$out = do { local $/; <SIGNED> };
if (!close SIGNED) {
xwarn "problem reading $file:";
return;
}
}
if ($file =~ /\.(changes|buildinfo)$/ and $out =~ /^Format:\s*(.*)$/mi) {
my $format = $1;
unless ($format =~ /^(\d+)\.(\d+)$/) {
xwarn "$file has an unrecognised format: $format\n";
return;
}
my ($major, $minor) = split /\./, $format;
$major += 0;
$minor += 0;
if ($file =~ /\.changes$/ and ($major != 1 or $minor > 8) or
$file =~ /\.buildinfo$/ and (($major != 0 or $minor > 2) and
($major != 1 or $minor > 0))) {
xwarn "$file is an unsupported format: $format\n";
return;
}
}
my @spec = map { split /\n/ } $out =~ /^(?:Checksums-Md5|Files):\s*\n((?:[ \t]+.*\n)+)/mgi;
unless (@spec) {
xwarn "no file spec lines in $file\n";
return;
}
my @checksums = map { split /\n/ } $out =~ /^Checksums-(\S+):\s*\n/mgi;
@checksums = grep {!/^(Md5|Sha(1|256))$/i} @checksums;
if (@checksums) {
xwarn "$file contains unsupported checksums:\n"
. join (", ", @checksums) . "\n";
return;
}
my %sha1s = map { reverse split /(\S+)\s*$/m }
$out =~ /^Checksums-Sha1:\s*\n((?:[ \t]+.*\n)+)/mgi;
my %sha256s = map { reverse split /(\S+)\s*$/m }
$out =~ /^Checksums-Sha256:\s*\n((?:[ \t]+.*\n)+)/mgi;
my $md5o = Digest::MD5->new or xdie "can't initialize MD5\n";
my $any;
for (@spec) {
unless (/^\s+([0-9a-f]{32})\s+(\d+)\s+(?:\S+\s+\S+\s+)?(\S+)\s*$/) {
xwarn "invalid file spec in $file `$_'\n";
next;
}
my ($md5, $size, $filename) = ($1, $2, $3);
my ($sha1, $sha1size, $sha256, $sha256size);
$filename !~ m,[/\x00],
or xdie "File name contains invalid characters: $file";
if (keys %sha1s) {
$sha1 = $sha1s{$filename};
unless (defined $sha1) {
xwarn "no sha1 for `$filename' in $file\n";
next;
}
unless ($sha1 =~ /^\s+([0-9a-f]{40})\s+(\d+)\s*$/) {
xwarn "invalid sha1 spec in $file `$sha1'\n";
next;
}
($sha1, $sha1size) = ($1, $2);
} else {
$sha1size = $size;
}
if (keys %sha256s) {
$sha256 = $sha256s{$filename};
unless (defined $sha256) {
xwarn "no sha256 for `$filename' in $file\n";
next;
}
unless ($sha256 =~ /^\s+([0-9a-f]{64})\s+(\d+)\s*$/) {
xwarn "invalid sha256 spec in $file `$sha256'\n";
next;
}
($sha256, $sha256size) = ($1, $2);
} else {
$sha256size = $size;
}
unless (open FILE, '<', $filename) {
if ($! == ENOENT) {
print STDERR " skipping $filename (not present)\n";
}
else {
xwarn "can't read $filename:";
}
next;
}
$any = 1;
print " validating $filename\n";
# size
my $this_size = -s FILE;
unless (defined $this_size) {
xwarn "can't fstat $filename:";
next;
}
unless ($this_size == $size) {
xwarn "invalid file length for $filename (wanted $size got $this_size)\n";
next;
}
unless ($this_size == $sha1size) {
xwarn "invalid sha1 file length for $filename (wanted $sha1size got $this_size)\n";
next;
}
unless ($this_size == $sha256size) {
xwarn "invalid sha256 file length for $filename (wanted $sha256size got $this_size)\n";
next;
}
# MD5
$md5o->reset;
$md5o->addfile(*FILE);
my $this_md5 = $md5o->hexdigest;
unless ($this_md5 eq $md5) {
xwarn "MD5 mismatch for $filename (wanted $md5 got $this_md5)\n";
next;
}
my $this_sha1;
eval {
spawn(exec => ['sha1sum', $filename],
to_string => \$this_sha1,
wait_child => 1);
};
($this_sha1) = split /\s/,$this_sha1,2;
$this_sha1 ||= '';
unless (! keys %sha1s or $this_sha1 eq $sha1) {
xwarn "SHA1 mismatch for $filename (wanted $sha1 got $this_sha1)\n";
next;
}
my $this_sha256;
eval {
spawn(exec => ['sha256sum', $filename],
to_string => \$this_sha256,
wait_child => 1);
};
($this_sha256) = split /\s/,$this_sha256,2;
$this_sha256 ||= '';
unless (! keys %sha256s or $this_sha256 eq $sha256) {
xwarn "SHA256 mismatch for $filename (wanted $sha256 got $this_sha256)\n";
next;
}
close FILE;
if ($filename =~ /\.(?:dsc|buildinfo)$/ && $verify_sigs) {
$sigcheck = check_signature $filename, @rings;
if ($sigcheck) {
xwarn "$filename failed signature check:\n$sigcheck";
next;
} else {
print " Good signature found\n";
}
}
}
$any or
xwarn "$file didn't specify any files present locally\n";
}
sub main {
@ARGV or xdie "no .changes, .buildinfo or .dsc files specified\n";
my @rings;
# Handle config file unless --no-conf or --noconf is specified
# The next stuff is boilerplate
if (@ARGV and $ARGV[0] =~ /^--no-?conf$/) {
$modified_conf_msg = " (no configuration files read)";
shift @ARGV;
} else {
my @config_files = ('/etc/devscripts.conf', '~/.devscripts');
my %config_vars = (
'DSCVERIFY_KEYRINGS' => '',
);
my %config_default = %config_vars;
my $shell_cmd;
# Set defaults
foreach my $var (keys %config_vars) {
$shell_cmd .= "$var='$config_vars{$var}';\n";
}
$shell_cmd .= 'for file in ' . join(" ",@config_files) . "; do\n";
$shell_cmd .= '[ -f $file ] && . $file; done;' . "\n";
# Read back values
foreach my $var (keys %config_vars) { $shell_cmd .= "echo \$$var;\n" }
my $shell_out = `/bin/bash -c '$shell_cmd'`;
@config_vars{keys %config_vars} = split /\n/, $shell_out, -1;
foreach my $var (sort keys %config_vars) {
if ($config_vars{$var} ne $config_default{$var}) {
$modified_conf_msg .= " $var=$config_vars{$var}\n";
}
}
$modified_conf_msg ||= " (none)\n";
chomp $modified_conf_msg;
$config_vars{'DSCVERIFY_KEYRINGS'} =~ s/^\s*:\s*//;
$config_vars{'DSCVERIFY_KEYRINGS'} =~ s/\s*:\s*$//;
@rings = split /\s*:\s*/, $config_vars{'DSCVERIFY_KEYRINGS'};
}
GetOptions(
'help' => sub { usage; exit 0; },
'version' => sub { print $version; exit 0; },
'sigcheck|sig-check!' => \$verify_sigs,
'u' => sub { $verify_sigs = 0 },
'noconf|no-conf' => sub {
die "--$_[0] is only acceptable as the first command-line option!\n";
},
'default-keyrings!' => \$use_default_keyrings,
'keyring=s@' => sub {
my $ring = $_[1];
if (-r $ring) { push @rings, $ring; }
else { die "Keyring $ring unreadable\n" }
},
'verbose' => \$verbose,
) or do { usage; exit 1 };
@ARGV or xdie "no .changes, .buildinfo or .dsc files specified\n";
@rings = get_rings @rings if $use_default_keyrings and $verify_sigs;
for my $file (@ARGV) {
process_file $file, @rings;
}
return 0;
}
$Exit = main || $Exit;
$Exit = 1 if $Exit and not $Exit % 256;
if ($Exit) { print STDERR "Validation FAILED!!\n"; }
else { print "All files validated successfully.\n"; }
exit $Exit;
|