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"
50 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
52 TAILQ_ENTRY(file) entry;
58 struct file *newfile(const char *, int);
59 static void closefile(struct file *);
60 int check_file_secrecy(int, const char *);
63 int yyerror(const char *, ...)
64 __attribute__((__format__ (printf, 1, 2)))
65 __attribute__((__nonnull__ (1)));
66 int kw_cmp(const void *, const void *);
72 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
74 TAILQ_ENTRY(sym) entry;
81 int symset(const char *, const char *, int);
82 char *symget(const char *);
86 static struct gotd *gotd;
87 static struct gotd_repo *new_repo;
88 static struct gotd_repo *conf_new_repo(const char *);
89 static enum gotd_procid gotd_proc_id;
101 %token PATH ERROR ON UNIX_SOCKET UNIX_GROUP USER REPOSITORY PERMIT DENY
104 %token <v.string> STRING
105 %token <v.number> NUMBER
106 %type <v.number> boolean
113 | grammar repository '\n'
117 if (strcasecmp($1, "1") == 0 ||
118 strcasecmp($1, "yes") == 0 ||
119 strcasecmp($1, "on") == 0)
121 else if (strcasecmp($1, "0") == 0 ||
122 strcasecmp($1, "off") == 0 ||
123 strcasecmp($1, "no") == 0)
126 yyerror("invalid boolean value '%s'", $1);
133 | NUMBER { $$ = $1; }
136 main : UNIX_SOCKET STRING {
137 if (gotd_proc_id == PROC_GOTD) {
138 if (strlcpy(gotd->unix_socket_path, $2,
139 sizeof(gotd->unix_socket_path)) >=
140 sizeof(gotd->unix_socket_path)) {
141 yyerror("%s: unix socket path too long",
149 | UNIX_GROUP STRING {
150 if (strlcpy(gotd->unix_group_name, $2,
151 sizeof(gotd->unix_group_name)) >=
152 sizeof(gotd->unix_group_name)) {
153 yyerror("%s: unix group name too long",
161 if (strlcpy(gotd->user_name, $2,
162 sizeof(gotd->user_name)) >=
163 sizeof(gotd->user_name)) {
164 yyerror("%s: user name too long", __func__);
172 repository : REPOSITORY STRING {
173 struct gotd_repo *repo;
175 TAILQ_FOREACH(repo, &gotd->repos, entry) {
176 if (strcmp(repo->name, $2) == 0) {
177 yyerror("duplicate repository '%s'", $2);
183 if (gotd_proc_id == PROC_GOTD) {
184 new_repo = conf_new_repo($2);
188 | REPOSITORY STRING {
189 struct gotd_repo *repo;
191 TAILQ_FOREACH(repo, &gotd->repos, entry) {
192 if (strcmp(repo->name, $2) == 0) {
193 yyerror("duplicate repository '%s'", $2);
199 if (gotd_proc_id == PROC_GOTD) {
200 new_repo = conf_new_repo($2);
203 } '{' optnl repoopts2 '}' {
207 repoopts1 : PATH STRING {
208 if (gotd_proc_id == PROC_GOTD) {
209 if (!got_path_is_absolute($2)) {
210 yyerror("%s: path %s is not absolute",
215 if (strlcpy(new_repo->path, $2,
216 sizeof(new_repo->path)) >=
217 sizeof(new_repo->path)) {
218 yyerror("%s: path truncated", __func__);
226 if (gotd_proc_id == PROC_GOTD) {
227 conf_new_access_rule(new_repo,
228 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
232 if (gotd_proc_id == PROC_GOTD) {
233 conf_new_access_rule(new_repo,
234 GOTD_ACCESS_PERMITTED,
235 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
239 if (gotd_proc_id == PROC_GOTD) {
240 conf_new_access_rule(new_repo,
241 GOTD_ACCESS_DENIED, 0, $2);
246 repoopts2 : repoopts2 repoopts1 nl
253 optnl : '\n' optnl /* zero or more newlines */
265 yyerror(const char *fmt, ...)
272 if (vasprintf(&msg, fmt, ap) == -1)
273 fatalx("yyerror vasprintf");
275 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
281 kw_cmp(const void *k, const void *e)
283 return (strcmp(k, ((const struct keywords *)e)->k_name));
289 /* This has to be sorted always. */
290 static const struct keywords keywords[] = {
294 { "permit", PERMIT },
295 { "repository", REPOSITORY },
298 { "unix_group", UNIX_GROUP },
299 { "unix_socket", UNIX_SOCKET },
302 const struct keywords *p;
304 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
305 sizeof(keywords[0]), kw_cmp);
313 #define MAXPUSHBACK 128
315 unsigned char *parsebuf;
317 unsigned char pushback_buffer[MAXPUSHBACK];
318 int pushback_index = 0;
326 /* Read character from the parsebuffer instead of input. */
327 if (parseindex >= 0) {
328 c = parsebuf[parseindex++];
337 return (pushback_buffer[--pushback_index]);
340 c = getc(file->stream);
342 yyerror("reached end of file while parsing "
347 c = getc(file->stream);
349 next = getc(file->stream);
354 yylval.lineno = file->lineno;
356 c = getc(file->stream);
372 if (pushback_index < MAXPUSHBACK-1)
373 return (pushback_buffer[pushback_index++] = c);
385 /* Skip to either EOF or the first real EOL. */
388 c = pushback_buffer[--pushback_index];
404 unsigned char buf[8096];
405 unsigned char *p, *val;
412 while (c == ' ' || c == '\t')
413 c = lgetc(0); /* nothing */
415 yylval.lineno = file->lineno;
418 while (c != '\n' && c != EOF)
419 c = lgetc(0); /* nothing */
421 if (c == '$' && parsebuf == NULL) {
427 if (p + 1 >= buf + sizeof(buf) - 1) {
428 yyerror("string too long");
431 if (isalnum(c) || c == '_') {
441 yyerror("macro '%s' not defined", buf);
460 } else if (c == '\\') {
461 next = lgetc(quotec);
464 if (next == quotec || c == ' ' || c == '\t')
466 else if (next == '\n') {
471 } else if (c == quotec) {
474 } else if (c == '\0') {
475 yyerror("syntax error");
478 if (p + 1 >= buf + sizeof(buf) - 1) {
479 yyerror("string too long");
484 yylval.v.string = strdup(buf);
485 if (yylval.v.string == NULL)
486 err(1, "yylex: strdup");
490 #define allowed_to_end_number(x) \
491 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
493 if (c == '-' || isdigit(c)) {
496 if ((unsigned)(p-buf) >= sizeof(buf)) {
497 yyerror("string too long");
501 } while (c != EOF && isdigit(c));
503 if (p == buf + 1 && buf[0] == '-')
505 if (c == EOF || allowed_to_end_number(c)) {
506 const char *errstr = NULL;
509 yylval.v.number = strtonum(buf, LLONG_MIN,
512 yyerror("\"%s\" invalid number: %s",
527 #define allowed_in_string(x) \
528 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
529 x != '{' && x != '}' && \
530 x != '!' && x != '=' && x != '#' && \
533 if (isalnum(c) || c == ':' || c == '_') {
536 if ((unsigned)(p-buf) >= sizeof(buf)) {
537 yyerror("string too long");
541 } while (c != EOF && (allowed_in_string(c)));
545 if (token == STRING) {
546 yylval.v.string = strdup(buf);
547 if (yylval.v.string == NULL)
548 err(1, "yylex: strdup");
553 yylval.lineno = file->lineno;
562 check_file_secrecy(int fd, const char *fname)
566 if (fstat(fd, &st)) {
567 log_warn("cannot stat %s", fname);
570 if (st.st_uid != 0 && st.st_uid != getuid()) {
571 log_warnx("%s: owner not root or current user", fname);
574 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
575 log_warnx("%s: group writable or world read/writable", fname);
582 newfile(const char *name, int secret)
586 nfile = calloc(1, sizeof(struct file));
591 nfile->name = strdup(name);
592 if (nfile->name == NULL) {
597 nfile->stream = fopen(nfile->name, "r");
598 if (nfile->stream == NULL) {
599 /* no warning, we don't require a conf file */
604 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
605 fclose(nfile->stream);
615 closefile(struct file *xfile)
617 fclose(xfile->stream);
623 parse_config(const char *filename, enum gotd_procid proc_id,
626 struct sym *sym, *next;
628 memset(env, 0, sizeof(*env));
631 gotd_proc_id = proc_id;
632 TAILQ_INIT(&gotd->repos);
634 /* Apply default values. */
635 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
636 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
637 fprintf(stderr, "%s: unix socket path too long", __func__);
640 if (strlcpy(gotd->unix_group_name, GOTD_UNIX_GROUP,
641 sizeof(gotd->unix_group_name)) >= sizeof(gotd->unix_group_name)) {
642 fprintf(stderr, "%s: unix group name too long", __func__);
645 if (strlcpy(gotd->user_name, GOTD_USER,
646 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
647 fprintf(stderr, "%s: user name too long", __func__);
651 file = newfile(filename, 0);
653 /* just return, as we don't require a conf file */
658 errors = file->errors;
661 /* Free macros and check which have not been used. */
662 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
663 if ((gotd->verbosity > 1) && !sym->used)
664 fprintf(stderr, "warning: macro '%s' not used\n",
669 TAILQ_REMOVE(&symhead, sym, entry);
680 static struct gotd_repo *
681 conf_new_repo(const char *name)
683 struct gotd_repo *repo;
685 if (strchr(name, '\n') != NULL) {
686 fatalx("%s: repository names must not contain linefeeds: %s",
687 getprogname(), name);
690 repo = calloc(1, sizeof(*repo));
692 fatalx("%s: calloc", __func__);
694 STAILQ_INIT(&repo->rules);
696 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
698 fatalx("%s: strlcpy", __func__);
700 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
707 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
708 int authorization, char *identifier)
710 struct gotd_access_rule *rule;
712 rule = calloc(1, sizeof(*rule));
716 rule->access = access;
717 rule->authorization = authorization;
718 rule->identifier = identifier;
720 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
724 symset(const char *nam, const char *val, int persist)
728 TAILQ_FOREACH(sym, &symhead, entry) {
729 if (strcmp(nam, sym->nam) == 0)
734 if (sym->persist == 1)
739 TAILQ_REMOVE(&symhead, sym, entry);
743 sym = calloc(1, sizeof(*sym));
747 sym->nam = strdup(nam);
748 if (sym->nam == NULL) {
752 sym->val = strdup(val);
753 if (sym->val == NULL) {
759 sym->persist = persist;
760 TAILQ_INSERT_TAIL(&symhead, sym, entry);
765 symget(const char *nam)
769 TAILQ_FOREACH(sym, &symhead, entry) {
770 if (strcmp(nam, sym->nam) == 0) {