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
119 | grammar repository '\n'
124 yyerror("invalid timeout: %lld", $1);
132 const char *type = "seconds";
137 yyerror("invalid number of seconds: %s", $1);
143 switch ($1[len - 1]) {
163 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
165 yyerror("number of %s is %s: %s", type,
176 main : LISTEN ON STRING {
177 if (!got_path_is_absolute($3))
178 yyerror("bad unix socket path \"%s\": "
179 "must be an absolute path", $3);
181 if (gotd_proc_id == PROC_LISTEN) {
182 if (strlcpy(gotd->unix_socket_path, $3,
183 sizeof(gotd->unix_socket_path)) >=
184 sizeof(gotd->unix_socket_path)) {
185 yyerror("%s: unix socket path too long",
194 if (strlcpy(gotd->user_name, $2,
195 sizeof(gotd->user_name)) >=
196 sizeof(gotd->user_name)) {
197 yyerror("%s: user name too long", __func__);
206 connection : CONNECTION '{' optnl conflags_l '}'
207 | CONNECTION conflags
209 conflags_l : conflags optnl conflags_l
213 conflags : REQUEST TIMEOUT timeout {
214 if ($3.tv_sec <= 0) {
215 yyerror("invalid timeout: %lld", $3.tv_sec);
218 memcpy(&gotd->request_timeout, &$3,
219 sizeof(gotd->request_timeout));
221 | LIMIT USER STRING NUMBER {
222 if (gotd_proc_id == PROC_LISTEN &&
223 conf_limit_user_connections($3, $4) == -1) {
231 repository : REPOSITORY STRING {
232 struct gotd_repo *repo;
234 TAILQ_FOREACH(repo, &gotd->repos, entry) {
235 if (strcmp(repo->name, $2) == 0) {
236 yyerror("duplicate repository '%s'", $2);
242 if (gotd_proc_id == PROC_GOTD ||
243 gotd_proc_id == PROC_AUTH) {
244 new_repo = conf_new_repo($2);
247 } '{' optnl repoopts2 '}' {
251 repoopts1 : PATH STRING {
252 if (gotd_proc_id == PROC_GOTD ||
253 gotd_proc_id == PROC_AUTH) {
254 if (!got_path_is_absolute($2)) {
255 yyerror("%s: path %s is not absolute",
260 if (realpath($2, new_repo->path) == NULL) {
261 yyerror("realpath %s: %s", $2, strerror(errno));
269 if (gotd_proc_id == PROC_AUTH) {
270 conf_new_access_rule(new_repo,
271 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
275 if (gotd_proc_id == PROC_AUTH) {
276 conf_new_access_rule(new_repo,
277 GOTD_ACCESS_PERMITTED,
278 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
282 if (gotd_proc_id == PROC_AUTH) {
283 conf_new_access_rule(new_repo,
284 GOTD_ACCESS_DENIED, 0, $2);
289 repoopts2 : repoopts2 repoopts1 nl
296 optnl : '\n' optnl /* zero or more newlines */
308 yyerror(const char *fmt, ...)
315 if (vasprintf(&msg, fmt, ap) == -1)
316 fatalx("yyerror vasprintf");
318 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
324 kw_cmp(const void *k, const void *e)
326 return (strcmp(k, ((const struct keywords *)e)->k_name));
332 /* This has to be sorted always. */
333 static const struct keywords keywords[] = {
334 { "connection", CONNECTION },
337 { "listen", LISTEN },
340 { "permit", PERMIT },
341 { "repository", REPOSITORY },
342 { "request", REQUEST },
345 { "timeout", TIMEOUT },
348 const struct keywords *p;
350 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
351 sizeof(keywords[0]), kw_cmp);
359 #define MAXPUSHBACK 128
361 unsigned char *parsebuf;
363 unsigned char pushback_buffer[MAXPUSHBACK];
364 int pushback_index = 0;
372 /* Read character from the parsebuffer instead of input. */
373 if (parseindex >= 0) {
374 c = parsebuf[parseindex++];
383 return (pushback_buffer[--pushback_index]);
386 c = getc(file->stream);
388 yyerror("reached end of file while parsing "
393 c = getc(file->stream);
395 next = getc(file->stream);
400 yylval.lineno = file->lineno;
402 c = getc(file->stream);
418 if (pushback_index < MAXPUSHBACK-1)
419 return (pushback_buffer[pushback_index++] = c);
431 /* Skip to either EOF or the first real EOL. */
434 c = pushback_buffer[--pushback_index];
450 unsigned char buf[8096];
451 unsigned char *p, *val;
458 while (c == ' ' || c == '\t')
459 c = lgetc(0); /* nothing */
461 yylval.lineno = file->lineno;
464 while (c != '\n' && c != EOF)
465 c = lgetc(0); /* nothing */
467 if (c == '$' && parsebuf == NULL) {
473 if (p + 1 >= buf + sizeof(buf) - 1) {
474 yyerror("string too long");
477 if (isalnum(c) || c == '_') {
487 yyerror("macro '%s' not defined", buf);
506 } else if (c == '\\') {
507 next = lgetc(quotec);
510 if (next == quotec || c == ' ' || c == '\t')
512 else if (next == '\n') {
517 } else if (c == quotec) {
520 } else if (c == '\0') {
521 yyerror("syntax error");
524 if (p + 1 >= buf + sizeof(buf) - 1) {
525 yyerror("string too long");
530 yylval.v.string = strdup(buf);
531 if (yylval.v.string == NULL)
532 err(1, "yylex: strdup");
536 #define allowed_to_end_number(x) \
537 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
539 if (c == '-' || isdigit(c)) {
542 if ((unsigned)(p-buf) >= sizeof(buf)) {
543 yyerror("string too long");
547 } while (c != EOF && isdigit(c));
549 if (p == buf + 1 && buf[0] == '-')
551 if (c == EOF || allowed_to_end_number(c)) {
552 const char *errstr = NULL;
555 yylval.v.number = strtonum(buf, LLONG_MIN,
558 yyerror("\"%s\" invalid number: %s",
573 #define allowed_in_string(x) \
574 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
575 x != '{' && x != '}' && \
576 x != '!' && x != '=' && x != '#' && \
579 if (isalnum(c) || c == ':' || c == '_') {
582 if ((unsigned)(p-buf) >= sizeof(buf)) {
583 yyerror("string too long");
587 } while (c != EOF && (allowed_in_string(c)));
591 if (token == STRING) {
592 yylval.v.string = strdup(buf);
593 if (yylval.v.string == NULL)
594 err(1, "yylex: strdup");
599 yylval.lineno = file->lineno;
608 check_file_secrecy(int fd, const char *fname)
612 if (fstat(fd, &st)) {
613 log_warn("cannot stat %s", fname);
616 if (st.st_uid != 0 && st.st_uid != getuid()) {
617 log_warnx("%s: owner not root or current user", fname);
620 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
621 log_warnx("%s: group writable or world read/writable", fname);
628 newfile(const char *name, int secret)
632 nfile = calloc(1, sizeof(struct file));
637 nfile->name = strdup(name);
638 if (nfile->name == NULL) {
643 nfile->stream = fopen(nfile->name, "r");
644 if (nfile->stream == NULL) {
645 /* no warning, we don't require a conf file */
650 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
651 fclose(nfile->stream);
661 closefile(struct file *xfile)
663 fclose(xfile->stream);
669 parse_config(const char *filename, enum gotd_procid proc_id,
672 struct sym *sym, *next;
673 struct gotd_repo *repo;
675 memset(env, 0, sizeof(*env));
678 gotd_proc_id = proc_id;
679 TAILQ_INIT(&gotd->repos);
681 /* Apply default values. */
682 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
683 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
684 fprintf(stderr, "%s: unix socket path too long", __func__);
687 if (strlcpy(gotd->user_name, GOTD_USER,
688 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
689 fprintf(stderr, "%s: user name too long", __func__);
693 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
694 gotd->request_timeout.tv_usec = 0;
696 file = newfile(filename, 0);
698 /* just return, as we don't require a conf file */
703 errors = file->errors;
706 /* Free macros and check which have not been used. */
707 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
708 if ((gotd->verbosity > 1) && !sym->used)
709 fprintf(stderr, "warning: macro '%s' not used\n",
714 TAILQ_REMOVE(&symhead, sym, entry);
722 TAILQ_FOREACH(repo, &gotd->repos, entry) {
723 if (repo->path[0] == '\0') {
724 log_warnx("repository \"%s\": no path provided in "
725 "configuration file", repo->name);
730 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
731 log_warnx("no repository defined in configuration file");
739 uid_connection_limit_cmp(const void *pa, const void *pb)
741 const struct gotd_uid_connection_limit *a = pa, *b = pb;
745 else if (a->uid > b->uid);
752 conf_limit_user_connections(const char *user, int maximum)
755 struct gotd_uid_connection_limit *limit;
759 yyerror("max connections cannot be smaller 1");
762 if (maximum > GOTD_MAXCLIENTS) {
763 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
767 if (gotd_auth_parseuid(user, &uid) == -1) {
768 yyerror("%s: no such user", user);
772 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
773 gotd->nconnection_limits, uid);
775 limit->max_connections = maximum;
779 limit = gotd->connection_limits;
780 nlimits = gotd->nconnection_limits + 1;
781 limit = reallocarray(limit, nlimits, sizeof(*limit));
783 fatal("reallocarray");
785 limit[nlimits - 1].uid = uid;
786 limit[nlimits - 1].max_connections = maximum;
788 gotd->connection_limits = limit;
789 gotd->nconnection_limits = nlimits;
790 qsort(gotd->connection_limits, gotd->nconnection_limits,
791 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
796 static struct gotd_repo *
797 conf_new_repo(const char *name)
799 struct gotd_repo *repo;
801 if (name[0] == '\0') {
802 fatalx("syntax error: empty repository name found in %s",
806 if (strchr(name, '\n') != NULL)
807 fatalx("repository names must not contain linefeeds: %s", name);
809 repo = calloc(1, sizeof(*repo));
811 fatalx("%s: calloc", __func__);
813 STAILQ_INIT(&repo->rules);
815 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
817 fatalx("%s: strlcpy", __func__);
819 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
826 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
827 int authorization, char *identifier)
829 struct gotd_access_rule *rule;
831 rule = calloc(1, sizeof(*rule));
835 rule->access = access;
836 rule->authorization = authorization;
837 rule->identifier = identifier;
839 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
843 symset(const char *nam, const char *val, int persist)
847 TAILQ_FOREACH(sym, &symhead, entry) {
848 if (strcmp(nam, sym->nam) == 0)
853 if (sym->persist == 1)
858 TAILQ_REMOVE(&symhead, sym, entry);
862 sym = calloc(1, sizeof(*sym));
866 sym->nam = strdup(nam);
867 if (sym->nam == NULL) {
871 sym->val = strdup(val);
872 if (sym->val == NULL) {
878 sym->persist = persist;
879 TAILQ_INSERT_TAIL(&symhead, sym, entry);
884 symget(const char *nam)
888 TAILQ_FOREACH(sym, &symhead, entry) {
889 if (strcmp(nam, sym->nam) == 0) {