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);
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)
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) {
644 /* no warning, we don't require a conf file */
649 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
650 fclose(nfile->stream);
660 closefile(struct file *xfile)
662 fclose(xfile->stream);
668 parse_config(const char *filename, enum gotd_procid proc_id,
671 struct sym *sym, *next;
672 struct gotd_repo *repo;
674 memset(env, 0, sizeof(*env));
677 gotd_proc_id = proc_id;
678 TAILQ_INIT(&gotd->repos);
680 /* Apply default values. */
681 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
682 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
683 fprintf(stderr, "%s: unix socket path too long", __func__);
686 if (strlcpy(gotd->user_name, GOTD_USER,
687 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
688 fprintf(stderr, "%s: user name too long", __func__);
692 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
693 gotd->request_timeout.tv_usec = 0;
695 file = newfile(filename, 0);
697 /* just return, as we don't require a conf file */
702 errors = file->errors;
705 /* Free macros and check which have not been used. */
706 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
707 if ((gotd->verbosity > 1) && !sym->used)
708 fprintf(stderr, "warning: macro '%s' not used\n",
713 TAILQ_REMOVE(&symhead, sym, entry);
721 TAILQ_FOREACH(repo, &gotd->repos, entry) {
722 if (repo->path[0] == '\0') {
723 log_warnx("repository \"%s\": no path provided in "
724 "configuration file", repo->name);
729 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
730 log_warnx("no repository defined in configuration file");
738 uid_connection_limit_cmp(const void *pa, const void *pb)
740 const struct gotd_uid_connection_limit *a = pa, *b = pb;
744 else if (a->uid > b->uid);
751 conf_limit_user_connections(const char *user, int maximum)
754 struct gotd_uid_connection_limit *limit;
758 yyerror("max connections cannot be smaller 1");
761 if (maximum > GOTD_MAXCLIENTS) {
762 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
766 if (gotd_auth_parseuid(user, &uid) == -1) {
767 yyerror("%s: no such user", user);
771 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
772 gotd->nconnection_limits, uid);
774 limit->max_connections = maximum;
778 limit = gotd->connection_limits;
779 nlimits = gotd->nconnection_limits + 1;
780 limit = reallocarray(limit, nlimits, sizeof(*limit));
782 fatal("reallocarray");
784 limit[nlimits - 1].uid = uid;
785 limit[nlimits - 1].max_connections = maximum;
787 gotd->connection_limits = limit;
788 gotd->nconnection_limits = nlimits;
789 qsort(gotd->connection_limits, gotd->nconnection_limits,
790 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
795 static struct gotd_repo *
796 conf_new_repo(const char *name)
798 struct gotd_repo *repo;
800 if (name[0] == '\0') {
801 fatalx("syntax error: empty repository name found in %s",
805 if (strchr(name, '\n') != NULL)
806 fatalx("repository names must not contain linefeeds: %s", name);
808 repo = calloc(1, sizeof(*repo));
810 fatalx("%s: calloc", __func__);
812 STAILQ_INIT(&repo->rules);
814 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
816 fatalx("%s: strlcpy", __func__);
818 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
825 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
826 int authorization, char *identifier)
828 struct gotd_access_rule *rule;
830 rule = calloc(1, sizeof(*rule));
834 rule->access = access;
835 rule->authorization = authorization;
836 rule->identifier = identifier;
838 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
842 symset(const char *nam, const char *val, int persist)
846 TAILQ_FOREACH(sym, &symhead, entry) {
847 if (strcmp(nam, sym->nam) == 0)
852 if (sym->persist == 1)
857 TAILQ_REMOVE(&symhead, sym, entry);
861 sym = calloc(1, sizeof(*sym));
865 sym->nam = strdup(nam);
866 if (sym->nam == NULL) {
870 sym->val = strdup(val);
871 if (sym->val == NULL) {
877 sym->persist = persist;
878 TAILQ_INSERT_TAIL(&symhead, sym, entry);
883 symget(const char *nam)
887 TAILQ_FOREACH(sym, &symhead, entry) {
888 if (strcmp(nam, sym->nam) == 0) {