2 a596b957 2022-07-14 tracey * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 a596b957 2022-07-14 tracey * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 a596b957 2022-07-14 tracey * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 a596b957 2022-07-14 tracey * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 a596b957 2022-07-14 tracey * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 a596b957 2022-07-14 tracey * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
11 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
12 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
14 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 a596b957 2022-07-14 tracey #include <sys/ioctl.h>
25 a596b957 2022-07-14 tracey #include <sys/types.h>
26 a596b957 2022-07-14 tracey #include <sys/queue.h>
27 a596b957 2022-07-14 tracey #include <sys/socket.h>
28 a596b957 2022-07-14 tracey #include <sys/stat.h>
30 a596b957 2022-07-14 tracey #include <net/if.h>
31 a596b957 2022-07-14 tracey #include <netinet/in.h>
33 a596b957 2022-07-14 tracey #include <arpa/inet.h>
35 a596b957 2022-07-14 tracey #include <ctype.h>
36 a596b957 2022-07-14 tracey #include <err.h>
37 a596b957 2022-07-14 tracey #include <errno.h>
38 a596b957 2022-07-14 tracey #include <event.h>
39 a596b957 2022-07-14 tracey #include <ifaddrs.h>
40 a596b957 2022-07-14 tracey #include <imsg.h>
41 a596b957 2022-07-14 tracey #include <limits.h>
42 a596b957 2022-07-14 tracey #include <netdb.h>
43 a596b957 2022-07-14 tracey #include <stdarg.h>
44 a596b957 2022-07-14 tracey #include <stdlib.h>
45 a596b957 2022-07-14 tracey #include <stdio.h>
46 a596b957 2022-07-14 tracey #include <string.h>
47 a596b957 2022-07-14 tracey #include <syslog.h>
48 a596b957 2022-07-14 tracey #include <unistd.h>
50 df2d3cd2 2023-03-11 op #include "got_sockaddr.h"
51 df2d3cd2 2023-03-11 op #include "got_reference.h"
53 a596b957 2022-07-14 tracey #include "proc.h"
54 a596b957 2022-07-14 tracey #include "gotwebd.h"
56 a596b957 2022-07-14 tracey TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
57 a596b957 2022-07-14 tracey static struct file {
58 a596b957 2022-07-14 tracey TAILQ_ENTRY(file) entry;
59 a596b957 2022-07-14 tracey FILE *stream;
60 a596b957 2022-07-14 tracey char *name;
61 a596b957 2022-07-14 tracey int lineno;
62 a596b957 2022-07-14 tracey int errors;
64 a596b957 2022-07-14 tracey struct file *newfile(const char *, int);
65 a596b957 2022-07-14 tracey static void closefile(struct file *);
66 a596b957 2022-07-14 tracey int check_file_secrecy(int, const char *);
67 a596b957 2022-07-14 tracey int yyparse(void);
68 a596b957 2022-07-14 tracey int yylex(void);
69 a596b957 2022-07-14 tracey int yyerror(const char *, ...)
70 a596b957 2022-07-14 tracey __attribute__((__format__ (printf, 1, 2)))
71 a596b957 2022-07-14 tracey __attribute__((__nonnull__ (1)));
72 a596b957 2022-07-14 tracey int kw_cmp(const void *, const void *);
73 a596b957 2022-07-14 tracey int lookup(char *);
74 a596b957 2022-07-14 tracey int lgetc(int);
75 a596b957 2022-07-14 tracey int lungetc(int);
76 a596b957 2022-07-14 tracey int findeol(void);
78 a596b957 2022-07-14 tracey TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
79 a596b957 2022-07-14 tracey struct sym {
80 a596b957 2022-07-14 tracey TAILQ_ENTRY(sym) entry;
82 a596b957 2022-07-14 tracey int persist;
83 a596b957 2022-07-14 tracey char *nam;
84 a596b957 2022-07-14 tracey char *val;
87 a596b957 2022-07-14 tracey int symset(const char *, const char *, int);
88 a596b957 2022-07-14 tracey char *symget(const char *);
90 a596b957 2022-07-14 tracey static int errors;
92 a596b957 2022-07-14 tracey static struct gotwebd *gotwebd;
93 a596b957 2022-07-14 tracey static struct server *new_srv;
94 a596b957 2022-07-14 tracey static struct server *conf_new_server(const char *);
95 a596b957 2022-07-14 tracey int getservice(const char *);
98 67d8de2a 2022-08-29 stsp int get_addrs(const char *, struct server *, in_port_t);
99 67d8de2a 2022-08-29 stsp int addr_dup_check(struct addresslist *, struct address *,
100 67d8de2a 2022-08-29 stsp const char *, const char *);
101 67d8de2a 2022-08-29 stsp int add_addr(struct server *, struct address *);
102 a596b957 2022-07-14 tracey struct address *host_v4(const char *);
103 a596b957 2022-07-14 tracey struct address *host_v6(const char *);
104 67d8de2a 2022-08-29 stsp int host_dns(const char *, struct server *,
105 a596b957 2022-07-14 tracey int, in_port_t, const char *, int);
106 67d8de2a 2022-08-29 stsp int host_if(const char *, struct server *,
107 a596b957 2022-07-14 tracey int, in_port_t, const char *, int);
108 67d8de2a 2022-08-29 stsp int host(const char *, struct server *,
109 a596b957 2022-07-14 tracey int, in_port_t, const char *, int);
110 a596b957 2022-07-14 tracey int is_if_in_group(const char *, const char *);
112 a596b957 2022-07-14 tracey typedef struct {
114 a596b957 2022-07-14 tracey long long number;
115 a596b957 2022-07-14 tracey char *string;
116 a596b957 2022-07-14 tracey in_port_t port;
118 a596b957 2022-07-14 tracey int lineno;
119 a596b957 2022-07-14 tracey } YYSTYPE;
123 d8473d93 2022-08-11 stsp %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
124 a596b957 2022-07-14 tracey %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
125 a596b957 2022-07-14 tracey %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
126 d5996b9e 2022-10-31 landry %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
127 3a1c1a1b 2023-01-04 op %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
129 a596b957 2022-07-14 tracey %token <v.string> STRING
130 a596b957 2022-07-14 tracey %type <v.port> fcgiport
131 a596b957 2022-07-14 tracey %token <v.number> NUMBER
132 a596b957 2022-07-14 tracey %type <v.number> boolean
136 47b307cd 2022-10-02 op grammar : /* empty */
137 a596b957 2022-07-14 tracey | grammar '\n'
138 47b307cd 2022-10-02 op | grammar varset '\n'
139 a596b957 2022-07-14 tracey | grammar main '\n'
140 a596b957 2022-07-14 tracey | grammar server '\n'
141 47b307cd 2022-10-02 op | grammar error '\n' { file->errors++; }
144 47b307cd 2022-10-02 op varset : STRING '=' STRING {
145 47b307cd 2022-10-02 op char *s = $1;
146 47b307cd 2022-10-02 op while (*s++) {
147 47b307cd 2022-10-02 op if (isspace((unsigned char)*s)) {
148 47b307cd 2022-10-02 op yyerror("macro name cannot contain "
149 47b307cd 2022-10-02 op "whitespace");
155 47b307cd 2022-10-02 op if (symset($1, $3, 0) == -1)
156 47b307cd 2022-10-02 op fatal("cannot store variable");
162 a596b957 2022-07-14 tracey boolean : STRING {
163 a596b957 2022-07-14 tracey if (strcasecmp($1, "1") == 0 ||
164 a596b957 2022-07-14 tracey strcasecmp($1, "yes") == 0 ||
165 a596b957 2022-07-14 tracey strcasecmp($1, "on") == 0)
167 a596b957 2022-07-14 tracey else if (strcasecmp($1, "0") == 0 ||
168 a596b957 2022-07-14 tracey strcasecmp($1, "off") == 0 ||
169 a596b957 2022-07-14 tracey strcasecmp($1, "no") == 0)
172 a596b957 2022-07-14 tracey yyerror("invalid boolean value '%s'", $1);
173 a596b957 2022-07-14 tracey free($1);
176 a596b957 2022-07-14 tracey free($1);
178 a596b957 2022-07-14 tracey | ON { $$ = 1; }
179 a596b957 2022-07-14 tracey | NUMBER { $$ = $1; }
182 17132eaa 2022-08-29 stsp fcgiport : PORT NUMBER {
183 17132eaa 2022-08-29 stsp if ($2 <= 0 || $2 > (int)USHRT_MAX) {
184 17132eaa 2022-08-29 stsp yyerror("invalid port: %lld", $2);
189 17132eaa 2022-08-29 stsp | PORT STRING {
192 17132eaa 2022-08-29 stsp if ((val = getservice($2)) == -1) {
193 17132eaa 2022-08-29 stsp yyerror("invalid port: %s", $2);
199 a596b957 2022-07-14 tracey $$ = val;
203 a596b957 2022-07-14 tracey main : PREFORK NUMBER {
204 a596b957 2022-07-14 tracey gotwebd->prefork_gotwebd = $2;
206 a596b957 2022-07-14 tracey | CHROOT STRING {
207 a596b957 2022-07-14 tracey n = strlcpy(gotwebd->httpd_chroot, $2,
208 a596b957 2022-07-14 tracey sizeof(gotwebd->httpd_chroot));
209 a596b957 2022-07-14 tracey if (n >= sizeof(gotwebd->httpd_chroot)) {
210 a596b957 2022-07-14 tracey yyerror("%s: httpd_chroot truncated", __func__);
211 a596b957 2022-07-14 tracey free($2);
214 a596b957 2022-07-14 tracey free($2);
216 a596b957 2022-07-14 tracey | UNIX_SOCKET boolean {
217 a596b957 2022-07-14 tracey gotwebd->unix_socket = $2;
219 a596b957 2022-07-14 tracey | UNIX_SOCKET_NAME STRING {
220 a596b957 2022-07-14 tracey n = snprintf(gotwebd->unix_socket_name,
221 a596b957 2022-07-14 tracey sizeof(gotwebd->unix_socket_name), "%s%s",
222 a596b957 2022-07-14 tracey strlen(gotwebd->httpd_chroot) ?
223 a596b957 2022-07-14 tracey gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
225 438d0cc3 2022-08-16 op (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
226 a596b957 2022-07-14 tracey yyerror("%s: unix_socket_name truncated",
227 a596b957 2022-07-14 tracey __func__);
228 a596b957 2022-07-14 tracey free($2);
231 a596b957 2022-07-14 tracey free($2);
235 a596b957 2022-07-14 tracey server : SERVER STRING {
236 a596b957 2022-07-14 tracey struct server *srv;
238 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
239 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
240 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
241 a596b957 2022-07-14 tracey free($2);
246 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
247 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
248 a596b957 2022-07-14 tracey free($2);
250 a596b957 2022-07-14 tracey | SERVER STRING {
251 a596b957 2022-07-14 tracey struct server *srv;
253 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
254 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
255 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
256 a596b957 2022-07-14 tracey free($2);
261 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
262 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
263 a596b957 2022-07-14 tracey free($2);
264 a596b957 2022-07-14 tracey } '{' optnl serveropts2 '}' {
268 a596b957 2022-07-14 tracey serveropts1 : REPOS_PATH STRING {
269 a596b957 2022-07-14 tracey n = strlcpy(new_srv->repos_path, $2,
270 a596b957 2022-07-14 tracey sizeof(new_srv->repos_path));
271 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->repos_path)) {
272 a596b957 2022-07-14 tracey yyerror("%s: repos_path truncated", __func__);
273 a596b957 2022-07-14 tracey free($2);
276 a596b957 2022-07-14 tracey free($2);
278 a596b957 2022-07-14 tracey | SITE_NAME STRING {
279 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_name, $2,
280 a596b957 2022-07-14 tracey sizeof(new_srv->site_name));
281 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_name)) {
282 a596b957 2022-07-14 tracey yyerror("%s: site_name truncated", __func__);
283 a596b957 2022-07-14 tracey free($2);
286 a596b957 2022-07-14 tracey free($2);
288 a596b957 2022-07-14 tracey | SITE_OWNER STRING {
289 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_owner, $2,
290 a596b957 2022-07-14 tracey sizeof(new_srv->site_owner));
291 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_owner)) {
292 a596b957 2022-07-14 tracey yyerror("%s: site_owner truncated", __func__);
293 a596b957 2022-07-14 tracey free($2);
296 a596b957 2022-07-14 tracey free($2);
298 a596b957 2022-07-14 tracey | SITE_LINK STRING {
299 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_link, $2,
300 a596b957 2022-07-14 tracey sizeof(new_srv->site_link));
301 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_link)) {
302 a596b957 2022-07-14 tracey yyerror("%s: site_link truncated", __func__);
303 a596b957 2022-07-14 tracey free($2);
306 a596b957 2022-07-14 tracey free($2);
308 a596b957 2022-07-14 tracey | LOGO STRING {
309 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
310 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo)) {
311 a596b957 2022-07-14 tracey yyerror("%s: logo truncated", __func__);
312 a596b957 2022-07-14 tracey free($2);
315 a596b957 2022-07-14 tracey free($2);
317 a596b957 2022-07-14 tracey | LOGO_URL STRING {
318 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo_url, $2,
319 a596b957 2022-07-14 tracey sizeof(new_srv->logo_url));
320 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo_url)) {
321 a596b957 2022-07-14 tracey yyerror("%s: logo_url truncated", __func__);
322 a596b957 2022-07-14 tracey free($2);
325 a596b957 2022-07-14 tracey free($2);
327 a596b957 2022-07-14 tracey | CUSTOM_CSS STRING {
328 a596b957 2022-07-14 tracey n = strlcpy(new_srv->custom_css, $2,
329 a596b957 2022-07-14 tracey sizeof(new_srv->custom_css));
330 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->custom_css)) {
331 a596b957 2022-07-14 tracey yyerror("%s: custom_css truncated", __func__);
332 a596b957 2022-07-14 tracey free($2);
335 a596b957 2022-07-14 tracey free($2);
337 17132eaa 2022-08-29 stsp | LISTEN ON STRING fcgiport {
338 67d8de2a 2022-08-29 stsp if (get_addrs($3, new_srv, $4) == -1) {
339 67d8de2a 2022-08-29 stsp yyerror("could not get addrs");
342 6c8aa58f 2022-08-30 stsp new_srv->fcgi_socket = 1;
344 3a1c1a1b 2023-01-04 op | LISTEN ON SOCKET STRING {
345 3a1c1a1b 2023-01-04 op if (!strcasecmp($4, "off") ||
346 3a1c1a1b 2023-01-04 op !strcasecmp($4, "no")) {
347 3a1c1a1b 2023-01-04 op new_srv->unix_socket = 0;
352 3a1c1a1b 2023-01-04 op new_srv->unix_socket = 1;
354 3a1c1a1b 2023-01-04 op n = snprintf(new_srv->unix_socket_name,
355 3a1c1a1b 2023-01-04 op sizeof(new_srv->unix_socket_name), "%s%s",
356 3a1c1a1b 2023-01-04 op strlen(gotwebd->httpd_chroot) ?
357 3a1c1a1b 2023-01-04 op gotwebd->httpd_chroot : D_HTTPD_CHROOT, $4);
359 3a1c1a1b 2023-01-04 op (size_t)n >= sizeof(new_srv->unix_socket_name)) {
360 3a1c1a1b 2023-01-04 op yyerror("%s: unix_socket_name truncated",
367 a596b957 2022-07-14 tracey | MAX_REPOS NUMBER {
368 a596b957 2022-07-14 tracey if ($2 > 0)
369 a596b957 2022-07-14 tracey new_srv->max_repos = $2;
371 a596b957 2022-07-14 tracey | SHOW_SITE_OWNER boolean {
372 a596b957 2022-07-14 tracey new_srv->show_site_owner = $2;
374 a596b957 2022-07-14 tracey | SHOW_REPO_OWNER boolean {
375 a596b957 2022-07-14 tracey new_srv->show_repo_owner = $2;
377 a596b957 2022-07-14 tracey | SHOW_REPO_AGE boolean {
378 a596b957 2022-07-14 tracey new_srv->show_repo_age = $2;
380 a596b957 2022-07-14 tracey | SHOW_REPO_DESCRIPTION boolean {
381 a596b957 2022-07-14 tracey new_srv->show_repo_description = $2;
383 a596b957 2022-07-14 tracey | SHOW_REPO_CLONEURL boolean {
384 a596b957 2022-07-14 tracey new_srv->show_repo_cloneurl = $2;
386 d5996b9e 2022-10-31 landry | RESPECT_EXPORTOK boolean {
387 d5996b9e 2022-10-31 landry new_srv->respect_exportok = $2;
389 a596b957 2022-07-14 tracey | MAX_REPOS_DISPLAY NUMBER {
390 a596b957 2022-07-14 tracey new_srv->max_repos_display = $2;
392 a596b957 2022-07-14 tracey | MAX_COMMITS_DISPLAY NUMBER {
393 a596b957 2022-07-14 tracey if ($2 > 0)
394 a596b957 2022-07-14 tracey new_srv->max_commits_display = $2;
398 a596b957 2022-07-14 tracey serveropts2 : serveropts2 serveropts1 nl
399 a596b957 2022-07-14 tracey | serveropts1 optnl
402 a596b957 2022-07-14 tracey nl : '\n' optnl
405 a596b957 2022-07-14 tracey optnl : '\n' optnl /* zero or more newlines */
406 a596b957 2022-07-14 tracey | /* empty */
411 a596b957 2022-07-14 tracey struct keywords {
412 a596b957 2022-07-14 tracey const char *k_name;
413 a596b957 2022-07-14 tracey int k_val;
417 a596b957 2022-07-14 tracey yyerror(const char *fmt, ...)
419 a596b957 2022-07-14 tracey va_list ap;
420 a596b957 2022-07-14 tracey char *msg;
422 a596b957 2022-07-14 tracey file->errors++;
423 a596b957 2022-07-14 tracey va_start(ap, fmt);
424 a596b957 2022-07-14 tracey if (vasprintf(&msg, fmt, ap) == -1)
425 a596b957 2022-07-14 tracey fatalx("yyerror vasprintf");
426 a596b957 2022-07-14 tracey va_end(ap);
427 a596b957 2022-07-14 tracey logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
428 a596b957 2022-07-14 tracey free(msg);
429 a596b957 2022-07-14 tracey return (0);
433 a596b957 2022-07-14 tracey kw_cmp(const void *k, const void *e)
435 a596b957 2022-07-14 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
439 a596b957 2022-07-14 tracey lookup(char *s)
441 a596b957 2022-07-14 tracey /* This has to be sorted always. */
442 a596b957 2022-07-14 tracey static const struct keywords keywords[] = {
443 a596b957 2022-07-14 tracey { "chroot", CHROOT },
444 a596b957 2022-07-14 tracey { "custom_css", CUSTOM_CSS },
445 d8473d93 2022-08-11 stsp { "listen", LISTEN },
446 a596b957 2022-07-14 tracey { "logo", LOGO },
447 8556b86b 2023-01-02 op { "logo_url", LOGO_URL },
448 a596b957 2022-07-14 tracey { "max_commits_display", MAX_COMMITS_DISPLAY },
449 a596b957 2022-07-14 tracey { "max_repos", MAX_REPOS },
450 a596b957 2022-07-14 tracey { "max_repos_display", MAX_REPOS_DISPLAY },
451 d8473d93 2022-08-11 stsp { "on", ON },
452 a596b957 2022-07-14 tracey { "port", PORT },
453 a596b957 2022-07-14 tracey { "prefork", PREFORK },
454 a596b957 2022-07-14 tracey { "repos_path", REPOS_PATH },
455 d5996b9e 2022-10-31 landry { "respect_exportok", RESPECT_EXPORTOK },
456 a596b957 2022-07-14 tracey { "server", SERVER },
457 a596b957 2022-07-14 tracey { "show_repo_age", SHOW_REPO_AGE },
458 a596b957 2022-07-14 tracey { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
459 a596b957 2022-07-14 tracey { "show_repo_description", SHOW_REPO_DESCRIPTION },
460 a596b957 2022-07-14 tracey { "show_repo_owner", SHOW_REPO_OWNER },
461 a596b957 2022-07-14 tracey { "show_site_owner", SHOW_SITE_OWNER },
462 a596b957 2022-07-14 tracey { "site_link", SITE_LINK },
463 a596b957 2022-07-14 tracey { "site_name", SITE_NAME },
464 a596b957 2022-07-14 tracey { "site_owner", SITE_OWNER },
465 3a1c1a1b 2023-01-04 op { "socket", SOCKET },
466 a596b957 2022-07-14 tracey { "unix_socket", UNIX_SOCKET },
467 a596b957 2022-07-14 tracey { "unix_socket_name", UNIX_SOCKET_NAME },
469 a596b957 2022-07-14 tracey const struct keywords *p;
471 a596b957 2022-07-14 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
472 a596b957 2022-07-14 tracey sizeof(keywords[0]), kw_cmp);
475 a596b957 2022-07-14 tracey return (p->k_val);
477 a596b957 2022-07-14 tracey return (STRING);
480 a596b957 2022-07-14 tracey #define MAXPUSHBACK 128
482 a596b957 2022-07-14 tracey unsigned char *parsebuf;
483 a596b957 2022-07-14 tracey int parseindex;
484 a596b957 2022-07-14 tracey unsigned char pushback_buffer[MAXPUSHBACK];
485 a596b957 2022-07-14 tracey int pushback_index = 0;
488 a596b957 2022-07-14 tracey lgetc(int quotec)
490 a596b957 2022-07-14 tracey int c, next;
492 a596b957 2022-07-14 tracey if (parsebuf) {
493 a596b957 2022-07-14 tracey /* Read character from the parsebuffer instead of input. */
494 a596b957 2022-07-14 tracey if (parseindex >= 0) {
495 a596b957 2022-07-14 tracey c = parsebuf[parseindex++];
496 a596b957 2022-07-14 tracey if (c != '\0')
497 a596b957 2022-07-14 tracey return (c);
498 a596b957 2022-07-14 tracey parsebuf = NULL;
500 a596b957 2022-07-14 tracey parseindex++;
503 a596b957 2022-07-14 tracey if (pushback_index)
504 a596b957 2022-07-14 tracey return (pushback_buffer[--pushback_index]);
506 a596b957 2022-07-14 tracey if (quotec) {
507 a596b957 2022-07-14 tracey c = getc(file->stream);
508 a596b957 2022-07-14 tracey if (c == EOF)
509 a596b957 2022-07-14 tracey yyerror("reached end of file while parsing "
510 a596b957 2022-07-14 tracey "quoted string");
511 a596b957 2022-07-14 tracey return (c);
514 a596b957 2022-07-14 tracey c = getc(file->stream);
515 a596b957 2022-07-14 tracey while (c == '\\') {
516 a596b957 2022-07-14 tracey next = getc(file->stream);
517 a596b957 2022-07-14 tracey if (next != '\n') {
518 a596b957 2022-07-14 tracey c = next;
521 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
522 a596b957 2022-07-14 tracey file->lineno++;
523 a596b957 2022-07-14 tracey c = getc(file->stream);
526 a596b957 2022-07-14 tracey return (c);
530 a596b957 2022-07-14 tracey lungetc(int c)
532 a596b957 2022-07-14 tracey if (c == EOF)
533 a596b957 2022-07-14 tracey return (EOF);
534 a596b957 2022-07-14 tracey if (parsebuf) {
535 a596b957 2022-07-14 tracey parseindex--;
536 a596b957 2022-07-14 tracey if (parseindex >= 0)
537 a596b957 2022-07-14 tracey return (c);
539 a596b957 2022-07-14 tracey if (pushback_index < MAXPUSHBACK-1)
540 a596b957 2022-07-14 tracey return (pushback_buffer[pushback_index++] = c);
542 a596b957 2022-07-14 tracey return (EOF);
546 a596b957 2022-07-14 tracey findeol(void)
550 a596b957 2022-07-14 tracey parsebuf = NULL;
552 a596b957 2022-07-14 tracey /* Skip to either EOF or the first real EOL. */
553 a596b957 2022-07-14 tracey while (1) {
554 a596b957 2022-07-14 tracey if (pushback_index)
555 a596b957 2022-07-14 tracey c = pushback_buffer[--pushback_index];
557 a596b957 2022-07-14 tracey c = lgetc(0);
558 a596b957 2022-07-14 tracey if (c == '\n') {
559 a596b957 2022-07-14 tracey file->lineno++;
562 a596b957 2022-07-14 tracey if (c == EOF)
565 a596b957 2022-07-14 tracey return (ERROR);
569 a596b957 2022-07-14 tracey yylex(void)
571 a596b957 2022-07-14 tracey unsigned char buf[8096];
572 a596b957 2022-07-14 tracey unsigned char *p, *val;
573 a596b957 2022-07-14 tracey int quotec, next, c;
574 a596b957 2022-07-14 tracey int token;
578 a596b957 2022-07-14 tracey c = lgetc(0);
579 a596b957 2022-07-14 tracey while (c == ' ' || c == '\t')
580 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
582 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
583 a596b957 2022-07-14 tracey if (c == '#') {
584 a596b957 2022-07-14 tracey c = lgetc(0);
585 a596b957 2022-07-14 tracey while (c != '\n' && c != EOF)
586 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
588 a596b957 2022-07-14 tracey if (c == '$' && parsebuf == NULL) {
589 a596b957 2022-07-14 tracey while (1) {
590 a596b957 2022-07-14 tracey c = lgetc(0);
591 a596b957 2022-07-14 tracey if (c == EOF)
592 a596b957 2022-07-14 tracey return (0);
594 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
595 a596b957 2022-07-14 tracey yyerror("string too long");
596 a596b957 2022-07-14 tracey return (findeol());
598 a596b957 2022-07-14 tracey if (isalnum(c) || c == '_') {
599 a596b957 2022-07-14 tracey *p++ = c;
600 a596b957 2022-07-14 tracey continue;
602 a596b957 2022-07-14 tracey *p = '\0';
603 a596b957 2022-07-14 tracey lungetc(c);
606 a596b957 2022-07-14 tracey val = symget(buf);
607 a596b957 2022-07-14 tracey if (val == NULL) {
608 a596b957 2022-07-14 tracey yyerror("macro '%s' not defined", buf);
609 a596b957 2022-07-14 tracey return (findeol());
611 a596b957 2022-07-14 tracey parsebuf = val;
612 a596b957 2022-07-14 tracey parseindex = 0;
613 a596b957 2022-07-14 tracey goto top;
616 a596b957 2022-07-14 tracey switch (c) {
617 a596b957 2022-07-14 tracey case '\'':
618 a596b957 2022-07-14 tracey case '"':
619 a596b957 2022-07-14 tracey quotec = c;
620 a596b957 2022-07-14 tracey while (1) {
621 a596b957 2022-07-14 tracey c = lgetc(quotec);
622 a596b957 2022-07-14 tracey if (c == EOF)
623 a596b957 2022-07-14 tracey return (0);
624 a596b957 2022-07-14 tracey if (c == '\n') {
625 a596b957 2022-07-14 tracey file->lineno++;
626 a596b957 2022-07-14 tracey continue;
627 a596b957 2022-07-14 tracey } else if (c == '\\') {
628 a596b957 2022-07-14 tracey next = lgetc(quotec);
629 a596b957 2022-07-14 tracey if (next == EOF)
630 a596b957 2022-07-14 tracey return (0);
631 a596b957 2022-07-14 tracey if (next == quotec || c == ' ' || c == '\t')
632 a596b957 2022-07-14 tracey c = next;
633 a596b957 2022-07-14 tracey else if (next == '\n') {
634 a596b957 2022-07-14 tracey file->lineno++;
635 a596b957 2022-07-14 tracey continue;
637 a596b957 2022-07-14 tracey lungetc(next);
638 a596b957 2022-07-14 tracey } else if (c == quotec) {
639 a596b957 2022-07-14 tracey *p = '\0';
641 a596b957 2022-07-14 tracey } else if (c == '\0') {
642 a596b957 2022-07-14 tracey yyerror("syntax error");
643 a596b957 2022-07-14 tracey return (findeol());
645 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
646 a596b957 2022-07-14 tracey yyerror("string too long");
647 a596b957 2022-07-14 tracey return (findeol());
649 a596b957 2022-07-14 tracey *p++ = c;
651 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
652 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
653 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
654 a596b957 2022-07-14 tracey return (STRING);
657 a596b957 2022-07-14 tracey #define allowed_to_end_number(x) \
658 a596b957 2022-07-14 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
660 a596b957 2022-07-14 tracey if (c == '-' || isdigit(c)) {
662 a596b957 2022-07-14 tracey *p++ = c;
663 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
664 a596b957 2022-07-14 tracey yyerror("string too long");
665 a596b957 2022-07-14 tracey return (findeol());
667 a596b957 2022-07-14 tracey c = lgetc(0);
668 a596b957 2022-07-14 tracey } while (c != EOF && isdigit(c));
669 a596b957 2022-07-14 tracey lungetc(c);
670 a596b957 2022-07-14 tracey if (p == buf + 1 && buf[0] == '-')
671 a596b957 2022-07-14 tracey goto nodigits;
672 a596b957 2022-07-14 tracey if (c == EOF || allowed_to_end_number(c)) {
673 a596b957 2022-07-14 tracey const char *errstr = NULL;
675 a596b957 2022-07-14 tracey *p = '\0';
676 a596b957 2022-07-14 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
677 a596b957 2022-07-14 tracey LLONG_MAX, &errstr);
678 a596b957 2022-07-14 tracey if (errstr) {
679 a596b957 2022-07-14 tracey yyerror("\"%s\" invalid number: %s",
680 a596b957 2022-07-14 tracey buf, errstr);
681 a596b957 2022-07-14 tracey return (findeol());
683 a596b957 2022-07-14 tracey return (NUMBER);
685 a596b957 2022-07-14 tracey nodigits:
686 a596b957 2022-07-14 tracey while (p > buf + 1)
687 a596b957 2022-07-14 tracey lungetc(*--p);
688 a596b957 2022-07-14 tracey c = *--p;
689 a596b957 2022-07-14 tracey if (c == '-')
690 a596b957 2022-07-14 tracey return (c);
694 a596b957 2022-07-14 tracey #define allowed_in_string(x) \
695 a596b957 2022-07-14 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
696 a596b957 2022-07-14 tracey x != '{' && x != '}' && \
697 a596b957 2022-07-14 tracey x != '!' && x != '=' && x != '#' && \
698 a596b957 2022-07-14 tracey x != ','))
700 a596b957 2022-07-14 tracey if (isalnum(c) || c == ':' || c == '_') {
702 a596b957 2022-07-14 tracey *p++ = c;
703 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
704 a596b957 2022-07-14 tracey yyerror("string too long");
705 a596b957 2022-07-14 tracey return (findeol());
707 a596b957 2022-07-14 tracey c = lgetc(0);
708 a596b957 2022-07-14 tracey } while (c != EOF && (allowed_in_string(c)));
709 a596b957 2022-07-14 tracey lungetc(c);
710 a596b957 2022-07-14 tracey *p = '\0';
711 a596b957 2022-07-14 tracey token = lookup(buf);
712 a596b957 2022-07-14 tracey if (token == STRING) {
713 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
714 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
715 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
717 a596b957 2022-07-14 tracey return (token);
719 a596b957 2022-07-14 tracey if (c == '\n') {
720 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
721 a596b957 2022-07-14 tracey file->lineno++;
723 a596b957 2022-07-14 tracey if (c == EOF)
724 a596b957 2022-07-14 tracey return (0);
725 a596b957 2022-07-14 tracey return (c);
729 a596b957 2022-07-14 tracey check_file_secrecy(int fd, const char *fname)
731 a596b957 2022-07-14 tracey struct stat st;
733 a596b957 2022-07-14 tracey if (fstat(fd, &st)) {
734 a596b957 2022-07-14 tracey log_warn("cannot stat %s", fname);
735 a596b957 2022-07-14 tracey return (-1);
737 a596b957 2022-07-14 tracey if (st.st_uid != 0 && st.st_uid != getuid()) {
738 a596b957 2022-07-14 tracey log_warnx("%s: owner not root or current user", fname);
739 a596b957 2022-07-14 tracey return (-1);
741 a596b957 2022-07-14 tracey if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
742 a596b957 2022-07-14 tracey log_warnx("%s: group writable or world read/writable", fname);
743 a596b957 2022-07-14 tracey return (-1);
745 a596b957 2022-07-14 tracey return (0);
748 a596b957 2022-07-14 tracey struct file *
749 a596b957 2022-07-14 tracey newfile(const char *name, int secret)
751 a596b957 2022-07-14 tracey struct file *nfile;
753 a596b957 2022-07-14 tracey nfile = calloc(1, sizeof(struct file));
754 a596b957 2022-07-14 tracey if (nfile == NULL) {
755 a596b957 2022-07-14 tracey log_warn("calloc");
756 a596b957 2022-07-14 tracey return (NULL);
758 a596b957 2022-07-14 tracey nfile->name = strdup(name);
759 a596b957 2022-07-14 tracey if (nfile->name == NULL) {
760 a596b957 2022-07-14 tracey log_warn("strdup");
761 a596b957 2022-07-14 tracey free(nfile);
762 a596b957 2022-07-14 tracey return (NULL);
764 a596b957 2022-07-14 tracey nfile->stream = fopen(nfile->name, "r");
765 a596b957 2022-07-14 tracey if (nfile->stream == NULL) {
766 a596b957 2022-07-14 tracey /* no warning, we don't require a conf file */
767 a596b957 2022-07-14 tracey free(nfile->name);
768 a596b957 2022-07-14 tracey free(nfile);
769 a596b957 2022-07-14 tracey return (NULL);
770 a596b957 2022-07-14 tracey } else if (secret &&
771 a596b957 2022-07-14 tracey check_file_secrecy(fileno(nfile->stream), nfile->name)) {
772 a596b957 2022-07-14 tracey fclose(nfile->stream);
773 a596b957 2022-07-14 tracey free(nfile->name);
774 a596b957 2022-07-14 tracey free(nfile);
775 a596b957 2022-07-14 tracey return (NULL);
777 a596b957 2022-07-14 tracey nfile->lineno = 1;
778 a596b957 2022-07-14 tracey return (nfile);
781 a596b957 2022-07-14 tracey static void
782 a596b957 2022-07-14 tracey closefile(struct file *xfile)
784 a596b957 2022-07-14 tracey fclose(xfile->stream);
785 a596b957 2022-07-14 tracey free(xfile->name);
786 a596b957 2022-07-14 tracey free(xfile);
789 a0037b73 2022-08-03 stsp static void
790 a0037b73 2022-08-03 stsp add_default_server(void)
792 a0037b73 2022-08-03 stsp new_srv = conf_new_server(D_SITENAME);
793 a0037b73 2022-08-03 stsp log_debug("%s: adding default server %s", __func__, D_SITENAME);
797 a596b957 2022-07-14 tracey parse_config(const char *filename, struct gotwebd *env)
799 a596b957 2022-07-14 tracey struct sym *sym, *next;
801 a596b957 2022-07-14 tracey if (config_init(env) == -1)
802 a596b957 2022-07-14 tracey fatalx("failed to initialize configuration");
804 a596b957 2022-07-14 tracey gotwebd = env;
806 a0037b73 2022-08-03 stsp file = newfile(filename, 0);
807 a0037b73 2022-08-03 stsp if (file == NULL) {
808 a0037b73 2022-08-03 stsp add_default_server();
809 a0037b73 2022-08-03 stsp sockets_parse_sockets(env);
810 a0037b73 2022-08-03 stsp /* just return, as we don't require a conf file */
811 a0037b73 2022-08-03 stsp return (0);
814 a596b957 2022-07-14 tracey yyparse();
815 a596b957 2022-07-14 tracey errors = file->errors;
816 a596b957 2022-07-14 tracey closefile(file);
818 a596b957 2022-07-14 tracey /* Free macros and check which have not been used. */
819 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
820 a596b957 2022-07-14 tracey if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
821 a596b957 2022-07-14 tracey fprintf(stderr, "warning: macro '%s' not used\n",
822 a596b957 2022-07-14 tracey sym->nam);
823 a596b957 2022-07-14 tracey if (!sym->persist) {
824 a596b957 2022-07-14 tracey free(sym->nam);
825 a596b957 2022-07-14 tracey free(sym->val);
826 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
827 a596b957 2022-07-14 tracey free(sym);
831 a596b957 2022-07-14 tracey if (errors)
832 a596b957 2022-07-14 tracey return (-1);
834 a596b957 2022-07-14 tracey /* just add default server if no config specified */
835 a0037b73 2022-08-03 stsp if (gotwebd->server_cnt == 0)
836 a0037b73 2022-08-03 stsp add_default_server();
838 a596b957 2022-07-14 tracey /* setup our listening sockets */
839 a596b957 2022-07-14 tracey sockets_parse_sockets(env);
841 a596b957 2022-07-14 tracey return (0);
844 a596b957 2022-07-14 tracey struct server *
845 a596b957 2022-07-14 tracey conf_new_server(const char *name)
847 a596b957 2022-07-14 tracey struct server *srv = NULL;
849 a596b957 2022-07-14 tracey srv = calloc(1, sizeof(*srv));
850 a596b957 2022-07-14 tracey if (srv == NULL)
851 a596b957 2022-07-14 tracey fatalx("%s: calloc", __func__);
853 a596b957 2022-07-14 tracey n = strlcpy(srv->name, name, sizeof(srv->name));
854 a596b957 2022-07-14 tracey if (n >= sizeof(srv->name))
855 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
856 a596b957 2022-07-14 tracey n = snprintf(srv->unix_socket_name,
857 a596b957 2022-07-14 tracey sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
858 a596b957 2022-07-14 tracey D_UNIX_SOCKET);
859 438d0cc3 2022-08-16 op if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
860 a596b957 2022-07-14 tracey fatalx("%s: snprintf", __func__);
861 a596b957 2022-07-14 tracey n = strlcpy(srv->repos_path, D_GOTPATH,
862 a596b957 2022-07-14 tracey sizeof(srv->repos_path));
863 a596b957 2022-07-14 tracey if (n >= sizeof(srv->repos_path))
864 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
865 a596b957 2022-07-14 tracey n = strlcpy(srv->site_name, D_SITENAME,
866 a596b957 2022-07-14 tracey sizeof(srv->site_name));
867 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_name))
868 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
869 a596b957 2022-07-14 tracey n = strlcpy(srv->site_owner, D_SITEOWNER,
870 a596b957 2022-07-14 tracey sizeof(srv->site_owner));
871 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_owner))
872 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
873 a596b957 2022-07-14 tracey n = strlcpy(srv->site_link, D_SITELINK,
874 a596b957 2022-07-14 tracey sizeof(srv->site_link));
875 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_link))
876 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
877 a596b957 2022-07-14 tracey n = strlcpy(srv->logo, D_GOTLOGO,
878 a596b957 2022-07-14 tracey sizeof(srv->logo));
879 a596b957 2022-07-14 tracey if (n >= sizeof(srv->logo))
880 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
881 a596b957 2022-07-14 tracey n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
882 a596b957 2022-07-14 tracey if (n >= sizeof(srv->logo_url))
883 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
884 a596b957 2022-07-14 tracey n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
885 a596b957 2022-07-14 tracey if (n >= sizeof(srv->custom_css))
886 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
888 a596b957 2022-07-14 tracey srv->show_site_owner = D_SHOWSOWNER;
889 a596b957 2022-07-14 tracey srv->show_repo_owner = D_SHOWROWNER;
890 a596b957 2022-07-14 tracey srv->show_repo_age = D_SHOWAGE;
891 a596b957 2022-07-14 tracey srv->show_repo_description = D_SHOWDESC;
892 a596b957 2022-07-14 tracey srv->show_repo_cloneurl = D_SHOWURL;
893 d5996b9e 2022-10-31 landry srv->respect_exportok = D_RESPECTEXPORTOK;
895 a596b957 2022-07-14 tracey srv->max_repos_display = D_MAXREPODISP;
896 a596b957 2022-07-14 tracey srv->max_commits_display = D_MAXCOMMITDISP;
897 a596b957 2022-07-14 tracey srv->max_repos = D_MAXREPO;
899 a596b957 2022-07-14 tracey srv->unix_socket = 1;
900 6c8aa58f 2022-08-30 stsp srv->fcgi_socket = 0;
902 e087e1f6 2022-08-16 stsp TAILQ_INIT(&srv->al);
903 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
904 a596b957 2022-07-14 tracey gotwebd->server_cnt++;
906 a596b957 2022-07-14 tracey return srv;
910 a596b957 2022-07-14 tracey symset(const char *nam, const char *val, int persist)
912 a596b957 2022-07-14 tracey struct sym *sym;
914 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
915 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0)
919 a596b957 2022-07-14 tracey if (sym != NULL) {
920 a596b957 2022-07-14 tracey if (sym->persist == 1)
921 a596b957 2022-07-14 tracey return (0);
923 a596b957 2022-07-14 tracey free(sym->nam);
924 a596b957 2022-07-14 tracey free(sym->val);
925 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
926 a596b957 2022-07-14 tracey free(sym);
929 a596b957 2022-07-14 tracey sym = calloc(1, sizeof(*sym));
930 a596b957 2022-07-14 tracey if (sym == NULL)
931 a596b957 2022-07-14 tracey return (-1);
933 a596b957 2022-07-14 tracey sym->nam = strdup(nam);
934 a596b957 2022-07-14 tracey if (sym->nam == NULL) {
935 a596b957 2022-07-14 tracey free(sym);
936 a596b957 2022-07-14 tracey return (-1);
938 a596b957 2022-07-14 tracey sym->val = strdup(val);
939 a596b957 2022-07-14 tracey if (sym->val == NULL) {
940 a596b957 2022-07-14 tracey free(sym->nam);
941 a596b957 2022-07-14 tracey free(sym);
942 a596b957 2022-07-14 tracey return (-1);
944 a596b957 2022-07-14 tracey sym->used = 0;
945 a596b957 2022-07-14 tracey sym->persist = persist;
946 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&symhead, sym, entry);
947 a596b957 2022-07-14 tracey return (0);
951 a596b957 2022-07-14 tracey cmdline_symset(char *s)
953 a596b957 2022-07-14 tracey char *sym, *val;
956 a596b957 2022-07-14 tracey val = strrchr(s, '=');
957 a596b957 2022-07-14 tracey if (val == NULL)
958 a596b957 2022-07-14 tracey return (-1);
960 4cdd299d 2022-09-05 op sym = strndup(s, val - s);
961 a596b957 2022-07-14 tracey if (sym == NULL)
962 4cdd299d 2022-09-05 op fatal("%s: strndup", __func__);
964 a596b957 2022-07-14 tracey ret = symset(sym, val + 1, 1);
965 a596b957 2022-07-14 tracey free(sym);
967 a596b957 2022-07-14 tracey return (ret);
971 a596b957 2022-07-14 tracey symget(const char *nam)
973 a596b957 2022-07-14 tracey struct sym *sym;
975 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
976 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0) {
977 a596b957 2022-07-14 tracey sym->used = 1;
978 a596b957 2022-07-14 tracey return (sym->val);
981 a596b957 2022-07-14 tracey return (NULL);
985 a596b957 2022-07-14 tracey getservice(const char *n)
987 a596b957 2022-07-14 tracey struct servent *s;
988 a596b957 2022-07-14 tracey const char *errstr;
989 a596b957 2022-07-14 tracey long long llval;
991 a596b957 2022-07-14 tracey llval = strtonum(n, 0, UINT16_MAX, &errstr);
992 a596b957 2022-07-14 tracey if (errstr) {
993 a596b957 2022-07-14 tracey s = getservbyname(n, "tcp");
994 a596b957 2022-07-14 tracey if (s == NULL)
995 a596b957 2022-07-14 tracey s = getservbyname(n, "udp");
996 a596b957 2022-07-14 tracey if (s == NULL)
997 a596b957 2022-07-14 tracey return (-1);
998 859aa9f4 2022-08-19 stsp return ntohs(s->s_port);
1001 859aa9f4 2022-08-19 stsp return (unsigned short)llval;
1004 a596b957 2022-07-14 tracey struct address *
1005 a596b957 2022-07-14 tracey host_v4(const char *s)
1007 a596b957 2022-07-14 tracey struct in_addr ina;
1008 a596b957 2022-07-14 tracey struct sockaddr_in *sain;
1009 a596b957 2022-07-14 tracey struct address *h;
1011 a596b957 2022-07-14 tracey memset(&ina, 0, sizeof(ina));
1012 a596b957 2022-07-14 tracey if (inet_pton(AF_INET, s, &ina) != 1)
1013 a596b957 2022-07-14 tracey return (NULL);
1015 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1016 a596b957 2022-07-14 tracey fatal(__func__);
1017 a596b957 2022-07-14 tracey sain = (struct sockaddr_in *)&h->ss;
1018 86b4b772 2022-07-29 stsp got_sockaddr_inet_init(sain, &ina);
1019 a596b957 2022-07-14 tracey if (sain->sin_addr.s_addr == INADDR_ANY)
1020 a596b957 2022-07-14 tracey h->prefixlen = 0; /* 0.0.0.0 address */
1022 a596b957 2022-07-14 tracey h->prefixlen = -1; /* host address */
1023 a596b957 2022-07-14 tracey return (h);
1026 a596b957 2022-07-14 tracey struct address *
1027 a596b957 2022-07-14 tracey host_v6(const char *s)
1029 a596b957 2022-07-14 tracey struct addrinfo hints, *res;
1030 86b4b772 2022-07-29 stsp struct sockaddr_in6 *sa_in6, *ra;
1031 a596b957 2022-07-14 tracey struct address *h = NULL;
1033 a596b957 2022-07-14 tracey memset(&hints, 0, sizeof(hints));
1034 a596b957 2022-07-14 tracey hints.ai_family = AF_INET6;
1035 a596b957 2022-07-14 tracey hints.ai_socktype = SOCK_DGRAM; /* dummy */
1036 a596b957 2022-07-14 tracey hints.ai_flags = AI_NUMERICHOST;
1037 a596b957 2022-07-14 tracey if (getaddrinfo(s, "0", &hints, &res) == 0) {
1038 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1039 a596b957 2022-07-14 tracey fatal(__func__);
1040 a596b957 2022-07-14 tracey sa_in6 = (struct sockaddr_in6 *)&h->ss;
1041 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in6 *)res->ai_addr;
1042 86b4b772 2022-07-29 stsp got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1043 86b4b772 2022-07-29 stsp ra->sin6_scope_id);
1044 a596b957 2022-07-14 tracey if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1045 a596b957 2022-07-14 tracey sizeof(sa_in6->sin6_addr)) == 0)
1046 a596b957 2022-07-14 tracey h->prefixlen = 0; /* any address */
1048 a596b957 2022-07-14 tracey h->prefixlen = -1; /* host address */
1049 a596b957 2022-07-14 tracey freeaddrinfo(res);
1052 a596b957 2022-07-14 tracey return (h);
1056 67d8de2a 2022-08-29 stsp host_dns(const char *s, struct server *new_srv, int max,
1057 a596b957 2022-07-14 tracey in_port_t port, const char *ifname, int ipproto)
1059 a596b957 2022-07-14 tracey struct addrinfo hints, *res0, *res;
1060 a596b957 2022-07-14 tracey int error, cnt = 0;
1061 a596b957 2022-07-14 tracey struct sockaddr_in *sain;
1062 a596b957 2022-07-14 tracey struct sockaddr_in6 *sin6;
1063 a596b957 2022-07-14 tracey struct address *h;
1065 67d8de2a 2022-08-29 stsp if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1066 a596b957 2022-07-14 tracey return (cnt);
1068 a596b957 2022-07-14 tracey memset(&hints, 0, sizeof(hints));
1069 a596b957 2022-07-14 tracey hints.ai_family = PF_UNSPEC;
1070 a596b957 2022-07-14 tracey hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1071 a596b957 2022-07-14 tracey hints.ai_flags = AI_ADDRCONFIG;
1072 a596b957 2022-07-14 tracey error = getaddrinfo(s, NULL, &hints, &res0);
1073 a596b957 2022-07-14 tracey if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1074 a596b957 2022-07-14 tracey return (0);
1075 a596b957 2022-07-14 tracey if (error) {
1076 a596b957 2022-07-14 tracey log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1077 a596b957 2022-07-14 tracey gai_strerror(error));
1078 a596b957 2022-07-14 tracey return (-1);
1081 a596b957 2022-07-14 tracey for (res = res0; res && cnt < max; res = res->ai_next) {
1082 a596b957 2022-07-14 tracey if (res->ai_family != AF_INET &&
1083 a596b957 2022-07-14 tracey res->ai_family != AF_INET6)
1084 a596b957 2022-07-14 tracey continue;
1085 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1086 a596b957 2022-07-14 tracey fatal(__func__);
1088 a596b957 2022-07-14 tracey if (port)
1089 a596b957 2022-07-14 tracey h->port = port;
1090 a596b957 2022-07-14 tracey if (ifname != NULL) {
1091 a596b957 2022-07-14 tracey if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1092 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1093 a596b957 2022-07-14 tracey log_warnx("%s: interface name truncated",
1094 a596b957 2022-07-14 tracey __func__);
1095 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1096 a596b957 2022-07-14 tracey free(h);
1097 a596b957 2022-07-14 tracey return (-1);
1100 a596b957 2022-07-14 tracey if (ipproto != -1)
1101 a596b957 2022-07-14 tracey h->ipproto = ipproto;
1102 a596b957 2022-07-14 tracey h->ss.ss_family = res->ai_family;
1103 a596b957 2022-07-14 tracey h->prefixlen = -1; /* host address */
1105 a596b957 2022-07-14 tracey if (res->ai_family == AF_INET) {
1106 86b4b772 2022-07-29 stsp struct sockaddr_in *ra;
1107 a596b957 2022-07-14 tracey sain = (struct sockaddr_in *)&h->ss;
1108 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in *)res->ai_addr;
1109 86b4b772 2022-07-29 stsp got_sockaddr_inet_init(sain, &ra->sin_addr);
1110 a596b957 2022-07-14 tracey } else {
1111 86b4b772 2022-07-29 stsp struct sockaddr_in6 *ra;
1112 a596b957 2022-07-14 tracey sin6 = (struct sockaddr_in6 *)&h->ss;
1113 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in6 *)res->ai_addr;
1114 86b4b772 2022-07-29 stsp got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1117 67d8de2a 2022-08-29 stsp if (add_addr(new_srv, h))
1118 67d8de2a 2022-08-29 stsp return -1;
1121 a596b957 2022-07-14 tracey if (cnt == max && res) {
1122 a596b957 2022-07-14 tracey log_warnx("%s: %s resolves to more than %d hosts", __func__,
1123 a596b957 2022-07-14 tracey s, max);
1125 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1126 a596b957 2022-07-14 tracey return (cnt);
1130 67d8de2a 2022-08-29 stsp host_if(const char *s, struct server *new_srv, int max,
1131 a596b957 2022-07-14 tracey in_port_t port, const char *ifname, int ipproto)
1133 a596b957 2022-07-14 tracey struct ifaddrs *ifap, *p;
1134 a596b957 2022-07-14 tracey struct sockaddr_in *sain;
1135 a596b957 2022-07-14 tracey struct sockaddr_in6 *sin6;
1136 a596b957 2022-07-14 tracey struct address *h;
1137 a596b957 2022-07-14 tracey int cnt = 0, af;
1139 a596b957 2022-07-14 tracey if (getifaddrs(&ifap) == -1)
1140 a596b957 2022-07-14 tracey fatal("getifaddrs");
1142 a596b957 2022-07-14 tracey /* First search for IPv4 addresses */
1143 a596b957 2022-07-14 tracey af = AF_INET;
1146 a596b957 2022-07-14 tracey for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1147 a596b957 2022-07-14 tracey if (p->ifa_addr == NULL ||
1148 a596b957 2022-07-14 tracey p->ifa_addr->sa_family != af ||
1149 a596b957 2022-07-14 tracey (strcmp(s, p->ifa_name) != 0 &&
1150 a596b957 2022-07-14 tracey !is_if_in_group(p->ifa_name, s)))
1151 a596b957 2022-07-14 tracey continue;
1152 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1153 a596b957 2022-07-14 tracey fatal("calloc");
1155 a596b957 2022-07-14 tracey if (port)
1156 a596b957 2022-07-14 tracey h->port = port;
1157 a596b957 2022-07-14 tracey if (ifname != NULL) {
1158 a596b957 2022-07-14 tracey if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1159 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1160 a596b957 2022-07-14 tracey log_warnx("%s: interface name truncated",
1161 a596b957 2022-07-14 tracey __func__);
1162 a596b957 2022-07-14 tracey free(h);
1163 a596b957 2022-07-14 tracey freeifaddrs(ifap);
1164 a596b957 2022-07-14 tracey return (-1);
1167 a596b957 2022-07-14 tracey if (ipproto != -1)
1168 a596b957 2022-07-14 tracey h->ipproto = ipproto;
1169 a596b957 2022-07-14 tracey h->ss.ss_family = af;
1170 a596b957 2022-07-14 tracey h->prefixlen = -1; /* host address */
1172 a596b957 2022-07-14 tracey if (af == AF_INET) {
1173 86b4b772 2022-07-29 stsp struct sockaddr_in *ra;
1174 a596b957 2022-07-14 tracey sain = (struct sockaddr_in *)&h->ss;
1175 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in *)p->ifa_addr;
1176 86b4b772 2022-07-29 stsp got_sockaddr_inet_init(sain, &ra->sin_addr);
1177 a596b957 2022-07-14 tracey } else {
1178 86b4b772 2022-07-29 stsp struct sockaddr_in6 *ra;
1179 a596b957 2022-07-14 tracey sin6 = (struct sockaddr_in6 *)&h->ss;
1180 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in6 *)p->ifa_addr;
1181 86b4b772 2022-07-29 stsp got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1182 86b4b772 2022-07-29 stsp ra->sin6_scope_id);
1185 67d8de2a 2022-08-29 stsp if (add_addr(new_srv, h))
1186 67d8de2a 2022-08-29 stsp return -1;
1189 a596b957 2022-07-14 tracey if (af == AF_INET) {
1190 a596b957 2022-07-14 tracey /* Next search for IPv6 addresses */
1191 a596b957 2022-07-14 tracey af = AF_INET6;
1192 a596b957 2022-07-14 tracey goto nextaf;
1195 a596b957 2022-07-14 tracey if (cnt > max) {
1196 a596b957 2022-07-14 tracey log_warnx("%s: %s resolves to more than %d hosts", __func__,
1197 a596b957 2022-07-14 tracey s, max);
1199 a596b957 2022-07-14 tracey freeifaddrs(ifap);
1200 a596b957 2022-07-14 tracey return (cnt);
1204 67d8de2a 2022-08-29 stsp host(const char *s, struct server *new_srv, int max,
1205 a596b957 2022-07-14 tracey in_port_t port, const char *ifname, int ipproto)
1207 a596b957 2022-07-14 tracey struct address *h;
1209 a596b957 2022-07-14 tracey h = host_v4(s);
1211 a596b957 2022-07-14 tracey /* IPv6 address? */
1212 a596b957 2022-07-14 tracey if (h == NULL)
1213 a596b957 2022-07-14 tracey h = host_v6(s);
1215 a596b957 2022-07-14 tracey if (h != NULL) {
1216 a596b957 2022-07-14 tracey if (port)
1217 a596b957 2022-07-14 tracey h->port = port;
1218 a596b957 2022-07-14 tracey if (ifname != NULL) {
1219 a596b957 2022-07-14 tracey if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1220 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1221 a596b957 2022-07-14 tracey log_warnx("%s: interface name truncated",
1222 a596b957 2022-07-14 tracey __func__);
1223 a596b957 2022-07-14 tracey free(h);
1224 a596b957 2022-07-14 tracey return (-1);
1227 a596b957 2022-07-14 tracey if (ipproto != -1)
1228 a596b957 2022-07-14 tracey h->ipproto = ipproto;
1230 67d8de2a 2022-08-29 stsp if (add_addr(new_srv, h))
1231 67d8de2a 2022-08-29 stsp return -1;
1232 a596b957 2022-07-14 tracey return (1);
1235 67d8de2a 2022-08-29 stsp return (host_dns(s, new_srv, max, port, ifname, ipproto));
1239 a596b957 2022-07-14 tracey is_if_in_group(const char *ifname, const char *groupname)
1241 a596b957 2022-07-14 tracey unsigned int len;
1242 a596b957 2022-07-14 tracey struct ifgroupreq ifgr;
1243 a596b957 2022-07-14 tracey struct ifg_req *ifg;
1245 a596b957 2022-07-14 tracey int ret = 0;
1247 a596b957 2022-07-14 tracey if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1248 a596b957 2022-07-14 tracey err(1, "socket");
1250 a596b957 2022-07-14 tracey memset(&ifgr, 0, sizeof(ifgr));
1251 a596b957 2022-07-14 tracey if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1252 a596b957 2022-07-14 tracey err(1, "IFNAMSIZ");
1253 a596b957 2022-07-14 tracey if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1254 a596b957 2022-07-14 tracey if (errno == EINVAL || errno == ENOTTY)
1255 a596b957 2022-07-14 tracey goto end;
1256 a596b957 2022-07-14 tracey err(1, "SIOCGIFGROUP");
1259 a596b957 2022-07-14 tracey len = ifgr.ifgr_len;
1260 a596b957 2022-07-14 tracey ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1261 a596b957 2022-07-14 tracey sizeof(struct ifg_req));
1262 a596b957 2022-07-14 tracey if (ifgr.ifgr_groups == NULL)
1263 a596b957 2022-07-14 tracey err(1, "getifgroups");
1264 a596b957 2022-07-14 tracey if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1265 a596b957 2022-07-14 tracey err(1, "SIOCGIFGROUP");
1267 a596b957 2022-07-14 tracey ifg = ifgr.ifgr_groups;
1268 a596b957 2022-07-14 tracey for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1269 a596b957 2022-07-14 tracey len -= sizeof(struct ifg_req);
1270 a596b957 2022-07-14 tracey if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1271 a596b957 2022-07-14 tracey ret = 1;
1275 a596b957 2022-07-14 tracey free(ifgr.ifgr_groups);
1278 a596b957 2022-07-14 tracey close(s);
1279 a596b957 2022-07-14 tracey return (ret);
1283 67d8de2a 2022-08-29 stsp get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1285 a596b957 2022-07-14 tracey if (strcmp("", addr) == 0) {
1286 67d8de2a 2022-08-29 stsp if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1287 67d8de2a 2022-08-29 stsp -1) <= 0) {
1288 a596b957 2022-07-14 tracey yyerror("invalid listen ip: %s",
1289 2fb25d87 2022-08-19 stsp "127.0.0.1");
1290 a596b957 2022-07-14 tracey return (-1);
1292 67d8de2a 2022-08-29 stsp if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1293 2fb25d87 2022-08-19 stsp yyerror("invalid listen ip: %s", "::1");
1294 a596b957 2022-07-14 tracey return (-1);
1296 a596b957 2022-07-14 tracey } else {
1297 67d8de2a 2022-08-29 stsp if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1298 a596b957 2022-07-14 tracey -1) <= 0) {
1299 a596b957 2022-07-14 tracey yyerror("invalid listen ip: %s", addr);
1300 a596b957 2022-07-14 tracey return (-1);
1303 a596b957 2022-07-14 tracey return (0);
1307 67d8de2a 2022-08-29 stsp addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1308 67d8de2a 2022-08-29 stsp const char *other_srv)
1310 67d8de2a 2022-08-29 stsp struct address *a;
1312 67d8de2a 2022-08-29 stsp char buf[INET6_ADDRSTRLEN];
1313 67d8de2a 2022-08-29 stsp const char *addrstr;
1315 67d8de2a 2022-08-29 stsp TAILQ_FOREACH(a, al, entry) {
1316 67d8de2a 2022-08-29 stsp if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1317 67d8de2a 2022-08-29 stsp a->port != h->port)
1320 67d8de2a 2022-08-29 stsp switch (h->ss.ss_family) {
1321 67d8de2a 2022-08-29 stsp case AF_INET:
1322 67d8de2a 2022-08-29 stsp ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1324 67d8de2a 2022-08-29 stsp case AF_INET6:
1325 67d8de2a 2022-08-29 stsp ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1328 67d8de2a 2022-08-29 stsp yyerror("unknown address family: %d", h->ss.ss_family);
1329 67d8de2a 2022-08-29 stsp return -1;
1331 67d8de2a 2022-08-29 stsp addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1332 67d8de2a 2022-08-29 stsp if (addrstr) {
1333 67d8de2a 2022-08-29 stsp if (other_srv) {
1334 67d8de2a 2022-08-29 stsp yyerror("server %s: duplicate fcgi listen "
1335 67d8de2a 2022-08-29 stsp "address %s:%d, already used by server %s",
1336 67d8de2a 2022-08-29 stsp new_srv, addrstr, h->port, other_srv);
1338 67d8de2a 2022-08-29 stsp log_warnx("server: %s: duplicate fcgi listen "
1339 67d8de2a 2022-08-29 stsp "address %s:%d", new_srv, addrstr, h->port);
1342 67d8de2a 2022-08-29 stsp if (other_srv) {
1343 67d8de2a 2022-08-29 stsp yyerror("server: %s: duplicate fcgi listen "
1344 67d8de2a 2022-08-29 stsp "address, already used by server %s",
1345 67d8de2a 2022-08-29 stsp new_srv, other_srv);
1347 67d8de2a 2022-08-29 stsp log_warnx("server %s: duplicate fcgi listen "
1348 67d8de2a 2022-08-29 stsp "address", new_srv);
1352 67d8de2a 2022-08-29 stsp return -1;
1359 67d8de2a 2022-08-29 stsp add_addr(struct server *new_srv, struct address *h)
1361 67d8de2a 2022-08-29 stsp struct server *srv;
1363 67d8de2a 2022-08-29 stsp /* Address cannot be shared between different servers. */
1364 67d8de2a 2022-08-29 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1365 67d8de2a 2022-08-29 stsp if (srv == new_srv)
1367 67d8de2a 2022-08-29 stsp if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1368 67d8de2a 2022-08-29 stsp return -1;
1371 67d8de2a 2022-08-29 stsp /* Tolerate duplicate address lines within the scope of a server. */
1372 67d8de2a 2022-08-29 stsp if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1373 67d8de2a 2022-08-29 stsp TAILQ_INSERT_TAIL(&new_srv->al, h, entry);