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