summaryrefslogtreecommitdiffstats
path: root/src/daemon.c
diff options
context:
space:
mode:
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>2016-04-27 11:14:13 +0300
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>2016-04-27 11:14:13 +0300
commit577103e7d293397bfe7de7c23d792c6f340a10b5 (patch)
treeca9235c55bfac995bdb412153c60621f72861678 /src/daemon.c
parentcd12e1d22727553a66619971ec368589b6b95573 (diff)
netdata now sets the supplementary groups of its user #115
Diffstat (limited to 'src/daemon.c')
-rw-r--r--src/daemon.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/daemon.c b/src/daemon.c
index 9dcf32f0ba..6b671bee1f 100644
--- a/src/daemon.c
+++ b/src/daemon.c
@@ -10,6 +10,7 @@
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
+#include <grp.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/stat.h>
@@ -85,6 +86,21 @@ int become_user(const char *username)
uid_t uid = pw->pw_uid;
gid_t gid = pw->pw_gid;
+ int ngroups = sysconf(_SC_NGROUPS_MAX);
+ gid_t *supplementary_groups = NULL;
+ if(ngroups) {
+ supplementary_groups = malloc(sizeof(gid_t) * ngroups);
+ if(supplementary_groups) {
+ if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
+ error("Cannot get supplementary groups of user '%s'.", username);
+ free(supplementary_groups);
+ supplementary_groups = NULL;
+ ngroups = 0;
+ }
+ }
+ else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
+ }
+
if(pidfile[0] && getuid() != uid) {
// we are dropping privileges
if(chown(pidfile, uid, gid) != 0)
@@ -102,6 +118,15 @@ int become_user(const char *username)
pidfd = -1;
}
+ if(supplementary_groups && ngroups) {
+ if(setgroups(ngroups, supplementary_groups) == -1)
+ error("Cannot set supplementary groups for user '%s'", username);
+
+ free(supplementary_groups);
+ supplementary_groups = NULL;
+ ngroups = 0;
+ }
+
if(setresgid(gid, gid, gid) != 0) {
error("Cannot switch to user's %s group (gid: %d).", username, gid);
return -1;