verify.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * dpkg - main program for package management
  3. * verify.c - verify package integrity
  4. *
  5. * Copyright © 2012 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 <http://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/pkg-spec.h>
  29. #include <dpkg/options.h>
  30. #include "filesdb.h"
  31. #include "infodb.h"
  32. #include "main.h"
  33. enum verify_result {
  34. VERIFY_NONE,
  35. VERIFY_PASS,
  36. VERIFY_FAIL,
  37. };
  38. struct verify_checks {
  39. enum verify_result md5sum;
  40. };
  41. typedef void verify_output_func(struct filenamenode *, struct verify_checks *);
  42. static int
  43. verify_result_rpm(enum verify_result result, int check)
  44. {
  45. switch (result) {
  46. case VERIFY_FAIL:
  47. return check;
  48. case VERIFY_PASS:
  49. return '.';
  50. case VERIFY_NONE:
  51. default:
  52. return '?';
  53. }
  54. }
  55. static void
  56. verify_output_rpm(struct filenamenode *namenode, struct verify_checks *checks)
  57. {
  58. char result[9];
  59. int attr;
  60. memset(result, '?', sizeof(result));
  61. result[2] = verify_result_rpm(checks->md5sum, '5');
  62. if (namenode->flags & fnnf_old_conff)
  63. attr = 'c';
  64. else
  65. attr = ' ';
  66. printf("%.9s %c %s\n", result, attr, namenode->name);
  67. }
  68. static verify_output_func *verify_output = verify_output_rpm;
  69. int
  70. verify_set_output(const char *name)
  71. {
  72. if (strcmp(name, "rpm") == 0)
  73. verify_output = verify_output_rpm;
  74. else
  75. return 1;
  76. return 0;
  77. }
  78. static void
  79. verify_package(struct pkginfo *pkg)
  80. {
  81. struct fileinlist *file;
  82. ensure_packagefiles_available(pkg);
  83. parse_filehash(pkg, &pkg->installed);
  84. oldconffsetflags(pkg->installed.conffiles);
  85. for (file = pkg->clientdata->files; file; file = file->next) {
  86. struct verify_checks checks;
  87. struct filenamenode *fnn;
  88. char hash[MD5HASHLEN + 1];
  89. int failures = 0;
  90. fnn = namenodetouse(file->namenode, pkg, &pkg->installed);
  91. if (strcmp(fnn->newhash, EMPTYHASHFLAG) == 0) {
  92. if (fnn->oldhash == NULL)
  93. continue;
  94. else
  95. fnn->newhash = fnn->oldhash;
  96. }
  97. memset(&checks, 0, sizeof(checks));
  98. md5hash(pkg, hash, fnn->name);
  99. if (strcmp(hash, fnn->newhash) != 0) {
  100. checks.md5sum = VERIFY_FAIL;
  101. failures++;
  102. }
  103. if (failures)
  104. verify_output(fnn, &checks);
  105. }
  106. }
  107. int
  108. verify(const char *const *argv)
  109. {
  110. struct pkginfo *pkg;
  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. struct dpkg_error err;
  123. pkg = pkg_spec_parse_pkg(thisarg, &err);
  124. if (pkg == NULL)
  125. badusage(_("--%s needs a valid package name but '%.250s' is not: %s"),
  126. cipaction->olong, thisarg, err.str);
  127. verify_package(pkg);
  128. }
  129. }
  130. modstatdb_shutdown();
  131. m_output(stdout, _("<standard output>"));
  132. return 0;
  133. }