072964e9433036c769e0c489aea26df024d5f759
[ossec-hids.git] / src / rootcheck / unix-process.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  * License details at the LICENSE file included with OSSEC or 
12  * online at: http://www.ossec.net/main/license/ .
13  */
14
15  
16 #include "shared.h"
17 #include "rootcheck.h"
18
19 #ifndef WIN32
20
21 char *_os_get_runps(char *ps, int mpid)
22 {
23     char *tmp_str, *nbuf;
24     char buf[OS_SIZE_2048 +1];
25     char command[OS_SIZE_1024 +1];
26     FILE *fp;
27     
28     
29     buf[0] = '\0';
30     command[0] = '\0';
31     command[OS_SIZE_1024] = '\0'; 
32     
33     
34     snprintf(command, OS_SIZE_1024, "%s -p %d 2> /dev/null", ps, mpid);
35
36     fp = popen(command, "r");
37     if(fp)
38     {
39         while(fgets(buf, OS_SIZE_2048, fp) != NULL)
40         {
41             tmp_str = strchr(buf, ':');
42             if(!tmp_str)
43             {
44                 continue;
45             }
46
47             nbuf = tmp_str++;
48
49             tmp_str = strchr(nbuf, ' ');
50             if(!tmp_str)
51             {
52                 continue;
53             }
54             tmp_str++;
55
56
57             /* Removing white spaces. */
58             while(*tmp_str == ' ')
59                  tmp_str++;
60
61                  
62             nbuf = tmp_str;
63             
64
65             tmp_str = strchr(nbuf, '\n');
66             if(tmp_str)
67             {
68                 *tmp_str = '\0';
69             }
70
71             pclose(fp);
72             return(strdup(nbuf));
73         }
74
75         pclose(fp);
76     }
77
78     return(NULL);
79 }
80
81
82
83 /* os_get_unix_process_list: Get list of Unix processes */
84 void *os_get_process_list()
85 {
86     int i = 1;
87     pid_t max_pid = MAX_PID;
88     OSList *p_list = NULL;
89     
90     char ps[OS_SIZE_1024 +1];
91
92
93     /* Checking where ps is */
94     memset(ps, '\0', OS_SIZE_1024 +1);
95     strncpy(ps, "/bin/ps", OS_SIZE_1024);
96     if(!is_file(ps))
97     {
98         strncpy(ps, "/usr/bin/ps", OS_SIZE_1024);
99         if(!is_file(ps))
100         {
101             merror("%s: ERROR: 'ps' not found.", ARGV0);
102             return(NULL);
103         }
104     }
105
106
107     /* Creating process list */
108     p_list = OSList_Create();
109     if(!p_list)
110     {
111         merror(LIST_ERROR, ARGV0);
112         return(NULL); 
113     }
114
115
116
117     for(i = 1; i<= max_pid; i++)
118     {
119         /* Checking if the pid is present. */
120         if((!((getsid(i) == -1)&&(errno == ESRCH))) &&
121           (!((getpgid(i) == -1)&&(errno == ESRCH))))
122          {
123              Proc_Info *p_info;
124              char *p_name;
125
126              p_name = _os_get_runps(ps, (int)i);
127              if(!p_name)
128              {
129                  continue;
130              }
131
132              os_calloc(1, sizeof(Proc_Info), p_info);
133              p_info->p_path = p_name;
134              p_info->p_name = NULL;
135              OSList_AddData(p_list, p_info);
136          }
137     }
138     
139     return((void *)p_list);
140 }
141          
142  
143 #endif
144
145 /* EOF */