izmjene licence
[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)", __ossec_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                 break;
122             case 'c':
123                 if(!optarg)
124                     ErrorExit("%s: -c needs an argument",ARGV0);
125                 cfg = optarg;
126                 break;
127             case 'f':
128                 force = 1;
129                 break;
130             default:
131                 help(ARGV0);
132                 break;
133         }
134
135     }
136
137
138     /*Check if the user/group given are valid */
139     uid = Privsep_GetUser(user);
140     gid = Privsep_GetGroup(group);
141     if((uid < 0)||(gid < 0))
142         ErrorExit(USER_ERROR,ARGV0,user,group);
143
144
145     /* Found user */
146     debug1(FOUND_USER, ARGV0);
147
148
149     /* Reading configuration file */
150     if(GlobalConf(cfg) < 0)
151     {
152         ErrorExit(CONFIG_ERROR,ARGV0, cfg);
153     }
154
155     debug1(READ_CONFIG, ARGV0);
156
157     /* Setting the group */
158     if(Privsep_SetGroup(gid) < 0)
159         ErrorExit(SETGID_ERROR,ARGV0,group);
160
161     /* Chrooting */
162     if(Privsep_Chroot(dir) < 0)
163         ErrorExit(CHROOT_ERROR,ARGV0,dir);
164
165     nowChroot();
166
167
168
169     /* Createing the lists for use in rules */
170     Lists_OP_CreateLists();
171
172     /* Reading the lists */
173     {
174         char **listfiles;
175         listfiles = Config.lists;
176         while(listfiles && *listfiles)
177         {
178             if(Lists_OP_LoadList(*listfiles) < 0)
179                 ErrorExit(LISTS_ERROR, ARGV0, *listfiles);
180             free(*listfiles);
181             listfiles++;
182         }
183         free(Config.lists);
184         Config.lists = NULL;
185     }
186
187     Lists_OP_MakeAll(force);
188
189     exit(0);
190 }
191
192 /* EOF */