LCOV - code coverage report
Current view: top level - src/main - verify.c (source / functions) Hit Total Coverage
Test: dpkg 1.21.11 C code coverage Lines: 0 93 0.0 %
Date: 2022-12-03 00:40:01 Functions: 0 7 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 43 0.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                 :            :         struct dpkg_error err;
     118                 :            :         static int fd;
     119                 :            :         char hash[MD5HASHLEN + 1];
     120                 :            : 
     121                 :          0 :         fd = open(filename, O_RDONLY);
     122                 :            : 
     123         [ #  # ]:          0 :         if (fd >= 0) {
     124                 :          0 :                 push_cleanup(cu_closefd, ehflag_bombout, 1, &fd);
     125         [ #  # ]:          0 :                 if (fd_md5(fd, hash, -1, &err) < 0)
     126                 :          0 :                         ohshit(_("cannot compute MD5 digest for file '%s': %s"),
     127                 :            :                                filename, err.str);
     128                 :          0 :                 pop_cleanup(ehflag_normaltidy); /* fd = open(cdr.buf) */
     129                 :          0 :                 close(fd);
     130                 :            : 
     131         [ #  # ]:          0 :                 if (strcmp(hash, fnn->newhash) == 0) {
     132                 :          0 :                         checks->md5sum = VERIFY_PASS;
     133                 :          0 :                         return 0;
     134                 :            :                 } else {
     135                 :          0 :                         checks->md5sum = VERIFY_FAIL;
     136                 :            :                 }
     137                 :            :         } else {
     138                 :          0 :                 checks->md5sum = VERIFY_NONE;
     139                 :            :         }
     140                 :            : 
     141                 :          0 :         return -1;
     142                 :            : }
     143                 :            : 
     144                 :            : static int
     145                 :          0 : verify_file(const char *filename, struct fsys_namenode *fnn,
     146                 :            :             struct pkginfo *pkg, struct verify_checks *checks)
     147                 :            : {
     148                 :            :         struct stat st;
     149                 :          0 :         int failures = 0;
     150                 :            : 
     151         [ #  # ]:          0 :         if (lstat(filename, &st) < 0) {
     152                 :          0 :                 checks->exists_errno = errno;
     153                 :          0 :                 checks->exists = VERIFY_FAIL;
     154                 :          0 :                 return 1;
     155                 :            :         }
     156                 :          0 :         checks->exists = VERIFY_PASS;
     157                 :            : 
     158   [ #  #  #  # ]:          0 :         if (fnn->newhash == NULL && fnn->oldhash != NULL)
     159                 :          0 :                 fnn->newhash = fnn->oldhash;
     160                 :            : 
     161         [ #  # ]:          0 :         if (fnn->newhash != NULL) {
     162                 :            :                 /* Mode check heuristic: If we know its digest, the pathname
     163                 :            :                  * must be a regular file. */
     164         [ #  # ]:          0 :                 if (!S_ISREG(st.st_mode)) {
     165                 :          0 :                         checks->mode = VERIFY_FAIL;
     166                 :          0 :                         failures++;
     167                 :            :                 }
     168                 :            : 
     169         [ #  # ]:          0 :                 if (verify_digest(filename, fnn, checks) < 0)
     170                 :          0 :                         failures++;
     171                 :            :         }
     172                 :            : 
     173                 :          0 :         return failures;
     174                 :            : }
     175                 :            : 
     176                 :            : static void
     177                 :          0 : verify_package(struct pkginfo *pkg)
     178                 :            : {
     179                 :            :         struct fsys_namenode_list *file;
     180                 :          0 :         struct varbuf filename = VARBUF_INIT;
     181                 :            : 
     182                 :          0 :         ensure_packagefiles_available(pkg);
     183                 :          0 :         parse_filehash(pkg, &pkg->installed);
     184                 :          0 :         pkg_conffiles_mark_old(pkg);
     185                 :            : 
     186         [ #  # ]:          0 :         for (file = pkg->files; file; file = file->next) {
     187                 :            :                 struct verify_checks checks;
     188                 :            :                 struct fsys_namenode *fnn;
     189                 :            : 
     190                 :          0 :                 fnn = namenodetouse(file->namenode, pkg, &pkg->installed);
     191                 :            : 
     192                 :          0 :                 varbuf_reset(&filename);
     193                 :          0 :                 varbuf_add_str(&filename, dpkg_fsys_get_dir());
     194                 :          0 :                 varbuf_add_str(&filename, fnn->name);
     195                 :          0 :                 varbuf_end_str(&filename);
     196                 :            : 
     197                 :          0 :                 memset(&checks, 0, sizeof(checks));
     198                 :            : 
     199         [ #  # ]:          0 :                 if (verify_file(filename.buf, fnn, pkg, &checks) > 0)
     200                 :          0 :                         verify_output(fnn, &checks);
     201                 :            :         }
     202                 :            : 
     203                 :          0 :         varbuf_destroy(&filename);
     204                 :          0 : }
     205                 :            : 
     206                 :            : int
     207                 :          0 : verify(const char *const *argv)
     208                 :            : {
     209                 :            :         struct pkginfo *pkg;
     210                 :          0 :         int rc = 0;
     211                 :            : 
     212                 :          0 :         modstatdb_open(msdbrw_readonly);
     213                 :          0 :         ensure_diversions();
     214                 :            : 
     215         [ #  # ]:          0 :         if (!*argv) {
     216                 :            :                 struct pkg_hash_iter *iter;
     217                 :            : 
     218                 :          0 :                 iter = pkg_hash_iter_new();
     219         [ #  # ]:          0 :                 while ((pkg = pkg_hash_iter_next_pkg(iter)))
     220                 :          0 :                         verify_package(pkg);
     221                 :          0 :                 pkg_hash_iter_free(iter);
     222                 :            :         } else {
     223                 :            :                 const char *thisarg;
     224                 :            : 
     225         [ #  # ]:          0 :                 while ((thisarg = *argv++)) {
     226                 :          0 :                         pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
     227         [ #  # ]:          0 :                         if (pkg->status == PKG_STAT_NOTINSTALLED) {
     228                 :          0 :                                 notice(_("package '%s' is not installed"),
     229                 :            :                                        pkg_name(pkg, pnaw_nonambig));
     230                 :          0 :                                 rc = 1;
     231                 :          0 :                                 continue;
     232                 :            :                         }
     233                 :            : 
     234                 :          0 :                         verify_package(pkg);
     235                 :            :                 }
     236                 :            :         }
     237                 :            : 
     238                 :          0 :         modstatdb_shutdown();
     239                 :            : 
     240                 :          0 :         m_output(stdout, _("<standard output>"));
     241                 :            : 
     242                 :          0 :         return rc;
     243                 :            : }

Generated by: LCOV version 1.16