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.

793 lines
16 KiB

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