exec plugin: Set the real group, effective group, and real and effective user.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Mon, 3 Sep 2007 13:59:08 +0000 (15:59 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Mon, 3 Sep 2007 13:59:08 +0000 (15:59 +0200)
In that order. The manpage and the config template have been updated.

src/collectd-exec.pod
src/collectd.conf.in
src/collectd.conf.pod
src/exec.c

index eaa4f8b..27d0eed 100644 (file)
@@ -8,8 +8,8 @@ collectd-exec - Documentation of collectd's C<exec plugin>
   LoadPlugin exec
   # ...
   <Plugin exec>
-    Exec myuser myprog
-    Exec otheruser /path/to/another/binary
+    Exec "myuser:mygroup" "myprog"
+    Exec "otheruser" "/path/to/another/binary"
   </Plugin>
 
 =head1 DESCRIPTION
index 313fd91..af2db91 100644 (file)
@@ -93,7 +93,7 @@
 #</Plugin>
 
 #<Plugin exec>
-#      Exec user "/path/to/exec"
+#      Exec "user:group" "/path/to/exec"
 #</Plugin>
 
 #<Plugin hddtemp>
index 3de2f24..29e26af 100644 (file)
@@ -249,9 +249,15 @@ output that is expected from it.
 =item B<Exec> I<User>[:[I<Group>]] I<Executable>
 
 Execute the executable I<Executable> as user I<User>. If the user name is
-followed by a colon and a group name, the program is executed as the specified
-group. If only the colon follows the user name the group defaults to the
-user's login group.
+followed by a colon and a group name, the effective group is set to that group.
+The real group and saved-set group will be set to the default group of that
+user. If no group is given the effective group ID will be the same as the real
+group ID.
+
+Please note that in order to change the user and/or group the daemon needs
+superuser privileges. If the daemon is run as an unprivileged user you must
+specify the same user/group here. If the daemon is run with superuser
+privileges, you must supply a non-root user here.
 
 =back
 
index e899a1c..d9f2d8c 100644 (file)
@@ -118,6 +118,7 @@ static void exec_child (program_list_t *pl)
   int status;
   int uid;
   int gid;
+  int egid;
   char *arg0;
 
   struct passwd *sp_ptr;
@@ -140,12 +141,16 @@ 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) {
@@ -165,27 +170,38 @@ static void exec_child (program_list_t *pl)
        exit (-1);
       }
 
-      gid = gr.gr_gid;
+      egid = gr.gr_gid;
     }
     else
     {
-      gid = sp.pw_gid;
+      egid = gid;
     }
+  } /* if (pl->group == NULL) */
+
+  status = setgid (gid);
+  if (status != 0)
+  {
+    ERROR ("exec plugin: setgid (%i) failed: %s",
+       gid, sstrerror (errno, errbuf, sizeof (errbuf)));
+    exit (-1);
+  }
 
-    status = setgid (gid);
-    if (0 != status)
+  if (egid != -1)
+  {
+    status = setegid (egid);
+    if (status != 0)
     {
-      ERROR ("exec plugin: setgid failed: %s",
-         sstrerror (errno, errbuf, sizeof (errbuf)));
+      ERROR ("exec plugin: setegid (%i) failed: %s",
+         egid, sstrerror (errno, errbuf, sizeof (errbuf)));
       exit (-1);
     }
-  } /* if (pl->group == NULL) */
+  }
 
   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);
   }