readconf.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* $Id: readconf.c,v 1.12 2002/08/29 19:38:56 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <sys/queue.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <assert.h>
  36. #include <limits.h>
  37. #include <fcntl.h>
  38. #include <unistd.h>
  39. #include "xmalloc.h"
  40. #include "lex.h"
  41. #include "error.h"
  42. #include "net.h"
  43. #include "data.h"
  44. __BEGIN_DECLS
  45. int read_mux(struct muxlist *mul, struct lex *);
  46. int read_source(struct sourcelist *sol, struct lex *);
  47. __END_DECLS
  48. /* mux <host> (port|:|,| ) <number> */
  49. int
  50. read_mux(struct muxlist *mul, struct lex *l)
  51. {
  52. struct mux *m;
  53. if (! SLIST_EMPTY(mul)) {
  54. warning("%s:%d: only one mux statement allowed",
  55. l->filename, l->cline);
  56. return 0;
  57. }
  58. lex_nexttoken(l);
  59. if (!lookup(l->token)) {
  60. warning("%s:%d: could not resolve '%s'",
  61. l->filename, l->cline, l->token );
  62. return 0;
  63. }
  64. m = add_mux(mul, lookup_address);
  65. m->ip = lookup_ip;
  66. /* check for port statement */
  67. lex_nexttoken(l);
  68. if (l->op == LXT_PORT || l->op == LXT_COLON || l->op == LXT_COMMA)
  69. lex_nexttoken(l);
  70. else {
  71. if (l->type != LXY_NUMBER) {
  72. lex_ungettoken(l);
  73. m->port = MONMUX_PORT;
  74. return 1;
  75. }
  76. }
  77. if (l->type != LXY_NUMBER) {
  78. parse_error(l, "<number>");
  79. return 0;
  80. }
  81. m->port = l->value;
  82. return 1;
  83. }
  84. /* source <host> { accept ... | write ... } */
  85. int
  86. read_source(struct sourcelist *sol, struct lex *l)
  87. {
  88. struct source *source;
  89. struct stream *stream;
  90. char sn[_POSIX2_LINE_MAX];
  91. char sa[_POSIX2_LINE_MAX];
  92. int st;
  93. /* get hostname */
  94. lex_nexttoken(l);
  95. if (!lookup(l->token)) {
  96. warning("%s:%d: could not resolve '%s'",
  97. l->filename, l->cline, l->token );
  98. return 0;
  99. }
  100. source = add_source(sol, lookup_address);
  101. source->ip = lookup_ip;
  102. EXPECT(l, LXT_BEGIN);
  103. while (lex_nexttoken(l)) {
  104. switch (l->op) {
  105. /* accept { cpu(x), ... } */
  106. case LXT_ACCEPT:
  107. EXPECT(l, LXT_BEGIN);
  108. while (lex_nexttoken(l) && l->op != LXT_END) {
  109. switch (l->op) {
  110. case LXT_CPU:
  111. case LXT_IF:
  112. case LXT_IO:
  113. case LXT_MEM:
  114. case LXT_PF:
  115. st = token2type(l->op);
  116. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  117. /* parse arg */
  118. lex_nexttoken(l);
  119. if (l->op == LXT_OPEN) {
  120. lex_nexttoken(l);
  121. if (l->op == LXT_CLOSE) {
  122. parse_error(l, "<stream argument>");
  123. return 0;
  124. }
  125. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  126. lex_nexttoken(l);
  127. if (l->op != LXT_CLOSE) {
  128. parse_error(l, ")");
  129. return 0;
  130. }
  131. } else {
  132. lex_ungettoken(l);
  133. sa[0]='\0';
  134. }
  135. if ((stream = add_source_stream(source, st, sa)) == NULL) {
  136. warning("%s:%d: stream %s(%s) redefined",
  137. l->filename, l->cline, sn, sa);
  138. return 0;
  139. }
  140. break; /* LXT_CPU/LXT_IF/LXT_IO/LXT_MEM/LXT_PF */
  141. case LXT_COMMA:
  142. break;
  143. default:
  144. parse_error(l, "{cpu|mem|if|io|pf}");
  145. return 0;
  146. break;
  147. }
  148. }
  149. break; /* LXT_ACCEPT */
  150. /* write cpu(0) in "filename" */
  151. case LXT_WRITE:
  152. lex_nexttoken(l);
  153. switch (l->op) {
  154. case LXT_CPU:
  155. case LXT_IF:
  156. case LXT_IO:
  157. case LXT_MEM:
  158. case LXT_PF:
  159. st = token2type(l->op);
  160. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  161. /* parse arg */
  162. lex_nexttoken(l);
  163. if (l->op == LXT_OPEN) {
  164. lex_nexttoken(l);
  165. if (l->op == LXT_CLOSE) {
  166. parse_error(l, "<stream argument>");
  167. return 0;
  168. }
  169. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  170. lex_nexttoken(l);
  171. if (l->op != LXT_CLOSE) {
  172. parse_error(l, ")");
  173. return 0;
  174. }
  175. } else {
  176. lex_ungettoken(l);
  177. sa[0]='\0';
  178. }
  179. EXPECT(l, LXT_IN);
  180. lex_nexttoken(l);
  181. if ((stream = find_source_stream(source, st, sa)) == NULL) {
  182. if (strlen(sa)) {
  183. warning("%s:%d: stream %s(%s) is not accepted for %s",
  184. l->filename, l->cline, sn, sa, source->name);
  185. return 0;
  186. } else {
  187. warning("%s:%d: stream %s is not accepted for %s",
  188. l->filename, l->cline, sn, source->name);
  189. return 0;
  190. }
  191. } else {
  192. int fd;
  193. /* try filename */
  194. if ((fd = open(l->token, O_RDWR | O_NONBLOCK, 0)) == -1) {
  195. warning("%s:%d: file '%s' cannot be opened",
  196. l->filename, l->cline, l->token);
  197. return 0;
  198. } else {
  199. close(fd);
  200. stream->file = xstrdup(l->token);
  201. }
  202. }
  203. break; /* LXT_CPU/LXT_IF/LXT_IO/LXT_MEM/LXT_PF */
  204. default:
  205. parse_error(l, "{cpu|mem|if|io}");
  206. return 0;
  207. break;
  208. }
  209. break; /* LXT_WRITE */
  210. case LXT_END:
  211. return 1;
  212. default:
  213. parse_error(l, "accept|write");
  214. return 0;
  215. }
  216. }
  217. warning("%s:%d: missing close brace on source statement",
  218. l->filename, l->cline);
  219. return 0;
  220. }
  221. /* Read monmux.conf */
  222. int
  223. read_config_file(struct muxlist *mul,
  224. struct sourcelist *sol,
  225. const char *filename)
  226. {
  227. struct lex *l;
  228. struct source *source;
  229. struct stream *stream;
  230. SLIST_INIT(mul);
  231. SLIST_INIT(sol);
  232. if ((l = open_lex(filename)) == NULL)
  233. return 0;
  234. info("reading configfile '%s'", filename);
  235. while (lex_nexttoken(l)) {
  236. /* expecting keyword now */
  237. switch (l->op) {
  238. case LXT_MUX:
  239. if (!read_mux(mul, l))
  240. return 0;
  241. break;
  242. case LXT_SOURCE:
  243. if (!read_source(sol, l))
  244. return 0;
  245. break;
  246. default:
  247. parse_error(l, "mux|source" );
  248. return 0;
  249. break;
  250. }
  251. }
  252. /* sanity checks */
  253. if (SLIST_EMPTY(mul)) {
  254. warning("%s: no mux statement seen",
  255. l->filename);
  256. return 0;
  257. }
  258. if (SLIST_EMPTY(sol)) {
  259. warning("%s: no source section seen",
  260. l->filename);
  261. return 0;
  262. } else {
  263. SLIST_FOREACH(source, sol, sources) {
  264. if (SLIST_EMPTY(&source->sl)) {
  265. warning("%s: no streams accepted for source '%s'",
  266. l->filename, source->name);
  267. return 0;
  268. } else {
  269. SLIST_FOREACH(stream, &source->sl, streams) {
  270. if (stream->file == NULL) {
  271. warning("%s: no filename specified for stream '%s(%s)' in source '%s'",
  272. l->filename, type2str(stream->type), stream->args, source->name);
  273. return 0;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. close_lex(l);
  280. return 1;
  281. }