Imported Upstream version 2.3
[ossec-hids.git] / src / win32 / add-localfile.c
1 /* @(#) $Id: add-localfile.c,v 1.13 2009/06/24 18:53:10 dcid Exp $ */
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 3) 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 <time.h>
18 #include "os_regex/os_regex.h"
19
20 #define OSSECCONF   "ossec.conf"
21 #define OS_MAXSTR   1024
22
23 int total;
24
25 int fileexist(char *file)
26 {
27     FILE *fp;
28
29     /* Opening file */
30     fp = fopen(file, "r");
31     if(!fp)
32         return(0);
33
34     fclose(fp);
35     return(1);
36 }
37
38 int dogrep(char *file, char *str)
39 {
40     char line[OS_MAXSTR +1];
41     FILE *fp;
42
43     /* Opening file */
44     fp = fopen(file, "r");
45     if(!fp)
46         return(0);
47
48     /* Clearing memory */
49     memset(line, '\0', OS_MAXSTR +1);
50
51     /* Reading file and looking for str */ 
52     while(fgets(line, OS_MAXSTR, fp) != NULL)
53     {
54         if(OS_Match(str, line))
55         {
56             fclose(fp);
57             return(1);
58         }
59     }
60
61     fclose(fp);
62     return(0);
63 }
64
65
66
67 /* Check is syscheck is present in the config */
68 int config_file(char *name, char *file, int quiet)
69 {
70     int add = 0;
71
72     char ffile[256];
73     FILE *fp;
74
75     ffile[255] = '\0';
76     
77
78     /* Checking if the file has a variable format */
79     if(strchr(file, '%') != NULL)
80     {
81         time_t tm;
82         struct tm *p;
83
84         tm = time(NULL);
85         p = localtime(&tm);
86
87         if(strftime(ffile, 255, file, p) == 0)
88         {
89             return(-1);
90         }
91     }
92     else
93     {
94         strncpy(ffile, file, 255);
95     }
96     
97     
98     /* Looking for ffile */
99     if(!fileexist(ffile))
100     {
101         if(quiet == 0)
102         {
103             printf("%s: Log file not existent: '%s'.\n", name, file);
104         }
105         return(-1);
106     }
107     
108     if(dogrep(OSSECCONF, file))
109     {
110         printf("%s: Log file already configured: '%s'.\n", 
111                     name, file);
112         return(0);
113     }
114     
115     
116     /* Add iis config config */
117     fp = fopen(OSSECCONF, "a");
118     if(!fp)
119     {
120         printf("%s: Unable to edit configuration file.\n", name);
121         return(0); 
122     }
123    
124     printf("%s: Adding log file to be monitored: '%s'.\n", name,file);
125     printf("%s: Continue? (y/n):", name);
126     while(1)
127     {
128         char u_buffer[256];
129         memset(u_buffer, '\0', 256);
130         if((fgets(u_buffer, 254, stdin) != NULL) &&
131             (strlen(u_buffer) < 250))
132         {
133             if((u_buffer[0] == 'y') || (u_buffer[0] == 'Y'))
134             {
135                 add = 1;
136                 break;
137             }
138             else if((u_buffer[0] == 'n') || (u_buffer[0] == 'N'))
139             {
140                 add = 0;
141                 break;
142             }
143         }
144         printf("%s: Continue? (y/n):", name);
145     }
146    
147     if(add == 0)
148     {
149         printf("%s: Action not taken.\n", name);
150         fclose(fp);
151         return(0);
152     }
153     
154     fprintf(fp, "\r\n" 
155     "\r\n"    
156     "<!-- Extra log file -->\r\n"
157     "<ossec_config>\r\n"
158     "  <localfile>\r\n"
159     "    <location>%s</location>\r\n"
160     "    <log_format>syslog</log_format>\r\n"
161     "  </localfile>\r\n"
162     "</ossec_config>\r\n\r\n", file);
163
164
165     printf("%s: Action completed.\n", name);
166     fclose(fp);
167
168     return(0);
169                     
170 }
171
172 /* Setup windows after install */
173 int main(int argc, char **argv)
174 {
175     int quiet = 0;
176     
177     if(argc < 2)
178     {
179         printf("%s: Invalid syntax.\n", argv[0]);
180         printf("Try: '%s <file_name>'\n\n", argv[0]);
181     }
182
183     /* Looking for the quiet option */
184     if((argc == 3) && (strcmp(argv[2],"--quiet") == 0))
185     {
186         quiet = 1;
187     }
188
189     
190     /* Checking if ossec was installed already */
191     if(!fileexist(OSSECCONF))
192     {
193         printf("%s: Unable to find ossec config: '%s'.\n", argv[0], OSSECCONF);
194     }
195
196     else
197     {
198         config_file(argv[0], argv[1], quiet);
199     }
200     
201     return(0);
202 }