Imported Upstream version 2.7
[ossec-hids.git] / src / analysisd / makelists.c
1 /* @(#) $Id: ./src/analysisd/makelists.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2010 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  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17 /* Part of the OSSEC
18  * Available at http://www.ossec.net
19  */
20
21
22 /* ossec-analysisd.
23  * Responsible for correlation and log decoding.
24  */
25 #ifdef ARGV0
26     #undef ARGV0
27     #define ARGV0 "ossec-testrule"
28 #endif
29
30 #include "shared.h"
31
32
33 /** Local headers **/
34 #include "active-response.h"
35 #include "config.h"
36 #include "rules.h"
37 #include "stats.h"
38 #include "lists_make.h"
39
40 #include "eventinfo.h"
41 #include "analysisd.h"
42
43 #include "picviz.h"
44
45
46
47 /** External functions prototypes (only called here) **/
48
49 /* For config  */
50 int GlobalConf(char * cfgfile);
51
52
53 /* For Lists */
54 void Lists_OP_CreateLists();
55
56 void makelist_help(const char *prog)
57 {
58     print_out(" ");
59     print_out("%s %s - %s (%s)", __name, __version, __author, __contact);
60     print_out("%s", __site);
61     print_out(" ");
62     print_out("  %s: -[Vhdt] [-u user] [-g group] [-c config] [-D dir]", prog);
63     print_out("    -V          Version and license message");
64     print_out("    -h          This help message");
65     print_out("    -d          Execute in debug mode");
66     print_out("    -f          Force rebuild of all databases");
67     print_out("    -u <user>   Run as 'user'");
68     print_out("    -g <group>  Run as 'group'");
69     print_out("    -c <config> Read the 'config' file");
70     print_out("    -D <dir>    Chroot to 'dir'");
71     print_out(" ");
72     exit(1);
73 }
74
75 /** int main(int argc, char **argv)
76  */
77 int main(int argc, char **argv)
78 {
79     int c = 0;
80     char *dir = DEFAULTDIR;
81     char *user = USER;
82     char *group = GROUPGLOBAL;
83     int uid = 0,gid = 0;
84     int force = 0;
85
86     char *cfg = DEFAULTCPATH;
87
88     /* Setting the name */
89     OS_SetName(ARGV0);
90
91     thishour = 0;
92     today = 0;
93     prev_year = 0;
94     memset(prev_month, '\0', 4);
95
96     while((c = getopt(argc, argv, "Vdhfu:g:D:c:")) != -1){
97         switch(c){
98             case 'V':
99                 print_version();
100                 break;
101             case 'h':
102                 makelist_help(ARGV0);
103                 break;
104             case 'd':
105                 nowDebug();
106                 break;
107             case 'u':
108                 if(!optarg)
109                     ErrorExit("%s: -u needs an argument",ARGV0);
110                 user = optarg;
111                 break;
112             case 'g':
113                 if(!optarg)
114                     ErrorExit("%s: -g needs an argument",ARGV0);
115                 group = optarg;
116                 break;
117             case 'D':
118                 if(!optarg)
119                     ErrorExit("%s: -D needs an argument",ARGV0);
120                 dir = optarg;
121             case 'c':
122                 if(!optarg)
123                     ErrorExit("%s: -c needs an argument",ARGV0);
124                 cfg = optarg;
125                 break;
126             case 'f':
127                 force = 1;
128                 break;
129             default:
130                 help(ARGV0);
131                 break;
132         }
133
134     }
135
136
137     /*Check if the user/group given are valid */
138     uid = Privsep_GetUser(user);
139     gid = Privsep_GetGroup(group);
140     if((uid < 0)||(gid < 0))
141         ErrorExit(USER_ERROR,ARGV0,user,group);
142
143
144     /* Found user */
145     debug1(FOUND_USER, ARGV0);
146
147
148     /* Reading configuration file */
149     if(GlobalConf(cfg) < 0)
150     {
151         ErrorExit(CONFIG_ERROR,ARGV0, cfg);
152     }
153
154     debug1(READ_CONFIG, ARGV0);
155
156     /* Setting the group */     
157     if(Privsep_SetGroup(gid) < 0)
158         ErrorExit(SETGID_ERROR,ARGV0,group);
159
160     /* Chrooting */
161     if(Privsep_Chroot(dir) < 0)
162         ErrorExit(CHROOT_ERROR,ARGV0,dir);
163
164     nowChroot();
165
166
167
168     /* Createing the lists for use in rules */
169     Lists_OP_CreateLists();
170
171     /* Reading the lists */
172     {
173         char **listfiles;
174         listfiles = Config.lists;
175         while(listfiles && *listfiles)
176         {
177             if(Lists_OP_LoadList(*listfiles) < 0)
178                 ErrorExit(LISTS_ERROR, ARGV0, *listfiles);
179             free(*listfiles);
180             listfiles++;
181         }
182         free(Config.lists);
183         Config.lists = NULL;
184     }
185
186     Lists_OP_MakeAll(force);
187
188     exit(0);
189 }
190
191 /* EOF */