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>
51 #include "got_sockaddr.h"
53 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
55 TAILQ_ENTRY(file) entry;
61 struct file *newfile(const char *, int);
62 static void closefile(struct file *);
63 int check_file_secrecy(int, const char *);
66 int yyerror(const char *, ...)
67 __attribute__((__format__ (printf, 1, 2)))
68 __attribute__((__nonnull__ (1)));
69 int kw_cmp(const void *, const void *);
75 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
77 TAILQ_ENTRY(sym) entry;
84 int symset(const char *, const char *, int);
85 char *symget(const char *);
89 static struct gotwebd *gotwebd;
90 static struct server *new_srv;
91 static struct server *conf_new_server(const char *);
92 int getservice(const char *);
95 int get_addrs(const char *, struct server *, in_port_t);
96 int addr_dup_check(struct addresslist *, struct address *,
97 const char *, const char *);
98 int add_addr(struct server *, struct address *);
99 struct address *host_v4(const char *);
100 struct address *host_v6(const char *);
101 int host_dns(const char *, struct server *,
102 int, in_port_t, const char *, int);
103 int host_if(const char *, struct server *,
104 int, in_port_t, const char *, int);
105 int host(const char *, struct server *,
106 int, in_port_t, const char *, int);
107 int is_if_in_group(const char *, const char *);
120 %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
121 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
122 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
123 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK
124 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
126 %token <v.string> STRING
127 %type <v.port> fcgiport
128 %token <v.number> NUMBER
129 %type <v.number> boolean
136 | grammar server '\n'
140 if (strcasecmp($1, "1") == 0 ||
141 strcasecmp($1, "yes") == 0 ||
142 strcasecmp($1, "on") == 0)
144 else if (strcasecmp($1, "0") == 0 ||
145 strcasecmp($1, "off") == 0 ||
146 strcasecmp($1, "no") == 0)
149 yyerror("invalid boolean value '%s'", $1);
156 | NUMBER { $$ = $1; }
159 fcgiport : PORT NUMBER {
160 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
161 yyerror("invalid port: %lld", $2);
169 if ((val = getservice($2)) == -1) {
170 yyerror("invalid port: %s", $2);
180 main : PREFORK NUMBER {
181 gotwebd->prefork_gotwebd = $2;
184 n = strlcpy(gotwebd->httpd_chroot, $2,
185 sizeof(gotwebd->httpd_chroot));
186 if (n >= sizeof(gotwebd->httpd_chroot)) {
187 yyerror("%s: httpd_chroot truncated", __func__);
193 | UNIX_SOCKET boolean {
194 gotwebd->unix_socket = $2;
196 | UNIX_SOCKET_NAME STRING {
197 n = snprintf(gotwebd->unix_socket_name,
198 sizeof(gotwebd->unix_socket_name), "%s%s",
199 strlen(gotwebd->httpd_chroot) ?
200 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
202 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
203 yyerror("%s: unix_socket_name truncated",
212 server : SERVER STRING {
215 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
216 if (strcmp(srv->name, $2) == 0) {
217 yyerror("server name exists '%s'", $2);
223 new_srv = conf_new_server($2);
224 log_debug("adding server %s", $2);
230 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
231 if (strcmp(srv->name, $2) == 0) {
232 yyerror("server name exists '%s'", $2);
238 new_srv = conf_new_server($2);
239 log_debug("adding server %s", $2);
241 } '{' optnl serveropts2 '}' {
245 serveropts1 : REPOS_PATH STRING {
246 n = strlcpy(new_srv->repos_path, $2,
247 sizeof(new_srv->repos_path));
248 if (n >= sizeof(new_srv->repos_path)) {
249 yyerror("%s: repos_path truncated", __func__);
256 n = strlcpy(new_srv->site_name, $2,
257 sizeof(new_srv->site_name));
258 if (n >= sizeof(new_srv->site_name)) {
259 yyerror("%s: site_name truncated", __func__);
265 | SITE_OWNER STRING {
266 n = strlcpy(new_srv->site_owner, $2,
267 sizeof(new_srv->site_owner));
268 if (n >= sizeof(new_srv->site_owner)) {
269 yyerror("%s: site_owner truncated", __func__);
276 n = strlcpy(new_srv->site_link, $2,
277 sizeof(new_srv->site_link));
278 if (n >= sizeof(new_srv->site_link)) {
279 yyerror("%s: site_link truncated", __func__);
286 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
287 if (n >= sizeof(new_srv->logo)) {
288 yyerror("%s: logo truncated", __func__);
295 n = strlcpy(new_srv->logo_url, $2,
296 sizeof(new_srv->logo_url));
297 if (n >= sizeof(new_srv->logo_url)) {
298 yyerror("%s: logo_url truncated", __func__);
304 | CUSTOM_CSS STRING {
305 n = strlcpy(new_srv->custom_css, $2,
306 sizeof(new_srv->custom_css));
307 if (n >= sizeof(new_srv->custom_css)) {
308 yyerror("%s: custom_css truncated", __func__);
314 | LISTEN ON STRING fcgiport {
315 if (get_addrs($3, new_srv, $4) == -1) {
316 yyerror("could not get addrs");
319 new_srv->fcgi_socket = 1;
323 new_srv->max_repos = $2;
325 | SHOW_SITE_OWNER boolean {
326 new_srv->show_site_owner = $2;
328 | SHOW_REPO_OWNER boolean {
329 new_srv->show_repo_owner = $2;
331 | SHOW_REPO_AGE boolean {
332 new_srv->show_repo_age = $2;
334 | SHOW_REPO_DESCRIPTION boolean {
335 new_srv->show_repo_description = $2;
337 | SHOW_REPO_CLONEURL boolean {
338 new_srv->show_repo_cloneurl = $2;
340 | MAX_REPOS_DISPLAY NUMBER {
341 new_srv->max_repos_display = $2;
343 | MAX_COMMITS_DISPLAY NUMBER {
345 new_srv->max_commits_display = $2;
347 | UNIX_SOCKET boolean {
348 new_srv->unix_socket = $2;
350 | UNIX_SOCKET_NAME STRING {
351 n = snprintf(new_srv->unix_socket_name,
352 sizeof(new_srv->unix_socket_name), "%s%s",
353 strlen(gotwebd->httpd_chroot) ?
354 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
356 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
357 yyerror("%s: unix_socket_name truncated",
366 serveropts2 : serveropts2 serveropts1 nl
373 optnl : '\n' optnl /* zero or more newlines */
385 yyerror(const char *fmt, ...)
392 if (vasprintf(&msg, fmt, ap) == -1)
393 fatalx("yyerror vasprintf");
395 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
401 kw_cmp(const void *k, const void *e)
403 return (strcmp(k, ((const struct keywords *)e)->k_name));
409 /* This has to be sorted always. */
410 static const struct keywords keywords[] = {
411 { "chroot", CHROOT },
412 { "custom_css", CUSTOM_CSS },
413 { "listen", LISTEN },
415 { "logo_url" , LOGO_URL },
416 { "max_commits_display", MAX_COMMITS_DISPLAY },
417 { "max_repos", MAX_REPOS },
418 { "max_repos_display", MAX_REPOS_DISPLAY },
421 { "prefork", PREFORK },
422 { "repos_path", REPOS_PATH },
423 { "server", SERVER },
424 { "show_repo_age", SHOW_REPO_AGE },
425 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
426 { "show_repo_description", SHOW_REPO_DESCRIPTION },
427 { "show_repo_owner", SHOW_REPO_OWNER },
428 { "show_site_owner", SHOW_SITE_OWNER },
429 { "site_link", SITE_LINK },
430 { "site_name", SITE_NAME },
431 { "site_owner", SITE_OWNER },
432 { "unix_socket", UNIX_SOCKET },
433 { "unix_socket_name", UNIX_SOCKET_NAME },
435 const struct keywords *p;
437 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
438 sizeof(keywords[0]), kw_cmp);
446 #define MAXPUSHBACK 128
448 unsigned char *parsebuf;
450 unsigned char pushback_buffer[MAXPUSHBACK];
451 int pushback_index = 0;
459 /* Read character from the parsebuffer instead of input. */
460 if (parseindex >= 0) {
461 c = parsebuf[parseindex++];
470 return (pushback_buffer[--pushback_index]);
473 c = getc(file->stream);
475 yyerror("reached end of file while parsing "
480 c = getc(file->stream);
482 next = getc(file->stream);
487 yylval.lineno = file->lineno;
489 c = getc(file->stream);
505 if (pushback_index < MAXPUSHBACK-1)
506 return (pushback_buffer[pushback_index++] = c);
518 /* Skip to either EOF or the first real EOL. */
521 c = pushback_buffer[--pushback_index];
537 unsigned char buf[8096];
538 unsigned char *p, *val;
545 while (c == ' ' || c == '\t')
546 c = lgetc(0); /* nothing */
548 yylval.lineno = file->lineno;
551 while (c != '\n' && c != EOF)
552 c = lgetc(0); /* nothing */
554 if (c == '$' && parsebuf == NULL) {
560 if (p + 1 >= buf + sizeof(buf) - 1) {
561 yyerror("string too long");
564 if (isalnum(c) || c == '_') {
574 yyerror("macro '%s' not defined", buf);
593 } else if (c == '\\') {
594 next = lgetc(quotec);
597 if (next == quotec || c == ' ' || c == '\t')
599 else if (next == '\n') {
604 } else if (c == quotec) {
607 } else if (c == '\0') {
608 yyerror("syntax error");
611 if (p + 1 >= buf + sizeof(buf) - 1) {
612 yyerror("string too long");
617 yylval.v.string = strdup(buf);
618 if (yylval.v.string == NULL)
619 err(1, "yylex: strdup");
623 #define allowed_to_end_number(x) \
624 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
626 if (c == '-' || isdigit(c)) {
629 if ((unsigned)(p-buf) >= sizeof(buf)) {
630 yyerror("string too long");
634 } while (c != EOF && isdigit(c));
636 if (p == buf + 1 && buf[0] == '-')
638 if (c == EOF || allowed_to_end_number(c)) {
639 const char *errstr = NULL;
642 yylval.v.number = strtonum(buf, LLONG_MIN,
645 yyerror("\"%s\" invalid number: %s",
660 #define allowed_in_string(x) \
661 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
662 x != '{' && x != '}' && \
663 x != '!' && x != '=' && x != '#' && \
666 if (isalnum(c) || c == ':' || c == '_') {
669 if ((unsigned)(p-buf) >= sizeof(buf)) {
670 yyerror("string too long");
674 } while (c != EOF && (allowed_in_string(c)));
678 if (token == STRING) {
679 yylval.v.string = strdup(buf);
680 if (yylval.v.string == NULL)
681 err(1, "yylex: strdup");
686 yylval.lineno = file->lineno;
695 check_file_secrecy(int fd, const char *fname)
699 if (fstat(fd, &st)) {
700 log_warn("cannot stat %s", fname);
703 if (st.st_uid != 0 && st.st_uid != getuid()) {
704 log_warnx("%s: owner not root or current user", fname);
707 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
708 log_warnx("%s: group writable or world read/writable", fname);
715 newfile(const char *name, int secret)
719 nfile = calloc(1, sizeof(struct file));
724 nfile->name = strdup(name);
725 if (nfile->name == NULL) {
730 nfile->stream = fopen(nfile->name, "r");
731 if (nfile->stream == NULL) {
732 /* no warning, we don't require a conf file */
737 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
738 fclose(nfile->stream);
748 closefile(struct file *xfile)
750 fclose(xfile->stream);
756 add_default_server(void)
758 new_srv = conf_new_server(D_SITENAME);
759 log_debug("%s: adding default server %s", __func__, D_SITENAME);
763 parse_config(const char *filename, struct gotwebd *env)
765 struct sym *sym, *next;
767 if (config_init(env) == -1)
768 fatalx("failed to initialize configuration");
772 file = newfile(filename, 0);
774 add_default_server();
775 sockets_parse_sockets(env);
776 /* just return, as we don't require a conf file */
781 errors = file->errors;
784 /* Free macros and check which have not been used. */
785 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
786 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
787 fprintf(stderr, "warning: macro '%s' not used\n",
792 TAILQ_REMOVE(&symhead, sym, entry);
800 /* just add default server if no config specified */
801 if (gotwebd->server_cnt == 0)
802 add_default_server();
804 /* setup our listening sockets */
805 sockets_parse_sockets(env);
811 conf_new_server(const char *name)
813 struct server *srv = NULL;
815 srv = calloc(1, sizeof(*srv));
817 fatalx("%s: calloc", __func__);
819 n = strlcpy(srv->name, name, sizeof(srv->name));
820 if (n >= sizeof(srv->name))
821 fatalx("%s: strlcpy", __func__);
822 n = snprintf(srv->unix_socket_name,
823 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
825 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
826 fatalx("%s: snprintf", __func__);
827 n = strlcpy(srv->repos_path, D_GOTPATH,
828 sizeof(srv->repos_path));
829 if (n >= sizeof(srv->repos_path))
830 fatalx("%s: strlcpy", __func__);
831 n = strlcpy(srv->site_name, D_SITENAME,
832 sizeof(srv->site_name));
833 if (n >= sizeof(srv->site_name))
834 fatalx("%s: strlcpy", __func__);
835 n = strlcpy(srv->site_owner, D_SITEOWNER,
836 sizeof(srv->site_owner));
837 if (n >= sizeof(srv->site_owner))
838 fatalx("%s: strlcpy", __func__);
839 n = strlcpy(srv->site_link, D_SITELINK,
840 sizeof(srv->site_link));
841 if (n >= sizeof(srv->site_link))
842 fatalx("%s: strlcpy", __func__);
843 n = strlcpy(srv->logo, D_GOTLOGO,
845 if (n >= sizeof(srv->logo))
846 fatalx("%s: strlcpy", __func__);
847 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
848 if (n >= sizeof(srv->logo_url))
849 fatalx("%s: strlcpy", __func__);
850 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
851 if (n >= sizeof(srv->custom_css))
852 fatalx("%s: strlcpy", __func__);
854 srv->show_site_owner = D_SHOWSOWNER;
855 srv->show_repo_owner = D_SHOWROWNER;
856 srv->show_repo_age = D_SHOWAGE;
857 srv->show_repo_description = D_SHOWDESC;
858 srv->show_repo_cloneurl = D_SHOWURL;
860 srv->max_repos_display = D_MAXREPODISP;
861 srv->max_commits_display = D_MAXCOMMITDISP;
862 srv->max_repos = D_MAXREPO;
864 srv->unix_socket = 1;
865 srv->fcgi_socket = 0;
867 TAILQ_INIT(&srv->al);
868 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
869 gotwebd->server_cnt++;
875 symset(const char *nam, const char *val, int persist)
879 TAILQ_FOREACH(sym, &symhead, entry) {
880 if (strcmp(nam, sym->nam) == 0)
885 if (sym->persist == 1)
890 TAILQ_REMOVE(&symhead, sym, entry);
894 sym = calloc(1, sizeof(*sym));
898 sym->nam = strdup(nam);
899 if (sym->nam == NULL) {
903 sym->val = strdup(val);
904 if (sym->val == NULL) {
910 sym->persist = persist;
911 TAILQ_INSERT_TAIL(&symhead, sym, entry);
916 cmdline_symset(char *s)
922 val = strrchr(s, '=');
926 len = strlen(s) - strlen(val) + 1;
929 fatal("%s: malloc", __func__);
931 memcpy(&sym, s, len);
933 ret = symset(sym, val + 1, 1);
940 symget(const char *nam)
944 TAILQ_FOREACH(sym, &symhead, entry) {
945 if (strcmp(nam, sym->nam) == 0) {
954 getservice(const char *n)
960 llval = strtonum(n, 0, UINT16_MAX, &errstr);
962 s = getservbyname(n, "tcp");
964 s = getservbyname(n, "udp");
967 return ntohs(s->s_port);
970 return (unsigned short)llval;
974 host_v4(const char *s)
977 struct sockaddr_in *sain;
980 memset(&ina, 0, sizeof(ina));
981 if (inet_pton(AF_INET, s, &ina) != 1)
984 if ((h = calloc(1, sizeof(*h))) == NULL)
986 sain = (struct sockaddr_in *)&h->ss;
987 got_sockaddr_inet_init(sain, &ina);
988 if (sain->sin_addr.s_addr == INADDR_ANY)
989 h->prefixlen = 0; /* 0.0.0.0 address */
991 h->prefixlen = -1; /* host address */
996 host_v6(const char *s)
998 struct addrinfo hints, *res;
999 struct sockaddr_in6 *sa_in6, *ra;
1000 struct address *h = NULL;
1002 memset(&hints, 0, sizeof(hints));
1003 hints.ai_family = AF_INET6;
1004 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1005 hints.ai_flags = AI_NUMERICHOST;
1006 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1007 if ((h = calloc(1, sizeof(*h))) == NULL)
1009 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1010 ra = (struct sockaddr_in6 *)res->ai_addr;
1011 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1013 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1014 sizeof(sa_in6->sin6_addr)) == 0)
1015 h->prefixlen = 0; /* any address */
1017 h->prefixlen = -1; /* host address */
1025 host_dns(const char *s, struct server *new_srv, int max,
1026 in_port_t port, const char *ifname, int ipproto)
1028 struct addrinfo hints, *res0, *res;
1030 struct sockaddr_in *sain;
1031 struct sockaddr_in6 *sin6;
1034 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1037 memset(&hints, 0, sizeof(hints));
1038 hints.ai_family = PF_UNSPEC;
1039 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1040 hints.ai_flags = AI_ADDRCONFIG;
1041 error = getaddrinfo(s, NULL, &hints, &res0);
1042 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1045 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1046 gai_strerror(error));
1050 for (res = res0; res && cnt < max; res = res->ai_next) {
1051 if (res->ai_family != AF_INET &&
1052 res->ai_family != AF_INET6)
1054 if ((h = calloc(1, sizeof(*h))) == NULL)
1059 if (ifname != NULL) {
1060 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1061 sizeof(h->ifname)) {
1062 log_warnx("%s: interface name truncated",
1070 h->ipproto = ipproto;
1071 h->ss.ss_family = res->ai_family;
1072 h->prefixlen = -1; /* host address */
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;
1139 h->prefixlen = -1; /* host address */
1141 if (af == AF_INET) {
1142 struct sockaddr_in *ra;
1143 sain = (struct sockaddr_in *)&h->ss;
1144 ra = (struct sockaddr_in *)p->ifa_addr;
1145 got_sockaddr_inet_init(sain, &ra->sin_addr);
1147 struct sockaddr_in6 *ra;
1148 sin6 = (struct sockaddr_in6 *)&h->ss;
1149 ra = (struct sockaddr_in6 *)p->ifa_addr;
1150 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1154 if (add_addr(new_srv, h))
1158 if (af == AF_INET) {
1159 /* Next search for IPv6 addresses */
1165 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1173 host(const char *s, struct server *new_srv, int max,
1174 in_port_t port, const char *ifname, int ipproto)
1187 if (ifname != NULL) {
1188 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1189 sizeof(h->ifname)) {
1190 log_warnx("%s: interface name truncated",
1197 h->ipproto = ipproto;
1199 if (add_addr(new_srv, h))
1204 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1208 is_if_in_group(const char *ifname, const char *groupname)
1210 /* TA: Check this... */
1211 #ifdef HAVE_STRUCT_IFGROUPREQ
1213 struct ifgroupreq ifgr;
1214 struct ifg_req *ifg;
1218 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1221 memset(&ifgr, 0, sizeof(ifgr));
1222 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1224 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1225 if (errno == EINVAL || errno == ENOTTY)
1227 err(1, "SIOCGIFGROUP");
1230 len = ifgr.ifgr_len;
1231 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1232 sizeof(struct ifg_req));
1233 if (ifgr.ifgr_groups == NULL)
1234 err(1, "getifgroups");
1235 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1236 err(1, "SIOCGIFGROUP");
1238 ifg = ifgr.ifgr_groups;
1239 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1240 len -= sizeof(struct ifg_req);
1241 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1246 free(ifgr.ifgr_groups);
1257 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1259 if (strcmp("", addr) == 0) {
1260 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1262 yyerror("invalid listen ip: %s",
1266 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1267 yyerror("invalid listen ip: %s", "::1");
1271 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1273 yyerror("invalid listen ip: %s", addr);
1281 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1282 const char *other_srv)
1286 char buf[INET6_ADDRSTRLEN];
1287 const char *addrstr;
1289 TAILQ_FOREACH(a, al, entry) {
1290 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1294 switch (h->ss.ss_family) {
1296 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1299 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1302 yyerror("unknown address family: %d", h->ss.ss_family);
1305 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1308 yyerror("server %s: duplicate fcgi listen "
1309 "address %s:%d, already used by server %s",
1310 new_srv, addrstr, h->port, other_srv);
1312 log_warnx("server: %s: duplicate fcgi listen "
1313 "address %s:%d", new_srv, addrstr, h->port);
1317 yyerror("server: %s: duplicate fcgi listen "
1318 "address, already used by server %s",
1319 new_srv, other_srv);
1321 log_warnx("server %s: duplicate fcgi listen "
1322 "address", new_srv);
1333 add_addr(struct server *new_srv, struct address *h)
1337 /* Address cannot be shared between different servers. */
1338 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1341 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1345 /* Tolerate duplicate address lines within the scope of a server. */
1346 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1347 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);