LCOV - code coverage report
Current view: top level - src/main - verify.c (source / functions) Coverage Total Hit
Test: dpkg 1.22.7-3-g89f48 C code coverage Lines: 0.0 % 91 0
Test Date: 2024-07-17 02:53:43 Functions: 0.0 % 7 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0.0 % 43 0

             Branch data     Line data    Source code
       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                 :             : 
      21                 :             : #include <config.h>
      22                 :             : #include <compat.h>
      23                 :             : 
      24                 :             : #include <errno.h>
      25                 :             : #include <fcntl.h>
      26                 :             : #include <unistd.h>
      27                 :             : #include <string.h>
      28                 :             : #include <stdbool.h>
      29                 :             : #include <stdlib.h>
      30                 :             : #include <stdio.h>
      31                 :             : 
      32                 :             : #include <dpkg/i18n.h>
      33                 :             : #include <dpkg/dpkg.h>
      34                 :             : #include <dpkg/dpkg-db.h>
      35                 :             : #include <dpkg/options.h>
      36                 :             : #include <dpkg/db-ctrl.h>
      37                 :             : #include <dpkg/db-fsys.h>
      38                 :             : #include <dpkg/buffer.h>
      39                 :             : 
      40                 :             : #include "main.h"
      41                 :             : 
      42                 :             : 
      43                 :             : enum verify_result {
      44                 :             :         VERIFY_NONE,
      45                 :             :         VERIFY_PASS,
      46                 :             :         VERIFY_FAIL,
      47                 :             : };
      48                 :             : 
      49                 :             : struct verify_checks {
      50                 :             :         int exists_errno;
      51                 :             :         enum verify_result exists;
      52                 :             :         enum verify_result mode;
      53                 :             :         enum verify_result md5sum;
      54                 :             : };
      55                 :             : 
      56                 :             : typedef void verify_output_func(struct fsys_namenode *, struct verify_checks *);
      57                 :             : 
      58                 :             : static int
      59                 :           0 : verify_result_rpm(enum verify_result result, int check)
      60                 :             : {
      61      [ #  #  # ]:           0 :         switch (result) {
      62                 :           0 :         case VERIFY_FAIL:
      63                 :           0 :                 return check;
      64                 :           0 :         case VERIFY_PASS:
      65                 :           0 :                 return '.';
      66                 :           0 :         case VERIFY_NONE:
      67                 :             :         default:
      68                 :           0 :                 return '?';
      69                 :             :         }
      70                 :             : }
      71                 :             : 
      72                 :             : static void
      73                 :           0 : verify_output_rpm(struct fsys_namenode *namenode, struct verify_checks *checks)
      74                 :             : {
      75                 :             :         char result[9];
      76                 :           0 :         char *error = NULL;
      77                 :             :         int attr;
      78                 :             : 
      79                 :           0 :         memset(result, '?', sizeof(result));
      80                 :             : 
      81         [ #  # ]:           0 :         if (checks->exists == VERIFY_FAIL) {
      82                 :           0 :                 memcpy(result, "missing  ", sizeof(result));
      83         [ #  # ]:           0 :                 if (checks->exists_errno != ENOENT)
      84                 :           0 :                         m_asprintf(&error, " (%s)", strerror(checks->exists_errno));
      85                 :             :         } else {
      86                 :           0 :                 result[1] = verify_result_rpm(checks->mode, 'M');
      87                 :           0 :                 result[2] = verify_result_rpm(checks->md5sum, '5');
      88                 :             :         }
      89                 :             : 
      90         [ #  # ]:           0 :         if (namenode->flags & FNNF_OLD_CONFF)
      91                 :           0 :                 attr = 'c';
      92                 :             :         else
      93                 :           0 :                 attr = ' ';
      94                 :             : 
      95         [ #  # ]:           0 :         printf("%.9s %c %s%s\n", result, attr, namenode->name, error ? error : "");
      96                 :             : 
      97                 :           0 :         free(error);
      98                 :           0 : }
      99                 :             : 
     100                 :             : static verify_output_func *verify_output = verify_output_rpm;
     101                 :             : 
     102                 :             : bool
     103                 :           0 : verify_set_output(const char *name)
     104                 :             : {
     105         [ #  # ]:           0 :         if (strcmp(name, "rpm") == 0)
     106                 :           0 :                 verify_output = verify_output_rpm;
     107                 :             :         else
     108                 :           0 :                 return false;
     109                 :             : 
     110                 :           0 :         return true;
     111                 :             : }
     112                 :             : 
     113                 :             : static int
     114                 :           0 : verify_digest(const char *filename, struct fsys_namenode *fnn,
     115                 :             :               struct verify_checks *checks)
     116                 :             : {
     117                 :             :         static int fd;
     118                 :             : 
     119                 :           0 :         fd = open(filename, O_RDONLY);
     120                 :             : 
     121         [ #  # ]:           0 :         if (fd >= 0) {
     122                 :             :                 struct dpkg_error err;
     123                 :             :                 char hash[MD5HASHLEN + 1];
     124                 :             : 
     125                 :           0 :                 push_cleanup(cu_closefd, ehflag_bombout, 1, &fd);
     126         [ #  # ]:           0 :                 if (fd_md5(fd, hash, -1, &err) < 0)
     127                 :           0 :                         ohshit(_("cannot compute MD5 digest for file '%s': %s"),
     128                 :             :                                filename, err.str);
     129                 :           0 :                 pop_cleanup(ehflag_normaltidy); /* fd = open(cdr.buf) */
     130                 :           0 :                 close(fd);
     131                 :             : 
     132         [ #  # ]:           0 :                 if (strcmp(hash, fnn->newhash) == 0) {
     133                 :           0 :                         checks->md5sum = VERIFY_PASS;
     134                 :           0 :                         return 0;
     135                 :             :                 } else {
     136                 :           0 :                         checks->md5sum = VERIFY_FAIL;
     137                 :             :                 }
     138                 :             :         } else {
     139                 :           0 :                 checks->md5sum = VERIFY_NONE;
     140                 :             :         }
     141                 :             : 
     142                 :           0 :         return -1;
     143                 :             : }
     144                 :             : 
     145                 :             : static int
     146                 :           0 : verify_file(const char *filename, struct fsys_namenode *fnn,
     147                 :             :             struct pkginfo *pkg, struct verify_checks *checks)
     148                 :             : {
     149                 :             :         struct stat st;
     150                 :           0 :         int failures = 0;
     151                 :             : 
     152         [ #  # ]:           0 :         if (lstat(filename, &st) < 0) {
     153                 :           0 :                 checks->exists_errno = errno;
     154                 :           0 :                 checks->exists = VERIFY_FAIL;
     155                 :           0 :                 return 1;
     156                 :             :         }
     157                 :           0 :         checks->exists = VERIFY_PASS;
     158                 :             : 
     159   [ #  #  #  # ]:           0 :         if (fnn->newhash == NULL && fnn->oldhash != NULL)
     160                 :           0 :                 fnn->newhash = fnn->oldhash;
     161                 :             : 
     162         [ #  # ]:           0 :         if (fnn->newhash != NULL) {
     163                 :             :                 /* Mode check heuristic: If we know its digest, the pathname
     164                 :             :                  * must be a regular file. */
     165         [ #  # ]:           0 :                 if (!S_ISREG(st.st_mode)) {
     166                 :           0 :                         checks->mode = VERIFY_FAIL;
     167                 :           0 :                         failures++;
     168                 :             :                 }
     169                 :             : 
     170         [ #  # ]:           0 :                 if (verify_digest(filename, fnn, checks) < 0)
     171                 :           0 :                         failures++;
     172                 :             :         }
     173                 :             : 
     174                 :           0 :         return failures;
     175                 :             : }
     176                 :             : 
     177                 :             : static void
     178                 :           0 : verify_package(struct pkginfo *pkg)
     179                 :             : {
     180                 :             :         struct fsys_namenode_list *file;
     181                 :           0 :         struct varbuf filename = VARBUF_INIT;
     182                 :             : 
     183                 :           0 :         ensure_packagefiles_available(pkg);
     184                 :           0 :         parse_filehash(pkg, &pkg->installed);
     185                 :           0 :         pkg_conffiles_mark_old(pkg);
     186                 :             : 
     187         [ #  # ]:           0 :         for (file = pkg->files; file; file = file->next) {
     188                 :             :                 struct verify_checks checks;
     189                 :             :                 struct fsys_namenode *fnn;
     190                 :             : 
     191                 :           0 :                 fnn = namenodetouse(file->namenode, pkg, &pkg->installed);
     192                 :             : 
     193                 :           0 :                 varbuf_set_str(&filename, dpkg_fsys_get_dir());
     194                 :           0 :                 varbuf_add_str(&filename, fnn->name);
     195                 :             : 
     196                 :           0 :                 memset(&checks, 0, sizeof(checks));
     197                 :             : 
     198         [ #  # ]:           0 :                 if (verify_file(varbuf_str(&filename), fnn, pkg, &checks) > 0)
     199                 :           0 :                         verify_output(fnn, &checks);
     200                 :             :         }
     201                 :             : 
     202                 :           0 :         varbuf_destroy(&filename);
     203                 :           0 : }
     204                 :             : 
     205                 :             : int
     206                 :           0 : verify(const char *const *argv)
     207                 :             : {
     208                 :             :         struct pkginfo *pkg;
     209                 :           0 :         int rc = 0;
     210                 :             : 
     211                 :           0 :         modstatdb_open(msdbrw_readonly);
     212                 :           0 :         ensure_diversions();
     213                 :             : 
     214         [ #  # ]:           0 :         if (!*argv) {
     215                 :             :                 struct pkg_hash_iter *iter;
     216                 :             : 
     217                 :           0 :                 iter = pkg_hash_iter_new();
     218         [ #  # ]:           0 :                 while ((pkg = pkg_hash_iter_next_pkg(iter)))
     219                 :           0 :                         verify_package(pkg);
     220                 :           0 :                 pkg_hash_iter_free(iter);
     221                 :             :         } else {
     222                 :             :                 const char *thisarg;
     223                 :             : 
     224         [ #  # ]:           0 :                 while ((thisarg = *argv++)) {
     225                 :           0 :                         pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
     226         [ #  # ]:           0 :                         if (pkg->status == PKG_STAT_NOTINSTALLED) {
     227                 :           0 :                                 notice(_("package '%s' is not installed"),
     228                 :             :                                        pkg_name(pkg, pnaw_nonambig));
     229                 :           0 :                                 rc = 1;
     230                 :           0 :                                 continue;
     231                 :             :                         }
     232                 :             : 
     233                 :           0 :                         verify_package(pkg);
     234                 :             :                 }
     235                 :             :         }
     236                 :             : 
     237                 :           0 :         modstatdb_shutdown();
     238                 :             : 
     239                 :           0 :         m_output(stdout, _("<standard output>"));
     240                 :             : 
     241                 :           0 :         return rc;
     242                 :             : }
        

Generated by: LCOV version 2.0-1