new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / client-agent / intcheck_op.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11 #include "agentd.h"
12 #include "os_crypto/md5/md5_op.h"
13 #include "os_crypto/sha1/sha1_op.h"
14
15
16 /* Send integrity checking information about a file to the server */
17 int intcheck_file(const char *file_name, const char *dir)
18 {
19     struct stat statbuf;
20     os_md5 mf_sum = "";
21     os_sha1 sf_sum = "";
22     char newsum[912 + 1];
23
24     newsum[0] = '\0';
25     newsum[912] = '\0';
26
27     /* Stat the file */
28 #ifdef WIN32
29     if (stat(file_name, &statbuf) < 0)
30 #else
31     if (lstat(file_name, &statbuf) < 0)
32 #endif
33     {
34         snprintf(newsum, 911, "%c:%s:-1 %s%s", SYSCHECK_MQ, SYSCHECK,
35                  dir, file_name);
36         send_msg(0, newsum);
37
38         return (1);
39     }
40
41     /* Generate new checksum */
42 #ifdef WIN32
43     if (S_ISREG(statbuf.st_mode))
44 #else
45     if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode))
46 #endif
47     {
48         /* Generate SHA-1 of the file */
49         if (OS_SHA1_File(file_name, sf_sum, OS_TEXT) < 0) {
50             strncpy(sf_sum, "xxx", 4);
51         }
52
53         /* Generate MD5 of the file */
54         if (OS_MD5_File(file_name, mf_sum, OS_TEXT) < 0) {
55             strncpy(mf_sum, "xxx", 4);
56         }
57     }
58
59     snprintf(newsum, 911, "%c:%s:%d:%d:%d:%d:%s:%s %s%s",
60              SYSCHECK_MQ, SYSCHECK,
61              (int)statbuf.st_size,
62              (int)statbuf.st_mode,
63              (int)statbuf.st_uid,
64              (int)statbuf.st_gid,
65              mf_sum,
66              sf_sum, dir, file_name);
67
68     send_msg(0, newsum);
69     return (1);
70 }
71