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.
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.
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.
24 8a35f56c 2022-07-16 thomas #include <sys/ioctl.h>
25 8a35f56c 2022-07-16 thomas #include <sys/types.h>
26 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
27 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
28 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
30 8a35f56c 2022-07-16 thomas #include <net/if.h>
31 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
33 8a35f56c 2022-07-16 thomas #include <arpa/inet.h>
35 8a35f56c 2022-07-16 thomas #include <ctype.h>
36 8a35f56c 2022-07-16 thomas #include <err.h>
37 8a35f56c 2022-07-16 thomas #include <errno.h>
38 8a35f56c 2022-07-16 thomas #include <event.h>
39 8a35f56c 2022-07-16 thomas #include <ifaddrs.h>
40 8a35f56c 2022-07-16 thomas #include <limits.h>
41 8a35f56c 2022-07-16 thomas #include <netdb.h>
42 8a35f56c 2022-07-16 thomas #include <stdarg.h>
43 8a35f56c 2022-07-16 thomas #include <stdlib.h>
44 8a35f56c 2022-07-16 thomas #include <stdio.h>
45 8a35f56c 2022-07-16 thomas #include <string.h>
46 8a35f56c 2022-07-16 thomas #include <syslog.h>
47 8a35f56c 2022-07-16 thomas #include <unistd.h>
49 8a35f56c 2022-07-16 thomas #include "proc.h"
50 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
51 cdbe1d7d 2022-08-06 thomas #include "got_sockaddr.h"
53 8a35f56c 2022-07-16 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
54 8a35f56c 2022-07-16 thomas static struct file {
55 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(file) entry;
56 8a35f56c 2022-07-16 thomas FILE *stream;
57 8a35f56c 2022-07-16 thomas char *name;
58 8a35f56c 2022-07-16 thomas int lineno;
59 8a35f56c 2022-07-16 thomas int errors;
61 8a35f56c 2022-07-16 thomas struct file *newfile(const char *, int);
62 8a35f56c 2022-07-16 thomas static void closefile(struct file *);
63 8a35f56c 2022-07-16 thomas int check_file_secrecy(int, const char *);
64 8a35f56c 2022-07-16 thomas int yyparse(void);
65 8a35f56c 2022-07-16 thomas int yylex(void);
66 8a35f56c 2022-07-16 thomas int yyerror(const char *, ...)
67 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)))
68 8a35f56c 2022-07-16 thomas __attribute__((__nonnull__ (1)));
69 8a35f56c 2022-07-16 thomas int kw_cmp(const void *, const void *);
70 8a35f56c 2022-07-16 thomas int lookup(char *);
71 8a35f56c 2022-07-16 thomas int lgetc(int);
72 8a35f56c 2022-07-16 thomas int lungetc(int);
73 8a35f56c 2022-07-16 thomas int findeol(void);
75 8a35f56c 2022-07-16 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 8a35f56c 2022-07-16 thomas struct sym {
77 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(sym) entry;
79 8a35f56c 2022-07-16 thomas int persist;
80 8a35f56c 2022-07-16 thomas char *nam;
81 8a35f56c 2022-07-16 thomas char *val;
84 8a35f56c 2022-07-16 thomas int symset(const char *, const char *, int);
85 8a35f56c 2022-07-16 thomas char *symget(const char *);
87 8a35f56c 2022-07-16 thomas static int errors;
89 8a35f56c 2022-07-16 thomas static struct gotwebd *gotwebd;
90 8a35f56c 2022-07-16 thomas static struct server *new_srv;
91 8a35f56c 2022-07-16 thomas static struct server *conf_new_server(const char *);
92 8a35f56c 2022-07-16 thomas int getservice(const char *);
95 e4c7e0b0 2022-08-30 thomas int get_addrs(const char *, struct server *, in_port_t);
96 e4c7e0b0 2022-08-30 thomas int addr_dup_check(struct addresslist *, struct address *,
97 e4c7e0b0 2022-08-30 thomas const char *, const char *);
98 e4c7e0b0 2022-08-30 thomas int add_addr(struct server *, struct address *);
99 8a35f56c 2022-07-16 thomas struct address *host_v4(const char *);
100 8a35f56c 2022-07-16 thomas struct address *host_v6(const char *);
101 e4c7e0b0 2022-08-30 thomas int host_dns(const char *, struct server *,
102 8a35f56c 2022-07-16 thomas int, in_port_t, const char *, int);
103 e4c7e0b0 2022-08-30 thomas int host_if(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(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 *);
109 8a35f56c 2022-07-16 thomas typedef struct {
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;
115 8a35f56c 2022-07-16 thomas int lineno;
116 8a35f56c 2022-07-16 thomas } YYSTYPE;
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 b73ef4cf 2022-08-30 thomas %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK
124 8a35f56c 2022-07-16 thomas %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
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
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++; }
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);
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);
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, "yes") == 0 ||
162 8a35f56c 2022-07-16 thomas strcasecmp($1, "on") == 0)
164 8a35f56c 2022-07-16 thomas else if (strcasecmp($1, "0") == 0 ||
165 8a35f56c 2022-07-16 thomas strcasecmp($1, "off") == 0 ||
166 8a35f56c 2022-07-16 thomas strcasecmp($1, "no") == 0)
169 8a35f56c 2022-07-16 thomas yyerror("invalid boolean value '%s'", $1);
170 8a35f56c 2022-07-16 thomas free($1);
173 8a35f56c 2022-07-16 thomas free($1);
175 8a35f56c 2022-07-16 thomas | ON { $$ = 1; }
176 8a35f56c 2022-07-16 thomas | NUMBER { $$ = $1; }
179 77fb808d 2022-08-29 thomas fcgiport : PORT NUMBER {
180 77fb808d 2022-08-29 thomas if ($2 <= 0 || $2 > (int)USHRT_MAX) {
181 77fb808d 2022-08-29 thomas yyerror("invalid port: %lld", $2);
186 77fb808d 2022-08-29 thomas | PORT STRING {
189 77fb808d 2022-08-29 thomas if ((val = getservice($2)) == -1) {
190 77fb808d 2022-08-29 thomas yyerror("invalid port: %s", $2);
191 77fb808d 2022-08-29 thomas free($2);
194 77fb808d 2022-08-29 thomas free($2);
196 8a35f56c 2022-07-16 thomas $$ = val;
200 8a35f56c 2022-07-16 thomas main : PREFORK NUMBER {
201 8a35f56c 2022-07-16 thomas gotwebd->prefork_gotwebd = $2;
203 8a35f56c 2022-07-16 thomas | CHROOT STRING {
204 8a35f56c 2022-07-16 thomas n = strlcpy(gotwebd->httpd_chroot, $2,
205 8a35f56c 2022-07-16 thomas sizeof(gotwebd->httpd_chroot));
206 8a35f56c 2022-07-16 thomas if (n >= sizeof(gotwebd->httpd_chroot)) {
207 8a35f56c 2022-07-16 thomas yyerror("%s: httpd_chroot truncated", __func__);
208 8a35f56c 2022-07-16 thomas free($2);
211 8a35f56c 2022-07-16 thomas free($2);
213 8a35f56c 2022-07-16 thomas | UNIX_SOCKET boolean {
214 8a35f56c 2022-07-16 thomas gotwebd->unix_socket = $2;
216 8a35f56c 2022-07-16 thomas | UNIX_SOCKET_NAME STRING {
217 8a35f56c 2022-07-16 thomas n = snprintf(gotwebd->unix_socket_name,
218 8a35f56c 2022-07-16 thomas sizeof(gotwebd->unix_socket_name), "%s%s",
219 8a35f56c 2022-07-16 thomas strlen(gotwebd->httpd_chroot) ?
220 8a35f56c 2022-07-16 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
221 717a78d4 2022-08-16 thomas if (n < 0 ||
222 717a78d4 2022-08-16 thomas (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
223 8a35f56c 2022-07-16 thomas yyerror("%s: unix_socket_name truncated",
224 8a35f56c 2022-07-16 thomas __func__);
225 8a35f56c 2022-07-16 thomas free($2);
228 8a35f56c 2022-07-16 thomas free($2);
232 8a35f56c 2022-07-16 thomas server : SERVER STRING {
233 8a35f56c 2022-07-16 thomas struct server *srv;
235 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
236 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, $2) == 0) {
237 8a35f56c 2022-07-16 thomas yyerror("server name exists '%s'", $2);
238 8a35f56c 2022-07-16 thomas free($2);
243 8a35f56c 2022-07-16 thomas new_srv = conf_new_server($2);
244 8a35f56c 2022-07-16 thomas log_debug("adding server %s", $2);
245 8a35f56c 2022-07-16 thomas free($2);
247 8a35f56c 2022-07-16 thomas | SERVER STRING {
248 8a35f56c 2022-07-16 thomas struct server *srv;
250 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
251 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, $2) == 0) {
252 8a35f56c 2022-07-16 thomas yyerror("server name exists '%s'", $2);
253 8a35f56c 2022-07-16 thomas free($2);
258 8a35f56c 2022-07-16 thomas new_srv = conf_new_server($2);
259 8a35f56c 2022-07-16 thomas log_debug("adding server %s", $2);
260 8a35f56c 2022-07-16 thomas free($2);
261 8a35f56c 2022-07-16 thomas } '{' optnl serveropts2 '}' {
265 8a35f56c 2022-07-16 thomas serveropts1 : REPOS_PATH STRING {
266 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->repos_path, $2,
267 8a35f56c 2022-07-16 thomas sizeof(new_srv->repos_path));
268 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->repos_path)) {
269 8a35f56c 2022-07-16 thomas yyerror("%s: repos_path truncated", __func__);
270 8a35f56c 2022-07-16 thomas free($2);
273 8a35f56c 2022-07-16 thomas free($2);
275 8a35f56c 2022-07-16 thomas | SITE_NAME STRING {
276 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_name, $2,
277 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_name));
278 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_name)) {
279 8a35f56c 2022-07-16 thomas yyerror("%s: site_name truncated", __func__);
280 8a35f56c 2022-07-16 thomas free($2);
283 8a35f56c 2022-07-16 thomas free($2);
285 8a35f56c 2022-07-16 thomas | SITE_OWNER STRING {
286 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_owner, $2,
287 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_owner));
288 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_owner)) {
289 8a35f56c 2022-07-16 thomas yyerror("%s: site_owner truncated", __func__);
290 8a35f56c 2022-07-16 thomas free($2);
293 8a35f56c 2022-07-16 thomas free($2);
295 8a35f56c 2022-07-16 thomas | SITE_LINK STRING {
296 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_link, $2,
297 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_link));
298 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_link)) {
299 8a35f56c 2022-07-16 thomas yyerror("%s: site_link truncated", __func__);
300 8a35f56c 2022-07-16 thomas free($2);
303 8a35f56c 2022-07-16 thomas free($2);
305 8a35f56c 2022-07-16 thomas | LOGO STRING {
306 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
307 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->logo)) {
308 8a35f56c 2022-07-16 thomas yyerror("%s: logo truncated", __func__);
309 8a35f56c 2022-07-16 thomas free($2);
312 8a35f56c 2022-07-16 thomas free($2);
314 8a35f56c 2022-07-16 thomas | LOGO_URL STRING {
315 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->logo_url, $2,
316 8a35f56c 2022-07-16 thomas sizeof(new_srv->logo_url));
317 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->logo_url)) {
318 8a35f56c 2022-07-16 thomas yyerror("%s: logo_url truncated", __func__);
319 8a35f56c 2022-07-16 thomas free($2);
322 8a35f56c 2022-07-16 thomas free($2);
324 8a35f56c 2022-07-16 thomas | CUSTOM_CSS STRING {
325 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->custom_css, $2,
326 8a35f56c 2022-07-16 thomas sizeof(new_srv->custom_css));
327 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->custom_css)) {
328 8a35f56c 2022-07-16 thomas yyerror("%s: custom_css truncated", __func__);
329 8a35f56c 2022-07-16 thomas free($2);
332 8a35f56c 2022-07-16 thomas free($2);
334 77fb808d 2022-08-29 thomas | LISTEN ON STRING fcgiport {
335 e4c7e0b0 2022-08-30 thomas if (get_addrs($3, new_srv, $4) == -1) {
336 e4c7e0b0 2022-08-30 thomas yyerror("could not get addrs");
339 e4317279 2022-08-30 thomas new_srv->fcgi_socket = 1;
341 8a35f56c 2022-07-16 thomas | MAX_REPOS NUMBER {
342 8a35f56c 2022-07-16 thomas if ($2 > 0)
343 8a35f56c 2022-07-16 thomas new_srv->max_repos = $2;
345 8a35f56c 2022-07-16 thomas | SHOW_SITE_OWNER boolean {
346 8a35f56c 2022-07-16 thomas new_srv->show_site_owner = $2;
348 8a35f56c 2022-07-16 thomas | SHOW_REPO_OWNER boolean {
349 8a35f56c 2022-07-16 thomas new_srv->show_repo_owner = $2;
351 8a35f56c 2022-07-16 thomas | SHOW_REPO_AGE boolean {
352 8a35f56c 2022-07-16 thomas new_srv->show_repo_age = $2;
354 8a35f56c 2022-07-16 thomas | SHOW_REPO_DESCRIPTION boolean {
355 8a35f56c 2022-07-16 thomas new_srv->show_repo_description = $2;
357 8a35f56c 2022-07-16 thomas | SHOW_REPO_CLONEURL boolean {
358 8a35f56c 2022-07-16 thomas new_srv->show_repo_cloneurl = $2;
360 8a35f56c 2022-07-16 thomas | MAX_REPOS_DISPLAY NUMBER {
361 8a35f56c 2022-07-16 thomas new_srv->max_repos_display = $2;
363 8a35f56c 2022-07-16 thomas | MAX_COMMITS_DISPLAY NUMBER {
364 8a35f56c 2022-07-16 thomas if ($2 > 0)
365 8a35f56c 2022-07-16 thomas new_srv->max_commits_display = $2;
367 8a35f56c 2022-07-16 thomas | UNIX_SOCKET boolean {
368 8a35f56c 2022-07-16 thomas new_srv->unix_socket = $2;
370 8a35f56c 2022-07-16 thomas | UNIX_SOCKET_NAME STRING {
371 8a35f56c 2022-07-16 thomas n = snprintf(new_srv->unix_socket_name,
372 8a35f56c 2022-07-16 thomas sizeof(new_srv->unix_socket_name), "%s%s",
373 8a35f56c 2022-07-16 thomas strlen(gotwebd->httpd_chroot) ?
374 8a35f56c 2022-07-16 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
375 717a78d4 2022-08-16 thomas if (n < 0 ||
376 717a78d4 2022-08-16 thomas (size_t)n >= sizeof(new_srv->unix_socket_name)) {
377 8a35f56c 2022-07-16 thomas yyerror("%s: unix_socket_name truncated",
378 8a35f56c 2022-07-16 thomas __func__);
379 8a35f56c 2022-07-16 thomas free($2);
382 8a35f56c 2022-07-16 thomas free($2);
386 8a35f56c 2022-07-16 thomas serveropts2 : serveropts2 serveropts1 nl
387 8a35f56c 2022-07-16 thomas | serveropts1 optnl
390 8a35f56c 2022-07-16 thomas nl : '\n' optnl
393 8a35f56c 2022-07-16 thomas optnl : '\n' optnl /* zero or more newlines */
394 8a35f56c 2022-07-16 thomas | /* empty */
399 8a35f56c 2022-07-16 thomas struct keywords {
400 8a35f56c 2022-07-16 thomas const char *k_name;
401 8a35f56c 2022-07-16 thomas int k_val;
405 8a35f56c 2022-07-16 thomas yyerror(const char *fmt, ...)
407 8a35f56c 2022-07-16 thomas va_list ap;
408 8a35f56c 2022-07-16 thomas char *msg;
410 8a35f56c 2022-07-16 thomas file->errors++;
411 8a35f56c 2022-07-16 thomas va_start(ap, fmt);
412 8a35f56c 2022-07-16 thomas if (vasprintf(&msg, fmt, ap) == -1)
413 8a35f56c 2022-07-16 thomas fatalx("yyerror vasprintf");
414 8a35f56c 2022-07-16 thomas va_end(ap);
415 8a35f56c 2022-07-16 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
416 8a35f56c 2022-07-16 thomas free(msg);
417 8a35f56c 2022-07-16 thomas return (0);
421 8a35f56c 2022-07-16 thomas kw_cmp(const void *k, const void *e)
423 8a35f56c 2022-07-16 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
427 8a35f56c 2022-07-16 thomas lookup(char *s)
429 8a35f56c 2022-07-16 thomas /* This has to be sorted always. */
430 8a35f56c 2022-07-16 thomas static const struct keywords keywords[] = {
431 8a35f56c 2022-07-16 thomas { "chroot", CHROOT },
432 8a35f56c 2022-07-16 thomas { "custom_css", CUSTOM_CSS },
433 c24a47af 2022-08-11 thomas { "listen", LISTEN },
434 8a35f56c 2022-07-16 thomas { "logo", LOGO },
435 8a35f56c 2022-07-16 thomas { "logo_url" , LOGO_URL },
436 8a35f56c 2022-07-16 thomas { "max_commits_display", MAX_COMMITS_DISPLAY },
437 8a35f56c 2022-07-16 thomas { "max_repos", MAX_REPOS },
438 8a35f56c 2022-07-16 thomas { "max_repos_display", MAX_REPOS_DISPLAY },
439 c24a47af 2022-08-11 thomas { "on", ON },
440 8a35f56c 2022-07-16 thomas { "port", PORT },
441 8a35f56c 2022-07-16 thomas { "prefork", PREFORK },
442 8a35f56c 2022-07-16 thomas { "repos_path", REPOS_PATH },
443 8a35f56c 2022-07-16 thomas { "server", SERVER },
444 8a35f56c 2022-07-16 thomas { "show_repo_age", SHOW_REPO_AGE },
445 8a35f56c 2022-07-16 thomas { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
446 8a35f56c 2022-07-16 thomas { "show_repo_description", SHOW_REPO_DESCRIPTION },
447 8a35f56c 2022-07-16 thomas { "show_repo_owner", SHOW_REPO_OWNER },
448 8a35f56c 2022-07-16 thomas { "show_site_owner", SHOW_SITE_OWNER },
449 8a35f56c 2022-07-16 thomas { "site_link", SITE_LINK },
450 8a35f56c 2022-07-16 thomas { "site_name", SITE_NAME },
451 8a35f56c 2022-07-16 thomas { "site_owner", SITE_OWNER },
452 8a35f56c 2022-07-16 thomas { "unix_socket", UNIX_SOCKET },
453 8a35f56c 2022-07-16 thomas { "unix_socket_name", UNIX_SOCKET_NAME },
455 8a35f56c 2022-07-16 thomas const struct keywords *p;
457 8a35f56c 2022-07-16 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
458 8a35f56c 2022-07-16 thomas sizeof(keywords[0]), kw_cmp);
461 8a35f56c 2022-07-16 thomas return (p->k_val);
463 8a35f56c 2022-07-16 thomas return (STRING);
466 8a35f56c 2022-07-16 thomas #define MAXPUSHBACK 128
468 8a35f56c 2022-07-16 thomas unsigned char *parsebuf;
469 8a35f56c 2022-07-16 thomas int parseindex;
470 8a35f56c 2022-07-16 thomas unsigned char pushback_buffer[MAXPUSHBACK];
471 8a35f56c 2022-07-16 thomas int pushback_index = 0;
474 8a35f56c 2022-07-16 thomas lgetc(int quotec)
476 8a35f56c 2022-07-16 thomas int c, next;
478 8a35f56c 2022-07-16 thomas if (parsebuf) {
479 8a35f56c 2022-07-16 thomas /* Read character from the parsebuffer instead of input. */
480 8a35f56c 2022-07-16 thomas if (parseindex >= 0) {
481 8a35f56c 2022-07-16 thomas c = parsebuf[parseindex++];
482 8a35f56c 2022-07-16 thomas if (c != '\0')
483 8a35f56c 2022-07-16 thomas return (c);
484 8a35f56c 2022-07-16 thomas parsebuf = NULL;
486 8a35f56c 2022-07-16 thomas parseindex++;
489 8a35f56c 2022-07-16 thomas if (pushback_index)
490 8a35f56c 2022-07-16 thomas return (pushback_buffer[--pushback_index]);
492 8a35f56c 2022-07-16 thomas if (quotec) {
493 8a35f56c 2022-07-16 thomas c = getc(file->stream);
494 8a35f56c 2022-07-16 thomas if (c == EOF)
495 8a35f56c 2022-07-16 thomas yyerror("reached end of file while parsing "
496 8a35f56c 2022-07-16 thomas "quoted string");
497 8a35f56c 2022-07-16 thomas return (c);
500 8a35f56c 2022-07-16 thomas c = getc(file->stream);
501 8a35f56c 2022-07-16 thomas while (c == '\\') {
502 8a35f56c 2022-07-16 thomas next = getc(file->stream);
503 8a35f56c 2022-07-16 thomas if (next != '\n') {
504 8a35f56c 2022-07-16 thomas c = next;
507 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
508 8a35f56c 2022-07-16 thomas file->lineno++;
509 8a35f56c 2022-07-16 thomas c = getc(file->stream);
512 8a35f56c 2022-07-16 thomas return (c);
516 8a35f56c 2022-07-16 thomas lungetc(int c)
518 8a35f56c 2022-07-16 thomas if (c == EOF)
519 8a35f56c 2022-07-16 thomas return (EOF);
520 8a35f56c 2022-07-16 thomas if (parsebuf) {
521 8a35f56c 2022-07-16 thomas parseindex--;
522 8a35f56c 2022-07-16 thomas if (parseindex >= 0)
523 8a35f56c 2022-07-16 thomas return (c);
525 8a35f56c 2022-07-16 thomas if (pushback_index < MAXPUSHBACK-1)
526 8a35f56c 2022-07-16 thomas return (pushback_buffer[pushback_index++] = c);
528 8a35f56c 2022-07-16 thomas return (EOF);
532 8a35f56c 2022-07-16 thomas findeol(void)
536 8a35f56c 2022-07-16 thomas parsebuf = NULL;
538 8a35f56c 2022-07-16 thomas /* Skip to either EOF or the first real EOL. */
539 8a35f56c 2022-07-16 thomas while (1) {
540 8a35f56c 2022-07-16 thomas if (pushback_index)
541 8a35f56c 2022-07-16 thomas c = pushback_buffer[--pushback_index];
543 8a35f56c 2022-07-16 thomas c = lgetc(0);
544 8a35f56c 2022-07-16 thomas if (c == '\n') {
545 8a35f56c 2022-07-16 thomas file->lineno++;
548 8a35f56c 2022-07-16 thomas if (c == EOF)
551 8a35f56c 2022-07-16 thomas return (ERROR);
555 8a35f56c 2022-07-16 thomas yylex(void)
557 8a35f56c 2022-07-16 thomas unsigned char buf[8096];
558 8a35f56c 2022-07-16 thomas unsigned char *p, *val;
559 8a35f56c 2022-07-16 thomas int quotec, next, c;
560 8a35f56c 2022-07-16 thomas int token;
564 8a35f56c 2022-07-16 thomas c = lgetc(0);
565 8a35f56c 2022-07-16 thomas while (c == ' ' || c == '\t')
566 8a35f56c 2022-07-16 thomas c = lgetc(0); /* nothing */
568 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
569 8a35f56c 2022-07-16 thomas if (c == '#') {
570 8a35f56c 2022-07-16 thomas c = lgetc(0);
571 8a35f56c 2022-07-16 thomas while (c != '\n' && c != EOF)
572 8a35f56c 2022-07-16 thomas c = lgetc(0); /* nothing */
574 8a35f56c 2022-07-16 thomas if (c == '$' && parsebuf == NULL) {
575 8a35f56c 2022-07-16 thomas while (1) {
576 8a35f56c 2022-07-16 thomas c = lgetc(0);
577 8a35f56c 2022-07-16 thomas if (c == EOF)
578 8a35f56c 2022-07-16 thomas return (0);
580 8a35f56c 2022-07-16 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
581 8a35f56c 2022-07-16 thomas yyerror("string too long");
582 8a35f56c 2022-07-16 thomas return (findeol());
584 8a35f56c 2022-07-16 thomas if (isalnum(c) || c == '_') {
585 8a35f56c 2022-07-16 thomas *p++ = c;
586 8a35f56c 2022-07-16 thomas continue;
588 8a35f56c 2022-07-16 thomas *p = '\0';
589 8a35f56c 2022-07-16 thomas lungetc(c);
592 8a35f56c 2022-07-16 thomas val = symget(buf);
593 8a35f56c 2022-07-16 thomas if (val == NULL) {
594 8a35f56c 2022-07-16 thomas yyerror("macro '%s' not defined", buf);
595 8a35f56c 2022-07-16 thomas return (findeol());
597 8a35f56c 2022-07-16 thomas parsebuf = val;
598 8a35f56c 2022-07-16 thomas parseindex = 0;
599 8a35f56c 2022-07-16 thomas goto top;
602 8a35f56c 2022-07-16 thomas switch (c) {
603 8a35f56c 2022-07-16 thomas case '\'':
604 8a35f56c 2022-07-16 thomas case '"':
605 8a35f56c 2022-07-16 thomas quotec = c;
606 8a35f56c 2022-07-16 thomas while (1) {
607 8a35f56c 2022-07-16 thomas c = lgetc(quotec);
608 8a35f56c 2022-07-16 thomas if (c == EOF)
609 8a35f56c 2022-07-16 thomas return (0);
610 8a35f56c 2022-07-16 thomas if (c == '\n') {
611 8a35f56c 2022-07-16 thomas file->lineno++;
612 8a35f56c 2022-07-16 thomas continue;
613 8a35f56c 2022-07-16 thomas } else if (c == '\\') {
614 8a35f56c 2022-07-16 thomas next = lgetc(quotec);
615 8a35f56c 2022-07-16 thomas if (next == EOF)
616 8a35f56c 2022-07-16 thomas return (0);
617 8a35f56c 2022-07-16 thomas if (next == quotec || c == ' ' || c == '\t')
618 8a35f56c 2022-07-16 thomas c = next;
619 8a35f56c 2022-07-16 thomas else if (next == '\n') {
620 8a35f56c 2022-07-16 thomas file->lineno++;
621 8a35f56c 2022-07-16 thomas continue;
623 8a35f56c 2022-07-16 thomas lungetc(next);
624 8a35f56c 2022-07-16 thomas } else if (c == quotec) {
625 8a35f56c 2022-07-16 thomas *p = '\0';
627 8a35f56c 2022-07-16 thomas } else if (c == '\0') {
628 8a35f56c 2022-07-16 thomas yyerror("syntax error");
629 8a35f56c 2022-07-16 thomas return (findeol());
631 8a35f56c 2022-07-16 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
632 8a35f56c 2022-07-16 thomas yyerror("string too long");
633 8a35f56c 2022-07-16 thomas return (findeol());
635 8a35f56c 2022-07-16 thomas *p++ = c;
637 8a35f56c 2022-07-16 thomas yylval.v.string = strdup(buf);
638 8a35f56c 2022-07-16 thomas if (yylval.v.string == NULL)
639 8a35f56c 2022-07-16 thomas err(1, "yylex: strdup");
640 8a35f56c 2022-07-16 thomas return (STRING);
643 8a35f56c 2022-07-16 thomas #define allowed_to_end_number(x) \
644 8a35f56c 2022-07-16 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
646 8a35f56c 2022-07-16 thomas if (c == '-' || isdigit(c)) {
648 8a35f56c 2022-07-16 thomas *p++ = c;
649 8a35f56c 2022-07-16 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
650 8a35f56c 2022-07-16 thomas yyerror("string too long");
651 8a35f56c 2022-07-16 thomas return (findeol());
653 8a35f56c 2022-07-16 thomas c = lgetc(0);
654 8a35f56c 2022-07-16 thomas } while (c != EOF && isdigit(c));
655 8a35f56c 2022-07-16 thomas lungetc(c);
656 8a35f56c 2022-07-16 thomas if (p == buf + 1 && buf[0] == '-')
657 8a35f56c 2022-07-16 thomas goto nodigits;
658 8a35f56c 2022-07-16 thomas if (c == EOF || allowed_to_end_number(c)) {
659 8a35f56c 2022-07-16 thomas const char *errstr = NULL;
661 8a35f56c 2022-07-16 thomas *p = '\0';
662 8a35f56c 2022-07-16 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
663 8a35f56c 2022-07-16 thomas LLONG_MAX, &errstr);
664 8a35f56c 2022-07-16 thomas if (errstr) {
665 8a35f56c 2022-07-16 thomas yyerror("\"%s\" invalid number: %s",
666 8a35f56c 2022-07-16 thomas buf, errstr);
667 8a35f56c 2022-07-16 thomas return (findeol());
669 8a35f56c 2022-07-16 thomas return (NUMBER);
671 8a35f56c 2022-07-16 thomas nodigits:
672 8a35f56c 2022-07-16 thomas while (p > buf + 1)
673 8a35f56c 2022-07-16 thomas lungetc(*--p);
674 8a35f56c 2022-07-16 thomas c = *--p;
675 8a35f56c 2022-07-16 thomas if (c == '-')
676 8a35f56c 2022-07-16 thomas return (c);
680 8a35f56c 2022-07-16 thomas #define allowed_in_string(x) \
681 8a35f56c 2022-07-16 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
682 8a35f56c 2022-07-16 thomas x != '{' && x != '}' && \
683 8a35f56c 2022-07-16 thomas x != '!' && x != '=' && x != '#' && \
684 8a35f56c 2022-07-16 thomas x != ','))
686 8a35f56c 2022-07-16 thomas if (isalnum(c) || c == ':' || c == '_') {
688 8a35f56c 2022-07-16 thomas *p++ = c;
689 8a35f56c 2022-07-16 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
690 8a35f56c 2022-07-16 thomas yyerror("string too long");
691 8a35f56c 2022-07-16 thomas return (findeol());
693 8a35f56c 2022-07-16 thomas c = lgetc(0);
694 8a35f56c 2022-07-16 thomas } while (c != EOF && (allowed_in_string(c)));
695 8a35f56c 2022-07-16 thomas lungetc(c);
696 8a35f56c 2022-07-16 thomas *p = '\0';
697 8a35f56c 2022-07-16 thomas token = lookup(buf);
698 8a35f56c 2022-07-16 thomas if (token == STRING) {
699 8a35f56c 2022-07-16 thomas yylval.v.string = strdup(buf);
700 8a35f56c 2022-07-16 thomas if (yylval.v.string == NULL)
701 8a35f56c 2022-07-16 thomas err(1, "yylex: strdup");
703 8a35f56c 2022-07-16 thomas return (token);
705 8a35f56c 2022-07-16 thomas if (c == '\n') {
706 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
707 8a35f56c 2022-07-16 thomas file->lineno++;
709 8a35f56c 2022-07-16 thomas if (c == EOF)
710 8a35f56c 2022-07-16 thomas return (0);
711 8a35f56c 2022-07-16 thomas return (c);
715 8a35f56c 2022-07-16 thomas check_file_secrecy(int fd, const char *fname)
717 8a35f56c 2022-07-16 thomas struct stat st;
719 8a35f56c 2022-07-16 thomas if (fstat(fd, &st)) {
720 8a35f56c 2022-07-16 thomas log_warn("cannot stat %s", fname);
721 8a35f56c 2022-07-16 thomas return (-1);
723 8a35f56c 2022-07-16 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
724 8a35f56c 2022-07-16 thomas log_warnx("%s: owner not root or current user", fname);
725 8a35f56c 2022-07-16 thomas return (-1);
727 8a35f56c 2022-07-16 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
728 8a35f56c 2022-07-16 thomas log_warnx("%s: group writable or world read/writable", fname);
729 8a35f56c 2022-07-16 thomas return (-1);
731 8a35f56c 2022-07-16 thomas return (0);
734 8a35f56c 2022-07-16 thomas struct file *
735 8a35f56c 2022-07-16 thomas newfile(const char *name, int secret)
737 8a35f56c 2022-07-16 thomas struct file *nfile;
739 8a35f56c 2022-07-16 thomas nfile = calloc(1, sizeof(struct file));
740 8a35f56c 2022-07-16 thomas if (nfile == NULL) {
741 8a35f56c 2022-07-16 thomas log_warn("calloc");
742 8a35f56c 2022-07-16 thomas return (NULL);
744 8a35f56c 2022-07-16 thomas nfile->name = strdup(name);
745 8a35f56c 2022-07-16 thomas if (nfile->name == NULL) {
746 8a35f56c 2022-07-16 thomas log_warn("strdup");
747 8a35f56c 2022-07-16 thomas free(nfile);
748 8a35f56c 2022-07-16 thomas return (NULL);
750 8a35f56c 2022-07-16 thomas nfile->stream = fopen(nfile->name, "r");
751 8a35f56c 2022-07-16 thomas if (nfile->stream == NULL) {
752 8a35f56c 2022-07-16 thomas /* no warning, we don't require a conf file */
753 8a35f56c 2022-07-16 thomas free(nfile->name);
754 8a35f56c 2022-07-16 thomas free(nfile);
755 8a35f56c 2022-07-16 thomas return (NULL);
756 8a35f56c 2022-07-16 thomas } else if (secret &&
757 8a35f56c 2022-07-16 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
758 8a35f56c 2022-07-16 thomas fclose(nfile->stream);
759 8a35f56c 2022-07-16 thomas free(nfile->name);
760 8a35f56c 2022-07-16 thomas free(nfile);
761 8a35f56c 2022-07-16 thomas return (NULL);
763 8a35f56c 2022-07-16 thomas nfile->lineno = 1;
764 8a35f56c 2022-07-16 thomas return (nfile);
767 8a35f56c 2022-07-16 thomas static void
768 8a35f56c 2022-07-16 thomas closefile(struct file *xfile)
770 8a35f56c 2022-07-16 thomas fclose(xfile->stream);
771 8a35f56c 2022-07-16 thomas free(xfile->name);
772 8a35f56c 2022-07-16 thomas free(xfile);
775 9f849004 2022-08-06 thomas static void
776 9f849004 2022-08-06 thomas add_default_server(void)
778 9f849004 2022-08-06 thomas new_srv = conf_new_server(D_SITENAME);
779 9f849004 2022-08-06 thomas log_debug("%s: adding default server %s", __func__, D_SITENAME);
783 8a35f56c 2022-07-16 thomas parse_config(const char *filename, struct gotwebd *env)
785 8a35f56c 2022-07-16 thomas struct sym *sym, *next;
787 8a35f56c 2022-07-16 thomas if (config_init(env) == -1)
788 8a35f56c 2022-07-16 thomas fatalx("failed to initialize configuration");
790 8a35f56c 2022-07-16 thomas gotwebd = env;
792 9f849004 2022-08-06 thomas file = newfile(filename, 0);
793 9f849004 2022-08-06 thomas if (file == NULL) {
794 9f849004 2022-08-06 thomas add_default_server();
795 9f849004 2022-08-06 thomas sockets_parse_sockets(env);
796 9f849004 2022-08-06 thomas /* just return, as we don't require a conf file */
797 9f849004 2022-08-06 thomas return (0);
800 8a35f56c 2022-07-16 thomas yyparse();
801 8a35f56c 2022-07-16 thomas errors = file->errors;
802 8a35f56c 2022-07-16 thomas closefile(file);
804 8a35f56c 2022-07-16 thomas /* Free macros and check which have not been used. */
805 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
806 8a35f56c 2022-07-16 thomas if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
807 8a35f56c 2022-07-16 thomas fprintf(stderr, "warning: macro '%s' not used\n",
808 8a35f56c 2022-07-16 thomas sym->nam);
809 8a35f56c 2022-07-16 thomas if (!sym->persist) {
810 8a35f56c 2022-07-16 thomas free(sym->nam);
811 8a35f56c 2022-07-16 thomas free(sym->val);
812 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&symhead, sym, entry);
813 8a35f56c 2022-07-16 thomas free(sym);
817 8a35f56c 2022-07-16 thomas if (errors)
818 8a35f56c 2022-07-16 thomas return (-1);
820 8a35f56c 2022-07-16 thomas /* just add default server if no config specified */
821 9f849004 2022-08-06 thomas if (gotwebd->server_cnt == 0)
822 9f849004 2022-08-06 thomas add_default_server();
824 8a35f56c 2022-07-16 thomas /* setup our listening sockets */
825 8a35f56c 2022-07-16 thomas sockets_parse_sockets(env);
827 8a35f56c 2022-07-16 thomas return (0);
830 8a35f56c 2022-07-16 thomas struct server *
831 8a35f56c 2022-07-16 thomas conf_new_server(const char *name)
833 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
835 8a35f56c 2022-07-16 thomas srv = calloc(1, sizeof(*srv));
836 8a35f56c 2022-07-16 thomas if (srv == NULL)
837 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
839 8a35f56c 2022-07-16 thomas n = strlcpy(srv->name, name, sizeof(srv->name));
840 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->name))
841 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
842 8a35f56c 2022-07-16 thomas n = snprintf(srv->unix_socket_name,
843 8a35f56c 2022-07-16 thomas sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
844 8a35f56c 2022-07-16 thomas D_UNIX_SOCKET);
845 717a78d4 2022-08-16 thomas if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
846 8a35f56c 2022-07-16 thomas fatalx("%s: snprintf", __func__);
847 8a35f56c 2022-07-16 thomas n = strlcpy(srv->repos_path, D_GOTPATH,
848 8a35f56c 2022-07-16 thomas sizeof(srv->repos_path));
849 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->repos_path))
850 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
851 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_name, D_SITENAME,
852 8a35f56c 2022-07-16 thomas sizeof(srv->site_name));
853 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_name))
854 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
855 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_owner, D_SITEOWNER,
856 8a35f56c 2022-07-16 thomas sizeof(srv->site_owner));
857 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_owner))
858 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
859 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_link, D_SITELINK,
860 8a35f56c 2022-07-16 thomas sizeof(srv->site_link));
861 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_link))
862 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
863 8a35f56c 2022-07-16 thomas n = strlcpy(srv->logo, D_GOTLOGO,
864 8a35f56c 2022-07-16 thomas sizeof(srv->logo));
865 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->logo))
866 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
867 8a35f56c 2022-07-16 thomas n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
868 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->logo_url))
869 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
870 8a35f56c 2022-07-16 thomas n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
871 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->custom_css))
872 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
874 8a35f56c 2022-07-16 thomas srv->show_site_owner = D_SHOWSOWNER;
875 8a35f56c 2022-07-16 thomas srv->show_repo_owner = D_SHOWROWNER;
876 8a35f56c 2022-07-16 thomas srv->show_repo_age = D_SHOWAGE;
877 8a35f56c 2022-07-16 thomas srv->show_repo_description = D_SHOWDESC;
878 8a35f56c 2022-07-16 thomas srv->show_repo_cloneurl = D_SHOWURL;
880 8a35f56c 2022-07-16 thomas srv->max_repos_display = D_MAXREPODISP;
881 8a35f56c 2022-07-16 thomas srv->max_commits_display = D_MAXCOMMITDISP;
882 8a35f56c 2022-07-16 thomas srv->max_repos = D_MAXREPO;
884 8a35f56c 2022-07-16 thomas srv->unix_socket = 1;
885 e4317279 2022-08-30 thomas srv->fcgi_socket = 0;
887 62f85214 2022-08-16 thomas TAILQ_INIT(&srv->al);
888 90d63d47 2022-08-16 thomas TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
889 8a35f56c 2022-07-16 thomas gotwebd->server_cnt++;
891 8a35f56c 2022-07-16 thomas return srv;
895 8a35f56c 2022-07-16 thomas symset(const char *nam, const char *val, int persist)
897 8a35f56c 2022-07-16 thomas struct sym *sym;
899 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sym, &symhead, entry) {
900 8a35f56c 2022-07-16 thomas if (strcmp(nam, sym->nam) == 0)
904 8a35f56c 2022-07-16 thomas if (sym != NULL) {
905 8a35f56c 2022-07-16 thomas if (sym->persist == 1)
906 8a35f56c 2022-07-16 thomas return (0);
908 8a35f56c 2022-07-16 thomas free(sym->nam);
909 8a35f56c 2022-07-16 thomas free(sym->val);
910 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&symhead, sym, entry);
911 8a35f56c 2022-07-16 thomas free(sym);
914 8a35f56c 2022-07-16 thomas sym = calloc(1, sizeof(*sym));
915 8a35f56c 2022-07-16 thomas if (sym == NULL)
916 8a35f56c 2022-07-16 thomas return (-1);
918 8a35f56c 2022-07-16 thomas sym->nam = strdup(nam);
919 8a35f56c 2022-07-16 thomas if (sym->nam == NULL) {
920 8a35f56c 2022-07-16 thomas free(sym);
921 8a35f56c 2022-07-16 thomas return (-1);
923 8a35f56c 2022-07-16 thomas sym->val = strdup(val);
924 8a35f56c 2022-07-16 thomas if (sym->val == NULL) {
925 8a35f56c 2022-07-16 thomas free(sym->nam);
926 8a35f56c 2022-07-16 thomas free(sym);
927 8a35f56c 2022-07-16 thomas return (-1);
929 8a35f56c 2022-07-16 thomas sym->used = 0;
930 8a35f56c 2022-07-16 thomas sym->persist = persist;
931 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
932 8a35f56c 2022-07-16 thomas return (0);
936 8a35f56c 2022-07-16 thomas cmdline_symset(char *s)
938 8a35f56c 2022-07-16 thomas char *sym, *val;
941 8a35f56c 2022-07-16 thomas val = strrchr(s, '=');
942 8a35f56c 2022-07-16 thomas if (val == NULL)
943 8a35f56c 2022-07-16 thomas return (-1);
945 43be1edb 2022-09-05 thomas sym = strndup(s, val - s);
946 8a35f56c 2022-07-16 thomas if (sym == NULL)
947 43be1edb 2022-09-05 thomas fatal("%s: strndup", __func__);
949 8a35f56c 2022-07-16 thomas ret = symset(sym, val + 1, 1);
950 8a35f56c 2022-07-16 thomas free(sym);
952 8a35f56c 2022-07-16 thomas return (ret);
956 8a35f56c 2022-07-16 thomas symget(const char *nam)
958 8a35f56c 2022-07-16 thomas struct sym *sym;
960 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sym, &symhead, entry) {
961 8a35f56c 2022-07-16 thomas if (strcmp(nam, sym->nam) == 0) {
962 8a35f56c 2022-07-16 thomas sym->used = 1;
963 8a35f56c 2022-07-16 thomas return (sym->val);
966 8a35f56c 2022-07-16 thomas return (NULL);
970 8a35f56c 2022-07-16 thomas getservice(const char *n)
972 8a35f56c 2022-07-16 thomas struct servent *s;
973 8a35f56c 2022-07-16 thomas const char *errstr;
974 8a35f56c 2022-07-16 thomas long long llval;
976 8a35f56c 2022-07-16 thomas llval = strtonum(n, 0, UINT16_MAX, &errstr);
977 8a35f56c 2022-07-16 thomas if (errstr) {
978 8a35f56c 2022-07-16 thomas s = getservbyname(n, "tcp");
979 8a35f56c 2022-07-16 thomas if (s == NULL)
980 8a35f56c 2022-07-16 thomas s = getservbyname(n, "udp");
981 8a35f56c 2022-07-16 thomas if (s == NULL)
982 8a35f56c 2022-07-16 thomas return (-1);
983 c19738c9 2022-08-27 thomas return ntohs(s->s_port);
986 c19738c9 2022-08-27 thomas return (unsigned short)llval;
989 8a35f56c 2022-07-16 thomas struct address *
990 8a35f56c 2022-07-16 thomas host_v4(const char *s)
992 8a35f56c 2022-07-16 thomas struct in_addr ina;
993 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
994 8a35f56c 2022-07-16 thomas struct address *h;
996 8a35f56c 2022-07-16 thomas memset(&ina, 0, sizeof(ina));
997 8a35f56c 2022-07-16 thomas if (inet_pton(AF_INET, s, &ina) != 1)
998 8a35f56c 2022-07-16 thomas return (NULL);
1000 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1001 8a35f56c 2022-07-16 thomas fatal(__func__);
1002 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1003 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet_init(sain, &ina);
1004 8a35f56c 2022-07-16 thomas if (sain->sin_addr.s_addr == INADDR_ANY)
1005 8a35f56c 2022-07-16 thomas h->prefixlen = 0; /* 0.0.0.0 address */
1007 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1008 8a35f56c 2022-07-16 thomas return (h);
1011 8a35f56c 2022-07-16 thomas struct address *
1012 8a35f56c 2022-07-16 thomas host_v6(const char *s)
1014 8a35f56c 2022-07-16 thomas struct addrinfo hints, *res;
1015 cdbe1d7d 2022-08-06 thomas struct sockaddr_in6 *sa_in6, *ra;
1016 8a35f56c 2022-07-16 thomas struct address *h = NULL;
1018 8a35f56c 2022-07-16 thomas memset(&hints, 0, sizeof(hints));
1019 8a35f56c 2022-07-16 thomas hints.ai_family = AF_INET6;
1020 8a35f56c 2022-07-16 thomas hints.ai_socktype = SOCK_DGRAM; /* dummy */
1021 8a35f56c 2022-07-16 thomas hints.ai_flags = AI_NUMERICHOST;
1022 8a35f56c 2022-07-16 thomas if (getaddrinfo(s, "0", &hints, &res) == 0) {
1023 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1024 8a35f56c 2022-07-16 thomas fatal(__func__);
1025 8a35f56c 2022-07-16 thomas sa_in6 = (struct sockaddr_in6 *)&h->ss;
1026 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in6 *)res->ai_addr;
1027 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1028 cdbe1d7d 2022-08-06 thomas ra->sin6_scope_id);
1029 8a35f56c 2022-07-16 thomas if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1030 8a35f56c 2022-07-16 thomas sizeof(sa_in6->sin6_addr)) == 0)
1031 8a35f56c 2022-07-16 thomas h->prefixlen = 0; /* any address */
1033 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1034 8a35f56c 2022-07-16 thomas freeaddrinfo(res);
1037 8a35f56c 2022-07-16 thomas return (h);
1041 e4c7e0b0 2022-08-30 thomas host_dns(const char *s, struct server *new_srv, int max,
1042 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1044 8a35f56c 2022-07-16 thomas struct addrinfo hints, *res0, *res;
1045 8a35f56c 2022-07-16 thomas int error, cnt = 0;
1046 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
1047 8a35f56c 2022-07-16 thomas struct sockaddr_in6 *sin6;
1048 8a35f56c 2022-07-16 thomas struct address *h;
1050 e4c7e0b0 2022-08-30 thomas if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1051 8a35f56c 2022-07-16 thomas return (cnt);
1053 8a35f56c 2022-07-16 thomas memset(&hints, 0, sizeof(hints));
1054 8a35f56c 2022-07-16 thomas hints.ai_family = PF_UNSPEC;
1055 8a35f56c 2022-07-16 thomas hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1056 8a35f56c 2022-07-16 thomas hints.ai_flags = AI_ADDRCONFIG;
1057 8a35f56c 2022-07-16 thomas error = getaddrinfo(s, NULL, &hints, &res0);
1058 8a35f56c 2022-07-16 thomas if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1059 8a35f56c 2022-07-16 thomas return (0);
1060 8a35f56c 2022-07-16 thomas if (error) {
1061 8a35f56c 2022-07-16 thomas log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1062 8a35f56c 2022-07-16 thomas gai_strerror(error));
1063 8a35f56c 2022-07-16 thomas return (-1);
1066 8a35f56c 2022-07-16 thomas for (res = res0; res && cnt < max; res = res->ai_next) {
1067 8a35f56c 2022-07-16 thomas if (res->ai_family != AF_INET &&
1068 8a35f56c 2022-07-16 thomas res->ai_family != AF_INET6)
1069 8a35f56c 2022-07-16 thomas continue;
1070 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1071 8a35f56c 2022-07-16 thomas fatal(__func__);
1073 8a35f56c 2022-07-16 thomas if (port)
1074 8a35f56c 2022-07-16 thomas h->port = port;
1075 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1076 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1077 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1078 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1079 8a35f56c 2022-07-16 thomas __func__);
1080 8a35f56c 2022-07-16 thomas freeaddrinfo(res0);
1081 8a35f56c 2022-07-16 thomas free(h);
1082 8a35f56c 2022-07-16 thomas return (-1);
1085 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1086 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1087 8a35f56c 2022-07-16 thomas h->ss.ss_family = res->ai_family;
1088 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1090 8a35f56c 2022-07-16 thomas if (res->ai_family == AF_INET) {
1091 cdbe1d7d 2022-08-06 thomas struct sockaddr_in *ra;
1092 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1093 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in *)res->ai_addr;
1094 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet_init(sain, &ra->sin_addr);
1095 8a35f56c 2022-07-16 thomas } else {
1096 cdbe1d7d 2022-08-06 thomas struct sockaddr_in6 *ra;
1097 8a35f56c 2022-07-16 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1098 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in6 *)res->ai_addr;
1099 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1102 e4c7e0b0 2022-08-30 thomas if (add_addr(new_srv, h))
1103 e4c7e0b0 2022-08-30 thomas return -1;
1106 8a35f56c 2022-07-16 thomas if (cnt == max && res) {
1107 8a35f56c 2022-07-16 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1108 8a35f56c 2022-07-16 thomas s, max);
1110 8a35f56c 2022-07-16 thomas freeaddrinfo(res0);
1111 8a35f56c 2022-07-16 thomas return (cnt);
1115 e4c7e0b0 2022-08-30 thomas host_if(const char *s, struct server *new_srv, int max,
1116 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1118 8a35f56c 2022-07-16 thomas struct ifaddrs *ifap, *p;
1119 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
1120 8a35f56c 2022-07-16 thomas struct sockaddr_in6 *sin6;
1121 8a35f56c 2022-07-16 thomas struct address *h;
1122 8a35f56c 2022-07-16 thomas int cnt = 0, af;
1124 8a35f56c 2022-07-16 thomas if (getifaddrs(&ifap) == -1)
1125 8a35f56c 2022-07-16 thomas fatal("getifaddrs");
1127 8a35f56c 2022-07-16 thomas /* First search for IPv4 addresses */
1128 8a35f56c 2022-07-16 thomas af = AF_INET;
1131 8a35f56c 2022-07-16 thomas for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1132 8a35f56c 2022-07-16 thomas if (p->ifa_addr == NULL ||
1133 8a35f56c 2022-07-16 thomas p->ifa_addr->sa_family != af ||
1134 8a35f56c 2022-07-16 thomas (strcmp(s, p->ifa_name) != 0 &&
1135 8a35f56c 2022-07-16 thomas !is_if_in_group(p->ifa_name, s)))
1136 8a35f56c 2022-07-16 thomas continue;
1137 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1138 8a35f56c 2022-07-16 thomas fatal("calloc");
1140 8a35f56c 2022-07-16 thomas if (port)
1141 8a35f56c 2022-07-16 thomas h->port = port;
1142 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1143 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1144 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1145 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1146 8a35f56c 2022-07-16 thomas __func__);
1147 8a35f56c 2022-07-16 thomas free(h);
1148 8a35f56c 2022-07-16 thomas freeifaddrs(ifap);
1149 8a35f56c 2022-07-16 thomas return (-1);
1152 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1153 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1154 8a35f56c 2022-07-16 thomas h->ss.ss_family = af;
1155 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1157 8a35f56c 2022-07-16 thomas if (af == AF_INET) {
1158 cdbe1d7d 2022-08-06 thomas struct sockaddr_in *ra;
1159 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1160 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in *)p->ifa_addr;
1161 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet_init(sain, &ra->sin_addr);
1162 8a35f56c 2022-07-16 thomas } else {
1163 cdbe1d7d 2022-08-06 thomas struct sockaddr_in6 *ra;
1164 8a35f56c 2022-07-16 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1165 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in6 *)p->ifa_addr;
1166 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1167 cdbe1d7d 2022-08-06 thomas ra->sin6_scope_id);
1170 e4c7e0b0 2022-08-30 thomas if (add_addr(new_srv, h))
1171 e4c7e0b0 2022-08-30 thomas return -1;
1174 8a35f56c 2022-07-16 thomas if (af == AF_INET) {
1175 8a35f56c 2022-07-16 thomas /* Next search for IPv6 addresses */
1176 8a35f56c 2022-07-16 thomas af = AF_INET6;
1177 8a35f56c 2022-07-16 thomas goto nextaf;
1180 8a35f56c 2022-07-16 thomas if (cnt > max) {
1181 8a35f56c 2022-07-16 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1182 8a35f56c 2022-07-16 thomas s, max);
1184 8a35f56c 2022-07-16 thomas freeifaddrs(ifap);
1185 8a35f56c 2022-07-16 thomas return (cnt);
1189 e4c7e0b0 2022-08-30 thomas host(const char *s, struct server *new_srv, int max,
1190 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1192 8a35f56c 2022-07-16 thomas struct address *h;
1194 8a35f56c 2022-07-16 thomas h = host_v4(s);
1196 8a35f56c 2022-07-16 thomas /* IPv6 address? */
1197 8a35f56c 2022-07-16 thomas if (h == NULL)
1198 8a35f56c 2022-07-16 thomas h = host_v6(s);
1200 8a35f56c 2022-07-16 thomas if (h != NULL) {
1201 8a35f56c 2022-07-16 thomas if (port)
1202 8a35f56c 2022-07-16 thomas h->port = port;
1203 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1204 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1205 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1206 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1207 8a35f56c 2022-07-16 thomas __func__);
1208 8a35f56c 2022-07-16 thomas free(h);
1209 8a35f56c 2022-07-16 thomas return (-1);
1212 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1213 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1215 e4c7e0b0 2022-08-30 thomas if (add_addr(new_srv, h))
1216 e4c7e0b0 2022-08-30 thomas return -1;
1217 8a35f56c 2022-07-16 thomas return (1);
1220 e4c7e0b0 2022-08-30 thomas return (host_dns(s, new_srv, max, port, ifname, ipproto));
1224 8a35f56c 2022-07-16 thomas is_if_in_group(const char *ifname, const char *groupname)
1226 ff36aeea 2022-07-16 thomas /* TA: Check this... */
1227 ff36aeea 2022-07-16 thomas #ifdef HAVE_STRUCT_IFGROUPREQ
1228 8a35f56c 2022-07-16 thomas unsigned int len;
1229 8a35f56c 2022-07-16 thomas struct ifgroupreq ifgr;
1230 8a35f56c 2022-07-16 thomas struct ifg_req *ifg;
1232 8a35f56c 2022-07-16 thomas int ret = 0;
1234 8a35f56c 2022-07-16 thomas if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1235 8a35f56c 2022-07-16 thomas err(1, "socket");
1237 8a35f56c 2022-07-16 thomas memset(&ifgr, 0, sizeof(ifgr));
1238 8a35f56c 2022-07-16 thomas if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1239 8a35f56c 2022-07-16 thomas err(1, "IFNAMSIZ");
1240 8a35f56c 2022-07-16 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1241 8a35f56c 2022-07-16 thomas if (errno == EINVAL || errno == ENOTTY)
1242 8a35f56c 2022-07-16 thomas goto end;
1243 8a35f56c 2022-07-16 thomas err(1, "SIOCGIFGROUP");
1246 8a35f56c 2022-07-16 thomas len = ifgr.ifgr_len;
1247 8a35f56c 2022-07-16 thomas ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1248 8a35f56c 2022-07-16 thomas sizeof(struct ifg_req));
1249 8a35f56c 2022-07-16 thomas if (ifgr.ifgr_groups == NULL)
1250 8a35f56c 2022-07-16 thomas err(1, "getifgroups");
1251 8a35f56c 2022-07-16 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1252 8a35f56c 2022-07-16 thomas err(1, "SIOCGIFGROUP");
1254 8a35f56c 2022-07-16 thomas ifg = ifgr.ifgr_groups;
1255 8a35f56c 2022-07-16 thomas for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1256 8a35f56c 2022-07-16 thomas len -= sizeof(struct ifg_req);
1257 8a35f56c 2022-07-16 thomas if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1258 8a35f56c 2022-07-16 thomas ret = 1;
1262 8a35f56c 2022-07-16 thomas free(ifgr.ifgr_groups);
1265 8a35f56c 2022-07-16 thomas close(s);
1266 8a35f56c 2022-07-16 thomas return (ret);
1268 ff36aeea 2022-07-16 thomas return (0);
1273 e4c7e0b0 2022-08-30 thomas get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1275 8a35f56c 2022-07-16 thomas if (strcmp("", addr) == 0) {
1276 e4c7e0b0 2022-08-30 thomas if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1277 e4c7e0b0 2022-08-30 thomas -1) <= 0) {
1278 8a35f56c 2022-07-16 thomas yyerror("invalid listen ip: %s",
1279 a90e3117 2022-08-27 thomas "127.0.0.1");
1280 8a35f56c 2022-07-16 thomas return (-1);
1282 e4c7e0b0 2022-08-30 thomas if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1283 a90e3117 2022-08-27 thomas yyerror("invalid listen ip: %s", "::1");
1284 8a35f56c 2022-07-16 thomas return (-1);
1286 8a35f56c 2022-07-16 thomas } else {
1287 e4c7e0b0 2022-08-30 thomas if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1288 8a35f56c 2022-07-16 thomas -1) <= 0) {
1289 8a35f56c 2022-07-16 thomas yyerror("invalid listen ip: %s", addr);
1290 8a35f56c 2022-07-16 thomas return (-1);
1293 8a35f56c 2022-07-16 thomas return (0);
1297 e4c7e0b0 2022-08-30 thomas addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1298 e4c7e0b0 2022-08-30 thomas const char *other_srv)
1300 e4c7e0b0 2022-08-30 thomas struct address *a;
1301 e4c7e0b0 2022-08-30 thomas void *ia;
1302 e4c7e0b0 2022-08-30 thomas char buf[INET6_ADDRSTRLEN];
1303 e4c7e0b0 2022-08-30 thomas const char *addrstr;
1305 e4c7e0b0 2022-08-30 thomas TAILQ_FOREACH(a, al, entry) {
1306 e4c7e0b0 2022-08-30 thomas if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1307 e4c7e0b0 2022-08-30 thomas a->port != h->port)
1308 e4c7e0b0 2022-08-30 thomas continue;
1310 e4c7e0b0 2022-08-30 thomas switch (h->ss.ss_family) {
1311 e4c7e0b0 2022-08-30 thomas case AF_INET:
1312 e4c7e0b0 2022-08-30 thomas ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1314 e4c7e0b0 2022-08-30 thomas case AF_INET6:
1315 e4c7e0b0 2022-08-30 thomas ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1317 e4c7e0b0 2022-08-30 thomas default:
1318 e4c7e0b0 2022-08-30 thomas yyerror("unknown address family: %d", h->ss.ss_family);
1319 e4c7e0b0 2022-08-30 thomas return -1;
1321 e4c7e0b0 2022-08-30 thomas addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1322 e4c7e0b0 2022-08-30 thomas if (addrstr) {
1323 e4c7e0b0 2022-08-30 thomas if (other_srv) {
1324 e4c7e0b0 2022-08-30 thomas yyerror("server %s: duplicate fcgi listen "
1325 e4c7e0b0 2022-08-30 thomas "address %s:%d, already used by server %s",
1326 e4c7e0b0 2022-08-30 thomas new_srv, addrstr, h->port, other_srv);
1327 e4c7e0b0 2022-08-30 thomas } else {
1328 e4c7e0b0 2022-08-30 thomas log_warnx("server: %s: duplicate fcgi listen "
1329 e4c7e0b0 2022-08-30 thomas "address %s:%d", new_srv, addrstr, h->port);
1331 e4c7e0b0 2022-08-30 thomas } else {
1332 e4c7e0b0 2022-08-30 thomas if (other_srv) {
1333 e4c7e0b0 2022-08-30 thomas yyerror("server: %s: duplicate fcgi listen "
1334 e4c7e0b0 2022-08-30 thomas "address, already used by server %s",
1335 e4c7e0b0 2022-08-30 thomas new_srv, other_srv);
1336 e4c7e0b0 2022-08-30 thomas } else {
1337 e4c7e0b0 2022-08-30 thomas log_warnx("server %s: duplicate fcgi listen "
1338 e4c7e0b0 2022-08-30 thomas "address", new_srv);
1342 e4c7e0b0 2022-08-30 thomas return -1;
1345 e4c7e0b0 2022-08-30 thomas return 0;
1349 e4c7e0b0 2022-08-30 thomas add_addr(struct server *new_srv, struct address *h)
1351 e4c7e0b0 2022-08-30 thomas struct server *srv;
1353 e4c7e0b0 2022-08-30 thomas /* Address cannot be shared between different servers. */
1354 e4c7e0b0 2022-08-30 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1355 e4c7e0b0 2022-08-30 thomas if (srv == new_srv)
1356 e4c7e0b0 2022-08-30 thomas continue;
1357 e4c7e0b0 2022-08-30 thomas if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1358 e4c7e0b0 2022-08-30 thomas return -1;
1361 e4c7e0b0 2022-08-30 thomas /* Tolerate duplicate address lines within the scope of a server. */
1362 e4c7e0b0 2022-08-30 thomas if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1363 e4c7e0b0 2022-08-30 thomas TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1365 e4c7e0b0 2022-08-30 thomas free(h);
1367 e4c7e0b0 2022-08-30 thomas return 0;