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 ON UNIX_SOCKET UNIX_GROUP 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 : UNIX_SOCKET STRING {
198 if (gotd_proc_id == PROC_LISTEN) {
199 if (strlcpy(gotd->unix_socket_path, $2,
200 sizeof(gotd->unix_socket_path)) >=
201 sizeof(gotd->unix_socket_path)) {
202 yyerror("%s: unix socket path too long",
210 | UNIX_GROUP STRING {
211 if (strlcpy(gotd->unix_group_name, $2,
212 sizeof(gotd->unix_group_name)) >=
213 sizeof(gotd->unix_group_name)) {
214 yyerror("%s: unix group name too long",
222 if (strlcpy(gotd->user_name, $2,
223 sizeof(gotd->user_name)) >=
224 sizeof(gotd->user_name)) {
225 yyerror("%s: user name too long", __func__);
234 connection : CONNECTION '{' optnl conflags_l '}'
235 | CONNECTION conflags
237 conflags_l : conflags optnl conflags_l
241 conflags : REQUEST TIMEOUT timeout {
242 if ($3.tv_sec <= 0) {
243 yyerror("invalid timeout: %lld", $3.tv_sec);
246 memcpy(&gotd->request_timeout, &$3,
247 sizeof(gotd->request_timeout));
249 | LIMIT USER STRING NUMBER {
250 if (gotd_proc_id == PROC_LISTEN &&
251 conf_limit_user_connections($3, $4) == -1) {
259 repository : REPOSITORY STRING {
260 struct gotd_repo *repo;
262 TAILQ_FOREACH(repo, &gotd->repos, entry) {
263 if (strcmp(repo->name, $2) == 0) {
264 yyerror("duplicate repository '%s'", $2);
270 if (gotd_proc_id == PROC_GOTD ||
271 gotd_proc_id == PROC_AUTH) {
272 new_repo = conf_new_repo($2);
275 } '{' optnl repoopts2 '}' {
279 repoopts1 : PATH STRING {
280 if (gotd_proc_id == PROC_GOTD ||
281 gotd_proc_id == PROC_AUTH) {
282 if (!got_path_is_absolute($2)) {
283 yyerror("%s: path %s is not absolute",
288 if (strlcpy(new_repo->path, $2,
289 sizeof(new_repo->path)) >=
290 sizeof(new_repo->path)) {
291 yyerror("%s: path truncated", __func__);
299 if (gotd_proc_id == PROC_AUTH) {
300 conf_new_access_rule(new_repo,
301 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
305 if (gotd_proc_id == PROC_AUTH) {
306 conf_new_access_rule(new_repo,
307 GOTD_ACCESS_PERMITTED,
308 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
312 if (gotd_proc_id == PROC_AUTH) {
313 conf_new_access_rule(new_repo,
314 GOTD_ACCESS_DENIED, 0, $2);
319 repoopts2 : repoopts2 repoopts1 nl
326 optnl : '\n' optnl /* zero or more newlines */
338 yyerror(const char *fmt, ...)
345 if (vasprintf(&msg, fmt, ap) == -1)
346 fatalx("yyerror vasprintf");
348 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
354 kw_cmp(const void *k, const void *e)
356 return (strcmp(k, ((const struct keywords *)e)->k_name));
362 /* This has to be sorted always. */
363 static const struct keywords keywords[] = {
364 { "connection", CONNECTION },
369 { "permit", PERMIT },
370 { "repository", REPOSITORY },
371 { "request", REQUEST },
374 { "timeout", TIMEOUT },
375 { "unix_group", UNIX_GROUP },
376 { "unix_socket", UNIX_SOCKET },
379 const struct keywords *p;
381 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
382 sizeof(keywords[0]), kw_cmp);
390 #define MAXPUSHBACK 128
392 unsigned char *parsebuf;
394 unsigned char pushback_buffer[MAXPUSHBACK];
395 int pushback_index = 0;
403 /* Read character from the parsebuffer instead of input. */
404 if (parseindex >= 0) {
405 c = parsebuf[parseindex++];
414 return (pushback_buffer[--pushback_index]);
417 c = getc(file->stream);
419 yyerror("reached end of file while parsing "
424 c = getc(file->stream);
426 next = getc(file->stream);
431 yylval.lineno = file->lineno;
433 c = getc(file->stream);
449 if (pushback_index < MAXPUSHBACK-1)
450 return (pushback_buffer[pushback_index++] = c);
462 /* Skip to either EOF or the first real EOL. */
465 c = pushback_buffer[--pushback_index];
481 unsigned char buf[8096];
482 unsigned char *p, *val;
489 while (c == ' ' || c == '\t')
490 c = lgetc(0); /* nothing */
492 yylval.lineno = file->lineno;
495 while (c != '\n' && c != EOF)
496 c = lgetc(0); /* nothing */
498 if (c == '$' && parsebuf == NULL) {
504 if (p + 1 >= buf + sizeof(buf) - 1) {
505 yyerror("string too long");
508 if (isalnum(c) || c == '_') {
518 yyerror("macro '%s' not defined", buf);
537 } else if (c == '\\') {
538 next = lgetc(quotec);
541 if (next == quotec || c == ' ' || c == '\t')
543 else if (next == '\n') {
548 } else if (c == quotec) {
551 } else if (c == '\0') {
552 yyerror("syntax error");
555 if (p + 1 >= buf + sizeof(buf) - 1) {
556 yyerror("string too long");
561 yylval.v.string = strdup(buf);
562 if (yylval.v.string == NULL)
563 err(1, "yylex: strdup");
567 #define allowed_to_end_number(x) \
568 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
570 if (c == '-' || isdigit(c)) {
573 if ((unsigned)(p-buf) >= sizeof(buf)) {
574 yyerror("string too long");
578 } while (c != EOF && isdigit(c));
580 if (p == buf + 1 && buf[0] == '-')
582 if (c == EOF || allowed_to_end_number(c)) {
583 const char *errstr = NULL;
586 yylval.v.number = strtonum(buf, LLONG_MIN,
589 yyerror("\"%s\" invalid number: %s",
604 #define allowed_in_string(x) \
605 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
606 x != '{' && x != '}' && \
607 x != '!' && x != '=' && x != '#' && \
610 if (isalnum(c) || c == ':' || c == '_') {
613 if ((unsigned)(p-buf) >= sizeof(buf)) {
614 yyerror("string too long");
618 } while (c != EOF && (allowed_in_string(c)));
622 if (token == STRING) {
623 yylval.v.string = strdup(buf);
624 if (yylval.v.string == NULL)
625 err(1, "yylex: strdup");
630 yylval.lineno = file->lineno;
639 check_file_secrecy(int fd, const char *fname)
643 if (fstat(fd, &st)) {
644 log_warn("cannot stat %s", fname);
647 if (st.st_uid != 0 && st.st_uid != getuid()) {
648 log_warnx("%s: owner not root or current user", fname);
651 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
652 log_warnx("%s: group writable or world read/writable", fname);
659 newfile(const char *name, int secret)
663 nfile = calloc(1, sizeof(struct file));
668 nfile->name = strdup(name);
669 if (nfile->name == NULL) {
674 nfile->stream = fopen(nfile->name, "r");
675 if (nfile->stream == NULL) {
676 /* no warning, we don't require a conf file */
681 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
682 fclose(nfile->stream);
692 closefile(struct file *xfile)
694 fclose(xfile->stream);
700 parse_config(const char *filename, enum gotd_procid proc_id,
703 struct sym *sym, *next;
704 struct gotd_repo *repo;
706 memset(env, 0, sizeof(*env));
709 gotd_proc_id = proc_id;
710 TAILQ_INIT(&gotd->repos);
712 /* Apply default values. */
713 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
714 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
715 fprintf(stderr, "%s: unix socket path too long", __func__);
718 if (strlcpy(gotd->unix_group_name, GOTD_UNIX_GROUP,
719 sizeof(gotd->unix_group_name)) >= sizeof(gotd->unix_group_name)) {
720 fprintf(stderr, "%s: unix group name too long", __func__);
723 if (strlcpy(gotd->user_name, GOTD_USER,
724 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
725 fprintf(stderr, "%s: user name too long", __func__);
729 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
730 gotd->request_timeout.tv_usec = 0;
732 file = newfile(filename, 0);
734 /* just return, as we don't require a conf file */
739 errors = file->errors;
742 /* Free macros and check which have not been used. */
743 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
744 if ((gotd->verbosity > 1) && !sym->used)
745 fprintf(stderr, "warning: macro '%s' not used\n",
750 TAILQ_REMOVE(&symhead, sym, entry);
758 TAILQ_FOREACH(repo, &gotd->repos, entry) {
759 if (repo->path[0] == '\0') {
760 log_warnx("repository \"%s\": no path provided in "
761 "configuration file", repo->name);
770 uid_connection_limit_cmp(const void *pa, const void *pb)
772 const struct gotd_uid_connection_limit *a = pa, *b = pb;
776 else if (a->uid > b->uid);
783 conf_limit_user_connections(const char *user, int maximum)
786 struct gotd_uid_connection_limit *limit;
790 yyerror("max connections cannot be smaller 1");
793 if (maximum > GOTD_MAXCLIENTS) {
794 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
798 if (gotd_auth_parseuid(user, &uid) == -1) {
799 yyerror("%s: no such user", user);
803 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
804 gotd->nconnection_limits, uid);
806 limit->max_connections = maximum;
810 limit = gotd->connection_limits;
811 nlimits = gotd->nconnection_limits + 1;
812 limit = reallocarray(limit, nlimits, sizeof(*limit));
814 fatal("reallocarray");
816 limit[nlimits - 1].uid = uid;
817 limit[nlimits - 1].max_connections = maximum;
819 gotd->connection_limits = limit;
820 gotd->nconnection_limits = nlimits;
821 qsort(gotd->connection_limits, gotd->nconnection_limits,
822 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
827 static struct gotd_repo *
828 conf_new_repo(const char *name)
830 struct gotd_repo *repo;
832 if (name[0] == '\0') {
833 fatalx("syntax error: empty repository name found in %s",
837 if (strchr(name, '\n') != NULL)
838 fatalx("repository names must not contain linefeeds: %s", name);
840 repo = calloc(1, sizeof(*repo));
842 fatalx("%s: calloc", __func__);
844 STAILQ_INIT(&repo->rules);
846 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
848 fatalx("%s: strlcpy", __func__);
850 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
857 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
858 int authorization, char *identifier)
860 struct gotd_access_rule *rule;
862 rule = calloc(1, sizeof(*rule));
866 rule->access = access;
867 rule->authorization = authorization;
868 rule->identifier = identifier;
870 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
874 symset(const char *nam, const char *val, int persist)
878 TAILQ_FOREACH(sym, &symhead, entry) {
879 if (strcmp(nam, sym->nam) == 0)
884 if (sym->persist == 1)
889 TAILQ_REMOVE(&symhead, sym, entry);
893 sym = calloc(1, sizeof(*sym));
897 sym->nam = strdup(nam);
898 if (sym->nam == NULL) {
902 sym->val = strdup(val);
903 if (sym->val == NULL) {
909 sym->persist = persist;
910 TAILQ_INSERT_TAIL(&symhead, sym, entry);
915 symget(const char *nam)
919 TAILQ_FOREACH(sym, &symhead, entry) {
920 if (strcmp(nam, sym->nam) == 0) {