1 15902770 2021-01-15 op /* -*- mode: fundamental; indent-tabs-mode: t; -*- */
5 15902770 2021-01-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
7 15902770 2021-01-15 op * Permission to use, copy, modify, and distribute this software for any
8 15902770 2021-01-15 op * purpose with or without fee is hereby granted, provided that the above
9 15902770 2021-01-15 op * copyright notice and this permission notice appear in all copies.
11 15902770 2021-01-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 15902770 2021-01-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 15902770 2021-01-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 15902770 2021-01-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 15902770 2021-01-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 15902770 2021-01-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 15902770 2021-01-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 15902770 2021-01-15 op #include <err.h>
21 15902770 2021-01-15 op #include <stdio.h>
23 15902770 2021-01-15 op #include "gmid.h"
26 15902770 2021-01-15 op * #define YYDEBUG 1
27 15902770 2021-01-15 op * int yydebug = 1;
30 15902770 2021-01-15 op struct vhost *host = &hosts[0];
31 15902770 2021-01-15 op size_t ihost = 0;
33 c8b74339 2021-01-24 op struct location *loc = &hosts[0].locations[0];
34 c8b74339 2021-01-24 op size_t iloc = 0;
36 13ed2fb6 2021-01-27 op int goterror = 0;
37 13ed2fb6 2021-01-27 op const char *config_path;
39 13ed2fb6 2021-01-27 op void yyerror(const char*);
40 13ed2fb6 2021-01-27 op int parse_portno(const char*);
41 13ed2fb6 2021-01-27 op void parse_conf(const char*);
45 15902770 2021-01-15 op /* for bison: */
46 15902770 2021-01-15 op /* %define parse.error verbose */
53 46af8c6c 2021-01-27 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
54 ae08ec7d 2021-01-25 op %token TCHROOT TUSER TSERVER
55 252908e6 2021-01-24 op %token TLOCATION TCERT TKEY TROOT TCGI TLANG TINDEX TAUTO
58 15902770 2021-01-15 op %token <str> TSTRING
59 15902770 2021-01-15 op %token <num> TNUM
60 15902770 2021-01-15 op %token <num> TBOOL
64 15902770 2021-01-15 op conf : options vhosts ;
66 15902770 2021-01-15 op options : /* empty */
67 15902770 2021-01-15 op | options option
70 46af8c6c 2021-01-27 op option : TIPV6 TBOOL { conf.ipv6 = $2; }
71 15902770 2021-01-15 op | TPORT TNUM { conf.port = $2; }
72 5bc3c98e 2021-01-15 op | TPROTOCOLS TSTRING {
73 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
74 5bc3c98e 2021-01-15 op errx(1, "invalid protocols string \"%s\"", $2);
76 b2a6b613 2021-01-21 op | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
77 ae08ec7d 2021-01-25 op | TCHROOT TSTRING { conf.chroot = $2; }
78 ae08ec7d 2021-01-25 op | TUSER TSTRING { conf.user = $2; }
81 15902770 2021-01-15 op vhosts : /* empty */
82 15902770 2021-01-15 op | vhosts vhost
85 c8b74339 2021-01-24 op vhost : TSERVER TSTRING '{' servopts locations '}' {
86 c8b74339 2021-01-24 op host->locations[0].match = (char*)"*";
87 15902770 2021-01-15 op host->domain = $2;
89 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
90 cbeee4ca 2021-01-28 op warnx("%s:%d \"%s\" looks like punycode: "
91 415ac7a2 2021-01-28 op "you should use the decoded hostname.",
92 415ac7a2 2021-01-28 op config_path, yylineno, $2);
95 15902770 2021-01-15 op if (host->cert == NULL || host->key == NULL ||
96 15902770 2021-01-15 op host->dir == NULL)
97 15902770 2021-01-15 op errx(1, "invalid vhost definition: %s", $2);
99 15902770 2021-01-15 op if (++ihost == HOSTSLEN)
100 15902770 2021-01-15 op errx(1, "too much vhosts defined");
103 c8b74339 2021-01-24 op loc = &host->locations[0];
106 15902770 2021-01-15 op | error '}' { yyerror("error in server directive"); }
109 15902770 2021-01-15 op servopts : /* empty */
110 15902770 2021-01-15 op | servopts servopt
113 15902770 2021-01-15 op servopt : TCERT TSTRING { host->cert = $2; }
114 15902770 2021-01-15 op | TKEY TSTRING { host->key = $2; }
115 15902770 2021-01-15 op | TROOT TSTRING { host->dir = $2; }
116 15902770 2021-01-15 op | TCGI TSTRING {
117 15902770 2021-01-15 op host->cgi = $2;
118 15902770 2021-01-15 op /* drop the starting '/', if any */
119 15902770 2021-01-15 op if (*host->cgi == '/')
125 c8b74339 2021-01-24 op locations : /* empty */
126 c8b74339 2021-01-24 op | locations location
129 c8b74339 2021-01-24 op location : TLOCATION TSTRING '{' locopts '}' {
130 c8b74339 2021-01-24 op loc->match = $2;
131 c8b74339 2021-01-24 op if (++iloc == LOCLEN)
132 c8b74339 2021-01-24 op errx(1, "too much location rules defined");
138 c8b74339 2021-01-24 op locopts : /* empty */
139 c8b74339 2021-01-24 op | locopts locopt
142 c8b74339 2021-01-24 op locopt : TDEFAULT TTYPE TSTRING {
143 fe5967cd 2021-01-27 op if (loc->default_mime != NULL)
144 fe5967cd 2021-01-27 op yyerror("`default type' specified more than once");
145 c8b74339 2021-01-24 op loc->default_mime = $3;
147 05c23a54 2021-01-19 op | TLANG TSTRING {
148 fe5967cd 2021-01-27 op if (loc->lang != NULL)
149 fe5967cd 2021-01-27 op yyerror("`lang' specified more than once");
150 c8b74339 2021-01-24 op loc->lang = $2;
152 e7a2a99b 2021-01-24 op | TINDEX TSTRING {
153 fe5967cd 2021-01-27 op if (loc->index != NULL)
154 fe5967cd 2021-01-27 op yyerror("`index' specified more than once");
155 c8b74339 2021-01-24 op loc->index = $2;
157 252908e6 2021-01-24 op | TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
163 13ed2fb6 2021-01-27 op yyerror(const char *msg)
165 13ed2fb6 2021-01-27 op goterror = 1;
166 13ed2fb6 2021-01-27 op fprintf(stderr, "%s:%d: %s\n", config_path, yylineno, msg);
170 13ed2fb6 2021-01-27 op parse_portno(const char *p)
172 13ed2fb6 2021-01-27 op const char *errstr;
175 13ed2fb6 2021-01-27 op n = strtonum(p, 0, UINT16_MAX, &errstr);
176 13ed2fb6 2021-01-27 op if (errstr != NULL)
177 13ed2fb6 2021-01-27 op errx(1, "port number is %s: %s", errstr, p);
182 13ed2fb6 2021-01-27 op parse_conf(const char *path)
184 13ed2fb6 2021-01-27 op config_path = path;
185 13ed2fb6 2021-01-27 op if ((yyin = fopen(path, "r")) == NULL)
186 13ed2fb6 2021-01-27 op fatal("cannot open config %s", path);
188 13ed2fb6 2021-01-27 op fclose(yyin);
190 13ed2fb6 2021-01-27 op if (goterror)