Imported Upstream version 2.7
[ossec-hids.git] / src / rootcheck / rootcheck.c
1 /* @(#) $Id: ./src/rootcheck/rootcheck.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right 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  * Rootcheck v 0.3
15  * Copyright (C) 2003 Daniel B. Cid <daniel@underlinux.com.br>
16  * http://www.ossec.net/rootcheck/
17  *
18  */
19
20 /* Included from the Rootcheck project */
21
22
23 #include "headers/shared.h"
24
25 #include "rootcheck.h"
26
27 #ifndef ARGV0
28 #define ARGV0 "rootcheck"
29 #endif
30
31
32
33 /** Prototypes **/
34 /* Read the new XML config */
35 int Read_Rootcheck_Config(char * cfgfile, rkconfig *cfg);
36
37
38 #ifndef OSSECHIDS
39
40 void rootcheck_help()
41 {
42     printf("\n");
43     printf("Rootcheck v0.8 (Mar/12/2008):\n");
44     printf("http://www.ossec.net/rootcheck/\n");
45     printf("Available options:\n");
46     printf("\t\t-h\t  This Help message\n");
47     printf("\t\t-c <file> Configuration file\n");
48     printf("\t\t-d\t  Enable debug\n");
49     printf("\t\t-D <dir>  Set the working directory\n");
50     printf("\t\t-s\t  Scans the whole system\n");
51     printf("\t\t-r\t  Read all the files for kernel-based detection\n");
52     printf("\n");
53     exit(0);
54 }
55
56 /* main v0.1
57  *
58  */
59 int main(int argc, char **argv)
60 {
61     int c;
62     int test_config = 0;
63
64 #else
65
66 int rootcheck_init(int test_config)
67 {
68     int c;
69
70 #endif
71
72     #ifdef OSSECHIDS
73     char *cfg = DEFAULTCPATH;
74     #else
75     char *cfg = "./rootcheck.conf";
76     #endif
77
78     /* Zeroing the structure, initializing default values */
79     rootcheck.workdir = NULL;
80     rootcheck.basedir = NULL;
81     rootcheck.unixaudit = NULL;
82     rootcheck.ignore = NULL;
83     rootcheck.rootkit_files = NULL;
84     rootcheck.rootkit_trojans = NULL;
85     rootcheck.winaudit = NULL;
86     rootcheck.winmalware = NULL;
87     rootcheck.winapps = NULL;
88     rootcheck.daemon = 1;
89     rootcheck.notify = QUEUE;
90     rootcheck.scanall = 0;
91     rootcheck.readall = 0;
92     rootcheck.disabled = 0;
93     rootcheck.alert_msg = NULL;
94     rootcheck.time = ROOTCHECK_WAIT;
95
96
97     rootcheck.checks.rc_dev = 1;
98     rootcheck.checks.rc_files = 1;
99     rootcheck.checks.rc_if = 1;
100     rootcheck.checks.rc_pids = 1;
101     rootcheck.checks.rc_ports = 1;
102     rootcheck.checks.rc_sys = 1;
103     rootcheck.checks.rc_trojans = 1;
104
105     #ifdef WIN32
106
107     rootcheck.checks.rc_winaudit = 1;
108     rootcheck.checks.rc_winmalware = 1;
109     rootcheck.checks.rc_winapps = 1;
110
111     #else
112
113     rootcheck.checks.rc_unixaudit = 1;
114
115     #endif
116
117     /* We store up to 255 alerts in there. */
118     os_calloc(256, sizeof(char *), rootcheck.alert_msg);
119     c = 0;
120     while(c <= 255)
121     {
122         rootcheck.alert_msg[c] = NULL;
123         c++;
124     }
125
126
127     #ifndef OSSECHIDS
128     rootcheck.notify = SYSLOG;
129     rootcheck.daemon = 0;
130     while((c = getopt(argc, argv, "VstrdhD:c:")) != -1)
131     {
132         switch(c)
133         {
134             case 'V':
135                 print_version();
136                 break;
137             case 'h':
138                 rootcheck_help();
139                 break;
140             case 'd':
141                 nowDebug();
142                 break;
143             case 'D':
144                 if(!optarg)
145                     ErrorExit("%s: -D needs an argument",ARGV0);
146                 rootcheck.workdir = optarg;
147                 break;
148             case 'c':
149                 if(!optarg)
150                     ErrorExit("%s: -c needs an argument",ARGV0);
151                 cfg = optarg;
152                 break;
153             case 's':
154                 rootcheck.scanall = 1;
155                 break;
156             case 't':
157                 test_config = 1;
158                 break;
159             case 'r':
160                 rootcheck.readall = 1;
161                 break;
162             default:
163                 rootcheck_help();
164                 break;
165         }
166
167     }
168
169
170     #ifdef WIN32
171     /* Starting Winsock */
172     {
173         WSADATA wsaData;
174         if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
175         {
176             ErrorExit("%s: WSAStartup() failed", ARGV0);
177         }
178     }
179     #endif
180
181
182     #endif /* OSSECHIDS */
183
184
185     /* Staring message */
186     debug1(STARTED_MSG,ARGV0);
187
188
189     /* Checking if the configuration is present */
190     if(File_DateofChange(cfg) < 0)
191     {
192         merror("%s: Configuration file '%s' not found",ARGV0,cfg);
193         return(-1);
194     }
195
196
197     /* Reading configuration  --function specified twice (check makefile) */
198     if(Read_Rootcheck_Config(cfg, &rootcheck) < 0)
199     {
200         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
201     }
202
203
204     /* If testing config, exit here */
205     if(test_config)
206         return(0);
207
208
209     /* Return 1 disables rootcheck */
210     if(rootcheck.disabled == 1)
211     {
212         verbose("%s: Rootcheck disabled. Exiting.", ARGV0);
213         return(1);
214     }
215
216
217     /* Checking if Unix audit file is configured. */
218     if(!rootcheck.unixaudit)
219     {
220         #ifndef WIN32
221         log2file("%s: System audit file not configured.", ARGV0);
222         #endif
223     }
224
225
226     /* Setting default values */
227     if(rootcheck.workdir == NULL)
228         rootcheck.workdir = DEFAULTDIR;
229
230
231     #ifdef OSSECHIDS
232
233
234     /* Start up message */
235     #ifdef WIN32
236     verbose(STARTUP_MSG, "ossec-rootcheck", getpid());
237     #else
238
239
240     /* Connect to the queue if configured to do so */
241     if(rootcheck.notify == QUEUE)
242     {
243         debug1("%s: Starting queue ...",ARGV0);
244
245         /* Starting the queue. */
246         if((rootcheck.queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
247         {
248             merror(QUEUE_ERROR,ARGV0,DEFAULTQPATH, strerror(errno));
249
250             /* 5 seconds to see if the agent starts */
251             sleep(5);
252             if((rootcheck.queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
253             {
254                 /* more 10 seconds wait.. */
255                 merror(QUEUE_ERROR,ARGV0,DEFAULTQPATH, strerror(errno));
256                 sleep(10);
257                 if((rootcheck.queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
258                     ErrorExit(QUEUE_FATAL,ARGV0,DEFAULTQPATH);
259             }
260         }
261     }
262
263     #endif /* Not win32 */
264
265     #endif /* ossec hids */
266
267
268     /* Initializing rk list */
269     rk_sys_name = calloc(MAX_RK_SYS +2, sizeof(char *));
270     rk_sys_file = calloc(MAX_RK_SYS +2, sizeof(char *));
271     if(!rk_sys_name || !rk_sys_file)
272     {
273         ErrorExit(MEM_ERROR, ARGV0);
274     }
275     rk_sys_name[0] = NULL;
276     rk_sys_file[0] = NULL;
277
278
279     #ifndef OSSECHIDS
280
281     #ifndef WIN32
282     /* Start the signal handling */
283     StartSIG(ARGV0);
284     #endif
285
286     #else
287     return(0);
288
289     #endif
290
291
292     debug1("%s: DEBUG: Running run_rk_check",ARGV0);
293     run_rk_check();
294
295
296     debug1("%s: DEBUG:  Leaving...",ARGV0);
297
298     return(0);
299 }
300
301 /* EOF */