]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
conf: create directories before dropping root
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 10 Aug 2015 15:02:12 +0000 (17:02 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 12 Aug 2015 12:45:20 +0000 (14:45 +0200)
Create logdir and dumpdir before dropping root. Set their uid/gid to the
user chronyd will switch to. This allows chronyd to create the
directories in a directory where the user won't have write permissions
(e.g. /var/lib).

conf.c
conf.h
logging.c
logging.h
main.c
sources.c

diff --git a/conf.c b/conf.c
index 6d4ee6707a1bd8494fdd3cda26d78855a906902e..9ebb268e3cb171f2b7f891d69d53a76ffcbd21a5 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -1258,6 +1258,15 @@ parse_include(char *line)
 
 /* ================================================== */
 
+void
+CNF_CreateDirs(uid_t uid, gid_t gid)
+{
+  UTI_CreateDirAndParents(logdir, 0755, uid, gid);
+  UTI_CreateDirAndParents(dumpdir, 0755, uid, gid);
+}
+
+/* ================================================== */
+
 void
 CNF_AddInitSources(void)
 {
diff --git a/conf.h b/conf.h
index ff74ad78dc6f0295448b78b131d32fb22780f336..f8437223c9881bba71cc3104f32c027ab936854a 100644 (file)
--- a/conf.h
+++ b/conf.h
@@ -39,6 +39,8 @@ extern char *CNF_GetRtcDevice(void);
 extern void CNF_ReadFile(const char *filename);
 extern void CNF_ParseLine(const char *filename, int number, char *line);
 
+extern void CNF_CreateDirs(uid_t uid, gid_t gid);
+
 extern void CNF_AddInitSources(void);
 extern void CNF_AddSources(void);
 extern void CNF_AddBroadcasts(void);
index 8257a8b2ff6ad3fa8d7ff4596f2a35dcc148882d..0ffaa29721cf457f4a8910b0f0d0589f952f80fb 100644 (file)
--- a/logging.c
+++ b/logging.c
@@ -299,18 +299,6 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...)
 
 /* ================================================== */
 
-void
-LOG_CreateLogFileDir(void)
-{
-  const char *logdir;
-
-  logdir = CNF_GetLogDir();
-
-  UTI_CreateDirAndParents(logdir, 0755, 0, 0);
-}
-
-/* ================================================== */
-
 void
 LOG_CycleLogFiles(void)
 {
index b4b4d18f7efa0dcae1f039eff6166ded44fdc75f..5455ec3ac3b76601e01a5522b4eafa443a5dc5b1 100644 (file)
--- a/logging.h
+++ b/logging.h
@@ -142,7 +142,6 @@ extern LOG_FileID LOG_FileOpen(const char *name, const char *banner);
 FORMAT_ATTRIBUTE_PRINTF(2, 3)
 extern void LOG_FileWrite(LOG_FileID id, const char *format, ...);
 
-extern void LOG_CreateLogFileDir(void);
 extern void LOG_CycleLogFiles(void);
 
 #endif /* GOT_LOGGING_H */
diff --git a/main.c b/main.c
index bd9b0081a3785845731523f70b49778b245369c5..95e99d2a9aa39f99eb9681884323bcc515388ad3 100644 (file)
--- a/main.c
+++ b/main.c
@@ -493,12 +493,13 @@ int main
   if ((pw = getpwnam(user)) == NULL)
     LOG_FATAL(LOGF_Main, "Could not get %s uid/gid", user);
 
+  /* Create all directories before dropping root */
+  CNF_CreateDirs(pw->pw_uid, pw->pw_gid);
+
   /* Drop root privileges if the user has non-zero uid or gid */
   if (pw->pw_uid || pw->pw_gid)
     SYS_DropRoot(pw->pw_uid, pw->pw_gid);
 
-  LOG_CreateLogFileDir();
-
   REF_Initialise();
   SST_Initialise();
   NIO_Initialise(address_family);
index 56a8cca82d4212c80906ade700e5a5a702f225ca..4ddee254efb66feb93a69acebcaf3cea2cdb1bed 100644 (file)
--- a/sources.c
+++ b/sources.c
@@ -1092,23 +1092,23 @@ SRC_DumpSources(void)
   direc_len = strlen(direc);
   file_len = direc_len + 24;
   filename = MallocArray(char, file_len); /* a bit of slack */
-  if (UTI_CreateDirAndParents(direc, 0755, 0, 0)) {
-    for (i=0; i<n_sources; i++) {
-      a = (sources[i]->ref_id) >> 24;
-      b = ((sources[i]->ref_id) >> 16) & 0xff;
-      c = ((sources[i]->ref_id) >> 8) & 0xff;
-      d = ((sources[i]->ref_id)) & 0xff;
-      
-      snprintf(filename, file_len-1, "%s/%d.%d.%d.%d.dat", direc, a, b, c, d);
-      out = fopen(filename, "w");
-      if (!out) {
-        LOG(LOGS_WARN, LOGF_Sources, "Could not open dump file %s", filename);
-      } else {
-        SST_SaveToFile(sources[i]->stats, out);
-        fclose(out);
-      }
+
+  for (i = 0; i < n_sources; i++) {
+    a = (sources[i]->ref_id) >> 24;
+    b = ((sources[i]->ref_id) >> 16) & 0xff;
+    c = ((sources[i]->ref_id) >> 8) & 0xff;
+    d = ((sources[i]->ref_id)) & 0xff;
+
+    snprintf(filename, file_len - 1, "%s/%d.%d.%d.%d.dat", direc, a, b, c, d);
+    out = fopen(filename, "w");
+    if (!out) {
+      LOG(LOGS_WARN, LOGF_Sources, "Could not open dump file %s", filename);
+    } else {
+      SST_SaveToFile(sources[i]->stats, out);
+      fclose(out);
     }
   }
+
   Free(filename);
 }