2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
4 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #include <sys/types.h>
27 #include <sys/queue.h>
43 #include "got_error.h"
51 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
53 TAILQ_ENTRY(file) entry;
59 struct file *newfile(const char *, int, int);
60 static void closefile(struct file *);
61 int check_file_secrecy(int, const char *);
64 int yyerror(const char *, ...)
65 __attribute__((__format__ (printf, 1, 2)))
66 __attribute__((__nonnull__ (1)));
67 int kw_cmp(const void *, const void *);
73 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 TAILQ_ENTRY(sym) entry;
82 int symset(const char *, const char *, int);
83 char *symget(const char *);
87 static struct gotd *gotd;
88 static struct gotd_repo *new_repo;
89 static int conf_limit_user_connections(const char *, int);
90 static struct gotd_repo *conf_new_repo(const char *);
91 static void conf_new_access_rule(struct gotd_repo *,
92 enum gotd_access, int, char *);
93 static enum gotd_procid gotd_proc_id;
106 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
107 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
109 %token <v.string> STRING
110 %token <v.number> NUMBER
118 | grammar repository '\n'
123 yyerror("invalid timeout: %lld", $1);
131 const char *type = "seconds";
136 yyerror("invalid number of seconds: %s", $1);
142 switch ($1[len - 1]) {
162 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
164 yyerror("number of %s is %s: %s", type,
175 main : LISTEN ON STRING {
176 if (!got_path_is_absolute($3))
177 yyerror("bad unix socket path \"%s\": "
178 "must be an absolute path", $3);
180 if (gotd_proc_id == PROC_LISTEN) {
181 if (strlcpy(gotd->unix_socket_path, $3,
182 sizeof(gotd->unix_socket_path)) >=
183 sizeof(gotd->unix_socket_path)) {
184 yyerror("%s: unix socket path too long",
193 if (strlcpy(gotd->user_name, $2,
194 sizeof(gotd->user_name)) >=
195 sizeof(gotd->user_name)) {
196 yyerror("%s: user name too long", __func__);
205 connection : CONNECTION '{' optnl conflags_l '}'
206 | CONNECTION conflags
208 conflags_l : conflags optnl conflags_l
212 conflags : REQUEST TIMEOUT timeout {
213 if ($3.tv_sec <= 0) {
214 yyerror("invalid timeout: %lld", $3.tv_sec);
217 memcpy(&gotd->request_timeout, &$3,
218 sizeof(gotd->request_timeout));
220 | LIMIT USER STRING NUMBER {
221 if (gotd_proc_id == PROC_LISTEN &&
222 conf_limit_user_connections($3, $4) == -1) {
230 repository : REPOSITORY STRING {
231 struct gotd_repo *repo;
233 TAILQ_FOREACH(repo, &gotd->repos, entry) {
234 if (strcmp(repo->name, $2) == 0) {
235 yyerror("duplicate repository '%s'", $2);
241 if (gotd_proc_id == PROC_GOTD ||
242 gotd_proc_id == PROC_AUTH) {
243 new_repo = conf_new_repo($2);
246 } '{' optnl repoopts2 '}' {
250 repoopts1 : PATH STRING {
251 if (gotd_proc_id == PROC_GOTD ||
252 gotd_proc_id == PROC_AUTH) {
253 if (!got_path_is_absolute($2)) {
254 yyerror("%s: path %s is not absolute",
259 if (realpath($2, new_repo->path) == NULL) {
260 yyerror("realpath %s: %s", $2, strerror(errno));
268 if (gotd_proc_id == PROC_AUTH) {
269 conf_new_access_rule(new_repo,
270 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
274 if (gotd_proc_id == PROC_AUTH) {
275 conf_new_access_rule(new_repo,
276 GOTD_ACCESS_PERMITTED,
277 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
281 if (gotd_proc_id == PROC_AUTH) {
282 conf_new_access_rule(new_repo,
283 GOTD_ACCESS_DENIED, 0, $2);
288 repoopts2 : repoopts2 repoopts1 nl
295 optnl : '\n' optnl /* zero or more newlines */
307 yyerror(const char *fmt, ...)
314 if (vasprintf(&msg, fmt, ap) == -1)
315 fatalx("yyerror vasprintf");
317 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
323 kw_cmp(const void *k, const void *e)
325 return (strcmp(k, ((const struct keywords *)e)->k_name));
331 /* This has to be sorted always. */
332 static const struct keywords keywords[] = {
333 { "connection", CONNECTION },
336 { "listen", LISTEN },
339 { "permit", PERMIT },
340 { "repository", REPOSITORY },
341 { "request", REQUEST },
344 { "timeout", TIMEOUT },
347 const struct keywords *p;
349 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
350 sizeof(keywords[0]), kw_cmp);
358 #define MAXPUSHBACK 128
360 unsigned char *parsebuf;
362 unsigned char pushback_buffer[MAXPUSHBACK];
363 int pushback_index = 0;
371 /* Read character from the parsebuffer instead of input. */
372 if (parseindex >= 0) {
373 c = parsebuf[parseindex++];
382 return (pushback_buffer[--pushback_index]);
385 c = getc(file->stream);
387 yyerror("reached end of file while parsing "
392 c = getc(file->stream);
394 next = getc(file->stream);
399 yylval.lineno = file->lineno;
401 c = getc(file->stream);
417 if (pushback_index < MAXPUSHBACK-1)
418 return (pushback_buffer[pushback_index++] = c);
430 /* Skip to either EOF or the first real EOL. */
433 c = pushback_buffer[--pushback_index];
449 unsigned char buf[8096];
450 unsigned char *p, *val;
457 while (c == ' ' || c == '\t')
458 c = lgetc(0); /* nothing */
460 yylval.lineno = file->lineno;
463 while (c != '\n' && c != EOF)
464 c = lgetc(0); /* nothing */
466 if (c == '$' && parsebuf == NULL) {
472 if (p + 1 >= buf + sizeof(buf) - 1) {
473 yyerror("string too long");
476 if (isalnum(c) || c == '_') {
486 yyerror("macro '%s' not defined", buf);
505 } else if (c == '\\') {
506 next = lgetc(quotec);
509 if (next == quotec || c == ' ' || c == '\t')
511 else if (next == '\n') {
516 } else if (c == quotec) {
519 } else if (c == '\0') {
520 yyerror("syntax error");
523 if (p + 1 >= buf + sizeof(buf) - 1) {
524 yyerror("string too long");
529 yylval.v.string = strdup(buf);
530 if (yylval.v.string == NULL)
531 err(1, "yylex: strdup");
535 #define allowed_to_end_number(x) \
536 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
538 if (c == '-' || isdigit(c)) {
541 if ((unsigned)(p-buf) >= sizeof(buf)) {
542 yyerror("string too long");
546 } while (c != EOF && isdigit(c));
548 if (p == buf + 1 && buf[0] == '-')
550 if (c == EOF || allowed_to_end_number(c)) {
551 const char *errstr = NULL;
554 yylval.v.number = strtonum(buf, LLONG_MIN,
557 yyerror("\"%s\" invalid number: %s",
572 #define allowed_in_string(x) \
573 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
574 x != '{' && x != '}' && \
575 x != '!' && x != '=' && x != '#' && \
578 if (isalnum(c) || c == ':' || c == '_') {
581 if ((unsigned)(p-buf) >= sizeof(buf)) {
582 yyerror("string too long");
586 } while (c != EOF && (allowed_in_string(c)));
590 if (token == STRING) {
591 yylval.v.string = strdup(buf);
592 if (yylval.v.string == NULL)
593 err(1, "yylex: strdup");
598 yylval.lineno = file->lineno;
607 check_file_secrecy(int fd, const char *fname)
611 if (fstat(fd, &st)) {
612 log_warn("cannot stat %s", fname);
615 if (st.st_uid != 0 && st.st_uid != getuid()) {
616 log_warnx("%s: owner not root or current user", fname);
619 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
620 log_warnx("%s: group writable or world read/writable", fname);
627 newfile(const char *name, int secret, int required)
631 nfile = calloc(1, sizeof(struct file));
636 nfile->name = strdup(name);
637 if (nfile->name == NULL) {
642 nfile->stream = fopen(nfile->name, "r");
643 if (nfile->stream == NULL) {
645 log_warn("open %s", nfile->name);
650 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
651 fclose(nfile->stream);
661 closefile(struct file *xfile)
663 fclose(xfile->stream);
669 parse_config(const char *filename, enum gotd_procid proc_id,
670 struct gotd *env, int require_config_file)
672 struct sym *sym, *next;
673 struct gotd_repo *repo;
675 memset(env, 0, sizeof(*env));
678 gotd_proc_id = proc_id;
679 TAILQ_INIT(&gotd->repos);
681 /* Apply default values. */
682 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
683 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
684 fprintf(stderr, "%s: unix socket path too long", __func__);
687 if (strlcpy(gotd->user_name, GOTD_USER,
688 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
689 fprintf(stderr, "%s: user name too long", __func__);
693 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
694 gotd->request_timeout.tv_usec = 0;
696 file = newfile(filename, 0, require_config_file);
698 return require_config_file ? -1 : 0;
701 errors = file->errors;
704 /* Free macros and check which have not been used. */
705 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
706 if ((gotd->verbosity > 1) && !sym->used)
707 fprintf(stderr, "warning: macro '%s' not used\n",
712 TAILQ_REMOVE(&symhead, sym, entry);
720 TAILQ_FOREACH(repo, &gotd->repos, entry) {
721 if (repo->path[0] == '\0') {
722 log_warnx("repository \"%s\": no path provided in "
723 "configuration file", repo->name);
728 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
729 log_warnx("no repository defined in configuration file");
737 uid_connection_limit_cmp(const void *pa, const void *pb)
739 const struct gotd_uid_connection_limit *a = pa, *b = pb;
743 else if (a->uid > b->uid);
750 conf_limit_user_connections(const char *user, int maximum)
753 struct gotd_uid_connection_limit *limit;
757 yyerror("max connections cannot be smaller 1");
760 if (maximum > GOTD_MAXCLIENTS) {
761 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
765 if (gotd_auth_parseuid(user, &uid) == -1) {
766 yyerror("%s: no such user", user);
770 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
771 gotd->nconnection_limits, uid);
773 limit->max_connections = maximum;
777 limit = gotd->connection_limits;
778 nlimits = gotd->nconnection_limits + 1;
779 limit = reallocarray(limit, nlimits, sizeof(*limit));
781 fatal("reallocarray");
783 limit[nlimits - 1].uid = uid;
784 limit[nlimits - 1].max_connections = maximum;
786 gotd->connection_limits = limit;
787 gotd->nconnection_limits = nlimits;
788 qsort(gotd->connection_limits, gotd->nconnection_limits,
789 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
794 static struct gotd_repo *
795 conf_new_repo(const char *name)
797 struct gotd_repo *repo;
799 if (name[0] == '\0') {
800 fatalx("syntax error: empty repository name found in %s",
804 if (strchr(name, '\n') != NULL)
805 fatalx("repository names must not contain linefeeds: %s", name);
807 repo = calloc(1, sizeof(*repo));
809 fatalx("%s: calloc", __func__);
811 STAILQ_INIT(&repo->rules);
813 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
815 fatalx("%s: strlcpy", __func__);
817 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
824 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
825 int authorization, char *identifier)
827 struct gotd_access_rule *rule;
829 rule = calloc(1, sizeof(*rule));
833 rule->access = access;
834 rule->authorization = authorization;
835 rule->identifier = identifier;
837 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
841 symset(const char *nam, const char *val, int persist)
845 TAILQ_FOREACH(sym, &symhead, entry) {
846 if (strcmp(nam, sym->nam) == 0)
851 if (sym->persist == 1)
856 TAILQ_REMOVE(&symhead, sym, entry);
860 sym = calloc(1, sizeof(*sym));
864 sym->nam = strdup(nam);
865 if (sym->nam == NULL) {
869 sym->val = strdup(val);
870 if (sym->val == NULL) {
876 sym->persist = persist;
877 TAILQ_INSERT_TAIL(&symhead, sym, entry);
882 symget(const char *nam)
886 TAILQ_FOREACH(sym, &symhead, entry) {
887 if (strcmp(nam, sym->nam) == 0) {
896 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
898 struct gotd_repo *repo;
901 TAILQ_FOREACH(repo, &gotd->repos, entry) {
902 namelen = strlen(repo->name);
903 if (strncmp(repo->name, repo_name, namelen) != 0)
905 if (repo_name[namelen] == '\0' ||
906 strcmp(&repo_name[namelen], ".git") == 0)