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(
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)) {
256 | BRANCH NAMESPACE STRING {
257 if (gotd_proc_id == PROC_GOTD ||
258 gotd_proc_id == PROC_REPO_WRITE) {
259 if (conf_protect_branch_namespace(new_repo,
268 if (gotd_proc_id == PROC_GOTD ||
269 gotd_proc_id == PROC_REPO_WRITE) {
270 if (conf_protect_branch(new_repo, $2)) {
278 repository : REPOSITORY STRING {
279 struct gotd_repo *repo;
281 TAILQ_FOREACH(repo, &gotd->repos, entry) {
282 if (strcmp(repo->name, $2) == 0) {
283 yyerror("duplicate repository '%s'", $2);
289 if (gotd_proc_id == PROC_GOTD ||
290 gotd_proc_id == PROC_AUTH ||
291 gotd_proc_id == PROC_REPO_WRITE) {
292 new_repo = conf_new_repo($2);
295 } '{' optnl repoopts2 '}' {
299 repoopts1 : PATH STRING {
300 if (gotd_proc_id == PROC_GOTD ||
301 gotd_proc_id == PROC_AUTH ||
302 gotd_proc_id == PROC_REPO_WRITE) {
303 if (!got_path_is_absolute($2)) {
304 yyerror("%s: path %s is not absolute",
309 if (realpath($2, new_repo->path) == NULL) {
310 yyerror("realpath %s: %s", $2, strerror(errno));
318 if (gotd_proc_id == PROC_AUTH) {
319 conf_new_access_rule(new_repo,
320 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
324 if (gotd_proc_id == PROC_AUTH) {
325 conf_new_access_rule(new_repo,
326 GOTD_ACCESS_PERMITTED,
327 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
331 if (gotd_proc_id == PROC_AUTH) {
332 conf_new_access_rule(new_repo,
333 GOTD_ACCESS_DENIED, 0, $2);
339 repoopts2 : repoopts2 repoopts1 nl
346 optnl : '\n' optnl /* zero or more newlines */
358 yyerror(const char *fmt, ...)
365 if (vasprintf(&msg, fmt, ap) == -1)
366 fatalx("yyerror vasprintf");
368 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
374 kw_cmp(const void *k, const void *e)
376 return (strcmp(k, ((const struct keywords *)e)->k_name));
382 /* This has to be sorted always. */
383 static const struct keywords keywords[] = {
384 { "branch", BRANCH },
385 { "connection", CONNECTION },
388 { "listen", LISTEN },
389 { "namespace", NAMESPACE },
392 { "permit", PERMIT },
393 { "protect", PROTECT },
394 { "repository", REPOSITORY },
395 { "request", REQUEST },
399 { "timeout", TIMEOUT },
402 const struct keywords *p;
404 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
405 sizeof(keywords[0]), kw_cmp);
413 #define MAXPUSHBACK 128
415 unsigned char *parsebuf;
417 unsigned char pushback_buffer[MAXPUSHBACK];
418 int pushback_index = 0;
426 /* Read character from the parsebuffer instead of input. */
427 if (parseindex >= 0) {
428 c = parsebuf[parseindex++];
437 return (pushback_buffer[--pushback_index]);
440 c = getc(file->stream);
442 yyerror("reached end of file while parsing "
447 c = getc(file->stream);
449 next = getc(file->stream);
454 yylval.lineno = file->lineno;
456 c = getc(file->stream);
472 if (pushback_index < MAXPUSHBACK-1)
473 return (pushback_buffer[pushback_index++] = c);
485 /* Skip to either EOF or the first real EOL. */
488 c = pushback_buffer[--pushback_index];
504 unsigned char buf[8096];
505 unsigned char *p, *val;
512 while (c == ' ' || c == '\t')
513 c = lgetc(0); /* nothing */
515 yylval.lineno = file->lineno;
518 while (c != '\n' && c != EOF)
519 c = lgetc(0); /* nothing */
521 if (c == '$' && parsebuf == NULL) {
527 if (p + 1 >= buf + sizeof(buf) - 1) {
528 yyerror("string too long");
531 if (isalnum(c) || c == '_') {
541 yyerror("macro '%s' not defined", buf);
560 } else if (c == '\\') {
561 next = lgetc(quotec);
564 if (next == quotec || c == ' ' || c == '\t')
566 else if (next == '\n') {
571 } else if (c == quotec) {
574 } else if (c == '\0') {
575 yyerror("syntax error");
578 if (p + 1 >= buf + sizeof(buf) - 1) {
579 yyerror("string too long");
584 yylval.v.string = strdup(buf);
585 if (yylval.v.string == NULL)
586 err(1, "yylex: strdup");
590 #define allowed_to_end_number(x) \
591 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
593 if (c == '-' || isdigit(c)) {
596 if ((unsigned)(p-buf) >= sizeof(buf)) {
597 yyerror("string too long");
601 } while (c != EOF && isdigit(c));
603 if (p == buf + 1 && buf[0] == '-')
605 if (c == EOF || allowed_to_end_number(c)) {
606 const char *errstr = NULL;
609 yylval.v.number = strtonum(buf, LLONG_MIN,
612 yyerror("\"%s\" invalid number: %s",
627 #define allowed_in_string(x) \
628 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
629 x != '{' && x != '}' && \
630 x != '!' && x != '=' && x != '#' && \
633 if (isalnum(c) || c == ':' || c == '_') {
636 if ((unsigned)(p-buf) >= sizeof(buf)) {
637 yyerror("string too long");
641 } while (c != EOF && (allowed_in_string(c)));
645 if (token == STRING) {
646 yylval.v.string = strdup(buf);
647 if (yylval.v.string == NULL)
648 err(1, "yylex: strdup");
653 yylval.lineno = file->lineno;
662 check_file_secrecy(int fd, const char *fname)
666 if (fstat(fd, &st)) {
667 log_warn("cannot stat %s", fname);
670 if (st.st_uid != 0 && st.st_uid != getuid()) {
671 log_warnx("%s: owner not root or current user", fname);
674 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
675 log_warnx("%s: group writable or world read/writable", fname);
682 newfile(const char *name, int secret, int required)
686 nfile = calloc(1, sizeof(struct file));
691 nfile->name = strdup(name);
692 if (nfile->name == NULL) {
697 nfile->stream = fopen(nfile->name, "r");
698 if (nfile->stream == NULL) {
700 log_warn("open %s", nfile->name);
705 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
706 fclose(nfile->stream);
716 closefile(struct file *xfile)
718 fclose(xfile->stream);
724 parse_config(const char *filename, enum gotd_procid proc_id,
725 struct gotd *env, int require_config_file)
727 struct sym *sym, *next;
728 struct gotd_repo *repo;
730 memset(env, 0, sizeof(*env));
733 gotd_proc_id = proc_id;
734 TAILQ_INIT(&gotd->repos);
736 /* Apply default values. */
737 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
738 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
739 fprintf(stderr, "%s: unix socket path too long", __func__);
742 if (strlcpy(gotd->user_name, GOTD_USER,
743 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
744 fprintf(stderr, "%s: user name too long", __func__);
748 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
749 gotd->request_timeout.tv_usec = 0;
751 file = newfile(filename, 0, require_config_file);
753 return require_config_file ? -1 : 0;
756 errors = file->errors;
759 /* Free macros and check which have not been used. */
760 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
761 if ((gotd->verbosity > 1) && !sym->used)
762 fprintf(stderr, "warning: macro '%s' not used\n",
767 TAILQ_REMOVE(&symhead, sym, entry);
775 TAILQ_FOREACH(repo, &gotd->repos, entry) {
776 if (repo->path[0] == '\0') {
777 log_warnx("repository \"%s\": no path provided in "
778 "configuration file", repo->name);
783 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
784 log_warnx("no repository defined in configuration file");
792 uid_connection_limit_cmp(const void *pa, const void *pb)
794 const struct gotd_uid_connection_limit *a = pa, *b = pb;
798 else if (a->uid > b->uid);
805 conf_limit_user_connections(const char *user, int maximum)
808 struct gotd_uid_connection_limit *limit;
812 yyerror("max connections cannot be smaller 1");
815 if (maximum > GOTD_MAXCLIENTS) {
816 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
820 if (gotd_auth_parseuid(user, &uid) == -1) {
821 yyerror("%s: no such user", user);
825 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
826 gotd->nconnection_limits, uid);
828 limit->max_connections = maximum;
832 limit = gotd->connection_limits;
833 nlimits = gotd->nconnection_limits + 1;
834 limit = reallocarray(limit, nlimits, sizeof(*limit));
836 fatal("reallocarray");
838 limit[nlimits - 1].uid = uid;
839 limit[nlimits - 1].max_connections = maximum;
841 gotd->connection_limits = limit;
842 gotd->nconnection_limits = nlimits;
843 qsort(gotd->connection_limits, gotd->nconnection_limits,
844 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
849 static struct gotd_repo *
850 conf_new_repo(const char *name)
852 struct gotd_repo *repo;
854 if (name[0] == '\0') {
855 fatalx("syntax error: empty repository name found in %s",
859 if (strchr(name, '\n') != NULL)
860 fatalx("repository names must not contain linefeeds: %s", name);
862 repo = calloc(1, sizeof(*repo));
864 fatalx("%s: calloc", __func__);
866 STAILQ_INIT(&repo->rules);
867 TAILQ_INIT(&repo->protected_tag_namespaces);
868 TAILQ_INIT(&repo->protected_branch_namespaces);
869 TAILQ_INIT(&repo->protected_branches);
871 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
873 fatalx("%s: strlcpy", __func__);
875 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
882 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
883 int authorization, char *identifier)
885 struct gotd_access_rule *rule;
887 rule = calloc(1, sizeof(*rule));
891 rule->access = access;
892 rule->authorization = authorization;
893 rule->identifier = identifier;
895 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
899 refname_is_valid(char *refname)
901 if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
902 yyerror("reference name must begin with \"refs/\": %s",
907 if (!got_ref_name_is_valid(refname)) {
908 yyerror("invalid reference name: %s", refname);
916 conf_protect_ref_namespace(struct got_pathlist_head *refs, char *namespace)
918 const struct got_error *error;
919 struct got_pathlist_entry *new;
922 got_path_strip_trailing_slashes(namespace);
923 if (!refname_is_valid(namespace))
925 if (asprintf(&s, "%s/", namespace) == -1) {
926 yyerror("asprintf: %s", strerror(errno));
930 error = got_pathlist_insert(&new, refs, s, NULL);
931 if (error || new == NULL) {
934 yyerror("got_pathlist_insert: %s", error->msg);
936 yyerror("duplicate protect namespace %s", namespace);
944 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
946 return conf_protect_ref_namespace(&repo->protected_tag_namespaces,
951 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
953 return conf_protect_ref_namespace(&repo->protected_branch_namespaces,
958 conf_protect_branch(struct gotd_repo *repo, char *branchname)
960 const struct got_error *error;
961 struct got_pathlist_entry *new;
964 if (strncmp(branchname, "refs/heads/", 11) != 0) {
965 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
966 yyerror("asprintf: %s", strerror(errno));
970 refname = strdup(branchname);
971 if (refname == NULL) {
972 yyerror("strdup: %s", strerror(errno));
977 if (!refname_is_valid(refname)) {
982 error = got_pathlist_insert(&new, &repo->protected_branches,
984 if (error || new == NULL) {
987 yyerror("got_pathlist_insert: %s", error->msg);
989 yyerror("duplicate protect branch %s", branchname);
997 symset(const char *nam, const char *val, int persist)
1001 TAILQ_FOREACH(sym, &symhead, entry) {
1002 if (strcmp(nam, sym->nam) == 0)
1007 if (sym->persist == 1)
1012 TAILQ_REMOVE(&symhead, sym, entry);
1016 sym = calloc(1, sizeof(*sym));
1020 sym->nam = strdup(nam);
1021 if (sym->nam == NULL) {
1025 sym->val = strdup(val);
1026 if (sym->val == NULL) {
1032 sym->persist = persist;
1033 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1038 symget(const char *nam)
1042 TAILQ_FOREACH(sym, &symhead, entry) {
1043 if (strcmp(nam, sym->nam) == 0) {
1052 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1054 struct gotd_repo *repo;
1057 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1058 namelen = strlen(repo->name);
1059 if (strncmp(repo->name, repo_name, namelen) != 0)
1061 if (repo_name[namelen] == '\0' ||
1062 strcmp(&repo_name[namelen], ".git") == 0)
1070 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1072 struct gotd_repo *repo;
1074 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1075 if (strcmp(repo->path, repo_path) == 0)