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.

761 lines
15 KiB

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