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