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 addresslist *, in_port_t);
97 struct address *host_v4(const char *);
98 struct address *host_v6(const char *);
99 int host_dns(const char *, struct addresslist *,
100 int, in_port_t, const char *, int);
101 int host_if(const char *, struct addresslist *,
102 int, in_port_t, const char *, int);
103 int host(const char *, struct addresslist *,
104 int, in_port_t, const char *, int);
105 int is_if_in_group(const char *, const char *);
118 %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
119 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
120 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
121 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK FCGI_SOCKET
122 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
124 %token <v.string> STRING
125 %type <v.port> fcgiport
126 %token <v.number> NUMBER
127 %type <v.number> boolean
134 | grammar server '\n'
138 if (strcasecmp($1, "1") == 0 ||
139 strcasecmp($1, "yes") == 0 ||
140 strcasecmp($1, "on") == 0)
142 else if (strcasecmp($1, "0") == 0 ||
143 strcasecmp($1, "off") == 0 ||
144 strcasecmp($1, "no") == 0)
147 yyerror("invalid boolean value '%s'", $1);
154 | NUMBER { $$ = $1; }
157 fcgiport : PORT NUMBER {
158 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
159 yyerror("invalid port: %lld", $2);
167 if ((val = getservice($2)) == -1) {
168 yyerror("invalid port: %s", $2);
178 main : PREFORK NUMBER {
179 gotwebd->prefork_gotwebd = $2;
182 n = strlcpy(gotwebd->httpd_chroot, $2,
183 sizeof(gotwebd->httpd_chroot));
184 if (n >= sizeof(gotwebd->httpd_chroot)) {
185 yyerror("%s: httpd_chroot truncated", __func__);
191 | FCGI_SOCKET boolean {
192 gotwebd->fcgi_socket = $2;
194 | UNIX_SOCKET boolean {
195 gotwebd->unix_socket = $2;
197 | UNIX_SOCKET_NAME STRING {
198 n = snprintf(gotwebd->unix_socket_name,
199 sizeof(gotwebd->unix_socket_name), "%s%s",
200 strlen(gotwebd->httpd_chroot) ?
201 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
203 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
204 yyerror("%s: unix_socket_name truncated",
213 server : SERVER STRING {
216 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
217 if (strcmp(srv->name, $2) == 0) {
218 yyerror("server name exists '%s'", $2);
224 new_srv = conf_new_server($2);
225 if (new_srv->fcgi_socket)
226 if (get_addrs(new_srv->fcgi_socket_bind,
228 new_srv->fcgi_socket_port) == -1) {
229 yyerror("could not get tcp iface "
233 log_debug("adding server %s", $2);
239 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
240 if (strcmp(srv->name, $2) == 0) {
241 yyerror("server name exists '%s'", $2);
247 new_srv = conf_new_server($2);
248 log_debug("adding server %s", $2);
250 } '{' optnl serveropts2 '}' {
251 if (new_srv->fcgi_socket) {
252 if (get_addrs(new_srv->fcgi_socket_bind,
253 &new_srv->al, new_srv->fcgi_socket_port)
255 yyerror("could not get tcp iface addr");
262 serveropts1 : REPOS_PATH STRING {
263 n = strlcpy(new_srv->repos_path, $2,
264 sizeof(new_srv->repos_path));
265 if (n >= sizeof(new_srv->repos_path)) {
266 yyerror("%s: repos_path truncated", __func__);
273 n = strlcpy(new_srv->site_name, $2,
274 sizeof(new_srv->site_name));
275 if (n >= sizeof(new_srv->site_name)) {
276 yyerror("%s: site_name truncated", __func__);
282 | SITE_OWNER STRING {
283 n = strlcpy(new_srv->site_owner, $2,
284 sizeof(new_srv->site_owner));
285 if (n >= sizeof(new_srv->site_owner)) {
286 yyerror("%s: site_owner truncated", __func__);
293 n = strlcpy(new_srv->site_link, $2,
294 sizeof(new_srv->site_link));
295 if (n >= sizeof(new_srv->site_link)) {
296 yyerror("%s: site_link truncated", __func__);
303 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
304 if (n >= sizeof(new_srv->logo)) {
305 yyerror("%s: logo truncated", __func__);
312 n = strlcpy(new_srv->logo_url, $2,
313 sizeof(new_srv->logo_url));
314 if (n >= sizeof(new_srv->logo_url)) {
315 yyerror("%s: logo_url truncated", __func__);
321 | CUSTOM_CSS STRING {
322 n = strlcpy(new_srv->custom_css, $2,
323 sizeof(new_srv->custom_css));
324 if (n >= sizeof(new_srv->custom_css)) {
325 yyerror("%s: custom_css truncated", __func__);
331 | LISTEN ON STRING fcgiport {
332 n = strlcpy(new_srv->fcgi_socket_bind, $3,
333 sizeof(new_srv->fcgi_socket_bind));
334 if (n >= sizeof(new_srv->fcgi_socket_bind)) {
335 yyerror("%s: fcgi_socket_bind truncated",
341 new_srv->fcgi_socket_port = $4;
345 new_srv->max_repos = $2;
347 | SHOW_SITE_OWNER boolean {
348 new_srv->show_site_owner = $2;
350 | SHOW_REPO_OWNER boolean {
351 new_srv->show_repo_owner = $2;
353 | SHOW_REPO_AGE boolean {
354 new_srv->show_repo_age = $2;
356 | SHOW_REPO_DESCRIPTION boolean {
357 new_srv->show_repo_description = $2;
359 | SHOW_REPO_CLONEURL boolean {
360 new_srv->show_repo_cloneurl = $2;
362 | MAX_REPOS_DISPLAY NUMBER {
363 new_srv->max_repos_display = $2;
365 | MAX_COMMITS_DISPLAY NUMBER {
367 new_srv->max_commits_display = $2;
369 | FCGI_SOCKET boolean {
370 new_srv->fcgi_socket = $2;
372 | UNIX_SOCKET boolean {
373 new_srv->unix_socket = $2;
375 | UNIX_SOCKET_NAME STRING {
376 n = snprintf(new_srv->unix_socket_name,
377 sizeof(new_srv->unix_socket_name), "%s%s",
378 strlen(gotwebd->httpd_chroot) ?
379 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
381 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
382 yyerror("%s: unix_socket_name truncated",
391 serveropts2 : serveropts2 serveropts1 nl
398 optnl : '\n' optnl /* zero or more newlines */
410 yyerror(const char *fmt, ...)
417 if (vasprintf(&msg, fmt, ap) == -1)
418 fatalx("yyerror vasprintf");
420 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
426 kw_cmp(const void *k, const void *e)
428 return (strcmp(k, ((const struct keywords *)e)->k_name));
434 /* This has to be sorted always. */
435 static const struct keywords keywords[] = {
436 { "chroot", CHROOT },
437 { "custom_css", CUSTOM_CSS },
438 { "fcgi_socket", FCGI_SOCKET },
439 { "listen", LISTEN },
441 { "logo_url" , LOGO_URL },
442 { "max_commits_display", MAX_COMMITS_DISPLAY },
443 { "max_repos", MAX_REPOS },
444 { "max_repos_display", MAX_REPOS_DISPLAY },
447 { "prefork", PREFORK },
448 { "repos_path", REPOS_PATH },
449 { "server", SERVER },
450 { "show_repo_age", SHOW_REPO_AGE },
451 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
452 { "show_repo_description", SHOW_REPO_DESCRIPTION },
453 { "show_repo_owner", SHOW_REPO_OWNER },
454 { "show_site_owner", SHOW_SITE_OWNER },
455 { "site_link", SITE_LINK },
456 { "site_name", SITE_NAME },
457 { "site_owner", SITE_OWNER },
458 { "unix_socket", UNIX_SOCKET },
459 { "unix_socket_name", UNIX_SOCKET_NAME },
461 const struct keywords *p;
463 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
464 sizeof(keywords[0]), kw_cmp);
472 #define MAXPUSHBACK 128
474 unsigned char *parsebuf;
476 unsigned char pushback_buffer[MAXPUSHBACK];
477 int pushback_index = 0;
485 /* Read character from the parsebuffer instead of input. */
486 if (parseindex >= 0) {
487 c = parsebuf[parseindex++];
496 return (pushback_buffer[--pushback_index]);
499 c = getc(file->stream);
501 yyerror("reached end of file while parsing "
506 c = getc(file->stream);
508 next = getc(file->stream);
513 yylval.lineno = file->lineno;
515 c = getc(file->stream);
531 if (pushback_index < MAXPUSHBACK-1)
532 return (pushback_buffer[pushback_index++] = c);
544 /* Skip to either EOF or the first real EOL. */
547 c = pushback_buffer[--pushback_index];
563 unsigned char buf[8096];
564 unsigned char *p, *val;
571 while (c == ' ' || c == '\t')
572 c = lgetc(0); /* nothing */
574 yylval.lineno = file->lineno;
577 while (c != '\n' && c != EOF)
578 c = lgetc(0); /* nothing */
580 if (c == '$' && parsebuf == NULL) {
586 if (p + 1 >= buf + sizeof(buf) - 1) {
587 yyerror("string too long");
590 if (isalnum(c) || c == '_') {
600 yyerror("macro '%s' not defined", buf);
619 } else if (c == '\\') {
620 next = lgetc(quotec);
623 if (next == quotec || c == ' ' || c == '\t')
625 else if (next == '\n') {
630 } else if (c == quotec) {
633 } else if (c == '\0') {
634 yyerror("syntax error");
637 if (p + 1 >= buf + sizeof(buf) - 1) {
638 yyerror("string too long");
643 yylval.v.string = strdup(buf);
644 if (yylval.v.string == NULL)
645 err(1, "yylex: strdup");
649 #define allowed_to_end_number(x) \
650 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
652 if (c == '-' || isdigit(c)) {
655 if ((unsigned)(p-buf) >= sizeof(buf)) {
656 yyerror("string too long");
660 } while (c != EOF && isdigit(c));
662 if (p == buf + 1 && buf[0] == '-')
664 if (c == EOF || allowed_to_end_number(c)) {
665 const char *errstr = NULL;
668 yylval.v.number = strtonum(buf, LLONG_MIN,
671 yyerror("\"%s\" invalid number: %s",
686 #define allowed_in_string(x) \
687 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
688 x != '{' && x != '}' && \
689 x != '!' && x != '=' && x != '#' && \
692 if (isalnum(c) || c == ':' || c == '_') {
695 if ((unsigned)(p-buf) >= sizeof(buf)) {
696 yyerror("string too long");
700 } while (c != EOF && (allowed_in_string(c)));
704 if (token == STRING) {
705 yylval.v.string = strdup(buf);
706 if (yylval.v.string == NULL)
707 err(1, "yylex: strdup");
712 yylval.lineno = file->lineno;
721 check_file_secrecy(int fd, const char *fname)
725 if (fstat(fd, &st)) {
726 log_warn("cannot stat %s", fname);
729 if (st.st_uid != 0 && st.st_uid != getuid()) {
730 log_warnx("%s: owner not root or current user", fname);
733 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
734 log_warnx("%s: group writable or world read/writable", fname);
741 newfile(const char *name, int secret)
745 nfile = calloc(1, sizeof(struct file));
750 nfile->name = strdup(name);
751 if (nfile->name == NULL) {
756 nfile->stream = fopen(nfile->name, "r");
757 if (nfile->stream == NULL) {
758 /* no warning, we don't require a conf file */
763 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
764 fclose(nfile->stream);
774 closefile(struct file *xfile)
776 fclose(xfile->stream);
782 add_default_server(void)
784 new_srv = conf_new_server(D_SITENAME);
785 log_debug("%s: adding default server %s", __func__, D_SITENAME);
789 parse_config(const char *filename, struct gotwebd *env)
791 struct sym *sym, *next;
793 if (config_init(env) == -1)
794 fatalx("failed to initialize configuration");
798 file = newfile(filename, 0);
800 add_default_server();
801 sockets_parse_sockets(env);
802 /* just return, as we don't require a conf file */
807 errors = file->errors;
810 /* Free macros and check which have not been used. */
811 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
812 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
813 fprintf(stderr, "warning: macro '%s' not used\n",
818 TAILQ_REMOVE(&symhead, sym, entry);
826 /* just add default server if no config specified */
827 if (gotwebd->server_cnt == 0)
828 add_default_server();
830 /* setup our listening sockets */
831 sockets_parse_sockets(env);
837 conf_new_server(const char *name)
839 struct server *srv = NULL;
842 srv = calloc(1, sizeof(*srv));
844 fatalx("%s: calloc", __func__);
846 n = strlcpy(srv->name, name, sizeof(srv->name));
847 if (n >= sizeof(srv->name))
848 fatalx("%s: strlcpy", __func__);
849 n = snprintf(srv->unix_socket_name,
850 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
852 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
853 fatalx("%s: snprintf", __func__);
854 n = strlcpy(srv->repos_path, D_GOTPATH,
855 sizeof(srv->repos_path));
856 if (n >= sizeof(srv->repos_path))
857 fatalx("%s: strlcpy", __func__);
858 n = strlcpy(srv->site_name, D_SITENAME,
859 sizeof(srv->site_name));
860 if (n >= sizeof(srv->site_name))
861 fatalx("%s: strlcpy", __func__);
862 n = strlcpy(srv->site_owner, D_SITEOWNER,
863 sizeof(srv->site_owner));
864 if (n >= sizeof(srv->site_owner))
865 fatalx("%s: strlcpy", __func__);
866 n = strlcpy(srv->site_link, D_SITELINK,
867 sizeof(srv->site_link));
868 if (n >= sizeof(srv->site_link))
869 fatalx("%s: strlcpy", __func__);
870 n = strlcpy(srv->logo, D_GOTLOGO,
872 if (n >= sizeof(srv->logo))
873 fatalx("%s: strlcpy", __func__);
874 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
875 if (n >= sizeof(srv->logo_url))
876 fatalx("%s: strlcpy", __func__);
877 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
878 if (n >= sizeof(srv->custom_css))
879 fatalx("%s: strlcpy", __func__);
881 val = getservice(D_FCGI_PORT);
882 srv->fcgi_socket_port = gotwebd->fcgi_socket_port ?
883 gotwebd->fcgi_socket_port: val;
885 srv->show_site_owner = D_SHOWSOWNER;
886 srv->show_repo_owner = D_SHOWROWNER;
887 srv->show_repo_age = D_SHOWAGE;
888 srv->show_repo_description = D_SHOWDESC;
889 srv->show_repo_cloneurl = D_SHOWURL;
891 srv->max_repos_display = D_MAXREPODISP;
892 srv->max_commits_display = D_MAXCOMMITDISP;
893 srv->max_repos = D_MAXREPO;
895 srv->unix_socket = 1;
896 srv->fcgi_socket = gotwebd->fcgi_socket ? gotwebd->fcgi_socket : 0;
898 TAILQ_INIT(&srv->al);
899 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
900 gotwebd->server_cnt++;
906 symset(const char *nam, const char *val, int persist)
910 TAILQ_FOREACH(sym, &symhead, entry) {
911 if (strcmp(nam, sym->nam) == 0)
916 if (sym->persist == 1)
921 TAILQ_REMOVE(&symhead, sym, entry);
925 sym = calloc(1, sizeof(*sym));
929 sym->nam = strdup(nam);
930 if (sym->nam == NULL) {
934 sym->val = strdup(val);
935 if (sym->val == NULL) {
941 sym->persist = persist;
942 TAILQ_INSERT_TAIL(&symhead, sym, entry);
947 cmdline_symset(char *s)
953 val = strrchr(s, '=');
957 len = strlen(s) - strlen(val) + 1;
960 fatal("%s: malloc", __func__);
962 memcpy(&sym, s, len);
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 addresslist *al, 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, al, 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 TAILQ_INSERT_HEAD(al, h, entry);
1120 if (cnt == max && res) {
1121 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1129 host_if(const char *s, struct addresslist *al, int max,
1130 in_port_t port, const char *ifname, int ipproto)
1132 struct ifaddrs *ifap, *p;
1133 struct sockaddr_in *sain;
1134 struct sockaddr_in6 *sin6;
1138 if (getifaddrs(&ifap) == -1)
1139 fatal("getifaddrs");
1141 /* First search for IPv4 addresses */
1145 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1146 if (p->ifa_addr == NULL ||
1147 p->ifa_addr->sa_family != af ||
1148 (strcmp(s, p->ifa_name) != 0 &&
1149 !is_if_in_group(p->ifa_name, s)))
1151 if ((h = calloc(1, sizeof(*h))) == NULL)
1156 if (ifname != NULL) {
1157 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1158 sizeof(h->ifname)) {
1159 log_warnx("%s: interface name truncated",
1167 h->ipproto = ipproto;
1168 h->ss.ss_family = af;
1169 h->prefixlen = -1; /* host address */
1171 if (af == AF_INET) {
1172 struct sockaddr_in *ra;
1173 sain = (struct sockaddr_in *)&h->ss;
1174 ra = (struct sockaddr_in *)p->ifa_addr;
1175 got_sockaddr_inet_init(sain, &ra->sin_addr);
1177 struct sockaddr_in6 *ra;
1178 sin6 = (struct sockaddr_in6 *)&h->ss;
1179 ra = (struct sockaddr_in6 *)p->ifa_addr;
1180 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1184 TAILQ_INSERT_HEAD(al, h, entry);
1187 if (af == AF_INET) {
1188 /* Next search for IPv6 addresses */
1194 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1202 host(const char *s, struct addresslist *al, int max,
1203 in_port_t port, const char *ifname, int ipproto)
1216 if (ifname != NULL) {
1217 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1218 sizeof(h->ifname)) {
1219 log_warnx("%s: interface name truncated",
1226 h->ipproto = ipproto;
1228 TAILQ_INSERT_HEAD(al, h, entry);
1232 return (host_dns(s, al, max, port, ifname, ipproto));
1236 is_if_in_group(const char *ifname, const char *groupname)
1239 struct ifgroupreq ifgr;
1240 struct ifg_req *ifg;
1244 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1247 memset(&ifgr, 0, sizeof(ifgr));
1248 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1250 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1251 if (errno == EINVAL || errno == ENOTTY)
1253 err(1, "SIOCGIFGROUP");
1256 len = ifgr.ifgr_len;
1257 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1258 sizeof(struct ifg_req));
1259 if (ifgr.ifgr_groups == NULL)
1260 err(1, "getifgroups");
1261 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1262 err(1, "SIOCGIFGROUP");
1264 ifg = ifgr.ifgr_groups;
1265 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1266 len -= sizeof(struct ifg_req);
1267 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1272 free(ifgr.ifgr_groups);
1280 get_addrs(const char *addr, struct addresslist *al, in_port_t port)
1282 if (strcmp("", addr) == 0) {
1283 if (host("127.0.0.1", al, 1, port, "127.0.0.1", -1) <= 0) {
1284 yyerror("invalid listen ip: %s",
1288 if (host("::1", al, 1, port, "::1", -1) <= 0) {
1289 yyerror("invalid listen ip: %s", "::1");
1293 if (host(addr, al, GOTWEBD_MAXIFACE, port, addr,
1295 yyerror("invalid listen ip: %s", addr);