Blame


1 8a35f56c 2022-07-16 thomas /*
2 8a35f56c 2022-07-16 thomas * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 8a35f56c 2022-07-16 thomas * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 8a35f56c 2022-07-16 thomas * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 8a35f56c 2022-07-16 thomas * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 8a35f56c 2022-07-16 thomas * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 8a35f56c 2022-07-16 thomas *
10 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
11 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
12 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
13 8a35f56c 2022-07-16 thomas *
14 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 8a35f56c 2022-07-16 thomas */
22 8a35f56c 2022-07-16 thomas
23 8a35f56c 2022-07-16 thomas %{
24 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
25 4fccd2fe 2023-03-08 thomas
26 8a35f56c 2022-07-16 thomas #include <sys/ioctl.h>
27 8a35f56c 2022-07-16 thomas #include <sys/types.h>
28 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
29 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
30 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
31 8a35f56c 2022-07-16 thomas
32 8a35f56c 2022-07-16 thomas #include <net/if.h>
33 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
34 8a35f56c 2022-07-16 thomas
35 8a35f56c 2022-07-16 thomas #include <arpa/inet.h>
36 8a35f56c 2022-07-16 thomas
37 8a35f56c 2022-07-16 thomas #include <ctype.h>
38 8a35f56c 2022-07-16 thomas #include <err.h>
39 8a35f56c 2022-07-16 thomas #include <errno.h>
40 8a35f56c 2022-07-16 thomas #include <event.h>
41 8a35f56c 2022-07-16 thomas #include <ifaddrs.h>
42 8a35f56c 2022-07-16 thomas #include <limits.h>
43 8a35f56c 2022-07-16 thomas #include <netdb.h>
44 8a35f56c 2022-07-16 thomas #include <stdarg.h>
45 8a35f56c 2022-07-16 thomas #include <stdlib.h>
46 8a35f56c 2022-07-16 thomas #include <stdio.h>
47 8a35f56c 2022-07-16 thomas #include <string.h>
48 8a35f56c 2022-07-16 thomas #include <syslog.h>
49 8a35f56c 2022-07-16 thomas #include <unistd.h>
50 161663e7 2023-03-11 thomas
51 161663e7 2023-03-11 thomas #include "got_sockaddr.h"
52 161663e7 2023-03-11 thomas #include "got_reference.h"
53 8a35f56c 2022-07-16 thomas
54 8a35f56c 2022-07-16 thomas #include "proc.h"
55 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
56 8a35f56c 2022-07-16 thomas
57 8a35f56c 2022-07-16 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
58 8a35f56c 2022-07-16 thomas static struct file {
59 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(file) entry;
60 8a35f56c 2022-07-16 thomas FILE *stream;
61 8a35f56c 2022-07-16 thomas char *name;
62 8a35f56c 2022-07-16 thomas int lineno;
63 8a35f56c 2022-07-16 thomas int errors;
64 8a35f56c 2022-07-16 thomas } *file;
65 8a35f56c 2022-07-16 thomas struct file *newfile(const char *, int);
66 8a35f56c 2022-07-16 thomas static void closefile(struct file *);
67 8a35f56c 2022-07-16 thomas int check_file_secrecy(int, const char *);
68 8a35f56c 2022-07-16 thomas int yyparse(void);
69 8a35f56c 2022-07-16 thomas int yylex(void);
70 8a35f56c 2022-07-16 thomas int yyerror(const char *, ...)
71 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)))
72 8a35f56c 2022-07-16 thomas __attribute__((__nonnull__ (1)));
73 8a35f56c 2022-07-16 thomas int kw_cmp(const void *, const void *);
74 8a35f56c 2022-07-16 thomas int lookup(char *);
75 8a35f56c 2022-07-16 thomas int lgetc(int);
76 8a35f56c 2022-07-16 thomas int lungetc(int);
77 8a35f56c 2022-07-16 thomas int findeol(void);
78 8a35f56c 2022-07-16 thomas
79 8a35f56c 2022-07-16 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
80 8a35f56c 2022-07-16 thomas struct sym {
81 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(sym) entry;
82 8a35f56c 2022-07-16 thomas int used;
83 8a35f56c 2022-07-16 thomas int persist;
84 8a35f56c 2022-07-16 thomas char *nam;
85 8a35f56c 2022-07-16 thomas char *val;
86 8a35f56c 2022-07-16 thomas };
87 8a35f56c 2022-07-16 thomas
88 8a35f56c 2022-07-16 thomas int symset(const char *, const char *, int);
89 8a35f56c 2022-07-16 thomas char *symget(const char *);
90 8a35f56c 2022-07-16 thomas
91 8a35f56c 2022-07-16 thomas static int errors;
92 8a35f56c 2022-07-16 thomas
93 8a35f56c 2022-07-16 thomas static struct gotwebd *gotwebd;
94 8a35f56c 2022-07-16 thomas static struct server *new_srv;
95 8a35f56c 2022-07-16 thomas static struct server *conf_new_server(const char *);
96 8a35f56c 2022-07-16 thomas int getservice(const char *);
97 8a35f56c 2022-07-16 thomas int n;
98 8a35f56c 2022-07-16 thomas
99 e4c7e0b0 2022-08-30 thomas int get_addrs(const char *, struct server *, in_port_t);
100 e4c7e0b0 2022-08-30 thomas int addr_dup_check(struct addresslist *, struct address *,
101 e4c7e0b0 2022-08-30 thomas const char *, const char *);
102 e4c7e0b0 2022-08-30 thomas int add_addr(struct server *, struct address *);
103 bccb4ec2 2023-06-01 thomas int host(const char *, struct server *,
104 8a35f56c 2022-07-16 thomas int, in_port_t, const char *, int);
105 e4c7e0b0 2022-08-30 thomas int host_if(const char *, struct server *,
106 8a35f56c 2022-07-16 thomas int, in_port_t, const char *, int);
107 8a35f56c 2022-07-16 thomas int is_if_in_group(const char *, const char *);
108 8a35f56c 2022-07-16 thomas
109 8a35f56c 2022-07-16 thomas typedef struct {
110 8a35f56c 2022-07-16 thomas union {
111 8a35f56c 2022-07-16 thomas long long number;
112 8a35f56c 2022-07-16 thomas char *string;
113 8a35f56c 2022-07-16 thomas in_port_t port;
114 8a35f56c 2022-07-16 thomas } v;
115 8a35f56c 2022-07-16 thomas int lineno;
116 8a35f56c 2022-07-16 thomas } YYSTYPE;
117 8a35f56c 2022-07-16 thomas
118 8a35f56c 2022-07-16 thomas %}
119 8a35f56c 2022-07-16 thomas
120 c24a47af 2022-08-11 thomas %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
121 8a35f56c 2022-07-16 thomas %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
122 8a35f56c 2022-07-16 thomas %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
123 3991b2a5 2022-10-31 thomas %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
124 bea82c4f 2023-01-06 thomas %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
125 8a35f56c 2022-07-16 thomas
126 8a35f56c 2022-07-16 thomas %token <v.string> STRING
127 8a35f56c 2022-07-16 thomas %type <v.port> fcgiport
128 8a35f56c 2022-07-16 thomas %token <v.number> NUMBER
129 8a35f56c 2022-07-16 thomas %type <v.number> boolean
130 8a35f56c 2022-07-16 thomas
131 8a35f56c 2022-07-16 thomas %%
132 8a35f56c 2022-07-16 thomas
133 cfab1835 2022-10-04 thomas grammar : /* empty */
134 8a35f56c 2022-07-16 thomas | grammar '\n'
135 cfab1835 2022-10-04 thomas | grammar varset '\n'
136 8a35f56c 2022-07-16 thomas | grammar main '\n'
137 8a35f56c 2022-07-16 thomas | grammar server '\n'
138 cfab1835 2022-10-04 thomas | grammar error '\n' { file->errors++; }
139 cfab1835 2022-10-04 thomas ;
140 cfab1835 2022-10-04 thomas
141 cfab1835 2022-10-04 thomas varset : STRING '=' STRING {
142 cfab1835 2022-10-04 thomas char *s = $1;
143 cfab1835 2022-10-04 thomas while (*s++) {
144 cfab1835 2022-10-04 thomas if (isspace((unsigned char)*s)) {
145 cfab1835 2022-10-04 thomas yyerror("macro name cannot contain "
146 cfab1835 2022-10-04 thomas "whitespace");
147 cfab1835 2022-10-04 thomas free($1);
148 cfab1835 2022-10-04 thomas free($3);
149 cfab1835 2022-10-04 thomas YYERROR;
150 cfab1835 2022-10-04 thomas }
151 cfab1835 2022-10-04 thomas }
152 cfab1835 2022-10-04 thomas if (symset($1, $3, 0) == -1)
153 cfab1835 2022-10-04 thomas fatal("cannot store variable");
154 cfab1835 2022-10-04 thomas free($1);
155 cfab1835 2022-10-04 thomas free($3);
156 cfab1835 2022-10-04 thomas }
157 8a35f56c 2022-07-16 thomas ;
158 8a35f56c 2022-07-16 thomas
159 8a35f56c 2022-07-16 thomas boolean : STRING {
160 8a35f56c 2022-07-16 thomas if (strcasecmp($1, "1") == 0 ||
161 8a35f56c 2022-07-16 thomas strcasecmp($1, "on") == 0)
162 8a35f56c 2022-07-16 thomas $$ = 1;
163 8a35f56c 2022-07-16 thomas else if (strcasecmp($1, "0") == 0 ||
164 ee003af8 2023-06-15 thomas strcasecmp($1, "off") == 0)
165 8a35f56c 2022-07-16 thomas $$ = 0;
166 8a35f56c 2022-07-16 thomas else {
167 8a35f56c 2022-07-16 thomas yyerror("invalid boolean value '%s'", $1);
168 8a35f56c 2022-07-16 thomas free($1);
169 8a35f56c 2022-07-16 thomas YYERROR;
170 8a35f56c 2022-07-16 thomas }
171 8a35f56c 2022-07-16 thomas free($1);
172 8a35f56c 2022-07-16 thomas }
173 8a35f56c 2022-07-16 thomas | ON { $$ = 1; }
174 a0bd573d 2023-06-15 thomas | NUMBER {
175 a0bd573d 2023-06-15 thomas if ($1 != 0 && $1 != 1) {
176 a0bd573d 2023-06-15 thomas yyerror("invalid boolean value '%lld'", $1);
177 a0bd573d 2023-06-15 thomas YYERROR;
178 a0bd573d 2023-06-15 thomas }
179 a0bd573d 2023-06-15 thomas $$ = $1;
180 a0bd573d 2023-06-15 thomas }
181 8a35f56c 2022-07-16 thomas ;
182 8a35f56c 2022-07-16 thomas
183 77fb808d 2022-08-29 thomas fcgiport : PORT NUMBER {
184 77fb808d 2022-08-29 thomas if ($2 <= 0 || $2 > (int)USHRT_MAX) {
185 77fb808d 2022-08-29 thomas yyerror("invalid port: %lld", $2);
186 8a35f56c 2022-07-16 thomas YYERROR;
187 8a35f56c 2022-07-16 thomas }
188 77fb808d 2022-08-29 thomas $$ = $2;
189 8a35f56c 2022-07-16 thomas }
190 77fb808d 2022-08-29 thomas | PORT STRING {
191 8a35f56c 2022-07-16 thomas int val;
192 8a35f56c 2022-07-16 thomas
193 77fb808d 2022-08-29 thomas if ((val = getservice($2)) == -1) {
194 77fb808d 2022-08-29 thomas yyerror("invalid port: %s", $2);
195 77fb808d 2022-08-29 thomas free($2);
196 8a35f56c 2022-07-16 thomas YYERROR;
197 8a35f56c 2022-07-16 thomas }
198 77fb808d 2022-08-29 thomas free($2);
199 8a35f56c 2022-07-16 thomas
200 8a35f56c 2022-07-16 thomas $$ = val;
201 8a35f56c 2022-07-16 thomas }
202 8a35f56c 2022-07-16 thomas ;
203 8a35f56c 2022-07-16 thomas
204 8a35f56c 2022-07-16 thomas main : PREFORK NUMBER {
205 a0bd573d 2023-06-15 thomas if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
206 a0bd573d 2023-06-15 thomas yyerror("prefork is %s: %lld",
207 a0bd573d 2023-06-15 thomas $2 <= 0 ? "too small" : "too large", $2);
208 a0bd573d 2023-06-15 thomas YYERROR;
209 a0bd573d 2023-06-15 thomas }
210 8a35f56c 2022-07-16 thomas gotwebd->prefork_gotwebd = $2;
211 8a35f56c 2022-07-16 thomas }
212 8a35f56c 2022-07-16 thomas | CHROOT STRING {
213 90757f0a 2023-06-15 thomas if (*$2 == '\0') {
214 90757f0a 2023-06-15 thomas yyerror("chroot path can't be an empty"
215 90757f0a 2023-06-15 thomas " string");
216 90757f0a 2023-06-15 thomas free($2);
217 90757f0a 2023-06-15 thomas YYERROR;
218 90757f0a 2023-06-15 thomas }
219 90757f0a 2023-06-15 thomas
220 8a35f56c 2022-07-16 thomas n = strlcpy(gotwebd->httpd_chroot, $2,
221 8a35f56c 2022-07-16 thomas sizeof(gotwebd->httpd_chroot));
222 8a35f56c 2022-07-16 thomas if (n >= sizeof(gotwebd->httpd_chroot)) {
223 8a35f56c 2022-07-16 thomas yyerror("%s: httpd_chroot truncated", __func__);
224 8a35f56c 2022-07-16 thomas free($2);
225 8a35f56c 2022-07-16 thomas YYERROR;
226 8a35f56c 2022-07-16 thomas }
227 8a35f56c 2022-07-16 thomas free($2);
228 8a35f56c 2022-07-16 thomas }
229 8a35f56c 2022-07-16 thomas | UNIX_SOCKET boolean {
230 8a35f56c 2022-07-16 thomas gotwebd->unix_socket = $2;
231 8a35f56c 2022-07-16 thomas }
232 8a35f56c 2022-07-16 thomas | UNIX_SOCKET_NAME STRING {
233 8a35f56c 2022-07-16 thomas n = snprintf(gotwebd->unix_socket_name,
234 8a35f56c 2022-07-16 thomas sizeof(gotwebd->unix_socket_name), "%s%s",
235 8a35f56c 2022-07-16 thomas strlen(gotwebd->httpd_chroot) ?
236 8a35f56c 2022-07-16 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
237 717a78d4 2022-08-16 thomas if (n < 0 ||
238 717a78d4 2022-08-16 thomas (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
239 8a35f56c 2022-07-16 thomas yyerror("%s: unix_socket_name truncated",
240 8a35f56c 2022-07-16 thomas __func__);
241 8a35f56c 2022-07-16 thomas free($2);
242 8a35f56c 2022-07-16 thomas YYERROR;
243 8a35f56c 2022-07-16 thomas }
244 8a35f56c 2022-07-16 thomas free($2);
245 8a35f56c 2022-07-16 thomas }
246 8a35f56c 2022-07-16 thomas ;
247 8a35f56c 2022-07-16 thomas
248 8a35f56c 2022-07-16 thomas server : SERVER STRING {
249 8a35f56c 2022-07-16 thomas struct server *srv;
250 8a35f56c 2022-07-16 thomas
251 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
252 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, $2) == 0) {
253 8a35f56c 2022-07-16 thomas yyerror("server name exists '%s'", $2);
254 8a35f56c 2022-07-16 thomas free($2);
255 8a35f56c 2022-07-16 thomas YYERROR;
256 8a35f56c 2022-07-16 thomas }
257 8a35f56c 2022-07-16 thomas }
258 8a35f56c 2022-07-16 thomas
259 8a35f56c 2022-07-16 thomas new_srv = conf_new_server($2);
260 8a35f56c 2022-07-16 thomas log_debug("adding server %s", $2);
261 8a35f56c 2022-07-16 thomas free($2);
262 8a35f56c 2022-07-16 thomas }
263 8a35f56c 2022-07-16 thomas | SERVER STRING {
264 8a35f56c 2022-07-16 thomas struct server *srv;
265 8a35f56c 2022-07-16 thomas
266 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
267 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, $2) == 0) {
268 8a35f56c 2022-07-16 thomas yyerror("server name exists '%s'", $2);
269 8a35f56c 2022-07-16 thomas free($2);
270 8a35f56c 2022-07-16 thomas YYERROR;
271 8a35f56c 2022-07-16 thomas }
272 8a35f56c 2022-07-16 thomas }
273 8a35f56c 2022-07-16 thomas
274 8a35f56c 2022-07-16 thomas new_srv = conf_new_server($2);
275 8a35f56c 2022-07-16 thomas log_debug("adding server %s", $2);
276 8a35f56c 2022-07-16 thomas free($2);
277 8a35f56c 2022-07-16 thomas } '{' optnl serveropts2 '}' {
278 8a35f56c 2022-07-16 thomas }
279 8a35f56c 2022-07-16 thomas ;
280 8a35f56c 2022-07-16 thomas
281 8a35f56c 2022-07-16 thomas serveropts1 : REPOS_PATH STRING {
282 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->repos_path, $2,
283 8a35f56c 2022-07-16 thomas sizeof(new_srv->repos_path));
284 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->repos_path)) {
285 8a35f56c 2022-07-16 thomas yyerror("%s: repos_path truncated", __func__);
286 8a35f56c 2022-07-16 thomas free($2);
287 8a35f56c 2022-07-16 thomas YYERROR;
288 8a35f56c 2022-07-16 thomas }
289 8a35f56c 2022-07-16 thomas free($2);
290 8a35f56c 2022-07-16 thomas }
291 8a35f56c 2022-07-16 thomas | SITE_NAME STRING {
292 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_name, $2,
293 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_name));
294 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_name)) {
295 8a35f56c 2022-07-16 thomas yyerror("%s: site_name truncated", __func__);
296 8a35f56c 2022-07-16 thomas free($2);
297 8a35f56c 2022-07-16 thomas YYERROR;
298 8a35f56c 2022-07-16 thomas }
299 8a35f56c 2022-07-16 thomas free($2);
300 8a35f56c 2022-07-16 thomas }
301 8a35f56c 2022-07-16 thomas | SITE_OWNER STRING {
302 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_owner, $2,
303 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_owner));
304 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_owner)) {
305 8a35f56c 2022-07-16 thomas yyerror("%s: site_owner truncated", __func__);
306 8a35f56c 2022-07-16 thomas free($2);
307 8a35f56c 2022-07-16 thomas YYERROR;
308 8a35f56c 2022-07-16 thomas }
309 8a35f56c 2022-07-16 thomas free($2);
310 8a35f56c 2022-07-16 thomas }
311 8a35f56c 2022-07-16 thomas | SITE_LINK STRING {
312 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_link, $2,
313 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_link));
314 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_link)) {
315 8a35f56c 2022-07-16 thomas yyerror("%s: site_link truncated", __func__);
316 8a35f56c 2022-07-16 thomas free($2);
317 8a35f56c 2022-07-16 thomas YYERROR;
318 8a35f56c 2022-07-16 thomas }
319 8a35f56c 2022-07-16 thomas free($2);
320 8a35f56c 2022-07-16 thomas }
321 8a35f56c 2022-07-16 thomas | LOGO STRING {
322 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
323 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->logo)) {
324 8a35f56c 2022-07-16 thomas yyerror("%s: logo truncated", __func__);
325 8a35f56c 2022-07-16 thomas free($2);
326 8a35f56c 2022-07-16 thomas YYERROR;
327 8a35f56c 2022-07-16 thomas }
328 8a35f56c 2022-07-16 thomas free($2);
329 8a35f56c 2022-07-16 thomas }
330 8a35f56c 2022-07-16 thomas | LOGO_URL STRING {
331 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->logo_url, $2,
332 8a35f56c 2022-07-16 thomas sizeof(new_srv->logo_url));
333 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->logo_url)) {
334 8a35f56c 2022-07-16 thomas yyerror("%s: logo_url truncated", __func__);
335 8a35f56c 2022-07-16 thomas free($2);
336 8a35f56c 2022-07-16 thomas YYERROR;
337 8a35f56c 2022-07-16 thomas }
338 8a35f56c 2022-07-16 thomas free($2);
339 8a35f56c 2022-07-16 thomas }
340 8a35f56c 2022-07-16 thomas | CUSTOM_CSS STRING {
341 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->custom_css, $2,
342 8a35f56c 2022-07-16 thomas sizeof(new_srv->custom_css));
343 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->custom_css)) {
344 8a35f56c 2022-07-16 thomas yyerror("%s: custom_css truncated", __func__);
345 8a35f56c 2022-07-16 thomas free($2);
346 8a35f56c 2022-07-16 thomas YYERROR;
347 8a35f56c 2022-07-16 thomas }
348 8a35f56c 2022-07-16 thomas free($2);
349 8a35f56c 2022-07-16 thomas }
350 77fb808d 2022-08-29 thomas | LISTEN ON STRING fcgiport {
351 e4c7e0b0 2022-08-30 thomas if (get_addrs($3, new_srv, $4) == -1) {
352 e4c7e0b0 2022-08-30 thomas yyerror("could not get addrs");
353 77fb808d 2022-08-29 thomas YYERROR;
354 77fb808d 2022-08-29 thomas }
355 e4317279 2022-08-30 thomas new_srv->fcgi_socket = 1;
356 77fb808d 2022-08-29 thomas }
357 bea82c4f 2023-01-06 thomas | LISTEN ON SOCKET STRING {
358 ee003af8 2023-06-15 thomas if (strcasecmp($4, "off") == 0) {
359 bea82c4f 2023-01-06 thomas new_srv->unix_socket = 0;
360 bea82c4f 2023-01-06 thomas free($4);
361 bea82c4f 2023-01-06 thomas YYACCEPT;
362 bea82c4f 2023-01-06 thomas }
363 bea82c4f 2023-01-06 thomas
364 bea82c4f 2023-01-06 thomas new_srv->unix_socket = 1;
365 bea82c4f 2023-01-06 thomas
366 bea82c4f 2023-01-06 thomas n = snprintf(new_srv->unix_socket_name,
367 bea82c4f 2023-01-06 thomas sizeof(new_srv->unix_socket_name), "%s%s",
368 bea82c4f 2023-01-06 thomas strlen(gotwebd->httpd_chroot) ?
369 bea82c4f 2023-01-06 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $4);
370 bea82c4f 2023-01-06 thomas if (n < 0 ||
371 bea82c4f 2023-01-06 thomas (size_t)n >= sizeof(new_srv->unix_socket_name)) {
372 bea82c4f 2023-01-06 thomas yyerror("%s: unix_socket_name truncated",
373 bea82c4f 2023-01-06 thomas __func__);
374 bea82c4f 2023-01-06 thomas free($4);
375 bea82c4f 2023-01-06 thomas YYERROR;
376 bea82c4f 2023-01-06 thomas }
377 bea82c4f 2023-01-06 thomas free($4);
378 bea82c4f 2023-01-06 thomas }
379 8a35f56c 2022-07-16 thomas | MAX_REPOS NUMBER {
380 a0bd573d 2023-06-15 thomas if ($2 <= 0) {
381 a0bd573d 2023-06-15 thomas yyerror("max_repos is too small: %lld", $2);
382 a0bd573d 2023-06-15 thomas YYERROR;
383 a0bd573d 2023-06-15 thomas }
384 a0bd573d 2023-06-15 thomas new_srv->max_repos = $2;
385 8a35f56c 2022-07-16 thomas }
386 8a35f56c 2022-07-16 thomas | SHOW_SITE_OWNER boolean {
387 8a35f56c 2022-07-16 thomas new_srv->show_site_owner = $2;
388 8a35f56c 2022-07-16 thomas }
389 8a35f56c 2022-07-16 thomas | SHOW_REPO_OWNER boolean {
390 8a35f56c 2022-07-16 thomas new_srv->show_repo_owner = $2;
391 8a35f56c 2022-07-16 thomas }
392 8a35f56c 2022-07-16 thomas | SHOW_REPO_AGE boolean {
393 8a35f56c 2022-07-16 thomas new_srv->show_repo_age = $2;
394 8a35f56c 2022-07-16 thomas }
395 8a35f56c 2022-07-16 thomas | SHOW_REPO_DESCRIPTION boolean {
396 8a35f56c 2022-07-16 thomas new_srv->show_repo_description = $2;
397 8a35f56c 2022-07-16 thomas }
398 8a35f56c 2022-07-16 thomas | SHOW_REPO_CLONEURL boolean {
399 8a35f56c 2022-07-16 thomas new_srv->show_repo_cloneurl = $2;
400 8a35f56c 2022-07-16 thomas }
401 3991b2a5 2022-10-31 thomas | RESPECT_EXPORTOK boolean {
402 3991b2a5 2022-10-31 thomas new_srv->respect_exportok = $2;
403 3991b2a5 2022-10-31 thomas }
404 8a35f56c 2022-07-16 thomas | MAX_REPOS_DISPLAY NUMBER {
405 a0bd573d 2023-06-15 thomas if ($2 <= 0) {
406 a0bd573d 2023-06-15 thomas yyerror("max_repos_display is too small: %lld",
407 a0bd573d 2023-06-15 thomas $2);
408 a0bd573d 2023-06-15 thomas YYERROR;
409 a0bd573d 2023-06-15 thomas }
410 a0bd573d 2023-06-15 thomas new_srv->max_repos_display = $2;
411 8a35f56c 2022-07-16 thomas }
412 8a35f56c 2022-07-16 thomas | MAX_COMMITS_DISPLAY NUMBER {
413 1fa505c4 2023-06-15 thomas if ($2 <= 1) {
414 1fa505c4 2023-06-15 thomas yyerror("max_commits_display is too small:"
415 1fa505c4 2023-06-15 thomas " %lld", $2);
416 1fa505c4 2023-06-15 thomas YYERROR;
417 1fa505c4 2023-06-15 thomas }
418 1fa505c4 2023-06-15 thomas new_srv->max_commits_display = $2;
419 8a35f56c 2022-07-16 thomas }
420 8a35f56c 2022-07-16 thomas ;
421 8a35f56c 2022-07-16 thomas
422 8a35f56c 2022-07-16 thomas serveropts2 : serveropts2 serveropts1 nl
423 8a35f56c 2022-07-16 thomas | serveropts1 optnl
424 8a35f56c 2022-07-16 thomas ;
425 8a35f56c 2022-07-16 thomas
426 8a35f56c 2022-07-16 thomas nl : '\n' optnl
427 8a35f56c 2022-07-16 thomas ;
428 8a35f56c 2022-07-16 thomas
429 8a35f56c 2022-07-16 thomas optnl : '\n' optnl /* zero or more newlines */
430 8a35f56c 2022-07-16 thomas | /* empty */
431 8a35f56c 2022-07-16 thomas ;
432 8a35f56c 2022-07-16 thomas
433 8a35f56c 2022-07-16 thomas %%
434 8a35f56c 2022-07-16 thomas
435 8a35f56c 2022-07-16 thomas struct keywords {
436 8a35f56c 2022-07-16 thomas const char *k_name;
437 8a35f56c 2022-07-16 thomas int k_val;
438 8a35f56c 2022-07-16 thomas };
439 8a35f56c 2022-07-16 thomas
440 8a35f56c 2022-07-16 thomas int
441 8a35f56c 2022-07-16 thomas yyerror(const char *fmt, ...)
442 8a35f56c 2022-07-16 thomas {
443 8a35f56c 2022-07-16 thomas va_list ap;
444 8a35f56c 2022-07-16 thomas char *msg;
445 8a35f56c 2022-07-16 thomas
446 8a35f56c 2022-07-16 thomas file->errors++;
447 8a35f56c 2022-07-16 thomas va_start(ap, fmt);
448 8a35f56c 2022-07-16 thomas if (vasprintf(&msg, fmt, ap) == -1)
449 8a35f56c 2022-07-16 thomas fatalx("yyerror vasprintf");
450 8a35f56c 2022-07-16 thomas va_end(ap);
451 8a35f56c 2022-07-16 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
452 8a35f56c 2022-07-16 thomas free(msg);
453 8a35f56c 2022-07-16 thomas return (0);
454 8a35f56c 2022-07-16 thomas }
455 8a35f56c 2022-07-16 thomas
456 8a35f56c 2022-07-16 thomas int
457 8a35f56c 2022-07-16 thomas kw_cmp(const void *k, const void *e)
458 8a35f56c 2022-07-16 thomas {
459 8a35f56c 2022-07-16 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
460 8a35f56c 2022-07-16 thomas }
461 8a35f56c 2022-07-16 thomas
462 8a35f56c 2022-07-16 thomas int
463 8a35f56c 2022-07-16 thomas lookup(char *s)
464 8a35f56c 2022-07-16 thomas {
465 8a35f56c 2022-07-16 thomas /* This has to be sorted always. */
466 8a35f56c 2022-07-16 thomas static const struct keywords keywords[] = {
467 8a35f56c 2022-07-16 thomas { "chroot", CHROOT },
468 8a35f56c 2022-07-16 thomas { "custom_css", CUSTOM_CSS },
469 c24a47af 2022-08-11 thomas { "listen", LISTEN },
470 8a35f56c 2022-07-16 thomas { "logo", LOGO },
471 f0b4ad15 2023-01-02 thomas { "logo_url", LOGO_URL },
472 8a35f56c 2022-07-16 thomas { "max_commits_display", MAX_COMMITS_DISPLAY },
473 8a35f56c 2022-07-16 thomas { "max_repos", MAX_REPOS },
474 8a35f56c 2022-07-16 thomas { "max_repos_display", MAX_REPOS_DISPLAY },
475 c24a47af 2022-08-11 thomas { "on", ON },
476 8a35f56c 2022-07-16 thomas { "port", PORT },
477 8a35f56c 2022-07-16 thomas { "prefork", PREFORK },
478 8a35f56c 2022-07-16 thomas { "repos_path", REPOS_PATH },
479 3991b2a5 2022-10-31 thomas { "respect_exportok", RESPECT_EXPORTOK },
480 8a35f56c 2022-07-16 thomas { "server", SERVER },
481 8a35f56c 2022-07-16 thomas { "show_repo_age", SHOW_REPO_AGE },
482 8a35f56c 2022-07-16 thomas { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
483 8a35f56c 2022-07-16 thomas { "show_repo_description", SHOW_REPO_DESCRIPTION },
484 8a35f56c 2022-07-16 thomas { "show_repo_owner", SHOW_REPO_OWNER },
485 8a35f56c 2022-07-16 thomas { "show_site_owner", SHOW_SITE_OWNER },
486 8a35f56c 2022-07-16 thomas { "site_link", SITE_LINK },
487 8a35f56c 2022-07-16 thomas { "site_name", SITE_NAME },
488 8a35f56c 2022-07-16 thomas { "site_owner", SITE_OWNER },
489 bea82c4f 2023-01-06 thomas { "socket", SOCKET },
490 8a35f56c 2022-07-16 thomas { "unix_socket", UNIX_SOCKET },
491 8a35f56c 2022-07-16 thomas { "unix_socket_name", UNIX_SOCKET_NAME },
492 8a35f56c 2022-07-16 thomas };
493 8a35f56c 2022-07-16 thomas const struct keywords *p;
494 8a35f56c 2022-07-16 thomas
495 8a35f56c 2022-07-16 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
496 8a35f56c 2022-07-16 thomas sizeof(keywords[0]), kw_cmp);
497 8a35f56c 2022-07-16 thomas
498 8a35f56c 2022-07-16 thomas if (p)
499 8a35f56c 2022-07-16 thomas return (p->k_val);
500 8a35f56c 2022-07-16 thomas else
501 8a35f56c 2022-07-16 thomas return (STRING);
502 8a35f56c 2022-07-16 thomas }
503 8a35f56c 2022-07-16 thomas
504 8a35f56c 2022-07-16 thomas #define MAXPUSHBACK 128
505 8a35f56c 2022-07-16 thomas
506 8a35f56c 2022-07-16 thomas unsigned char *parsebuf;
507 8a35f56c 2022-07-16 thomas int parseindex;
508 8a35f56c 2022-07-16 thomas unsigned char pushback_buffer[MAXPUSHBACK];
509 8a35f56c 2022-07-16 thomas int pushback_index = 0;
510 8a35f56c 2022-07-16 thomas
511 8a35f56c 2022-07-16 thomas int
512 8a35f56c 2022-07-16 thomas lgetc(int quotec)
513 8a35f56c 2022-07-16 thomas {
514 8a35f56c 2022-07-16 thomas int c, next;
515 8a35f56c 2022-07-16 thomas
516 8a35f56c 2022-07-16 thomas if (parsebuf) {
517 8a35f56c 2022-07-16 thomas /* Read character from the parsebuffer instead of input. */
518 8a35f56c 2022-07-16 thomas if (parseindex >= 0) {
519 8a35f56c 2022-07-16 thomas c = parsebuf[parseindex++];
520 8a35f56c 2022-07-16 thomas if (c != '\0')
521 8a35f56c 2022-07-16 thomas return (c);
522 8a35f56c 2022-07-16 thomas parsebuf = NULL;
523 8a35f56c 2022-07-16 thomas } else
524 8a35f56c 2022-07-16 thomas parseindex++;
525 8a35f56c 2022-07-16 thomas }
526 8a35f56c 2022-07-16 thomas
527 8a35f56c 2022-07-16 thomas if (pushback_index)
528 8a35f56c 2022-07-16 thomas return (pushback_buffer[--pushback_index]);
529 8a35f56c 2022-07-16 thomas
530 8a35f56c 2022-07-16 thomas if (quotec) {
531 8a35f56c 2022-07-16 thomas c = getc(file->stream);
532 8a35f56c 2022-07-16 thomas if (c == EOF)
533 8a35f56c 2022-07-16 thomas yyerror("reached end of file while parsing "
534 8a35f56c 2022-07-16 thomas "quoted string");
535 8a35f56c 2022-07-16 thomas return (c);
536 8a35f56c 2022-07-16 thomas }
537 8a35f56c 2022-07-16 thomas
538 8a35f56c 2022-07-16 thomas c = getc(file->stream);
539 8a35f56c 2022-07-16 thomas while (c == '\\') {
540 8a35f56c 2022-07-16 thomas next = getc(file->stream);
541 8a35f56c 2022-07-16 thomas if (next != '\n') {
542 8a35f56c 2022-07-16 thomas c = next;
543 8a35f56c 2022-07-16 thomas break;
544 8a35f56c 2022-07-16 thomas }
545 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
546 8a35f56c 2022-07-16 thomas file->lineno++;
547 8a35f56c 2022-07-16 thomas c = getc(file->stream);
548 8a35f56c 2022-07-16 thomas }
549 8a35f56c 2022-07-16 thomas
550 8a35f56c 2022-07-16 thomas return (c);
551 8a35f56c 2022-07-16 thomas }
552 8a35f56c 2022-07-16 thomas
553 8a35f56c 2022-07-16 thomas int
554 8a35f56c 2022-07-16 thomas lungetc(int c)
555 8a35f56c 2022-07-16 thomas {
556 8a35f56c 2022-07-16 thomas if (c == EOF)
557 8a35f56c 2022-07-16 thomas return (EOF);
558 8a35f56c 2022-07-16 thomas if (parsebuf) {
559 8a35f56c 2022-07-16 thomas parseindex--;
560 8a35f56c 2022-07-16 thomas if (parseindex >= 0)
561 8a35f56c 2022-07-16 thomas return (c);
562 8a35f56c 2022-07-16 thomas }
563 8a35f56c 2022-07-16 thomas if (pushback_index < MAXPUSHBACK-1)
564 8a35f56c 2022-07-16 thomas return (pushback_buffer[pushback_index++] = c);
565 8a35f56c 2022-07-16 thomas else
566 8a35f56c 2022-07-16 thomas return (EOF);
567 8a35f56c 2022-07-16 thomas }
568 8a35f56c 2022-07-16 thomas
569 8a35f56c 2022-07-16 thomas int
570 8a35f56c 2022-07-16 thomas findeol(void)
571 8a35f56c 2022-07-16 thomas {
572 8a35f56c 2022-07-16 thomas int c;
573 8a35f56c 2022-07-16 thomas
574 8a35f56c 2022-07-16 thomas parsebuf = NULL;
575 8a35f56c 2022-07-16 thomas
576 8a35f56c 2022-07-16 thomas /* Skip to either EOF or the first real EOL. */
577 8a35f56c 2022-07-16 thomas while (1) {
578 8a35f56c 2022-07-16 thomas if (pushback_index)
579 8a35f56c 2022-07-16 thomas c = pushback_buffer[--pushback_index];
580 8a35f56c 2022-07-16 thomas else
581 8a35f56c 2022-07-16 thomas c = lgetc(0);
582 8a35f56c 2022-07-16 thomas if (c == '\n') {
583 8a35f56c 2022-07-16 thomas file->lineno++;
584 8a35f56c 2022-07-16 thomas break;
585 8a35f56c 2022-07-16 thomas }
586 8a35f56c 2022-07-16 thomas if (c == EOF)
587 8a35f56c 2022-07-16 thomas break;
588 8a35f56c 2022-07-16 thomas }
589 8a35f56c 2022-07-16 thomas return (ERROR);
590 8a35f56c 2022-07-16 thomas }
591 8a35f56c 2022-07-16 thomas
592 8a35f56c 2022-07-16 thomas int
593 8a35f56c 2022-07-16 thomas yylex(void)
594 8a35f56c 2022-07-16 thomas {
595 8a35f56c 2022-07-16 thomas unsigned char buf[8096];
596 8a35f56c 2022-07-16 thomas unsigned char *p, *val;
597 8a35f56c 2022-07-16 thomas int quotec, next, c;
598 8a35f56c 2022-07-16 thomas int token;
599 8a35f56c 2022-07-16 thomas
600 8a35f56c 2022-07-16 thomas top:
601 8a35f56c 2022-07-16 thomas p = buf;
602 8a35f56c 2022-07-16 thomas c = lgetc(0);
603 8a35f56c 2022-07-16 thomas while (c == ' ' || c == '\t')
604 8a35f56c 2022-07-16 thomas c = lgetc(0); /* nothing */
605 8a35f56c 2022-07-16 thomas
606 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
607 8a35f56c 2022-07-16 thomas if (c == '#') {
608 8a35f56c 2022-07-16 thomas c = lgetc(0);
609 8a35f56c 2022-07-16 thomas while (c != '\n' && c != EOF)
610 8a35f56c 2022-07-16 thomas c = lgetc(0); /* nothing */
611 8a35f56c 2022-07-16 thomas }
612 8a35f56c 2022-07-16 thomas if (c == '$' && parsebuf == NULL) {
613 8a35f56c 2022-07-16 thomas while (1) {
614 8a35f56c 2022-07-16 thomas c = lgetc(0);
615 8a35f56c 2022-07-16 thomas if (c == EOF)
616 8a35f56c 2022-07-16 thomas return (0);
617 8a35f56c 2022-07-16 thomas
618 8a35f56c 2022-07-16 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
619 8a35f56c 2022-07-16 thomas yyerror("string too long");
620 8a35f56c 2022-07-16 thomas return (findeol());
621 8a35f56c 2022-07-16 thomas }
622 8a35f56c 2022-07-16 thomas if (isalnum(c) || c == '_') {
623 8a35f56c 2022-07-16 thomas *p++ = c;
624 8a35f56c 2022-07-16 thomas continue;
625 8a35f56c 2022-07-16 thomas }
626 8a35f56c 2022-07-16 thomas *p = '\0';
627 8a35f56c 2022-07-16 thomas lungetc(c);
628 8a35f56c 2022-07-16 thomas break;
629 8a35f56c 2022-07-16 thomas }
630 8a35f56c 2022-07-16 thomas val = symget(buf);
631 8a35f56c 2022-07-16 thomas if (val == NULL) {
632 8a35f56c 2022-07-16 thomas yyerror("macro '%s' not defined", buf);
633 8a35f56c 2022-07-16 thomas return (findeol());
634 8a35f56c 2022-07-16 thomas }
635 8a35f56c 2022-07-16 thomas parsebuf = val;
636 8a35f56c 2022-07-16 thomas parseindex = 0;
637 8a35f56c 2022-07-16 thomas goto top;
638 8a35f56c 2022-07-16 thomas }
639 8a35f56c 2022-07-16 thomas
640 8a35f56c 2022-07-16 thomas switch (c) {
641 8a35f56c 2022-07-16 thomas case '\'':
642 8a35f56c 2022-07-16 thomas case '"':
643 8a35f56c 2022-07-16 thomas quotec = c;
644 8a35f56c 2022-07-16 thomas while (1) {
645 8a35f56c 2022-07-16 thomas c = lgetc(quotec);
646 8a35f56c 2022-07-16 thomas if (c == EOF)
647 8a35f56c 2022-07-16 thomas return (0);
648 8a35f56c 2022-07-16 thomas if (c == '\n') {
649 8a35f56c 2022-07-16 thomas file->lineno++;
650 8a35f56c 2022-07-16 thomas continue;
651 8a35f56c 2022-07-16 thomas } else if (c == '\\') {
652 8a35f56c 2022-07-16 thomas next = lgetc(quotec);
653 8a35f56c 2022-07-16 thomas if (next == EOF)
654 8a35f56c 2022-07-16 thomas return (0);
655 8a35f56c 2022-07-16 thomas if (next == quotec || c == ' ' || c == '\t')
656 8a35f56c 2022-07-16 thomas c = next;
657 8a35f56c 2022-07-16 thomas else if (next == '\n') {
658 8a35f56c 2022-07-16 thomas file->lineno++;
659 8a35f56c 2022-07-16 thomas continue;
660 8a35f56c 2022-07-16 thomas } else
661 8a35f56c 2022-07-16 thomas lungetc(next);
662 8a35f56c 2022-07-16 thomas } else if (c == quotec) {
663 8a35f56c 2022-07-16 thomas *p = '\0';
664 8a35f56c 2022-07-16 thomas break;
665 8a35f56c 2022-07-16 thomas } else if (c == '\0') {
666 8a35f56c 2022-07-16 thomas yyerror("syntax error");
667 8a35f56c 2022-07-16 thomas return (findeol());
668 8a35f56c 2022-07-16 thomas }
669 8a35f56c 2022-07-16 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
670 8a35f56c 2022-07-16 thomas yyerror("string too long");
671 8a35f56c 2022-07-16 thomas return (findeol());
672 8a35f56c 2022-07-16 thomas }
673 8a35f56c 2022-07-16 thomas *p++ = c;
674 8a35f56c 2022-07-16 thomas }
675 8a35f56c 2022-07-16 thomas yylval.v.string = strdup(buf);
676 8a35f56c 2022-07-16 thomas if (yylval.v.string == NULL)
677 8a35f56c 2022-07-16 thomas err(1, "yylex: strdup");
678 8a35f56c 2022-07-16 thomas return (STRING);
679 8a35f56c 2022-07-16 thomas }
680 8a35f56c 2022-07-16 thomas
681 8a35f56c 2022-07-16 thomas #define allowed_to_end_number(x) \
682 8a35f56c 2022-07-16 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
683 8a35f56c 2022-07-16 thomas
684 8a35f56c 2022-07-16 thomas if (c == '-' || isdigit(c)) {
685 8a35f56c 2022-07-16 thomas do {
686 8a35f56c 2022-07-16 thomas *p++ = c;
687 8a35f56c 2022-07-16 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
688 8a35f56c 2022-07-16 thomas yyerror("string too long");
689 8a35f56c 2022-07-16 thomas return (findeol());
690 8a35f56c 2022-07-16 thomas }
691 8a35f56c 2022-07-16 thomas c = lgetc(0);
692 8a35f56c 2022-07-16 thomas } while (c != EOF && isdigit(c));
693 8a35f56c 2022-07-16 thomas lungetc(c);
694 8a35f56c 2022-07-16 thomas if (p == buf + 1 && buf[0] == '-')
695 8a35f56c 2022-07-16 thomas goto nodigits;
696 8a35f56c 2022-07-16 thomas if (c == EOF || allowed_to_end_number(c)) {
697 8a35f56c 2022-07-16 thomas const char *errstr = NULL;
698 8a35f56c 2022-07-16 thomas
699 8a35f56c 2022-07-16 thomas *p = '\0';
700 8a35f56c 2022-07-16 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
701 8a35f56c 2022-07-16 thomas LLONG_MAX, &errstr);
702 8a35f56c 2022-07-16 thomas if (errstr) {
703 8a35f56c 2022-07-16 thomas yyerror("\"%s\" invalid number: %s",
704 8a35f56c 2022-07-16 thomas buf, errstr);
705 8a35f56c 2022-07-16 thomas return (findeol());
706 8a35f56c 2022-07-16 thomas }
707 8a35f56c 2022-07-16 thomas return (NUMBER);
708 8a35f56c 2022-07-16 thomas } else {
709 8a35f56c 2022-07-16 thomas nodigits:
710 8a35f56c 2022-07-16 thomas while (p > buf + 1)
711 8a35f56c 2022-07-16 thomas lungetc(*--p);
712 8a35f56c 2022-07-16 thomas c = *--p;
713 8a35f56c 2022-07-16 thomas if (c == '-')
714 8a35f56c 2022-07-16 thomas return (c);
715 8a35f56c 2022-07-16 thomas }
716 8a35f56c 2022-07-16 thomas }
717 8a35f56c 2022-07-16 thomas
718 8a35f56c 2022-07-16 thomas #define allowed_in_string(x) \
719 8a35f56c 2022-07-16 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
720 8a35f56c 2022-07-16 thomas x != '{' && x != '}' && \
721 8a35f56c 2022-07-16 thomas x != '!' && x != '=' && x != '#' && \
722 8a35f56c 2022-07-16 thomas x != ','))
723 8a35f56c 2022-07-16 thomas
724 8a35f56c 2022-07-16 thomas if (isalnum(c) || c == ':' || c == '_') {
725 8a35f56c 2022-07-16 thomas do {
726 8a35f56c 2022-07-16 thomas *p++ = c;
727 8a35f56c 2022-07-16 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
728 8a35f56c 2022-07-16 thomas yyerror("string too long");
729 8a35f56c 2022-07-16 thomas return (findeol());
730 8a35f56c 2022-07-16 thomas }
731 8a35f56c 2022-07-16 thomas c = lgetc(0);
732 8a35f56c 2022-07-16 thomas } while (c != EOF && (allowed_in_string(c)));
733 8a35f56c 2022-07-16 thomas lungetc(c);
734 8a35f56c 2022-07-16 thomas *p = '\0';
735 8a35f56c 2022-07-16 thomas token = lookup(buf);
736 8a35f56c 2022-07-16 thomas if (token == STRING) {
737 8a35f56c 2022-07-16 thomas yylval.v.string = strdup(buf);
738 8a35f56c 2022-07-16 thomas if (yylval.v.string == NULL)
739 8a35f56c 2022-07-16 thomas err(1, "yylex: strdup");
740 8a35f56c 2022-07-16 thomas }
741 8a35f56c 2022-07-16 thomas return (token);
742 8a35f56c 2022-07-16 thomas }
743 8a35f56c 2022-07-16 thomas if (c == '\n') {
744 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
745 8a35f56c 2022-07-16 thomas file->lineno++;
746 8a35f56c 2022-07-16 thomas }
747 8a35f56c 2022-07-16 thomas if (c == EOF)
748 8a35f56c 2022-07-16 thomas return (0);
749 8a35f56c 2022-07-16 thomas return (c);
750 8a35f56c 2022-07-16 thomas }
751 8a35f56c 2022-07-16 thomas
752 8a35f56c 2022-07-16 thomas int
753 8a35f56c 2022-07-16 thomas check_file_secrecy(int fd, const char *fname)
754 8a35f56c 2022-07-16 thomas {
755 8a35f56c 2022-07-16 thomas struct stat st;
756 8a35f56c 2022-07-16 thomas
757 8a35f56c 2022-07-16 thomas if (fstat(fd, &st)) {
758 8a35f56c 2022-07-16 thomas log_warn("cannot stat %s", fname);
759 8a35f56c 2022-07-16 thomas return (-1);
760 8a35f56c 2022-07-16 thomas }
761 8a35f56c 2022-07-16 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
762 8a35f56c 2022-07-16 thomas log_warnx("%s: owner not root or current user", fname);
763 8a35f56c 2022-07-16 thomas return (-1);
764 8a35f56c 2022-07-16 thomas }
765 8a35f56c 2022-07-16 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
766 8a35f56c 2022-07-16 thomas log_warnx("%s: group writable or world read/writable", fname);
767 8a35f56c 2022-07-16 thomas return (-1);
768 8a35f56c 2022-07-16 thomas }
769 8a35f56c 2022-07-16 thomas return (0);
770 8a35f56c 2022-07-16 thomas }
771 8a35f56c 2022-07-16 thomas
772 8a35f56c 2022-07-16 thomas struct file *
773 8a35f56c 2022-07-16 thomas newfile(const char *name, int secret)
774 8a35f56c 2022-07-16 thomas {
775 8a35f56c 2022-07-16 thomas struct file *nfile;
776 8a35f56c 2022-07-16 thomas
777 8a35f56c 2022-07-16 thomas nfile = calloc(1, sizeof(struct file));
778 8a35f56c 2022-07-16 thomas if (nfile == NULL) {
779 8a35f56c 2022-07-16 thomas log_warn("calloc");
780 8a35f56c 2022-07-16 thomas return (NULL);
781 8a35f56c 2022-07-16 thomas }
782 8a35f56c 2022-07-16 thomas nfile->name = strdup(name);
783 8a35f56c 2022-07-16 thomas if (nfile->name == NULL) {
784 8a35f56c 2022-07-16 thomas log_warn("strdup");
785 8a35f56c 2022-07-16 thomas free(nfile);
786 8a35f56c 2022-07-16 thomas return (NULL);
787 8a35f56c 2022-07-16 thomas }
788 8a35f56c 2022-07-16 thomas nfile->stream = fopen(nfile->name, "r");
789 8a35f56c 2022-07-16 thomas if (nfile->stream == NULL) {
790 8a35f56c 2022-07-16 thomas /* no warning, we don't require a conf file */
791 8a35f56c 2022-07-16 thomas free(nfile->name);
792 8a35f56c 2022-07-16 thomas free(nfile);
793 8a35f56c 2022-07-16 thomas return (NULL);
794 8a35f56c 2022-07-16 thomas } else if (secret &&
795 8a35f56c 2022-07-16 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
796 8a35f56c 2022-07-16 thomas fclose(nfile->stream);
797 8a35f56c 2022-07-16 thomas free(nfile->name);
798 8a35f56c 2022-07-16 thomas free(nfile);
799 8a35f56c 2022-07-16 thomas return (NULL);
800 8a35f56c 2022-07-16 thomas }
801 8a35f56c 2022-07-16 thomas nfile->lineno = 1;
802 8a35f56c 2022-07-16 thomas return (nfile);
803 8a35f56c 2022-07-16 thomas }
804 8a35f56c 2022-07-16 thomas
805 8a35f56c 2022-07-16 thomas static void
806 8a35f56c 2022-07-16 thomas closefile(struct file *xfile)
807 8a35f56c 2022-07-16 thomas {
808 8a35f56c 2022-07-16 thomas fclose(xfile->stream);
809 8a35f56c 2022-07-16 thomas free(xfile->name);
810 8a35f56c 2022-07-16 thomas free(xfile);
811 8a35f56c 2022-07-16 thomas }
812 8a35f56c 2022-07-16 thomas
813 9f849004 2022-08-06 thomas static void
814 9f849004 2022-08-06 thomas add_default_server(void)
815 9f849004 2022-08-06 thomas {
816 9f849004 2022-08-06 thomas new_srv = conf_new_server(D_SITENAME);
817 9f849004 2022-08-06 thomas log_debug("%s: adding default server %s", __func__, D_SITENAME);
818 9f849004 2022-08-06 thomas }
819 9f849004 2022-08-06 thomas
820 8a35f56c 2022-07-16 thomas int
821 8a35f56c 2022-07-16 thomas parse_config(const char *filename, struct gotwebd *env)
822 8a35f56c 2022-07-16 thomas {
823 8a35f56c 2022-07-16 thomas struct sym *sym, *next;
824 8a35f56c 2022-07-16 thomas
825 8a35f56c 2022-07-16 thomas if (config_init(env) == -1)
826 8a35f56c 2022-07-16 thomas fatalx("failed to initialize configuration");
827 8a35f56c 2022-07-16 thomas
828 8a35f56c 2022-07-16 thomas gotwebd = env;
829 9f849004 2022-08-06 thomas
830 9f849004 2022-08-06 thomas file = newfile(filename, 0);
831 9f849004 2022-08-06 thomas if (file == NULL) {
832 9f849004 2022-08-06 thomas add_default_server();
833 9f849004 2022-08-06 thomas sockets_parse_sockets(env);
834 9f849004 2022-08-06 thomas /* just return, as we don't require a conf file */
835 9f849004 2022-08-06 thomas return (0);
836 9f849004 2022-08-06 thomas }
837 8a35f56c 2022-07-16 thomas
838 8a35f56c 2022-07-16 thomas yyparse();
839 8a35f56c 2022-07-16 thomas errors = file->errors;
840 8a35f56c 2022-07-16 thomas closefile(file);
841 8a35f56c 2022-07-16 thomas
842 8a35f56c 2022-07-16 thomas /* Free macros and check which have not been used. */
843 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
844 8a35f56c 2022-07-16 thomas if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
845 8a35f56c 2022-07-16 thomas fprintf(stderr, "warning: macro '%s' not used\n",
846 8a35f56c 2022-07-16 thomas sym->nam);
847 8a35f56c 2022-07-16 thomas if (!sym->persist) {
848 8a35f56c 2022-07-16 thomas free(sym->nam);
849 8a35f56c 2022-07-16 thomas free(sym->val);
850 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&symhead, sym, entry);
851 8a35f56c 2022-07-16 thomas free(sym);
852 8a35f56c 2022-07-16 thomas }
853 8a35f56c 2022-07-16 thomas }
854 8a35f56c 2022-07-16 thomas
855 8a35f56c 2022-07-16 thomas if (errors)
856 8a35f56c 2022-07-16 thomas return (-1);
857 8a35f56c 2022-07-16 thomas
858 8a35f56c 2022-07-16 thomas /* just add default server if no config specified */
859 9f849004 2022-08-06 thomas if (gotwebd->server_cnt == 0)
860 9f849004 2022-08-06 thomas add_default_server();
861 8a35f56c 2022-07-16 thomas
862 8a35f56c 2022-07-16 thomas /* setup our listening sockets */
863 8a35f56c 2022-07-16 thomas sockets_parse_sockets(env);
864 8a35f56c 2022-07-16 thomas
865 8a35f56c 2022-07-16 thomas return (0);
866 8a35f56c 2022-07-16 thomas }
867 8a35f56c 2022-07-16 thomas
868 8a35f56c 2022-07-16 thomas struct server *
869 8a35f56c 2022-07-16 thomas conf_new_server(const char *name)
870 8a35f56c 2022-07-16 thomas {
871 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
872 8a35f56c 2022-07-16 thomas
873 8a35f56c 2022-07-16 thomas srv = calloc(1, sizeof(*srv));
874 8a35f56c 2022-07-16 thomas if (srv == NULL)
875 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
876 8a35f56c 2022-07-16 thomas
877 8a35f56c 2022-07-16 thomas n = strlcpy(srv->name, name, sizeof(srv->name));
878 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->name))
879 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
880 8a35f56c 2022-07-16 thomas n = snprintf(srv->unix_socket_name,
881 8a35f56c 2022-07-16 thomas sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
882 8a35f56c 2022-07-16 thomas D_UNIX_SOCKET);
883 717a78d4 2022-08-16 thomas if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
884 8a35f56c 2022-07-16 thomas fatalx("%s: snprintf", __func__);
885 8a35f56c 2022-07-16 thomas n = strlcpy(srv->repos_path, D_GOTPATH,
886 8a35f56c 2022-07-16 thomas sizeof(srv->repos_path));
887 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->repos_path))
888 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
889 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_name, D_SITENAME,
890 8a35f56c 2022-07-16 thomas sizeof(srv->site_name));
891 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_name))
892 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
893 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_owner, D_SITEOWNER,
894 8a35f56c 2022-07-16 thomas sizeof(srv->site_owner));
895 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_owner))
896 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
897 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_link, D_SITELINK,
898 8a35f56c 2022-07-16 thomas sizeof(srv->site_link));
899 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_link))
900 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
901 8a35f56c 2022-07-16 thomas n = strlcpy(srv->logo, D_GOTLOGO,
902 8a35f56c 2022-07-16 thomas sizeof(srv->logo));
903 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->logo))
904 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
905 8a35f56c 2022-07-16 thomas n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
906 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->logo_url))
907 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
908 8a35f56c 2022-07-16 thomas n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
909 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->custom_css))
910 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
911 8a35f56c 2022-07-16 thomas
912 8a35f56c 2022-07-16 thomas srv->show_site_owner = D_SHOWSOWNER;
913 8a35f56c 2022-07-16 thomas srv->show_repo_owner = D_SHOWROWNER;
914 8a35f56c 2022-07-16 thomas srv->show_repo_age = D_SHOWAGE;
915 8a35f56c 2022-07-16 thomas srv->show_repo_description = D_SHOWDESC;
916 8a35f56c 2022-07-16 thomas srv->show_repo_cloneurl = D_SHOWURL;
917 3991b2a5 2022-10-31 thomas srv->respect_exportok = D_RESPECTEXPORTOK;
918 8a35f56c 2022-07-16 thomas
919 8a35f56c 2022-07-16 thomas srv->max_repos_display = D_MAXREPODISP;
920 8a35f56c 2022-07-16 thomas srv->max_commits_display = D_MAXCOMMITDISP;
921 8a35f56c 2022-07-16 thomas srv->max_repos = D_MAXREPO;
922 8a35f56c 2022-07-16 thomas
923 8a35f56c 2022-07-16 thomas srv->unix_socket = 1;
924 e4317279 2022-08-30 thomas srv->fcgi_socket = 0;
925 8a35f56c 2022-07-16 thomas
926 62f85214 2022-08-16 thomas TAILQ_INIT(&srv->al);
927 90d63d47 2022-08-16 thomas TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
928 8a35f56c 2022-07-16 thomas gotwebd->server_cnt++;
929 8a35f56c 2022-07-16 thomas
930 8a35f56c 2022-07-16 thomas return srv;
931 8a35f56c 2022-07-16 thomas };
932 8a35f56c 2022-07-16 thomas
933 8a35f56c 2022-07-16 thomas int
934 8a35f56c 2022-07-16 thomas symset(const char *nam, const char *val, int persist)
935 8a35f56c 2022-07-16 thomas {
936 8a35f56c 2022-07-16 thomas struct sym *sym;
937 8a35f56c 2022-07-16 thomas
938 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sym, &symhead, entry) {
939 8a35f56c 2022-07-16 thomas if (strcmp(nam, sym->nam) == 0)
940 8a35f56c 2022-07-16 thomas break;
941 8a35f56c 2022-07-16 thomas }
942 8a35f56c 2022-07-16 thomas
943 8a35f56c 2022-07-16 thomas if (sym != NULL) {
944 8a35f56c 2022-07-16 thomas if (sym->persist == 1)
945 8a35f56c 2022-07-16 thomas return (0);
946 8a35f56c 2022-07-16 thomas else {
947 8a35f56c 2022-07-16 thomas free(sym->nam);
948 8a35f56c 2022-07-16 thomas free(sym->val);
949 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&symhead, sym, entry);
950 8a35f56c 2022-07-16 thomas free(sym);
951 8a35f56c 2022-07-16 thomas }
952 8a35f56c 2022-07-16 thomas }
953 8a35f56c 2022-07-16 thomas sym = calloc(1, sizeof(*sym));
954 8a35f56c 2022-07-16 thomas if (sym == NULL)
955 8a35f56c 2022-07-16 thomas return (-1);
956 8a35f56c 2022-07-16 thomas
957 8a35f56c 2022-07-16 thomas sym->nam = strdup(nam);
958 8a35f56c 2022-07-16 thomas if (sym->nam == NULL) {
959 8a35f56c 2022-07-16 thomas free(sym);
960 8a35f56c 2022-07-16 thomas return (-1);
961 8a35f56c 2022-07-16 thomas }
962 8a35f56c 2022-07-16 thomas sym->val = strdup(val);
963 8a35f56c 2022-07-16 thomas if (sym->val == NULL) {
964 8a35f56c 2022-07-16 thomas free(sym->nam);
965 8a35f56c 2022-07-16 thomas free(sym);
966 8a35f56c 2022-07-16 thomas return (-1);
967 8a35f56c 2022-07-16 thomas }
968 8a35f56c 2022-07-16 thomas sym->used = 0;
969 8a35f56c 2022-07-16 thomas sym->persist = persist;
970 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
971 8a35f56c 2022-07-16 thomas return (0);
972 8a35f56c 2022-07-16 thomas }
973 8a35f56c 2022-07-16 thomas
974 8a35f56c 2022-07-16 thomas int
975 8a35f56c 2022-07-16 thomas cmdline_symset(char *s)
976 8a35f56c 2022-07-16 thomas {
977 8a35f56c 2022-07-16 thomas char *sym, *val;
978 8a35f56c 2022-07-16 thomas int ret;
979 8a35f56c 2022-07-16 thomas
980 8a35f56c 2022-07-16 thomas val = strrchr(s, '=');
981 8a35f56c 2022-07-16 thomas if (val == NULL)
982 8a35f56c 2022-07-16 thomas return (-1);
983 8a35f56c 2022-07-16 thomas
984 43be1edb 2022-09-05 thomas sym = strndup(s, val - s);
985 8a35f56c 2022-07-16 thomas if (sym == NULL)
986 43be1edb 2022-09-05 thomas fatal("%s: strndup", __func__);
987 8a35f56c 2022-07-16 thomas
988 8a35f56c 2022-07-16 thomas ret = symset(sym, val + 1, 1);
989 8a35f56c 2022-07-16 thomas free(sym);
990 8a35f56c 2022-07-16 thomas
991 8a35f56c 2022-07-16 thomas return (ret);
992 8a35f56c 2022-07-16 thomas }
993 8a35f56c 2022-07-16 thomas
994 8a35f56c 2022-07-16 thomas char *
995 8a35f56c 2022-07-16 thomas symget(const char *nam)
996 8a35f56c 2022-07-16 thomas {
997 8a35f56c 2022-07-16 thomas struct sym *sym;
998 8a35f56c 2022-07-16 thomas
999 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1000 8a35f56c 2022-07-16 thomas if (strcmp(nam, sym->nam) == 0) {
1001 8a35f56c 2022-07-16 thomas sym->used = 1;
1002 8a35f56c 2022-07-16 thomas return (sym->val);
1003 8a35f56c 2022-07-16 thomas }
1004 8a35f56c 2022-07-16 thomas }
1005 8a35f56c 2022-07-16 thomas return (NULL);
1006 8a35f56c 2022-07-16 thomas }
1007 8a35f56c 2022-07-16 thomas
1008 8a35f56c 2022-07-16 thomas int
1009 8a35f56c 2022-07-16 thomas getservice(const char *n)
1010 8a35f56c 2022-07-16 thomas {
1011 8a35f56c 2022-07-16 thomas struct servent *s;
1012 8a35f56c 2022-07-16 thomas const char *errstr;
1013 8a35f56c 2022-07-16 thomas long long llval;
1014 8a35f56c 2022-07-16 thomas
1015 8a35f56c 2022-07-16 thomas llval = strtonum(n, 0, UINT16_MAX, &errstr);
1016 8a35f56c 2022-07-16 thomas if (errstr) {
1017 8a35f56c 2022-07-16 thomas s = getservbyname(n, "tcp");
1018 8a35f56c 2022-07-16 thomas if (s == NULL)
1019 8a35f56c 2022-07-16 thomas s = getservbyname(n, "udp");
1020 8a35f56c 2022-07-16 thomas if (s == NULL)
1021 8a35f56c 2022-07-16 thomas return (-1);
1022 c19738c9 2022-08-27 thomas return ntohs(s->s_port);
1023 8a35f56c 2022-07-16 thomas }
1024 8a35f56c 2022-07-16 thomas
1025 c19738c9 2022-08-27 thomas return (unsigned short)llval;
1026 8a35f56c 2022-07-16 thomas }
1027 8a35f56c 2022-07-16 thomas
1028 8a35f56c 2022-07-16 thomas int
1029 bccb4ec2 2023-06-01 thomas host(const char *s, struct server *new_srv, int max,
1030 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1031 8a35f56c 2022-07-16 thomas {
1032 8a35f56c 2022-07-16 thomas struct addrinfo hints, *res0, *res;
1033 8a35f56c 2022-07-16 thomas int error, cnt = 0;
1034 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
1035 8a35f56c 2022-07-16 thomas struct sockaddr_in6 *sin6;
1036 8a35f56c 2022-07-16 thomas struct address *h;
1037 8a35f56c 2022-07-16 thomas
1038 e4c7e0b0 2022-08-30 thomas if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1039 8a35f56c 2022-07-16 thomas return (cnt);
1040 8a35f56c 2022-07-16 thomas
1041 8a35f56c 2022-07-16 thomas memset(&hints, 0, sizeof(hints));
1042 fb0cb708 2023-06-01 thomas hints.ai_family = AF_UNSPEC;
1043 fb0cb708 2023-06-01 thomas hints.ai_socktype = SOCK_STREAM; /* DUMMY */
1044 8a35f56c 2022-07-16 thomas hints.ai_flags = AI_ADDRCONFIG;
1045 8a35f56c 2022-07-16 thomas error = getaddrinfo(s, NULL, &hints, &res0);
1046 8a35f56c 2022-07-16 thomas if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1047 8a35f56c 2022-07-16 thomas return (0);
1048 8a35f56c 2022-07-16 thomas if (error) {
1049 8a35f56c 2022-07-16 thomas log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1050 8a35f56c 2022-07-16 thomas gai_strerror(error));
1051 8a35f56c 2022-07-16 thomas return (-1);
1052 8a35f56c 2022-07-16 thomas }
1053 8a35f56c 2022-07-16 thomas
1054 8a35f56c 2022-07-16 thomas for (res = res0; res && cnt < max; res = res->ai_next) {
1055 8a35f56c 2022-07-16 thomas if (res->ai_family != AF_INET &&
1056 8a35f56c 2022-07-16 thomas res->ai_family != AF_INET6)
1057 8a35f56c 2022-07-16 thomas continue;
1058 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1059 8a35f56c 2022-07-16 thomas fatal(__func__);
1060 8a35f56c 2022-07-16 thomas
1061 8a35f56c 2022-07-16 thomas if (port)
1062 8a35f56c 2022-07-16 thomas h->port = port;
1063 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1064 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1065 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1066 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1067 8a35f56c 2022-07-16 thomas __func__);
1068 8a35f56c 2022-07-16 thomas freeaddrinfo(res0);
1069 8a35f56c 2022-07-16 thomas free(h);
1070 8a35f56c 2022-07-16 thomas return (-1);
1071 8a35f56c 2022-07-16 thomas }
1072 8a35f56c 2022-07-16 thomas }
1073 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1074 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1075 8a35f56c 2022-07-16 thomas h->ss.ss_family = res->ai_family;
1076 8a35f56c 2022-07-16 thomas
1077 8a35f56c 2022-07-16 thomas if (res->ai_family == AF_INET) {
1078 cdbe1d7d 2022-08-06 thomas struct sockaddr_in *ra;
1079 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1080 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in *)res->ai_addr;
1081 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet_init(sain, &ra->sin_addr);
1082 8a35f56c 2022-07-16 thomas } else {
1083 cdbe1d7d 2022-08-06 thomas struct sockaddr_in6 *ra;
1084 8a35f56c 2022-07-16 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1085 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in6 *)res->ai_addr;
1086 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1087 8a35f56c 2022-07-16 thomas }
1088 8a35f56c 2022-07-16 thomas
1089 e4c7e0b0 2022-08-30 thomas if (add_addr(new_srv, h))
1090 e4c7e0b0 2022-08-30 thomas return -1;
1091 8a35f56c 2022-07-16 thomas cnt++;
1092 8a35f56c 2022-07-16 thomas }
1093 8a35f56c 2022-07-16 thomas if (cnt == max && res) {
1094 8a35f56c 2022-07-16 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1095 8a35f56c 2022-07-16 thomas s, max);
1096 8a35f56c 2022-07-16 thomas }
1097 8a35f56c 2022-07-16 thomas freeaddrinfo(res0);
1098 8a35f56c 2022-07-16 thomas return (cnt);
1099 8a35f56c 2022-07-16 thomas }
1100 8a35f56c 2022-07-16 thomas
1101 8a35f56c 2022-07-16 thomas int
1102 e4c7e0b0 2022-08-30 thomas host_if(const char *s, struct server *new_srv, int max,
1103 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1104 8a35f56c 2022-07-16 thomas {
1105 8a35f56c 2022-07-16 thomas struct ifaddrs *ifap, *p;
1106 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
1107 8a35f56c 2022-07-16 thomas struct sockaddr_in6 *sin6;
1108 8a35f56c 2022-07-16 thomas struct address *h;
1109 8a35f56c 2022-07-16 thomas int cnt = 0, af;
1110 8a35f56c 2022-07-16 thomas
1111 8a35f56c 2022-07-16 thomas if (getifaddrs(&ifap) == -1)
1112 8a35f56c 2022-07-16 thomas fatal("getifaddrs");
1113 8a35f56c 2022-07-16 thomas
1114 8a35f56c 2022-07-16 thomas /* First search for IPv4 addresses */
1115 8a35f56c 2022-07-16 thomas af = AF_INET;
1116 8a35f56c 2022-07-16 thomas
1117 8a35f56c 2022-07-16 thomas nextaf:
1118 8a35f56c 2022-07-16 thomas for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1119 8a35f56c 2022-07-16 thomas if (p->ifa_addr == NULL ||
1120 8a35f56c 2022-07-16 thomas p->ifa_addr->sa_family != af ||
1121 8a35f56c 2022-07-16 thomas (strcmp(s, p->ifa_name) != 0 &&
1122 8a35f56c 2022-07-16 thomas !is_if_in_group(p->ifa_name, s)))
1123 8a35f56c 2022-07-16 thomas continue;
1124 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1125 8a35f56c 2022-07-16 thomas fatal("calloc");
1126 8a35f56c 2022-07-16 thomas
1127 8a35f56c 2022-07-16 thomas if (port)
1128 8a35f56c 2022-07-16 thomas h->port = port;
1129 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1130 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1131 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1132 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1133 8a35f56c 2022-07-16 thomas __func__);
1134 8a35f56c 2022-07-16 thomas free(h);
1135 8a35f56c 2022-07-16 thomas freeifaddrs(ifap);
1136 8a35f56c 2022-07-16 thomas return (-1);
1137 8a35f56c 2022-07-16 thomas }
1138 8a35f56c 2022-07-16 thomas }
1139 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1140 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1141 8a35f56c 2022-07-16 thomas h->ss.ss_family = af;
1142 8a35f56c 2022-07-16 thomas
1143 8a35f56c 2022-07-16 thomas if (af == AF_INET) {
1144 cdbe1d7d 2022-08-06 thomas struct sockaddr_in *ra;
1145 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1146 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in *)p->ifa_addr;
1147 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet_init(sain, &ra->sin_addr);
1148 8a35f56c 2022-07-16 thomas } else {
1149 cdbe1d7d 2022-08-06 thomas struct sockaddr_in6 *ra;
1150 8a35f56c 2022-07-16 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1151 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in6 *)p->ifa_addr;
1152 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1153 cdbe1d7d 2022-08-06 thomas ra->sin6_scope_id);
1154 8a35f56c 2022-07-16 thomas }
1155 8a35f56c 2022-07-16 thomas
1156 e4c7e0b0 2022-08-30 thomas if (add_addr(new_srv, h))
1157 e4c7e0b0 2022-08-30 thomas return -1;
1158 8a35f56c 2022-07-16 thomas cnt++;
1159 8a35f56c 2022-07-16 thomas }
1160 8a35f56c 2022-07-16 thomas if (af == AF_INET) {
1161 8a35f56c 2022-07-16 thomas /* Next search for IPv6 addresses */
1162 8a35f56c 2022-07-16 thomas af = AF_INET6;
1163 8a35f56c 2022-07-16 thomas goto nextaf;
1164 8a35f56c 2022-07-16 thomas }
1165 8a35f56c 2022-07-16 thomas
1166 8a35f56c 2022-07-16 thomas if (cnt > max) {
1167 8a35f56c 2022-07-16 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1168 8a35f56c 2022-07-16 thomas s, max);
1169 8a35f56c 2022-07-16 thomas }
1170 8a35f56c 2022-07-16 thomas freeifaddrs(ifap);
1171 8a35f56c 2022-07-16 thomas return (cnt);
1172 8a35f56c 2022-07-16 thomas }
1173 8a35f56c 2022-07-16 thomas
1174 8a35f56c 2022-07-16 thomas int
1175 8a35f56c 2022-07-16 thomas is_if_in_group(const char *ifname, const char *groupname)
1176 8a35f56c 2022-07-16 thomas {
1177 ff36aeea 2022-07-16 thomas /* TA: Check this... */
1178 ff36aeea 2022-07-16 thomas #ifdef HAVE_STRUCT_IFGROUPREQ
1179 8a35f56c 2022-07-16 thomas unsigned int len;
1180 8a35f56c 2022-07-16 thomas struct ifgroupreq ifgr;
1181 8a35f56c 2022-07-16 thomas struct ifg_req *ifg;
1182 8a35f56c 2022-07-16 thomas int s;
1183 8a35f56c 2022-07-16 thomas int ret = 0;
1184 8a35f56c 2022-07-16 thomas
1185 8a35f56c 2022-07-16 thomas if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1186 8a35f56c 2022-07-16 thomas err(1, "socket");
1187 8a35f56c 2022-07-16 thomas
1188 8a35f56c 2022-07-16 thomas memset(&ifgr, 0, sizeof(ifgr));
1189 8a35f56c 2022-07-16 thomas if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1190 8a35f56c 2022-07-16 thomas err(1, "IFNAMSIZ");
1191 8a35f56c 2022-07-16 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1192 8a35f56c 2022-07-16 thomas if (errno == EINVAL || errno == ENOTTY)
1193 8a35f56c 2022-07-16 thomas goto end;
1194 8a35f56c 2022-07-16 thomas err(1, "SIOCGIFGROUP");
1195 8a35f56c 2022-07-16 thomas }
1196 8a35f56c 2022-07-16 thomas
1197 8a35f56c 2022-07-16 thomas len = ifgr.ifgr_len;
1198 8a35f56c 2022-07-16 thomas ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1199 8a35f56c 2022-07-16 thomas sizeof(struct ifg_req));
1200 8a35f56c 2022-07-16 thomas if (ifgr.ifgr_groups == NULL)
1201 8a35f56c 2022-07-16 thomas err(1, "getifgroups");
1202 8a35f56c 2022-07-16 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1203 8a35f56c 2022-07-16 thomas err(1, "SIOCGIFGROUP");
1204 8a35f56c 2022-07-16 thomas
1205 8a35f56c 2022-07-16 thomas ifg = ifgr.ifgr_groups;
1206 8a35f56c 2022-07-16 thomas for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1207 8a35f56c 2022-07-16 thomas len -= sizeof(struct ifg_req);
1208 8a35f56c 2022-07-16 thomas if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1209 8a35f56c 2022-07-16 thomas ret = 1;
1210 8a35f56c 2022-07-16 thomas break;
1211 8a35f56c 2022-07-16 thomas }
1212 8a35f56c 2022-07-16 thomas }
1213 8a35f56c 2022-07-16 thomas free(ifgr.ifgr_groups);
1214 8a35f56c 2022-07-16 thomas
1215 8a35f56c 2022-07-16 thomas end:
1216 8a35f56c 2022-07-16 thomas close(s);
1217 8a35f56c 2022-07-16 thomas return (ret);
1218 ff36aeea 2022-07-16 thomas #else
1219 ff36aeea 2022-07-16 thomas return (0);
1220 ff36aeea 2022-07-16 thomas #endif
1221 8a35f56c 2022-07-16 thomas }
1222 8a35f56c 2022-07-16 thomas
1223 8a35f56c 2022-07-16 thomas int
1224 e4c7e0b0 2022-08-30 thomas get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1225 8a35f56c 2022-07-16 thomas {
1226 8a35f56c 2022-07-16 thomas if (strcmp("", addr) == 0) {
1227 e4c7e0b0 2022-08-30 thomas if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1228 e4c7e0b0 2022-08-30 thomas -1) <= 0) {
1229 8a35f56c 2022-07-16 thomas yyerror("invalid listen ip: %s",
1230 a90e3117 2022-08-27 thomas "127.0.0.1");
1231 8a35f56c 2022-07-16 thomas return (-1);
1232 8a35f56c 2022-07-16 thomas }
1233 e4c7e0b0 2022-08-30 thomas if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1234 a90e3117 2022-08-27 thomas yyerror("invalid listen ip: %s", "::1");
1235 8a35f56c 2022-07-16 thomas return (-1);
1236 8a35f56c 2022-07-16 thomas }
1237 8a35f56c 2022-07-16 thomas } else {
1238 e4c7e0b0 2022-08-30 thomas if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1239 8a35f56c 2022-07-16 thomas -1) <= 0) {
1240 8a35f56c 2022-07-16 thomas yyerror("invalid listen ip: %s", addr);
1241 8a35f56c 2022-07-16 thomas return (-1);
1242 8a35f56c 2022-07-16 thomas }
1243 8a35f56c 2022-07-16 thomas }
1244 8a35f56c 2022-07-16 thomas return (0);
1245 e4c7e0b0 2022-08-30 thomas }
1246 e4c7e0b0 2022-08-30 thomas
1247 e4c7e0b0 2022-08-30 thomas int
1248 e4c7e0b0 2022-08-30 thomas addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1249 e4c7e0b0 2022-08-30 thomas const char *other_srv)
1250 e4c7e0b0 2022-08-30 thomas {
1251 e4c7e0b0 2022-08-30 thomas struct address *a;
1252 e4c7e0b0 2022-08-30 thomas void *ia;
1253 e4c7e0b0 2022-08-30 thomas char buf[INET6_ADDRSTRLEN];
1254 e4c7e0b0 2022-08-30 thomas const char *addrstr;
1255 e4c7e0b0 2022-08-30 thomas
1256 e4c7e0b0 2022-08-30 thomas TAILQ_FOREACH(a, al, entry) {
1257 e4c7e0b0 2022-08-30 thomas if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1258 e4c7e0b0 2022-08-30 thomas a->port != h->port)
1259 e4c7e0b0 2022-08-30 thomas continue;
1260 e4c7e0b0 2022-08-30 thomas
1261 e4c7e0b0 2022-08-30 thomas switch (h->ss.ss_family) {
1262 e4c7e0b0 2022-08-30 thomas case AF_INET:
1263 e4c7e0b0 2022-08-30 thomas ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1264 e4c7e0b0 2022-08-30 thomas break;
1265 e4c7e0b0 2022-08-30 thomas case AF_INET6:
1266 e4c7e0b0 2022-08-30 thomas ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1267 e4c7e0b0 2022-08-30 thomas break;
1268 e4c7e0b0 2022-08-30 thomas default:
1269 e4c7e0b0 2022-08-30 thomas yyerror("unknown address family: %d", h->ss.ss_family);
1270 e4c7e0b0 2022-08-30 thomas return -1;
1271 e4c7e0b0 2022-08-30 thomas }
1272 e4c7e0b0 2022-08-30 thomas addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1273 e4c7e0b0 2022-08-30 thomas if (addrstr) {
1274 e4c7e0b0 2022-08-30 thomas if (other_srv) {
1275 e4c7e0b0 2022-08-30 thomas yyerror("server %s: duplicate fcgi listen "
1276 e4c7e0b0 2022-08-30 thomas "address %s:%d, already used by server %s",
1277 e4c7e0b0 2022-08-30 thomas new_srv, addrstr, h->port, other_srv);
1278 e4c7e0b0 2022-08-30 thomas } else {
1279 e4c7e0b0 2022-08-30 thomas log_warnx("server: %s: duplicate fcgi listen "
1280 e4c7e0b0 2022-08-30 thomas "address %s:%d", new_srv, addrstr, h->port);
1281 e4c7e0b0 2022-08-30 thomas }
1282 e4c7e0b0 2022-08-30 thomas } else {
1283 e4c7e0b0 2022-08-30 thomas if (other_srv) {
1284 e4c7e0b0 2022-08-30 thomas yyerror("server: %s: duplicate fcgi listen "
1285 e4c7e0b0 2022-08-30 thomas "address, already used by server %s",
1286 e4c7e0b0 2022-08-30 thomas new_srv, other_srv);
1287 e4c7e0b0 2022-08-30 thomas } else {
1288 e4c7e0b0 2022-08-30 thomas log_warnx("server %s: duplicate fcgi listen "
1289 e4c7e0b0 2022-08-30 thomas "address", new_srv);
1290 e4c7e0b0 2022-08-30 thomas }
1291 e4c7e0b0 2022-08-30 thomas }
1292 e4c7e0b0 2022-08-30 thomas
1293 e4c7e0b0 2022-08-30 thomas return -1;
1294 e4c7e0b0 2022-08-30 thomas }
1295 e4c7e0b0 2022-08-30 thomas
1296 e4c7e0b0 2022-08-30 thomas return 0;
1297 8a35f56c 2022-07-16 thomas }
1298 e4c7e0b0 2022-08-30 thomas
1299 e4c7e0b0 2022-08-30 thomas int
1300 e4c7e0b0 2022-08-30 thomas add_addr(struct server *new_srv, struct address *h)
1301 e4c7e0b0 2022-08-30 thomas {
1302 e4c7e0b0 2022-08-30 thomas struct server *srv;
1303 e4c7e0b0 2022-08-30 thomas
1304 e4c7e0b0 2022-08-30 thomas /* Address cannot be shared between different servers. */
1305 e4c7e0b0 2022-08-30 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1306 e4c7e0b0 2022-08-30 thomas if (srv == new_srv)
1307 e4c7e0b0 2022-08-30 thomas continue;
1308 e4c7e0b0 2022-08-30 thomas if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1309 e4c7e0b0 2022-08-30 thomas return -1;
1310 e4c7e0b0 2022-08-30 thomas }
1311 e4c7e0b0 2022-08-30 thomas
1312 e4c7e0b0 2022-08-30 thomas /* Tolerate duplicate address lines within the scope of a server. */
1313 e4c7e0b0 2022-08-30 thomas if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1314 e4c7e0b0 2022-08-30 thomas TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1315 e4c7e0b0 2022-08-30 thomas else
1316 e4c7e0b0 2022-08-30 thomas free(h);
1317 e4c7e0b0 2022-08-30 thomas
1318 e4c7e0b0 2022-08-30 thomas return 0;
1319 e4c7e0b0 2022-08-30 thomas }