d136d6e2a29b9cff0dc5aeef09625434238c10b0
[ossec-hids.git] / src / rootcheck / check_rc_readproc.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 #ifndef WIN32
14 #include "shared.h"
15 #include "rootcheck.h"
16
17 #define PROC    0
18 #define PID     1
19 #define TASK    2
20
21 int proc_pid_found;
22
23
24 /** Prototypes **/
25 int read_proc_dir(char *dir_name, char *pid, int position);
26
27
28 int read_proc_file(char *file_name, char *pid, int position)
29 {
30     struct stat statbuf;
31    
32     if(lstat(file_name, &statbuf) < 0)
33     {
34         return(-1);
35     }
36     
37     /* If directory, read the directory */
38     else if(S_ISDIR(statbuf.st_mode))
39     {
40         return(read_proc_dir(file_name, pid, position));
41     }
42     
43     return(0);
44 }
45
46 /* read_dir v0.1
47  *
48  */
49 int read_proc_dir(char *dir_name, char *pid, int position)
50 {
51     DIR *dp;
52     
53         struct dirent *entry;
54         
55     
56     if((dir_name == NULL)||(strlen(dir_name) > PATH_MAX))
57     {
58         merror("%s: Invalid directory given",ARGV0);
59         return(-1);
60     }
61     
62     /* Opening the directory given */
63     dp = opendir(dir_name);
64         if(!dp)
65     {
66         return(0);
67     }
68
69     while((entry = readdir(dp)) != NULL)
70     {
71         char f_name[PATH_MAX +2];
72
73         /* Just ignore . and ..  */
74         if((strcmp(entry->d_name,".") == 0) ||
75            (strcmp(entry->d_name,"..") == 0))  
76             continue;
77
78         if(position == PROC)
79         {
80             char *tmp_str;
81
82             tmp_str = entry->d_name;
83
84             while(*tmp_str != '\0')
85             {
86                 if(!isdigit((int)*tmp_str))
87                     break;
88                 tmp_str++;
89             }
90
91             if(*tmp_str != '\0')
92                 continue;
93            
94             
95             snprintf(f_name, PATH_MAX +1, "%s/%s",dir_name, entry->d_name);
96
97             read_proc_file(f_name, pid, position+1);
98         }
99
100         else if(position == PID)
101         {
102             if(strcmp(entry->d_name, "task") == 0)
103             {
104                 snprintf(f_name, PATH_MAX +1, "%s/%s",dir_name, entry->d_name);
105                 read_proc_file(f_name, pid, position+1);
106             }
107         }
108
109         else if(position == TASK)
110         {
111             /* checking under proc/pid/task/lwp */
112             if(strcmp(entry->d_name, pid) == 0)
113             {
114                 proc_pid_found = 1;
115                 break;
116             }
117         }
118         else
119         {
120             break;
121         }
122     }
123
124     closedir(dp);
125     
126     return(0);
127 }
128
129
130 /*  int check_rc_readproc(int pid): v0.1
131  *  Reads the /proc directory (if present) and checks
132  *  if the given pid is there (or as a PID or as a thread).
133  */
134 int check_rc_readproc(int pid)
135 {
136     char char_pid[32];
137
138     proc_pid_found = 0;
139    
140     /* NL threads */ 
141     snprintf(char_pid, 31, "/proc/.%d", pid);
142     if(is_file(char_pid))
143         return(1);
144     
145     
146     snprintf(char_pid, 31, "%d", pid);
147     
148     read_proc_dir("/proc", char_pid, PROC);
149     
150     return(proc_pid_found);
151 }
152
153 /* EOF */
154 #endif