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