new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / win32 / add-localfile.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <time.h>
15
16 #include "os_regex/os_regex.h"
17
18 #define OSSECCONF   "ossec.conf"
19 #define OS_MAXSTR   1024
20
21 int total;
22
23
24 int fileexist(char *file)
25 {
26     FILE *fp;
27
28     /* Open file */
29     fp = fopen(file, "r");
30     if (!fp) {
31         return (0);
32     }
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     /* Open file */
44     fp = fopen(file, "r");
45     if (!fp) {
46         return (0);
47     }
48
49     /* Clear memory */
50     memset(line, '\0', OS_MAXSTR + 1);
51
52     /* Read file and look for str */
53     while (fgets(line, OS_MAXSTR, fp) != NULL) {
54         if (OS_Match(str, line)) {
55             fclose(fp);
56             return (1);
57         }
58     }
59
60     fclose(fp);
61     return (0);
62 }
63
64 /* Check if syscheck is present in the config */
65 int config_file(char *name, char *file, int quiet)
66 {
67     char ffile[256];
68     FILE *fp;
69
70     ffile[255] = '\0';
71
72     /* Check if the file has a variable format */
73     if (strchr(file, '%') != NULL) {
74         time_t tm;
75         struct tm *p;
76
77         tm = time(NULL);
78         p = localtime(&tm);
79
80         if (strftime(ffile, 255, file, p) == 0) {
81             return (-1);
82         }
83     } else {
84         strncpy(ffile, file, 255);
85     }
86
87     /* Look for ffile */
88     if (!fileexist(ffile)) {
89         if (quiet == 0) {
90             printf("%s: Log file not existent: '%s'.\n", name, file);
91         }
92         return (-1);
93     }
94
95     if (dogrep(OSSECCONF, file)) {
96         printf("%s: Log file already configured: '%s'.\n",
97                name, file);
98         return (0);
99     }
100
101     /* Add IIS config */
102     fp = fopen(OSSECCONF, "a");
103     if (!fp) {
104         printf("%s: Unable to edit configuration file.\n", name);
105         return (0);
106     }
107
108     printf("%s: Adding log file to be monitored: '%s'.\n", name, file);
109     fprintf(fp, "\r\n"
110             "\r\n"
111             "<!-- Extra log file -->\r\n"
112             "<ossec_config>\r\n"
113             "  <localfile>\r\n"
114             "    <location>%s</location>\r\n"
115             "    <log_format>syslog</log_format>\r\n"
116             "  </localfile>\r\n"
117             "</ossec_config>\r\n\r\n", file);
118
119     printf("%s: Action completed.\n", name);
120     fclose(fp);
121
122     return (0);
123 }
124
125 /* Setup Windows after install */
126 int main(int argc, char **argv)
127 {
128     int quiet = 0;
129
130     if (argc < 2) {
131         printf("%s: Invalid syntax.\n", argv[0]);
132         printf("Try: '%s <file_name>'\n\n", argv[0]);
133     }
134
135     /* Look for the quiet option */
136     if ((argc == 3) && (strcmp(argv[2], "--quiet") == 0)) {
137         quiet = 1;
138     }
139
140     /* Check if OSSEC-HIDS was installed already */
141     if (!fileexist(OSSECCONF)) {
142         printf("%s: Unable to find ossec config: '%s'.\n", argv[0], OSSECCONF);
143     } else {
144         config_file(argv[0], argv[1], quiet);
145     }
146
147     return (0);
148 }