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