4 * Copyright (c) 2021 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.
35 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
37 TAILQ_ENTRY(file) entry;
48 struct file *pushfile(const char *, int);
52 void yyerror(const char *, ...)
53 __attribute__((__format__ (printf, 1, 2)))
54 __attribute__((__nonnull__ (1)));
55 void yywarn(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
70 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
72 TAILQ_ENTRY(sym) entry;
79 int symset(const char *, const char *, int);
80 char *symget(const char *);
82 struct vhost *new_vhost(void);
83 struct location *new_location(void);
84 char *ensure_absolute_path(char*);
85 int check_block_code(int);
86 char *check_block_fmt(char*);
87 int check_strip_no(int);
88 int check_port_num(int);
89 int check_prefork_num(int);
90 void advance_loc(void);
91 void only_once(const void*, const char*);
92 void only_oncei(int, const char*);
93 int fastcgi_conf(char *, char *, char *);
94 void add_param(char *, char *, int);
96 static struct vhost *host;
97 static struct location *loc;
111 /* %define parse.error verbose */
115 %token CA CERT CGI CHROOT CLIENT
117 %token ENTRYPOINT ENV
119 %token INCLUDE INDEX IPV6
121 %token LANG LOCATION LOG
124 %token PARAM PORT PREFORK PROTOCOLS PROXY
125 %token RELAY_TO REQUIRE RETURN ROOT
126 %token SERVER SPAWN STRIP
127 %token TCP TOEXT TYPE USER
132 %token <v.string> STRING
133 %token <v.number> NUM
135 %type <v.number> bool
136 %type <v.string> string
146 | conf error '\n' { file->errors++; }
149 include : INCLUDE STRING {
152 if ((nfile = pushfile($2, 0)) == NULL) {
153 yyerror("failed to include file %s", $2);
164 bool : ON { $$ = 1; }
168 string : string STRING {
169 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
172 yyerror("string: asprintf: %s", strerror(errno));
181 varset : STRING '=' string {
184 if (isspace((unsigned char)*s)) {
185 yyerror("macro name cannot contain "
198 option : CHROOT string { conf.chroot = $2; }
199 | IPV6 bool { conf.ipv6 = $2; }
200 | MIME STRING string {
201 yywarn("`mime MIME EXT' is deprecated and will be "
202 "removed in a future version, please use the new "
203 "syntax: `map MIME to-ext EXT'");
204 add_mime(&conf.mime, $2, $3);
206 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
207 | PORT NUM { conf.port = check_port_num($2); }
208 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
210 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
211 yyerror("invalid protocols string \"%s\"", $2);
214 | USER string { conf.user = $2; }
217 vhost : SERVER string {
219 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
221 loc = new_location();
222 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
224 loc->match = xstrdup("*");
227 if (strstr($2, "xn--") != NULL) {
228 yywarn("\"%s\" looks like punycode: you "
229 "should use the decoded hostname", $2);
231 } '{' optnl servopts locations '}' {
232 if (host->cert == NULL || host->key == NULL)
233 yyerror("invalid vhost definition: %s", $2);
235 | error '}' { yyerror("bad server directive"); }
238 servopts : /* empty */
239 | servopts servopt optnl
242 servopt : ALIAS string {
245 a = xcalloc(1, sizeof(*a));
247 if (TAILQ_EMPTY(&host->aliases))
248 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
250 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
253 only_once(host->cert, "cert");
254 host->cert = ensure_absolute_path($2);
257 only_once(host->cgi, "cgi");
258 /* drop the starting '/', if any */
260 memmove($2, $2+1, strlen($2));
263 | ENTRYPOINT string {
264 only_once(host->entrypoint, "entrypoint");
266 memmove($2, $2+1, strlen($2));
267 host->entrypoint = $2;
269 | ENV string '=' string {
270 add_param($2, $4, 1);
273 only_once(host->key, "key");
274 host->key = ensure_absolute_path($2);
277 only_once(host->ocsp, "ocsp");
278 host->ocsp = ensure_absolute_path($2);
280 | PARAM string '=' string {
281 add_param($2, $4, 0);
287 proxy : PROXY proxy_opt
288 | PROXY '{' optnl proxy_opts '}'
291 proxy_opts : /* empty */
292 | proxy_opts proxy_opt optnl
295 proxy_opt : CERT string {
296 struct proxy *p = &host->proxy;
298 only_once(p->cert, "proxy cert");
299 ensure_absolute_path($2);
300 p->cert = tls_load_file($2, &p->certlen, NULL);
302 yyerror("can't load cert %s", $2);
306 struct proxy *p = &host->proxy;
308 only_once(p->key, "proxy key");
309 ensure_absolute_path($2);
310 p->key = tls_load_file($2, &p->keylen, NULL);
312 yyerror("can't load key %s", $2);
316 struct proxy *p = &host->proxy;
318 if (tls_config_parse_protocols(&p->protocols, $2) == -1)
319 yyerror("invalid protocols string \"%s\"", $2);
325 struct proxy *p = &host->proxy;
327 only_once(p->host, "proxy relay-to");
330 if ((at = strchr($2, ':')) != NULL) {
336 strtonum(p->port, 1, UINT16_MAX, &errstr);
338 yyerror("proxy port is %s: %s", errstr,
342 host->proxy.noverifyname = !$2;
346 locations : /* empty */
347 | locations location optnl
350 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
351 /* drop the starting '/' if any */
353 memmove($3, $3+1, strlen($3));
359 locopts : /* empty */
360 | locopts locopt optnl
363 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
364 | BLOCK RETURN NUM string {
365 only_once(loc->block_fmt, "block");
366 loc->block_fmt = check_block_fmt($4);
367 loc->block_code = check_block_code($3);
370 only_once(loc->block_fmt, "block");
371 loc->block_fmt = xstrdup("temporary failure");
372 loc->block_code = check_block_code($3);
373 if ($3 >= 30 && $3 < 40)
374 yyerror("missing `meta' for block return %d", $3);
377 only_once(loc->block_fmt, "block");
378 loc->block_fmt = xstrdup("temporary failure");
379 loc->block_code = 40;
381 | DEFAULT TYPE string {
382 only_once(loc->default_mime, "default type");
383 loc->default_mime = $3;
387 only_once(loc->index, "index");
391 only_once(loc->lang, "lang");
394 | LOG bool { loc->disable_log = !$2; }
395 | REQUIRE CLIENT CA string {
396 only_once(loc->reqca, "require client ca");
397 ensure_absolute_path($4);
398 if ((loc->reqca = load_ca($4)) == NULL)
399 yyerror("couldn't load ca cert: %s", $4);
403 only_once(loc->dir, "root");
404 loc->dir = ensure_absolute_path($2);
406 | STRIP NUM { loc->strip = check_strip_no($2); }
409 fastcgi : SPAWN string {
410 only_oncei(loc->fcgi, "fastcgi");
411 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
414 only_oncei(loc->fcgi, "fastcgi");
415 loc->fcgi = fastcgi_conf($1, NULL, NULL);
417 | TCP string PORT NUM {
419 if (asprintf(&c, "%d", $4) == -1)
421 only_oncei(loc->fcgi, "fastcgi");
422 loc->fcgi = fastcgi_conf($2, c, NULL);
425 only_oncei(loc->fcgi, "fastcgi");
426 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
428 | TCP string PORT string {
429 only_oncei(loc->fcgi, "fastcgi");
430 loc->fcgi = fastcgi_conf($2, $4, NULL);
434 optnl : '\n' optnl /* zero or more newlines */
435 | ';' optnl /* semicolons too */
441 static struct keyword {
445 /* these MUST be sorted */
454 {"default", DEFAULT},
455 {"entrypoint", ENTRYPOINT},
457 {"fastcgi", FASTCGI},
462 {"location", LOCATION},
471 {"prefork", PREFORK},
472 {"protocols", PROTOCOLS},
474 {"relay-to", RELAY_TO},
475 {"require", REQUIRE},
485 {"verifyname", VERIFYNAME},
489 yyerror(const char *msg, ...)
496 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
497 vfprintf(stderr, msg, ap);
498 fprintf(stderr, "\n");
503 yywarn(const char *msg, ...)
508 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
509 vfprintf(stderr, msg, ap);
510 fprintf(stderr, "\n");
515 kw_cmp(const void *k, const void *e)
517 return strcmp(k, ((struct keyword *)e)->word);
523 const struct keyword *p;
525 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
526 sizeof(keywords[0]), kw_cmp);
534 #define START_EXPAND 1
535 #define DONE_EXPAND 2
537 static int expanding;
545 if (file->ungetpos > 0)
546 c = file->ungetbuf[--file->ungetpos];
548 c = getc(file->stream);
550 if (c == START_EXPAND)
552 else if (c == DONE_EXPAND)
566 if ((c = igetc()) == EOF) {
567 yyerror("reached end of file while parsing "
569 if (file == topfile || popfile() == EOF)
576 while ((c = igetc()) == '\\') {
582 yylval.lineno = file->lineno;
588 * Fake EOL when hit EOF for the first time. This gets line
589 * count right if last line in included file is syntactically
590 * invalid and has no newline.
592 if (file->eof_reached == 0) {
593 file->eof_reached = 1;
597 if (file == topfile || popfile() == EOF)
611 if (file->ungetpos >= file->ungetsize) {
612 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
616 file->ungetsize *= 2;
618 file->ungetbuf[file->ungetpos++] = c;
626 /* Skip to either EOF or the first real EOL. */
649 while ((c = lgetc(0)) == ' ' || c == '\t')
652 yylval.lineno = file->lineno;
654 while ((c = lgetc(0)) != '\n' && c != EOF)
656 if (c == '$' && !expanding) {
658 if ((c = lgetc(0)) == EOF)
660 if (p + 1 >= buf + sizeof(buf) -1) {
661 yyerror("string too long");
664 if (isalnum(c) || c == '_') {
674 yyerror("macro `%s' not defined", buf);
677 yylval.v.string = xstrdup(val);
680 if (c == '@' && !expanding) {
682 if ((c = lgetc(0)) == EOF)
685 if (p + 1 >= buf + sizeof(buf) - 1) {
686 yyerror("string too long");
689 if (isalnum(c) || c == '_') {
699 yyerror("macro '%s' not defined", buf);
702 p = val + strlen(val) - 1;
703 lungetc(DONE_EXPAND);
708 lungetc(START_EXPAND);
717 if ((c = lgetc(quotec)) == EOF)
722 } else if (c == '\\') {
723 if ((next = lgetc(quotec)) == EOF)
725 if (next == quotec || next == ' ' ||
728 else if (next == '\n') {
733 } else if (c == quotec) {
736 } else if (c == '\0') {
737 yyerror("invalid syntax");
740 if (p + 1 >= buf + sizeof(buf) - 1) {
741 yyerror("string too long");
746 yylval.v.string = strdup(buf);
747 if (yylval.v.string == NULL)
748 err(1, "yylex: strdup");
752 #define allowed_to_end_number(x) \
753 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
755 if (c == '-' || isdigit(c)) {
758 if ((size_t)(p-buf) >= sizeof(buf)) {
759 yyerror("string too long");
762 } while ((c = lgetc(0)) != EOF && isdigit(c));
764 if (p == buf + 1 && buf[0] == '-')
766 if (c == EOF || allowed_to_end_number(c)) {
767 const char *errstr = NULL;
770 yylval.v.number = strtonum(buf, LLONG_MIN,
773 yyerror("\"%s\" invalid number: %s",
788 #define allowed_in_string(x) \
789 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
790 x != '{' && x != '}' && \
791 x != '!' && x != '=' && x != '#' && \
792 x != ',' && x != ';'))
794 if (isalnum(c) || c == ':' || c == '_') {
797 if ((size_t)(p-buf) >= sizeof(buf)) {
798 yyerror("string too long");
801 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
804 if ((token = lookup(buf)) == STRING)
805 yylval.v.string = xstrdup(buf);
809 yylval.lineno = file->lineno;
818 pushfile(const char *name, int secret)
822 nfile = xcalloc(1, sizeof(*nfile));
823 nfile->name = xstrdup(name);
824 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
825 log_warn(NULL, "can't open %s: %s", nfile->name,
831 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
832 nfile->ungetsize = 16;
833 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
834 TAILQ_INSERT_TAIL(&files, nfile, entry);
843 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
844 prev->errors += file->errors;
846 TAILQ_REMOVE(&files, file, entry);
847 fclose(file->stream);
849 free(file->ungetbuf);
852 return file ? 0 : EOF;
856 parse_conf(const char *filename)
858 struct sym *sym, *next;
860 file = pushfile(filename, 0);
866 errors = file->errors;
869 /* Free macros and check which have not been used. */
870 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
871 /* TODO: warn if !sym->used */
875 TAILQ_REMOVE(&symhead, sym, entry);
888 /* struct location *l; */
889 /* struct envlist *e; */
890 /* struct alist *a; */
892 if (conf.chroot != NULL)
893 printf("chroot \"%s\"\n", conf.chroot);
894 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
895 /* XXX: defined mimes? */
896 printf("port %d\n", conf.port);
897 printf("prefork %d\n", conf.prefork);
898 /* XXX: protocols? */
899 if (conf.user != NULL)
900 printf("user \"%s\"\n", conf.user);
902 TAILQ_FOREACH(h, &hosts, vhosts) {
903 printf("\nserver \"%s\" {\n", h->domain);
904 printf(" cert \"%s\"\n", h->cert);
905 printf(" key \"%s\"\n", h->key);
906 /* TODO: print locations... */
912 symset(const char *name, const char *val, int persist)
916 TAILQ_FOREACH(sym, &symhead, entry) {
917 if (!strcmp(name, sym->name))
927 TAILQ_REMOVE(&symhead, sym, entry);
932 sym = xcalloc(1, sizeof(*sym));
933 sym->name = xstrdup(name);
934 sym->val = xstrdup(val);
936 sym->persist = persist;
938 TAILQ_INSERT_TAIL(&symhead, sym, entry);
943 cmdline_symset(char *s)
948 if ((val = strrchr(s, '=')) == NULL)
950 sym = xcalloc(1, val - s + 1);
951 memcpy(sym, s, val - s);
952 ret = symset(sym, val + 1, 1);
958 symget(const char *nam)
962 TAILQ_FOREACH(sym, &symhead, entry) {
963 if (strcmp(nam, sym->name) == 0) {
976 v = xcalloc(1, sizeof(*v));
977 v->proxy.protocols = TLS_PROTOCOLS_DEFAULT;
986 l = xcalloc(1, sizeof(*l));
993 ensure_absolute_path(char *path)
995 if (path == NULL || *path != '/')
996 yyerror("not an absolute path: %s", path);
1001 check_block_code(int n)
1003 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1004 yyerror("invalid block code %d", n);
1009 check_block_fmt(char *fmt)
1013 for (s = fmt; *s; ++s) {
1024 yyerror("invalid format specifier %%%c", *s);
1032 check_strip_no(int n)
1035 yyerror("invalid strip number %d", n);
1040 check_port_num(int n)
1042 if (n <= 0 || n >= UINT16_MAX)
1043 yyerror("port number is %s: %d",
1044 n <= 0 ? "too small" : "too large",
1050 check_prefork_num(int n)
1052 if (n <= 0 || n >= PROC_MAX)
1053 yyerror("invalid prefork number %d", n);
1060 loc = new_location();
1061 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1065 only_once(const void *ptr, const char *name)
1068 yyerror("`%s' specified more than once", name);
1072 only_oncei(int i, const char *name)
1075 yyerror("`%s' specified more than once", name);
1079 fastcgi_conf(char *path, char *port, char *prog)
1084 for (i = 0; i < FCGI_MAX; ++i) {
1087 if (f->path == NULL) {
1095 /* XXX: what to do with prog? */
1096 if (!strcmp(f->path, path) &&
1097 ((port == NULL && f->port == NULL) ||
1098 !strcmp(f->port, port))) {
1105 yyerror("too much `fastcgi' rules defined.");
1110 add_param(char *name, char *val, int env)
1120 e = xcalloc(1, sizeof(*e));
1124 TAILQ_INSERT_HEAD(h, e, envs);
1126 TAILQ_INSERT_TAIL(h, e, envs);