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"
45 #include "got_reference.h"
52 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
54 TAILQ_ENTRY(file) entry;
60 struct file *newfile(const char *, int, int);
61 static void closefile(struct file *);
62 int check_file_secrecy(int, const char *);
65 int yyerror(const char *, ...)
66 __attribute__((__format__ (printf, 1, 2)))
67 __attribute__((__nonnull__ (1)));
68 int kw_cmp(const void *, const void *);
74 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 TAILQ_ENTRY(sym) entry;
83 int symset(const char *, const char *, int);
84 char *symget(const char *);
88 static struct gotd *gotd;
89 static struct gotd_repo *new_repo;
90 static int conf_limit_user_connections(const char *, int);
91 static struct gotd_repo *conf_new_repo(const char *);
92 static void conf_new_access_rule(struct gotd_repo *,
93 enum gotd_access, int, char *);
94 static int conf_protect_ref_namespace(char **,
95 struct got_pathlist_head *, char *);
96 static int conf_protect_tag_namespace(struct gotd_repo *,
98 static int conf_protect_branch_namespace(
99 struct gotd_repo *, char *);
100 static int conf_protect_branch(struct gotd_repo *,
102 static enum gotd_procid gotd_proc_id;
115 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
116 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
117 %token PROTECT NAMESPACE BRANCH TAG
119 %token <v.string> STRING
120 %token <v.number> NUMBER
128 | grammar repository '\n'
133 yyerror("invalid timeout: %lld", $1);
141 const char *type = "seconds";
146 yyerror("invalid number of seconds: %s", $1);
152 switch ($1[len - 1]) {
172 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
174 yyerror("number of %s is %s: %s", type,
185 main : LISTEN ON STRING {
186 if (!got_path_is_absolute($3))
187 yyerror("bad unix socket path \"%s\": "
188 "must be an absolute path", $3);
190 if (gotd_proc_id == PROC_LISTEN) {
191 if (strlcpy(gotd->unix_socket_path, $3,
192 sizeof(gotd->unix_socket_path)) >=
193 sizeof(gotd->unix_socket_path)) {
194 yyerror("%s: unix socket path too long",
203 if (strlcpy(gotd->user_name, $2,
204 sizeof(gotd->user_name)) >=
205 sizeof(gotd->user_name)) {
206 yyerror("%s: user name too long", __func__);
215 connection : CONNECTION '{' optnl conflags_l '}'
216 | CONNECTION conflags
218 conflags_l : conflags optnl conflags_l
222 conflags : REQUEST TIMEOUT timeout {
223 if ($3.tv_sec <= 0) {
224 yyerror("invalid timeout: %lld", $3.tv_sec);
227 memcpy(&gotd->request_timeout, &$3,
228 sizeof(gotd->request_timeout));
230 | LIMIT USER STRING NUMBER {
231 if (gotd_proc_id == PROC_LISTEN &&
232 conf_limit_user_connections($3, $4) == -1) {
240 protect : PROTECT '{' optnl protectflags_l '}'
241 | PROTECT protectflags
243 protectflags_l : protectflags optnl protectflags_l
247 protectflags : TAG NAMESPACE STRING {
248 if (gotd_proc_id == PROC_GOTD ||
249 gotd_proc_id == PROC_REPO_WRITE) {
250 if (conf_protect_tag_namespace(new_repo, $3)) {
257 | BRANCH NAMESPACE STRING {
258 if (gotd_proc_id == PROC_GOTD ||
259 gotd_proc_id == PROC_REPO_WRITE) {
260 if (conf_protect_branch_namespace(new_repo,
269 if (gotd_proc_id == PROC_GOTD ||
270 gotd_proc_id == PROC_REPO_WRITE) {
271 if (conf_protect_branch(new_repo, $2)) {
280 repository : REPOSITORY STRING {
281 struct gotd_repo *repo;
283 TAILQ_FOREACH(repo, &gotd->repos, entry) {
284 if (strcmp(repo->name, $2) == 0) {
285 yyerror("duplicate repository '%s'", $2);
291 if (gotd_proc_id == PROC_GOTD ||
292 gotd_proc_id == PROC_AUTH ||
293 gotd_proc_id == PROC_REPO_WRITE) {
294 new_repo = conf_new_repo($2);
297 } '{' optnl repoopts2 '}' {
301 repoopts1 : PATH STRING {
302 if (gotd_proc_id == PROC_GOTD ||
303 gotd_proc_id == PROC_AUTH ||
304 gotd_proc_id == PROC_REPO_WRITE) {
305 if (!got_path_is_absolute($2)) {
306 yyerror("%s: path %s is not absolute",
311 if (realpath($2, new_repo->path) == NULL) {
312 yyerror("realpath %s: %s", $2,
315 * Give admin a chance to create
316 * missing repositories at run-time.
318 if (errno != ENOENT) {
321 } else if (strlcpy(new_repo->path, $2,
322 sizeof(new_repo->path)) >=
323 sizeof(new_repo->path))
324 yyerror("path too long");
330 if (gotd_proc_id == PROC_AUTH) {
331 conf_new_access_rule(new_repo,
332 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
337 if (gotd_proc_id == PROC_AUTH) {
338 conf_new_access_rule(new_repo,
339 GOTD_ACCESS_PERMITTED,
340 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
345 if (gotd_proc_id == PROC_AUTH) {
346 conf_new_access_rule(new_repo,
347 GOTD_ACCESS_DENIED, 0, $2);
354 repoopts2 : repoopts2 repoopts1 nl
361 optnl : '\n' optnl /* zero or more newlines */
373 yyerror(const char *fmt, ...)
380 if (vasprintf(&msg, fmt, ap) == -1)
381 fatalx("yyerror vasprintf");
383 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
389 kw_cmp(const void *k, const void *e)
391 return (strcmp(k, ((const struct keywords *)e)->k_name));
397 /* This has to be sorted always. */
398 static const struct keywords keywords[] = {
399 { "branch", BRANCH },
400 { "connection", CONNECTION },
403 { "listen", LISTEN },
404 { "namespace", NAMESPACE },
407 { "permit", PERMIT },
408 { "protect", PROTECT },
409 { "repository", REPOSITORY },
410 { "request", REQUEST },
414 { "timeout", TIMEOUT },
417 const struct keywords *p;
419 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
420 sizeof(keywords[0]), kw_cmp);
428 #define MAXPUSHBACK 128
430 unsigned char *parsebuf;
432 unsigned char pushback_buffer[MAXPUSHBACK];
433 int pushback_index = 0;
441 /* Read character from the parsebuffer instead of input. */
442 if (parseindex >= 0) {
443 c = parsebuf[parseindex++];
452 return (pushback_buffer[--pushback_index]);
455 c = getc(file->stream);
457 yyerror("reached end of file while parsing "
462 c = getc(file->stream);
464 next = getc(file->stream);
469 yylval.lineno = file->lineno;
471 c = getc(file->stream);
487 if (pushback_index < MAXPUSHBACK-1)
488 return (pushback_buffer[pushback_index++] = c);
500 /* Skip to either EOF or the first real EOL. */
503 c = pushback_buffer[--pushback_index];
519 unsigned char buf[8096];
520 unsigned char *p, *val;
527 while (c == ' ' || c == '\t')
528 c = lgetc(0); /* nothing */
530 yylval.lineno = file->lineno;
533 while (c != '\n' && c != EOF)
534 c = lgetc(0); /* nothing */
536 if (c == '$' && parsebuf == NULL) {
542 if (p + 1 >= buf + sizeof(buf) - 1) {
543 yyerror("string too long");
546 if (isalnum(c) || c == '_') {
556 yyerror("macro '%s' not defined", buf);
575 } else if (c == '\\') {
576 next = lgetc(quotec);
579 if (next == quotec || c == ' ' || c == '\t')
581 else if (next == '\n') {
586 } else if (c == quotec) {
589 } else if (c == '\0') {
590 yyerror("syntax error");
593 if (p + 1 >= buf + sizeof(buf) - 1) {
594 yyerror("string too long");
599 yylval.v.string = strdup(buf);
600 if (yylval.v.string == NULL)
601 err(1, "yylex: strdup");
605 #define allowed_to_end_number(x) \
606 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
608 if (c == '-' || isdigit(c)) {
611 if ((unsigned)(p-buf) >= sizeof(buf)) {
612 yyerror("string too long");
616 } while (c != EOF && isdigit(c));
618 if (p == buf + 1 && buf[0] == '-')
620 if (c == EOF || allowed_to_end_number(c)) {
621 const char *errstr = NULL;
624 yylval.v.number = strtonum(buf, LLONG_MIN,
627 yyerror("\"%s\" invalid number: %s",
642 #define allowed_in_string(x) \
643 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
644 x != '{' && x != '}' && \
645 x != '!' && x != '=' && x != '#' && \
648 if (isalnum(c) || c == ':' || c == '_') {
651 if ((unsigned)(p-buf) >= sizeof(buf)) {
652 yyerror("string too long");
656 } while (c != EOF && (allowed_in_string(c)));
660 if (token == STRING) {
661 yylval.v.string = strdup(buf);
662 if (yylval.v.string == NULL)
663 err(1, "yylex: strdup");
668 yylval.lineno = file->lineno;
677 check_file_secrecy(int fd, const char *fname)
681 if (fstat(fd, &st)) {
682 log_warn("cannot stat %s", fname);
685 if (st.st_uid != 0 && st.st_uid != getuid()) {
686 log_warnx("%s: owner not root or current user", fname);
689 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
690 log_warnx("%s: group writable or world read/writable", fname);
697 newfile(const char *name, int secret, int required)
701 nfile = calloc(1, sizeof(struct file));
706 nfile->name = strdup(name);
707 if (nfile->name == NULL) {
712 nfile->stream = fopen(nfile->name, "r");
713 if (nfile->stream == NULL) {
715 log_warn("open %s", nfile->name);
720 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
721 fclose(nfile->stream);
731 closefile(struct file *xfile)
733 fclose(xfile->stream);
739 parse_config(const char *filename, enum gotd_procid proc_id,
740 struct gotd *env, int require_config_file)
742 struct sym *sym, *next;
743 struct gotd_repo *repo;
745 memset(env, 0, sizeof(*env));
748 gotd_proc_id = proc_id;
749 TAILQ_INIT(&gotd->repos);
751 /* Apply default values. */
752 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
753 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
754 fprintf(stderr, "%s: unix socket path too long", __func__);
757 if (strlcpy(gotd->user_name, GOTD_USER,
758 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
759 fprintf(stderr, "%s: user name too long", __func__);
763 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
764 gotd->request_timeout.tv_usec = 0;
766 file = newfile(filename, 0, require_config_file);
768 return require_config_file ? -1 : 0;
771 errors = file->errors;
774 /* Free macros and check which have not been used. */
775 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
776 if ((gotd->verbosity > 1) && !sym->used)
777 fprintf(stderr, "warning: macro '%s' not used\n",
782 TAILQ_REMOVE(&symhead, sym, entry);
790 TAILQ_FOREACH(repo, &gotd->repos, entry) {
791 if (repo->path[0] == '\0') {
792 log_warnx("repository \"%s\": no path provided in "
793 "configuration file", repo->name);
798 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
799 log_warnx("no repository defined in configuration file");
807 uid_connection_limit_cmp(const void *pa, const void *pb)
809 const struct gotd_uid_connection_limit *a = pa, *b = pb;
813 else if (a->uid > b->uid);
820 conf_limit_user_connections(const char *user, int maximum)
823 struct gotd_uid_connection_limit *limit;
827 yyerror("max connections cannot be smaller 1");
830 if (maximum > GOTD_MAXCLIENTS) {
831 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
835 if (gotd_auth_parseuid(user, &uid) == -1) {
836 yyerror("%s: no such user", user);
840 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
841 gotd->nconnection_limits, uid);
843 limit->max_connections = maximum;
847 limit = gotd->connection_limits;
848 nlimits = gotd->nconnection_limits + 1;
849 limit = reallocarray(limit, nlimits, sizeof(*limit));
851 fatal("reallocarray");
853 limit[nlimits - 1].uid = uid;
854 limit[nlimits - 1].max_connections = maximum;
856 gotd->connection_limits = limit;
857 gotd->nconnection_limits = nlimits;
858 qsort(gotd->connection_limits, gotd->nconnection_limits,
859 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
864 static struct gotd_repo *
865 conf_new_repo(const char *name)
867 struct gotd_repo *repo;
869 if (name[0] == '\0') {
870 fatalx("syntax error: empty repository name found in %s",
874 if (strchr(name, '\n') != NULL)
875 fatalx("repository names must not contain linefeeds: %s", name);
877 repo = calloc(1, sizeof(*repo));
879 fatalx("%s: calloc", __func__);
881 STAILQ_INIT(&repo->rules);
882 TAILQ_INIT(&repo->protected_tag_namespaces);
883 TAILQ_INIT(&repo->protected_branch_namespaces);
884 TAILQ_INIT(&repo->protected_branches);
886 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
888 fatalx("%s: strlcpy", __func__);
890 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
897 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
898 int authorization, char *identifier)
900 struct gotd_access_rule *rule;
902 rule = calloc(1, sizeof(*rule));
906 rule->access = access;
907 rule->authorization = authorization;
908 rule->identifier = identifier;
910 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
914 refname_is_valid(char *refname)
916 if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
917 yyerror("reference name must begin with \"refs/\": %s",
922 if (!got_ref_name_is_valid(refname)) {
923 yyerror("invalid reference name: %s", refname);
931 conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
934 const struct got_error *error;
935 struct got_pathlist_entry *pe;
940 got_path_strip_trailing_slashes(namespace);
941 if (!refname_is_valid(namespace))
943 if (asprintf(&s, "%s/", namespace) == -1) {
944 yyerror("asprintf: %s", strerror(errno));
948 error = got_pathlist_insert(&pe, refs, s, NULL);
949 if (error || pe == NULL) {
952 yyerror("got_pathlist_insert: %s", error->msg);
954 yyerror("duplicate protected namespace %s", namespace);
963 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
965 struct got_pathlist_entry *pe;
968 if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
972 TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
973 if (strcmp(pe->path, new) == 0) {
974 yyerror("duplicate protected namespace %s", namespace);
983 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
985 struct got_pathlist_entry *pe;
988 if (conf_protect_ref_namespace(&new,
989 &repo->protected_branch_namespaces, namespace) == -1)
992 TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
993 if (strcmp(pe->path, new) == 0) {
994 yyerror("duplicate protected namespace %s", namespace);
1003 conf_protect_branch(struct gotd_repo *repo, char *branchname)
1005 const struct got_error *error;
1006 struct got_pathlist_entry *new;
1009 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1010 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1011 yyerror("asprintf: %s", strerror(errno));
1015 refname = strdup(branchname);
1016 if (refname == NULL) {
1017 yyerror("strdup: %s", strerror(errno));
1022 if (!refname_is_valid(refname)) {
1027 error = got_pathlist_insert(&new, &repo->protected_branches,
1029 if (error || new == NULL) {
1032 yyerror("got_pathlist_insert: %s", error->msg);
1034 yyerror("duplicate protect branch %s", branchname);
1042 symset(const char *nam, const char *val, int persist)
1046 TAILQ_FOREACH(sym, &symhead, entry) {
1047 if (strcmp(nam, sym->nam) == 0)
1052 if (sym->persist == 1)
1057 TAILQ_REMOVE(&symhead, sym, entry);
1061 sym = calloc(1, sizeof(*sym));
1065 sym->nam = strdup(nam);
1066 if (sym->nam == NULL) {
1070 sym->val = strdup(val);
1071 if (sym->val == NULL) {
1077 sym->persist = persist;
1078 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1083 symget(const char *nam)
1087 TAILQ_FOREACH(sym, &symhead, entry) {
1088 if (strcmp(nam, sym->nam) == 0) {
1097 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1099 struct gotd_repo *repo;
1102 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1103 namelen = strlen(repo->name);
1104 if (strncmp(repo->name, repo_name, namelen) != 0)
1106 if (repo_name[namelen] == '\0' ||
1107 strcmp(&repo_name[namelen], ".git") == 0)
1115 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1117 struct gotd_repo *repo;
1119 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1120 if (strcmp(repo->path, repo_path) == 0)