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 FCGI_SOCKET
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 | FCGI_SOCKET boolean {
195 gotwebd->fcgi_socket = $2;
197 | UNIX_SOCKET boolean {
198 gotwebd->unix_socket = $2;
200 | UNIX_SOCKET_NAME STRING {
201 n = snprintf(gotwebd->unix_socket_name,
202 sizeof(gotwebd->unix_socket_name), "%s%s",
203 strlen(gotwebd->httpd_chroot) ?
204 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
206 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
207 yyerror("%s: unix_socket_name truncated",
216 server : SERVER STRING {
219 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
220 if (strcmp(srv->name, $2) == 0) {
221 yyerror("server name exists '%s'", $2);
227 new_srv = conf_new_server($2);
228 log_debug("adding server %s", $2);
234 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
235 if (strcmp(srv->name, $2) == 0) {
236 yyerror("server name exists '%s'", $2);
242 new_srv = conf_new_server($2);
243 log_debug("adding server %s", $2);
245 } '{' optnl serveropts2 '}' {
249 serveropts1 : REPOS_PATH STRING {
250 n = strlcpy(new_srv->repos_path, $2,
251 sizeof(new_srv->repos_path));
252 if (n >= sizeof(new_srv->repos_path)) {
253 yyerror("%s: repos_path truncated", __func__);
260 n = strlcpy(new_srv->site_name, $2,
261 sizeof(new_srv->site_name));
262 if (n >= sizeof(new_srv->site_name)) {
263 yyerror("%s: site_name truncated", __func__);
269 | SITE_OWNER STRING {
270 n = strlcpy(new_srv->site_owner, $2,
271 sizeof(new_srv->site_owner));
272 if (n >= sizeof(new_srv->site_owner)) {
273 yyerror("%s: site_owner truncated", __func__);
280 n = strlcpy(new_srv->site_link, $2,
281 sizeof(new_srv->site_link));
282 if (n >= sizeof(new_srv->site_link)) {
283 yyerror("%s: site_link truncated", __func__);
290 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
291 if (n >= sizeof(new_srv->logo)) {
292 yyerror("%s: logo truncated", __func__);
299 n = strlcpy(new_srv->logo_url, $2,
300 sizeof(new_srv->logo_url));
301 if (n >= sizeof(new_srv->logo_url)) {
302 yyerror("%s: logo_url truncated", __func__);
308 | CUSTOM_CSS STRING {
309 n = strlcpy(new_srv->custom_css, $2,
310 sizeof(new_srv->custom_css));
311 if (n >= sizeof(new_srv->custom_css)) {
312 yyerror("%s: custom_css truncated", __func__);
318 | LISTEN ON STRING fcgiport {
319 if (get_addrs($3, new_srv, $4) == -1) {
320 yyerror("could not get addrs");
326 new_srv->max_repos = $2;
328 | SHOW_SITE_OWNER boolean {
329 new_srv->show_site_owner = $2;
331 | SHOW_REPO_OWNER boolean {
332 new_srv->show_repo_owner = $2;
334 | SHOW_REPO_AGE boolean {
335 new_srv->show_repo_age = $2;
337 | SHOW_REPO_DESCRIPTION boolean {
338 new_srv->show_repo_description = $2;
340 | SHOW_REPO_CLONEURL boolean {
341 new_srv->show_repo_cloneurl = $2;
343 | MAX_REPOS_DISPLAY NUMBER {
344 new_srv->max_repos_display = $2;
346 | MAX_COMMITS_DISPLAY NUMBER {
348 new_srv->max_commits_display = $2;
350 | FCGI_SOCKET boolean {
351 new_srv->fcgi_socket = $2;
353 | UNIX_SOCKET boolean {
354 new_srv->unix_socket = $2;
356 | UNIX_SOCKET_NAME STRING {
357 n = snprintf(new_srv->unix_socket_name,
358 sizeof(new_srv->unix_socket_name), "%s%s",
359 strlen(gotwebd->httpd_chroot) ?
360 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
362 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
363 yyerror("%s: unix_socket_name truncated",
372 serveropts2 : serveropts2 serveropts1 nl
379 optnl : '\n' optnl /* zero or more newlines */
391 yyerror(const char *fmt, ...)
398 if (vasprintf(&msg, fmt, ap) == -1)
399 fatalx("yyerror vasprintf");
401 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
407 kw_cmp(const void *k, const void *e)
409 return (strcmp(k, ((const struct keywords *)e)->k_name));
415 /* This has to be sorted always. */
416 static const struct keywords keywords[] = {
417 { "chroot", CHROOT },
418 { "custom_css", CUSTOM_CSS },
419 { "fcgi_socket", FCGI_SOCKET },
420 { "listen", LISTEN },
422 { "logo_url" , LOGO_URL },
423 { "max_commits_display", MAX_COMMITS_DISPLAY },
424 { "max_repos", MAX_REPOS },
425 { "max_repos_display", MAX_REPOS_DISPLAY },
428 { "prefork", PREFORK },
429 { "repos_path", REPOS_PATH },
430 { "server", SERVER },
431 { "show_repo_age", SHOW_REPO_AGE },
432 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
433 { "show_repo_description", SHOW_REPO_DESCRIPTION },
434 { "show_repo_owner", SHOW_REPO_OWNER },
435 { "show_site_owner", SHOW_SITE_OWNER },
436 { "site_link", SITE_LINK },
437 { "site_name", SITE_NAME },
438 { "site_owner", SITE_OWNER },
439 { "unix_socket", UNIX_SOCKET },
440 { "unix_socket_name", UNIX_SOCKET_NAME },
442 const struct keywords *p;
444 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
445 sizeof(keywords[0]), kw_cmp);
453 #define MAXPUSHBACK 128
455 unsigned char *parsebuf;
457 unsigned char pushback_buffer[MAXPUSHBACK];
458 int pushback_index = 0;
466 /* Read character from the parsebuffer instead of input. */
467 if (parseindex >= 0) {
468 c = parsebuf[parseindex++];
477 return (pushback_buffer[--pushback_index]);
480 c = getc(file->stream);
482 yyerror("reached end of file while parsing "
487 c = getc(file->stream);
489 next = getc(file->stream);
494 yylval.lineno = file->lineno;
496 c = getc(file->stream);
512 if (pushback_index < MAXPUSHBACK-1)
513 return (pushback_buffer[pushback_index++] = c);
525 /* Skip to either EOF or the first real EOL. */
528 c = pushback_buffer[--pushback_index];
544 unsigned char buf[8096];
545 unsigned char *p, *val;
552 while (c == ' ' || c == '\t')
553 c = lgetc(0); /* nothing */
555 yylval.lineno = file->lineno;
558 while (c != '\n' && c != EOF)
559 c = lgetc(0); /* nothing */
561 if (c == '$' && parsebuf == NULL) {
567 if (p + 1 >= buf + sizeof(buf) - 1) {
568 yyerror("string too long");
571 if (isalnum(c) || c == '_') {
581 yyerror("macro '%s' not defined", buf);
600 } else if (c == '\\') {
601 next = lgetc(quotec);
604 if (next == quotec || c == ' ' || c == '\t')
606 else if (next == '\n') {
611 } else if (c == quotec) {
614 } else if (c == '\0') {
615 yyerror("syntax error");
618 if (p + 1 >= buf + sizeof(buf) - 1) {
619 yyerror("string too long");
624 yylval.v.string = strdup(buf);
625 if (yylval.v.string == NULL)
626 err(1, "yylex: strdup");
630 #define allowed_to_end_number(x) \
631 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
633 if (c == '-' || isdigit(c)) {
636 if ((unsigned)(p-buf) >= sizeof(buf)) {
637 yyerror("string too long");
641 } while (c != EOF && isdigit(c));
643 if (p == buf + 1 && buf[0] == '-')
645 if (c == EOF || allowed_to_end_number(c)) {
646 const char *errstr = NULL;
649 yylval.v.number = strtonum(buf, LLONG_MIN,
652 yyerror("\"%s\" invalid number: %s",
667 #define allowed_in_string(x) \
668 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
669 x != '{' && x != '}' && \
670 x != '!' && x != '=' && x != '#' && \
673 if (isalnum(c) || c == ':' || c == '_') {
676 if ((unsigned)(p-buf) >= sizeof(buf)) {
677 yyerror("string too long");
681 } while (c != EOF && (allowed_in_string(c)));
685 if (token == STRING) {
686 yylval.v.string = strdup(buf);
687 if (yylval.v.string == NULL)
688 err(1, "yylex: strdup");
693 yylval.lineno = file->lineno;
702 check_file_secrecy(int fd, const char *fname)
706 if (fstat(fd, &st)) {
707 log_warn("cannot stat %s", fname);
710 if (st.st_uid != 0 && st.st_uid != getuid()) {
711 log_warnx("%s: owner not root or current user", fname);
714 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
715 log_warnx("%s: group writable or world read/writable", fname);
722 newfile(const char *name, int secret)
726 nfile = calloc(1, sizeof(struct file));
731 nfile->name = strdup(name);
732 if (nfile->name == NULL) {
737 nfile->stream = fopen(nfile->name, "r");
738 if (nfile->stream == NULL) {
739 /* no warning, we don't require a conf file */
744 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
745 fclose(nfile->stream);
755 closefile(struct file *xfile)
757 fclose(xfile->stream);
763 add_default_server(void)
765 new_srv = conf_new_server(D_SITENAME);
766 log_debug("%s: adding default server %s", __func__, D_SITENAME);
770 parse_config(const char *filename, struct gotwebd *env)
772 struct sym *sym, *next;
774 if (config_init(env) == -1)
775 fatalx("failed to initialize configuration");
779 file = newfile(filename, 0);
781 add_default_server();
782 sockets_parse_sockets(env);
783 /* just return, as we don't require a conf file */
788 errors = file->errors;
791 /* Free macros and check which have not been used. */
792 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
793 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
794 fprintf(stderr, "warning: macro '%s' not used\n",
799 TAILQ_REMOVE(&symhead, sym, entry);
807 /* just add default server if no config specified */
808 if (gotwebd->server_cnt == 0)
809 add_default_server();
811 /* setup our listening sockets */
812 sockets_parse_sockets(env);
818 conf_new_server(const char *name)
820 struct server *srv = NULL;
822 srv = calloc(1, sizeof(*srv));
824 fatalx("%s: calloc", __func__);
826 n = strlcpy(srv->name, name, sizeof(srv->name));
827 if (n >= sizeof(srv->name))
828 fatalx("%s: strlcpy", __func__);
829 n = snprintf(srv->unix_socket_name,
830 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
832 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
833 fatalx("%s: snprintf", __func__);
834 n = strlcpy(srv->repos_path, D_GOTPATH,
835 sizeof(srv->repos_path));
836 if (n >= sizeof(srv->repos_path))
837 fatalx("%s: strlcpy", __func__);
838 n = strlcpy(srv->site_name, D_SITENAME,
839 sizeof(srv->site_name));
840 if (n >= sizeof(srv->site_name))
841 fatalx("%s: strlcpy", __func__);
842 n = strlcpy(srv->site_owner, D_SITEOWNER,
843 sizeof(srv->site_owner));
844 if (n >= sizeof(srv->site_owner))
845 fatalx("%s: strlcpy", __func__);
846 n = strlcpy(srv->site_link, D_SITELINK,
847 sizeof(srv->site_link));
848 if (n >= sizeof(srv->site_link))
849 fatalx("%s: strlcpy", __func__);
850 n = strlcpy(srv->logo, D_GOTLOGO,
852 if (n >= sizeof(srv->logo))
853 fatalx("%s: strlcpy", __func__);
854 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
855 if (n >= sizeof(srv->logo_url))
856 fatalx("%s: strlcpy", __func__);
857 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
858 if (n >= sizeof(srv->custom_css))
859 fatalx("%s: strlcpy", __func__);
861 srv->show_site_owner = D_SHOWSOWNER;
862 srv->show_repo_owner = D_SHOWROWNER;
863 srv->show_repo_age = D_SHOWAGE;
864 srv->show_repo_description = D_SHOWDESC;
865 srv->show_repo_cloneurl = D_SHOWURL;
867 srv->max_repos_display = D_MAXREPODISP;
868 srv->max_commits_display = D_MAXCOMMITDISP;
869 srv->max_repos = D_MAXREPO;
871 srv->unix_socket = 1;
872 srv->fcgi_socket = gotwebd->fcgi_socket ? gotwebd->fcgi_socket : 0;
874 TAILQ_INIT(&srv->al);
875 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
876 gotwebd->server_cnt++;
882 symset(const char *nam, const char *val, int persist)
886 TAILQ_FOREACH(sym, &symhead, entry) {
887 if (strcmp(nam, sym->nam) == 0)
892 if (sym->persist == 1)
897 TAILQ_REMOVE(&symhead, sym, entry);
901 sym = calloc(1, sizeof(*sym));
905 sym->nam = strdup(nam);
906 if (sym->nam == NULL) {
910 sym->val = strdup(val);
911 if (sym->val == NULL) {
917 sym->persist = persist;
918 TAILQ_INSERT_TAIL(&symhead, sym, entry);
923 cmdline_symset(char *s)
929 val = strrchr(s, '=');
933 len = strlen(s) - strlen(val) + 1;
936 fatal("%s: malloc", __func__);
938 memcpy(&sym, s, len);
940 ret = symset(sym, val + 1, 1);
947 symget(const char *nam)
951 TAILQ_FOREACH(sym, &symhead, entry) {
952 if (strcmp(nam, sym->nam) == 0) {
961 getservice(const char *n)
967 llval = strtonum(n, 0, UINT16_MAX, &errstr);
969 s = getservbyname(n, "tcp");
971 s = getservbyname(n, "udp");
974 return ntohs(s->s_port);
977 return (unsigned short)llval;
981 host_v4(const char *s)
984 struct sockaddr_in *sain;
987 memset(&ina, 0, sizeof(ina));
988 if (inet_pton(AF_INET, s, &ina) != 1)
991 if ((h = calloc(1, sizeof(*h))) == NULL)
993 sain = (struct sockaddr_in *)&h->ss;
994 got_sockaddr_inet_init(sain, &ina);
995 if (sain->sin_addr.s_addr == INADDR_ANY)
996 h->prefixlen = 0; /* 0.0.0.0 address */
998 h->prefixlen = -1; /* host address */
1003 host_v6(const char *s)
1005 struct addrinfo hints, *res;
1006 struct sockaddr_in6 *sa_in6, *ra;
1007 struct address *h = NULL;
1009 memset(&hints, 0, sizeof(hints));
1010 hints.ai_family = AF_INET6;
1011 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1012 hints.ai_flags = AI_NUMERICHOST;
1013 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1014 if ((h = calloc(1, sizeof(*h))) == NULL)
1016 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1017 ra = (struct sockaddr_in6 *)res->ai_addr;
1018 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1020 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1021 sizeof(sa_in6->sin6_addr)) == 0)
1022 h->prefixlen = 0; /* any address */
1024 h->prefixlen = -1; /* host address */
1032 host_dns(const char *s, struct server *new_srv, int max,
1033 in_port_t port, const char *ifname, int ipproto)
1035 struct addrinfo hints, *res0, *res;
1037 struct sockaddr_in *sain;
1038 struct sockaddr_in6 *sin6;
1041 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1044 memset(&hints, 0, sizeof(hints));
1045 hints.ai_family = PF_UNSPEC;
1046 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1047 hints.ai_flags = AI_ADDRCONFIG;
1048 error = getaddrinfo(s, NULL, &hints, &res0);
1049 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1052 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1053 gai_strerror(error));
1057 for (res = res0; res && cnt < max; res = res->ai_next) {
1058 if (res->ai_family != AF_INET &&
1059 res->ai_family != AF_INET6)
1061 if ((h = calloc(1, sizeof(*h))) == NULL)
1066 if (ifname != NULL) {
1067 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1068 sizeof(h->ifname)) {
1069 log_warnx("%s: interface name truncated",
1077 h->ipproto = ipproto;
1078 h->ss.ss_family = res->ai_family;
1079 h->prefixlen = -1; /* host address */
1081 if (res->ai_family == AF_INET) {
1082 struct sockaddr_in *ra;
1083 sain = (struct sockaddr_in *)&h->ss;
1084 ra = (struct sockaddr_in *)res->ai_addr;
1085 got_sockaddr_inet_init(sain, &ra->sin_addr);
1087 struct sockaddr_in6 *ra;
1088 sin6 = (struct sockaddr_in6 *)&h->ss;
1089 ra = (struct sockaddr_in6 *)res->ai_addr;
1090 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1093 if (add_addr(new_srv, h))
1097 if (cnt == max && res) {
1098 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1106 host_if(const char *s, struct server *new_srv, int max,
1107 in_port_t port, const char *ifname, int ipproto)
1109 struct ifaddrs *ifap, *p;
1110 struct sockaddr_in *sain;
1111 struct sockaddr_in6 *sin6;
1115 if (getifaddrs(&ifap) == -1)
1116 fatal("getifaddrs");
1118 /* First search for IPv4 addresses */
1122 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1123 if (p->ifa_addr == NULL ||
1124 p->ifa_addr->sa_family != af ||
1125 (strcmp(s, p->ifa_name) != 0 &&
1126 !is_if_in_group(p->ifa_name, s)))
1128 if ((h = calloc(1, sizeof(*h))) == NULL)
1133 if (ifname != NULL) {
1134 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1135 sizeof(h->ifname)) {
1136 log_warnx("%s: interface name truncated",
1144 h->ipproto = ipproto;
1145 h->ss.ss_family = af;
1146 h->prefixlen = -1; /* host address */
1148 if (af == AF_INET) {
1149 struct sockaddr_in *ra;
1150 sain = (struct sockaddr_in *)&h->ss;
1151 ra = (struct sockaddr_in *)p->ifa_addr;
1152 got_sockaddr_inet_init(sain, &ra->sin_addr);
1154 struct sockaddr_in6 *ra;
1155 sin6 = (struct sockaddr_in6 *)&h->ss;
1156 ra = (struct sockaddr_in6 *)p->ifa_addr;
1157 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1161 if (add_addr(new_srv, h))
1165 if (af == AF_INET) {
1166 /* Next search for IPv6 addresses */
1172 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1180 host(const char *s, struct server *new_srv, int max,
1181 in_port_t port, const char *ifname, int ipproto)
1194 if (ifname != NULL) {
1195 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1196 sizeof(h->ifname)) {
1197 log_warnx("%s: interface name truncated",
1204 h->ipproto = ipproto;
1206 if (add_addr(new_srv, h))
1211 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1215 is_if_in_group(const char *ifname, const char *groupname)
1218 struct ifgroupreq ifgr;
1219 struct ifg_req *ifg;
1223 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1226 memset(&ifgr, 0, sizeof(ifgr));
1227 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1229 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1230 if (errno == EINVAL || errno == ENOTTY)
1232 err(1, "SIOCGIFGROUP");
1235 len = ifgr.ifgr_len;
1236 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1237 sizeof(struct ifg_req));
1238 if (ifgr.ifgr_groups == NULL)
1239 err(1, "getifgroups");
1240 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1241 err(1, "SIOCGIFGROUP");
1243 ifg = ifgr.ifgr_groups;
1244 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1245 len -= sizeof(struct ifg_req);
1246 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1251 free(ifgr.ifgr_groups);
1259 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1261 if (strcmp("", addr) == 0) {
1262 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1264 yyerror("invalid listen ip: %s",
1268 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1269 yyerror("invalid listen ip: %s", "::1");
1273 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1275 yyerror("invalid listen ip: %s", addr);
1283 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1284 const char *other_srv)
1288 char buf[INET6_ADDRSTRLEN];
1289 const char *addrstr;
1291 TAILQ_FOREACH(a, al, entry) {
1292 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1296 switch (h->ss.ss_family) {
1298 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1301 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1304 yyerror("unknown address family: %d", h->ss.ss_family);
1307 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1310 yyerror("server %s: duplicate fcgi listen "
1311 "address %s:%d, already used by server %s",
1312 new_srv, addrstr, h->port, other_srv);
1314 log_warnx("server: %s: duplicate fcgi listen "
1315 "address %s:%d", new_srv, addrstr, h->port);
1319 yyerror("server: %s: duplicate fcgi listen "
1320 "address, already used by server %s",
1321 new_srv, other_srv);
1323 log_warnx("server %s: duplicate fcgi listen "
1324 "address", new_srv);
1335 add_addr(struct server *new_srv, struct address *h)
1339 /* Address cannot be shared between different servers. */
1340 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1343 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1347 /* Tolerate duplicate address lines within the scope of a server. */
1348 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1349 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);