Checksums.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. # Copyright © 2008 Frank Lichtenheld <djpig@debian.org>
  2. # Copyright © 2008, 2012-2015 Guillem Jover <guillem@debian.org>
  3. # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. package Dpkg::Checksums;
  18. use strict;
  19. use warnings;
  20. our $VERSION = '1.03';
  21. our @EXPORT = qw(
  22. checksums_is_supported
  23. checksums_get_list
  24. checksums_get_property
  25. );
  26. use Exporter qw(import);
  27. use Carp;
  28. use Digest;
  29. use Dpkg::Gettext;
  30. use Dpkg::ErrorHandling;
  31. =encoding utf8
  32. =head1 NAME
  33. Dpkg::Checksums - generate and manipulate file checksums
  34. =head1 DESCRIPTION
  35. This module provides an object that can generate and manipulate
  36. various file checksums as well as some methods to query information
  37. about supported checksums.
  38. =head1 FUNCTIONS
  39. =over 4
  40. =cut
  41. my $CHECKSUMS = {
  42. md5 => {
  43. name => 'MD5',
  44. regex => qr/[0-9a-f]{32}/,
  45. strong => 0,
  46. },
  47. sha1 => {
  48. name => 'SHA-1',
  49. regex => qr/[0-9a-f]{40}/,
  50. strong => 0,
  51. },
  52. sha256 => {
  53. name => 'SHA-256',
  54. regex => qr/[0-9a-f]{64}/,
  55. strong => 1,
  56. },
  57. };
  58. =item @list = checksums_get_list()
  59. Returns the list of supported checksums algorithms.
  60. =cut
  61. sub checksums_get_list() {
  62. my @list = sort keys %{$CHECKSUMS};
  63. return @list;
  64. }
  65. =item $bool = checksums_is_supported($alg)
  66. Returns a boolean indicating whether the given checksum algorithm is
  67. supported. The checksum algorithm is case-insensitive.
  68. =cut
  69. sub checksums_is_supported($) {
  70. my $alg = shift;
  71. return exists $CHECKSUMS->{lc($alg)};
  72. }
  73. =item $value = checksums_get_property($alg, $property)
  74. Returns the requested property of the checksum algorithm. Returns undef if
  75. either the property or the checksum algorithm doesn't exist. Valid
  76. properties currently include "name" (returns the name of the digest
  77. algorithm), "regex" for the regular expression describing the common
  78. string representation of the checksum, and "strong" for a boolean describing
  79. whether the checksum algorithm is considered cryptographically strong.
  80. =cut
  81. sub checksums_get_property($$) {
  82. my ($alg, $property) = @_;
  83. carp 'obsolete checksums program property' if $property eq 'program';
  84. return unless checksums_is_supported($alg);
  85. return $CHECKSUMS->{lc($alg)}{$property};
  86. }
  87. =back
  88. =head1 METHODS
  89. =over 4
  90. =item $ck = Dpkg::Checksums->new()
  91. Create a new Dpkg::Checksums object. This object is able to store
  92. the checksums of several files to later export them or verify them.
  93. =cut
  94. sub new {
  95. my ($this, %opts) = @_;
  96. my $class = ref($this) || $this;
  97. my $self = {};
  98. bless $self, $class;
  99. $self->reset();
  100. return $self;
  101. }
  102. =item $ck->reset()
  103. Forget about all checksums stored. The object is again in the same state
  104. as if it was newly created.
  105. =cut
  106. sub reset {
  107. my $self = shift;
  108. $self->{files} = [];
  109. $self->{checksums} = {};
  110. $self->{size} = {};
  111. }
  112. =item $ck->add_from_file($filename, %opts)
  113. Add checksums information for the file $filename. The file must exists
  114. for the call to succeed. If you don't want the given filename to appear
  115. when you later export the checksums you might want to set the "key"
  116. option with the public name that you want to use. Also if you don't want
  117. to generate all the checksums, you can pass an array reference of the
  118. wanted checksums in the "checksums" option.
  119. It the object already contains checksums information associated the
  120. filename (or key), it will error out if the newly computed information
  121. does not match what's stored, and the caller did not request that it be
  122. updated with the boolean "update" option.
  123. =cut
  124. sub add_from_file {
  125. my ($self, $file, %opts) = @_;
  126. my $key = exists $opts{key} ? $opts{key} : $file;
  127. my @alg;
  128. if (exists $opts{checksums}) {
  129. push @alg, map { lc } @{$opts{checksums}};
  130. } else {
  131. push @alg, checksums_get_list();
  132. }
  133. push @{$self->{files}}, $key unless exists $self->{size}{$key};
  134. (my @s = stat($file)) or syserr(g_('cannot fstat file %s'), $file);
  135. if (not $opts{update} and exists $self->{size}{$key} and
  136. $self->{size}{$key} != $s[7]) {
  137. error(g_('file %s has size %u instead of expected %u'),
  138. $file, $s[7], $self->{size}{$key});
  139. }
  140. $self->{size}{$key} = $s[7];
  141. foreach my $alg (@alg) {
  142. my $digest = Digest->new($CHECKSUMS->{$alg}{name});
  143. open my $fh, '<', $file or syserr(g_('cannot open file %s'), $file);
  144. $digest->addfile($fh);
  145. close $fh;
  146. my $newsum = $digest->hexdigest;
  147. if (not $opts{update} and exists $self->{checksums}{$key}{$alg} and
  148. $self->{checksums}{$key}{$alg} ne $newsum) {
  149. error(g_('file %s has checksum %s instead of expected %s (algorithm %s)'),
  150. $file, $newsum, $self->{checksums}{$key}{$alg}, $alg);
  151. }
  152. $self->{checksums}{$key}{$alg} = $newsum;
  153. }
  154. }
  155. =item $ck->add_from_string($alg, $value, %opts)
  156. Add checksums of type $alg that are stored in the $value variable.
  157. $value can be multi-lines, each line should be a space separated list
  158. of checksum, file size and filename. Leading or trailing spaces are
  159. not allowed.
  160. It the object already contains checksums information associated to the
  161. filenames, it will error out if the newly read information does not match
  162. what's stored, and the caller did not request that it be updated with
  163. the boolean "update" option.
  164. =cut
  165. sub add_from_string {
  166. my ($self, $alg, $fieldtext, %opts) = @_;
  167. $alg = lc($alg);
  168. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  169. my $regex = checksums_get_property($alg, 'regex');
  170. my $checksums = $self->{checksums};
  171. for my $checksum (split /\n */, $fieldtext) {
  172. next if $checksum eq '';
  173. unless ($checksum =~ m/^($regex)\s+(\d+)\s+($rx_fname)$/) {
  174. error(g_('invalid line in %s checksums string: %s'),
  175. $alg, $checksum);
  176. }
  177. my ($sum, $size, $file) = ($1, $2, $3);
  178. if (not $opts{update} and exists($checksums->{$file}{$alg})
  179. and $checksums->{$file}{$alg} ne $sum) {
  180. error(g_("conflicting checksums '%s' and '%s' for file '%s'"),
  181. $checksums->{$file}{$alg}, $sum, $file);
  182. }
  183. if (not $opts{update} and exists $self->{size}{$file}
  184. and $self->{size}{$file} != $size) {
  185. error(g_("conflicting file sizes '%u' and '%u' for file '%s'"),
  186. $self->{size}{$file}, $size, $file);
  187. }
  188. push @{$self->{files}}, $file unless exists $self->{size}{$file};
  189. $checksums->{$file}{$alg} = $sum;
  190. $self->{size}{$file} = $size;
  191. }
  192. }
  193. =item $ck->add_from_control($control, %opts)
  194. Read checksums from Checksums-* fields stored in the Dpkg::Control object
  195. $control. It uses $self->add_from_string() on the field values to do the
  196. actual work.
  197. If the option "use_files_for_md5" evaluates to true, then the "Files"
  198. field is used in place of the "Checksums-Md5" field. By default the option
  199. is false.
  200. =cut
  201. sub add_from_control {
  202. my ($self, $control, %opts) = @_;
  203. $opts{use_files_for_md5} //= 0;
  204. foreach my $alg (checksums_get_list()) {
  205. my $key = "Checksums-$alg";
  206. $key = 'Files' if ($opts{use_files_for_md5} and $alg eq 'md5');
  207. if (exists $control->{$key}) {
  208. $self->add_from_string($alg, $control->{$key}, %opts);
  209. }
  210. }
  211. }
  212. =item @files = $ck->get_files()
  213. Return the list of files whose checksums are stored in the object.
  214. =cut
  215. sub get_files {
  216. my $self = shift;
  217. return @{$self->{files}};
  218. }
  219. =item $bool = $ck->has_file($file)
  220. Return true if we have checksums for the given file. Returns false
  221. otherwise.
  222. =cut
  223. sub has_file {
  224. my ($self, $file) = @_;
  225. return exists $self->{size}{$file};
  226. }
  227. =item $ck->remove_file($file)
  228. Remove all checksums of the given file.
  229. =cut
  230. sub remove_file {
  231. my ($self, $file) = @_;
  232. return unless $self->has_file($file);
  233. delete $self->{checksums}{$file};
  234. delete $self->{size}{$file};
  235. @{$self->{files}} = grep { $_ ne $file } $self->get_files();
  236. }
  237. =item $checksum = $ck->get_checksum($file, $alg)
  238. Return the checksum of type $alg for the requested $file. This will not
  239. compute the checksum but only return the checksum stored in the object, if
  240. any.
  241. If $alg is not defined, it returns a reference to a hash: keys are
  242. the checksum algorithms and values are the checksums themselves. The
  243. hash returned must not be modified, it's internal to the object.
  244. =cut
  245. sub get_checksum {
  246. my ($self, $file, $alg) = @_;
  247. $alg = lc($alg) if defined $alg;
  248. if (exists $self->{checksums}{$file}) {
  249. return $self->{checksums}{$file} unless defined $alg;
  250. return $self->{checksums}{$file}{$alg};
  251. }
  252. return;
  253. }
  254. =item $size = $ck->get_size($file)
  255. Return the size of the requested file if it's available in the object.
  256. =cut
  257. sub get_size {
  258. my ($self, $file) = @_;
  259. return $self->{size}{$file};
  260. }
  261. =item $bool = $ck->has_strong_checksums($file)
  262. Return a boolean on whether the file has a strong checksum.
  263. =cut
  264. sub has_strong_checksums {
  265. my ($self, $file) = @_;
  266. foreach my $alg (checksums_get_list()) {
  267. return 1 if defined $self->get_checksum($file, $alg) and
  268. checksums_get_property($alg, 'strong');
  269. }
  270. return 0;
  271. }
  272. =item $ck->export_to_string($alg, %opts)
  273. Return a multi-line string containing the checksums of type $alg. The
  274. string can be stored as-is in a Checksum-* field of a Dpkg::Control
  275. object.
  276. =cut
  277. sub export_to_string {
  278. my ($self, $alg, %opts) = @_;
  279. my $res = '';
  280. foreach my $file ($self->get_files()) {
  281. my $sum = $self->get_checksum($file, $alg);
  282. my $size = $self->get_size($file);
  283. next unless defined $sum and defined $size;
  284. $res .= "\n$sum $size $file";
  285. }
  286. return $res;
  287. }
  288. =item $ck->export_to_control($control, %opts)
  289. Export the checksums in the Checksums-* fields of the Dpkg::Control
  290. $control object.
  291. =cut
  292. sub export_to_control {
  293. my ($self, $control, %opts) = @_;
  294. $opts{use_files_for_md5} //= 0;
  295. foreach my $alg (checksums_get_list()) {
  296. my $key = "Checksums-$alg";
  297. $key = 'Files' if ($opts{use_files_for_md5} and $alg eq 'md5');
  298. $control->{$key} = $self->export_to_string($alg, %opts);
  299. }
  300. }
  301. =back
  302. =head1 CHANGES
  303. =head2 Version 1.03 (dpkg 1.18.5)
  304. New property: Add new 'strong' property.
  305. New member: $ck->has_strong_checksums().
  306. =head2 Version 1.02 (dpkg 1.18.0)
  307. Obsolete property: Getting the 'program' checksum property will carp() and
  308. return undef, the Digest module is used internally now.
  309. New property: Add new 'name' property with the name of the Digest algorithm
  310. to use.
  311. =head2 Version 1.01 (dpkg 1.17.6)
  312. New argument: Accept an options argument in $ck->export_to_string().
  313. New option: Accept new option 'update' in $ck->add_from_file() and
  314. $ck->add_from_control().
  315. =head2 Version 1.00 (dpkg 1.15.6)
  316. Mark the module as public.
  317. =head1 AUTHOR
  318. Raphaël Hertzog <hertzog@debian.org>.
  319. =cut
  320. 1;