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>
50 #include "got_sockaddr.h"
51 #include "got_reference.h"
56 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
58 TAILQ_ENTRY(file) entry;
64 struct file *newfile(const char *, int);
65 static void closefile(struct file *);
66 int check_file_secrecy(int, const char *);
69 int yyerror(const char *, ...)
70 __attribute__((__format__ (printf, 1, 2)))
71 __attribute__((__nonnull__ (1)));
72 int kw_cmp(const void *, const void *);
78 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
80 TAILQ_ENTRY(sym) entry;
87 int symset(const char *, const char *, int);
88 char *symget(const char *);
92 static struct gotwebd *gotwebd;
93 static struct server *new_srv;
94 static struct server *conf_new_server(const char *);
95 int getservice(const char *);
98 int get_addrs(const char *, const char *, struct server *);
99 int addr_dup_check(struct addresslist *, struct address *,
100 const char *, const char *);
101 int add_addr(struct server *, struct address *);
113 %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
114 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
115 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
116 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
117 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
119 %token <v.string> STRING
120 %token <v.number> NUMBER
121 %type <v.number> boolean
122 %type <v.string> listen_addr
126 grammar : /* empty */
128 | grammar varset '\n'
130 | grammar server '\n'
131 | grammar error '\n' { file->errors++; }
134 varset : STRING '=' STRING {
137 if (isspace((unsigned char)*s)) {
138 yyerror("macro name cannot contain "
145 if (symset($1, $3, 0) == -1)
146 fatal("cannot store variable");
153 if (strcasecmp($1, "1") == 0 ||
154 strcasecmp($1, "on") == 0)
156 else if (strcasecmp($1, "0") == 0 ||
157 strcasecmp($1, "off") == 0)
160 yyerror("invalid boolean value '%s'", $1);
168 if ($1 != 0 && $1 != 1) {
169 yyerror("invalid boolean value '%lld'", $1);
176 listen_addr : '*' { $$ = NULL; }
180 main : PREFORK NUMBER {
181 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
182 yyerror("prefork is %s: %lld",
183 $2 <= 0 ? "too small" : "too large", $2);
186 gotwebd->prefork_gotwebd = $2;
190 yyerror("chroot path can't be an empty"
196 n = strlcpy(gotwebd->httpd_chroot, $2,
197 sizeof(gotwebd->httpd_chroot));
198 if (n >= sizeof(gotwebd->httpd_chroot)) {
199 yyerror("%s: httpd_chroot truncated", __func__);
205 | UNIX_SOCKET boolean {
206 gotwebd->unix_socket = $2;
208 | UNIX_SOCKET_NAME STRING {
209 n = snprintf(gotwebd->unix_socket_name,
210 sizeof(gotwebd->unix_socket_name), "%s%s",
211 gotwebd->httpd_chroot, $2);
213 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
214 yyerror("%s: unix_socket_name truncated",
223 server : SERVER STRING {
226 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
227 if (strcmp(srv->name, $2) == 0) {
228 yyerror("server name exists '%s'", $2);
234 new_srv = conf_new_server($2);
235 log_debug("adding server %s", $2);
241 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
242 if (strcmp(srv->name, $2) == 0) {
243 yyerror("server name exists '%s'", $2);
249 new_srv = conf_new_server($2);
250 log_debug("adding server %s", $2);
252 } '{' optnl serveropts2 '}' {
256 serveropts1 : REPOS_PATH STRING {
257 n = strlcpy(new_srv->repos_path, $2,
258 sizeof(new_srv->repos_path));
259 if (n >= sizeof(new_srv->repos_path)) {
260 yyerror("%s: repos_path truncated", __func__);
267 n = strlcpy(new_srv->site_name, $2,
268 sizeof(new_srv->site_name));
269 if (n >= sizeof(new_srv->site_name)) {
270 yyerror("%s: site_name truncated", __func__);
276 | SITE_OWNER STRING {
277 n = strlcpy(new_srv->site_owner, $2,
278 sizeof(new_srv->site_owner));
279 if (n >= sizeof(new_srv->site_owner)) {
280 yyerror("%s: site_owner truncated", __func__);
287 n = strlcpy(new_srv->site_link, $2,
288 sizeof(new_srv->site_link));
289 if (n >= sizeof(new_srv->site_link)) {
290 yyerror("%s: site_link truncated", __func__);
297 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
298 if (n >= sizeof(new_srv->logo)) {
299 yyerror("%s: logo truncated", __func__);
306 n = strlcpy(new_srv->logo_url, $2,
307 sizeof(new_srv->logo_url));
308 if (n >= sizeof(new_srv->logo_url)) {
309 yyerror("%s: logo_url truncated", __func__);
315 | CUSTOM_CSS STRING {
316 n = strlcpy(new_srv->custom_css, $2,
317 sizeof(new_srv->custom_css));
318 if (n >= sizeof(new_srv->custom_css)) {
319 yyerror("%s: custom_css truncated", __func__);
325 | LISTEN ON listen_addr PORT STRING {
326 if (get_addrs($3, $5, new_srv) == -1) {
327 yyerror("could not get addrs");
332 new_srv->fcgi_socket = 1;
334 | LISTEN ON listen_addr PORT NUMBER {
338 n = snprintf(portno, sizeof(portno), "%lld",
340 if (n < 0 || (size_t)n >= sizeof(portno))
341 fatalx("port number too long: %lld",
344 if (get_addrs($3, portno, new_srv) == -1) {
345 yyerror("could not get addrs");
349 new_srv->fcgi_socket = 1;
351 | LISTEN ON SOCKET STRING {
352 if (strcasecmp($4, "off") == 0) {
353 new_srv->unix_socket = 0;
358 new_srv->unix_socket = 1;
360 n = snprintf(new_srv->unix_socket_name,
361 sizeof(new_srv->unix_socket_name), "%s%s",
362 gotwebd->httpd_chroot, $4);
364 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
365 yyerror("%s: unix_socket_name truncated",
374 yyerror("max_repos is too small: %lld", $2);
377 new_srv->max_repos = $2;
379 | SHOW_SITE_OWNER boolean {
380 new_srv->show_site_owner = $2;
382 | SHOW_REPO_OWNER boolean {
383 new_srv->show_repo_owner = $2;
385 | SHOW_REPO_AGE boolean {
386 new_srv->show_repo_age = $2;
388 | SHOW_REPO_DESCRIPTION boolean {
389 new_srv->show_repo_description = $2;
391 | SHOW_REPO_CLONEURL boolean {
392 new_srv->show_repo_cloneurl = $2;
394 | RESPECT_EXPORTOK boolean {
395 new_srv->respect_exportok = $2;
397 | MAX_REPOS_DISPLAY NUMBER {
399 yyerror("max_repos_display is too small: %lld",
403 new_srv->max_repos_display = $2;
405 | MAX_COMMITS_DISPLAY NUMBER {
407 yyerror("max_commits_display is too small:"
411 new_srv->max_commits_display = $2;
415 serveropts2 : serveropts2 serveropts1 nl
422 optnl : '\n' optnl /* zero or more newlines */
434 yyerror(const char *fmt, ...)
441 if (vasprintf(&msg, fmt, ap) == -1)
442 fatalx("yyerror vasprintf");
444 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
450 kw_cmp(const void *k, const void *e)
452 return (strcmp(k, ((const struct keywords *)e)->k_name));
458 /* This has to be sorted always. */
459 static const struct keywords keywords[] = {
460 { "chroot", CHROOT },
461 { "custom_css", CUSTOM_CSS },
462 { "listen", LISTEN },
464 { "logo_url", LOGO_URL },
465 { "max_commits_display", MAX_COMMITS_DISPLAY },
466 { "max_repos", MAX_REPOS },
467 { "max_repos_display", MAX_REPOS_DISPLAY },
470 { "prefork", PREFORK },
471 { "repos_path", REPOS_PATH },
472 { "respect_exportok", RESPECT_EXPORTOK },
473 { "server", SERVER },
474 { "show_repo_age", SHOW_REPO_AGE },
475 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
476 { "show_repo_description", SHOW_REPO_DESCRIPTION },
477 { "show_repo_owner", SHOW_REPO_OWNER },
478 { "show_site_owner", SHOW_SITE_OWNER },
479 { "site_link", SITE_LINK },
480 { "site_name", SITE_NAME },
481 { "site_owner", SITE_OWNER },
482 { "socket", SOCKET },
483 { "unix_socket", UNIX_SOCKET },
484 { "unix_socket_name", UNIX_SOCKET_NAME },
486 const struct keywords *p;
488 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
489 sizeof(keywords[0]), kw_cmp);
497 #define MAXPUSHBACK 128
499 unsigned char *parsebuf;
501 unsigned char pushback_buffer[MAXPUSHBACK];
502 int pushback_index = 0;
510 /* Read character from the parsebuffer instead of input. */
511 if (parseindex >= 0) {
512 c = parsebuf[parseindex++];
521 return (pushback_buffer[--pushback_index]);
524 c = getc(file->stream);
526 yyerror("reached end of file while parsing "
531 c = getc(file->stream);
533 next = getc(file->stream);
538 yylval.lineno = file->lineno;
540 c = getc(file->stream);
556 if (pushback_index < MAXPUSHBACK-1)
557 return (pushback_buffer[pushback_index++] = c);
569 /* Skip to either EOF or the first real EOL. */
572 c = pushback_buffer[--pushback_index];
588 unsigned char buf[8096];
589 unsigned char *p, *val;
596 while (c == ' ' || c == '\t')
597 c = lgetc(0); /* nothing */
599 yylval.lineno = file->lineno;
602 while (c != '\n' && c != EOF)
603 c = lgetc(0); /* nothing */
605 if (c == '$' && parsebuf == NULL) {
611 if (p + 1 >= buf + sizeof(buf) - 1) {
612 yyerror("string too long");
615 if (isalnum(c) || c == '_') {
625 yyerror("macro '%s' not defined", buf);
644 } else if (c == '\\') {
645 next = lgetc(quotec);
648 if (next == quotec || c == ' ' || c == '\t')
650 else if (next == '\n') {
655 } else if (c == quotec) {
658 } else if (c == '\0') {
659 yyerror("syntax error");
662 if (p + 1 >= buf + sizeof(buf) - 1) {
663 yyerror("string too long");
668 yylval.v.string = strdup(buf);
669 if (yylval.v.string == NULL)
670 err(1, "yylex: strdup");
674 #define allowed_to_end_number(x) \
675 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
677 if (c == '-' || isdigit(c)) {
680 if ((unsigned)(p-buf) >= sizeof(buf)) {
681 yyerror("string too long");
685 } while (c != EOF && isdigit(c));
687 if (p == buf + 1 && buf[0] == '-')
689 if (c == EOF || allowed_to_end_number(c)) {
690 const char *errstr = NULL;
693 yylval.v.number = strtonum(buf, LLONG_MIN,
696 yyerror("\"%s\" invalid number: %s",
711 #define allowed_in_string(x) \
712 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
713 x != '{' && x != '}' && \
714 x != '!' && x != '=' && x != '#' && \
717 if (isalnum(c) || c == ':' || c == '_') {
720 if ((unsigned)(p-buf) >= sizeof(buf)) {
721 yyerror("string too long");
725 } while (c != EOF && (allowed_in_string(c)));
729 if (token == STRING) {
730 yylval.v.string = strdup(buf);
731 if (yylval.v.string == NULL)
732 err(1, "yylex: strdup");
737 yylval.lineno = file->lineno;
746 check_file_secrecy(int fd, const char *fname)
750 if (fstat(fd, &st)) {
751 log_warn("cannot stat %s", fname);
754 if (st.st_uid != 0 && st.st_uid != getuid()) {
755 log_warnx("%s: owner not root or current user", fname);
758 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
759 log_warnx("%s: group writable or world read/writable", fname);
766 newfile(const char *name, int secret)
770 nfile = calloc(1, sizeof(struct file));
775 nfile->name = strdup(name);
776 if (nfile->name == NULL) {
781 nfile->stream = fopen(nfile->name, "r");
782 if (nfile->stream == NULL) {
783 /* no warning, we don't require a conf file */
788 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
789 fclose(nfile->stream);
799 closefile(struct file *xfile)
801 fclose(xfile->stream);
807 add_default_server(void)
809 new_srv = conf_new_server(D_SITENAME);
810 log_debug("%s: adding default server %s", __func__, D_SITENAME);
814 parse_config(const char *filename, struct gotwebd *env)
816 struct sym *sym, *next;
818 if (config_init(env) == -1)
819 fatalx("failed to initialize configuration");
823 file = newfile(filename, 0);
825 add_default_server();
826 sockets_parse_sockets(env);
827 /* just return, as we don't require a conf file */
832 errors = file->errors;
835 /* Free macros and check which have not been used. */
836 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
837 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
838 fprintf(stderr, "warning: macro '%s' not used\n",
843 TAILQ_REMOVE(&symhead, sym, entry);
851 /* just add default server if no config specified */
852 if (gotwebd->server_cnt == 0)
853 add_default_server();
855 /* setup our listening sockets */
856 sockets_parse_sockets(env);
862 conf_new_server(const char *name)
864 struct server *srv = NULL;
866 srv = calloc(1, sizeof(*srv));
868 fatalx("%s: calloc", __func__);
870 n = strlcpy(srv->name, name, sizeof(srv->name));
871 if (n >= sizeof(srv->name))
872 fatalx("%s: strlcpy", __func__);
873 n = snprintf(srv->unix_socket_name,
874 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
876 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
877 fatalx("%s: snprintf", __func__);
878 n = strlcpy(srv->repos_path, D_GOTPATH,
879 sizeof(srv->repos_path));
880 if (n >= sizeof(srv->repos_path))
881 fatalx("%s: strlcpy", __func__);
882 n = strlcpy(srv->site_name, D_SITENAME,
883 sizeof(srv->site_name));
884 if (n >= sizeof(srv->site_name))
885 fatalx("%s: strlcpy", __func__);
886 n = strlcpy(srv->site_owner, D_SITEOWNER,
887 sizeof(srv->site_owner));
888 if (n >= sizeof(srv->site_owner))
889 fatalx("%s: strlcpy", __func__);
890 n = strlcpy(srv->site_link, D_SITELINK,
891 sizeof(srv->site_link));
892 if (n >= sizeof(srv->site_link))
893 fatalx("%s: strlcpy", __func__);
894 n = strlcpy(srv->logo, D_GOTLOGO,
896 if (n >= sizeof(srv->logo))
897 fatalx("%s: strlcpy", __func__);
898 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
899 if (n >= sizeof(srv->logo_url))
900 fatalx("%s: strlcpy", __func__);
901 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
902 if (n >= sizeof(srv->custom_css))
903 fatalx("%s: strlcpy", __func__);
905 srv->show_site_owner = D_SHOWSOWNER;
906 srv->show_repo_owner = D_SHOWROWNER;
907 srv->show_repo_age = D_SHOWAGE;
908 srv->show_repo_description = D_SHOWDESC;
909 srv->show_repo_cloneurl = D_SHOWURL;
910 srv->respect_exportok = D_RESPECTEXPORTOK;
912 srv->max_repos_display = D_MAXREPODISP;
913 srv->max_commits_display = D_MAXCOMMITDISP;
914 srv->max_repos = D_MAXREPO;
916 srv->unix_socket = 1;
917 srv->fcgi_socket = 0;
919 TAILQ_INIT(&srv->al);
920 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
921 gotwebd->server_cnt++;
927 symset(const char *nam, const char *val, int persist)
931 TAILQ_FOREACH(sym, &symhead, entry) {
932 if (strcmp(nam, sym->nam) == 0)
937 if (sym->persist == 1)
942 TAILQ_REMOVE(&symhead, sym, entry);
946 sym = calloc(1, sizeof(*sym));
950 sym->nam = strdup(nam);
951 if (sym->nam == NULL) {
955 sym->val = strdup(val);
956 if (sym->val == NULL) {
962 sym->persist = persist;
963 TAILQ_INSERT_TAIL(&symhead, sym, entry);
968 cmdline_symset(char *s)
973 val = strrchr(s, '=');
977 sym = strndup(s, val - s);
979 fatal("%s: strndup", __func__);
981 ret = symset(sym, val + 1, 1);
988 symget(const char *nam)
992 TAILQ_FOREACH(sym, &symhead, entry) {
993 if (strcmp(nam, sym->nam) == 0) {
1002 get_addrs(const char *hostname, const char *servname, struct server *new_srv)
1004 struct addrinfo hints, *res0, *res;
1006 struct sockaddr_in *sain, *ra;
1007 struct sockaddr_in6 *sin6, *ra6;
1010 memset(&hints, 0, sizeof(hints));
1011 hints.ai_family = AF_UNSPEC;
1012 hints.ai_socktype = SOCK_STREAM;
1013 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
1014 error = getaddrinfo(hostname, servname, &hints, &res0);
1016 log_warnx("%s: could not parse \"%s:%s\": %s", __func__,
1017 hostname, servname, gai_strerror(error));
1021 for (res = res0; res; res = res->ai_next) {
1022 if ((h = calloc(1, sizeof(*h))) == NULL)
1025 if (hostname == NULL) {
1026 strlcpy(h->ifname, "*", sizeof(h->ifname));
1028 if (strlcpy(h->ifname, hostname, sizeof(h->ifname)) >=
1029 sizeof(h->ifname)) {
1030 log_warnx("%s: address truncated", __func__);
1036 h->ss.ss_family = res->ai_family;
1038 switch (res->ai_family) {
1040 sain = (struct sockaddr_in *)&h->ss;
1041 ra = (struct sockaddr_in *)res->ai_addr;
1042 h->port = ntohs(ra->sin_port);
1043 got_sockaddr_inet_init(sain, &ra->sin_addr);
1046 sin6 = (struct sockaddr_in6 *)&h->ss;
1047 ra6 = (struct sockaddr_in6 *)res->ai_addr;
1048 h->port = ntohs(ra6->sin6_port);
1049 got_sockaddr_inet6_init(sin6, &ra6->sin6_addr, 0);
1052 fatalx("unknown address family %d", res->ai_family);
1055 if (add_addr(new_srv, h))
1063 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1064 const char *other_srv)
1068 char buf[INET6_ADDRSTRLEN];
1069 const char *addrstr;
1071 TAILQ_FOREACH(a, al, entry) {
1072 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1076 switch (h->ss.ss_family) {
1078 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1081 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1084 yyerror("unknown address family: %d", h->ss.ss_family);
1087 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1090 yyerror("server %s: duplicate fcgi listen "
1091 "address %s:%d, already used by server %s",
1092 new_srv, addrstr, h->port, other_srv);
1094 log_warnx("server: %s: duplicate fcgi listen "
1095 "address %s:%d", new_srv, addrstr, h->port);
1099 yyerror("server: %s: duplicate fcgi listen "
1100 "address, already used by server %s",
1101 new_srv, other_srv);
1103 log_warnx("server %s: duplicate fcgi listen "
1104 "address", new_srv);
1115 add_addr(struct server *new_srv, struct address *h)
1119 /* Address cannot be shared between different servers. */
1120 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1123 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1127 /* Tolerate duplicate address lines within the scope of a server. */
1128 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1129 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);