exec plugin: Use `setgroups' to set the list of supplementary group IDs.
[collectd.git] / src / exec.c
index 5bddfc5..b8b538b 100644 (file)
 #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>
@@ -37,6 +39,7 @@ typedef struct program_list_s program_list_t;
 struct program_list_s
 {
   char           *user;
+  char           *group;
   char           *exec;
   int             pid;
   program_list_t *next;
@@ -45,26 +48,6 @@ struct program_list_s
 /*
  * 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"
@@ -115,6 +98,12 @@ static int exec_config (const char *key, const char *value)
 
     pl->next = pl_head;
     pl_head = pl;
+
+    pl->group = strchr (pl->user, ':');
+    if (NULL != pl->group) {
+      *pl->group = '\0';
+      pl->group++;
+    }
   }
   else
   {
@@ -124,59 +113,21 @@ static int exec_config (const char *key, const char *value)
   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",
@@ -190,17 +141,86 @@ static void exec_child (program_list_t *pl)
   }
 
   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)
+    {
+      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);
   }
 
@@ -262,6 +282,18 @@ static int fork_child (program_list_t *pl)
   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;
@@ -282,6 +314,7 @@ static void *exec_read_one (void *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);
   }
@@ -289,46 +322,17 @@ static void *exec_read_one (void *arg)
   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;
-
-    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;
-    }
 
-    value = strchr (type_instance, ',');
-    if (value == NULL)
-      continue;
-    *value = '\0';
-    value++;
+    /* Remove newline from end. */
+    while ((len > 0) && ((buffer[len - 1] == '\n')
+         || (buffer[len - 1] == '\r')))
+      buffer[--len] = '\0';
 
-    DEBUG ("value = %s", value);
+    DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
 
-    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);
@@ -384,20 +388,11 @@ static int exec_shutdown (void)
   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 */
 
 /*