4 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
5 * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
6 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
7 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
8 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
9 * Copyright (c) 2001 Markus Friedl. All rights reserved.
10 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
11 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
38 TAILQ_ENTRY(file) entry;
49 struct file *pushfile(const char *, int);
53 void yyerror(const char *, ...)
54 __attribute__((__format__ (printf, 1, 2)))
55 __attribute__((__nonnull__ (1)));
56 void yywarn(const char *, ...)
57 __attribute__((__format__ (printf, 1, 2)))
58 __attribute__((__nonnull__ (1)));
59 int kw_cmp(const void *, const void *);
71 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
73 TAILQ_ENTRY(sym) entry;
80 int symset(const char *, const char *, int);
81 char *symget(const char *);
83 struct vhost *new_vhost(void);
84 struct location *new_location(void);
85 struct proxy *new_proxy(void);
86 char *ensure_absolute_path(char*);
87 int check_block_code(int);
88 char *check_block_fmt(char*);
89 int check_strip_no(int);
90 int check_port_num(int);
91 int check_prefork_num(int);
92 void advance_loc(void);
93 void advance_proxy(void);
94 void parsehp(char *, char **, const char **, const char *);
95 int fastcgi_conf(const char *, const char *, char *);
96 void add_param(char *, char *);
97 int getservice(const char *);
99 static struct vhost *host;
100 static struct location *loc;
101 static struct proxy *proxy;
102 static char *current_media;
116 /* %define parse.error verbose */
120 %token CA CERT CHROOT CLIENT
122 %token FASTCGI FOR_HOST
123 %token INCLUDE INDEX IPV6
125 %token LANG LOCATION LOG
127 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
128 %token RELAY_TO REQUIRE RETURN ROOT
129 %token SERVER SNI SPAWN STRIP
130 %token TCP TOEXT TYPE TYPES
136 %token <v.string> STRING
137 %token <v.number> NUM
139 %type <v.number> bool proxy_port
140 %type <v.string> string numberstring
151 | conf error '\n' { file->errors++; }
154 include : INCLUDE STRING {
157 if ((nfile = pushfile($2, 0)) == NULL) {
158 yyerror("failed to include file %s", $2);
169 bool : ON { $$ = 1; }
173 string : string STRING {
174 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
177 yyerror("string: asprintf: %s", strerror(errno));
188 if (asprintf(&s, "%d", $1) == -1) {
189 yyerror("asprintf: number");
197 varset : STRING '=' string {
200 if (isspace((unsigned char)*s)) {
201 yyerror("macro name cannot contain "
214 option : CHROOT string {
215 if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
217 yyerror("chroot path too long");
220 | IPV6 bool { conf.ipv6 = $2; }
221 | PORT NUM { conf.port = check_port_num($2); }
222 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
224 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
225 yyerror("invalid protocols string \"%s\"", $2);
229 if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
231 yyerror("user name too long");
236 vhost : SERVER string {
238 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
240 loc = new_location();
241 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
243 TAILQ_INIT(&host->proxies);
245 (void) strlcpy(loc->match, "*", sizeof(loc->match));
246 (void) strlcpy(host->domain, $2, sizeof(host->domain));
248 if (strstr($2, "xn--") != NULL) {
249 yywarn("\"%s\" looks like punycode: you "
250 "should use the decoded hostname", $2);
254 } '{' optnl servbody '}' {
255 if (*host->cert == '\0' || *host->key == '\0')
256 yyerror("invalid vhost definition: %s", $2);
258 | error '}' { yyerror("bad server directive"); }
261 servbody : /* empty */
262 | servbody servopt optnl
263 | servbody location optnl
264 | servbody proxy optnl
267 servopt : ALIAS string {
270 a = xcalloc(1, sizeof(*a));
271 (void) strlcpy(a->alias, $2, sizeof(a->alias));
273 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
276 ensure_absolute_path($2);
277 (void) strlcpy(host->cert, $2, sizeof(host->cert));
281 ensure_absolute_path($2);
282 (void) strlcpy(host->key, $2, sizeof(host->key));
286 ensure_absolute_path($2);
287 (void) strlcpy(host->ocsp, $2, sizeof(host->ocsp));
290 | PARAM string '=' string {
296 proxy : PROXY { advance_proxy(); }
297 proxy_matches '{' optnl proxy_opts '}' {
298 if (*proxy->host == '\0')
299 yyerror("invalid proxy block: missing `relay-to' option");
301 if ((proxy->cert == NULL && proxy->key != NULL) ||
302 (proxy->cert != NULL && proxy->key == NULL))
303 yyerror("invalid proxy block: missing cert or key");
307 proxy_matches : /* empty */
308 | proxy_matches proxy_match
311 proxy_port : /* empty */ { $$ = 1965; }
313 if (($$ = getservice($2)) == -1)
314 yyerror("invalid port number %s", $2);
317 | PORT NUM { $$ = $2; }
320 proxy_match : PROTO string {
321 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
324 | FOR_HOST string proxy_port {
325 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
326 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
332 proxy_opts : /* empty */
333 | proxy_opts proxy_opt optnl
336 proxy_opt : CERT string {
337 tls_unload_file(proxy->cert, proxy->certlen);
338 ensure_absolute_path($2);
339 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
340 if (proxy->cert == NULL)
341 yyerror("can't load cert %s", $2);
345 tls_unload_file(proxy->key, proxy->keylen);
346 ensure_absolute_path($2);
347 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
348 if (proxy->key == NULL)
349 yyerror("can't load key %s", $2);
353 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
354 yyerror("invalid protocols string \"%s\"", $2);
357 | RELAY_TO string proxy_port {
358 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
359 (void) snprintf(proxy->port, sizeof(proxy->port),
363 | REQUIRE CLIENT CA string {
364 ensure_absolute_path($4);
365 if ((proxy->reqca = load_ca($4)) == NULL)
366 yyerror("couldn't load ca cert: %s", $4);
370 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
377 proxy->noverifyname = !$2;
381 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
382 /* drop the starting '/' if any */
384 memmove($3, $3+1, strlen($3));
385 (void) strlcpy(loc->match, $3, sizeof(loc->match));
391 locopts : /* empty */
392 | locopts locopt optnl
395 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
396 | BLOCK RETURN NUM string {
398 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
399 loc->block_code = check_block_code($3);
403 (void) strlcpy(loc->block_fmt, "temporary failure",
404 sizeof(loc->block_fmt));
405 loc->block_code = check_block_code($3);
406 if ($3 >= 30 && $3 < 40)
407 yyerror("missing `meta' for block return %d", $3);
410 (void) strlcpy(loc->block_fmt, "temporary failure",
411 sizeof(loc->block_fmt));
412 loc->block_code = 40;
414 | DEFAULT TYPE string {
415 (void) strlcpy(loc->default_mime, $3,
416 sizeof(loc->default_mime));
421 (void) strlcpy(loc->index, $2, sizeof(loc->index));
425 (void) strlcpy(loc->lang, $2,
429 | LOG bool { loc->disable_log = !$2; }
430 | REQUIRE CLIENT CA string {
431 ensure_absolute_path($4);
432 if ((loc->reqca = load_ca($4)) == NULL)
433 yyerror("couldn't load ca cert: %s", $4);
437 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
440 | STRIP NUM { loc->strip = check_strip_no($2); }
443 fastcgi : SPAWN string {
444 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
448 loc->fcgi = fastcgi_conf($1, NULL, NULL);
451 | TCP string PORT NUM {
453 if (asprintf(&c, "%d", $4) == -1)
455 loc->fcgi = fastcgi_conf($2, c, NULL);
459 loc->fcgi = fastcgi_conf($2, "9000", NULL);
462 | TCP string PORT string {
463 loc->fcgi = fastcgi_conf($2, $4, NULL);
469 types : TYPES '{' optnl mediaopts_l '}' ;
471 mediaopts_l : mediaopts_l mediaoptsl nl
475 mediaoptsl : STRING {
478 } medianames_l optsemicolon
482 medianames_l : medianames_l medianamesl
486 medianamesl : numberstring {
487 if (add_mime(&conf.mime, current_media, $1) == -1)
496 optnl : '\n' optnl /* zero or more newlines */
497 | ';' optnl /* semicolons too */
507 static const struct keyword {
511 /* these MUST be sorted */
519 {"default", DEFAULT},
520 {"fastcgi", FASTCGI},
521 {"for-host", FOR_HOST},
522 {"include", INCLUDE},
527 {"location", LOCATION},
534 {"prefork", PREFORK},
536 {"protocols", PROTOCOLS},
538 {"relay-to", RELAY_TO},
539 {"require", REQUIRE},
550 {"use-tls", USE_TLS},
552 {"verifyname", VERIFYNAME},
556 yyerror(const char *msg, ...)
563 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
564 vfprintf(stderr, msg, ap);
565 fprintf(stderr, "\n");
570 yywarn(const char *msg, ...)
575 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
576 vfprintf(stderr, msg, ap);
577 fprintf(stderr, "\n");
582 kw_cmp(const void *k, const void *e)
584 return strcmp(k, ((struct keyword *)e)->word);
590 const struct keyword *p;
592 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
593 sizeof(keywords[0]), kw_cmp);
601 #define START_EXPAND 1
602 #define DONE_EXPAND 2
604 static int expanding;
612 if (file->ungetpos > 0)
613 c = file->ungetbuf[--file->ungetpos];
615 c = getc(file->stream);
617 if (c == START_EXPAND)
619 else if (c == DONE_EXPAND)
633 if ((c = igetc()) == EOF) {
634 yyerror("reached end of file while parsing "
636 if (file == topfile || popfile() == EOF)
643 while ((c = igetc()) == '\\') {
649 yylval.lineno = file->lineno;
655 * Fake EOL when hit EOF for the first time. This gets line
656 * count right if last line in included file is syntactically
657 * invalid and has no newline.
659 if (file->eof_reached == 0) {
660 file->eof_reached = 1;
664 if (file == topfile || popfile() == EOF)
678 if (file->ungetpos >= file->ungetsize) {
679 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
683 file->ungetsize *= 2;
685 file->ungetbuf[file->ungetpos++] = c;
693 /* Skip to either EOF or the first real EOL. */
716 while ((c = lgetc(0)) == ' ' || c == '\t')
719 yylval.lineno = file->lineno;
721 while ((c = lgetc(0)) != '\n' && c != EOF)
723 if (c == '$' && !expanding) {
725 if ((c = lgetc(0)) == EOF)
727 if (p + 1 >= buf + sizeof(buf) -1) {
728 yyerror("string too long");
731 if (isalnum(c) || c == '_') {
741 yyerror("macro `%s' not defined", buf);
744 yylval.v.string = xstrdup(val);
747 if (c == '@' && !expanding) {
749 if ((c = lgetc(0)) == EOF)
752 if (p + 1 >= buf + sizeof(buf) - 1) {
753 yyerror("string too long");
756 if (isalnum(c) || c == '_') {
766 yyerror("macro '%s' not defined", buf);
769 p = val + strlen(val) - 1;
770 lungetc(DONE_EXPAND);
775 lungetc(START_EXPAND);
784 if ((c = lgetc(quotec)) == EOF)
789 } else if (c == '\\') {
790 if ((next = lgetc(quotec)) == EOF)
792 if (next == quotec || next == ' ' ||
795 else if (next == '\n') {
800 } else if (c == quotec) {
803 } else if (c == '\0') {
804 yyerror("invalid syntax");
807 if (p + 1 >= buf + sizeof(buf) - 1) {
808 yyerror("string too long");
813 yylval.v.string = strdup(buf);
814 if (yylval.v.string == NULL)
815 err(1, "yylex: strdup");
819 #define allowed_to_end_number(x) \
820 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
822 if (c == '-' || isdigit(c)) {
825 if ((size_t)(p-buf) >= sizeof(buf)) {
826 yyerror("string too long");
829 } while ((c = lgetc(0)) != EOF && isdigit(c));
831 if (p == buf + 1 && buf[0] == '-')
833 if (c == EOF || allowed_to_end_number(c)) {
834 const char *errstr = NULL;
837 yylval.v.number = strtonum(buf, LLONG_MIN,
840 yyerror("\"%s\" invalid number: %s",
855 #define allowed_in_string(x) \
856 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
857 x != '{' && x != '}' && \
858 x != '!' && x != '=' && x != '#' && \
859 x != ',' && x != ';'))
861 if (isalnum(c) || c == ':' || c == '_') {
864 if ((size_t)(p-buf) >= sizeof(buf)) {
865 yyerror("string too long");
868 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
871 if ((token = lookup(buf)) == STRING)
872 yylval.v.string = xstrdup(buf);
876 yylval.lineno = file->lineno;
885 pushfile(const char *name, int secret)
889 nfile = xcalloc(1, sizeof(*nfile));
890 nfile->name = xstrdup(name);
891 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
892 log_warn(NULL, "can't open %s: %s", nfile->name,
898 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
899 nfile->ungetsize = 16;
900 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
901 TAILQ_INSERT_TAIL(&files, nfile, entry);
910 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
911 prev->errors += file->errors;
913 TAILQ_REMOVE(&files, file, entry);
914 fclose(file->stream);
916 free(file->ungetbuf);
919 return file ? 0 : EOF;
923 parse_conf(const char *filename)
925 struct sym *sym, *next;
927 file = pushfile(filename, 0);
933 errors = file->errors;
936 /* Free macros and check which have not been used. */
937 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
938 /* TODO: warn if !sym->used */
942 TAILQ_REMOVE(&symhead, sym, entry);
955 /* struct location *l; */
956 /* struct envlist *e; */
957 /* struct alist *a; */
959 if (*conf.chroot != '\0')
960 printf("chroot \"%s\"\n", conf.chroot);
961 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
962 /* XXX: defined mimes? */
963 printf("port %d\n", conf.port);
964 printf("prefork %d\n", conf.prefork);
965 /* XXX: protocols? */
966 if (*conf.user != '\0')
967 printf("user \"%s\"\n", conf.user);
969 TAILQ_FOREACH(h, &hosts, vhosts) {
970 printf("\nserver \"%s\" {\n", h->domain);
971 printf(" cert \"%s\"\n", h->cert);
972 printf(" key \"%s\"\n", h->key);
973 /* TODO: print locations... */
979 symset(const char *name, const char *val, int persist)
983 TAILQ_FOREACH(sym, &symhead, entry) {
984 if (!strcmp(name, sym->name))
994 TAILQ_REMOVE(&symhead, sym, entry);
999 sym = xcalloc(1, sizeof(*sym));
1000 sym->name = xstrdup(name);
1001 sym->val = xstrdup(val);
1003 sym->persist = persist;
1005 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1010 cmdline_symset(char *s)
1015 if ((val = strrchr(s, '=')) == NULL)
1017 sym = xcalloc(1, val - s + 1);
1018 memcpy(sym, s, val - s);
1019 ret = symset(sym, val + 1, 1);
1025 symget(const char *nam)
1029 TAILQ_FOREACH(sym, &symhead, entry) {
1030 if (strcmp(nam, sym->name) == 0) {
1043 h = xcalloc(1, sizeof(*h));
1044 TAILQ_INIT(&h->locations);
1045 TAILQ_INIT(&h->params);
1046 TAILQ_INIT(&h->aliases);
1047 TAILQ_INIT(&h->proxies);
1056 l = xcalloc(1, sizeof(*l));
1067 conf.can_open_sockets = 1;
1069 p = xcalloc(1, sizeof(*p));
1070 p->protocols = TLS_PROTOCOLS_DEFAULT;
1075 ensure_absolute_path(char *path)
1077 if (path == NULL || *path != '/')
1078 yyerror("not an absolute path: %s", path);
1083 check_block_code(int n)
1085 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1086 yyerror("invalid block code %d", n);
1091 check_block_fmt(char *fmt)
1095 for (s = fmt; *s; ++s) {
1106 yyerror("invalid format specifier %%%c", *s);
1114 check_strip_no(int n)
1117 yyerror("invalid strip number %d", n);
1122 check_port_num(int n)
1124 if (n <= 0 || n >= UINT16_MAX)
1125 yyerror("port number is %s: %d",
1126 n <= 0 ? "too small" : "too large",
1132 check_prefork_num(int n)
1134 if (n <= 0 || n >= PROC_MAX)
1135 yyerror("invalid prefork number %d", n);
1142 loc = new_location();
1143 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1149 proxy = new_proxy();
1150 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1154 parsehp(char *str, char **host, const char **port, const char *def)
1161 if ((at = strchr(str, ':')) != NULL) {
1167 strtonum(*port, 1, UINT16_MAX, &errstr);
1169 yyerror("port is %s: %s", errstr, *port);
1173 fastcgi_conf(const char *path, const char *port, char *prog)
1178 conf.can_open_sockets = 1;
1180 for (i = 0; i < FCGI_MAX; ++i) {
1183 if (*f->path == '\0') {
1185 (void) strlcpy(f->path, path, sizeof(f->path));
1186 (void) strlcpy(f->port, port, sizeof(f->port));
1190 if (!strcmp(f->path, path) &&
1191 ((port == NULL && *f->port == '\0') ||
1192 !strcmp(f->port, port)))
1196 yyerror("too much `fastcgi' rules defined.");
1201 add_param(char *name, char *val)
1204 struct envhead *h = &host->params;
1206 e = xcalloc(1, sizeof(*e));
1207 (void) strlcpy(e->name, name, sizeof(e->name));
1208 (void) strlcpy(e->value, val, sizeof(e->value));
1209 TAILQ_INSERT_TAIL(h, e, envs);
1213 getservice(const char *n)
1219 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1221 s = getservbyname(n, "tcp");
1223 s = getservbyname(n, "udp");
1226 return (ntohs(s->s_port));
1229 return ((unsigned short)llval);