3 * This file contains the ntpd configuration code.
5 * Written By: Sachin Kamboj
6 * University of Delaware
8 * Some parts borrowed from the older ntp_config.c
17 # include <netinfo/ni.h>
22 #ifdef HAVE_SYS_PARAM_H
23 # include <sys/param.h>
27 # define SIGCHLD SIGCLD
29 #ifdef HAVE_SYS_WAIT_H
30 # include <sys/wait.h>
34 #include <isc/result.h>
39 #include "ntp_unixtime.h"
40 #include "ntp_refclock.h"
41 #include "ntp_filegen.h"
42 #include "ntp_stdlib.h"
43 #include "lib_strbuf.h"
44 #include "ntp_assert.h"
45 #include "ntp_random.h"
47 * [Bug 467]: Some linux headers collide with CONFIG_PHONE and CONFIG_KEYS
48 * so #include these later.
50 #include "ntp_config.h"
51 #include "ntp_cmdargs.h"
52 #include "ntp_scanner.h"
53 #include "ntp_parser.h"
54 #include "ntpd-opts.h"
56 #ifndef IGNORE_DNS_ERRORS
59 # define DNSFLAGS GAIR_F_IGNDNSERR
62 extern int yyparse(void);
65 #if defined(HAVE_SYS_MMAN_H)
66 # include <sys/mman.h>
69 /* list of servers from command line for config_peers() */
70 int cmdline_server_count;
71 char ** cmdline_servers;
73 /* Current state of memory locking:
75 * 0: memory locking disabled
76 * 1: Memory locking enabled
81 * "logconfig" building blocks
84 const char * const name;
88 static struct masks logcfg_class[] = {
89 { "clock", NLOG_OCLOCK },
90 { "peer", NLOG_OPEER },
91 { "sync", NLOG_OSYNC },
96 /* logcfg_noclass_items[] masks are complete and must not be shifted */
97 static struct masks logcfg_noclass_items[] = {
98 { "allall", NLOG_SYSMASK | NLOG_PEERMASK | NLOG_CLOCKMASK | NLOG_SYNCMASK },
99 { "allinfo", NLOG_SYSINFO | NLOG_PEERINFO | NLOG_CLOCKINFO | NLOG_SYNCINFO },
100 { "allevents", NLOG_SYSEVENT | NLOG_PEEREVENT | NLOG_CLOCKEVENT | NLOG_SYNCEVENT },
101 { "allstatus", NLOG_SYSSTATUS | NLOG_PEERSTATUS | NLOG_CLOCKSTATUS | NLOG_SYNCSTATUS },
102 { "allstatistics", NLOG_SYSSTATIST | NLOG_PEERSTATIST | NLOG_CLOCKSTATIST | NLOG_SYNCSTATIST },
103 /* the remainder are misspellings of clockall, peerall, sysall, and syncall. */
104 { "allclock", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OCLOCK },
105 { "allpeer", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OPEER },
106 { "allsys", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OSYS },
107 { "allsync", (NLOG_INFO | NLOG_STATIST | NLOG_EVENT | NLOG_STATUS) << NLOG_OSYNC },
111 /* logcfg_class_items[] masks are shiftable by NLOG_O* counts */
112 static struct masks logcfg_class_items[] = {
113 { "all", NLOG_INFO | NLOG_EVENT | NLOG_STATUS | NLOG_STATIST },
114 { "info", NLOG_INFO },
115 { "events", NLOG_EVENT },
116 { "status", NLOG_STATUS },
117 { "statistics", NLOG_STATIST },
121 typedef struct peer_resolved_ctx_tag {
123 int host_mode; /* T_* token identifier */
126 u_char hmode; /* MODE_* */
135 #define MAXPHONE 10 /* maximum number of phone strings */
136 #define MAXPPS 20 /* maximum length of PPS device string */
139 * Miscellaneous macros
141 #define ISEOL(c) ((c) == '#' || (c) == '\n' || (c) == '\0')
142 #define ISSPACE(c) ((c) == ' ' || (c) == '\t')
144 #define _UC(str) ((char *)(intptr_t)(str))
147 * Definitions of things either imported from or exported to outside
149 extern int yydebug; /* ntp_parser.c (.y) */
150 config_tree cfgt; /* Parser output stored here */
151 struct config_tree_tag *cfg_tree_history; /* History of configs */
152 char *sys_phone[MAXPHONE] = {NULL}; /* ACTS phone numbers */
153 char default_keysdir[] = NTP_KEYSDIR;
154 char *keysdir = default_keysdir; /* crypto keys directory */
155 char * saveconfigdir;
156 #if defined(HAVE_SCHED_SETSCHEDULER)
157 int config_priority_override = 0;
161 const char *config_file;
162 static char default_ntp_signd_socket[] =
163 #ifdef NTP_SIGND_PATH
168 char *ntp_signd_socket = default_ntp_signd_socket;
170 struct netinfo_config_state *config_netinfo = NULL;
171 int check_netinfo = 1;
172 #endif /* HAVE_NETINFO */
174 char *alt_config_file;
176 char config_file_storage[MAX_PATH];
177 char alt_config_file_storage[MAX_PATH];
178 #endif /* SYS_WINNT */
182 * NetInfo configuration state
184 struct netinfo_config_state {
185 void *domain; /* domain with config */
186 ni_id config_dir; /* ID config dir */
187 int prop_index; /* current property */
188 int val_index; /* current value */
189 char **val_list; /* value list */
193 struct REMOTE_CONFIG_INFO remote_config; /* Remote configuration buffer and
195 int old_config_style = 1; /* A boolean flag, which when set,
196 * indicates that the old configuration
197 * format with a newline at the end of
198 * every command is being used
200 int cryptosw; /* crypto command called */
202 extern char *stats_drift_file; /* name of the driftfile */
204 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
206 * backwards compatibility flags
208 bc_entry bc_list[] = {
209 { T_Bc_bugXXXX, 1 } /* default enabled */
213 * declare an int pointer for each flag for quick testing without
214 * walking bc_list. If the pointer is consumed by libntp rather
215 * than ntpd, declare it in a libntp source file pointing to storage
216 * initialized with the appropriate value for other libntp clients, and
217 * redirect it to point into bc_list during ntpd startup.
219 int *p_bcXXXX_enabled = &bc_list[0].enabled;
222 /* FUNCTION PROTOTYPES */
224 static void init_syntax_tree(config_tree *);
225 static void apply_enable_disable(attr_val_fifo *q, int enable);
228 static void free_auth_node(config_tree *);
229 static void free_all_config_trees(void);
231 static void free_config_access(config_tree *);
232 static void free_config_auth(config_tree *);
233 static void free_config_fudge(config_tree *);
234 static void free_config_logconfig(config_tree *);
235 static void free_config_monitor(config_tree *);
236 static void free_config_nic_rules(config_tree *);
237 static void free_config_other_modes(config_tree *);
238 static void free_config_peers(config_tree *);
239 static void free_config_phone(config_tree *);
240 static void free_config_reset_counters(config_tree *);
241 static void free_config_rlimit(config_tree *);
242 static void free_config_setvar(config_tree *);
243 static void free_config_system_opts(config_tree *);
244 static void free_config_tinker(config_tree *);
245 static void free_config_tos(config_tree *);
246 static void free_config_trap(config_tree *);
247 static void free_config_ttl(config_tree *);
248 static void free_config_unpeers(config_tree *);
249 static void free_config_vars(config_tree *);
252 static void free_config_sim(config_tree *);
254 static void destroy_address_fifo(address_fifo *);
255 #define FREE_ADDRESS_FIFO(pf) \
257 destroy_address_fifo(pf); \
260 void free_all_config_trees(void); /* atexit() */
261 static void free_config_tree(config_tree *ptree);
262 #endif /* FREE_CFG_T */
264 static void destroy_restrict_node(restrict_node *my_node);
265 static int is_sane_resolved_address(sockaddr_u *peeraddr, int hmode);
266 static void save_and_apply_config_tree(int/*BOOL*/ from_file);
267 static void destroy_int_fifo(int_fifo *);
268 #define FREE_INT_FIFO(pf) \
270 destroy_int_fifo(pf); \
273 static void destroy_string_fifo(string_fifo *);
274 #define FREE_STRING_FIFO(pf) \
276 destroy_string_fifo(pf); \
279 static void destroy_attr_val_fifo(attr_val_fifo *);
280 #define FREE_ATTR_VAL_FIFO(pf) \
282 destroy_attr_val_fifo(pf); \
285 static void destroy_filegen_fifo(filegen_fifo *);
286 #define FREE_FILEGEN_FIFO(pf) \
288 destroy_filegen_fifo(pf); \
291 static void destroy_restrict_fifo(restrict_fifo *);
292 #define FREE_RESTRICT_FIFO(pf) \
294 destroy_restrict_fifo(pf); \
297 static void destroy_setvar_fifo(setvar_fifo *);
298 #define FREE_SETVAR_FIFO(pf) \
300 destroy_setvar_fifo(pf); \
303 static void destroy_addr_opts_fifo(addr_opts_fifo *);
304 #define FREE_ADDR_OPTS_FIFO(pf) \
306 destroy_addr_opts_fifo(pf); \
310 static void config_logconfig(config_tree *);
311 static void config_monitor(config_tree *);
312 static void config_rlimit(config_tree *);
313 static void config_system_opts(config_tree *);
314 static void config_tinker(config_tree *);
315 static void config_tos(config_tree *);
316 static void config_vars(config_tree *);
319 static sockaddr_u *get_next_address(address_node *addr);
320 static void config_sim(config_tree *);
321 static void config_ntpdsim(config_tree *);
322 #else /* !SIM follows */
323 static void config_ntpd(config_tree *, int/*BOOL*/ input_from_file);
324 static void config_other_modes(config_tree *);
325 static void config_auth(config_tree *);
326 static void config_access(config_tree *);
327 static void config_mdnstries(config_tree *);
328 static void config_phone(config_tree *);
329 static void config_setvar(config_tree *);
330 static void config_ttl(config_tree *);
331 static void config_trap(config_tree *);
332 static void config_fudge(config_tree *);
333 static void config_peers(config_tree *);
334 static void config_unpeers(config_tree *);
335 static void config_nic_rules(config_tree *, int/*BOOL*/ input_from_file);
336 static void config_reset_counters(config_tree *);
337 static u_char get_correct_host_mode(int token);
338 static int peerflag_bits(peer_node *);
342 static void peer_name_resolved(int, int, void *, const char *, const char *,
343 const struct addrinfo *,
344 const struct addrinfo *);
345 static void unpeer_name_resolved(int, int, void *, const char *, const char *,
346 const struct addrinfo *,
347 const struct addrinfo *);
348 static void trap_name_resolved(int, int, void *, const char *, const char *,
349 const struct addrinfo *,
350 const struct addrinfo *);
355 t_REF, /* Refclock */
356 t_MSK /* Network Mask */
359 static void ntpd_set_tod_using(const char *);
360 static char * normal_dtoa(double);
361 static u_int32 get_pfxmatch(const char **, struct masks *);
362 static u_int32 get_match(const char *, struct masks *);
363 static u_int32 get_logmask(const char *);
365 static int getnetnum(const char *num, sockaddr_u *addr, int complain,
366 enum gnn_type a_type);
371 /* FUNCTIONS FOR INITIALIZATION
372 * ----------------------------
381 if (ptree->auth.keys) {
382 free(ptree->auth.keys);
383 ptree->auth.keys = NULL;
386 if (ptree->auth.keysdir) {
387 free(ptree->auth.keysdir);
388 ptree->auth.keysdir = NULL;
391 if (ptree->auth.ntp_signd_socket) {
392 free(ptree->auth.ntp_signd_socket);
393 ptree->auth.ntp_signd_socket = NULL;
405 ptree->mdnstries = 5;
411 free_all_config_trees(void)
416 ptree = cfg_tree_history;
418 while (ptree != NULL) {
420 free_config_tree(ptree);
431 #if defined(_MSC_VER) && defined (_DEBUG)
435 if (ptree->source.value.s != NULL)
436 free(ptree->source.value.s);
438 free_config_other_modes(ptree);
439 free_config_auth(ptree);
440 free_config_tos(ptree);
441 free_config_monitor(ptree);
442 free_config_access(ptree);
443 free_config_tinker(ptree);
444 free_config_rlimit(ptree);
445 free_config_system_opts(ptree);
446 free_config_logconfig(ptree);
447 free_config_phone(ptree);
448 free_config_setvar(ptree);
449 free_config_ttl(ptree);
450 free_config_trap(ptree);
451 free_config_fudge(ptree);
452 free_config_vars(ptree);
453 free_config_peers(ptree);
454 free_config_unpeers(ptree);
455 free_config_nic_rules(ptree);
456 free_config_reset_counters(ptree);
458 free_config_sim(ptree);
460 free_auth_node(ptree);
464 #if defined(_MSC_VER) && defined (_DEBUG)
468 #endif /* FREE_CFG_T */
474 dump_all_config_trees(
479 config_tree * cfg_ptr;
483 for (cfg_ptr = cfg_tree_history;
485 cfg_ptr = cfg_ptr->link)
486 return_value |= dump_config_tree(cfg_ptr, df, comment);
492 /* The config dumper */
501 unpeer_node *unpeern;
504 address_node *peer_addr;
505 address_node *fudge_addr;
506 filegen_node *fgen_node;
507 restrict_node *rest_node;
508 addr_opts_node *addr_opts;
509 setvar_node *setv_node;
510 nic_rule_node *rule_node;
513 int_node *counter_set;
514 string_node *str_node;
516 const char *s = NULL;
522 DPRINTF(1, ("dump_config_tree(%p)\n", ptree));
525 if (!strftime(timestamp, sizeof(timestamp),
527 localtime(&ptree->timestamp)))
530 fprintf(df, "# %s %s %s\n",
532 (CONF_SOURCE_NTPQ == ptree->source.attr)
533 ? "ntpq remote config from"
534 : "startup configuration file",
535 ptree->source.value.s);
538 /* For options I didn't find documentation I'll just output its name and the cor. value */
539 atrv = HEAD_PFIFO(ptree->vars);
540 for ( ; atrv != NULL; atrv = atrv->link) {
541 switch (atrv->type) {
544 fprintf(df, "\n# dump error:\n"
545 "# unknown vars type %d (%s) for %s\n",
546 atrv->type, token_name(atrv->type),
547 token_name(atrv->attr));
551 fprintf(df, "%s %s\n", keyword(atrv->attr),
552 normal_dtoa(atrv->value.d));
556 fprintf(df, "%s %d\n", keyword(atrv->attr),
561 fprintf(df, "%s \"%s\"", keyword(atrv->attr),
563 if (T_Driftfile == atrv->attr &&
564 atrv->link != NULL &&
565 T_WanderThreshold == atrv->link->attr) {
568 normal_dtoa(atrv->value.d));
576 atrv = HEAD_PFIFO(ptree->logconfig);
578 fprintf(df, "logconfig");
579 for ( ; atrv != NULL; atrv = atrv->link)
580 fprintf(df, " %c%s", atrv->attr, atrv->value.s);
584 if (ptree->stats_dir)
585 fprintf(df, "statsdir \"%s\"\n", ptree->stats_dir);
587 i_n = HEAD_PFIFO(ptree->stats_list);
589 fprintf(df, "statistics");
590 for ( ; i_n != NULL; i_n = i_n->link)
591 fprintf(df, " %s", keyword(i_n->i));
595 fgen_node = HEAD_PFIFO(ptree->filegen_opts);
596 for ( ; fgen_node != NULL; fgen_node = fgen_node->link) {
597 atrv = HEAD_PFIFO(fgen_node->options);
599 fprintf(df, "filegen %s",
600 keyword(fgen_node->filegen_token));
601 for ( ; atrv != NULL; atrv = atrv->link) {
602 switch (atrv->attr) {
605 fprintf(df, "\n# dump error:\n"
606 "# unknown filegen option token %s\n"
608 token_name(atrv->attr),
609 keyword(fgen_node->filegen_token));
613 fprintf(df, " file %s",
618 fprintf(df, " type %s",
619 keyword(atrv->value.i));
624 keyword(atrv->value.i));
632 atrv = HEAD_PFIFO(ptree->auth.crypto_cmd_list);
634 fprintf(df, "crypto");
635 for ( ; atrv != NULL; atrv = atrv->link) {
636 fprintf(df, " %s %s", keyword(atrv->attr),
642 if (ptree->auth.revoke != 0)
643 fprintf(df, "revoke %d\n", ptree->auth.revoke);
645 if (ptree->auth.keysdir != NULL)
646 fprintf(df, "keysdir \"%s\"\n", ptree->auth.keysdir);
648 if (ptree->auth.keys != NULL)
649 fprintf(df, "keys \"%s\"\n", ptree->auth.keys);
651 atrv = HEAD_PFIFO(ptree->auth.trusted_key_list);
653 fprintf(df, "trustedkey");
654 for ( ; atrv != NULL; atrv = atrv->link) {
655 if (T_Integer == atrv->type)
656 fprintf(df, " %d", atrv->value.i);
657 else if (T_Intrange == atrv->type)
658 fprintf(df, " (%d ... %d)",
663 fprintf(df, "\n# dump error:\n"
664 "# unknown trustedkey attr type %d\n"
665 "trustedkey", atrv->type);
671 if (ptree->auth.control_key)
672 fprintf(df, "controlkey %d\n", ptree->auth.control_key);
674 if (ptree->auth.request_key)
675 fprintf(df, "requestkey %d\n", ptree->auth.request_key);
677 /* dump enable list, then disable list */
678 for (enable = 1; enable >= 0; enable--) {
680 ? HEAD_PFIFO(ptree->enable_opts)
681 : HEAD_PFIFO(ptree->disable_opts);
683 fprintf(df, "%s", (enable)
686 for ( ; atrv != NULL; atrv = atrv->link)
688 keyword(atrv->value.i));
693 atrv = HEAD_PFIFO(ptree->orphan_cmds);
696 for ( ; atrv != NULL; atrv = atrv->link) {
697 switch (atrv->type) {
700 fprintf(df, "\n# dump error:\n"
701 "# unknown tos attr type %d %s\n"
703 token_name(atrv->type));
707 fprintf(df, " %s %s",
709 normal_dtoa(atrv->value.d));
716 atrv = HEAD_PFIFO(ptree->rlimit);
718 fprintf(df, "rlimit");
719 for ( ; atrv != NULL; atrv = atrv->link) {
720 INSIST(T_Integer == atrv->type);
721 fprintf(df, " %s %d", keyword(atrv->attr),
727 atrv = HEAD_PFIFO(ptree->tinker);
729 fprintf(df, "tinker");
730 for ( ; atrv != NULL; atrv = atrv->link) {
731 INSIST(T_Double == atrv->type);
732 fprintf(df, " %s %s", keyword(atrv->attr),
733 normal_dtoa(atrv->value.d));
738 if (ptree->broadcastclient)
739 fprintf(df, "broadcastclient\n");
741 peern = HEAD_PFIFO(ptree->peers);
742 for ( ; peern != NULL; peern = peern->link) {
744 fprintf(df, "%s", keyword(peern->host_mode));
745 switch (addr->type) {
748 fprintf(df, "# dump error:\n"
749 "# unknown peer family %d for:\n"
751 keyword(peern->host_mode));
765 fprintf(df, " %s", addr->address);
767 if (peern->minpoll != 0)
768 fprintf(df, " minpoll %u", peern->minpoll);
770 if (peern->maxpoll != 0)
771 fprintf(df, " maxpoll %u", peern->maxpoll);
773 if (peern->ttl != 0) {
774 if (strlen(addr->address) > 8
775 && !memcmp(addr->address, "127.127.", 8))
776 fprintf(df, " mode %u", peern->ttl);
778 fprintf(df, " ttl %u", peern->ttl);
781 if (peern->peerversion != NTP_VERSION)
782 fprintf(df, " version %u", peern->peerversion);
784 if (peern->peerkey != 0)
785 fprintf(df, " key %u", peern->peerkey);
787 if (peern->group != NULL)
788 fprintf(df, " ident \"%s\"", peern->group);
790 atrv = HEAD_PFIFO(peern->peerflags);
791 for ( ; atrv != NULL; atrv = atrv->link) {
792 INSIST(T_Flag == atrv->attr);
793 INSIST(T_Integer == atrv->type);
794 fprintf(df, " %s", keyword(atrv->value.i));
799 addr_opts = HEAD_PFIFO(ptree->fudge);
800 for ( ; addr_opts != NULL; addr_opts = addr_opts->link) {
801 peer_addr = peern->addr;
802 fudge_addr = addr_opts->addr;
804 s1 = peer_addr->address;
805 s2 = fudge_addr->address;
810 fprintf(df, "fudge %s", s1);
812 for (atrv = HEAD_PFIFO(addr_opts->options);
816 switch (atrv->type) {
819 fprintf(df, "\n# dump error:\n"
820 "# unknown fudge atrv->type %d\n"
821 "fudge %s", atrv->type,
826 fprintf(df, " %s %s",
828 normal_dtoa(atrv->value.d));
832 fprintf(df, " %s %d",
838 fprintf(df, " %s %s",
848 addr = HEAD_PFIFO(ptree->manycastserver);
850 fprintf(df, "manycastserver");
851 for ( ; addr != NULL; addr = addr->link)
852 fprintf(df, " %s", addr->address);
856 addr = HEAD_PFIFO(ptree->multicastclient);
858 fprintf(df, "multicastclient");
859 for ( ; addr != NULL; addr = addr->link)
860 fprintf(df, " %s", addr->address);
865 for (unpeern = HEAD_PFIFO(ptree->unpeers);
867 unpeern = unpeern->link)
868 fprintf(df, "unpeer %s\n", unpeern->addr->address);
870 atrv = HEAD_PFIFO(ptree->mru_opts);
873 for ( ; atrv != NULL; atrv = atrv->link)
874 fprintf(df, " %s %d", keyword(atrv->attr),
879 atrv = HEAD_PFIFO(ptree->discard_opts);
881 fprintf(df, "discard");
882 for ( ; atrv != NULL; atrv = atrv->link)
883 fprintf(df, " %s %d", keyword(atrv->attr),
889 for (rest_node = HEAD_PFIFO(ptree->restrict_opts);
891 rest_node = rest_node->link) {
893 if (NULL == rest_node->addr) {
895 flags = HEAD_PFIFO(rest_node->flags);
896 for ( ; flags != NULL; flags = flags->link)
897 if (T_Source == flags->i) {
902 s = rest_node->addr->address;
904 fprintf(df, "restrict %s", s);
905 if (rest_node->mask != NULL)
906 fprintf(df, " mask %s",
907 rest_node->mask->address);
908 flags = HEAD_PFIFO(rest_node->flags);
909 for ( ; flags != NULL; flags = flags->link)
910 if (T_Source != flags->i)
911 fprintf(df, " %s", keyword(flags->i));
915 rule_node = HEAD_PFIFO(ptree->nic_rules);
916 for ( ; rule_node != NULL; rule_node = rule_node->link) {
917 fprintf(df, "interface %s %s\n",
918 keyword(rule_node->action),
919 (rule_node->match_class)
920 ? keyword(rule_node->match_class)
921 : rule_node->if_name);
924 str_node = HEAD_PFIFO(ptree->phone);
925 if (str_node != NULL) {
926 fprintf(df, "phone");
927 for ( ; str_node != NULL; str_node = str_node->link)
928 fprintf(df, " \"%s\"", str_node->s);
932 setv_node = HEAD_PFIFO(ptree->setvar);
933 for ( ; setv_node != NULL; setv_node = setv_node->link) {
934 s1 = quote_if_needed(setv_node->var);
935 s2 = quote_if_needed(setv_node->val);
936 fprintf(df, "setvar %s = %s", s1, s2);
939 if (setv_node->isdefault)
940 fprintf(df, " default");
944 i_n = HEAD_PFIFO(ptree->ttl);
947 for( ; i_n != NULL; i_n = i_n->link)
948 fprintf(df, " %d", i_n->i);
952 addr_opts = HEAD_PFIFO(ptree->trap);
953 for ( ; addr_opts != NULL; addr_opts = addr_opts->link) {
954 addr = addr_opts->addr;
955 fprintf(df, "trap %s", addr->address);
956 atrv = HEAD_PFIFO(addr_opts->options);
957 for ( ; atrv != NULL; atrv = atrv->link) {
958 switch (atrv->attr) {
961 fprintf(df, "\n# dump error:\n"
962 "# unknown trap token %d\n"
963 "trap %s", atrv->attr,
968 fprintf(df, " port %d", atrv->value.i);
972 fprintf(df, " interface %s",
980 counter_set = HEAD_PFIFO(ptree->reset_counters);
981 if (counter_set != NULL) {
982 fprintf(df, "reset");
983 for ( ; counter_set != NULL;
984 counter_set = counter_set->link)
985 fprintf(df, " %s", keyword(counter_set->i));
991 #endif /* SAVECONFIG */
995 /* generic fifo routines for structs linked by 1st member */
1008 pf = emalloc_zero(sizeof(*pf));
1010 CHECK_FIFO_CONSISTENCY(*pf);
1012 LINK_FIFO(*pf, pe, link);
1013 CHECK_FIFO_CONSISTENCY(*pf);
1035 CONCAT_FIFO(*pf1, *pf2, link);
1042 /* FUNCTIONS FOR CREATING NODES ON THE SYNTAX TREE
1043 * -----------------------------------------------
1054 my_val = emalloc_zero(sizeof(*my_val));
1055 my_val->attr = attr;
1056 my_val->value.d = value;
1057 my_val->type = T_Double;
1071 my_val = emalloc_zero(sizeof(*my_val));
1072 my_val->attr = attr;
1073 my_val->value.i = value;
1074 my_val->type = T_Integer;
1088 my_val = emalloc_zero(sizeof(*my_val));
1089 my_val->attr = attr;
1090 my_val->value.u = value;
1091 my_val->type = T_U_int;
1098 create_attr_rangeval(
1106 my_val = emalloc_zero(sizeof(*my_val));
1107 my_val->attr = attr;
1108 my_val->value.r.first = first;
1109 my_val->value.r.last = last;
1110 my_val->type = T_Intrange;
1124 my_val = emalloc_zero(sizeof(*my_val));
1125 my_val->attr = attr;
1126 if (NULL == s) /* free() hates NULL */
1128 my_val->value.s = _UC(s);
1129 my_val->type = T_String;
1142 i_n = emalloc_zero(sizeof(*i_n));
1156 sn = emalloc_zero(sizeof(*sn));
1164 create_address_node(
1169 address_node *my_node;
1171 REQUIRE(NULL != addr);
1172 REQUIRE(AF_INET == type || AF_INET6 == type || AF_UNSPEC == type);
1173 my_node = emalloc_zero(sizeof(*my_node));
1174 my_node->address = addr;
1175 my_node->type = (u_short)type;
1182 destroy_address_node(
1183 address_node *my_node
1186 if (NULL == my_node)
1188 REQUIRE(NULL != my_node->address);
1190 free(my_node->address);
1198 address_node * addr,
1199 attr_val_fifo * options
1207 my_node = emalloc_zero(sizeof(*my_node));
1209 /* Initialize node values to default */
1210 my_node->peerversion = NTP_VERSION;
1212 /* Now set the node to the read values */
1213 my_node->host_mode = hmode;
1214 my_node->addr = addr;
1217 * the options FIFO mixes items that will be saved in the
1218 * peer_node as explicit members, such as minpoll, and
1219 * those that are moved intact to the peer_node's peerflags
1220 * FIFO. The options FIFO is consumed and reclaimed here.
1223 if (options != NULL)
1224 CHECK_FIFO_CONSISTENCY(*options);
1225 while (options != NULL) {
1226 UNLINK_FIFO(option, *options, link);
1227 if (NULL == option) {
1233 /* Check the kind of option being set */
1234 switch (option->attr) {
1237 APPEND_G_FIFO(my_node->peerflags, option);
1242 if (option->value.i < NTP_MINPOLL ||
1243 option->value.i > UCHAR_MAX) {
1245 "minpoll: provided value (%d) is out of range [%d-%d])",
1246 option->value.i, NTP_MINPOLL,
1248 my_node->minpoll = NTP_MINPOLL;
1251 (u_char)option->value.u;
1256 if (option->value.i < 0 ||
1257 option->value.i > NTP_MAXPOLL) {
1259 "maxpoll: provided value (%d) is out of range [0-%d])",
1260 option->value.i, NTP_MAXPOLL);
1261 my_node->maxpoll = NTP_MAXPOLL;
1264 (u_char)option->value.u;
1269 if (option->value.u >= MAX_TTL) {
1270 msyslog(LOG_ERR, "ttl: invalid argument");
1273 my_node->ttl = (u_char)option->value.u;
1278 my_node->ttl = option->value.u;
1282 if (option->value.u >= KEYID_T_MAX) {
1283 msyslog(LOG_ERR, "key: invalid argument");
1287 (keyid_t)option->value.u;
1292 if (option->value.u >= UCHAR_MAX) {
1293 msyslog(LOG_ERR, "version: invalid argument");
1296 my_node->peerversion =
1297 (u_char)option->value.u;
1302 my_node->group = option->value.s;
1307 "Unknown peer/server option token %s",
1308 token_name(option->attr));
1315 /* Check if errors were reported. If yes, ignore the node */
1330 unpeer_node * my_node;
1334 my_node = emalloc_zero(sizeof(*my_node));
1337 * From the parser's perspective an association ID fits into
1338 * its generic T_String definition of a name/address "address".
1339 * We treat all valid 16-bit numbers as association IDs.
1341 pch = addr->address;
1342 while (*pch && isdigit((unsigned char)*pch))
1346 && 1 == sscanf(addr->address, "%u", &u)
1347 && u <= ASSOCID_MAX) {
1348 my_node->assocID = (associd_t)u;
1349 destroy_address_node(addr);
1350 my_node->addr = NULL;
1352 my_node->assocID = 0;
1353 my_node->addr = addr;
1360 create_filegen_node(
1362 attr_val_fifo * options
1365 filegen_node *my_node;
1367 my_node = emalloc_zero(sizeof(*my_node));
1368 my_node->filegen_token = filegen_token;
1369 my_node->options = options;
1376 create_restrict_node(
1377 address_node * addr,
1378 address_node * mask,
1383 restrict_node *my_node;
1385 my_node = emalloc_zero(sizeof(*my_node));
1386 my_node->addr = addr;
1387 my_node->mask = mask;
1388 my_node->flags = flags;
1389 my_node->line_no = line_no;
1396 destroy_restrict_node(
1397 restrict_node *my_node
1400 /* With great care, free all the memory occupied by
1403 destroy_address_node(my_node->addr);
1404 destroy_address_node(my_node->mask);
1405 destroy_int_fifo(my_node->flags);
1419 UNLINK_FIFO(i_n, *fifo, link);
1430 destroy_string_fifo(
1438 UNLINK_FIFO(sn, *fifo, link);
1450 destroy_attr_val_fifo(
1451 attr_val_fifo * av_fifo
1456 if (av_fifo != NULL) {
1458 UNLINK_FIFO(av, *av_fifo, link);
1461 if (T_String == av->type)
1471 destroy_filegen_fifo(
1479 UNLINK_FIFO(fg, *fifo, link);
1482 destroy_attr_val_fifo(fg->options);
1491 destroy_restrict_fifo(
1492 restrict_fifo * fifo
1499 UNLINK_FIFO(rn, *fifo, link);
1502 destroy_restrict_node(rn);
1510 destroy_setvar_fifo(
1518 UNLINK_FIFO(sv, *fifo, link);
1531 destroy_addr_opts_fifo(
1532 addr_opts_fifo * fifo
1535 addr_opts_node * aon;
1539 UNLINK_FIFO(aon, *fifo, link);
1542 destroy_address_node(aon->addr);
1543 destroy_attr_val_fifo(aon->options);
1558 setvar_node * my_node;
1561 /* do not allow = in the variable name */
1562 pch = strchr(var, '=');
1566 /* Now store the string into a setvar_node */
1567 my_node = emalloc_zero(sizeof(*my_node));
1570 my_node->isdefault = isdefault;
1577 create_nic_rule_node(
1579 char *if_name, /* interface name or numeric address */
1583 nic_rule_node *my_node;
1585 REQUIRE(match_class != 0 || if_name != NULL);
1587 my_node = emalloc_zero(sizeof(*my_node));
1588 my_node->match_class = match_class;
1589 my_node->if_name = if_name;
1590 my_node->action = action;
1597 create_addr_opts_node(
1598 address_node * addr,
1599 attr_val_fifo * options
1602 addr_opts_node *my_node;
1604 my_node = emalloc_zero(sizeof(*my_node));
1605 my_node->addr = addr;
1606 my_node->options = options;
1614 create_sim_script_info(
1616 attr_val_fifo * script_queue
1619 script_info *my_info;
1620 attr_val *my_attr_val;
1622 my_info = emalloc_zero(sizeof(*my_info));
1624 /* Initialize Script Info with default values*/
1625 my_info->duration = duration;
1626 my_info->prop_delay = NET_DLY;
1627 my_info->proc_delay = PROC_DLY;
1629 /* Traverse the script_queue and fill out non-default values */
1631 for (my_attr_val = HEAD_PFIFO(script_queue);
1632 my_attr_val != NULL;
1633 my_attr_val = my_attr_val->link) {
1635 /* Set the desired value */
1636 switch (my_attr_val->attr) {
1639 my_info->freq_offset = my_attr_val->value.d;
1643 my_info->wander = my_attr_val->value.d;
1647 my_info->jitter = my_attr_val->value.d;
1651 my_info->prop_delay = my_attr_val->value.d;
1655 my_info->proc_delay = my_attr_val->value.d;
1659 msyslog(LOG_ERR, "Unknown script token %d",
1675 const char addr_prefix[] = "192.168.0.";
1676 static int curr_addr_num = 1;
1677 #define ADDR_LENGTH 16 + 1 /* room for 192.168.1.255 */
1678 char addr_string[ADDR_LENGTH];
1679 sockaddr_u *final_addr;
1680 struct addrinfo *ptr;
1683 final_addr = emalloc(sizeof(*final_addr));
1685 if (addr->type == T_String) {
1686 snprintf(addr_string, sizeof(addr_string), "%s%d",
1687 addr_prefix, curr_addr_num++);
1688 printf("Selecting ip address %s for hostname %s\n",
1689 addr_string, addr->address);
1690 gai_err = getaddrinfo(addr_string, "ntp", NULL, &ptr);
1692 gai_err = getaddrinfo(addr->address, "ntp", NULL, &ptr);
1696 fprintf(stderr, "ERROR!! Could not get a new address\n");
1699 memcpy(final_addr, ptr->ai_addr, ptr->ai_addrlen);
1700 fprintf(stderr, "Successful in setting ip address of simulated server to: %s\n",
1712 address_node * addr,
1713 double server_offset,
1714 script_info_fifo * script
1717 server_info *my_info;
1719 my_info = emalloc_zero(sizeof(*my_info));
1720 my_info->server_time = server_offset;
1721 my_info->addr = get_next_address(addr);
1722 my_info->script = script;
1723 UNLINK_FIFO(my_info->curr_script, *my_info->script, link);
1731 attr_val_fifo * init_opts,
1732 server_info_fifo * servers
1737 my_node = emalloc(sizeof(*my_node));
1738 my_node->init_opts = init_opts;
1739 my_node->servers = servers;
1747 /* FUNCTIONS FOR PERFORMING THE CONFIGURATION
1748 * ------------------------------------------
1757 sockaddr_u addr_sock;
1758 address_node * addr_node;
1760 if (ptree->broadcastclient)
1761 proto_config(PROTO_BROADCLIENT, ptree->broadcastclient,
1764 addr_node = HEAD_PFIFO(ptree->manycastserver);
1765 while (addr_node != NULL) {
1766 ZERO_SOCK(&addr_sock);
1767 AF(&addr_sock) = addr_node->type;
1768 if (1 == getnetnum(addr_node->address, &addr_sock, 1,
1770 proto_config(PROTO_MULTICAST_ADD,
1772 sys_manycastserver = 1;
1774 addr_node = addr_node->link;
1777 /* Configure the multicast clients */
1778 addr_node = HEAD_PFIFO(ptree->multicastclient);
1779 if (addr_node != NULL) {
1781 ZERO_SOCK(&addr_sock);
1782 AF(&addr_sock) = addr_node->type;
1783 if (1 == getnetnum(addr_node->address,
1784 &addr_sock, 1, t_UNK)) {
1785 proto_config(PROTO_MULTICAST_ADD, 0, 0.,
1788 addr_node = addr_node->link;
1789 } while (addr_node != NULL);
1790 proto_config(PROTO_MULTICAST_ADD, 1, 0., NULL);
1798 destroy_address_fifo(
1799 address_fifo * pfifo
1802 address_node * addr_node;
1804 if (pfifo != NULL) {
1806 UNLINK_FIFO(addr_node, *pfifo, link);
1807 if (addr_node == NULL)
1809 destroy_address_node(addr_node);
1817 free_config_other_modes(
1821 FREE_ADDRESS_FIFO(ptree->manycastserver);
1822 FREE_ADDRESS_FIFO(ptree->multicastclient);
1824 #endif /* FREE_CFG_T */
1842 /* Crypto Command */
1845 item = -1; /* quiet warning */
1847 my_val = HEAD_PFIFO(ptree->auth.crypto_cmd_list);
1848 for (; my_val != NULL; my_val = my_val->link) {
1849 switch (my_val->attr) {
1856 item = CRYPTO_CONF_PRIV;
1860 item = CRYPTO_CONF_IDENT;
1864 item = CRYPTO_CONF_PW;
1868 item = CRYPTO_CONF_RAND;
1872 item = CRYPTO_CONF_NID;
1875 crypto_config(item, my_val->value.s);
1877 #endif /* AUTOKEY */
1879 /* Keysdir Command */
1880 if (ptree->auth.keysdir) {
1881 if (keysdir != default_keysdir)
1883 keysdir = estrdup(ptree->auth.keysdir);
1887 /* ntp_signd_socket Command */
1888 if (ptree->auth.ntp_signd_socket) {
1889 if (ntp_signd_socket != default_ntp_signd_socket)
1890 free(ntp_signd_socket);
1891 ntp_signd_socket = estrdup(ptree->auth.ntp_signd_socket);
1895 if (ptree->auth.cryptosw && !cryptosw) {
1899 #endif /* AUTOKEY */
1902 * Count the number of trusted keys to preallocate storage and
1903 * size the hash table.
1906 my_val = HEAD_PFIFO(ptree->auth.trusted_key_list);
1907 for (; my_val != NULL; my_val = my_val->link) {
1908 if (T_Integer == my_val->type) {
1909 first = my_val->value.i;
1910 if (first > 1 && first <= NTP_MAXKEY)
1913 REQUIRE(T_Intrange == my_val->type);
1914 first = my_val->value.r.first;
1915 last = my_val->value.r.last;
1916 if (!(first > last || first < 1 ||
1917 last > NTP_MAXKEY)) {
1918 count += 1 + last - first;
1922 auth_prealloc_symkeys(count);
1925 if (ptree->auth.keys)
1926 getauthkeys(ptree->auth.keys);
1928 /* Control Key Command */
1929 if (ptree->auth.control_key)
1930 ctl_auth_keyid = (keyid_t)ptree->auth.control_key;
1932 /* Requested Key Command */
1933 if (ptree->auth.request_key) {
1934 DPRINTF(4, ("set info_auth_keyid to %08lx\n",
1935 (u_long) ptree->auth.request_key));
1936 info_auth_keyid = (keyid_t)ptree->auth.request_key;
1939 /* Trusted Key Command */
1940 my_val = HEAD_PFIFO(ptree->auth.trusted_key_list);
1941 for (; my_val != NULL; my_val = my_val->link) {
1942 if (T_Integer == my_val->type) {
1943 first = my_val->value.i;
1944 if (first >= 1 && first <= NTP_MAXKEY) {
1945 authtrust(first, TRUE);
1948 "Ignoring invalid trustedkey %d, min 1 max %d.",
1952 first = my_val->value.r.first;
1953 last = my_val->value.r.last;
1954 if (first > last || first < 1 ||
1955 last > NTP_MAXKEY) {
1957 "Ignoring invalid trustedkey range %d ... %d, min 1 max %d.",
1958 first, last, NTP_MAXKEY);
1960 for (i = first; i <= last; i++) {
1968 /* crypto revoke command */
1969 if (ptree->auth.revoke)
1970 sys_revoke = 1UL << ptree->auth.revoke;
1971 #endif /* AUTOKEY */
1982 destroy_attr_val_fifo(ptree->auth.crypto_cmd_list);
1983 ptree->auth.crypto_cmd_list = NULL;
1984 destroy_attr_val_fifo(ptree->auth.trusted_key_list);
1985 ptree->auth.trusted_key_list = NULL;
1987 #endif /* FREE_CFG_T */
2000 item = -1; /* quiet warning */
2002 tos = HEAD_PFIFO(ptree->orphan_cmds);
2003 for (; tos != NULL; tos = tos->link) {
2013 msyslog(LOG_WARNING,
2014 "Using maximum bcpollbstep ceiling %d, %g requested",
2017 } else if (val < 0) {
2018 msyslog(LOG_WARNING,
2019 "Using minimum bcpollbstep floor %d, %g requested",
2023 item = PROTO_BCPOLLBSTEP;
2027 if (val > STRATUM_UNSPEC - 1) {
2028 msyslog(LOG_WARNING,
2029 "Using maximum tos ceiling %d, %g requested",
2030 STRATUM_UNSPEC - 1, val);
2031 val = STRATUM_UNSPEC - 1;
2033 item = PROTO_CEILING;
2041 item = PROTO_COHORT;
2045 item = PROTO_ORPHAN;
2049 item = PROTO_ORPHWAIT;
2053 item = PROTO_MINDISP;
2057 item = PROTO_MAXDIST;
2061 item = PROTO_MINCLOCK;
2065 item = PROTO_MAXCLOCK;
2069 item = PROTO_MINSANE;
2073 item = PROTO_BEACON;
2076 proto_config(item, 0, val, NULL);
2087 FREE_ATTR_VAL_FIFO(ptree->orphan_cmds);
2089 #endif /* FREE_CFG_T */
2097 int_node *pfilegen_token;
2098 const char *filegen_string;
2099 const char *filegen_file;
2101 filegen_node *my_node;
2106 /* Set the statistics directory */
2107 if (ptree->stats_dir)
2108 stats_config(STATS_STATSDIR, ptree->stats_dir);
2111 * Calling filegen_get is brain dead. Doing a string
2112 * comparison to find the relavant filegen structure is
2115 * Through the parser, we already know which filegen is
2116 * being specified. Hence, we should either store a
2117 * pointer to the specified structure in the syntax tree
2118 * or an index into a filegen array.
2120 * Need to change the filegen code to reflect the above.
2123 /* Turn on the specified statistics */
2124 pfilegen_token = HEAD_PFIFO(ptree->stats_list);
2125 for (; pfilegen_token != NULL; pfilegen_token = pfilegen_token->link) {
2126 filegen_string = keyword(pfilegen_token->i);
2127 filegen = filegen_get(filegen_string);
2128 if (NULL == filegen) {
2130 "stats %s unrecognized",
2134 DPRINTF(4, ("enabling filegen for %s statistics '%s%s'\n",
2135 filegen_string, filegen->dir,
2137 filegen_flag = filegen->flag;
2138 filegen_flag |= FGEN_FLAG_ENABLED;
2139 filegen_config(filegen, statsdir, filegen_string,
2140 filegen->type, filegen_flag);
2143 /* Configure the statistics with the options */
2144 my_node = HEAD_PFIFO(ptree->filegen_opts);
2145 for (; my_node != NULL; my_node = my_node->link) {
2146 filegen_string = keyword(my_node->filegen_token);
2147 filegen = filegen_get(filegen_string);
2148 if (NULL == filegen) {
2150 "filegen category '%s' unrecognized",
2154 filegen_file = filegen_string;
2156 /* Initialize the filegen variables to their pre-configuration states */
2157 filegen_flag = filegen->flag;
2158 filegen_type = filegen->type;
2160 /* "filegen ... enabled" is the default (when filegen is used) */
2161 filegen_flag |= FGEN_FLAG_ENABLED;
2163 my_opts = HEAD_PFIFO(my_node->options);
2164 for (; my_opts != NULL; my_opts = my_opts->link) {
2165 switch (my_opts->attr) {
2168 filegen_file = my_opts->value.s;
2172 switch (my_opts->value.i) {
2179 filegen_type = FILEGEN_NONE;
2183 filegen_type = FILEGEN_PID;
2187 filegen_type = FILEGEN_DAY;
2191 filegen_type = FILEGEN_WEEK;
2195 filegen_type = FILEGEN_MONTH;
2199 filegen_type = FILEGEN_YEAR;
2203 filegen_type = FILEGEN_AGE;
2209 switch (my_opts->value.i) {
2212 filegen_flag |= FGEN_FLAG_LINK;
2216 filegen_flag &= ~FGEN_FLAG_LINK;
2220 filegen_flag |= FGEN_FLAG_ENABLED;
2224 filegen_flag &= ~FGEN_FLAG_ENABLED;
2229 "Unknown filegen flag token %d",
2237 "Unknown filegen option token %d",
2242 filegen_config(filegen, statsdir, filegen_file,
2243 filegen_type, filegen_flag);
2250 free_config_monitor(
2254 if (ptree->stats_dir) {
2255 free(ptree->stats_dir);
2256 ptree->stats_dir = NULL;
2259 FREE_INT_FIFO(ptree->stats_list);
2260 FREE_FILEGEN_FIFO(ptree->filegen_opts);
2262 #endif /* FREE_CFG_T */
2271 static int warned_signd;
2273 restrict_node * my_node;
2274 int_node * curr_flag;
2277 struct addrinfo hints;
2278 struct addrinfo * ai_list;
2279 struct addrinfo * pai;
2281 int restrict_default;
2285 const char * signd_warning =
2286 #ifdef HAVE_NTP_SIGND
2287 "MS-SNTP signd operations currently block ntpd degrading service to all clients.";
2289 "mssntp restrict bit ignored, this ntpd was configured without --enable-ntp-signd.";
2292 /* Configure the mru options */
2293 my_opt = HEAD_PFIFO(ptree->mru_opts);
2294 for (; my_opt != NULL; my_opt = my_opt->link) {
2298 switch (my_opt->attr) {
2301 if (0 <= my_opt->value.i)
2302 mru_incalloc = my_opt->value.u;
2308 if (0 <= my_opt->value.i)
2309 mru_incalloc = (my_opt->value.u * 1024U)
2310 / sizeof(mon_entry);
2316 if (0 <= my_opt->value.i)
2317 mru_initalloc = my_opt->value.u;
2323 if (0 <= my_opt->value.i)
2324 mru_initalloc = (my_opt->value.u * 1024U)
2325 / sizeof(mon_entry);
2331 if (0 <= my_opt->value.i)
2332 mru_mindepth = my_opt->value.u;
2338 mru_maxage = my_opt->value.i;
2342 if (0 <= my_opt->value.i)
2343 mru_maxdepth = my_opt->value.u;
2345 mru_maxdepth = UINT_MAX;
2349 if (0 <= my_opt->value.i)
2350 mru_maxdepth = (my_opt->value.u * 1024U) /
2353 mru_maxdepth = UINT_MAX;
2358 "Unknown mru option %s (%d)",
2359 keyword(my_opt->attr), my_opt->attr);
2364 "mru %s %d out of range, ignored.",
2365 keyword(my_opt->attr), my_opt->value.i);
2368 /* Configure the discard options */
2369 my_opt = HEAD_PFIFO(ptree->discard_opts);
2370 for (; my_opt != NULL; my_opt = my_opt->link) {
2372 switch (my_opt->attr) {
2375 if (0 <= my_opt->value.i &&
2376 my_opt->value.i <= UCHAR_MAX)
2377 ntp_minpoll = (u_char)my_opt->value.u;
2380 "discard average %d out of range, ignored.",
2385 ntp_minpkt = my_opt->value.i;
2389 mon_age = my_opt->value.i;
2394 "Unknown discard option %s (%d)",
2395 keyword(my_opt->attr), my_opt->attr);
2400 /* Configure the restrict options */
2401 my_node = HEAD_PFIFO(ptree->restrict_opts);
2402 for (; my_node != NULL; my_node = my_node->link) {
2403 /* Parse the flags */
2407 curr_flag = HEAD_PFIFO(my_node->flags);
2408 for (; curr_flag != NULL; curr_flag = curr_flag->link) {
2409 switch (curr_flag->i) {
2416 mflags |= RESM_NTPONLY;
2420 mflags |= RESM_SOURCE;
2428 flags |= RES_IGNORE;
2436 flags |= RES_MSSNTP;
2440 flags |= RES_LIMITED;
2444 flags |= RES_LPTRAP;
2448 flags |= RES_NOMODIFY;
2452 flags |= RES_NOMRULIST;
2456 flags |= RES_NOPEER;
2460 flags |= RES_NOQUERY;
2464 flags |= RES_DONTSERVE;
2468 flags |= RES_NOTRAP;
2472 flags |= RES_DONTTRUST;
2476 flags |= RES_VERSION;
2481 if ((RES_MSSNTP & flags) && !warned_signd) {
2483 fprintf(stderr, "%s\n", signd_warning);
2484 msyslog(LOG_WARNING, "%s", signd_warning);
2487 /* It would be swell if we could identify the line number */
2488 if ((RES_KOD & flags) && !(RES_LIMITED & flags)) {
2489 const char *kod_where = (my_node->addr)
2490 ? my_node->addr->address
2491 : (mflags & RESM_SOURCE)
2494 const char *kod_warn = "KOD does nothing without LIMITED.";
2496 fprintf(stderr, "restrict %s: %s\n", kod_where, kod_warn);
2497 msyslog(LOG_WARNING, "restrict %s: %s", kod_where, kod_warn);
2503 restrict_default = 0;
2505 if (NULL == my_node->addr) {
2507 if (!(RESM_SOURCE & mflags)) {
2509 * The user specified a default rule
2510 * without a -4 / -6 qualifier, add to
2513 restrict_default = 1;
2515 /* apply "restrict source ..." */
2516 DPRINTF(1, ("restrict source template mflags %x flags %x\n",
2518 hack_restrict(RESTRICT_FLAGS, NULL,
2519 NULL, mflags, flags, 0);
2523 /* Resolve the specified address */
2524 AF(&addr) = (u_short)my_node->addr->type;
2526 if (getnetnum(my_node->addr->address,
2527 &addr, 1, t_UNK) != 1) {
2529 * Attempt a blocking lookup. This
2530 * is in violation of the nonblocking
2531 * design of ntpd's mainline code. The
2532 * alternative of running without the
2533 * restriction until the name resolved
2535 * Ideally some scheme could be used for
2536 * restrict directives in the startup
2537 * ntp.conf to delay starting up the
2538 * protocol machinery until after all
2539 * restrict hosts have been resolved.
2543 hints.ai_protocol = IPPROTO_UDP;
2544 hints.ai_socktype = SOCK_DGRAM;
2545 hints.ai_family = my_node->addr->type;
2546 rc = getaddrinfo(my_node->addr->address,
2551 "restrict: ignoring line %d, address/host '%s' unusable.",
2553 my_node->addr->address);
2556 INSIST(ai_list != NULL);
2558 INSIST(pai->ai_addr != NULL);
2559 INSIST(sizeof(addr) >=
2561 memcpy(&addr, pai->ai_addr,
2563 INSIST(AF_INET == AF(&addr) ||
2564 AF_INET6 == AF(&addr));
2567 SET_HOSTMASK(&mask, AF(&addr));
2569 /* Resolve the mask */
2570 if (my_node->mask) {
2572 AF(&mask) = my_node->mask->type;
2573 if (getnetnum(my_node->mask->address,
2574 &mask, 1, t_MSK) != 1) {
2576 "restrict: ignoring line %d, mask '%s' unusable.",
2578 my_node->mask->address);
2585 if (restrict_default) {
2586 AF(&addr) = AF_INET;
2587 AF(&mask) = AF_INET;
2588 hack_restrict(RESTRICT_FLAGS, &addr,
2589 &mask, mflags, flags, 0);
2590 AF(&addr) = AF_INET6;
2591 AF(&mask) = AF_INET6;
2595 hack_restrict(RESTRICT_FLAGS, &addr,
2596 &mask, mflags, flags, 0);
2598 NULL != (pai = pai->ai_next)) {
2599 INSIST(pai->ai_addr != NULL);
2600 INSIST(sizeof(addr) >=
2603 memcpy(&addr, pai->ai_addr,
2605 INSIST(AF_INET == AF(&addr) ||
2606 AF_INET6 == AF(&addr));
2607 SET_HOSTMASK(&mask, AF(&addr));
2609 } while (pai != NULL);
2611 if (ai_list != NULL)
2612 freeaddrinfo(ai_list);
2624 FREE_ATTR_VAL_FIFO(ptree->mru_opts);
2625 FREE_ATTR_VAL_FIFO(ptree->discard_opts);
2626 FREE_RESTRICT_FIFO(ptree->restrict_opts);
2628 #endif /* FREE_CFG_T */
2636 attr_val * rlimit_av;
2638 rlimit_av = HEAD_PFIFO(ptree->rlimit);
2639 for (; rlimit_av != NULL; rlimit_av = rlimit_av->link) {
2640 switch (rlimit_av->attr) {
2647 /* What if we HAVE_OPT(SAVECONFIGQUIT) ? */
2648 if (rlimit_av->value.i == -1) {
2649 # if defined(HAVE_MLOCKALL)
2650 if (cur_memlock != 0) {
2651 if (-1 == munlockall()) {
2652 msyslog(LOG_ERR, "munlockall() failed: %m");
2656 # endif /* HAVE_MLOCKALL */
2657 } else if (rlimit_av->value.i >= 0) {
2658 #if defined(RLIMIT_MEMLOCK)
2659 # if defined(HAVE_MLOCKALL)
2660 if (cur_memlock != 1) {
2661 if (-1 == mlockall(MCL_CURRENT|MCL_FUTURE)) {
2662 msyslog(LOG_ERR, "mlockall() failed: %m");
2665 # endif /* HAVE_MLOCKALL */
2666 ntp_rlimit(RLIMIT_MEMLOCK,
2667 (rlim_t)(rlimit_av->value.i * 1024 * 1024),
2672 /* STDERR as well would be fine... */
2673 msyslog(LOG_WARNING, "'rlimit memlock' specified but is not available on this system.");
2674 #endif /* RLIMIT_MEMLOCK */
2676 msyslog(LOG_WARNING, "'rlimit memlock' value of %d is unexpected!", rlimit_av->value.i);
2681 #if defined(RLIMIT_STACK)
2682 ntp_rlimit(RLIMIT_STACK,
2683 (rlim_t)(rlimit_av->value.i * 4096),
2687 /* STDERR as well would be fine... */
2688 msyslog(LOG_WARNING, "'rlimit stacksize' specified but is not available on this system.");
2689 #endif /* RLIMIT_STACK */
2693 #if defined(RLIMIT_NOFILE)
2694 ntp_rlimit(RLIMIT_NOFILE,
2695 (rlim_t)(rlimit_av->value.i),
2699 /* STDERR as well would be fine... */
2700 msyslog(LOG_WARNING, "'rlimit filenum' specified but is not available on this system.");
2701 #endif /* RLIMIT_NOFILE */
2718 item = -1; /* quiet warning */
2720 tinker = HEAD_PFIFO(ptree->tinker);
2721 for (; tinker != NULL; tinker = tinker->link) {
2722 switch (tinker->attr) {
2741 item = LOOP_HUFFPUFF;
2753 item = LOOP_MAX_BACK;
2757 item = LOOP_MAX_FWD;
2761 item = LOOP_MINSTEP;
2768 loop_config(item, tinker->value.d);
2779 FREE_ATTR_VAL_FIFO(ptree->rlimit);
2787 FREE_ATTR_VAL_FIFO(ptree->tinker);
2789 #endif /* FREE_CFG_T */
2793 * config_nic_rules - apply interface listen/ignore/drop items
2799 int/*BOOL*/ input_from_file
2802 nic_rule_node * curr_node;
2804 nic_rule_match match_type;
2805 nic_rule_action action;
2811 curr_node = HEAD_PFIFO(ptree->nic_rules);
2813 if (curr_node != NULL
2814 && (HAVE_OPT( NOVIRTUALIPS ) || HAVE_OPT( INTERFACE ))) {
2816 "interface/nic rules are not allowed with --interface (-I) or --novirtualips (-L)%s",
2817 (input_from_file) ? ", exiting" : "");
2818 if (input_from_file)
2824 for (; curr_node != NULL; curr_node = curr_node->link) {
2826 if_name = curr_node->if_name;
2827 if (if_name != NULL)
2828 if_name = estrdup(if_name);
2830 switch (curr_node->match_class) {
2835 * this assignment quiets a gcc "may be used
2836 * uninitialized" warning and is here for no
2839 match_type = MATCH_ALL;
2846 * 0 is out of range for valid token T_...
2847 * and in a nic_rules_node indicates the
2848 * interface descriptor is either a name or
2849 * address, stored in if_name in either case.
2851 INSIST(if_name != NULL);
2852 pchSlash = strchr(if_name, '/');
2853 if (pchSlash != NULL)
2855 if (is_ip_address(if_name, AF_UNSPEC, &addr)) {
2856 match_type = MATCH_IFADDR;
2857 if (pchSlash != NULL
2858 && 1 == sscanf(pchSlash + 1, "%d",
2861 SIZEOF_INADDR(AF(&addr));
2862 prefixlen = max(-1, prefixlen);
2863 prefixlen = min(prefixlen,
2867 match_type = MATCH_IFNAME;
2868 if (pchSlash != NULL)
2874 match_type = MATCH_ALL;
2878 match_type = MATCH_IPV4;
2882 match_type = MATCH_IPV6;
2886 match_type = MATCH_WILDCARD;
2890 switch (curr_node->action) {
2895 * this assignment quiets a gcc "may be used
2896 * uninitialized" warning and is here for no
2899 action = ACTION_LISTEN;
2905 action = ACTION_LISTEN;
2909 action = ACTION_IGNORE;
2913 action = ACTION_DROP;
2917 add_nic_rule(match_type, if_name, prefixlen,
2919 timer_interfacetimeout(current_time + 2);
2920 if (if_name != NULL)
2929 free_config_nic_rules(
2933 nic_rule_node *curr_node;
2935 if (ptree->nic_rules != NULL) {
2937 UNLINK_FIFO(curr_node, *ptree->nic_rules, link);
2938 if (NULL == curr_node)
2940 free(curr_node->if_name);
2943 free(ptree->nic_rules);
2944 ptree->nic_rules = NULL;
2947 #endif /* FREE_CFG_T */
2951 apply_enable_disable(
2952 attr_val_fifo * fifo,
2956 attr_val *curr_flag;
2958 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
2962 for (curr_flag = HEAD_PFIFO(fifo);
2964 curr_flag = curr_flag->link) {
2966 option = curr_flag->value.i;
2971 "can not apply enable/disable token %d, unknown",
2976 proto_config(PROTO_AUTHENTICATE, enable, 0., NULL);
2980 proto_config(PROTO_BROADCLIENT, enable, 0., NULL);
2984 proto_config(PROTO_CAL, enable, 0., NULL);
2988 proto_config(PROTO_KERNEL, enable, 0., NULL);
2992 proto_config(PROTO_MONITOR, enable, 0., NULL);
2996 proto_config(PROTO_MODE7, enable, 0., NULL);
3000 proto_config(PROTO_NTP, enable, 0., NULL);
3004 proto_config(PROTO_PCEDIGEST, enable, 0., NULL);
3008 proto_config(PROTO_FILEGEN, enable, 0., NULL);
3012 proto_config(PROTO_UECRYPTO, enable, 0., NULL);
3016 proto_config(PROTO_UECRYPTONAK, enable, 0., NULL);
3020 proto_config(PROTO_UEDIGEST, enable, 0., NULL);
3023 #ifdef BC_LIST_FRAMEWORK_NOT_YET_USED
3026 while (pentry->token) {
3027 if (pentry->token == option)
3031 if (!pentry->token) {
3033 "compat token %d not in bc_list[]",
3037 pentry->enabled = enable;
3050 apply_enable_disable(ptree->enable_opts, 1);
3051 apply_enable_disable(ptree->disable_opts, 0);
3057 free_config_system_opts(
3061 FREE_ATTR_VAL_FIFO(ptree->enable_opts);
3062 FREE_ATTR_VAL_FIFO(ptree->disable_opts);
3064 #endif /* FREE_CFG_T */
3074 my_lc = HEAD_PFIFO(ptree->logconfig);
3075 for (; my_lc != NULL; my_lc = my_lc->link) {
3076 switch (my_lc->attr) {
3079 ntp_syslogmask |= get_logmask(my_lc->value.s);
3083 ntp_syslogmask &= ~get_logmask(my_lc->value.s);
3087 ntp_syslogmask = get_logmask(my_lc->value.s);
3099 free_config_logconfig(
3103 FREE_ATTR_VAL_FIFO(ptree->logconfig);
3105 #endif /* FREE_CFG_T */
3118 sn = HEAD_PFIFO(ptree->phone);
3119 for (; sn != NULL; sn = sn->link) {
3120 /* need to leave array entry for NULL terminator */
3121 if (i < COUNTOF(sys_phone) - 1) {
3122 sys_phone[i++] = estrdup(sn->s);
3123 sys_phone[i] = NULL;
3126 "phone: Number of phone entries exceeds %zu. Ignoring phone %s...",
3127 (COUNTOF(sys_phone) - 1), sn->s);
3138 #ifdef HAVE_DNSREGISTRATION
3139 extern int mdnstries;
3140 mdnstries = ptree->mdnstries;
3141 #endif /* HAVE_DNSREGISTRATION */
3150 FREE_STRING_FIFO(ptree->phone);
3152 #endif /* FREE_CFG_T */
3161 setvar_node *my_node;
3162 size_t varlen, vallen, octets;
3166 my_node = HEAD_PFIFO(ptree->setvar);
3167 for (; my_node != NULL; my_node = my_node->link) {
3168 varlen = strlen(my_node->var);
3169 vallen = strlen(my_node->val);
3170 octets = varlen + vallen + 1 + 1;
3171 str = erealloc(str, octets);
3172 snprintf(str, octets, "%s=%s", my_node->var,
3174 set_sys_var(str, octets, (my_node->isdefault)
3190 FREE_SETVAR_FIFO(ptree->setvar);
3192 #endif /* FREE_CFG_T */
3204 curr_ttl = HEAD_PFIFO(ptree->ttl);
3205 for (; curr_ttl != NULL; curr_ttl = curr_ttl->link) {
3206 if (i < COUNTOF(sys_ttl))
3207 sys_ttl[i++] = (u_char)curr_ttl->i;
3210 "ttl: Number of TTL entries exceeds %zu. Ignoring TTL %d...",
3211 COUNTOF(sys_ttl), curr_ttl->i);
3224 FREE_INT_FIFO(ptree->ttl);
3226 #endif /* FREE_CFG_T */
3235 addr_opts_node *curr_trap;
3237 sockaddr_u addr_sock;
3238 sockaddr_u peeraddr;
3239 struct interface *localaddr;
3240 struct addrinfo hints;
3242 settrap_parms *pstp;
3247 /* silence warning about addr_sock potentially uninitialized */
3248 AF(&addr_sock) = AF_UNSPEC;
3250 curr_trap = HEAD_PFIFO(ptree->trap);
3251 for (; curr_trap != NULL; curr_trap = curr_trap->link) {
3256 curr_opt = HEAD_PFIFO(curr_trap->options);
3257 for (; curr_opt != NULL; curr_opt = curr_opt->link) {
3258 if (T_Port == curr_opt->attr) {
3259 if (curr_opt->value.i < 1
3260 || curr_opt->value.i > USHRT_MAX) {
3262 "invalid port number "
3267 port = (u_short)curr_opt->value.i;
3269 else if (T_Interface == curr_opt->attr) {
3270 /* Resolve the interface address */
3271 ZERO_SOCK(&addr_sock);
3272 if (getnetnum(curr_opt->value.s,
3273 &addr_sock, 1, t_UNK) != 1) {
3278 localaddr = findinterface(&addr_sock);
3280 if (NULL == localaddr) {
3282 "can't find interface with address %s",
3289 /* Now process the trap for the specified interface
3295 ZERO_SOCK(&peeraddr);
3296 rc = getnetnum(curr_trap->addr->address,
3297 &peeraddr, 1, t_UNK);
3301 "trap: unable to use IP address %s.",
3302 curr_trap->addr->address);
3303 #else /* WORKER follows */
3305 * save context and hand it off
3306 * for name resolution.
3309 hints.ai_protocol = IPPROTO_UDP;
3310 hints.ai_socktype = SOCK_DGRAM;
3311 snprintf(port_text, sizeof(port_text),
3313 hints.ai_flags = Z_AI_NUMERICSERV;
3314 pstp = emalloc_zero(sizeof(*pstp));
3315 if (localaddr != NULL) {
3316 hints.ai_family = localaddr->family;
3317 pstp->ifaddr_nonnull = 1;
3318 memcpy(&pstp->ifaddr,
3320 sizeof(pstp->ifaddr));
3322 rc = getaddrinfo_sometime(
3323 curr_trap->addr->address,
3326 &trap_name_resolved,
3330 "config_trap: getaddrinfo_sometime(%s,%s): %m",
3331 curr_trap->addr->address,
3336 /* port is at same location for v4 and v6 */
3337 SET_PORT(&peeraddr, port);
3339 if (NULL == localaddr)
3340 localaddr = ANY_INTERFACE_CHOOSE(&peeraddr);
3342 AF(&peeraddr) = AF(&addr_sock);
3344 if (!ctlsettrap(&peeraddr, localaddr, 0,
3347 "set trap %s -> %s failed.",
3356 * trap_name_resolved()
3358 * Callback invoked when config_trap()'s DNS lookup completes.
3367 const char * service,
3368 const struct addrinfo * hints,
3369 const struct addrinfo * res
3372 settrap_parms *pstp;
3373 struct interface *localaddr;
3374 sockaddr_u peeraddr;
3382 "giving up resolving trap host %s: %s (%d)",
3383 name, gai_strerror(rescode), rescode);
3387 INSIST(sizeof(peeraddr) >= res->ai_addrlen);
3389 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
3391 if (pstp->ifaddr_nonnull)
3392 localaddr = findinterface(&pstp->ifaddr);
3393 if (NULL == localaddr)
3394 localaddr = ANY_INTERFACE_CHOOSE(&peeraddr);
3395 if (!ctlsettrap(&peeraddr, localaddr, 0, NTP_VERSION))
3396 msyslog(LOG_ERR, "set trap %s -> %s failed.",
3397 latoa(localaddr), stoa(&peeraddr));
3400 # endif /* WORKER */
3410 FREE_ADDR_OPTS_FIFO(ptree->trap);
3412 #endif /* FREE_CFG_T */
3421 addr_opts_node *curr_fudge;
3423 sockaddr_u addr_sock;
3424 address_node *addr_node;
3425 struct refclockstat clock_stat;
3428 curr_fudge = HEAD_PFIFO(ptree->fudge);
3429 for (; curr_fudge != NULL; curr_fudge = curr_fudge->link) {
3432 /* Get the reference clock address and
3433 * ensure that it is sane
3435 addr_node = curr_fudge->addr;
3436 ZERO_SOCK(&addr_sock);
3437 if (getnetnum(addr_node->address, &addr_sock, 1, t_REF)
3441 "unrecognized fudge reference clock address %s, line ignored",
3445 if (!ISREFCLOCKADR(&addr_sock)) {
3448 "inappropriate address %s for the fudge command, line ignored",
3452 /* Parse all the options to the fudge command */
3454 curr_opt = HEAD_PFIFO(curr_fudge->options);
3455 for (; curr_opt != NULL; curr_opt = curr_opt->link) {
3456 switch (curr_opt->attr) {
3459 clock_stat.haveflags |= CLK_HAVETIME1;
3460 clock_stat.fudgetime1 = curr_opt->value.d;
3464 clock_stat.haveflags |= CLK_HAVETIME2;
3465 clock_stat.fudgetime2 = curr_opt->value.d;
3469 clock_stat.haveflags |= CLK_HAVEVAL1;
3470 clock_stat.fudgeval1 = curr_opt->value.i;
3474 clock_stat.haveflags |= CLK_HAVEVAL2;
3475 clock_stat.fudgeval2 = 0;
3476 memcpy(&clock_stat.fudgeval2,
3478 min(strlen(curr_opt->value.s), 4));
3482 clock_stat.haveflags |= CLK_HAVEFLAG1;
3483 if (curr_opt->value.i)
3484 clock_stat.flags |= CLK_FLAG1;
3486 clock_stat.flags &= ~CLK_FLAG1;
3490 clock_stat.haveflags |= CLK_HAVEFLAG2;
3491 if (curr_opt->value.i)
3492 clock_stat.flags |= CLK_FLAG2;
3494 clock_stat.flags &= ~CLK_FLAG2;
3498 clock_stat.haveflags |= CLK_HAVEFLAG3;
3499 if (curr_opt->value.i)
3500 clock_stat.flags |= CLK_FLAG3;
3502 clock_stat.flags &= ~CLK_FLAG3;
3506 clock_stat.haveflags |= CLK_HAVEFLAG4;
3507 if (curr_opt->value.i)
3508 clock_stat.flags |= CLK_FLAG4;
3510 clock_stat.flags &= ~CLK_FLAG4;
3515 "Unexpected fudge flag %s (%d) for %s",
3516 token_name(curr_opt->attr),
3517 curr_opt->attr, stoa(&addr_sock));
3518 exit(curr_opt->attr ? curr_opt->attr : 1);
3523 refclock_control(&addr_sock, &clock_stat, NULL);
3536 FREE_ADDR_OPTS_FIFO(ptree->fudge);
3538 #endif /* FREE_CFG_T */
3549 curr_var = HEAD_PFIFO(ptree->vars);
3550 for (; curr_var != NULL; curr_var = curr_var->link) {
3551 /* Determine which variable to set and set it */
3552 switch (curr_var->attr) {
3554 case T_Broadcastdelay:
3555 proto_config(PROTO_BROADDELAY, 0, curr_var->value.d, NULL);
3559 loop_config(LOOP_TICK, curr_var->value.d);
3563 if ('\0' == curr_var->value.s[0]) {
3564 stats_drift_file = 0;
3565 msyslog(LOG_INFO, "config: driftfile disabled");
3567 stats_config(STATS_FREQ_FILE, curr_var->value.s);
3571 /* DSCP is in the upper 6 bits of the IP TOS/DS field */
3572 qos = curr_var->value.i << 2;
3576 sys_ident = curr_var->value.s;
3579 case T_WanderThreshold: /* FALLTHROUGH */
3581 wander_threshold = curr_var->value.d;
3585 stats_config(STATS_LEAP_FILE, curr_var->value.s);
3589 case T_Leapsmearinterval:
3590 leap_smear_intv = curr_var->value.i;
3591 msyslog(LOG_INFO, "config: leap smear interval %i s", leap_smear_intv);
3596 stats_config(STATS_PID_FILE, curr_var->value.s);
3600 if (-1 == change_logfile(curr_var->value.s, TRUE))
3602 "Cannot open logfile %s: %m",
3606 case T_Saveconfigdir:
3607 if (saveconfigdir != NULL)
3608 free(saveconfigdir);
3609 len = strlen(curr_var->value.s);
3611 saveconfigdir = NULL;
3612 } else if (DIR_SEP != curr_var->value.s[len - 1]
3613 #ifdef SYS_WINNT /* slash is also a dir. sep. on Windows */
3614 && '/' != curr_var->value.s[len - 1]
3618 saveconfigdir = emalloc(len + 1);
3619 snprintf(saveconfigdir, len + 1,
3624 saveconfigdir = estrdup(
3631 sys_automax = curr_var->value.i;
3637 "config_vars(): unexpected token %d",
3650 FREE_ATTR_VAL_FIFO(ptree->vars);
3652 #endif /* FREE_CFG_T */
3655 /* Define a function to check if a resolved address is sane.
3656 * If yes, return 1, else return 0;
3659 is_sane_resolved_address(
3660 sockaddr_u * peeraddr,
3664 if (!ISREFCLOCKADR(peeraddr) && ISBADADR(peeraddr)) {
3666 "attempt to configure invalid address %s",
3671 * Shouldn't be able to specify multicast
3672 * address for server/peer!
3673 * and unicast address for manycastclient!
3675 if ((T_Server == hmode || T_Peer == hmode || T_Pool == hmode)
3676 && IS_MCAST(peeraddr)) {
3678 "attempt to configure invalid address %s",
3682 if (T_Manycastclient == hmode && !IS_MCAST(peeraddr)) {
3684 "attempt to configure invalid address %s",
3689 if (IS_IPV6(peeraddr) && !ipv6_works)
3692 /* Ok, all tests succeeded, now we can return 1 */
3699 get_correct_host_mode(
3707 case T_Manycastclient:
3714 return MODE_BROADCAST;
3723 * peerflag_bits() get config_peers() peerflags value from a
3724 * peer_node's queue of flag attr_val entries.
3734 /* translate peerflags options to bits */
3736 option = HEAD_PFIFO(pn->peerflags);
3737 for (; option != NULL; option = option->link) {
3738 switch (option->value.i) {
3745 peerflags |= FLAG_SKEY;
3749 peerflags |= FLAG_BURST;
3753 peerflags |= FLAG_IBURST;
3757 peerflags |= FLAG_NOSELECT;
3761 peerflags |= FLAG_PREEMPT;
3765 peerflags |= FLAG_PREFER;
3769 peerflags |= FLAG_TRUE;
3773 peerflags |= FLAG_XLEAVE;
3787 sockaddr_u peeraddr;
3788 struct addrinfo hints;
3789 peer_node * curr_peer;
3790 peer_resolved_ctx * ctx;
3793 /* add servers named on the command line with iburst implied */
3795 cmdline_server_count > 0;
3796 cmdline_server_count--, cmdline_servers++) {
3798 ZERO_SOCK(&peeraddr);
3800 * If we have a numeric address, we can safely
3801 * proceed in the mainline with it. Otherwise, hand
3802 * the hostname off to the blocking child.
3804 if (is_ip_address(*cmdline_servers, AF_UNSPEC,
3807 SET_PORT(&peeraddr, NTP_PORT);
3808 if (is_sane_resolved_address(&peeraddr,
3823 /* we have a hostname to resolve */
3825 ctx = emalloc_zero(sizeof(*ctx));
3826 ctx->family = AF_UNSPEC;
3827 ctx->host_mode = T_Server;
3828 ctx->hmode = MODE_CLIENT;
3829 ctx->version = NTP_VERSION;
3830 ctx->flags = FLAG_IBURST;
3833 hints.ai_family = (u_short)ctx->family;
3834 hints.ai_socktype = SOCK_DGRAM;
3835 hints.ai_protocol = IPPROTO_UDP;
3837 getaddrinfo_sometime_ex(*cmdline_servers,
3840 &peer_name_resolved,
3841 (void *)ctx, DNSFLAGS);
3842 # else /* !WORKER follows */
3844 "hostname %s can not be used, please use IP address instead.",
3845 curr_peer->addr->address);
3850 /* add associations from the configuration file */
3851 curr_peer = HEAD_PFIFO(ptree->peers);
3852 for (; curr_peer != NULL; curr_peer = curr_peer->link) {
3853 ZERO_SOCK(&peeraddr);
3854 /* Find the correct host-mode */
3855 hmode = get_correct_host_mode(curr_peer->host_mode);
3858 if (T_Pool == curr_peer->host_mode) {
3859 AF(&peeraddr) = curr_peer->addr->type;
3862 curr_peer->addr->address,
3865 curr_peer->peerversion,
3868 peerflag_bits(curr_peer),
3873 * If we have a numeric address, we can safely
3874 * proceed in the mainline with it. Otherwise, hand
3875 * the hostname off to the blocking child.
3877 } else if (is_ip_address(curr_peer->addr->address,
3878 curr_peer->addr->type, &peeraddr)) {
3880 SET_PORT(&peeraddr, NTP_PORT);
3881 if (is_sane_resolved_address(&peeraddr,
3882 curr_peer->host_mode))
3888 curr_peer->peerversion,
3891 peerflag_bits(curr_peer),
3896 /* we have a hostname to resolve */
3898 ctx = emalloc_zero(sizeof(*ctx));
3899 ctx->family = curr_peer->addr->type;
3900 ctx->host_mode = curr_peer->host_mode;
3902 ctx->version = curr_peer->peerversion;
3903 ctx->minpoll = curr_peer->minpoll;
3904 ctx->maxpoll = curr_peer->maxpoll;
3905 ctx->flags = peerflag_bits(curr_peer);
3906 ctx->ttl = curr_peer->ttl;
3907 ctx->keyid = curr_peer->peerkey;
3908 ctx->group = curr_peer->group;
3911 hints.ai_family = ctx->family;
3912 hints.ai_socktype = SOCK_DGRAM;
3913 hints.ai_protocol = IPPROTO_UDP;
3915 getaddrinfo_sometime_ex(curr_peer->addr->address,
3918 &peer_name_resolved, ctx,
3920 # else /* !WORKER follows */
3922 "hostname %s can not be used, please use IP address instead.",
3923 curr_peer->addr->address);
3931 * peer_name_resolved()
3933 * Callback invoked when config_peers()'s DNS lookup completes.
3942 const char * service,
3943 const struct addrinfo * hints,
3944 const struct addrinfo * res
3947 sockaddr_u peeraddr;
3948 peer_resolved_ctx * ctx;
3950 const char * fam_spec;
3957 DPRINTF(1, ("peer_name_resolved(%s) rescode %d\n", name, rescode));
3962 "giving up resolving host %s: %s (%d)",
3963 name, gai_strerror(rescode), rescode);
3967 /* Loop to configure a single association */
3968 for (; res != NULL; res = res->ai_next) {
3969 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
3970 if (is_sane_resolved_address(&peeraddr,
3972 NLOG(NLOG_SYSINFO) {
3974 fam_spec = (AF_INET6 == af)
3979 msyslog(LOG_INFO, "DNS %s %s-> %s",
4009 peer_node *curr_peer;
4011 if (ptree->peers != NULL) {
4013 UNLINK_FIFO(curr_peer, *ptree->peers, link);
4014 if (NULL == curr_peer)
4016 destroy_address_node(curr_peer->addr);
4017 destroy_attr_val_fifo(curr_peer->peerflags);
4021 ptree->peers = NULL;
4024 #endif /* FREE_CFG_T */
4033 sockaddr_u peeraddr;
4034 struct addrinfo hints;
4035 unpeer_node * curr_unpeer;
4040 curr_unpeer = HEAD_PFIFO(ptree->unpeers);
4041 for (; curr_unpeer != NULL; curr_unpeer = curr_unpeer->link) {
4043 * Either AssocID will be zero, and we unpeer by name/
4044 * address addr, or it is nonzero and addr NULL.
4046 if (curr_unpeer->assocID) {
4047 p = findpeerbyassoc(curr_unpeer->assocID);
4049 msyslog(LOG_NOTICE, "unpeered %s",
4051 peer_clear(p, "GONE");
4059 AF(&peeraddr) = curr_unpeer->addr->type;
4060 name = curr_unpeer->addr->address;
4061 rc = getnetnum(name, &peeraddr, 0, t_UNK);
4062 /* Do we have a numeric address? */
4064 DPRINTF(1, ("unpeer: searching for %s\n",
4066 p = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);
4068 msyslog(LOG_NOTICE, "unpeered %s",
4070 peer_clear(p, "GONE");
4077 * It's not a numeric IP address, it's a hostname.
4078 * Check for associations with a matching hostname.
4080 for (p = peer_list; p != NULL; p = p->p_link)
4081 if (p->hostname != NULL)
4082 if (!strcasecmp(p->hostname, name))
4085 msyslog(LOG_NOTICE, "unpeered %s", name);
4086 peer_clear(p, "GONE");
4089 /* Resolve the hostname to address(es). */
4092 hints.ai_family = curr_unpeer->addr->type;
4093 hints.ai_socktype = SOCK_DGRAM;
4094 hints.ai_protocol = IPPROTO_UDP;
4095 getaddrinfo_sometime(name, "ntp", &hints,
4097 &unpeer_name_resolved, NULL);
4098 # else /* !WORKER follows */
4100 "hostname %s can not be used, please use IP address instead.",
4109 * unpeer_name_resolved()
4111 * Callback invoked when config_unpeers()'s DNS lookup completes.
4115 unpeer_name_resolved(
4120 const char * service,
4121 const struct addrinfo * hints,
4122 const struct addrinfo * res
4125 sockaddr_u peeraddr;
4128 const char * fam_spec;
4132 DPRINTF(1, ("unpeer_name_resolved(%s) rescode %d\n", name, rescode));
4135 msyslog(LOG_ERR, "giving up resolving unpeer %s: %s (%d)",
4136 name, gai_strerror(rescode), rescode);
4140 * Loop through the addresses found
4142 for (; res != NULL; res = res->ai_next) {
4143 INSIST(res->ai_addrlen <= sizeof(peeraddr));
4144 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
4145 DPRINTF(1, ("unpeer: searching for peer %s\n",
4147 peer = findexistingpeer(&peeraddr, NULL, NULL, -1, 0);
4150 fam_spec = (AF_INET6 == af)
4155 msyslog(LOG_NOTICE, "unpeered %s %s-> %s", name,
4156 fam_spec, stoa(&peeraddr));
4157 peer_clear(peer, "GONE");
4167 free_config_unpeers(
4171 unpeer_node *curr_unpeer;
4173 if (ptree->unpeers != NULL) {
4175 UNLINK_FIFO(curr_unpeer, *ptree->unpeers, link);
4176 if (NULL == curr_unpeer)
4178 destroy_address_node(curr_unpeer->addr);
4181 free(ptree->unpeers);
4184 #endif /* FREE_CFG_T */
4189 config_reset_counters(
4193 int_node *counter_set;
4195 for (counter_set = HEAD_PFIFO(ptree->reset_counters);
4196 counter_set != NULL;
4197 counter_set = counter_set->link) {
4198 switch (counter_set->i) {
4200 DPRINTF(1, ("config_reset_counters %s (%d) invalid\n",
4201 keyword(counter_set->i), counter_set->i));
4239 free_config_reset_counters(
4243 FREE_INT_FIFO(ptree->reset_counters);
4245 #endif /* FREE_CFG_T */
4255 server_info *serv_info;
4256 attr_val *init_stmt;
4259 /* Check if a simulate block was found in the configuration code.
4260 * If not, return an error and exit
4262 sim_n = HEAD_PFIFO(ptree->sim_details);
4263 if (NULL == sim_n) {
4264 fprintf(stderr, "ERROR!! I couldn't find a \"simulate\" block for configuring the simulator.\n");
4265 fprintf(stderr, "\tCheck your configuration file.\n");
4269 /* Process the initialization statements
4270 * -------------------------------------
4272 init_stmt = HEAD_PFIFO(sim_n->init_opts);
4273 for (; init_stmt != NULL; init_stmt = init_stmt->link) {
4274 switch(init_stmt->attr) {
4277 simulation.beep_delay = init_stmt->value.d;
4280 case T_Sim_Duration:
4281 simulation.end_time = init_stmt->value.d;
4286 "Unknown simulator init token %d\n",
4292 /* Process the server list
4293 * -----------------------
4295 simulation.num_of_servers = 0;
4296 serv_info = HEAD_PFIFO(sim_n->servers);
4297 for (; serv_info != NULL; serv_info = serv_info->link)
4298 simulation.num_of_servers++;
4299 simulation.servers = eallocarray(simulation.num_of_servers,
4300 sizeof(simulation.servers[0]));
4303 serv_info = HEAD_PFIFO(sim_n->servers);
4304 for (; serv_info != NULL; serv_info = serv_info->link) {
4305 if (NULL == serv_info) {
4306 fprintf(stderr, "Simulator server list is corrupt\n");
4309 simulation.servers[i] = *serv_info;
4310 simulation.servers[i].link = NULL;
4315 printf("Creating server associations\n");
4316 create_server_associations();
4317 fprintf(stderr,"\tServer associations successfully created!!\n");
4328 server_info *serv_n;
4329 script_info *script_n;
4331 if (NULL == ptree->sim_details)
4333 sim_n = HEAD_PFIFO(ptree->sim_details);
4334 free(ptree->sim_details);
4335 ptree->sim_details = NULL;
4339 FREE_ATTR_VAL_FIFO(sim_n->init_opts);
4341 UNLINK_FIFO(serv_n, *sim_n->servers, link);
4344 free(serv_n->curr_script);
4345 if (serv_n->script != NULL) {
4347 UNLINK_FIFO(script_n, *serv_n->script,
4349 if (script_n == NULL)
4353 free(serv_n->script);
4359 #endif /* FREE_CFG_T */
4363 /* Define two different config functions. One for the daemon and the other for
4364 * the simulator. The simulator ignores a lot of the standard ntpd configuration
4371 int/*BOOL*/ input_from_files
4374 config_nic_rules(ptree, input_from_files);
4375 config_monitor(ptree);
4378 config_access(ptree);
4379 config_tinker(ptree);
4380 config_rlimit(ptree);
4381 config_system_opts(ptree);
4382 config_logconfig(ptree);
4383 config_phone(ptree);
4384 config_mdnstries(ptree);
4385 config_setvar(ptree);
4392 config_other_modes(ptree);
4393 config_peers(ptree);
4394 config_unpeers(ptree);
4395 config_fudge(ptree);
4396 config_reset_counters(ptree);
4398 #ifdef TEST_BLOCKING_WORKER
4400 struct addrinfo hints;
4403 hints.ai_socktype = SOCK_STREAM;
4404 hints.ai_protocol = IPPROTO_TCP;
4405 getaddrinfo_sometime("www.cnn.com", "ntp", &hints,
4407 gai_test_callback, (void *)1);
4408 hints.ai_family = AF_INET6;
4409 getaddrinfo_sometime("ipv6.google.com", "ntp", &hints,
4411 gai_test_callback, (void *)0x600);
4424 printf("Configuring Simulator...\n");
4425 printf("Some ntpd-specific commands in the configuration file will be ignored.\n");
4428 config_monitor(ptree);
4429 config_tinker(ptree);
4431 config_rlimit(ptree); /* not needed for the simulator */
4432 config_system_opts(ptree);
4433 config_logconfig(ptree);
4441 * config_remotely() - implements ntpd side of ntpq :config
4445 sockaddr_u * remote_addr
4450 snprintf(origin, sizeof(origin), "remote config from %s",
4452 lex_init_stack(origin, NULL); /* no checking needed... */
4453 init_syntax_tree(&cfgt);
4457 cfgt.source.attr = CONF_SOURCE_NTPQ;
4458 cfgt.timestamp = time(NULL);
4459 cfgt.source.value.s = estrdup(stoa(remote_addr));
4461 DPRINTF(1, ("Finished Parsing!!\n"));
4463 save_and_apply_config_tree(FALSE);
4468 * getconfig() - process startup configuration file e.g /etc/ntp.conf
4479 atexit(free_all_config_trees);
4482 config_file = CONFIG_FILE;
4485 if (!ExpandEnvironmentStringsA(temp, config_file_storage,
4486 sizeof(config_file_storage))) {
4487 msyslog(LOG_ERR, "ExpandEnvironmentStrings CONFIG_FILE failed: %m");
4490 config_file = config_file_storage;
4492 temp = ALT_CONFIG_FILE;
4493 if (!ExpandEnvironmentStringsA(temp, alt_config_file_storage,
4494 sizeof(alt_config_file_storage))) {
4495 msyslog(LOG_ERR, "ExpandEnvironmentStrings ALT_CONFIG_FILE failed: %m");
4498 alt_config_file = alt_config_file_storage;
4499 #endif /* SYS_WINNT */
4502 * install a non default variable with this daemon version
4504 snprintf(line, sizeof(line), "daemon_version=\"%s\"", Version);
4505 set_sys_var(line, strlen(line) + 1, RO);
4508 * Set up for the first time step to install a variable showing
4509 * which syscall is being used to step.
4511 set_tod_using = &ntpd_set_tod_using;
4513 getCmdOpts(argc, argv);
4514 init_syntax_tree(&cfgt);
4516 !lex_init_stack(FindConfig(config_file), "r")
4518 /* If there is no config_file, try NetInfo. */
4519 && check_netinfo && !(config_netinfo = get_netinfo_config())
4520 #endif /* HAVE_NETINFO */
4522 msyslog(LOG_INFO, "getconfig: Couldn't open <%s>: %m", FindConfig(config_file));
4528 /* Under WinNT try alternate_config_file name, first NTP.CONF, then NTP.INI */
4530 if (!lex_init_stack(FindConfig(alt_config_file), "r")) {
4532 * Broadcast clients can sometimes run without
4533 * a configuration file.
4535 msyslog(LOG_INFO, "getconfig: Couldn't open <%s>: %m", FindConfig(alt_config_file));
4540 cfgt.source.value.s = estrdup(alt_config_file);
4541 #endif /* SYS_WINNT */
4543 cfgt.source.value.s = estrdup(config_file);
4546 /*** BULK OF THE PARSER ***/
4548 yydebug = !!(debug >= 5);
4553 DPRINTF(1, ("Finished Parsing!!\n"));
4555 cfgt.source.attr = CONF_SOURCE_FILE;
4556 cfgt.timestamp = time(NULL);
4558 save_and_apply_config_tree(TRUE);
4562 free_netinfo_config(config_netinfo);
4563 #endif /* HAVE_NETINFO */
4568 save_and_apply_config_tree(int/*BOOL*/ input_from_file)
4572 config_tree *punlinked;
4576 * Keep all the configuration trees applied since startup in
4577 * a list that can be used to dump the configuration back to
4580 ptree = emalloc(sizeof(*ptree));
4581 memcpy(ptree, &cfgt, sizeof(*ptree));
4584 LINK_TAIL_SLIST(cfg_tree_history, ptree, link, config_tree);
4587 if (HAVE_OPT( SAVECONFIGQUIT )) {
4592 dumpfile = fopen(OPT_ARG( SAVECONFIGQUIT ), "w");
4593 if (NULL == dumpfile) {
4596 "can not create save file %s, error %d %m\n",
4597 OPT_ARG(SAVECONFIGQUIT), err);
4601 dumpfailed = dump_all_config_trees(dumpfile, 0);
4604 "--saveconfigquit %s error %d\n",
4605 OPT_ARG( SAVECONFIGQUIT ),
4609 "configuration saved to %s\n",
4610 OPT_ARG( SAVECONFIGQUIT ));
4614 #endif /* SAVECONFIG */
4616 /* The actual configuration done depends on whether we are configuring the
4617 * simulator or the daemon. Perform a check and call the appropriate
4618 * function as needed.
4622 config_ntpd(ptree, input_from_file);
4624 config_ntpdsim(ptree);
4628 * With configure --disable-saveconfig, there's no use keeping
4629 * the config tree around after application, so free it.
4632 UNLINK_SLIST(punlinked, cfg_tree_history, ptree, link,
4634 INSIST(punlinked == ptree);
4635 free_config_tree(ptree);
4647 snprintf(line, sizeof(line), "settimeofday=\"%s\"", which);
4648 set_sys_var(line, strlen(line) + 1, RO);
4662 snprintf(buf, LIB_BUFLENGTH, "%g", d);
4664 /* use lowercase 'e', strip any leading zeroes in exponent */
4665 pch_e = strchr(buf, 'e');
4666 if (NULL == pch_e) {
4667 pch_e = strchr(buf, 'E');
4676 while ('0' == *pch_nz)
4678 if (pch_nz == pch_e)
4680 strlcpy(pch_e, pch_nz, LIB_BUFLENGTH - (pch_e - buf));
4686 /* FUNCTIONS COPIED FROM THE OLDER ntp_config.c
4687 * --------------------------------------------
4692 * get_pfxmatch - find value for prefixmatch
4693 * and update char * accordingly
4701 while (m->name != NULL) {
4702 if (strncmp(*pstr, m->name, strlen(m->name)) == 0) {
4703 *pstr += strlen(m->name);
4713 * get_match - find logmask value
4721 while (m->name != NULL) {
4722 if (strcmp(str, m->name) == 0)
4731 * get_logmask - build bitmask for ntp_syslogmask
4742 mask = get_match(str, logcfg_noclass_items);
4747 offset = get_pfxmatch(&t, logcfg_class);
4748 mask = get_match(t, logcfg_class_items);
4751 return mask << offset;
4753 msyslog(LOG_ERR, "logconfig: '%s' not recognized - ignored",
4763 * get_netinfo_config - find the nearest NetInfo domain with an ntp
4764 * configuration and initialize the configuration state.
4766 static struct netinfo_config_state *
4767 get_netinfo_config(void)
4772 struct netinfo_config_state *config;
4774 if (ni_open(NULL, ".", &domain) != NI_OK) return NULL;
4776 while ((status = ni_pathsearch(domain, &config_dir, NETINFO_CONFIG_DIR)) == NI_NODIR) {
4778 if (ni_open(domain, "..", &next_domain) != NI_OK) {
4779 ni_free(next_domain);
4783 domain = next_domain;
4785 if (status != NI_OK) {
4790 config = emalloc(sizeof(*config));
4791 config->domain = domain;
4792 config->config_dir = config_dir;
4793 config->prop_index = 0;
4794 config->val_index = 0;
4795 config->val_list = NULL;
4802 * free_netinfo_config - release NetInfo configuration state
4805 free_netinfo_config(
4806 struct netinfo_config_state *config
4809 ni_free(config->domain);
4815 * gettokens_netinfo - return tokens from NetInfo
4819 struct netinfo_config_state *config,
4824 int prop_index = config->prop_index;
4825 int val_index = config->val_index;
4826 char **val_list = config->val_list;
4829 * Iterate through each keyword and look for a property that matches it.
4833 for (; prop_index < COUNTOF(keywords); prop_index++)
4835 ni_namelist namelist;
4836 struct keyword current_prop = keywords[prop_index];
4840 * For each value associated in the property, we're going to return
4841 * a separate line. We squirrel away the values in the config state
4842 * so the next time through, we don't need to do this lookup.
4845 if (NI_OK == ni_lookupprop(config->domain,
4846 &config->config_dir, current_prop.text,
4849 /* Found the property, but it has no values */
4850 if (namelist.ni_namelist_len == 0) continue;
4854 (namelist.ni_namelist_len + 1),
4856 val_list = config->val_list;
4859 index < namelist.ni_namelist_len;
4863 value = namelist.ni_namelist_val[index];
4864 val_list[index] = estrdup(value);
4866 val_list[index] = NULL;
4870 ni_namelist_free(&namelist);
4872 config->prop_index = prop_index;
4875 /* No list; we're done here. */
4877 return CONFIG_UNKNOWN;
4880 * We have a list of values for the current property.
4881 * Iterate through them and return each in order.
4883 if (val_list[val_index]) {
4886 char *tokens = val_list[val_index];
4888 msyslog(LOG_INFO, "%s %s", keywords[prop_index].text, val_list[val_index]);
4890 (const char*)tokenlist[0] = keywords[prop_index].text;
4891 for (ntok = 1; ntok < MAXTOKENS; ntok++) {
4892 tokenlist[ntok] = tokens;
4893 while (!ISEOL(*tokens) && (!ISSPACE(*tokens) || quoted))
4894 quoted ^= (*tokens++ == '"');
4896 if (ISEOL(*tokens)) {
4899 } else { /* must be space */
4901 while (ISSPACE(*tokens))
4908 if (ntok == MAXTOKENS) {
4909 /* HMS: chomp it to lose the EOL? */
4911 "gettokens_netinfo: too many tokens. Ignoring: %s",
4914 *ntokens = ntok + 1;
4917 config->val_index++; /* HMS: Should this be in the 'else'? */
4919 return keywords[prop_index].keytype;
4922 /* We're done with the current property. */
4923 prop_index = ++config->prop_index;
4925 /* Free val_list and reset counters. */
4926 for (val_index = 0; val_list[val_index]; val_index++)
4927 free(val_list[val_index]);
4929 val_list = config->val_list = NULL;
4930 val_index = config->val_index = 0;
4934 #endif /* HAVE_NETINFO */
4938 * getnetnum - return a net number (this is crude, but careful)
4940 * returns 1 for success, and mysteriously, 0 for most failures, and
4941 * -1 if the address found is IPv6 and we believe IPv6 isn't working.
4949 enum gnn_type a_type /* ignored */
4952 REQUIRE(AF_UNSPEC == AF(addr) ||
4953 AF_INET == AF(addr) ||
4954 AF_INET6 == AF(addr));
4956 if (!is_ip_address(num, AF(addr), addr))
4959 if (IS_IPV6(addr) && !ipv6_works)
4962 # ifdef ISC_PLATFORM_HAVESALEN
4963 addr->sa.sa_len = SIZEOF_SOCKADDR(AF(addr));
4965 SET_PORT(addr, NTP_PORT);
4967 DPRINTF(2, ("getnetnum given %s, got %s\n", num, stoa(addr)));
4973 #if defined(HAVE_SETRLIMIT)
4979 const char * rl_sstr
4985 # ifdef RLIMIT_MEMLOCK
4986 case RLIMIT_MEMLOCK:
4988 * The default RLIMIT_MEMLOCK is very low on Linux systems.
4989 * Unless we increase this limit malloc calls are likely to
4990 * fail if we drop root privilege. To be useful the value
4991 * has to be larger than the largest ntpd resident set size.
4993 DPRINTF(2, ("ntp_rlimit: MEMLOCK: %d %s\n",
4994 (int)(rl_value / rl_scale), rl_sstr));
4995 rl.rlim_cur = rl.rlim_max = rl_value;
4996 if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1)
4997 msyslog(LOG_ERR, "Cannot set RLIMIT_MEMLOCK: %m");
4999 # endif /* RLIMIT_MEMLOCK */
5001 # ifdef RLIMIT_NOFILE
5004 * For large systems the default file descriptor limit may
5007 DPRINTF(2, ("ntp_rlimit: NOFILE: %d %s\n",
5008 (int)(rl_value / rl_scale), rl_sstr));
5009 rl.rlim_cur = rl.rlim_max = rl_value;
5010 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
5011 msyslog(LOG_ERR, "Cannot set RLIMIT_NOFILE: %m");
5013 # endif /* RLIMIT_NOFILE */
5015 # ifdef RLIMIT_STACK
5018 * Provide a way to set the stack limit to something
5019 * smaller, so that we don't lock a lot of unused
5022 DPRINTF(2, ("ntp_rlimit: STACK: %d %s pages\n",
5023 (int)(rl_value / rl_scale), rl_sstr));
5024 if (-1 == getrlimit(RLIMIT_STACK, &rl)) {
5025 msyslog(LOG_ERR, "getrlimit(RLIMIT_STACK) failed: %m");
5027 if (rl_value > rl.rlim_max) {
5028 msyslog(LOG_WARNING,
5029 "ntp_rlimit: using maximum allowed stack limit %lu instead of %lu.",
5030 (u_long)rl.rlim_max,
5032 rl_value = rl.rlim_max;
5034 rl.rlim_cur = rl_value;
5035 if (-1 == setrlimit(RLIMIT_STACK, &rl)) {
5037 "ntp_rlimit: Cannot set RLIMIT_STACK: %m");
5041 # endif /* RLIMIT_STACK */
5044 INSIST(!"Unexpected setrlimit() case!");
5048 #endif /* HAVE_SETRLIMIT */