new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / analysisd / alerts / getloglocation.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 /* Get the log directory/file based on the day/month/year */
11
12 #include "getloglocation.h"
13 #include "config.h"
14
15 /* Global definitions */
16 FILE *_eflog;
17 FILE *_aflog;
18 FILE *_fflog;
19 FILE *_jflog;
20 FILE *_ejflog;
21
22 /* Global variables */
23 static int  __crt_day;
24 static char __elogfile[OS_FLSIZE + 1];
25 static char __alogfile[OS_FLSIZE + 1];
26 static char __flogfile[OS_FLSIZE + 1];
27 static char __jlogfile[OS_FLSIZE + 1];
28 static char __ejlogfile[OS_FLSIZE + 1];
29
30 void OS_InitLog()
31 {
32     OS_InitFwLog();
33
34     __crt_day = 0;
35
36     /* Alerts and events log file */
37     memset(__alogfile, '\0', OS_FLSIZE + 1);
38     memset(__elogfile, '\0', OS_FLSIZE + 1);
39     memset(__flogfile, '\0', OS_FLSIZE + 1);
40     memset(__jlogfile, '\0', OS_FLSIZE + 1);
41     memset(__ejlogfile, '\0', OS_FLSIZE + 1);
42
43     _eflog = NULL;
44     _aflog = NULL;
45     _fflog = NULL;
46     _jflog = NULL;
47     _ejflog = NULL;
48
49     /* Set the umask */
50     umask(0027);
51 }
52
53 int OS_GetLogLocation(const Eventinfo *lf)
54 {
55     /* Check what directories to create
56      * Check if the year directory is there
57      * If not, create it. Same for the month directory.
58      */
59     
60      
61     /* For the events */
62     if (_eflog) {
63         if (ftell(_eflog) == 0) {
64             unlink(__elogfile);
65         }
66         fclose(_eflog);
67         _eflog = NULL;
68     }
69
70     snprintf(__elogfile, OS_FLSIZE, "%s/%d/", EVENTS, lf->year);
71     if (IsDir(__elogfile) == -1)
72         if (mkdir(__elogfile, 0770) == -1) {
73             ErrorExit(MKDIR_ERROR, ARGV0, __elogfile, errno, strerror(errno));
74         }
75
76     snprintf(__elogfile, OS_FLSIZE, "%s/%d/%s", EVENTS, lf->year, lf->mon);
77
78     if (IsDir(__elogfile) == -1)
79         if (mkdir(__elogfile, 0770) == -1) {
80             ErrorExit(MKDIR_ERROR, ARGV0, __elogfile, errno, strerror(errno));
81         }
82
83     /* Create the logfile name */
84     snprintf(__elogfile, OS_FLSIZE, "%s/%d/%s/ossec-%s-%02d.log",
85              EVENTS,
86              lf->year,
87              lf->mon,
88              "archive",
89              lf->day);
90
91     _eflog = fopen(__elogfile, "a");
92     if (!_eflog) {
93         ErrorExit("%s: Error opening logfile: '%s'", ARGV0, __elogfile);
94     }
95
96     /* Create a symlink */
97     unlink(EVENTS_DAILY);
98
99     if (link(__elogfile, EVENTS_DAILY) == -1) {
100         ErrorExit(LINK_ERROR, ARGV0, __elogfile, EVENTS_DAILY, errno, strerror(errno));
101     }
102     /* For the events in JSON */
103     if (Config.logall_json) {
104         /* Create the json archives logfile name */
105
106         if (_ejflog) {
107             if (ftell(_ejflog) == 0) {
108                 unlink(__ejlogfile);
109             }
110             fclose(_ejflog);
111             _ejflog = NULL;
112         }
113
114         snprintf(__ejlogfile, OS_FLSIZE, "%s/%d/", EVENTS, lf->year);
115         if (IsDir(__ejlogfile) == -1)
116             if (mkdir(__ejlogfile, 0770) == -1) {
117                 ErrorExit(MKDIR_ERROR, ARGV0, __ejlogfile, errno, strerror(errno));
118         }
119
120         snprintf(__ejlogfile, OS_FLSIZE, "%s/%d/%s", EVENTS, lf->year, lf->mon);
121
122         if (IsDir(__ejlogfile) == -1)
123             if (mkdir(__ejlogfile, 0770) == -1) {
124                 ErrorExit(MKDIR_ERROR, ARGV0, __ejlogfile, errno, strerror(errno));
125         }
126
127
128         snprintf(__ejlogfile, OS_FLSIZE, "%s/%d/%s/ossec-%s-%02d.json",
129                  EVENTS,
130                  lf->year,
131                  lf->mon,
132                  "archive",
133                  lf->day);
134
135         _ejflog = fopen(__ejlogfile, "a");
136
137         if (!_ejflog) {
138             ErrorExit("%s: Error opening logfile: '%s'", ARGV0, __ejlogfile);
139         }
140
141         /* Create a symlink */
142         unlink(EVENTSJSON_DAILY);
143
144         if (link(__ejlogfile, EVENTSJSON_DAILY) == -1) {
145             ErrorExit(LINK_ERROR, ARGV0, __ejlogfile, EVENTSJSON_DAILY, errno, strerror(errno));
146         }
147     }
148     
149     /* For the alerts logs */
150     if (_aflog) {
151         if (ftell(_aflog) == 0) {
152             unlink(__alogfile);
153         }
154         fclose(_aflog);
155         _aflog = NULL;
156     }
157
158     snprintf(__alogfile, OS_FLSIZE, "%s/%d/", ALERTS, lf->year);
159     if (IsDir(__alogfile) == -1)
160         if (mkdir(__alogfile, 0770) == -1) {
161             ErrorExit(MKDIR_ERROR, ARGV0, __alogfile, errno, strerror(errno));
162         }
163
164     snprintf(__alogfile, OS_FLSIZE, "%s/%d/%s", ALERTS, lf->year, lf->mon);
165
166     if (IsDir(__alogfile) == -1)
167         if (mkdir(__alogfile, 0770) == -1) {
168             ErrorExit(MKDIR_ERROR, ARGV0, __alogfile, errno, strerror(errno));
169         }
170
171     /* Create the logfile name */
172     snprintf(__alogfile, OS_FLSIZE, "%s/%d/%s/ossec-%s-%02d.log",
173              ALERTS,
174              lf->year,
175              lf->mon,
176              "alerts",
177              lf->day);
178
179     _aflog = fopen(__alogfile, "a");
180
181     if (!_aflog) {
182         ErrorExit("%s: Error opening logfile: '%s'", ARGV0, __alogfile);
183     }
184
185     /* Create a symlink */
186     unlink(ALERTS_DAILY);
187
188     if (link(__alogfile, ALERTS_DAILY) == -1) {
189         ErrorExit(LINK_ERROR, ARGV0, __alogfile, ALERTS_DAILY, errno, strerror(errno));
190     }
191
192     if (Config.jsonout_output) {
193
194         if (_jflog) {
195             if (ftell(_jflog) == 0) {
196                 unlink(__jlogfile);
197             }
198             fclose(_jflog);
199             _jflog = NULL;
200         }
201
202         snprintf(__jlogfile, OS_FLSIZE, "%s/%d/", ALERTS, lf->year);
203         if (IsDir(__jlogfile) == -1)
204             if (mkdir(__jlogfile, 0770) == -1) {
205                 ErrorExit(MKDIR_ERROR, ARGV0, __jlogfile, errno, strerror(errno));
206             }
207
208         snprintf(__jlogfile, OS_FLSIZE, "%s/%d/%s", ALERTS, lf->year, lf->mon);
209
210         if (IsDir(__jlogfile) == -1)
211             if (mkdir(__jlogfile, 0770) == -1) {
212                 ErrorExit(MKDIR_ERROR, ARGV0, __jlogfile, errno, strerror(errno));
213             }
214
215
216
217         /* Create the json logfile name */
218         snprintf(__jlogfile, OS_FLSIZE, "%s/%d/%s/ossec-%s-%02d.json",
219                  ALERTS,
220                  lf->year,
221                  lf->mon,
222                  "alerts",
223                  lf->day);
224
225         _jflog = fopen(__jlogfile, "a");
226
227         if (!_jflog) {
228             ErrorExit("%s: Error opening logfile: '%s'", ARGV0, __jlogfile);
229         }
230
231         /* Create a symlink */
232         unlink(ALERTSJSON_DAILY);
233
234         if (link(__jlogfile, ALERTSJSON_DAILY) == -1) {
235             ErrorExit(LINK_ERROR, ARGV0, __jlogfile, ALERTSJSON_DAILY, errno, strerror(errno));
236         }
237
238     }
239
240     /* For the firewall events */
241     if (_fflog) {
242         if (ftell(_fflog) == 0) {
243             unlink(__flogfile);
244         }
245         fclose(_fflog);
246         _fflog = NULL;
247     }
248
249     snprintf(__flogfile, OS_FLSIZE, "%s/%d/", FWLOGS, lf->year);
250     if (IsDir(__flogfile) == -1)
251         if (mkdir(__flogfile, 0770) == -1) {
252             ErrorExit(MKDIR_ERROR, ARGV0, __flogfile, errno, strerror(errno));
253         }
254
255     snprintf(__flogfile, OS_FLSIZE, "%s/%d/%s", FWLOGS, lf->year, lf->mon);
256
257     if (IsDir(__flogfile) == -1)
258         if (mkdir(__flogfile, 0770) == -1) {
259             ErrorExit(MKDIR_ERROR, ARGV0, __flogfile, errno, strerror(errno));
260         }
261
262     /* Create the logfile name */
263     snprintf(__flogfile, OS_FLSIZE, "%s/%d/%s/ossec-%s-%02d.log",
264              FWLOGS,
265              lf->year,
266              lf->mon,
267              "firewall",
268              lf->day);
269
270     _fflog = fopen(__flogfile, "a");
271
272     if (!_fflog) {
273         ErrorExit("%s: Error opening logfile: '%s'", ARGV0, __flogfile);
274     }
275
276     /* Create a symlink */
277     unlink(FWLOGS_DAILY);
278
279     if (link(__flogfile, FWLOGS_DAILY) == -1) {
280         ErrorExit(LINK_ERROR, ARGV0, __flogfile, FWLOGS_DAILY, errno, strerror(errno));
281     }
282
283     /* Setting the new day */
284     __crt_day = lf->day;
285
286     return (0);
287 }
288