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>
52 #include "got_sockaddr.h"
54 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
56 TAILQ_ENTRY(file) entry;
62 struct file *newfile(const char *, int);
63 static void closefile(struct file *);
64 int check_file_secrecy(int, const char *);
67 int yyerror(const char *, ...)
68 __attribute__((__format__ (printf, 1, 2)))
69 __attribute__((__nonnull__ (1)));
70 int kw_cmp(const void *, const void *);
76 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
78 TAILQ_ENTRY(sym) entry;
85 int symset(const char *, const char *, int);
86 char *symget(const char *);
90 static struct gotwebd *gotwebd;
91 static struct server *new_srv;
92 static struct server *conf_new_server(const char *);
93 int getservice(const char *);
96 int get_addrs(const char *, struct server *, in_port_t);
97 int addr_dup_check(struct addresslist *, struct address *,
98 const char *, const char *);
99 int add_addr(struct server *, struct address *);
100 struct address *host_v4(const char *);
101 struct address *host_v6(const char *);
102 int host_dns(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 host(const char *, struct server *,
107 int, in_port_t, const char *, int);
108 int is_if_in_group(const char *, const char *);
121 %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
122 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
123 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
124 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK
125 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
127 %token <v.string> STRING
128 %type <v.port> fcgiport
129 %token <v.number> NUMBER
130 %type <v.number> boolean
134 grammar : /* empty */
136 | grammar varset '\n'
138 | grammar server '\n'
139 | grammar error '\n' { file->errors++; }
142 varset : STRING '=' STRING {
145 if (isspace((unsigned char)*s)) {
146 yyerror("macro name cannot contain "
153 if (symset($1, $3, 0) == -1)
154 fatal("cannot store variable");
161 if (strcasecmp($1, "1") == 0 ||
162 strcasecmp($1, "yes") == 0 ||
163 strcasecmp($1, "on") == 0)
165 else if (strcasecmp($1, "0") == 0 ||
166 strcasecmp($1, "off") == 0 ||
167 strcasecmp($1, "no") == 0)
170 yyerror("invalid boolean value '%s'", $1);
177 | NUMBER { $$ = $1; }
180 fcgiport : PORT NUMBER {
181 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
182 yyerror("invalid port: %lld", $2);
190 if ((val = getservice($2)) == -1) {
191 yyerror("invalid port: %s", $2);
201 main : PREFORK NUMBER {
202 gotwebd->prefork_gotwebd = $2;
205 n = strlcpy(gotwebd->httpd_chroot, $2,
206 sizeof(gotwebd->httpd_chroot));
207 if (n >= sizeof(gotwebd->httpd_chroot)) {
208 yyerror("%s: httpd_chroot truncated", __func__);
214 | UNIX_SOCKET boolean {
215 gotwebd->unix_socket = $2;
217 | UNIX_SOCKET_NAME STRING {
218 n = snprintf(gotwebd->unix_socket_name,
219 sizeof(gotwebd->unix_socket_name), "%s%s",
220 strlen(gotwebd->httpd_chroot) ?
221 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
223 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
224 yyerror("%s: unix_socket_name truncated",
233 server : SERVER STRING {
236 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
237 if (strcmp(srv->name, $2) == 0) {
238 yyerror("server name exists '%s'", $2);
244 new_srv = conf_new_server($2);
245 log_debug("adding server %s", $2);
251 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
252 if (strcmp(srv->name, $2) == 0) {
253 yyerror("server name exists '%s'", $2);
259 new_srv = conf_new_server($2);
260 log_debug("adding server %s", $2);
262 } '{' optnl serveropts2 '}' {
266 serveropts1 : REPOS_PATH STRING {
267 n = strlcpy(new_srv->repos_path, $2,
268 sizeof(new_srv->repos_path));
269 if (n >= sizeof(new_srv->repos_path)) {
270 yyerror("%s: repos_path truncated", __func__);
277 n = strlcpy(new_srv->site_name, $2,
278 sizeof(new_srv->site_name));
279 if (n >= sizeof(new_srv->site_name)) {
280 yyerror("%s: site_name truncated", __func__);
286 | SITE_OWNER STRING {
287 n = strlcpy(new_srv->site_owner, $2,
288 sizeof(new_srv->site_owner));
289 if (n >= sizeof(new_srv->site_owner)) {
290 yyerror("%s: site_owner truncated", __func__);
297 n = strlcpy(new_srv->site_link, $2,
298 sizeof(new_srv->site_link));
299 if (n >= sizeof(new_srv->site_link)) {
300 yyerror("%s: site_link truncated", __func__);
307 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
308 if (n >= sizeof(new_srv->logo)) {
309 yyerror("%s: logo truncated", __func__);
316 n = strlcpy(new_srv->logo_url, $2,
317 sizeof(new_srv->logo_url));
318 if (n >= sizeof(new_srv->logo_url)) {
319 yyerror("%s: logo_url truncated", __func__);
325 | CUSTOM_CSS STRING {
326 n = strlcpy(new_srv->custom_css, $2,
327 sizeof(new_srv->custom_css));
328 if (n >= sizeof(new_srv->custom_css)) {
329 yyerror("%s: custom_css truncated", __func__);
335 | LISTEN ON STRING fcgiport {
336 if (get_addrs($3, new_srv, $4) == -1) {
337 yyerror("could not get addrs");
340 new_srv->fcgi_socket = 1;
344 new_srv->max_repos = $2;
346 | SHOW_SITE_OWNER boolean {
347 new_srv->show_site_owner = $2;
349 | SHOW_REPO_OWNER boolean {
350 new_srv->show_repo_owner = $2;
352 | SHOW_REPO_AGE boolean {
353 new_srv->show_repo_age = $2;
355 | SHOW_REPO_DESCRIPTION boolean {
356 new_srv->show_repo_description = $2;
358 | SHOW_REPO_CLONEURL boolean {
359 new_srv->show_repo_cloneurl = $2;
361 | MAX_REPOS_DISPLAY NUMBER {
362 new_srv->max_repos_display = $2;
364 | MAX_COMMITS_DISPLAY NUMBER {
366 new_srv->max_commits_display = $2;
368 | UNIX_SOCKET boolean {
369 new_srv->unix_socket = $2;
371 | UNIX_SOCKET_NAME STRING {
372 n = snprintf(new_srv->unix_socket_name,
373 sizeof(new_srv->unix_socket_name), "%s%s",
374 strlen(gotwebd->httpd_chroot) ?
375 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
377 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
378 yyerror("%s: unix_socket_name truncated",
387 serveropts2 : serveropts2 serveropts1 nl
394 optnl : '\n' optnl /* zero or more newlines */
406 yyerror(const char *fmt, ...)
413 if (vasprintf(&msg, fmt, ap) == -1)
414 fatalx("yyerror vasprintf");
416 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
422 kw_cmp(const void *k, const void *e)
424 return (strcmp(k, ((const struct keywords *)e)->k_name));
430 /* This has to be sorted always. */
431 static const struct keywords keywords[] = {
432 { "chroot", CHROOT },
433 { "custom_css", CUSTOM_CSS },
434 { "listen", LISTEN },
436 { "logo_url" , LOGO_URL },
437 { "max_commits_display", MAX_COMMITS_DISPLAY },
438 { "max_repos", MAX_REPOS },
439 { "max_repos_display", MAX_REPOS_DISPLAY },
442 { "prefork", PREFORK },
443 { "repos_path", REPOS_PATH },
444 { "server", SERVER },
445 { "show_repo_age", SHOW_REPO_AGE },
446 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
447 { "show_repo_description", SHOW_REPO_DESCRIPTION },
448 { "show_repo_owner", SHOW_REPO_OWNER },
449 { "show_site_owner", SHOW_SITE_OWNER },
450 { "site_link", SITE_LINK },
451 { "site_name", SITE_NAME },
452 { "site_owner", SITE_OWNER },
453 { "unix_socket", UNIX_SOCKET },
454 { "unix_socket_name", UNIX_SOCKET_NAME },
456 const struct keywords *p;
458 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
459 sizeof(keywords[0]), kw_cmp);
467 #define MAXPUSHBACK 128
469 unsigned char *parsebuf;
471 unsigned char pushback_buffer[MAXPUSHBACK];
472 int pushback_index = 0;
480 /* Read character from the parsebuffer instead of input. */
481 if (parseindex >= 0) {
482 c = parsebuf[parseindex++];
491 return (pushback_buffer[--pushback_index]);
494 c = getc(file->stream);
496 yyerror("reached end of file while parsing "
501 c = getc(file->stream);
503 next = getc(file->stream);
508 yylval.lineno = file->lineno;
510 c = getc(file->stream);
526 if (pushback_index < MAXPUSHBACK-1)
527 return (pushback_buffer[pushback_index++] = c);
539 /* Skip to either EOF or the first real EOL. */
542 c = pushback_buffer[--pushback_index];
558 unsigned char buf[8096];
559 unsigned char *p, *val;
566 while (c == ' ' || c == '\t')
567 c = lgetc(0); /* nothing */
569 yylval.lineno = file->lineno;
572 while (c != '\n' && c != EOF)
573 c = lgetc(0); /* nothing */
575 if (c == '$' && parsebuf == NULL) {
581 if (p + 1 >= buf + sizeof(buf) - 1) {
582 yyerror("string too long");
585 if (isalnum(c) || c == '_') {
595 yyerror("macro '%s' not defined", buf);
614 } else if (c == '\\') {
615 next = lgetc(quotec);
618 if (next == quotec || c == ' ' || c == '\t')
620 else if (next == '\n') {
625 } else if (c == quotec) {
628 } else if (c == '\0') {
629 yyerror("syntax error");
632 if (p + 1 >= buf + sizeof(buf) - 1) {
633 yyerror("string too long");
638 yylval.v.string = strdup(buf);
639 if (yylval.v.string == NULL)
640 err(1, "yylex: strdup");
644 #define allowed_to_end_number(x) \
645 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
647 if (c == '-' || isdigit(c)) {
650 if ((unsigned)(p-buf) >= sizeof(buf)) {
651 yyerror("string too long");
655 } while (c != EOF && isdigit(c));
657 if (p == buf + 1 && buf[0] == '-')
659 if (c == EOF || allowed_to_end_number(c)) {
660 const char *errstr = NULL;
663 yylval.v.number = strtonum(buf, LLONG_MIN,
666 yyerror("\"%s\" invalid number: %s",
681 #define allowed_in_string(x) \
682 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
683 x != '{' && x != '}' && \
684 x != '!' && x != '=' && x != '#' && \
687 if (isalnum(c) || c == ':' || c == '_') {
690 if ((unsigned)(p-buf) >= sizeof(buf)) {
691 yyerror("string too long");
695 } while (c != EOF && (allowed_in_string(c)));
699 if (token == STRING) {
700 yylval.v.string = strdup(buf);
701 if (yylval.v.string == NULL)
702 err(1, "yylex: strdup");
707 yylval.lineno = file->lineno;
716 check_file_secrecy(int fd, const char *fname)
720 if (fstat(fd, &st)) {
721 log_warn("cannot stat %s", fname);
724 if (st.st_uid != 0 && st.st_uid != getuid()) {
725 log_warnx("%s: owner not root or current user", fname);
728 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
729 log_warnx("%s: group writable or world read/writable", fname);
736 newfile(const char *name, int secret)
740 nfile = calloc(1, sizeof(struct file));
745 nfile->name = strdup(name);
746 if (nfile->name == NULL) {
751 nfile->stream = fopen(nfile->name, "r");
752 if (nfile->stream == NULL) {
753 /* no warning, we don't require a conf file */
758 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
759 fclose(nfile->stream);
769 closefile(struct file *xfile)
771 fclose(xfile->stream);
777 add_default_server(void)
779 new_srv = conf_new_server(D_SITENAME);
780 log_debug("%s: adding default server %s", __func__, D_SITENAME);
784 parse_config(const char *filename, struct gotwebd *env)
786 struct sym *sym, *next;
788 if (config_init(env) == -1)
789 fatalx("failed to initialize configuration");
793 file = newfile(filename, 0);
795 add_default_server();
796 sockets_parse_sockets(env);
797 /* just return, as we don't require a conf file */
802 errors = file->errors;
805 /* Free macros and check which have not been used. */
806 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
807 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
808 fprintf(stderr, "warning: macro '%s' not used\n",
813 TAILQ_REMOVE(&symhead, sym, entry);
821 /* just add default server if no config specified */
822 if (gotwebd->server_cnt == 0)
823 add_default_server();
825 /* setup our listening sockets */
826 sockets_parse_sockets(env);
832 conf_new_server(const char *name)
834 struct server *srv = NULL;
836 srv = calloc(1, sizeof(*srv));
838 fatalx("%s: calloc", __func__);
840 n = strlcpy(srv->name, name, sizeof(srv->name));
841 if (n >= sizeof(srv->name))
842 fatalx("%s: strlcpy", __func__);
843 n = snprintf(srv->unix_socket_name,
844 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
846 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
847 fatalx("%s: snprintf", __func__);
848 n = strlcpy(srv->repos_path, D_GOTPATH,
849 sizeof(srv->repos_path));
850 if (n >= sizeof(srv->repos_path))
851 fatalx("%s: strlcpy", __func__);
852 n = strlcpy(srv->site_name, D_SITENAME,
853 sizeof(srv->site_name));
854 if (n >= sizeof(srv->site_name))
855 fatalx("%s: strlcpy", __func__);
856 n = strlcpy(srv->site_owner, D_SITEOWNER,
857 sizeof(srv->site_owner));
858 if (n >= sizeof(srv->site_owner))
859 fatalx("%s: strlcpy", __func__);
860 n = strlcpy(srv->site_link, D_SITELINK,
861 sizeof(srv->site_link));
862 if (n >= sizeof(srv->site_link))
863 fatalx("%s: strlcpy", __func__);
864 n = strlcpy(srv->logo, D_GOTLOGO,
866 if (n >= sizeof(srv->logo))
867 fatalx("%s: strlcpy", __func__);
868 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
869 if (n >= sizeof(srv->logo_url))
870 fatalx("%s: strlcpy", __func__);
871 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
872 if (n >= sizeof(srv->custom_css))
873 fatalx("%s: strlcpy", __func__);
875 srv->show_site_owner = D_SHOWSOWNER;
876 srv->show_repo_owner = D_SHOWROWNER;
877 srv->show_repo_age = D_SHOWAGE;
878 srv->show_repo_description = D_SHOWDESC;
879 srv->show_repo_cloneurl = D_SHOWURL;
881 srv->max_repos_display = D_MAXREPODISP;
882 srv->max_commits_display = D_MAXCOMMITDISP;
883 srv->max_repos = D_MAXREPO;
885 srv->unix_socket = 1;
886 srv->fcgi_socket = 0;
888 TAILQ_INIT(&srv->al);
889 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
890 gotwebd->server_cnt++;
896 symset(const char *nam, const char *val, int persist)
900 TAILQ_FOREACH(sym, &symhead, entry) {
901 if (strcmp(nam, sym->nam) == 0)
906 if (sym->persist == 1)
911 TAILQ_REMOVE(&symhead, sym, entry);
915 sym = calloc(1, sizeof(*sym));
919 sym->nam = strdup(nam);
920 if (sym->nam == NULL) {
924 sym->val = strdup(val);
925 if (sym->val == NULL) {
931 sym->persist = persist;
932 TAILQ_INSERT_TAIL(&symhead, sym, entry);
937 cmdline_symset(char *s)
942 val = strrchr(s, '=');
946 sym = strndup(s, val - s);
948 fatal("%s: strndup", __func__);
950 ret = symset(sym, val + 1, 1);
957 symget(const char *nam)
961 TAILQ_FOREACH(sym, &symhead, entry) {
962 if (strcmp(nam, sym->nam) == 0) {
971 getservice(const char *n)
977 llval = strtonum(n, 0, UINT16_MAX, &errstr);
979 s = getservbyname(n, "tcp");
981 s = getservbyname(n, "udp");
984 return ntohs(s->s_port);
987 return (unsigned short)llval;
991 host_v4(const char *s)
994 struct sockaddr_in *sain;
997 memset(&ina, 0, sizeof(ina));
998 if (inet_pton(AF_INET, s, &ina) != 1)
1001 if ((h = calloc(1, sizeof(*h))) == NULL)
1003 sain = (struct sockaddr_in *)&h->ss;
1004 got_sockaddr_inet_init(sain, &ina);
1005 if (sain->sin_addr.s_addr == INADDR_ANY)
1006 h->prefixlen = 0; /* 0.0.0.0 address */
1008 h->prefixlen = -1; /* host address */
1013 host_v6(const char *s)
1015 struct addrinfo hints, *res;
1016 struct sockaddr_in6 *sa_in6, *ra;
1017 struct address *h = NULL;
1019 memset(&hints, 0, sizeof(hints));
1020 hints.ai_family = AF_INET6;
1021 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1022 hints.ai_flags = AI_NUMERICHOST;
1023 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1024 if ((h = calloc(1, sizeof(*h))) == NULL)
1026 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1027 ra = (struct sockaddr_in6 *)res->ai_addr;
1028 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1030 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1031 sizeof(sa_in6->sin6_addr)) == 0)
1032 h->prefixlen = 0; /* any address */
1034 h->prefixlen = -1; /* host address */
1042 host_dns(const char *s, struct server *new_srv, int max,
1043 in_port_t port, const char *ifname, int ipproto)
1045 struct addrinfo hints, *res0, *res;
1047 struct sockaddr_in *sain;
1048 struct sockaddr_in6 *sin6;
1051 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1054 memset(&hints, 0, sizeof(hints));
1055 hints.ai_family = PF_UNSPEC;
1056 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1057 hints.ai_flags = AI_ADDRCONFIG;
1058 error = getaddrinfo(s, NULL, &hints, &res0);
1059 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1062 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1063 gai_strerror(error));
1067 for (res = res0; res && cnt < max; res = res->ai_next) {
1068 if (res->ai_family != AF_INET &&
1069 res->ai_family != AF_INET6)
1071 if ((h = calloc(1, sizeof(*h))) == NULL)
1076 if (ifname != NULL) {
1077 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1078 sizeof(h->ifname)) {
1079 log_warnx("%s: interface name truncated",
1087 h->ipproto = ipproto;
1088 h->ss.ss_family = res->ai_family;
1089 h->prefixlen = -1; /* host address */
1091 if (res->ai_family == AF_INET) {
1092 struct sockaddr_in *ra;
1093 sain = (struct sockaddr_in *)&h->ss;
1094 ra = (struct sockaddr_in *)res->ai_addr;
1095 got_sockaddr_inet_init(sain, &ra->sin_addr);
1097 struct sockaddr_in6 *ra;
1098 sin6 = (struct sockaddr_in6 *)&h->ss;
1099 ra = (struct sockaddr_in6 *)res->ai_addr;
1100 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1103 if (add_addr(new_srv, h))
1107 if (cnt == max && res) {
1108 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1116 host_if(const char *s, struct server *new_srv, int max,
1117 in_port_t port, const char *ifname, int ipproto)
1119 struct ifaddrs *ifap, *p;
1120 struct sockaddr_in *sain;
1121 struct sockaddr_in6 *sin6;
1125 if (getifaddrs(&ifap) == -1)
1126 fatal("getifaddrs");
1128 /* First search for IPv4 addresses */
1132 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1133 if (p->ifa_addr == NULL ||
1134 p->ifa_addr->sa_family != af ||
1135 (strcmp(s, p->ifa_name) != 0 &&
1136 !is_if_in_group(p->ifa_name, s)))
1138 if ((h = calloc(1, sizeof(*h))) == NULL)
1143 if (ifname != NULL) {
1144 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1145 sizeof(h->ifname)) {
1146 log_warnx("%s: interface name truncated",
1154 h->ipproto = ipproto;
1155 h->ss.ss_family = af;
1156 h->prefixlen = -1; /* host address */
1158 if (af == AF_INET) {
1159 struct sockaddr_in *ra;
1160 sain = (struct sockaddr_in *)&h->ss;
1161 ra = (struct sockaddr_in *)p->ifa_addr;
1162 got_sockaddr_inet_init(sain, &ra->sin_addr);
1164 struct sockaddr_in6 *ra;
1165 sin6 = (struct sockaddr_in6 *)&h->ss;
1166 ra = (struct sockaddr_in6 *)p->ifa_addr;
1167 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1171 if (add_addr(new_srv, h))
1175 if (af == AF_INET) {
1176 /* Next search for IPv6 addresses */
1182 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1190 host(const char *s, struct server *new_srv, int max,
1191 in_port_t port, const char *ifname, int ipproto)
1204 if (ifname != NULL) {
1205 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1206 sizeof(h->ifname)) {
1207 log_warnx("%s: interface name truncated",
1214 h->ipproto = ipproto;
1216 if (add_addr(new_srv, h))
1221 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1225 is_if_in_group(const char *ifname, const char *groupname)
1228 struct ifgroupreq ifgr;
1229 struct ifg_req *ifg;
1233 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1236 memset(&ifgr, 0, sizeof(ifgr));
1237 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1239 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1240 if (errno == EINVAL || errno == ENOTTY)
1242 err(1, "SIOCGIFGROUP");
1245 len = ifgr.ifgr_len;
1246 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1247 sizeof(struct ifg_req));
1248 if (ifgr.ifgr_groups == NULL)
1249 err(1, "getifgroups");
1250 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1251 err(1, "SIOCGIFGROUP");
1253 ifg = ifgr.ifgr_groups;
1254 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1255 len -= sizeof(struct ifg_req);
1256 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1261 free(ifgr.ifgr_groups);
1269 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1271 if (strcmp("", addr) == 0) {
1272 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1274 yyerror("invalid listen ip: %s",
1278 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1279 yyerror("invalid listen ip: %s", "::1");
1283 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1285 yyerror("invalid listen ip: %s", addr);
1293 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1294 const char *other_srv)
1298 char buf[INET6_ADDRSTRLEN];
1299 const char *addrstr;
1301 TAILQ_FOREACH(a, al, entry) {
1302 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1306 switch (h->ss.ss_family) {
1308 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1311 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1314 yyerror("unknown address family: %d", h->ss.ss_family);
1317 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1320 yyerror("server %s: duplicate fcgi listen "
1321 "address %s:%d, already used by server %s",
1322 new_srv, addrstr, h->port, other_srv);
1324 log_warnx("server: %s: duplicate fcgi listen "
1325 "address %s:%d", new_srv, addrstr, h->port);
1329 yyerror("server: %s: duplicate fcgi listen "
1330 "address, already used by server %s",
1331 new_srv, other_srv);
1333 log_warnx("server %s: duplicate fcgi listen "
1334 "address", new_srv);
1345 add_addr(struct server *new_srv, struct address *h)
1349 /* Address cannot be shared between different servers. */
1350 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1353 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1357 /* Tolerate duplicate address lines within the scope of a server. */
1358 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1359 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);