Imported Upstream version 2.7
[ossec-hids.git] / src / win32 / setup-iis.c
1 /* @(#) $Id: ./src/win32/setup-iis.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 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
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <dirent.h>
20 #include <time.h>
21 #include <windows.h>
22 #include "os_regex/os_regex.h"
23
24
25 #define OSSECCONF   "ossec.conf"
26 #define OS_MAXSTR   1024
27
28
29 int total;
30
31
32 int direxist(char *dir)
33 {
34     DIR *dp;
35
36     /* Opening dir */
37     dp = opendir(dir);
38     if(dp == NULL)
39         return(0);
40
41     closedir(dp);
42     return(1);
43 }
44
45
46 int fileexist(char *file)
47 {
48     FILE *fp;
49
50     /* Opening file */
51     fp = fopen(file, "r");
52     if(!fp)
53         return(0);
54
55     fclose(fp);
56     return(1);
57 }
58
59 int dogrep(char *file, char *str)
60 {
61     char line[OS_MAXSTR +1];
62     FILE *fp;
63
64     /* Opening file */
65     fp = fopen(file, "r");
66     if(!fp)
67         return(0);
68
69     /* Clearing memory */
70     memset(line, '\0', OS_MAXSTR +1);
71
72     /* Reading file and looking for str */
73     while(fgets(line, OS_MAXSTR, fp) != NULL)
74     {
75         if(OS_Match(str, line))
76         {
77             fclose(fp);
78             return(1);
79         }
80     }
81
82     fclose(fp);
83     return(0);
84 }
85
86
87 /* Getting Windows directory */
88 static void get_win_dir(char *file, int f_size)
89 {
90     ExpandEnvironmentStrings("%WINDIR%", file, f_size);
91
92     if(!direxist(file))
93     {
94         strncpy(file, "C:\\WINDOWS", f_size);
95     }
96 }
97
98
99
100 int config_dir(char *name, char *dir, char *vfile)
101 {
102     FILE *fp;
103
104     if(!direxist(dir))
105     {
106         return(0);
107     }
108
109     if(dogrep(OSSECCONF, vfile))
110     {
111         printf("%s: Log file already configured: '%s'.\n",
112                 name, vfile);
113         return(1);
114     }
115
116     printf("%s: IIS directory found, but no valid log.\n", name);
117     printf("%s: You may have it configured in a format different\n"
118            "               than W3C Extended or you just don't have today's\n"
119            "               log available.\n", name);
120     printf("%s: http://www.ossec.net/en/manual.html#iis\n\n", name);
121
122
123     /* Add iis config config */
124     fp = fopen(OSSECCONF, "a");
125     if(!fp)
126     {
127         printf("%s: Unable to edit configuration file.\n", name);
128         return(1);
129     }
130
131     fprintf(fp, "\r\n"
132             "\r\n"
133             "<!-- IIS log file -->\r\n"
134             "<ossec_config>\r\n"
135             "  <localfile>\r\n"
136             "    <location>%s</location>\r\n"
137             "    <log_format>iis</log_format>\r\n"
138             "  </localfile>\r\n"
139             "</ossec_config>\r\n\r\n", vfile);
140
141     printf("%s: Action completed.\n", name);
142
143     total++;
144     fclose(fp);
145
146     return(1);
147
148
149 }
150
151
152 /* Check if the iis file is present in the config */
153 int config_iis(char *name, char *file, char *vfile)
154 {
155     FILE *fp;
156
157     if(!fileexist(file))
158     {
159         return(0);
160     }
161
162     total++;
163
164     if(dogrep(OSSECCONF, vfile))
165     {
166         printf("%s: Log file already configured: '%s'.\n",
167                 name, vfile);
168         return(1);
169     }
170
171     printf("%s: Adding IIS log file to be monitored: '%s'.\n", name,vfile);
172
173
174     /* Add iis config config */
175     fp = fopen(OSSECCONF, "a");
176     if(!fp)
177     {
178         printf("%s: Unable to edit configuration file.\n", name);
179         return(1);
180     }
181
182     fprintf(fp, "\r\n"
183             "\r\n"
184             "<!-- IIS log file -->\r\n"
185             "<ossec_config>\r\n"
186             "  <localfile>\r\n"
187             "    <location>%s</location>\r\n"
188             "    <log_format>iis</log_format>\r\n"
189             "  </localfile>\r\n"
190             "</ossec_config>\r\n\r\n", vfile);
191
192     printf("%s: Action completed.\n", name);
193     fclose(fp);
194
195     return(1);
196
197 }
198
199 /* Setup windows after install */
200 int main(int argc, char **argv)
201 {
202     int i = 0;
203
204     time_t tm;
205     struct tm *p;
206
207     char win_dir[2048];
208
209
210     if(argc >= 2)
211     {
212         if(chdir(argv[1]) != 0)
213         {
214             printf("%s: Invalid directory: '%s'.\n", argv[0], argv[1]);
215             return(0);
216         }
217     }
218
219     /* Checking if ossec was installed already */
220     if(!fileexist(OSSECCONF))
221     {
222         printf("%s: Unable to find ossec config: '%s'", argv[0], OSSECCONF);
223         exit(0);
224     }
225
226     /* Getting todays day */
227     tm = time(NULL);
228     p = localtime(&tm);
229
230     total = 0;
231
232     printf("%s: Looking for IIS log files to monitor.\r\n",
233                 argv[0]);
234     printf("%s: For more information: http://www.ossec.net/en/win.html\r\n",
235                 argv[0]);
236     printf("\r\n");
237
238
239     /* Getting windows directory */
240     get_win_dir(win_dir, sizeof(win_dir) -1);
241
242
243     /* Looking for IIS log files */
244     while(i <= 254)
245     {
246         char lfile[OS_MAXSTR +1];
247         char vfile[OS_MAXSTR +1];
248
249         i++;
250
251         /* Searching for NCSA */
252         snprintf(lfile,
253                 OS_MAXSTR,
254                 "%s\\System32\\LogFiles\\W3SVC%d\\nc%02d%02d%02d.log",
255                 win_dir,i, (p->tm_year+1900)-2000, p->tm_mon+1, p->tm_mday);
256         snprintf(vfile,
257                 OS_MAXSTR,
258                 "%s\\System32\\LogFiles\\W3SVC%d\\nc%%y%%m%%d.log",
259                 win_dir, i);
260
261         /* Try dir-based */
262         config_iis(argv[0], lfile, vfile);
263
264
265         /* Searching for W3C extended */
266         snprintf(lfile,
267                 OS_MAXSTR,
268                 "%s\\System32\\LogFiles\\W3SVC%d\\ex%02d%02d%02d.log",
269                 win_dir, i, (p->tm_year+1900)-2000, p->tm_mon+1, p->tm_mday);
270
271         snprintf(vfile,
272                 OS_MAXSTR,
273                 "%s\\System32\\LogFiles\\W3SVC%d\\ex%%y%%m%%d.log",
274                 win_dir, i);
275
276         /* Try dir-based */
277         if(config_iis(argv[0], lfile, vfile) == 0)
278         {
279             snprintf(lfile,
280                     OS_MAXSTR,
281                     "%s\\System32\\LogFiles\\W3SVC%d", win_dir, i);
282             config_dir(argv[0], lfile, vfile);
283         }
284
285
286         /* Searching for FTP Extended format */
287         snprintf(lfile,
288              OS_MAXSTR,
289              "%s\\System32\\LogFiles\\MSFTPSVC%d\\ex%02d%02d%02d.log",
290              win_dir, i, (p->tm_year+1900)-2000, p->tm_mon+1, p->tm_mday);
291
292         snprintf(vfile,
293              OS_MAXSTR,
294              "%s\\System32\\LogFiles\\MSFTPSVC%d\\ex%%y%%m%%d.log",
295              win_dir, i);
296         if(config_iis(argv[0], lfile, vfile) == 0)
297         {
298             snprintf(lfile,
299                     OS_MAXSTR,
300                     "%s\\System32\\LogFiles\\MSFTPSVC%d", win_dir, i);
301             config_dir(argv[0], lfile, vfile);
302         }
303
304
305         /* Searching for IIS SMTP logs */
306         snprintf(lfile,
307              OS_MAXSTR,
308              "%s\\System32\\LogFiles\\SMTPSVC%d\\ex%02d%02d%02d.log",
309              win_dir, i, (p->tm_year+1900)-2000, p->tm_mon+1, p->tm_mday);
310
311         snprintf(vfile,
312              OS_MAXSTR,
313              "%s\\System32\\LogFiles\\SMTPSVC%d\\ex%%y%%m%%d.log",
314              win_dir, i);
315         if(config_iis(argv[0], lfile, vfile) == 0)
316         {
317             snprintf(lfile,
318                     OS_MAXSTR,
319                     "%s\\System32\\LogFiles\\SMTPSVC%d",win_dir, i);
320             config_dir(argv[0], lfile, vfile);
321         }
322     }
323
324     if(total == 0)
325     {
326         printf("%s: No IIS log added. Look at the link above for more "
327                "information.\r\n", argv[0]);
328     }
329
330     return(0);
331 }