Imported Upstream version 2.7
[ossec-hids.git] / src / rootcheck / check_rc_files.c
1 /* @(#) $Id: ./src/rootcheck/check_rc_files.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 #include "shared.h"
15 #include "rootcheck.h"
16
17
18
19 /* check_rc_files:
20  * Read the file pointer specified (rootkit_files)
21  * and check if the configured file is there
22  */
23 void check_rc_files(char *basedir, FILE *fp)
24 {
25     char buf[OS_SIZE_1024 +1];
26     char file_path[OS_SIZE_1024 +1];
27
28     char *file;
29     char *name;
30     char *link;
31
32     int _errors = 0;
33     int _total = 0;
34
35
36     debug1("%s: DEBUG: Starting on check_rc_files", ARGV0);
37
38     while(fgets(buf, OS_SIZE_1024, fp) != NULL)
39     {
40         char *nbuf;
41
42         /* Removing end of line */
43         nbuf = strchr(buf, '\n');
44         if(nbuf)
45         {
46             *nbuf = '\0';
47         }
48
49         /* Assigning buf to be used */
50         nbuf = buf;
51
52         /* Excluding commented lines or blanked ones */
53         while(*nbuf != '\0')
54         {
55             if(*nbuf == ' ' || *nbuf == '\t')
56             {
57                 nbuf++;
58                 continue;
59             }
60             else if(*nbuf == '#')
61                 goto newline;
62             else
63                 break;
64         }
65
66         if(*nbuf == '\0')
67             goto newline;
68
69         /* File now may be valid */
70         file = nbuf;
71         name = nbuf;
72
73
74         /* Getting the file and the rootkit name */
75         while(*nbuf != '\0')
76         {
77             if(*nbuf == ' ' || *nbuf == '\t')
78             {
79                 /* Setting the limit for the file */
80                 *nbuf = '\0';
81                 nbuf++;
82                 break;
83             }
84             else
85             {
86                 nbuf++;
87             }
88         }
89
90         if(*nbuf == '\0')
91             goto newline;
92
93
94         /* Some ugly code to remove spaces and \t */
95         while(*nbuf != '\0')
96         {
97            if(*nbuf == '!')
98            {
99                nbuf++;
100                if(*nbuf == ' ' || *nbuf == '\t')
101                {
102                    nbuf++;
103                    name = nbuf;
104
105                    break;
106                }
107            }
108            else if(*nbuf == ' ' || *nbuf == '\t')
109            {
110                nbuf++;
111                continue;
112            }
113            else
114            {
115                goto newline;
116            }
117         }
118
119
120         /* Getting the link (if present) */
121         link = strchr(nbuf, ':');
122         if(link)
123         {
124             *link = '\0';
125
126             link++;
127             if(*link == ':')
128             {
129                 link++;
130             }
131         }
132
133
134         /* Cleaning any space of \t at the end */
135         nbuf = strchr(nbuf, ' ');
136         if(nbuf)
137         {
138             *nbuf = '\0';
139         }
140
141         nbuf = strchr(nbuf, '\t');
142         if(nbuf)
143         {
144             *nbuf = '\0';
145         }
146
147         _total++;
148
149
150         /* Checking if it is a file to search everywhere */
151         if(*file == '*')
152         {
153             if(rk_sys_count >= MAX_RK_SYS)
154             {
155                 merror(MAX_RK_MSG, ARGV0, MAX_RK_SYS);
156             }
157
158             else
159             {
160                 /* Removing * / from the file */
161                 file++;
162                 if(*file == '/')
163                     file++;
164
165                 /* Memory assignment */
166                 rk_sys_file[rk_sys_count] = strdup(file);
167                 rk_sys_name[rk_sys_count] = strdup(name);
168
169                 if(!rk_sys_name[rk_sys_count] ||
170                    !rk_sys_file[rk_sys_count] )
171                 {
172                     merror(MEM_ERROR, ARGV0);
173
174                     if(rk_sys_file[rk_sys_count])
175                         free(rk_sys_file[rk_sys_count]);
176                     if(rk_sys_name[rk_sys_count])
177                         free(rk_sys_name[rk_sys_count]);
178
179                     rk_sys_file[rk_sys_count] = NULL;
180                     rk_sys_name[rk_sys_count] = NULL;
181                 }
182
183                 rk_sys_count++;
184
185                 /* Always assigning the last as NULL */
186                 rk_sys_file[rk_sys_count] = NULL;
187                 rk_sys_name[rk_sys_count] = NULL;
188             }
189             continue;
190         }
191
192         snprintf(file_path, OS_SIZE_1024, "%s/%s",basedir, file);
193
194         /* Checking if file exists */
195         if(is_file(file_path))
196         {
197             char op_msg[OS_SIZE_1024 +1];
198
199             _errors = 1;
200             snprintf(op_msg, OS_SIZE_1024, "Rootkit '%s' detected "
201                      "by the presence of file '%s'.",name, file_path);
202
203             notify_rk(ALERT_ROOTKIT_FOUND, op_msg);
204         }
205
206         newline:
207             continue;
208     }
209
210     if(_errors == 0)
211     {
212         char op_msg[OS_SIZE_1024 +1];
213         snprintf(op_msg,OS_SIZE_1024,"No presence of public rootkits detected."
214                                     " Analyzed %d files.", _total);
215         notify_rk(ALERT_OK, op_msg);
216     }
217 }
218
219
220 /* EOF */