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>
45 #include "got_error.h"
53 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
55 TAILQ_ENTRY(file) entry;
61 struct file *newfile(const char *, int);
62 static void closefile(struct file *);
63 int check_file_secrecy(int, const char *);
66 int yyerror(const char *, ...)
67 __attribute__((__format__ (printf, 1, 2)))
68 __attribute__((__nonnull__ (1)));
69 int kw_cmp(const void *, const void *);
75 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
77 TAILQ_ENTRY(sym) entry;
84 int symset(const char *, const char *, int);
85 char *symget(const char *);
89 static struct gotd *gotd;
90 static struct gotd_repo *new_repo;
91 static int conf_limit_user_connections(const char *, int);
92 static struct gotd_repo *conf_new_repo(const char *);
93 static void conf_new_access_rule(struct gotd_repo *,
94 enum gotd_access, int, char *);
95 static enum gotd_procid gotd_proc_id;
108 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
109 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
111 %token <v.string> STRING
112 %token <v.number> NUMBER
120 | grammar repository '\n'
125 yyerror("invalid timeout: %lld", $1);
133 const char *type = "seconds";
138 yyerror("invalid number of seconds: %s", $1);
144 switch ($1[len - 1]) {
164 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
166 yyerror("number of %s is %s: %s", type,
177 main : LISTEN ON STRING {
178 if (!got_path_is_absolute($3))
179 yyerror("bad unix socket path \"%s\": "
180 "must be an absolute path", $3);
182 if (gotd_proc_id == PROC_LISTEN) {
183 if (strlcpy(gotd->unix_socket_path, $3,
184 sizeof(gotd->unix_socket_path)) >=
185 sizeof(gotd->unix_socket_path)) {
186 yyerror("%s: unix socket path too long",
195 if (strlcpy(gotd->user_name, $2,
196 sizeof(gotd->user_name)) >=
197 sizeof(gotd->user_name)) {
198 yyerror("%s: user name too long", __func__);
207 connection : CONNECTION '{' optnl conflags_l '}'
208 | CONNECTION conflags
210 conflags_l : conflags optnl conflags_l
214 conflags : REQUEST TIMEOUT timeout {
215 if ($3.tv_sec <= 0) {
216 yyerror("invalid timeout: %lld", $3.tv_sec);
219 memcpy(&gotd->request_timeout, &$3,
220 sizeof(gotd->request_timeout));
222 | LIMIT USER STRING NUMBER {
223 if (gotd_proc_id == PROC_LISTEN &&
224 conf_limit_user_connections($3, $4) == -1) {
232 repository : REPOSITORY STRING {
233 struct gotd_repo *repo;
235 TAILQ_FOREACH(repo, &gotd->repos, entry) {
236 if (strcmp(repo->name, $2) == 0) {
237 yyerror("duplicate repository '%s'", $2);
243 if (gotd_proc_id == PROC_GOTD ||
244 gotd_proc_id == PROC_AUTH) {
245 new_repo = conf_new_repo($2);
248 } '{' optnl repoopts2 '}' {
252 repoopts1 : PATH STRING {
253 if (gotd_proc_id == PROC_GOTD ||
254 gotd_proc_id == PROC_AUTH) {
255 if (!got_path_is_absolute($2)) {
256 yyerror("%s: path %s is not absolute",
261 if (realpath($2, new_repo->path) == NULL) {
262 yyerror("realpath %s: %s", $2, strerror(errno));
270 if (gotd_proc_id == PROC_AUTH) {
271 conf_new_access_rule(new_repo,
272 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
276 if (gotd_proc_id == PROC_AUTH) {
277 conf_new_access_rule(new_repo,
278 GOTD_ACCESS_PERMITTED,
279 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
283 if (gotd_proc_id == PROC_AUTH) {
284 conf_new_access_rule(new_repo,
285 GOTD_ACCESS_DENIED, 0, $2);
290 repoopts2 : repoopts2 repoopts1 nl
297 optnl : '\n' optnl /* zero or more newlines */
309 yyerror(const char *fmt, ...)
316 if (vasprintf(&msg, fmt, ap) == -1)
317 fatalx("yyerror vasprintf");
319 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
325 kw_cmp(const void *k, const void *e)
327 return (strcmp(k, ((const struct keywords *)e)->k_name));
333 /* This has to be sorted always. */
334 static const struct keywords keywords[] = {
335 { "connection", CONNECTION },
338 { "listen", LISTEN },
341 { "permit", PERMIT },
342 { "repository", REPOSITORY },
343 { "request", REQUEST },
346 { "timeout", TIMEOUT },
349 const struct keywords *p;
351 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
352 sizeof(keywords[0]), kw_cmp);
360 #define MAXPUSHBACK 128
362 unsigned char *parsebuf;
364 unsigned char pushback_buffer[MAXPUSHBACK];
365 int pushback_index = 0;
373 /* Read character from the parsebuffer instead of input. */
374 if (parseindex >= 0) {
375 c = parsebuf[parseindex++];
384 return (pushback_buffer[--pushback_index]);
387 c = getc(file->stream);
389 yyerror("reached end of file while parsing "
394 c = getc(file->stream);
396 next = getc(file->stream);
401 yylval.lineno = file->lineno;
403 c = getc(file->stream);
419 if (pushback_index < MAXPUSHBACK-1)
420 return (pushback_buffer[pushback_index++] = c);
432 /* Skip to either EOF or the first real EOL. */
435 c = pushback_buffer[--pushback_index];
451 unsigned char buf[8096];
452 unsigned char *p, *val;
459 while (c == ' ' || c == '\t')
460 c = lgetc(0); /* nothing */
462 yylval.lineno = file->lineno;
465 while (c != '\n' && c != EOF)
466 c = lgetc(0); /* nothing */
468 if (c == '$' && parsebuf == NULL) {
474 if (p + 1 >= buf + sizeof(buf) - 1) {
475 yyerror("string too long");
478 if (isalnum(c) || c == '_') {
488 yyerror("macro '%s' not defined", buf);
507 } else if (c == '\\') {
508 next = lgetc(quotec);
511 if (next == quotec || c == ' ' || c == '\t')
513 else if (next == '\n') {
518 } else if (c == quotec) {
521 } else if (c == '\0') {
522 yyerror("syntax error");
525 if (p + 1 >= buf + sizeof(buf) - 1) {
526 yyerror("string too long");
531 yylval.v.string = strdup(buf);
532 if (yylval.v.string == NULL)
533 err(1, "yylex: strdup");
537 #define allowed_to_end_number(x) \
538 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
540 if (c == '-' || isdigit(c)) {
543 if ((unsigned)(p-buf) >= sizeof(buf)) {
544 yyerror("string too long");
548 } while (c != EOF && isdigit(c));
550 if (p == buf + 1 && buf[0] == '-')
552 if (c == EOF || allowed_to_end_number(c)) {
553 const char *errstr = NULL;
556 yylval.v.number = strtonum(buf, LLONG_MIN,
559 yyerror("\"%s\" invalid number: %s",
574 #define allowed_in_string(x) \
575 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
576 x != '{' && x != '}' && \
577 x != '!' && x != '=' && x != '#' && \
580 if (isalnum(c) || c == ':' || c == '_') {
583 if ((unsigned)(p-buf) >= sizeof(buf)) {
584 yyerror("string too long");
588 } while (c != EOF && (allowed_in_string(c)));
592 if (token == STRING) {
593 yylval.v.string = strdup(buf);
594 if (yylval.v.string == NULL)
595 err(1, "yylex: strdup");
600 yylval.lineno = file->lineno;
609 check_file_secrecy(int fd, const char *fname)
613 if (fstat(fd, &st)) {
614 log_warn("cannot stat %s", fname);
617 if (st.st_uid != 0 && st.st_uid != getuid()) {
618 log_warnx("%s: owner not root or current user", fname);
621 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
622 log_warnx("%s: group writable or world read/writable", fname);
629 newfile(const char *name, int secret)
633 nfile = calloc(1, sizeof(struct file));
638 nfile->name = strdup(name);
639 if (nfile->name == NULL) {
644 nfile->stream = fopen(nfile->name, "r");
645 if (nfile->stream == NULL) {
646 /* no warning, we don't require a conf file */
651 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
652 fclose(nfile->stream);
662 closefile(struct file *xfile)
664 fclose(xfile->stream);
670 parse_config(const char *filename, enum gotd_procid proc_id,
673 struct sym *sym, *next;
674 struct gotd_repo *repo;
676 memset(env, 0, sizeof(*env));
679 gotd_proc_id = proc_id;
680 TAILQ_INIT(&gotd->repos);
682 /* Apply default values. */
683 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
684 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
685 fprintf(stderr, "%s: unix socket path too long", __func__);
688 if (strlcpy(gotd->user_name, GOTD_USER,
689 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
690 fprintf(stderr, "%s: user name too long", __func__);
694 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
695 gotd->request_timeout.tv_usec = 0;
697 file = newfile(filename, 0);
699 /* just return, as we don't require a conf file */
704 errors = file->errors;
707 /* Free macros and check which have not been used. */
708 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
709 if ((gotd->verbosity > 1) && !sym->used)
710 fprintf(stderr, "warning: macro '%s' not used\n",
715 TAILQ_REMOVE(&symhead, sym, entry);
723 TAILQ_FOREACH(repo, &gotd->repos, entry) {
724 if (repo->path[0] == '\0') {
725 log_warnx("repository \"%s\": no path provided in "
726 "configuration file", repo->name);
731 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
732 log_warnx("no repository defined in configuration file");
740 uid_connection_limit_cmp(const void *pa, const void *pb)
742 const struct gotd_uid_connection_limit *a = pa, *b = pb;
746 else if (a->uid > b->uid);
753 conf_limit_user_connections(const char *user, int maximum)
756 struct gotd_uid_connection_limit *limit;
760 yyerror("max connections cannot be smaller 1");
763 if (maximum > GOTD_MAXCLIENTS) {
764 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
768 if (gotd_auth_parseuid(user, &uid) == -1) {
769 yyerror("%s: no such user", user);
773 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
774 gotd->nconnection_limits, uid);
776 limit->max_connections = maximum;
780 limit = gotd->connection_limits;
781 nlimits = gotd->nconnection_limits + 1;
782 limit = reallocarray(limit, nlimits, sizeof(*limit));
784 fatal("reallocarray");
786 limit[nlimits - 1].uid = uid;
787 limit[nlimits - 1].max_connections = maximum;
789 gotd->connection_limits = limit;
790 gotd->nconnection_limits = nlimits;
791 qsort(gotd->connection_limits, gotd->nconnection_limits,
792 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
797 static struct gotd_repo *
798 conf_new_repo(const char *name)
800 struct gotd_repo *repo;
802 if (name[0] == '\0') {
803 fatalx("syntax error: empty repository name found in %s",
807 if (strchr(name, '\n') != NULL)
808 fatalx("repository names must not contain linefeeds: %s", name);
810 repo = calloc(1, sizeof(*repo));
812 fatalx("%s: calloc", __func__);
814 STAILQ_INIT(&repo->rules);
816 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
818 fatalx("%s: strlcpy", __func__);
820 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
827 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
828 int authorization, char *identifier)
830 struct gotd_access_rule *rule;
832 rule = calloc(1, sizeof(*rule));
836 rule->access = access;
837 rule->authorization = authorization;
838 rule->identifier = identifier;
840 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
844 symset(const char *nam, const char *val, int persist)
848 TAILQ_FOREACH(sym, &symhead, entry) {
849 if (strcmp(nam, sym->nam) == 0)
854 if (sym->persist == 1)
859 TAILQ_REMOVE(&symhead, sym, entry);
863 sym = calloc(1, sizeof(*sym));
867 sym->nam = strdup(nam);
868 if (sym->nam == NULL) {
872 sym->val = strdup(val);
873 if (sym->val == NULL) {
879 sym->persist = persist;
880 TAILQ_INSERT_TAIL(&symhead, sym, entry);
885 symget(const char *nam)
889 TAILQ_FOREACH(sym, &symhead, entry) {
890 if (strcmp(nam, sym->nam) == 0) {