From: Sebastian Harl Date: Thu, 30 Nov 2006 11:02:10 +0000 (+0100) Subject: email plugin: Change owner and mode of the UNIX socket if possible. X-Git-Tag: collectd-3.11.0~39^3~1 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=8389947b881b59af994a9b6c8af706446edb25f2;p=collectd.git email plugin: Change owner and mode of the UNIX socket if possible. Set the owner and mode to COLLECTD_GRP_NAME (defaulting to "collectd") and 0770 respectively. A couple of different daemons running as different users might want to connect to it. Signed-off-by: Sebastian Harl --- diff --git a/configure.in b/configure.in index ca10e217..823845a1 100644 --- a/configure.in +++ b/configure.in @@ -277,6 +277,7 @@ AC_CHECK_HEADERS(linux/un.h, [], [], #endif ]) AC_CHECK_HEADERS(sys/un.h) +AC_CHECK_HEADERS(grp.h) # For debugging interface (variable number of arguments) AC_CHECK_HEADERS(stdarg.h) diff --git a/src/collectd.h b/src/collectd.h index 4d75db48..d8f74f95 100644 --- a/src/collectd.h +++ b/src/collectd.h @@ -200,6 +200,10 @@ #define MODE_LOCAL 0x04 #define MODE_LOG 0x08 +#ifndef COLLECTD_GRP_NAME +# define COLLECTD_GRP_NAME "collectd" +#endif + #ifndef COLLECTD_STEP # define COLLECTD_STEP "10" #endif diff --git a/src/email.c b/src/email.c index 5f612cd7..27370aee 100644 --- a/src/email.c +++ b/src/email.c @@ -55,6 +55,10 @@ # include #endif /* HAVE_LINUX_UN_H | HAVE_SYS_UN_H */ +#if HAVE_GRP_H +# include +#endif /* HAVE_GRP_H */ + #define MODULE_NAME "email" /* 256 bytes ought to be enough for anybody ;-) */ @@ -484,6 +488,29 @@ static void *open_connection (void *arg) pthread_exit ((void *)1); } + if ((uid_t)0 == geteuid ()) { + struct group *grp; + + errno = 0; + if (NULL != (grp = getgrnam (COLLECTD_GRP_NAME))) { + errno = 0; + if (0 != chown (SOCK_PATH, (uid_t)-1, grp->gr_gid)) { + syslog (LOG_WARNING, "chown() failed: %s", strerror (errno)); + } + } + else { + syslog (LOG_WARNING, "getgrnam() failed: %s", strerror (errno)); + } + } + else { + syslog (LOG_WARNING, "not running as root"); + } + + errno = 0; + if (0 != chmod (SOCK_PATH, S_IRWXU | S_IRWXG)) { + syslog (LOG_WARNING, "chmod() failed: %s", strerror (errno)); + } + { /* initialize queue of available threads */ int i = 0;