21ac7c7320e7d520610b3a0e4ef2ab57514f0d1e
1 /**
2 * collectd - src/exec.c
3 * Copyright (C) 2007 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_cmd_putval.h"
27 #include <sys/types.h>
28 #include <pwd.h>
29 #include <grp.h>
30 #include <signal.h>
32 #include <pthread.h>
34 /*
35 * Private data types
36 */
37 struct program_list_s;
38 typedef struct program_list_s program_list_t;
39 struct program_list_s
40 {
41 char *user;
42 char *group;
43 char *exec;
44 int pid;
45 program_list_t *next;
46 };
48 /*
49 * Private variables
50 */
51 static const char *config_keys[] =
52 {
53 "Exec"
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57 static program_list_t *pl_head = NULL;
59 /*
60 * Functions
61 */
62 static int exec_config (const char *key, const char *value)
63 {
64 if (strcasecmp ("Exec", key) == 0)
65 {
66 program_list_t *pl;
67 pl = (program_list_t *) malloc (sizeof (program_list_t));
68 if (pl == NULL)
69 return (1);
70 memset (pl, '\0', sizeof (program_list_t));
72 pl->user = strdup (value);
73 if (pl->user == NULL)
74 {
75 sfree (pl);
76 return (1);
77 }
79 pl->exec = strchr (pl->user, ' ');
80 if (pl->exec == NULL)
81 {
82 sfree (pl->user);
83 sfree (pl);
84 return (1);
85 }
86 while (*pl->exec == ' ')
87 {
88 *pl->exec = '\0';
89 pl->exec++;
90 }
92 if (*pl->exec == '\0')
93 {
94 sfree (pl->user);
95 sfree (pl);
96 return (1);
97 }
99 pl->next = pl_head;
100 pl_head = pl;
102 pl->group = strchr (pl->user, ':');
103 if (NULL != pl->group) {
104 *pl->group = '\0';
105 pl->group++;
106 }
107 }
108 else
109 {
110 return (-1);
111 }
113 return (0);
114 } /* int exec_config */
116 static void exec_child (program_list_t *pl)
117 {
118 int status;
119 int uid;
120 int gid;
121 char *arg0;
123 struct passwd *sp_ptr;
124 struct passwd sp;
125 char nambuf[2048];
126 char errbuf[1024];
128 sp_ptr = NULL;
129 status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
130 if (status != 0)
131 {
132 ERROR ("exec plugin: getpwnam_r failed: %s",
133 sstrerror (errno, errbuf, sizeof (errbuf)));
134 exit (-1);
135 }
136 if (sp_ptr == NULL)
137 {
138 ERROR ("exec plugin: No such user: `%s'", pl->user);
139 exit (-1);
140 }
142 uid = sp.pw_uid;
143 if (uid == 0)
144 {
145 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
146 exit (-1);
147 }
149 status = setuid (uid);
150 if (status != 0)
151 {
152 ERROR ("exec plugin: setuid failed: %s",
153 sstrerror (errno, errbuf, sizeof (errbuf)));
154 exit (-1);
155 }
157 if (NULL != pl->group)
158 {
159 if ('\0' != *pl->group) {
160 struct group *gr_ptr = NULL;
161 struct group gr;
163 status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
164 if (0 != status)
165 {
166 ERROR ("exec plugin: getgrnam_r failed: %s",
167 sstrerror (errno, errbuf, sizeof (errbuf)));
168 exit (-1);
169 }
170 if (NULL == gr_ptr)
171 {
172 ERROR ("exec plugin: No such group: `%s'", pl->group);
173 exit (-1);
174 }
176 gid = gr.gr_gid;
177 }
178 else
179 {
180 gid = sp.pw_gid;
181 }
183 status = setgid (gid);
184 if (0 != status)
185 {
186 ERROR ("exec plugin: setgid failed: %s",
187 sstrerror (errno, errbuf, sizeof (errbuf)));
188 exit (-1);
189 }
190 }
192 arg0 = strrchr (pl->exec, '/');
193 if (arg0 != NULL)
194 arg0++;
195 if ((arg0 == NULL) || (*arg0 == '\0'))
196 arg0 = pl->exec;
198 status = execlp (pl->exec, arg0, (char *) 0);
200 ERROR ("exec plugin: exec failed: %s",
201 sstrerror (errno, errbuf, sizeof (errbuf)));
202 exit (-1);
203 } /* void exec_child */
205 static int fork_child (program_list_t *pl)
206 {
207 int fd_pipe[2];
208 int status;
210 if (pl->pid != 0)
211 return (-1);
213 status = pipe (fd_pipe);
214 if (status != 0)
215 {
216 char errbuf[1024];
217 ERROR ("exec plugin: pipe failed: %s",
218 sstrerror (errno, errbuf, sizeof (errbuf)));
219 return (-1);
220 }
222 pl->pid = fork ();
223 if (pl->pid < 0)
224 {
225 char errbuf[1024];
226 ERROR ("exec plugin: fork failed: %s",
227 sstrerror (errno, errbuf, sizeof (errbuf)));
228 return (-1);
229 }
230 else if (pl->pid == 0)
231 {
232 close (fd_pipe[0]);
234 /* Connect the pipe to STDOUT and STDERR */
235 if (fd_pipe[1] != STDOUT_FILENO)
236 dup2 (fd_pipe[1], STDOUT_FILENO);
237 if (fd_pipe[1] != STDERR_FILENO)
238 dup2 (fd_pipe[1], STDERR_FILENO);
239 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
240 close (fd_pipe[1]);
242 exec_child (pl);
243 /* does not return */
244 }
246 close (fd_pipe[1]);
247 return (fd_pipe[0]);
248 } /* int fork_child */
250 static int parse_line (char *buffer)
251 {
252 char *fields[256];
253 int fields_num;
255 fields[0] = "PUTVAL";
256 fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1);
258 handle_putval (stdout, fields, fields_num + 1);
259 } /* int parse_line */
261 static void *exec_read_one (void *arg)
262 {
263 program_list_t *pl = (program_list_t *) arg;
264 int fd;
265 FILE *fh;
266 char buffer[1024];
268 fd = fork_child (pl);
269 if (fd < 0)
270 pthread_exit ((void *) 1);
272 assert (pl->pid != 0);
274 fh = fdopen (fd, "r");
275 if (fh == NULL)
276 {
277 char errbuf[1024];
278 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
279 sstrerror (errno, errbuf, sizeof (errbuf)));
280 kill (pl->pid, SIGTERM);
281 close (fd);
282 pthread_exit ((void *) 1);
283 }
285 while (fgets (buffer, sizeof (buffer), fh) != NULL)
286 {
287 int len;
289 len = strlen (buffer);
291 /* Remove newline from end. */
292 while ((len > 0) && ((buffer[len - 1] == '\n')
293 || (buffer[len - 1] == '\r')))
294 buffer[--len] = '\0';
296 DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
298 parse_line (buffer);
299 } /* while (fgets) */
301 fclose (fh);
302 pl->pid = 0;
304 pthread_exit ((void *) 0);
305 return (NULL);
306 } /* void *exec_read_one */
308 static int exec_read (void)
309 {
310 program_list_t *pl;
312 for (pl = pl_head; pl != NULL; pl = pl->next)
313 {
314 pthread_t t;
315 pthread_attr_t attr;
317 if (pl->pid != 0)
318 continue;
320 pthread_attr_init (&attr);
321 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
322 pthread_create (&t, &attr, exec_read_one, (void *) pl);
323 } /* for (pl) */
325 return (0);
326 } /* int exec_read */
328 static int exec_shutdown (void)
329 {
330 program_list_t *pl;
331 program_list_t *next;
333 pl = pl_head;
334 while (pl != NULL)
335 {
336 next = pl->next;
338 if (pl->pid > 0)
339 {
340 kill (pl->pid, SIGTERM);
341 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
342 }
344 sfree (pl->user);
345 sfree (pl);
347 pl = next;
348 } /* while (pl) */
349 pl_head = NULL;
351 return (0);
352 } /* int exec_shutdown */
354 void module_register (void)
355 {
356 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
357 plugin_register_read ("exec", exec_read);
358 plugin_register_shutdown ("exec", exec_shutdown);
359 } /* void module_register */
361 /*
362 * vim:shiftwidth=2:softtabstop=2:tabstop=8
363 */