2 * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <sys/queue.h>
27 #include <sys/socket.h>
31 #include <netinet/in.h>
33 #include <arpa/inet.h>
50 #include "got_sockaddr.h"
51 #include "got_reference.h"
56 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
58 TAILQ_ENTRY(file) entry;
64 struct file *newfile(const char *, int);
65 static void closefile(struct file *);
66 int check_file_secrecy(int, const char *);
69 int yyerror(const char *, ...)
70 __attribute__((__format__ (printf, 1, 2)))
71 __attribute__((__nonnull__ (1)));
72 int kw_cmp(const void *, const void *);
78 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
80 TAILQ_ENTRY(sym) entry;
87 int symset(const char *, const char *, int);
88 char *symget(const char *);
92 static struct gotwebd *gotwebd;
93 static struct server *new_srv;
94 static struct server *conf_new_server(const char *);
95 int getservice(const char *);
98 int get_addrs(const char *, struct server *, in_port_t);
99 int addr_dup_check(struct addresslist *, struct address *,
100 const char *, const char *);
101 int add_addr(struct server *, struct address *);
102 int host(const char *, struct server *,
103 int, in_port_t, const char *, int);
104 int host_if(const char *, struct server *,
105 int, in_port_t, const char *, int);
106 int is_if_in_group(const char *, const char *);
119 %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
120 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
121 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
122 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
123 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
125 %token <v.string> STRING
126 %type <v.port> fcgiport
127 %token <v.number> NUMBER
128 %type <v.number> boolean
132 grammar : /* empty */
134 | grammar varset '\n'
136 | grammar server '\n'
137 | grammar error '\n' { file->errors++; }
140 varset : STRING '=' STRING {
143 if (isspace((unsigned char)*s)) {
144 yyerror("macro name cannot contain "
151 if (symset($1, $3, 0) == -1)
152 fatal("cannot store variable");
159 if (strcasecmp($1, "1") == 0 ||
160 strcasecmp($1, "on") == 0)
162 else if (strcasecmp($1, "0") == 0 ||
163 strcasecmp($1, "off") == 0)
166 yyerror("invalid boolean value '%s'", $1);
174 if ($1 != 0 && $1 != 1) {
175 yyerror("invalid boolean value '%lld'", $1);
182 fcgiport : PORT NUMBER {
183 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
184 yyerror("invalid port: %lld", $2);
192 if ((val = getservice($2)) == -1) {
193 yyerror("invalid port: %s", $2);
203 main : PREFORK NUMBER {
204 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
205 yyerror("prefork is %s: %lld",
206 $2 <= 0 ? "too small" : "too large", $2);
209 gotwebd->prefork_gotwebd = $2;
213 yyerror("chroot path can't be an empty"
219 n = strlcpy(gotwebd->httpd_chroot, $2,
220 sizeof(gotwebd->httpd_chroot));
221 if (n >= sizeof(gotwebd->httpd_chroot)) {
222 yyerror("%s: httpd_chroot truncated", __func__);
228 | UNIX_SOCKET boolean {
229 gotwebd->unix_socket = $2;
231 | UNIX_SOCKET_NAME STRING {
232 n = snprintf(gotwebd->unix_socket_name,
233 sizeof(gotwebd->unix_socket_name), "%s%s",
234 gotwebd->httpd_chroot, $2);
236 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
237 yyerror("%s: unix_socket_name truncated",
246 server : SERVER STRING {
249 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
250 if (strcmp(srv->name, $2) == 0) {
251 yyerror("server name exists '%s'", $2);
257 new_srv = conf_new_server($2);
258 log_debug("adding server %s", $2);
264 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
265 if (strcmp(srv->name, $2) == 0) {
266 yyerror("server name exists '%s'", $2);
272 new_srv = conf_new_server($2);
273 log_debug("adding server %s", $2);
275 } '{' optnl serveropts2 '}' {
279 serveropts1 : REPOS_PATH STRING {
280 n = strlcpy(new_srv->repos_path, $2,
281 sizeof(new_srv->repos_path));
282 if (n >= sizeof(new_srv->repos_path)) {
283 yyerror("%s: repos_path truncated", __func__);
290 n = strlcpy(new_srv->site_name, $2,
291 sizeof(new_srv->site_name));
292 if (n >= sizeof(new_srv->site_name)) {
293 yyerror("%s: site_name truncated", __func__);
299 | SITE_OWNER STRING {
300 n = strlcpy(new_srv->site_owner, $2,
301 sizeof(new_srv->site_owner));
302 if (n >= sizeof(new_srv->site_owner)) {
303 yyerror("%s: site_owner truncated", __func__);
310 n = strlcpy(new_srv->site_link, $2,
311 sizeof(new_srv->site_link));
312 if (n >= sizeof(new_srv->site_link)) {
313 yyerror("%s: site_link truncated", __func__);
320 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
321 if (n >= sizeof(new_srv->logo)) {
322 yyerror("%s: logo truncated", __func__);
329 n = strlcpy(new_srv->logo_url, $2,
330 sizeof(new_srv->logo_url));
331 if (n >= sizeof(new_srv->logo_url)) {
332 yyerror("%s: logo_url truncated", __func__);
338 | CUSTOM_CSS STRING {
339 n = strlcpy(new_srv->custom_css, $2,
340 sizeof(new_srv->custom_css));
341 if (n >= sizeof(new_srv->custom_css)) {
342 yyerror("%s: custom_css truncated", __func__);
348 | LISTEN ON STRING fcgiport {
349 if (get_addrs($3, new_srv, $4) == -1) {
350 yyerror("could not get addrs");
353 new_srv->fcgi_socket = 1;
355 | LISTEN ON SOCKET STRING {
356 if (strcasecmp($4, "off") == 0) {
357 new_srv->unix_socket = 0;
362 new_srv->unix_socket = 1;
364 n = snprintf(new_srv->unix_socket_name,
365 sizeof(new_srv->unix_socket_name), "%s%s",
366 gotwebd->httpd_chroot, $4);
368 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
369 yyerror("%s: unix_socket_name truncated",
378 yyerror("max_repos is too small: %lld", $2);
381 new_srv->max_repos = $2;
383 | SHOW_SITE_OWNER boolean {
384 new_srv->show_site_owner = $2;
386 | SHOW_REPO_OWNER boolean {
387 new_srv->show_repo_owner = $2;
389 | SHOW_REPO_AGE boolean {
390 new_srv->show_repo_age = $2;
392 | SHOW_REPO_DESCRIPTION boolean {
393 new_srv->show_repo_description = $2;
395 | SHOW_REPO_CLONEURL boolean {
396 new_srv->show_repo_cloneurl = $2;
398 | RESPECT_EXPORTOK boolean {
399 new_srv->respect_exportok = $2;
401 | MAX_REPOS_DISPLAY NUMBER {
403 yyerror("max_repos_display is too small: %lld",
407 new_srv->max_repos_display = $2;
409 | MAX_COMMITS_DISPLAY NUMBER {
411 yyerror("max_commits_display is too small:"
415 new_srv->max_commits_display = $2;
419 serveropts2 : serveropts2 serveropts1 nl
426 optnl : '\n' optnl /* zero or more newlines */
438 yyerror(const char *fmt, ...)
445 if (vasprintf(&msg, fmt, ap) == -1)
446 fatalx("yyerror vasprintf");
448 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
454 kw_cmp(const void *k, const void *e)
456 return (strcmp(k, ((const struct keywords *)e)->k_name));
462 /* This has to be sorted always. */
463 static const struct keywords keywords[] = {
464 { "chroot", CHROOT },
465 { "custom_css", CUSTOM_CSS },
466 { "listen", LISTEN },
468 { "logo_url", LOGO_URL },
469 { "max_commits_display", MAX_COMMITS_DISPLAY },
470 { "max_repos", MAX_REPOS },
471 { "max_repos_display", MAX_REPOS_DISPLAY },
474 { "prefork", PREFORK },
475 { "repos_path", REPOS_PATH },
476 { "respect_exportok", RESPECT_EXPORTOK },
477 { "server", SERVER },
478 { "show_repo_age", SHOW_REPO_AGE },
479 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
480 { "show_repo_description", SHOW_REPO_DESCRIPTION },
481 { "show_repo_owner", SHOW_REPO_OWNER },
482 { "show_site_owner", SHOW_SITE_OWNER },
483 { "site_link", SITE_LINK },
484 { "site_name", SITE_NAME },
485 { "site_owner", SITE_OWNER },
486 { "socket", SOCKET },
487 { "unix_socket", UNIX_SOCKET },
488 { "unix_socket_name", UNIX_SOCKET_NAME },
490 const struct keywords *p;
492 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
493 sizeof(keywords[0]), kw_cmp);
501 #define MAXPUSHBACK 128
503 unsigned char *parsebuf;
505 unsigned char pushback_buffer[MAXPUSHBACK];
506 int pushback_index = 0;
514 /* Read character from the parsebuffer instead of input. */
515 if (parseindex >= 0) {
516 c = parsebuf[parseindex++];
525 return (pushback_buffer[--pushback_index]);
528 c = getc(file->stream);
530 yyerror("reached end of file while parsing "
535 c = getc(file->stream);
537 next = getc(file->stream);
542 yylval.lineno = file->lineno;
544 c = getc(file->stream);
560 if (pushback_index < MAXPUSHBACK-1)
561 return (pushback_buffer[pushback_index++] = c);
573 /* Skip to either EOF or the first real EOL. */
576 c = pushback_buffer[--pushback_index];
592 unsigned char buf[8096];
593 unsigned char *p, *val;
600 while (c == ' ' || c == '\t')
601 c = lgetc(0); /* nothing */
603 yylval.lineno = file->lineno;
606 while (c != '\n' && c != EOF)
607 c = lgetc(0); /* nothing */
609 if (c == '$' && parsebuf == NULL) {
615 if (p + 1 >= buf + sizeof(buf) - 1) {
616 yyerror("string too long");
619 if (isalnum(c) || c == '_') {
629 yyerror("macro '%s' not defined", buf);
648 } else if (c == '\\') {
649 next = lgetc(quotec);
652 if (next == quotec || c == ' ' || c == '\t')
654 else if (next == '\n') {
659 } else if (c == quotec) {
662 } else if (c == '\0') {
663 yyerror("syntax error");
666 if (p + 1 >= buf + sizeof(buf) - 1) {
667 yyerror("string too long");
672 yylval.v.string = strdup(buf);
673 if (yylval.v.string == NULL)
674 err(1, "yylex: strdup");
678 #define allowed_to_end_number(x) \
679 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
681 if (c == '-' || isdigit(c)) {
684 if ((unsigned)(p-buf) >= sizeof(buf)) {
685 yyerror("string too long");
689 } while (c != EOF && isdigit(c));
691 if (p == buf + 1 && buf[0] == '-')
693 if (c == EOF || allowed_to_end_number(c)) {
694 const char *errstr = NULL;
697 yylval.v.number = strtonum(buf, LLONG_MIN,
700 yyerror("\"%s\" invalid number: %s",
715 #define allowed_in_string(x) \
716 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
717 x != '{' && x != '}' && \
718 x != '!' && x != '=' && x != '#' && \
721 if (isalnum(c) || c == ':' || c == '_') {
724 if ((unsigned)(p-buf) >= sizeof(buf)) {
725 yyerror("string too long");
729 } while (c != EOF && (allowed_in_string(c)));
733 if (token == STRING) {
734 yylval.v.string = strdup(buf);
735 if (yylval.v.string == NULL)
736 err(1, "yylex: strdup");
741 yylval.lineno = file->lineno;
750 check_file_secrecy(int fd, const char *fname)
754 if (fstat(fd, &st)) {
755 log_warn("cannot stat %s", fname);
758 if (st.st_uid != 0 && st.st_uid != getuid()) {
759 log_warnx("%s: owner not root or current user", fname);
762 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
763 log_warnx("%s: group writable or world read/writable", fname);
770 newfile(const char *name, int secret)
774 nfile = calloc(1, sizeof(struct file));
779 nfile->name = strdup(name);
780 if (nfile->name == NULL) {
785 nfile->stream = fopen(nfile->name, "r");
786 if (nfile->stream == NULL) {
787 /* no warning, we don't require a conf file */
792 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
793 fclose(nfile->stream);
803 closefile(struct file *xfile)
805 fclose(xfile->stream);
811 add_default_server(void)
813 new_srv = conf_new_server(D_SITENAME);
814 log_debug("%s: adding default server %s", __func__, D_SITENAME);
818 parse_config(const char *filename, struct gotwebd *env)
820 struct sym *sym, *next;
822 if (config_init(env) == -1)
823 fatalx("failed to initialize configuration");
827 file = newfile(filename, 0);
829 add_default_server();
830 sockets_parse_sockets(env);
831 /* just return, as we don't require a conf file */
836 errors = file->errors;
839 /* Free macros and check which have not been used. */
840 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
841 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
842 fprintf(stderr, "warning: macro '%s' not used\n",
847 TAILQ_REMOVE(&symhead, sym, entry);
855 /* just add default server if no config specified */
856 if (gotwebd->server_cnt == 0)
857 add_default_server();
859 /* setup our listening sockets */
860 sockets_parse_sockets(env);
866 conf_new_server(const char *name)
868 struct server *srv = NULL;
870 srv = calloc(1, sizeof(*srv));
872 fatalx("%s: calloc", __func__);
874 n = strlcpy(srv->name, name, sizeof(srv->name));
875 if (n >= sizeof(srv->name))
876 fatalx("%s: strlcpy", __func__);
877 n = snprintf(srv->unix_socket_name,
878 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
880 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
881 fatalx("%s: snprintf", __func__);
882 n = strlcpy(srv->repos_path, D_GOTPATH,
883 sizeof(srv->repos_path));
884 if (n >= sizeof(srv->repos_path))
885 fatalx("%s: strlcpy", __func__);
886 n = strlcpy(srv->site_name, D_SITENAME,
887 sizeof(srv->site_name));
888 if (n >= sizeof(srv->site_name))
889 fatalx("%s: strlcpy", __func__);
890 n = strlcpy(srv->site_owner, D_SITEOWNER,
891 sizeof(srv->site_owner));
892 if (n >= sizeof(srv->site_owner))
893 fatalx("%s: strlcpy", __func__);
894 n = strlcpy(srv->site_link, D_SITELINK,
895 sizeof(srv->site_link));
896 if (n >= sizeof(srv->site_link))
897 fatalx("%s: strlcpy", __func__);
898 n = strlcpy(srv->logo, D_GOTLOGO,
900 if (n >= sizeof(srv->logo))
901 fatalx("%s: strlcpy", __func__);
902 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
903 if (n >= sizeof(srv->logo_url))
904 fatalx("%s: strlcpy", __func__);
905 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
906 if (n >= sizeof(srv->custom_css))
907 fatalx("%s: strlcpy", __func__);
909 srv->show_site_owner = D_SHOWSOWNER;
910 srv->show_repo_owner = D_SHOWROWNER;
911 srv->show_repo_age = D_SHOWAGE;
912 srv->show_repo_description = D_SHOWDESC;
913 srv->show_repo_cloneurl = D_SHOWURL;
914 srv->respect_exportok = D_RESPECTEXPORTOK;
916 srv->max_repos_display = D_MAXREPODISP;
917 srv->max_commits_display = D_MAXCOMMITDISP;
918 srv->max_repos = D_MAXREPO;
920 srv->unix_socket = 1;
921 srv->fcgi_socket = 0;
923 TAILQ_INIT(&srv->al);
924 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
925 gotwebd->server_cnt++;
931 symset(const char *nam, const char *val, int persist)
935 TAILQ_FOREACH(sym, &symhead, entry) {
936 if (strcmp(nam, sym->nam) == 0)
941 if (sym->persist == 1)
946 TAILQ_REMOVE(&symhead, sym, entry);
950 sym = calloc(1, sizeof(*sym));
954 sym->nam = strdup(nam);
955 if (sym->nam == NULL) {
959 sym->val = strdup(val);
960 if (sym->val == NULL) {
966 sym->persist = persist;
967 TAILQ_INSERT_TAIL(&symhead, sym, entry);
972 cmdline_symset(char *s)
977 val = strrchr(s, '=');
981 sym = strndup(s, val - s);
983 fatal("%s: strndup", __func__);
985 ret = symset(sym, val + 1, 1);
992 symget(const char *nam)
996 TAILQ_FOREACH(sym, &symhead, entry) {
997 if (strcmp(nam, sym->nam) == 0) {
1006 getservice(const char *n)
1012 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1014 s = getservbyname(n, "tcp");
1016 s = getservbyname(n, "udp");
1019 return ntohs(s->s_port);
1022 return (unsigned short)llval;
1026 host(const char *s, struct server *new_srv, int max,
1027 in_port_t port, const char *ifname, int ipproto)
1029 struct addrinfo hints, *res0, *res;
1031 struct sockaddr_in *sain;
1032 struct sockaddr_in6 *sin6;
1035 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1038 memset(&hints, 0, sizeof(hints));
1039 hints.ai_family = AF_UNSPEC;
1040 hints.ai_socktype = SOCK_STREAM; /* DUMMY */
1041 hints.ai_flags = AI_ADDRCONFIG;
1042 error = getaddrinfo(s, NULL, &hints, &res0);
1043 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1046 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1047 gai_strerror(error));
1051 for (res = res0; res && cnt < max; res = res->ai_next) {
1052 if (res->ai_family != AF_INET &&
1053 res->ai_family != AF_INET6)
1055 if ((h = calloc(1, sizeof(*h))) == NULL)
1060 if (ifname != NULL) {
1061 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1062 sizeof(h->ifname)) {
1063 log_warnx("%s: interface name truncated",
1071 h->ipproto = ipproto;
1072 h->ss.ss_family = res->ai_family;
1074 if (res->ai_family == AF_INET) {
1075 struct sockaddr_in *ra;
1076 sain = (struct sockaddr_in *)&h->ss;
1077 ra = (struct sockaddr_in *)res->ai_addr;
1078 got_sockaddr_inet_init(sain, &ra->sin_addr);
1080 struct sockaddr_in6 *ra;
1081 sin6 = (struct sockaddr_in6 *)&h->ss;
1082 ra = (struct sockaddr_in6 *)res->ai_addr;
1083 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1086 if (add_addr(new_srv, h))
1090 if (cnt == max && res) {
1091 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1099 host_if(const char *s, struct server *new_srv, int max,
1100 in_port_t port, const char *ifname, int ipproto)
1102 struct ifaddrs *ifap, *p;
1103 struct sockaddr_in *sain;
1104 struct sockaddr_in6 *sin6;
1108 if (getifaddrs(&ifap) == -1)
1109 fatal("getifaddrs");
1111 /* First search for IPv4 addresses */
1115 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1116 if (p->ifa_addr == NULL ||
1117 p->ifa_addr->sa_family != af ||
1118 (strcmp(s, p->ifa_name) != 0 &&
1119 !is_if_in_group(p->ifa_name, s)))
1121 if ((h = calloc(1, sizeof(*h))) == NULL)
1126 if (ifname != NULL) {
1127 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1128 sizeof(h->ifname)) {
1129 log_warnx("%s: interface name truncated",
1137 h->ipproto = ipproto;
1138 h->ss.ss_family = af;
1140 if (af == AF_INET) {
1141 struct sockaddr_in *ra;
1142 sain = (struct sockaddr_in *)&h->ss;
1143 ra = (struct sockaddr_in *)p->ifa_addr;
1144 got_sockaddr_inet_init(sain, &ra->sin_addr);
1146 struct sockaddr_in6 *ra;
1147 sin6 = (struct sockaddr_in6 *)&h->ss;
1148 ra = (struct sockaddr_in6 *)p->ifa_addr;
1149 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1153 if (add_addr(new_srv, h))
1157 if (af == AF_INET) {
1158 /* Next search for IPv6 addresses */
1164 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1172 is_if_in_group(const char *ifname, const char *groupname)
1175 struct ifgroupreq ifgr;
1176 struct ifg_req *ifg;
1180 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1183 memset(&ifgr, 0, sizeof(ifgr));
1184 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1186 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1187 if (errno == EINVAL || errno == ENOTTY)
1189 err(1, "SIOCGIFGROUP");
1192 len = ifgr.ifgr_len;
1193 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1194 sizeof(struct ifg_req));
1195 if (ifgr.ifgr_groups == NULL)
1196 err(1, "getifgroups");
1197 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1198 err(1, "SIOCGIFGROUP");
1200 ifg = ifgr.ifgr_groups;
1201 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1202 len -= sizeof(struct ifg_req);
1203 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1208 free(ifgr.ifgr_groups);
1216 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1218 if (strcmp("", addr) == 0) {
1219 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1221 yyerror("invalid listen ip: %s",
1225 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1226 yyerror("invalid listen ip: %s", "::1");
1230 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1232 yyerror("invalid listen ip: %s", addr);
1240 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1241 const char *other_srv)
1245 char buf[INET6_ADDRSTRLEN];
1246 const char *addrstr;
1248 TAILQ_FOREACH(a, al, entry) {
1249 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1253 switch (h->ss.ss_family) {
1255 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1258 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1261 yyerror("unknown address family: %d", h->ss.ss_family);
1264 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1267 yyerror("server %s: duplicate fcgi listen "
1268 "address %s:%d, already used by server %s",
1269 new_srv, addrstr, h->port, other_srv);
1271 log_warnx("server: %s: duplicate fcgi listen "
1272 "address %s:%d", new_srv, addrstr, h->port);
1276 yyerror("server: %s: duplicate fcgi listen "
1277 "address, already used by server %s",
1278 new_srv, other_srv);
1280 log_warnx("server %s: duplicate fcgi listen "
1281 "address", new_srv);
1292 add_addr(struct server *new_srv, struct address *h)
1296 /* Address cannot be shared between different servers. */
1297 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1300 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1304 /* Tolerate duplicate address lines within the scope of a server. */
1305 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1306 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);