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
133 grammar : /* empty */
135 | grammar varset '\n'
137 | grammar server '\n'
138 | grammar error '\n' { file->errors++; }
141 varset : STRING '=' STRING {
144 if (isspace((unsigned char)*s)) {
145 yyerror("macro name cannot contain "
152 if (symset($1, $3, 0) == -1)
153 fatal("cannot store variable");
160 if (strcasecmp($1, "1") == 0 ||
161 strcasecmp($1, "yes") == 0 ||
162 strcasecmp($1, "on") == 0)
164 else if (strcasecmp($1, "0") == 0 ||
165 strcasecmp($1, "off") == 0 ||
166 strcasecmp($1, "no") == 0)
169 yyerror("invalid boolean value '%s'", $1);
176 | NUMBER { $$ = $1; }
179 fcgiport : PORT NUMBER {
180 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
181 yyerror("invalid port: %lld", $2);
189 if ((val = getservice($2)) == -1) {
190 yyerror("invalid port: %s", $2);
200 main : PREFORK NUMBER {
201 gotwebd->prefork_gotwebd = $2;
204 n = strlcpy(gotwebd->httpd_chroot, $2,
205 sizeof(gotwebd->httpd_chroot));
206 if (n >= sizeof(gotwebd->httpd_chroot)) {
207 yyerror("%s: httpd_chroot truncated", __func__);
213 | UNIX_SOCKET boolean {
214 gotwebd->unix_socket = $2;
216 | UNIX_SOCKET_NAME STRING {
217 n = snprintf(gotwebd->unix_socket_name,
218 sizeof(gotwebd->unix_socket_name), "%s%s",
219 strlen(gotwebd->httpd_chroot) ?
220 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
222 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
223 yyerror("%s: unix_socket_name truncated",
232 server : SERVER STRING {
235 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
236 if (strcmp(srv->name, $2) == 0) {
237 yyerror("server name exists '%s'", $2);
243 new_srv = conf_new_server($2);
244 log_debug("adding server %s", $2);
250 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
251 if (strcmp(srv->name, $2) == 0) {
252 yyerror("server name exists '%s'", $2);
258 new_srv = conf_new_server($2);
259 log_debug("adding server %s", $2);
261 } '{' optnl serveropts2 '}' {
265 serveropts1 : REPOS_PATH STRING {
266 n = strlcpy(new_srv->repos_path, $2,
267 sizeof(new_srv->repos_path));
268 if (n >= sizeof(new_srv->repos_path)) {
269 yyerror("%s: repos_path truncated", __func__);
276 n = strlcpy(new_srv->site_name, $2,
277 sizeof(new_srv->site_name));
278 if (n >= sizeof(new_srv->site_name)) {
279 yyerror("%s: site_name truncated", __func__);
285 | SITE_OWNER STRING {
286 n = strlcpy(new_srv->site_owner, $2,
287 sizeof(new_srv->site_owner));
288 if (n >= sizeof(new_srv->site_owner)) {
289 yyerror("%s: site_owner truncated", __func__);
296 n = strlcpy(new_srv->site_link, $2,
297 sizeof(new_srv->site_link));
298 if (n >= sizeof(new_srv->site_link)) {
299 yyerror("%s: site_link truncated", __func__);
306 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
307 if (n >= sizeof(new_srv->logo)) {
308 yyerror("%s: logo truncated", __func__);
315 n = strlcpy(new_srv->logo_url, $2,
316 sizeof(new_srv->logo_url));
317 if (n >= sizeof(new_srv->logo_url)) {
318 yyerror("%s: logo_url truncated", __func__);
324 | CUSTOM_CSS STRING {
325 n = strlcpy(new_srv->custom_css, $2,
326 sizeof(new_srv->custom_css));
327 if (n >= sizeof(new_srv->custom_css)) {
328 yyerror("%s: custom_css truncated", __func__);
334 | LISTEN ON STRING fcgiport {
335 if (get_addrs($3, new_srv, $4) == -1) {
336 yyerror("could not get addrs");
339 new_srv->fcgi_socket = 1;
343 new_srv->max_repos = $2;
345 | SHOW_SITE_OWNER boolean {
346 new_srv->show_site_owner = $2;
348 | SHOW_REPO_OWNER boolean {
349 new_srv->show_repo_owner = $2;
351 | SHOW_REPO_AGE boolean {
352 new_srv->show_repo_age = $2;
354 | SHOW_REPO_DESCRIPTION boolean {
355 new_srv->show_repo_description = $2;
357 | SHOW_REPO_CLONEURL boolean {
358 new_srv->show_repo_cloneurl = $2;
360 | MAX_REPOS_DISPLAY NUMBER {
361 new_srv->max_repos_display = $2;
363 | MAX_COMMITS_DISPLAY NUMBER {
365 new_srv->max_commits_display = $2;
367 | UNIX_SOCKET boolean {
368 new_srv->unix_socket = $2;
370 | UNIX_SOCKET_NAME STRING {
371 n = snprintf(new_srv->unix_socket_name,
372 sizeof(new_srv->unix_socket_name), "%s%s",
373 strlen(gotwebd->httpd_chroot) ?
374 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
376 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
377 yyerror("%s: unix_socket_name truncated",
386 serveropts2 : serveropts2 serveropts1 nl
393 optnl : '\n' optnl /* zero or more newlines */
405 yyerror(const char *fmt, ...)
412 if (vasprintf(&msg, fmt, ap) == -1)
413 fatalx("yyerror vasprintf");
415 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
421 kw_cmp(const void *k, const void *e)
423 return (strcmp(k, ((const struct keywords *)e)->k_name));
429 /* This has to be sorted always. */
430 static const struct keywords keywords[] = {
431 { "chroot", CHROOT },
432 { "custom_css", CUSTOM_CSS },
433 { "listen", LISTEN },
435 { "logo_url" , LOGO_URL },
436 { "max_commits_display", MAX_COMMITS_DISPLAY },
437 { "max_repos", MAX_REPOS },
438 { "max_repos_display", MAX_REPOS_DISPLAY },
441 { "prefork", PREFORK },
442 { "repos_path", REPOS_PATH },
443 { "server", SERVER },
444 { "show_repo_age", SHOW_REPO_AGE },
445 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
446 { "show_repo_description", SHOW_REPO_DESCRIPTION },
447 { "show_repo_owner", SHOW_REPO_OWNER },
448 { "show_site_owner", SHOW_SITE_OWNER },
449 { "site_link", SITE_LINK },
450 { "site_name", SITE_NAME },
451 { "site_owner", SITE_OWNER },
452 { "unix_socket", UNIX_SOCKET },
453 { "unix_socket_name", UNIX_SOCKET_NAME },
455 const struct keywords *p;
457 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
458 sizeof(keywords[0]), kw_cmp);
466 #define MAXPUSHBACK 128
468 unsigned char *parsebuf;
470 unsigned char pushback_buffer[MAXPUSHBACK];
471 int pushback_index = 0;
479 /* Read character from the parsebuffer instead of input. */
480 if (parseindex >= 0) {
481 c = parsebuf[parseindex++];
490 return (pushback_buffer[--pushback_index]);
493 c = getc(file->stream);
495 yyerror("reached end of file while parsing "
500 c = getc(file->stream);
502 next = getc(file->stream);
507 yylval.lineno = file->lineno;
509 c = getc(file->stream);
525 if (pushback_index < MAXPUSHBACK-1)
526 return (pushback_buffer[pushback_index++] = c);
538 /* Skip to either EOF or the first real EOL. */
541 c = pushback_buffer[--pushback_index];
557 unsigned char buf[8096];
558 unsigned char *p, *val;
565 while (c == ' ' || c == '\t')
566 c = lgetc(0); /* nothing */
568 yylval.lineno = file->lineno;
571 while (c != '\n' && c != EOF)
572 c = lgetc(0); /* nothing */
574 if (c == '$' && parsebuf == NULL) {
580 if (p + 1 >= buf + sizeof(buf) - 1) {
581 yyerror("string too long");
584 if (isalnum(c) || c == '_') {
594 yyerror("macro '%s' not defined", buf);
613 } else if (c == '\\') {
614 next = lgetc(quotec);
617 if (next == quotec || c == ' ' || c == '\t')
619 else if (next == '\n') {
624 } else if (c == quotec) {
627 } else if (c == '\0') {
628 yyerror("syntax error");
631 if (p + 1 >= buf + sizeof(buf) - 1) {
632 yyerror("string too long");
637 yylval.v.string = strdup(buf);
638 if (yylval.v.string == NULL)
639 err(1, "yylex: strdup");
643 #define allowed_to_end_number(x) \
644 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
646 if (c == '-' || isdigit(c)) {
649 if ((unsigned)(p-buf) >= sizeof(buf)) {
650 yyerror("string too long");
654 } while (c != EOF && isdigit(c));
656 if (p == buf + 1 && buf[0] == '-')
658 if (c == EOF || allowed_to_end_number(c)) {
659 const char *errstr = NULL;
662 yylval.v.number = strtonum(buf, LLONG_MIN,
665 yyerror("\"%s\" invalid number: %s",
680 #define allowed_in_string(x) \
681 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
682 x != '{' && x != '}' && \
683 x != '!' && x != '=' && x != '#' && \
686 if (isalnum(c) || c == ':' || c == '_') {
689 if ((unsigned)(p-buf) >= sizeof(buf)) {
690 yyerror("string too long");
694 } while (c != EOF && (allowed_in_string(c)));
698 if (token == STRING) {
699 yylval.v.string = strdup(buf);
700 if (yylval.v.string == NULL)
701 err(1, "yylex: strdup");
706 yylval.lineno = file->lineno;
715 check_file_secrecy(int fd, const char *fname)
719 if (fstat(fd, &st)) {
720 log_warn("cannot stat %s", fname);
723 if (st.st_uid != 0 && st.st_uid != getuid()) {
724 log_warnx("%s: owner not root or current user", fname);
727 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
728 log_warnx("%s: group writable or world read/writable", fname);
735 newfile(const char *name, int secret)
739 nfile = calloc(1, sizeof(struct file));
744 nfile->name = strdup(name);
745 if (nfile->name == NULL) {
750 nfile->stream = fopen(nfile->name, "r");
751 if (nfile->stream == NULL) {
752 /* no warning, we don't require a conf file */
757 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
758 fclose(nfile->stream);
768 closefile(struct file *xfile)
770 fclose(xfile->stream);
776 add_default_server(void)
778 new_srv = conf_new_server(D_SITENAME);
779 log_debug("%s: adding default server %s", __func__, D_SITENAME);
783 parse_config(const char *filename, struct gotwebd *env)
785 struct sym *sym, *next;
787 if (config_init(env) == -1)
788 fatalx("failed to initialize configuration");
792 file = newfile(filename, 0);
794 add_default_server();
795 sockets_parse_sockets(env);
796 /* just return, as we don't require a conf file */
801 errors = file->errors;
804 /* Free macros and check which have not been used. */
805 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
806 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
807 fprintf(stderr, "warning: macro '%s' not used\n",
812 TAILQ_REMOVE(&symhead, sym, entry);
820 /* just add default server if no config specified */
821 if (gotwebd->server_cnt == 0)
822 add_default_server();
824 /* setup our listening sockets */
825 sockets_parse_sockets(env);
831 conf_new_server(const char *name)
833 struct server *srv = NULL;
835 srv = calloc(1, sizeof(*srv));
837 fatalx("%s: calloc", __func__);
839 n = strlcpy(srv->name, name, sizeof(srv->name));
840 if (n >= sizeof(srv->name))
841 fatalx("%s: strlcpy", __func__);
842 n = snprintf(srv->unix_socket_name,
843 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
845 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
846 fatalx("%s: snprintf", __func__);
847 n = strlcpy(srv->repos_path, D_GOTPATH,
848 sizeof(srv->repos_path));
849 if (n >= sizeof(srv->repos_path))
850 fatalx("%s: strlcpy", __func__);
851 n = strlcpy(srv->site_name, D_SITENAME,
852 sizeof(srv->site_name));
853 if (n >= sizeof(srv->site_name))
854 fatalx("%s: strlcpy", __func__);
855 n = strlcpy(srv->site_owner, D_SITEOWNER,
856 sizeof(srv->site_owner));
857 if (n >= sizeof(srv->site_owner))
858 fatalx("%s: strlcpy", __func__);
859 n = strlcpy(srv->site_link, D_SITELINK,
860 sizeof(srv->site_link));
861 if (n >= sizeof(srv->site_link))
862 fatalx("%s: strlcpy", __func__);
863 n = strlcpy(srv->logo, D_GOTLOGO,
865 if (n >= sizeof(srv->logo))
866 fatalx("%s: strlcpy", __func__);
867 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
868 if (n >= sizeof(srv->logo_url))
869 fatalx("%s: strlcpy", __func__);
870 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
871 if (n >= sizeof(srv->custom_css))
872 fatalx("%s: strlcpy", __func__);
874 srv->show_site_owner = D_SHOWSOWNER;
875 srv->show_repo_owner = D_SHOWROWNER;
876 srv->show_repo_age = D_SHOWAGE;
877 srv->show_repo_description = D_SHOWDESC;
878 srv->show_repo_cloneurl = D_SHOWURL;
880 srv->max_repos_display = D_MAXREPODISP;
881 srv->max_commits_display = D_MAXCOMMITDISP;
882 srv->max_repos = D_MAXREPO;
884 srv->unix_socket = 1;
885 srv->fcgi_socket = 0;
887 TAILQ_INIT(&srv->al);
888 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
889 gotwebd->server_cnt++;
895 symset(const char *nam, const char *val, int persist)
899 TAILQ_FOREACH(sym, &symhead, entry) {
900 if (strcmp(nam, sym->nam) == 0)
905 if (sym->persist == 1)
910 TAILQ_REMOVE(&symhead, sym, entry);
914 sym = calloc(1, sizeof(*sym));
918 sym->nam = strdup(nam);
919 if (sym->nam == NULL) {
923 sym->val = strdup(val);
924 if (sym->val == NULL) {
930 sym->persist = persist;
931 TAILQ_INSERT_TAIL(&symhead, sym, entry);
936 cmdline_symset(char *s)
941 val = strrchr(s, '=');
945 sym = strndup(s, val - s);
947 fatal("%s: strndup", __func__);
949 ret = symset(sym, val + 1, 1);
956 symget(const char *nam)
960 TAILQ_FOREACH(sym, &symhead, entry) {
961 if (strcmp(nam, sym->nam) == 0) {
970 getservice(const char *n)
976 llval = strtonum(n, 0, UINT16_MAX, &errstr);
978 s = getservbyname(n, "tcp");
980 s = getservbyname(n, "udp");
983 return ntohs(s->s_port);
986 return (unsigned short)llval;
990 host_v4(const char *s)
993 struct sockaddr_in *sain;
996 memset(&ina, 0, sizeof(ina));
997 if (inet_pton(AF_INET, s, &ina) != 1)
1000 if ((h = calloc(1, sizeof(*h))) == NULL)
1002 sain = (struct sockaddr_in *)&h->ss;
1003 got_sockaddr_inet_init(sain, &ina);
1004 if (sain->sin_addr.s_addr == INADDR_ANY)
1005 h->prefixlen = 0; /* 0.0.0.0 address */
1007 h->prefixlen = -1; /* host address */
1012 host_v6(const char *s)
1014 struct addrinfo hints, *res;
1015 struct sockaddr_in6 *sa_in6, *ra;
1016 struct address *h = NULL;
1018 memset(&hints, 0, sizeof(hints));
1019 hints.ai_family = AF_INET6;
1020 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1021 hints.ai_flags = AI_NUMERICHOST;
1022 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1023 if ((h = calloc(1, sizeof(*h))) == NULL)
1025 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1026 ra = (struct sockaddr_in6 *)res->ai_addr;
1027 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1029 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1030 sizeof(sa_in6->sin6_addr)) == 0)
1031 h->prefixlen = 0; /* any address */
1033 h->prefixlen = -1; /* host address */
1041 host_dns(const char *s, struct server *new_srv, int max,
1042 in_port_t port, const char *ifname, int ipproto)
1044 struct addrinfo hints, *res0, *res;
1046 struct sockaddr_in *sain;
1047 struct sockaddr_in6 *sin6;
1050 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1053 memset(&hints, 0, sizeof(hints));
1054 hints.ai_family = PF_UNSPEC;
1055 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1056 hints.ai_flags = AI_ADDRCONFIG;
1057 error = getaddrinfo(s, NULL, &hints, &res0);
1058 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1061 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1062 gai_strerror(error));
1066 for (res = res0; res && cnt < max; res = res->ai_next) {
1067 if (res->ai_family != AF_INET &&
1068 res->ai_family != AF_INET6)
1070 if ((h = calloc(1, sizeof(*h))) == NULL)
1075 if (ifname != NULL) {
1076 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1077 sizeof(h->ifname)) {
1078 log_warnx("%s: interface name truncated",
1086 h->ipproto = ipproto;
1087 h->ss.ss_family = res->ai_family;
1088 h->prefixlen = -1; /* host address */
1090 if (res->ai_family == AF_INET) {
1091 struct sockaddr_in *ra;
1092 sain = (struct sockaddr_in *)&h->ss;
1093 ra = (struct sockaddr_in *)res->ai_addr;
1094 got_sockaddr_inet_init(sain, &ra->sin_addr);
1096 struct sockaddr_in6 *ra;
1097 sin6 = (struct sockaddr_in6 *)&h->ss;
1098 ra = (struct sockaddr_in6 *)res->ai_addr;
1099 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1102 if (add_addr(new_srv, h))
1106 if (cnt == max && res) {
1107 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1115 host_if(const char *s, struct server *new_srv, int max,
1116 in_port_t port, const char *ifname, int ipproto)
1118 struct ifaddrs *ifap, *p;
1119 struct sockaddr_in *sain;
1120 struct sockaddr_in6 *sin6;
1124 if (getifaddrs(&ifap) == -1)
1125 fatal("getifaddrs");
1127 /* First search for IPv4 addresses */
1131 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1132 if (p->ifa_addr == NULL ||
1133 p->ifa_addr->sa_family != af ||
1134 (strcmp(s, p->ifa_name) != 0 &&
1135 !is_if_in_group(p->ifa_name, s)))
1137 if ((h = calloc(1, sizeof(*h))) == NULL)
1142 if (ifname != NULL) {
1143 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1144 sizeof(h->ifname)) {
1145 log_warnx("%s: interface name truncated",
1153 h->ipproto = ipproto;
1154 h->ss.ss_family = af;
1155 h->prefixlen = -1; /* host address */
1157 if (af == AF_INET) {
1158 struct sockaddr_in *ra;
1159 sain = (struct sockaddr_in *)&h->ss;
1160 ra = (struct sockaddr_in *)p->ifa_addr;
1161 got_sockaddr_inet_init(sain, &ra->sin_addr);
1163 struct sockaddr_in6 *ra;
1164 sin6 = (struct sockaddr_in6 *)&h->ss;
1165 ra = (struct sockaddr_in6 *)p->ifa_addr;
1166 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1170 if (add_addr(new_srv, h))
1174 if (af == AF_INET) {
1175 /* Next search for IPv6 addresses */
1181 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1189 host(const char *s, struct server *new_srv, int max,
1190 in_port_t port, const char *ifname, int ipproto)
1203 if (ifname != NULL) {
1204 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1205 sizeof(h->ifname)) {
1206 log_warnx("%s: interface name truncated",
1213 h->ipproto = ipproto;
1215 if (add_addr(new_srv, h))
1220 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1224 is_if_in_group(const char *ifname, const char *groupname)
1226 /* TA: Check this... */
1227 #ifdef HAVE_STRUCT_IFGROUPREQ
1229 struct ifgroupreq ifgr;
1230 struct ifg_req *ifg;
1234 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1237 memset(&ifgr, 0, sizeof(ifgr));
1238 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1240 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1241 if (errno == EINVAL || errno == ENOTTY)
1243 err(1, "SIOCGIFGROUP");
1246 len = ifgr.ifgr_len;
1247 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1248 sizeof(struct ifg_req));
1249 if (ifgr.ifgr_groups == NULL)
1250 err(1, "getifgroups");
1251 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1252 err(1, "SIOCGIFGROUP");
1254 ifg = ifgr.ifgr_groups;
1255 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1256 len -= sizeof(struct ifg_req);
1257 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1262 free(ifgr.ifgr_groups);
1273 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1275 if (strcmp("", addr) == 0) {
1276 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1278 yyerror("invalid listen ip: %s",
1282 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1283 yyerror("invalid listen ip: %s", "::1");
1287 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1289 yyerror("invalid listen ip: %s", addr);
1297 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1298 const char *other_srv)
1302 char buf[INET6_ADDRSTRLEN];
1303 const char *addrstr;
1305 TAILQ_FOREACH(a, al, entry) {
1306 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1310 switch (h->ss.ss_family) {
1312 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1315 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1318 yyerror("unknown address family: %d", h->ss.ss_family);
1321 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1324 yyerror("server %s: duplicate fcgi listen "
1325 "address %s:%d, already used by server %s",
1326 new_srv, addrstr, h->port, other_srv);
1328 log_warnx("server: %s: duplicate fcgi listen "
1329 "address %s:%d", new_srv, addrstr, h->port);
1333 yyerror("server: %s: duplicate fcgi listen "
1334 "address, already used by server %s",
1335 new_srv, other_srv);
1337 log_warnx("server %s: duplicate fcgi listen "
1338 "address", new_srv);
1349 add_addr(struct server *new_srv, struct address *h)
1353 /* Address cannot be shared between different servers. */
1354 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1357 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1361 /* Tolerate duplicate address lines within the scope of a server. */
1362 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1363 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);