verify.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * dpkg - main program for package management
  3. * verify.c - verify package integrity
  4. *
  5. * Copyright © 2012-2015 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <string.h>
  23. #include <stdbool.h>
  24. #include <stdio.h>
  25. #include <dpkg/i18n.h>
  26. #include <dpkg/dpkg.h>
  27. #include <dpkg/dpkg-db.h>
  28. #include <dpkg/options.h>
  29. #include "filesdb.h"
  30. #include "infodb.h"
  31. #include "main.h"
  32. enum verify_result {
  33. VERIFY_NONE,
  34. VERIFY_PASS,
  35. VERIFY_FAIL,
  36. };
  37. struct verify_checks {
  38. enum verify_result md5sum;
  39. };
  40. typedef void verify_output_func(struct filenamenode *, struct verify_checks *);
  41. static int
  42. verify_result_rpm(enum verify_result result, int check)
  43. {
  44. switch (result) {
  45. case VERIFY_FAIL:
  46. return check;
  47. case VERIFY_PASS:
  48. return '.';
  49. case VERIFY_NONE:
  50. default:
  51. return '?';
  52. }
  53. }
  54. static void
  55. verify_output_rpm(struct filenamenode *namenode, struct verify_checks *checks)
  56. {
  57. char result[9];
  58. int attr;
  59. memset(result, '?', sizeof(result));
  60. result[2] = verify_result_rpm(checks->md5sum, '5');
  61. if (namenode->flags & fnnf_old_conff)
  62. attr = 'c';
  63. else
  64. attr = ' ';
  65. printf("%.9s %c %s\n", result, attr, namenode->name);
  66. }
  67. static verify_output_func *verify_output = verify_output_rpm;
  68. bool
  69. verify_set_output(const char *name)
  70. {
  71. if (strcmp(name, "rpm") == 0)
  72. verify_output = verify_output_rpm;
  73. else
  74. return false;
  75. return true;
  76. }
  77. static void
  78. verify_package(struct pkginfo *pkg)
  79. {
  80. struct fileinlist *file;
  81. ensure_packagefiles_available(pkg);
  82. parse_filehash(pkg, &pkg->installed);
  83. pkg_conffiles_mark_old(pkg);
  84. for (file = pkg->clientdata->files; file; file = file->next) {
  85. struct verify_checks checks;
  86. struct filenamenode *fnn;
  87. char hash[MD5HASHLEN + 1];
  88. int failures = 0;
  89. fnn = namenodetouse(file->namenode, pkg, &pkg->installed);
  90. if (strcmp(fnn->newhash, EMPTYHASHFLAG) == 0) {
  91. if (fnn->oldhash == NULL)
  92. continue;
  93. else
  94. fnn->newhash = fnn->oldhash;
  95. }
  96. memset(&checks, 0, sizeof(checks));
  97. md5hash(pkg, hash, fnn->name);
  98. if (strcmp(hash, fnn->newhash) != 0) {
  99. checks.md5sum = VERIFY_FAIL;
  100. failures++;
  101. }
  102. if (failures)
  103. verify_output(fnn, &checks);
  104. }
  105. }
  106. int
  107. verify(const char *const *argv)
  108. {
  109. struct pkginfo *pkg;
  110. int rc = 0;
  111. modstatdb_open(msdbrw_readonly);
  112. ensure_diversions();
  113. if (!*argv) {
  114. struct pkgiterator *it;
  115. it = pkg_db_iter_new();
  116. while ((pkg = pkg_db_iter_next_pkg(it)))
  117. verify_package(pkg);
  118. pkg_db_iter_free(it);
  119. } else {
  120. const char *thisarg;
  121. while ((thisarg = *argv++)) {
  122. pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
  123. if (pkg->status == PKG_STAT_NOTINSTALLED) {
  124. notice(_("package '%s' is not installed"),
  125. pkg_name(pkg, pnaw_nonambig));
  126. rc = 1;
  127. continue;
  128. }
  129. verify_package(pkg);
  130. }
  131. }
  132. modstatdb_shutdown();
  133. m_output(stdout, _("<standard output>"));
  134. return rc;
  135. }