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 void conf_new_access_rule(struct gotd_repo *,
90 enum gotd_access, int, char *);
91 static enum gotd_procid gotd_proc_id;
103 %token PATH ERROR ON UNIX_SOCKET UNIX_GROUP USER REPOSITORY PERMIT DENY
106 %token <v.string> STRING
107 %token <v.number> NUMBER
108 %type <v.number> boolean
115 | grammar repository '\n'
119 if (strcasecmp($1, "1") == 0 ||
120 strcasecmp($1, "yes") == 0 ||
121 strcasecmp($1, "on") == 0)
123 else if (strcasecmp($1, "0") == 0 ||
124 strcasecmp($1, "off") == 0 ||
125 strcasecmp($1, "no") == 0)
128 yyerror("invalid boolean value '%s'", $1);
135 | NUMBER { $$ = $1; }
138 main : UNIX_SOCKET STRING {
139 if (gotd_proc_id == PROC_LISTEN) {
140 if (strlcpy(gotd->unix_socket_path, $2,
141 sizeof(gotd->unix_socket_path)) >=
142 sizeof(gotd->unix_socket_path)) {
143 yyerror("%s: unix socket path too long",
151 | UNIX_GROUP STRING {
152 if (strlcpy(gotd->unix_group_name, $2,
153 sizeof(gotd->unix_group_name)) >=
154 sizeof(gotd->unix_group_name)) {
155 yyerror("%s: unix group name too long",
163 if (strlcpy(gotd->user_name, $2,
164 sizeof(gotd->user_name)) >=
165 sizeof(gotd->user_name)) {
166 yyerror("%s: user name too long", __func__);
174 repository : REPOSITORY STRING {
175 struct gotd_repo *repo;
177 TAILQ_FOREACH(repo, &gotd->repos, entry) {
178 if (strcmp(repo->name, $2) == 0) {
179 yyerror("duplicate repository '%s'", $2);
185 if (gotd_proc_id == PROC_GOTD) {
186 new_repo = conf_new_repo($2);
190 | REPOSITORY STRING {
191 struct gotd_repo *repo;
193 TAILQ_FOREACH(repo, &gotd->repos, entry) {
194 if (strcmp(repo->name, $2) == 0) {
195 yyerror("duplicate repository '%s'", $2);
201 if (gotd_proc_id == PROC_GOTD) {
202 new_repo = conf_new_repo($2);
205 } '{' optnl repoopts2 '}' {
209 repoopts1 : PATH STRING {
210 if (gotd_proc_id == PROC_GOTD) {
211 if (!got_path_is_absolute($2)) {
212 yyerror("%s: path %s is not absolute",
217 if (strlcpy(new_repo->path, $2,
218 sizeof(new_repo->path)) >=
219 sizeof(new_repo->path)) {
220 yyerror("%s: path truncated", __func__);
228 if (gotd_proc_id == PROC_GOTD) {
229 conf_new_access_rule(new_repo,
230 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
234 if (gotd_proc_id == PROC_GOTD) {
235 conf_new_access_rule(new_repo,
236 GOTD_ACCESS_PERMITTED,
237 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
241 if (gotd_proc_id == PROC_GOTD) {
242 conf_new_access_rule(new_repo,
243 GOTD_ACCESS_DENIED, 0, $2);
248 repoopts2 : repoopts2 repoopts1 nl
255 optnl : '\n' optnl /* zero or more newlines */
267 yyerror(const char *fmt, ...)
274 if (vasprintf(&msg, fmt, ap) == -1)
275 fatalx("yyerror vasprintf");
277 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
283 kw_cmp(const void *k, const void *e)
285 return (strcmp(k, ((const struct keywords *)e)->k_name));
291 /* This has to be sorted always. */
292 static const struct keywords keywords[] = {
296 { "permit", PERMIT },
297 { "repository", REPOSITORY },
300 { "unix_group", UNIX_GROUP },
301 { "unix_socket", UNIX_SOCKET },
304 const struct keywords *p;
306 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
307 sizeof(keywords[0]), kw_cmp);
315 #define MAXPUSHBACK 128
317 unsigned char *parsebuf;
319 unsigned char pushback_buffer[MAXPUSHBACK];
320 int pushback_index = 0;
328 /* Read character from the parsebuffer instead of input. */
329 if (parseindex >= 0) {
330 c = parsebuf[parseindex++];
339 return (pushback_buffer[--pushback_index]);
342 c = getc(file->stream);
344 yyerror("reached end of file while parsing "
349 c = getc(file->stream);
351 next = getc(file->stream);
356 yylval.lineno = file->lineno;
358 c = getc(file->stream);
374 if (pushback_index < MAXPUSHBACK-1)
375 return (pushback_buffer[pushback_index++] = c);
387 /* Skip to either EOF or the first real EOL. */
390 c = pushback_buffer[--pushback_index];
406 unsigned char buf[8096];
407 unsigned char *p, *val;
414 while (c == ' ' || c == '\t')
415 c = lgetc(0); /* nothing */
417 yylval.lineno = file->lineno;
420 while (c != '\n' && c != EOF)
421 c = lgetc(0); /* nothing */
423 if (c == '$' && parsebuf == NULL) {
429 if (p + 1 >= buf + sizeof(buf) - 1) {
430 yyerror("string too long");
433 if (isalnum(c) || c == '_') {
443 yyerror("macro '%s' not defined", buf);
462 } else if (c == '\\') {
463 next = lgetc(quotec);
466 if (next == quotec || c == ' ' || c == '\t')
468 else if (next == '\n') {
473 } else if (c == quotec) {
476 } else if (c == '\0') {
477 yyerror("syntax error");
480 if (p + 1 >= buf + sizeof(buf) - 1) {
481 yyerror("string too long");
486 yylval.v.string = strdup(buf);
487 if (yylval.v.string == NULL)
488 err(1, "yylex: strdup");
492 #define allowed_to_end_number(x) \
493 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
495 if (c == '-' || isdigit(c)) {
498 if ((unsigned)(p-buf) >= sizeof(buf)) {
499 yyerror("string too long");
503 } while (c != EOF && isdigit(c));
505 if (p == buf + 1 && buf[0] == '-')
507 if (c == EOF || allowed_to_end_number(c)) {
508 const char *errstr = NULL;
511 yylval.v.number = strtonum(buf, LLONG_MIN,
514 yyerror("\"%s\" invalid number: %s",
529 #define allowed_in_string(x) \
530 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
531 x != '{' && x != '}' && \
532 x != '!' && x != '=' && x != '#' && \
535 if (isalnum(c) || c == ':' || c == '_') {
538 if ((unsigned)(p-buf) >= sizeof(buf)) {
539 yyerror("string too long");
543 } while (c != EOF && (allowed_in_string(c)));
547 if (token == STRING) {
548 yylval.v.string = strdup(buf);
549 if (yylval.v.string == NULL)
550 err(1, "yylex: strdup");
555 yylval.lineno = file->lineno;
564 check_file_secrecy(int fd, const char *fname)
568 if (fstat(fd, &st)) {
569 log_warn("cannot stat %s", fname);
572 if (st.st_uid != 0 && st.st_uid != getuid()) {
573 log_warnx("%s: owner not root or current user", fname);
576 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
577 log_warnx("%s: group writable or world read/writable", fname);
584 newfile(const char *name, int secret)
588 nfile = calloc(1, sizeof(struct file));
593 nfile->name = strdup(name);
594 if (nfile->name == NULL) {
599 nfile->stream = fopen(nfile->name, "r");
600 if (nfile->stream == NULL) {
601 /* no warning, we don't require a conf file */
606 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
607 fclose(nfile->stream);
617 closefile(struct file *xfile)
619 fclose(xfile->stream);
625 parse_config(const char *filename, enum gotd_procid proc_id,
628 struct sym *sym, *next;
630 memset(env, 0, sizeof(*env));
633 gotd_proc_id = proc_id;
634 TAILQ_INIT(&gotd->repos);
636 /* Apply default values. */
637 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
638 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
639 fprintf(stderr, "%s: unix socket path too long", __func__);
642 if (strlcpy(gotd->unix_group_name, GOTD_UNIX_GROUP,
643 sizeof(gotd->unix_group_name)) >= sizeof(gotd->unix_group_name)) {
644 fprintf(stderr, "%s: unix group name too long", __func__);
647 if (strlcpy(gotd->user_name, GOTD_USER,
648 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
649 fprintf(stderr, "%s: user name too long", __func__);
653 file = newfile(filename, 0);
655 /* just return, as we don't require a conf file */
660 errors = file->errors;
663 /* Free macros and check which have not been used. */
664 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
665 if ((gotd->verbosity > 1) && !sym->used)
666 fprintf(stderr, "warning: macro '%s' not used\n",
671 TAILQ_REMOVE(&symhead, sym, entry);
682 static struct gotd_repo *
683 conf_new_repo(const char *name)
685 struct gotd_repo *repo;
687 if (strchr(name, '\n') != NULL) {
688 fatalx("%s: repository names must not contain linefeeds: %s",
689 getprogname(), name);
692 repo = calloc(1, sizeof(*repo));
694 fatalx("%s: calloc", __func__);
696 STAILQ_INIT(&repo->rules);
698 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
700 fatalx("%s: strlcpy", __func__);
702 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
709 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
710 int authorization, char *identifier)
712 struct gotd_access_rule *rule;
714 rule = calloc(1, sizeof(*rule));
718 rule->access = access;
719 rule->authorization = authorization;
720 rule->identifier = identifier;
722 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
726 symset(const char *nam, const char *val, int persist)
730 TAILQ_FOREACH(sym, &symhead, entry) {
731 if (strcmp(nam, sym->nam) == 0)
736 if (sym->persist == 1)
741 TAILQ_REMOVE(&symhead, sym, entry);
745 sym = calloc(1, sizeof(*sym));
749 sym->nam = strdup(nam);
750 if (sym->nam == NULL) {
754 sym->val = strdup(val);
755 if (sym->val == NULL) {
761 sym->persist = persist;
762 TAILQ_INSERT_TAIL(&symhead, sym, entry);
767 symget(const char *nam)
771 TAILQ_FOREACH(sym, &symhead, entry) {
772 if (strcmp(nam, sym->nam) == 0) {