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 extern int yyparse(void);
59 #if defined(HAVE_SYS_MMAN_H)
60 # include <sys/mman.h>
63 /* list of servers from command line for config_peers() */
64 int cmdline_server_count;
65 char ** cmdline_servers;
67 /* Current state of memory locking:
69 * 0: memory locking disabled
70 * 1: Memory locking enabled
75 * "logconfig" building blocks
78 const char * const name;
82 static struct masks logcfg_class[] = {
83 { "clock", NLOG_OCLOCK },
84 { "peer", NLOG_OPEER },
85 { "sync", NLOG_OSYNC },
90 /* logcfg_noclass_items[] masks are complete and must not be shifted */
91 static struct masks logcfg_noclass_items[] = {
92 { "allall", NLOG_SYSMASK | NLOG_PEERMASK | NLOG_CLOCKMASK | NLOG_SYNCMASK },
93 { "allinfo", NLOG_SYSINFO | NLOG_PEERINFO | NLOG_CLOCKINFO | NLOG_SYNCINFO },
94 { "allevents", NLOG_SYSEVENT | NLOG_PEEREVENT | NLOG_CLOCKEVENT | NLOG_SYNCEVENT },
95 { "allstatus", NLOG_SYSSTATUS | NLOG_PEERSTATUS | NLOG_CLOCKSTATUS | NLOG_SYNCSTATUS },
96 { "allstatistics", NLOG_SYSSTATIST | NLOG_PEERSTATIST | NLOG_CLOCKSTATIST | NLOG_SYNCSTATIST },
97 /* the remainder are misspellings of clockall, peerall, sysall, and syncall. */
98 { "allclock", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OCLOCK },
99 { "allpeer", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OPEER },
100 { "allsys", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OSYS },
101 { "allsync", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OSYNC },
105 /* logcfg_class_items[] masks are shiftable by NLOG_O* counts */
106 static struct masks logcfg_class_items[] = {
107 { "all", NLOG_INFO | NLOG_EVENT | NLOG_STATUS | NLOG_STATIST },
108 { "info", NLOG_INFO },
109 { "events", NLOG_EVENT },
110 { "status", NLOG_STATUS },
111 { "statistics", NLOG_STATIST },
115 typedef struct peer_resolved_ctx_tag {
117 int host_mode; /* T_* token identifier */
120 u_char hmode; /* MODE_* */
129 #define MAXPHONE 10 /* maximum number of phone strings */
130 #define MAXPPS 20 /* maximum length of PPS device string */
133 * Miscellaneous macros
135 #define ISEOL(c) ((c) == '#' || (c) == '\n' || (c) == '\0')
136 #define ISSPACE(c) ((c) == ' ' || (c) == '\t')
138 #define _UC(str) ((char *)(intptr_t)(str))
141 * Definitions of things either imported from or exported to outside
143 extern int yydebug; /* ntp_parser.c (.y) */
144 config_tree cfgt; /* Parser output stored here */
145 struct config_tree_tag *cfg_tree_history; /* History of configs */
146 char *sys_phone[MAXPHONE] = {NULL}; /* ACTS phone numbers */
147 char default_keysdir[] = NTP_KEYSDIR;
148 char *keysdir = default_keysdir; /* crypto keys directory */
149 char * saveconfigdir;
150 #if defined(HAVE_SCHED_SETSCHEDULER)
151 int config_priority_override = 0;
155 const char *config_file;
156 static char default_ntp_signd_socket[] =
157 #ifdef NTP_SIGND_PATH
162 char *ntp_signd_socket = default_ntp_signd_socket;
164 struct netinfo_config_state *config_netinfo = NULL;
165 int check_netinfo = 1;
166 #endif /* HAVE_NETINFO */
168 char *alt_config_file;
170 char config_file_storage[MAX_PATH];
171 char alt_config_file_storage[MAX_PATH];
172 #endif /* SYS_WINNT */
176 * NetInfo configuration state
178 struct netinfo_config_state {
179 void *domain; /* domain with config */
180 ni_id config_dir; /* ID config dir */
181 int prop_index; /* current property */
182 int val_index; /* current value */
183 char **val_list; /* value list */
187 struct REMOTE_CONFIG_INFO remote_config; /* Remote configuration buffer and
189 int old_config_style = 1; /* A boolean flag, which when set,
190 * indicates that the old configuration
191 * format with a newline at the end of
192 * every command is being used
194 int cryptosw; /* crypto command called */
196 extern char *stats_drift_file; /* name of the driftfile */
198 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
200 * backwards compatibility flags
202 bc_entry bc_list[] = {
203 { T_Bc_bugXXXX, 1 } /* default enabled */
207 * declare an int pointer for each flag for quick testing without
208 * walking bc_list. If the pointer is consumed by libntp rather
209 * than ntpd, declare it in a libntp source file pointing to storage
210 * initialized with the appropriate value for other libntp clients, and
211 * redirect it to point into bc_list during ntpd startup.
213 int *p_bcXXXX_enabled = &bc_list[0].enabled;
216 /* FUNCTION PROTOTYPES */
218 static void init_syntax_tree(config_tree *);
219 static void apply_enable_disable(attr_val_fifo *q, int enable);
222 static void free_auth_node(config_tree *);
223 static void free_all_config_trees(void);
225 static void free_config_access(config_tree *);
226 static void free_config_auth(config_tree *);
227 static void free_config_fudge(config_tree *);
228 static void free_config_logconfig(config_tree *);
229 static void free_config_monitor(config_tree *);
230 static void free_config_nic_rules(config_tree *);
231 static void free_config_other_modes(config_tree *);
232 static void free_config_peers(config_tree *);
233 static void free_config_phone(config_tree *);
234 static void free_config_reset_counters(config_tree *);
235 static void free_config_rlimit(config_tree *);
236 static void free_config_setvar(config_tree *);
237 static void free_config_system_opts(config_tree *);
238 static void free_config_tinker(config_tree *);
239 static void free_config_tos(config_tree *);
240 static void free_config_trap(config_tree *);
241 static void free_config_ttl(config_tree *);
242 static void free_config_unpeers(config_tree *);
243 static void free_config_vars(config_tree *);
246 static void free_config_sim(config_tree *);
248 static void destroy_address_fifo(address_fifo *);
249 #define FREE_ADDRESS_FIFO(pf) \
251 destroy_address_fifo(pf); \
254 void free_all_config_trees(void); /* atexit() */
255 static void free_config_tree(config_tree *ptree);
256 #endif /* FREE_CFG_T */
258 static void destroy_restrict_node(restrict_node *my_node);
259 static int is_sane_resolved_address(sockaddr_u *peeraddr, int hmode);
260 static void save_and_apply_config_tree(int/*BOOL*/ from_file);
261 static void destroy_int_fifo(int_fifo *);
262 #define FREE_INT_FIFO(pf) \
264 destroy_int_fifo(pf); \
267 static void destroy_string_fifo(string_fifo *);
268 #define FREE_STRING_FIFO(pf) \
270 destroy_string_fifo(pf); \
273 static void destroy_attr_val_fifo(attr_val_fifo *);
274 #define FREE_ATTR_VAL_FIFO(pf) \
276 destroy_attr_val_fifo(pf); \
279 static void destroy_filegen_fifo(filegen_fifo *);
280 #define FREE_FILEGEN_FIFO(pf) \
282 destroy_filegen_fifo(pf); \
285 static void destroy_restrict_fifo(restrict_fifo *);
286 #define FREE_RESTRICT_FIFO(pf) \
288 destroy_restrict_fifo(pf); \
291 static void destroy_setvar_fifo(setvar_fifo *);
292 #define FREE_SETVAR_FIFO(pf) \
294 destroy_setvar_fifo(pf); \
297 static void destroy_addr_opts_fifo(addr_opts_fifo *);
298 #define FREE_ADDR_OPTS_FIFO(pf) \
300 destroy_addr_opts_fifo(pf); \
304 static void config_logconfig(config_tree *);
305 static void config_monitor(config_tree *);
306 static void config_rlimit(config_tree *);
307 static void config_system_opts(config_tree *);
308 static void config_tinker(config_tree *);
309 static void config_tos(config_tree *);
310 static void config_vars(config_tree *);
313 static sockaddr_u *get_next_address(address_node *addr);
314 static void config_sim(config_tree *);
315 static void config_ntpdsim(config_tree *);
316 #else /* !SIM follows */
317 static void config_ntpd(config_tree *, int/*BOOL*/ input_from_file);
318 static void config_other_modes(config_tree *);
319 static void config_auth(config_tree *);
320 static void config_access(config_tree *);
321 static void config_mdnstries(config_tree *);
322 static void config_phone(config_tree *);
323 static void config_setvar(config_tree *);
324 static void config_ttl(config_tree *);
325 static void config_trap(config_tree *);
326 static void config_fudge(config_tree *);
327 static void config_peers(config_tree *);
328 static void config_unpeers(config_tree *);
329 static void config_nic_rules(config_tree *, int/*BOOL*/ input_from_file);
330 static void config_reset_counters(config_tree *);
331 static u_char get_correct_host_mode(int token);
332 static int peerflag_bits(peer_node *);
336 static void peer_name_resolved(int, int, void *, const char *, const char *,
337 const struct addrinfo *,
338 const struct addrinfo *);
339 static void unpeer_name_resolved(int, int, void *, const char *, const char *,
340 const struct addrinfo *,
341 const struct addrinfo *);
342 static void trap_name_resolved(int, int, void *, const char *, const char *,
343 const struct addrinfo *,
344 const struct addrinfo *);
349 t_REF, /* Refclock */
350 t_MSK /* Network Mask */
353 static void ntpd_set_tod_using(const char *);
354 static char * normal_dtoa(double);
355 static u_int32 get_pfxmatch(const char **, struct masks *);
356 static u_int32 get_match(const char *, struct masks *);
357 static u_int32 get_logmask(const char *);
359 static int getnetnum(const char *num, sockaddr_u *addr, int complain,
360 enum gnn_type a_type);
365 /* FUNCTIONS FOR INITIALIZATION
366 * ----------------------------
375 if (ptree->auth.keys) {
376 free(ptree->auth.keys);
377 ptree->auth.keys = NULL;
380 if (ptree->auth.keysdir) {
381 free(ptree->auth.keysdir);
382 ptree->auth.keysdir = NULL;
385 if (ptree->auth.ntp_signd_socket) {
386 free(ptree->auth.ntp_signd_socket);
387 ptree->auth.ntp_signd_socket = NULL;
399 ptree->mdnstries = 5;
405 free_all_config_trees(void)
410 ptree = cfg_tree_history;
412 while (ptree != NULL) {
414 free_config_tree(ptree);
425 #if defined(_MSC_VER) && defined (_DEBUG)
429 if (ptree->source.value.s != NULL)
430 free(ptree->source.value.s);
432 free_config_other_modes(ptree);
433 free_config_auth(ptree);
434 free_config_tos(ptree);
435 free_config_monitor(ptree);
436 free_config_access(ptree);
437 free_config_tinker(ptree);
438 free_config_rlimit(ptree);
439 free_config_system_opts(ptree);
440 free_config_logconfig(ptree);
441 free_config_phone(ptree);
442 free_config_setvar(ptree);
443 free_config_ttl(ptree);
444 free_config_trap(ptree);
445 free_config_fudge(ptree);
446 free_config_vars(ptree);
447 free_config_peers(ptree);
448 free_config_unpeers(ptree);
449 free_config_nic_rules(ptree);
450 free_config_reset_counters(ptree);
452 free_config_sim(ptree);
454 free_auth_node(ptree);
458 #if defined(_MSC_VER) && defined (_DEBUG)
462 #endif /* FREE_CFG_T */
468 dump_all_config_trees(
473 config_tree * cfg_ptr;
477 for (cfg_ptr = cfg_tree_history;
479 cfg_ptr = cfg_ptr->link)
480 return_value |= dump_config_tree(cfg_ptr, df, comment);
486 /* The config dumper */
495 unpeer_node *unpeern;
498 address_node *peer_addr;
499 address_node *fudge_addr;
500 filegen_node *fgen_node;
501 restrict_node *rest_node;
502 addr_opts_node *addr_opts;
503 setvar_node *setv_node;
504 nic_rule_node *rule_node;
507 int_node *counter_set;
508 string_node *str_node;
510 const char *s = NULL;
516 DPRINTF(1, ("dump_config_tree(%p)\n", ptree));
519 if (!strftime(timestamp, sizeof(timestamp),
521 localtime(&ptree->timestamp)))
524 fprintf(df, "# %s %s %s\n",
526 (CONF_SOURCE_NTPQ == ptree->source.attr)
527 ? "ntpq remote config from"
528 : "startup configuration file",
529 ptree->source.value.s);
532 /* For options I didn't find documentation I'll just output its name and the cor. value */
533 atrv = HEAD_PFIFO(ptree->vars);
534 for ( ; atrv != NULL; atrv = atrv->link) {
535 switch (atrv->type) {
538 fprintf(df, "\n# dump error:\n"
539 "# unknown vars type %d (%s) for %s\n",
540 atrv->type, token_name(atrv->type),
541 token_name(atrv->attr));
545 fprintf(df, "%s %s\n", keyword(atrv->attr),
546 normal_dtoa(atrv->value.d));
550 fprintf(df, "%s %d\n", keyword(atrv->attr),
555 fprintf(df, "%s \"%s\"", keyword(atrv->attr),
557 if (T_Driftfile == atrv->attr &&
558 atrv->link != NULL &&
559 T_WanderThreshold == atrv->link->attr) {
562 normal_dtoa(atrv->value.d));
570 atrv = HEAD_PFIFO(ptree->logconfig);
572 fprintf(df, "logconfig");
573 for ( ; atrv != NULL; atrv = atrv->link)
574 fprintf(df, " %c%s", atrv->attr, atrv->value.s);
578 if (ptree->stats_dir)
579 fprintf(df, "statsdir \"%s\"\n", ptree->stats_dir);
581 i_n = HEAD_PFIFO(ptree->stats_list);
583 fprintf(df, "statistics");
584 for ( ; i_n != NULL; i_n = i_n->link)
585 fprintf(df, " %s", keyword(i_n->i));
589 fgen_node = HEAD_PFIFO(ptree->filegen_opts);
590 for ( ; fgen_node != NULL; fgen_node = fgen_node->link) {
591 atrv = HEAD_PFIFO(fgen_node->options);
593 fprintf(df, "filegen %s",
594 keyword(fgen_node->filegen_token));
595 for ( ; atrv != NULL; atrv = atrv->link) {
596 switch (atrv->attr) {
599 fprintf(df, "\n# dump error:\n"
600 "# unknown filegen option token %s\n"
602 token_name(atrv->attr),
603 keyword(fgen_node->filegen_token));
607 fprintf(df, " file %s",
612 fprintf(df, " type %s",
613 keyword(atrv->value.i));
618 keyword(atrv->value.i));
626 atrv = HEAD_PFIFO(ptree->auth.crypto_cmd_list);
628 fprintf(df, "crypto");
629 for ( ; atrv != NULL; atrv = atrv->link) {
630 fprintf(df, " %s %s", keyword(atrv->attr),
636 if (ptree->auth.revoke != 0)
637 fprintf(df, "revoke %d\n", ptree->auth.revoke);
639 if (ptree->auth.keysdir != NULL)
640 fprintf(df, "keysdir \"%s\"\n", ptree->auth.keysdir);
642 if (ptree->auth.keys != NULL)
643 fprintf(df, "keys \"%s\"\n", ptree->auth.keys);
645 atrv = HEAD_PFIFO(ptree->auth.trusted_key_list);
647 fprintf(df, "trustedkey");
648 for ( ; atrv != NULL; atrv = atrv->link) {
649 if (T_Integer == atrv->type)
650 fprintf(df, " %d", atrv->value.i);
651 else if (T_Intrange == atrv->type)
652 fprintf(df, " (%d ... %d)",
657 fprintf(df, "\n# dump error:\n"
658 "# unknown trustedkey attr type %d\n"
659 "trustedkey", atrv->type);
665 if (ptree->auth.control_key)
666 fprintf(df, "controlkey %d\n", ptree->auth.control_key);
668 if (ptree->auth.request_key)
669 fprintf(df, "requestkey %d\n", ptree->auth.request_key);
671 /* dump enable list, then disable list */
672 for (enable = 1; enable >= 0; enable--) {
674 ? HEAD_PFIFO(ptree->enable_opts)
675 : HEAD_PFIFO(ptree->disable_opts);
677 fprintf(df, "%s", (enable)
680 for ( ; atrv != NULL; atrv = atrv->link)
682 keyword(atrv->value.i));
687 atrv = HEAD_PFIFO(ptree->orphan_cmds);
690 for ( ; atrv != NULL; atrv = atrv->link) {
691 switch (atrv->type) {
694 fprintf(df, "\n# dump error:\n"
695 "# unknown tos attr type %d %s\n"
697 token_name(atrv->type));
701 fprintf(df, " %s %s",
703 normal_dtoa(atrv->value.d));
710 atrv = HEAD_PFIFO(ptree->rlimit);
712 fprintf(df, "rlimit");
713 for ( ; atrv != NULL; atrv = atrv->link) {
714 INSIST(T_Integer == atrv->type);
715 fprintf(df, " %s %d", keyword(atrv->attr),
721 atrv = HEAD_PFIFO(ptree->tinker);
723 fprintf(df, "tinker");
724 for ( ; atrv != NULL; atrv = atrv->link) {
725 INSIST(T_Double == atrv->type);
726 fprintf(df, " %s %s", keyword(atrv->attr),
727 normal_dtoa(atrv->value.d));
732 if (ptree->broadcastclient)
733 fprintf(df, "broadcastclient\n");
735 peern = HEAD_PFIFO(ptree->peers);
736 for ( ; peern != NULL; peern = peern->link) {
738 fprintf(df, "%s", keyword(peern->host_mode));
739 switch (addr->type) {
742 fprintf(df, "# dump error:\n"
743 "# unknown peer family %d for:\n"
745 keyword(peern->host_mode));
759 fprintf(df, " %s", addr->address);
761 if (peern->minpoll != 0)
762 fprintf(df, " minpoll %u", peern->minpoll);
764 if (peern->maxpoll != 0)
765 fprintf(df, " maxpoll %u", peern->maxpoll);
767 if (peern->ttl != 0) {
768 if (strlen(addr->address) > 8
769 && !memcmp(addr->address, "127.127.", 8))
770 fprintf(df, " mode %u", peern->ttl);
772 fprintf(df, " ttl %u", peern->ttl);
775 if (peern->peerversion != NTP_VERSION)
776 fprintf(df, " version %u", peern->peerversion);
778 if (peern->peerkey != 0)
779 fprintf(df, " key %u", peern->peerkey);
781 if (peern->group != NULL)
782 fprintf(df, " ident \"%s\"", peern->group);
784 atrv = HEAD_PFIFO(peern->peerflags);
785 for ( ; atrv != NULL; atrv = atrv->link) {
786 INSIST(T_Flag == atrv->attr);
787 INSIST(T_Integer == atrv->type);
788 fprintf(df, " %s", keyword(atrv->value.i));
793 addr_opts = HEAD_PFIFO(ptree->fudge);
794 for ( ; addr_opts != NULL; addr_opts = addr_opts->link) {
795 peer_addr = peern->addr;
796 fudge_addr = addr_opts->addr;
798 s1 = peer_addr->address;
799 s2 = fudge_addr->address;
804 fprintf(df, "fudge %s", s1);
806 for (atrv = HEAD_PFIFO(addr_opts->options);
810 switch (atrv->type) {
813 fprintf(df, "\n# dump error:\n"
814 "# unknown fudge atrv->type %d\n"
815 "fudge %s", atrv->type,
820 fprintf(df, " %s %s",
822 normal_dtoa(atrv->value.d));
826 fprintf(df, " %s %d",
832 fprintf(df, " %s %s",
842 addr = HEAD_PFIFO(ptree->manycastserver);
844 fprintf(df, "manycastserver");
845 for ( ; addr != NULL; addr = addr->link)
846 fprintf(df, " %s", addr->address);
850 addr = HEAD_PFIFO(ptree->multicastclient);
852 fprintf(df, "multicastclient");
853 for ( ; addr != NULL; addr = addr->link)
854 fprintf(df, " %s", addr->address);
859 for (unpeern = HEAD_PFIFO(ptree->unpeers);
861 unpeern = unpeern->link)
862 fprintf(df, "unpeer %s\n", unpeern->addr->address);
864 atrv = HEAD_PFIFO(ptree->mru_opts);
867 for ( ; atrv != NULL; atrv = atrv->link)
868 fprintf(df, " %s %d", keyword(atrv->attr),
873 atrv = HEAD_PFIFO(ptree->discard_opts);
875 fprintf(df, "discard");
876 for ( ; atrv != NULL; atrv = atrv->link)
877 fprintf(df, " %s %d", keyword(atrv->attr),
883 for (rest_node = HEAD_PFIFO(ptree->restrict_opts);
885 rest_node = rest_node->link) {
887 if (NULL == rest_node->addr) {
889 flags = HEAD_PFIFO(rest_node->flags);
890 for ( ; flags != NULL; flags = flags->link)
891 if (T_Source == flags->i) {
896 s = rest_node->addr->address;
898 fprintf(df, "restrict %s", s);
899 if (rest_node->mask != NULL)
900 fprintf(df, " mask %s",
901 rest_node->mask->address);
902 flags = HEAD_PFIFO(rest_node->flags);
903 for ( ; flags != NULL; flags = flags->link)
904 if (T_Source != flags->i)
905 fprintf(df, " %s", keyword(flags->i));
909 rule_node = HEAD_PFIFO(ptree->nic_rules);
910 for ( ; rule_node != NULL; rule_node = rule_node->link) {
911 fprintf(df, "interface %s %s\n",
912 keyword(rule_node->action),
913 (rule_node->match_class)
914 ? keyword(rule_node->match_class)
915 : rule_node->if_name);
918 str_node = HEAD_PFIFO(ptree->phone);
919 if (str_node != NULL) {
920 fprintf(df, "phone");
921 for ( ; str_node != NULL; str_node = str_node->link)
922 fprintf(df, " \"%s\"", str_node->s);
926 setv_node = HEAD_PFIFO(ptree->setvar);
927 for ( ; setv_node != NULL; setv_node = setv_node->link) {
928 s1 = quote_if_needed(setv_node->var);
929 s2 = quote_if_needed(setv_node->val);
930 fprintf(df, "setvar %s = %s", s1, s2);
933 if (setv_node->isdefault)
934 fprintf(df, " default");
938 i_n = HEAD_PFIFO(ptree->ttl);
941 for( ; i_n != NULL; i_n = i_n->link)
942 fprintf(df, " %d", i_n->i);
946 addr_opts = HEAD_PFIFO(ptree->trap);
947 for ( ; addr_opts != NULL; addr_opts = addr_opts->link) {
948 addr = addr_opts->addr;
949 fprintf(df, "trap %s", addr->address);
950 atrv = HEAD_PFIFO(addr_opts->options);
951 for ( ; atrv != NULL; atrv = atrv->link) {
952 switch (atrv->attr) {
955 fprintf(df, "\n# dump error:\n"
956 "# unknown trap token %d\n"
957 "trap %s", atrv->attr,
962 fprintf(df, " port %d", atrv->value.i);
966 fprintf(df, " interface %s",
974 counter_set = HEAD_PFIFO(ptree->reset_counters);
975 if (counter_set != NULL) {
976 fprintf(df, "reset");
977 for ( ; counter_set != NULL;
978 counter_set = counter_set->link)
979 fprintf(df, " %s", keyword(counter_set->i));
985 #endif /* SAVECONFIG */
989 /* generic fifo routines for structs linked by 1st member */
1002 pf = emalloc_zero(sizeof(*pf));
1004 CHECK_FIFO_CONSISTENCY(*pf);
1006 LINK_FIFO(*pf, pe, link);
1007 CHECK_FIFO_CONSISTENCY(*pf);
1029 CONCAT_FIFO(*pf1, *pf2, link);
1036 /* FUNCTIONS FOR CREATING NODES ON THE SYNTAX TREE
1037 * -----------------------------------------------
1048 my_val = emalloc_zero(sizeof(*my_val));
1049 my_val->attr = attr;
1050 my_val->value.d = value;
1051 my_val->type = T_Double;
1065 my_val = emalloc_zero(sizeof(*my_val));
1066 my_val->attr = attr;
1067 my_val->value.i = value;
1068 my_val->type = T_Integer;
1082 my_val = emalloc_zero(sizeof(*my_val));
1083 my_val->attr = attr;
1084 my_val->value.u = value;
1085 my_val->type = T_U_int;
1092 create_attr_rangeval(
1100 my_val = emalloc_zero(sizeof(*my_val));
1101 my_val->attr = attr;
1102 my_val->value.r.first = first;
1103 my_val->value.r.last = last;
1104 my_val->type = T_Intrange;
1118 my_val = emalloc_zero(sizeof(*my_val));
1119 my_val->attr = attr;
1120 if (NULL == s) /* free() hates NULL */
1122 my_val->value.s = _UC(s);
1123 my_val->type = T_String;
1136 i_n = emalloc_zero(sizeof(*i_n));
1150 sn = emalloc_zero(sizeof(*sn));
1158 create_address_node(
1163 address_node *my_node;
1165 REQUIRE(NULL != addr);
1166 REQUIRE(AF_INET == type || AF_INET6 == type || AF_UNSPEC == type);
1167 my_node = emalloc_zero(sizeof(*my_node));
1168 my_node->address = addr;
1169 my_node->type = (u_short)type;
1176 destroy_address_node(
1177 address_node *my_node
1180 if (NULL == my_node)
1182 REQUIRE(NULL != my_node->address);
1184 free(my_node->address);
1192 address_node * addr,
1193 attr_val_fifo * options
1201 my_node = emalloc_zero(sizeof(*my_node));
1203 /* Initialize node values to default */
1204 my_node->peerversion = NTP_VERSION;
1206 /* Now set the node to the read values */
1207 my_node->host_mode = hmode;
1208 my_node->addr = addr;
1211 * the options FIFO mixes items that will be saved in the
1212 * peer_node as explicit members, such as minpoll, and
1213 * those that are moved intact to the peer_node's peerflags
1214 * FIFO. The options FIFO is consumed and reclaimed here.
1217 if (options != NULL)
1218 CHECK_FIFO_CONSISTENCY(*options);
1219 while (options != NULL) {
1220 UNLINK_FIFO(option, *options, link);
1221 if (NULL == option) {
1227 /* Check the kind of option being set */
1228 switch (option->attr) {
1231 APPEND_G_FIFO(my_node->peerflags, option);
1236 if (option->value.i < NTP_MINPOLL ||
1237 option->value.i > UCHAR_MAX) {
1239 "minpoll: provided value (%d) is out of range [%d-%d])",
1240 option->value.i, NTP_MINPOLL,
1242 my_node->minpoll = NTP_MINPOLL;
1245 (u_char)option->value.u;
1250 if (option->value.i < 0 ||
1251 option->value.i > NTP_MAXPOLL) {
1253 "maxpoll: provided value (%d) is out of range [0-%d])",
1254 option->value.i, NTP_MAXPOLL);
1255 my_node->maxpoll = NTP_MAXPOLL;
1258 (u_char)option->value.u;
1263 if (option->value.u >= MAX_TTL) {
1264 msyslog(LOG_ERR, "ttl: invalid argument");
1267 my_node->ttl = (u_char)option->value.u;
1272 my_node->ttl = option->value.u;
1276 if (option->value.u >= KEYID_T_MAX) {
1277 msyslog(LOG_ERR, "key: invalid argument");
1281 (keyid_t)option->value.u;
1286 if (option->value.u >= UCHAR_MAX) {
1287 msyslog(LOG_ERR, "version: invalid argument");
1290 my_node->peerversion =
1291 (u_char)option->value.u;
1296 my_node->group = option->value.s;
1301 "Unknown peer/server option token %s",
1302 token_name(option->attr));
1309 /* Check if errors were reported. If yes, ignore the node */
1324 unpeer_node * my_node;
1328 my_node = emalloc_zero(sizeof(*my_node));
1331 * From the parser's perspective an association ID fits into
1332 * its generic T_String definition of a name/address "address".
1333 * We treat all valid 16-bit numbers as association IDs.
1335 pch = addr->address;
1336 while (*pch && isdigit((unsigned char)*pch))
1340 && 1 == sscanf(addr->address, "%u", &u)
1341 && u <= ASSOCID_MAX) {
1342 my_node->assocID = (associd_t)u;
1343 destroy_address_node(addr);
1344 my_node->addr = NULL;
1346 my_node->assocID = 0;
1347 my_node->addr = addr;
1354 create_filegen_node(
1356 attr_val_fifo * options
1359 filegen_node *my_node;
1361 my_node = emalloc_zero(sizeof(*my_node));
1362 my_node->filegen_token = filegen_token;
1363 my_node->options = options;
1370 create_restrict_node(
1371 address_node * addr,
1372 address_node * mask,
1377 restrict_node *my_node;
1379 my_node = emalloc_zero(sizeof(*my_node));
1380 my_node->addr = addr;
1381 my_node->mask = mask;
1382 my_node->flags = flags;
1383 my_node->line_no = line_no;
1390 destroy_restrict_node(
1391 restrict_node *my_node
1394 /* With great care, free all the memory occupied by
1397 destroy_address_node(my_node->addr);
1398 destroy_address_node(my_node->mask);
1399 destroy_int_fifo(my_node->flags);
1413 UNLINK_FIFO(i_n, *fifo, link);
1424 destroy_string_fifo(
1432 UNLINK_FIFO(sn, *fifo, link);
1444 destroy_attr_val_fifo(
1445 attr_val_fifo * av_fifo
1450 if (av_fifo != NULL) {
1452 UNLINK_FIFO(av, *av_fifo, link);
1455 if (T_String == av->type)
1465 destroy_filegen_fifo(
1473 UNLINK_FIFO(fg, *fifo, link);
1476 destroy_attr_val_fifo(fg->options);
1485 destroy_restrict_fifo(
1486 restrict_fifo * fifo
1493 UNLINK_FIFO(rn, *fifo, link);
1496 destroy_restrict_node(rn);
1504 destroy_setvar_fifo(
1512 UNLINK_FIFO(sv, *fifo, link);
1525 destroy_addr_opts_fifo(
1526 addr_opts_fifo * fifo
1529 addr_opts_node * aon;
1533 UNLINK_FIFO(aon, *fifo, link);
1536 destroy_address_node(aon->addr);
1537 destroy_attr_val_fifo(aon->options);
1552 setvar_node * my_node;
1555 /* do not allow = in the variable name */
1556 pch = strchr(var, '=');
1560 /* Now store the string into a setvar_node */
1561 my_node = emalloc_zero(sizeof(*my_node));
1564 my_node->isdefault = isdefault;
1571 create_nic_rule_node(
1573 char *if_name, /* interface name or numeric address */
1577 nic_rule_node *my_node;
1579 REQUIRE(match_class != 0 || if_name != NULL);
1581 my_node = emalloc_zero(sizeof(*my_node));
1582 my_node->match_class = match_class;
1583 my_node->if_name = if_name;
1584 my_node->action = action;
1591 create_addr_opts_node(
1592 address_node * addr,
1593 attr_val_fifo * options
1596 addr_opts_node *my_node;
1598 my_node = emalloc_zero(sizeof(*my_node));
1599 my_node->addr = addr;
1600 my_node->options = options;
1608 create_sim_script_info(
1610 attr_val_fifo * script_queue
1613 script_info *my_info;
1614 attr_val *my_attr_val;
1616 my_info = emalloc_zero(sizeof(*my_info));
1618 /* Initialize Script Info with default values*/
1619 my_info->duration = duration;
1620 my_info->prop_delay = NET_DLY;
1621 my_info->proc_delay = PROC_DLY;
1623 /* Traverse the script_queue and fill out non-default values */
1625 for (my_attr_val = HEAD_PFIFO(script_queue);
1626 my_attr_val != NULL;
1627 my_attr_val = my_attr_val->link) {
1629 /* Set the desired value */
1630 switch (my_attr_val->attr) {
1633 my_info->freq_offset = my_attr_val->value.d;
1637 my_info->wander = my_attr_val->value.d;
1641 my_info->jitter = my_attr_val->value.d;
1645 my_info->prop_delay = my_attr_val->value.d;
1649 my_info->proc_delay = my_attr_val->value.d;
1653 msyslog(LOG_ERR, "Unknown script token %d",
1669 const char addr_prefix[] = "192.168.0.";
1670 static int curr_addr_num = 1;
1671 #define ADDR_LENGTH 16 + 1 /* room for 192.168.1.255 */
1672 char addr_string[ADDR_LENGTH];
1673 sockaddr_u *final_addr;
1674 struct addrinfo *ptr;
1677 final_addr = emalloc(sizeof(*final_addr));
1679 if (addr->type == T_String) {
1680 snprintf(addr_string, sizeof(addr_string), "%s%d",
1681 addr_prefix, curr_addr_num++);
1682 printf("Selecting ip address %s for hostname %s\n",
1683 addr_string, addr->address);
1684 gai_err = getaddrinfo(addr_string, "ntp", NULL, &ptr);
1686 gai_err = getaddrinfo(addr->address, "ntp", NULL, &ptr);
1690 fprintf(stderr, "ERROR!! Could not get a new address\n");
1693 memcpy(final_addr, ptr->ai_addr, ptr->ai_addrlen);
1694 fprintf(stderr, "Successful in setting ip address of simulated server to: %s\n",
1706 address_node * addr,
1707 double server_offset,
1708 script_info_fifo * script
1711 server_info *my_info;
1713 my_info = emalloc_zero(sizeof(*my_info));
1714 my_info->server_time = server_offset;
1715 my_info->addr = get_next_address(addr);
1716 my_info->script = script;
1717 UNLINK_FIFO(my_info->curr_script, *my_info->script, link);
1725 attr_val_fifo * init_opts,
1726 server_info_fifo * servers
1731 my_node = emalloc(sizeof(*my_node));
1732 my_node->init_opts = init_opts;
1733 my_node->servers = servers;
1741 /* FUNCTIONS FOR PERFORMING THE CONFIGURATION
1742 * ------------------------------------------
1751 sockaddr_u addr_sock;
1752 address_node * addr_node;
1754 if (ptree->broadcastclient)
1755 proto_config(PROTO_BROADCLIENT, ptree->broadcastclient,
1758 addr_node = HEAD_PFIFO(ptree->manycastserver);
1759 while (addr_node != NULL) {
1760 ZERO_SOCK(&addr_sock);
1761 AF(&addr_sock) = addr_node->type;
1762 if (1 == getnetnum(addr_node->address, &addr_sock, 1,
1764 proto_config(PROTO_MULTICAST_ADD,
1766 sys_manycastserver = 1;
1768 addr_node = addr_node->link;
1771 /* Configure the multicast clients */
1772 addr_node = HEAD_PFIFO(ptree->multicastclient);
1773 if (addr_node != NULL) {
1775 ZERO_SOCK(&addr_sock);
1776 AF(&addr_sock) = addr_node->type;
1777 if (1 == getnetnum(addr_node->address,
1778 &addr_sock, 1, t_UNK)) {
1779 proto_config(PROTO_MULTICAST_ADD, 0, 0.,
1782 addr_node = addr_node->link;
1783 } while (addr_node != NULL);
1784 proto_config(PROTO_MULTICAST_ADD, 1, 0., NULL);
1792 destroy_address_fifo(
1793 address_fifo * pfifo
1796 address_node * addr_node;
1798 if (pfifo != NULL) {
1800 UNLINK_FIFO(addr_node, *pfifo, link);
1801 if (addr_node == NULL)
1803 destroy_address_node(addr_node);
1811 free_config_other_modes(
1815 FREE_ADDRESS_FIFO(ptree->manycastserver);
1816 FREE_ADDRESS_FIFO(ptree->multicastclient);
1818 #endif /* FREE_CFG_T */
1836 /* Crypto Command */
1839 item = -1; /* quiet warning */
1841 my_val = HEAD_PFIFO(ptree->auth.crypto_cmd_list);
1842 for (; my_val != NULL; my_val = my_val->link) {
1843 switch (my_val->attr) {
1850 item = CRYPTO_CONF_PRIV;
1854 item = CRYPTO_CONF_IDENT;
1858 item = CRYPTO_CONF_PW;
1862 item = CRYPTO_CONF_RAND;
1866 item = CRYPTO_CONF_NID;
1869 crypto_config(item, my_val->value.s);
1871 #endif /* AUTOKEY */
1873 /* Keysdir Command */
1874 if (ptree->auth.keysdir) {
1875 if (keysdir != default_keysdir)
1877 keysdir = estrdup(ptree->auth.keysdir);
1881 /* ntp_signd_socket Command */
1882 if (ptree->auth.ntp_signd_socket) {
1883 if (ntp_signd_socket != default_ntp_signd_socket)
1884 free(ntp_signd_socket);
1885 ntp_signd_socket = estrdup(ptree->auth.ntp_signd_socket);
1889 if (ptree->auth.cryptosw && !cryptosw) {
1893 #endif /* AUTOKEY */
1896 * Count the number of trusted keys to preallocate storage and
1897 * size the hash table.
1900 my_val = HEAD_PFIFO(ptree->auth.trusted_key_list);
1901 for (; my_val != NULL; my_val = my_val->link) {
1902 if (T_Integer == my_val->type) {
1903 first = my_val->value.i;
1904 if (first > 1 && first <= NTP_MAXKEY)
1907 REQUIRE(T_Intrange == my_val->type);
1908 first = my_val->value.r.first;
1909 last = my_val->value.r.last;
1910 if (!(first > last || first < 1 ||
1911 last > NTP_MAXKEY)) {
1912 count += 1 + last - first;
1916 auth_prealloc_symkeys(count);
1919 if (ptree->auth.keys)
1920 getauthkeys(ptree->auth.keys);
1922 /* Control Key Command */
1923 if (ptree->auth.control_key)
1924 ctl_auth_keyid = (keyid_t)ptree->auth.control_key;
1926 /* Requested Key Command */
1927 if (ptree->auth.request_key) {
1928 DPRINTF(4, ("set info_auth_keyid to %08lx\n",
1929 (u_long) ptree->auth.request_key));
1930 info_auth_keyid = (keyid_t)ptree->auth.request_key;
1933 /* Trusted Key Command */
1934 my_val = HEAD_PFIFO(ptree->auth.trusted_key_list);
1935 for (; my_val != NULL; my_val = my_val->link) {
1936 if (T_Integer == my_val->type) {
1937 first = my_val->value.i;
1938 if (first >= 1 && first <= NTP_MAXKEY) {
1939 authtrust(first, TRUE);
1942 "Ignoring invalid trustedkey %d, min 1 max %d.",
1946 first = my_val->value.r.first;
1947 last = my_val->value.r.last;
1948 if (first > last || first < 1 ||
1949 last > NTP_MAXKEY) {
1951 "Ignoring invalid trustedkey range %d ... %d, min 1 max %d.",
1952 first, last, NTP_MAXKEY);
1954 for (i = first; i <= last; i++) {
1962 /* crypto revoke command */
1963 if (ptree->auth.revoke)
1964 sys_revoke = 1UL << ptree->auth.revoke;
1965 #endif /* AUTOKEY */
1976 destroy_attr_val_fifo(ptree->auth.crypto_cmd_list);
1977 ptree->auth.crypto_cmd_list = NULL;
1978 destroy_attr_val_fifo(ptree->auth.trusted_key_list);
1979 ptree->auth.trusted_key_list = NULL;
1981 #endif /* FREE_CFG_T */
1994 item = -1; /* quiet warning */
1996 tos = HEAD_PFIFO(ptree->orphan_cmds);
1997 for (; tos != NULL; tos = tos->link) {
2006 if (val > STRATUM_UNSPEC - 1) {
2007 msyslog(LOG_WARNING,
2008 "Using maximum tos ceiling %d, %g requested",
2009 STRATUM_UNSPEC - 1, val);
2010 val = STRATUM_UNSPEC - 1;
2012 item = PROTO_CEILING;
2020 item = PROTO_COHORT;
2024 item = PROTO_ORPHAN;
2028 item = PROTO_ORPHWAIT;
2032 item = PROTO_MINDISP;
2036 item = PROTO_MAXDIST;
2040 item = PROTO_MINCLOCK;
2044 item = PROTO_MAXCLOCK;
2048 item = PROTO_MINSANE;
2052 item = PROTO_BEACON;
2055 proto_config(item, 0, val, NULL);
2066 FREE_ATTR_VAL_FIFO(ptree->orphan_cmds);
2068 #endif /* FREE_CFG_T */
2076 int_node *pfilegen_token;
2077 const char *filegen_string;
2078 const char *filegen_file;
2080 filegen_node *my_node;
2085 /* Set the statistics directory */
2086 if (ptree->stats_dir)
2087 stats_config(STATS_STATSDIR, ptree->stats_dir);
2090 * Calling filegen_get is brain dead. Doing a string
2091 * comparison to find the relavant filegen structure is
2094 * Through the parser, we already know which filegen is
2095 * being specified. Hence, we should either store a
2096 * pointer to the specified structure in the syntax tree
2097 * or an index into a filegen array.
2099 * Need to change the filegen code to reflect the above.
2102 /* Turn on the specified statistics */
2103 pfilegen_token = HEAD_PFIFO(ptree->stats_list);
2104 for (; pfilegen_token != NULL; pfilegen_token = pfilegen_token->link) {
2105 filegen_string = keyword(pfilegen_token->i);
2106 filegen = filegen_get(filegen_string);
2107 if (NULL == filegen) {
2109 "stats %s unrecognized",
2113 DPRINTF(4, ("enabling filegen for %s statistics '%s%s'\n",
2114 filegen_string, filegen->dir,
2116 filegen_flag = filegen->flag;
2117 filegen_flag |= FGEN_FLAG_ENABLED;
2118 filegen_config(filegen, statsdir, filegen_string,
2119 filegen->type, filegen_flag);
2122 /* Configure the statistics with the options */
2123 my_node = HEAD_PFIFO(ptree->filegen_opts);
2124 for (; my_node != NULL; my_node = my_node->link) {
2125 filegen_string = keyword(my_node->filegen_token);
2126 filegen = filegen_get(filegen_string);
2127 if (NULL == filegen) {
2129 "filegen category '%s' unrecognized",
2133 filegen_file = filegen_string;
2135 /* Initialize the filegen variables to their pre-configuration states */
2136 filegen_flag = filegen->flag;
2137 filegen_type = filegen->type;
2139 /* "filegen ... enabled" is the default (when filegen is used) */
2140 filegen_flag |= FGEN_FLAG_ENABLED;
2142 my_opts = HEAD_PFIFO(my_node->options);
2143 for (; my_opts != NULL; my_opts = my_opts->link) {
2144 switch (my_opts->attr) {
2147 filegen_file = my_opts->value.s;
2151 switch (my_opts->value.i) {
2158 filegen_type = FILEGEN_NONE;
2162 filegen_type = FILEGEN_PID;
2166 filegen_type = FILEGEN_DAY;
2170 filegen_type = FILEGEN_WEEK;
2174 filegen_type = FILEGEN_MONTH;
2178 filegen_type = FILEGEN_YEAR;
2182 filegen_type = FILEGEN_AGE;
2188 switch (my_opts->value.i) {
2191 filegen_flag |= FGEN_FLAG_LINK;
2195 filegen_flag &= ~FGEN_FLAG_LINK;
2199 filegen_flag |= FGEN_FLAG_ENABLED;
2203 filegen_flag &= ~FGEN_FLAG_ENABLED;
2208 "Unknown filegen flag token %d",
2216 "Unknown filegen option token %d",
2221 filegen_config(filegen, statsdir, filegen_file,
2222 filegen_type, filegen_flag);
2229 free_config_monitor(
2233 if (ptree->stats_dir) {
2234 free(ptree->stats_dir);
2235 ptree->stats_dir = NULL;
2238 FREE_INT_FIFO(ptree->stats_list);
2239 FREE_FILEGEN_FIFO(ptree->filegen_opts);
2241 #endif /* FREE_CFG_T */
2250 static int warned_signd;
2252 restrict_node * my_node;
2253 int_node * curr_flag;
2256 struct addrinfo hints;
2257 struct addrinfo * ai_list;
2258 struct addrinfo * pai;
2260 int restrict_default;
2264 const char * signd_warning =
2265 #ifdef HAVE_NTP_SIGND
2266 "MS-SNTP signd operations currently block ntpd degrading service to all clients.";
2268 "mssntp restrict bit ignored, this ntpd was configured without --enable-ntp-signd.";
2271 /* Configure the mru options */
2272 my_opt = HEAD_PFIFO(ptree->mru_opts);
2273 for (; my_opt != NULL; my_opt = my_opt->link) {
2277 switch (my_opt->attr) {
2280 if (0 <= my_opt->value.i)
2281 mru_incalloc = my_opt->value.u;
2287 if (0 <= my_opt->value.i)
2288 mru_incalloc = (my_opt->value.u * 1024U)
2289 / sizeof(mon_entry);
2295 if (0 <= my_opt->value.i)
2296 mru_initalloc = my_opt->value.u;
2302 if (0 <= my_opt->value.i)
2303 mru_initalloc = (my_opt->value.u * 1024U)
2304 / sizeof(mon_entry);
2310 if (0 <= my_opt->value.i)
2311 mru_mindepth = my_opt->value.u;
2317 mru_maxage = my_opt->value.i;
2321 if (0 <= my_opt->value.i)
2322 mru_maxdepth = my_opt->value.u;
2324 mru_maxdepth = UINT_MAX;
2328 if (0 <= my_opt->value.i)
2329 mru_maxdepth = (my_opt->value.u * 1024U) /
2332 mru_maxdepth = UINT_MAX;
2337 "Unknown mru option %s (%d)",
2338 keyword(my_opt->attr), my_opt->attr);
2343 "mru %s %d out of range, ignored.",
2344 keyword(my_opt->attr), my_opt->value.i);
2347 /* Configure the discard options */
2348 my_opt = HEAD_PFIFO(ptree->discard_opts);
2349 for (; my_opt != NULL; my_opt = my_opt->link) {
2351 switch (my_opt->attr) {
2354 if (0 <= my_opt->value.i &&
2355 my_opt->value.i <= UCHAR_MAX)
2356 ntp_minpoll = (u_char)my_opt->value.u;
2359 "discard average %d out of range, ignored.",
2364 ntp_minpkt = my_opt->value.i;
2368 mon_age = my_opt->value.i;
2373 "Unknown discard option %s (%d)",
2374 keyword(my_opt->attr), my_opt->attr);
2379 /* Configure the restrict options */
2380 my_node = HEAD_PFIFO(ptree->restrict_opts);
2381 for (; my_node != NULL; my_node = my_node->link) {
2382 /* Parse the flags */
2386 curr_flag = HEAD_PFIFO(my_node->flags);
2387 for (; curr_flag != NULL; curr_flag = curr_flag->link) {
2388 switch (curr_flag->i) {
2395 mflags |= RESM_NTPONLY;
2399 mflags |= RESM_SOURCE;
2407 flags |= RES_IGNORE;
2415 flags |= RES_MSSNTP;
2419 flags |= RES_LIMITED;
2423 flags |= RES_LPTRAP;
2427 flags |= RES_NOMODIFY;
2431 flags |= RES_NOMRULIST;
2435 flags |= RES_NOPEER;
2439 flags |= RES_NOQUERY;
2443 flags |= RES_DONTSERVE;
2447 flags |= RES_NOTRAP;
2451 flags |= RES_DONTTRUST;
2455 flags |= RES_VERSION;
2460 if ((RES_MSSNTP & flags) && !warned_signd) {
2462 fprintf(stderr, "%s\n", signd_warning);
2463 msyslog(LOG_WARNING, "%s", signd_warning);
2466 /* It would be swell if we could identify the line number */
2467 if ((RES_KOD & flags) && !(RES_LIMITED & flags)) {
2468 const char *kod_where = (my_node->addr)
2469 ? my_node->addr->address
2470 : (mflags & RESM_SOURCE)
2473 const char *kod_warn = "KOD does nothing without LIMITED.";
2475 fprintf(stderr, "restrict %s: %s\n", kod_where, kod_warn);
2476 msyslog(LOG_WARNING, "restrict %s: %s", kod_where, kod_warn);
2482 restrict_default = 0;
2484 if (NULL == my_node->addr) {
2486 if (!(RESM_SOURCE & mflags)) {
2488 * The user specified a default rule
2489 * without a -4 / -6 qualifier, add to
2492 restrict_default = 1;
2494 /* apply "restrict source ..." */
2495 DPRINTF(1, ("restrict source template mflags %x flags %x\n",
2497 hack_restrict(RESTRICT_FLAGS, NULL,
2498 NULL, mflags, flags, 0);
2502 /* Resolve the specified address */
2503 AF(&addr) = (u_short)my_node->addr->type;
2505 if (getnetnum(my_node->addr->address,
2506 &addr, 1, t_UNK) != 1) {
2508 * Attempt a blocking lookup. This
2509 * is in violation of the nonblocking
2510 * design of ntpd's mainline code. The
2511 * alternative of running without the
2512 * restriction until the name resolved
2514 * Ideally some scheme could be used for
2515 * restrict directives in the startup
2516 * ntp.conf to delay starting up the
2517 * protocol machinery until after all
2518 * restrict hosts have been resolved.
2522 hints.ai_protocol = IPPROTO_UDP;
2523 hints.ai_socktype = SOCK_DGRAM;
2524 hints.ai_family = my_node->addr->type;
2525 rc = getaddrinfo(my_node->addr->address,
2530 "restrict: ignoring line %d, address/host '%s' unusable.",
2532 my_node->addr->address);
2535 INSIST(ai_list != NULL);
2537 INSIST(pai->ai_addr != NULL);
2538 INSIST(sizeof(addr) >=
2540 memcpy(&addr, pai->ai_addr,
2542 INSIST(AF_INET == AF(&addr) ||
2543 AF_INET6 == AF(&addr));
2546 SET_HOSTMASK(&mask, AF(&addr));
2548 /* Resolve the mask */
2549 if (my_node->mask) {
2551 AF(&mask) = my_node->mask->type;
2552 if (getnetnum(my_node->mask->address,
2553 &mask, 1, t_MSK) != 1) {
2555 "restrict: ignoring line %d, mask '%s' unusable.",
2557 my_node->mask->address);
2564 if (restrict_default) {
2565 AF(&addr) = AF_INET;
2566 AF(&mask) = AF_INET;
2567 hack_restrict(RESTRICT_FLAGS, &addr,
2568 &mask, mflags, flags, 0);
2569 AF(&addr) = AF_INET6;
2570 AF(&mask) = AF_INET6;
2574 hack_restrict(RESTRICT_FLAGS, &addr,
2575 &mask, mflags, flags, 0);
2577 NULL != (pai = pai->ai_next)) {
2578 INSIST(pai->ai_addr != NULL);
2579 INSIST(sizeof(addr) >=
2582 memcpy(&addr, pai->ai_addr,
2584 INSIST(AF_INET == AF(&addr) ||
2585 AF_INET6 == AF(&addr));
2586 SET_HOSTMASK(&mask, AF(&addr));
2588 } while (pai != NULL);
2590 if (ai_list != NULL)
2591 freeaddrinfo(ai_list);
2603 FREE_ATTR_VAL_FIFO(ptree->mru_opts);
2604 FREE_ATTR_VAL_FIFO(ptree->discard_opts);
2605 FREE_RESTRICT_FIFO(ptree->restrict_opts);
2607 #endif /* FREE_CFG_T */
2615 attr_val * rlimit_av;
2617 rlimit_av = HEAD_PFIFO(ptree->rlimit);
2618 for (; rlimit_av != NULL; rlimit_av = rlimit_av->link) {
2619 switch (rlimit_av->attr) {
2626 /* What if we HAVE_OPT(SAVECONFIGQUIT) ? */
2627 if (rlimit_av->value.i == -1) {
2628 # if defined(HAVE_MLOCKALL)
2629 if (cur_memlock != 0) {
2630 if (-1 == munlockall()) {
2631 msyslog(LOG_ERR, "munlockall() failed: %m");
2635 # endif /* HAVE_MLOCKALL */
2636 } else if (rlimit_av->value.i >= 0) {
2637 #if defined(RLIMIT_MEMLOCK)
2638 # if defined(HAVE_MLOCKALL)
2639 if (cur_memlock != 1) {
2640 if (-1 == mlockall(MCL_CURRENT|MCL_FUTURE)) {
2641 msyslog(LOG_ERR, "mlockall() failed: %m");
2644 # endif /* HAVE_MLOCKALL */
2645 ntp_rlimit(RLIMIT_MEMLOCK,
2646 (rlim_t)(rlimit_av->value.i * 1024 * 1024),
2651 /* STDERR as well would be fine... */
2652 msyslog(LOG_WARNING, "'rlimit memlock' specified but is not available on this system.");
2653 #endif /* RLIMIT_MEMLOCK */
2655 msyslog(LOG_WARNING, "'rlimit memlock' value of %d is unexpected!", rlimit_av->value.i);
2660 #if defined(RLIMIT_STACK)
2661 ntp_rlimit(RLIMIT_STACK,
2662 (rlim_t)(rlimit_av->value.i * 4096),
2666 /* STDERR as well would be fine... */
2667 msyslog(LOG_WARNING, "'rlimit stacksize' specified but is not available on this system.");
2668 #endif /* RLIMIT_STACK */
2672 #if defined(RLIMIT_NOFILE)
2673 ntp_rlimit(RLIMIT_NOFILE,
2674 (rlim_t)(rlimit_av->value.i),
2678 /* STDERR as well would be fine... */
2679 msyslog(LOG_WARNING, "'rlimit filenum' specified but is not available on this system.");
2680 #endif /* RLIMIT_NOFILE */
2697 item = -1; /* quiet warning */
2699 tinker = HEAD_PFIFO(ptree->tinker);
2700 for (; tinker != NULL; tinker = tinker->link) {
2701 switch (tinker->attr) {
2720 item = LOOP_HUFFPUFF;
2732 item = LOOP_MAX_BACK;
2736 item = LOOP_MAX_FWD;
2740 item = LOOP_MINSTEP;
2747 loop_config(item, tinker->value.d);
2758 FREE_ATTR_VAL_FIFO(ptree->rlimit);
2766 FREE_ATTR_VAL_FIFO(ptree->tinker);
2768 #endif /* FREE_CFG_T */
2772 * config_nic_rules - apply interface listen/ignore/drop items
2778 int/*BOOL*/ input_from_file
2781 nic_rule_node * curr_node;
2783 nic_rule_match match_type;
2784 nic_rule_action action;
2790 curr_node = HEAD_PFIFO(ptree->nic_rules);
2792 if (curr_node != NULL
2793 && (HAVE_OPT( NOVIRTUALIPS ) || HAVE_OPT( INTERFACE ))) {
2795 "interface/nic rules are not allowed with --interface (-I) or --novirtualips (-L)%s",
2796 (input_from_file) ? ", exiting" : "");
2797 if (input_from_file)
2803 for (; curr_node != NULL; curr_node = curr_node->link) {
2805 if_name = curr_node->if_name;
2806 if (if_name != NULL)
2807 if_name = estrdup(if_name);
2809 switch (curr_node->match_class) {
2814 * this assignment quiets a gcc "may be used
2815 * uninitialized" warning and is here for no
2818 match_type = MATCH_ALL;
2825 * 0 is out of range for valid token T_...
2826 * and in a nic_rules_node indicates the
2827 * interface descriptor is either a name or
2828 * address, stored in if_name in either case.
2830 INSIST(if_name != NULL);
2831 pchSlash = strchr(if_name, '/');
2832 if (pchSlash != NULL)
2834 if (is_ip_address(if_name, AF_UNSPEC, &addr)) {
2835 match_type = MATCH_IFADDR;
2836 if (pchSlash != NULL
2837 && 1 == sscanf(pchSlash + 1, "%d",
2840 SIZEOF_INADDR(AF(&addr));
2841 prefixlen = max(-1, prefixlen);
2842 prefixlen = min(prefixlen,
2846 match_type = MATCH_IFNAME;
2847 if (pchSlash != NULL)
2853 match_type = MATCH_ALL;
2857 match_type = MATCH_IPV4;
2861 match_type = MATCH_IPV6;
2865 match_type = MATCH_WILDCARD;
2869 switch (curr_node->action) {
2874 * this assignment quiets a gcc "may be used
2875 * uninitialized" warning and is here for no
2878 action = ACTION_LISTEN;
2884 action = ACTION_LISTEN;
2888 action = ACTION_IGNORE;
2892 action = ACTION_DROP;
2896 add_nic_rule(match_type, if_name, prefixlen,
2898 timer_interfacetimeout(current_time + 2);
2899 if (if_name != NULL)
2908 free_config_nic_rules(
2912 nic_rule_node *curr_node;
2914 if (ptree->nic_rules != NULL) {
2916 UNLINK_FIFO(curr_node, *ptree->nic_rules, link);
2917 if (NULL == curr_node)
2919 free(curr_node->if_name);
2922 free(ptree->nic_rules);
2923 ptree->nic_rules = NULL;
2926 #endif /* FREE_CFG_T */
2930 apply_enable_disable(
2931 attr_val_fifo * fifo,
2935 attr_val *curr_flag;
2937 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
2941 for (curr_flag = HEAD_PFIFO(fifo);
2943 curr_flag = curr_flag->link) {
2945 option = curr_flag->value.i;
2950 "can not apply enable/disable token %d, unknown",
2955 proto_config(PROTO_AUTHENTICATE, enable, 0., NULL);
2959 proto_config(PROTO_BROADCLIENT, enable, 0., NULL);
2963 proto_config(PROTO_CAL, enable, 0., NULL);
2967 proto_config(PROTO_KERNEL, enable, 0., NULL);
2971 proto_config(PROTO_MONITOR, enable, 0., NULL);
2975 proto_config(PROTO_NTP, enable, 0., NULL);
2979 proto_config(PROTO_MODE7, enable, 0., NULL);
2983 proto_config(PROTO_FILEGEN, enable, 0., NULL);
2987 proto_config(PROTO_UECRYPTO, enable, 0., NULL);
2991 proto_config(PROTO_UECRYPTONAK, enable, 0., NULL);
2995 proto_config(PROTO_UEDIGEST, enable, 0., NULL);
2998 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
3001 while (pentry->token) {
3002 if (pentry->token == option)
3006 if (!pentry->token) {
3008 "compat token %d not in bc_list[]",
3012 pentry->enabled = enable;
3025 apply_enable_disable(ptree->enable_opts, 1);
3026 apply_enable_disable(ptree->disable_opts, 0);
3032 free_config_system_opts(
3036 FREE_ATTR_VAL_FIFO(ptree->enable_opts);
3037 FREE_ATTR_VAL_FIFO(ptree->disable_opts);
3039 #endif /* FREE_CFG_T */
3049 my_lc = HEAD_PFIFO(ptree->logconfig);
3050 for (; my_lc != NULL; my_lc = my_lc->link) {
3051 switch (my_lc->attr) {
3054 ntp_syslogmask |= get_logmask(my_lc->value.s);
3058 ntp_syslogmask &= ~get_logmask(my_lc->value.s);
3062 ntp_syslogmask = get_logmask(my_lc->value.s);
3074 free_config_logconfig(
3078 FREE_ATTR_VAL_FIFO(ptree->logconfig);
3080 #endif /* FREE_CFG_T */
3093 sn = HEAD_PFIFO(ptree->phone);
3094 for (; sn != NULL; sn = sn->link) {
3095 /* need to leave array entry for NULL terminator */
3096 if (i < COUNTOF(sys_phone) - 1) {
3097 sys_phone[i++] = estrdup(sn->s);
3098 sys_phone[i] = NULL;
3101 "phone: Number of phone entries exceeds %zu. Ignoring phone %s...",
3102 (COUNTOF(sys_phone) - 1), sn->s);
3113 #ifdef HAVE_DNSREGISTRATION
3114 extern int mdnstries;
3115 mdnstries = ptree->mdnstries;
3116 #endif /* HAVE_DNSREGISTRATION */
3125 FREE_STRING_FIFO(ptree->phone);
3127 #endif /* FREE_CFG_T */
3136 setvar_node *my_node;
3137 size_t varlen, vallen, octets;
3141 my_node = HEAD_PFIFO(ptree->setvar);
3142 for (; my_node != NULL; my_node = my_node->link) {
3143 varlen = strlen(my_node->var);
3144 vallen = strlen(my_node->val);
3145 octets = varlen + vallen + 1 + 1;
3146 str = erealloc(str, octets);
3147 snprintf(str, octets, "%s=%s", my_node->var,
3149 set_sys_var(str, octets, (my_node->isdefault)
3165 FREE_SETVAR_FIFO(ptree->setvar);
3167 #endif /* FREE_CFG_T */
3179 curr_ttl = HEAD_PFIFO(ptree->ttl);
3180 for (; curr_ttl != NULL; curr_ttl = curr_ttl->link) {
3181 if (i < COUNTOF(sys_ttl))
3182 sys_ttl[i++] = (u_char)curr_ttl->i;
3185 "ttl: Number of TTL entries exceeds %zu. Ignoring TTL %d...",
3186 COUNTOF(sys_ttl), curr_ttl->i);
3199 FREE_INT_FIFO(ptree->ttl);
3201 #endif /* FREE_CFG_T */
3210 addr_opts_node *curr_trap;
3212 sockaddr_u addr_sock;
3213 sockaddr_u peeraddr;
3214 struct interface *localaddr;
3215 struct addrinfo hints;
3217 settrap_parms *pstp;
3222 /* silence warning about addr_sock potentially uninitialized */
3223 AF(&addr_sock) = AF_UNSPEC;
3225 curr_trap = HEAD_PFIFO(ptree->trap);
3226 for (; curr_trap != NULL; curr_trap = curr_trap->link) {
3231 curr_opt = HEAD_PFIFO(curr_trap->options);
3232 for (; curr_opt != NULL; curr_opt = curr_opt->link) {
3233 if (T_Port == curr_opt->attr) {
3234 if (curr_opt->value.i < 1
3235 || curr_opt->value.i > USHRT_MAX) {
3237 "invalid port number "
3242 port = (u_short)curr_opt->value.i;
3244 else if (T_Interface == curr_opt->attr) {
3245 /* Resolve the interface address */
3246 ZERO_SOCK(&addr_sock);
3247 if (getnetnum(curr_opt->value.s,
3248 &addr_sock, 1, t_UNK) != 1) {
3253 localaddr = findinterface(&addr_sock);
3255 if (NULL == localaddr) {
3257 "can't find interface with address %s",
3264 /* Now process the trap for the specified interface
3270 ZERO_SOCK(&peeraddr);
3271 rc = getnetnum(curr_trap->addr->address,
3272 &peeraddr, 1, t_UNK);
3276 "trap: unable to use IP address %s.",
3277 curr_trap->addr->address);
3278 #else /* WORKER follows */
3280 * save context and hand it off
3281 * for name resolution.
3284 hints.ai_protocol = IPPROTO_UDP;
3285 hints.ai_socktype = SOCK_DGRAM;
3286 snprintf(port_text, sizeof(port_text),
3288 hints.ai_flags = Z_AI_NUMERICSERV;
3289 pstp = emalloc_zero(sizeof(*pstp));
3290 if (localaddr != NULL) {
3291 hints.ai_family = localaddr->family;
3292 pstp->ifaddr_nonnull = 1;
3293 memcpy(&pstp->ifaddr,
3295 sizeof(pstp->ifaddr));
3297 rc = getaddrinfo_sometime(
3298 curr_trap->addr->address,
3301 &trap_name_resolved,
3305 "config_trap: getaddrinfo_sometime(%s,%s): %m",
3306 curr_trap->addr->address,
3311 /* port is at same location for v4 and v6 */
3312 SET_PORT(&peeraddr, port);
3314 if (NULL == localaddr)
3315 localaddr = ANY_INTERFACE_CHOOSE(&peeraddr);
3317 AF(&peeraddr) = AF(&addr_sock);
3319 if (!ctlsettrap(&peeraddr, localaddr, 0,
3322 "set trap %s -> %s failed.",
3331 * trap_name_resolved()
3333 * Callback invoked when config_trap()'s DNS lookup completes.
3342 const char * service,
3343 const struct addrinfo * hints,
3344 const struct addrinfo * res
3347 settrap_parms *pstp;
3348 struct interface *localaddr;
3349 sockaddr_u peeraddr;
3357 "giving up resolving trap host %s: %s (%d)",
3358 name, gai_strerror(rescode), rescode);
3362 INSIST(sizeof(peeraddr) >= res->ai_addrlen);
3364 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
3366 if (pstp->ifaddr_nonnull)
3367 localaddr = findinterface(&pstp->ifaddr);
3368 if (NULL == localaddr)
3369 localaddr = ANY_INTERFACE_CHOOSE(&peeraddr);
3370 if (!ctlsettrap(&peeraddr, localaddr, 0, NTP_VERSION))
3371 msyslog(LOG_ERR, "set trap %s -> %s failed.",
3372 latoa(localaddr), stoa(&peeraddr));
3375 # endif /* WORKER */
3385 FREE_ADDR_OPTS_FIFO(ptree->trap);
3387 #endif /* FREE_CFG_T */
3396 addr_opts_node *curr_fudge;
3398 sockaddr_u addr_sock;
3399 address_node *addr_node;
3400 struct refclockstat clock_stat;
3403 curr_fudge = HEAD_PFIFO(ptree->fudge);
3404 for (; curr_fudge != NULL; curr_fudge = curr_fudge->link) {
3407 /* Get the reference clock address and
3408 * ensure that it is sane
3410 addr_node = curr_fudge->addr;
3411 ZERO_SOCK(&addr_sock);
3412 if (getnetnum(addr_node->address, &addr_sock, 1, t_REF)
3416 "unrecognized fudge reference clock address %s, line ignored",
3420 if (!ISREFCLOCKADR(&addr_sock)) {
3423 "inappropriate address %s for the fudge command, line ignored",
3427 /* Parse all the options to the fudge command */
3429 curr_opt = HEAD_PFIFO(curr_fudge->options);
3430 for (; curr_opt != NULL; curr_opt = curr_opt->link) {
3431 switch (curr_opt->attr) {
3434 clock_stat.haveflags |= CLK_HAVETIME1;
3435 clock_stat.fudgetime1 = curr_opt->value.d;
3439 clock_stat.haveflags |= CLK_HAVETIME2;
3440 clock_stat.fudgetime2 = curr_opt->value.d;
3444 clock_stat.haveflags |= CLK_HAVEVAL1;
3445 clock_stat.fudgeval1 = curr_opt->value.i;
3449 clock_stat.haveflags |= CLK_HAVEVAL2;
3450 clock_stat.fudgeval2 = 0;
3451 memcpy(&clock_stat.fudgeval2,
3453 min(strlen(curr_opt->value.s), 4));
3457 clock_stat.haveflags |= CLK_HAVEFLAG1;
3458 if (curr_opt->value.i)
3459 clock_stat.flags |= CLK_FLAG1;
3461 clock_stat.flags &= ~CLK_FLAG1;
3465 clock_stat.haveflags |= CLK_HAVEFLAG2;
3466 if (curr_opt->value.i)
3467 clock_stat.flags |= CLK_FLAG2;
3469 clock_stat.flags &= ~CLK_FLAG2;
3473 clock_stat.haveflags |= CLK_HAVEFLAG3;
3474 if (curr_opt->value.i)
3475 clock_stat.flags |= CLK_FLAG3;
3477 clock_stat.flags &= ~CLK_FLAG3;
3481 clock_stat.haveflags |= CLK_HAVEFLAG4;
3482 if (curr_opt->value.i)
3483 clock_stat.flags |= CLK_FLAG4;
3485 clock_stat.flags &= ~CLK_FLAG4;
3490 "Unexpected fudge flag %s (%d) for %s",
3491 token_name(curr_opt->attr),
3492 curr_opt->attr, stoa(&addr_sock));
3493 exit(curr_opt->attr ? curr_opt->attr : 1);
3498 refclock_control(&addr_sock, &clock_stat, NULL);
3511 FREE_ADDR_OPTS_FIFO(ptree->fudge);
3513 #endif /* FREE_CFG_T */
3524 curr_var = HEAD_PFIFO(ptree->vars);
3525 for (; curr_var != NULL; curr_var = curr_var->link) {
3526 /* Determine which variable to set and set it */
3527 switch (curr_var->attr) {
3529 case T_Broadcastdelay:
3530 proto_config(PROTO_BROADDELAY, 0, curr_var->value.d, NULL);
3534 loop_config(LOOP_TICK, curr_var->value.d);
3538 if ('\0' == curr_var->value.s[0]) {
3539 stats_drift_file = 0;
3540 msyslog(LOG_INFO, "config: driftfile disabled");
3542 stats_config(STATS_FREQ_FILE, curr_var->value.s);
3546 /* DSCP is in the upper 6 bits of the IP TOS/DS field */
3547 qos = curr_var->value.i << 2;
3551 sys_ident = curr_var->value.s;
3554 case T_WanderThreshold: /* FALLTHROUGH */
3556 wander_threshold = curr_var->value.d;
3560 stats_config(STATS_LEAP_FILE, curr_var->value.s);
3564 case T_Leapsmearinterval:
3565 leap_smear_intv = curr_var->value.i;
3566 msyslog(LOG_INFO, "config: leap smear interval %i s", leap_smear_intv);
3571 stats_config(STATS_PID_FILE, curr_var->value.s);
3575 if (-1 == change_logfile(curr_var->value.s, TRUE))
3577 "Cannot open logfile %s: %m",
3581 case T_Saveconfigdir:
3582 if (saveconfigdir != NULL)
3583 free(saveconfigdir);
3584 len = strlen(curr_var->value.s);
3586 saveconfigdir = NULL;
3587 } else if (DIR_SEP != curr_var->value.s[len - 1]
3588 #ifdef SYS_WINNT /* slash is also a dir. sep. on Windows */
3589 && '/' != curr_var->value.s[len - 1]
3593 saveconfigdir = emalloc(len + 1);
3594 snprintf(saveconfigdir, len + 1,
3599 saveconfigdir = estrdup(
3606 sys_automax = curr_var->value.i;
3612 "config_vars(): unexpected token %d",
3625 FREE_ATTR_VAL_FIFO(ptree->vars);
3627 #endif /* FREE_CFG_T */
3630 /* Define a function to check if a resolved address is sane.
3631 * If yes, return 1, else return 0;
3634 is_sane_resolved_address(
3635 sockaddr_u * peeraddr,
3639 if (!ISREFCLOCKADR(peeraddr) && ISBADADR(peeraddr)) {
3641 "attempt to configure invalid address %s",
3646 * Shouldn't be able to specify multicast
3647 * address for server/peer!
3648 * and unicast address for manycastclient!
3650 if ((T_Server == hmode || T_Peer == hmode || T_Pool == hmode)
3651 && IS_MCAST(peeraddr)) {
3653 "attempt to configure invalid address %s",
3657 if (T_Manycastclient == hmode && !IS_MCAST(peeraddr)) {
3659 "attempt to configure invalid address %s",
3664 if (IS_IPV6(peeraddr) && !ipv6_works)
3667 /* Ok, all tests succeeded, now we can return 1 */
3674 get_correct_host_mode(
3682 case T_Manycastclient:
3689 return MODE_BROADCAST;
3698 * peerflag_bits() get config_peers() peerflags value from a
3699 * peer_node's queue of flag attr_val entries.
3709 /* translate peerflags options to bits */
3711 option = HEAD_PFIFO(pn->peerflags);
3712 for (; option != NULL; option = option->link) {
3713 switch (option->value.i) {
3720 peerflags |= FLAG_SKEY;
3724 peerflags |= FLAG_BURST;
3728 peerflags |= FLAG_IBURST;
3732 peerflags |= FLAG_NOSELECT;
3736 peerflags |= FLAG_PREEMPT;
3740 peerflags |= FLAG_PREFER;
3744 peerflags |= FLAG_TRUE;
3748 peerflags |= FLAG_XLEAVE;
3762 sockaddr_u peeraddr;
3763 struct addrinfo hints;
3764 peer_node * curr_peer;
3765 peer_resolved_ctx * ctx;
3768 /* add servers named on the command line with iburst implied */
3770 cmdline_server_count > 0;
3771 cmdline_server_count--, cmdline_servers++) {
3773 ZERO_SOCK(&peeraddr);
3775 * If we have a numeric address, we can safely
3776 * proceed in the mainline with it. Otherwise, hand
3777 * the hostname off to the blocking child.
3779 if (is_ip_address(*cmdline_servers, AF_UNSPEC,
3782 SET_PORT(&peeraddr, NTP_PORT);
3783 if (is_sane_resolved_address(&peeraddr,
3798 /* we have a hostname to resolve */
3800 ctx = emalloc_zero(sizeof(*ctx));
3801 ctx->family = AF_UNSPEC;
3802 ctx->host_mode = T_Server;
3803 ctx->hmode = MODE_CLIENT;
3804 ctx->version = NTP_VERSION;
3805 ctx->flags = FLAG_IBURST;
3808 hints.ai_family = (u_short)ctx->family;
3809 hints.ai_socktype = SOCK_DGRAM;
3810 hints.ai_protocol = IPPROTO_UDP;
3812 getaddrinfo_sometime(*cmdline_servers,
3815 &peer_name_resolved,
3817 # else /* !WORKER follows */
3819 "hostname %s can not be used, please use IP address instead.",
3820 curr_peer->addr->address);
3825 /* add associations from the configuration file */
3826 curr_peer = HEAD_PFIFO(ptree->peers);
3827 for (; curr_peer != NULL; curr_peer = curr_peer->link) {
3828 ZERO_SOCK(&peeraddr);
3829 /* Find the correct host-mode */
3830 hmode = get_correct_host_mode(curr_peer->host_mode);
3833 if (T_Pool == curr_peer->host_mode) {
3834 AF(&peeraddr) = curr_peer->addr->type;
3837 curr_peer->addr->address,
3840 curr_peer->peerversion,
3843 peerflag_bits(curr_peer),
3848 * If we have a numeric address, we can safely
3849 * proceed in the mainline with it. Otherwise, hand
3850 * the hostname off to the blocking child.
3852 } else if (is_ip_address(curr_peer->addr->address,
3853 curr_peer->addr->type, &peeraddr)) {
3855 SET_PORT(&peeraddr, NTP_PORT);
3856 if (is_sane_resolved_address(&peeraddr,
3857 curr_peer->host_mode))
3863 curr_peer->peerversion,
3866 peerflag_bits(curr_peer),
3871 /* we have a hostname to resolve */
3873 ctx = emalloc_zero(sizeof(*ctx));
3874 ctx->family = curr_peer->addr->type;
3875 ctx->host_mode = curr_peer->host_mode;
3877 ctx->version = curr_peer->peerversion;
3878 ctx->minpoll = curr_peer->minpoll;
3879 ctx->maxpoll = curr_peer->maxpoll;
3880 ctx->flags = peerflag_bits(curr_peer);
3881 ctx->ttl = curr_peer->ttl;
3882 ctx->keyid = curr_peer->peerkey;
3883 ctx->group = curr_peer->group;
3886 hints.ai_family = ctx->family;
3887 hints.ai_socktype = SOCK_DGRAM;
3888 hints.ai_protocol = IPPROTO_UDP;
3890 getaddrinfo_sometime(curr_peer->addr->address,
3893 &peer_name_resolved, ctx);
3894 # else /* !WORKER follows */
3896 "hostname %s can not be used, please use IP address instead.",
3897 curr_peer->addr->address);
3905 * peer_name_resolved()
3907 * Callback invoked when config_peers()'s DNS lookup completes.
3916 const char * service,
3917 const struct addrinfo * hints,
3918 const struct addrinfo * res
3921 sockaddr_u peeraddr;
3922 peer_resolved_ctx * ctx;
3924 const char * fam_spec;
3931 DPRINTF(1, ("peer_name_resolved(%s) rescode %d\n", name, rescode));
3934 #ifndef IGNORE_DNS_ERRORS
3937 "giving up resolving host %s: %s (%d)",
3938 name, gai_strerror(rescode), rescode);
3939 #else /* IGNORE_DNS_ERRORS follows */
3940 getaddrinfo_sometime(name, service, hints,
3942 &peer_name_resolved, context);
3947 /* Loop to configure a single association */
3948 for (; res != NULL; res = res->ai_next) {
3949 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
3950 if (is_sane_resolved_address(&peeraddr,
3952 NLOG(NLOG_SYSINFO) {
3954 fam_spec = (AF_INET6 == af)
3959 msyslog(LOG_INFO, "DNS %s %s-> %s",
3989 peer_node *curr_peer;
3991 if (ptree->peers != NULL) {
3993 UNLINK_FIFO(curr_peer, *ptree->peers, link);
3994 if (NULL == curr_peer)
3996 destroy_address_node(curr_peer->addr);
3997 destroy_attr_val_fifo(curr_peer->peerflags);
4001 ptree->peers = NULL;
4004 #endif /* FREE_CFG_T */
4013 sockaddr_u peeraddr;
4014 struct addrinfo hints;
4015 unpeer_node * curr_unpeer;
4020 curr_unpeer = HEAD_PFIFO(ptree->unpeers);
4021 for (; curr_unpeer != NULL; curr_unpeer = curr_unpeer->link) {
4023 * Either AssocID will be zero, and we unpeer by name/
4024 * address addr, or it is nonzero and addr NULL.
4026 if (curr_unpeer->assocID) {
4027 p = findpeerbyassoc(curr_unpeer->assocID);
4029 msyslog(LOG_NOTICE, "unpeered %s",
4031 peer_clear(p, "GONE");
4039 AF(&peeraddr) = curr_unpeer->addr->type;
4040 name = curr_unpeer->addr->address;
4041 rc = getnetnum(name, &peeraddr, 0, t_UNK);
4042 /* Do we have a numeric address? */
4044 DPRINTF(1, ("unpeer: searching for %s\n",
4046 p = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);
4048 msyslog(LOG_NOTICE, "unpeered %s",
4050 peer_clear(p, "GONE");
4057 * It's not a numeric IP address, it's a hostname.
4058 * Check for associations with a matching hostname.
4060 for (p = peer_list; p != NULL; p = p->p_link)
4061 if (p->hostname != NULL)
4062 if (!strcasecmp(p->hostname, name))
4065 msyslog(LOG_NOTICE, "unpeered %s", name);
4066 peer_clear(p, "GONE");
4069 /* Resolve the hostname to address(es). */
4072 hints.ai_family = curr_unpeer->addr->type;
4073 hints.ai_socktype = SOCK_DGRAM;
4074 hints.ai_protocol = IPPROTO_UDP;
4075 getaddrinfo_sometime(name, "ntp", &hints,
4077 &unpeer_name_resolved, NULL);
4078 # else /* !WORKER follows */
4080 "hostname %s can not be used, please use IP address instead.",
4089 * unpeer_name_resolved()
4091 * Callback invoked when config_unpeers()'s DNS lookup completes.
4095 unpeer_name_resolved(
4100 const char * service,
4101 const struct addrinfo * hints,
4102 const struct addrinfo * res
4105 sockaddr_u peeraddr;
4108 const char * fam_spec;
4112 DPRINTF(1, ("unpeer_name_resolved(%s) rescode %d\n", name, rescode));
4115 msyslog(LOG_ERR, "giving up resolving unpeer %s: %s (%d)",
4116 name, gai_strerror(rescode), rescode);
4120 * Loop through the addresses found
4122 for (; res != NULL; res = res->ai_next) {
4123 INSIST(res->ai_addrlen <= sizeof(peeraddr));
4124 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
4125 DPRINTF(1, ("unpeer: searching for peer %s\n",
4127 peer = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);
4130 fam_spec = (AF_INET6 == af)
4135 msyslog(LOG_NOTICE, "unpeered %s %s-> %s", name,
4136 fam_spec, stoa(&peeraddr));
4137 peer_clear(peer, "GONE");
4147 free_config_unpeers(
4151 unpeer_node *curr_unpeer;
4153 if (ptree->unpeers != NULL) {
4155 UNLINK_FIFO(curr_unpeer, *ptree->unpeers, link);
4156 if (NULL == curr_unpeer)
4158 destroy_address_node(curr_unpeer->addr);
4161 free(ptree->unpeers);
4164 #endif /* FREE_CFG_T */
4169 config_reset_counters(
4173 int_node *counter_set;
4175 for (counter_set = HEAD_PFIFO(ptree->reset_counters);
4176 counter_set != NULL;
4177 counter_set = counter_set->link) {
4178 switch (counter_set->i) {
4180 DPRINTF(1, ("config_reset_counters %s (%d) invalid\n",
4181 keyword(counter_set->i), counter_set->i));
4219 free_config_reset_counters(
4223 FREE_INT_FIFO(ptree->reset_counters);
4225 #endif /* FREE_CFG_T */
4235 server_info *serv_info;
4236 attr_val *init_stmt;
4239 /* Check if a simulate block was found in the configuration code.
4240 * If not, return an error and exit
4242 sim_n = HEAD_PFIFO(ptree->sim_details);
4243 if (NULL == sim_n) {
4244 fprintf(stderr, "ERROR!! I couldn't find a \"simulate\" block for configuring the simulator.\n");
4245 fprintf(stderr, "\tCheck your configuration file.\n");
4249 /* Process the initialization statements
4250 * -------------------------------------
4252 init_stmt = HEAD_PFIFO(sim_n->init_opts);
4253 for (; init_stmt != NULL; init_stmt = init_stmt->link) {
4254 switch(init_stmt->attr) {
4257 simulation.beep_delay = init_stmt->value.d;
4260 case T_Sim_Duration:
4261 simulation.end_time = init_stmt->value.d;
4266 "Unknown simulator init token %d\n",
4272 /* Process the server list
4273 * -----------------------
4275 simulation.num_of_servers = 0;
4276 serv_info = HEAD_PFIFO(sim_n->servers);
4277 for (; serv_info != NULL; serv_info = serv_info->link)
4278 simulation.num_of_servers++;
4279 simulation.servers = eallocarray(simulation.num_of_servers,
4280 sizeof(simulation.servers[0]));
4283 serv_info = HEAD_PFIFO(sim_n->servers);
4284 for (; serv_info != NULL; serv_info = serv_info->link) {
4285 if (NULL == serv_info) {
4286 fprintf(stderr, "Simulator server list is corrupt\n");
4289 simulation.servers[i] = *serv_info;
4290 simulation.servers[i].link = NULL;
4295 printf("Creating server associations\n");
4296 create_server_associations();
4297 fprintf(stderr,"\tServer associations successfully created!!\n");
4308 server_info *serv_n;
4309 script_info *script_n;
4311 if (NULL == ptree->sim_details)
4313 sim_n = HEAD_PFIFO(ptree->sim_details);
4314 free(ptree->sim_details);
4315 ptree->sim_details = NULL;
4319 FREE_ATTR_VAL_FIFO(sim_n->init_opts);
4321 UNLINK_FIFO(serv_n, *sim_n->servers, link);
4324 free(serv_n->curr_script);
4325 if (serv_n->script != NULL) {
4327 UNLINK_FIFO(script_n, *serv_n->script,
4329 if (script_n == NULL)
4333 free(serv_n->script);
4339 #endif /* FREE_CFG_T */
4343 /* Define two different config functions. One for the daemon and the other for
4344 * the simulator. The simulator ignores a lot of the standard ntpd configuration
4351 int/*BOOL*/ input_from_files
4354 config_nic_rules(ptree, input_from_files);
4355 config_monitor(ptree);
4358 config_access(ptree);
4359 config_tinker(ptree);
4360 config_rlimit(ptree);
4361 config_system_opts(ptree);
4362 config_logconfig(ptree);
4363 config_phone(ptree);
4364 config_mdnstries(ptree);
4365 config_setvar(ptree);
4372 config_other_modes(ptree);
4373 config_peers(ptree);
4374 config_unpeers(ptree);
4375 config_fudge(ptree);
4376 config_reset_counters(ptree);
4378 #ifdef TEST_BLOCKING_WORKER
4380 struct addrinfo hints;
4383 hints.ai_socktype = SOCK_STREAM;
4384 hints.ai_protocol = IPPROTO_TCP;
4385 getaddrinfo_sometime("www.cnn.com", "ntp", &hints,
4387 gai_test_callback, (void *)1);
4388 hints.ai_family = AF_INET6;
4389 getaddrinfo_sometime("ipv6.google.com", "ntp", &hints,
4391 gai_test_callback, (void *)0x600);
4404 printf("Configuring Simulator...\n");
4405 printf("Some ntpd-specific commands in the configuration file will be ignored.\n");
4408 config_monitor(ptree);
4409 config_tinker(ptree);
4411 config_rlimit(ptree); /* not needed for the simulator */
4412 config_system_opts(ptree);
4413 config_logconfig(ptree);
4421 * config_remotely() - implements ntpd side of ntpq :config
4425 sockaddr_u * remote_addr
4430 snprintf(origin, sizeof(origin), "remote config from %s",
4432 lex_init_stack(origin, NULL); /* no checking needed... */
4433 init_syntax_tree(&cfgt);
4437 cfgt.source.attr = CONF_SOURCE_NTPQ;
4438 cfgt.timestamp = time(NULL);
4439 cfgt.source.value.s = estrdup(stoa(remote_addr));
4441 DPRINTF(1, ("Finished Parsing!!\n"));
4443 save_and_apply_config_tree(FALSE);
4448 * getconfig() - process startup configuration file e.g /etc/ntp.conf
4459 atexit(free_all_config_trees);
4462 config_file = CONFIG_FILE;
4465 if (!ExpandEnvironmentStringsA(temp, config_file_storage,
4466 sizeof(config_file_storage))) {
4467 msyslog(LOG_ERR, "ExpandEnvironmentStrings CONFIG_FILE failed: %m");
4470 config_file = config_file_storage;
4472 temp = ALT_CONFIG_FILE;
4473 if (!ExpandEnvironmentStringsA(temp, alt_config_file_storage,
4474 sizeof(alt_config_file_storage))) {
4475 msyslog(LOG_ERR, "ExpandEnvironmentStrings ALT_CONFIG_FILE failed: %m");
4478 alt_config_file = alt_config_file_storage;
4479 #endif /* SYS_WINNT */
4482 * install a non default variable with this daemon version
4484 snprintf(line, sizeof(line), "daemon_version=\"%s\"", Version);
4485 set_sys_var(line, strlen(line) + 1, RO);
4488 * Set up for the first time step to install a variable showing
4489 * which syscall is being used to step.
4491 set_tod_using = &ntpd_set_tod_using;
4493 getCmdOpts(argc, argv);
4494 init_syntax_tree(&cfgt);
4496 !lex_init_stack(FindConfig(config_file), "r")
4498 /* If there is no config_file, try NetInfo. */
4499 && check_netinfo && !(config_netinfo = get_netinfo_config())
4500 #endif /* HAVE_NETINFO */
4502 msyslog(LOG_INFO, "getconfig: Couldn't open <%s>: %m", FindConfig(config_file));
4508 /* Under WinNT try alternate_config_file name, first NTP.CONF, then NTP.INI */
4510 if (!lex_init_stack(FindConfig(alt_config_file), "r")) {
4512 * Broadcast clients can sometimes run without
4513 * a configuration file.
4515 msyslog(LOG_INFO, "getconfig: Couldn't open <%s>: %m", FindConfig(alt_config_file));
4520 cfgt.source.value.s = estrdup(alt_config_file);
4521 #endif /* SYS_WINNT */
4523 cfgt.source.value.s = estrdup(config_file);
4526 /*** BULK OF THE PARSER ***/
4528 yydebug = !!(debug >= 5);
4533 DPRINTF(1, ("Finished Parsing!!\n"));
4535 cfgt.source.attr = CONF_SOURCE_FILE;
4536 cfgt.timestamp = time(NULL);
4538 save_and_apply_config_tree(TRUE);
4542 free_netinfo_config(config_netinfo);
4543 #endif /* HAVE_NETINFO */
4548 save_and_apply_config_tree(int/*BOOL*/ input_from_file)
4552 config_tree *punlinked;
4556 * Keep all the configuration trees applied since startup in
4557 * a list that can be used to dump the configuration back to
4560 ptree = emalloc(sizeof(*ptree));
4561 memcpy(ptree, &cfgt, sizeof(*ptree));
4564 LINK_TAIL_SLIST(cfg_tree_history, ptree, link, config_tree);
4567 if (HAVE_OPT( SAVECONFIGQUIT )) {
4572 dumpfile = fopen(OPT_ARG( SAVECONFIGQUIT ), "w");
4573 if (NULL == dumpfile) {
4576 "can not create save file %s, error %d %m\n",
4577 OPT_ARG(SAVECONFIGQUIT), err);
4581 dumpfailed = dump_all_config_trees(dumpfile, 0);
4584 "--saveconfigquit %s error %d\n",
4585 OPT_ARG( SAVECONFIGQUIT ),
4589 "configuration saved to %s\n",
4590 OPT_ARG( SAVECONFIGQUIT ));
4594 #endif /* SAVECONFIG */
4596 /* The actual configuration done depends on whether we are configuring the
4597 * simulator or the daemon. Perform a check and call the appropriate
4598 * function as needed.
4602 config_ntpd(ptree, input_from_file);
4604 config_ntpdsim(ptree);
4608 * With configure --disable-saveconfig, there's no use keeping
4609 * the config tree around after application, so free it.
4612 UNLINK_SLIST(punlinked, cfg_tree_history, ptree, link,
4614 INSIST(punlinked == ptree);
4615 free_config_tree(ptree);
4627 snprintf(line, sizeof(line), "settimeofday=\"%s\"", which);
4628 set_sys_var(line, strlen(line) + 1, RO);
4642 snprintf(buf, LIB_BUFLENGTH, "%g", d);
4644 /* use lowercase 'e', strip any leading zeroes in exponent */
4645 pch_e = strchr(buf, 'e');
4646 if (NULL == pch_e) {
4647 pch_e = strchr(buf, 'E');
4656 while ('0' == *pch_nz)
4658 if (pch_nz == pch_e)
4660 strlcpy(pch_e, pch_nz, LIB_BUFLENGTH - (pch_e - buf));
4666 /* FUNCTIONS COPIED FROM THE OLDER ntp_config.c
4667 * --------------------------------------------
4672 * get_pfxmatch - find value for prefixmatch
4673 * and update char * accordingly
4681 while (m->name != NULL) {
4682 if (strncmp(*pstr, m->name, strlen(m->name)) == 0) {
4683 *pstr += strlen(m->name);
4693 * get_match - find logmask value
4701 while (m->name != NULL) {
4702 if (strcmp(str, m->name) == 0)
4711 * get_logmask - build bitmask for ntp_syslogmask
4722 mask = get_match(str, logcfg_noclass_items);
4727 offset = get_pfxmatch(&t, logcfg_class);
4728 mask = get_match(t, logcfg_class_items);
4731 return mask << offset;
4733 msyslog(LOG_ERR, "logconfig: '%s' not recognized - ignored",
4743 * get_netinfo_config - find the nearest NetInfo domain with an ntp
4744 * configuration and initialize the configuration state.
4746 static struct netinfo_config_state *
4747 get_netinfo_config(void)
4752 struct netinfo_config_state *config;
4754 if (ni_open(NULL, ".", &domain) != NI_OK) return NULL;
4756 while ((status = ni_pathsearch(domain, &config_dir, NETINFO_CONFIG_DIR)) == NI_NODIR) {
4758 if (ni_open(domain, "..", &next_domain) != NI_OK) {
4759 ni_free(next_domain);
4763 domain = next_domain;
4765 if (status != NI_OK) {
4770 config = emalloc(sizeof(*config));
4771 config->domain = domain;
4772 config->config_dir = config_dir;
4773 config->prop_index = 0;
4774 config->val_index = 0;
4775 config->val_list = NULL;
4782 * free_netinfo_config - release NetInfo configuration state
4785 free_netinfo_config(
4786 struct netinfo_config_state *config
4789 ni_free(config->domain);
4795 * gettokens_netinfo - return tokens from NetInfo
4799 struct netinfo_config_state *config,
4804 int prop_index = config->prop_index;
4805 int val_index = config->val_index;
4806 char **val_list = config->val_list;
4809 * Iterate through each keyword and look for a property that matches it.
4813 for (; prop_index < COUNTOF(keywords); prop_index++)
4815 ni_namelist namelist;
4816 struct keyword current_prop = keywords[prop_index];
4820 * For each value associated in the property, we're going to return
4821 * a separate line. We squirrel away the values in the config state
4822 * so the next time through, we don't need to do this lookup.
4825 if (NI_OK == ni_lookupprop(config->domain,
4826 &config->config_dir, current_prop.text,
4829 /* Found the property, but it has no values */
4830 if (namelist.ni_namelist_len == 0) continue;
4834 (namelist.ni_namelist_len + 1),
4836 val_list = config->val_list;
4839 index < namelist.ni_namelist_len;
4843 value = namelist.ni_namelist_val[index];
4844 val_list[index] = estrdup(value);
4846 val_list[index] = NULL;
4850 ni_namelist_free(&namelist);
4852 config->prop_index = prop_index;
4855 /* No list; we're done here. */
4857 return CONFIG_UNKNOWN;
4860 * We have a list of values for the current property.
4861 * Iterate through them and return each in order.
4863 if (val_list[val_index]) {
4866 char *tokens = val_list[val_index];
4868 msyslog(LOG_INFO, "%s %s", keywords[prop_index].text, val_list[val_index]);
4870 (const char*)tokenlist[0] = keywords[prop_index].text;
4871 for (ntok = 1; ntok < MAXTOKENS; ntok++) {
4872 tokenlist[ntok] = tokens;
4873 while (!ISEOL(*tokens) && (!ISSPACE(*tokens) || quoted))
4874 quoted ^= (*tokens++ == '"');
4876 if (ISEOL(*tokens)) {
4879 } else { /* must be space */
4881 while (ISSPACE(*tokens))
4888 if (ntok == MAXTOKENS) {
4889 /* HMS: chomp it to lose the EOL? */
4891 "gettokens_netinfo: too many tokens. Ignoring: %s",
4894 *ntokens = ntok + 1;
4897 config->val_index++; /* HMS: Should this be in the 'else'? */
4899 return keywords[prop_index].keytype;
4902 /* We're done with the current property. */
4903 prop_index = ++config->prop_index;
4905 /* Free val_list and reset counters. */
4906 for (val_index = 0; val_list[val_index]; val_index++)
4907 free(val_list[val_index]);
4909 val_list = config->val_list = NULL;
4910 val_index = config->val_index = 0;
4914 #endif /* HAVE_NETINFO */
4918 * getnetnum - return a net number (this is crude, but careful)
4920 * returns 1 for success, and mysteriously, 0 for most failures, and
4921 * -1 if the address found is IPv6 and we believe IPv6 isn't working.
4929 enum gnn_type a_type /* ignored */
4932 REQUIRE(AF_UNSPEC == AF(addr) ||
4933 AF_INET == AF(addr) ||
4934 AF_INET6 == AF(addr));
4936 if (!is_ip_address(num, AF(addr), addr))
4939 if (IS_IPV6(addr) && !ipv6_works)
4942 # ifdef ISC_PLATFORM_HAVESALEN
4943 addr->sa.sa_len = SIZEOF_SOCKADDR(AF(addr));
4945 SET_PORT(addr, NTP_PORT);
4947 DPRINTF(2, ("getnetnum given %s, got %s\n", num, stoa(addr)));
4953 #if defined(HAVE_SETRLIMIT)
4959 const char * rl_sstr
4965 # ifdef RLIMIT_MEMLOCK
4966 case RLIMIT_MEMLOCK:
4968 * The default RLIMIT_MEMLOCK is very low on Linux systems.
4969 * Unless we increase this limit malloc calls are likely to
4970 * fail if we drop root privilege. To be useful the value
4971 * has to be larger than the largest ntpd resident set size.
4973 DPRINTF(2, ("ntp_rlimit: MEMLOCK: %d %s\n",
4974 (int)(rl_value / rl_scale), rl_sstr));
4975 rl.rlim_cur = rl.rlim_max = rl_value;
4976 if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1)
4977 msyslog(LOG_ERR, "Cannot set RLIMIT_MEMLOCK: %m");
4979 # endif /* RLIMIT_MEMLOCK */
4981 # ifdef RLIMIT_NOFILE
4984 * For large systems the default file descriptor limit may
4987 DPRINTF(2, ("ntp_rlimit: NOFILE: %d %s\n",
4988 (int)(rl_value / rl_scale), rl_sstr));
4989 rl.rlim_cur = rl.rlim_max = rl_value;
4990 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
4991 msyslog(LOG_ERR, "Cannot set RLIMIT_NOFILE: %m");
4993 # endif /* RLIMIT_NOFILE */
4995 # ifdef RLIMIT_STACK
4998 * Provide a way to set the stack limit to something
4999 * smaller, so that we don't lock a lot of unused
5002 DPRINTF(2, ("ntp_rlimit: STACK: %d %s pages\n",
5003 (int)(rl_value / rl_scale), rl_sstr));
5004 if (-1 == getrlimit(RLIMIT_STACK, &rl)) {
5005 msyslog(LOG_ERR, "getrlimit(RLIMIT_STACK) failed: %m");
5007 if (rl_value > rl.rlim_max) {
5008 msyslog(LOG_WARNING,
5009 "ntp_rlimit: using maximum allowed stack limit %lu instead of %lu.",
5010 (u_long)rl.rlim_max,
5012 rl_value = rl.rlim_max;
5014 rl.rlim_cur = rl_value;
5015 if (-1 == setrlimit(RLIMIT_STACK, &rl)) {
5017 "ntp_rlimit: Cannot set RLIMIT_STACK: %m");
5021 # endif /* RLIMIT_STACK */
5024 INSIST(!"Unexpected setrlimit() case!");
5028 #endif /* HAVE_SETRLIMIT */