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