Imported Upstream version 2.5.1
[ossec-hids.git] / src / win32 / add-localfile.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 <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     fprintf(fp, "\r\n" 
126     "\r\n"    
127     "<!-- Extra log file -->\r\n"
128     "<ossec_config>\r\n"
129     "  <localfile>\r\n"
130     "    <location>%s</location>\r\n"
131     "    <log_format>syslog</log_format>\r\n"
132     "  </localfile>\r\n"
133     "</ossec_config>\r\n\r\n", file);
134
135
136     printf("%s: Action completed.\n", name);
137     fclose(fp);
138
139     return(0);
140                     
141 }
142
143 /* Setup windows after install */
144 int main(int argc, char **argv)
145 {
146     int quiet = 0;
147     
148     if(argc < 2)
149     {
150         printf("%s: Invalid syntax.\n", argv[0]);
151         printf("Try: '%s <file_name>'\n\n", argv[0]);
152     }
153
154     /* Looking for the quiet option */
155     if((argc == 3) && (strcmp(argv[2],"--quiet") == 0))
156     {
157         quiet = 1;
158     }
159
160     
161     /* Checking if ossec was installed already */
162     if(!fileexist(OSSECCONF))
163     {
164         printf("%s: Unable to find ossec config: '%s'.\n", argv[0], OSSECCONF);
165     }
166
167     else
168     {
169         config_file(argv[0], argv[1], quiet);
170     }
171     
172     return(0);
173 }