File 0001-Support-specifying-file-owner-group-when-creating-RR.patch of Package collectd

From 2f1026eec70f2b33257577e735c7e8a25bd5e85a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Kundr=C3=A1t?= <jan.kundrat@cesnet.cz>
Date: Sat, 8 Feb 2014 05:34:06 +0100
Subject: [PATCH] Support specifying file owner/group when creating RRD files
 with rrdcached

Nope, this doesn't handle the created directories. There's no sane way to do
this without modifying rrd.
---
 src/rrdcached.c       | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/utils_rrdcreate.c | 12 ++++++++++
 src/utils_rrdcreate.h |  2 ++
 3 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/src/rrdcached.c b/src/rrdcached.c
index e77be2d..e0b9601 100644
--- a/src/rrdcached.c
+++ b/src/rrdcached.c
@@ -24,6 +24,9 @@
 #include "common.h"
 #include "utils_rrdcreate.h"
 
+#include <sys/types.h>
+#include <grp.h>
+
 #undef HAVE_CONFIG_H
 #include <rrd.h>
 #include <rrd_client.h>
@@ -33,6 +36,7 @@
  */
 static char *datadir = NULL;
 static char *daemon_address = NULL;
+static char *file_user_group = NULL;
 static _Bool config_create_files = 1;
 static _Bool config_collect_stats = 1;
 static rrdcreate_config_t rrdcreate_config =
@@ -48,7 +52,9 @@ static rrdcreate_config_t rrdcreate_config =
 	/* consolidation_functions = */ NULL,
 	/* consolidation_functions_num = */ 0,
 
-	/* async = */ 0
+	/* async = */ 0,
+	/* file_uid = */ -1,
+	/* file_gid = */ -1
 };
 
 /*
@@ -243,6 +249,60 @@ static int rc_config (oconfig_item_t *ci)
       status = cf_util_get_boolean (child, &config_create_files);
     else if (strcasecmp ("CreateFilesAsync", key) == 0)
       status = cf_util_get_boolean (child, &rrdcreate_config.async);
+	else if (strcasecmp ("CreateFilesUser", key) == 0)
+	{
+	  status = cf_util_get_string (child, &file_user_group);
+	  if (status == 0)
+	  {
+        struct passwd *sp_ptr;
+        struct passwd sp;
+	    char nambuf[2048];
+
+		sp_ptr = NULL;
+		status = getpwnam_r (file_user_group, &sp, nambuf, sizeof (nambuf), &sp_ptr);
+		if (status != 0)
+		{
+		  char errbuf[1024];
+		  ERROR ("rrdcached plugin: Failed to get user information for user ``%s'': %s",
+			  file_user_group, sstrerror( errno, errbuf, sizeof (errbuf)));
+		}
+		else if (sp_ptr == NULL)
+		{
+		  ERROR ("rrdcached plugin: No such user: `%s'", file_user_group);
+		}
+		else
+		{
+		  rrdcreate_config.file_uid = sp.pw_uid;
+		}
+	  }
+	}
+	else if (strcasecmp ("CreateFilesGroup", key) == 0)
+	{
+	  status = cf_util_get_string (child, &file_user_group);
+	  if (status == 0)
+	  {
+        struct group *gr_ptr;
+        struct group gr;
+	    char nambuf[2048];
+
+		gr_ptr = NULL;
+		status = getgrnam_r (file_user_group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
+		if (status != 0)
+		{
+		  char errbuf[1024];
+		  ERROR ("rrdcached plugin: Failed to get group information for group ``%s'': %s",
+			  file_user_group, sstrerror( errno, errbuf, sizeof (errbuf)));
+		}
+		else if (gr_ptr == NULL)
+		{
+		  ERROR ("rrdcached plugin: No such group: `%s'", file_user_group);
+		}
+		else
+		{
+		  rrdcreate_config.file_gid = gr.gr_gid;
+		}
+	  }
+	}
     else if (strcasecmp ("CollectStatistics", key) == 0)
       status = cf_util_get_boolean (child, &config_collect_stats);
     else if (strcasecmp ("StepSize", key) == 0)
diff --git a/src/utils_rrdcreate.c b/src/utils_rrdcreate.c
index a34e0da..e5baecb 100644
--- a/src/utils_rrdcreate.c
+++ b/src/utils_rrdcreate.c
@@ -716,6 +716,18 @@ int cu_rrd_create_file (const char *filename, /* {{{ */
       DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
           filename);
     }
+
+    if (cfg->file_uid != -1 || cfg->file_gid != -1)
+    {
+      status = chown(filename, cfg->file_uid, cfg->file_gid);
+      if (status != 0)
+      {
+        char errbuf[1024];
+        WARNING ("cu_rrd_create_file: cannot chown (%s) to %i:%i: %s.",
+            filename, cfg->file_uid, cfg->file_gid,
+            sstrerror( errno, errbuf, sizeof (errbuf)));
+      }
+    }
   }
 
   free (argv);
diff --git a/src/utils_rrdcreate.h b/src/utils_rrdcreate.h
index fdfd6ec..ebe28f6 100644
--- a/src/utils_rrdcreate.h
+++ b/src/utils_rrdcreate.h
@@ -40,6 +40,8 @@ struct rrdcreate_config_s
   size_t consolidation_functions_num;
 
   _Bool async;
+  int file_uid;
+  int file_gid;
 };
 typedef struct rrdcreate_config_s rrdcreate_config_t;
 
-- 
1.8.1.5