new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / rootcheck / unix-process.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11 #include "rootcheck.h"
12
13
14 #ifndef WIN32
15
16 static char *_os_get_runps(const char *ps, int mpid)
17 {
18     char *tmp_str, *nbuf;
19     char buf[OS_SIZE_2048 + 1];
20     char command[OS_SIZE_1024 + 1];
21     FILE *fp;
22
23     buf[0] = '\0';
24     command[0] = '\0';
25     command[OS_SIZE_1024] = '\0';
26
27     snprintf(command, OS_SIZE_1024, "%s -p %d 2> /dev/null", ps, mpid);
28     fp = popen(command, "r");
29     if (fp) {
30         while (fgets(buf, OS_SIZE_2048, fp) != NULL) {
31             tmp_str = strchr(buf, ':');
32             if (!tmp_str) {
33                 continue;
34             }
35
36             nbuf = tmp_str++;
37
38             tmp_str = strchr(nbuf, ' ');
39             if (!tmp_str) {
40                 continue;
41             }
42             tmp_str++;
43
44             /* Remove whitespaces */
45             while (*tmp_str == ' ') {
46                 tmp_str++;
47             }
48
49             nbuf = tmp_str;
50
51             tmp_str = strchr(nbuf, '\n');
52             if (tmp_str) {
53                 *tmp_str = '\0';
54             }
55
56             pclose(fp);
57             return (strdup(nbuf));
58         }
59
60         pclose(fp);
61     }
62
63     return (NULL);
64 }
65
66 /* Get list of Unix processes */
67 OSList *os_get_process_list()
68 {
69     int i = 1;
70     pid_t max_pid = MAX_PID;
71     OSList *p_list = NULL;
72     char ps[OS_SIZE_1024 + 1];
73
74     /* Check where ps is */
75     memset(ps, '\0', OS_SIZE_1024 + 1);
76     strncpy(ps, "/bin/ps", OS_SIZE_1024);
77     if (!is_file(ps)) {
78         strncpy(ps, "/usr/bin/ps", OS_SIZE_1024);
79         if (!is_file(ps)) {
80             merror("%s: ERROR: 'ps' not found.", ARGV0);
81             return (NULL);
82         }
83     }
84
85     /* Create process list */
86     p_list = OSList_Create();
87     if (!p_list) {
88         merror(LIST_ERROR, ARGV0);
89         return (NULL);
90     }
91
92     for (i = 1; i <= max_pid; i++) {
93         /* Check if the pid is present */
94         if ((!((getsid(i) == -1) && (errno == ESRCH))) &&
95                 (!((getpgid(i) == -1) && (errno == ESRCH)))) {
96             Proc_Info *p_info;
97             char *p_name;
98
99             p_name = _os_get_runps(ps, (int)i);
100             if (!p_name) {
101                 continue;
102             }
103
104             os_calloc(1, sizeof(Proc_Info), p_info);
105             p_info->p_path = p_name;
106             p_info->p_name = NULL;
107             OSList_AddData(p_list, p_info);
108         }
109     }
110
111     return (p_list);
112 }
113
114 #endif /* WIN32 */
115