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 struct address *host_v4(const char *);
103 struct address *host_v6(const char *);
104 int host_dns(const char *, struct server *,
105 int, in_port_t, const char *, int);
106 int host_if(const char *, struct server *,
107 int, in_port_t, const char *, int);
108 int host(const char *, struct server *,
109 int, in_port_t, const char *, int);
110 int is_if_in_group(const char *, const char *);
123 %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
124 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
125 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
126 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
127 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
129 %token <v.string> STRING
130 %type <v.port> fcgiport
131 %token <v.number> NUMBER
132 %type <v.number> boolean
136 grammar : /* empty */
138 | grammar varset '\n'
140 | grammar server '\n'
141 | grammar error '\n' { file->errors++; }
144 varset : STRING '=' STRING {
147 if (isspace((unsigned char)*s)) {
148 yyerror("macro name cannot contain "
155 if (symset($1, $3, 0) == -1)
156 fatal("cannot store variable");
163 if (strcasecmp($1, "1") == 0 ||
164 strcasecmp($1, "yes") == 0 ||
165 strcasecmp($1, "on") == 0)
167 else if (strcasecmp($1, "0") == 0 ||
168 strcasecmp($1, "off") == 0 ||
169 strcasecmp($1, "no") == 0)
172 yyerror("invalid boolean value '%s'", $1);
179 | NUMBER { $$ = $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 gotwebd->prefork_gotwebd = $2;
207 n = strlcpy(gotwebd->httpd_chroot, $2,
208 sizeof(gotwebd->httpd_chroot));
209 if (n >= sizeof(gotwebd->httpd_chroot)) {
210 yyerror("%s: httpd_chroot truncated", __func__);
216 | UNIX_SOCKET boolean {
217 gotwebd->unix_socket = $2;
219 | UNIX_SOCKET_NAME STRING {
220 n = snprintf(gotwebd->unix_socket_name,
221 sizeof(gotwebd->unix_socket_name), "%s%s",
222 strlen(gotwebd->httpd_chroot) ?
223 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
225 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
226 yyerror("%s: unix_socket_name truncated",
235 server : SERVER STRING {
238 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
239 if (strcmp(srv->name, $2) == 0) {
240 yyerror("server name exists '%s'", $2);
246 new_srv = conf_new_server($2);
247 log_debug("adding server %s", $2);
253 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
254 if (strcmp(srv->name, $2) == 0) {
255 yyerror("server name exists '%s'", $2);
261 new_srv = conf_new_server($2);
262 log_debug("adding server %s", $2);
264 } '{' optnl serveropts2 '}' {
268 serveropts1 : REPOS_PATH STRING {
269 n = strlcpy(new_srv->repos_path, $2,
270 sizeof(new_srv->repos_path));
271 if (n >= sizeof(new_srv->repos_path)) {
272 yyerror("%s: repos_path truncated", __func__);
279 n = strlcpy(new_srv->site_name, $2,
280 sizeof(new_srv->site_name));
281 if (n >= sizeof(new_srv->site_name)) {
282 yyerror("%s: site_name truncated", __func__);
288 | SITE_OWNER STRING {
289 n = strlcpy(new_srv->site_owner, $2,
290 sizeof(new_srv->site_owner));
291 if (n >= sizeof(new_srv->site_owner)) {
292 yyerror("%s: site_owner truncated", __func__);
299 n = strlcpy(new_srv->site_link, $2,
300 sizeof(new_srv->site_link));
301 if (n >= sizeof(new_srv->site_link)) {
302 yyerror("%s: site_link truncated", __func__);
309 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
310 if (n >= sizeof(new_srv->logo)) {
311 yyerror("%s: logo truncated", __func__);
318 n = strlcpy(new_srv->logo_url, $2,
319 sizeof(new_srv->logo_url));
320 if (n >= sizeof(new_srv->logo_url)) {
321 yyerror("%s: logo_url truncated", __func__);
327 | CUSTOM_CSS STRING {
328 n = strlcpy(new_srv->custom_css, $2,
329 sizeof(new_srv->custom_css));
330 if (n >= sizeof(new_srv->custom_css)) {
331 yyerror("%s: custom_css truncated", __func__);
337 | LISTEN ON STRING fcgiport {
338 if (get_addrs($3, new_srv, $4) == -1) {
339 yyerror("could not get addrs");
342 new_srv->fcgi_socket = 1;
344 | LISTEN ON SOCKET STRING {
345 if (!strcasecmp($4, "off") ||
346 !strcasecmp($4, "no")) {
347 new_srv->unix_socket = 0;
352 new_srv->unix_socket = 1;
354 n = snprintf(new_srv->unix_socket_name,
355 sizeof(new_srv->unix_socket_name), "%s%s",
356 strlen(gotwebd->httpd_chroot) ?
357 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $4);
359 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
360 yyerror("%s: unix_socket_name truncated",
369 new_srv->max_repos = $2;
371 | SHOW_SITE_OWNER boolean {
372 new_srv->show_site_owner = $2;
374 | SHOW_REPO_OWNER boolean {
375 new_srv->show_repo_owner = $2;
377 | SHOW_REPO_AGE boolean {
378 new_srv->show_repo_age = $2;
380 | SHOW_REPO_DESCRIPTION boolean {
381 new_srv->show_repo_description = $2;
383 | SHOW_REPO_CLONEURL boolean {
384 new_srv->show_repo_cloneurl = $2;
386 | RESPECT_EXPORTOK boolean {
387 new_srv->respect_exportok = $2;
389 | MAX_REPOS_DISPLAY NUMBER {
390 new_srv->max_repos_display = $2;
392 | MAX_COMMITS_DISPLAY NUMBER {
394 new_srv->max_commits_display = $2;
398 serveropts2 : serveropts2 serveropts1 nl
405 optnl : '\n' optnl /* zero or more newlines */
417 yyerror(const char *fmt, ...)
424 if (vasprintf(&msg, fmt, ap) == -1)
425 fatalx("yyerror vasprintf");
427 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
433 kw_cmp(const void *k, const void *e)
435 return (strcmp(k, ((const struct keywords *)e)->k_name));
441 /* This has to be sorted always. */
442 static const struct keywords keywords[] = {
443 { "chroot", CHROOT },
444 { "custom_css", CUSTOM_CSS },
445 { "listen", LISTEN },
447 { "logo_url", LOGO_URL },
448 { "max_commits_display", MAX_COMMITS_DISPLAY },
449 { "max_repos", MAX_REPOS },
450 { "max_repos_display", MAX_REPOS_DISPLAY },
453 { "prefork", PREFORK },
454 { "repos_path", REPOS_PATH },
455 { "respect_exportok", RESPECT_EXPORTOK },
456 { "server", SERVER },
457 { "show_repo_age", SHOW_REPO_AGE },
458 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
459 { "show_repo_description", SHOW_REPO_DESCRIPTION },
460 { "show_repo_owner", SHOW_REPO_OWNER },
461 { "show_site_owner", SHOW_SITE_OWNER },
462 { "site_link", SITE_LINK },
463 { "site_name", SITE_NAME },
464 { "site_owner", SITE_OWNER },
465 { "socket", SOCKET },
466 { "unix_socket", UNIX_SOCKET },
467 { "unix_socket_name", UNIX_SOCKET_NAME },
469 const struct keywords *p;
471 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
472 sizeof(keywords[0]), kw_cmp);
480 #define MAXPUSHBACK 128
482 unsigned char *parsebuf;
484 unsigned char pushback_buffer[MAXPUSHBACK];
485 int pushback_index = 0;
493 /* Read character from the parsebuffer instead of input. */
494 if (parseindex >= 0) {
495 c = parsebuf[parseindex++];
504 return (pushback_buffer[--pushback_index]);
507 c = getc(file->stream);
509 yyerror("reached end of file while parsing "
514 c = getc(file->stream);
516 next = getc(file->stream);
521 yylval.lineno = file->lineno;
523 c = getc(file->stream);
539 if (pushback_index < MAXPUSHBACK-1)
540 return (pushback_buffer[pushback_index++] = c);
552 /* Skip to either EOF or the first real EOL. */
555 c = pushback_buffer[--pushback_index];
571 unsigned char buf[8096];
572 unsigned char *p, *val;
579 while (c == ' ' || c == '\t')
580 c = lgetc(0); /* nothing */
582 yylval.lineno = file->lineno;
585 while (c != '\n' && c != EOF)
586 c = lgetc(0); /* nothing */
588 if (c == '$' && parsebuf == NULL) {
594 if (p + 1 >= buf + sizeof(buf) - 1) {
595 yyerror("string too long");
598 if (isalnum(c) || c == '_') {
608 yyerror("macro '%s' not defined", buf);
627 } else if (c == '\\') {
628 next = lgetc(quotec);
631 if (next == quotec || c == ' ' || c == '\t')
633 else if (next == '\n') {
638 } else if (c == quotec) {
641 } else if (c == '\0') {
642 yyerror("syntax error");
645 if (p + 1 >= buf + sizeof(buf) - 1) {
646 yyerror("string too long");
651 yylval.v.string = strdup(buf);
652 if (yylval.v.string == NULL)
653 err(1, "yylex: strdup");
657 #define allowed_to_end_number(x) \
658 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
660 if (c == '-' || isdigit(c)) {
663 if ((unsigned)(p-buf) >= sizeof(buf)) {
664 yyerror("string too long");
668 } while (c != EOF && isdigit(c));
670 if (p == buf + 1 && buf[0] == '-')
672 if (c == EOF || allowed_to_end_number(c)) {
673 const char *errstr = NULL;
676 yylval.v.number = strtonum(buf, LLONG_MIN,
679 yyerror("\"%s\" invalid number: %s",
694 #define allowed_in_string(x) \
695 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
696 x != '{' && x != '}' && \
697 x != '!' && x != '=' && x != '#' && \
700 if (isalnum(c) || c == ':' || c == '_') {
703 if ((unsigned)(p-buf) >= sizeof(buf)) {
704 yyerror("string too long");
708 } while (c != EOF && (allowed_in_string(c)));
712 if (token == STRING) {
713 yylval.v.string = strdup(buf);
714 if (yylval.v.string == NULL)
715 err(1, "yylex: strdup");
720 yylval.lineno = file->lineno;
729 check_file_secrecy(int fd, const char *fname)
733 if (fstat(fd, &st)) {
734 log_warn("cannot stat %s", fname);
737 if (st.st_uid != 0 && st.st_uid != getuid()) {
738 log_warnx("%s: owner not root or current user", fname);
741 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
742 log_warnx("%s: group writable or world read/writable", fname);
749 newfile(const char *name, int secret)
753 nfile = calloc(1, sizeof(struct file));
758 nfile->name = strdup(name);
759 if (nfile->name == NULL) {
764 nfile->stream = fopen(nfile->name, "r");
765 if (nfile->stream == NULL) {
766 /* no warning, we don't require a conf file */
771 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
772 fclose(nfile->stream);
782 closefile(struct file *xfile)
784 fclose(xfile->stream);
790 add_default_server(void)
792 new_srv = conf_new_server(D_SITENAME);
793 log_debug("%s: adding default server %s", __func__, D_SITENAME);
797 parse_config(const char *filename, struct gotwebd *env)
799 struct sym *sym, *next;
801 if (config_init(env) == -1)
802 fatalx("failed to initialize configuration");
806 file = newfile(filename, 0);
808 add_default_server();
809 sockets_parse_sockets(env);
810 /* just return, as we don't require a conf file */
815 errors = file->errors;
818 /* Free macros and check which have not been used. */
819 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
820 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
821 fprintf(stderr, "warning: macro '%s' not used\n",
826 TAILQ_REMOVE(&symhead, sym, entry);
834 /* just add default server if no config specified */
835 if (gotwebd->server_cnt == 0)
836 add_default_server();
838 /* setup our listening sockets */
839 sockets_parse_sockets(env);
845 conf_new_server(const char *name)
847 struct server *srv = NULL;
849 srv = calloc(1, sizeof(*srv));
851 fatalx("%s: calloc", __func__);
853 n = strlcpy(srv->name, name, sizeof(srv->name));
854 if (n >= sizeof(srv->name))
855 fatalx("%s: strlcpy", __func__);
856 n = snprintf(srv->unix_socket_name,
857 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
859 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
860 fatalx("%s: snprintf", __func__);
861 n = strlcpy(srv->repos_path, D_GOTPATH,
862 sizeof(srv->repos_path));
863 if (n >= sizeof(srv->repos_path))
864 fatalx("%s: strlcpy", __func__);
865 n = strlcpy(srv->site_name, D_SITENAME,
866 sizeof(srv->site_name));
867 if (n >= sizeof(srv->site_name))
868 fatalx("%s: strlcpy", __func__);
869 n = strlcpy(srv->site_owner, D_SITEOWNER,
870 sizeof(srv->site_owner));
871 if (n >= sizeof(srv->site_owner))
872 fatalx("%s: strlcpy", __func__);
873 n = strlcpy(srv->site_link, D_SITELINK,
874 sizeof(srv->site_link));
875 if (n >= sizeof(srv->site_link))
876 fatalx("%s: strlcpy", __func__);
877 n = strlcpy(srv->logo, D_GOTLOGO,
879 if (n >= sizeof(srv->logo))
880 fatalx("%s: strlcpy", __func__);
881 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
882 if (n >= sizeof(srv->logo_url))
883 fatalx("%s: strlcpy", __func__);
884 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
885 if (n >= sizeof(srv->custom_css))
886 fatalx("%s: strlcpy", __func__);
888 srv->show_site_owner = D_SHOWSOWNER;
889 srv->show_repo_owner = D_SHOWROWNER;
890 srv->show_repo_age = D_SHOWAGE;
891 srv->show_repo_description = D_SHOWDESC;
892 srv->show_repo_cloneurl = D_SHOWURL;
893 srv->respect_exportok = D_RESPECTEXPORTOK;
895 srv->max_repos_display = D_MAXREPODISP;
896 srv->max_commits_display = D_MAXCOMMITDISP;
897 srv->max_repos = D_MAXREPO;
899 srv->unix_socket = 1;
900 srv->fcgi_socket = 0;
902 TAILQ_INIT(&srv->al);
903 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
904 gotwebd->server_cnt++;
910 symset(const char *nam, const char *val, int persist)
914 TAILQ_FOREACH(sym, &symhead, entry) {
915 if (strcmp(nam, sym->nam) == 0)
920 if (sym->persist == 1)
925 TAILQ_REMOVE(&symhead, sym, entry);
929 sym = calloc(1, sizeof(*sym));
933 sym->nam = strdup(nam);
934 if (sym->nam == NULL) {
938 sym->val = strdup(val);
939 if (sym->val == NULL) {
945 sym->persist = persist;
946 TAILQ_INSERT_TAIL(&symhead, sym, entry);
951 cmdline_symset(char *s)
956 val = strrchr(s, '=');
960 sym = strndup(s, val - s);
962 fatal("%s: strndup", __func__);
964 ret = symset(sym, val + 1, 1);
971 symget(const char *nam)
975 TAILQ_FOREACH(sym, &symhead, entry) {
976 if (strcmp(nam, sym->nam) == 0) {
985 getservice(const char *n)
991 llval = strtonum(n, 0, UINT16_MAX, &errstr);
993 s = getservbyname(n, "tcp");
995 s = getservbyname(n, "udp");
998 return ntohs(s->s_port);
1001 return (unsigned short)llval;
1005 host_v4(const char *s)
1008 struct sockaddr_in *sain;
1011 memset(&ina, 0, sizeof(ina));
1012 if (inet_pton(AF_INET, s, &ina) != 1)
1015 if ((h = calloc(1, sizeof(*h))) == NULL)
1017 sain = (struct sockaddr_in *)&h->ss;
1018 got_sockaddr_inet_init(sain, &ina);
1019 if (sain->sin_addr.s_addr == INADDR_ANY)
1020 h->prefixlen = 0; /* 0.0.0.0 address */
1022 h->prefixlen = -1; /* host address */
1027 host_v6(const char *s)
1029 struct addrinfo hints, *res;
1030 struct sockaddr_in6 *sa_in6, *ra;
1031 struct address *h = NULL;
1033 memset(&hints, 0, sizeof(hints));
1034 hints.ai_family = AF_INET6;
1035 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1036 hints.ai_flags = AI_NUMERICHOST;
1037 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1038 if ((h = calloc(1, sizeof(*h))) == NULL)
1040 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1041 ra = (struct sockaddr_in6 *)res->ai_addr;
1042 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1044 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1045 sizeof(sa_in6->sin6_addr)) == 0)
1046 h->prefixlen = 0; /* any address */
1048 h->prefixlen = -1; /* host address */
1056 host_dns(const char *s, struct server *new_srv, int max,
1057 in_port_t port, const char *ifname, int ipproto)
1059 struct addrinfo hints, *res0, *res;
1061 struct sockaddr_in *sain;
1062 struct sockaddr_in6 *sin6;
1065 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1068 memset(&hints, 0, sizeof(hints));
1069 hints.ai_family = PF_UNSPEC;
1070 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1071 hints.ai_flags = AI_ADDRCONFIG;
1072 error = getaddrinfo(s, NULL, &hints, &res0);
1073 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1076 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1077 gai_strerror(error));
1081 for (res = res0; res && cnt < max; res = res->ai_next) {
1082 if (res->ai_family != AF_INET &&
1083 res->ai_family != AF_INET6)
1085 if ((h = calloc(1, sizeof(*h))) == NULL)
1090 if (ifname != NULL) {
1091 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1092 sizeof(h->ifname)) {
1093 log_warnx("%s: interface name truncated",
1101 h->ipproto = ipproto;
1102 h->ss.ss_family = res->ai_family;
1103 h->prefixlen = -1; /* host address */
1105 if (res->ai_family == AF_INET) {
1106 struct sockaddr_in *ra;
1107 sain = (struct sockaddr_in *)&h->ss;
1108 ra = (struct sockaddr_in *)res->ai_addr;
1109 got_sockaddr_inet_init(sain, &ra->sin_addr);
1111 struct sockaddr_in6 *ra;
1112 sin6 = (struct sockaddr_in6 *)&h->ss;
1113 ra = (struct sockaddr_in6 *)res->ai_addr;
1114 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1117 if (add_addr(new_srv, h))
1121 if (cnt == max && res) {
1122 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1130 host_if(const char *s, struct server *new_srv, int max,
1131 in_port_t port, const char *ifname, int ipproto)
1133 struct ifaddrs *ifap, *p;
1134 struct sockaddr_in *sain;
1135 struct sockaddr_in6 *sin6;
1139 if (getifaddrs(&ifap) == -1)
1140 fatal("getifaddrs");
1142 /* First search for IPv4 addresses */
1146 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1147 if (p->ifa_addr == NULL ||
1148 p->ifa_addr->sa_family != af ||
1149 (strcmp(s, p->ifa_name) != 0 &&
1150 !is_if_in_group(p->ifa_name, s)))
1152 if ((h = calloc(1, sizeof(*h))) == NULL)
1157 if (ifname != NULL) {
1158 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1159 sizeof(h->ifname)) {
1160 log_warnx("%s: interface name truncated",
1168 h->ipproto = ipproto;
1169 h->ss.ss_family = af;
1170 h->prefixlen = -1; /* host address */
1172 if (af == AF_INET) {
1173 struct sockaddr_in *ra;
1174 sain = (struct sockaddr_in *)&h->ss;
1175 ra = (struct sockaddr_in *)p->ifa_addr;
1176 got_sockaddr_inet_init(sain, &ra->sin_addr);
1178 struct sockaddr_in6 *ra;
1179 sin6 = (struct sockaddr_in6 *)&h->ss;
1180 ra = (struct sockaddr_in6 *)p->ifa_addr;
1181 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1185 if (add_addr(new_srv, h))
1189 if (af == AF_INET) {
1190 /* Next search for IPv6 addresses */
1196 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1204 host(const char *s, struct server *new_srv, int max,
1205 in_port_t port, const char *ifname, int ipproto)
1218 if (ifname != NULL) {
1219 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1220 sizeof(h->ifname)) {
1221 log_warnx("%s: interface name truncated",
1228 h->ipproto = ipproto;
1230 if (add_addr(new_srv, h))
1235 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1239 is_if_in_group(const char *ifname, const char *groupname)
1242 struct ifgroupreq ifgr;
1243 struct ifg_req *ifg;
1247 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1250 memset(&ifgr, 0, sizeof(ifgr));
1251 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1253 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1254 if (errno == EINVAL || errno == ENOTTY)
1256 err(1, "SIOCGIFGROUP");
1259 len = ifgr.ifgr_len;
1260 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1261 sizeof(struct ifg_req));
1262 if (ifgr.ifgr_groups == NULL)
1263 err(1, "getifgroups");
1264 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1265 err(1, "SIOCGIFGROUP");
1267 ifg = ifgr.ifgr_groups;
1268 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1269 len -= sizeof(struct ifg_req);
1270 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1275 free(ifgr.ifgr_groups);
1283 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1285 if (strcmp("", addr) == 0) {
1286 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1288 yyerror("invalid listen ip: %s",
1292 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1293 yyerror("invalid listen ip: %s", "::1");
1297 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1299 yyerror("invalid listen ip: %s", addr);
1307 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1308 const char *other_srv)
1312 char buf[INET6_ADDRSTRLEN];
1313 const char *addrstr;
1315 TAILQ_FOREACH(a, al, entry) {
1316 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1320 switch (h->ss.ss_family) {
1322 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1325 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1328 yyerror("unknown address family: %d", h->ss.ss_family);
1331 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1334 yyerror("server %s: duplicate fcgi listen "
1335 "address %s:%d, already used by server %s",
1336 new_srv, addrstr, h->port, other_srv);
1338 log_warnx("server: %s: duplicate fcgi listen "
1339 "address %s:%d", new_srv, addrstr, h->port);
1343 yyerror("server: %s: duplicate fcgi listen "
1344 "address, already used by server %s",
1345 new_srv, other_srv);
1347 log_warnx("server %s: duplicate fcgi listen "
1348 "address", new_srv);
1359 add_addr(struct server *new_srv, struct address *h)
1363 /* Address cannot be shared between different servers. */
1364 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1367 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1371 /* Tolerate duplicate address lines within the scope of a server. */
1372 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1373 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);