]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
main: don't require root privileges with -Q option
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 12 Jul 2017 16:38:44 +0000 (18:38 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 13 Jul 2017 14:10:54 +0000 (16:10 +0200)
If the -Q option is specified, disable by default pidfile, ntpport,
cmdport, Unix domain command socket, and clock control, in order to
allow starting chronyd without root privileges and/or when another
chronyd instance is already running.

conf.c
conf.h
doc/chronyd.adoc
main.c
test/unit/clientlog.c
test/unit/keys.c
test/unit/ntp_core.c
test/unit/ntp_sources.c
test/unit/smooth.c
test/unit/sources.c

diff --git a/conf.c b/conf.c
index c6d4bedde926cb0633b424396fbc0743606347de..ccc365ed9087b8cd21ed923b2401110be11ca30b 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -323,7 +323,7 @@ check_number_of_args(char *line, int num)
 /* ================================================== */
 
 void
-CNF_Initialise(int r)
+CNF_Initialise(int r, int client_only)
 {
   restarted = r;
 
@@ -339,11 +339,18 @@ CNF_Initialise(int r)
 
   dumpdir = Strdup("");
   logdir = Strdup("");
-  bind_cmd_path = Strdup(DEFAULT_COMMAND_SOCKET);
-  pidfile = Strdup(DEFAULT_PID_FILE);
   rtc_device = Strdup(DEFAULT_RTC_DEVICE);
   hwclock_file = Strdup(DEFAULT_HWCLOCK_FILE);
   user = Strdup(DEFAULT_USER);
+
+  if (client_only) {
+    cmd_port = ntp_port = 0;
+    bind_cmd_path = Strdup("");
+    pidfile = Strdup("");
+  } else {
+    bind_cmd_path = Strdup(DEFAULT_COMMAND_SOCKET);
+    pidfile = Strdup(DEFAULT_PID_FILE);
+  }
 }
 
 /* ================================================== */
diff --git a/conf.h b/conf.h
index 0dde15364dd55269212b46aab8b09aca43680935..b3d143ba1edf62d865f8945cf01f7cdd42a7ffd3 100644 (file)
--- a/conf.h
+++ b/conf.h
@@ -31,7 +31,7 @@
 #include "addressing.h"
 #include "reference.h"
 
-extern void CNF_Initialise(int restarted);
+extern void CNF_Initialise(int restarted, int client_only);
 extern void CNF_Finalise(void);
 
 extern char *CNF_GetRtcDevice(void);
index d1ac4d44cd2c9db35c7c71b66d65eaed60f5dff9..59e0c8fa477bc37adaaa127e58861df6f1c0930f 100644 (file)
@@ -75,8 +75,9 @@ When run in this mode, *chronyd* will set the system clock once and exit. It
 will not detach from the terminal.
 
 *-Q*::
-This option is similar to *-q*, but it will only print the offset without any
-corrections of the clock.
+This option is similar to the *-q* option, except it only prints the offset
+without making any corrections of the clock and it allows *chronyd* to be
+started without root privileges.
 
 *-r*::
 This option will try to reload and then delete files containing sample
diff --git a/main.c b/main.c
index baab7d0160b02cc3ef66228c59b3412fe53bed3f..5c6a28174e73b753d303c9d56a0cc9fcbed17819 100644 (file)
--- a/main.c
+++ b/main.c
@@ -86,6 +86,10 @@ static void
 delete_pidfile(void)
 {
   const char *pidfile = CNF_GetPidFile();
+
+  if (!pidfile[0])
+    return;
+
   /* Don't care if this fails, there's not a lot we can do */
   unlink(pidfile);
 }
@@ -274,6 +278,9 @@ write_pidfile(void)
   const char *pidfile = CNF_GetPidFile();
   FILE *out;
 
+  if (!pidfile[0])
+    return;
+
   out = fopen(pidfile, "w");
   if (!out) {
     LOG_FATAL("Could not open %s : %s", pidfile, strerror(errno));
@@ -388,7 +395,7 @@ int main
   char *user = NULL, *log_file = NULL;
   struct passwd *pw;
   int opt, debug = 0, nofork = 0, address_family = IPADDR_UNSPEC;
-  int do_init_rtc = 0, restarted = 0, timeout = 0;
+  int do_init_rtc = 0, restarted = 0, client_only = 0, timeout = 0;
   int scfilter_level = 0, lock_memory = 0, sched_priority = 0;
   int clock_control = 1, system_log = 1;
   int config_args = 0;
@@ -444,6 +451,8 @@ int main
       case 'Q':
         ref_mode = opt == 'q' ? REF_ModeUpdateOnce : REF_ModePrintOnce;
         nofork = 1;
+        client_only = 1;
+        clock_control = 0;
         system_log = 0;
         break;
       case 'r':
@@ -473,9 +482,8 @@ int main
     }
   }
 
-  if (getuid() != 0) {
+  if (getuid() && !client_only)
     LOG_FATAL("Not superuser");
-  }
 
   /* Turn into a daemon */
   if (!nofork) {
@@ -494,7 +502,7 @@ int main
 
   DNS_SetAddressFamily(address_family);
 
-  CNF_Initialise(restarted);
+  CNF_Initialise(restarted, client_only);
 
   /* Parse the config file or the remaining command line arguments */
   config_args = argc - optind;
@@ -548,8 +556,8 @@ int main
   /* 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)
+  /* Drop root privileges if the specified user has a non-zero UID */
+  if (!geteuid() && (pw->pw_uid || pw->pw_gid))
     SYS_DropRoot(pw->pw_uid, pw->pw_gid);
 
   REF_Initialise();
index 51f3c9da2cb8f2b40e5c764de2ebc2af7c8d3fe7..515ad1ad9f657d63b8d1252e14561ca3cc737442 100644 (file)
@@ -33,7 +33,7 @@ test_unit(void)
     "cmdratelimit interval 3 burst 4 leak 3",
   };
 
-  CNF_Initialise(0);
+  CNF_Initialise(0, 0);
   for (i = 0; i < sizeof conf / sizeof conf[0]; i++)
     CNF_ParseLine(NULL, i + 1, conf[i]);
 
index 264285f5904ea4ace4575b8bc34fc1253c13e01f..f8e01b270f2300230744ccac63a1cf8cd03a3e20 100644 (file)
@@ -90,7 +90,7 @@ test_unit(void)
     "keyfile "KEYFILE
   };
 
-  CNF_Initialise(0);
+  CNF_Initialise(0, 0);
   for (i = 0; i < sizeof conf / sizeof conf[0]; i++)
     CNF_ParseLine(NULL, i + 1, conf[i]);
 
index 34a0682e7feeb4b3110962a9df56cbb36b7510c9..6dacd3311054c6a024a74423da270e448e61d3d2 100644 (file)
@@ -218,7 +218,7 @@ test_unit(void)
   CPS_NTP_Source source;
   NTP_Remote_Address remote_addr;
 
-  CNF_Initialise(0);
+  CNF_Initialise(0, 0);
   for (i = 0; i < sizeof conf / sizeof conf[0]; i++)
     CNF_ParseLine(NULL, i + 1, conf[i]);
 
index f88d27a228862a424812b81f9c98994d0ab8582f..ea8f19cbb6d4893a2691abbbc089f84b2768683d 100644 (file)
@@ -34,7 +34,7 @@ test_unit(void)
 
   memset(&params, 0, sizeof (params));
 
-  CNF_Initialise(0);
+  CNF_Initialise(0, 0);
   CNF_ParseLine(NULL, 1, conf);
 
   LCL_Initialise();
index 772f07e0889e9fbf4f836ac938e9f63650da212f..998a4d1e9a2e1b1e64ba6434a43f39f2341c1d5c 100644 (file)
@@ -29,7 +29,7 @@ test_unit(void)
   double offset, freq, wander;
   char conf[] = "smoothtime 300 0.01";
 
-  CNF_Initialise(0);
+  CNF_Initialise(0, 0);
   CNF_ParseLine(NULL, 1, conf);
 
   LCL_Initialise();
index c9f33241a3337280585fe933582cc080c63956f4..4b1aa721bd91dfd0affb1b5debdc3b16b3c50ec8 100644 (file)
@@ -31,7 +31,7 @@ test_unit(void)
   double offset, delay, disp;
   struct timespec ts;
 
-  CNF_Initialise(0);
+  CNF_Initialise(0, 0);
   LCL_Initialise();
   TST_RegisterDummyDrivers();
   SCH_Initialise();