xref: /lighttpd1.4/src/configparser.y (revision 1fad70f4)
1 %token_prefix TK_
2 %extra_argument {config_t *ctx}
3 %name configparser
4 
5 %include {
6 #include "first.h"
7 #include "base.h"
8 #include "configfile.h"
9 #include "buffer.h"
10 #include "array.h"
11 #include "http_header.h" /* http_header_hkey_get() */
12 #include "request.h" /* http_request_host_normalize() */
13 
14 #include <ctype.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 
20 static data_config * configparser_get_data_config(const array *a, const char *k, const size_t klen) {
21   return (data_config *)array_get_data_unset(a, k, klen);
22 }
23 
24 static void configparser_push(config_t *ctx, data_config *dc, int isnew) {
25   if (isnew) {
26     dc->context_ndx = ctx->all_configs->used;
27     force_assert(dc->context_ndx > ctx->current->context_ndx);
28     array_insert_unique(ctx->all_configs, (data_unset *)dc);
29     dc->parent = ctx->current;
30     vector_config_weak_push(&dc->parent->children, dc);
31   }
32   if (ctx->configs_stack.used > 0 && ctx->current->context_ndx == 0) {
33     fprintf(stderr, "Cannot use conditionals inside a global { ... } block\n");
34     exit(-1);
35   }
36   vector_config_weak_push(&ctx->configs_stack, ctx->current);
37   ctx->current = dc;
38 }
39 
40 static data_config *configparser_pop(config_t *ctx) {
41   data_config *old = ctx->current;
42   ctx->current = vector_config_weak_pop(&ctx->configs_stack);
43   return old;
44 }
45 
46 /* return a copied variable */
47 static data_unset *configparser_get_variable(config_t *ctx, const buffer *key) {
48   const data_unset *du;
49   data_config *dc;
50 
51 #if 0
52   fprintf(stderr, "get var %s\n", key->ptr);
53 #endif
54   for (dc = ctx->current; dc; dc = dc->parent) {
55 #if 0
56     fprintf(stderr, "get var on block: %s\n", dc->key.ptr);
57     array_print(dc->value, 0);
58 #endif
59     if (NULL != (du = array_get_element_klen(dc->value, CONST_BUF_LEN(key)))) {
60       data_unset *du_copy = du->fn->copy(du);
61       buffer_clear(&du_copy->key);
62       return du_copy;
63     }
64   }
65   return NULL;
66 }
67 
68 /* op1 is to be eat/return by this function if success, op1->key is not cared
69    op2 is left untouch, unreferenced
70  */
71 static data_unset *configparser_merge_data(data_unset *op1, const data_unset *op2) {
72   /* type mismatch */
73   if (op1->type != op2->type) {
74     if (op1->type == TYPE_STRING && op2->type == TYPE_INTEGER) {
75       data_string *ds = (data_string *)op1;
76       buffer_append_int(&ds->value, ((data_integer*)op2)->value);
77       return op1;
78     } else if (op1->type == TYPE_INTEGER && op2->type == TYPE_STRING) {
79       data_string *ds = data_string_init();
80       buffer_append_int(&ds->value, ((data_integer*)op1)->value);
81       buffer_append_string_buffer(&ds->value, &((data_string*)op2)->value);
82       op1->fn->free(op1);
83       return (data_unset *)ds;
84     } else {
85       fprintf(stderr, "data type mismatch, cannot merge\n");
86       op1->fn->free(op1);
87       return NULL;
88     }
89   }
90 
91   switch (op1->type) {
92     case TYPE_STRING:
93       buffer_append_string_buffer(&((data_string *)op1)->value, &((data_string *)op2)->value);
94       break;
95     case TYPE_INTEGER:
96       ((data_integer *)op1)->value += ((data_integer *)op2)->value;
97       break;
98     case TYPE_ARRAY: {
99       array *dst = &((data_array *)op1)->value;
100       array *src = &((data_array *)op2)->value;
101       data_unset *du;
102       size_t i;
103 
104       for (i = 0; i < src->used; i ++) {
105         du = (data_unset *)src->data[i];
106         if (du) {
107           if (buffer_is_empty(&du->key) || !array_get_element_klen(dst, CONST_BUF_LEN(&du->key))) {
108             array_insert_unique(dst, du->fn->copy(du));
109           } else {
110             fprintf(stderr, "Duplicate array-key '%s'\n", du->key.ptr);
111             op1->fn->free(op1);
112             return NULL;
113           }
114         }
115       }
116       break;
117     default:
118       force_assert(0);
119       break;
120     }
121   }
122   return op1;
123 }
124 
125 static int configparser_remoteip_normalize_compat(buffer *rvalue) {
126   /* $HTTP["remoteip"] IPv6 accepted with or without '[]' for config compat
127    * http_request_host_normalize() expects IPv6 with '[]',
128    * and config processing at runtime expects COMP_HTTP_REMOTE_IP
129    * compared without '[]', so strip '[]' after normalization */
130   buffer *b = buffer_init();
131   int rc;
132 
133   if (rvalue->ptr[0] != '[') {
134       buffer_append_string_len(b, CONST_STR_LEN("["));
135       buffer_append_string_buffer(b, rvalue);
136       buffer_append_string_len(b, CONST_STR_LEN("]"));
137   } else {
138       buffer_append_string_buffer(b, rvalue);
139   }
140 
141   rc = http_request_host_normalize(b, 0);
142 
143   if (0 == rc) {
144     /* remove surrounding '[]' */
145     size_t blen = buffer_string_length(b);
146     if (blen > 1) buffer_copy_string_len(rvalue, b->ptr+1, blen-2);
147   }
148 
149   buffer_free(b);
150   return rc;
151 }
152 
153 }
154 
155 %parse_failure {
156   ctx->ok = 0;
157 }
158 
159 input ::= metalines.
160 metalines ::= metalines metaline.
161 metalines ::= .
162 metaline ::= varline.
163 metaline ::= global.
164 metaline ::= condlines(A) EOL. { A = NULL; }
165 metaline ::= include.
166 metaline ::= include_shell.
167 metaline ::= EOL.
168 
169 %type       value                  {data_unset *}
170 %type       expression             {data_unset *}
171 %type       aelement               {data_unset *}
172 %type       condline               {data_config *}
173 %type       cond_else              {data_config *}
174 %type       condlines              {data_config *}
175 %type       aelements              {array *}
176 %type       array                  {array *}
177 %type       key                    {buffer *}
178 %type       stringop               {buffer *}
179 
180 %type       cond                   {config_cond_t }
181 
182 %destructor value                  { if ($$) $$->fn->free($$); }
183 %destructor expression             { if ($$) $$->fn->free($$); }
184 %destructor aelement               { if ($$) $$->fn->free($$); }
185 %destructor aelements              { array_free($$); }
186 %destructor array                  { array_free($$); }
187 %destructor key                    { buffer_free($$); }
188 %destructor stringop               { buffer_free($$); }
189 
190 %token_type                        {buffer *}
191 %token_destructor                  { buffer_free($$); }
192 
193 varline ::= key(A) ASSIGN expression(B). {
194   if (ctx->ok) {
195     buffer_copy_buffer(&B->key, A);
196     if (strncmp(A->ptr, "env.", sizeof("env.") - 1) == 0) {
197       fprintf(stderr, "Setting env variable is not supported in conditional %d %s: %s\n",
198           ctx->current->context_ndx,
199           ctx->current->key.ptr, A->ptr);
200       ctx->ok = 0;
201     } else if (NULL == array_get_element_klen(ctx->current->value, CONST_BUF_LEN(&B->key))) {
202       array_insert_unique(ctx->current->value, B);
203       B = NULL;
204     } else {
205       fprintf(stderr, "Duplicate config variable in conditional %d %s: %s\n",
206               ctx->current->context_ndx,
207               ctx->current->key.ptr, B->key.ptr);
208       ctx->ok = 0;
209     }
210   }
211   buffer_free(A);
212   A = NULL;
213   if (B) B->fn->free(B);
214   B = NULL;
215 }
216 
217 varline ::= key(A) FORCE_ASSIGN expression(B). {
218   if (ctx->ok) {
219     if (strncmp(A->ptr, "env.", sizeof("env.") - 1) == 0) {
220       fprintf(stderr, "Setting env variable is not supported in conditional %d %s: %s\n",
221               ctx->current->context_ndx,
222               ctx->current->key.ptr, A->ptr);
223       ctx->ok = 0;
224     } else {
225       buffer_copy_buffer(&B->key, A);
226       array_replace(ctx->current->value, B);
227       B = NULL;
228     }
229   }
230   buffer_free(A);
231   A = NULL;
232   if (B) B->fn->free(B);
233   B = NULL;
234 }
235 
236 varline ::= key(A) APPEND expression(B). {
237   if (ctx->ok) {
238     array *vars = ctx->current->value;
239     data_unset *du;
240 
241     if (strncmp(A->ptr, "env.", sizeof("env.") - 1) == 0) {
242       fprintf(stderr, "Appending env variable is not supported in conditional %d %s: %s\n",
243           ctx->current->context_ndx,
244           ctx->current->key.ptr, A->ptr);
245       ctx->ok = 0;
246     } else if (NULL != (du = array_extract_element_klen(vars, CONST_BUF_LEN(A))) || NULL != (du = configparser_get_variable(ctx, A))) {
247       du = configparser_merge_data(du, B);
248       if (NULL == du) {
249         ctx->ok = 0;
250       }
251       else {
252         buffer_copy_buffer(&du->key, A);
253         array_insert_unique(ctx->current->value, du);
254       }
255     } else {
256       buffer_copy_buffer(&B->key, A);
257       array_insert_unique(ctx->current->value, B);
258       B = NULL;
259     }
260   }
261   buffer_free(A);
262   A = NULL;
263   if (B) B->fn->free(B);
264   B = NULL;
265 }
266 
267 key(A) ::= LKEY(B). {
268   if (strchr(B->ptr, '.') == NULL) {
269     A = buffer_init_string("var.");
270     buffer_append_string_buffer(A, B);
271   } else {
272     A = B;
273     B = NULL;
274   }
275   buffer_free(B);
276   B = NULL;
277 }
278 
279 expression(A) ::= expression(B) PLUS value(C). {
280   A = NULL;
281   if (ctx->ok) {
282     A = configparser_merge_data(B, C);
283     B = NULL;
284     if (NULL == A) {
285       ctx->ok = 0;
286     }
287   }
288   if (B) B->fn->free(B);
289   B = NULL;
290   if (C) C->fn->free(C);
291   C = NULL;
292 }
293 
294 expression(A) ::= value(B). {
295   A = B;
296   B = NULL;
297 }
298 
299 value(A) ::= key(B). {
300   A = NULL;
301   if (ctx->ok) {
302     if (strncmp(B->ptr, "env.", sizeof("env.") - 1) == 0) {
303       char *env;
304 
305       if (NULL != (env = getenv(B->ptr + 4))) {
306         data_string *ds;
307         ds = data_string_init();
308         buffer_append_string(&ds->value, env);
309         A = (data_unset *)ds;
310       }
311       else {
312         fprintf(stderr, "Undefined env variable: %s\n", B->ptr + 4);
313         ctx->ok = 0;
314       }
315     } else if (NULL == (A = configparser_get_variable(ctx, B))) {
316       fprintf(stderr, "Undefined config variable: %s\n", B->ptr);
317       ctx->ok = 0;
318     }
319   }
320   buffer_free(B);
321   B = NULL;
322 }
323 
324 value(A) ::= STRING(B). {
325   A = (data_unset *)data_string_init();
326   /* assumes data_string_init() result does not require swap and buffer_free()*/
327   memcpy(&((data_string *)A)->value, B, sizeof(*B));
328   free(B);
329   B = NULL;
330 }
331 
332 value(A) ::= INTEGER(B). {
333   char *endptr;
334   A = (data_unset *)data_integer_init();
335   errno = 0;
336   ((data_integer *)(A))->value = strtol(B->ptr, &endptr, 10);
337   /* skip trailing whitespace */
338   if (endptr != B->ptr) while (isspace(*(unsigned char *)endptr)) endptr++;
339   if (0 != errno || *endptr != '\0') {
340     fprintf(stderr, "error parsing number: '%s'\n", B->ptr);
341     ctx->ok = 0;
342   }
343   buffer_free(B);
344   B = NULL;
345 }
346 value(A) ::= array(B). {
347   A = (data_unset *)data_array_init();
348   /* assumes data_array_init() result does not require swap and array_free() */
349   memcpy(&((data_array *)(A))->value, B, sizeof(*B));
350   free(B);
351   B = NULL;
352 }
353 array(A) ::= LPARAN RPARAN. {
354   A = array_init(8);
355 }
356 array(A) ::= LPARAN aelements(B) RPARAN. {
357   A = B;
358   B = NULL;
359 }
360 
361 aelements(A) ::= aelements(C) COMMA aelement(B). {
362   A = NULL;
363   if (ctx->ok) {
364     if (buffer_is_empty(&B->key) ||
365         NULL == array_get_element_klen(C, CONST_BUF_LEN(&B->key))) {
366       array_insert_unique(C, B);
367       B = NULL;
368     } else {
369       fprintf(stderr, "Error: duplicate array-key: %s. Please get rid of the duplicate entry.\n",
370               B->key.ptr);
371       ctx->ok = 0;
372     }
373 
374     A = C;
375     C = NULL;
376   }
377   array_free(C);
378   C = NULL;
379   if (B) B->fn->free(B);
380   B = NULL;
381 }
382 
383 aelements(A) ::= aelements(C) COMMA. {
384   A = C;
385   C = NULL;
386 }
387 
388 aelements(A) ::= aelement(B). {
389   A = NULL;
390   if (ctx->ok) {
391     A = array_init(4);
392     array_insert_unique(A, B);
393     B = NULL;
394   }
395   if (B) B->fn->free(B);
396   B = NULL;
397 }
398 
399 aelement(A) ::= expression(B). {
400   A = B;
401   B = NULL;
402 }
403 aelement(A) ::= stringop(B) ARRAY_ASSIGN expression(C). {
404   A = NULL;
405   if (ctx->ok) {
406     buffer_copy_buffer(&C->key, B);
407 
408     A = C;
409     C = NULL;
410   }
411   if (C) C->fn->free(C);
412   C = NULL;
413   buffer_free(B);
414   B = NULL;
415 }
416 
417 eols ::= EOL.
418 eols ::= .
419 
420 globalstart ::= GLOBAL. {
421   data_config *dc;
422   dc = configparser_get_data_config(ctx->srv->config_context, CONST_STR_LEN("global"));
423   force_assert(dc);
424   configparser_push(ctx, dc, 0);
425 }
426 
427 global ::= globalstart LCURLY metalines RCURLY. {
428   force_assert(ctx->current);
429   configparser_pop(ctx);
430   force_assert(ctx->current);
431 }
432 
433 condlines(A) ::= condlines(B) eols ELSE condline(C). {
434   A = NULL;
435   if (ctx->ok) {
436     if (B->context_ndx >= C->context_ndx) {
437       fprintf(stderr, "unreachable else condition\n");
438       ctx->ok = 0;
439     }
440     if (B->cond == CONFIG_COND_ELSE) {
441       fprintf(stderr, "unreachable condition following else catch-all\n");
442       ctx->ok = 0;
443     }
444     C->prev = B;
445     B->next = C;
446     A = C;
447   }
448   B = NULL;
449   C = NULL;
450 }
451 
452 condlines(A) ::= condlines(B) eols ELSE cond_else(C). {
453   A = NULL;
454   if (ctx->ok) {
455     if (B->context_ndx >= C->context_ndx) {
456       fprintf(stderr, "unreachable else condition\n");
457       ctx->ok = 0;
458     }
459     if (B->cond == CONFIG_COND_ELSE) {
460       fprintf(stderr, "unreachable condition following else catch-all\n");
461       ctx->ok = 0;
462     }
463   }
464   if (ctx->ok) {
465     size_t pos;
466     data_config *dc;
467     dc = (data_config *)array_extract_element_klen(ctx->all_configs, CONST_BUF_LEN(&C->key));
468     force_assert(C == dc);
469     buffer_copy_buffer(&C->key, &B->key);
470     C->comp = B->comp;
471     /*buffer_copy_buffer(C->comp_key, B->comp_key);*/
472     /*buffer_copy_buffer(&C->string, &B->string);*/
473     pos = buffer_string_length(&C->key)-buffer_string_length(&B->string)-2;
474     switch(B->cond) {
475     case CONFIG_COND_NE:
476       C->key.ptr[pos] = '='; /* opposite cond */
477       /*buffer_copy_string_len(C->op, CONST_STR_LEN("=="));*/
478       break;
479     case CONFIG_COND_EQ:
480       C->key.ptr[pos] = '!'; /* opposite cond */
481       /*buffer_copy_string_len(C->op, CONST_STR_LEN("!="));*/
482       break;
483     case CONFIG_COND_NOMATCH:
484       C->key.ptr[pos] = '='; /* opposite cond */
485       /*buffer_copy_string_len(C->op, CONST_STR_LEN("=~"));*/
486       break;
487     case CONFIG_COND_MATCH:
488       C->key.ptr[pos] = '!'; /* opposite cond */
489       /*buffer_copy_string_len(C->op, CONST_STR_LEN("!~"));*/
490       break;
491     default: /* should not happen; CONFIG_COND_ELSE checked further above */
492       force_assert(0);
493     }
494 
495     if (NULL == (dc = configparser_get_data_config(ctx->all_configs, CONST_BUF_LEN(&C->key)))) {
496       /* re-insert into ctx->all_configs with new C->key */
497       array_insert_unique(ctx->all_configs, (data_unset *)C);
498       C->prev = B;
499       B->next = C;
500     } else {
501       fprintf(stderr, "unreachable else condition\n");
502       ctx->ok = 0;
503       C->fn->free((data_unset *)C);
504       C = dc;
505     }
506 
507     A = C;
508   }
509   B = NULL;
510   C = NULL;
511 }
512 
513 condlines(A) ::= condline(B). {
514   A = B;
515   B = NULL;
516 }
517 
518 condline(A) ::= context LCURLY metalines RCURLY. {
519   A = NULL;
520   if (ctx->ok) {
521     data_config *cur;
522 
523     cur = ctx->current;
524     configparser_pop(ctx);
525 
526     force_assert(cur && ctx->current);
527 
528     A = cur;
529   }
530 }
531 
532 cond_else(A) ::= context_else LCURLY metalines RCURLY. {
533   A = NULL;
534   if (ctx->ok) {
535     data_config *cur;
536 
537     cur = ctx->current;
538     configparser_pop(ctx);
539 
540     force_assert(cur && ctx->current);
541 
542     A = cur;
543   }
544 }
545 
546 context ::= DOLLAR SRVVARNAME(B) LBRACKET stringop(C) RBRACKET cond(E) expression(D). {
547   data_config *dc;
548   buffer *b = NULL, *rvalue;
549   const char *op = NULL;
550 
551   if (ctx->ok && D->type != TYPE_STRING) {
552     fprintf(stderr, "rvalue must be string");
553     ctx->ok = 0;
554   }
555 
556   if (ctx->ok) {
557     switch(E) {
558     case CONFIG_COND_NE:
559       op = "!=";
560       break;
561     case CONFIG_COND_EQ:
562       op = "==";
563       break;
564     case CONFIG_COND_NOMATCH:
565       op = "!~";
566       break;
567     case CONFIG_COND_MATCH:
568       op = "=~";
569       break;
570     default:
571       force_assert(0);
572       return; /* unreachable */
573     }
574 
575     b = buffer_init();
576     buffer_copy_buffer(b, &ctx->current->key);
577     buffer_append_string_len(b, CONST_STR_LEN("/"));
578     buffer_append_string_buffer(b, B);
579     buffer_append_string_buffer(b, C);
580     buffer_append_string_len(b, op, 2);
581     rvalue = &((data_string*)D)->value;
582     buffer_append_string_buffer(b, rvalue);
583 
584     if (NULL != (dc = configparser_get_data_config(ctx->all_configs, CONST_BUF_LEN(b)))) {
585       configparser_push(ctx, dc, 0);
586     } else {
587       static const struct {
588         comp_key_t comp;
589         char *comp_key;
590         size_t len;
591       } comps[] = {
592         { COMP_SERVER_SOCKET,      CONST_STR_LEN("SERVER[\"socket\"]"   ) },
593         { COMP_HTTP_URL,           CONST_STR_LEN("HTTP[\"url\"]"        ) },
594         { COMP_HTTP_HOST,          CONST_STR_LEN("HTTP[\"host\"]"       ) },
595         { COMP_HTTP_REQUEST_HEADER,CONST_STR_LEN("HTTP[\"referer\"]"    ) },
596         { COMP_HTTP_USER_AGENT,    CONST_STR_LEN("HTTP[\"useragent\"]"  ) },
597         { COMP_HTTP_REQUEST_HEADER,CONST_STR_LEN("HTTP[\"user-agent\"]" ) },
598         { COMP_HTTP_LANGUAGE,      CONST_STR_LEN("HTTP[\"language\"]"   ) },
599         { COMP_HTTP_REQUEST_HEADER,CONST_STR_LEN("HTTP[\"cookie\"]"     ) },
600         { COMP_HTTP_REMOTE_IP,     CONST_STR_LEN("HTTP[\"remoteip\"]"   ) },
601         { COMP_HTTP_REMOTE_IP,     CONST_STR_LEN("HTTP[\"remote-ip\"]"   ) },
602         { COMP_HTTP_QUERY_STRING,  CONST_STR_LEN("HTTP[\"querystring\"]") },
603         { COMP_HTTP_QUERY_STRING,  CONST_STR_LEN("HTTP[\"query-string\"]") },
604         { COMP_HTTP_REQUEST_METHOD, CONST_STR_LEN("HTTP[\"request-method\"]") },
605         { COMP_HTTP_SCHEME,        CONST_STR_LEN("HTTP[\"scheme\"]"     ) },
606         { COMP_UNSET, NULL, 0 },
607       };
608       size_t i;
609 
610       dc = data_config_init();
611 
612       buffer_copy_buffer(&dc->key, b);
613       dc->op = op;
614       buffer_copy_buffer(dc->comp_tag, C);
615       buffer_copy_buffer(dc->comp_key, B);
616       buffer_append_string_len(dc->comp_key, CONST_STR_LEN("[\""));
617       buffer_append_string_buffer(dc->comp_key, C);
618       buffer_append_string_len(dc->comp_key, CONST_STR_LEN("\"]"));
619       dc->cond = E;
620 
621       for (i = 0; comps[i].comp_key; i ++) {
622         if (buffer_is_equal_string(
623               dc->comp_key, comps[i].comp_key, comps[i].len)) {
624           dc->comp = comps[i].comp;
625           break;
626         }
627       }
628       if (COMP_UNSET == dc->comp) {
629         if (buffer_is_equal_string(B, CONST_STR_LEN("REQUEST_HEADER"))) {
630           dc->comp = COMP_HTTP_REQUEST_HEADER;
631         }
632         else {
633           fprintf(stderr, "error comp_key %s", dc->comp_key->ptr);
634           ctx->ok = 0;
635         }
636       }
637       else if (COMP_HTTP_LANGUAGE == dc->comp) {
638         dc->comp = COMP_HTTP_REQUEST_HEADER;
639         buffer_copy_string_len(dc->comp_tag, CONST_STR_LEN("Accept-Language"));
640       }
641       else if (COMP_HTTP_USER_AGENT == dc->comp) {
642         dc->comp = COMP_HTTP_REQUEST_HEADER;
643         buffer_copy_string_len(dc->comp_tag, CONST_STR_LEN("User-Agent"));
644       }
645       else if (COMP_HTTP_REMOTE_IP == dc->comp
646                && (dc->cond == CONFIG_COND_EQ || dc->cond == CONFIG_COND_NE)) {
647         char * const slash = strchr(rvalue->ptr, '/'); /* CIDR mask */
648         char * const colon = strchr(rvalue->ptr, ':'); /* IPv6 */
649         if (NULL != slash && slash == rvalue->ptr){/*(skip AF_UNIX /path/file)*/
650         }
651         else if (NULL != slash) {
652           char *nptr;
653           const unsigned long nm_bits = strtoul(slash + 1, &nptr, 10);
654           if (*nptr || 0 == nm_bits || nm_bits > (NULL != colon ? 128 : 32)) {
655             /*(also rejects (slash+1 == nptr) which results in nm_bits = 0)*/
656             fprintf(stderr, "invalid or missing netmask: %s\n", rvalue->ptr);
657             ctx->ok = 0;
658           }
659           else {
660             int rc;
661             buffer_string_set_length(rvalue, (size_t)(slash - rvalue->ptr)); /*(truncate)*/
662             rc = (NULL == colon)
663               ? http_request_host_normalize(rvalue, 0)
664               : configparser_remoteip_normalize_compat(rvalue);
665             buffer_append_string_len(rvalue, CONST_STR_LEN("/"));
666             buffer_append_int(rvalue, (int)nm_bits);
667             if (0 != rc) {
668               fprintf(stderr, "invalid IP addr: %s\n", rvalue->ptr);
669               ctx->ok = 0;
670             }
671           }
672         }
673         else {
674           int rc = (NULL == colon)
675             ? http_request_host_normalize(rvalue, 0)
676             : configparser_remoteip_normalize_compat(rvalue);
677           if (0 != rc) {
678             fprintf(stderr, "invalid IP addr: %s\n", rvalue->ptr);
679             ctx->ok = 0;
680           }
681         }
682       }
683       else if (COMP_SERVER_SOCKET == dc->comp) {
684         /*(redundant with parsing in network.c; not actually required here)*/
685         if (rvalue->ptr[0] != ':' /*(network.c special-cases ":" and "[]")*/
686             && !(rvalue->ptr[0] == '[' && rvalue->ptr[1] == ']')) {
687           if (http_request_host_normalize(rvalue, 0)) {
688             fprintf(stderr, "invalid IP addr: %s\n", rvalue->ptr);
689             ctx->ok = 0;
690           }
691         }
692       }
693       else if (COMP_HTTP_HOST == dc->comp) {
694         if (dc->cond == CONFIG_COND_EQ || dc->cond == CONFIG_COND_NE) {
695           if (http_request_host_normalize(rvalue, 0)) {
696             fprintf(stderr, "invalid IP addr: %s\n", rvalue->ptr);
697             ctx->ok = 0;
698           }
699         }
700       }
701 
702       if (COMP_HTTP_REQUEST_HEADER == dc->comp) {
703         dc->ext = http_header_hkey_get(CONST_BUF_LEN(dc->comp_tag));
704       }
705 
706       buffer_copy_buffer(&dc->string, rvalue);
707 
708       if (ctx->ok) switch(E) {
709       case CONFIG_COND_NE:
710       case CONFIG_COND_EQ:
711         break;
712       case CONFIG_COND_NOMATCH:
713       case CONFIG_COND_MATCH: {
714         if (!data_config_pcre_compile(dc)) {
715           ctx->ok = 0;
716         }
717         break;
718       }
719 
720       default:
721         fprintf(stderr, "unknown condition for $%s[%s]\n",
722                         B->ptr, C->ptr);
723         ctx->ok = 0;
724         break;
725       }
726 
727       if (ctx->ok) {
728         configparser_push(ctx, dc, 1);
729       } else {
730         dc->fn->free((data_unset*) dc);
731       }
732     }
733   }
734 
735   buffer_free(b);
736   buffer_free(B);
737   B = NULL;
738   buffer_free(C);
739   C = NULL;
740   if (D) D->fn->free(D);
741   D = NULL;
742 }
743 
744 context_else ::= . {
745   if (ctx->ok) {
746     data_config *dc = data_config_init();
747     buffer_copy_buffer(&dc->key, &ctx->current->key);
748     buffer_append_string_len(&dc->key, CONST_STR_LEN("/"));
749     buffer_append_string_len(&dc->key, CONST_STR_LEN("else_tmp_token"));
750     dc->cond = CONFIG_COND_ELSE;
751     configparser_push(ctx, dc, 1);
752   }
753 }
754 
755 cond(A) ::= EQ. {
756   A = CONFIG_COND_EQ;
757 }
758 cond(A) ::= MATCH. {
759   A = CONFIG_COND_MATCH;
760 }
761 cond(A) ::= NE. {
762   A = CONFIG_COND_NE;
763 }
764 cond(A) ::= NOMATCH. {
765   A = CONFIG_COND_NOMATCH;
766 }
767 
768 stringop(A) ::= expression(B). {
769   A = NULL;
770   if (ctx->ok) {
771     if (B->type == TYPE_STRING) {
772       A = buffer_init_buffer(&((data_string*)B)->value);
773     } else if (B->type == TYPE_INTEGER) {
774       A = buffer_init();
775       buffer_append_int(A, ((data_integer *)B)->value);
776     } else {
777       fprintf(stderr, "operand must be string");
778       ctx->ok = 0;
779     }
780   }
781   if (B) B->fn->free(B);
782   B = NULL;
783 }
784 
785 include ::= INCLUDE stringop(A). {
786   if (ctx->ok) {
787     if (0 != config_parse_file(ctx->srv, ctx, A->ptr)) {
788       ctx->ok = 0;
789     }
790   }
791   buffer_free(A);
792   A = NULL;
793 }
794 
795 include_shell ::= INCLUDE_SHELL stringop(A). {
796   if (ctx->ok) {
797     if (0 != config_parse_cmd(ctx->srv, ctx, A->ptr)) {
798       ctx->ok = 0;
799     }
800   }
801   buffer_free(A);
802   A = NULL;
803 }
804