1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
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, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <verify_files_hashes.h>
26 
27 #include <actuator.h>
28 #include <rlist.h>
29 #include <policy.h>
30 #include <client_code.h>
31 #include <files_interfaces.h>
32 #include <files_lib.h>
33 #include <hash.h>
34 #include <misc_lib.h>
35 #include <eval_context.h>
36 #include <known_dirs.h>
37 
CompareFileHashes(const char * file1,const char * file2,const struct stat * sstat,const struct stat * dstat,const FileCopy * fc,AgentConnection * conn)38 bool CompareFileHashes(const char *file1, const char *file2, const struct stat *sstat, const struct stat *dstat, const FileCopy *fc, AgentConnection *conn)
39 {
40     unsigned char digest1[EVP_MAX_MD_SIZE + 1] = { 0 }, digest2[EVP_MAX_MD_SIZE + 1] = { 0 };
41     int i;
42 
43     if (sstat->st_size != dstat->st_size)
44     {
45         Log(LOG_LEVEL_DEBUG, "File sizes differ, no need to compute checksum");
46         return true;
47     }
48 
49     if (conn == NULL)
50     {
51         HashFile(file1, digest1, CF_DEFAULT_DIGEST, false);
52         HashFile(file2, digest2, CF_DEFAULT_DIGEST, false);
53 
54         for (i = 0; i < EVP_MAX_MD_SIZE; i++)
55         {
56             if (digest1[i] != digest2[i])
57             {
58                 return true;
59             }
60         }
61 
62         Log(LOG_LEVEL_DEBUG, "Files were identical");
63         return false;           /* only if files are identical */
64     }
65     else
66     {
67         assert(fc->servers && strcmp(RlistScalarValue(fc->servers), "localhost"));
68         return CompareHashNet(file1, file2, fc->encrypt, conn);  /* client.c */
69     }
70 }
71 
CompareBinaryFiles(const char * file1,const char * file2,const struct stat * sstat,const struct stat * dstat,const FileCopy * fc,AgentConnection * conn)72 bool CompareBinaryFiles(const char *file1, const char *file2, const struct stat *sstat, const struct stat *dstat, const FileCopy *fc, AgentConnection *conn)
73 {
74     int fd1, fd2, bytes1, bytes2;
75     char buff1[BUFSIZ], buff2[BUFSIZ];
76 
77     if (sstat->st_size != dstat->st_size)
78     {
79         Log(LOG_LEVEL_DEBUG, "File sizes differ, no need to compute checksum");
80         return true;
81     }
82 
83     if (conn == NULL)
84     {
85         fd1 = safe_open(file1, O_RDONLY | O_BINARY);
86         fd2 = safe_open(file2, O_RDONLY | O_BINARY);
87 
88         do
89         {
90             bytes1 = read(fd1, buff1, BUFSIZ);
91             bytes2 = read(fd2, buff2, BUFSIZ);
92 
93             if ((bytes1 != bytes2) || (memcmp(buff1, buff2, bytes1) != 0))
94             {
95                 Log(LOG_LEVEL_VERBOSE, "Binary Comparison mismatch...");
96                 close(fd2);
97                 close(fd1);
98                 return true;
99             }
100         } while (bytes1 > 0);
101 
102         close(fd2);
103         close(fd1);
104 
105         return false;           /* only if files are identical */
106     }
107     else
108     {
109         assert(fc->servers && strcmp(RlistScalarValue(fc->servers), "localhost"));
110         Log(LOG_LEVEL_DEBUG, "Using network checksum instead");
111         return CompareHashNet(file1, file2, fc->encrypt, conn);  /* client.c */
112     }
113 }
114