4 * What follows is a simplified version of the config parsing code
5 * in ntpd/ntp_config.c. We only parse a subset of the configuration
6 * syntax, and don't bother whining about things we don't understand.
17 #include "ntp_unixtime.h"
18 #include "ntp_filegen.h"
20 #include "ntp_syslog.h"
21 #include "ntp_stdlib.h"
28 * These routines are used to read the configuration file at
29 * startup time. An entry in the file must fit on a single line.
30 * Entries are processed as multiple tokens separated by white space
31 * Lines are considered terminated when a '#' is encountered. Blank
36 * Configuration file name
40 # define CONFIG_FILE "/etc/ntp.conf"
41 # else /* SYS_WINNT */
42 # define CONFIG_FILE "%windir%\\ntp.conf"
43 # define ALT_CONFIG_FILE "%windir%\\ntp.ini"
44 # endif /* SYS_WINNT */
45 #endif /* not CONFIG_FILE */
49 * We understand the following configuration entries and defaults.
51 * peer [ addr ] [ version 3 ] [ key 0 ] [ minpoll 6 ] [ maxpoll 10 ]
52 * server [ addr ] [ version 3 ] [ key 0 ] [ minpoll 6 ] [ maxpoll 10 ]
56 #define CONFIG_UNKNOWN 0
59 #define CONFIG_SERVER 2
62 #define CONF_MOD_VERSION 1
63 #define CONF_MOD_KEY 2
64 #define CONF_MOD_MINPOLL 3
65 #define CONF_MOD_MAXPOLL 4
66 #define CONF_MOD_PREFER 5
67 #define CONF_MOD_BURST 6
68 #define CONF_MOD_SKEY 7
69 #define CONF_MOD_TTL 8
70 #define CONF_MOD_MODE 9
73 * Translation table - keywords to function index
83 static struct keyword keywords[] = {
84 { "peer", CONFIG_PEER },
85 { "server", CONFIG_SERVER },
86 { "keys", CONFIG_KEYS },
87 { "", CONFIG_UNKNOWN }
91 * "peer", "server", "broadcast" modifier keywords
93 static struct keyword mod_keywords[] = {
94 { "version", CONF_MOD_VERSION },
95 { "key", CONF_MOD_KEY },
96 { "minpoll", CONF_MOD_MINPOLL },
97 { "maxpoll", CONF_MOD_MAXPOLL },
98 { "prefer", CONF_MOD_PREFER },
99 { "burst", CONF_MOD_BURST },
100 { "autokey", CONF_MOD_SKEY },
101 { "mode", CONF_MOD_MODE }, /* reference clocks */
102 { "ttl", CONF_MOD_TTL }, /* NTP peers */
103 { "", CONFIG_UNKNOWN }
109 #define MAXTOKENS 20 /* 20 tokens on line */
110 #define MAXLINE 1024 /* maximum length of line */
111 #define MAXFILENAME 128 /* maximum length of a file name (alloca()?) */
114 * Miscellaneous macros
116 #define STRSAME(s1, s2) (*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
117 #define ISEOL(c) ((c) == '#' || (c) == '\n' || (c) == '\0')
118 #define ISSPACE(c) ((c) == ' ' || (c) == '\t')
119 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
122 * Systemwide parameters and flags
124 extern struct server **sys_servers; /* the server list */
125 extern int sys_numservers; /* number of servers to poll */
126 extern char *key_file;
129 * Function prototypes
131 static int gettokens P((FILE *, char *, char **, int *));
132 static int matchkey P((char *, struct keyword *));
133 static int getnetnum P((const char *num, struct sockaddr_in *addr,
138 * loadservers - load list of NTP servers from configuration file
152 /* u_long peerkey; */
154 struct sockaddr_in peeraddr;
157 char *(tokens[MAXTOKENS]);
160 const char *config_file;
162 char *alt_config_file;
164 char config_file_storage[MAX_PATH];
165 char alt_config_file_storage[MAX_PATH];
166 #endif /* SYS_WINNT */
167 struct server *server, *srvlist;
170 * Initialize, initialize
179 config_file = cfgpath ? cfgpath : CONFIG_FILE;
182 config_file = cfgpath;
185 if (!ExpandEnvironmentStrings((LPCTSTR)temp, (LPTSTR)config_file_storage, (DWORD)sizeof(config_file_storage))) {
186 msyslog(LOG_ERR, "ExpandEnvironmentStrings CONFIG_FILE failed: %m\n");
189 config_file = config_file_storage;
192 temp = ALT_CONFIG_FILE;
193 if (!ExpandEnvironmentStrings((LPCTSTR)temp, (LPTSTR)alt_config_file_storage, (DWORD)sizeof(alt_config_file_storage))) {
194 msyslog(LOG_ERR, "ExpandEnvironmentStrings ALT_CONFIG_FILE failed: %m\n");
197 alt_config_file = alt_config_file_storage;
199 #endif /* SYS_WINNT */
201 if ((fp = fopen(FindConfig(config_file), "r")) == NULL)
203 fprintf(stderr, "getconfig: Couldn't open <%s>\n", FindConfig(config_file));
204 msyslog(LOG_INFO, "getconfig: Couldn't open <%s>", FindConfig(config_file));
206 /* Under WinNT try alternate_config_file name, first NTP.CONF, then NTP.INI */
208 if ((fp = fopen(FindConfig(alt_config_file), "r")) == NULL) {
211 * Broadcast clients can sometimes run without
212 * a configuration file.
215 fprintf(stderr, "getconfig: Couldn't open <%s>\n", FindConfig(alt_config_file));
216 msyslog(LOG_INFO, "getconfig: Couldn't open <%s>", FindConfig(alt_config_file));
219 #else /* not SYS_WINNT */
221 #endif /* not SYS_WINNT */
224 while ((tok = gettokens(fp, line, tokens, &ntokens))
232 "No address for %s, line ignored",
237 if (!getnetnum(tokens[1], &peeraddr, 1)) {
238 /* Resolve now, or lose! */
243 /* Shouldn't be able to specify multicast */
244 if (IN_CLASSD(ntohl(peeraddr.sin_addr.s_addr))
245 || ISBADADR(&peeraddr)) {
247 "attempt to configure invalid address %s",
253 peerversion = NTP_VERSION;
254 minpoll = NTP_MINDPOLL;
255 maxpoll = NTP_MAXDPOLL;
259 for (i = 2; i < ntokens; i++)
260 switch (matchkey(tokens[i], mod_keywords)) {
261 case CONF_MOD_VERSION:
262 if (i >= ntokens-1) {
264 "peer/server version requires an argument");
268 peerversion = atoi(tokens[++i]);
269 if ((u_char)peerversion > NTP_VERSION
270 || (u_char)peerversion < NTP_OLDVERSION) {
272 "inappropriate version number %s, line ignored",
279 if (i >= ntokens-1) {
281 "key: argument required");
286 /* peerkey = (int)atol(tokens[i]); */
287 peerflags |= FLAG_AUTHENABLE;
290 case CONF_MOD_MINPOLL:
291 if (i >= ntokens-1) {
293 "minpoll: argument required");
297 minpoll = atoi(tokens[++i]);
298 if (minpoll < NTP_MINPOLL)
299 minpoll = NTP_MINPOLL;
302 case CONF_MOD_MAXPOLL:
303 if (i >= ntokens-1) {
305 "maxpoll: argument required"
310 maxpoll = atoi(tokens[++i]);
311 if (maxpoll > NTP_MAXPOLL)
312 maxpoll = NTP_MAXPOLL;
315 case CONF_MOD_PREFER:
316 peerflags |= FLAG_PREFER;
320 peerflags |= FLAG_BURST;
324 peerflags |= FLAG_SKEY | FLAG_AUTHENABLE;
328 if (i >= ntokens-1) {
330 "ttl: argument required");
335 /* ttl = atoi(tokens[i]); */
339 if (i >= ntokens-1) {
341 "mode: argument required");
346 /* ttl = atoi(tokens[i]); */
353 if (minpoll > maxpoll) {
354 msyslog(LOG_ERR, "config error: minpoll > maxpoll");
358 server = (struct server *)emalloc(sizeof(struct server));
359 memset((char *)server, 0, sizeof(struct server));
360 server->srcadr = peeraddr;
361 server->version = peerversion;
362 server->dispersion = PEER_MAXDISP;
363 server->next_server = srvlist;
371 key_file = (char *) emalloc(strlen(tokens[1]) + 1);
372 strcpy(key_file, tokens[1]);
379 /* build final list */
380 sys_numservers = srvcnt;
381 sys_servers = (struct server **)
382 emalloc(sys_numservers * sizeof(struct server *));
383 for(i=0;i<sys_numservers;i++) {
384 sys_servers[i] = srvlist;
385 srvlist = srvlist->next_server;
392 * gettokens - read a line and return tokens
405 register int quoted = 0;
408 * Find start of first token
411 while ((cp = fgets(line, MAXLINE, fp)) != NULL) {
420 return CONFIG_UNKNOWN; /* hack. Is recognized as EOF */
424 * Now separate out the tokens
429 tokenlist[ntok++] = cp;
430 while (!ISEOL(*cp) && (!ISSPACE(*cp) || quoted))
431 quoted ^= (*cp++ == '"');
436 } else { /* must be space */
443 if (ntok == MAXTOKENS)
451 ntok = matchkey(tokenlist[0], keywords);
452 if (ntok == CONFIG_UNKNOWN)
460 * matchkey - match a keyword to a list
465 register struct keyword *keys
469 if (keys->keytype == CONFIG_UNKNOWN) {
470 return CONFIG_UNKNOWN;
472 if (STRSAME(word, keys->text))
473 return keys->keytype;
480 * getnetnum - return a net number (this is crude, but careful)
485 struct sockaddr_in *addr,
489 register const char *cp;
493 char buf[80]; /* will core dump on really stupid stuff */
496 /* XXX ELIMINATE replace with decodenetnum */
499 for (i = 0; i < 4; i++) {
501 while (isdigit((int)*cp))
509 } else if (*cp != '\0')
520 printf("getnetnum %s step %d buf %s temp %d netnum %lu\n",
521 num, i, buf, temp, (u_long)netnum);
528 "getnetnum: \"%s\" invalid host number, line ignored",
533 "getnetnum: \"%s\" invalid host number, line ignored\n",
540 * make up socket address. Clear it out for neatness.
542 memset((void *)addr, 0, sizeof(struct sockaddr_in));
543 addr->sin_family = AF_INET;
544 addr->sin_port = htons(NTP_PORT);
545 addr->sin_addr.s_addr = htonl(netnum);
548 printf("getnetnum given %s, got %s (%lx)\n",
549 num, ntoa(addr), (u_long)netnum);