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"
57 /* Bison still(!) does not emit usable prototypes for the calling code */
58 int yyparse (struct FILE_INFO *ip_file);
60 /* list of servers from command line for config_peers() */
61 int cmdline_server_count;
62 char ** cmdline_servers;
64 /* set to zero if admin doesn't want memory locked */
68 * "logconfig" building blocks
71 const char * const name;
75 static struct masks logcfg_class[] = {
76 { "clock", NLOG_OCLOCK },
77 { "peer", NLOG_OPEER },
78 { "sync", NLOG_OSYNC },
83 /* logcfg_noclass_items[] masks are complete and must not be shifted */
84 static struct masks logcfg_noclass_items[] = {
85 { "allall", NLOG_SYSMASK | NLOG_PEERMASK | NLOG_CLOCKMASK | NLOG_SYNCMASK },
86 { "allinfo", NLOG_SYSINFO | NLOG_PEERINFO | NLOG_CLOCKINFO | NLOG_SYNCINFO },
87 { "allevents", NLOG_SYSEVENT | NLOG_PEEREVENT | NLOG_CLOCKEVENT | NLOG_SYNCEVENT },
88 { "allstatus", NLOG_SYSSTATUS | NLOG_PEERSTATUS | NLOG_CLOCKSTATUS | NLOG_SYNCSTATUS },
89 { "allstatistics", NLOG_SYSSTATIST | NLOG_PEERSTATIST | NLOG_CLOCKSTATIST | NLOG_SYNCSTATIST },
90 /* the remainder are misspellings of clockall, peerall, sysall, and syncall. */
91 { "allclock", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OCLOCK },
92 { "allpeer", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OPEER },
93 { "allsys", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OSYS },
94 { "allsync", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OSYNC },
98 /* logcfg_class_items[] masks are shiftable by NLOG_O* counts */
99 static struct masks logcfg_class_items[] = {
100 { "all", NLOG_INFO | NLOG_EVENT | NLOG_STATUS | NLOG_STATIST },
101 { "info", NLOG_INFO },
102 { "events", NLOG_EVENT },
103 { "status", NLOG_STATUS },
104 { "statistics", NLOG_STATIST },
108 typedef struct peer_resolved_ctx_tag {
110 int host_mode; /* T_* token identifier */
113 u_char hmode; /* MODE_* */
122 #define MAXPHONE 10 /* maximum number of phone strings */
123 #define MAXPPS 20 /* maximum length of PPS device string */
126 * Miscellaneous macros
128 #define ISEOL(c) ((c) == '#' || (c) == '\n' || (c) == '\0')
129 #define ISSPACE(c) ((c) == ' ' || (c) == '\t')
132 * Definitions of things either imported from or exported to outside
134 extern int yydebug; /* ntp_parser.c (.y) */
135 int curr_include_level; /* The current include level */
136 struct FILE_INFO *fp[MAXINCLUDELEVEL+1];
137 config_tree cfgt; /* Parser output stored here */
138 struct config_tree_tag *cfg_tree_history; /* History of configs */
139 char *sys_phone[MAXPHONE] = {NULL}; /* ACTS phone numbers */
140 char default_keysdir[] = NTP_KEYSDIR;
141 char *keysdir = default_keysdir; /* crypto keys directory */
142 char * saveconfigdir;
143 #if defined(HAVE_SCHED_SETSCHEDULER)
144 int config_priority_override = 0;
148 const char *config_file;
149 static char default_ntp_signd_socket[] =
150 #ifdef NTP_SIGND_PATH
155 char *ntp_signd_socket = default_ntp_signd_socket;
157 struct netinfo_config_state *config_netinfo = NULL;
158 int check_netinfo = 1;
159 #endif /* HAVE_NETINFO */
161 char *alt_config_file;
163 char config_file_storage[MAX_PATH];
164 char alt_config_file_storage[MAX_PATH];
165 #endif /* SYS_WINNT */
169 * NetInfo configuration state
171 struct netinfo_config_state {
172 void *domain; /* domain with config */
173 ni_id config_dir; /* ID config dir */
174 int prop_index; /* current property */
175 int val_index; /* current value */
176 char **val_list; /* value list */
180 struct REMOTE_CONFIG_INFO remote_config; /* Remote configuration buffer and
182 int input_from_file = 1; /* A boolean flag, which when set, indicates that
183 the input is to be taken from the configuration
184 file, instead of the remote-configuration buffer
187 int old_config_style = 1; /* A boolean flag, which when set,
188 * indicates that the old configuration
189 * format with a newline at the end of
190 * every command is being used
192 int cryptosw; /* crypto command called */
194 extern char *stats_drift_file; /* name of the driftfile */
196 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
198 * backwards compatibility flags
200 bc_entry bc_list[] = {
201 { T_Bc_bugXXXX, 1 } /* default enabled */
205 * declare an int pointer for each flag for quick testing without
206 * walking bc_list. If the pointer is consumed by libntp rather
207 * than ntpd, declare it in a libntp source file pointing to storage
208 * initialized with the appropriate value for other libntp clients, and
209 * redirect it to point into bc_list during ntpd startup.
211 int *p_bcXXXX_enabled = &bc_list[0].enabled;
214 /* FUNCTION PROTOTYPES */
216 static void init_syntax_tree(config_tree *);
217 static void apply_enable_disable(attr_val_fifo *q, int enable);
220 static void free_auth_node(config_tree *);
221 static void free_all_config_trees(void);
223 static void free_config_access(config_tree *);
224 static void free_config_auth(config_tree *);
225 static void free_config_fudge(config_tree *);
226 static void free_config_logconfig(config_tree *);
227 static void free_config_monitor(config_tree *);
228 static void free_config_nic_rules(config_tree *);
229 static void free_config_other_modes(config_tree *);
230 static void free_config_peers(config_tree *);
231 static void free_config_phone(config_tree *);
232 static void free_config_reset_counters(config_tree *);
233 static void free_config_rlimit(config_tree *);
234 static void free_config_setvar(config_tree *);
235 static void free_config_system_opts(config_tree *);
236 static void free_config_tinker(config_tree *);
237 static void free_config_tos(config_tree *);
238 static void free_config_trap(config_tree *);
239 static void free_config_ttl(config_tree *);
240 static void free_config_unpeers(config_tree *);
241 static void free_config_vars(config_tree *);
244 static void free_config_sim(config_tree *);
246 static void destroy_address_fifo(address_fifo *);
247 #define FREE_ADDRESS_FIFO(pf) \
249 destroy_address_fifo(pf); \
252 void free_all_config_trees(void); /* atexit() */
253 static void free_config_tree(config_tree *ptree);
254 #endif /* FREE_CFG_T */
256 static void destroy_restrict_node(restrict_node *my_node);
257 static int is_sane_resolved_address(sockaddr_u *peeraddr, int hmode);
258 static void save_and_apply_config_tree(void);
259 static void destroy_int_fifo(int_fifo *);
260 #define FREE_INT_FIFO(pf) \
262 destroy_int_fifo(pf); \
265 static void destroy_string_fifo(string_fifo *);
266 #define FREE_STRING_FIFO(pf) \
268 destroy_string_fifo(pf); \
271 static void destroy_attr_val_fifo(attr_val_fifo *);
272 #define FREE_ATTR_VAL_FIFO(pf) \
274 destroy_attr_val_fifo(pf); \
277 static void destroy_filegen_fifo(filegen_fifo *);
278 #define FREE_FILEGEN_FIFO(pf) \
280 destroy_filegen_fifo(pf); \
283 static void destroy_restrict_fifo(restrict_fifo *);
284 #define FREE_RESTRICT_FIFO(pf) \
286 destroy_restrict_fifo(pf); \
289 static void destroy_setvar_fifo(setvar_fifo *);
290 #define FREE_SETVAR_FIFO(pf) \
292 destroy_setvar_fifo(pf); \
295 static void destroy_addr_opts_fifo(addr_opts_fifo *);
296 #define FREE_ADDR_OPTS_FIFO(pf) \
298 destroy_addr_opts_fifo(pf); \
302 static void config_logconfig(config_tree *);
303 static void config_monitor(config_tree *);
304 static void config_rlimit(config_tree *);
305 static void config_system_opts(config_tree *);
306 static void config_tinker(config_tree *);
307 static void config_tos(config_tree *);
308 static void config_vars(config_tree *);
311 static sockaddr_u *get_next_address(address_node *addr);
312 static void config_sim(config_tree *);
313 static void config_ntpdsim(config_tree *);
314 #else /* !SIM follows */
315 static void config_ntpd(config_tree *);
316 static void config_other_modes(config_tree *);
317 static void config_auth(config_tree *);
318 static void config_access(config_tree *);
319 static void config_phone(config_tree *);
320 static void config_setvar(config_tree *);
321 static void config_ttl(config_tree *);
322 static void config_trap(config_tree *);
323 static void config_fudge(config_tree *);
324 static void config_peers(config_tree *);
325 static void config_unpeers(config_tree *);
326 static void config_nic_rules(config_tree *);
327 static void config_reset_counters(config_tree *);
328 static u_char get_correct_host_mode(int token);
329 static int peerflag_bits(peer_node *);
333 static void peer_name_resolved(int, int, void *, const char *, const char *,
334 const struct addrinfo *,
335 const struct addrinfo *);
336 static void unpeer_name_resolved(int, int, void *, const char *, const char *,
337 const struct addrinfo *,
338 const struct addrinfo *);
339 static void trap_name_resolved(int, int, void *, const char *, const char *,
340 const struct addrinfo *,
341 const struct addrinfo *);
346 t_REF, /* Refclock */
347 t_MSK /* Network Mask */
350 static void ntpd_set_tod_using(const char *);
351 static char * normal_dtoa(double);
352 static u_int32 get_pfxmatch(const char **, struct masks *);
353 static u_int32 get_match(const char *, struct masks *);
354 static u_int32 get_logmask(const char *);
356 static int getnetnum(const char *num, sockaddr_u *addr, int complain,
357 enum gnn_type a_type);
361 /* FUNCTIONS FOR INITIALIZATION
362 * ----------------------------
371 if (ptree->auth.keys) {
372 free(ptree->auth.keys);
373 ptree->auth.keys = NULL;
376 if (ptree->auth.keysdir) {
377 free(ptree->auth.keysdir);
378 ptree->auth.keysdir = NULL;
381 if (ptree->auth.ntp_signd_socket) {
382 free(ptree->auth.ntp_signd_socket);
383 ptree->auth.ntp_signd_socket = NULL;
400 free_all_config_trees(void)
405 ptree = cfg_tree_history;
407 while (ptree != NULL) {
409 free_config_tree(ptree);
420 #if defined(_MSC_VER) && defined (_DEBUG)
424 if (ptree->source.value.s != NULL)
425 free(ptree->source.value.s);
427 free_config_other_modes(ptree);
428 free_config_auth(ptree);
429 free_config_tos(ptree);
430 free_config_monitor(ptree);
431 free_config_access(ptree);
432 free_config_tinker(ptree);
433 free_config_rlimit(ptree);
434 free_config_system_opts(ptree);
435 free_config_logconfig(ptree);
436 free_config_phone(ptree);
437 free_config_setvar(ptree);
438 free_config_ttl(ptree);
439 free_config_trap(ptree);
440 free_config_fudge(ptree);
441 free_config_vars(ptree);
442 free_config_peers(ptree);
443 free_config_unpeers(ptree);
444 free_config_nic_rules(ptree);
445 free_config_reset_counters(ptree);
447 free_config_sim(ptree);
449 free_auth_node(ptree);
453 #if defined(_MSC_VER) && defined (_DEBUG)
457 #endif /* FREE_CFG_T */
463 dump_all_config_trees(
468 config_tree * cfg_ptr;
472 for (cfg_ptr = cfg_tree_history;
474 cfg_ptr = cfg_ptr->link)
475 return_value |= dump_config_tree(cfg_ptr, df, comment);
481 /* The config dumper */
490 unpeer_node *unpeern;
493 address_node *peer_addr;
494 address_node *fudge_addr;
495 filegen_node *fgen_node;
496 restrict_node *rest_node;
497 addr_opts_node *addr_opts;
498 setvar_node *setv_node;
499 nic_rule_node *rule_node;
502 int_node *counter_set;
503 string_node *str_node;
511 DPRINTF(1, ("dump_config_tree(%p)\n", ptree));
514 if (!strftime(timestamp, sizeof(timestamp),
516 localtime(&ptree->timestamp)))
519 fprintf(df, "# %s %s %s\n",
521 (CONF_SOURCE_NTPQ == ptree->source.attr)
522 ? "ntpq remote config from"
523 : "startup configuration file",
524 ptree->source.value.s);
527 /* For options I didn't find documentation I'll just output its name and the cor. value */
528 atrv = HEAD_PFIFO(ptree->vars);
529 for ( ; atrv != NULL; atrv = atrv->link) {
530 switch (atrv->type) {
533 fprintf(df, "\n# dump error:\n"
534 "# unknown vars type %d (%s) for %s\n",
535 atrv->type, token_name(atrv->type),
536 token_name(atrv->attr));
540 fprintf(df, "%s %s\n", keyword(atrv->attr),
541 normal_dtoa(atrv->value.d));
545 fprintf(df, "%s %d\n", keyword(atrv->attr),
550 fprintf(df, "%s \"%s\"", keyword(atrv->attr),
552 if (T_Driftfile == atrv->attr &&
553 atrv->link != NULL &&
554 T_WanderThreshold == atrv->link->attr) {
557 normal_dtoa(atrv->value.d));
565 atrv = HEAD_PFIFO(ptree->logconfig);
567 fprintf(df, "logconfig");
568 for ( ; atrv != NULL; atrv = atrv->link)
569 fprintf(df, " %c%s", atrv->attr, atrv->value.s);
573 if (ptree->stats_dir)
574 fprintf(df, "statsdir \"%s\"\n", ptree->stats_dir);
576 i_n = HEAD_PFIFO(ptree->stats_list);
578 fprintf(df, "statistics");
579 for ( ; i_n != NULL; i_n = i_n->link)
580 fprintf(df, " %s", keyword(i_n->i));
584 fgen_node = HEAD_PFIFO(ptree->filegen_opts);
585 for ( ; fgen_node != NULL; fgen_node = fgen_node->link) {
586 atrv = HEAD_PFIFO(fgen_node->options);
588 fprintf(df, "filegen %s",
589 keyword(fgen_node->filegen_token));
590 for ( ; atrv != NULL; atrv = atrv->link) {
591 switch (atrv->attr) {
594 fprintf(df, "\n# dump error:\n"
595 "# unknown filegen option token %s\n"
597 token_name(atrv->attr),
598 keyword(fgen_node->filegen_token));
602 fprintf(df, " file %s",
607 fprintf(df, " type %s",
608 keyword(atrv->value.i));
613 keyword(atrv->value.i));
621 atrv = HEAD_PFIFO(ptree->auth.crypto_cmd_list);
623 fprintf(df, "crypto");
624 for ( ; atrv != NULL; atrv = atrv->link) {
625 fprintf(df, " %s %s", keyword(atrv->attr),
631 if (ptree->auth.revoke != 0)
632 fprintf(df, "revoke %d\n", ptree->auth.revoke);
634 if (ptree->auth.keysdir != NULL)
635 fprintf(df, "keysdir \"%s\"\n", ptree->auth.keysdir);
637 if (ptree->auth.keys != NULL)
638 fprintf(df, "keys \"%s\"\n", ptree->auth.keys);
640 atrv = HEAD_PFIFO(ptree->auth.trusted_key_list);
642 fprintf(df, "trustedkey");
643 for ( ; atrv != NULL; atrv = atrv->link) {
644 if (T_Integer == atrv->type)
645 fprintf(df, " %d", atrv->value.i);
646 else if (T_Intrange == atrv->type)
647 fprintf(df, " (%d ... %d)",
652 fprintf(df, "\n# dump error:\n"
653 "# unknown trustedkey attr type %d\n"
654 "trustedkey", atrv->type);
660 if (ptree->auth.control_key)
661 fprintf(df, "controlkey %d\n", ptree->auth.control_key);
663 if (ptree->auth.request_key)
664 fprintf(df, "requestkey %d\n", ptree->auth.request_key);
666 /* dump enable list, then disable list */
667 for (enable = 1; enable >= 0; enable--) {
669 ? HEAD_PFIFO(ptree->enable_opts)
670 : HEAD_PFIFO(ptree->disable_opts);
672 fprintf(df, "%s", (enable)
675 for ( ; atrv != NULL; atrv = atrv->link)
677 keyword(atrv->value.i));
682 atrv = HEAD_PFIFO(ptree->orphan_cmds);
685 for ( ; atrv != NULL; atrv = atrv->link) {
686 switch (atrv->type) {
689 fprintf(df, "\n# dump error:\n"
690 "# unknown tos attr type %d %s\n"
692 token_name(atrv->type));
696 fprintf(df, " %s %s",
698 normal_dtoa(atrv->value.d));
705 atrv = HEAD_PFIFO(ptree->rlimit);
707 fprintf(df, "rlimit");
708 for ( ; atrv != NULL; atrv = atrv->link) {
709 INSIST(T_Integer == atrv->type);
710 fprintf(df, " %s %d", keyword(atrv->attr),
716 atrv = HEAD_PFIFO(ptree->tinker);
718 fprintf(df, "tinker");
719 for ( ; atrv != NULL; atrv = atrv->link) {
720 INSIST(T_Double == atrv->type);
721 fprintf(df, " %s %s", keyword(atrv->attr),
722 normal_dtoa(atrv->value.d));
727 if (ptree->broadcastclient)
728 fprintf(df, "broadcastclient\n");
730 peern = HEAD_PFIFO(ptree->peers);
731 for ( ; peern != NULL; peern = peern->link) {
733 fprintf(df, "%s", keyword(peern->host_mode));
734 switch (addr->type) {
737 fprintf(df, "# dump error:\n"
738 "# unknown peer family %d for:\n"
740 keyword(peern->host_mode));
754 fprintf(df, " %s", addr->address);
756 if (peern->minpoll != 0)
757 fprintf(df, " minpoll %u", peern->minpoll);
759 if (peern->maxpoll != 0)
760 fprintf(df, " maxpoll %u", peern->maxpoll);
762 if (peern->ttl != 0) {
763 if (strlen(addr->address) > 8
764 && !memcmp(addr->address, "127.127.", 8))
765 fprintf(df, " mode %u", peern->ttl);
767 fprintf(df, " ttl %u", peern->ttl);
770 if (peern->peerversion != NTP_VERSION)
771 fprintf(df, " version %u", peern->peerversion);
773 if (peern->peerkey != 0)
774 fprintf(df, " key %u", peern->peerkey);
776 if (peern->group != NULL)
777 fprintf(df, " ident \"%s\"", peern->group);
779 atrv = HEAD_PFIFO(peern->peerflags);
780 for ( ; atrv != NULL; atrv = atrv->link) {
781 INSIST(T_Flag == atrv->attr);
782 INSIST(T_Integer == atrv->type);
783 fprintf(df, " %s", keyword(atrv->value.i));
788 addr_opts = HEAD_PFIFO(ptree->fudge);
789 for ( ; addr_opts != NULL; addr_opts = addr_opts->link) {
790 peer_addr = peern->addr;
791 fudge_addr = addr_opts->addr;
793 s1 = peer_addr->address;
794 s2 = fudge_addr->address;
799 fprintf(df, "fudge %s", s1);
801 for (atrv = HEAD_PFIFO(addr_opts->options);
805 switch (atrv->type) {
808 fprintf(df, "\n# dump error:\n"
809 "# unknown fudge atrv->type %d\n"
810 "fudge %s", atrv->type,
815 fprintf(df, " %s %s",
817 normal_dtoa(atrv->value.d));
821 fprintf(df, " %s %d",
827 fprintf(df, " %s %s",
837 addr = HEAD_PFIFO(ptree->manycastserver);
839 fprintf(df, "manycastserver");
840 for ( ; addr != NULL; addr = addr->link)
841 fprintf(df, " %s", addr->address);
845 addr = HEAD_PFIFO(ptree->multicastclient);
847 fprintf(df, "multicastclient");
848 for ( ; addr != NULL; addr = addr->link)
849 fprintf(df, " %s", addr->address);
854 for (unpeern = HEAD_PFIFO(ptree->unpeers);
856 unpeern = unpeern->link)
857 fprintf(df, "unpeer %s\n", unpeern->addr->address);
859 atrv = HEAD_PFIFO(ptree->mru_opts);
862 for ( ; atrv != NULL; atrv = atrv->link)
863 fprintf(df, " %s %d", keyword(atrv->attr),
868 atrv = HEAD_PFIFO(ptree->discard_opts);
870 fprintf(df, "discard");
871 for ( ; atrv != NULL; atrv = atrv->link)
872 fprintf(df, " %s %d", keyword(atrv->attr),
878 for (rest_node = HEAD_PFIFO(ptree->restrict_opts);
880 rest_node = rest_node->link) {
882 if (NULL == rest_node->addr) {
884 flags = HEAD_PFIFO(rest_node->flags);
885 for ( ; flags != NULL; flags = flags->link)
886 if (T_Source == flags->i) {
891 s = rest_node->addr->address;
893 fprintf(df, "restrict %s", s);
894 if (rest_node->mask != NULL)
895 fprintf(df, " mask %s",
896 rest_node->mask->address);
897 flags = HEAD_PFIFO(rest_node->flags);
898 for ( ; flags != NULL; flags = flags->link)
899 if (T_Source != flags->i)
900 fprintf(df, " %s", keyword(flags->i));
904 rule_node = HEAD_PFIFO(ptree->nic_rules);
905 for ( ; rule_node != NULL; rule_node = rule_node->link) {
906 fprintf(df, "interface %s %s\n",
907 keyword(rule_node->action),
908 (rule_node->match_class)
909 ? keyword(rule_node->match_class)
910 : rule_node->if_name);
913 str_node = HEAD_PFIFO(ptree->phone);
914 if (str_node != NULL) {
915 fprintf(df, "phone");
916 for ( ; str_node != NULL; str_node = str_node->link)
917 fprintf(df, " \"%s\"", str_node->s);
921 setv_node = HEAD_PFIFO(ptree->setvar);
922 for ( ; setv_node != NULL; setv_node = setv_node->link) {
923 s1 = quote_if_needed(setv_node->var);
924 s2 = quote_if_needed(setv_node->val);
925 fprintf(df, "setvar %s = %s", s1, s2);
928 if (setv_node->isdefault)
929 fprintf(df, " default");
933 i_n = HEAD_PFIFO(ptree->ttl);
936 for( ; i_n != NULL; i_n = i_n->link)
937 fprintf(df, " %d", i_n->i);
941 addr_opts = HEAD_PFIFO(ptree->trap);
942 for ( ; addr_opts != NULL; addr_opts = addr_opts->link) {
943 addr = addr_opts->addr;
944 fprintf(df, "trap %s", addr->address);
945 atrv = HEAD_PFIFO(addr_opts->options);
946 for ( ; atrv != NULL; atrv = atrv->link) {
947 switch (atrv->attr) {
950 fprintf(df, "\n# dump error:\n"
951 "# unknown trap token %d\n"
952 "trap %s", atrv->attr,
957 fprintf(df, " port %d", atrv->value.i);
961 fprintf(df, " interface %s",
969 counter_set = HEAD_PFIFO(ptree->reset_counters);
970 if (counter_set != NULL) {
971 fprintf(df, "reset");
972 for ( ; counter_set != NULL;
973 counter_set = counter_set->link)
974 fprintf(df, " %s", keyword(counter_set->i));
980 #endif /* SAVECONFIG */
984 /* generic fifo routines for structs linked by 1st member */
997 pf = emalloc_zero(sizeof(*pf));
999 CHECK_FIFO_CONSISTENCY(*pf);
1001 LINK_FIFO(*pf, pe, link);
1002 CHECK_FIFO_CONSISTENCY(*pf);
1024 CONCAT_FIFO(*pf1, *pf2, link);
1031 /* FUNCTIONS FOR CREATING NODES ON THE SYNTAX TREE
1032 * -----------------------------------------------
1043 my_val = emalloc_zero(sizeof(*my_val));
1044 my_val->attr = attr;
1045 my_val->value.d = value;
1046 my_val->type = T_Double;
1060 my_val = emalloc_zero(sizeof(*my_val));
1061 my_val->attr = attr;
1062 my_val->value.i = value;
1063 my_val->type = T_Integer;
1077 my_val = emalloc_zero(sizeof(*my_val));
1078 my_val->attr = attr;
1079 my_val->value.u = value;
1080 my_val->type = T_U_int;
1087 create_attr_rangeval(
1095 my_val = emalloc_zero(sizeof(*my_val));
1096 my_val->attr = attr;
1097 my_val->value.r.first = first;
1098 my_val->value.r.last = last;
1099 my_val->type = T_Intrange;
1113 my_val = emalloc_zero(sizeof(*my_val));
1114 my_val->attr = attr;
1115 if (NULL == s) /* free() hates NULL */
1117 my_val->value.s = s;
1118 my_val->type = T_String;
1131 i_n = emalloc_zero(sizeof(*i_n));
1145 sn = emalloc_zero(sizeof(*sn));
1153 create_address_node(
1158 address_node *my_node;
1160 NTP_REQUIRE(NULL != addr);
1161 NTP_REQUIRE(AF_INET == type ||
1162 AF_INET6 == type || AF_UNSPEC == type);
1163 my_node = emalloc_zero(sizeof(*my_node));
1164 my_node->address = addr;
1165 my_node->type = (u_short)type;
1172 destroy_address_node(
1173 address_node *my_node
1176 if (NULL == my_node)
1178 NTP_REQUIRE(NULL != my_node->address);
1180 free(my_node->address);
1188 address_node * addr,
1189 attr_val_fifo * options
1197 my_node = emalloc_zero(sizeof(*my_node));
1199 /* Initialize node values to default */
1200 my_node->peerversion = NTP_VERSION;
1202 /* Now set the node to the read values */
1203 my_node->host_mode = hmode;
1204 my_node->addr = addr;
1207 * the options FIFO mixes items that will be saved in the
1208 * peer_node as explicit members, such as minpoll, and
1209 * those that are moved intact to the peer_node's peerflags
1210 * FIFO. The options FIFO is consumed and reclaimed here.
1213 if (options != NULL)
1214 CHECK_FIFO_CONSISTENCY(*options);
1215 while (options != NULL) {
1216 UNLINK_FIFO(option, *options, link);
1217 if (NULL == option) {
1223 /* Check the kind of option being set */
1224 switch (option->attr) {
1227 APPEND_G_FIFO(my_node->peerflags, option);
1232 if (option->value.i < NTP_MINPOLL ||
1233 option->value.i > UCHAR_MAX) {
1235 "minpoll: provided value (%d) is out of range [%d-%d])",
1236 option->value.i, NTP_MINPOLL,
1238 my_node->minpoll = NTP_MINPOLL;
1241 (u_char)option->value.u;
1246 if (option->value.i < 0 ||
1247 option->value.i > NTP_MAXPOLL) {
1249 "maxpoll: provided value (%d) is out of range [0-%d])",
1250 option->value.i, NTP_MAXPOLL);
1251 my_node->maxpoll = NTP_MAXPOLL;
1254 (u_char)option->value.u;
1259 if (option->value.u >= MAX_TTL) {
1260 msyslog(LOG_ERR, "ttl: invalid argument");
1263 my_node->ttl = (u_char)option->value.u;
1268 my_node->ttl = option->value.u;
1272 if (option->value.u >= KEYID_T_MAX) {
1273 msyslog(LOG_ERR, "key: invalid argument");
1277 (keyid_t)option->value.u;
1282 if (option->value.u >= UCHAR_MAX) {
1283 msyslog(LOG_ERR, "version: invalid argument");
1286 my_node->peerversion =
1287 (u_char)option->value.u;
1292 my_node->group = option->value.s;
1297 "Unknown peer/server option token %s",
1298 token_name(option->attr));
1305 /* Check if errors were reported. If yes, ignore the node */
1320 unpeer_node * my_node;
1324 my_node = emalloc_zero(sizeof(*my_node));
1327 * From the parser's perspective an association ID fits into
1328 * its generic T_String definition of a name/address "address".
1329 * We treat all valid 16-bit numbers as association IDs.
1331 pch = addr->address;
1332 while (*pch && isdigit(*pch))
1336 && 1 == sscanf(addr->address, "%u", &u)
1337 && u <= ASSOCID_MAX) {
1338 my_node->assocID = (associd_t)u;
1339 destroy_address_node(addr);
1340 my_node->addr = NULL;
1342 my_node->assocID = 0;
1343 my_node->addr = addr;
1350 create_filegen_node(
1352 attr_val_fifo * options
1355 filegen_node *my_node;
1357 my_node = emalloc_zero(sizeof(*my_node));
1358 my_node->filegen_token = filegen_token;
1359 my_node->options = options;
1366 create_restrict_node(
1367 address_node * addr,
1368 address_node * mask,
1373 restrict_node *my_node;
1375 my_node = emalloc_zero(sizeof(*my_node));
1376 my_node->addr = addr;
1377 my_node->mask = mask;
1378 my_node->flags = flags;
1379 my_node->line_no = line_no;
1386 destroy_restrict_node(
1387 restrict_node *my_node
1390 /* With great care, free all the memory occupied by
1393 destroy_address_node(my_node->addr);
1394 destroy_address_node(my_node->mask);
1395 destroy_int_fifo(my_node->flags);
1409 UNLINK_FIFO(i_n, *fifo, link);
1420 destroy_string_fifo(
1428 UNLINK_FIFO(sn, *fifo, link);
1440 destroy_attr_val_fifo(
1441 attr_val_fifo * av_fifo
1446 if (av_fifo != NULL) {
1448 UNLINK_FIFO(av, *av_fifo, link);
1451 if (T_String == av->type)
1461 destroy_filegen_fifo(
1469 UNLINK_FIFO(fg, *fifo, link);
1472 destroy_attr_val_fifo(fg->options);
1481 destroy_restrict_fifo(
1482 restrict_fifo * fifo
1489 UNLINK_FIFO(rn, *fifo, link);
1492 destroy_restrict_node(rn);
1500 destroy_setvar_fifo(
1508 UNLINK_FIFO(sv, *fifo, link);
1521 destroy_addr_opts_fifo(
1522 addr_opts_fifo * fifo
1525 addr_opts_node * aon;
1529 UNLINK_FIFO(aon, *fifo, link);
1532 destroy_address_node(aon->addr);
1533 destroy_attr_val_fifo(aon->options);
1548 setvar_node * my_node;
1551 /* do not allow = in the variable name */
1552 pch = strchr(var, '=');
1556 /* Now store the string into a setvar_node */
1557 my_node = emalloc_zero(sizeof(*my_node));
1560 my_node->isdefault = isdefault;
1567 create_nic_rule_node(
1569 char *if_name, /* interface name or numeric address */
1573 nic_rule_node *my_node;
1575 NTP_REQUIRE(match_class != 0 || if_name != NULL);
1577 my_node = emalloc_zero(sizeof(*my_node));
1578 my_node->match_class = match_class;
1579 my_node->if_name = if_name;
1580 my_node->action = action;
1587 create_addr_opts_node(
1588 address_node * addr,
1589 attr_val_fifo * options
1592 addr_opts_node *my_node;
1594 my_node = emalloc_zero(sizeof(*my_node));
1595 my_node->addr = addr;
1596 my_node->options = options;
1604 create_sim_script_info(
1606 attr_val_fifo * script_queue
1609 script_info *my_info;
1610 attr_val *my_attr_val;
1612 my_info = emalloc_zero(sizeof(*my_info));
1614 /* Initialize Script Info with default values*/
1615 my_info->duration = duration;
1616 my_info->prop_delay = NET_DLY;
1617 my_info->proc_delay = PROC_DLY;
1619 /* Traverse the script_queue and fill out non-default values */
1621 for (my_attr_val = HEAD_PFIFO(script_queue);
1622 my_attr_val != NULL;
1623 my_attr_val = my_attr_val->link) {
1625 /* Set the desired value */
1626 switch (my_attr_val->attr) {
1629 my_info->freq_offset = my_attr_val->value.d;
1633 my_info->wander = my_attr_val->value.d;
1637 my_info->jitter = my_attr_val->value.d;
1641 my_info->prop_delay = my_attr_val->value.d;
1645 my_info->proc_delay = my_attr_val->value.d;
1649 msyslog(LOG_ERR, "Unknown script token %d",
1665 const char addr_prefix[] = "192.168.0.";
1666 static int curr_addr_num = 1;
1667 #define ADDR_LENGTH 16 + 1 /* room for 192.168.1.255 */
1668 char addr_string[ADDR_LENGTH];
1669 sockaddr_u *final_addr;
1670 struct addrinfo *ptr;
1673 final_addr = emalloc(sizeof(*final_addr));
1675 if (addr->type == T_String) {
1676 snprintf(addr_string, sizeof(addr_string), "%s%d",
1677 addr_prefix, curr_addr_num++);
1678 printf("Selecting ip address %s for hostname %s\n",
1679 addr_string, addr->address);
1680 gai_err = getaddrinfo(addr_string, "ntp", NULL, &ptr);
1682 gai_err = getaddrinfo(addr->address, "ntp", NULL, &ptr);
1686 fprintf(stderr, "ERROR!! Could not get a new address\n");
1689 memcpy(final_addr, ptr->ai_addr, ptr->ai_addrlen);
1690 fprintf(stderr, "Successful in setting ip address of simulated server to: %s\n",
1702 address_node * addr,
1703 double server_offset,
1704 script_info_fifo * script
1707 server_info *my_info;
1709 my_info = emalloc_zero(sizeof(*my_info));
1710 my_info->server_time = server_offset;
1711 my_info->addr = get_next_address(addr);
1712 my_info->script = script;
1713 UNLINK_FIFO(my_info->curr_script, *my_info->script, link);
1721 attr_val_fifo * init_opts,
1722 server_info_fifo * servers
1727 my_node = emalloc(sizeof(*my_node));
1728 my_node->init_opts = init_opts;
1729 my_node->servers = servers;
1737 /* FUNCTIONS FOR PERFORMING THE CONFIGURATION
1738 * ------------------------------------------
1747 sockaddr_u addr_sock;
1748 address_node * addr_node;
1750 if (ptree->broadcastclient)
1751 proto_config(PROTO_BROADCLIENT, ptree->broadcastclient,
1754 addr_node = HEAD_PFIFO(ptree->manycastserver);
1755 while (addr_node != NULL) {
1756 ZERO_SOCK(&addr_sock);
1757 AF(&addr_sock) = addr_node->type;
1758 if (1 == getnetnum(addr_node->address, &addr_sock, 1,
1760 proto_config(PROTO_MULTICAST_ADD,
1762 sys_manycastserver = 1;
1764 addr_node = addr_node->link;
1767 /* Configure the multicast clients */
1768 addr_node = HEAD_PFIFO(ptree->multicastclient);
1769 if (addr_node != NULL) {
1771 ZERO_SOCK(&addr_sock);
1772 AF(&addr_sock) = addr_node->type;
1773 if (1 == getnetnum(addr_node->address,
1774 &addr_sock, 1, t_UNK)) {
1775 proto_config(PROTO_MULTICAST_ADD, 0, 0.,
1778 addr_node = addr_node->link;
1779 } while (addr_node != NULL);
1780 proto_config(PROTO_MULTICAST_ADD, 1, 0., NULL);
1788 destroy_address_fifo(
1789 address_fifo * pfifo
1792 address_node * addr_node;
1794 if (pfifo != NULL) {
1796 UNLINK_FIFO(addr_node, *pfifo, link);
1797 if (addr_node == NULL)
1799 destroy_address_node(addr_node);
1807 free_config_other_modes(
1811 FREE_ADDRESS_FIFO(ptree->manycastserver);
1812 FREE_ADDRESS_FIFO(ptree->multicastclient);
1814 #endif /* FREE_CFG_T */
1832 /* Crypto Command */
1834 item = -1; /* quiet warning */
1835 my_val = HEAD_PFIFO(ptree->auth.crypto_cmd_list);
1836 for (; my_val != NULL; my_val = my_val->link) {
1837 switch (my_val->attr) {
1844 item = CRYPTO_CONF_PRIV;
1848 item = CRYPTO_CONF_IDENT;
1852 item = CRYPTO_CONF_PW;
1856 item = CRYPTO_CONF_RAND;
1860 item = CRYPTO_CONF_NID;
1863 crypto_config(item, my_val->value.s);
1865 #endif /* AUTOKEY */
1867 /* Keysdir Command */
1868 if (ptree->auth.keysdir) {
1869 if (keysdir != default_keysdir)
1871 keysdir = estrdup(ptree->auth.keysdir);
1875 /* ntp_signd_socket Command */
1876 if (ptree->auth.ntp_signd_socket) {
1877 if (ntp_signd_socket != default_ntp_signd_socket)
1878 free(ntp_signd_socket);
1879 ntp_signd_socket = estrdup(ptree->auth.ntp_signd_socket);
1883 if (ptree->auth.cryptosw && !cryptosw) {
1887 #endif /* AUTOKEY */
1890 * Count the number of trusted keys to preallocate storage and
1891 * size the hash table.
1894 my_val = HEAD_PFIFO(ptree->auth.trusted_key_list);
1895 for (; my_val != NULL; my_val = my_val->link) {
1896 if (T_Integer == my_val->type) {
1897 first = my_val->value.i;
1898 if (first > 1 && first <= NTP_MAXKEY)
1901 REQUIRE(T_Intrange == my_val->type);
1902 first = my_val->value.r.first;
1903 last = my_val->value.r.last;
1904 if (!(first > last || first < 1 ||
1905 last > NTP_MAXKEY)) {
1906 count += 1 + last - first;
1910 auth_prealloc_symkeys(count);
1913 if (ptree->auth.keys)
1914 getauthkeys(ptree->auth.keys);
1916 /* Control Key Command */
1917 if (ptree->auth.control_key)
1918 ctl_auth_keyid = (keyid_t)ptree->auth.control_key;
1920 /* Requested Key Command */
1921 if (ptree->auth.request_key) {
1922 DPRINTF(4, ("set info_auth_keyid to %08lx\n",
1923 (u_long) ptree->auth.request_key));
1924 info_auth_keyid = (keyid_t)ptree->auth.request_key;
1927 /* Trusted Key Command */
1928 my_val = HEAD_PFIFO(ptree->auth.trusted_key_list);
1929 for (; my_val != NULL; my_val = my_val->link) {
1930 if (T_Integer == my_val->type) {
1931 first = my_val->value.i;
1932 if (first >= 1 && first <= NTP_MAXKEY) {
1933 authtrust(first, TRUE);
1936 "Ignoring invalid trustedkey %d, min 1 max %d.",
1940 first = my_val->value.r.first;
1941 last = my_val->value.r.last;
1942 if (first > last || first < 1 ||
1943 last > NTP_MAXKEY) {
1945 "Ignoring invalid trustedkey range %d ... %d, min 1 max %d.",
1946 first, last, NTP_MAXKEY);
1948 for (i = first; i <= last; i++) {
1956 /* crypto revoke command */
1957 if (ptree->auth.revoke)
1958 sys_revoke = 1UL << ptree->auth.revoke;
1959 #endif /* AUTOKEY */
1970 destroy_attr_val_fifo(ptree->auth.crypto_cmd_list);
1971 ptree->auth.crypto_cmd_list = NULL;
1972 destroy_attr_val_fifo(ptree->auth.trusted_key_list);
1973 ptree->auth.trusted_key_list = NULL;
1975 #endif /* FREE_CFG_T */
1987 item = -1; /* quiet warning */
1988 tos = HEAD_PFIFO(ptree->orphan_cmds);
1989 for (; tos != NULL; tos = tos->link) {
1998 if (val > STRATUM_UNSPEC - 1) {
1999 msyslog(LOG_WARNING,
2000 "Using maximum tos ceiling %d, %g requested",
2001 STRATUM_UNSPEC - 1, val);
2002 val = STRATUM_UNSPEC - 1;
2004 item = PROTO_CEILING;
2012 item = PROTO_COHORT;
2016 item = PROTO_ORPHAN;
2020 item = PROTO_ORPHWAIT;
2024 item = PROTO_MINDISP;
2028 item = PROTO_MAXDIST;
2032 item = PROTO_MINCLOCK;
2036 item = PROTO_MAXCLOCK;
2040 item = PROTO_MINSANE;
2044 item = PROTO_BEACON;
2047 proto_config(item, 0, val, NULL);
2058 FREE_ATTR_VAL_FIFO(ptree->orphan_cmds);
2060 #endif /* FREE_CFG_T */
2068 int_node *pfilegen_token;
2069 const char *filegen_string;
2070 const char *filegen_file;
2072 filegen_node *my_node;
2077 /* Set the statistics directory */
2078 if (ptree->stats_dir)
2079 stats_config(STATS_STATSDIR, ptree->stats_dir);
2082 * Calling filegen_get is brain dead. Doing a string
2083 * comparison to find the relavant filegen structure is
2086 * Through the parser, we already know which filegen is
2087 * being specified. Hence, we should either store a
2088 * pointer to the specified structure in the syntax tree
2089 * or an index into a filegen array.
2091 * Need to change the filegen code to reflect the above.
2094 /* Turn on the specified statistics */
2095 pfilegen_token = HEAD_PFIFO(ptree->stats_list);
2096 for (; pfilegen_token != NULL; pfilegen_token = pfilegen_token->link) {
2097 filegen_string = keyword(pfilegen_token->i);
2098 filegen = filegen_get(filegen_string);
2099 if (NULL == filegen) {
2101 "stats %s unrecognized",
2105 DPRINTF(4, ("enabling filegen for %s statistics '%s%s'\n",
2106 filegen_string, filegen->dir,
2108 filegen_flag = filegen->flag;
2109 filegen_flag |= FGEN_FLAG_ENABLED;
2110 filegen_config(filegen, statsdir, filegen_string,
2111 filegen->type, filegen_flag);
2114 /* Configure the statistics with the options */
2115 my_node = HEAD_PFIFO(ptree->filegen_opts);
2116 for (; my_node != NULL; my_node = my_node->link) {
2117 filegen_string = keyword(my_node->filegen_token);
2118 filegen = filegen_get(filegen_string);
2119 if (NULL == filegen) {
2121 "filegen category '%s' unrecognized",
2125 filegen_file = filegen_string;
2127 /* Initialize the filegen variables to their pre-configuration states */
2128 filegen_flag = filegen->flag;
2129 filegen_type = filegen->type;
2131 /* "filegen ... enabled" is the default (when filegen is used) */
2132 filegen_flag |= FGEN_FLAG_ENABLED;
2134 my_opts = HEAD_PFIFO(my_node->options);
2135 for (; my_opts != NULL; my_opts = my_opts->link) {
2136 switch (my_opts->attr) {
2139 filegen_file = my_opts->value.s;
2143 switch (my_opts->value.i) {
2150 filegen_type = FILEGEN_NONE;
2154 filegen_type = FILEGEN_PID;
2158 filegen_type = FILEGEN_DAY;
2162 filegen_type = FILEGEN_WEEK;
2166 filegen_type = FILEGEN_MONTH;
2170 filegen_type = FILEGEN_YEAR;
2174 filegen_type = FILEGEN_AGE;
2180 switch (my_opts->value.i) {
2183 filegen_flag |= FGEN_FLAG_LINK;
2187 filegen_flag &= ~FGEN_FLAG_LINK;
2191 filegen_flag |= FGEN_FLAG_ENABLED;
2195 filegen_flag &= ~FGEN_FLAG_ENABLED;
2200 "Unknown filegen flag token %d",
2208 "Unknown filegen option token %d",
2213 filegen_config(filegen, statsdir, filegen_file,
2214 filegen_type, filegen_flag);
2221 free_config_monitor(
2225 if (ptree->stats_dir) {
2226 free(ptree->stats_dir);
2227 ptree->stats_dir = NULL;
2230 FREE_INT_FIFO(ptree->stats_list);
2231 FREE_FILEGEN_FIFO(ptree->filegen_opts);
2233 #endif /* FREE_CFG_T */
2242 static int warned_signd;
2244 restrict_node * my_node;
2245 int_node * curr_flag;
2248 struct addrinfo hints;
2249 struct addrinfo * ai_list;
2250 struct addrinfo * pai;
2252 int restrict_default;
2256 const char * signd_warning =
2257 #ifdef HAVE_NTP_SIGND
2258 "MS-SNTP signd operations currently block ntpd degrading service to all clients.";
2260 "mssntp restrict bit ignored, this ntpd was configured without --enable-ntp-signd.";
2263 /* Configure the mru options */
2264 my_opt = HEAD_PFIFO(ptree->mru_opts);
2265 for (; my_opt != NULL; my_opt = my_opt->link) {
2269 switch (my_opt->attr) {
2272 if (0 <= my_opt->value.i)
2273 mru_incalloc = my_opt->value.u;
2279 if (0 <= my_opt->value.i)
2280 mru_incalloc = (my_opt->value.u * 1024U)
2281 / sizeof(mon_entry);
2287 if (0 <= my_opt->value.i)
2288 mru_initalloc = my_opt->value.u;
2294 if (0 <= my_opt->value.i)
2295 mru_initalloc = (my_opt->value.u * 1024U)
2296 / sizeof(mon_entry);
2302 if (0 <= my_opt->value.i)
2303 mru_mindepth = my_opt->value.u;
2309 mru_maxage = my_opt->value.i;
2313 if (0 <= my_opt->value.i)
2314 mru_maxdepth = my_opt->value.u;
2316 mru_maxdepth = UINT_MAX;
2320 if (0 <= my_opt->value.i)
2321 mru_maxdepth = (my_opt->value.u * 1024U) /
2324 mru_maxdepth = UINT_MAX;
2329 "Unknown mru option %s (%d)",
2330 keyword(my_opt->attr), my_opt->attr);
2335 "mru %s %d out of range, ignored.",
2336 keyword(my_opt->attr), my_opt->value.i);
2339 /* Configure the discard options */
2340 my_opt = HEAD_PFIFO(ptree->discard_opts);
2341 for (; my_opt != NULL; my_opt = my_opt->link) {
2343 switch (my_opt->attr) {
2346 if (0 <= my_opt->value.i &&
2347 my_opt->value.i <= UCHAR_MAX)
2348 ntp_minpoll = (u_char)my_opt->value.u;
2351 "discard average %d out of range, ignored.",
2356 ntp_minpkt = my_opt->value.i;
2360 mon_age = my_opt->value.i;
2365 "Unknown discard option %s (%d)",
2366 keyword(my_opt->attr), my_opt->attr);
2371 /* Configure the restrict options */
2372 my_node = HEAD_PFIFO(ptree->restrict_opts);
2373 for (; my_node != NULL; my_node = my_node->link) {
2374 /* Parse the flags */
2378 curr_flag = HEAD_PFIFO(my_node->flags);
2379 for (; curr_flag != NULL; curr_flag = curr_flag->link) {
2380 switch (curr_flag->i) {
2387 mflags |= RESM_NTPONLY;
2391 mflags |= RESM_SOURCE;
2399 flags |= RES_IGNORE;
2407 flags |= RES_MSSNTP;
2411 flags |= RES_LIMITED;
2415 flags |= RES_LPTRAP;
2419 flags |= RES_NOMODIFY;
2423 flags |= RES_NOMRULIST;
2427 flags |= RES_NOPEER;
2431 flags |= RES_NOQUERY;
2435 flags |= RES_DONTSERVE;
2439 flags |= RES_NOTRAP;
2443 flags |= RES_DONTTRUST;
2447 flags |= RES_VERSION;
2452 if ((RES_MSSNTP & flags) && !warned_signd) {
2454 fprintf(stderr, "%s\n", signd_warning);
2455 msyslog(LOG_WARNING, "%s", signd_warning);
2458 /* It would be swell if we could identify the line number */
2459 if ((RES_KOD & flags) && !(RES_LIMITED & flags)) {
2460 char *kod_where = (my_node->addr)
2461 ? my_node->addr->address
2462 : (mflags & RESM_SOURCE)
2465 char *kod_warn = "KOD does nothing without LIMITED.";
2467 fprintf(stderr, "restrict %s: %s\n", kod_where, kod_warn);
2468 msyslog(LOG_WARNING, "restrict %s: %s", kod_where, kod_warn);
2474 restrict_default = 0;
2476 if (NULL == my_node->addr) {
2478 if (!(RESM_SOURCE & mflags)) {
2480 * The user specified a default rule
2481 * without a -4 / -6 qualifier, add to
2484 restrict_default = 1;
2486 /* apply "restrict source ..." */
2487 DPRINTF(1, ("restrict source template mflags %x flags %x\n",
2489 hack_restrict(RESTRICT_FLAGS, NULL,
2490 NULL, mflags, flags, 0);
2494 /* Resolve the specified address */
2495 AF(&addr) = (u_short)my_node->addr->type;
2497 if (getnetnum(my_node->addr->address,
2498 &addr, 1, t_UNK) != 1) {
2500 * Attempt a blocking lookup. This
2501 * is in violation of the nonblocking
2502 * design of ntpd's mainline code. The
2503 * alternative of running without the
2504 * restriction until the name resolved
2506 * Ideally some scheme could be used for
2507 * restrict directives in the startup
2508 * ntp.conf to delay starting up the
2509 * protocol machinery until after all
2510 * restrict hosts have been resolved.
2514 hints.ai_protocol = IPPROTO_UDP;
2515 hints.ai_socktype = SOCK_DGRAM;
2516 hints.ai_family = my_node->addr->type;
2517 rc = getaddrinfo(my_node->addr->address,
2522 "restrict: ignoring line %d, address/host '%s' unusable.",
2524 my_node->addr->address);
2527 INSIST(ai_list != NULL);
2529 INSIST(pai->ai_addr != NULL);
2530 INSIST(sizeof(addr) >=
2532 memcpy(&addr, pai->ai_addr,
2534 INSIST(AF_INET == AF(&addr) ||
2535 AF_INET6 == AF(&addr));
2538 SET_HOSTMASK(&mask, AF(&addr));
2540 /* Resolve the mask */
2541 if (my_node->mask) {
2543 AF(&mask) = my_node->mask->type;
2544 if (getnetnum(my_node->mask->address,
2545 &mask, 1, t_MSK) != 1) {
2547 "restrict: ignoring line %d, mask '%s' unusable.",
2549 my_node->mask->address);
2556 if (restrict_default) {
2557 AF(&addr) = AF_INET;
2558 AF(&mask) = AF_INET;
2559 hack_restrict(RESTRICT_FLAGS, &addr,
2560 &mask, mflags, flags, 0);
2561 AF(&addr) = AF_INET6;
2562 AF(&mask) = AF_INET6;
2566 hack_restrict(RESTRICT_FLAGS, &addr,
2567 &mask, mflags, flags, 0);
2569 NULL != (pai = pai->ai_next)) {
2570 INSIST(pai->ai_addr != NULL);
2571 INSIST(sizeof(addr) >=
2574 memcpy(&addr, pai->ai_addr,
2576 INSIST(AF_INET == AF(&addr) ||
2577 AF_INET6 == AF(&addr));
2578 SET_HOSTMASK(&mask, AF(&addr));
2580 } while (pai != NULL);
2582 if (ai_list != NULL)
2583 freeaddrinfo(ai_list);
2595 FREE_ATTR_VAL_FIFO(ptree->mru_opts);
2596 FREE_ATTR_VAL_FIFO(ptree->discard_opts);
2597 FREE_RESTRICT_FIFO(ptree->restrict_opts);
2599 #endif /* FREE_CFG_T */
2607 attr_val * rlimit_av;
2609 rlimit_av = HEAD_PFIFO(ptree->rlimit);
2610 for (; rlimit_av != NULL; rlimit_av = rlimit_av->link) {
2611 switch (rlimit_av->attr) {
2618 if (rlimit_av->value.i != 0) {
2619 #if defined(RLIMIT_MEMLOCK)
2620 ntp_rlimit(RLIMIT_MEMLOCK,
2621 (rlim_t)(rlimit_av->value.i * 1024 * 1024),
2625 /* STDERR as well would be fine... */
2626 msyslog(LOG_WARNING, "'rlimit memlock' specified but is not available on this system.");
2627 #endif /* RLIMIT_MEMLOCK */
2634 #if defined(RLIMIT_STACK)
2635 ntp_rlimit(RLIMIT_STACK,
2636 (rlim_t)(rlimit_av->value.i * 4096),
2640 /* STDERR as well would be fine... */
2641 msyslog(LOG_WARNING, "'rlimit stacksize' specified but is not available on this system.");
2642 #endif /* RLIMIT_STACK */
2646 #if defined(RLIMIT_NOFILE)
2647 ntp_rlimit(RLIMIT_NOFILE,
2648 (rlim_t)(rlimit_av->value.i),
2652 /* STDERR as well would be fine... */
2653 msyslog(LOG_WARNING, "'rlimit filenum' specified but is not available on this system.");
2654 #endif /* RLIMIT_NOFILE */
2670 item = -1; /* quiet warning */
2671 tinker = HEAD_PFIFO(ptree->tinker);
2672 for (; tinker != NULL; tinker = tinker->link) {
2673 switch (tinker->attr) {
2692 item = LOOP_HUFFPUFF;
2704 item = LOOP_MINSTEP;
2711 loop_config(item, tinker->value.d);
2722 FREE_ATTR_VAL_FIFO(ptree->rlimit);
2730 FREE_ATTR_VAL_FIFO(ptree->tinker);
2732 #endif /* FREE_CFG_T */
2736 * config_nic_rules - apply interface listen/ignore/drop items
2744 nic_rule_node * curr_node;
2746 nic_rule_match match_type;
2747 nic_rule_action action;
2753 curr_node = HEAD_PFIFO(ptree->nic_rules);
2755 if (curr_node != NULL
2756 && (HAVE_OPT( NOVIRTUALIPS ) || HAVE_OPT( INTERFACE ))) {
2758 "interface/nic rules are not allowed with --interface (-I) or --novirtualips (-L)%s",
2759 (input_from_file) ? ", exiting" : "");
2760 if (input_from_file)
2766 for (; curr_node != NULL; curr_node = curr_node->link) {
2768 if_name = curr_node->if_name;
2769 if (if_name != NULL)
2770 if_name = estrdup(if_name);
2772 switch (curr_node->match_class) {
2776 * this assignment quiets a gcc "may be used
2777 * uninitialized" warning and is here for no
2780 match_type = MATCH_ALL;
2786 * 0 is out of range for valid token T_...
2787 * and in a nic_rules_node indicates the
2788 * interface descriptor is either a name or
2789 * address, stored in if_name in either case.
2791 INSIST(if_name != NULL);
2792 pchSlash = strchr(if_name, '/');
2793 if (pchSlash != NULL)
2795 if (is_ip_address(if_name, AF_UNSPEC, &addr)) {
2796 match_type = MATCH_IFADDR;
2797 if (pchSlash != NULL
2798 && 1 == sscanf(pchSlash + 1, "%d",
2801 SIZEOF_INADDR(AF(&addr));
2802 prefixlen = max(-1, prefixlen);
2803 prefixlen = min(prefixlen,
2807 match_type = MATCH_IFNAME;
2808 if (pchSlash != NULL)
2814 match_type = MATCH_ALL;
2818 match_type = MATCH_IPV4;
2822 match_type = MATCH_IPV6;
2826 match_type = MATCH_WILDCARD;
2830 switch (curr_node->action) {
2834 * this assignment quiets a gcc "may be used
2835 * uninitialized" warning and is here for no
2838 action = ACTION_LISTEN;
2843 action = ACTION_LISTEN;
2847 action = ACTION_IGNORE;
2851 action = ACTION_DROP;
2855 add_nic_rule(match_type, if_name, prefixlen,
2857 timer_interfacetimeout(current_time + 2);
2858 if (if_name != NULL)
2867 free_config_nic_rules(
2871 nic_rule_node *curr_node;
2873 if (ptree->nic_rules != NULL) {
2875 UNLINK_FIFO(curr_node, *ptree->nic_rules, link);
2876 if (NULL == curr_node)
2878 free(curr_node->if_name);
2881 free(ptree->nic_rules);
2882 ptree->nic_rules = NULL;
2885 #endif /* FREE_CFG_T */
2889 apply_enable_disable(
2890 attr_val_fifo * fifo,
2894 attr_val *curr_flag;
2896 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
2900 for (curr_flag = HEAD_PFIFO(fifo);
2902 curr_flag = curr_flag->link) {
2904 option = curr_flag->value.i;
2909 "can not apply enable/disable token %d, unknown",
2914 proto_config(PROTO_AUTHENTICATE, enable, 0., NULL);
2918 proto_config(PROTO_BROADCLIENT, enable, 0., NULL);
2922 proto_config(PROTO_CAL, enable, 0., NULL);
2926 proto_config(PROTO_KERNEL, enable, 0., NULL);
2930 proto_config(PROTO_MONITOR, enable, 0., NULL);
2934 proto_config(PROTO_NTP, enable, 0., NULL);
2938 proto_config(PROTO_MODE7, enable, 0., NULL);
2942 proto_config(PROTO_FILEGEN, enable, 0., NULL);
2945 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
2948 while (pentry->token) {
2949 if (pentry->token == option)
2953 if (!pentry->token) {
2955 "compat token %d not in bc_list[]",
2959 pentry->enabled = enable;
2972 apply_enable_disable(ptree->enable_opts, 1);
2973 apply_enable_disable(ptree->disable_opts, 0);
2979 free_config_system_opts(
2983 FREE_ATTR_VAL_FIFO(ptree->enable_opts);
2984 FREE_ATTR_VAL_FIFO(ptree->disable_opts);
2986 #endif /* FREE_CFG_T */
2996 my_lc = HEAD_PFIFO(ptree->logconfig);
2997 for (; my_lc != NULL; my_lc = my_lc->link) {
2998 switch (my_lc->attr) {
3001 ntp_syslogmask |= get_logmask(my_lc->value.s);
3005 ntp_syslogmask &= ~get_logmask(my_lc->value.s);
3009 ntp_syslogmask = get_logmask(my_lc->value.s);
3021 free_config_logconfig(
3025 FREE_ATTR_VAL_FIFO(ptree->logconfig);
3027 #endif /* FREE_CFG_T */
3040 sn = HEAD_PFIFO(ptree->phone);
3041 for (; sn != NULL; sn = sn->link) {
3042 /* need to leave array entry for NULL terminator */
3043 if (i < COUNTOF(sys_phone) - 1) {
3044 sys_phone[i++] = estrdup(sn->s);
3045 sys_phone[i] = NULL;
3048 "phone: Number of phone entries exceeds %lu. Ignoring phone %s...",
3049 (u_long)(COUNTOF(sys_phone) - 1), sn->s);
3062 FREE_STRING_FIFO(ptree->phone);
3064 #endif /* FREE_CFG_T */
3073 setvar_node *my_node;
3074 size_t varlen, vallen, octets;
3078 my_node = HEAD_PFIFO(ptree->setvar);
3079 for (; my_node != NULL; my_node = my_node->link) {
3080 varlen = strlen(my_node->var);
3081 vallen = strlen(my_node->val);
3082 octets = varlen + vallen + 1 + 1;
3083 str = erealloc(str, octets);
3084 snprintf(str, octets, "%s=%s", my_node->var,
3086 set_sys_var(str, octets, (my_node->isdefault)
3102 FREE_SETVAR_FIFO(ptree->setvar);
3104 #endif /* FREE_CFG_T */
3116 curr_ttl = HEAD_PFIFO(ptree->ttl);
3117 for (; curr_ttl != NULL; curr_ttl = curr_ttl->link) {
3118 if (i < COUNTOF(sys_ttl))
3119 sys_ttl[i++] = (u_char)curr_ttl->i;
3122 "ttl: Number of TTL entries exceeds %lu. Ignoring TTL %d...",
3123 (u_long)COUNTOF(sys_ttl), curr_ttl->i);
3136 FREE_INT_FIFO(ptree->ttl);
3138 #endif /* FREE_CFG_T */
3147 addr_opts_node *curr_trap;
3149 sockaddr_u addr_sock;
3150 sockaddr_u peeraddr;
3151 struct interface *localaddr;
3152 struct addrinfo hints;
3154 settrap_parms *pstp;
3159 /* silence warning about addr_sock potentially uninitialized */
3160 AF(&addr_sock) = AF_UNSPEC;
3162 curr_trap = HEAD_PFIFO(ptree->trap);
3163 for (; curr_trap != NULL; curr_trap = curr_trap->link) {
3168 curr_opt = HEAD_PFIFO(curr_trap->options);
3169 for (; curr_opt != NULL; curr_opt = curr_opt->link) {
3170 if (T_Port == curr_opt->attr) {
3171 if (curr_opt->value.i < 1
3172 || curr_opt->value.i > USHRT_MAX) {
3174 "invalid port number "
3179 port = (u_short)curr_opt->value.i;
3181 else if (T_Interface == curr_opt->attr) {
3182 /* Resolve the interface address */
3183 ZERO_SOCK(&addr_sock);
3184 if (getnetnum(curr_opt->value.s,
3185 &addr_sock, 1, t_UNK) != 1) {
3190 localaddr = findinterface(&addr_sock);
3192 if (NULL == localaddr) {
3194 "can't find interface with address %s",
3201 /* Now process the trap for the specified interface
3207 ZERO_SOCK(&peeraddr);
3208 rc = getnetnum(curr_trap->addr->address,
3209 &peeraddr, 1, t_UNK);
3213 "trap: unable to use IP address %s.",
3214 curr_trap->addr->address);
3215 #else /* WORKER follows */
3217 * save context and hand it off
3218 * for name resolution.
3221 hints.ai_protocol = IPPROTO_UDP;
3222 hints.ai_socktype = SOCK_DGRAM;
3223 snprintf(port_text, sizeof(port_text),
3225 hints.ai_flags = Z_AI_NUMERICSERV;
3226 pstp = emalloc_zero(sizeof(*pstp));
3227 if (localaddr != NULL) {
3228 hints.ai_family = localaddr->family;
3229 pstp->ifaddr_nonnull = 1;
3230 memcpy(&pstp->ifaddr,
3232 sizeof(pstp->ifaddr));
3234 rc = getaddrinfo_sometime(
3235 curr_trap->addr->address,
3238 &trap_name_resolved,
3242 "config_trap: getaddrinfo_sometime(%s,%s): %m",
3243 curr_trap->addr->address,
3248 /* port is at same location for v4 and v6 */
3249 SET_PORT(&peeraddr, port);
3251 if (NULL == localaddr)
3252 localaddr = ANY_INTERFACE_CHOOSE(&peeraddr);
3254 AF(&peeraddr) = AF(&addr_sock);
3256 if (!ctlsettrap(&peeraddr, localaddr, 0,
3259 "set trap %s -> %s failed.",
3268 * trap_name_resolved()
3270 * Callback invoked when config_trap()'s DNS lookup completes.
3279 const char * service,
3280 const struct addrinfo * hints,
3281 const struct addrinfo * res
3284 settrap_parms *pstp;
3285 struct interface *localaddr;
3286 sockaddr_u peeraddr;
3294 "giving up resolving trap host %s: %s (%d)",
3295 name, gai_strerror(rescode), rescode);
3299 INSIST(sizeof(peeraddr) >= res->ai_addrlen);
3301 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
3303 if (pstp->ifaddr_nonnull)
3304 localaddr = findinterface(&pstp->ifaddr);
3305 if (NULL == localaddr)
3306 localaddr = ANY_INTERFACE_CHOOSE(&peeraddr);
3307 if (!ctlsettrap(&peeraddr, localaddr, 0, NTP_VERSION))
3308 msyslog(LOG_ERR, "set trap %s -> %s failed.",
3309 latoa(localaddr), stoa(&peeraddr));
3312 # endif /* WORKER */
3322 FREE_ADDR_OPTS_FIFO(ptree->trap);
3324 #endif /* FREE_CFG_T */
3333 addr_opts_node *curr_fudge;
3335 sockaddr_u addr_sock;
3336 address_node *addr_node;
3337 struct refclockstat clock_stat;
3340 curr_fudge = HEAD_PFIFO(ptree->fudge);
3341 for (; curr_fudge != NULL; curr_fudge = curr_fudge->link) {
3344 /* Get the reference clock address and
3345 * ensure that it is sane
3347 addr_node = curr_fudge->addr;
3348 ZERO_SOCK(&addr_sock);
3349 if (getnetnum(addr_node->address, &addr_sock, 1, t_REF)
3353 "unrecognized fudge reference clock address %s, line ignored",
3357 if (!ISREFCLOCKADR(&addr_sock)) {
3360 "inappropriate address %s for the fudge command, line ignored",
3364 /* Parse all the options to the fudge command */
3366 curr_opt = HEAD_PFIFO(curr_fudge->options);
3367 for (; curr_opt != NULL; curr_opt = curr_opt->link) {
3368 switch (curr_opt->attr) {
3371 clock_stat.haveflags |= CLK_HAVETIME1;
3372 clock_stat.fudgetime1 = curr_opt->value.d;
3376 clock_stat.haveflags |= CLK_HAVETIME2;
3377 clock_stat.fudgetime2 = curr_opt->value.d;
3381 clock_stat.haveflags |= CLK_HAVEVAL1;
3382 clock_stat.fudgeval1 = curr_opt->value.i;
3386 clock_stat.haveflags |= CLK_HAVEVAL2;
3387 clock_stat.fudgeval2 = 0;
3388 memcpy(&clock_stat.fudgeval2,
3390 min(strlen(curr_opt->value.s), 4));
3394 clock_stat.haveflags |= CLK_HAVEFLAG1;
3395 if (curr_opt->value.i)
3396 clock_stat.flags |= CLK_FLAG1;
3398 clock_stat.flags &= ~CLK_FLAG1;
3402 clock_stat.haveflags |= CLK_HAVEFLAG2;
3403 if (curr_opt->value.i)
3404 clock_stat.flags |= CLK_FLAG2;
3406 clock_stat.flags &= ~CLK_FLAG2;
3410 clock_stat.haveflags |= CLK_HAVEFLAG3;
3411 if (curr_opt->value.i)
3412 clock_stat.flags |= CLK_FLAG3;
3414 clock_stat.flags &= ~CLK_FLAG3;
3418 clock_stat.haveflags |= CLK_HAVEFLAG4;
3419 if (curr_opt->value.i)
3420 clock_stat.flags |= CLK_FLAG4;
3422 clock_stat.flags &= ~CLK_FLAG4;
3427 "Unexpected fudge flag %s (%d) for %s",
3428 token_name(curr_opt->attr),
3429 curr_opt->attr, stoa(&addr_sock));
3430 exit(curr_opt->attr ? curr_opt->attr : 1);
3435 refclock_control(&addr_sock, &clock_stat, NULL);
3448 FREE_ADDR_OPTS_FIFO(ptree->fudge);
3450 #endif /* FREE_CFG_T */
3461 curr_var = HEAD_PFIFO(ptree->vars);
3462 for (; curr_var != NULL; curr_var = curr_var->link) {
3463 /* Determine which variable to set and set it */
3464 switch (curr_var->attr) {
3466 case T_Broadcastdelay:
3467 proto_config(PROTO_BROADDELAY, 0, curr_var->value.d, NULL);
3471 loop_config(LOOP_TICK, curr_var->value.d);
3475 if ('\0' == curr_var->value.s[0]) {
3476 stats_drift_file = 0;
3477 msyslog(LOG_INFO, "config: driftfile disabled");
3479 stats_config(STATS_FREQ_FILE, curr_var->value.s);
3483 sys_ident = curr_var->value.s;
3486 case T_WanderThreshold: /* FALLTHROUGH */
3488 wander_threshold = curr_var->value.d;
3492 stats_config(STATS_LEAP_FILE, curr_var->value.s);
3496 stats_config(STATS_PID_FILE, curr_var->value.s);
3500 if (-1 == change_logfile(curr_var->value.s, TRUE))
3502 "Cannot open logfile %s: %m",
3506 case T_Saveconfigdir:
3507 if (saveconfigdir != NULL)
3508 free(saveconfigdir);
3509 len = strlen(curr_var->value.s);
3511 saveconfigdir = NULL;
3512 } else if (DIR_SEP != curr_var->value.s[len - 1]
3513 #ifdef SYS_WINNT /* slash is also a dir. sep. on Windows */
3514 && '/' != curr_var->value.s[len - 1]
3518 saveconfigdir = emalloc(len + 1);
3519 snprintf(saveconfigdir, len + 1,
3524 saveconfigdir = estrdup(
3531 sys_automax = curr_var->value.i;
3537 "config_vars(): unexpected token %d",
3550 FREE_ATTR_VAL_FIFO(ptree->vars);
3552 #endif /* FREE_CFG_T */
3555 /* Define a function to check if a resolved address is sane.
3556 * If yes, return 1, else return 0;
3559 is_sane_resolved_address(
3560 sockaddr_u * peeraddr,
3564 if (!ISREFCLOCKADR(peeraddr) && ISBADADR(peeraddr)) {
3566 "attempt to configure invalid address %s",
3571 * Shouldn't be able to specify multicast
3572 * address for server/peer!
3573 * and unicast address for manycastclient!
3575 if ((T_Server == hmode || T_Peer == hmode || T_Pool == hmode)
3576 && IS_MCAST(peeraddr)) {
3578 "attempt to configure invalid address %s",
3582 if (T_Manycastclient == hmode && !IS_MCAST(peeraddr)) {
3584 "attempt to configure invalid address %s",
3589 if (IS_IPV6(peeraddr) && !ipv6_works)
3592 /* Ok, all tests succeeded, now we can return 1 */
3599 get_correct_host_mode(
3607 case T_Manycastclient:
3614 return MODE_BROADCAST;
3623 * peerflag_bits() get config_peers() peerflags value from a
3624 * peer_node's queue of flag attr_val entries.
3634 /* translate peerflags options to bits */
3636 option = HEAD_PFIFO(pn->peerflags);
3637 for (; option != NULL; option = option->link) {
3638 switch (option->value.i) {
3645 peerflags |= FLAG_SKEY;
3649 peerflags |= FLAG_BURST;
3653 peerflags |= FLAG_IBURST;
3657 peerflags |= FLAG_NOSELECT;
3661 peerflags |= FLAG_PREEMPT;
3665 peerflags |= FLAG_PREFER;
3669 peerflags |= FLAG_TRUE;
3673 peerflags |= FLAG_XLEAVE;
3687 sockaddr_u peeraddr;
3688 struct addrinfo hints;
3689 peer_node * curr_peer;
3690 peer_resolved_ctx * ctx;
3693 /* add servers named on the command line with iburst implied */
3695 cmdline_server_count > 0;
3696 cmdline_server_count--, cmdline_servers++) {
3698 ZERO_SOCK(&peeraddr);
3700 * If we have a numeric address, we can safely
3701 * proceed in the mainline with it. Otherwise, hand
3702 * the hostname off to the blocking child.
3704 if (is_ip_address(*cmdline_servers, AF_UNSPEC,
3707 SET_PORT(&peeraddr, NTP_PORT);
3708 if (is_sane_resolved_address(&peeraddr,
3723 /* we have a hostname to resolve */
3725 ctx = emalloc_zero(sizeof(*ctx));
3726 ctx->family = AF_UNSPEC;
3727 ctx->host_mode = T_Server;
3728 ctx->hmode = MODE_CLIENT;
3729 ctx->version = NTP_VERSION;
3730 ctx->flags = FLAG_IBURST;
3733 hints.ai_family = (u_short)ctx->family;
3734 hints.ai_socktype = SOCK_DGRAM;
3735 hints.ai_protocol = IPPROTO_UDP;
3737 getaddrinfo_sometime(*cmdline_servers,
3740 &peer_name_resolved,
3742 # else /* !WORKER follows */
3744 "hostname %s can not be used, please use IP address instead.",
3745 curr_peer->addr->address);
3750 /* add associations from the configuration file */
3751 curr_peer = HEAD_PFIFO(ptree->peers);
3752 for (; curr_peer != NULL; curr_peer = curr_peer->link) {
3753 ZERO_SOCK(&peeraddr);
3754 /* Find the correct host-mode */
3755 hmode = get_correct_host_mode(curr_peer->host_mode);
3758 if (T_Pool == curr_peer->host_mode) {
3759 AF(&peeraddr) = curr_peer->addr->type;
3762 curr_peer->addr->address,
3765 curr_peer->peerversion,
3768 peerflag_bits(curr_peer),
3773 * If we have a numeric address, we can safely
3774 * proceed in the mainline with it. Otherwise, hand
3775 * the hostname off to the blocking child.
3777 } else if (is_ip_address(curr_peer->addr->address,
3778 curr_peer->addr->type, &peeraddr)) {
3780 SET_PORT(&peeraddr, NTP_PORT);
3781 if (is_sane_resolved_address(&peeraddr,
3782 curr_peer->host_mode))
3788 curr_peer->peerversion,
3791 peerflag_bits(curr_peer),
3796 /* we have a hostname to resolve */
3798 ctx = emalloc_zero(sizeof(*ctx));
3799 ctx->family = curr_peer->addr->type;
3800 ctx->host_mode = curr_peer->host_mode;
3802 ctx->version = curr_peer->peerversion;
3803 ctx->minpoll = curr_peer->minpoll;
3804 ctx->maxpoll = curr_peer->maxpoll;
3805 ctx->flags = peerflag_bits(curr_peer);
3806 ctx->ttl = curr_peer->ttl;
3807 ctx->keyid = curr_peer->peerkey;
3808 ctx->group = curr_peer->group;
3811 hints.ai_family = ctx->family;
3812 hints.ai_socktype = SOCK_DGRAM;
3813 hints.ai_protocol = IPPROTO_UDP;
3815 getaddrinfo_sometime(curr_peer->addr->address,
3818 &peer_name_resolved, ctx);
3819 # else /* !WORKER follows */
3821 "hostname %s can not be used, please use IP address instead.",
3822 curr_peer->addr->address);
3830 * peer_name_resolved()
3832 * Callback invoked when config_peers()'s DNS lookup completes.
3841 const char * service,
3842 const struct addrinfo * hints,
3843 const struct addrinfo * res
3846 sockaddr_u peeraddr;
3847 peer_resolved_ctx * ctx;
3849 const char * fam_spec;
3856 DPRINTF(1, ("peer_name_resolved(%s) rescode %d\n", name, rescode));
3859 #ifndef IGNORE_DNS_ERRORS
3862 "giving up resolving host %s: %s (%d)",
3863 name, gai_strerror(rescode), rescode);
3864 #else /* IGNORE_DNS_ERRORS follows */
3865 getaddrinfo_sometime(name, service, hints,
3867 &peer_name_resolved, context);
3872 /* Loop to configure a single association */
3873 for (; res != NULL; res = res->ai_next) {
3874 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
3875 if (is_sane_resolved_address(&peeraddr,
3877 NLOG(NLOG_SYSINFO) {
3879 fam_spec = (AF_INET6 == af)
3884 msyslog(LOG_INFO, "DNS %s %s-> %s",
3914 peer_node *curr_peer;
3916 if (ptree->peers != NULL) {
3918 UNLINK_FIFO(curr_peer, *ptree->peers, link);
3919 if (NULL == curr_peer)
3921 destroy_address_node(curr_peer->addr);
3922 destroy_attr_val_fifo(curr_peer->peerflags);
3926 ptree->peers = NULL;
3929 #endif /* FREE_CFG_T */
3938 sockaddr_u peeraddr;
3939 struct addrinfo hints;
3940 unpeer_node * curr_unpeer;
3945 curr_unpeer = HEAD_PFIFO(ptree->unpeers);
3946 for (; curr_unpeer != NULL; curr_unpeer = curr_unpeer->link) {
3948 * Either AssocID will be zero, and we unpeer by name/
3949 * address addr, or it is nonzero and addr NULL.
3951 if (curr_unpeer->assocID) {
3952 p = findpeerbyassoc(curr_unpeer->assocID);
3954 msyslog(LOG_NOTICE, "unpeered %s",
3956 peer_clear(p, "GONE");
3964 AF(&peeraddr) = curr_unpeer->addr->type;
3965 name = curr_unpeer->addr->address;
3966 rc = getnetnum(name, &peeraddr, 0, t_UNK);
3967 /* Do we have a numeric address? */
3969 DPRINTF(1, ("unpeer: searching for %s\n",
3971 p = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);
3973 msyslog(LOG_NOTICE, "unpeered %s",
3975 peer_clear(p, "GONE");
3982 * It's not a numeric IP address, it's a hostname.
3983 * Check for associations with a matching hostname.
3985 for (p = peer_list; p != NULL; p = p->p_link)
3986 if (p->hostname != NULL)
3987 if (!strcasecmp(p->hostname, name))
3990 msyslog(LOG_NOTICE, "unpeered %s", name);
3991 peer_clear(p, "GONE");
3994 /* Resolve the hostname to address(es). */
3997 hints.ai_family = curr_unpeer->addr->type;
3998 hints.ai_socktype = SOCK_DGRAM;
3999 hints.ai_protocol = IPPROTO_UDP;
4000 getaddrinfo_sometime(name, "ntp", &hints,
4002 &unpeer_name_resolved, NULL);
4003 # else /* !WORKER follows */
4005 "hostname %s can not be used, please use IP address instead.",
4014 * unpeer_name_resolved()
4016 * Callback invoked when config_unpeers()'s DNS lookup completes.
4020 unpeer_name_resolved(
4025 const char * service,
4026 const struct addrinfo * hints,
4027 const struct addrinfo * res
4030 sockaddr_u peeraddr;
4033 const char * fam_spec;
4037 DPRINTF(1, ("unpeer_name_resolved(%s) rescode %d\n", name, rescode));
4040 msyslog(LOG_ERR, "giving up resolving unpeer %s: %s (%d)",
4041 name, gai_strerror(rescode), rescode);
4045 * Loop through the addresses found
4047 for (; res != NULL; res = res->ai_next) {
4048 INSIST(res->ai_addrlen <= sizeof(peeraddr));
4049 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
4050 DPRINTF(1, ("unpeer: searching for peer %s\n",
4052 peer = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);
4055 fam_spec = (AF_INET6 == af)
4060 msyslog(LOG_NOTICE, "unpeered %s %s-> %s", name,
4061 fam_spec, stoa(&peeraddr));
4062 peer_clear(peer, "GONE");
4072 free_config_unpeers(
4076 unpeer_node *curr_unpeer;
4078 if (ptree->unpeers != NULL) {
4080 UNLINK_FIFO(curr_unpeer, *ptree->unpeers, link);
4081 if (NULL == curr_unpeer)
4083 destroy_address_node(curr_unpeer->addr);
4086 free(ptree->unpeers);
4089 #endif /* FREE_CFG_T */
4094 config_reset_counters(
4098 int_node *counter_set;
4100 for (counter_set = HEAD_PFIFO(ptree->reset_counters);
4101 counter_set != NULL;
4102 counter_set = counter_set->link) {
4103 switch (counter_set->i) {
4105 DPRINTF(1, ("config_reset_counters %s (%d) invalid\n",
4106 keyword(counter_set->i), counter_set->i));
4144 free_config_reset_counters(
4148 FREE_INT_FIFO(ptree->reset_counters);
4150 #endif /* FREE_CFG_T */
4160 server_info *serv_info;
4161 attr_val *init_stmt;
4164 /* Check if a simulate block was found in the configuration code.
4165 * If not, return an error and exit
4167 sim_n = HEAD_PFIFO(ptree->sim_details);
4168 if (NULL == sim_n) {
4169 fprintf(stderr, "ERROR!! I couldn't find a \"simulate\" block for configuring the simulator.\n");
4170 fprintf(stderr, "\tCheck your configuration file.\n");
4174 /* Process the initialization statements
4175 * -------------------------------------
4177 init_stmt = HEAD_PFIFO(sim_n->init_opts);
4178 for (; init_stmt != NULL; init_stmt = init_stmt->link) {
4179 switch(init_stmt->attr) {
4182 simulation.beep_delay = init_stmt->value.d;
4185 case T_Sim_Duration:
4186 simulation.end_time = init_stmt->value.d;
4191 "Unknown simulator init token %d\n",
4197 /* Process the server list
4198 * -----------------------
4200 simulation.num_of_servers = 0;
4201 serv_info = HEAD_PFIFO(sim_n->servers);
4202 for (; serv_info != NULL; serv_info = serv_info->link)
4203 simulation.num_of_servers++;
4204 simulation.servers = emalloc(simulation.num_of_servers *
4205 sizeof(simulation.servers[0]));
4208 serv_info = HEAD_PFIFO(sim_n->servers);
4209 for (; serv_info != NULL; serv_info = serv_info->link) {
4210 if (NULL == serv_info) {
4211 fprintf(stderr, "Simulator server list is corrupt\n");
4214 simulation.servers[i] = *serv_info;
4215 simulation.servers[i].link = NULL;
4220 printf("Creating server associations\n");
4221 create_server_associations();
4222 fprintf(stderr,"\tServer associations successfully created!!\n");
4233 server_info *serv_n;
4234 script_info *script_n;
4236 if (NULL == ptree->sim_details)
4238 sim_n = HEAD_PFIFO(ptree->sim_details);
4239 free(ptree->sim_details);
4240 ptree->sim_details = NULL;
4244 FREE_ATTR_VAL_FIFO(sim_n->init_opts);
4246 UNLINK_FIFO(serv_n, *sim_n->servers, link);
4249 free(serv_n->curr_script);
4250 if (serv_n->script != NULL) {
4252 UNLINK_FIFO(script_n, *serv_n->script,
4254 if (script_n == NULL)
4258 free(serv_n->script);
4264 #endif /* FREE_CFG_T */
4268 /* Define two different config functions. One for the daemon and the other for
4269 * the simulator. The simulator ignores a lot of the standard ntpd configuration
4278 config_nic_rules(ptree);
4280 config_monitor(ptree);
4283 config_access(ptree);
4284 config_tinker(ptree);
4285 config_rlimit(ptree);
4286 config_system_opts(ptree);
4287 config_logconfig(ptree);
4288 config_phone(ptree);
4289 config_setvar(ptree);
4293 config_other_modes(ptree);
4294 config_peers(ptree);
4295 config_unpeers(ptree);
4296 config_fudge(ptree);
4297 config_reset_counters(ptree);
4299 #ifdef TEST_BLOCKING_WORKER
4301 struct addrinfo hints;
4304 hints.ai_socktype = SOCK_STREAM;
4305 hints.ai_protocol = IPPROTO_TCP;
4306 getaddrinfo_sometime("www.cnn.com", "ntp", &hints,
4308 gai_test_callback, (void *)1);
4309 hints.ai_family = AF_INET6;
4310 getaddrinfo_sometime("ipv6.google.com", "ntp", &hints,
4312 gai_test_callback, (void *)0x600);
4325 printf("Configuring Simulator...\n");
4326 printf("Some ntpd-specific commands in the configuration file will be ignored.\n");
4329 config_monitor(ptree);
4330 config_tinker(ptree);
4332 config_rlimit(ptree); /* not needed for the simulator */
4333 config_system_opts(ptree);
4334 config_logconfig(ptree);
4342 * config_remotely() - implements ntpd side of ntpq :config
4346 sockaddr_u * remote_addr
4349 struct FILE_INFO remote_cuckoo;
4352 snprintf(origin, sizeof(origin), "remote config from %s",
4354 ZERO(remote_cuckoo);
4355 remote_cuckoo.fname = origin;
4356 remote_cuckoo.line_no = 1;
4357 remote_cuckoo.col_no = 1;
4358 input_from_file = 0;
4360 init_syntax_tree(&cfgt);
4361 yyparse(&remote_cuckoo);
4362 cfgt.source.attr = CONF_SOURCE_NTPQ;
4363 cfgt.timestamp = time(NULL);
4364 cfgt.source.value.s = estrdup(stoa(remote_addr));
4366 DPRINTF(1, ("Finished Parsing!!\n"));
4368 save_and_apply_config_tree();
4370 input_from_file = 1;
4375 * getconfig() - process startup configuration file e.g /etc/ntp.conf
4386 atexit(free_all_config_trees);
4389 config_file = CONFIG_FILE;
4392 if (!ExpandEnvironmentStringsA(temp, config_file_storage,
4393 sizeof(config_file_storage))) {
4394 msyslog(LOG_ERR, "ExpandEnvironmentStrings CONFIG_FILE failed: %m");
4397 config_file = config_file_storage;
4399 temp = ALT_CONFIG_FILE;
4400 if (!ExpandEnvironmentStringsA(temp, alt_config_file_storage,
4401 sizeof(alt_config_file_storage))) {
4402 msyslog(LOG_ERR, "ExpandEnvironmentStrings ALT_CONFIG_FILE failed: %m");
4405 alt_config_file = alt_config_file_storage;
4406 #endif /* SYS_WINNT */
4409 * install a non default variable with this daemon version
4411 snprintf(line, sizeof(line), "daemon_version=\"%s\"", Version);
4412 set_sys_var(line, strlen(line) + 1, RO);
4415 * Set up for the first time step to install a variable showing
4416 * which syscall is being used to step.
4418 set_tod_using = &ntpd_set_tod_using;
4420 getCmdOpts(argc, argv);
4421 init_syntax_tree(&cfgt);
4422 curr_include_level = 0;
4424 (fp[curr_include_level] = F_OPEN(FindConfig(config_file), "r")) == NULL
4426 /* If there is no config_file, try NetInfo. */
4427 && check_netinfo && !(config_netinfo = get_netinfo_config())
4428 #endif /* HAVE_NETINFO */
4430 msyslog(LOG_INFO, "getconfig: Couldn't open <%s>", FindConfig(config_file));
4436 /* Under WinNT try alternate_config_file name, first NTP.CONF, then NTP.INI */
4438 if ((fp[curr_include_level] = F_OPEN(FindConfig(alt_config_file), "r")) == NULL) {
4441 * Broadcast clients can sometimes run without
4442 * a configuration file.
4444 msyslog(LOG_INFO, "getconfig: Couldn't open <%s>", FindConfig(alt_config_file));
4449 cfgt.source.value.s = estrdup(alt_config_file);
4450 #endif /* SYS_WINNT */
4452 cfgt.source.value.s = estrdup(config_file);
4455 /*** BULK OF THE PARSER ***/
4457 yydebug = !!(debug >= 5);
4459 yyparse(fp[curr_include_level]);
4461 DPRINTF(1, ("Finished Parsing!!\n"));
4463 cfgt.source.attr = CONF_SOURCE_FILE;
4464 cfgt.timestamp = time(NULL);
4466 save_and_apply_config_tree();
4468 while (curr_include_level != -1)
4469 FCLOSE(fp[curr_include_level--]);
4473 free_netinfo_config(config_netinfo);
4474 #endif /* HAVE_NETINFO */
4479 save_and_apply_config_tree(void)
4483 config_tree *punlinked;
4487 * Keep all the configuration trees applied since startup in
4488 * a list that can be used to dump the configuration back to
4491 ptree = emalloc(sizeof(*ptree));
4492 memcpy(ptree, &cfgt, sizeof(*ptree));
4495 LINK_TAIL_SLIST(cfg_tree_history, ptree, link, config_tree);
4498 if (HAVE_OPT( SAVECONFIGQUIT )) {
4503 dumpfile = fopen(OPT_ARG( SAVECONFIGQUIT ), "w");
4504 if (NULL == dumpfile) {
4507 "can not create save file %s, error %d %m\n",
4508 OPT_ARG(SAVECONFIGQUIT), err);
4512 dumpfailed = dump_all_config_trees(dumpfile, 0);
4515 "--saveconfigquit %s error %d\n",
4516 OPT_ARG( SAVECONFIGQUIT ),
4520 "configuration saved to %s\n",
4521 OPT_ARG( SAVECONFIGQUIT ));
4525 #endif /* SAVECONFIG */
4527 /* The actual configuration done depends on whether we are configuring the
4528 * simulator or the daemon. Perform a check and call the appropriate
4529 * function as needed.
4535 config_ntpdsim(ptree);
4539 * With configure --disable-saveconfig, there's no use keeping
4540 * the config tree around after application, so free it.
4543 UNLINK_SLIST(punlinked, cfg_tree_history, ptree, link,
4545 INSIST(punlinked == ptree);
4546 free_config_tree(ptree);
4558 snprintf(line, sizeof(line), "settimeofday=\"%s\"", which);
4559 set_sys_var(line, strlen(line) + 1, RO);
4573 snprintf(buf, LIB_BUFLENGTH, "%g", d);
4575 /* use lowercase 'e', strip any leading zeroes in exponent */
4576 pch_e = strchr(buf, 'e');
4577 if (NULL == pch_e) {
4578 pch_e = strchr(buf, 'E');
4587 while ('0' == *pch_nz)
4589 if (pch_nz == pch_e)
4591 strlcpy(pch_e, pch_nz, LIB_BUFLENGTH - (pch_e - buf));
4597 /* FUNCTIONS COPIED FROM THE OLDER ntp_config.c
4598 * --------------------------------------------
4603 * get_pfxmatch - find value for prefixmatch
4604 * and update char * accordingly
4612 while (m->name != NULL) {
4613 if (strncmp(*pstr, m->name, strlen(m->name)) == 0) {
4614 *pstr += strlen(m->name);
4624 * get_match - find logmask value
4632 while (m->name != NULL) {
4633 if (strcmp(str, m->name) == 0)
4642 * get_logmask - build bitmask for ntp_syslogmask
4653 mask = get_match(str, logcfg_noclass_items);
4658 offset = get_pfxmatch(&t, logcfg_class);
4659 mask = get_match(t, logcfg_class_items);
4662 return mask << offset;
4664 msyslog(LOG_ERR, "logconfig: '%s' not recognized - ignored",
4674 * get_netinfo_config - find the nearest NetInfo domain with an ntp
4675 * configuration and initialize the configuration state.
4677 static struct netinfo_config_state *
4678 get_netinfo_config(void)
4683 struct netinfo_config_state *config;
4685 if (ni_open(NULL, ".", &domain) != NI_OK) return NULL;
4687 while ((status = ni_pathsearch(domain, &config_dir, NETINFO_CONFIG_DIR)) == NI_NODIR) {
4689 if (ni_open(domain, "..", &next_domain) != NI_OK) {
4690 ni_free(next_domain);
4694 domain = next_domain;
4696 if (status != NI_OK) {
4701 config = emalloc(sizeof(*config));
4702 config->domain = domain;
4703 config->config_dir = config_dir;
4704 config->prop_index = 0;
4705 config->val_index = 0;
4706 config->val_list = NULL;
4713 * free_netinfo_config - release NetInfo configuration state
4716 free_netinfo_config(
4717 struct netinfo_config_state *config
4720 ni_free(config->domain);
4726 * gettokens_netinfo - return tokens from NetInfo
4730 struct netinfo_config_state *config,
4735 int prop_index = config->prop_index;
4736 int val_index = config->val_index;
4737 char **val_list = config->val_list;
4740 * Iterate through each keyword and look for a property that matches it.
4744 for (; prop_index < COUNTOF(keywords); prop_index++)
4746 ni_namelist namelist;
4747 struct keyword current_prop = keywords[prop_index];
4751 * For each value associated in the property, we're going to return
4752 * a separate line. We squirrel away the values in the config state
4753 * so the next time through, we don't need to do this lookup.
4756 if (NI_OK == ni_lookupprop(config->domain,
4757 &config->config_dir, current_prop.text,
4760 /* Found the property, but it has no values */
4761 if (namelist.ni_namelist_len == 0) continue;
4764 emalloc(sizeof(char*) *
4765 (namelist.ni_namelist_len + 1));
4766 val_list = config->val_list;
4769 index < namelist.ni_namelist_len;
4773 value = namelist.ni_namelist_val[index];
4774 val_list[index] = estrdup(value);
4776 val_list[index] = NULL;
4780 ni_namelist_free(&namelist);
4782 config->prop_index = prop_index;
4785 /* No list; we're done here. */
4787 return CONFIG_UNKNOWN;
4790 * We have a list of values for the current property.
4791 * Iterate through them and return each in order.
4793 if (val_list[val_index]) {
4796 char *tokens = val_list[val_index];
4798 msyslog(LOG_INFO, "%s %s", keywords[prop_index].text, val_list[val_index]);
4800 (const char*)tokenlist[0] = keywords[prop_index].text;
4801 for (ntok = 1; ntok < MAXTOKENS; ntok++) {
4802 tokenlist[ntok] = tokens;
4803 while (!ISEOL(*tokens) && (!ISSPACE(*tokens) || quoted))
4804 quoted ^= (*tokens++ == '"');
4806 if (ISEOL(*tokens)) {
4809 } else { /* must be space */
4811 while (ISSPACE(*tokens))
4818 if (ntok == MAXTOKENS) {
4819 /* HMS: chomp it to lose the EOL? */
4821 "gettokens_netinfo: too many tokens. Ignoring: %s",
4824 *ntokens = ntok + 1;
4827 config->val_index++; /* HMS: Should this be in the 'else'? */
4829 return keywords[prop_index].keytype;
4832 /* We're done with the current property. */
4833 prop_index = ++config->prop_index;
4835 /* Free val_list and reset counters. */
4836 for (val_index = 0; val_list[val_index]; val_index++)
4837 free(val_list[val_index]);
4839 val_list = config->val_list = NULL;
4840 val_index = config->val_index = 0;
4844 #endif /* HAVE_NETINFO */
4848 * getnetnum - return a net number (this is crude, but careful)
4850 * returns 1 for success, and mysteriously, 0 for most failures, and
4851 * -1 if the address found is IPv6 and we believe IPv6 isn't working.
4859 enum gnn_type a_type /* ignored */
4862 NTP_REQUIRE(AF_UNSPEC == AF(addr) ||
4863 AF_INET == AF(addr) ||
4864 AF_INET6 == AF(addr));
4866 if (!is_ip_address(num, AF(addr), addr))
4869 if (IS_IPV6(addr) && !ipv6_works)
4872 # ifdef ISC_PLATFORM_HAVESALEN
4873 addr->sa.sa_len = SIZEOF_SOCKADDR(AF(addr));
4875 SET_PORT(addr, NTP_PORT);
4877 DPRINTF(2, ("getnetnum given %s, got %s\n", num, stoa(addr)));
4883 #if defined(HAVE_SETRLIMIT)
4895 # ifdef RLIMIT_MEMLOCK
4896 case RLIMIT_MEMLOCK:
4898 * The default RLIMIT_MEMLOCK is very low on Linux systems.
4899 * Unless we increase this limit malloc calls are likely to
4900 * fail if we drop root privilege. To be useful the value
4901 * has to be larger than the largest ntpd resident set size.
4903 DPRINTF(2, ("ntp_rlimit: MEMLOCK: %d %s\n",
4904 (int)(rl_value / rl_scale), rl_sstr));
4905 rl.rlim_cur = rl.rlim_max = rl_value;
4906 if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1)
4907 msyslog(LOG_ERR, "Cannot set RLIMIT_MEMLOCK: %m");
4909 # endif /* RLIMIT_MEMLOCK */
4911 # ifdef RLIMIT_NOFILE
4914 * For large systems the default file descriptor limit may
4917 DPRINTF(2, ("ntp_rlimit: NOFILE: %d %s\n",
4918 (int)(rl_value / rl_scale), rl_sstr));
4919 rl.rlim_cur = rl.rlim_max = rl_value;
4920 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
4921 msyslog(LOG_ERR, "Cannot set RLIMIT_NOFILE: %m");
4923 # endif /* RLIMIT_NOFILE */
4925 # ifdef RLIMIT_STACK
4928 * Provide a way to set the stack limit to something
4929 * smaller, so that we don't lock a lot of unused
4932 DPRINTF(2, ("ntp_rlimit: STACK: %d %s pages\n",
4933 (int)(rl_value / rl_scale), rl_sstr));
4934 if (-1 == getrlimit(RLIMIT_STACK, &rl)) {
4935 msyslog(LOG_ERR, "getrlimit() failed: %m");
4937 if (rl_value > rl.rlim_max) {
4938 msyslog(LOG_WARNING,
4939 "ntp_rlimit: using maximum allowed stack limit %lu instead of %lu.",
4940 (u_long)rl.rlim_max,
4942 rl_value = rl.rlim_max;
4944 if (-1 == setrlimit(RLIMIT_STACK, &rl)) {
4946 "ntp_rlimit: Cannot adjust stack limit: %m");
4950 # endif /* RLIMIT_STACK */
4953 INSIST(!"Unexpected setrlimit() case!");
4957 #endif /* HAVE_SETRLIMIT */