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 *);
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 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); }
444 loc->fcgi = fastcgi_conf($1, NULL);
447 | TCP string PORT NUM {
449 if (asprintf(&c, "%d", $4) == -1)
451 loc->fcgi = fastcgi_conf($2, c);
455 loc->fcgi = fastcgi_conf($2, "9000");
458 | TCP string PORT string {
459 loc->fcgi = fastcgi_conf($2, $4);
465 types : TYPES '{' optnl mediaopts_l '}' ;
467 mediaopts_l : mediaopts_l mediaoptsl nl
471 mediaoptsl : STRING {
474 } medianames_l optsemicolon
478 medianames_l : medianames_l medianamesl
482 medianamesl : numberstring {
483 if (add_mime(&conf.mime, current_media, $1) == -1)
492 optnl : '\n' optnl /* zero or more newlines */
493 | ';' optnl /* semicolons too */
503 static const struct keyword {
507 /* these MUST be sorted */
515 {"default", DEFAULT},
516 {"fastcgi", FASTCGI},
517 {"for-host", FOR_HOST},
518 {"include", INCLUDE},
523 {"location", LOCATION},
530 {"prefork", PREFORK},
532 {"protocols", PROTOCOLS},
534 {"relay-to", RELAY_TO},
535 {"require", REQUIRE},
545 {"use-tls", USE_TLS},
547 {"verifyname", VERIFYNAME},
551 yyerror(const char *msg, ...)
558 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
559 vfprintf(stderr, msg, ap);
560 fprintf(stderr, "\n");
565 yywarn(const char *msg, ...)
570 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
571 vfprintf(stderr, msg, ap);
572 fprintf(stderr, "\n");
577 kw_cmp(const void *k, const void *e)
579 return strcmp(k, ((struct keyword *)e)->word);
585 const struct keyword *p;
587 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
588 sizeof(keywords[0]), kw_cmp);
596 #define START_EXPAND 1
597 #define DONE_EXPAND 2
599 static int expanding;
607 if (file->ungetpos > 0)
608 c = file->ungetbuf[--file->ungetpos];
610 c = getc(file->stream);
612 if (c == START_EXPAND)
614 else if (c == DONE_EXPAND)
628 if ((c = igetc()) == EOF) {
629 yyerror("reached end of file while parsing "
631 if (file == topfile || popfile() == EOF)
638 while ((c = igetc()) == '\\') {
644 yylval.lineno = file->lineno;
650 * Fake EOL when hit EOF for the first time. This gets line
651 * count right if last line in included file is syntactically
652 * invalid and has no newline.
654 if (file->eof_reached == 0) {
655 file->eof_reached = 1;
659 if (file == topfile || popfile() == EOF)
673 if (file->ungetpos >= file->ungetsize) {
674 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
678 file->ungetsize *= 2;
680 file->ungetbuf[file->ungetpos++] = c;
688 /* Skip to either EOF or the first real EOL. */
711 while ((c = lgetc(0)) == ' ' || c == '\t')
714 yylval.lineno = file->lineno;
716 while ((c = lgetc(0)) != '\n' && c != EOF)
718 if (c == '$' && !expanding) {
720 if ((c = lgetc(0)) == EOF)
722 if (p + 1 >= buf + sizeof(buf) -1) {
723 yyerror("string too long");
726 if (isalnum(c) || c == '_') {
736 yyerror("macro `%s' not defined", buf);
739 yylval.v.string = xstrdup(val);
742 if (c == '@' && !expanding) {
744 if ((c = lgetc(0)) == EOF)
747 if (p + 1 >= buf + sizeof(buf) - 1) {
748 yyerror("string too long");
751 if (isalnum(c) || c == '_') {
761 yyerror("macro '%s' not defined", buf);
764 p = val + strlen(val) - 1;
765 lungetc(DONE_EXPAND);
770 lungetc(START_EXPAND);
779 if ((c = lgetc(quotec)) == EOF)
784 } else if (c == '\\') {
785 if ((next = lgetc(quotec)) == EOF)
787 if (next == quotec || next == ' ' ||
790 else if (next == '\n') {
795 } else if (c == quotec) {
798 } else if (c == '\0') {
799 yyerror("invalid syntax");
802 if (p + 1 >= buf + sizeof(buf) - 1) {
803 yyerror("string too long");
808 yylval.v.string = strdup(buf);
809 if (yylval.v.string == NULL)
810 err(1, "yylex: strdup");
814 #define allowed_to_end_number(x) \
815 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
817 if (c == '-' || isdigit(c)) {
820 if ((size_t)(p-buf) >= sizeof(buf)) {
821 yyerror("string too long");
824 } while ((c = lgetc(0)) != EOF && isdigit(c));
826 if (p == buf + 1 && buf[0] == '-')
828 if (c == EOF || allowed_to_end_number(c)) {
829 const char *errstr = NULL;
832 yylval.v.number = strtonum(buf, LLONG_MIN,
835 yyerror("\"%s\" invalid number: %s",
850 #define allowed_in_string(x) \
851 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
852 x != '{' && x != '}' && \
853 x != '!' && x != '=' && x != '#' && \
854 x != ',' && x != ';'))
856 if (isalnum(c) || c == ':' || c == '_') {
859 if ((size_t)(p-buf) >= sizeof(buf)) {
860 yyerror("string too long");
863 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
866 if ((token = lookup(buf)) == STRING)
867 yylval.v.string = xstrdup(buf);
871 yylval.lineno = file->lineno;
880 pushfile(const char *name, int secret)
884 nfile = xcalloc(1, sizeof(*nfile));
885 nfile->name = xstrdup(name);
886 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
887 log_warn(NULL, "can't open %s: %s", nfile->name,
893 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
894 nfile->ungetsize = 16;
895 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
896 TAILQ_INSERT_TAIL(&files, nfile, entry);
905 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
906 prev->errors += file->errors;
908 TAILQ_REMOVE(&files, file, entry);
909 fclose(file->stream);
911 free(file->ungetbuf);
914 return file ? 0 : EOF;
918 parse_conf(const char *filename)
920 struct sym *sym, *next;
922 file = pushfile(filename, 0);
928 errors = file->errors;
931 /* Free macros and check which have not been used. */
932 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
933 /* TODO: warn if !sym->used */
937 TAILQ_REMOVE(&symhead, sym, entry);
950 /* struct location *l; */
951 /* struct envlist *e; */
952 /* struct alist *a; */
954 if (*conf.chroot != '\0')
955 printf("chroot \"%s\"\n", conf.chroot);
956 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
957 /* XXX: defined mimes? */
958 printf("port %d\n", conf.port);
959 printf("prefork %d\n", conf.prefork);
960 /* XXX: protocols? */
961 if (*conf.user != '\0')
962 printf("user \"%s\"\n", conf.user);
964 TAILQ_FOREACH(h, &hosts, vhosts) {
965 printf("\nserver \"%s\" {\n", h->domain);
966 printf(" cert \"%s\"\n", h->cert);
967 printf(" key \"%s\"\n", h->key);
968 /* TODO: print locations... */
974 symset(const char *name, const char *val, int persist)
978 TAILQ_FOREACH(sym, &symhead, entry) {
979 if (!strcmp(name, sym->name))
989 TAILQ_REMOVE(&symhead, sym, entry);
994 sym = xcalloc(1, sizeof(*sym));
995 sym->name = xstrdup(name);
996 sym->val = xstrdup(val);
998 sym->persist = persist;
1000 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1005 cmdline_symset(char *s)
1010 if ((val = strrchr(s, '=')) == NULL)
1012 sym = xcalloc(1, val - s + 1);
1013 memcpy(sym, s, val - s);
1014 ret = symset(sym, val + 1, 1);
1020 symget(const char *nam)
1024 TAILQ_FOREACH(sym, &symhead, entry) {
1025 if (strcmp(nam, sym->name) == 0) {
1038 h = xcalloc(1, sizeof(*h));
1039 TAILQ_INIT(&h->locations);
1040 TAILQ_INIT(&h->params);
1041 TAILQ_INIT(&h->aliases);
1042 TAILQ_INIT(&h->proxies);
1051 l = xcalloc(1, sizeof(*l));
1062 conf.can_open_sockets = 1;
1064 p = xcalloc(1, sizeof(*p));
1065 p->protocols = TLS_PROTOCOLS_DEFAULT;
1070 ensure_absolute_path(char *path)
1072 if (path == NULL || *path != '/')
1073 yyerror("not an absolute path: %s", path);
1078 check_block_code(int n)
1080 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1081 yyerror("invalid block code %d", n);
1086 check_block_fmt(char *fmt)
1090 for (s = fmt; *s; ++s) {
1101 yyerror("invalid format specifier %%%c", *s);
1109 check_strip_no(int n)
1112 yyerror("invalid strip number %d", n);
1117 check_port_num(int n)
1119 if (n <= 0 || n >= UINT16_MAX)
1120 yyerror("port number is %s: %d",
1121 n <= 0 ? "too small" : "too large",
1127 check_prefork_num(int n)
1129 if (n <= 0 || n >= PROC_MAX)
1130 yyerror("invalid prefork number %d", n);
1137 loc = new_location();
1138 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1144 proxy = new_proxy();
1145 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1149 parsehp(char *str, char **host, const char **port, const char *def)
1156 if ((at = strchr(str, ':')) != NULL) {
1162 strtonum(*port, 1, UINT16_MAX, &errstr);
1164 yyerror("port is %s: %s", errstr, *port);
1168 fastcgi_conf(const char *path, const char *port)
1173 conf.can_open_sockets = 1;
1175 for (i = 0; i < FCGI_MAX; ++i) {
1178 if (*f->path == '\0') {
1180 (void) strlcpy(f->path, path, sizeof(f->path));
1181 (void) strlcpy(f->port, port, sizeof(f->port));
1185 if (!strcmp(f->path, path) &&
1186 ((port == NULL && *f->port == '\0') ||
1187 !strcmp(f->port, port)))
1191 yyerror("too much `fastcgi' rules defined.");
1196 add_param(char *name, char *val)
1199 struct envhead *h = &host->params;
1201 e = xcalloc(1, sizeof(*e));
1202 (void) strlcpy(e->name, name, sizeof(e->name));
1203 (void) strlcpy(e->value, val, sizeof(e->value));
1204 TAILQ_INSERT_TAIL(h, e, envs);
1208 getservice(const char *n)
1214 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1216 s = getservbyname(n, "tcp");
1218 s = getservbyname(n, "udp");
1221 return (ntohs(s->s_port));
1224 return ((unsigned short)llval);