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