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