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>
44 #include "got_error.h"
52 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
54 TAILQ_ENTRY(file) entry;
60 struct file *newfile(const char *, 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 enum gotd_procid gotd_proc_id;
107 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
108 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
110 %token <v.string> STRING
111 %token <v.number> NUMBER
112 %type <v.number> boolean
120 | grammar repository '\n'
124 if (strcasecmp($1, "1") == 0 ||
125 strcasecmp($1, "yes") == 0 ||
126 strcasecmp($1, "on") == 0)
128 else if (strcasecmp($1, "0") == 0 ||
129 strcasecmp($1, "off") == 0 ||
130 strcasecmp($1, "no") == 0)
133 yyerror("invalid boolean value '%s'", $1);
140 | NUMBER { $$ = $1; }
145 yyerror("invalid timeout: %lld", $1);
153 const char *type = "seconds";
158 yyerror("invalid number of seconds: %s", $1);
164 switch ($1[len - 1]) {
184 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
186 yyerror("number of %s is %s: %s", type,
197 main : LISTEN ON STRING {
198 if (gotd_proc_id == PROC_LISTEN) {
199 if (strlcpy(gotd->unix_socket_path, $3,
200 sizeof(gotd->unix_socket_path)) >=
201 sizeof(gotd->unix_socket_path)) {
202 yyerror("%s: unix socket path too long",
211 if (strlcpy(gotd->user_name, $2,
212 sizeof(gotd->user_name)) >=
213 sizeof(gotd->user_name)) {
214 yyerror("%s: user name too long", __func__);
223 connection : CONNECTION '{' optnl conflags_l '}'
224 | CONNECTION conflags
226 conflags_l : conflags optnl conflags_l
230 conflags : REQUEST TIMEOUT timeout {
231 if ($3.tv_sec <= 0) {
232 yyerror("invalid timeout: %lld", $3.tv_sec);
235 memcpy(&gotd->request_timeout, &$3,
236 sizeof(gotd->request_timeout));
238 | LIMIT USER STRING NUMBER {
239 if (gotd_proc_id == PROC_LISTEN &&
240 conf_limit_user_connections($3, $4) == -1) {
248 repository : REPOSITORY STRING {
249 struct gotd_repo *repo;
251 TAILQ_FOREACH(repo, &gotd->repos, entry) {
252 if (strcmp(repo->name, $2) == 0) {
253 yyerror("duplicate repository '%s'", $2);
259 if (gotd_proc_id == PROC_GOTD ||
260 gotd_proc_id == PROC_AUTH) {
261 new_repo = conf_new_repo($2);
264 } '{' optnl repoopts2 '}' {
268 repoopts1 : PATH STRING {
269 if (gotd_proc_id == PROC_GOTD ||
270 gotd_proc_id == PROC_AUTH) {
271 if (!got_path_is_absolute($2)) {
272 yyerror("%s: path %s is not absolute",
277 if (realpath($2, new_repo->path) == NULL) {
278 yyerror("realpath %s: %s", $2, strerror(errno));
286 if (gotd_proc_id == PROC_AUTH) {
287 conf_new_access_rule(new_repo,
288 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
292 if (gotd_proc_id == PROC_AUTH) {
293 conf_new_access_rule(new_repo,
294 GOTD_ACCESS_PERMITTED,
295 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
299 if (gotd_proc_id == PROC_AUTH) {
300 conf_new_access_rule(new_repo,
301 GOTD_ACCESS_DENIED, 0, $2);
306 repoopts2 : repoopts2 repoopts1 nl
313 optnl : '\n' optnl /* zero or more newlines */
325 yyerror(const char *fmt, ...)
332 if (vasprintf(&msg, fmt, ap) == -1)
333 fatalx("yyerror vasprintf");
335 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
341 kw_cmp(const void *k, const void *e)
343 return (strcmp(k, ((const struct keywords *)e)->k_name));
349 /* This has to be sorted always. */
350 static const struct keywords keywords[] = {
351 { "connection", CONNECTION },
354 { "listen", LISTEN },
357 { "permit", PERMIT },
358 { "repository", REPOSITORY },
359 { "request", REQUEST },
362 { "timeout", TIMEOUT },
365 const struct keywords *p;
367 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
368 sizeof(keywords[0]), kw_cmp);
376 #define MAXPUSHBACK 128
378 unsigned char *parsebuf;
380 unsigned char pushback_buffer[MAXPUSHBACK];
381 int pushback_index = 0;
389 /* Read character from the parsebuffer instead of input. */
390 if (parseindex >= 0) {
391 c = parsebuf[parseindex++];
400 return (pushback_buffer[--pushback_index]);
403 c = getc(file->stream);
405 yyerror("reached end of file while parsing "
410 c = getc(file->stream);
412 next = getc(file->stream);
417 yylval.lineno = file->lineno;
419 c = getc(file->stream);
435 if (pushback_index < MAXPUSHBACK-1)
436 return (pushback_buffer[pushback_index++] = c);
448 /* Skip to either EOF or the first real EOL. */
451 c = pushback_buffer[--pushback_index];
467 unsigned char buf[8096];
468 unsigned char *p, *val;
475 while (c == ' ' || c == '\t')
476 c = lgetc(0); /* nothing */
478 yylval.lineno = file->lineno;
481 while (c != '\n' && c != EOF)
482 c = lgetc(0); /* nothing */
484 if (c == '$' && parsebuf == NULL) {
490 if (p + 1 >= buf + sizeof(buf) - 1) {
491 yyerror("string too long");
494 if (isalnum(c) || c == '_') {
504 yyerror("macro '%s' not defined", buf);
523 } else if (c == '\\') {
524 next = lgetc(quotec);
527 if (next == quotec || c == ' ' || c == '\t')
529 else if (next == '\n') {
534 } else if (c == quotec) {
537 } else if (c == '\0') {
538 yyerror("syntax error");
541 if (p + 1 >= buf + sizeof(buf) - 1) {
542 yyerror("string too long");
547 yylval.v.string = strdup(buf);
548 if (yylval.v.string == NULL)
549 err(1, "yylex: strdup");
553 #define allowed_to_end_number(x) \
554 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
556 if (c == '-' || isdigit(c)) {
559 if ((unsigned)(p-buf) >= sizeof(buf)) {
560 yyerror("string too long");
564 } while (c != EOF && isdigit(c));
566 if (p == buf + 1 && buf[0] == '-')
568 if (c == EOF || allowed_to_end_number(c)) {
569 const char *errstr = NULL;
572 yylval.v.number = strtonum(buf, LLONG_MIN,
575 yyerror("\"%s\" invalid number: %s",
590 #define allowed_in_string(x) \
591 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
592 x != '{' && x != '}' && \
593 x != '!' && x != '=' && x != '#' && \
596 if (isalnum(c) || c == ':' || c == '_') {
599 if ((unsigned)(p-buf) >= sizeof(buf)) {
600 yyerror("string too long");
604 } while (c != EOF && (allowed_in_string(c)));
608 if (token == STRING) {
609 yylval.v.string = strdup(buf);
610 if (yylval.v.string == NULL)
611 err(1, "yylex: strdup");
616 yylval.lineno = file->lineno;
625 check_file_secrecy(int fd, const char *fname)
629 if (fstat(fd, &st)) {
630 log_warn("cannot stat %s", fname);
633 if (st.st_uid != 0 && st.st_uid != getuid()) {
634 log_warnx("%s: owner not root or current user", fname);
637 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
638 log_warnx("%s: group writable or world read/writable", fname);
645 newfile(const char *name, int secret)
649 nfile = calloc(1, sizeof(struct file));
654 nfile->name = strdup(name);
655 if (nfile->name == NULL) {
660 nfile->stream = fopen(nfile->name, "r");
661 if (nfile->stream == NULL) {
662 /* no warning, we don't require a conf file */
667 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
668 fclose(nfile->stream);
678 closefile(struct file *xfile)
680 fclose(xfile->stream);
686 parse_config(const char *filename, enum gotd_procid proc_id,
689 struct sym *sym, *next;
690 struct gotd_repo *repo;
692 memset(env, 0, sizeof(*env));
695 gotd_proc_id = proc_id;
696 TAILQ_INIT(&gotd->repos);
698 /* Apply default values. */
699 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
700 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
701 fprintf(stderr, "%s: unix socket path too long", __func__);
704 if (strlcpy(gotd->user_name, GOTD_USER,
705 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
706 fprintf(stderr, "%s: user name too long", __func__);
710 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
711 gotd->request_timeout.tv_usec = 0;
713 file = newfile(filename, 0);
715 /* just return, as we don't require a conf file */
720 errors = file->errors;
723 /* Free macros and check which have not been used. */
724 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
725 if ((gotd->verbosity > 1) && !sym->used)
726 fprintf(stderr, "warning: macro '%s' not used\n",
731 TAILQ_REMOVE(&symhead, sym, entry);
739 TAILQ_FOREACH(repo, &gotd->repos, entry) {
740 if (repo->path[0] == '\0') {
741 log_warnx("repository \"%s\": no path provided in "
742 "configuration file", repo->name);
751 uid_connection_limit_cmp(const void *pa, const void *pb)
753 const struct gotd_uid_connection_limit *a = pa, *b = pb;
757 else if (a->uid > b->uid);
764 conf_limit_user_connections(const char *user, int maximum)
767 struct gotd_uid_connection_limit *limit;
771 yyerror("max connections cannot be smaller 1");
774 if (maximum > GOTD_MAXCLIENTS) {
775 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
779 if (gotd_auth_parseuid(user, &uid) == -1) {
780 yyerror("%s: no such user", user);
784 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
785 gotd->nconnection_limits, uid);
787 limit->max_connections = maximum;
791 limit = gotd->connection_limits;
792 nlimits = gotd->nconnection_limits + 1;
793 limit = reallocarray(limit, nlimits, sizeof(*limit));
795 fatal("reallocarray");
797 limit[nlimits - 1].uid = uid;
798 limit[nlimits - 1].max_connections = maximum;
800 gotd->connection_limits = limit;
801 gotd->nconnection_limits = nlimits;
802 qsort(gotd->connection_limits, gotd->nconnection_limits,
803 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
808 static struct gotd_repo *
809 conf_new_repo(const char *name)
811 struct gotd_repo *repo;
813 if (name[0] == '\0') {
814 fatalx("syntax error: empty repository name found in %s",
818 if (strchr(name, '\n') != NULL)
819 fatalx("repository names must not contain linefeeds: %s", name);
821 repo = calloc(1, sizeof(*repo));
823 fatalx("%s: calloc", __func__);
825 STAILQ_INIT(&repo->rules);
827 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
829 fatalx("%s: strlcpy", __func__);
831 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
838 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
839 int authorization, char *identifier)
841 struct gotd_access_rule *rule;
843 rule = calloc(1, sizeof(*rule));
847 rule->access = access;
848 rule->authorization = authorization;
849 rule->identifier = identifier;
851 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
855 symset(const char *nam, const char *val, int persist)
859 TAILQ_FOREACH(sym, &symhead, entry) {
860 if (strcmp(nam, sym->nam) == 0)
865 if (sym->persist == 1)
870 TAILQ_REMOVE(&symhead, sym, entry);
874 sym = calloc(1, sizeof(*sym));
878 sym->nam = strdup(nam);
879 if (sym->nam == NULL) {
883 sym->val = strdup(val);
884 if (sym->val == NULL) {
890 sym->persist = persist;
891 TAILQ_INSERT_TAIL(&symhead, sym, entry);
896 symget(const char *nam)
900 TAILQ_FOREACH(sym, &symhead, entry) {
901 if (strcmp(nam, sym->nam) == 0) {