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