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