"Fossies" - the Fresh Open Source Software Archive

Member "chrony-3.5/conf.c" (10 May 2019, 49779 Bytes) of package /linux/misc/chrony-3.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "conf.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.3_vs_3.4.

    1 /*
    2   chronyd/chronyc - Programs for keeping computer clocks accurate.
    3 
    4  **********************************************************************
    5  * Copyright (C) Richard P. Curnow  1997-2003
    6  * Copyright (C) Miroslav Lichvar  2009-2017
    7  * 
    8  * This program is free software; you can redistribute it and/or modify
    9  * it under the terms of version 2 of the GNU General Public License as
   10  * published by the Free Software Foundation.
   11  * 
   12  * This program is distributed in the hope that it will be useful, but
   13  * WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15  * General Public License for more details.
   16  * 
   17  * You should have received a copy of the GNU General Public License along
   18  * with this program; if not, write to the Free Software Foundation, Inc.,
   19  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   20  * 
   21  **********************************************************************
   22 
   23   =======================================================================
   24 
   25   Module that reads and processes the configuration file.
   26   */
   27 
   28 #include "config.h"
   29 
   30 #include "sysincl.h"
   31 
   32 #include "array.h"
   33 #include "conf.h"
   34 #include "ntp_sources.h"
   35 #include "ntp_core.h"
   36 #include "refclock.h"
   37 #include "cmdmon.h"
   38 #include "srcparams.h"
   39 #include "logging.h"
   40 #include "nameserv.h"
   41 #include "memory.h"
   42 #include "cmdparse.h"
   43 #include "util.h"
   44 
   45 /* ================================================== */
   46 /* Forward prototypes */
   47 
   48 static int parse_string(char *line, char **result);
   49 static int parse_int(char *line, int *result);
   50 static int parse_double(char *line, double *result);
   51 static int parse_null(char *line);
   52 
   53 static void parse_allow_deny(char *line, ARR_Instance restrictions, int allow);
   54 static void parse_bindacqaddress(char *);
   55 static void parse_bindaddress(char *);
   56 static void parse_bindcmdaddress(char *);
   57 static void parse_broadcast(char *);
   58 static void parse_clientloglimit(char *);
   59 static void parse_fallbackdrift(char *);
   60 static void parse_hwtimestamp(char *);
   61 static void parse_include(char *);
   62 static void parse_initstepslew(char *);
   63 static void parse_leapsecmode(char *);
   64 static void parse_local(char *);
   65 static void parse_log(char *);
   66 static void parse_mailonchange(char *);
   67 static void parse_makestep(char *);
   68 static void parse_maxchange(char *);
   69 static void parse_ratelimit(char *line, int *enabled, int *interval,
   70                             int *burst, int *leak);
   71 static void parse_refclock(char *);
   72 static void parse_smoothtime(char *);
   73 static void parse_source(char *line, NTP_Source_Type type, int pool);
   74 static void parse_tempcomp(char *);
   75 
   76 /* ================================================== */
   77 /* Configuration variables */
   78 
   79 static int restarted = 0;
   80 static char *rtc_device;
   81 static int acquisition_port = -1;
   82 static int ntp_port = NTP_PORT;
   83 static char *keys_file = NULL;
   84 static char *drift_file = NULL;
   85 static char *rtc_file = NULL;
   86 static double max_update_skew = 1000.0;
   87 static double correction_time_ratio = 3.0;
   88 static double max_clock_error = 1.0; /* in ppm */
   89 static double max_drift = 500000.0; /* in ppm */
   90 static double max_slew_rate = 1e6 / 12.0; /* in ppm */
   91 
   92 static double max_distance = 3.0;
   93 static double max_jitter = 1.0;
   94 static double reselect_distance = 1e-4;
   95 static double stratum_weight = 1e-3;
   96 static double combine_limit = 3.0;
   97 
   98 static int cmd_port = DEFAULT_CANDM_PORT;
   99 
  100 static int raw_measurements = 0;
  101 static int do_log_measurements = 0;
  102 static int do_log_statistics = 0;
  103 static int do_log_tracking = 0;
  104 static int do_log_rtc = 0;
  105 static int do_log_refclocks = 0;
  106 static int do_log_tempcomp = 0;
  107 static int log_banner = 32;
  108 static char *logdir;
  109 static char *dumpdir;
  110 
  111 static int enable_local=0;
  112 static int local_stratum;
  113 static int local_orphan;
  114 static double local_distance;
  115 
  116 /* Threshold (in seconds) - if absolute value of initial error is less
  117    than this, slew instead of stepping */
  118 static double init_slew_threshold;
  119 /* Array of IPAddr */
  120 static ARR_Instance init_sources;
  121 
  122 static int enable_manual=0;
  123 
  124 /* Flag set if the RTC runs UTC (default is it runs local time
  125    incl. daylight saving). */
  126 static int rtc_on_utc = 0;
  127 
  128 /* Filename used to read the hwclock(8) LOCAL/UTC setting */
  129 static char *hwclock_file;
  130 
  131 /* Flag set if the RTC should be automatically synchronised by kernel */
  132 static int rtc_sync = 0;
  133 
  134 /* Limit and threshold for clock stepping */
  135 static int make_step_limit = 0;
  136 static double make_step_threshold = 0.0;
  137 
  138 /* Threshold for automatic RTC trimming */
  139 static double rtc_autotrim_threshold = 0.0;
  140 
  141 /* Minimum number of selectables sources required to update the clock */
  142 static int min_sources = 1;
  143 
  144 /* Number of updates before offset checking, number of ignored updates
  145    before exiting and the maximum allowed offset */
  146 static int max_offset_delay = -1;
  147 static int max_offset_ignore;
  148 static double max_offset;
  149 
  150 /* Maximum and minimum number of samples per source */
  151 static int max_samples = 0; /* no limit */
  152 static int min_samples = 6;
  153 
  154 /* Threshold for a time adjustment to be logged to syslog */
  155 static double log_change_threshold = 1.0;
  156 
  157 static char *mail_user_on_change = NULL;
  158 static double mail_change_threshold = 0.0;
  159 
  160 /* Flag indicating that we don't want to log clients, e.g. to save
  161    memory */
  162 static int no_client_log = 0;
  163 
  164 /* Limit memory allocated for the clients log */
  165 static unsigned long client_log_limit = 524288;
  166 
  167 /* Minimum and maximum fallback drift intervals */
  168 static int fb_drift_min = 0;
  169 static int fb_drift_max = 0;
  170 
  171 /* IP addresses for binding the NTP server sockets to.  UNSPEC family means
  172    INADDR_ANY will be used */
  173 static IPAddr bind_address4, bind_address6;
  174 
  175 /* IP addresses for binding the NTP client sockets to.  UNSPEC family means
  176    INADDR_ANY will be used */
  177 static IPAddr bind_acq_address4, bind_acq_address6;
  178 
  179 /* IP addresses for binding the command socket to.  UNSPEC family means
  180    the loopback address will be used */
  181 static IPAddr bind_cmd_address4, bind_cmd_address6;
  182 
  183 /* Path to the Unix domain command socket. */
  184 static char *bind_cmd_path;
  185 
  186 /* Path to Samba (ntp_signd) socket. */
  187 static char *ntp_signd_socket = NULL;
  188 
  189 /* Filename to use for storing pid of running chronyd, to prevent multiple
  190  * chronyds being started. */
  191 static char *pidfile;
  192 
  193 /* Rate limiting parameters */
  194 static int ntp_ratelimit_enabled = 0;
  195 static int ntp_ratelimit_interval = 3;
  196 static int ntp_ratelimit_burst = 8;
  197 static int ntp_ratelimit_leak = 2;
  198 static int cmd_ratelimit_enabled = 0;
  199 static int cmd_ratelimit_interval = -4;
  200 static int cmd_ratelimit_burst = 8;
  201 static int cmd_ratelimit_leak = 2;
  202 
  203 /* Smoothing constants */
  204 static double smooth_max_freq = 0.0; /* in ppm */
  205 static double smooth_max_wander = 0.0; /* in ppm/s */
  206 static int smooth_leap_only = 0;
  207 
  208 /* Temperature sensor, update interval and compensation coefficients */
  209 static char *tempcomp_sensor_file = NULL;
  210 static char *tempcomp_point_file = NULL;
  211 static double tempcomp_interval;
  212 static double tempcomp_T0, tempcomp_k0, tempcomp_k1, tempcomp_k2;
  213 
  214 static int sched_priority = 0;
  215 static int lock_memory = 0;
  216 
  217 /* Leap second handling mode */
  218 static REF_LeapMode leapsec_mode = REF_LeapModeSystem;
  219 
  220 /* Name of a system timezone containing leap seconds occuring at midnight */
  221 static char *leapsec_tz = NULL;
  222 
  223 /* Name of the user to which will be dropped root privileges. */
  224 static char *user;
  225 
  226 /* Array of CNF_HwTsInterface */
  227 static ARR_Instance hwts_interfaces;
  228 
  229 typedef struct {
  230   NTP_Source_Type type;
  231   int pool;
  232   CPS_NTP_Source params;
  233 } NTP_Source;
  234 
  235 /* Array of NTP_Source */
  236 static ARR_Instance ntp_sources;
  237 
  238 /* Array of RefclockParameters */
  239 static ARR_Instance refclock_sources;
  240 
  241 typedef struct _AllowDeny {
  242   IPAddr ip;
  243   int subnet_bits;
  244   int all; /* 1 to override existing more specific defns */
  245   int allow; /* 0 for deny, 1 for allow */
  246 } AllowDeny;
  247 
  248 /* Arrays of AllowDeny */
  249 static ARR_Instance ntp_restrictions;
  250 static ARR_Instance cmd_restrictions;
  251 
  252 typedef struct {
  253   IPAddr addr;
  254   unsigned short port;
  255   int interval;
  256 } NTP_Broadcast_Destination;
  257 
  258 /* Array of NTP_Broadcast_Destination */
  259 static ARR_Instance broadcasts;
  260 
  261 /* ================================================== */
  262 
  263 /* The line number in the configuration file being processed */
  264 static int line_number;
  265 static const char *processed_file;
  266 static const char *processed_command;
  267 
  268 /* ================================================== */
  269 
  270 static void
  271 command_parse_error(void)
  272 {
  273     LOG_FATAL("Could not parse %s directive at line %d%s%s",
  274         processed_command, line_number, processed_file ? " in file " : "",
  275         processed_file ? processed_file : "");
  276 }
  277 
  278 /* ================================================== */
  279 
  280 static void
  281 other_parse_error(const char *message)
  282 {
  283     LOG_FATAL("%s at line %d%s%s",
  284         message, line_number, processed_file ? " in file " : "",
  285         processed_file ? processed_file : "");
  286 }
  287 
  288 /* ================================================== */
  289 
  290 static int
  291 get_number_of_args(char *line)
  292 {
  293   int num = 0;
  294 
  295   /* The line is normalized, between arguments is just one space */
  296   if (*line == ' ')
  297     line++;
  298   if (*line)
  299     num++;
  300   for (; *line; line++) {
  301     if (*line == ' ')
  302       num++;
  303   }
  304 
  305   return num;
  306 }
  307 
  308 /* ================================================== */
  309 
  310 static void
  311 check_number_of_args(char *line, int num)
  312 {
  313   num -= get_number_of_args(line);
  314 
  315   if (num) {
  316     LOG_FATAL("%s arguments for %s directive at line %d%s%s",
  317         num > 0 ? "Missing" : "Too many",
  318         processed_command, line_number, processed_file ? " in file " : "",
  319         processed_file ? processed_file : "");
  320   }
  321 }
  322 
  323 /* ================================================== */
  324 
  325 void
  326 CNF_Initialise(int r, int client_only)
  327 {
  328   restarted = r;
  329 
  330   hwts_interfaces = ARR_CreateInstance(sizeof (CNF_HwTsInterface));
  331 
  332   init_sources = ARR_CreateInstance(sizeof (IPAddr));
  333   ntp_sources = ARR_CreateInstance(sizeof (NTP_Source));
  334   refclock_sources = ARR_CreateInstance(sizeof (RefclockParameters));
  335   broadcasts = ARR_CreateInstance(sizeof (NTP_Broadcast_Destination));
  336 
  337   ntp_restrictions = ARR_CreateInstance(sizeof (AllowDeny));
  338   cmd_restrictions = ARR_CreateInstance(sizeof (AllowDeny));
  339 
  340   dumpdir = Strdup("");
  341   logdir = Strdup("");
  342   rtc_device = Strdup(DEFAULT_RTC_DEVICE);
  343   hwclock_file = Strdup(DEFAULT_HWCLOCK_FILE);
  344   user = Strdup(DEFAULT_USER);
  345 
  346   if (client_only) {
  347     cmd_port = ntp_port = 0;
  348     bind_cmd_path = Strdup("");
  349     pidfile = Strdup("");
  350   } else {
  351     bind_cmd_path = Strdup(DEFAULT_COMMAND_SOCKET);
  352     pidfile = Strdup(DEFAULT_PID_FILE);
  353   }
  354 }
  355 
  356 /* ================================================== */
  357 
  358 void
  359 CNF_Finalise(void)
  360 {
  361   unsigned int i;
  362 
  363   for (i = 0; i < ARR_GetSize(hwts_interfaces); i++)
  364     Free(((CNF_HwTsInterface *)ARR_GetElement(hwts_interfaces, i))->name);
  365   ARR_DestroyInstance(hwts_interfaces);
  366 
  367   for (i = 0; i < ARR_GetSize(ntp_sources); i++)
  368     Free(((NTP_Source *)ARR_GetElement(ntp_sources, i))->params.name);
  369 
  370   ARR_DestroyInstance(init_sources);
  371   ARR_DestroyInstance(ntp_sources);
  372   ARR_DestroyInstance(refclock_sources);
  373   ARR_DestroyInstance(broadcasts);
  374 
  375   ARR_DestroyInstance(ntp_restrictions);
  376   ARR_DestroyInstance(cmd_restrictions);
  377 
  378   Free(drift_file);
  379   Free(dumpdir);
  380   Free(hwclock_file);
  381   Free(keys_file);
  382   Free(leapsec_tz);
  383   Free(logdir);
  384   Free(bind_cmd_path);
  385   Free(ntp_signd_socket);
  386   Free(pidfile);
  387   Free(rtc_device);
  388   Free(rtc_file);
  389   Free(user);
  390   Free(mail_user_on_change);
  391   Free(tempcomp_sensor_file);
  392   Free(tempcomp_point_file);
  393 }
  394 
  395 /* ================================================== */
  396 
  397 /* Read the configuration file */
  398 void
  399 CNF_ReadFile(const char *filename)
  400 {
  401   FILE *in;
  402   char line[2048];
  403   int i;
  404 
  405   in = fopen(filename, "r");
  406   if (!in) {
  407     LOG_FATAL("Could not open configuration file %s : %s",
  408               filename, strerror(errno));
  409     return;
  410   }
  411 
  412   DEBUG_LOG("Reading %s", filename);
  413 
  414   for (i = 1; fgets(line, sizeof(line), in); i++) {
  415     CNF_ParseLine(filename, i, line);
  416   }
  417 
  418   fclose(in);
  419 }
  420 
  421 /* ================================================== */
  422 
  423 /* Parse one configuration line */
  424 void
  425 CNF_ParseLine(const char *filename, int number, char *line)
  426 {
  427   char *p, *command;
  428 
  429   /* Set global variables used in error messages */
  430   processed_file = filename;
  431   line_number = number;
  432 
  433   /* Remove extra white-space and comments */
  434   CPS_NormalizeLine(line);
  435 
  436   /* Skip blank lines */
  437   if (!*line)
  438     return;
  439 
  440   /* We have a real line, now try to match commands */
  441   processed_command = command = line;
  442   p = CPS_SplitWord(line);
  443 
  444   if (!strcasecmp(command, "acquisitionport")) {
  445     parse_int(p, &acquisition_port);
  446   } else if (!strcasecmp(command, "allow")) {
  447     parse_allow_deny(p, ntp_restrictions, 1);
  448   } else if (!strcasecmp(command, "bindacqaddress")) {
  449     parse_bindacqaddress(p);
  450   } else if (!strcasecmp(command, "bindaddress")) {
  451     parse_bindaddress(p);
  452   } else if (!strcasecmp(command, "bindcmdaddress")) {
  453     parse_bindcmdaddress(p);
  454   } else if (!strcasecmp(command, "broadcast")) {
  455     parse_broadcast(p);
  456   } else if (!strcasecmp(command, "clientloglimit")) {
  457     parse_clientloglimit(p);
  458   } else if (!strcasecmp(command, "cmdallow")) {
  459     parse_allow_deny(p, cmd_restrictions, 1);
  460   } else if (!strcasecmp(command, "cmddeny")) {
  461     parse_allow_deny(p, cmd_restrictions, 0);
  462   } else if (!strcasecmp(command, "cmdport")) {
  463     parse_int(p, &cmd_port);
  464   } else if (!strcasecmp(command, "cmdratelimit")) {
  465     parse_ratelimit(p, &cmd_ratelimit_enabled, &cmd_ratelimit_interval,
  466                     &cmd_ratelimit_burst, &cmd_ratelimit_leak);
  467   } else if (!strcasecmp(command, "combinelimit")) {
  468     parse_double(p, &combine_limit);
  469   } else if (!strcasecmp(command, "corrtimeratio")) {
  470     parse_double(p, &correction_time_ratio);
  471   } else if (!strcasecmp(command, "deny")) {
  472     parse_allow_deny(p, ntp_restrictions, 0);
  473   } else if (!strcasecmp(command, "driftfile")) {
  474     parse_string(p, &drift_file);
  475   } else if (!strcasecmp(command, "dumpdir")) {
  476     parse_string(p, &dumpdir);
  477   } else if (!strcasecmp(command, "dumponexit")) {
  478     /* Silently ignored */
  479   } else if (!strcasecmp(command, "fallbackdrift")) {
  480     parse_fallbackdrift(p);
  481   } else if (!strcasecmp(command, "hwclockfile")) {
  482     parse_string(p, &hwclock_file);
  483   } else if (!strcasecmp(command, "hwtimestamp")) {
  484     parse_hwtimestamp(p);
  485   } else if (!strcasecmp(command, "include")) {
  486     parse_include(p);
  487   } else if (!strcasecmp(command, "initstepslew")) {
  488     parse_initstepslew(p);
  489   } else if (!strcasecmp(command, "keyfile")) {
  490     parse_string(p, &keys_file);
  491   } else if (!strcasecmp(command, "leapsecmode")) {
  492     parse_leapsecmode(p);
  493   } else if (!strcasecmp(command, "leapsectz")) {
  494     parse_string(p, &leapsec_tz);
  495   } else if (!strcasecmp(command, "local")) {
  496     parse_local(p);
  497   } else if (!strcasecmp(command, "lock_all")) {
  498     lock_memory = parse_null(p);
  499   } else if (!strcasecmp(command, "log")) {
  500     parse_log(p);
  501   } else if (!strcasecmp(command, "logbanner")) {
  502     parse_int(p, &log_banner);
  503   } else if (!strcasecmp(command, "logchange")) {
  504     parse_double(p, &log_change_threshold);
  505   } else if (!strcasecmp(command, "logdir")) {
  506     parse_string(p, &logdir);
  507   } else if (!strcasecmp(command, "mailonchange")) {
  508     parse_mailonchange(p);
  509   } else if (!strcasecmp(command, "makestep")) {
  510     parse_makestep(p);
  511   } else if (!strcasecmp(command, "manual")) {
  512     enable_manual = parse_null(p);
  513   } else if (!strcasecmp(command, "maxchange")) {
  514     parse_maxchange(p);
  515   } else if (!strcasecmp(command, "maxclockerror")) {
  516     parse_double(p, &max_clock_error);
  517   } else if (!strcasecmp(command, "maxdistance")) {
  518     parse_double(p, &max_distance);
  519   } else if (!strcasecmp(command, "maxdrift")) {
  520     parse_double(p, &max_drift);
  521   } else if (!strcasecmp(command, "maxjitter")) {
  522     parse_double(p, &max_jitter);
  523   } else if (!strcasecmp(command, "maxsamples")) {
  524     parse_int(p, &max_samples);
  525   } else if (!strcasecmp(command, "maxslewrate")) {
  526     parse_double(p, &max_slew_rate);
  527   } else if (!strcasecmp(command, "maxupdateskew")) {
  528     parse_double(p, &max_update_skew);
  529   } else if (!strcasecmp(command, "minsamples")) {
  530     parse_int(p, &min_samples);
  531   } else if (!strcasecmp(command, "minsources")) {
  532     parse_int(p, &min_sources);
  533   } else if (!strcasecmp(command, "noclientlog")) {
  534     no_client_log = parse_null(p);
  535   } else if (!strcasecmp(command, "ntpsigndsocket")) {
  536     parse_string(p, &ntp_signd_socket);
  537   } else if (!strcasecmp(command, "peer")) {
  538     parse_source(p, NTP_PEER, 0);
  539   } else if (!strcasecmp(command, "pidfile")) {
  540     parse_string(p, &pidfile);
  541   } else if (!strcasecmp(command, "pool")) {
  542     parse_source(p, NTP_SERVER, 1);
  543   } else if (!strcasecmp(command, "port")) {
  544     parse_int(p, &ntp_port);
  545   } else if (!strcasecmp(command, "ratelimit")) {
  546     parse_ratelimit(p, &ntp_ratelimit_enabled, &ntp_ratelimit_interval,
  547                     &ntp_ratelimit_burst, &ntp_ratelimit_leak);
  548   } else if (!strcasecmp(command, "refclock")) {
  549     parse_refclock(p);
  550   } else if (!strcasecmp(command, "reselectdist")) {
  551     parse_double(p, &reselect_distance);
  552   } else if (!strcasecmp(command, "rtcautotrim")) {
  553     parse_double(p, &rtc_autotrim_threshold);
  554   } else if (!strcasecmp(command, "rtcdevice")) {
  555     parse_string(p, &rtc_device);
  556   } else if (!strcasecmp(command, "rtcfile")) {
  557     parse_string(p, &rtc_file);
  558   } else if (!strcasecmp(command, "rtconutc")) {
  559     rtc_on_utc = parse_null(p);
  560   } else if (!strcasecmp(command, "rtcsync")) {
  561     rtc_sync = parse_null(p);
  562   } else if (!strcasecmp(command, "sched_priority")) {
  563     parse_int(p, &sched_priority);
  564   } else if (!strcasecmp(command, "server")) {
  565     parse_source(p, NTP_SERVER, 0);
  566   } else if (!strcasecmp(command, "smoothtime")) {
  567     parse_smoothtime(p);
  568   } else if (!strcasecmp(command, "stratumweight")) {
  569     parse_double(p, &stratum_weight);
  570   } else if (!strcasecmp(command, "tempcomp")) {
  571     parse_tempcomp(p);
  572   } else if (!strcasecmp(command, "user")) {
  573     parse_string(p, &user);
  574   } else if (!strcasecmp(command, "commandkey") ||
  575              !strcasecmp(command, "generatecommandkey") ||
  576              !strcasecmp(command, "linux_freq_scale") ||
  577              !strcasecmp(command, "linux_hz")) {
  578     LOG(LOGS_WARN, "%s directive is no longer supported", command);
  579   } else {
  580     other_parse_error("Invalid command");
  581   }
  582 }
  583 
  584 /* ================================================== */
  585 
  586 static int
  587 parse_string(char *line, char **result)
  588 {
  589   check_number_of_args(line, 1);
  590   Free(*result);
  591   *result = Strdup(line);
  592   return 1;
  593 }
  594 
  595 /* ================================================== */
  596 
  597 static int
  598 parse_int(char *line, int *result)
  599 {
  600   check_number_of_args(line, 1);
  601   if (sscanf(line, "%d", result) != 1) {
  602     command_parse_error();
  603     return 0;
  604   }
  605   return 1;
  606 }
  607 
  608 /* ================================================== */
  609 
  610 static int
  611 parse_double(char *line, double *result)
  612 {
  613   check_number_of_args(line, 1);
  614   if (sscanf(line, "%lf", result) != 1) {
  615     command_parse_error();
  616     return 0;
  617   }
  618   return 1;
  619 }
  620 
  621 /* ================================================== */
  622 
  623 static int
  624 parse_null(char *line)
  625 {
  626   check_number_of_args(line, 0);
  627   return 1;
  628 }
  629 
  630 /* ================================================== */
  631 
  632 static void
  633 parse_source(char *line, NTP_Source_Type type, int pool)
  634 {
  635   NTP_Source source;
  636 
  637   source.type = type;
  638   source.pool = pool;
  639 
  640   if (!CPS_ParseNTPSourceAdd(line, &source.params)) {
  641     command_parse_error();
  642     return;
  643   }
  644 
  645   source.params.name = Strdup(source.params.name);
  646   ARR_AppendElement(ntp_sources, &source);
  647 }
  648 
  649 /* ================================================== */
  650 
  651 static void
  652 parse_ratelimit(char *line, int *enabled, int *interval, int *burst, int *leak)
  653 {
  654   int n, val;
  655   char *opt;
  656 
  657   *enabled = 1;
  658 
  659   while (*line) {
  660     opt = line;
  661     line = CPS_SplitWord(line);
  662     if (sscanf(line, "%d%n", &val, &n) != 1) {
  663       command_parse_error();
  664       return;
  665     }
  666     line += n;
  667     if (!strcasecmp(opt, "interval"))
  668       *interval = val;
  669     else if (!strcasecmp(opt, "burst"))
  670       *burst = val;
  671     else if (!strcasecmp(opt, "leak"))
  672       *leak = val;
  673     else
  674       command_parse_error();
  675   }
  676 }
  677 
  678 /* ================================================== */
  679 
  680 static void
  681 parse_refclock(char *line)
  682 {
  683   int n, poll, dpoll, filter_length, pps_rate, min_samples, max_samples, sel_options;
  684   int max_lock_age, pps_forced, stratum, tai;
  685   uint32_t ref_id, lock_ref_id;
  686   double offset, delay, precision, max_dispersion, pulse_width;
  687   char *p, *cmd, *name, *param;
  688   unsigned char ref[5];
  689   RefclockParameters *refclock;
  690 
  691   poll = 4;
  692   dpoll = 0;
  693   filter_length = 64;
  694   pps_forced = 0;
  695   pps_rate = 0;
  696   min_samples = SRC_DEFAULT_MINSAMPLES;
  697   max_samples = SRC_DEFAULT_MAXSAMPLES;
  698   sel_options = 0;
  699   offset = 0.0;
  700   delay = 1e-9;
  701   precision = 0.0;
  702   max_dispersion = 0.0;
  703   pulse_width = 0.0;
  704   ref_id = 0;
  705   max_lock_age = 2;
  706   lock_ref_id = 0;
  707   stratum = 0;
  708   tai = 0;
  709 
  710   if (!*line) {
  711     command_parse_error();
  712     return;
  713   }
  714 
  715   p = line;
  716   line = CPS_SplitWord(line);
  717 
  718   if (!*line) {
  719     command_parse_error();
  720     return;
  721   }
  722 
  723   name = Strdup(p);
  724 
  725   p = line;
  726   line = CPS_SplitWord(line);
  727   param = Strdup(p);
  728 
  729   for (cmd = line; *cmd; line += n, cmd = line) {
  730     line = CPS_SplitWord(line);
  731 
  732     if (!strcasecmp(cmd, "refid")) {
  733       if (sscanf(line, "%4s%n", (char *)ref, &n) != 1)
  734         break;
  735       ref_id = (uint32_t)ref[0] << 24 | ref[1] << 16 | ref[2] << 8 | ref[3];
  736     } else if (!strcasecmp(cmd, "lock")) {
  737       if (sscanf(line, "%4s%n", (char *)ref, &n) != 1)
  738         break;
  739       lock_ref_id = (uint32_t)ref[0] << 24 | ref[1] << 16 | ref[2] << 8 | ref[3];
  740     } else if (!strcasecmp(cmd, "poll")) {
  741       if (sscanf(line, "%d%n", &poll, &n) != 1) {
  742         break;
  743       }
  744     } else if (!strcasecmp(cmd, "dpoll")) {
  745       if (sscanf(line, "%d%n", &dpoll, &n) != 1) {
  746         break;
  747       }
  748     } else if (!strcasecmp(cmd, "filter")) {
  749       if (sscanf(line, "%d%n", &filter_length, &n) != 1) {
  750         break;
  751       }
  752     } else if (!strcasecmp(cmd, "rate")) {
  753       if (sscanf(line, "%d%n", &pps_rate, &n) != 1)
  754         break;
  755     } else if (!strcasecmp(cmd, "minsamples")) {
  756       if (sscanf(line, "%d%n", &min_samples, &n) != 1)
  757         break;
  758     } else if (!strcasecmp(cmd, "maxlockage")) {
  759       if (sscanf(line, "%d%n", &max_lock_age, &n) != 1)
  760         break;
  761     } else if (!strcasecmp(cmd, "maxsamples")) {
  762       if (sscanf(line, "%d%n", &max_samples, &n) != 1)
  763         break;
  764     } else if (!strcasecmp(cmd, "offset")) {
  765       if (sscanf(line, "%lf%n", &offset, &n) != 1)
  766         break;
  767     } else if (!strcasecmp(cmd, "delay")) {
  768       if (sscanf(line, "%lf%n", &delay, &n) != 1)
  769         break;
  770     } else if (!strcasecmp(cmd, "pps")) {
  771       n = 0;
  772       pps_forced = 1;
  773     } else if (!strcasecmp(cmd, "precision")) {
  774       if (sscanf(line, "%lf%n", &precision, &n) != 1)
  775         break;
  776     } else if (!strcasecmp(cmd, "maxdispersion")) {
  777       if (sscanf(line, "%lf%n", &max_dispersion, &n) != 1)
  778         break;
  779     } else if (!strcasecmp(cmd, "stratum")) {
  780       if (sscanf(line, "%d%n", &stratum, &n) != 1 ||
  781           stratum >= NTP_MAX_STRATUM || stratum < 0)
  782         break;
  783     } else if (!strcasecmp(cmd, "tai")) {
  784       n = 0;
  785       tai = 1;
  786     } else if (!strcasecmp(cmd, "width")) {
  787       if (sscanf(line, "%lf%n", &pulse_width, &n) != 1)
  788         break;
  789     } else if (!strcasecmp(cmd, "noselect")) {
  790       n = 0;
  791       sel_options |= SRC_SELECT_NOSELECT;
  792     } else if (!strcasecmp(cmd, "prefer")) {
  793       n = 0;
  794       sel_options |= SRC_SELECT_PREFER;
  795     } else if (!strcasecmp(cmd, "trust")) {
  796       n = 0;
  797       sel_options |= SRC_SELECT_TRUST;
  798     } else if (!strcasecmp(cmd, "require")) {
  799       n = 0;
  800       sel_options |= SRC_SELECT_REQUIRE;
  801     } else {
  802       other_parse_error("Invalid refclock option");
  803       return;
  804     }
  805   }
  806 
  807   if (*cmd) {
  808     command_parse_error();
  809     return;
  810   }
  811 
  812   refclock = (RefclockParameters *)ARR_GetNewElement(refclock_sources);
  813   refclock->driver_name = name;
  814   refclock->driver_parameter = param;
  815   refclock->driver_poll = dpoll;
  816   refclock->poll = poll;
  817   refclock->filter_length = filter_length;
  818   refclock->pps_forced = pps_forced;
  819   refclock->pps_rate = pps_rate;
  820   refclock->min_samples = min_samples;
  821   refclock->max_samples = max_samples;
  822   refclock->sel_options = sel_options;
  823   refclock->stratum = stratum;
  824   refclock->tai = tai;
  825   refclock->offset = offset;
  826   refclock->delay = delay;
  827   refclock->precision = precision;
  828   refclock->max_dispersion = max_dispersion;
  829   refclock->pulse_width = pulse_width;
  830   refclock->ref_id = ref_id;
  831   refclock->max_lock_age = max_lock_age;
  832   refclock->lock_ref_id = lock_ref_id;
  833 }
  834 
  835 /* ================================================== */
  836 
  837 static void
  838 parse_log(char *line)
  839 {
  840   char *log_name;
  841   do {
  842     log_name = line;
  843     line = CPS_SplitWord(line);
  844     if (*log_name) {
  845       if (!strcmp(log_name, "rawmeasurements")) {
  846         do_log_measurements = 1;
  847         raw_measurements = 1;
  848       } else if (!strcmp(log_name, "measurements")) {
  849         do_log_measurements = 1;
  850       } else if (!strcmp(log_name, "statistics")) {
  851         do_log_statistics = 1;
  852       } else if (!strcmp(log_name, "tracking")) {
  853         do_log_tracking = 1;
  854       } else if (!strcmp(log_name, "rtc")) {
  855         do_log_rtc = 1;
  856       } else if (!strcmp(log_name, "refclocks")) {
  857         do_log_refclocks = 1;
  858       } else if (!strcmp(log_name, "tempcomp")) {
  859         do_log_tempcomp = 1;
  860       } else {
  861         other_parse_error("Invalid log parameter");
  862         break;
  863       }
  864     } else {
  865       break;
  866     }
  867   } while (1);
  868 }
  869 
  870 /* ================================================== */
  871 
  872 static void
  873 parse_local(char *line)
  874 {
  875   if (!CPS_ParseLocal(line, &local_stratum, &local_orphan, &local_distance))
  876     command_parse_error();
  877   enable_local = 1;
  878 }
  879 
  880 /* ================================================== */
  881 
  882 static void
  883 parse_initstepslew(char *line)
  884 {
  885   char *p, *hostname;
  886   IPAddr ip_addr;
  887 
  888   /* Ignore the line if chronyd was started with -R. */
  889   if (restarted) {
  890     return;
  891   }
  892 
  893   ARR_SetSize(init_sources, 0);
  894   p = CPS_SplitWord(line);
  895 
  896   if (sscanf(line, "%lf", &init_slew_threshold) != 1) {
  897     command_parse_error();
  898     return;
  899   }
  900 
  901   while (*p) {
  902     hostname = p;
  903     p = CPS_SplitWord(p);
  904     if (*hostname) {
  905       if (DNS_Name2IPAddress(hostname, &ip_addr, 1) == DNS_Success) {
  906         ARR_AppendElement(init_sources, &ip_addr);
  907       } else {
  908         LOG(LOGS_WARN, "Could not resolve address of initstepslew server %s", hostname);
  909       }
  910     }
  911   }
  912 }
  913 
  914 /* ================================================== */
  915 
  916 static void
  917 parse_leapsecmode(char *line)
  918 {
  919   if (!strcasecmp(line, "system"))
  920     leapsec_mode = REF_LeapModeSystem;
  921   else if (!strcasecmp(line, "slew"))
  922     leapsec_mode = REF_LeapModeSlew;
  923   else if (!strcasecmp(line, "step"))
  924     leapsec_mode = REF_LeapModeStep;
  925   else if (!strcasecmp(line, "ignore"))
  926     leapsec_mode = REF_LeapModeIgnore;
  927   else
  928     command_parse_error();
  929 }
  930 
  931 /* ================================================== */
  932 
  933 static void
  934 parse_clientloglimit(char *line)
  935 {
  936   check_number_of_args(line, 1);
  937   if (sscanf(line, "%lu", &client_log_limit) != 1) {
  938     command_parse_error();
  939   }
  940 }
  941 
  942 /* ================================================== */
  943 
  944 static void
  945 parse_fallbackdrift(char *line)
  946 {
  947   check_number_of_args(line, 2);
  948   if (sscanf(line, "%d %d", &fb_drift_min, &fb_drift_max) != 2) {
  949     command_parse_error();
  950   }
  951 }
  952 
  953 /* ================================================== */
  954 
  955 static void
  956 parse_makestep(char *line)
  957 {
  958   check_number_of_args(line, 2);
  959   if (sscanf(line, "%lf %d", &make_step_threshold, &make_step_limit) != 2) {
  960     make_step_limit = 0;
  961     command_parse_error();
  962   }
  963 
  964   /* Disable limited makestep if chronyd was started with -R. */
  965   if (restarted && make_step_limit > 0) {
  966     make_step_limit = 0;
  967   }
  968 }
  969 
  970 /* ================================================== */
  971 
  972 static void
  973 parse_maxchange(char *line)
  974 {
  975   check_number_of_args(line, 3);
  976   if (sscanf(line, "%lf %d %d", &max_offset, &max_offset_delay, &max_offset_ignore) != 3) {
  977     max_offset_delay = -1;
  978     command_parse_error();
  979   }
  980 }
  981 
  982 /* ================================================== */
  983 
  984 static void
  985 parse_mailonchange(char *line)
  986 {
  987   char *address;
  988   check_number_of_args(line, 2);
  989   address = line;
  990   line = CPS_SplitWord(line);
  991   Free(mail_user_on_change);
  992   if (sscanf(line, "%lf", &mail_change_threshold) == 1) {
  993     mail_user_on_change = Strdup(address);
  994   } else {
  995     mail_user_on_change = NULL;
  996     command_parse_error();
  997   }
  998 }
  999 
 1000 /* ================================================== */
 1001 
 1002 static void
 1003 parse_allow_deny(char *line, ARR_Instance restrictions, int allow)
 1004 {
 1005   char *p;
 1006   unsigned long a, b, c, d, n;
 1007   int all = 0;
 1008   AllowDeny *new_node = NULL;
 1009   IPAddr ip_addr;
 1010 
 1011   p = line;
 1012 
 1013   if (!strncmp(p, "all", 3)) {
 1014     all = 1;
 1015     p = CPS_SplitWord(line);
 1016   }
 1017 
 1018   if (!*p) {
 1019     /* Empty line applies to all addresses */
 1020     new_node = (AllowDeny *)ARR_GetNewElement(restrictions);
 1021     new_node->allow = allow;
 1022     new_node->all = all;
 1023     new_node->ip.family = IPADDR_UNSPEC;
 1024     new_node->subnet_bits = 0;
 1025   } else {
 1026     char *slashpos;
 1027     slashpos = strchr(p, '/');
 1028     if (slashpos) *slashpos = 0;
 1029 
 1030     check_number_of_args(p, 1);
 1031     n = 0;
 1032     if (UTI_StringToIP(p, &ip_addr) ||
 1033         (n = sscanf(p, "%lu.%lu.%lu.%lu", &a, &b, &c, &d)) >= 1) {
 1034       new_node = (AllowDeny *)ARR_GetNewElement(restrictions);
 1035       new_node->allow = allow;
 1036       new_node->all = all;
 1037 
 1038       if (n == 0) {
 1039         new_node->ip = ip_addr;
 1040         if (ip_addr.family == IPADDR_INET6)
 1041           new_node->subnet_bits = 128;
 1042         else
 1043           new_node->subnet_bits = 32;
 1044       } else {
 1045         new_node->ip.family = IPADDR_INET4;
 1046 
 1047         a &= 0xff;
 1048         b &= 0xff;
 1049         c &= 0xff;
 1050         d &= 0xff;
 1051         
 1052         switch (n) {
 1053           case 1:
 1054             new_node->ip.addr.in4 = (a<<24);
 1055             new_node->subnet_bits = 8;
 1056             break;
 1057           case 2:
 1058             new_node->ip.addr.in4 = (a<<24) | (b<<16);
 1059             new_node->subnet_bits = 16;
 1060             break;
 1061           case 3:
 1062             new_node->ip.addr.in4 = (a<<24) | (b<<16) | (c<<8);
 1063             new_node->subnet_bits = 24;
 1064             break;
 1065           case 4:
 1066             new_node->ip.addr.in4 = (a<<24) | (b<<16) | (c<<8) | d;
 1067             new_node->subnet_bits = 32;
 1068             break;
 1069           default:
 1070             assert(0);
 1071         }
 1072       }
 1073       
 1074       if (slashpos) {
 1075         int specified_subnet_bits, n;
 1076         n = sscanf(slashpos+1, "%d", &specified_subnet_bits);
 1077         if (n == 1) {
 1078           new_node->subnet_bits = specified_subnet_bits;
 1079         } else {
 1080           command_parse_error();
 1081         }
 1082       }
 1083 
 1084     } else {
 1085       if (!slashpos && DNS_Name2IPAddress(p, &ip_addr, 1) == DNS_Success) {
 1086         new_node = (AllowDeny *)ARR_GetNewElement(restrictions);
 1087         new_node->allow = allow;
 1088         new_node->all = all;
 1089         new_node->ip = ip_addr;
 1090         if (ip_addr.family == IPADDR_INET6)
 1091           new_node->subnet_bits = 128;
 1092         else
 1093           new_node->subnet_bits = 32;
 1094       } else {
 1095         command_parse_error();
 1096       }      
 1097     }
 1098   }
 1099 }
 1100   
 1101 /* ================================================== */
 1102 
 1103 static void
 1104 parse_bindacqaddress(char *line)
 1105 {
 1106   IPAddr ip;
 1107 
 1108   check_number_of_args(line, 1);
 1109   if (UTI_StringToIP(line, &ip)) {
 1110     if (ip.family == IPADDR_INET4)
 1111       bind_acq_address4 = ip;
 1112     else if (ip.family == IPADDR_INET6)
 1113       bind_acq_address6 = ip;
 1114   } else {
 1115     command_parse_error();
 1116   }
 1117 }
 1118 
 1119 /* ================================================== */
 1120 
 1121 static void
 1122 parse_bindaddress(char *line)
 1123 {
 1124   IPAddr ip;
 1125 
 1126   check_number_of_args(line, 1);
 1127   if (UTI_StringToIP(line, &ip)) {
 1128     if (ip.family == IPADDR_INET4)
 1129       bind_address4 = ip;
 1130     else if (ip.family == IPADDR_INET6)
 1131       bind_address6 = ip;
 1132   } else {
 1133     command_parse_error();
 1134   }
 1135 }
 1136 
 1137 /* ================================================== */
 1138 
 1139 static void
 1140 parse_bindcmdaddress(char *line)
 1141 {
 1142   IPAddr ip;
 1143 
 1144   check_number_of_args(line, 1);
 1145 
 1146   /* Address starting with / is for the Unix domain socket */
 1147   if (line[0] == '/') {
 1148     parse_string(line, &bind_cmd_path);
 1149     /* / disables the socket */
 1150     if (!strcmp(bind_cmd_path, "/"))
 1151         bind_cmd_path[0] = '\0';
 1152   } else if (UTI_StringToIP(line, &ip)) {
 1153     if (ip.family == IPADDR_INET4)
 1154       bind_cmd_address4 = ip;
 1155     else if (ip.family == IPADDR_INET6)
 1156       bind_cmd_address6 = ip;
 1157   } else {
 1158     command_parse_error();
 1159   }
 1160 }
 1161 
 1162 /* ================================================== */
 1163 
 1164 static void
 1165 parse_broadcast(char *line)
 1166 {
 1167   /* Syntax : broadcast <interval> <broadcast-IP-addr> [<port>] */
 1168   NTP_Broadcast_Destination *destination;
 1169   int port;
 1170   int interval;
 1171   char *p;
 1172   IPAddr ip;
 1173   
 1174   p = line;
 1175   line = CPS_SplitWord(line);
 1176 
 1177   if (sscanf(p, "%d", &interval) != 1) {
 1178     command_parse_error();
 1179     return;
 1180   }
 1181 
 1182   p = line;
 1183   line = CPS_SplitWord(line);
 1184 
 1185   if (!UTI_StringToIP(p, &ip)) {
 1186     command_parse_error();
 1187     return;
 1188   }
 1189 
 1190   p = line;
 1191   line = CPS_SplitWord(line);
 1192 
 1193   if (*p) {
 1194     if (sscanf(p, "%d", &port) != 1 || *line) {
 1195       command_parse_error();
 1196       return;
 1197     }
 1198   } else {
 1199     /* default port */
 1200     port = NTP_PORT;
 1201   }
 1202 
 1203   destination = (NTP_Broadcast_Destination *)ARR_GetNewElement(broadcasts);
 1204   destination->addr = ip;
 1205   destination->port = port;
 1206   destination->interval = interval;
 1207 }
 1208 
 1209 /* ================================================== */
 1210 
 1211 static void
 1212 parse_smoothtime(char *line)
 1213 {
 1214   if (get_number_of_args(line) != 3)
 1215     check_number_of_args(line, 2);
 1216 
 1217   if (sscanf(line, "%lf %lf", &smooth_max_freq, &smooth_max_wander) != 2) {
 1218     smooth_max_freq = 0.0;
 1219     command_parse_error();
 1220   }
 1221 
 1222   line = CPS_SplitWord(CPS_SplitWord(line));
 1223   smooth_leap_only = 0;
 1224 
 1225   if (*line) {
 1226     if (!strcasecmp(line, "leaponly"))
 1227       smooth_leap_only = 1;
 1228     else
 1229       command_parse_error();
 1230   }
 1231 }
 1232 
 1233 /* ================================================== */
 1234 static void
 1235 parse_tempcomp(char *line)
 1236 {
 1237   char *p;
 1238   int point_form;
 1239 
 1240   point_form = get_number_of_args(line) == 3;
 1241 
 1242   if (!point_form)
 1243     check_number_of_args(line, 6);
 1244 
 1245   p = line;
 1246   line = CPS_SplitWord(line);
 1247 
 1248   if (!*p) {
 1249     command_parse_error();
 1250     return;
 1251   }
 1252 
 1253   Free(tempcomp_point_file);
 1254 
 1255   if (point_form) {
 1256     if (sscanf(line, "%lf", &tempcomp_interval) != 1) {
 1257       command_parse_error();
 1258       return;
 1259     }
 1260     tempcomp_point_file = Strdup(CPS_SplitWord(line));
 1261   } else {
 1262     if (sscanf(line, "%lf %lf %lf %lf %lf", &tempcomp_interval,
 1263                &tempcomp_T0, &tempcomp_k0, &tempcomp_k1, &tempcomp_k2) != 5) {
 1264       command_parse_error();
 1265       return;
 1266     }
 1267     tempcomp_point_file = NULL;
 1268   }
 1269 
 1270   Free(tempcomp_sensor_file);
 1271   tempcomp_sensor_file = Strdup(p);
 1272 }
 1273 
 1274 /* ================================================== */
 1275 
 1276 static void
 1277 parse_hwtimestamp(char *line)
 1278 {
 1279   CNF_HwTsInterface *iface;
 1280   char *p, filter[5];
 1281   int n;
 1282 
 1283   if (!*line) {
 1284     command_parse_error();
 1285     return;
 1286   }
 1287 
 1288   p = line;
 1289   line = CPS_SplitWord(line);
 1290 
 1291   iface = ARR_GetNewElement(hwts_interfaces);
 1292   iface->name = Strdup(p);
 1293   iface->minpoll = 0;
 1294   iface->min_samples = 2;
 1295   iface->max_samples = 16;
 1296   iface->nocrossts = 0;
 1297   iface->rxfilter = CNF_HWTS_RXFILTER_ANY;
 1298   iface->precision = 100.0e-9;
 1299   iface->tx_comp = 0.0;
 1300   iface->rx_comp = 0.0;
 1301 
 1302   for (p = line; *p; line += n, p = line) {
 1303     line = CPS_SplitWord(line);
 1304 
 1305     if (!strcasecmp(p, "maxsamples")) {
 1306       if (sscanf(line, "%d%n", &iface->max_samples, &n) != 1)
 1307         break;
 1308     } else if (!strcasecmp(p, "minpoll")) {
 1309       if (sscanf(line, "%d%n", &iface->minpoll, &n) != 1)
 1310         break;
 1311     } else if (!strcasecmp(p, "minsamples")) {
 1312       if (sscanf(line, "%d%n", &iface->min_samples, &n) != 1)
 1313         break;
 1314     } else if (!strcasecmp(p, "precision")) {
 1315       if (sscanf(line, "%lf%n", &iface->precision, &n) != 1)
 1316         break;
 1317     } else if (!strcasecmp(p, "rxcomp")) {
 1318       if (sscanf(line, "%lf%n", &iface->rx_comp, &n) != 1)
 1319         break;
 1320     } else if (!strcasecmp(p, "txcomp")) {
 1321       if (sscanf(line, "%lf%n", &iface->tx_comp, &n) != 1)
 1322         break;
 1323     } else if (!strcasecmp(p, "rxfilter")) {
 1324       if (sscanf(line, "%4s%n", filter, &n) != 1)
 1325         break;
 1326       if (!strcasecmp(filter, "none"))
 1327         iface->rxfilter = CNF_HWTS_RXFILTER_NONE;
 1328       else if (!strcasecmp(filter, "ntp"))
 1329         iface->rxfilter = CNF_HWTS_RXFILTER_NTP;
 1330       else if (!strcasecmp(filter, "all"))
 1331         iface->rxfilter = CNF_HWTS_RXFILTER_ALL;
 1332       else
 1333         break;
 1334     } else if (!strcasecmp(p, "nocrossts")) {
 1335       n = 0;
 1336       iface->nocrossts = 1;
 1337     } else {
 1338       break;
 1339     }
 1340   }
 1341 
 1342   if (*p)
 1343     command_parse_error();
 1344 }
 1345 
 1346 /* ================================================== */
 1347 
 1348 static void
 1349 parse_include(char *line)
 1350 {
 1351   glob_t gl;
 1352   size_t i;
 1353   int r;
 1354 
 1355   check_number_of_args(line, 1);
 1356 
 1357   if ((r = glob(line,
 1358 #ifdef GLOB_NOMAGIC
 1359                 GLOB_NOMAGIC |
 1360 #endif
 1361                 GLOB_ERR, NULL, &gl)) != 0) {
 1362     if (r != GLOB_NOMATCH)
 1363       LOG_FATAL("Could not search for files matching %s", line);
 1364 
 1365     DEBUG_LOG("glob of %s failed", line);
 1366     return;
 1367   }
 1368 
 1369   for (i = 0; i < gl.gl_pathc; i++)
 1370     CNF_ReadFile(gl.gl_pathv[i]);
 1371 
 1372   globfree(&gl);
 1373 }
 1374 
 1375 /* ================================================== */
 1376 
 1377 void
 1378 CNF_CreateDirs(uid_t uid, gid_t gid)
 1379 {
 1380   char *dir;
 1381 
 1382   /* Create a directory for the Unix domain command socket */
 1383   if (bind_cmd_path[0]) {
 1384     dir = UTI_PathToDir(bind_cmd_path);
 1385     UTI_CreateDirAndParents(dir, 0770, uid, gid);
 1386 
 1387     /* Check the permissions and owner/group in case the directory already
 1388        existed.  It MUST NOT be accessible by others as permissions on Unix
 1389        domain sockets are ignored on some systems (e.g. Solaris). */
 1390     if (!UTI_CheckDirPermissions(dir, 0770, uid, gid)) {
 1391       LOG(LOGS_WARN, "Disabled command socket %s", bind_cmd_path);
 1392       bind_cmd_path[0] = '\0';
 1393     }
 1394 
 1395     Free(dir);
 1396   }
 1397 
 1398   if (logdir[0])
 1399     UTI_CreateDirAndParents(logdir, 0755, uid, gid);
 1400   if (dumpdir[0])
 1401     UTI_CreateDirAndParents(dumpdir, 0755, uid, gid);
 1402 }
 1403 
 1404 /* ================================================== */
 1405 
 1406 void
 1407 CNF_AddInitSources(void)
 1408 {
 1409   CPS_NTP_Source cps_source;
 1410   NTP_Remote_Address ntp_addr;
 1411   char dummy_hostname[2] = "H";
 1412   unsigned int i;
 1413 
 1414   for (i = 0; i < ARR_GetSize(init_sources); i++) {
 1415     /* Get the default NTP params */
 1416     CPS_ParseNTPSourceAdd(dummy_hostname, &cps_source);
 1417 
 1418     /* Add the address as an offline iburst server */
 1419     ntp_addr.ip_addr = *(IPAddr *)ARR_GetElement(init_sources, i);
 1420     ntp_addr.port = cps_source.port;
 1421     cps_source.params.iburst = 1;
 1422     cps_source.params.connectivity = SRC_OFFLINE;
 1423 
 1424     NSR_AddSource(&ntp_addr, NTP_SERVER, &cps_source.params);
 1425   }
 1426 
 1427   ARR_SetSize(init_sources, 0);
 1428 }
 1429 
 1430 /* ================================================== */
 1431 
 1432 void
 1433 CNF_AddSources(void)
 1434 {
 1435   NTP_Source *source;
 1436   unsigned int i;
 1437 
 1438   for (i = 0; i < ARR_GetSize(ntp_sources); i++) {
 1439     source = (NTP_Source *)ARR_GetElement(ntp_sources, i);
 1440     NSR_AddSourceByName(source->params.name, source->params.port,
 1441                         source->pool, source->type, &source->params.params);
 1442     Free(source->params.name);
 1443   }
 1444 
 1445   ARR_SetSize(ntp_sources, 0);
 1446 }
 1447 
 1448 /* ================================================== */
 1449 
 1450 void
 1451 CNF_AddRefclocks(void)
 1452 {
 1453   unsigned int i;
 1454 
 1455   for (i = 0; i < ARR_GetSize(refclock_sources); i++) {
 1456     RCL_AddRefclock((RefclockParameters *)ARR_GetElement(refclock_sources, i));
 1457   }
 1458 
 1459   ARR_SetSize(refclock_sources, 0);
 1460 }
 1461 
 1462 /* ================================================== */
 1463 
 1464 void
 1465 CNF_AddBroadcasts(void)
 1466 {
 1467   unsigned int i;
 1468   NTP_Broadcast_Destination *destination;
 1469 
 1470   for (i = 0; i < ARR_GetSize(broadcasts); i++) {
 1471     destination = (NTP_Broadcast_Destination *)ARR_GetElement(broadcasts, i);
 1472     NCR_AddBroadcastDestination(&destination->addr, destination->port,
 1473                                 destination->interval);
 1474   }
 1475 
 1476   ARR_SetSize(broadcasts, 0);
 1477 }
 1478 
 1479 /* ================================================== */
 1480 
 1481 int
 1482 CNF_GetNTPPort(void)
 1483 {
 1484   return ntp_port;
 1485 }
 1486 
 1487 /* ================================================== */
 1488 
 1489 int
 1490 CNF_GetAcquisitionPort(void)
 1491 {
 1492   return acquisition_port;
 1493 }
 1494 
 1495 /* ================================================== */
 1496 
 1497 char *
 1498 CNF_GetDriftFile(void)
 1499 {
 1500   return drift_file;
 1501 }
 1502 
 1503 /* ================================================== */
 1504 
 1505 int
 1506 CNF_GetLogBanner(void)
 1507 {
 1508   return log_banner;
 1509 }
 1510 
 1511 /* ================================================== */
 1512 
 1513 char *
 1514 CNF_GetLogDir(void)
 1515 {
 1516   return logdir;
 1517 }
 1518 
 1519 /* ================================================== */
 1520 
 1521 char *
 1522 CNF_GetDumpDir(void)
 1523 {
 1524   return dumpdir;
 1525 }
 1526 
 1527 /* ================================================== */
 1528 
 1529 int
 1530 CNF_GetLogMeasurements(int *raw)
 1531 {
 1532   *raw = raw_measurements;
 1533   return do_log_measurements;
 1534 }
 1535 
 1536 /* ================================================== */
 1537 
 1538 int
 1539 CNF_GetLogStatistics(void)
 1540 {
 1541   return do_log_statistics;
 1542 }
 1543 
 1544 /* ================================================== */
 1545 
 1546 int
 1547 CNF_GetLogTracking(void)
 1548 {
 1549   return do_log_tracking;
 1550 }
 1551 
 1552 /* ================================================== */
 1553 
 1554 int
 1555 CNF_GetLogRtc(void)
 1556 {
 1557   return do_log_rtc;
 1558 }
 1559 
 1560 /* ================================================== */
 1561 
 1562 int
 1563 CNF_GetLogRefclocks(void)
 1564 {
 1565   return do_log_refclocks;
 1566 }
 1567 
 1568 /* ================================================== */
 1569 
 1570 int
 1571 CNF_GetLogTempComp(void)
 1572 {
 1573   return do_log_tempcomp;
 1574 }
 1575 
 1576 /* ================================================== */
 1577 
 1578 char *
 1579 CNF_GetKeysFile(void)
 1580 {
 1581   return keys_file;
 1582 }
 1583 
 1584 /* ================================================== */
 1585 
 1586 double
 1587 CNF_GetRtcAutotrim(void)
 1588 {
 1589   return rtc_autotrim_threshold;
 1590 }
 1591 
 1592 /* ================================================== */
 1593 
 1594 char *
 1595 CNF_GetRtcFile(void)
 1596 {
 1597   return rtc_file;
 1598 }
 1599 
 1600 /* ================================================== */
 1601 
 1602 char *
 1603 CNF_GetRtcDevice(void)
 1604 {
 1605   return rtc_device;
 1606 }
 1607 
 1608 /* ================================================== */
 1609 
 1610 double
 1611 CNF_GetMaxUpdateSkew(void)
 1612 {
 1613   return max_update_skew;
 1614 }
 1615 
 1616 /* ================================================== */
 1617 
 1618 double
 1619 CNF_GetMaxDrift(void)
 1620 {
 1621   return max_drift;
 1622 }
 1623 
 1624 /* ================================================== */
 1625 
 1626 double
 1627 CNF_GetMaxClockError(void)
 1628 {
 1629   return max_clock_error;
 1630 }
 1631 
 1632 /* ================================================== */
 1633 
 1634 double
 1635 CNF_GetCorrectionTimeRatio(void)
 1636 {
 1637   return correction_time_ratio;
 1638 }
 1639 
 1640 /* ================================================== */
 1641 
 1642 double
 1643 CNF_GetMaxSlewRate(void)
 1644 {
 1645   return max_slew_rate;
 1646 }
 1647 
 1648 /* ================================================== */
 1649 
 1650 double
 1651 CNF_GetMaxDistance(void)
 1652 {
 1653   return max_distance;
 1654 }
 1655 
 1656 /* ================================================== */
 1657 
 1658 double
 1659 CNF_GetMaxJitter(void)
 1660 {
 1661   return max_jitter;
 1662 }
 1663 
 1664 /* ================================================== */
 1665 
 1666 double
 1667 CNF_GetReselectDistance(void)
 1668 {
 1669   return reselect_distance;
 1670 }
 1671 
 1672 /* ================================================== */
 1673 
 1674 double
 1675 CNF_GetStratumWeight(void)
 1676 {
 1677   return stratum_weight;
 1678 }
 1679 
 1680 /* ================================================== */
 1681 
 1682 double
 1683 CNF_GetCombineLimit(void)
 1684 {
 1685   return combine_limit;
 1686 }
 1687 
 1688 /* ================================================== */
 1689 
 1690 int
 1691 CNF_GetManualEnabled(void)
 1692 {
 1693   return enable_manual;
 1694 }
 1695 
 1696 /* ================================================== */
 1697 
 1698 int
 1699 CNF_GetCommandPort(void) {
 1700   return cmd_port;
 1701 }
 1702 
 1703 /* ================================================== */
 1704 
 1705 int
 1706 CNF_AllowLocalReference(int *stratum, int *orphan, double *distance)
 1707 {
 1708   if (enable_local) {
 1709     *stratum = local_stratum;
 1710     *orphan = local_orphan;
 1711     *distance = local_distance;
 1712     return 1;
 1713   } else {
 1714     return 0;
 1715   }
 1716 }
 1717 
 1718 /* ================================================== */
 1719 
 1720 int
 1721 CNF_GetRtcOnUtc(void)
 1722 {
 1723   return rtc_on_utc;
 1724 }
 1725 
 1726 /* ================================================== */
 1727 
 1728 int
 1729 CNF_GetRtcSync(void)
 1730 {
 1731   return rtc_sync;
 1732 }
 1733 
 1734 /* ================================================== */
 1735 
 1736 void
 1737 CNF_GetMakeStep(int *limit, double *threshold)
 1738 {
 1739   *limit = make_step_limit;
 1740   *threshold = make_step_threshold;
 1741 }
 1742 
 1743 /* ================================================== */
 1744 
 1745 void
 1746 CNF_GetMaxChange(int *delay, int *ignore, double *offset)
 1747 {
 1748   *delay = max_offset_delay;
 1749   *ignore = max_offset_ignore;
 1750   *offset = max_offset;
 1751 }
 1752 
 1753 /* ================================================== */
 1754 
 1755 double
 1756 CNF_GetLogChange(void)
 1757 {
 1758   return log_change_threshold;
 1759 }
 1760 
 1761 /* ================================================== */
 1762 
 1763 void
 1764 CNF_GetMailOnChange(int *enabled, double *threshold, char **user)
 1765 {
 1766   if (mail_user_on_change) {
 1767     *enabled = 1;
 1768     *threshold = mail_change_threshold;
 1769     *user = mail_user_on_change;
 1770   } else {
 1771     *enabled = 0;
 1772     *threshold = 0.0;
 1773     *user = NULL;
 1774   }
 1775 }  
 1776 
 1777 /* ================================================== */
 1778 
 1779 void
 1780 CNF_SetupAccessRestrictions(void)
 1781 {
 1782   AllowDeny *node;
 1783   int status;
 1784   unsigned int i;
 1785 
 1786   for (i = 0; i < ARR_GetSize(ntp_restrictions); i++) {
 1787     node = ARR_GetElement(ntp_restrictions, i);
 1788     status = NCR_AddAccessRestriction(&node->ip, node->subnet_bits, node->allow, node->all);
 1789     if (!status) {
 1790       LOG_FATAL("Bad subnet in %s/%d", UTI_IPToString(&node->ip), node->subnet_bits);
 1791     }
 1792   }
 1793 
 1794   for (i = 0; i < ARR_GetSize(cmd_restrictions); i++) {
 1795     node = ARR_GetElement(cmd_restrictions, i);
 1796     status = CAM_AddAccessRestriction(&node->ip, node->subnet_bits, node->allow, node->all);
 1797     if (!status) {
 1798       LOG_FATAL("Bad subnet in %s/%d", UTI_IPToString(&node->ip), node->subnet_bits);
 1799     }
 1800   }
 1801 
 1802   ARR_SetSize(ntp_restrictions, 0);
 1803   ARR_SetSize(cmd_restrictions, 0);
 1804 }
 1805 
 1806 /* ================================================== */
 1807 
 1808 int
 1809 CNF_GetNoClientLog(void)
 1810 {
 1811   return no_client_log;
 1812 }
 1813 
 1814 /* ================================================== */
 1815 
 1816 unsigned long
 1817 CNF_GetClientLogLimit(void)
 1818 {
 1819   return client_log_limit;
 1820 }
 1821 
 1822 /* ================================================== */
 1823 
 1824 void
 1825 CNF_GetFallbackDrifts(int *min, int *max)
 1826 {
 1827   *min = fb_drift_min;
 1828   *max = fb_drift_max;
 1829 }
 1830 
 1831 /* ================================================== */
 1832 
 1833 void
 1834 CNF_GetBindAddress(int family, IPAddr *addr)
 1835 {
 1836   if (family == IPADDR_INET4)
 1837     *addr = bind_address4;
 1838   else if (family == IPADDR_INET6)
 1839     *addr = bind_address6;
 1840   else
 1841     addr->family = IPADDR_UNSPEC;
 1842 }
 1843 
 1844 /* ================================================== */
 1845 
 1846 void
 1847 CNF_GetBindAcquisitionAddress(int family, IPAddr *addr)
 1848 {
 1849   if (family == IPADDR_INET4)
 1850     *addr = bind_acq_address4;
 1851   else if (family == IPADDR_INET6)
 1852     *addr = bind_acq_address6;
 1853   else
 1854     addr->family = IPADDR_UNSPEC;
 1855 }
 1856 
 1857 /* ================================================== */
 1858 
 1859 char *
 1860 CNF_GetBindCommandPath(void)
 1861 {
 1862   return bind_cmd_path;
 1863 }
 1864 
 1865 /* ================================================== */
 1866 
 1867 void
 1868 CNF_GetBindCommandAddress(int family, IPAddr *addr)
 1869 {
 1870   if (family == IPADDR_INET4)
 1871     *addr = bind_cmd_address4;
 1872   else if (family == IPADDR_INET6)
 1873     *addr = bind_cmd_address6;
 1874   else
 1875     addr->family = IPADDR_UNSPEC;
 1876 }
 1877 
 1878 /* ================================================== */
 1879 
 1880 char *
 1881 CNF_GetNtpSigndSocket(void)
 1882 {
 1883   return ntp_signd_socket;
 1884 }
 1885 
 1886 /* ================================================== */
 1887 
 1888 char *
 1889 CNF_GetPidFile(void)
 1890 {
 1891   return pidfile;
 1892 }
 1893 
 1894 /* ================================================== */
 1895 
 1896 REF_LeapMode
 1897 CNF_GetLeapSecMode(void)
 1898 {
 1899   return leapsec_mode;
 1900 }
 1901 
 1902 /* ================================================== */
 1903 
 1904 char *
 1905 CNF_GetLeapSecTimezone(void)
 1906 {
 1907   return leapsec_tz;
 1908 }
 1909 
 1910 /* ================================================== */
 1911 
 1912 int
 1913 CNF_GetSchedPriority(void)
 1914 {
 1915   return sched_priority;
 1916 }
 1917 
 1918 /* ================================================== */
 1919 
 1920 int
 1921 CNF_GetLockMemory(void)
 1922 {
 1923   return lock_memory;
 1924 }
 1925 
 1926 /* ================================================== */
 1927 
 1928 int CNF_GetNTPRateLimit(int *interval, int *burst, int *leak)
 1929 {
 1930   *interval = ntp_ratelimit_interval;
 1931   *burst = ntp_ratelimit_burst;
 1932   *leak = ntp_ratelimit_leak;
 1933   return ntp_ratelimit_enabled;
 1934 }
 1935 
 1936 /* ================================================== */
 1937 
 1938 int CNF_GetCommandRateLimit(int *interval, int *burst, int *leak)
 1939 {
 1940   *interval = cmd_ratelimit_interval;
 1941   *burst = cmd_ratelimit_burst;
 1942   *leak = cmd_ratelimit_leak;
 1943   return cmd_ratelimit_enabled;
 1944 }
 1945 
 1946 /* ================================================== */
 1947 
 1948 void
 1949 CNF_GetSmooth(double *max_freq, double *max_wander, int *leap_only)
 1950 {
 1951   *max_freq = smooth_max_freq;
 1952   *max_wander = smooth_max_wander;
 1953   *leap_only = smooth_leap_only;
 1954 }
 1955 
 1956 /* ================================================== */
 1957 
 1958 void
 1959 CNF_GetTempComp(char **file, double *interval, char **point_file, double *T0, double *k0, double *k1, double *k2)
 1960 {
 1961   *file = tempcomp_sensor_file;
 1962   *point_file = tempcomp_point_file;
 1963   *interval = tempcomp_interval;
 1964   *T0 = tempcomp_T0;
 1965   *k0 = tempcomp_k0;
 1966   *k1 = tempcomp_k1;
 1967   *k2 = tempcomp_k2;
 1968 }
 1969 
 1970 /* ================================================== */
 1971 
 1972 char *
 1973 CNF_GetUser(void)
 1974 {
 1975   return user;
 1976 }
 1977 
 1978 /* ================================================== */
 1979 
 1980 int
 1981 CNF_GetMaxSamples(void)
 1982 {
 1983   return max_samples;
 1984 }
 1985 
 1986 /* ================================================== */
 1987 
 1988 int
 1989 CNF_GetMinSamples(void)
 1990 {
 1991   return min_samples;
 1992 }
 1993 
 1994 /* ================================================== */
 1995 
 1996 int
 1997 CNF_GetMinSources(void)
 1998 {
 1999   return min_sources;
 2000 }
 2001 
 2002 /* ================================================== */
 2003 
 2004 char *
 2005 CNF_GetHwclockFile(void)
 2006 {
 2007   return hwclock_file;
 2008 }
 2009 
 2010 /* ================================================== */
 2011 
 2012 int
 2013 CNF_GetInitSources(void)
 2014 {
 2015   return ARR_GetSize(init_sources);
 2016 }
 2017 
 2018 /* ================================================== */
 2019 
 2020 double
 2021 CNF_GetInitStepThreshold(void)
 2022 {
 2023   return init_slew_threshold;
 2024 }
 2025 
 2026 /* ================================================== */
 2027 
 2028 int
 2029 CNF_GetHwTsInterface(unsigned int index, CNF_HwTsInterface **iface)
 2030 {
 2031   if (index >= ARR_GetSize(hwts_interfaces))
 2032     return 0;
 2033 
 2034   *iface = (CNF_HwTsInterface *)ARR_GetElement(hwts_interfaces, index);
 2035   return 1;
 2036 }