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