Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

824 lines
17 KiB

20 years ago
9 years ago
  1. /* $OpenBSD: parse.y,v 1.74 2019/11/06 13:35:25 otto Exp $ */
  2. /*
  3. * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
  4. * Copyright (c) 2001 Markus Friedl. All rights reserved.
  5. * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
  6. * Copyright (c) 2001 Theo de Raadt. All rights reserved.
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. */
  20. %{
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #include <ctype.h>
  26. #include <errno.h>
  27. #include <limits.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <syslog.h>
  33. #include "ntpd.h"
  34. TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
  35. static struct file {
  36. TAILQ_ENTRY(file) entry;
  37. FILE *stream;
  38. char *name;
  39. int lineno;
  40. int errors;
  41. } *file, *topfile;
  42. struct file *pushfile(const char *);
  43. int popfile(void);
  44. int yyparse(void);
  45. int yylex(void);
  46. int yyerror(const char *, ...)
  47. __attribute__((__format__ (printf, 1, 2)))
  48. __attribute__((__nonnull__ (1)));
  49. int kw_cmp(const void *, const void *);
  50. int lookup(char *);
  51. int lgetc(int);
  52. int lungetc(int);
  53. int findeol(void);
  54. struct ntpd_conf *conf;
  55. struct sockaddr_in query_addr4;
  56. struct sockaddr_in6 query_addr6;
  57. int poolseqnum;
  58. struct opts {
  59. int weight;
  60. int correction;
  61. int stratum;
  62. int rtable;
  63. char *refstr;
  64. } opts;
  65. void opts_default(void);
  66. typedef struct {
  67. union {
  68. int64_t number;
  69. char *string;
  70. struct ntp_addr_wrap *addr;
  71. struct opts opts;
  72. } v;
  73. int lineno;
  74. } YYSTYPE;
  75. %}
  76. %token LISTEN ON CONSTRAINT CONSTRAINTS FROM QUERY
  77. %token SERVER SERVERS SENSOR CORRECTION RTABLE REFID STRATUM WEIGHT
  78. %token ERROR
  79. %token <v.string> STRING
  80. %token <v.number> NUMBER
  81. %type <v.addr> address url urllist
  82. %type <v.opts> listen_opts listen_opts_l listen_opt
  83. %type <v.opts> server_opts server_opts_l server_opt
  84. %type <v.opts> sensor_opts sensor_opts_l sensor_opt
  85. %type <v.opts> correction
  86. %type <v.opts> rtable
  87. %type <v.opts> refid
  88. %type <v.opts> stratum
  89. %type <v.opts> weight
  90. %%
  91. grammar : /* empty */
  92. | grammar '\n'
  93. | grammar main '\n'
  94. | grammar error '\n' { file->errors++; }
  95. ;
  96. main : LISTEN ON address listen_opts {
  97. struct listen_addr *la;
  98. struct ntp_addr *h, *next;
  99. if ((h = $3->a) == NULL &&
  100. (host_dns($3->name, 0, &h) == -1 || !h)) {
  101. yyerror("could not resolve \"%s\"", $3->name);
  102. free($3->name);
  103. free($3);
  104. YYERROR;
  105. }
  106. for (; h != NULL; h = next) {
  107. next = h->next;
  108. la = calloc(1, sizeof(struct listen_addr));
  109. if (la == NULL)
  110. fatal("listen on calloc");
  111. la->fd = -1;
  112. la->rtable = $4.rtable;
  113. memcpy(&la->sa, &h->ss,
  114. sizeof(struct sockaddr_storage));
  115. TAILQ_INSERT_TAIL(&conf->listen_addrs, la,
  116. entry);
  117. free(h);
  118. }
  119. free($3->name);
  120. free($3);
  121. }
  122. | QUERY FROM STRING {
  123. struct sockaddr_in sin4;
  124. struct sockaddr_in6 sin6;
  125. memset(&sin4, 0, sizeof(sin4));
  126. sin4.sin_family = AF_INET;
  127. sin4.sin_len = sizeof(struct sockaddr_in);
  128. memset(&sin6, 0, sizeof(sin6));
  129. sin6.sin6_family = AF_INET6;
  130. sin6.sin6_len = sizeof(struct sockaddr_in6);
  131. if (inet_pton(AF_INET, $3, &sin4.sin_addr) == 1)
  132. memcpy(&query_addr4, &sin4, sin4.sin_len);
  133. else if (inet_pton(AF_INET6, $3, &sin6.sin6_addr) == 1)
  134. memcpy(&query_addr6, &sin6, sin6.sin6_len);
  135. else {
  136. yyerror("invalid IPv4 or IPv6 address: %s\n",
  137. $3);
  138. free($3);
  139. YYERROR;
  140. }
  141. free($3);
  142. }
  143. | SERVERS address server_opts {
  144. struct ntp_peer *p;
  145. struct ntp_addr *h, *next;
  146. h = $2->a;
  147. do {
  148. if (h != NULL) {
  149. next = h->next;
  150. if (h->ss.ss_family != AF_INET &&
  151. h->ss.ss_family != AF_INET6) {
  152. yyerror("IPv4 or IPv6 address "
  153. "or hostname expected");
  154. free(h);
  155. free($2->name);
  156. free($2);
  157. YYERROR;
  158. }
  159. h->next = NULL;
  160. } else
  161. next = NULL;
  162. p = new_peer();
  163. p->weight = $3.weight;
  164. p->query_addr4 = query_addr4;
  165. p->query_addr6 = query_addr6;
  166. p->addr = h;
  167. p->addr_head.a = h;
  168. p->addr_head.pool = ++poolseqnum;
  169. p->addr_head.name = strdup($2->name);
  170. if (p->addr_head.name == NULL)
  171. fatal(NULL);
  172. if (p->addr != NULL)
  173. p->state = STATE_DNS_DONE;
  174. TAILQ_INSERT_TAIL(&conf->ntp_peers, p, entry);
  175. h = next;
  176. } while (h != NULL);
  177. free($2->name);
  178. free($2);
  179. }
  180. | SERVER address server_opts {
  181. struct ntp_peer *p;
  182. struct ntp_addr *h, *next;
  183. p = new_peer();
  184. for (h = $2->a; h != NULL; h = next) {
  185. next = h->next;
  186. if (h->ss.ss_family != AF_INET &&
  187. h->ss.ss_family != AF_INET6) {
  188. yyerror("IPv4 or IPv6 address "
  189. "or hostname expected");
  190. free(h);
  191. free(p);
  192. free($2->name);
  193. free($2);
  194. YYERROR;
  195. }
  196. h->next = p->addr;
  197. p->addr = h;
  198. }
  199. p->weight = $3.weight;
  200. p->query_addr4 = query_addr4;
  201. p->query_addr6 = query_addr6;
  202. p->addr_head.a = p->addr;
  203. p->addr_head.pool = 0;
  204. p->addr_head.name = strdup($2->name);
  205. if (p->addr_head.name == NULL)
  206. fatal(NULL);
  207. if (p->addr != NULL)
  208. p->state = STATE_DNS_DONE;
  209. TAILQ_INSERT_TAIL(&conf->ntp_peers, p, entry);
  210. free($2->name);
  211. free($2);
  212. }
  213. | CONSTRAINTS FROM url {
  214. struct constraint *p;
  215. struct ntp_addr *h, *next;
  216. h = $3->a;
  217. do {
  218. if (h != NULL) {
  219. next = h->next;
  220. if (h->ss.ss_family != AF_INET &&
  221. h->ss.ss_family != AF_INET6) {
  222. yyerror("IPv4 or IPv6 address "
  223. "or hostname expected");
  224. free(h);
  225. free($3->name);
  226. free($3->path);
  227. free($3);
  228. YYERROR;
  229. }
  230. h->next = NULL;
  231. } else
  232. next = NULL;
  233. p = new_constraint();
  234. p->addr = h;
  235. p->addr_head.a = h;
  236. p->addr_head.pool = ++poolseqnum;
  237. p->addr_head.name = strdup($3->name);
  238. p->addr_head.path = strdup($3->path);
  239. if (p->addr_head.name == NULL ||
  240. p->addr_head.path == NULL)
  241. fatal(NULL);
  242. if (p->addr != NULL)
  243. p->state = STATE_DNS_DONE;
  244. constraint_add(p);
  245. h = next;
  246. } while (h != NULL);
  247. free($3->name);
  248. free($3);
  249. }
  250. | CONSTRAINT FROM urllist {
  251. struct constraint *p;
  252. struct ntp_addr *h, *next;
  253. p = new_constraint();
  254. for (h = $3->a; h != NULL; h = next) {
  255. next = h->next;
  256. if (h->ss.ss_family != AF_INET &&
  257. h->ss.ss_family != AF_INET6) {
  258. yyerror("IPv4 or IPv6 address "
  259. "or hostname expected");
  260. free(h);
  261. free(p);
  262. free($3->name);
  263. free($3->path);
  264. free($3);
  265. YYERROR;
  266. }
  267. h->next = p->addr;
  268. p->addr = h;
  269. }
  270. p->addr_head.a = p->addr;
  271. p->addr_head.pool = 0;
  272. p->addr_head.name = strdup($3->name);
  273. p->addr_head.path = strdup($3->path);
  274. if (p->addr_head.name == NULL ||
  275. p->addr_head.path == NULL)
  276. fatal(NULL);
  277. if (p->addr != NULL)
  278. p->state = STATE_DNS_DONE;
  279. constraint_add(p);
  280. free($3->name);
  281. free($3);
  282. }
  283. | SENSOR STRING sensor_opts {
  284. struct ntp_conf_sensor *s;
  285. s = new_sensor($2);
  286. s->weight = $3.weight;
  287. s->correction = $3.correction;
  288. s->refstr = $3.refstr;
  289. s->stratum = $3.stratum;
  290. free($2);
  291. TAILQ_INSERT_TAIL(&conf->ntp_conf_sensors, s, entry);
  292. }
  293. ;
  294. address : STRING {
  295. if (($$ = calloc(1, sizeof(struct ntp_addr_wrap))) ==
  296. NULL)
  297. fatal(NULL);
  298. host($1, &$$->a);
  299. $$->name = $1;
  300. }
  301. ;
  302. urllist : urllist address {
  303. struct ntp_addr *p, *q = NULL;
  304. struct in_addr ina;
  305. struct in6_addr in6a;
  306. if (inet_pton(AF_INET, $2->name, &ina) != 1 &&
  307. inet_pton(AF_INET6, $2->name, &in6a) != 1) {
  308. yyerror("url can only be followed by IP "
  309. "addresses");
  310. free($2->name);
  311. free($2);
  312. YYERROR;
  313. }
  314. p = $2->a;
  315. while (p != NULL) {
  316. q = p;
  317. p = p->next;
  318. }
  319. if (q != NULL) {
  320. q->next = $1->a;
  321. $1->a = $2->a;
  322. free($2);
  323. }
  324. $$ = $1;
  325. }
  326. | url {
  327. $$ = $1;
  328. }
  329. ;
  330. url : STRING {
  331. char *hname, *path;
  332. if (($$ = calloc(1, sizeof(struct ntp_addr_wrap))) ==
  333. NULL)
  334. fatal("calloc");
  335. if (strncmp("https://", $1,
  336. strlen("https://")) != 0) {
  337. host($1, &$$->a);
  338. $$->name = $1;
  339. } else {
  340. hname = $1 + strlen("https://");
  341. path = hname + strcspn(hname, "/\\");
  342. if (*path != '\0') {
  343. if (($$->path = strdup(path)) == NULL)
  344. fatal("strdup");
  345. *path = '\0';
  346. }
  347. host(hname, &$$->a);
  348. if (($$->name = strdup(hname)) == NULL)
  349. fatal("strdup");
  350. }
  351. if ($$->path == NULL &&
  352. ($$->path = strdup("/")) == NULL)
  353. fatal("strdup");
  354. }
  355. ;
  356. listen_opts : { opts_default(); }
  357. listen_opts_l
  358. { $$ = opts; }
  359. | { opts_default(); $$ = opts; }
  360. ;
  361. listen_opts_l : listen_opts_l listen_opt
  362. | listen_opt
  363. ;
  364. listen_opt : rtable
  365. ;
  366. server_opts : { opts_default(); }
  367. server_opts_l
  368. { $$ = opts; }
  369. | { opts_default(); $$ = opts; }
  370. ;
  371. server_opts_l : server_opts_l server_opt
  372. | server_opt
  373. ;
  374. server_opt : weight
  375. ;
  376. sensor_opts : { opts_default(); }
  377. sensor_opts_l
  378. { $$ = opts; }
  379. | { opts_default(); $$ = opts; }
  380. ;
  381. sensor_opts_l : sensor_opts_l sensor_opt
  382. | sensor_opt
  383. ;
  384. sensor_opt : correction
  385. | refid
  386. | stratum
  387. | weight
  388. ;
  389. correction : CORRECTION NUMBER {
  390. if ($2 < -127000000 || $2 > 127000000) {
  391. yyerror("correction must be between "
  392. "-127000000 and 127000000 microseconds");
  393. YYERROR;
  394. }
  395. opts.correction = $2;
  396. }
  397. ;
  398. refid : REFID STRING {
  399. size_t l = strlen($2);
  400. if (l < 1 || l > 4) {
  401. yyerror("refid must be 1 to 4 characters");
  402. free($2);
  403. YYERROR;
  404. }
  405. opts.refstr = $2;
  406. }
  407. ;
  408. stratum : STRATUM NUMBER {
  409. if ($2 < 1 || $2 > 15) {
  410. yyerror("stratum must be between "
  411. "1 and 15");
  412. YYERROR;
  413. }
  414. opts.stratum = $2;
  415. }
  416. ;
  417. weight : WEIGHT NUMBER {
  418. if ($2 < 1 || $2 > 10) {
  419. yyerror("weight must be between 1 and 10");
  420. YYERROR;
  421. }
  422. opts.weight = $2;
  423. }
  424. rtable : RTABLE NUMBER {
  425. if ($2 < 0 || $2 > RT_TABLEID_MAX) {
  426. yyerror("rtable must be between 1"
  427. " and RT_TABLEID_MAX");
  428. YYERROR;
  429. }
  430. opts.rtable = $2;
  431. }
  432. ;
  433. %%
  434. void
  435. opts_default(void)
  436. {
  437. memset(&opts, 0, sizeof opts);
  438. opts.weight = 1;
  439. opts.stratum = 1;
  440. }
  441. struct keywords {
  442. const char *k_name;
  443. int k_val;
  444. };
  445. int
  446. yyerror(const char *fmt, ...)
  447. {
  448. va_list ap;
  449. char *msg;
  450. file->errors++;
  451. va_start(ap, fmt);
  452. if (vasprintf(&msg, fmt, ap) == -1)
  453. fatalx("yyerror vasprintf");
  454. va_end(ap);
  455. log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
  456. free(msg);
  457. return (0);
  458. }
  459. int
  460. kw_cmp(const void *k, const void *e)
  461. {
  462. return (strcmp(k, ((const struct keywords *)e)->k_name));
  463. }
  464. int
  465. lookup(char *s)
  466. {
  467. /* this has to be sorted always */
  468. static const struct keywords keywords[] = {
  469. { "constraint", CONSTRAINT},
  470. { "constraints", CONSTRAINTS},
  471. { "correction", CORRECTION},
  472. { "from", FROM},
  473. { "listen", LISTEN},
  474. { "on", ON},
  475. { "query", QUERY},
  476. { "refid", REFID},
  477. { "rtable", RTABLE},
  478. { "sensor", SENSOR},
  479. { "server", SERVER},
  480. { "servers", SERVERS},
  481. { "stratum", STRATUM},
  482. { "weight", WEIGHT}
  483. };
  484. const struct keywords *p;
  485. p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
  486. sizeof(keywords[0]), kw_cmp);
  487. if (p)
  488. return (p->k_val);
  489. else
  490. return (STRING);
  491. }
  492. #define MAXPUSHBACK 128
  493. u_char *parsebuf;
  494. int parseindex;
  495. u_char pushback_buffer[MAXPUSHBACK];
  496. int pushback_index = 0;
  497. int
  498. lgetc(int quotec)
  499. {
  500. int c, next;
  501. if (parsebuf) {
  502. /* Read character from the parsebuffer instead of input. */
  503. if (parseindex >= 0) {
  504. c = parsebuf[parseindex++];
  505. if (c != '\0')
  506. return (c);
  507. parsebuf = NULL;
  508. } else
  509. parseindex++;
  510. }
  511. if (pushback_index)
  512. return (pushback_buffer[--pushback_index]);
  513. if (quotec) {
  514. if ((c = getc(file->stream)) == EOF) {
  515. yyerror("reached end of file while parsing "
  516. "quoted string");
  517. if (file == topfile || popfile() == EOF)
  518. return (EOF);
  519. return (quotec);
  520. }
  521. return (c);
  522. }
  523. while ((c = getc(file->stream)) == '\\') {
  524. next = getc(file->stream);
  525. if (next != '\n') {
  526. c = next;
  527. break;
  528. }
  529. yylval.lineno = file->lineno;
  530. file->lineno++;
  531. }
  532. while (c == EOF) {
  533. if (file == topfile || popfile() == EOF)
  534. return (EOF);
  535. c = getc(file->stream);
  536. }
  537. return (c);
  538. }
  539. int
  540. lungetc(int c)
  541. {
  542. if (c == EOF)
  543. return (EOF);
  544. if (parsebuf) {
  545. parseindex--;
  546. if (parseindex >= 0)
  547. return (c);
  548. }
  549. if (pushback_index < MAXPUSHBACK-1)
  550. return (pushback_buffer[pushback_index++] = c);
  551. else
  552. return (EOF);
  553. }
  554. int
  555. findeol(void)
  556. {
  557. int c;
  558. parsebuf = NULL;
  559. /* skip to either EOF or the first real EOL */
  560. while (1) {
  561. if (pushback_index)
  562. c = pushback_buffer[--pushback_index];
  563. else
  564. c = lgetc(0);
  565. if (c == '\n') {
  566. file->lineno++;
  567. break;
  568. }
  569. if (c == EOF)
  570. break;
  571. }
  572. return (ERROR);
  573. }
  574. int
  575. yylex(void)
  576. {
  577. u_char buf[8096];
  578. u_char *p;
  579. int quotec, next, c;
  580. int token;
  581. p = buf;
  582. while ((c = lgetc(0)) == ' ' || c == '\t')
  583. ; /* nothing */
  584. yylval.lineno = file->lineno;
  585. if (c == '#')
  586. while ((c = lgetc(0)) != '\n' && c != EOF)
  587. ; /* nothing */
  588. switch (c) {
  589. case '\'':
  590. case '"':
  591. quotec = c;
  592. while (1) {
  593. if ((c = lgetc(quotec)) == EOF)
  594. return (0);
  595. if (c == '\n') {
  596. file->lineno++;
  597. continue;
  598. } else if (c == '\\') {
  599. if ((next = lgetc(quotec)) == EOF)
  600. return (0);
  601. if (next == quotec || next == ' ' ||
  602. next == '\t')
  603. c = next;
  604. else if (next == '\n') {
  605. file->lineno++;
  606. continue;
  607. } else
  608. lungetc(next);
  609. } else if (c == quotec) {
  610. *p = '\0';
  611. break;
  612. } else if (c == '\0') {
  613. yyerror("syntax error");
  614. return (findeol());
  615. }
  616. if (p + 1 >= buf + sizeof(buf) - 1) {
  617. yyerror("string too long");
  618. return (findeol());
  619. }
  620. *p++ = c;
  621. }
  622. yylval.v.string = strdup(buf);
  623. if (yylval.v.string == NULL)
  624. fatal("yylex: strdup");
  625. return (STRING);
  626. }
  627. #define allowed_to_end_number(x) \
  628. (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
  629. if (c == '-' || isdigit(c)) {
  630. do {
  631. *p++ = c;
  632. if ((size_t)(p-buf) >= sizeof(buf)) {
  633. yyerror("string too long");
  634. return (findeol());
  635. }
  636. } while ((c = lgetc(0)) != EOF && isdigit(c));
  637. lungetc(c);
  638. if (p == buf + 1 && buf[0] == '-')
  639. goto nodigits;
  640. if (c == EOF || allowed_to_end_number(c)) {
  641. const char *errstr = NULL;
  642. *p = '\0';
  643. yylval.v.number = strtonum(buf, LLONG_MIN,
  644. LLONG_MAX, &errstr);
  645. if (errstr) {
  646. yyerror("\"%s\" invalid number: %s",
  647. buf, errstr);
  648. return (findeol());
  649. }
  650. return (NUMBER);
  651. } else {
  652. nodigits:
  653. while (p > buf + 1)
  654. lungetc(*--p);
  655. c = *--p;
  656. if (c == '-')
  657. return (c);
  658. }
  659. }
  660. #define allowed_in_string(x) \
  661. (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
  662. x != '{' && x != '}' && x != '<' && x != '>' && \
  663. x != '!' && x != '=' && x != '/' && x != '#' && \
  664. x != ','))
  665. if (isalnum(c) || c == ':' || c == '_' || c == '*') {
  666. do {
  667. *p++ = c;
  668. if ((size_t)(p-buf) >= sizeof(buf)) {
  669. yyerror("string too long");
  670. return (findeol());
  671. }
  672. } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
  673. lungetc(c);
  674. *p = '\0';
  675. if ((token = lookup(buf)) == STRING)
  676. if ((yylval.v.string = strdup(buf)) == NULL)
  677. fatal("yylex: strdup");
  678. return (token);
  679. }
  680. if (c == '\n') {
  681. yylval.lineno = file->lineno;
  682. file->lineno++;
  683. }
  684. if (c == EOF)
  685. return (0);
  686. return (c);
  687. }
  688. struct file *
  689. pushfile(const char *name)
  690. {
  691. struct file *nfile;
  692. if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
  693. log_warn("%s", __func__);
  694. return (NULL);
  695. }
  696. if ((nfile->name = strdup(name)) == NULL) {
  697. log_warn("%s", __func__);
  698. free(nfile);
  699. return (NULL);
  700. }
  701. if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
  702. log_warn("%s: %s", __func__, nfile->name);
  703. free(nfile->name);
  704. free(nfile);
  705. return (NULL);
  706. }
  707. nfile->lineno = 1;
  708. TAILQ_INSERT_TAIL(&files, nfile, entry);
  709. return (nfile);
  710. }
  711. int
  712. popfile(void)
  713. {
  714. struct file *prev;
  715. if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
  716. prev->errors += file->errors;
  717. TAILQ_REMOVE(&files, file, entry);
  718. fclose(file->stream);
  719. free(file->name);
  720. free(file);
  721. file = prev;
  722. return (file ? 0 : EOF);
  723. }
  724. int
  725. parse_config(const char *filename, struct ntpd_conf *xconf)
  726. {
  727. int errors = 0;
  728. conf = xconf;
  729. TAILQ_INIT(&conf->listen_addrs);
  730. TAILQ_INIT(&conf->ntp_peers);
  731. TAILQ_INIT(&conf->ntp_conf_sensors);
  732. TAILQ_INIT(&conf->constraints);
  733. if ((file = pushfile(filename)) == NULL) {
  734. return (-1);
  735. }
  736. topfile = file;
  737. yyparse();
  738. errors = file->errors;
  739. popfile();
  740. return (errors ? -1 : 0);
  741. }