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
137 | grammar server '\n'
141 if (strcasecmp($1, "1") == 0 ||
142 strcasecmp($1, "yes") == 0 ||
143 strcasecmp($1, "on") == 0)
145 else if (strcasecmp($1, "0") == 0 ||
146 strcasecmp($1, "off") == 0 ||
147 strcasecmp($1, "no") == 0)
150 yyerror("invalid boolean value '%s'", $1);
157 | NUMBER { $$ = $1; }
160 fcgiport : PORT NUMBER {
161 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
162 yyerror("invalid port: %lld", $2);
170 if ((val = getservice($2)) == -1) {
171 yyerror("invalid port: %s", $2);
181 main : PREFORK NUMBER {
182 gotwebd->prefork_gotwebd = $2;
185 n = strlcpy(gotwebd->httpd_chroot, $2,
186 sizeof(gotwebd->httpd_chroot));
187 if (n >= sizeof(gotwebd->httpd_chroot)) {
188 yyerror("%s: httpd_chroot truncated", __func__);
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 log_debug("adding server %s", $2);
231 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
232 if (strcmp(srv->name, $2) == 0) {
233 yyerror("server name exists '%s'", $2);
239 new_srv = conf_new_server($2);
240 log_debug("adding server %s", $2);
242 } '{' optnl serveropts2 '}' {
246 serveropts1 : REPOS_PATH STRING {
247 n = strlcpy(new_srv->repos_path, $2,
248 sizeof(new_srv->repos_path));
249 if (n >= sizeof(new_srv->repos_path)) {
250 yyerror("%s: repos_path truncated", __func__);
257 n = strlcpy(new_srv->site_name, $2,
258 sizeof(new_srv->site_name));
259 if (n >= sizeof(new_srv->site_name)) {
260 yyerror("%s: site_name truncated", __func__);
266 | SITE_OWNER STRING {
267 n = strlcpy(new_srv->site_owner, $2,
268 sizeof(new_srv->site_owner));
269 if (n >= sizeof(new_srv->site_owner)) {
270 yyerror("%s: site_owner truncated", __func__);
277 n = strlcpy(new_srv->site_link, $2,
278 sizeof(new_srv->site_link));
279 if (n >= sizeof(new_srv->site_link)) {
280 yyerror("%s: site_link truncated", __func__);
287 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
288 if (n >= sizeof(new_srv->logo)) {
289 yyerror("%s: logo truncated", __func__);
296 n = strlcpy(new_srv->logo_url, $2,
297 sizeof(new_srv->logo_url));
298 if (n >= sizeof(new_srv->logo_url)) {
299 yyerror("%s: logo_url truncated", __func__);
305 | CUSTOM_CSS STRING {
306 n = strlcpy(new_srv->custom_css, $2,
307 sizeof(new_srv->custom_css));
308 if (n >= sizeof(new_srv->custom_css)) {
309 yyerror("%s: custom_css truncated", __func__);
315 | LISTEN ON STRING fcgiport {
316 if (get_addrs($3, new_srv, $4) == -1) {
317 yyerror("could not get addrs");
320 new_srv->fcgi_socket = 1;
324 new_srv->max_repos = $2;
326 | SHOW_SITE_OWNER boolean {
327 new_srv->show_site_owner = $2;
329 | SHOW_REPO_OWNER boolean {
330 new_srv->show_repo_owner = $2;
332 | SHOW_REPO_AGE boolean {
333 new_srv->show_repo_age = $2;
335 | SHOW_REPO_DESCRIPTION boolean {
336 new_srv->show_repo_description = $2;
338 | SHOW_REPO_CLONEURL boolean {
339 new_srv->show_repo_cloneurl = $2;
341 | MAX_REPOS_DISPLAY NUMBER {
342 new_srv->max_repos_display = $2;
344 | MAX_COMMITS_DISPLAY NUMBER {
346 new_srv->max_commits_display = $2;
348 | UNIX_SOCKET boolean {
349 new_srv->unix_socket = $2;
351 | UNIX_SOCKET_NAME STRING {
352 n = snprintf(new_srv->unix_socket_name,
353 sizeof(new_srv->unix_socket_name), "%s%s",
354 strlen(gotwebd->httpd_chroot) ?
355 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
357 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
358 yyerror("%s: unix_socket_name truncated",
367 serveropts2 : serveropts2 serveropts1 nl
374 optnl : '\n' optnl /* zero or more newlines */
386 yyerror(const char *fmt, ...)
393 if (vasprintf(&msg, fmt, ap) == -1)
394 fatalx("yyerror vasprintf");
396 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
402 kw_cmp(const void *k, const void *e)
404 return (strcmp(k, ((const struct keywords *)e)->k_name));
410 /* This has to be sorted always. */
411 static const struct keywords keywords[] = {
412 { "chroot", CHROOT },
413 { "custom_css", CUSTOM_CSS },
414 { "listen", LISTEN },
416 { "logo_url" , LOGO_URL },
417 { "max_commits_display", MAX_COMMITS_DISPLAY },
418 { "max_repos", MAX_REPOS },
419 { "max_repos_display", MAX_REPOS_DISPLAY },
422 { "prefork", PREFORK },
423 { "repos_path", REPOS_PATH },
424 { "server", SERVER },
425 { "show_repo_age", SHOW_REPO_AGE },
426 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
427 { "show_repo_description", SHOW_REPO_DESCRIPTION },
428 { "show_repo_owner", SHOW_REPO_OWNER },
429 { "show_site_owner", SHOW_SITE_OWNER },
430 { "site_link", SITE_LINK },
431 { "site_name", SITE_NAME },
432 { "site_owner", SITE_OWNER },
433 { "unix_socket", UNIX_SOCKET },
434 { "unix_socket_name", UNIX_SOCKET_NAME },
436 const struct keywords *p;
438 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
439 sizeof(keywords[0]), kw_cmp);
447 #define MAXPUSHBACK 128
449 unsigned char *parsebuf;
451 unsigned char pushback_buffer[MAXPUSHBACK];
452 int pushback_index = 0;
460 /* Read character from the parsebuffer instead of input. */
461 if (parseindex >= 0) {
462 c = parsebuf[parseindex++];
471 return (pushback_buffer[--pushback_index]);
474 c = getc(file->stream);
476 yyerror("reached end of file while parsing "
481 c = getc(file->stream);
483 next = getc(file->stream);
488 yylval.lineno = file->lineno;
490 c = getc(file->stream);
506 if (pushback_index < MAXPUSHBACK-1)
507 return (pushback_buffer[pushback_index++] = c);
519 /* Skip to either EOF or the first real EOL. */
522 c = pushback_buffer[--pushback_index];
538 unsigned char buf[8096];
539 unsigned char *p, *val;
546 while (c == ' ' || c == '\t')
547 c = lgetc(0); /* nothing */
549 yylval.lineno = file->lineno;
552 while (c != '\n' && c != EOF)
553 c = lgetc(0); /* nothing */
555 if (c == '$' && parsebuf == NULL) {
561 if (p + 1 >= buf + sizeof(buf) - 1) {
562 yyerror("string too long");
565 if (isalnum(c) || c == '_') {
575 yyerror("macro '%s' not defined", buf);
594 } else if (c == '\\') {
595 next = lgetc(quotec);
598 if (next == quotec || c == ' ' || c == '\t')
600 else if (next == '\n') {
605 } else if (c == quotec) {
608 } else if (c == '\0') {
609 yyerror("syntax error");
612 if (p + 1 >= buf + sizeof(buf) - 1) {
613 yyerror("string too long");
618 yylval.v.string = strdup(buf);
619 if (yylval.v.string == NULL)
620 err(1, "yylex: strdup");
624 #define allowed_to_end_number(x) \
625 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
627 if (c == '-' || isdigit(c)) {
630 if ((unsigned)(p-buf) >= sizeof(buf)) {
631 yyerror("string too long");
635 } while (c != EOF && isdigit(c));
637 if (p == buf + 1 && buf[0] == '-')
639 if (c == EOF || allowed_to_end_number(c)) {
640 const char *errstr = NULL;
643 yylval.v.number = strtonum(buf, LLONG_MIN,
646 yyerror("\"%s\" invalid number: %s",
661 #define allowed_in_string(x) \
662 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
663 x != '{' && x != '}' && \
664 x != '!' && x != '=' && x != '#' && \
667 if (isalnum(c) || c == ':' || c == '_') {
670 if ((unsigned)(p-buf) >= sizeof(buf)) {
671 yyerror("string too long");
675 } while (c != EOF && (allowed_in_string(c)));
679 if (token == STRING) {
680 yylval.v.string = strdup(buf);
681 if (yylval.v.string == NULL)
682 err(1, "yylex: strdup");
687 yylval.lineno = file->lineno;
696 check_file_secrecy(int fd, const char *fname)
700 if (fstat(fd, &st)) {
701 log_warn("cannot stat %s", fname);
704 if (st.st_uid != 0 && st.st_uid != getuid()) {
705 log_warnx("%s: owner not root or current user", fname);
708 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
709 log_warnx("%s: group writable or world read/writable", fname);
716 newfile(const char *name, int secret)
720 nfile = calloc(1, sizeof(struct file));
725 nfile->name = strdup(name);
726 if (nfile->name == NULL) {
731 nfile->stream = fopen(nfile->name, "r");
732 if (nfile->stream == NULL) {
733 /* no warning, we don't require a conf file */
738 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
739 fclose(nfile->stream);
749 closefile(struct file *xfile)
751 fclose(xfile->stream);
757 add_default_server(void)
759 new_srv = conf_new_server(D_SITENAME);
760 log_debug("%s: adding default server %s", __func__, D_SITENAME);
764 parse_config(const char *filename, struct gotwebd *env)
766 struct sym *sym, *next;
768 if (config_init(env) == -1)
769 fatalx("failed to initialize configuration");
773 file = newfile(filename, 0);
775 add_default_server();
776 sockets_parse_sockets(env);
777 /* just return, as we don't require a conf file */
782 errors = file->errors;
785 /* Free macros and check which have not been used. */
786 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
787 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
788 fprintf(stderr, "warning: macro '%s' not used\n",
793 TAILQ_REMOVE(&symhead, sym, entry);
801 /* just add default server if no config specified */
802 if (gotwebd->server_cnt == 0)
803 add_default_server();
805 /* setup our listening sockets */
806 sockets_parse_sockets(env);
812 conf_new_server(const char *name)
814 struct server *srv = NULL;
816 srv = calloc(1, sizeof(*srv));
818 fatalx("%s: calloc", __func__);
820 n = strlcpy(srv->name, name, sizeof(srv->name));
821 if (n >= sizeof(srv->name))
822 fatalx("%s: strlcpy", __func__);
823 n = snprintf(srv->unix_socket_name,
824 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
826 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
827 fatalx("%s: snprintf", __func__);
828 n = strlcpy(srv->repos_path, D_GOTPATH,
829 sizeof(srv->repos_path));
830 if (n >= sizeof(srv->repos_path))
831 fatalx("%s: strlcpy", __func__);
832 n = strlcpy(srv->site_name, D_SITENAME,
833 sizeof(srv->site_name));
834 if (n >= sizeof(srv->site_name))
835 fatalx("%s: strlcpy", __func__);
836 n = strlcpy(srv->site_owner, D_SITEOWNER,
837 sizeof(srv->site_owner));
838 if (n >= sizeof(srv->site_owner))
839 fatalx("%s: strlcpy", __func__);
840 n = strlcpy(srv->site_link, D_SITELINK,
841 sizeof(srv->site_link));
842 if (n >= sizeof(srv->site_link))
843 fatalx("%s: strlcpy", __func__);
844 n = strlcpy(srv->logo, D_GOTLOGO,
846 if (n >= sizeof(srv->logo))
847 fatalx("%s: strlcpy", __func__);
848 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
849 if (n >= sizeof(srv->logo_url))
850 fatalx("%s: strlcpy", __func__);
851 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
852 if (n >= sizeof(srv->custom_css))
853 fatalx("%s: strlcpy", __func__);
855 srv->show_site_owner = D_SHOWSOWNER;
856 srv->show_repo_owner = D_SHOWROWNER;
857 srv->show_repo_age = D_SHOWAGE;
858 srv->show_repo_description = D_SHOWDESC;
859 srv->show_repo_cloneurl = D_SHOWURL;
861 srv->max_repos_display = D_MAXREPODISP;
862 srv->max_commits_display = D_MAXCOMMITDISP;
863 srv->max_repos = D_MAXREPO;
865 srv->unix_socket = 1;
866 srv->fcgi_socket = 0;
868 TAILQ_INIT(&srv->al);
869 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
870 gotwebd->server_cnt++;
876 symset(const char *nam, const char *val, int persist)
880 TAILQ_FOREACH(sym, &symhead, entry) {
881 if (strcmp(nam, sym->nam) == 0)
886 if (sym->persist == 1)
891 TAILQ_REMOVE(&symhead, sym, entry);
895 sym = calloc(1, sizeof(*sym));
899 sym->nam = strdup(nam);
900 if (sym->nam == NULL) {
904 sym->val = strdup(val);
905 if (sym->val == NULL) {
911 sym->persist = persist;
912 TAILQ_INSERT_TAIL(&symhead, sym, entry);
917 cmdline_symset(char *s)
923 val = strrchr(s, '=');
927 len = strlen(s) - strlen(val) + 1;
930 fatal("%s: malloc", __func__);
932 memcpy(&sym, s, len);
934 ret = symset(sym, val + 1, 1);
941 symget(const char *nam)
945 TAILQ_FOREACH(sym, &symhead, entry) {
946 if (strcmp(nam, sym->nam) == 0) {
955 getservice(const char *n)
961 llval = strtonum(n, 0, UINT16_MAX, &errstr);
963 s = getservbyname(n, "tcp");
965 s = getservbyname(n, "udp");
968 return ntohs(s->s_port);
971 return (unsigned short)llval;
975 host_v4(const char *s)
978 struct sockaddr_in *sain;
981 memset(&ina, 0, sizeof(ina));
982 if (inet_pton(AF_INET, s, &ina) != 1)
985 if ((h = calloc(1, sizeof(*h))) == NULL)
987 sain = (struct sockaddr_in *)&h->ss;
988 got_sockaddr_inet_init(sain, &ina);
989 if (sain->sin_addr.s_addr == INADDR_ANY)
990 h->prefixlen = 0; /* 0.0.0.0 address */
992 h->prefixlen = -1; /* host address */
997 host_v6(const char *s)
999 struct addrinfo hints, *res;
1000 struct sockaddr_in6 *sa_in6, *ra;
1001 struct address *h = NULL;
1003 memset(&hints, 0, sizeof(hints));
1004 hints.ai_family = AF_INET6;
1005 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1006 hints.ai_flags = AI_NUMERICHOST;
1007 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1008 if ((h = calloc(1, sizeof(*h))) == NULL)
1010 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1011 ra = (struct sockaddr_in6 *)res->ai_addr;
1012 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1014 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1015 sizeof(sa_in6->sin6_addr)) == 0)
1016 h->prefixlen = 0; /* any address */
1018 h->prefixlen = -1; /* host address */
1026 host_dns(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 = PF_UNSPEC;
1040 hints.ai_socktype = SOCK_DGRAM; /* 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;
1073 h->prefixlen = -1; /* host address */
1075 if (res->ai_family == AF_INET) {
1076 struct sockaddr_in *ra;
1077 sain = (struct sockaddr_in *)&h->ss;
1078 ra = (struct sockaddr_in *)res->ai_addr;
1079 got_sockaddr_inet_init(sain, &ra->sin_addr);
1081 struct sockaddr_in6 *ra;
1082 sin6 = (struct sockaddr_in6 *)&h->ss;
1083 ra = (struct sockaddr_in6 *)res->ai_addr;
1084 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1087 if (add_addr(new_srv, h))
1091 if (cnt == max && res) {
1092 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1100 host_if(const char *s, struct server *new_srv, int max,
1101 in_port_t port, const char *ifname, int ipproto)
1103 struct ifaddrs *ifap, *p;
1104 struct sockaddr_in *sain;
1105 struct sockaddr_in6 *sin6;
1109 if (getifaddrs(&ifap) == -1)
1110 fatal("getifaddrs");
1112 /* First search for IPv4 addresses */
1116 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1117 if (p->ifa_addr == NULL ||
1118 p->ifa_addr->sa_family != af ||
1119 (strcmp(s, p->ifa_name) != 0 &&
1120 !is_if_in_group(p->ifa_name, s)))
1122 if ((h = calloc(1, sizeof(*h))) == NULL)
1127 if (ifname != NULL) {
1128 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1129 sizeof(h->ifname)) {
1130 log_warnx("%s: interface name truncated",
1138 h->ipproto = ipproto;
1139 h->ss.ss_family = af;
1140 h->prefixlen = -1; /* host address */
1142 if (af == AF_INET) {
1143 struct sockaddr_in *ra;
1144 sain = (struct sockaddr_in *)&h->ss;
1145 ra = (struct sockaddr_in *)p->ifa_addr;
1146 got_sockaddr_inet_init(sain, &ra->sin_addr);
1148 struct sockaddr_in6 *ra;
1149 sin6 = (struct sockaddr_in6 *)&h->ss;
1150 ra = (struct sockaddr_in6 *)p->ifa_addr;
1151 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1155 if (add_addr(new_srv, h))
1159 if (af == AF_INET) {
1160 /* Next search for IPv6 addresses */
1166 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1174 host(const char *s, struct server *new_srv, int max,
1175 in_port_t port, const char *ifname, int ipproto)
1188 if (ifname != NULL) {
1189 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1190 sizeof(h->ifname)) {
1191 log_warnx("%s: interface name truncated",
1198 h->ipproto = ipproto;
1200 if (add_addr(new_srv, h))
1205 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1209 is_if_in_group(const char *ifname, const char *groupname)
1212 struct ifgroupreq ifgr;
1213 struct ifg_req *ifg;
1217 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1220 memset(&ifgr, 0, sizeof(ifgr));
1221 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1223 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1224 if (errno == EINVAL || errno == ENOTTY)
1226 err(1, "SIOCGIFGROUP");
1229 len = ifgr.ifgr_len;
1230 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1231 sizeof(struct ifg_req));
1232 if (ifgr.ifgr_groups == NULL)
1233 err(1, "getifgroups");
1234 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1235 err(1, "SIOCGIFGROUP");
1237 ifg = ifgr.ifgr_groups;
1238 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1239 len -= sizeof(struct ifg_req);
1240 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1245 free(ifgr.ifgr_groups);
1253 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1255 if (strcmp("", addr) == 0) {
1256 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1258 yyerror("invalid listen ip: %s",
1262 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1263 yyerror("invalid listen ip: %s", "::1");
1267 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1269 yyerror("invalid listen ip: %s", addr);
1277 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1278 const char *other_srv)
1282 char buf[INET6_ADDRSTRLEN];
1283 const char *addrstr;
1285 TAILQ_FOREACH(a, al, entry) {
1286 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1290 switch (h->ss.ss_family) {
1292 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1295 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1298 yyerror("unknown address family: %d", h->ss.ss_family);
1301 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1304 yyerror("server %s: duplicate fcgi listen "
1305 "address %s:%d, already used by server %s",
1306 new_srv, addrstr, h->port, other_srv);
1308 log_warnx("server: %s: duplicate fcgi listen "
1309 "address %s:%d", new_srv, addrstr, h->port);
1313 yyerror("server: %s: duplicate fcgi listen "
1314 "address, already used by server %s",
1315 new_srv, other_srv);
1317 log_warnx("server %s: duplicate fcgi listen "
1318 "address", new_srv);
1329 add_addr(struct server *new_srv, struct address *h)
1333 /* Address cannot be shared between different servers. */
1334 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1337 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1341 /* Tolerate duplicate address lines within the scope of a server. */
1342 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1343 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);