Blob


1 /*
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.
9 *
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.
13 *
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.
21 */
23 %{
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <sys/queue.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
30 #include <net/if.h>
31 #include <netinet/in.h>
33 #include <arpa/inet.h>
35 #include <ctype.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <event.h>
39 #include <ifaddrs.h>
40 #include <limits.h>
41 #include <netdb.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include <unistd.h>
49 #include "proc.h"
50 #include "gotwebd.h"
51 #include "got_sockaddr.h"
53 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
54 static struct file {
55 TAILQ_ENTRY(file) entry;
56 FILE *stream;
57 char *name;
58 int lineno;
59 int errors;
60 } *file;
61 struct file *newfile(const char *, int);
62 static void closefile(struct file *);
63 int check_file_secrecy(int, const char *);
64 int yyparse(void);
65 int yylex(void);
66 int yyerror(const char *, ...)
67 __attribute__((__format__ (printf, 1, 2)))
68 __attribute__((__nonnull__ (1)));
69 int kw_cmp(const void *, const void *);
70 int lookup(char *);
71 int lgetc(int);
72 int lungetc(int);
73 int findeol(void);
75 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 struct sym {
77 TAILQ_ENTRY(sym) entry;
78 int used;
79 int persist;
80 char *nam;
81 char *val;
82 };
84 int symset(const char *, const char *, int);
85 char *symget(const char *);
87 static int errors;
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 *);
93 int n;
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 *);
109 typedef struct {
110 union {
111 long long number;
112 char *string;
113 in_port_t port;
114 } v;
115 int lineno;
116 } YYSTYPE;
118 %}
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
131 %%
133 grammar :
134 | grammar '\n'
135 | grammar main '\n'
136 | grammar server '\n'
139 boolean : STRING {
140 if (strcasecmp($1, "1") == 0 ||
141 strcasecmp($1, "yes") == 0 ||
142 strcasecmp($1, "on") == 0)
143 $$ = 1;
144 else if (strcasecmp($1, "0") == 0 ||
145 strcasecmp($1, "off") == 0 ||
146 strcasecmp($1, "no") == 0)
147 $$ = 0;
148 else {
149 yyerror("invalid boolean value '%s'", $1);
150 free($1);
151 YYERROR;
153 free($1);
155 | ON { $$ = 1; }
156 | NUMBER { $$ = $1; }
159 fcgiport : PORT NUMBER {
160 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
161 yyerror("invalid port: %lld", $2);
162 YYERROR;
164 $$ = $2;
166 | PORT STRING {
167 int val;
169 if ((val = getservice($2)) == -1) {
170 yyerror("invalid port: %s", $2);
171 free($2);
172 YYERROR;
174 free($2);
176 $$ = val;
180 main : PREFORK NUMBER {
181 gotwebd->prefork_gotwebd = $2;
183 | CHROOT STRING {
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__);
188 free($2);
189 YYERROR;
191 free($2);
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);
201 if (n < 0 ||
202 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
203 yyerror("%s: unix_socket_name truncated",
204 __func__);
205 free($2);
206 YYERROR;
208 free($2);
212 server : SERVER STRING {
213 struct server *srv;
215 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
216 if (strcmp(srv->name, $2) == 0) {
217 yyerror("server name exists '%s'", $2);
218 free($2);
219 YYERROR;
223 new_srv = conf_new_server($2);
224 log_debug("adding server %s", $2);
225 free($2);
227 | SERVER STRING {
228 struct server *srv;
230 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
231 if (strcmp(srv->name, $2) == 0) {
232 yyerror("server name exists '%s'", $2);
233 free($2);
234 YYERROR;
238 new_srv = conf_new_server($2);
239 log_debug("adding server %s", $2);
240 free($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__);
250 free($2);
251 YYERROR;
253 free($2);
255 | SITE_NAME STRING {
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__);
260 free($2);
261 YYERROR;
263 free($2);
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__);
270 free($2);
271 YYERROR;
273 free($2);
275 | SITE_LINK STRING {
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__);
280 free($2);
281 YYERROR;
283 free($2);
285 | LOGO STRING {
286 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
287 if (n >= sizeof(new_srv->logo)) {
288 yyerror("%s: logo truncated", __func__);
289 free($2);
290 YYERROR;
292 free($2);
294 | LOGO_URL STRING {
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__);
299 free($2);
300 YYERROR;
302 free($2);
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__);
309 free($2);
310 YYERROR;
312 free($2);
314 | LISTEN ON STRING fcgiport {
315 if (get_addrs($3, new_srv, $4) == -1) {
316 yyerror("could not get addrs");
317 YYERROR;
319 new_srv->fcgi_socket = 1;
321 | MAX_REPOS NUMBER {
322 if ($2 > 0)
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 {
344 if ($2 > 0)
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);
355 if (n < 0 ||
356 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
357 yyerror("%s: unix_socket_name truncated",
358 __func__);
359 free($2);
360 YYERROR;
362 free($2);
366 serveropts2 : serveropts2 serveropts1 nl
367 | serveropts1 optnl
370 nl : '\n' optnl
373 optnl : '\n' optnl /* zero or more newlines */
374 | /* empty */
377 %%
379 struct keywords {
380 const char *k_name;
381 int k_val;
382 };
384 int
385 yyerror(const char *fmt, ...)
387 va_list ap;
388 char *msg;
390 file->errors++;
391 va_start(ap, fmt);
392 if (vasprintf(&msg, fmt, ap) == -1)
393 fatalx("yyerror vasprintf");
394 va_end(ap);
395 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
396 free(msg);
397 return (0);
400 int
401 kw_cmp(const void *k, const void *e)
403 return (strcmp(k, ((const struct keywords *)e)->k_name));
406 int
407 lookup(char *s)
409 /* This has to be sorted always. */
410 static const struct keywords keywords[] = {
411 { "chroot", CHROOT },
412 { "custom_css", CUSTOM_CSS },
413 { "listen", LISTEN },
414 { "logo", LOGO },
415 { "logo_url" , LOGO_URL },
416 { "max_commits_display", MAX_COMMITS_DISPLAY },
417 { "max_repos", MAX_REPOS },
418 { "max_repos_display", MAX_REPOS_DISPLAY },
419 { "on", ON },
420 { "port", PORT },
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 },
434 };
435 const struct keywords *p;
437 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
438 sizeof(keywords[0]), kw_cmp);
440 if (p)
441 return (p->k_val);
442 else
443 return (STRING);
446 #define MAXPUSHBACK 128
448 unsigned char *parsebuf;
449 int parseindex;
450 unsigned char pushback_buffer[MAXPUSHBACK];
451 int pushback_index = 0;
453 int
454 lgetc(int quotec)
456 int c, next;
458 if (parsebuf) {
459 /* Read character from the parsebuffer instead of input. */
460 if (parseindex >= 0) {
461 c = parsebuf[parseindex++];
462 if (c != '\0')
463 return (c);
464 parsebuf = NULL;
465 } else
466 parseindex++;
469 if (pushback_index)
470 return (pushback_buffer[--pushback_index]);
472 if (quotec) {
473 c = getc(file->stream);
474 if (c == EOF)
475 yyerror("reached end of file while parsing "
476 "quoted string");
477 return (c);
480 c = getc(file->stream);
481 while (c == '\\') {
482 next = getc(file->stream);
483 if (next != '\n') {
484 c = next;
485 break;
487 yylval.lineno = file->lineno;
488 file->lineno++;
489 c = getc(file->stream);
492 return (c);
495 int
496 lungetc(int c)
498 if (c == EOF)
499 return (EOF);
500 if (parsebuf) {
501 parseindex--;
502 if (parseindex >= 0)
503 return (c);
505 if (pushback_index < MAXPUSHBACK-1)
506 return (pushback_buffer[pushback_index++] = c);
507 else
508 return (EOF);
511 int
512 findeol(void)
514 int c;
516 parsebuf = NULL;
518 /* Skip to either EOF or the first real EOL. */
519 while (1) {
520 if (pushback_index)
521 c = pushback_buffer[--pushback_index];
522 else
523 c = lgetc(0);
524 if (c == '\n') {
525 file->lineno++;
526 break;
528 if (c == EOF)
529 break;
531 return (ERROR);
534 int
535 yylex(void)
537 unsigned char buf[8096];
538 unsigned char *p, *val;
539 int quotec, next, c;
540 int token;
542 top:
543 p = buf;
544 c = lgetc(0);
545 while (c == ' ' || c == '\t')
546 c = lgetc(0); /* nothing */
548 yylval.lineno = file->lineno;
549 if (c == '#') {
550 c = lgetc(0);
551 while (c != '\n' && c != EOF)
552 c = lgetc(0); /* nothing */
554 if (c == '$' && parsebuf == NULL) {
555 while (1) {
556 c = lgetc(0);
557 if (c == EOF)
558 return (0);
560 if (p + 1 >= buf + sizeof(buf) - 1) {
561 yyerror("string too long");
562 return (findeol());
564 if (isalnum(c) || c == '_') {
565 *p++ = c;
566 continue;
568 *p = '\0';
569 lungetc(c);
570 break;
572 val = symget(buf);
573 if (val == NULL) {
574 yyerror("macro '%s' not defined", buf);
575 return (findeol());
577 parsebuf = val;
578 parseindex = 0;
579 goto top;
582 switch (c) {
583 case '\'':
584 case '"':
585 quotec = c;
586 while (1) {
587 c = lgetc(quotec);
588 if (c == EOF)
589 return (0);
590 if (c == '\n') {
591 file->lineno++;
592 continue;
593 } else if (c == '\\') {
594 next = lgetc(quotec);
595 if (next == EOF)
596 return (0);
597 if (next == quotec || c == ' ' || c == '\t')
598 c = next;
599 else if (next == '\n') {
600 file->lineno++;
601 continue;
602 } else
603 lungetc(next);
604 } else if (c == quotec) {
605 *p = '\0';
606 break;
607 } else if (c == '\0') {
608 yyerror("syntax error");
609 return (findeol());
611 if (p + 1 >= buf + sizeof(buf) - 1) {
612 yyerror("string too long");
613 return (findeol());
615 *p++ = c;
617 yylval.v.string = strdup(buf);
618 if (yylval.v.string == NULL)
619 err(1, "yylex: strdup");
620 return (STRING);
623 #define allowed_to_end_number(x) \
624 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
626 if (c == '-' || isdigit(c)) {
627 do {
628 *p++ = c;
629 if ((unsigned)(p-buf) >= sizeof(buf)) {
630 yyerror("string too long");
631 return (findeol());
633 c = lgetc(0);
634 } while (c != EOF && isdigit(c));
635 lungetc(c);
636 if (p == buf + 1 && buf[0] == '-')
637 goto nodigits;
638 if (c == EOF || allowed_to_end_number(c)) {
639 const char *errstr = NULL;
641 *p = '\0';
642 yylval.v.number = strtonum(buf, LLONG_MIN,
643 LLONG_MAX, &errstr);
644 if (errstr) {
645 yyerror("\"%s\" invalid number: %s",
646 buf, errstr);
647 return (findeol());
649 return (NUMBER);
650 } else {
651 nodigits:
652 while (p > buf + 1)
653 lungetc(*--p);
654 c = *--p;
655 if (c == '-')
656 return (c);
660 #define allowed_in_string(x) \
661 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
662 x != '{' && x != '}' && \
663 x != '!' && x != '=' && x != '#' && \
664 x != ','))
666 if (isalnum(c) || c == ':' || c == '_') {
667 do {
668 *p++ = c;
669 if ((unsigned)(p-buf) >= sizeof(buf)) {
670 yyerror("string too long");
671 return (findeol());
673 c = lgetc(0);
674 } while (c != EOF && (allowed_in_string(c)));
675 lungetc(c);
676 *p = '\0';
677 token = lookup(buf);
678 if (token == STRING) {
679 yylval.v.string = strdup(buf);
680 if (yylval.v.string == NULL)
681 err(1, "yylex: strdup");
683 return (token);
685 if (c == '\n') {
686 yylval.lineno = file->lineno;
687 file->lineno++;
689 if (c == EOF)
690 return (0);
691 return (c);
694 int
695 check_file_secrecy(int fd, const char *fname)
697 struct stat st;
699 if (fstat(fd, &st)) {
700 log_warn("cannot stat %s", fname);
701 return (-1);
703 if (st.st_uid != 0 && st.st_uid != getuid()) {
704 log_warnx("%s: owner not root or current user", fname);
705 return (-1);
707 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
708 log_warnx("%s: group writable or world read/writable", fname);
709 return (-1);
711 return (0);
714 struct file *
715 newfile(const char *name, int secret)
717 struct file *nfile;
719 nfile = calloc(1, sizeof(struct file));
720 if (nfile == NULL) {
721 log_warn("calloc");
722 return (NULL);
724 nfile->name = strdup(name);
725 if (nfile->name == NULL) {
726 log_warn("strdup");
727 free(nfile);
728 return (NULL);
730 nfile->stream = fopen(nfile->name, "r");
731 if (nfile->stream == NULL) {
732 /* no warning, we don't require a conf file */
733 free(nfile->name);
734 free(nfile);
735 return (NULL);
736 } else if (secret &&
737 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
738 fclose(nfile->stream);
739 free(nfile->name);
740 free(nfile);
741 return (NULL);
743 nfile->lineno = 1;
744 return (nfile);
747 static void
748 closefile(struct file *xfile)
750 fclose(xfile->stream);
751 free(xfile->name);
752 free(xfile);
755 static void
756 add_default_server(void)
758 new_srv = conf_new_server(D_SITENAME);
759 log_debug("%s: adding default server %s", __func__, D_SITENAME);
762 int
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");
770 gotwebd = env;
772 file = newfile(filename, 0);
773 if (file == NULL) {
774 add_default_server();
775 sockets_parse_sockets(env);
776 /* just return, as we don't require a conf file */
777 return (0);
780 yyparse();
781 errors = file->errors;
782 closefile(file);
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",
788 sym->nam);
789 if (!sym->persist) {
790 free(sym->nam);
791 free(sym->val);
792 TAILQ_REMOVE(&symhead, sym, entry);
793 free(sym);
797 if (errors)
798 return (-1);
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);
807 return (0);
810 struct server *
811 conf_new_server(const char *name)
813 struct server *srv = NULL;
815 srv = calloc(1, sizeof(*srv));
816 if (srv == NULL)
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,
824 D_UNIX_SOCKET);
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,
844 sizeof(srv->logo));
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++;
871 return srv;
872 };
874 int
875 symset(const char *nam, const char *val, int persist)
877 struct sym *sym;
879 TAILQ_FOREACH(sym, &symhead, entry) {
880 if (strcmp(nam, sym->nam) == 0)
881 break;
884 if (sym != NULL) {
885 if (sym->persist == 1)
886 return (0);
887 else {
888 free(sym->nam);
889 free(sym->val);
890 TAILQ_REMOVE(&symhead, sym, entry);
891 free(sym);
894 sym = calloc(1, sizeof(*sym));
895 if (sym == NULL)
896 return (-1);
898 sym->nam = strdup(nam);
899 if (sym->nam == NULL) {
900 free(sym);
901 return (-1);
903 sym->val = strdup(val);
904 if (sym->val == NULL) {
905 free(sym->nam);
906 free(sym);
907 return (-1);
909 sym->used = 0;
910 sym->persist = persist;
911 TAILQ_INSERT_TAIL(&symhead, sym, entry);
912 return (0);
915 int
916 cmdline_symset(char *s)
918 char *sym, *val;
919 int ret;
921 val = strrchr(s, '=');
922 if (val == NULL)
923 return (-1);
925 sym = strndup(s, val - s);
926 if (sym == NULL)
927 fatal("%s: strndup", __func__);
929 ret = symset(sym, val + 1, 1);
930 free(sym);
932 return (ret);
935 char *
936 symget(const char *nam)
938 struct sym *sym;
940 TAILQ_FOREACH(sym, &symhead, entry) {
941 if (strcmp(nam, sym->nam) == 0) {
942 sym->used = 1;
943 return (sym->val);
946 return (NULL);
949 int
950 getservice(const char *n)
952 struct servent *s;
953 const char *errstr;
954 long long llval;
956 llval = strtonum(n, 0, UINT16_MAX, &errstr);
957 if (errstr) {
958 s = getservbyname(n, "tcp");
959 if (s == NULL)
960 s = getservbyname(n, "udp");
961 if (s == NULL)
962 return (-1);
963 return ntohs(s->s_port);
966 return (unsigned short)llval;
969 struct address *
970 host_v4(const char *s)
972 struct in_addr ina;
973 struct sockaddr_in *sain;
974 struct address *h;
976 memset(&ina, 0, sizeof(ina));
977 if (inet_pton(AF_INET, s, &ina) != 1)
978 return (NULL);
980 if ((h = calloc(1, sizeof(*h))) == NULL)
981 fatal(__func__);
982 sain = (struct sockaddr_in *)&h->ss;
983 got_sockaddr_inet_init(sain, &ina);
984 if (sain->sin_addr.s_addr == INADDR_ANY)
985 h->prefixlen = 0; /* 0.0.0.0 address */
986 else
987 h->prefixlen = -1; /* host address */
988 return (h);
991 struct address *
992 host_v6(const char *s)
994 struct addrinfo hints, *res;
995 struct sockaddr_in6 *sa_in6, *ra;
996 struct address *h = NULL;
998 memset(&hints, 0, sizeof(hints));
999 hints.ai_family = AF_INET6;
1000 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1001 hints.ai_flags = AI_NUMERICHOST;
1002 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1003 if ((h = calloc(1, sizeof(*h))) == NULL)
1004 fatal(__func__);
1005 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1006 ra = (struct sockaddr_in6 *)res->ai_addr;
1007 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1008 ra->sin6_scope_id);
1009 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1010 sizeof(sa_in6->sin6_addr)) == 0)
1011 h->prefixlen = 0; /* any address */
1012 else
1013 h->prefixlen = -1; /* host address */
1014 freeaddrinfo(res);
1017 return (h);
1020 int
1021 host_dns(const char *s, struct server *new_srv, int max,
1022 in_port_t port, const char *ifname, int ipproto)
1024 struct addrinfo hints, *res0, *res;
1025 int error, cnt = 0;
1026 struct sockaddr_in *sain;
1027 struct sockaddr_in6 *sin6;
1028 struct address *h;
1030 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1031 return (cnt);
1033 memset(&hints, 0, sizeof(hints));
1034 hints.ai_family = PF_UNSPEC;
1035 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1036 hints.ai_flags = AI_ADDRCONFIG;
1037 error = getaddrinfo(s, NULL, &hints, &res0);
1038 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1039 return (0);
1040 if (error) {
1041 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1042 gai_strerror(error));
1043 return (-1);
1046 for (res = res0; res && cnt < max; res = res->ai_next) {
1047 if (res->ai_family != AF_INET &&
1048 res->ai_family != AF_INET6)
1049 continue;
1050 if ((h = calloc(1, sizeof(*h))) == NULL)
1051 fatal(__func__);
1053 if (port)
1054 h->port = port;
1055 if (ifname != NULL) {
1056 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1057 sizeof(h->ifname)) {
1058 log_warnx("%s: interface name truncated",
1059 __func__);
1060 freeaddrinfo(res0);
1061 free(h);
1062 return (-1);
1065 if (ipproto != -1)
1066 h->ipproto = ipproto;
1067 h->ss.ss_family = res->ai_family;
1068 h->prefixlen = -1; /* host address */
1070 if (res->ai_family == AF_INET) {
1071 struct sockaddr_in *ra;
1072 sain = (struct sockaddr_in *)&h->ss;
1073 ra = (struct sockaddr_in *)res->ai_addr;
1074 got_sockaddr_inet_init(sain, &ra->sin_addr);
1075 } else {
1076 struct sockaddr_in6 *ra;
1077 sin6 = (struct sockaddr_in6 *)&h->ss;
1078 ra = (struct sockaddr_in6 *)res->ai_addr;
1079 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1082 if (add_addr(new_srv, h))
1083 return -1;
1084 cnt++;
1086 if (cnt == max && res) {
1087 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1088 s, max);
1090 freeaddrinfo(res0);
1091 return (cnt);
1094 int
1095 host_if(const char *s, struct server *new_srv, int max,
1096 in_port_t port, const char *ifname, int ipproto)
1098 struct ifaddrs *ifap, *p;
1099 struct sockaddr_in *sain;
1100 struct sockaddr_in6 *sin6;
1101 struct address *h;
1102 int cnt = 0, af;
1104 if (getifaddrs(&ifap) == -1)
1105 fatal("getifaddrs");
1107 /* First search for IPv4 addresses */
1108 af = AF_INET;
1110 nextaf:
1111 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1112 if (p->ifa_addr == NULL ||
1113 p->ifa_addr->sa_family != af ||
1114 (strcmp(s, p->ifa_name) != 0 &&
1115 !is_if_in_group(p->ifa_name, s)))
1116 continue;
1117 if ((h = calloc(1, sizeof(*h))) == NULL)
1118 fatal("calloc");
1120 if (port)
1121 h->port = port;
1122 if (ifname != NULL) {
1123 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1124 sizeof(h->ifname)) {
1125 log_warnx("%s: interface name truncated",
1126 __func__);
1127 free(h);
1128 freeifaddrs(ifap);
1129 return (-1);
1132 if (ipproto != -1)
1133 h->ipproto = ipproto;
1134 h->ss.ss_family = af;
1135 h->prefixlen = -1; /* host address */
1137 if (af == AF_INET) {
1138 struct sockaddr_in *ra;
1139 sain = (struct sockaddr_in *)&h->ss;
1140 ra = (struct sockaddr_in *)p->ifa_addr;
1141 got_sockaddr_inet_init(sain, &ra->sin_addr);
1142 } else {
1143 struct sockaddr_in6 *ra;
1144 sin6 = (struct sockaddr_in6 *)&h->ss;
1145 ra = (struct sockaddr_in6 *)p->ifa_addr;
1146 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1147 ra->sin6_scope_id);
1150 if (add_addr(new_srv, h))
1151 return -1;
1152 cnt++;
1154 if (af == AF_INET) {
1155 /* Next search for IPv6 addresses */
1156 af = AF_INET6;
1157 goto nextaf;
1160 if (cnt > max) {
1161 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1162 s, max);
1164 freeifaddrs(ifap);
1165 return (cnt);
1168 int
1169 host(const char *s, struct server *new_srv, int max,
1170 in_port_t port, const char *ifname, int ipproto)
1172 struct address *h;
1174 h = host_v4(s);
1176 /* IPv6 address? */
1177 if (h == NULL)
1178 h = host_v6(s);
1180 if (h != NULL) {
1181 if (port)
1182 h->port = port;
1183 if (ifname != NULL) {
1184 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1185 sizeof(h->ifname)) {
1186 log_warnx("%s: interface name truncated",
1187 __func__);
1188 free(h);
1189 return (-1);
1192 if (ipproto != -1)
1193 h->ipproto = ipproto;
1195 if (add_addr(new_srv, h))
1196 return -1;
1197 return (1);
1200 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1203 int
1204 is_if_in_group(const char *ifname, const char *groupname)
1206 /* TA: Check this... */
1207 #ifdef HAVE_STRUCT_IFGROUPREQ
1208 unsigned int len;
1209 struct ifgroupreq ifgr;
1210 struct ifg_req *ifg;
1211 int s;
1212 int ret = 0;
1214 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1215 err(1, "socket");
1217 memset(&ifgr, 0, sizeof(ifgr));
1218 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1219 err(1, "IFNAMSIZ");
1220 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1221 if (errno == EINVAL || errno == ENOTTY)
1222 goto end;
1223 err(1, "SIOCGIFGROUP");
1226 len = ifgr.ifgr_len;
1227 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1228 sizeof(struct ifg_req));
1229 if (ifgr.ifgr_groups == NULL)
1230 err(1, "getifgroups");
1231 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1232 err(1, "SIOCGIFGROUP");
1234 ifg = ifgr.ifgr_groups;
1235 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1236 len -= sizeof(struct ifg_req);
1237 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1238 ret = 1;
1239 break;
1242 free(ifgr.ifgr_groups);
1244 end:
1245 close(s);
1246 return (ret);
1247 #else
1248 return (0);
1249 #endif
1252 int
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",
1257 -1) <= 0) {
1258 yyerror("invalid listen ip: %s",
1259 "127.0.0.1");
1260 return (-1);
1262 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1263 yyerror("invalid listen ip: %s", "::1");
1264 return (-1);
1266 } else {
1267 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1268 -1) <= 0) {
1269 yyerror("invalid listen ip: %s", addr);
1270 return (-1);
1273 return (0);
1276 int
1277 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1278 const char *other_srv)
1280 struct address *a;
1281 void *ia;
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 ||
1287 a->port != h->port)
1288 continue;
1290 switch (h->ss.ss_family) {
1291 case AF_INET:
1292 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1293 break;
1294 case AF_INET6:
1295 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1296 break;
1297 default:
1298 yyerror("unknown address family: %d", h->ss.ss_family);
1299 return -1;
1301 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1302 if (addrstr) {
1303 if (other_srv) {
1304 yyerror("server %s: duplicate fcgi listen "
1305 "address %s:%d, already used by server %s",
1306 new_srv, addrstr, h->port, other_srv);
1307 } else {
1308 log_warnx("server: %s: duplicate fcgi listen "
1309 "address %s:%d", new_srv, addrstr, h->port);
1311 } else {
1312 if (other_srv) {
1313 yyerror("server: %s: duplicate fcgi listen "
1314 "address, already used by server %s",
1315 new_srv, other_srv);
1316 } else {
1317 log_warnx("server %s: duplicate fcgi listen "
1318 "address", new_srv);
1322 return -1;
1325 return 0;
1328 int
1329 add_addr(struct server *new_srv, struct address *h)
1331 struct server *srv;
1333 /* Address cannot be shared between different servers. */
1334 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1335 if (srv == new_srv)
1336 continue;
1337 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1338 return -1;
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);
1344 else
1345 free(h);
1347 return 0;