3 * This file contains the ntpd configuration code.
5 * Written By: Sachin Kamboj
6 * University of Delaware
8 * Some parts borrowed from the older ntp_config.c
17 # include <netinfo/ni.h>
22 #ifdef HAVE_SYS_PARAM_H
23 # include <sys/param.h>
27 # define SIGCHLD SIGCLD
29 #ifdef HAVE_SYS_WAIT_H
30 # include <sys/wait.h>
34 #include <isc/result.h>
39 #include "ntp_unixtime.h"
40 #include "ntp_refclock.h"
41 #include "ntp_filegen.h"
42 #include "ntp_stdlib.h"
43 #include "lib_strbuf.h"
44 #include "ntp_assert.h"
45 #include "ntp_random.h"
47 * [Bug 467]: Some linux headers collide with CONFIG_PHONE and CONFIG_KEYS
48 * so #include these later.
50 #include "ntp_config.h"
51 #include "ntp_cmdargs.h"
52 #include "ntp_scanner.h"
53 #include "ntp_parser.h"
54 #include "ntpd-opts.h"
56 #ifndef IGNORE_DNS_ERRORS
59 # define DNSFLAGS GAIR_F_IGNDNSERR
62 extern int yyparse(void);
65 #if defined(HAVE_SYS_MMAN_H)
66 # include <sys/mman.h>
69 /* list of servers from command line for config_peers() */
70 int cmdline_server_count;
71 char ** cmdline_servers;
73 /* Current state of memory locking:
75 * 0: memory locking disabled
76 * 1: Memory locking enabled
81 * "logconfig" building blocks
84 const char * const name;
88 static struct masks logcfg_class[] = {
89 { "clock", NLOG_OCLOCK },
90 { "peer", NLOG_OPEER },
91 { "sync", NLOG_OSYNC },
96 /* logcfg_noclass_items[] masks are complete and must not be shifted */
97 static struct masks logcfg_noclass_items[] = {
98 { "allall", NLOG_SYSMASK | NLOG_PEERMASK | NLOG_CLOCKMASK | NLOG_SYNCMASK },
99 { "allinfo", NLOG_SYSINFO | NLOG_PEERINFO | NLOG_CLOCKINFO | NLOG_SYNCINFO },
100 { "allevents", NLOG_SYSEVENT | NLOG_PEEREVENT | NLOG_CLOCKEVENT | NLOG_SYNCEVENT },
101 { "allstatus", NLOG_SYSSTATUS | NLOG_PEERSTATUS | NLOG_CLOCKSTATUS | NLOG_SYNCSTATUS },
102 { "allstatistics", NLOG_SYSSTATIST | NLOG_PEERSTATIST | NLOG_CLOCKSTATIST | NLOG_SYNCSTATIST },
103 /* the remainder are misspellings of clockall, peerall, sysall, and syncall. */
104 { "allclock", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OCLOCK },
105 { "allpeer", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OPEER },
106 { "allsys", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OSYS },
107 { "allsync", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OSYNC },
111 /* logcfg_class_items[] masks are shiftable by NLOG_O* counts */
112 static struct masks logcfg_class_items[] = {
113 { "all", NLOG_INFO | NLOG_EVENT | NLOG_STATUS | NLOG_STATIST },
114 { "info", NLOG_INFO },
115 { "events", NLOG_EVENT },
116 { "status", NLOG_STATUS },
117 { "statistics", NLOG_STATIST },
121 typedef struct peer_resolved_ctx_tag {
123 int host_mode; /* T_* token identifier */
126 u_char hmode; /* MODE_* */
135 #define MAXPHONE 10 /* maximum number of phone strings */
136 #define MAXPPS 20 /* maximum length of PPS device string */
139 * Miscellaneous macros
141 #define ISEOL(c) ((c) == '#' || (c) == '\n' || (c) == '\0')
142 #define ISSPACE(c) ((c) == ' ' || (c) == '\t')
144 #define _UC(str) ((char *)(intptr_t)(str))
147 * Definitions of things either imported from or exported to outside
149 extern int yydebug; /* ntp_parser.c (.y) */
150 config_tree cfgt; /* Parser output stored here */
151 struct config_tree_tag *cfg_tree_history; /* History of configs */
152 char * sys_phone[MAXPHONE] = {NULL}; /* ACTS phone numbers */
153 char default_keysdir[] = NTP_KEYSDIR;
154 char * keysdir = default_keysdir; /* crypto keys directory */
155 char * saveconfigdir;
156 #if defined(HAVE_SCHED_SETSCHEDULER)
157 int config_priority_override = 0;
161 const char *config_file;
162 static char default_ntp_signd_socket[] =
163 #ifdef NTP_SIGND_PATH
168 char *ntp_signd_socket = default_ntp_signd_socket;
170 struct netinfo_config_state *config_netinfo = NULL;
171 int check_netinfo = 1;
172 #endif /* HAVE_NETINFO */
174 char *alt_config_file;
176 char config_file_storage[MAX_PATH];
177 char alt_config_file_storage[MAX_PATH];
178 #endif /* SYS_WINNT */
182 * NetInfo configuration state
184 struct netinfo_config_state {
185 void *domain; /* domain with config */
186 ni_id config_dir; /* ID config dir */
187 int prop_index; /* current property */
188 int val_index; /* current value */
189 char **val_list; /* value list */
193 struct REMOTE_CONFIG_INFO remote_config; /* Remote configuration buffer and
195 int old_config_style = 1; /* A boolean flag, which when set,
196 * indicates that the old configuration
197 * format with a newline at the end of
198 * every command is being used
200 int cryptosw; /* crypto command called */
202 extern char *stats_drift_file; /* name of the driftfile */
204 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
206 * backwards compatibility flags
208 bc_entry bc_list[] = {
209 { T_Bc_bugXXXX, 1 } /* default enabled */
213 * declare an int pointer for each flag for quick testing without
214 * walking bc_list. If the pointer is consumed by libntp rather
215 * than ntpd, declare it in a libntp source file pointing to storage
216 * initialized with the appropriate value for other libntp clients, and
217 * redirect it to point into bc_list during ntpd startup.
219 int *p_bcXXXX_enabled = &bc_list[0].enabled;
222 /* FUNCTION PROTOTYPES */
224 static void init_syntax_tree(config_tree *);
225 static void apply_enable_disable(attr_val_fifo *q, int enable);
228 static void free_auth_node(config_tree *);
229 static void free_all_config_trees(void);
231 static void free_config_access(config_tree *);
232 static void free_config_auth(config_tree *);
233 static void free_config_fudge(config_tree *);
234 static void free_config_logconfig(config_tree *);
235 static void free_config_monitor(config_tree *);
236 static void free_config_nic_rules(config_tree *);
237 static void free_config_other_modes(config_tree *);
238 static void free_config_peers(config_tree *);
239 static void free_config_phone(config_tree *);
240 static void free_config_reset_counters(config_tree *);
241 static void free_config_rlimit(config_tree *);
242 static void free_config_setvar(config_tree *);
243 static void free_config_system_opts(config_tree *);
244 static void free_config_tinker(config_tree *);
245 static void free_config_tos(config_tree *);
246 static void free_config_trap(config_tree *);
247 static void free_config_ttl(config_tree *);
248 static void free_config_unpeers(config_tree *);
249 static void free_config_vars(config_tree *);
252 static void free_config_sim(config_tree *);
254 static void destroy_address_fifo(address_fifo *);
255 #define FREE_ADDRESS_FIFO(pf) \
257 destroy_address_fifo(pf); \
260 void free_all_config_trees(void); /* atexit() */
261 static void free_config_tree(config_tree *ptree);
262 #endif /* FREE_CFG_T */
264 static void destroy_restrict_node(restrict_node *my_node);
265 static int is_sane_resolved_address(sockaddr_u *peeraddr, int hmode);
266 static void save_and_apply_config_tree(int/*BOOL*/ from_file);
267 static void destroy_int_fifo(int_fifo *);
268 #define FREE_INT_FIFO(pf) \
270 destroy_int_fifo(pf); \
273 static void destroy_string_fifo(string_fifo *);
274 #define FREE_STRING_FIFO(pf) \
276 destroy_string_fifo(pf); \
279 static void destroy_attr_val_fifo(attr_val_fifo *);
280 #define FREE_ATTR_VAL_FIFO(pf) \
282 destroy_attr_val_fifo(pf); \
285 static void destroy_filegen_fifo(filegen_fifo *);
286 #define FREE_FILEGEN_FIFO(pf) \
288 destroy_filegen_fifo(pf); \
291 static void destroy_restrict_fifo(restrict_fifo *);
292 #define FREE_RESTRICT_FIFO(pf) \
294 destroy_restrict_fifo(pf); \
297 static void destroy_setvar_fifo(setvar_fifo *);
298 #define FREE_SETVAR_FIFO(pf) \
300 destroy_setvar_fifo(pf); \
303 static void destroy_addr_opts_fifo(addr_opts_fifo *);
304 #define FREE_ADDR_OPTS_FIFO(pf) \
306 destroy_addr_opts_fifo(pf); \
310 static void config_logconfig(config_tree *);
311 static void config_monitor(config_tree *);
312 static void config_rlimit(config_tree *);
313 static void config_system_opts(config_tree *);
314 static void config_tinker(config_tree *);
315 static int config_tos_clock(config_tree *);
316 static void config_tos(config_tree *);
317 static void config_vars(config_tree *);
320 static sockaddr_u *get_next_address(address_node *addr);
321 static void config_sim(config_tree *);
322 static void config_ntpdsim(config_tree *);
323 #else /* !SIM follows */
324 static void config_ntpd(config_tree *, int/*BOOL*/ input_from_file);
325 static void config_other_modes(config_tree *);
326 static void config_auth(config_tree *);
327 static void config_access(config_tree *);
328 static void config_mdnstries(config_tree *);
329 static void config_phone(config_tree *);
330 static void config_setvar(config_tree *);
331 static void config_ttl(config_tree *);
332 static void config_trap(config_tree *);
333 static void config_fudge(config_tree *);
334 static void config_peers(config_tree *);
335 static void config_unpeers(config_tree *);
336 static void config_nic_rules(config_tree *, int/*BOOL*/ input_from_file);
337 static void config_reset_counters(config_tree *);
338 static u_char get_correct_host_mode(int token);
339 static int peerflag_bits(peer_node *);
343 static void peer_name_resolved(int, int, void *, const char *, const char *,
344 const struct addrinfo *,
345 const struct addrinfo *);
346 static void unpeer_name_resolved(int, int, void *, const char *, const char *,
347 const struct addrinfo *,
348 const struct addrinfo *);
349 static void trap_name_resolved(int, int, void *, const char *, const char *,
350 const struct addrinfo *,
351 const struct addrinfo *);
356 t_REF, /* Refclock */
357 t_MSK /* Network Mask */
360 static void ntpd_set_tod_using(const char *);
361 static char * normal_dtoa(double);
362 static u_int32 get_pfxmatch(const char **, struct masks *);
363 static u_int32 get_match(const char *, struct masks *);
364 static u_int32 get_logmask(const char *);
365 static int/*BOOL*/ is_refclk_addr(const address_node * addr);
367 static void appendstr(char *, size_t, char *);
371 static int getnetnum(const char *num, sockaddr_u *addr, int complain,
372 enum gnn_type a_type);
376 #if defined(__GNUC__) /* this covers CLANG, too */
377 static void __attribute__((noreturn,format(printf,1,2))) fatal_error(const char *fmt, ...)
378 #elif defined(_MSC_VER)
379 static void __declspec(noreturn) fatal_error(const char *fmt, ...)
381 static void fatal_error(const char *fmt, ...)
387 mvsyslog(LOG_EMERG, fmt, va);
393 /* FUNCTIONS FOR INITIALIZATION
394 * ----------------------------
403 if (ptree->auth.keys) {
404 free(ptree->auth.keys);
405 ptree->auth.keys = NULL;
408 if (ptree->auth.keysdir) {
409 free(ptree->auth.keysdir);
410 ptree->auth.keysdir = NULL;
413 if (ptree->auth.ntp_signd_socket) {
414 free(ptree->auth.ntp_signd_socket);
415 ptree->auth.ntp_signd_socket = NULL;
427 ptree->mdnstries = 5;
433 free_all_config_trees(void)
438 ptree = cfg_tree_history;
440 while (ptree != NULL) {
442 free_config_tree(ptree);
453 #if defined(_MSC_VER) && defined (_DEBUG)
457 if (ptree->source.value.s != NULL)
458 free(ptree->source.value.s);
460 free_config_other_modes(ptree);
461 free_config_auth(ptree);
462 free_config_tos(ptree);
463 free_config_monitor(ptree);
464 free_config_access(ptree);
465 free_config_tinker(ptree);
466 free_config_rlimit(ptree);
467 free_config_system_opts(ptree);
468 free_config_logconfig(ptree);
469 free_config_phone(ptree);
470 free_config_setvar(ptree);
471 free_config_ttl(ptree);
472 free_config_trap(ptree);
473 free_config_fudge(ptree);
474 free_config_vars(ptree);
475 free_config_peers(ptree);
476 free_config_unpeers(ptree);
477 free_config_nic_rules(ptree);
478 free_config_reset_counters(ptree);
480 free_config_sim(ptree);
482 free_auth_node(ptree);
486 #if defined(_MSC_VER) && defined (_DEBUG)
490 #endif /* FREE_CFG_T */
496 dump_all_config_trees(
501 config_tree * cfg_ptr;
505 for (cfg_ptr = cfg_tree_history;
507 cfg_ptr = cfg_ptr->link)
508 return_value |= dump_config_tree(cfg_ptr, df, comment);
514 /* The config dumper */
523 unpeer_node *unpeern;
526 address_node *peer_addr;
527 address_node *fudge_addr;
528 filegen_node *fgen_node;
529 restrict_node *rest_node;
530 addr_opts_node *addr_opts;
531 setvar_node *setv_node;
532 nic_rule_node *rule_node;
534 int_node *flag_tok_fifo;
535 int_node *counter_set;
536 string_node *str_node;
538 const char *s = NULL;
544 DPRINTF(1, ("dump_config_tree(%p)\n", ptree));
547 if (!strftime(timestamp, sizeof(timestamp),
549 localtime(&ptree->timestamp)))
552 fprintf(df, "# %s %s %s\n",
554 (CONF_SOURCE_NTPQ == ptree->source.attr)
555 ? "ntpq remote config from"
556 : "startup configuration file",
557 ptree->source.value.s);
561 * For options without documentation we just output the name
564 atrv = HEAD_PFIFO(ptree->vars);
565 for ( ; atrv != NULL; atrv = atrv->link) {
566 switch (atrv->type) {
569 fprintf(df, "\n# dump error:\n"
570 "# unknown vars type %d (%s) for %s\n",
571 atrv->type, token_name(atrv->type),
572 token_name(atrv->attr));
576 fprintf(df, "%s %s\n", keyword(atrv->attr),
577 normal_dtoa(atrv->value.d));
581 fprintf(df, "%s %d\n", keyword(atrv->attr),
586 fprintf(df, "%s \"%s\"", keyword(atrv->attr),
588 if (T_Driftfile == atrv->attr &&
589 atrv->link != NULL &&
590 T_WanderThreshold == atrv->link->attr) {
593 normal_dtoa(atrv->value.d));
601 atrv = HEAD_PFIFO(ptree->logconfig);
603 fprintf(df, "logconfig");
604 for ( ; atrv != NULL; atrv = atrv->link)
605 fprintf(df, " %c%s", atrv->attr, atrv->value.s);
609 if (ptree->stats_dir)
610 fprintf(df, "statsdir \"%s\"\n", ptree->stats_dir);
612 i_n = HEAD_PFIFO(ptree->stats_list);
614 fprintf(df, "statistics");
615 for ( ; i_n != NULL; i_n = i_n->link)
616 fprintf(df, " %s", keyword(i_n->i));
620 fgen_node = HEAD_PFIFO(ptree->filegen_opts);
621 for ( ; fgen_node != NULL; fgen_node = fgen_node->link) {
622 atrv = HEAD_PFIFO(fgen_node->options);
624 fprintf(df, "filegen %s",
625 keyword(fgen_node->filegen_token));
626 for ( ; atrv != NULL; atrv = atrv->link) {
627 switch (atrv->attr) {
630 fprintf(df, "\n# dump error:\n"
631 "# unknown filegen option token %s\n"
633 token_name(atrv->attr),
634 keyword(fgen_node->filegen_token));
638 fprintf(df, " file %s",
643 fprintf(df, " type %s",
644 keyword(atrv->value.i));
649 keyword(atrv->value.i));
657 atrv = HEAD_PFIFO(ptree->auth.crypto_cmd_list);
659 fprintf(df, "crypto");
660 for ( ; atrv != NULL; atrv = atrv->link) {
661 fprintf(df, " %s %s", keyword(atrv->attr),
667 if (ptree->auth.revoke != 0)
668 fprintf(df, "revoke %d\n", ptree->auth.revoke);
670 if (ptree->auth.keysdir != NULL)
671 fprintf(df, "keysdir \"%s\"\n", ptree->auth.keysdir);
673 if (ptree->auth.keys != NULL)
674 fprintf(df, "keys \"%s\"\n", ptree->auth.keys);
676 atrv = HEAD_PFIFO(ptree->auth.trusted_key_list);
678 fprintf(df, "trustedkey");
679 for ( ; atrv != NULL; atrv = atrv->link) {
680 if (T_Integer == atrv->type)
681 fprintf(df, " %d", atrv->value.i);
682 else if (T_Intrange == atrv->type)
683 fprintf(df, " (%d ... %d)",
688 fprintf(df, "\n# dump error:\n"
689 "# unknown trustedkey attr type %d\n"
690 "trustedkey", atrv->type);
696 if (ptree->auth.control_key)
697 fprintf(df, "controlkey %d\n", ptree->auth.control_key);
699 if (ptree->auth.request_key)
700 fprintf(df, "requestkey %d\n", ptree->auth.request_key);
702 /* dump enable list, then disable list */
703 for (enable = 1; enable >= 0; enable--) {
705 ? HEAD_PFIFO(ptree->enable_opts)
706 : HEAD_PFIFO(ptree->disable_opts);
708 fprintf(df, "%s", (enable)
711 for ( ; atrv != NULL; atrv = atrv->link)
713 keyword(atrv->value.i));
718 atrv = HEAD_PFIFO(ptree->orphan_cmds);
721 for ( ; atrv != NULL; atrv = atrv->link) {
722 switch (atrv->type) {
725 fprintf(df, "\n# dump error:\n"
726 "# unknown tos attr type %d %s\n"
728 token_name(atrv->type));
732 if (atrv->attr == T_Basedate) {
734 ntpcal_rd_to_date(&jd, atrv->value.i + DAY_NTP_STARTS);
735 fprintf(df, " %s \"%04hu-%02hu-%02hu\"",
736 keyword(atrv->attr), jd.year,
738 (u_short)jd.monthday);
740 fprintf(df, " %s %d",
747 fprintf(df, " %s %s",
749 normal_dtoa(atrv->value.d));
756 atrv = HEAD_PFIFO(ptree->rlimit);
758 fprintf(df, "rlimit");
759 for ( ; atrv != NULL; atrv = atrv->link) {
760 INSIST(T_Integer == atrv->type);
761 fprintf(df, " %s %d", keyword(atrv->attr),
767 atrv = HEAD_PFIFO(ptree->tinker);
769 fprintf(df, "tinker");
770 for ( ; atrv != NULL; atrv = atrv->link) {
771 INSIST(T_Double == atrv->type);
772 fprintf(df, " %s %s", keyword(atrv->attr),
773 normal_dtoa(atrv->value.d));
778 if (ptree->broadcastclient)
779 fprintf(df, "broadcastclient\n");
781 peern = HEAD_PFIFO(ptree->peers);
782 for ( ; peern != NULL; peern = peern->link) {
784 fprintf(df, "%s", keyword(peern->host_mode));
785 switch (addr->type) {
788 fprintf(df, "# dump error:\n"
789 "# unknown peer family %d for:\n"
791 keyword(peern->host_mode));
805 fprintf(df, " %s", addr->address);
807 if (peern->minpoll != 0)
808 fprintf(df, " minpoll %u", peern->minpoll);
810 if (peern->maxpoll != 0)
811 fprintf(df, " maxpoll %u", peern->maxpoll);
813 if (peern->ttl != 0) {
814 if (strlen(addr->address) > 8
815 && !memcmp(addr->address, "127.127.", 8))
816 fprintf(df, " mode %u", peern->ttl);
818 fprintf(df, " ttl %u", peern->ttl);
821 if (peern->peerversion != NTP_VERSION)
822 fprintf(df, " version %u", peern->peerversion);
824 if (peern->peerkey != 0)
825 fprintf(df, " key %u", peern->peerkey);
827 if (peern->group != NULL)
828 fprintf(df, " ident \"%s\"", peern->group);
830 atrv = HEAD_PFIFO(peern->peerflags);
831 for ( ; atrv != NULL; atrv = atrv->link) {
832 INSIST(T_Flag == atrv->attr);
833 INSIST(T_Integer == atrv->type);
834 fprintf(df, " %s", keyword(atrv->value.i));
839 addr_opts = HEAD_PFIFO(ptree->fudge);
840 for ( ; addr_opts != NULL; addr_opts = addr_opts->link) {
841 peer_addr = peern->addr;
842 fudge_addr = addr_opts->addr;
844 s1 = peer_addr->address;
845 s2 = fudge_addr->address;
850 fprintf(df, "fudge %s", s1);
852 for (atrv = HEAD_PFIFO(addr_opts->options);
856 switch (atrv->type) {
859 fprintf(df, "\n# dump error:\n"
860 "# unknown fudge atrv->type %d\n"
861 "fudge %s", atrv->type,
866 fprintf(df, " %s %s",
868 normal_dtoa(atrv->value.d));
872 fprintf(df, " %s %d",
878 fprintf(df, " %s %s",
888 addr = HEAD_PFIFO(ptree->manycastserver);
890 fprintf(df, "manycastserver");
891 for ( ; addr != NULL; addr = addr->link)
892 fprintf(df, " %s", addr->address);
896 addr = HEAD_PFIFO(ptree->multicastclient);
898 fprintf(df, "multicastclient");
899 for ( ; addr != NULL; addr = addr->link)
900 fprintf(df, " %s", addr->address);
905 for (unpeern = HEAD_PFIFO(ptree->unpeers);
907 unpeern = unpeern->link)
908 fprintf(df, "unpeer %s\n", unpeern->addr->address);
910 atrv = HEAD_PFIFO(ptree->mru_opts);
913 for ( ; atrv != NULL; atrv = atrv->link)
914 fprintf(df, " %s %d", keyword(atrv->attr),
919 atrv = HEAD_PFIFO(ptree->discard_opts);
921 fprintf(df, "discard");
922 for ( ; atrv != NULL; atrv = atrv->link)
923 fprintf(df, " %s %d", keyword(atrv->attr),
928 for (rest_node = HEAD_PFIFO(ptree->restrict_opts);
930 rest_node = rest_node->link) {
933 if (NULL == rest_node->addr) {
935 /* Don't need to set is_default=1 here */
936 flag_tok_fifo = HEAD_PFIFO(rest_node->flag_tok_fifo);
937 for ( ; flag_tok_fifo != NULL; flag_tok_fifo = flag_tok_fifo->link) {
938 if (T_Source == flag_tok_fifo->i) {
944 const char *ap = rest_node->addr->address;
948 mp = rest_node->mask->address;
950 if ( rest_node->addr->type == AF_INET
951 && !strcmp(ap, "0.0.0.0")
952 && !strcmp(mp, "0.0.0.0")) {
955 } else if ( rest_node->mask
956 && rest_node->mask->type == AF_INET6
958 && !strcmp(mp, "::")) {
965 fprintf(df, "restrict %s", s);
966 if (rest_node->mask != NULL && !is_default)
967 fprintf(df, " mask %s",
968 rest_node->mask->address);
969 fprintf(df, " ippeerlimit %d", rest_node->ippeerlimit);
970 flag_tok_fifo = HEAD_PFIFO(rest_node->flag_tok_fifo);
971 for ( ; flag_tok_fifo != NULL; flag_tok_fifo = flag_tok_fifo->link)
972 if (T_Source != flag_tok_fifo->i)
973 fprintf(df, " %s", keyword(flag_tok_fifo->i));
977 rule_node = HEAD_PFIFO(ptree->nic_rules);
978 for ( ; rule_node != NULL; rule_node = rule_node->link) {
979 fprintf(df, "interface %s %s\n",
980 keyword(rule_node->action),
981 (rule_node->match_class)
982 ? keyword(rule_node->match_class)
983 : rule_node->if_name);
986 str_node = HEAD_PFIFO(ptree->phone);
987 if (str_node != NULL) {
988 fprintf(df, "phone");
989 for ( ; str_node != NULL; str_node = str_node->link)
990 fprintf(df, " \"%s\"", str_node->s);
994 setv_node = HEAD_PFIFO(ptree->setvar);
995 for ( ; setv_node != NULL; setv_node = setv_node->link) {
996 s1 = quote_if_needed(setv_node->var);
997 s2 = quote_if_needed(setv_node->val);
998 fprintf(df, "setvar %s = %s", s1, s2);
1001 if (setv_node->isdefault)
1002 fprintf(df, " default");
1006 i_n = HEAD_PFIFO(ptree->ttl);
1009 for( ; i_n != NULL; i_n = i_n->link)
1010 fprintf(df, " %d", i_n->i);
1014 addr_opts = HEAD_PFIFO(ptree->trap);
1015 for ( ; addr_opts != NULL; addr_opts = addr_opts->link) {
1016 addr = addr_opts->addr;
1017 fprintf(df, "trap %s", addr->address);
1018 atrv = HEAD_PFIFO(addr_opts->options);
1019 for ( ; atrv != NULL; atrv = atrv->link) {
1020 switch (atrv->attr) {
1023 fprintf(df, "\n# dump error:\n"
1024 "# unknown trap token %d\n"
1025 "trap %s", atrv->attr,
1030 fprintf(df, " port %d", atrv->value.i);
1034 fprintf(df, " interface %s",
1042 counter_set = HEAD_PFIFO(ptree->reset_counters);
1043 if (counter_set != NULL) {
1044 fprintf(df, "reset");
1045 for ( ; counter_set != NULL;
1046 counter_set = counter_set->link)
1047 fprintf(df, " %s", keyword(counter_set->i));
1053 #endif /* SAVECONFIG */
1057 /* generic fifo routines for structs linked by 1st member */
1070 pf = emalloc_zero(sizeof(*pf));
1072 CHECK_FIFO_CONSISTENCY(*pf);
1074 LINK_FIFO(*pf, pe, link);
1075 CHECK_FIFO_CONSISTENCY(*pf);
1097 CONCAT_FIFO(*pf1, *pf2, link);
1109 any_node * np = NULL;
1110 any_node_fifo * pf1 = fifo;
1116 UNLINK_FIFO(np, *pf1, link);
1126 /* FUNCTIONS FOR CREATING NODES ON THE SYNTAX TREE
1127 * -----------------------------------------------
1136 if (T_String == av->type)
1150 my_val = emalloc_zero(sizeof(*my_val));
1151 my_val->attr = attr;
1152 my_val->value.d = value;
1153 my_val->type = T_Double;
1167 my_val = emalloc_zero(sizeof(*my_val));
1168 my_val->attr = attr;
1169 my_val->value.i = value;
1170 my_val->type = T_Integer;
1184 my_val = emalloc_zero(sizeof(*my_val));
1185 my_val->attr = attr;
1186 my_val->value.u = value;
1187 my_val->type = T_U_int;
1194 create_attr_rangeval(
1202 my_val = emalloc_zero(sizeof(*my_val));
1203 my_val->attr = attr;
1204 my_val->value.r.first = first;
1205 my_val->value.r.last = last;
1206 my_val->type = T_Intrange;
1220 my_val = emalloc_zero(sizeof(*my_val));
1221 my_val->attr = attr;
1222 if (NULL == s) /* free() hates NULL */
1224 my_val->value.s = _UC(s);
1225 my_val->type = T_String;
1238 i_n = emalloc_zero(sizeof(*i_n));
1252 sn = emalloc_zero(sizeof(*sn));
1260 create_address_node(
1265 address_node *my_node;
1267 REQUIRE(NULL != addr);
1268 REQUIRE(AF_INET == type || AF_INET6 == type || AF_UNSPEC == type);
1269 my_node = emalloc_zero(sizeof(*my_node));
1270 my_node->address = addr;
1271 my_node->type = (u_short)type;
1278 destroy_address_node(
1279 address_node *my_node
1282 if (NULL == my_node)
1284 REQUIRE(NULL != my_node->address);
1286 free(my_node->address);
1294 address_node * addr,
1295 attr_val_fifo * options
1303 my_node = emalloc_zero(sizeof(*my_node));
1305 /* Initialize node values to default */
1306 my_node->peerversion = NTP_VERSION;
1308 /* Now set the node to the read values */
1309 my_node->host_mode = hmode;
1310 my_node->addr = addr;
1313 * the options FIFO mixes items that will be saved in the
1314 * peer_node as explicit members, such as minpoll, and
1315 * those that are moved intact to the peer_node's peerflags
1316 * FIFO. The options FIFO is consumed and reclaimed here.
1319 if (options != NULL)
1320 CHECK_FIFO_CONSISTENCY(*options);
1321 while (options != NULL) {
1322 UNLINK_FIFO(option, *options, link);
1323 if (NULL == option) {
1329 /* Check the kind of option being set */
1330 switch (option->attr) {
1333 APPEND_G_FIFO(my_node->peerflags, option);
1338 if (option->value.i < NTP_MINPOLL ||
1339 option->value.i > UCHAR_MAX) {
1341 "minpoll: provided value (%d) is out of range [%d-%d])",
1342 option->value.i, NTP_MINPOLL,
1344 my_node->minpoll = NTP_MINPOLL;
1347 (u_char)option->value.u;
1352 if (option->value.i < 0 ||
1353 option->value.i > NTP_MAXPOLL) {
1355 "maxpoll: provided value (%d) is out of range [0-%d])",
1356 option->value.i, NTP_MAXPOLL);
1357 my_node->maxpoll = NTP_MAXPOLL;
1360 (u_char)option->value.u;
1365 if (is_refclk_addr(addr)) {
1366 msyslog(LOG_ERR, "'ttl' does not apply for refclocks");
1368 } else if (option->value.u >= MAX_TTL) {
1369 msyslog(LOG_ERR, "ttl: invalid argument");
1372 my_node->ttl = (u_char)option->value.u;
1377 if (is_refclk_addr(addr)) {
1378 my_node->ttl = option->value.u;
1380 msyslog(LOG_ERR, "'mode' does not apply for network peers");
1386 if (option->value.u >= KEYID_T_MAX) {
1387 msyslog(LOG_ERR, "key: invalid argument");
1391 (keyid_t)option->value.u;
1396 if (option->value.u >= UCHAR_MAX) {
1397 msyslog(LOG_ERR, "version: invalid argument");
1400 my_node->peerversion =
1401 (u_char)option->value.u;
1406 my_node->group = option->value.s;
1411 "Unknown peer/server option token %s",
1412 token_name(option->attr));
1419 /* Check if errors were reported. If yes, ignore the node */
1434 unpeer_node * my_node;
1438 my_node = emalloc_zero(sizeof(*my_node));
1441 * From the parser's perspective an association ID fits into
1442 * its generic T_String definition of a name/address "address".
1443 * We treat all valid 16-bit numbers as association IDs.
1445 for (u = 0, pch = (u_char*)addr->address; isdigit(*pch); ++pch) {
1446 /* accumulate with overflow retention */
1447 u = (10 * u + *pch - '0') | (u & 0xFF000000u);
1450 if (!*pch && u <= ASSOCID_MAX) {
1451 my_node->assocID = (associd_t)u;
1452 my_node->addr = NULL;
1453 destroy_address_node(addr);
1455 my_node->assocID = 0;
1456 my_node->addr = addr;
1463 create_filegen_node(
1465 attr_val_fifo * options
1468 filegen_node *my_node;
1470 my_node = emalloc_zero(sizeof(*my_node));
1471 my_node->filegen_token = filegen_token;
1472 my_node->options = options;
1479 create_restrict_node(
1480 address_node * addr,
1481 address_node * mask,
1483 int_fifo * flag_tok_fifo,
1487 restrict_node *my_node;
1489 my_node = emalloc_zero(sizeof(*my_node));
1490 my_node->addr = addr;
1491 my_node->mask = mask;
1492 my_node->ippeerlimit = ippeerlimit;
1493 my_node->flag_tok_fifo = flag_tok_fifo;
1494 my_node->line_no = line_no;
1501 destroy_restrict_node(
1502 restrict_node *my_node
1505 /* With great care, free all the memory occupied by
1508 destroy_address_node(my_node->addr);
1509 destroy_address_node(my_node->mask);
1510 destroy_int_fifo(my_node->flag_tok_fifo);
1524 UNLINK_FIFO(i_n, *fifo, link);
1535 destroy_string_fifo(
1543 UNLINK_FIFO(sn, *fifo, link);
1555 destroy_attr_val_fifo(
1556 attr_val_fifo * av_fifo
1561 if (av_fifo != NULL) {
1563 UNLINK_FIFO(av, *av_fifo, link);
1566 destroy_attr_val(av);
1574 destroy_filegen_fifo(
1582 UNLINK_FIFO(fg, *fifo, link);
1585 destroy_attr_val_fifo(fg->options);
1594 destroy_restrict_fifo(
1595 restrict_fifo * fifo
1602 UNLINK_FIFO(rn, *fifo, link);
1605 destroy_restrict_node(rn);
1613 destroy_setvar_fifo(
1621 UNLINK_FIFO(sv, *fifo, link);
1634 destroy_addr_opts_fifo(
1635 addr_opts_fifo * fifo
1638 addr_opts_node * aon;
1642 UNLINK_FIFO(aon, *fifo, link);
1645 destroy_address_node(aon->addr);
1646 destroy_attr_val_fifo(aon->options);
1661 setvar_node * my_node;
1664 /* do not allow = in the variable name */
1665 pch = strchr(var, '=');
1669 /* Now store the string into a setvar_node */
1670 my_node = emalloc_zero(sizeof(*my_node));
1673 my_node->isdefault = isdefault;
1680 create_nic_rule_node(
1682 char *if_name, /* interface name or numeric address */
1686 nic_rule_node *my_node;
1688 REQUIRE(match_class != 0 || if_name != NULL);
1690 my_node = emalloc_zero(sizeof(*my_node));
1691 my_node->match_class = match_class;
1692 my_node->if_name = if_name;
1693 my_node->action = action;
1700 create_addr_opts_node(
1701 address_node * addr,
1702 attr_val_fifo * options
1705 addr_opts_node *my_node;
1707 my_node = emalloc_zero(sizeof(*my_node));
1708 my_node->addr = addr;
1709 my_node->options = options;
1717 create_sim_script_info(
1719 attr_val_fifo * script_queue
1722 script_info *my_info;
1723 attr_val *my_attr_val;
1725 my_info = emalloc_zero(sizeof(*my_info));
1727 /* Initialize Script Info with default values*/
1728 my_info->duration = duration;
1729 my_info->prop_delay = NET_DLY;
1730 my_info->proc_delay = PROC_DLY;
1732 /* Traverse the script_queue and fill out non-default values */
1734 for (my_attr_val = HEAD_PFIFO(script_queue);
1735 my_attr_val != NULL;
1736 my_attr_val = my_attr_val->link) {
1738 /* Set the desired value */
1739 switch (my_attr_val->attr) {
1742 my_info->freq_offset = my_attr_val->value.d;
1746 my_info->wander = my_attr_val->value.d;
1750 my_info->jitter = my_attr_val->value.d;
1754 my_info->prop_delay = my_attr_val->value.d;
1758 my_info->proc_delay = my_attr_val->value.d;
1762 msyslog(LOG_ERR, "Unknown script token %d",
1778 const char addr_prefix[] = "192.168.0.";
1779 static int curr_addr_num = 1;
1780 #define ADDR_LENGTH 16 + 1 /* room for 192.168.1.255 */
1781 char addr_string[ADDR_LENGTH];
1782 sockaddr_u *final_addr;
1783 struct addrinfo *ptr;
1786 final_addr = emalloc(sizeof(*final_addr));
1788 if (addr->type == T_String) {
1789 snprintf(addr_string, sizeof(addr_string), "%s%d",
1790 addr_prefix, curr_addr_num++);
1791 printf("Selecting ip address %s for hostname %s\n",
1792 addr_string, addr->address);
1793 gai_err = getaddrinfo(addr_string, "ntp", NULL, &ptr);
1795 gai_err = getaddrinfo(addr->address, "ntp", NULL, &ptr);
1799 fprintf(stderr, "ERROR!! Could not get a new address\n");
1802 memcpy(final_addr, ptr->ai_addr, ptr->ai_addrlen);
1803 fprintf(stderr, "Successful in setting ip address of simulated server to: %s\n",
1815 address_node * addr,
1816 double server_offset,
1817 script_info_fifo * script
1820 server_info *my_info;
1822 my_info = emalloc_zero(sizeof(*my_info));
1823 my_info->server_time = server_offset;
1824 my_info->addr = get_next_address(addr);
1825 my_info->script = script;
1826 UNLINK_FIFO(my_info->curr_script, *my_info->script, link);
1834 attr_val_fifo * init_opts,
1835 server_info_fifo * servers
1840 my_node = emalloc(sizeof(*my_node));
1841 my_node->init_opts = init_opts;
1842 my_node->servers = servers;
1850 /* FUNCTIONS FOR PERFORMING THE CONFIGURATION
1851 * ------------------------------------------
1860 sockaddr_u addr_sock;
1861 address_node * addr_node;
1863 if (ptree->broadcastclient)
1864 proto_config(PROTO_BROADCLIENT, ptree->broadcastclient,
1867 addr_node = HEAD_PFIFO(ptree->manycastserver);
1868 while (addr_node != NULL) {
1869 ZERO_SOCK(&addr_sock);
1870 AF(&addr_sock) = addr_node->type;
1871 if (1 == getnetnum(addr_node->address, &addr_sock, 1,
1873 proto_config(PROTO_MULTICAST_ADD,
1875 sys_manycastserver = 1;
1877 addr_node = addr_node->link;
1880 /* Configure the multicast clients */
1881 addr_node = HEAD_PFIFO(ptree->multicastclient);
1882 if (addr_node != NULL) {
1884 ZERO_SOCK(&addr_sock);
1885 AF(&addr_sock) = addr_node->type;
1886 if (1 == getnetnum(addr_node->address,
1887 &addr_sock, 1, t_UNK)) {
1888 proto_config(PROTO_MULTICAST_ADD, 0, 0.,
1891 addr_node = addr_node->link;
1892 } while (addr_node != NULL);
1893 proto_config(PROTO_MULTICAST_ADD, 1, 0., NULL);
1901 destroy_address_fifo(
1902 address_fifo * pfifo
1905 address_node * addr_node;
1907 if (pfifo != NULL) {
1909 UNLINK_FIFO(addr_node, *pfifo, link);
1910 if (addr_node == NULL)
1912 destroy_address_node(addr_node);
1920 free_config_other_modes(
1924 FREE_ADDRESS_FIFO(ptree->manycastserver);
1925 FREE_ADDRESS_FIFO(ptree->multicastclient);
1927 #endif /* FREE_CFG_T */
1945 /* Crypto Command */
1947 my_val = HEAD_PFIFO(ptree->auth.crypto_cmd_list);
1948 for (; my_val != NULL; my_val = my_val->link) {
1949 switch (my_val->attr) {
1952 fatal_error("config_auth: attr-token=%d", my_val->attr);
1955 item = CRYPTO_CONF_PRIV;
1959 item = CRYPTO_CONF_IDENT;
1963 item = CRYPTO_CONF_PW;
1967 item = CRYPTO_CONF_RAND;
1971 item = CRYPTO_CONF_NID;
1974 crypto_config(item, my_val->value.s);
1976 #endif /* AUTOKEY */
1978 /* Keysdir Command */
1979 if (ptree->auth.keysdir) {
1980 if (keysdir != default_keysdir)
1982 keysdir = estrdup(ptree->auth.keysdir);
1986 /* ntp_signd_socket Command */
1987 if (ptree->auth.ntp_signd_socket) {
1988 if (ntp_signd_socket != default_ntp_signd_socket)
1989 free(ntp_signd_socket);
1990 ntp_signd_socket = estrdup(ptree->auth.ntp_signd_socket);
1994 if (ptree->auth.cryptosw && !cryptosw) {
1998 #endif /* AUTOKEY */
2001 * Count the number of trusted keys to preallocate storage and
2002 * size the hash table.
2005 my_val = HEAD_PFIFO(ptree->auth.trusted_key_list);
2006 for (; my_val != NULL; my_val = my_val->link) {
2007 if (T_Integer == my_val->type) {
2008 first = my_val->value.i;
2009 if (first > 1 && first <= NTP_MAXKEY)
2012 REQUIRE(T_Intrange == my_val->type);
2013 first = my_val->value.r.first;
2014 last = my_val->value.r.last;
2015 if (!(first > last || first < 1 ||
2016 last > NTP_MAXKEY)) {
2017 count += 1 + last - first;
2021 auth_prealloc_symkeys(count);
2024 if (ptree->auth.keys)
2025 getauthkeys(ptree->auth.keys);
2027 /* Control Key Command */
2028 if (ptree->auth.control_key)
2029 ctl_auth_keyid = (keyid_t)ptree->auth.control_key;
2031 /* Requested Key Command */
2032 if (ptree->auth.request_key) {
2033 DPRINTF(4, ("set info_auth_keyid to %08lx\n",
2034 (u_long) ptree->auth.request_key));
2035 info_auth_keyid = (keyid_t)ptree->auth.request_key;
2038 /* Trusted Key Command */
2039 my_val = HEAD_PFIFO(ptree->auth.trusted_key_list);
2040 for (; my_val != NULL; my_val = my_val->link) {
2041 if (T_Integer == my_val->type) {
2042 first = my_val->value.i;
2043 if (first >= 1 && first <= NTP_MAXKEY) {
2044 authtrust(first, TRUE);
2047 "Ignoring invalid trustedkey %d, min 1 max %d.",
2051 first = my_val->value.r.first;
2052 last = my_val->value.r.last;
2053 if (first > last || first < 1 ||
2054 last > NTP_MAXKEY) {
2056 "Ignoring invalid trustedkey range %d ... %d, min 1 max %d.",
2057 first, last, NTP_MAXKEY);
2059 for (i = first; i <= last; i++) {
2067 /* crypto revoke command */
2068 if (ptree->auth.revoke)
2069 sys_revoke = 1UL << ptree->auth.revoke;
2070 #endif /* AUTOKEY */
2081 destroy_attr_val_fifo(ptree->auth.crypto_cmd_list);
2082 ptree->auth.crypto_cmd_list = NULL;
2083 destroy_attr_val_fifo(ptree->auth.trusted_key_list);
2084 ptree->auth.trusted_key_list = NULL;
2086 #endif /* FREE_CFG_T */
2089 /* Configure low-level clock-related parameters. Return TRUE if the
2090 * clock might need adjustment like era-checking after the call, FALSE
2102 tos = HEAD_PFIFO(ptree->orphan_cmds);
2103 for (; tos != NULL; tos = tos->link) {
2110 basedate_set_day(tos->value.i);
2127 /* [Bug 2896] For the daemon to work properly it is essential
2128 * that minsane < minclock <= maxclock.
2130 * If either constraint is violated, the daemon will be or might
2131 * become dysfunctional. Fixing the values is too fragile here,
2132 * since three variables with interdependecies are involved. We
2133 * just log an error but do not stop: This might be caused by
2134 * remote config, and it might be fixed by remote config, too.
2136 int l_maxclock = sys_maxclock;
2137 int l_minclock = sys_minclock;
2138 int l_minsane = sys_minsane;
2140 /* -*- phase one: inspect / sanitize the values */
2141 tos = HEAD_PFIFO(ptree->orphan_cmds);
2142 for (; tos != NULL; tos = tos->link) {
2143 /* not all attributes are doubles (any more), so loading
2144 * 'val' in all cases is not a good idea: It should be
2145 * done as needed in every case processed here.
2154 msyslog(LOG_WARNING,
2155 "Using maximum bcpollbstep ceiling %d, %d requested",
2158 } else if (val < 0) {
2159 msyslog(LOG_WARNING,
2160 "Using minimum bcpollbstep floor %d, %d requested",
2168 if (val > STRATUM_UNSPEC - 1) {
2169 msyslog(LOG_WARNING,
2170 "Using maximum tos ceiling %d, %d requested",
2171 STRATUM_UNSPEC - 1, (int)val);
2172 tos->value.d = STRATUM_UNSPEC - 1;
2173 } else if (val < 1) {
2174 msyslog(LOG_WARNING,
2175 "Using minimum tos floor %d, %d requested",
2183 if ((int)tos->value.d < 1)
2185 l_minclock = (int)tos->value.d;
2190 if ((int)tos->value.d < 1)
2192 l_maxclock = (int)tos->value.d;
2197 if ((int)tos->value.d < 1)
2199 l_minsane = (int)tos->value.d;
2204 if ( ! (l_minsane < l_minclock && l_minclock <= l_maxclock)) {
2206 "tos error: must have minsane (%d) < minclock (%d) <= maxclock (%d)"
2207 " - daemon will not operate properly!",
2208 l_minsane, l_minclock, l_maxclock);
2211 /* -*- phase two: forward the values to the protocol machinery */
2212 tos = HEAD_PFIFO(ptree->orphan_cmds);
2213 for (; tos != NULL; tos = tos->link) {
2217 fatal_error("config-tos: attr-token=%d", tos->attr);
2220 item = PROTO_BCPOLLBSTEP;
2224 item = PROTO_CEILING;
2232 item = PROTO_COHORT;
2236 item = PROTO_ORPHAN;
2240 item = PROTO_ORPHWAIT;
2244 item = PROTO_MINDISP;
2248 item = PROTO_MAXDIST;
2252 item = PROTO_MINCLOCK;
2256 item = PROTO_MAXCLOCK;
2260 item = PROTO_MINSANE;
2264 item = PROTO_BEACON;
2268 continue; /* SKIP proto-config for this! */
2270 proto_config(item, 0, tos->value.d, NULL);
2281 FREE_ATTR_VAL_FIFO(ptree->orphan_cmds);
2283 #endif /* FREE_CFG_T */
2291 int_node *pfilegen_token;
2292 const char *filegen_string;
2293 const char *filegen_file;
2295 filegen_node *my_node;
2300 /* Set the statistics directory */
2301 if (ptree->stats_dir)
2302 stats_config(STATS_STATSDIR, ptree->stats_dir);
2305 * Calling filegen_get is brain dead. Doing a string
2306 * comparison to find the relavant filegen structure is
2309 * Through the parser, we already know which filegen is
2310 * being specified. Hence, we should either store a
2311 * pointer to the specified structure in the syntax tree
2312 * or an index into a filegen array.
2314 * Need to change the filegen code to reflect the above.
2317 /* Turn on the specified statistics */
2318 pfilegen_token = HEAD_PFIFO(ptree->stats_list);
2319 for (; pfilegen_token != NULL; pfilegen_token = pfilegen_token->link) {
2320 filegen_string = keyword(pfilegen_token->i);
2321 filegen = filegen_get(filegen_string);
2322 if (NULL == filegen) {
2324 "stats %s unrecognized",
2328 DPRINTF(4, ("enabling filegen for %s statistics '%s%s'\n",
2329 filegen_string, filegen->dir,
2331 filegen_flag = filegen->flag;
2332 filegen_flag |= FGEN_FLAG_ENABLED;
2333 filegen_config(filegen, statsdir, filegen_string,
2334 filegen->type, filegen_flag);
2337 /* Configure the statistics with the options */
2338 my_node = HEAD_PFIFO(ptree->filegen_opts);
2339 for (; my_node != NULL; my_node = my_node->link) {
2340 filegen_string = keyword(my_node->filegen_token);
2341 filegen = filegen_get(filegen_string);
2342 if (NULL == filegen) {
2344 "filegen category '%s' unrecognized",
2348 filegen_file = filegen_string;
2350 /* Initialize the filegen variables to their pre-configuration states */
2351 filegen_flag = filegen->flag;
2352 filegen_type = filegen->type;
2354 /* "filegen ... enabled" is the default (when filegen is used) */
2355 filegen_flag |= FGEN_FLAG_ENABLED;
2357 my_opts = HEAD_PFIFO(my_node->options);
2358 for (; my_opts != NULL; my_opts = my_opts->link) {
2359 switch (my_opts->attr) {
2362 filegen_file = my_opts->value.s;
2366 switch (my_opts->value.i) {
2369 fatal_error("config-monitor: type-token=%d", my_opts->value.i);
2372 filegen_type = FILEGEN_NONE;
2376 filegen_type = FILEGEN_PID;
2380 filegen_type = FILEGEN_DAY;
2384 filegen_type = FILEGEN_WEEK;
2388 filegen_type = FILEGEN_MONTH;
2392 filegen_type = FILEGEN_YEAR;
2396 filegen_type = FILEGEN_AGE;
2402 switch (my_opts->value.i) {
2405 filegen_flag |= FGEN_FLAG_LINK;
2409 filegen_flag &= ~FGEN_FLAG_LINK;
2413 filegen_flag |= FGEN_FLAG_ENABLED;
2417 filegen_flag &= ~FGEN_FLAG_ENABLED;
2422 "Unknown filegen flag token %d",
2430 "Unknown filegen option token %d",
2435 filegen_config(filegen, statsdir, filegen_file,
2436 filegen_type, filegen_flag);
2443 free_config_monitor(
2447 if (ptree->stats_dir) {
2448 free(ptree->stats_dir);
2449 ptree->stats_dir = NULL;
2452 FREE_INT_FIFO(ptree->stats_list);
2453 FREE_FILEGEN_FIFO(ptree->filegen_opts);
2455 #endif /* FREE_CFG_T */
2464 static int warned_signd;
2466 restrict_node * my_node;
2467 int_node * curr_tok_fifo;
2470 struct addrinfo hints;
2471 struct addrinfo * ai_list;
2472 struct addrinfo * pai;
2474 int restrict_default;
2479 const char * signd_warning =
2480 #ifdef HAVE_NTP_SIGND
2481 "MS-SNTP signd operations currently block ntpd degrading service to all clients.";
2483 "mssntp restrict bit ignored, this ntpd was configured without --enable-ntp-signd.";
2486 /* Configure the mru options */
2487 my_opt = HEAD_PFIFO(ptree->mru_opts);
2488 for (; my_opt != NULL; my_opt = my_opt->link) {
2492 switch (my_opt->attr) {
2495 if (0 <= my_opt->value.i)
2496 mru_incalloc = my_opt->value.u;
2502 if (0 <= my_opt->value.i)
2503 mru_incalloc = (my_opt->value.u * 1024U)
2504 / sizeof(mon_entry);
2510 if (0 <= my_opt->value.i)
2511 mru_initalloc = my_opt->value.u;
2517 if (0 <= my_opt->value.i)
2518 mru_initalloc = (my_opt->value.u * 1024U)
2519 / sizeof(mon_entry);
2525 if (0 <= my_opt->value.i)
2526 mru_mindepth = my_opt->value.u;
2532 mru_maxage = my_opt->value.i;
2536 if (0 <= my_opt->value.i)
2537 mru_maxdepth = my_opt->value.u;
2539 mru_maxdepth = UINT_MAX;
2543 if (0 <= my_opt->value.i)
2544 mru_maxdepth = (my_opt->value.u * 1024U) /
2547 mru_maxdepth = UINT_MAX;
2552 "Unknown mru option %s (%d)",
2553 keyword(my_opt->attr), my_opt->attr);
2558 "mru %s %d out of range, ignored.",
2559 keyword(my_opt->attr), my_opt->value.i);
2562 /* Configure the discard options */
2563 my_opt = HEAD_PFIFO(ptree->discard_opts);
2564 for (; my_opt != NULL; my_opt = my_opt->link) {
2566 switch (my_opt->attr) {
2569 if (0 <= my_opt->value.i &&
2570 my_opt->value.i <= UCHAR_MAX)
2571 ntp_minpoll = (u_char)my_opt->value.u;
2574 "discard average %d out of range, ignored.",
2579 ntp_minpkt = my_opt->value.i;
2583 mon_age = my_opt->value.i;
2588 "Unknown discard option %s (%d)",
2589 keyword(my_opt->attr), my_opt->attr);
2594 /* Configure the restrict options */
2595 my_node = HEAD_PFIFO(ptree->restrict_opts);
2597 for (; my_node != NULL; my_node = my_node->link) {
2598 /* Grab the ippeerlmit */
2599 ippeerlimit = my_node->ippeerlimit;
2601 DPRINTF(1, ("config_access: top-level node %p: ippeerlimit %d\n", my_node, ippeerlimit));
2603 /* Parse the flags */
2607 curr_tok_fifo = HEAD_PFIFO(my_node->flag_tok_fifo);
2608 for (; curr_tok_fifo != NULL; curr_tok_fifo = curr_tok_fifo->link) {
2609 switch (curr_tok_fifo->i) {
2612 fatal_error("config_access: flag-type-token=%d", curr_tok_fifo->i);
2615 mflags |= RESM_NTPONLY;
2619 mflags |= RESM_SOURCE;
2623 rflags |= RES_FLAKE;
2627 rflags |= RES_IGNORE;
2635 rflags |= RES_MSSNTP;
2639 rflags |= RES_LIMITED;
2643 rflags |= RES_LPTRAP;
2647 rflags |= RES_NOMODIFY;
2651 rflags |= RES_NOMRULIST;
2655 rflags |= RES_NOEPEER;
2659 rflags |= RES_NOPEER;
2663 rflags |= RES_NOQUERY;
2667 rflags |= RES_DONTSERVE;
2671 rflags |= RES_NOTRAP;
2675 rflags |= RES_DONTTRUST;
2679 rflags |= RES_VERSION;
2684 if ((RES_MSSNTP & rflags) && !warned_signd) {
2686 fprintf(stderr, "%s\n", signd_warning);
2687 msyslog(LOG_WARNING, "%s", signd_warning);
2690 /* It would be swell if we could identify the line number */
2691 if ((RES_KOD & rflags) && !(RES_LIMITED & rflags)) {
2692 const char *kod_where = (my_node->addr)
2693 ? my_node->addr->address
2694 : (mflags & RESM_SOURCE)
2697 const char *kod_warn = "KOD does nothing without LIMITED.";
2699 fprintf(stderr, "restrict %s: %s\n", kod_where, kod_warn);
2700 msyslog(LOG_WARNING, "restrict %s: %s", kod_where, kod_warn);
2706 restrict_default = 0;
2708 if (NULL == my_node->addr) {
2710 if (!(RESM_SOURCE & mflags)) {
2712 * The user specified a default rule
2713 * without a -4 / -6 qualifier, add to
2716 restrict_default = 1;
2718 /* apply "restrict source ..." */
2719 DPRINTF(1, ("restrict source template ippeerlimit %d mflags %x rflags %x\n",
2720 ippeerlimit, mflags, rflags));
2721 hack_restrict(RESTRICT_FLAGS, NULL, NULL,
2722 ippeerlimit, mflags, rflags, 0);
2726 /* Resolve the specified address */
2727 AF(&addr) = (u_short)my_node->addr->type;
2729 if (getnetnum(my_node->addr->address,
2730 &addr, 1, t_UNK) != 1) {
2732 * Attempt a blocking lookup. This
2733 * is in violation of the nonblocking
2734 * design of ntpd's mainline code. The
2735 * alternative of running without the
2736 * restriction until the name resolved
2738 * Ideally some scheme could be used for
2739 * restrict directives in the startup
2740 * ntp.conf to delay starting up the
2741 * protocol machinery until after all
2742 * restrict hosts have been resolved.
2746 hints.ai_protocol = IPPROTO_UDP;
2747 hints.ai_socktype = SOCK_DGRAM;
2748 hints.ai_family = my_node->addr->type;
2749 rc = getaddrinfo(my_node->addr->address,
2754 "restrict: ignoring line %d, address/host '%s' unusable.",
2756 my_node->addr->address);
2759 INSIST(ai_list != NULL);
2761 INSIST(pai->ai_addr != NULL);
2762 INSIST(sizeof(addr) >=
2764 memcpy(&addr, pai->ai_addr,
2766 INSIST(AF_INET == AF(&addr) ||
2767 AF_INET6 == AF(&addr));
2770 SET_HOSTMASK(&mask, AF(&addr));
2772 /* Resolve the mask */
2773 if (my_node->mask) {
2775 AF(&mask) = my_node->mask->type;
2776 if (getnetnum(my_node->mask->address,
2777 &mask, 1, t_MSK) != 1) {
2779 "restrict: ignoring line %d, mask '%s' unusable.",
2781 my_node->mask->address);
2788 if (restrict_default) {
2789 AF(&addr) = AF_INET;
2790 AF(&mask) = AF_INET;
2791 hack_restrict(RESTRICT_FLAGS, &addr, &mask,
2792 ippeerlimit, mflags, rflags, 0);
2793 AF(&addr) = AF_INET6;
2794 AF(&mask) = AF_INET6;
2798 hack_restrict(RESTRICT_FLAGS, &addr, &mask,
2799 ippeerlimit, mflags, rflags, 0);
2801 NULL != (pai = pai->ai_next)) {
2802 INSIST(pai->ai_addr != NULL);
2803 INSIST(sizeof(addr) >=
2806 memcpy(&addr, pai->ai_addr,
2808 INSIST(AF_INET == AF(&addr) ||
2809 AF_INET6 == AF(&addr));
2810 SET_HOSTMASK(&mask, AF(&addr));
2812 } while (pai != NULL);
2814 if (ai_list != NULL)
2815 freeaddrinfo(ai_list);
2827 FREE_ATTR_VAL_FIFO(ptree->mru_opts);
2828 FREE_ATTR_VAL_FIFO(ptree->discard_opts);
2829 FREE_RESTRICT_FIFO(ptree->restrict_opts);
2831 #endif /* FREE_CFG_T */
2839 attr_val * rlimit_av;
2841 rlimit_av = HEAD_PFIFO(ptree->rlimit);
2842 for (; rlimit_av != NULL; rlimit_av = rlimit_av->link) {
2843 switch (rlimit_av->attr) {
2846 fatal_error("config-rlimit: value-token=%d", rlimit_av->attr);
2849 /* What if we HAVE_OPT(SAVECONFIGQUIT) ? */
2850 if (HAVE_OPT( SAVECONFIGQUIT )) {
2853 if (rlimit_av->value.i == -1) {
2854 # if defined(HAVE_MLOCKALL)
2855 if (cur_memlock != 0) {
2856 if (-1 == munlockall()) {
2857 msyslog(LOG_ERR, "munlockall() failed: %m");
2861 # endif /* HAVE_MLOCKALL */
2862 } else if (rlimit_av->value.i >= 0) {
2863 #if defined(RLIMIT_MEMLOCK)
2864 # if defined(HAVE_MLOCKALL)
2865 if (cur_memlock != 1) {
2866 if (-1 == mlockall(MCL_CURRENT|MCL_FUTURE)) {
2867 msyslog(LOG_ERR, "mlockall() failed: %m");
2870 # endif /* HAVE_MLOCKALL */
2871 ntp_rlimit(RLIMIT_MEMLOCK,
2872 (rlim_t)(rlimit_av->value.i * 1024 * 1024),
2877 /* STDERR as well would be fine... */
2878 msyslog(LOG_WARNING, "'rlimit memlock' specified but is not available on this system.");
2879 #endif /* RLIMIT_MEMLOCK */
2881 msyslog(LOG_WARNING, "'rlimit memlock' value of %d is unexpected!", rlimit_av->value.i);
2886 #if defined(RLIMIT_STACK)
2887 ntp_rlimit(RLIMIT_STACK,
2888 (rlim_t)(rlimit_av->value.i * 4096),
2892 /* STDERR as well would be fine... */
2893 msyslog(LOG_WARNING, "'rlimit stacksize' specified but is not available on this system.");
2894 #endif /* RLIMIT_STACK */
2898 #if defined(RLIMIT_NOFILE)
2899 ntp_rlimit(RLIMIT_NOFILE,
2900 (rlim_t)(rlimit_av->value.i),
2904 /* STDERR as well would be fine... */
2905 msyslog(LOG_WARNING, "'rlimit filenum' specified but is not available on this system.");
2906 #endif /* RLIMIT_NOFILE */
2922 tinker = HEAD_PFIFO(ptree->tinker);
2923 for (; tinker != NULL; tinker = tinker->link) {
2924 switch (tinker->attr) {
2927 fatal_error("config_tinker: attr-token=%d", tinker->attr);
2942 item = LOOP_HUFFPUFF;
2954 item = LOOP_MAX_BACK;
2958 item = LOOP_MAX_FWD;
2962 item = LOOP_MINSTEP;
2969 loop_config(item, tinker->value.d);
2980 FREE_ATTR_VAL_FIFO(ptree->rlimit);
2988 FREE_ATTR_VAL_FIFO(ptree->tinker);
2990 #endif /* FREE_CFG_T */
2994 * config_nic_rules - apply interface listen/ignore/drop items
3000 int/*BOOL*/ input_from_file
3003 nic_rule_node * curr_node;
3005 nic_rule_match match_type;
3006 nic_rule_action action;
3012 curr_node = HEAD_PFIFO(ptree->nic_rules);
3014 if (curr_node != NULL
3015 && (HAVE_OPT( NOVIRTUALIPS ) || HAVE_OPT( INTERFACE ))) {
3017 "interface/nic rules are not allowed with --interface (-I) or --novirtualips (-L)%s",
3018 (input_from_file) ? ", exiting" : "");
3019 if (input_from_file)
3025 for (; curr_node != NULL; curr_node = curr_node->link) {
3027 if_name = curr_node->if_name;
3028 if (if_name != NULL)
3029 if_name = estrdup(if_name);
3031 switch (curr_node->match_class) {
3034 fatal_error("config_nic_rules: match-class-token=%d", curr_node->match_class);
3038 * 0 is out of range for valid token T_...
3039 * and in a nic_rules_node indicates the
3040 * interface descriptor is either a name or
3041 * address, stored in if_name in either case.
3043 INSIST(if_name != NULL);
3044 pchSlash = strchr(if_name, '/');
3045 if (pchSlash != NULL)
3047 if (is_ip_address(if_name, AF_UNSPEC, &addr)) {
3048 match_type = MATCH_IFADDR;
3049 if (pchSlash != NULL
3050 && 1 == sscanf(pchSlash + 1, "%d",
3053 SIZEOF_INADDR(AF(&addr));
3054 prefixlen = max(-1, prefixlen);
3055 prefixlen = min(prefixlen,
3059 match_type = MATCH_IFNAME;
3060 if (pchSlash != NULL)
3066 match_type = MATCH_ALL;
3070 match_type = MATCH_IPV4;
3074 match_type = MATCH_IPV6;
3078 match_type = MATCH_WILDCARD;
3082 switch (curr_node->action) {
3085 fatal_error("config_nic_rules: action-token=%d", curr_node->action);
3088 action = ACTION_LISTEN;
3092 action = ACTION_IGNORE;
3096 action = ACTION_DROP;
3100 add_nic_rule(match_type, if_name, prefixlen,
3102 timer_interfacetimeout(current_time + 2);
3103 if (if_name != NULL)
3112 free_config_nic_rules(
3116 nic_rule_node *curr_node;
3118 if (ptree->nic_rules != NULL) {
3120 UNLINK_FIFO(curr_node, *ptree->nic_rules, link);
3121 if (NULL == curr_node)
3123 free(curr_node->if_name);
3126 free(ptree->nic_rules);
3127 ptree->nic_rules = NULL;
3130 #endif /* FREE_CFG_T */
3134 apply_enable_disable(
3135 attr_val_fifo * fifo,
3139 attr_val *curr_tok_fifo;
3141 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
3145 for (curr_tok_fifo = HEAD_PFIFO(fifo);
3146 curr_tok_fifo != NULL;
3147 curr_tok_fifo = curr_tok_fifo->link) {
3149 option = curr_tok_fifo->value.i;
3154 "can not apply enable/disable token %d, unknown",
3159 proto_config(PROTO_AUTHENTICATE, enable, 0., NULL);
3163 proto_config(PROTO_BROADCLIENT, enable, 0., NULL);
3167 proto_config(PROTO_CAL, enable, 0., NULL);
3171 proto_config(PROTO_KERNEL, enable, 0., NULL);
3175 proto_config(PROTO_MONITOR, enable, 0., NULL);
3179 proto_config(PROTO_MODE7, enable, 0., NULL);
3183 proto_config(PROTO_NTP, enable, 0., NULL);
3187 proto_config(PROTO_PCEDIGEST, enable, 0., NULL);
3191 proto_config(PROTO_FILEGEN, enable, 0., NULL);
3195 proto_config(PROTO_UECRYPTO, enable, 0., NULL);
3199 proto_config(PROTO_UECRYPTONAK, enable, 0., NULL);
3203 proto_config(PROTO_UEDIGEST, enable, 0., NULL);
3206 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
3209 while (pentry->token) {
3210 if (pentry->token == option)
3214 if (!pentry->token) {
3216 "compat token %d not in bc_list[]",
3220 pentry->enabled = enable;
3233 apply_enable_disable(ptree->enable_opts, 1);
3234 apply_enable_disable(ptree->disable_opts, 0);
3240 free_config_system_opts(
3244 FREE_ATTR_VAL_FIFO(ptree->enable_opts);
3245 FREE_ATTR_VAL_FIFO(ptree->disable_opts);
3247 #endif /* FREE_CFG_T */
3257 my_lc = HEAD_PFIFO(ptree->logconfig);
3258 for (; my_lc != NULL; my_lc = my_lc->link) {
3259 switch (my_lc->attr) {
3262 ntp_syslogmask |= get_logmask(my_lc->value.s);
3266 ntp_syslogmask &= ~get_logmask(my_lc->value.s);
3270 ntp_syslogmask = get_logmask(my_lc->value.s);
3273 fatal_error("config-logconfig: modifier='%c'", my_lc->attr);
3281 free_config_logconfig(
3285 FREE_ATTR_VAL_FIFO(ptree->logconfig);
3287 #endif /* FREE_CFG_T */
3300 sn = HEAD_PFIFO(ptree->phone);
3301 for (; sn != NULL; sn = sn->link) {
3302 /* need to leave array entry for NULL terminator */
3303 if (i < COUNTOF(sys_phone) - 1) {
3304 sys_phone[i++] = estrdup(sn->s);
3305 sys_phone[i] = NULL;
3308 "phone: Number of phone entries exceeds %zu. Ignoring phone %s...",
3309 (COUNTOF(sys_phone) - 1), sn->s);
3320 #ifdef HAVE_DNSREGISTRATION
3321 extern int mdnstries;
3322 mdnstries = ptree->mdnstries;
3323 #endif /* HAVE_DNSREGISTRATION */
3332 FREE_STRING_FIFO(ptree->phone);
3334 #endif /* FREE_CFG_T */
3343 setvar_node *my_node;
3344 size_t varlen, vallen, octets;
3348 my_node = HEAD_PFIFO(ptree->setvar);
3349 for (; my_node != NULL; my_node = my_node->link) {
3350 varlen = strlen(my_node->var);
3351 vallen = strlen(my_node->val);
3352 octets = varlen + vallen + 1 + 1;
3353 str = erealloc(str, octets);
3354 snprintf(str, octets, "%s=%s", my_node->var,
3356 set_sys_var(str, octets, (my_node->isdefault)
3372 FREE_SETVAR_FIFO(ptree->setvar);
3374 #endif /* FREE_CFG_T */
3386 curr_ttl = HEAD_PFIFO(ptree->ttl);
3387 for (; curr_ttl != NULL; curr_ttl = curr_ttl->link) {
3388 if (i < COUNTOF(sys_ttl))
3389 sys_ttl[i++] = (u_char)curr_ttl->i;
3392 "ttl: Number of TTL entries exceeds %zu. Ignoring TTL %d...",
3393 COUNTOF(sys_ttl), curr_ttl->i);
3395 sys_ttlmax = (i) ? (i - 1) : 0;
3406 FREE_INT_FIFO(ptree->ttl);
3408 #endif /* FREE_CFG_T */
3417 addr_opts_node *curr_trap;
3419 sockaddr_u addr_sock;
3420 sockaddr_u peeraddr;
3421 struct interface *localaddr;
3422 struct addrinfo hints;
3424 settrap_parms *pstp;
3429 /* silence warning about addr_sock potentially uninitialized */
3430 AF(&addr_sock) = AF_UNSPEC;
3432 curr_trap = HEAD_PFIFO(ptree->trap);
3433 for (; curr_trap != NULL; curr_trap = curr_trap->link) {
3438 curr_opt = HEAD_PFIFO(curr_trap->options);
3439 for (; curr_opt != NULL; curr_opt = curr_opt->link) {
3440 if (T_Port == curr_opt->attr) {
3441 if (curr_opt->value.i < 1
3442 || curr_opt->value.i > USHRT_MAX) {
3444 "invalid port number "
3449 port = (u_short)curr_opt->value.i;
3451 else if (T_Interface == curr_opt->attr) {
3452 /* Resolve the interface address */
3453 ZERO_SOCK(&addr_sock);
3454 if (getnetnum(curr_opt->value.s,
3455 &addr_sock, 1, t_UNK) != 1) {
3460 localaddr = findinterface(&addr_sock);
3462 if (NULL == localaddr) {
3464 "can't find interface with address %s",
3471 /* Now process the trap for the specified interface
3477 ZERO_SOCK(&peeraddr);
3478 rc = getnetnum(curr_trap->addr->address,
3479 &peeraddr, 1, t_UNK);
3483 "trap: unable to use IP address %s.",
3484 curr_trap->addr->address);
3485 #else /* WORKER follows */
3487 * save context and hand it off
3488 * for name resolution.
3491 hints.ai_protocol = IPPROTO_UDP;
3492 hints.ai_socktype = SOCK_DGRAM;
3493 snprintf(port_text, sizeof(port_text),
3495 hints.ai_flags = Z_AI_NUMERICSERV;
3496 pstp = emalloc_zero(sizeof(*pstp));
3497 if (localaddr != NULL) {
3498 hints.ai_family = localaddr->family;
3499 pstp->ifaddr_nonnull = 1;
3500 memcpy(&pstp->ifaddr,
3502 sizeof(pstp->ifaddr));
3504 rc = getaddrinfo_sometime(
3505 curr_trap->addr->address,
3508 &trap_name_resolved,
3512 "config_trap: getaddrinfo_sometime(%s,%s): %m",
3513 curr_trap->addr->address,
3518 /* port is at same location for v4 and v6 */
3519 SET_PORT(&peeraddr, port);
3521 if (NULL == localaddr)
3522 localaddr = ANY_INTERFACE_CHOOSE(&peeraddr);
3524 AF(&peeraddr) = AF(&addr_sock);
3526 if (!ctlsettrap(&peeraddr, localaddr, 0,
3529 "set trap %s -> %s failed.",
3538 * trap_name_resolved()
3540 * Callback invoked when config_trap()'s DNS lookup completes.
3549 const char * service,
3550 const struct addrinfo * hints,
3551 const struct addrinfo * res
3554 settrap_parms *pstp;
3555 struct interface *localaddr;
3556 sockaddr_u peeraddr;
3564 "giving up resolving trap host %s: %s (%d)",
3565 name, gai_strerror(rescode), rescode);
3569 INSIST(sizeof(peeraddr) >= res->ai_addrlen);
3571 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
3573 if (pstp->ifaddr_nonnull)
3574 localaddr = findinterface(&pstp->ifaddr);
3575 if (NULL == localaddr)
3576 localaddr = ANY_INTERFACE_CHOOSE(&peeraddr);
3577 if (!ctlsettrap(&peeraddr, localaddr, 0, NTP_VERSION))
3578 msyslog(LOG_ERR, "set trap %s -> %s failed.",
3579 latoa(localaddr), stoa(&peeraddr));
3582 # endif /* WORKER */
3592 FREE_ADDR_OPTS_FIFO(ptree->trap);
3594 #endif /* FREE_CFG_T */
3603 addr_opts_node *curr_fudge;
3605 sockaddr_u addr_sock;
3606 address_node *addr_node;
3607 struct refclockstat clock_stat;
3610 curr_fudge = HEAD_PFIFO(ptree->fudge);
3611 for (; curr_fudge != NULL; curr_fudge = curr_fudge->link) {
3614 /* Get the reference clock address and
3615 * ensure that it is sane
3617 addr_node = curr_fudge->addr;
3618 ZERO_SOCK(&addr_sock);
3619 if (getnetnum(addr_node->address, &addr_sock, 1, t_REF)
3623 "unrecognized fudge reference clock address %s, line ignored",
3627 if (!ISREFCLOCKADR(&addr_sock)) {
3630 "inappropriate address %s for the fudge command, line ignored",
3634 /* Parse all the options to the fudge command */
3636 curr_opt = HEAD_PFIFO(curr_fudge->options);
3637 for (; curr_opt != NULL; curr_opt = curr_opt->link) {
3638 switch (curr_opt->attr) {
3641 clock_stat.haveflags |= CLK_HAVETIME1;
3642 clock_stat.fudgetime1 = curr_opt->value.d;
3646 clock_stat.haveflags |= CLK_HAVETIME2;
3647 clock_stat.fudgetime2 = curr_opt->value.d;
3651 clock_stat.haveflags |= CLK_HAVEVAL1;
3652 clock_stat.fudgeval1 = curr_opt->value.i;
3656 clock_stat.haveflags |= CLK_HAVEVAL2;
3657 clock_stat.fudgeval2 = 0;
3658 memcpy(&clock_stat.fudgeval2,
3660 min(strlen(curr_opt->value.s), 4));
3664 clock_stat.haveflags |= CLK_HAVEFLAG1;
3665 if (curr_opt->value.i)
3666 clock_stat.flags |= CLK_FLAG1;
3668 clock_stat.flags &= ~CLK_FLAG1;
3672 clock_stat.haveflags |= CLK_HAVEFLAG2;
3673 if (curr_opt->value.i)
3674 clock_stat.flags |= CLK_FLAG2;
3676 clock_stat.flags &= ~CLK_FLAG2;
3680 clock_stat.haveflags |= CLK_HAVEFLAG3;
3681 if (curr_opt->value.i)
3682 clock_stat.flags |= CLK_FLAG3;
3684 clock_stat.flags &= ~CLK_FLAG3;
3688 clock_stat.haveflags |= CLK_HAVEFLAG4;
3689 if (curr_opt->value.i)
3690 clock_stat.flags |= CLK_FLAG4;
3692 clock_stat.flags &= ~CLK_FLAG4;
3697 "Unexpected fudge flag %s (%d) for %s",
3698 token_name(curr_opt->attr),
3699 curr_opt->attr, stoa(&addr_sock));
3700 exit(curr_opt->attr ? curr_opt->attr : 1);
3705 refclock_control(&addr_sock, &clock_stat, NULL);
3718 FREE_ADDR_OPTS_FIFO(ptree->fudge);
3720 #endif /* FREE_CFG_T */
3731 curr_var = HEAD_PFIFO(ptree->vars);
3732 for (; curr_var != NULL; curr_var = curr_var->link) {
3733 /* Determine which variable to set and set it */
3734 switch (curr_var->attr) {
3736 case T_Broadcastdelay:
3737 proto_config(PROTO_BROADDELAY, 0, curr_var->value.d, NULL);
3741 loop_config(LOOP_TICK, curr_var->value.d);
3745 if ('\0' == curr_var->value.s[0]) {
3746 stats_drift_file = 0;
3747 msyslog(LOG_INFO, "config: driftfile disabled");
3749 stats_config(STATS_FREQ_FILE, curr_var->value.s);
3753 /* DSCP is in the upper 6 bits of the IP TOS/DS field */
3754 qos = curr_var->value.i << 2;
3758 sys_ident = curr_var->value.s;
3761 case T_WanderThreshold: /* FALLTHROUGH */
3763 wander_threshold = curr_var->value.d;
3767 stats_config(STATS_LEAP_FILE, curr_var->value.s);
3771 case T_Leapsmearinterval:
3772 leap_smear_intv = curr_var->value.i;
3773 msyslog(LOG_INFO, "config: leap smear interval %i s", leap_smear_intv);
3778 stats_config(STATS_PID_FILE, curr_var->value.s);
3782 if (-1 == change_logfile(curr_var->value.s, TRUE))
3784 "Cannot open logfile %s: %m",
3788 case T_Saveconfigdir:
3789 if (saveconfigdir != NULL)
3790 free(saveconfigdir);
3791 len = strlen(curr_var->value.s);
3793 saveconfigdir = NULL;
3794 } else if (DIR_SEP != curr_var->value.s[len - 1]
3795 #ifdef SYS_WINNT /* slash is also a dir. sep. on Windows */
3796 && '/' != curr_var->value.s[len - 1]
3800 saveconfigdir = emalloc(len + 1);
3801 snprintf(saveconfigdir, len + 1,
3806 saveconfigdir = estrdup(
3813 sys_automax = curr_var->value.i;
3819 "config_vars(): unexpected token %d",
3832 FREE_ATTR_VAL_FIFO(ptree->vars);
3834 #endif /* FREE_CFG_T */
3837 /* Define a function to check if a resolved address is sane.
3838 * If yes, return 1, else return 0;
3841 is_sane_resolved_address(
3842 sockaddr_u * peeraddr,
3846 if (!ISREFCLOCKADR(peeraddr) && ISBADADR(peeraddr)) {
3848 "attempt to configure invalid address %s",
3853 * Shouldn't be able to specify multicast
3854 * address for server/peer!
3855 * and unicast address for manycastclient!
3857 if ((T_Server == hmode || T_Peer == hmode || T_Pool == hmode)
3858 && IS_MCAST(peeraddr)) {
3860 "attempt to configure invalid address %s",
3864 if (T_Manycastclient == hmode && !IS_MCAST(peeraddr)) {
3866 "attempt to configure invalid address %s",
3871 if (IS_IPV6(peeraddr) && !ipv6_works)
3874 /* Ok, all tests succeeded, now we can return 1 */
3881 get_correct_host_mode(
3889 case T_Manycastclient:
3896 return MODE_BROADCAST;
3905 * peerflag_bits() get config_peers() peerflags value from a
3906 * peer_node's queue of flag attr_val entries.
3916 /* translate peerflags options to bits */
3918 option = HEAD_PFIFO(pn->peerflags);
3919 for (; option != NULL; option = option->link) {
3920 switch (option->value.i) {
3923 fatal_error("peerflag_bits: option-token=%d", option->value.i);
3926 peerflags |= FLAG_SKEY;
3930 peerflags |= FLAG_BURST;
3934 peerflags |= FLAG_IBURST;
3938 peerflags |= FLAG_NOSELECT;
3942 peerflags |= FLAG_PREEMPT;
3946 peerflags |= FLAG_PREFER;
3950 peerflags |= FLAG_TRUE;
3954 peerflags |= FLAG_XLEAVE;
3968 sockaddr_u peeraddr;
3969 struct addrinfo hints;
3970 peer_node * curr_peer;
3971 peer_resolved_ctx * ctx;
3974 /* add servers named on the command line with iburst implied */
3976 cmdline_server_count > 0;
3977 cmdline_server_count--, cmdline_servers++) {
3979 ZERO_SOCK(&peeraddr);
3981 * If we have a numeric address, we can safely
3982 * proceed in the mainline with it. Otherwise, hand
3983 * the hostname off to the blocking child.
3985 * Note that if we're told to add the peer here, we
3986 * do that regardless of ippeerlimit.
3988 if (is_ip_address(*cmdline_servers, AF_UNSPEC,
3991 SET_PORT(&peeraddr, NTP_PORT);
3992 if (is_sane_resolved_address(&peeraddr,
4008 /* we have a hostname to resolve */
4010 ctx = emalloc_zero(sizeof(*ctx));
4011 ctx->family = AF_UNSPEC;
4012 ctx->host_mode = T_Server;
4013 ctx->hmode = MODE_CLIENT;
4014 ctx->version = NTP_VERSION;
4015 ctx->flags = FLAG_IBURST;
4018 hints.ai_family = (u_short)ctx->family;
4019 hints.ai_socktype = SOCK_DGRAM;
4020 hints.ai_protocol = IPPROTO_UDP;
4022 getaddrinfo_sometime_ex(*cmdline_servers,
4025 &peer_name_resolved,
4026 (void *)ctx, DNSFLAGS);
4027 # else /* !WORKER follows */
4029 "hostname %s can not be used, please use IP address instead.",
4030 curr_peer->addr->address);
4035 /* add associations from the configuration file */
4036 curr_peer = HEAD_PFIFO(ptree->peers);
4037 for (; curr_peer != NULL; curr_peer = curr_peer->link) {
4038 ZERO_SOCK(&peeraddr);
4039 /* Find the correct host-mode */
4040 hmode = get_correct_host_mode(curr_peer->host_mode);
4043 if (T_Pool == curr_peer->host_mode) {
4044 AF(&peeraddr) = curr_peer->addr->type;
4047 curr_peer->addr->address,
4051 curr_peer->peerversion,
4054 peerflag_bits(curr_peer),
4059 * If we have a numeric address, we can safely
4060 * proceed in the mainline with it. Otherwise, hand
4061 * the hostname off to the blocking child.
4063 } else if (is_ip_address(curr_peer->addr->address,
4064 curr_peer->addr->type, &peeraddr)) {
4066 SET_PORT(&peeraddr, NTP_PORT);
4067 if (is_sane_resolved_address(&peeraddr,
4068 curr_peer->host_mode))
4075 curr_peer->peerversion,
4078 peerflag_bits(curr_peer),
4083 /* we have a hostname to resolve */
4085 ctx = emalloc_zero(sizeof(*ctx));
4086 ctx->family = curr_peer->addr->type;
4087 ctx->host_mode = curr_peer->host_mode;
4089 ctx->version = curr_peer->peerversion;
4090 ctx->minpoll = curr_peer->minpoll;
4091 ctx->maxpoll = curr_peer->maxpoll;
4092 ctx->flags = peerflag_bits(curr_peer);
4093 ctx->ttl = curr_peer->ttl;
4094 ctx->keyid = curr_peer->peerkey;
4095 ctx->group = curr_peer->group;
4098 hints.ai_family = ctx->family;
4099 hints.ai_socktype = SOCK_DGRAM;
4100 hints.ai_protocol = IPPROTO_UDP;
4102 getaddrinfo_sometime_ex(curr_peer->addr->address,
4105 &peer_name_resolved, ctx,
4107 # else /* !WORKER follows */
4109 "hostname %s can not be used, please use IP address instead.",
4110 curr_peer->addr->address);
4118 * peer_name_resolved()
4120 * Callback invoked when config_peers()'s DNS lookup completes.
4129 const char * service,
4130 const struct addrinfo * hints,
4131 const struct addrinfo * res
4134 sockaddr_u peeraddr;
4135 peer_resolved_ctx * ctx;
4137 const char * fam_spec;
4144 DPRINTF(1, ("peer_name_resolved(%s) rescode %d\n", name, rescode));
4149 "giving up resolving host %s: %s (%d)",
4150 name, gai_strerror(rescode), rescode);
4154 /* Loop to configure a single association */
4155 for (; res != NULL; res = res->ai_next) {
4156 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
4157 if (is_sane_resolved_address(&peeraddr,
4159 NLOG(NLOG_SYSINFO) {
4161 fam_spec = (AF_INET6 == af)
4166 msyslog(LOG_INFO, "DNS %s %s-> %s",
4197 peer_node *curr_peer;
4199 if (ptree->peers != NULL) {
4201 UNLINK_FIFO(curr_peer, *ptree->peers, link);
4202 if (NULL == curr_peer)
4204 destroy_address_node(curr_peer->addr);
4205 destroy_attr_val_fifo(curr_peer->peerflags);
4209 ptree->peers = NULL;
4212 #endif /* FREE_CFG_T */
4221 sockaddr_u peeraddr;
4222 struct addrinfo hints;
4223 unpeer_node * curr_unpeer;
4228 curr_unpeer = HEAD_PFIFO(ptree->unpeers);
4229 for (; curr_unpeer != NULL; curr_unpeer = curr_unpeer->link) {
4231 * If we have no address attached, assume we have to
4232 * unpeer by AssocID.
4234 if (!curr_unpeer->addr) {
4235 p = findpeerbyassoc(curr_unpeer->assocID);
4237 msyslog(LOG_NOTICE, "unpeered %s",
4239 peer_clear(p, "GONE");
4246 AF(&peeraddr) = curr_unpeer->addr->type;
4247 name = curr_unpeer->addr->address;
4248 rc = getnetnum(name, &peeraddr, 0, t_UNK);
4249 /* Do we have a numeric address? */
4251 DPRINTF(1, ("unpeer: searching for %s\n",
4253 p = findexistingpeer(&peeraddr, NULL, NULL, -1, 0, NULL);
4255 msyslog(LOG_NOTICE, "unpeered %s",
4257 peer_clear(p, "GONE");
4263 * It's not a numeric IP address, it's a hostname.
4264 * Check for associations with a matching hostname.
4266 for (p = peer_list; p != NULL; p = p->p_link)
4267 if (p->hostname != NULL)
4268 if (!strcasecmp(p->hostname, name))
4271 msyslog(LOG_NOTICE, "unpeered %s", name);
4272 peer_clear(p, "GONE");
4275 /* Resolve the hostname to address(es). */
4278 hints.ai_family = curr_unpeer->addr->type;
4279 hints.ai_socktype = SOCK_DGRAM;
4280 hints.ai_protocol = IPPROTO_UDP;
4281 getaddrinfo_sometime(name, "ntp", &hints,
4283 &unpeer_name_resolved, NULL);
4284 # else /* !WORKER follows */
4286 "hostname %s can not be used, please use IP address instead.",
4295 * unpeer_name_resolved()
4297 * Callback invoked when config_unpeers()'s DNS lookup completes.
4301 unpeer_name_resolved(
4306 const char * service,
4307 const struct addrinfo * hints,
4308 const struct addrinfo * res
4311 sockaddr_u peeraddr;
4314 const char * fam_spec;
4318 DPRINTF(1, ("unpeer_name_resolved(%s) rescode %d\n", name, rescode));
4321 msyslog(LOG_ERR, "giving up resolving unpeer %s: %s (%d)",
4322 name, gai_strerror(rescode), rescode);
4326 * Loop through the addresses found
4328 for (; res != NULL; res = res->ai_next) {
4329 INSIST(res->ai_addrlen <= sizeof(peeraddr));
4330 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
4331 DPRINTF(1, ("unpeer: searching for peer %s\n",
4333 peer = findexistingpeer(&peeraddr, NULL, NULL, -1, 0, NULL);
4336 fam_spec = (AF_INET6 == af)
4341 msyslog(LOG_NOTICE, "unpeered %s %s-> %s", name,
4342 fam_spec, stoa(&peeraddr));
4343 peer_clear(peer, "GONE");
4353 free_config_unpeers(
4357 unpeer_node *curr_unpeer;
4359 if (ptree->unpeers != NULL) {
4361 UNLINK_FIFO(curr_unpeer, *ptree->unpeers, link);
4362 if (NULL == curr_unpeer)
4364 destroy_address_node(curr_unpeer->addr);
4367 free(ptree->unpeers);
4370 #endif /* FREE_CFG_T */
4375 config_reset_counters(
4379 int_node *counter_set;
4381 for (counter_set = HEAD_PFIFO(ptree->reset_counters);
4382 counter_set != NULL;
4383 counter_set = counter_set->link) {
4384 switch (counter_set->i) {
4386 DPRINTF(1, ("config_reset_counters %s (%d) invalid\n",
4387 keyword(counter_set->i), counter_set->i));
4425 free_config_reset_counters(
4429 FREE_INT_FIFO(ptree->reset_counters);
4431 #endif /* FREE_CFG_T */
4441 server_info *serv_info;
4442 attr_val *init_stmt;
4445 /* Check if a simulate block was found in the configuration code.
4446 * If not, return an error and exit
4448 sim_n = HEAD_PFIFO(ptree->sim_details);
4449 if (NULL == sim_n) {
4450 fprintf(stderr, "ERROR!! I couldn't find a \"simulate\" block for configuring the simulator.\n");
4451 fprintf(stderr, "\tCheck your configuration file.\n");
4455 /* Process the initialization statements
4456 * -------------------------------------
4458 init_stmt = HEAD_PFIFO(sim_n->init_opts);
4459 for (; init_stmt != NULL; init_stmt = init_stmt->link) {
4460 switch(init_stmt->attr) {
4463 simulation.beep_delay = init_stmt->value.d;
4466 case T_Sim_Duration:
4467 simulation.end_time = init_stmt->value.d;
4472 "Unknown simulator init token %d\n",
4478 /* Process the server list
4479 * -----------------------
4481 simulation.num_of_servers = 0;
4482 serv_info = HEAD_PFIFO(sim_n->servers);
4483 for (; serv_info != NULL; serv_info = serv_info->link)
4484 simulation.num_of_servers++;
4485 simulation.servers = eallocarray(simulation.num_of_servers,
4486 sizeof(simulation.servers[0]));
4489 serv_info = HEAD_PFIFO(sim_n->servers);
4490 for (; serv_info != NULL; serv_info = serv_info->link) {
4491 if (NULL == serv_info) {
4492 fprintf(stderr, "Simulator server list is corrupt\n");
4495 simulation.servers[i] = *serv_info;
4496 simulation.servers[i].link = NULL;
4501 printf("Creating server associations\n");
4502 create_server_associations();
4503 fprintf(stderr,"\tServer associations successfully created!!\n");
4514 server_info *serv_n;
4515 script_info *script_n;
4517 if (NULL == ptree->sim_details)
4519 sim_n = HEAD_PFIFO(ptree->sim_details);
4520 free(ptree->sim_details);
4521 ptree->sim_details = NULL;
4525 FREE_ATTR_VAL_FIFO(sim_n->init_opts);
4527 UNLINK_FIFO(serv_n, *sim_n->servers, link);
4530 free(serv_n->curr_script);
4531 if (serv_n->script != NULL) {
4533 UNLINK_FIFO(script_n, *serv_n->script,
4535 if (script_n == NULL)
4539 free(serv_n->script);
4545 #endif /* FREE_CFG_T */
4549 /* Define two different config functions. One for the daemon and the other for
4550 * the simulator. The simulator ignores a lot of the standard ntpd configuration
4557 int/*BOOL*/ input_from_files
4560 /* [Bug 3435] check and esure clock sanity if configured from
4561 * file and clock sanity parameters (-> basedate) are given. Do
4562 * this ASAP, so we don't disturb the closed loop controller.
4564 if (input_from_files) {
4565 if (config_tos_clock(ptree))
4569 config_nic_rules(ptree, input_from_files);
4570 config_monitor(ptree);
4573 config_access(ptree);
4574 config_tinker(ptree);
4575 config_rlimit(ptree);
4576 config_system_opts(ptree);
4577 config_logconfig(ptree);
4578 config_phone(ptree);
4579 config_mdnstries(ptree);
4580 config_setvar(ptree);
4584 io_open_sockets(); /* [bug 2837] dep. on config_vars() */
4586 config_trap(ptree); /* [bug 2923] dep. on io_open_sockets() */
4587 config_other_modes(ptree);
4588 config_peers(ptree);
4589 config_unpeers(ptree);
4590 config_fudge(ptree);
4591 config_reset_counters(ptree);
4599 #ifdef TEST_BLOCKING_WORKER
4601 struct addrinfo hints;
4604 hints.ai_socktype = SOCK_STREAM;
4605 hints.ai_protocol = IPPROTO_TCP;
4606 getaddrinfo_sometime("www.cnn.com", "ntp", &hints,
4608 gai_test_callback, (void *)1);
4609 hints.ai_family = AF_INET6;
4610 getaddrinfo_sometime("ipv6.google.com", "ntp", &hints,
4612 gai_test_callback, (void *)0x600);
4625 printf("Configuring Simulator...\n");
4626 printf("Some ntpd-specific commands in the configuration file will be ignored.\n");
4629 config_monitor(ptree);
4630 config_tinker(ptree);
4632 config_rlimit(ptree); /* not needed for the simulator */
4633 config_system_opts(ptree);
4634 config_logconfig(ptree);
4642 * config_remotely() - implements ntpd side of ntpq :config
4646 sockaddr_u * remote_addr
4651 snprintf(origin, sizeof(origin), "remote config from %s",
4653 lex_init_stack(origin, NULL); /* no checking needed... */
4654 init_syntax_tree(&cfgt);
4658 cfgt.source.attr = CONF_SOURCE_NTPQ;
4659 cfgt.timestamp = time(NULL);
4660 cfgt.source.value.s = estrdup(stoa(remote_addr));
4662 DPRINTF(1, ("Finished Parsing!!\n"));
4664 save_and_apply_config_tree(FALSE);
4669 * getconfig() - process startup configuration file e.g /etc/ntp.conf
4680 atexit(free_all_config_trees);
4683 config_file = CONFIG_FILE;
4686 if (!ExpandEnvironmentStringsA(temp, config_file_storage,
4687 sizeof(config_file_storage))) {
4688 msyslog(LOG_ERR, "ExpandEnvironmentStrings CONFIG_FILE failed: %m");
4691 config_file = config_file_storage;
4693 temp = ALT_CONFIG_FILE;
4694 if (!ExpandEnvironmentStringsA(temp, alt_config_file_storage,
4695 sizeof(alt_config_file_storage))) {
4696 msyslog(LOG_ERR, "ExpandEnvironmentStrings ALT_CONFIG_FILE failed: %m");
4699 alt_config_file = alt_config_file_storage;
4700 #endif /* SYS_WINNT */
4703 * install a non default variable with this daemon version
4705 snprintf(line, sizeof(line), "daemon_version=\"%s\"", Version);
4706 set_sys_var(line, strlen(line) + 1, RO);
4709 * Set up for the first time step to install a variable showing
4710 * which syscall is being used to step.
4712 set_tod_using = &ntpd_set_tod_using;
4714 getCmdOpts(argc, argv);
4715 init_syntax_tree(&cfgt);
4717 !lex_init_stack(FindConfig(config_file), "r")
4719 /* If there is no config_file, try NetInfo. */
4720 && check_netinfo && !(config_netinfo = get_netinfo_config())
4721 #endif /* HAVE_NETINFO */
4723 msyslog(LOG_INFO, "getconfig: Couldn't open <%s>: %m", FindConfig(config_file));
4729 /* Under WinNT try alternate_config_file name, first NTP.CONF, then NTP.INI */
4731 if (!lex_init_stack(FindConfig(alt_config_file), "r")) {
4733 * Broadcast clients can sometimes run without
4734 * a configuration file.
4736 msyslog(LOG_INFO, "getconfig: Couldn't open <%s>: %m", FindConfig(alt_config_file));
4741 cfgt.source.value.s = estrdup(alt_config_file);
4742 #endif /* SYS_WINNT */
4744 cfgt.source.value.s = estrdup(config_file);
4747 /*** BULK OF THE PARSER ***/
4749 yydebug = !!(debug >= 5);
4754 DPRINTF(1, ("Finished Parsing!!\n"));
4756 cfgt.source.attr = CONF_SOURCE_FILE;
4757 cfgt.timestamp = time(NULL);
4759 save_and_apply_config_tree(TRUE);
4763 free_netinfo_config(config_netinfo);
4764 #endif /* HAVE_NETINFO */
4769 save_and_apply_config_tree(int/*BOOL*/ input_from_file)
4773 config_tree *punlinked;
4777 * Keep all the configuration trees applied since startup in
4778 * a list that can be used to dump the configuration back to
4781 ptree = emalloc(sizeof(*ptree));
4782 memcpy(ptree, &cfgt, sizeof(*ptree));
4785 LINK_TAIL_SLIST(cfg_tree_history, ptree, link, config_tree);
4788 if (HAVE_OPT( SAVECONFIGQUIT )) {
4793 dumpfile = fopen(OPT_ARG( SAVECONFIGQUIT ), "w");
4794 if (NULL == dumpfile) {
4797 "can not create save file %s, error %d %m\n",
4798 OPT_ARG(SAVECONFIGQUIT), err);
4802 dumpfailed = dump_all_config_trees(dumpfile, 0);
4805 "--saveconfigquit %s error %d\n",
4806 OPT_ARG( SAVECONFIGQUIT ),
4810 "configuration saved to %s\n",
4811 OPT_ARG( SAVECONFIGQUIT ));
4815 #endif /* SAVECONFIG */
4817 /* The actual configuration done depends on whether we are configuring the
4818 * simulator or the daemon. Perform a check and call the appropriate
4819 * function as needed.
4823 config_ntpd(ptree, input_from_file);
4825 config_ntpdsim(ptree);
4829 * With configure --disable-saveconfig, there's no use keeping
4830 * the config tree around after application, so free it.
4833 UNLINK_SLIST(punlinked, cfg_tree_history, ptree, link,
4835 INSIST(punlinked == ptree);
4836 free_config_tree(ptree);
4840 /* Hack to disambiguate 'server' statements for refclocks and network peers.
4841 * Please note the qualification 'hack'. It's just that.
4845 const address_node * addr
4848 return addr && addr->address && !strncmp(addr->address, "127.127.", 6);
4858 snprintf(line, sizeof(line), "settimeofday=\"%s\"", which);
4859 set_sys_var(line, strlen(line) + 1, RO);
4873 snprintf(buf, LIB_BUFLENGTH, "%g", d);
4875 /* use lowercase 'e', strip any leading zeroes in exponent */
4876 pch_e = strchr(buf, 'e');
4877 if (NULL == pch_e) {
4878 pch_e = strchr(buf, 'E');
4887 while ('0' == *pch_nz)
4889 if (pch_nz == pch_e)
4891 strlcpy(pch_e, pch_nz, LIB_BUFLENGTH - (pch_e - buf));
4897 /* FUNCTIONS COPIED FROM THE OLDER ntp_config.c
4898 * --------------------------------------------
4903 * get_pfxmatch - find value for prefixmatch
4904 * and update char * accordingly
4912 while (m->name != NULL) {
4913 if (strncmp(*pstr, m->name, strlen(m->name)) == 0) {
4914 *pstr += strlen(m->name);
4924 * get_match - find logmask value
4932 while (m->name != NULL) {
4933 if (strcmp(str, m->name) == 0)
4942 * get_logmask - build bitmask for ntp_syslogmask
4953 mask = get_match(str, logcfg_noclass_items);
4958 offset = get_pfxmatch(&t, logcfg_class);
4959 mask = get_match(t, logcfg_class_items);
4962 return mask << offset;
4964 msyslog(LOG_ERR, "logconfig: '%s' not recognized - ignored",
4974 * get_netinfo_config - find the nearest NetInfo domain with an ntp
4975 * configuration and initialize the configuration state.
4977 static struct netinfo_config_state *
4978 get_netinfo_config(void)
4983 struct netinfo_config_state *config;
4985 if (ni_open(NULL, ".", &domain) != NI_OK) return NULL;
4987 while ((status = ni_pathsearch(domain, &config_dir, NETINFO_CONFIG_DIR)) == NI_NODIR) {
4989 if (ni_open(domain, "..", &next_domain) != NI_OK) {
4990 ni_free(next_domain);
4994 domain = next_domain;
4996 if (status != NI_OK) {
5001 config = emalloc(sizeof(*config));
5002 config->domain = domain;
5003 config->config_dir = config_dir;
5004 config->prop_index = 0;
5005 config->val_index = 0;
5006 config->val_list = NULL;
5013 * free_netinfo_config - release NetInfo configuration state
5016 free_netinfo_config(
5017 struct netinfo_config_state *config
5020 ni_free(config->domain);
5026 * gettokens_netinfo - return tokens from NetInfo
5030 struct netinfo_config_state *config,
5035 int prop_index = config->prop_index;
5036 int val_index = config->val_index;
5037 char **val_list = config->val_list;
5040 * Iterate through each keyword and look for a property that matches it.
5044 for (; prop_index < COUNTOF(keywords); prop_index++)
5046 ni_namelist namelist;
5047 struct keyword current_prop = keywords[prop_index];
5051 * For each value associated in the property, we're going to return
5052 * a separate line. We squirrel away the values in the config state
5053 * so the next time through, we don't need to do this lookup.
5056 if (NI_OK == ni_lookupprop(config->domain,
5057 &config->config_dir, current_prop.text,
5060 /* Found the property, but it has no values */
5061 if (namelist.ni_namelist_len == 0) continue;
5065 (namelist.ni_namelist_len + 1),
5067 val_list = config->val_list;
5070 index < namelist.ni_namelist_len;
5074 value = namelist.ni_namelist_val[index];
5075 val_list[index] = estrdup(value);
5077 val_list[index] = NULL;
5081 ni_namelist_free(&namelist);
5083 config->prop_index = prop_index;
5086 /* No list; we're done here. */
5088 return CONFIG_UNKNOWN;
5091 * We have a list of values for the current property.
5092 * Iterate through them and return each in order.
5094 if (val_list[val_index]) {
5097 char *tokens = val_list[val_index];
5099 msyslog(LOG_INFO, "%s %s", keywords[prop_index].text, val_list[val_index]);
5101 (const char*)tokenlist[0] = keywords[prop_index].text;
5102 for (ntok = 1; ntok < MAXTOKENS; ntok++) {
5103 tokenlist[ntok] = tokens;
5104 while (!ISEOL(*tokens) && (!ISSPACE(*tokens) || quoted))
5105 quoted ^= (*tokens++ == '"');
5107 if (ISEOL(*tokens)) {
5110 } else { /* must be space */
5112 while (ISSPACE(*tokens))
5119 if (ntok == MAXTOKENS) {
5120 /* HMS: chomp it to lose the EOL? */
5122 "gettokens_netinfo: too many tokens. Ignoring: %s",
5125 *ntokens = ntok + 1;
5128 config->val_index++; /* HMS: Should this be in the 'else'? */
5130 return keywords[prop_index].keytype;
5133 /* We're done with the current property. */
5134 prop_index = ++config->prop_index;
5136 /* Free val_list and reset counters. */
5137 for (val_index = 0; val_list[val_index]; val_index++)
5138 free(val_list[val_index]);
5140 val_list = config->val_list = NULL;
5141 val_index = config->val_index = 0;
5145 #endif /* HAVE_NETINFO */
5149 * getnetnum - return a net number (this is crude, but careful)
5151 * returns 1 for success, and mysteriously, 0 for most failures, and
5152 * -1 if the address found is IPv6 and we believe IPv6 isn't working.
5160 enum gnn_type a_type /* ignored */
5163 REQUIRE(AF_UNSPEC == AF(addr) ||
5164 AF_INET == AF(addr) ||
5165 AF_INET6 == AF(addr));
5167 if (!is_ip_address(num, AF(addr), addr))
5170 if (IS_IPV6(addr) && !ipv6_works)
5173 # ifdef ISC_PLATFORM_HAVESALEN
5174 addr->sa.sa_len = SIZEOF_SOCKADDR(AF(addr));
5176 SET_PORT(addr, NTP_PORT);
5178 DPRINTF(2, ("getnetnum given %s, got %s\n", num, stoa(addr)));
5184 #if defined(HAVE_SETRLIMIT)
5190 const char * rl_sstr
5196 # ifdef RLIMIT_MEMLOCK
5197 case RLIMIT_MEMLOCK:
5198 if (HAVE_OPT( SAVECONFIGQUIT )) {
5202 * The default RLIMIT_MEMLOCK is very low on Linux systems.
5203 * Unless we increase this limit malloc calls are likely to
5204 * fail if we drop root privilege. To be useful the value
5205 * has to be larger than the largest ntpd resident set size.
5207 DPRINTF(2, ("ntp_rlimit: MEMLOCK: %d %s\n",
5208 (int)(rl_value / rl_scale), rl_sstr));
5209 rl.rlim_cur = rl.rlim_max = rl_value;
5210 if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1)
5211 msyslog(LOG_ERR, "Cannot set RLIMIT_MEMLOCK: %m");
5213 # endif /* RLIMIT_MEMLOCK */
5215 # ifdef RLIMIT_NOFILE
5218 * For large systems the default file descriptor limit may
5221 DPRINTF(2, ("ntp_rlimit: NOFILE: %d %s\n",
5222 (int)(rl_value / rl_scale), rl_sstr));
5223 rl.rlim_cur = rl.rlim_max = rl_value;
5224 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
5225 msyslog(LOG_ERR, "Cannot set RLIMIT_NOFILE: %m");
5227 # endif /* RLIMIT_NOFILE */
5229 # ifdef RLIMIT_STACK
5232 * Provide a way to set the stack limit to something
5233 * smaller, so that we don't lock a lot of unused
5236 DPRINTF(2, ("ntp_rlimit: STACK: %d %s pages\n",
5237 (int)(rl_value / rl_scale), rl_sstr));
5238 if (-1 == getrlimit(RLIMIT_STACK, &rl)) {
5239 msyslog(LOG_ERR, "getrlimit(RLIMIT_STACK) failed: %m");
5241 if (rl_value > rl.rlim_max) {
5242 msyslog(LOG_WARNING,
5243 "ntp_rlimit: using maximum allowed stack limit %lu instead of %lu.",
5244 (u_long)rl.rlim_max,
5246 rl_value = rl.rlim_max;
5248 rl.rlim_cur = rl_value;
5249 if (-1 == setrlimit(RLIMIT_STACK, &rl)) {
5251 "ntp_rlimit: Cannot set RLIMIT_STACK: %m");
5255 # endif /* RLIMIT_STACK */
5258 fatal_error("ntp_rlimit: unexpected RLIMIT case: %d", rl_what);
5261 #endif /* HAVE_SETRLIMIT */
5265 build_iflags(u_int32 iflags)
5267 static char ifs[1024];
5271 if (iflags & INT_UP) {
5273 appendstr(ifs, sizeof ifs, "up");
5276 if (iflags & INT_PPP) {
5278 appendstr(ifs, sizeof ifs, "ppp");
5281 if (iflags & INT_LOOPBACK) {
5282 iflags &= ~INT_LOOPBACK;
5283 appendstr(ifs, sizeof ifs, "loopback");
5286 if (iflags & INT_BROADCAST) {
5287 iflags &= ~INT_BROADCAST;
5288 appendstr(ifs, sizeof ifs, "broadcast");
5291 if (iflags & INT_MULTICAST) {
5292 iflags &= ~INT_MULTICAST;
5293 appendstr(ifs, sizeof ifs, "multicast");
5296 if (iflags & INT_BCASTOPEN) {
5297 iflags &= ~INT_BCASTOPEN;
5298 appendstr(ifs, sizeof ifs, "bcastopen");
5301 if (iflags & INT_MCASTOPEN) {
5302 iflags &= ~INT_MCASTOPEN;
5303 appendstr(ifs, sizeof ifs, "mcastopen");
5306 if (iflags & INT_WILDCARD) {
5307 iflags &= ~INT_WILDCARD;
5308 appendstr(ifs, sizeof ifs, "wildcard");
5311 if (iflags & INT_MCASTIF) {
5312 iflags &= ~INT_MCASTIF;
5313 appendstr(ifs, sizeof ifs, "MCASTif");
5316 if (iflags & INT_PRIVACY) {
5317 iflags &= ~INT_PRIVACY;
5318 appendstr(ifs, sizeof ifs, "IPv6privacy");
5321 if (iflags & INT_BCASTXMIT) {
5322 iflags &= ~INT_BCASTXMIT;
5323 appendstr(ifs, sizeof ifs, "bcastxmit");
5329 snprintf(string, sizeof string, "%0x", iflags);
5330 appendstr(ifs, sizeof ifs, string);
5338 build_mflags(u_short mflags)
5340 static char mfs[1024];
5344 if (mflags & RESM_NTPONLY) {
5345 mflags &= ~RESM_NTPONLY;
5346 appendstr(mfs, sizeof mfs, "ntponly");
5349 if (mflags & RESM_SOURCE) {
5350 mflags &= ~RESM_SOURCE;
5351 appendstr(mfs, sizeof mfs, "source");
5357 snprintf(string, sizeof string, "%0x", mflags);
5358 appendstr(mfs, sizeof mfs, string);
5366 build_rflags(u_short rflags)
5368 static char rfs[1024];
5372 if (rflags & RES_FLAKE) {
5373 rflags &= ~RES_FLAKE;
5374 appendstr(rfs, sizeof rfs, "flake");
5377 if (rflags & RES_IGNORE) {
5378 rflags &= ~RES_IGNORE;
5379 appendstr(rfs, sizeof rfs, "ignore");
5382 if (rflags & RES_KOD) {
5384 appendstr(rfs, sizeof rfs, "kod");
5387 if (rflags & RES_MSSNTP) {
5388 rflags &= ~RES_MSSNTP;
5389 appendstr(rfs, sizeof rfs, "mssntp");
5392 if (rflags & RES_LIMITED) {
5393 rflags &= ~RES_LIMITED;
5394 appendstr(rfs, sizeof rfs, "limited");
5397 if (rflags & RES_LPTRAP) {
5398 rflags &= ~RES_LPTRAP;
5399 appendstr(rfs, sizeof rfs, "lptrap");
5402 if (rflags & RES_NOMODIFY) {
5403 rflags &= ~RES_NOMODIFY;
5404 appendstr(rfs, sizeof rfs, "nomodify");
5407 if (rflags & RES_NOMRULIST) {
5408 rflags &= ~RES_NOMRULIST;
5409 appendstr(rfs, sizeof rfs, "nomrulist");
5412 if (rflags & RES_NOEPEER) {
5413 rflags &= ~RES_NOEPEER;
5414 appendstr(rfs, sizeof rfs, "noepeer");
5417 if (rflags & RES_NOPEER) {
5418 rflags &= ~RES_NOPEER;
5419 appendstr(rfs, sizeof rfs, "nopeer");
5422 if (rflags & RES_NOQUERY) {
5423 rflags &= ~RES_NOQUERY;
5424 appendstr(rfs, sizeof rfs, "noquery");
5427 if (rflags & RES_DONTSERVE) {
5428 rflags &= ~RES_DONTSERVE;
5429 appendstr(rfs, sizeof rfs, "dontserve");
5432 if (rflags & RES_NOTRAP) {
5433 rflags &= ~RES_NOTRAP;
5434 appendstr(rfs, sizeof rfs, "notrap");
5437 if (rflags & RES_DONTTRUST) {
5438 rflags &= ~RES_DONTTRUST;
5439 appendstr(rfs, sizeof rfs, "notrust");
5442 if (rflags & RES_VERSION) {
5443 rflags &= ~RES_VERSION;
5444 appendstr(rfs, sizeof rfs, "version");
5450 snprintf(string, sizeof string, "%0x", rflags);
5451 appendstr(rfs, sizeof rfs, string);
5454 if ('\0' == rfs[0]) {
5455 appendstr(rfs, sizeof rfs, "(none)");
5469 if (*string != '\0') {
5470 (void)strlcat(string, ",", s);
5472 (void)strlcat(string, new, s);