cc4b47d9b898bcbc8541cb53ccc6b2ec3a1fc9c0
[ossec-hids.git] / src / analysisd / dodiff.c
1 /* @(#) $Id: ./src/analysisd/dodiff.c, 2012/07/23 dcid Exp $
2  */
3
4 /* Copyright (C) 2010 Trend Micro Inc.
5  * All rights 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  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17
18 #include "eventinfo.h"
19 #include "shared.h"
20
21 char flastcontent[OS_SIZE_8192 +1];
22 char *fmsglast = "Previous output:";
23
24 static int _add2last(char *str, int strsize, char *file)
25 {
26     FILE *fp;
27
28     fp = fopen(file, "w");
29     if(!fp)
30     {
31         /* Try to create the directories. */
32         char *dirrule = NULL;
33         char *diragent = NULL;
34
35         dirrule = strrchr(file, '/');
36         if(!dirrule)
37         {
38             merror("%s: ERROR: Invalid file name to diff: %s",
39                    ARGV0, file);
40             return(0);
41         }
42         *dirrule = '\0';
43
44         diragent = strrchr(file, '/');
45         if(!diragent)
46         {
47             merror("%s: ERROR: Invalid file name to diff (2): %s",
48                    ARGV0, file);
49             return(0);
50         }
51         *diragent = '\0';
52
53         /* Checking if the diragent exists. */
54         if(IsDir(file) != 0)
55         {
56             if(mkdir(file, 0770) == -1)
57             {
58                 merror(MKDIR_ERROR, ARGV0, file);
59                 return(0);
60             }
61         }
62         *diragent = '/';
63
64         if(IsDir(file) != 0)
65         {
66             if(mkdir(file, 0770) == -1)
67             {
68                 merror(MKDIR_ERROR, ARGV0, file);
69                 return(0);
70             }
71         }
72         *dirrule = '/';
73
74         fp = fopen(file, "w");
75         if(!fp)
76         {
77             merror(FOPEN_ERROR, ARGV0, file);
78             return(0);
79         }
80     }
81
82     fwrite(str, strsize + 1, 1, fp);
83     fclose(fp);
84     return(1);
85 }
86
87
88 int doDiff(RuleInfo *currently_rule, Eventinfo *lf)
89 {
90     int date_of_change;
91     char *htpt = NULL;
92     char flastfile[OS_SIZE_2048 +1];
93     char flastcontent[OS_SIZE_8192 +1];
94
95
96     /* Cleaning up global. */
97     flastcontent[0] = '\0';
98     flastcontent[OS_SIZE_8192] = '\0';
99     currently_rule->last_events[0] = NULL;
100
101
102
103     if(lf->hostname[0] == '(')
104     {
105         htpt = strchr(lf->hostname, ')');
106         if(htpt)
107         {
108             *htpt = '\0';
109         }
110         snprintf(flastfile, OS_SIZE_2048, "%s/%s/%d/%s", DIFF_DIR, lf->hostname+1,
111                  currently_rule->sigid, DIFF_LAST_FILE);
112
113         if(htpt)
114         {
115             *htpt = ')';
116         }
117         htpt = NULL;
118     }
119     else
120     {
121         snprintf(flastfile, OS_SIZE_2048, "%s/%s/%d/%s", DIFF_DIR, lf->hostname,
122                  currently_rule->sigid, DIFF_LAST_FILE);
123     }
124
125     /* lf->size can't be too long. */
126     if(lf->size >= OS_SIZE_8192)
127     {
128         merror("%s: ERROR: event size (%d) too long for diff.", ARGV0, lf->size);
129         return(0);
130     }
131
132
133     /* Checking if last diff exists. */
134     date_of_change = File_DateofChange(flastfile);
135     if(date_of_change <= 0)
136     {
137         if(!_add2last(lf->log, lf->size, flastfile))
138         {
139             merror("%s: ERROR: unable to create last file: %s", ARGV0, flastfile);
140             return(0);
141         }
142         return(0);
143     }
144     else
145     {
146         FILE *fp;
147         int n;
148         fp = fopen(flastfile,"r");
149         if(!fp)
150         {
151             merror(FOPEN_ERROR, ARGV0, flastfile);
152             return(0);
153         }
154
155         n = fread(flastcontent, 1, OS_SIZE_8192, fp);
156         if(n > 0)
157         {
158             flastcontent[n] = '\0';
159         }
160         else
161         {
162             merror("%s: ERROR: read error on %s", ARGV0, flastfile);
163             fclose(fp);
164             return(0);
165         }
166         fclose(fp);
167     }
168
169     /* Nothing changed. */
170     if(strcmp(flastcontent, lf->log) == 0)
171     {
172         return(0);
173     }
174
175
176     if(!_add2last(lf->log, lf->size, flastfile))
177     {
178         merror("%s: ERROR: unable to create last file: %s", ARGV0, flastfile);
179     }
180
181     currently_rule->last_events[0] = fmsglast;
182     currently_rule->last_events[1] = flastcontent;
183     return(1);
184
185 }
186
187
188
189 /* EOF */