Checksums.pm 11 KB

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