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