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 <imsg.h>
41 #include <limits.h>
42 #include <netdb.h>
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <syslog.h>
48 #include <unistd.h>
50 #include "proc.h"
51 #include "gotwebd.h"
52 #include "got_sockaddr.h"
54 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
55 static struct file {
56 TAILQ_ENTRY(file) entry;
57 FILE *stream;
58 char *name;
59 int lineno;
60 int errors;
61 } *file;
62 struct file *newfile(const char *, int);
63 static void closefile(struct file *);
64 int check_file_secrecy(int, const char *);
65 int yyparse(void);
66 int yylex(void);
67 int yyerror(const char *, ...)
68 __attribute__((__format__ (printf, 1, 2)))
69 __attribute__((__nonnull__ (1)));
70 int kw_cmp(const void *, const void *);
71 int lookup(char *);
72 int lgetc(int);
73 int lungetc(int);
74 int findeol(void);
76 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
77 struct sym {
78 TAILQ_ENTRY(sym) entry;
79 int used;
80 int persist;
81 char *nam;
82 char *val;
83 };
85 int symset(const char *, const char *, int);
86 char *symget(const char *);
88 static int errors;
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 *);
94 int n;
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 *);
110 typedef struct {
111 union {
112 long long number;
113 char *string;
114 in_port_t port;
115 } v;
116 int lineno;
117 } YYSTYPE;
119 %}
121 %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
122 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
123 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
124 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK
125 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
127 %token <v.string> STRING
128 %type <v.port> fcgiport
129 %token <v.number> NUMBER
130 %type <v.number> boolean
132 %%
134 grammar :
135 | grammar '\n'
136 | grammar main '\n'
137 | grammar server '\n'
140 boolean : STRING {
141 if (strcasecmp($1, "1") == 0 ||
142 strcasecmp($1, "yes") == 0 ||
143 strcasecmp($1, "on") == 0)
144 $$ = 1;
145 else if (strcasecmp($1, "0") == 0 ||
146 strcasecmp($1, "off") == 0 ||
147 strcasecmp($1, "no") == 0)
148 $$ = 0;
149 else {
150 yyerror("invalid boolean value '%s'", $1);
151 free($1);
152 YYERROR;
154 free($1);
156 | ON { $$ = 1; }
157 | NUMBER { $$ = $1; }
160 fcgiport : PORT NUMBER {
161 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
162 yyerror("invalid port: %lld", $2);
163 YYERROR;
165 $$ = $2;
167 | PORT STRING {
168 int val;
170 if ((val = getservice($2)) == -1) {
171 yyerror("invalid port: %s", $2);
172 free($2);
173 YYERROR;
175 free($2);
177 $$ = val;
181 main : PREFORK NUMBER {
182 gotwebd->prefork_gotwebd = $2;
184 | CHROOT STRING {
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__);
189 free($2);
190 YYERROR;
192 free($2);
194 | UNIX_SOCKET boolean {
195 gotwebd->unix_socket = $2;
197 | UNIX_SOCKET_NAME STRING {
198 n = snprintf(gotwebd->unix_socket_name,
199 sizeof(gotwebd->unix_socket_name), "%s%s",
200 strlen(gotwebd->httpd_chroot) ?
201 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
202 if (n < 0 ||
203 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
204 yyerror("%s: unix_socket_name truncated",
205 __func__);
206 free($2);
207 YYERROR;
209 free($2);
213 server : SERVER STRING {
214 struct server *srv;
216 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
217 if (strcmp(srv->name, $2) == 0) {
218 yyerror("server name exists '%s'", $2);
219 free($2);
220 YYERROR;
224 new_srv = conf_new_server($2);
225 log_debug("adding server %s", $2);
226 free($2);
228 | SERVER STRING {
229 struct server *srv;
231 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
232 if (strcmp(srv->name, $2) == 0) {
233 yyerror("server name exists '%s'", $2);
234 free($2);
235 YYERROR;
239 new_srv = conf_new_server($2);
240 log_debug("adding server %s", $2);
241 free($2);
242 } '{' optnl serveropts2 '}' {
246 serveropts1 : REPOS_PATH STRING {
247 n = strlcpy(new_srv->repos_path, $2,
248 sizeof(new_srv->repos_path));
249 if (n >= sizeof(new_srv->repos_path)) {
250 yyerror("%s: repos_path truncated", __func__);
251 free($2);
252 YYERROR;
254 free($2);
256 | SITE_NAME STRING {
257 n = strlcpy(new_srv->site_name, $2,
258 sizeof(new_srv->site_name));
259 if (n >= sizeof(new_srv->site_name)) {
260 yyerror("%s: site_name truncated", __func__);
261 free($2);
262 YYERROR;
264 free($2);
266 | SITE_OWNER STRING {
267 n = strlcpy(new_srv->site_owner, $2,
268 sizeof(new_srv->site_owner));
269 if (n >= sizeof(new_srv->site_owner)) {
270 yyerror("%s: site_owner truncated", __func__);
271 free($2);
272 YYERROR;
274 free($2);
276 | SITE_LINK STRING {
277 n = strlcpy(new_srv->site_link, $2,
278 sizeof(new_srv->site_link));
279 if (n >= sizeof(new_srv->site_link)) {
280 yyerror("%s: site_link truncated", __func__);
281 free($2);
282 YYERROR;
284 free($2);
286 | LOGO STRING {
287 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
288 if (n >= sizeof(new_srv->logo)) {
289 yyerror("%s: logo truncated", __func__);
290 free($2);
291 YYERROR;
293 free($2);
295 | LOGO_URL STRING {
296 n = strlcpy(new_srv->logo_url, $2,
297 sizeof(new_srv->logo_url));
298 if (n >= sizeof(new_srv->logo_url)) {
299 yyerror("%s: logo_url truncated", __func__);
300 free($2);
301 YYERROR;
303 free($2);
305 | CUSTOM_CSS STRING {
306 n = strlcpy(new_srv->custom_css, $2,
307 sizeof(new_srv->custom_css));
308 if (n >= sizeof(new_srv->custom_css)) {
309 yyerror("%s: custom_css truncated", __func__);
310 free($2);
311 YYERROR;
313 free($2);
315 | LISTEN ON STRING fcgiport {
316 if (get_addrs($3, new_srv, $4) == -1) {
317 yyerror("could not get addrs");
318 YYERROR;
320 new_srv->fcgi_socket = 1;
322 | MAX_REPOS NUMBER {
323 if ($2 > 0)
324 new_srv->max_repos = $2;
326 | SHOW_SITE_OWNER boolean {
327 new_srv->show_site_owner = $2;
329 | SHOW_REPO_OWNER boolean {
330 new_srv->show_repo_owner = $2;
332 | SHOW_REPO_AGE boolean {
333 new_srv->show_repo_age = $2;
335 | SHOW_REPO_DESCRIPTION boolean {
336 new_srv->show_repo_description = $2;
338 | SHOW_REPO_CLONEURL boolean {
339 new_srv->show_repo_cloneurl = $2;
341 | MAX_REPOS_DISPLAY NUMBER {
342 new_srv->max_repos_display = $2;
344 | MAX_COMMITS_DISPLAY NUMBER {
345 if ($2 > 0)
346 new_srv->max_commits_display = $2;
348 | UNIX_SOCKET boolean {
349 new_srv->unix_socket = $2;
351 | UNIX_SOCKET_NAME STRING {
352 n = snprintf(new_srv->unix_socket_name,
353 sizeof(new_srv->unix_socket_name), "%s%s",
354 strlen(gotwebd->httpd_chroot) ?
355 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
356 if (n < 0 ||
357 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
358 yyerror("%s: unix_socket_name truncated",
359 __func__);
360 free($2);
361 YYERROR;
363 free($2);
367 serveropts2 : serveropts2 serveropts1 nl
368 | serveropts1 optnl
371 nl : '\n' optnl
374 optnl : '\n' optnl /* zero or more newlines */
375 | /* empty */
378 %%
380 struct keywords {
381 const char *k_name;
382 int k_val;
383 };
385 int
386 yyerror(const char *fmt, ...)
388 va_list ap;
389 char *msg;
391 file->errors++;
392 va_start(ap, fmt);
393 if (vasprintf(&msg, fmt, ap) == -1)
394 fatalx("yyerror vasprintf");
395 va_end(ap);
396 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
397 free(msg);
398 return (0);
401 int
402 kw_cmp(const void *k, const void *e)
404 return (strcmp(k, ((const struct keywords *)e)->k_name));
407 int
408 lookup(char *s)
410 /* This has to be sorted always. */
411 static const struct keywords keywords[] = {
412 { "chroot", CHROOT },
413 { "custom_css", CUSTOM_CSS },
414 { "listen", LISTEN },
415 { "logo", LOGO },
416 { "logo_url" , LOGO_URL },
417 { "max_commits_display", MAX_COMMITS_DISPLAY },
418 { "max_repos", MAX_REPOS },
419 { "max_repos_display", MAX_REPOS_DISPLAY },
420 { "on", ON },
421 { "port", PORT },
422 { "prefork", PREFORK },
423 { "repos_path", REPOS_PATH },
424 { "server", SERVER },
425 { "show_repo_age", SHOW_REPO_AGE },
426 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
427 { "show_repo_description", SHOW_REPO_DESCRIPTION },
428 { "show_repo_owner", SHOW_REPO_OWNER },
429 { "show_site_owner", SHOW_SITE_OWNER },
430 { "site_link", SITE_LINK },
431 { "site_name", SITE_NAME },
432 { "site_owner", SITE_OWNER },
433 { "unix_socket", UNIX_SOCKET },
434 { "unix_socket_name", UNIX_SOCKET_NAME },
435 };
436 const struct keywords *p;
438 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
439 sizeof(keywords[0]), kw_cmp);
441 if (p)
442 return (p->k_val);
443 else
444 return (STRING);
447 #define MAXPUSHBACK 128
449 unsigned char *parsebuf;
450 int parseindex;
451 unsigned char pushback_buffer[MAXPUSHBACK];
452 int pushback_index = 0;
454 int
455 lgetc(int quotec)
457 int c, next;
459 if (parsebuf) {
460 /* Read character from the parsebuffer instead of input. */
461 if (parseindex >= 0) {
462 c = parsebuf[parseindex++];
463 if (c != '\0')
464 return (c);
465 parsebuf = NULL;
466 } else
467 parseindex++;
470 if (pushback_index)
471 return (pushback_buffer[--pushback_index]);
473 if (quotec) {
474 c = getc(file->stream);
475 if (c == EOF)
476 yyerror("reached end of file while parsing "
477 "quoted string");
478 return (c);
481 c = getc(file->stream);
482 while (c == '\\') {
483 next = getc(file->stream);
484 if (next != '\n') {
485 c = next;
486 break;
488 yylval.lineno = file->lineno;
489 file->lineno++;
490 c = getc(file->stream);
493 return (c);
496 int
497 lungetc(int c)
499 if (c == EOF)
500 return (EOF);
501 if (parsebuf) {
502 parseindex--;
503 if (parseindex >= 0)
504 return (c);
506 if (pushback_index < MAXPUSHBACK-1)
507 return (pushback_buffer[pushback_index++] = c);
508 else
509 return (EOF);
512 int
513 findeol(void)
515 int c;
517 parsebuf = NULL;
519 /* Skip to either EOF or the first real EOL. */
520 while (1) {
521 if (pushback_index)
522 c = pushback_buffer[--pushback_index];
523 else
524 c = lgetc(0);
525 if (c == '\n') {
526 file->lineno++;
527 break;
529 if (c == EOF)
530 break;
532 return (ERROR);
535 int
536 yylex(void)
538 unsigned char buf[8096];
539 unsigned char *p, *val;
540 int quotec, next, c;
541 int token;
543 top:
544 p = buf;
545 c = lgetc(0);
546 while (c == ' ' || c == '\t')
547 c = lgetc(0); /* nothing */
549 yylval.lineno = file->lineno;
550 if (c == '#') {
551 c = lgetc(0);
552 while (c != '\n' && c != EOF)
553 c = lgetc(0); /* nothing */
555 if (c == '$' && parsebuf == NULL) {
556 while (1) {
557 c = lgetc(0);
558 if (c == EOF)
559 return (0);
561 if (p + 1 >= buf + sizeof(buf) - 1) {
562 yyerror("string too long");
563 return (findeol());
565 if (isalnum(c) || c == '_') {
566 *p++ = c;
567 continue;
569 *p = '\0';
570 lungetc(c);
571 break;
573 val = symget(buf);
574 if (val == NULL) {
575 yyerror("macro '%s' not defined", buf);
576 return (findeol());
578 parsebuf = val;
579 parseindex = 0;
580 goto top;
583 switch (c) {
584 case '\'':
585 case '"':
586 quotec = c;
587 while (1) {
588 c = lgetc(quotec);
589 if (c == EOF)
590 return (0);
591 if (c == '\n') {
592 file->lineno++;
593 continue;
594 } else if (c == '\\') {
595 next = lgetc(quotec);
596 if (next == EOF)
597 return (0);
598 if (next == quotec || c == ' ' || c == '\t')
599 c = next;
600 else if (next == '\n') {
601 file->lineno++;
602 continue;
603 } else
604 lungetc(next);
605 } else if (c == quotec) {
606 *p = '\0';
607 break;
608 } else if (c == '\0') {
609 yyerror("syntax error");
610 return (findeol());
612 if (p + 1 >= buf + sizeof(buf) - 1) {
613 yyerror("string too long");
614 return (findeol());
616 *p++ = c;
618 yylval.v.string = strdup(buf);
619 if (yylval.v.string == NULL)
620 err(1, "yylex: strdup");
621 return (STRING);
624 #define allowed_to_end_number(x) \
625 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
627 if (c == '-' || isdigit(c)) {
628 do {
629 *p++ = c;
630 if ((unsigned)(p-buf) >= sizeof(buf)) {
631 yyerror("string too long");
632 return (findeol());
634 c = lgetc(0);
635 } while (c != EOF && isdigit(c));
636 lungetc(c);
637 if (p == buf + 1 && buf[0] == '-')
638 goto nodigits;
639 if (c == EOF || allowed_to_end_number(c)) {
640 const char *errstr = NULL;
642 *p = '\0';
643 yylval.v.number = strtonum(buf, LLONG_MIN,
644 LLONG_MAX, &errstr);
645 if (errstr) {
646 yyerror("\"%s\" invalid number: %s",
647 buf, errstr);
648 return (findeol());
650 return (NUMBER);
651 } else {
652 nodigits:
653 while (p > buf + 1)
654 lungetc(*--p);
655 c = *--p;
656 if (c == '-')
657 return (c);
661 #define allowed_in_string(x) \
662 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
663 x != '{' && x != '}' && \
664 x != '!' && x != '=' && x != '#' && \
665 x != ','))
667 if (isalnum(c) || c == ':' || c == '_') {
668 do {
669 *p++ = c;
670 if ((unsigned)(p-buf) >= sizeof(buf)) {
671 yyerror("string too long");
672 return (findeol());
674 c = lgetc(0);
675 } while (c != EOF && (allowed_in_string(c)));
676 lungetc(c);
677 *p = '\0';
678 token = lookup(buf);
679 if (token == STRING) {
680 yylval.v.string = strdup(buf);
681 if (yylval.v.string == NULL)
682 err(1, "yylex: strdup");
684 return (token);
686 if (c == '\n') {
687 yylval.lineno = file->lineno;
688 file->lineno++;
690 if (c == EOF)
691 return (0);
692 return (c);
695 int
696 check_file_secrecy(int fd, const char *fname)
698 struct stat st;
700 if (fstat(fd, &st)) {
701 log_warn("cannot stat %s", fname);
702 return (-1);
704 if (st.st_uid != 0 && st.st_uid != getuid()) {
705 log_warnx("%s: owner not root or current user", fname);
706 return (-1);
708 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
709 log_warnx("%s: group writable or world read/writable", fname);
710 return (-1);
712 return (0);
715 struct file *
716 newfile(const char *name, int secret)
718 struct file *nfile;
720 nfile = calloc(1, sizeof(struct file));
721 if (nfile == NULL) {
722 log_warn("calloc");
723 return (NULL);
725 nfile->name = strdup(name);
726 if (nfile->name == NULL) {
727 log_warn("strdup");
728 free(nfile);
729 return (NULL);
731 nfile->stream = fopen(nfile->name, "r");
732 if (nfile->stream == NULL) {
733 /* no warning, we don't require a conf file */
734 free(nfile->name);
735 free(nfile);
736 return (NULL);
737 } else if (secret &&
738 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
739 fclose(nfile->stream);
740 free(nfile->name);
741 free(nfile);
742 return (NULL);
744 nfile->lineno = 1;
745 return (nfile);
748 static void
749 closefile(struct file *xfile)
751 fclose(xfile->stream);
752 free(xfile->name);
753 free(xfile);
756 static void
757 add_default_server(void)
759 new_srv = conf_new_server(D_SITENAME);
760 log_debug("%s: adding default server %s", __func__, D_SITENAME);
763 int
764 parse_config(const char *filename, struct gotwebd *env)
766 struct sym *sym, *next;
768 if (config_init(env) == -1)
769 fatalx("failed to initialize configuration");
771 gotwebd = env;
773 file = newfile(filename, 0);
774 if (file == NULL) {
775 add_default_server();
776 sockets_parse_sockets(env);
777 /* just return, as we don't require a conf file */
778 return (0);
781 yyparse();
782 errors = file->errors;
783 closefile(file);
785 /* Free macros and check which have not been used. */
786 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
787 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
788 fprintf(stderr, "warning: macro '%s' not used\n",
789 sym->nam);
790 if (!sym->persist) {
791 free(sym->nam);
792 free(sym->val);
793 TAILQ_REMOVE(&symhead, sym, entry);
794 free(sym);
798 if (errors)
799 return (-1);
801 /* just add default server if no config specified */
802 if (gotwebd->server_cnt == 0)
803 add_default_server();
805 /* setup our listening sockets */
806 sockets_parse_sockets(env);
808 return (0);
811 struct server *
812 conf_new_server(const char *name)
814 struct server *srv = NULL;
816 srv = calloc(1, sizeof(*srv));
817 if (srv == NULL)
818 fatalx("%s: calloc", __func__);
820 n = strlcpy(srv->name, name, sizeof(srv->name));
821 if (n >= sizeof(srv->name))
822 fatalx("%s: strlcpy", __func__);
823 n = snprintf(srv->unix_socket_name,
824 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
825 D_UNIX_SOCKET);
826 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
827 fatalx("%s: snprintf", __func__);
828 n = strlcpy(srv->repos_path, D_GOTPATH,
829 sizeof(srv->repos_path));
830 if (n >= sizeof(srv->repos_path))
831 fatalx("%s: strlcpy", __func__);
832 n = strlcpy(srv->site_name, D_SITENAME,
833 sizeof(srv->site_name));
834 if (n >= sizeof(srv->site_name))
835 fatalx("%s: strlcpy", __func__);
836 n = strlcpy(srv->site_owner, D_SITEOWNER,
837 sizeof(srv->site_owner));
838 if (n >= sizeof(srv->site_owner))
839 fatalx("%s: strlcpy", __func__);
840 n = strlcpy(srv->site_link, D_SITELINK,
841 sizeof(srv->site_link));
842 if (n >= sizeof(srv->site_link))
843 fatalx("%s: strlcpy", __func__);
844 n = strlcpy(srv->logo, D_GOTLOGO,
845 sizeof(srv->logo));
846 if (n >= sizeof(srv->logo))
847 fatalx("%s: strlcpy", __func__);
848 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
849 if (n >= sizeof(srv->logo_url))
850 fatalx("%s: strlcpy", __func__);
851 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
852 if (n >= sizeof(srv->custom_css))
853 fatalx("%s: strlcpy", __func__);
855 srv->show_site_owner = D_SHOWSOWNER;
856 srv->show_repo_owner = D_SHOWROWNER;
857 srv->show_repo_age = D_SHOWAGE;
858 srv->show_repo_description = D_SHOWDESC;
859 srv->show_repo_cloneurl = D_SHOWURL;
861 srv->max_repos_display = D_MAXREPODISP;
862 srv->max_commits_display = D_MAXCOMMITDISP;
863 srv->max_repos = D_MAXREPO;
865 srv->unix_socket = 1;
866 srv->fcgi_socket = 0;
868 TAILQ_INIT(&srv->al);
869 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
870 gotwebd->server_cnt++;
872 return srv;
873 };
875 int
876 symset(const char *nam, const char *val, int persist)
878 struct sym *sym;
880 TAILQ_FOREACH(sym, &symhead, entry) {
881 if (strcmp(nam, sym->nam) == 0)
882 break;
885 if (sym != NULL) {
886 if (sym->persist == 1)
887 return (0);
888 else {
889 free(sym->nam);
890 free(sym->val);
891 TAILQ_REMOVE(&symhead, sym, entry);
892 free(sym);
895 sym = calloc(1, sizeof(*sym));
896 if (sym == NULL)
897 return (-1);
899 sym->nam = strdup(nam);
900 if (sym->nam == NULL) {
901 free(sym);
902 return (-1);
904 sym->val = strdup(val);
905 if (sym->val == NULL) {
906 free(sym->nam);
907 free(sym);
908 return (-1);
910 sym->used = 0;
911 sym->persist = persist;
912 TAILQ_INSERT_TAIL(&symhead, sym, entry);
913 return (0);
916 int
917 cmdline_symset(char *s)
919 char *sym, *val;
920 int ret;
922 val = strrchr(s, '=');
923 if (val == NULL)
924 return (-1);
926 sym = strndup(s, val - s);
927 if (sym == NULL)
928 fatal("%s: strndup", __func__);
930 ret = symset(sym, val + 1, 1);
931 free(sym);
933 return (ret);
936 char *
937 symget(const char *nam)
939 struct sym *sym;
941 TAILQ_FOREACH(sym, &symhead, entry) {
942 if (strcmp(nam, sym->nam) == 0) {
943 sym->used = 1;
944 return (sym->val);
947 return (NULL);
950 int
951 getservice(const char *n)
953 struct servent *s;
954 const char *errstr;
955 long long llval;
957 llval = strtonum(n, 0, UINT16_MAX, &errstr);
958 if (errstr) {
959 s = getservbyname(n, "tcp");
960 if (s == NULL)
961 s = getservbyname(n, "udp");
962 if (s == NULL)
963 return (-1);
964 return ntohs(s->s_port);
967 return (unsigned short)llval;
970 struct address *
971 host_v4(const char *s)
973 struct in_addr ina;
974 struct sockaddr_in *sain;
975 struct address *h;
977 memset(&ina, 0, sizeof(ina));
978 if (inet_pton(AF_INET, s, &ina) != 1)
979 return (NULL);
981 if ((h = calloc(1, sizeof(*h))) == NULL)
982 fatal(__func__);
983 sain = (struct sockaddr_in *)&h->ss;
984 got_sockaddr_inet_init(sain, &ina);
985 if (sain->sin_addr.s_addr == INADDR_ANY)
986 h->prefixlen = 0; /* 0.0.0.0 address */
987 else
988 h->prefixlen = -1; /* host address */
989 return (h);
992 struct address *
993 host_v6(const char *s)
995 struct addrinfo hints, *res;
996 struct sockaddr_in6 *sa_in6, *ra;
997 struct address *h = NULL;
999 memset(&hints, 0, sizeof(hints));
1000 hints.ai_family = AF_INET6;
1001 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1002 hints.ai_flags = AI_NUMERICHOST;
1003 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1004 if ((h = calloc(1, sizeof(*h))) == NULL)
1005 fatal(__func__);
1006 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1007 ra = (struct sockaddr_in6 *)res->ai_addr;
1008 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1009 ra->sin6_scope_id);
1010 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1011 sizeof(sa_in6->sin6_addr)) == 0)
1012 h->prefixlen = 0; /* any address */
1013 else
1014 h->prefixlen = -1; /* host address */
1015 freeaddrinfo(res);
1018 return (h);
1021 int
1022 host_dns(const char *s, struct server *new_srv, int max,
1023 in_port_t port, const char *ifname, int ipproto)
1025 struct addrinfo hints, *res0, *res;
1026 int error, cnt = 0;
1027 struct sockaddr_in *sain;
1028 struct sockaddr_in6 *sin6;
1029 struct address *h;
1031 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1032 return (cnt);
1034 memset(&hints, 0, sizeof(hints));
1035 hints.ai_family = PF_UNSPEC;
1036 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1037 hints.ai_flags = AI_ADDRCONFIG;
1038 error = getaddrinfo(s, NULL, &hints, &res0);
1039 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1040 return (0);
1041 if (error) {
1042 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1043 gai_strerror(error));
1044 return (-1);
1047 for (res = res0; res && cnt < max; res = res->ai_next) {
1048 if (res->ai_family != AF_INET &&
1049 res->ai_family != AF_INET6)
1050 continue;
1051 if ((h = calloc(1, sizeof(*h))) == NULL)
1052 fatal(__func__);
1054 if (port)
1055 h->port = port;
1056 if (ifname != NULL) {
1057 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1058 sizeof(h->ifname)) {
1059 log_warnx("%s: interface name truncated",
1060 __func__);
1061 freeaddrinfo(res0);
1062 free(h);
1063 return (-1);
1066 if (ipproto != -1)
1067 h->ipproto = ipproto;
1068 h->ss.ss_family = res->ai_family;
1069 h->prefixlen = -1; /* host address */
1071 if (res->ai_family == AF_INET) {
1072 struct sockaddr_in *ra;
1073 sain = (struct sockaddr_in *)&h->ss;
1074 ra = (struct sockaddr_in *)res->ai_addr;
1075 got_sockaddr_inet_init(sain, &ra->sin_addr);
1076 } else {
1077 struct sockaddr_in6 *ra;
1078 sin6 = (struct sockaddr_in6 *)&h->ss;
1079 ra = (struct sockaddr_in6 *)res->ai_addr;
1080 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1083 if (add_addr(new_srv, h))
1084 return -1;
1085 cnt++;
1087 if (cnt == max && res) {
1088 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1089 s, max);
1091 freeaddrinfo(res0);
1092 return (cnt);
1095 int
1096 host_if(const char *s, struct server *new_srv, int max,
1097 in_port_t port, const char *ifname, int ipproto)
1099 struct ifaddrs *ifap, *p;
1100 struct sockaddr_in *sain;
1101 struct sockaddr_in6 *sin6;
1102 struct address *h;
1103 int cnt = 0, af;
1105 if (getifaddrs(&ifap) == -1)
1106 fatal("getifaddrs");
1108 /* First search for IPv4 addresses */
1109 af = AF_INET;
1111 nextaf:
1112 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1113 if (p->ifa_addr == NULL ||
1114 p->ifa_addr->sa_family != af ||
1115 (strcmp(s, p->ifa_name) != 0 &&
1116 !is_if_in_group(p->ifa_name, s)))
1117 continue;
1118 if ((h = calloc(1, sizeof(*h))) == NULL)
1119 fatal("calloc");
1121 if (port)
1122 h->port = port;
1123 if (ifname != NULL) {
1124 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1125 sizeof(h->ifname)) {
1126 log_warnx("%s: interface name truncated",
1127 __func__);
1128 free(h);
1129 freeifaddrs(ifap);
1130 return (-1);
1133 if (ipproto != -1)
1134 h->ipproto = ipproto;
1135 h->ss.ss_family = af;
1136 h->prefixlen = -1; /* host address */
1138 if (af == AF_INET) {
1139 struct sockaddr_in *ra;
1140 sain = (struct sockaddr_in *)&h->ss;
1141 ra = (struct sockaddr_in *)p->ifa_addr;
1142 got_sockaddr_inet_init(sain, &ra->sin_addr);
1143 } else {
1144 struct sockaddr_in6 *ra;
1145 sin6 = (struct sockaddr_in6 *)&h->ss;
1146 ra = (struct sockaddr_in6 *)p->ifa_addr;
1147 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1148 ra->sin6_scope_id);
1151 if (add_addr(new_srv, h))
1152 return -1;
1153 cnt++;
1155 if (af == AF_INET) {
1156 /* Next search for IPv6 addresses */
1157 af = AF_INET6;
1158 goto nextaf;
1161 if (cnt > max) {
1162 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1163 s, max);
1165 freeifaddrs(ifap);
1166 return (cnt);
1169 int
1170 host(const char *s, struct server *new_srv, int max,
1171 in_port_t port, const char *ifname, int ipproto)
1173 struct address *h;
1175 h = host_v4(s);
1177 /* IPv6 address? */
1178 if (h == NULL)
1179 h = host_v6(s);
1181 if (h != NULL) {
1182 if (port)
1183 h->port = port;
1184 if (ifname != NULL) {
1185 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1186 sizeof(h->ifname)) {
1187 log_warnx("%s: interface name truncated",
1188 __func__);
1189 free(h);
1190 return (-1);
1193 if (ipproto != -1)
1194 h->ipproto = ipproto;
1196 if (add_addr(new_srv, h))
1197 return -1;
1198 return (1);
1201 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1204 int
1205 is_if_in_group(const char *ifname, const char *groupname)
1207 unsigned int len;
1208 struct ifgroupreq ifgr;
1209 struct ifg_req *ifg;
1210 int s;
1211 int ret = 0;
1213 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1214 err(1, "socket");
1216 memset(&ifgr, 0, sizeof(ifgr));
1217 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1218 err(1, "IFNAMSIZ");
1219 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1220 if (errno == EINVAL || errno == ENOTTY)
1221 goto end;
1222 err(1, "SIOCGIFGROUP");
1225 len = ifgr.ifgr_len;
1226 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1227 sizeof(struct ifg_req));
1228 if (ifgr.ifgr_groups == NULL)
1229 err(1, "getifgroups");
1230 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1231 err(1, "SIOCGIFGROUP");
1233 ifg = ifgr.ifgr_groups;
1234 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1235 len -= sizeof(struct ifg_req);
1236 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1237 ret = 1;
1238 break;
1241 free(ifgr.ifgr_groups);
1243 end:
1244 close(s);
1245 return (ret);
1248 int
1249 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1251 if (strcmp("", addr) == 0) {
1252 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1253 -1) <= 0) {
1254 yyerror("invalid listen ip: %s",
1255 "127.0.0.1");
1256 return (-1);
1258 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1259 yyerror("invalid listen ip: %s", "::1");
1260 return (-1);
1262 } else {
1263 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1264 -1) <= 0) {
1265 yyerror("invalid listen ip: %s", addr);
1266 return (-1);
1269 return (0);
1272 int
1273 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1274 const char *other_srv)
1276 struct address *a;
1277 void *ia;
1278 char buf[INET6_ADDRSTRLEN];
1279 const char *addrstr;
1281 TAILQ_FOREACH(a, al, entry) {
1282 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1283 a->port != h->port)
1284 continue;
1286 switch (h->ss.ss_family) {
1287 case AF_INET:
1288 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1289 break;
1290 case AF_INET6:
1291 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1292 break;
1293 default:
1294 yyerror("unknown address family: %d", h->ss.ss_family);
1295 return -1;
1297 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1298 if (addrstr) {
1299 if (other_srv) {
1300 yyerror("server %s: duplicate fcgi listen "
1301 "address %s:%d, already used by server %s",
1302 new_srv, addrstr, h->port, other_srv);
1303 } else {
1304 log_warnx("server: %s: duplicate fcgi listen "
1305 "address %s:%d", new_srv, addrstr, h->port);
1307 } else {
1308 if (other_srv) {
1309 yyerror("server: %s: duplicate fcgi listen "
1310 "address, already used by server %s",
1311 new_srv, other_srv);
1312 } else {
1313 log_warnx("server %s: duplicate fcgi listen "
1314 "address", new_srv);
1318 return -1;
1321 return 0;
1324 int
1325 add_addr(struct server *new_srv, struct address *h)
1327 struct server *srv;
1329 /* Address cannot be shared between different servers. */
1330 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1331 if (srv == new_srv)
1332 continue;
1333 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1334 return -1;
1337 /* Tolerate duplicate address lines within the scope of a server. */
1338 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1339 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1340 else
1341 free(h);
1343 return 0;