2 56324ebe 2022-07-14 thomas * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 56324ebe 2022-07-14 thomas * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 56324ebe 2022-07-14 thomas * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 56324ebe 2022-07-14 thomas * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 56324ebe 2022-07-14 thomas * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 56324ebe 2022-07-14 thomas * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 56324ebe 2022-07-14 thomas * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 56324ebe 2022-07-14 thomas * Permission to use, copy, modify, and distribute this software for any
11 56324ebe 2022-07-14 thomas * purpose with or without fee is hereby granted, provided that the above
12 56324ebe 2022-07-14 thomas * copyright notice and this permission notice appear in all copies.
14 56324ebe 2022-07-14 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 56324ebe 2022-07-14 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 56324ebe 2022-07-14 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 56324ebe 2022-07-14 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 56324ebe 2022-07-14 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 56324ebe 2022-07-14 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 56324ebe 2022-07-14 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 56324ebe 2022-07-14 thomas #include <sys/ioctl.h>
25 56324ebe 2022-07-14 thomas #include <sys/types.h>
26 56324ebe 2022-07-14 thomas #include <sys/socket.h>
27 56324ebe 2022-07-14 thomas #include <sys/stat.h>
29 56324ebe 2022-07-14 thomas #include <net/if.h>
30 56324ebe 2022-07-14 thomas #include <netinet/in.h>
32 56324ebe 2022-07-14 thomas #include <arpa/inet.h>
34 56324ebe 2022-07-14 thomas #include <ctype.h>
35 56324ebe 2022-07-14 thomas #include <err.h>
36 56324ebe 2022-07-14 thomas #include <errno.h>
37 56324ebe 2022-07-14 thomas #include <event.h>
38 56324ebe 2022-07-14 thomas #include <ifaddrs.h>
39 56324ebe 2022-07-14 thomas #include <limits.h>
40 56324ebe 2022-07-14 thomas #include <netdb.h>
41 56324ebe 2022-07-14 thomas #include <stdarg.h>
42 56324ebe 2022-07-14 thomas #include <stdlib.h>
43 56324ebe 2022-07-14 thomas #include <stdio.h>
44 56324ebe 2022-07-14 thomas #include <string.h>
45 56324ebe 2022-07-14 thomas #include <syslog.h>
46 56324ebe 2022-07-14 thomas #include <tls.h>
47 56324ebe 2022-07-14 thomas #include <unistd.h>
49 56324ebe 2022-07-14 thomas #include "proc.h"
50 56324ebe 2022-07-14 thomas #include "gotwebd.h"
51 d7cad54e 2022-07-14 thomas #include "got_compat.h"
53 56324ebe 2022-07-14 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
54 56324ebe 2022-07-14 thomas static struct file {
55 56324ebe 2022-07-14 thomas TAILQ_ENTRY(file) entry;
56 56324ebe 2022-07-14 thomas FILE *stream;
57 56324ebe 2022-07-14 thomas char *name;
58 56324ebe 2022-07-14 thomas int lineno;
59 56324ebe 2022-07-14 thomas int errors;
61 56324ebe 2022-07-14 thomas struct file *newfile(const char *, int);
62 56324ebe 2022-07-14 thomas static void closefile(struct file *);
63 56324ebe 2022-07-14 thomas int check_file_secrecy(int, const char *);
64 56324ebe 2022-07-14 thomas int yyparse(void);
65 56324ebe 2022-07-14 thomas int yylex(void);
66 56324ebe 2022-07-14 thomas int yyerror(const char *, ...)
67 56324ebe 2022-07-14 thomas __attribute__((__format__ (printf, 1, 2)))
68 56324ebe 2022-07-14 thomas __attribute__((__nonnull__ (1)));
69 56324ebe 2022-07-14 thomas int kw_cmp(const void *, const void *);
70 56324ebe 2022-07-14 thomas int lookup(char *);
71 56324ebe 2022-07-14 thomas int lgetc(int);
72 56324ebe 2022-07-14 thomas int lungetc(int);
73 56324ebe 2022-07-14 thomas int findeol(void);
75 56324ebe 2022-07-14 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 56324ebe 2022-07-14 thomas struct sym {
77 56324ebe 2022-07-14 thomas TAILQ_ENTRY(sym) entry;
79 56324ebe 2022-07-14 thomas int persist;
80 56324ebe 2022-07-14 thomas char *nam;
81 56324ebe 2022-07-14 thomas char *val;
84 56324ebe 2022-07-14 thomas int symset(const char *, const char *, int);
85 56324ebe 2022-07-14 thomas char *symget(const char *);
87 56324ebe 2022-07-14 thomas static int errors;
89 56324ebe 2022-07-14 thomas static struct gotwebd *gotwebd;
90 56324ebe 2022-07-14 thomas static struct server *new_srv;
91 56324ebe 2022-07-14 thomas static struct server *conf_new_server(const char *);
92 56324ebe 2022-07-14 thomas int getservice(const char *);
95 56324ebe 2022-07-14 thomas int get_addrs(const char *, struct addresslist *, in_port_t);
96 56324ebe 2022-07-14 thomas struct address *host_v4(const char *);
97 56324ebe 2022-07-14 thomas struct address *host_v6(const char *);
98 56324ebe 2022-07-14 thomas int host_dns(const char *, struct addresslist *,
99 56324ebe 2022-07-14 thomas int, in_port_t, const char *, int);
100 56324ebe 2022-07-14 thomas int host_if(const char *, struct addresslist *,
101 56324ebe 2022-07-14 thomas int, in_port_t, const char *, int);
102 56324ebe 2022-07-14 thomas int host(const char *, struct addresslist *,
103 56324ebe 2022-07-14 thomas int, in_port_t, const char *, int);
104 56324ebe 2022-07-14 thomas int is_if_in_group(const char *, const char *);
106 56324ebe 2022-07-14 thomas typedef struct {
108 56324ebe 2022-07-14 thomas long long number;
109 56324ebe 2022-07-14 thomas char *string;
110 56324ebe 2022-07-14 thomas in_port_t port;
112 56324ebe 2022-07-14 thomas int lineno;
113 56324ebe 2022-07-14 thomas } YYSTYPE;
117 56324ebe 2022-07-14 thomas %token BIND INTERFACE WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
118 56324ebe 2022-07-14 thomas %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
119 56324ebe 2022-07-14 thomas %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
120 56324ebe 2022-07-14 thomas %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK FCGI_SOCKET
121 56324ebe 2022-07-14 thomas %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
123 56324ebe 2022-07-14 thomas %token <v.string> STRING
124 56324ebe 2022-07-14 thomas %type <v.port> fcgiport
125 56324ebe 2022-07-14 thomas %token <v.number> NUMBER
126 56324ebe 2022-07-14 thomas %type <v.number> boolean
130 56324ebe 2022-07-14 thomas grammar :
131 56324ebe 2022-07-14 thomas | grammar '\n'
132 56324ebe 2022-07-14 thomas | grammar main '\n'
133 56324ebe 2022-07-14 thomas | grammar server '\n'
136 56324ebe 2022-07-14 thomas boolean : STRING {
137 56324ebe 2022-07-14 thomas if (strcasecmp($1, "1") == 0 ||
138 56324ebe 2022-07-14 thomas strcasecmp($1, "yes") == 0 ||
139 56324ebe 2022-07-14 thomas strcasecmp($1, "on") == 0)
141 56324ebe 2022-07-14 thomas else if (strcasecmp($1, "0") == 0 ||
142 56324ebe 2022-07-14 thomas strcasecmp($1, "off") == 0 ||
143 56324ebe 2022-07-14 thomas strcasecmp($1, "no") == 0)
146 56324ebe 2022-07-14 thomas yyerror("invalid boolean value '%s'", $1);
147 56324ebe 2022-07-14 thomas free($1);
150 56324ebe 2022-07-14 thomas free($1);
152 56324ebe 2022-07-14 thomas | ON { $$ = 1; }
153 56324ebe 2022-07-14 thomas | NUMBER { $$ = $1; }
156 56324ebe 2022-07-14 thomas fcgiport : NUMBER {
157 56324ebe 2022-07-14 thomas if ($1 <= 0 || $1 > (int)USHRT_MAX) {
158 56324ebe 2022-07-14 thomas yyerror("invalid port: %lld", $1);
161 56324ebe 2022-07-14 thomas $$ = htons($1);
163 56324ebe 2022-07-14 thomas | STRING {
166 56324ebe 2022-07-14 thomas if ((val = getservice($1)) == -1) {
167 56324ebe 2022-07-14 thomas yyerror("invalid port: %s", $1);
168 56324ebe 2022-07-14 thomas free($1);
171 56324ebe 2022-07-14 thomas free($1);
173 56324ebe 2022-07-14 thomas $$ = val;
177 56324ebe 2022-07-14 thomas main : PREFORK NUMBER {
178 56324ebe 2022-07-14 thomas gotwebd->prefork_gotwebd = $2;
180 56324ebe 2022-07-14 thomas | CHROOT STRING {
181 56324ebe 2022-07-14 thomas n = strlcpy(gotwebd->httpd_chroot, $2,
182 56324ebe 2022-07-14 thomas sizeof(gotwebd->httpd_chroot));
183 56324ebe 2022-07-14 thomas if (n >= sizeof(gotwebd->httpd_chroot)) {
184 56324ebe 2022-07-14 thomas yyerror("%s: httpd_chroot truncated", __func__);
185 56324ebe 2022-07-14 thomas free($2);
188 56324ebe 2022-07-14 thomas free($2);
190 56324ebe 2022-07-14 thomas | FCGI_SOCKET boolean {
191 56324ebe 2022-07-14 thomas gotwebd->fcgi_socket = $2;
193 56324ebe 2022-07-14 thomas | FCGI_SOCKET boolean {
194 56324ebe 2022-07-14 thomas gotwebd->fcgi_socket = $2;
195 56324ebe 2022-07-14 thomas } '{' optnl socketopts4 '}'
196 56324ebe 2022-07-14 thomas | UNIX_SOCKET boolean {
197 56324ebe 2022-07-14 thomas gotwebd->unix_socket = $2;
199 56324ebe 2022-07-14 thomas | UNIX_SOCKET_NAME STRING {
200 56324ebe 2022-07-14 thomas n = snprintf(gotwebd->unix_socket_name,
201 56324ebe 2022-07-14 thomas sizeof(gotwebd->unix_socket_name), "%s%s",
202 56324ebe 2022-07-14 thomas strlen(gotwebd->httpd_chroot) ?
203 56324ebe 2022-07-14 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
204 56324ebe 2022-07-14 thomas if (n < 0) {
205 56324ebe 2022-07-14 thomas yyerror("%s: unix_socket_name truncated",
206 56324ebe 2022-07-14 thomas __func__);
207 56324ebe 2022-07-14 thomas free($2);
210 56324ebe 2022-07-14 thomas free($2);
214 56324ebe 2022-07-14 thomas server : SERVER STRING {
215 56324ebe 2022-07-14 thomas struct server *srv;
217 56324ebe 2022-07-14 thomas TAILQ_FOREACH(srv, gotwebd->servers, entry) {
218 56324ebe 2022-07-14 thomas if (strcmp(srv->name, $2) == 0) {
219 56324ebe 2022-07-14 thomas yyerror("server name exists '%s'", $2);
220 56324ebe 2022-07-14 thomas free($2);
225 56324ebe 2022-07-14 thomas new_srv = conf_new_server($2);
226 56324ebe 2022-07-14 thomas if (new_srv->fcgi_socket)
227 56324ebe 2022-07-14 thomas if (get_addrs(new_srv->fcgi_socket_bind,
228 56324ebe 2022-07-14 thomas new_srv->al,
229 56324ebe 2022-07-14 thomas new_srv->fcgi_socket_port) == -1) {
230 56324ebe 2022-07-14 thomas yyerror("could not get tcp iface "
231 56324ebe 2022-07-14 thomas "addrs");
234 56324ebe 2022-07-14 thomas log_debug("adding server %s", $2);
235 56324ebe 2022-07-14 thomas free($2);
237 56324ebe 2022-07-14 thomas | SERVER STRING {
238 56324ebe 2022-07-14 thomas struct server *srv;
240 56324ebe 2022-07-14 thomas TAILQ_FOREACH(srv, gotwebd->servers, entry) {
241 56324ebe 2022-07-14 thomas if (strcmp(srv->name, $2) == 0) {
242 56324ebe 2022-07-14 thomas yyerror("server name exists '%s'", $2);
243 56324ebe 2022-07-14 thomas free($2);
248 56324ebe 2022-07-14 thomas new_srv = conf_new_server($2);
249 56324ebe 2022-07-14 thomas log_debug("adding server %s", $2);
250 56324ebe 2022-07-14 thomas free($2);
251 56324ebe 2022-07-14 thomas } '{' optnl serveropts2 '}' {
252 56324ebe 2022-07-14 thomas if (get_addrs(new_srv->fcgi_socket_bind,
253 56324ebe 2022-07-14 thomas new_srv->al, new_srv->fcgi_socket_port) == -1) {
254 56324ebe 2022-07-14 thomas yyerror("could not get tcp iface addrs");
260 56324ebe 2022-07-14 thomas serveropts1 : REPOS_PATH STRING {
261 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->repos_path, $2,
262 56324ebe 2022-07-14 thomas sizeof(new_srv->repos_path));
263 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->repos_path)) {
264 56324ebe 2022-07-14 thomas yyerror("%s: repos_path truncated", __func__);
265 56324ebe 2022-07-14 thomas free($2);
268 56324ebe 2022-07-14 thomas free($2);
270 56324ebe 2022-07-14 thomas | SITE_NAME STRING {
271 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->site_name, $2,
272 56324ebe 2022-07-14 thomas sizeof(new_srv->site_name));
273 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->site_name)) {
274 56324ebe 2022-07-14 thomas yyerror("%s: site_name truncated", __func__);
275 56324ebe 2022-07-14 thomas free($2);
278 56324ebe 2022-07-14 thomas free($2);
280 56324ebe 2022-07-14 thomas | SITE_OWNER STRING {
281 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->site_owner, $2,
282 56324ebe 2022-07-14 thomas sizeof(new_srv->site_owner));
283 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->site_owner)) {
284 56324ebe 2022-07-14 thomas yyerror("%s: site_owner truncated", __func__);
285 56324ebe 2022-07-14 thomas free($2);
288 56324ebe 2022-07-14 thomas free($2);
290 56324ebe 2022-07-14 thomas | SITE_LINK STRING {
291 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->site_link, $2,
292 56324ebe 2022-07-14 thomas sizeof(new_srv->site_link));
293 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->site_link)) {
294 56324ebe 2022-07-14 thomas yyerror("%s: site_link truncated", __func__);
295 56324ebe 2022-07-14 thomas free($2);
298 56324ebe 2022-07-14 thomas free($2);
300 56324ebe 2022-07-14 thomas | LOGO STRING {
301 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
302 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->logo)) {
303 56324ebe 2022-07-14 thomas yyerror("%s: logo truncated", __func__);
304 56324ebe 2022-07-14 thomas free($2);
307 56324ebe 2022-07-14 thomas free($2);
309 56324ebe 2022-07-14 thomas | LOGO_URL STRING {
310 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->logo_url, $2,
311 56324ebe 2022-07-14 thomas sizeof(new_srv->logo_url));
312 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->logo_url)) {
313 56324ebe 2022-07-14 thomas yyerror("%s: logo_url truncated", __func__);
314 56324ebe 2022-07-14 thomas free($2);
317 56324ebe 2022-07-14 thomas free($2);
319 56324ebe 2022-07-14 thomas | CUSTOM_CSS STRING {
320 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->custom_css, $2,
321 56324ebe 2022-07-14 thomas sizeof(new_srv->custom_css));
322 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->custom_css)) {
323 56324ebe 2022-07-14 thomas yyerror("%s: custom_css truncated", __func__);
324 56324ebe 2022-07-14 thomas free($2);
327 56324ebe 2022-07-14 thomas free($2);
329 56324ebe 2022-07-14 thomas | MAX_REPOS NUMBER {
330 56324ebe 2022-07-14 thomas if ($2 > 0)
331 56324ebe 2022-07-14 thomas new_srv->max_repos = $2;
333 56324ebe 2022-07-14 thomas | SHOW_SITE_OWNER boolean {
334 56324ebe 2022-07-14 thomas new_srv->show_site_owner = $2;
336 56324ebe 2022-07-14 thomas | SHOW_REPO_OWNER boolean {
337 56324ebe 2022-07-14 thomas new_srv->show_repo_owner = $2;
339 56324ebe 2022-07-14 thomas | SHOW_REPO_AGE boolean {
340 56324ebe 2022-07-14 thomas new_srv->show_repo_age = $2;
342 56324ebe 2022-07-14 thomas | SHOW_REPO_DESCRIPTION boolean {
343 56324ebe 2022-07-14 thomas new_srv->show_repo_description = $2;
345 56324ebe 2022-07-14 thomas | SHOW_REPO_CLONEURL boolean {
346 56324ebe 2022-07-14 thomas new_srv->show_repo_cloneurl = $2;
348 56324ebe 2022-07-14 thomas | MAX_REPOS_DISPLAY NUMBER {
349 56324ebe 2022-07-14 thomas new_srv->max_repos_display = $2;
351 56324ebe 2022-07-14 thomas | MAX_COMMITS_DISPLAY NUMBER {
352 56324ebe 2022-07-14 thomas if ($2 > 0)
353 56324ebe 2022-07-14 thomas new_srv->max_commits_display = $2;
355 56324ebe 2022-07-14 thomas | FCGI_SOCKET boolean {
356 56324ebe 2022-07-14 thomas new_srv->fcgi_socket = $2;
358 56324ebe 2022-07-14 thomas | FCGI_SOCKET boolean {
359 56324ebe 2022-07-14 thomas new_srv->fcgi_socket = $2;
360 56324ebe 2022-07-14 thomas } '{' optnl socketopts2 '}'
361 56324ebe 2022-07-14 thomas | UNIX_SOCKET boolean {
362 56324ebe 2022-07-14 thomas new_srv->unix_socket = $2;
364 56324ebe 2022-07-14 thomas | UNIX_SOCKET_NAME STRING {
365 56324ebe 2022-07-14 thomas n = snprintf(new_srv->unix_socket_name,
366 56324ebe 2022-07-14 thomas sizeof(new_srv->unix_socket_name), "%s%s",
367 56324ebe 2022-07-14 thomas strlen(gotwebd->httpd_chroot) ?
368 56324ebe 2022-07-14 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
369 56324ebe 2022-07-14 thomas if (n < 0) {
370 56324ebe 2022-07-14 thomas yyerror("%s: unix_socket_name truncated",
371 56324ebe 2022-07-14 thomas __func__);
372 56324ebe 2022-07-14 thomas free($2);
375 56324ebe 2022-07-14 thomas free($2);
379 56324ebe 2022-07-14 thomas serveropts2 : serveropts2 serveropts1 nl
380 56324ebe 2022-07-14 thomas | serveropts1 optnl
383 56324ebe 2022-07-14 thomas socketopts1 : BIND INTERFACE STRING {
384 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->fcgi_socket_bind, $3,
385 56324ebe 2022-07-14 thomas sizeof(new_srv->fcgi_socket_bind));
386 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->fcgi_socket_bind)) {
387 56324ebe 2022-07-14 thomas yyerror("%s: fcgi_socket_bind truncated",
388 56324ebe 2022-07-14 thomas __func__);
389 56324ebe 2022-07-14 thomas free($3);
392 56324ebe 2022-07-14 thomas free($3);
394 56324ebe 2022-07-14 thomas | PORT fcgiport {
395 56324ebe 2022-07-14 thomas struct server *srv;
397 56324ebe 2022-07-14 thomas TAILQ_FOREACH(srv, gotwebd->servers, entry) {
398 56324ebe 2022-07-14 thomas if (srv->fcgi_socket_port == $2) {
399 56324ebe 2022-07-14 thomas yyerror("port already assigned");
403 56324ebe 2022-07-14 thomas new_srv->fcgi_socket_port = $2;
407 56324ebe 2022-07-14 thomas socketopts2 : socketopts2 socketopts1 nl
408 56324ebe 2022-07-14 thomas | socketopts1 optnl
411 56324ebe 2022-07-14 thomas socketopts3 : BIND INTERFACE STRING {
412 56324ebe 2022-07-14 thomas n = strlcpy(gotwebd->fcgi_socket_bind, $3,
413 56324ebe 2022-07-14 thomas sizeof(gotwebd->fcgi_socket_bind));
414 56324ebe 2022-07-14 thomas if (n >= sizeof(gotwebd->fcgi_socket_bind)) {
415 56324ebe 2022-07-14 thomas yyerror("%s: fcgi_socket_bind truncated",
416 56324ebe 2022-07-14 thomas __func__);
417 56324ebe 2022-07-14 thomas free($3);
420 56324ebe 2022-07-14 thomas free($3);
422 56324ebe 2022-07-14 thomas | PORT fcgiport {
423 56324ebe 2022-07-14 thomas gotwebd->fcgi_socket_port = $2;
427 56324ebe 2022-07-14 thomas socketopts4 : socketopts4 socketopts3 nl
428 56324ebe 2022-07-14 thomas | socketopts3 optnl
431 56324ebe 2022-07-14 thomas nl : '\n' optnl
434 56324ebe 2022-07-14 thomas optnl : '\n' optnl /* zero or more newlines */
435 56324ebe 2022-07-14 thomas | /* empty */
440 56324ebe 2022-07-14 thomas struct keywords {
441 56324ebe 2022-07-14 thomas const char *k_name;
442 56324ebe 2022-07-14 thomas int k_val;
446 56324ebe 2022-07-14 thomas yyerror(const char *fmt, ...)
448 56324ebe 2022-07-14 thomas va_list ap;
449 56324ebe 2022-07-14 thomas char *msg;
451 56324ebe 2022-07-14 thomas file->errors++;
452 56324ebe 2022-07-14 thomas va_start(ap, fmt);
453 56324ebe 2022-07-14 thomas if (vasprintf(&msg, fmt, ap) == -1)
454 56324ebe 2022-07-14 thomas fatalx("yyerror vasprintf");
455 56324ebe 2022-07-14 thomas va_end(ap);
456 56324ebe 2022-07-14 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
457 56324ebe 2022-07-14 thomas free(msg);
458 56324ebe 2022-07-14 thomas return (0);
462 56324ebe 2022-07-14 thomas kw_cmp(const void *k, const void *e)
464 56324ebe 2022-07-14 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
468 56324ebe 2022-07-14 thomas lookup(char *s)
470 56324ebe 2022-07-14 thomas /* This has to be sorted always. */
471 56324ebe 2022-07-14 thomas static const struct keywords keywords[] = {
472 56324ebe 2022-07-14 thomas { "bind", BIND },
473 56324ebe 2022-07-14 thomas { "chroot", CHROOT },
474 56324ebe 2022-07-14 thomas { "custom_css", CUSTOM_CSS },
475 56324ebe 2022-07-14 thomas { "fcgi_socket", FCGI_SOCKET },
476 56324ebe 2022-07-14 thomas { "interface", INTERFACE },
477 56324ebe 2022-07-14 thomas { "logo", LOGO },
478 56324ebe 2022-07-14 thomas { "logo_url" , LOGO_URL },
479 56324ebe 2022-07-14 thomas { "max_commits_display", MAX_COMMITS_DISPLAY },
480 56324ebe 2022-07-14 thomas { "max_repos", MAX_REPOS },
481 56324ebe 2022-07-14 thomas { "max_repos_display", MAX_REPOS_DISPLAY },
482 56324ebe 2022-07-14 thomas { "port", PORT },
483 56324ebe 2022-07-14 thomas { "prefork", PREFORK },
484 56324ebe 2022-07-14 thomas { "repos_path", REPOS_PATH },
485 56324ebe 2022-07-14 thomas { "server", SERVER },
486 56324ebe 2022-07-14 thomas { "show_repo_age", SHOW_REPO_AGE },
487 56324ebe 2022-07-14 thomas { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
488 56324ebe 2022-07-14 thomas { "show_repo_description", SHOW_REPO_DESCRIPTION },
489 56324ebe 2022-07-14 thomas { "show_repo_owner", SHOW_REPO_OWNER },
490 56324ebe 2022-07-14 thomas { "show_site_owner", SHOW_SITE_OWNER },
491 56324ebe 2022-07-14 thomas { "site_link", SITE_LINK },
492 56324ebe 2022-07-14 thomas { "site_name", SITE_NAME },
493 56324ebe 2022-07-14 thomas { "site_owner", SITE_OWNER },
494 56324ebe 2022-07-14 thomas { "unix_socket", UNIX_SOCKET },
495 56324ebe 2022-07-14 thomas { "unix_socket_name", UNIX_SOCKET_NAME },
497 56324ebe 2022-07-14 thomas const struct keywords *p;
499 56324ebe 2022-07-14 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
500 56324ebe 2022-07-14 thomas sizeof(keywords[0]), kw_cmp);
503 56324ebe 2022-07-14 thomas return (p->k_val);
505 56324ebe 2022-07-14 thomas return (STRING);
508 56324ebe 2022-07-14 thomas #define MAXPUSHBACK 128
510 56324ebe 2022-07-14 thomas unsigned char *parsebuf;
511 56324ebe 2022-07-14 thomas int parseindex;
512 56324ebe 2022-07-14 thomas unsigned char pushback_buffer[MAXPUSHBACK];
513 56324ebe 2022-07-14 thomas int pushback_index = 0;
516 56324ebe 2022-07-14 thomas lgetc(int quotec)
518 56324ebe 2022-07-14 thomas int c, next;
520 56324ebe 2022-07-14 thomas if (parsebuf) {
521 56324ebe 2022-07-14 thomas /* Read character from the parsebuffer instead of input. */
522 56324ebe 2022-07-14 thomas if (parseindex >= 0) {
523 56324ebe 2022-07-14 thomas c = parsebuf[parseindex++];
524 56324ebe 2022-07-14 thomas if (c != '\0')
525 56324ebe 2022-07-14 thomas return (c);
526 56324ebe 2022-07-14 thomas parsebuf = NULL;
528 56324ebe 2022-07-14 thomas parseindex++;
531 56324ebe 2022-07-14 thomas if (pushback_index)
532 56324ebe 2022-07-14 thomas return (pushback_buffer[--pushback_index]);
534 56324ebe 2022-07-14 thomas if (quotec) {
535 56324ebe 2022-07-14 thomas c = getc(file->stream);
536 56324ebe 2022-07-14 thomas if (c == EOF)
537 56324ebe 2022-07-14 thomas yyerror("reached end of file while parsing "
538 56324ebe 2022-07-14 thomas "quoted string");
539 56324ebe 2022-07-14 thomas return (c);
542 56324ebe 2022-07-14 thomas c = getc(file->stream);
543 56324ebe 2022-07-14 thomas while (c == '\\') {
544 56324ebe 2022-07-14 thomas next = getc(file->stream);
545 56324ebe 2022-07-14 thomas if (next != '\n') {
546 56324ebe 2022-07-14 thomas c = next;
549 56324ebe 2022-07-14 thomas yylval.lineno = file->lineno;
550 56324ebe 2022-07-14 thomas file->lineno++;
551 56324ebe 2022-07-14 thomas c = getc(file->stream);
554 56324ebe 2022-07-14 thomas return (c);
558 56324ebe 2022-07-14 thomas lungetc(int c)
560 56324ebe 2022-07-14 thomas if (c == EOF)
561 56324ebe 2022-07-14 thomas return (EOF);
562 56324ebe 2022-07-14 thomas if (parsebuf) {
563 56324ebe 2022-07-14 thomas parseindex--;
564 56324ebe 2022-07-14 thomas if (parseindex >= 0)
565 56324ebe 2022-07-14 thomas return (c);
567 56324ebe 2022-07-14 thomas if (pushback_index < MAXPUSHBACK-1)
568 56324ebe 2022-07-14 thomas return (pushback_buffer[pushback_index++] = c);
570 56324ebe 2022-07-14 thomas return (EOF);
574 56324ebe 2022-07-14 thomas findeol(void)
578 56324ebe 2022-07-14 thomas parsebuf = NULL;
580 56324ebe 2022-07-14 thomas /* Skip to either EOF or the first real EOL. */
581 56324ebe 2022-07-14 thomas while (1) {
582 56324ebe 2022-07-14 thomas if (pushback_index)
583 56324ebe 2022-07-14 thomas c = pushback_buffer[--pushback_index];
585 56324ebe 2022-07-14 thomas c = lgetc(0);
586 56324ebe 2022-07-14 thomas if (c == '\n') {
587 56324ebe 2022-07-14 thomas file->lineno++;
590 56324ebe 2022-07-14 thomas if (c == EOF)
593 56324ebe 2022-07-14 thomas return (ERROR);
597 56324ebe 2022-07-14 thomas yylex(void)
599 56324ebe 2022-07-14 thomas unsigned char buf[8096];
600 56324ebe 2022-07-14 thomas unsigned char *p, *val;
601 56324ebe 2022-07-14 thomas int quotec, next, c;
602 56324ebe 2022-07-14 thomas int token;
606 56324ebe 2022-07-14 thomas c = lgetc(0);
607 56324ebe 2022-07-14 thomas while (c == ' ' || c == '\t')
608 56324ebe 2022-07-14 thomas c = lgetc(0); /* nothing */
610 56324ebe 2022-07-14 thomas yylval.lineno = file->lineno;
611 56324ebe 2022-07-14 thomas if (c == '#') {
612 56324ebe 2022-07-14 thomas c = lgetc(0);
613 56324ebe 2022-07-14 thomas while (c != '\n' && c != EOF)
614 56324ebe 2022-07-14 thomas c = lgetc(0); /* nothing */
616 56324ebe 2022-07-14 thomas if (c == '$' && parsebuf == NULL) {
617 56324ebe 2022-07-14 thomas while (1) {
618 56324ebe 2022-07-14 thomas c = lgetc(0);
619 56324ebe 2022-07-14 thomas if (c == EOF)
620 56324ebe 2022-07-14 thomas return (0);
622 56324ebe 2022-07-14 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
623 56324ebe 2022-07-14 thomas yyerror("string too long");
624 56324ebe 2022-07-14 thomas return (findeol());
626 56324ebe 2022-07-14 thomas if (isalnum(c) || c == '_') {
627 56324ebe 2022-07-14 thomas *p++ = c;
628 56324ebe 2022-07-14 thomas continue;
630 56324ebe 2022-07-14 thomas *p = '\0';
631 56324ebe 2022-07-14 thomas lungetc(c);
634 56324ebe 2022-07-14 thomas val = symget(buf);
635 56324ebe 2022-07-14 thomas if (val == NULL) {
636 56324ebe 2022-07-14 thomas yyerror("macro '%s' not defined", buf);
637 56324ebe 2022-07-14 thomas return (findeol());
639 56324ebe 2022-07-14 thomas parsebuf = val;
640 56324ebe 2022-07-14 thomas parseindex = 0;
641 56324ebe 2022-07-14 thomas goto top;
644 56324ebe 2022-07-14 thomas switch (c) {
645 56324ebe 2022-07-14 thomas case '\'':
646 56324ebe 2022-07-14 thomas case '"':
647 56324ebe 2022-07-14 thomas quotec = c;
648 56324ebe 2022-07-14 thomas while (1) {
649 56324ebe 2022-07-14 thomas c = lgetc(quotec);
650 56324ebe 2022-07-14 thomas if (c == EOF)
651 56324ebe 2022-07-14 thomas return (0);
652 56324ebe 2022-07-14 thomas if (c == '\n') {
653 56324ebe 2022-07-14 thomas file->lineno++;
654 56324ebe 2022-07-14 thomas continue;
655 56324ebe 2022-07-14 thomas } else if (c == '\\') {
656 56324ebe 2022-07-14 thomas next = lgetc(quotec);
657 56324ebe 2022-07-14 thomas if (next == EOF)
658 56324ebe 2022-07-14 thomas return (0);
659 56324ebe 2022-07-14 thomas if (next == quotec || c == ' ' || c == '\t')
660 56324ebe 2022-07-14 thomas c = next;
661 56324ebe 2022-07-14 thomas else if (next == '\n') {
662 56324ebe 2022-07-14 thomas file->lineno++;
663 56324ebe 2022-07-14 thomas continue;
665 56324ebe 2022-07-14 thomas lungetc(next);
666 56324ebe 2022-07-14 thomas } else if (c == quotec) {
667 56324ebe 2022-07-14 thomas *p = '\0';
669 56324ebe 2022-07-14 thomas } else if (c == '\0') {
670 56324ebe 2022-07-14 thomas yyerror("syntax error");
671 56324ebe 2022-07-14 thomas return (findeol());
673 56324ebe 2022-07-14 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
674 56324ebe 2022-07-14 thomas yyerror("string too long");
675 56324ebe 2022-07-14 thomas return (findeol());
677 56324ebe 2022-07-14 thomas *p++ = c;
679 56324ebe 2022-07-14 thomas yylval.v.string = strdup(buf);
680 56324ebe 2022-07-14 thomas if (yylval.v.string == NULL)
681 56324ebe 2022-07-14 thomas err(1, "yylex: strdup");
682 56324ebe 2022-07-14 thomas return (STRING);
685 56324ebe 2022-07-14 thomas #define allowed_to_end_number(x) \
686 56324ebe 2022-07-14 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
688 56324ebe 2022-07-14 thomas if (c == '-' || isdigit(c)) {
690 56324ebe 2022-07-14 thomas *p++ = c;
691 56324ebe 2022-07-14 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
692 56324ebe 2022-07-14 thomas yyerror("string too long");
693 56324ebe 2022-07-14 thomas return (findeol());
695 56324ebe 2022-07-14 thomas c = lgetc(0);
696 56324ebe 2022-07-14 thomas } while (c != EOF && isdigit(c));
697 56324ebe 2022-07-14 thomas lungetc(c);
698 56324ebe 2022-07-14 thomas if (p == buf + 1 && buf[0] == '-')
699 56324ebe 2022-07-14 thomas goto nodigits;
700 56324ebe 2022-07-14 thomas if (c == EOF || allowed_to_end_number(c)) {
701 56324ebe 2022-07-14 thomas const char *errstr = NULL;
703 56324ebe 2022-07-14 thomas *p = '\0';
704 56324ebe 2022-07-14 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
705 56324ebe 2022-07-14 thomas LLONG_MAX, &errstr);
706 56324ebe 2022-07-14 thomas if (errstr) {
707 56324ebe 2022-07-14 thomas yyerror("\"%s\" invalid number: %s",
708 56324ebe 2022-07-14 thomas buf, errstr);
709 56324ebe 2022-07-14 thomas return (findeol());
711 56324ebe 2022-07-14 thomas return (NUMBER);
713 56324ebe 2022-07-14 thomas nodigits:
714 56324ebe 2022-07-14 thomas while (p > buf + 1)
715 56324ebe 2022-07-14 thomas lungetc(*--p);
716 56324ebe 2022-07-14 thomas c = *--p;
717 56324ebe 2022-07-14 thomas if (c == '-')
718 56324ebe 2022-07-14 thomas return (c);
722 56324ebe 2022-07-14 thomas #define allowed_in_string(x) \
723 56324ebe 2022-07-14 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
724 56324ebe 2022-07-14 thomas x != '{' && x != '}' && \
725 56324ebe 2022-07-14 thomas x != '!' && x != '=' && x != '#' && \
726 56324ebe 2022-07-14 thomas x != ','))
728 56324ebe 2022-07-14 thomas if (isalnum(c) || c == ':' || c == '_') {
730 56324ebe 2022-07-14 thomas *p++ = c;
731 56324ebe 2022-07-14 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
732 56324ebe 2022-07-14 thomas yyerror("string too long");
733 56324ebe 2022-07-14 thomas return (findeol());
735 56324ebe 2022-07-14 thomas c = lgetc(0);
736 56324ebe 2022-07-14 thomas } while (c != EOF && (allowed_in_string(c)));
737 56324ebe 2022-07-14 thomas lungetc(c);
738 56324ebe 2022-07-14 thomas *p = '\0';
739 56324ebe 2022-07-14 thomas token = lookup(buf);
740 56324ebe 2022-07-14 thomas if (token == STRING) {
741 56324ebe 2022-07-14 thomas yylval.v.string = strdup(buf);
742 56324ebe 2022-07-14 thomas if (yylval.v.string == NULL)
743 56324ebe 2022-07-14 thomas err(1, "yylex: strdup");
745 56324ebe 2022-07-14 thomas return (token);
747 56324ebe 2022-07-14 thomas if (c == '\n') {
748 56324ebe 2022-07-14 thomas yylval.lineno = file->lineno;
749 56324ebe 2022-07-14 thomas file->lineno++;
751 56324ebe 2022-07-14 thomas if (c == EOF)
752 56324ebe 2022-07-14 thomas return (0);
753 56324ebe 2022-07-14 thomas return (c);
757 56324ebe 2022-07-14 thomas check_file_secrecy(int fd, const char *fname)
759 56324ebe 2022-07-14 thomas struct stat st;
761 56324ebe 2022-07-14 thomas if (fstat(fd, &st)) {
762 56324ebe 2022-07-14 thomas log_warn("cannot stat %s", fname);
763 56324ebe 2022-07-14 thomas return (-1);
765 56324ebe 2022-07-14 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
766 56324ebe 2022-07-14 thomas log_warnx("%s: owner not root or current user", fname);
767 56324ebe 2022-07-14 thomas return (-1);
769 56324ebe 2022-07-14 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
770 56324ebe 2022-07-14 thomas log_warnx("%s: group writable or world read/writable", fname);
771 56324ebe 2022-07-14 thomas return (-1);
773 56324ebe 2022-07-14 thomas return (0);
776 56324ebe 2022-07-14 thomas struct file *
777 56324ebe 2022-07-14 thomas newfile(const char *name, int secret)
779 56324ebe 2022-07-14 thomas struct file *nfile;
781 56324ebe 2022-07-14 thomas nfile = calloc(1, sizeof(struct file));
782 56324ebe 2022-07-14 thomas if (nfile == NULL) {
783 56324ebe 2022-07-14 thomas log_warn("calloc");
784 56324ebe 2022-07-14 thomas return (NULL);
786 56324ebe 2022-07-14 thomas nfile->name = strdup(name);
787 56324ebe 2022-07-14 thomas if (nfile->name == NULL) {
788 56324ebe 2022-07-14 thomas log_warn("strdup");
789 56324ebe 2022-07-14 thomas free(nfile);
790 56324ebe 2022-07-14 thomas return (NULL);
792 56324ebe 2022-07-14 thomas nfile->stream = fopen(nfile->name, "r");
793 56324ebe 2022-07-14 thomas if (nfile->stream == NULL) {
794 56324ebe 2022-07-14 thomas /* no warning, we don't require a conf file */
795 56324ebe 2022-07-14 thomas free(nfile->name);
796 56324ebe 2022-07-14 thomas free(nfile);
797 56324ebe 2022-07-14 thomas return (NULL);
798 56324ebe 2022-07-14 thomas } else if (secret &&
799 56324ebe 2022-07-14 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
800 56324ebe 2022-07-14 thomas fclose(nfile->stream);
801 56324ebe 2022-07-14 thomas free(nfile->name);
802 56324ebe 2022-07-14 thomas free(nfile);
803 56324ebe 2022-07-14 thomas return (NULL);
805 56324ebe 2022-07-14 thomas nfile->lineno = 1;
806 56324ebe 2022-07-14 thomas return (nfile);
809 56324ebe 2022-07-14 thomas static void
810 56324ebe 2022-07-14 thomas closefile(struct file *xfile)
812 56324ebe 2022-07-14 thomas fclose(xfile->stream);
813 56324ebe 2022-07-14 thomas free(xfile->name);
814 56324ebe 2022-07-14 thomas free(xfile);
818 56324ebe 2022-07-14 thomas parse_config(const char *filename, struct gotwebd *env)
820 56324ebe 2022-07-14 thomas struct sym *sym, *next;
822 56324ebe 2022-07-14 thomas file = newfile(filename, 0);
823 56324ebe 2022-07-14 thomas if (file == NULL)
824 56324ebe 2022-07-14 thomas /* just return, as we don't require a conf file */
825 56324ebe 2022-07-14 thomas return (0);
827 56324ebe 2022-07-14 thomas if (config_init(env) == -1)
828 56324ebe 2022-07-14 thomas fatalx("failed to initialize configuration");
830 56324ebe 2022-07-14 thomas gotwebd = env;
832 56324ebe 2022-07-14 thomas yyparse();
833 56324ebe 2022-07-14 thomas errors = file->errors;
834 56324ebe 2022-07-14 thomas closefile(file);
836 56324ebe 2022-07-14 thomas /* Free macros and check which have not been used. */
837 56324ebe 2022-07-14 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
838 56324ebe 2022-07-14 thomas if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
839 56324ebe 2022-07-14 thomas fprintf(stderr, "warning: macro '%s' not used\n",
840 56324ebe 2022-07-14 thomas sym->nam);
841 56324ebe 2022-07-14 thomas if (!sym->persist) {
842 56324ebe 2022-07-14 thomas free(sym->nam);
843 56324ebe 2022-07-14 thomas free(sym->val);
844 56324ebe 2022-07-14 thomas TAILQ_REMOVE(&symhead, sym, entry);
845 56324ebe 2022-07-14 thomas free(sym);
849 56324ebe 2022-07-14 thomas if (errors)
850 56324ebe 2022-07-14 thomas return (-1);
852 56324ebe 2022-07-14 thomas /* just add default server if no config specified */
853 56324ebe 2022-07-14 thomas if (gotwebd->server_cnt == 0) {
854 56324ebe 2022-07-14 thomas new_srv = conf_new_server(D_SITENAME);
855 56324ebe 2022-07-14 thomas log_debug("%s: adding default server %s", __func__, D_SITENAME);
858 56324ebe 2022-07-14 thomas /* setup our listening sockets */
859 56324ebe 2022-07-14 thomas sockets_parse_sockets(env);
861 56324ebe 2022-07-14 thomas return (0);
864 56324ebe 2022-07-14 thomas struct server *
865 56324ebe 2022-07-14 thomas conf_new_server(const char *name)
867 56324ebe 2022-07-14 thomas struct server *srv = NULL;
870 56324ebe 2022-07-14 thomas srv = calloc(1, sizeof(*srv));
871 56324ebe 2022-07-14 thomas if (srv == NULL)
872 56324ebe 2022-07-14 thomas fatalx("%s: calloc", __func__);
874 56324ebe 2022-07-14 thomas n = strlcpy(srv->name, name, sizeof(srv->name));
875 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->name))
876 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
877 56324ebe 2022-07-14 thomas n = snprintf(srv->unix_socket_name,
878 56324ebe 2022-07-14 thomas sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
879 56324ebe 2022-07-14 thomas D_UNIX_SOCKET);
880 56324ebe 2022-07-14 thomas if (n < 0)
881 56324ebe 2022-07-14 thomas fatalx("%s: snprintf", __func__);
882 56324ebe 2022-07-14 thomas n = strlcpy(srv->repos_path, D_GOTPATH,
883 56324ebe 2022-07-14 thomas sizeof(srv->repos_path));
884 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->repos_path))
885 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
886 56324ebe 2022-07-14 thomas n = strlcpy(srv->site_name, D_SITENAME,
887 56324ebe 2022-07-14 thomas sizeof(srv->site_name));
888 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->site_name))
889 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
890 56324ebe 2022-07-14 thomas n = strlcpy(srv->site_owner, D_SITEOWNER,
891 56324ebe 2022-07-14 thomas sizeof(srv->site_owner));
892 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->site_owner))
893 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
894 56324ebe 2022-07-14 thomas n = strlcpy(srv->site_link, D_SITELINK,
895 56324ebe 2022-07-14 thomas sizeof(srv->site_link));
896 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->site_link))
897 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
898 56324ebe 2022-07-14 thomas n = strlcpy(srv->logo, D_GOTLOGO,
899 56324ebe 2022-07-14 thomas sizeof(srv->logo));
900 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->logo))
901 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
902 56324ebe 2022-07-14 thomas n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
903 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->logo_url))
904 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
905 56324ebe 2022-07-14 thomas n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
906 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->custom_css))
907 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
909 56324ebe 2022-07-14 thomas val = getservice(D_FCGI_PORT);
910 56324ebe 2022-07-14 thomas srv->fcgi_socket_port = gotwebd->fcgi_socket_port ?
911 56324ebe 2022-07-14 thomas gotwebd->fcgi_socket_port: htons(val);
913 56324ebe 2022-07-14 thomas srv->show_site_owner = D_SHOWSOWNER;
914 56324ebe 2022-07-14 thomas srv->show_repo_owner = D_SHOWROWNER;
915 56324ebe 2022-07-14 thomas srv->show_repo_age = D_SHOWAGE;
916 56324ebe 2022-07-14 thomas srv->show_repo_description = D_SHOWDESC;
917 56324ebe 2022-07-14 thomas srv->show_repo_cloneurl = D_SHOWURL;
919 56324ebe 2022-07-14 thomas srv->max_repos_display = D_MAXREPODISP;
920 56324ebe 2022-07-14 thomas srv->max_commits_display = D_MAXCOMMITDISP;
921 56324ebe 2022-07-14 thomas srv->max_repos = D_MAXREPO;
923 56324ebe 2022-07-14 thomas srv->unix_socket = 1;
924 56324ebe 2022-07-14 thomas srv->fcgi_socket = gotwebd->fcgi_socket ? gotwebd->fcgi_socket : 0;
926 56324ebe 2022-07-14 thomas if ((srv->al = calloc(1, sizeof(*srv->al))) == NULL)
927 56324ebe 2022-07-14 thomas fatalx("%s: calloc", __func__);
929 56324ebe 2022-07-14 thomas TAILQ_INIT(srv->al);
930 56324ebe 2022-07-14 thomas TAILQ_INSERT_TAIL(gotwebd->servers, srv, entry);
931 56324ebe 2022-07-14 thomas gotwebd->server_cnt++;
933 56324ebe 2022-07-14 thomas return srv;
937 56324ebe 2022-07-14 thomas symset(const char *nam, const char *val, int persist)
939 56324ebe 2022-07-14 thomas struct sym *sym;
941 56324ebe 2022-07-14 thomas TAILQ_FOREACH(sym, &symhead, entry) {
942 56324ebe 2022-07-14 thomas if (strcmp(nam, sym->nam) == 0)
946 56324ebe 2022-07-14 thomas if (sym != NULL) {
947 56324ebe 2022-07-14 thomas if (sym->persist == 1)
948 56324ebe 2022-07-14 thomas return (0);
950 56324ebe 2022-07-14 thomas free(sym->nam);
951 56324ebe 2022-07-14 thomas free(sym->val);
952 56324ebe 2022-07-14 thomas TAILQ_REMOVE(&symhead, sym, entry);
953 56324ebe 2022-07-14 thomas free(sym);
956 56324ebe 2022-07-14 thomas sym = calloc(1, sizeof(*sym));
957 56324ebe 2022-07-14 thomas if (sym == NULL)
958 56324ebe 2022-07-14 thomas return (-1);
960 56324ebe 2022-07-14 thomas sym->nam = strdup(nam);
961 56324ebe 2022-07-14 thomas if (sym->nam == NULL) {
962 56324ebe 2022-07-14 thomas free(sym);
963 56324ebe 2022-07-14 thomas return (-1);
965 56324ebe 2022-07-14 thomas sym->val = strdup(val);
966 56324ebe 2022-07-14 thomas if (sym->val == NULL) {
967 56324ebe 2022-07-14 thomas free(sym->nam);
968 56324ebe 2022-07-14 thomas free(sym);
969 56324ebe 2022-07-14 thomas return (-1);
971 56324ebe 2022-07-14 thomas sym->used = 0;
972 56324ebe 2022-07-14 thomas sym->persist = persist;
973 56324ebe 2022-07-14 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
974 56324ebe 2022-07-14 thomas return (0);
978 56324ebe 2022-07-14 thomas cmdline_symset(char *s)
980 56324ebe 2022-07-14 thomas char *sym, *val;
982 56324ebe 2022-07-14 thomas size_t len;
984 56324ebe 2022-07-14 thomas val = strrchr(s, '=');
985 56324ebe 2022-07-14 thomas if (val == NULL)
986 56324ebe 2022-07-14 thomas return (-1);
988 56324ebe 2022-07-14 thomas len = strlen(s) - strlen(val) + 1;
989 56324ebe 2022-07-14 thomas sym = malloc(len);
990 56324ebe 2022-07-14 thomas if (sym == NULL)
991 56324ebe 2022-07-14 thomas fatal("%s: malloc", __func__);
993 56324ebe 2022-07-14 thomas memcpy(&sym, s, len);
995 56324ebe 2022-07-14 thomas ret = symset(sym, val + 1, 1);
996 56324ebe 2022-07-14 thomas free(sym);
998 56324ebe 2022-07-14 thomas return (ret);
1002 56324ebe 2022-07-14 thomas symget(const char *nam)
1004 56324ebe 2022-07-14 thomas struct sym *sym;
1006 56324ebe 2022-07-14 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1007 56324ebe 2022-07-14 thomas if (strcmp(nam, sym->nam) == 0) {
1008 56324ebe 2022-07-14 thomas sym->used = 1;
1009 56324ebe 2022-07-14 thomas return (sym->val);
1012 56324ebe 2022-07-14 thomas return (NULL);
1016 56324ebe 2022-07-14 thomas getservice(const char *n)
1018 56324ebe 2022-07-14 thomas struct servent *s;
1019 56324ebe 2022-07-14 thomas const char *errstr;
1020 56324ebe 2022-07-14 thomas long long llval;
1022 56324ebe 2022-07-14 thomas llval = strtonum(n, 0, UINT16_MAX, &errstr);
1023 56324ebe 2022-07-14 thomas if (errstr) {
1024 56324ebe 2022-07-14 thomas s = getservbyname(n, "tcp");
1025 56324ebe 2022-07-14 thomas if (s == NULL)
1026 56324ebe 2022-07-14 thomas s = getservbyname(n, "udp");
1027 56324ebe 2022-07-14 thomas if (s == NULL)
1028 56324ebe 2022-07-14 thomas return (-1);
1029 56324ebe 2022-07-14 thomas return (s->s_port);
1032 56324ebe 2022-07-14 thomas return (htons((unsigned short)llval));
1035 56324ebe 2022-07-14 thomas struct address *
1036 56324ebe 2022-07-14 thomas host_v4(const char *s)
1038 56324ebe 2022-07-14 thomas struct in_addr ina;
1039 56324ebe 2022-07-14 thomas struct sockaddr_in *sain;
1040 56324ebe 2022-07-14 thomas struct address *h;
1042 56324ebe 2022-07-14 thomas memset(&ina, 0, sizeof(ina));
1043 56324ebe 2022-07-14 thomas if (inet_pton(AF_INET, s, &ina) != 1)
1044 56324ebe 2022-07-14 thomas return (NULL);
1046 56324ebe 2022-07-14 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1047 56324ebe 2022-07-14 thomas fatal(__func__);
1048 56324ebe 2022-07-14 thomas sain = (struct sockaddr_in *)&h->ss;
1049 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1050 d7cad54e 2022-07-14 thomas #ifndef __linux__
1051 56324ebe 2022-07-14 thomas sain->sin_len = sizeof(struct sockaddr_in);
1053 56324ebe 2022-07-14 thomas sain->sin_family = AF_INET;
1054 56324ebe 2022-07-14 thomas sain->sin_addr.s_addr = ina.s_addr;
1055 56324ebe 2022-07-14 thomas if (sain->sin_addr.s_addr == INADDR_ANY)
1056 56324ebe 2022-07-14 thomas h->prefixlen = 0; /* 0.0.0.0 address */
1058 56324ebe 2022-07-14 thomas h->prefixlen = -1; /* host address */
1059 56324ebe 2022-07-14 thomas return (h);
1062 56324ebe 2022-07-14 thomas struct address *
1063 56324ebe 2022-07-14 thomas host_v6(const char *s)
1065 56324ebe 2022-07-14 thomas struct addrinfo hints, *res;
1066 56324ebe 2022-07-14 thomas struct sockaddr_in6 *sa_in6;
1067 56324ebe 2022-07-14 thomas struct address *h = NULL;
1069 56324ebe 2022-07-14 thomas memset(&hints, 0, sizeof(hints));
1070 56324ebe 2022-07-14 thomas hints.ai_family = AF_INET6;
1071 56324ebe 2022-07-14 thomas hints.ai_socktype = SOCK_DGRAM; /* dummy */
1072 56324ebe 2022-07-14 thomas hints.ai_flags = AI_NUMERICHOST;
1073 56324ebe 2022-07-14 thomas if (getaddrinfo(s, "0", &hints, &res) == 0) {
1074 56324ebe 2022-07-14 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1075 56324ebe 2022-07-14 thomas fatal(__func__);
1076 56324ebe 2022-07-14 thomas sa_in6 = (struct sockaddr_in6 *)&h->ss;
1077 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1078 d7cad54e 2022-07-14 thomas #ifndef __linux__
1079 56324ebe 2022-07-14 thomas sa_in6->sin6_len = sizeof(struct sockaddr_in6);
1081 56324ebe 2022-07-14 thomas sa_in6->sin6_family = AF_INET6;
1082 56324ebe 2022-07-14 thomas memcpy(&sa_in6->sin6_addr,
1083 56324ebe 2022-07-14 thomas &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1084 56324ebe 2022-07-14 thomas sizeof(sa_in6->sin6_addr));
1085 56324ebe 2022-07-14 thomas sa_in6->sin6_scope_id =
1086 56324ebe 2022-07-14 thomas ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
1087 56324ebe 2022-07-14 thomas if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1088 56324ebe 2022-07-14 thomas sizeof(sa_in6->sin6_addr)) == 0)
1089 56324ebe 2022-07-14 thomas h->prefixlen = 0; /* any address */
1091 56324ebe 2022-07-14 thomas h->prefixlen = -1; /* host address */
1092 56324ebe 2022-07-14 thomas freeaddrinfo(res);
1095 56324ebe 2022-07-14 thomas return (h);
1099 56324ebe 2022-07-14 thomas host_dns(const char *s, struct addresslist *al, int max,
1100 56324ebe 2022-07-14 thomas in_port_t port, const char *ifname, int ipproto)
1102 56324ebe 2022-07-14 thomas struct addrinfo hints, *res0, *res;
1103 56324ebe 2022-07-14 thomas int error, cnt = 0;
1104 56324ebe 2022-07-14 thomas struct sockaddr_in *sain;
1105 56324ebe 2022-07-14 thomas struct sockaddr_in6 *sin6;
1106 56324ebe 2022-07-14 thomas struct address *h;
1108 56324ebe 2022-07-14 thomas if ((cnt = host_if(s, al, max, port, ifname, ipproto)) != 0)
1109 56324ebe 2022-07-14 thomas return (cnt);
1111 56324ebe 2022-07-14 thomas memset(&hints, 0, sizeof(hints));
1112 56324ebe 2022-07-14 thomas hints.ai_family = PF_UNSPEC;
1113 56324ebe 2022-07-14 thomas hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1114 56324ebe 2022-07-14 thomas hints.ai_flags = AI_ADDRCONFIG;
1115 56324ebe 2022-07-14 thomas error = getaddrinfo(s, NULL, &hints, &res0);
1116 56324ebe 2022-07-14 thomas if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1117 56324ebe 2022-07-14 thomas return (0);
1118 56324ebe 2022-07-14 thomas if (error) {
1119 56324ebe 2022-07-14 thomas log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1120 56324ebe 2022-07-14 thomas gai_strerror(error));
1121 56324ebe 2022-07-14 thomas return (-1);
1124 56324ebe 2022-07-14 thomas for (res = res0; res && cnt < max; res = res->ai_next) {
1125 56324ebe 2022-07-14 thomas if (res->ai_family != AF_INET &&
1126 56324ebe 2022-07-14 thomas res->ai_family != AF_INET6)
1127 56324ebe 2022-07-14 thomas continue;
1128 56324ebe 2022-07-14 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1129 56324ebe 2022-07-14 thomas fatal(__func__);
1131 56324ebe 2022-07-14 thomas if (port)
1132 56324ebe 2022-07-14 thomas h->port = port;
1133 56324ebe 2022-07-14 thomas if (ifname != NULL) {
1134 56324ebe 2022-07-14 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1135 56324ebe 2022-07-14 thomas sizeof(h->ifname)) {
1136 56324ebe 2022-07-14 thomas log_warnx("%s: interface name truncated",
1137 56324ebe 2022-07-14 thomas __func__);
1138 56324ebe 2022-07-14 thomas freeaddrinfo(res0);
1139 56324ebe 2022-07-14 thomas free(h);
1140 56324ebe 2022-07-14 thomas return (-1);
1143 56324ebe 2022-07-14 thomas if (ipproto != -1)
1144 56324ebe 2022-07-14 thomas h->ipproto = ipproto;
1145 56324ebe 2022-07-14 thomas h->ss.ss_family = res->ai_family;
1146 56324ebe 2022-07-14 thomas h->prefixlen = -1; /* host address */
1148 56324ebe 2022-07-14 thomas if (res->ai_family == AF_INET) {
1149 56324ebe 2022-07-14 thomas sain = (struct sockaddr_in *)&h->ss;
1150 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1151 d7cad54e 2022-07-14 thomas #ifndef __linux__
1152 56324ebe 2022-07-14 thomas sain->sin_len = sizeof(struct sockaddr_in);
1154 56324ebe 2022-07-14 thomas sain->sin_addr.s_addr = ((struct sockaddr_in *)
1155 56324ebe 2022-07-14 thomas res->ai_addr)->sin_addr.s_addr;
1156 56324ebe 2022-07-14 thomas } else {
1157 56324ebe 2022-07-14 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1158 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1159 d7cad54e 2022-07-14 thomas #ifndef __linux__
1160 56324ebe 2022-07-14 thomas sin6->sin6_len = sizeof(struct sockaddr_in6);
1162 56324ebe 2022-07-14 thomas memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
1163 56324ebe 2022-07-14 thomas res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
1166 56324ebe 2022-07-14 thomas TAILQ_INSERT_HEAD(al, h, entry);
1169 56324ebe 2022-07-14 thomas if (cnt == max && res) {
1170 56324ebe 2022-07-14 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1171 56324ebe 2022-07-14 thomas s, max);
1173 56324ebe 2022-07-14 thomas freeaddrinfo(res0);
1174 56324ebe 2022-07-14 thomas return (cnt);
1178 56324ebe 2022-07-14 thomas host_if(const char *s, struct addresslist *al, int max,
1179 56324ebe 2022-07-14 thomas in_port_t port, const char *ifname, int ipproto)
1181 56324ebe 2022-07-14 thomas struct ifaddrs *ifap, *p;
1182 56324ebe 2022-07-14 thomas struct sockaddr_in *sain;
1183 56324ebe 2022-07-14 thomas struct sockaddr_in6 *sin6;
1184 56324ebe 2022-07-14 thomas struct address *h;
1185 56324ebe 2022-07-14 thomas int cnt = 0, af;
1187 56324ebe 2022-07-14 thomas if (getifaddrs(&ifap) == -1)
1188 56324ebe 2022-07-14 thomas fatal("getifaddrs");
1190 56324ebe 2022-07-14 thomas /* First search for IPv4 addresses */
1191 56324ebe 2022-07-14 thomas af = AF_INET;
1194 56324ebe 2022-07-14 thomas for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1195 56324ebe 2022-07-14 thomas if (p->ifa_addr == NULL ||
1196 56324ebe 2022-07-14 thomas p->ifa_addr->sa_family != af ||
1197 56324ebe 2022-07-14 thomas (strcmp(s, p->ifa_name) != 0 &&
1198 56324ebe 2022-07-14 thomas !is_if_in_group(p->ifa_name, s)))
1199 56324ebe 2022-07-14 thomas continue;
1200 56324ebe 2022-07-14 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1201 56324ebe 2022-07-14 thomas fatal("calloc");
1203 56324ebe 2022-07-14 thomas if (port)
1204 56324ebe 2022-07-14 thomas h->port = port;
1205 56324ebe 2022-07-14 thomas if (ifname != NULL) {
1206 56324ebe 2022-07-14 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1207 56324ebe 2022-07-14 thomas sizeof(h->ifname)) {
1208 56324ebe 2022-07-14 thomas log_warnx("%s: interface name truncated",
1209 56324ebe 2022-07-14 thomas __func__);
1210 56324ebe 2022-07-14 thomas free(h);
1211 56324ebe 2022-07-14 thomas freeifaddrs(ifap);
1212 56324ebe 2022-07-14 thomas return (-1);
1215 56324ebe 2022-07-14 thomas if (ipproto != -1)
1216 56324ebe 2022-07-14 thomas h->ipproto = ipproto;
1217 56324ebe 2022-07-14 thomas h->ss.ss_family = af;
1218 56324ebe 2022-07-14 thomas h->prefixlen = -1; /* host address */
1220 56324ebe 2022-07-14 thomas if (af == AF_INET) {
1221 56324ebe 2022-07-14 thomas sain = (struct sockaddr_in *)&h->ss;
1222 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1223 d7cad54e 2022-07-14 thomas #ifndef __linux__
1224 56324ebe 2022-07-14 thomas sain->sin_len = sizeof(struct sockaddr_in);
1226 56324ebe 2022-07-14 thomas sain->sin_addr.s_addr = ((struct sockaddr_in *)
1227 56324ebe 2022-07-14 thomas p->ifa_addr)->sin_addr.s_addr;
1228 56324ebe 2022-07-14 thomas } else {
1229 56324ebe 2022-07-14 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1230 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1231 d7cad54e 2022-07-14 thomas #ifndef __linux__
1232 56324ebe 2022-07-14 thomas sin6->sin6_len = sizeof(struct sockaddr_in6);
1234 56324ebe 2022-07-14 thomas memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
1235 56324ebe 2022-07-14 thomas p->ifa_addr)->sin6_addr, sizeof(struct in6_addr));
1236 56324ebe 2022-07-14 thomas sin6->sin6_scope_id = ((struct sockaddr_in6 *)
1237 56324ebe 2022-07-14 thomas p->ifa_addr)->sin6_scope_id;
1240 56324ebe 2022-07-14 thomas TAILQ_INSERT_HEAD(al, h, entry);
1243 56324ebe 2022-07-14 thomas if (af == AF_INET) {
1244 56324ebe 2022-07-14 thomas /* Next search for IPv6 addresses */
1245 56324ebe 2022-07-14 thomas af = AF_INET6;
1246 56324ebe 2022-07-14 thomas goto nextaf;
1249 56324ebe 2022-07-14 thomas if (cnt > max) {
1250 56324ebe 2022-07-14 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1251 56324ebe 2022-07-14 thomas s, max);
1253 56324ebe 2022-07-14 thomas freeifaddrs(ifap);
1254 56324ebe 2022-07-14 thomas return (cnt);
1258 56324ebe 2022-07-14 thomas host(const char *s, struct addresslist *al, int max,
1259 56324ebe 2022-07-14 thomas in_port_t port, const char *ifname, int ipproto)
1261 56324ebe 2022-07-14 thomas struct address *h;
1263 56324ebe 2022-07-14 thomas h = host_v4(s);
1265 56324ebe 2022-07-14 thomas /* IPv6 address? */
1266 56324ebe 2022-07-14 thomas if (h == NULL)
1267 56324ebe 2022-07-14 thomas h = host_v6(s);
1269 56324ebe 2022-07-14 thomas if (h != NULL) {
1270 56324ebe 2022-07-14 thomas if (port)
1271 56324ebe 2022-07-14 thomas h->port = port;
1272 56324ebe 2022-07-14 thomas if (ifname != NULL) {
1273 56324ebe 2022-07-14 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1274 56324ebe 2022-07-14 thomas sizeof(h->ifname)) {
1275 56324ebe 2022-07-14 thomas log_warnx("%s: interface name truncated",
1276 56324ebe 2022-07-14 thomas __func__);
1277 56324ebe 2022-07-14 thomas free(h);
1278 56324ebe 2022-07-14 thomas return (-1);
1281 56324ebe 2022-07-14 thomas if (ipproto != -1)
1282 56324ebe 2022-07-14 thomas h->ipproto = ipproto;
1284 56324ebe 2022-07-14 thomas TAILQ_INSERT_HEAD(al, h, entry);
1285 56324ebe 2022-07-14 thomas return (1);
1288 56324ebe 2022-07-14 thomas return (host_dns(s, al, max, port, ifname, ipproto));
1292 56324ebe 2022-07-14 thomas is_if_in_group(const char *ifname, const char *groupname)
1294 d7cad54e 2022-07-14 thomas /* TA: Check this... */
1295 d7cad54e 2022-07-14 thomas #ifdef HAVE_STRUCT_IFGROUPREQ
1296 56324ebe 2022-07-14 thomas unsigned int len;
1297 56324ebe 2022-07-14 thomas struct ifgroupreq ifgr;
1298 56324ebe 2022-07-14 thomas struct ifg_req *ifg;
1300 56324ebe 2022-07-14 thomas int ret = 0;
1302 56324ebe 2022-07-14 thomas if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1303 56324ebe 2022-07-14 thomas err(1, "socket");
1305 56324ebe 2022-07-14 thomas memset(&ifgr, 0, sizeof(ifgr));
1306 56324ebe 2022-07-14 thomas if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1307 56324ebe 2022-07-14 thomas err(1, "IFNAMSIZ");
1308 56324ebe 2022-07-14 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1309 56324ebe 2022-07-14 thomas if (errno == EINVAL || errno == ENOTTY)
1310 56324ebe 2022-07-14 thomas goto end;
1311 56324ebe 2022-07-14 thomas err(1, "SIOCGIFGROUP");
1314 56324ebe 2022-07-14 thomas len = ifgr.ifgr_len;
1315 56324ebe 2022-07-14 thomas ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1316 56324ebe 2022-07-14 thomas sizeof(struct ifg_req));
1317 56324ebe 2022-07-14 thomas if (ifgr.ifgr_groups == NULL)
1318 56324ebe 2022-07-14 thomas err(1, "getifgroups");
1319 56324ebe 2022-07-14 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1320 56324ebe 2022-07-14 thomas err(1, "SIOCGIFGROUP");
1322 56324ebe 2022-07-14 thomas ifg = ifgr.ifgr_groups;
1323 56324ebe 2022-07-14 thomas for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1324 56324ebe 2022-07-14 thomas len -= sizeof(struct ifg_req);
1325 56324ebe 2022-07-14 thomas if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1326 56324ebe 2022-07-14 thomas ret = 1;
1330 56324ebe 2022-07-14 thomas free(ifgr.ifgr_groups);
1333 56324ebe 2022-07-14 thomas close(s);
1334 56324ebe 2022-07-14 thomas return (ret);
1336 d7cad54e 2022-07-14 thomas return (0);
1341 56324ebe 2022-07-14 thomas get_addrs(const char *addr, struct addresslist *al, in_port_t port)
1343 56324ebe 2022-07-14 thomas if (strcmp("", addr) == 0) {
1344 56324ebe 2022-07-14 thomas if (host("0.0.0.0", al, 1, port, "0.0.0.0", -1) <= 0) {
1345 56324ebe 2022-07-14 thomas yyerror("invalid listen ip: %s",
1346 56324ebe 2022-07-14 thomas "0.0.0.0");
1347 56324ebe 2022-07-14 thomas return (-1);
1349 56324ebe 2022-07-14 thomas if (host("::", al, 1, port, "::", -1) <= 0) {
1350 56324ebe 2022-07-14 thomas yyerror("invalid listen ip: %s", "::");
1351 56324ebe 2022-07-14 thomas return (-1);
1353 56324ebe 2022-07-14 thomas } else {
1354 56324ebe 2022-07-14 thomas if (host(addr, al, GOTWEBD_MAXIFACE, port, addr,
1355 56324ebe 2022-07-14 thomas -1) <= 0) {
1356 56324ebe 2022-07-14 thomas yyerror("invalid listen ip: %s", addr);
1357 56324ebe 2022-07-14 thomas return (-1);
1360 56324ebe 2022-07-14 thomas return (0);