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.
38 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
40 TAILQ_ENTRY(file) entry;
51 struct file *pushfile(const char *, int);
55 void yyerror(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 void yywarn(const char *, ...)
59 __attribute__((__format__ (printf, 1, 2)))
60 __attribute__((__nonnull__ (1)));
61 int kw_cmp(const void *, const void *);
73 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 TAILQ_ENTRY(sym) entry;
82 int symset(const char *, const char *, int);
83 char *symget(const char *);
85 struct vhost *new_vhost(void);
86 struct location *new_location(void);
87 struct proxy *new_proxy(void);
88 char *ensure_absolute_path(char*);
89 int check_block_code(int);
90 char *check_block_fmt(char*);
91 int check_strip_no(int);
92 int check_port_num(int);
93 int check_prefork_num(int);
94 void advance_loc(void);
95 void advance_proxy(void);
96 void parsehp(char *, char **, const char **, const char *);
97 int fastcgi_conf(const char *, const char *);
98 void add_param(char *, char *);
99 int getservice(const char *);
101 static struct vhost *host;
102 static struct location *loc;
103 static struct proxy *proxy;
104 static char *current_media;
118 /* %define parse.error verbose */
122 %token CA CERT CHROOT CLIENT
124 %token FASTCGI FOR_HOST
125 %token INCLUDE INDEX IPV6
127 %token LANG LOCATION LOG
129 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
130 %token RELAY_TO REQUIRE RETURN ROOT
131 %token SERVER SNI STRIP
132 %token TCP TOEXT TYPE TYPES
138 %token <v.string> STRING
139 %token <v.number> NUM
141 %type <v.number> bool proxy_port
142 %type <v.string> string numberstring
153 | conf error '\n' { file->errors++; }
156 include : INCLUDE STRING {
159 if ((nfile = pushfile($2, 0)) == NULL) {
160 yyerror("failed to include file %s", $2);
171 bool : ON { $$ = 1; }
175 string : string STRING {
176 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
179 yyerror("string: asprintf: %s", strerror(errno));
190 if (asprintf(&s, "%d", $1) == -1) {
191 yyerror("asprintf: number");
199 varset : STRING '=' string {
202 if (isspace((unsigned char)*s)) {
203 yyerror("macro name cannot contain "
216 option : CHROOT string {
217 if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
219 yyerror("chroot path too long");
222 | IPV6 bool { conf.ipv6 = $2; }
223 | PORT NUM { conf.port = check_port_num($2); }
224 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
226 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
227 yyerror("invalid protocols string \"%s\"", $2);
231 if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
233 yyerror("user name too long");
238 vhost : SERVER string {
240 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
242 loc = new_location();
243 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
245 TAILQ_INIT(&host->proxies);
247 (void) strlcpy(loc->match, "*", sizeof(loc->match));
248 (void) strlcpy(host->domain, $2, sizeof(host->domain));
250 if (strstr($2, "xn--") != NULL) {
251 yywarn("\"%s\" looks like punycode: you "
252 "should use the decoded hostname", $2);
256 } '{' optnl servbody '}' {
257 if (host->cert_path == NULL ||
258 host->key_path == NULL)
259 yyerror("invalid vhost definition: %s", $2);
261 | error '}' { yyerror("bad server directive"); }
264 servbody : /* empty */
265 | servbody servopt optnl
266 | servbody location optnl
267 | servbody proxy optnl
270 servopt : ALIAS string {
273 a = xcalloc(1, sizeof(*a));
274 (void) strlcpy(a->alias, $2, sizeof(a->alias));
276 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
279 ensure_absolute_path($2);
280 free(host->cert_path);
281 host->cert_path = $2;
284 ensure_absolute_path($2);
285 free(host->key_path);
289 ensure_absolute_path($2);
290 free(host->ocsp_path);
291 host->ocsp_path = $2;
293 | PARAM string '=' string {
299 proxy : PROXY { advance_proxy(); }
300 proxy_matches '{' optnl proxy_opts '}' {
301 if (*proxy->host == '\0')
302 yyerror("invalid proxy block: missing `relay-to' option");
304 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
305 (proxy->cert_path != NULL && proxy->key_path == NULL))
306 yyerror("invalid proxy block: missing cert or key");
310 proxy_matches : /* empty */
311 | proxy_matches proxy_match
314 proxy_port : /* empty */ { $$ = 1965; }
316 if (($$ = getservice($2)) == -1)
317 yyerror("invalid port number %s", $2);
320 | PORT NUM { $$ = $2; }
323 proxy_match : PROTO string {
324 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
327 | FOR_HOST string proxy_port {
328 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
329 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
335 proxy_opts : /* empty */
336 | proxy_opts proxy_opt optnl
339 proxy_opt : CERT string {
341 ensure_absolute_path($2);
342 proxy->cert_path = $2;
346 ensure_absolute_path($2);
347 proxy->key_path = $2;
350 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
351 yyerror("invalid protocols string \"%s\"", $2);
354 | RELAY_TO string proxy_port {
355 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
356 (void) snprintf(proxy->port, sizeof(proxy->port),
360 | REQUIRE CLIENT CA string {
361 ensure_absolute_path($4);
362 proxy->reqca_path = $4;
365 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
372 proxy->noverifyname = !$2;
376 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
377 /* drop the starting '/' if any */
379 memmove($3, $3+1, strlen($3));
380 (void) strlcpy(loc->match, $3, sizeof(loc->match));
386 locopts : /* empty */
387 | locopts locopt optnl
390 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
391 | BLOCK RETURN NUM string {
393 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
394 loc->block_code = check_block_code($3);
398 (void) strlcpy(loc->block_fmt, "temporary failure",
399 sizeof(loc->block_fmt));
400 loc->block_code = check_block_code($3);
401 if ($3 >= 30 && $3 < 40)
402 yyerror("missing `meta' for block return %d", $3);
405 (void) strlcpy(loc->block_fmt, "temporary failure",
406 sizeof(loc->block_fmt));
407 loc->block_code = 40;
409 | DEFAULT TYPE string {
410 (void) strlcpy(loc->default_mime, $3,
411 sizeof(loc->default_mime));
416 (void) strlcpy(loc->index, $2, sizeof(loc->index));
420 (void) strlcpy(loc->lang, $2,
424 | LOG bool { loc->disable_log = !$2; }
425 | REQUIRE CLIENT CA string {
426 ensure_absolute_path($4);
427 loc->reqca_path = $4;
430 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
433 | STRIP NUM { loc->strip = check_strip_no($2); }
437 loc->fcgi = fastcgi_conf($1, NULL);
440 | TCP string PORT NUM {
442 if (asprintf(&c, "%d", $4) == -1)
444 loc->fcgi = fastcgi_conf($2, c);
448 loc->fcgi = fastcgi_conf($2, "9000");
451 | TCP string PORT string {
452 loc->fcgi = fastcgi_conf($2, $4);
458 types : TYPES '{' optnl mediaopts_l '}' ;
460 mediaopts_l : mediaopts_l mediaoptsl nl
464 mediaoptsl : STRING {
467 } medianames_l optsemicolon
471 medianames_l : medianames_l medianamesl
475 medianamesl : numberstring {
476 if (add_mime(&conf.mime, current_media, $1) == -1)
485 optnl : '\n' optnl /* zero or more newlines */
486 | ';' optnl /* semicolons too */
496 static const struct keyword {
500 /* these MUST be sorted */
508 {"default", DEFAULT},
509 {"fastcgi", FASTCGI},
510 {"for-host", FOR_HOST},
511 {"include", INCLUDE},
516 {"location", LOCATION},
523 {"prefork", PREFORK},
525 {"protocols", PROTOCOLS},
527 {"relay-to", RELAY_TO},
528 {"require", REQUIRE},
538 {"use-tls", USE_TLS},
540 {"verifyname", VERIFYNAME},
544 yyerror(const char *msg, ...)
551 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
552 vfprintf(stderr, msg, ap);
553 fprintf(stderr, "\n");
558 yywarn(const char *msg, ...)
563 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
564 vfprintf(stderr, msg, ap);
565 fprintf(stderr, "\n");
570 kw_cmp(const void *k, const void *e)
572 return strcmp(k, ((struct keyword *)e)->word);
578 const struct keyword *p;
580 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
581 sizeof(keywords[0]), kw_cmp);
589 #define START_EXPAND 1
590 #define DONE_EXPAND 2
592 static int expanding;
600 if (file->ungetpos > 0)
601 c = file->ungetbuf[--file->ungetpos];
603 c = getc(file->stream);
605 if (c == START_EXPAND)
607 else if (c == DONE_EXPAND)
621 if ((c = igetc()) == EOF) {
622 yyerror("reached end of file while parsing "
624 if (file == topfile || popfile() == EOF)
631 while ((c = igetc()) == '\\') {
637 yylval.lineno = file->lineno;
643 * Fake EOL when hit EOF for the first time. This gets line
644 * count right if last line in included file is syntactically
645 * invalid and has no newline.
647 if (file->eof_reached == 0) {
648 file->eof_reached = 1;
652 if (file == topfile || popfile() == EOF)
666 if (file->ungetpos >= file->ungetsize) {
667 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
671 file->ungetsize *= 2;
673 file->ungetbuf[file->ungetpos++] = c;
681 /* Skip to either EOF or the first real EOL. */
704 while ((c = lgetc(0)) == ' ' || c == '\t')
707 yylval.lineno = file->lineno;
709 while ((c = lgetc(0)) != '\n' && c != EOF)
711 if (c == '$' && !expanding) {
713 if ((c = lgetc(0)) == EOF)
715 if (p + 1 >= buf + sizeof(buf) -1) {
716 yyerror("string too long");
719 if (isalnum(c) || c == '_') {
729 yyerror("macro `%s' not defined", buf);
732 yylval.v.string = xstrdup(val);
735 if (c == '@' && !expanding) {
737 if ((c = lgetc(0)) == EOF)
740 if (p + 1 >= buf + sizeof(buf) - 1) {
741 yyerror("string too long");
744 if (isalnum(c) || c == '_') {
754 yyerror("macro '%s' not defined", buf);
757 p = val + strlen(val) - 1;
758 lungetc(DONE_EXPAND);
763 lungetc(START_EXPAND);
772 if ((c = lgetc(quotec)) == EOF)
777 } else if (c == '\\') {
778 if ((next = lgetc(quotec)) == EOF)
780 if (next == quotec || next == ' ' ||
783 else if (next == '\n') {
788 } else if (c == quotec) {
791 } else if (c == '\0') {
792 yyerror("invalid syntax");
795 if (p + 1 >= buf + sizeof(buf) - 1) {
796 yyerror("string too long");
801 yylval.v.string = strdup(buf);
802 if (yylval.v.string == NULL)
803 fatal("yylex: strdup");
807 #define allowed_to_end_number(x) \
808 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
810 if (c == '-' || isdigit(c)) {
813 if ((size_t)(p-buf) >= sizeof(buf)) {
814 yyerror("string too long");
817 } while ((c = lgetc(0)) != EOF && isdigit(c));
819 if (p == buf + 1 && buf[0] == '-')
821 if (c == EOF || allowed_to_end_number(c)) {
822 const char *errstr = NULL;
825 yylval.v.number = strtonum(buf, LLONG_MIN,
828 yyerror("\"%s\" invalid number: %s",
843 #define allowed_in_string(x) \
844 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
845 x != '{' && x != '}' && \
846 x != '!' && x != '=' && x != '#' && \
847 x != ',' && x != ';'))
849 if (isalnum(c) || c == ':' || c == '_') {
852 if ((size_t)(p-buf) >= sizeof(buf)) {
853 yyerror("string too long");
856 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
859 if ((token = lookup(buf)) == STRING)
860 yylval.v.string = xstrdup(buf);
864 yylval.lineno = file->lineno;
873 pushfile(const char *name, int secret)
877 nfile = xcalloc(1, sizeof(*nfile));
878 nfile->name = xstrdup(name);
879 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
880 log_warn("can't open %s", nfile->name);
885 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
886 nfile->ungetsize = 16;
887 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
888 TAILQ_INSERT_TAIL(&files, nfile, entry);
897 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
898 prev->errors += file->errors;
900 TAILQ_REMOVE(&files, file, entry);
901 fclose(file->stream);
903 free(file->ungetbuf);
906 return file ? 0 : EOF;
910 parse_conf(const char *filename)
912 struct sym *sym, *next;
914 file = pushfile(filename, 0);
920 errors = file->errors;
923 /* Free macros and check which have not been used. */
924 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
925 /* TODO: warn if !sym->used */
929 TAILQ_REMOVE(&symhead, sym, entry);
942 /* struct location *l; */
943 /* struct envlist *e; */
944 /* struct alist *a; */
946 if (*conf.chroot != '\0')
947 printf("chroot \"%s\"\n", conf.chroot);
948 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
949 /* XXX: defined mimes? */
950 printf("port %d\n", conf.port);
951 printf("prefork %d\n", conf.prefork);
952 /* XXX: protocols? */
953 if (*conf.user != '\0')
954 printf("user \"%s\"\n", conf.user);
956 TAILQ_FOREACH(h, &hosts, vhosts) {
957 printf("\nserver \"%s\" {\n", h->domain);
958 printf(" cert \"%s\"\n", h->cert);
959 printf(" key \"%s\"\n", h->key);
960 /* TODO: print locations... */
966 symset(const char *name, const char *val, int persist)
970 TAILQ_FOREACH(sym, &symhead, entry) {
971 if (!strcmp(name, sym->name))
981 TAILQ_REMOVE(&symhead, sym, entry);
986 sym = xcalloc(1, sizeof(*sym));
987 sym->name = xstrdup(name);
988 sym->val = xstrdup(val);
990 sym->persist = persist;
992 TAILQ_INSERT_TAIL(&symhead, sym, entry);
997 cmdline_symset(char *s)
1002 if ((val = strrchr(s, '=')) == NULL)
1004 sym = xcalloc(1, val - s + 1);
1005 memcpy(sym, s, val - s);
1006 ret = symset(sym, val + 1, 1);
1012 symget(const char *nam)
1016 TAILQ_FOREACH(sym, &symhead, entry) {
1017 if (strcmp(nam, sym->name) == 0) {
1026 ensure_absolute_path(char *path)
1028 if (path == NULL || *path != '/')
1029 yyerror("not an absolute path: %s", path);
1034 check_block_code(int n)
1036 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1037 yyerror("invalid block code %d", n);
1042 check_block_fmt(char *fmt)
1046 for (s = fmt; *s; ++s) {
1057 yyerror("invalid format specifier %%%c", *s);
1065 check_strip_no(int n)
1068 yyerror("invalid strip number %d", n);
1073 check_port_num(int n)
1075 if (n <= 0 || n >= UINT16_MAX)
1076 yyerror("port number is %s: %d",
1077 n <= 0 ? "too small" : "too large",
1083 check_prefork_num(int n)
1085 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1086 yyerror("invalid prefork number %d", n);
1093 loc = new_location();
1094 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1100 proxy = new_proxy();
1101 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1105 parsehp(char *str, char **host, const char **port, const char *def)
1112 if ((at = strchr(str, ':')) != NULL) {
1118 strtonum(*port, 1, UINT16_MAX, &errstr);
1120 yyerror("port is %s: %s", errstr, *port);
1124 fastcgi_conf(const char *path, const char *port)
1129 TAILQ_FOREACH(f, &conf.fcgi, fcgi) {
1130 if (!strcmp(f->path, path) &&
1131 ((port == NULL && *f->port == '\0') ||
1132 !strcmp(f->port, port)))
1137 f = xcalloc(1, sizeof(*f));
1139 (void)strlcpy(f->path, path, sizeof(f->path));
1141 (void)strlcpy(f->port, port, sizeof(f->port));
1142 TAILQ_INSERT_TAIL(&conf.fcgi, f, fcgi);
1148 add_param(char *name, char *val)
1151 struct envhead *h = &host->params;
1153 e = xcalloc(1, sizeof(*e));
1154 (void) strlcpy(e->name, name, sizeof(e->name));
1155 (void) strlcpy(e->value, val, sizeof(e->value));
1156 TAILQ_INSERT_TAIL(h, e, envs);
1160 getservice(const char *n)
1166 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1168 s = getservbyname(n, "tcp");
1170 s = getservbyname(n, "udp");
1173 return (ntohs(s->s_port));
1176 return ((unsigned short)llval);