#include "collectd.h"
#include "common.h"
#include "plugin.h"
+#include "utils_cmd_putval.h"
#include <sys/types.h>
#include <pwd.h>
+#include <grp.h>
#include <signal.h>
#include <pthread.h>
struct program_list_s
{
char *user;
+ char *group;
char *exec;
int pid;
program_list_t *next;
/*
* Private variables
*/
-static data_source_t dsrc_counter[1] =
-{
- {"value", DS_TYPE_COUNTER, NAN, NAN}
-};
-
-static data_set_t ds_counter =
-{
- "counter", STATIC_ARRAY_SIZE (dsrc_counter), dsrc_counter
-};
-
-static data_source_t dsrc_gauge[1] =
-{
- {"value", DS_TYPE_GAUGE, NAN, NAN}
-};
-
-static data_set_t ds_gauge =
-{
- "gauge", STATIC_ARRAY_SIZE (dsrc_gauge), dsrc_gauge
-};
-
static const char *config_keys[] =
{
"Exec"
pl->next = pl_head;
pl_head = pl;
+
+ pl->group = strchr (pl->user, ':');
+ if (NULL != pl->group) {
+ *pl->group = '\0';
+ pl->group++;
+ }
}
else
{
return (0);
} /* int exec_config */
-static void submit_counter (const char *type_instance, counter_t value)
-{
- value_t values[1];
- value_list_t vl = VALUE_LIST_INIT;
-
- DEBUG ("type_instance = %s; value = %llu;", type_instance, value);
-
- values[0].counter = value;
-
- vl.values = values;
- vl.values_len = 1;
- vl.time = time (NULL);
- strcpy (vl.host, hostname_g);
- strcpy (vl.plugin, "exec");
- strcpy (vl.plugin_instance, "");
- strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
-
- plugin_dispatch_values ("counter", &vl);
-} /* void submit_counter */
-
-static void submit_gauge (const char *type_instance, gauge_t value)
-{
- value_t values[1];
- value_list_t vl = VALUE_LIST_INIT;
-
- DEBUG ("type_instance = %s; value = %lf;", type_instance, value);
-
- values[0].gauge = value;
-
- vl.values = values;
- vl.values_len = 1;
- vl.time = time (NULL);
- strcpy (vl.host, hostname_g);
- strcpy (vl.plugin, "exec");
- strcpy (vl.plugin_instance, "");
- strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
-
- plugin_dispatch_values ("gauge", &vl);
-} /* void submit_counter */
-
static void exec_child (program_list_t *pl)
{
int status;
int uid;
+ int gid;
+ int egid;
char *arg0;
struct passwd *sp_ptr;
struct passwd sp;
- char pwnambuf[2048];
+ char nambuf[2048];
char errbuf[1024];
sp_ptr = NULL;
- status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
+ status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
if (status != 0)
{
ERROR ("exec plugin: getpwnam_r failed: %s",
}
uid = sp.pw_uid;
+ gid = sp.pw_gid;
if (uid == 0)
{
ERROR ("exec plugin: Cowardly refusing to exec program as root.");
exit (-1);
}
+ /* The group configured in the configfile is set as effective group, because
+ * this way the forked process can (re-)gain the user's primary group. */
+ egid = -1;
+ if (NULL != pl->group)
+ {
+ if ('\0' != *pl->group) {
+ struct group *gr_ptr = NULL;
+ struct group gr;
+
+ status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
+ if (0 != status)
+ {
+ ERROR ("exec plugin: getgrnam_r failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ exit (-1);
+ }
+ if (NULL == gr_ptr)
+ {
+ ERROR ("exec plugin: No such group: `%s'", pl->group);
+ exit (-1);
+ }
+
+ egid = gr.gr_gid;
+ }
+ else
+ {
+ egid = gid;
+ }
+ } /* if (pl->group == NULL) */
+
+#if HAVE_SETGROUPS
+ if (getuid () == 0)
+ {
+ gid_t glist[2];
+ size_t glist_len;
+
+ glist[0] = gid;
+ glist_len = 1;
+
+ if ((gid != egid) && (egid != -1))
+ {
+ glist[1] = egid;
+ glist_len = 2;
+ }
+
+ setgroups (glist_len, glist);
+ }
+#endif /* HAVE_SETGROUPS */
+
+ status = setgid (gid);
+ if (status != 0)
+ {
+ ERROR ("exec plugin: setgid (%i) failed: %s",
+ gid, sstrerror (errno, errbuf, sizeof (errbuf)));
+ exit (-1);
+ }
+
+ if (egid != -1)
+ {
+ status = setegid (egid);
+ if (status != 0)
+ {
+ ERROR ("exec plugin: setegid (%i) failed: %s",
+ egid, sstrerror (errno, errbuf, sizeof (errbuf)));
+ exit (-1);
+ }
+ }
+
status = setuid (uid);
if (status != 0)
{
- ERROR ("exec plugin: setuid failed: %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
+ ERROR ("exec plugin: setuid (%i) failed: %s",
+ uid, sstrerror (errno, errbuf, sizeof (errbuf)));
exit (-1);
}
}
else if (pl->pid == 0)
{
- close (fd_pipe[0]);
+ int fd_num;
+ int fd;
+
+ /* Close all file descriptors but the pipe end we need. */
+ fd_num = getdtablesize ();
+ for (fd = 0; fd < fd_num; fd++)
+ {
+ if (fd == fd_pipe[1])
+ continue;
+ close (fd);
+ }
/* Connect the pipe to STDOUT and STDERR */
if (fd_pipe[1] != STDOUT_FILENO)
return (fd_pipe[0]);
} /* int fork_child */
+static int parse_line (char *buffer)
+{
+ char *fields[256];
+ int fields_num;
+
+ fields[0] = "PUTVAL";
+ fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1);
+
+ handle_putval (stdout, fields, fields_num + 1);
+ return (0);
+} /* int parse_line */
+
static void *exec_read_one (void *arg)
{
program_list_t *pl = (program_list_t *) arg;
ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
sstrerror (errno, errbuf, sizeof (errbuf)));
kill (pl->pid, SIGTERM);
+ pl->pid = 0;
close (fd);
pthread_exit ((void *) 1);
}
while (fgets (buffer, sizeof (buffer), fh) != NULL)
{
int len;
- char *type;
- char *type_instance;
- char *value;
-
- DEBUG ("buffer = %s", buffer);
len = strlen (buffer);
- if (len < 5)
- continue;
-
- if (buffer[0] == '#')
- continue;
- type = buffer;
+ /* Remove newline from end. */
+ while ((len > 0) && ((buffer[len - 1] == '\n')
+ || (buffer[len - 1] == '\r')))
+ buffer[--len] = '\0';
- type_instance = strchr (type, ',');
- if (type_instance == NULL)
- continue;
- *type_instance = '\0';
- type_instance++;
-
- if ((strcasecmp ("counter", type) != 0)
- && (strcasecmp ("gauge", type) != 0))
- {
- WARNING ("exec plugin: Received invalid type: %s", type);
- continue;
- }
+ DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
- value = strchr (type_instance, ',');
- if (value == NULL)
- continue;
- *value = '\0';
- value++;
-
- DEBUG ("value = %s", value);
-
- if (strcasecmp ("counter", type) == 0)
- submit_counter (type_instance, atoll (value));
- else
- submit_gauge (type_instance, atof (value));
+ parse_line (buffer);
} /* while (fgets) */
fclose (fh);
return (0);
} /* int exec_shutdown */
-void module_register (modreg_e load)
+void module_register (void)
{
- if (load & MR_DATASETS)
- {
- plugin_register_data_set (&ds_counter);
- plugin_register_data_set (&ds_gauge);
- }
-
- if (load & MR_READ)
- {
- plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
- plugin_register_read ("exec", exec_read);
- plugin_register_shutdown ("exec", exec_shutdown);
- }
+ plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
+ plugin_register_read ("exec", exec_read);
+ plugin_register_shutdown ("exec", exec_shutdown);
} /* void module_register */
/*