Imported Upstream version 2.7
[ossec-hids.git] / src / rootcheck / rootcheck-config.c
1 /* @(#) $Id: ./src/rootcheck/rootcheck-config.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 #ifndef OSSECHIDS
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "shared.h"
21
22 #include "os_xml/os_xml.h"
23
24 #include "rootcheck.h"
25
26
27 /*evaluate boolean with two arguments
28  * str: input string, "yes"|"no"
29  * default_val: 1(yes)|0(no)
30  */
31 short eval_bool2(char *str, short default_val)
32 {
33     short ret = default_val;
34
35     if (str == NULL)
36         return(ret);
37     else if (strcmp(str, "yes") == 0)
38         ret = 1;
39     else if (strcmp(str, "no") == 0)
40         ret = 0;
41
42     free(str);
43     return(ret);
44 }
45
46
47 /* Read_Rootcheck_Config: Reads the rootcheck config
48  */
49 int Read_Rootcheck_Config(char * cfgfile)
50 {
51     OS_XML xml;
52
53     char *str = NULL;
54
55
56     /* XML Definitions */
57     char *(xml_daemon[])={xml_rootcheck,"daemon", NULL};
58     char *(xml_notify[])={xml_rootcheck, "notify", NULL};
59     char *(xml_base_dir[])={xml_rootcheck, "base_directory", NULL};
60     char *(xml_workdir[])={xml_rootcheck, "work_directory", NULL};
61     char *(xml_rootkit_files[])={xml_rootcheck, "rootkit_files", NULL};
62     char *(xml_rootkit_trojans[])={xml_rootcheck, "rootkit_trojans", NULL};
63     char *(xml_rootkit_unixaudit[])={xml_rootcheck, "system_audit", NULL};
64     char *(xml_rootkit_winaudit[])={xml_rootcheck, "windows_audit", NULL};
65     char *(xml_rootkit_winapps[])={xml_rootcheck, "windows_apps", NULL};
66     char *(xml_rootkit_winmalware[])={xml_rootcheck, "windows_malware", NULL};
67     char *(xml_scanall[])={xml_rootcheck, "scanall", NULL};
68     char *(xml_readall[])={xml_rootcheck, "readall", NULL};
69     char *(xml_time[])={xml_rootcheck, "frequency", NULL};
70
71     char *(xml_check_dev[])={xml_rootcheck, "check_dev", NULL};
72     char *(xml_check_files[])={xml_rootcheck, "check_files", NULL};
73     char *(xml_check_if[])={xml_rootcheck, "check_if", NULL};
74     char *(xml_check_pids[])={xml_rootcheck, "check_pids", NULL};
75     char *(xml_check_ports[])={xml_rootcheck, "check_ports", NULL};
76     char *(xml_check_sys[])={xml_rootcheck, "check_sys", NULL};
77     char *(xml_check_trojans[])={xml_rootcheck, "check_trojans", NULL};
78
79     #ifdef WIN32
80
81     char *(xml_check_winapps[])={xml_rootcheck, "check_winapps", NULL};
82     char *(xml_check_winaudit[])={xml_rootcheck, "check_winaudit", NULL};
83     char *(xml_check_winmalware[])={xml_rootcheck, "check_winmalware", NULL};
84
85     #else
86
87     char *(xml_check_unixaudit[])={xml_rootcheck, "check_unixaudit", NULL};
88
89     #endif
90
91     /* :) */
92     xml_time[2] = NULL;
93
94     if(OS_ReadXML(cfgfile,&xml) < 0)
95     {
96         merror("config_op: XML error: %s",xml.err);
97         return(OS_INVALID);
98     }
99
100     if(!OS_RootElementExist(&xml,xml_rootcheck))
101     {
102         OS_ClearXML(&xml);
103         merror("%s: Rootcheck configuration not found. ",ARGV0);
104         return(-1);
105     }
106
107
108     /* run as a daemon */
109     rootcheck.daemon = eval_bool2(OS_GetOneContentforElement(&xml,xml_daemon), rootcheck.daemon);
110
111     /* time  */
112     #ifdef OSSECHIDS
113     str = OS_GetOneContentforElement(&xml,xml_time);
114     if(str)
115     {
116         if(!OS_StrIsNum(str))
117         {
118             merror("Invalid frequency time '%s' for the rootkit "
119                     "detection (must be int).", str);
120             return(OS_INVALID);
121         }
122
123         rootcheck.time = atoi(str);
124
125         free(str);
126         str = NULL;
127     }
128     #endif
129
130
131     /* Scan all flag */
132     if(!rootcheck.scanall)
133     {
134         rootcheck.scanall = eval_bool2(OS_GetOneContentforElement(&xml,xml_scanall), 0);
135     }
136
137
138     /* read all flag */
139     if(!rootcheck.readall)
140     {
141         rootcheck.readall = eval_bool2(OS_GetOneContentforElement(&xml,xml_readall), 0);
142     }
143
144
145     /* Notifications type */
146     str  = OS_GetOneContentforElement(&xml,xml_notify);
147     if(str)
148     {
149         if(strcasecmp(str,"queue") == 0)
150             rootcheck.notify = QUEUE;
151         else if(strcasecmp(str,"syslog") == 0)
152             rootcheck.notify = SYSLOG;
153         else
154         {
155             merror("%s: Invalid notification option. Only "
156                       "'syslog' or 'queue' are allowed.",ARGV0);
157             return(-1);
158         }
159
160         free(str);
161         str = NULL;
162     }
163     else
164     {
165         /* Default to SYSLOG */
166         rootcheck.notify = SYSLOG;
167     }
168
169     /* Getting work directory */
170     if(!rootcheck.workdir)
171         rootcheck.workdir  = OS_GetOneContentforElement(&xml,xml_workdir);
172
173
174     rootcheck.rootkit_files  = OS_GetOneContentforElement
175                                (&xml,xml_rootkit_files);
176     rootcheck.rootkit_trojans  = OS_GetOneContentforElement
177                                (&xml,xml_rootkit_trojans);
178
179     rootcheck.unixaudit = OS_GetContents
180                                 (&xml,xml_rootkit_unixaudit);
181
182     rootcheck.winaudit  = OS_GetOneContentforElement
183                                 (&xml,xml_rootkit_winaudit);
184
185     rootcheck.winapps  = OS_GetOneContentforElement
186                                 (&xml,xml_rootkit_winapps);
187
188     rootcheck.winmalware  = OS_GetOneContentforElement
189                                 (&xml,xml_rootkit_winmalware);
190
191     rootcheck.basedir  = OS_GetOneContentforElement(&xml, xml_base_dir);
192
193     rootcheck.checks.rc_dev = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_dev), 1);
194     rootcheck.checks.rc_files = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_files), 1);
195     rootcheck.checks.rc_if = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_if), 1);
196     rootcheck.checks.rc_pids = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_pids), 1);
197     rootcheck.checks.rc_ports = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_ports), 1);
198     rootcheck.checks.rc_sys = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_sys), 1);
199     rootcheck.checks.rc_trojans = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_trojans), 1);
200
201     #ifdef WIN32
202
203     rootcheck.checks.rc_winapps = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_winapps), 1);
204     rootcheck.checks.rc_winaudit = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_winaudit), 1);
205     rootcheck.checks.rc_winmalware = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_winmalware), 1);
206
207     #else
208
209     rootcheck.checks.rc_unixaudit = eval_bool2(OS_GetOneContentforElement(&xml,xml_check_unixaudit), 1);
210
211     #endif
212
213     OS_ClearXML(&xml);
214
215     debug1("%s: DEBUG: Daemon set to '%d'",ARGV0, rootcheck.daemon);
216     debug1("%s: DEBUG: alert set to '%d'",ARGV0, rootcheck.notify);
217
218     return(0);
219 }
220
221 /* EOF */
222 #endif