]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
config: fix the load order for the early config access
authorJaroslav Kysela <perex@perex.cz>
Tue, 17 Mar 2015 23:06:41 +0000 (00:06 +0100)
committerJaroslav Kysela <perex@perex.cz>
Tue, 17 Mar 2015 23:06:41 +0000 (00:06 +0100)
src/config.c
src/config.h
src/dvr/dvr_rec.c
src/main.c
src/settings.c
src/timeshift/timeshift_filemgr.c
src/tvheadend.h
src/utils.c

index abe5ba6aa1c4bad604d0ff5545d7388a7f871d6d..b8f0c4f2de3d3ec5d0f55b651721a2437a71287a 100644 (file)
@@ -1200,7 +1200,7 @@ dobackup(const char *oldver)
   }
 
   snprintf(outfile, sizeof(outfile), "%s/backup", root);
-  if (makedirs(outfile, 0700))
+  if (makedirs(outfile, 0700, -1, -1))
     goto fatal;
   if (chdir(root)) {
     tvherror("config", "unable to find directory '%s'", root);
@@ -1363,7 +1363,7 @@ config_check ( void )
 static int config_newcfg = 0;
 
 void
-config_boot ( const char *path )
+config_boot ( const char *path, gid_t gid, uid_t uid )
 {
   struct stat st;
   char buf[1024];
@@ -1378,7 +1378,7 @@ config_boot ( const char *path )
   /* Ensure directory exists */
   if (stat(path, &st)) {
     config_newcfg = 1;
-    if (makedirs(path, 0700)) {
+    if (makedirs(path, 0700, gid, uid)) {
       tvhwarn("START", "failed to create settings directory %s,"
                        " settings will not be saved", path);
       return;
@@ -1402,6 +1402,8 @@ config_boot ( const char *path )
   if ((config_lock_fd = file_lock(config_lock, 3)) < 0)
     exit(78); /* config error */
 
+  chown(config_lock, uid, gid);
+
   /* Load global settings */
   config = hts_settings_load("config");
   if (!config) {
@@ -1413,6 +1415,16 @@ config_boot ( const char *path )
 void
 config_init ( int backup )
 {
+  const char *path = hts_settings_get_root();
+
+  if (access(path, R_OK | W_OK)) {
+    tvhwarn("START", "configuration path %s is not r/w"
+                     " for UID:%d GID:%d [e=%s],"
+                     " settings will not be saved",
+            path, getuid(), getgid(), strerror(errno));
+    return;
+  }
+
   /* Store version number */
   if (config_newcfg) {
     htsmsg_set_u32(config, "version", ARRAY_SIZE(config_migrate_table));
index 659ae777c5f18b23c94a516a584753acd2c2f7a7..edf940a25986f2c8d3aa3588ef2f438b1f27904e 100644 (file)
@@ -23,7 +23,7 @@
 
 #include "htsmsg.h"
 
-void        config_boot    ( const char *path );
+void        config_boot    ( const char *path, gid_t gid, uid_t uid );
 void        config_init    ( int backup );
 void        config_done    ( void );
 void        config_save    ( void );
index 741a2e8aef26abe168e2f574f68e376eb2f01771..a8377f0872d3d25e33355a389b8e976a81ce05a5 100644 (file)
@@ -257,7 +257,7 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss)
     }
   }
 
-  if (makedirs(path, cfg->dvr_muxcnf.m_directory_permissions) != 0)
+  if (makedirs(path, cfg->dvr_muxcnf.m_directory_permissions, -1, -1) != 0)
     return -1;
   
   /* Construct final name */
index 1fc808b95d1304eefeff0531e3776598b3d08bc5..021409653a40eb8d2105303f2b738ba166918696 100644 (file)
@@ -442,6 +442,8 @@ main(int argc, char **argv)
   int  log_level   = LOG_INFO;
   int  log_options = TVHLOG_OPT_MILLIS | TVHLOG_OPT_STDERR | TVHLOG_OPT_SYSLOG;
   const char *log_debug = NULL, *log_trace = NULL;
+  gid_t gid = -1;
+  uid_t uid = -1;
   char buf[512];
   FILE *pidfile = NULL;
   extern int dvb_bouquets_parse;
@@ -708,21 +710,9 @@ main(int argc, char **argv)
   signal(SIGPIPE, handle_sigpipe); // will be redundant later
   signal(SIGILL, handle_sigill);   // see handler..
 
-  uuid_init();
-  config_boot(opt_config);
-  tcp_server_preinit(opt_ipv6);
-  http_server_init(opt_bindaddr);    // bind to ports only
-  htsp_init(opt_bindaddr);          // bind to ports only
-  satip_server_init(opt_satip_rtsp); // bind to ports only
-
-  if (opt_fork)
-    pidfile = tvh_fopen(opt_pidpath, "w+");
-
   /* Set priviledges */
   if(opt_fork || opt_group || opt_user) {
     const char *homedir;
-    gid_t gid;
-    uid_t uid;
     struct group  *grp = getgrnam(opt_group ?: "video");
     struct passwd *pw  = opt_user ? getpwnam(opt_user) : NULL;
 
@@ -754,16 +744,27 @@ main(int argc, char **argv)
     } else {
       uid = 1;
     }
-    if ((getgid() != gid) && setgid(gid)) {
-      tvhlog(LOG_ALERT, "START",
-             "setgid(%d) failed, do you have permission?", gid);
-      return 1;
-    }
-    if ((getuid() != uid) && setuid(uid)) {
-      tvhlog(LOG_ALERT, "START",
-             "setuid(%d) failed, do you have permission?", uid);
-      return 1;
-    }
+  }
+
+  uuid_init();
+  config_boot(opt_config, gid, uid);
+  tcp_server_preinit(opt_ipv6);
+  http_server_init(opt_bindaddr);    // bind to ports only
+  htsp_init(opt_bindaddr);          // bind to ports only
+  satip_server_init(opt_satip_rtsp); // bind to ports only
+
+  if (opt_fork)
+    pidfile = tvh_fopen(opt_pidpath, "w+");
+
+  if (gid >= 0 && (getgid() != gid) && setgid(gid)) {
+    tvhlog(LOG_ALERT, "START",
+           "setgid(%d) failed, do you have permission?", gid);
+    return 1;
+  }
+  if (uid >= 0 && (getuid() != uid) && setuid(uid)) {
+    tvhlog(LOG_ALERT, "START",
+           "setuid(%d) failed, do you have permission?", uid);
+    return 1;
   }
 
   /* Daemonise */
index 294376c71a34ad953414b2b2c4f42a9fdd570e0e..1fa9483f47ae493ffd39f6a76fdaa050a9a33c64 100644 (file)
@@ -82,7 +82,7 @@ hts_settings_makedirs ( const char *inpath )
     }
     x--;
   }
-  return makedirs(path, 0700);
+  return makedirs(path, 0700, -1, -1);
 }
 
 /**
index 46d83d5cb365cda887e6d46d09af1343a426b4eb..04c6ea0a78f03f162fc2ec919ccb6253bf7c05db 100644 (file)
@@ -143,7 +143,7 @@ int timeshift_filemgr_makedirs ( int index, char *buf, size_t len )
   if (timeshift_filemgr_get_root(buf, len))
     return 1;
   snprintf(buf+strlen(buf), len-strlen(buf), "/%d", index);
-  return makedirs(buf, 0700);
+  return makedirs(buf, 0700, -1, -1);
 }
 
 /*
index 2a281fd92dd030844d1ce085ddd2daef800c23d3..1fb91e676e74f136690adfa3188f9cacec721f49 100644 (file)
@@ -706,7 +706,7 @@ static inline uint8_t *sbuf_peek(sbuf_t *sb, int off) { return sb->sb_data + off
 
 char *md5sum ( const char *str );
 
-int makedirs ( const char *path, int mode );
+int makedirs ( const char *path, int mode, gid_t gid, uid_t uid );
 
 int rmtree ( const char *path );
 
index 845bfa4d6193fa47e272b4fbe1bc77b160f5ebbb..6e62f02c67f27416a31e92d46096f921c03b4ba8 100644 (file)
@@ -472,7 +472,7 @@ md5sum ( const char *str )
 }
 
 int
-makedirs ( const char *inpath, int mode )
+makedirs ( const char *inpath, int mode, gid_t gid, uid_t uid )
 {
   int err, ok;
   size_t x;
@@ -491,15 +491,18 @@ makedirs ( const char *inpath, int mode )
       path[x] = 0;
       if (stat(path, &st)) {
         err = mkdir(path, mode);
-        tvhtrace("settings", "Creating directory \"%s\" with octal permissions \"%o\"", path, mode);
+        if (!err && gid >= 0 && uid >= 0)
+          err = chown(path, uid, gid);
+        tvhtrace("settings", "Creating directory \"%s\" with octal permissions "
+                             "\"%o\" gid %d uid %d", path, mode, gid, uid);
       } else {
         err   = S_ISDIR(st.st_mode) ? 0 : 1;
         errno = ENOTDIR;
       }
       if (err) {
-             tvhlog(LOG_ALERT, "settings", "Unable to create dir \"%s\": %s",
-                    path, strerror(errno));
-             return -1;
+        tvhlog(LOG_ALERT, "settings", "Unable to create dir \"%s\": %s",
+               path, strerror(errno));
+        return -1;
       }
       path[x] = '/';
     }