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.

619 lines
12 KiB

20 years ago
  1. /* $OpenBSD: parse.y,v 1.47 2010/08/03 18:42:40 henning 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. int kw_cmp(const void *, const void *);
  48. int lookup(char *);
  49. int lgetc(int);
  50. int lungetc(int);
  51. int findeol(void);
  52. struct ntpd_conf *conf;
  53. struct opts {
  54. int weight;
  55. int correction;
  56. char *refstr;
  57. } opts;
  58. void opts_default(void);
  59. typedef struct {
  60. union {
  61. int64_t number;
  62. char *string;
  63. struct ntp_addr_wrap *addr;
  64. struct opts opts;
  65. } v;
  66. int lineno;
  67. } YYSTYPE;
  68. %}
  69. %token LISTEN ON
  70. %token SERVER SERVERS SENSOR CORRECTION REFID WEIGHT
  71. %token ERROR
  72. %token <v.string> STRING
  73. %token <v.number> NUMBER
  74. %type <v.addr> address
  75. %type <v.opts> server_opts server_opts_l server_opt
  76. %type <v.opts> sensor_opts sensor_opts_l sensor_opt
  77. %type <v.opts> correction
  78. %type <v.opts> refid
  79. %type <v.opts> weight
  80. %%
  81. grammar : /* empty */
  82. | grammar '\n'
  83. | grammar main '\n'
  84. | grammar error '\n' { file->errors++; }
  85. ;
  86. main : LISTEN ON address {
  87. struct listen_addr *la;
  88. struct ntp_addr *h, *next;
  89. if ((h = $3->a) == NULL &&
  90. (host_dns($3->name, &h) == -1 || !h)) {
  91. yyerror("could not resolve \"%s\"", $3->name);
  92. free($3->name);
  93. free($3);
  94. YYERROR;
  95. }
  96. for (; h != NULL; h = next) {
  97. next = h->next;
  98. if (h->ss.ss_family == AF_UNSPEC) {
  99. conf->listen_all = 1;
  100. free(h);
  101. continue;
  102. }
  103. la = calloc(1, sizeof(struct listen_addr));
  104. if (la == NULL)
  105. fatal("listen on calloc");
  106. la->fd = -1;
  107. memcpy(&la->sa, &h->ss,
  108. sizeof(struct sockaddr_storage));
  109. TAILQ_INSERT_TAIL(&conf->listen_addrs, la,
  110. entry);
  111. free(h);
  112. }
  113. free($3->name);
  114. free($3);
  115. }
  116. | SERVERS address server_opts {
  117. struct ntp_peer *p;
  118. struct ntp_addr *h, *next;
  119. h = $2->a;
  120. do {
  121. if (h != NULL) {
  122. next = h->next;
  123. if (h->ss.ss_family != AF_INET &&
  124. h->ss.ss_family != AF_INET6) {
  125. yyerror("IPv4 or IPv6 address "
  126. "or hostname expected");
  127. free(h);
  128. free($2->name);
  129. free($2);
  130. YYERROR;
  131. }
  132. h->next = NULL;
  133. } else
  134. next = NULL;
  135. p = new_peer();
  136. p->weight = $3.weight;
  137. p->addr = h;
  138. p->addr_head.a = h;
  139. p->addr_head.pool = 1;
  140. p->addr_head.name = strdup($2->name);
  141. if (p->addr_head.name == NULL)
  142. fatal(NULL);
  143. if (p->addr != NULL)
  144. p->state = STATE_DNS_DONE;
  145. TAILQ_INSERT_TAIL(&conf->ntp_peers, p, entry);
  146. h = next;
  147. } while (h != NULL);
  148. free($2->name);
  149. free($2);
  150. }
  151. | SERVER address server_opts {
  152. struct ntp_peer *p;
  153. struct ntp_addr *h, *next;
  154. p = new_peer();
  155. for (h = $2->a; h != NULL; h = next) {
  156. next = h->next;
  157. if (h->ss.ss_family != AF_INET &&
  158. h->ss.ss_family != AF_INET6) {
  159. yyerror("IPv4 or IPv6 address "
  160. "or hostname expected");
  161. free(h);
  162. free(p);
  163. free($2->name);
  164. free($2);
  165. YYERROR;
  166. }
  167. h->next = p->addr;
  168. p->addr = h;
  169. }
  170. p->weight = $3.weight;
  171. p->addr_head.a = p->addr;
  172. p->addr_head.pool = 0;
  173. p->addr_head.name = strdup($2->name);
  174. if (p->addr_head.name == NULL)
  175. fatal(NULL);
  176. if (p->addr != NULL)
  177. p->state = STATE_DNS_DONE;
  178. TAILQ_INSERT_TAIL(&conf->ntp_peers, p, entry);
  179. free($2->name);
  180. free($2);
  181. }
  182. | SENSOR STRING sensor_opts {
  183. struct ntp_conf_sensor *s;
  184. s = new_sensor($2);
  185. s->weight = $3.weight;
  186. s->correction = $3.correction;
  187. s->refstr = $3.refstr;
  188. free($2);
  189. TAILQ_INSERT_TAIL(&conf->ntp_conf_sensors, s, entry);
  190. }
  191. ;
  192. address : STRING {
  193. if (($$ = calloc(1, sizeof(struct ntp_addr_wrap))) ==
  194. NULL)
  195. fatal(NULL);
  196. if (host($1, &$$->a) == -1) {
  197. yyerror("could not parse address spec \"%s\"",
  198. $1);
  199. free($1);
  200. free($$);
  201. YYERROR;
  202. }
  203. $$->name = $1;
  204. }
  205. ;
  206. server_opts : { opts_default(); }
  207. server_opts_l
  208. { $$ = opts; }
  209. | { opts_default(); $$ = opts; }
  210. ;
  211. server_opts_l : server_opts_l server_opt
  212. | server_opt
  213. ;
  214. server_opt : weight
  215. ;
  216. sensor_opts : { opts_default(); }
  217. sensor_opts_l
  218. { $$ = opts; }
  219. | { opts_default(); $$ = opts; }
  220. ;
  221. sensor_opts_l : sensor_opts_l sensor_opt
  222. | sensor_opt
  223. ;
  224. sensor_opt : correction
  225. | refid
  226. | weight
  227. ;
  228. correction : CORRECTION NUMBER {
  229. if ($2 < -127000000 || $2 > 127000000) {
  230. yyerror("correction must be between "
  231. "-127000000 and 127000000 microseconds");
  232. YYERROR;
  233. }
  234. opts.correction = $2;
  235. }
  236. ;
  237. refid : REFID STRING {
  238. size_t l = strlen($2);
  239. if (l < 1 || l > 4) {
  240. yyerror("refid must be 1 to 4 characters");
  241. free($2);
  242. YYERROR;
  243. }
  244. opts.refstr = $2;
  245. }
  246. ;
  247. weight : WEIGHT NUMBER {
  248. if ($2 < 1 || $2 > 10) {
  249. yyerror("weight must be between 1 and 10");
  250. YYERROR;
  251. }
  252. opts.weight = $2;
  253. }
  254. ;
  255. %%
  256. void
  257. opts_default(void)
  258. {
  259. bzero(&opts, sizeof opts);
  260. opts.weight = 1;
  261. }
  262. struct keywords {
  263. const char *k_name;
  264. int k_val;
  265. };
  266. int
  267. yyerror(const char *fmt, ...)
  268. {
  269. va_list ap;
  270. char *nfmt;
  271. file->errors++;
  272. va_start(ap, fmt);
  273. if (asprintf(&nfmt, "%s:%d: %s", file->name, yylval.lineno, fmt) == -1)
  274. fatalx("yyerror asprintf");
  275. vlog(LOG_CRIT, nfmt, ap);
  276. va_end(ap);
  277. free(nfmt);
  278. return (0);
  279. }
  280. int
  281. kw_cmp(const void *k, const void *e)
  282. {
  283. return (strcmp(k, ((const struct keywords *)e)->k_name));
  284. }
  285. int
  286. lookup(char *s)
  287. {
  288. /* this has to be sorted always */
  289. static const struct keywords keywords[] = {
  290. { "correction", CORRECTION},
  291. { "listen", LISTEN},
  292. { "on", ON},
  293. { "refid", REFID},
  294. { "sensor", SENSOR},
  295. { "server", SERVER},
  296. { "servers", SERVERS},
  297. { "weight", WEIGHT}
  298. };
  299. const struct keywords *p;
  300. p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
  301. sizeof(keywords[0]), kw_cmp);
  302. if (p)
  303. return (p->k_val);
  304. else
  305. return (STRING);
  306. }
  307. #define MAXPUSHBACK 128
  308. char *parsebuf;
  309. int parseindex;
  310. char pushback_buffer[MAXPUSHBACK];
  311. int pushback_index = 0;
  312. int
  313. lgetc(int quotec)
  314. {
  315. int c, next;
  316. if (parsebuf) {
  317. /* Read character from the parsebuffer instead of input. */
  318. if (parseindex >= 0) {
  319. c = parsebuf[parseindex++];
  320. if (c != '\0')
  321. return (c);
  322. parsebuf = NULL;
  323. } else
  324. parseindex++;
  325. }
  326. if (pushback_index)
  327. return (pushback_buffer[--pushback_index]);
  328. if (quotec) {
  329. if ((c = getc(file->stream)) == EOF) {
  330. yyerror("reached end of file while parsing "
  331. "quoted string");
  332. if (file == topfile || popfile() == EOF)
  333. return (EOF);
  334. return (quotec);
  335. }
  336. return (c);
  337. }
  338. while ((c = getc(file->stream)) == '\\') {
  339. next = getc(file->stream);
  340. if (next != '\n') {
  341. c = next;
  342. break;
  343. }
  344. yylval.lineno = file->lineno;
  345. file->lineno++;
  346. }
  347. while (c == EOF) {
  348. if (file == topfile || popfile() == EOF)
  349. return (EOF);
  350. c = getc(file->stream);
  351. }
  352. return (c);
  353. }
  354. int
  355. lungetc(int c)
  356. {
  357. if (c == EOF)
  358. return (EOF);
  359. if (parsebuf) {
  360. parseindex--;
  361. if (parseindex >= 0)
  362. return (c);
  363. }
  364. if (pushback_index < MAXPUSHBACK-1)
  365. return (pushback_buffer[pushback_index++] = c);
  366. else
  367. return (EOF);
  368. }
  369. int
  370. findeol(void)
  371. {
  372. int c;
  373. parsebuf = NULL;
  374. /* skip to either EOF or the first real EOL */
  375. while (1) {
  376. if (pushback_index)
  377. c = pushback_buffer[--pushback_index];
  378. else
  379. c = lgetc(0);
  380. if (c == '\n') {
  381. file->lineno++;
  382. break;
  383. }
  384. if (c == EOF)
  385. break;
  386. }
  387. return (ERROR);
  388. }
  389. int
  390. yylex(void)
  391. {
  392. char buf[8096];
  393. char *p;
  394. int quotec, next, c;
  395. int token;
  396. p = buf;
  397. while ((c = lgetc(0)) == ' ' || c == '\t')
  398. ; /* nothing */
  399. yylval.lineno = file->lineno;
  400. if (c == '#')
  401. while ((c = lgetc(0)) != '\n' && c != EOF)
  402. ; /* nothing */
  403. switch (c) {
  404. case '\'':
  405. case '"':
  406. quotec = c;
  407. while (1) {
  408. if ((c = lgetc(quotec)) == EOF)
  409. return (0);
  410. if (c == '\n') {
  411. file->lineno++;
  412. continue;
  413. } else if (c == '\\') {
  414. if ((next = lgetc(quotec)) == EOF)
  415. return (0);
  416. if (next == quotec || c == ' ' || c == '\t')
  417. c = next;
  418. else if (next == '\n') {
  419. file->lineno++;
  420. continue;
  421. } else
  422. lungetc(next);
  423. } else if (c == quotec) {
  424. *p = '\0';
  425. break;
  426. }
  427. if (p + 1 >= buf + sizeof(buf) - 1) {
  428. yyerror("string too long");
  429. return (findeol());
  430. }
  431. *p++ = (char)c;
  432. }
  433. yylval.v.string = strdup(buf);
  434. if (yylval.v.string == NULL)
  435. fatal("yylex: strdup");
  436. return (STRING);
  437. }
  438. #define allowed_to_end_number(x) \
  439. (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
  440. if (c == '-' || isdigit(c)) {
  441. do {
  442. *p++ = c;
  443. if ((unsigned)(p-buf) >= sizeof(buf)) {
  444. yyerror("string too long");
  445. return (findeol());
  446. }
  447. } while ((c = lgetc(0)) != EOF && isdigit(c));
  448. lungetc(c);
  449. if (p == buf + 1 && buf[0] == '-')
  450. goto nodigits;
  451. if (c == EOF || allowed_to_end_number(c)) {
  452. const char *errstr = NULL;
  453. *p = '\0';
  454. yylval.v.number = strtonum(buf, LLONG_MIN,
  455. LLONG_MAX, &errstr);
  456. if (errstr) {
  457. yyerror("\"%s\" invalid number: %s",
  458. buf, errstr);
  459. return (findeol());
  460. }
  461. return (NUMBER);
  462. } else {
  463. nodigits:
  464. while (p > buf + 1)
  465. lungetc(*--p);
  466. c = *--p;
  467. if (c == '-')
  468. return (c);
  469. }
  470. }
  471. #define allowed_in_string(x) \
  472. (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
  473. x != '{' && x != '}' && x != '<' && x != '>' && \
  474. x != '!' && x != '=' && x != '/' && x != '#' && \
  475. x != ','))
  476. if (isalnum(c) || c == ':' || c == '_' || c == '*') {
  477. do {
  478. *p++ = c;
  479. if ((unsigned)(p-buf) >= sizeof(buf)) {
  480. yyerror("string too long");
  481. return (findeol());
  482. }
  483. } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
  484. lungetc(c);
  485. *p = '\0';
  486. if ((token = lookup(buf)) == STRING)
  487. if ((yylval.v.string = strdup(buf)) == NULL)
  488. fatal("yylex: strdup");
  489. return (token);
  490. }
  491. if (c == '\n') {
  492. yylval.lineno = file->lineno;
  493. file->lineno++;
  494. }
  495. if (c == EOF)
  496. return (0);
  497. return (c);
  498. }
  499. struct file *
  500. pushfile(const char *name)
  501. {
  502. struct file *nfile;
  503. if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
  504. log_warn("malloc");
  505. return (NULL);
  506. }
  507. if ((nfile->name = strdup(name)) == NULL) {
  508. log_warn("malloc");
  509. free(nfile);
  510. return (NULL);
  511. }
  512. if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
  513. log_warn("%s", nfile->name);
  514. free(nfile->name);
  515. free(nfile);
  516. return (NULL);
  517. }
  518. nfile->lineno = 1;
  519. TAILQ_INSERT_TAIL(&files, nfile, entry);
  520. return (nfile);
  521. }
  522. int
  523. popfile(void)
  524. {
  525. struct file *prev;
  526. if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
  527. prev->errors += file->errors;
  528. TAILQ_REMOVE(&files, file, entry);
  529. fclose(file->stream);
  530. free(file->name);
  531. free(file);
  532. file = prev;
  533. return (file ? 0 : EOF);
  534. }
  535. int
  536. parse_config(const char *filename, struct ntpd_conf *xconf)
  537. {
  538. int errors = 0;
  539. conf = xconf;
  540. TAILQ_INIT(&conf->listen_addrs);
  541. TAILQ_INIT(&conf->ntp_peers);
  542. TAILQ_INIT(&conf->ntp_conf_sensors);
  543. if ((file = pushfile(filename)) == NULL) {
  544. return (-1);
  545. }
  546. topfile = file;
  547. yyparse();
  548. errors = file->errors;
  549. popfile();
  550. return (errors ? -1 : 0);
  551. }