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
103 %token <v.string> STRING
104 %token <v.number> NUMBER
105 %type <v.number> boolean
112 | grammar repository '\n'
116 if (strcasecmp($1, "1") == 0 ||
117 strcasecmp($1, "yes") == 0 ||
118 strcasecmp($1, "on") == 0)
120 else if (strcasecmp($1, "0") == 0 ||
121 strcasecmp($1, "off") == 0 ||
122 strcasecmp($1, "no") == 0)
125 yyerror("invalid boolean value '%s'", $1);
132 | NUMBER { $$ = $1; }
135 main : UNIX_SOCKET STRING {
136 if (gotd_proc_id == PROC_GOTD) {
137 if (strlcpy(gotd->unix_socket_path, $2,
138 sizeof(gotd->unix_socket_path)) >=
139 sizeof(gotd->unix_socket_path)) {
140 yyerror("%s: unix socket path too long",
148 | UNIX_GROUP STRING {
149 if (strlcpy(gotd->unix_group_name, $2,
150 sizeof(gotd->unix_group_name)) >=
151 sizeof(gotd->unix_group_name)) {
152 yyerror("%s: unix group name too long",
160 if (strlcpy(gotd->user_name, $2,
161 sizeof(gotd->user_name)) >=
162 sizeof(gotd->user_name)) {
163 yyerror("%s: user name too long", __func__);
171 repository : REPOSITORY STRING {
172 struct gotd_repo *repo;
174 TAILQ_FOREACH(repo, &gotd->repos, entry) {
175 if (strcmp(repo->name, $2) == 0) {
176 yyerror("duplicate repository '%s'", $2);
182 if (gotd_proc_id == PROC_GOTD) {
183 new_repo = conf_new_repo($2);
187 | REPOSITORY STRING {
188 struct gotd_repo *repo;
190 TAILQ_FOREACH(repo, &gotd->repos, entry) {
191 if (strcmp(repo->name, $2) == 0) {
192 yyerror("duplicate repository '%s'", $2);
198 if (gotd_proc_id == PROC_GOTD) {
199 new_repo = conf_new_repo($2);
202 } '{' optnl repoopts2 '}' {
206 repoopts1 : PATH STRING {
207 if (gotd_proc_id == PROC_GOTD) {
208 if (!got_path_is_absolute($2)) {
209 yyerror("%s: path %s is not absolute",
214 if (strlcpy(new_repo->path, $2,
215 sizeof(new_repo->path)) >=
216 sizeof(new_repo->path)) {
217 yyerror("%s: path truncated", __func__);
226 repoopts2 : repoopts2 repoopts1 nl
233 optnl : '\n' optnl /* zero or more newlines */
245 yyerror(const char *fmt, ...)
252 if (vasprintf(&msg, fmt, ap) == -1)
253 fatalx("yyerror vasprintf");
255 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
261 kw_cmp(const void *k, const void *e)
263 return (strcmp(k, ((const struct keywords *)e)->k_name));
269 /* This has to be sorted always. */
270 static const struct keywords keywords[] = {
273 { "repository", REPOSITORY },
274 { "unix_group", UNIX_GROUP },
275 { "unix_socket", UNIX_SOCKET },
278 const struct keywords *p;
280 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
281 sizeof(keywords[0]), kw_cmp);
289 #define MAXPUSHBACK 128
291 unsigned char *parsebuf;
293 unsigned char pushback_buffer[MAXPUSHBACK];
294 int pushback_index = 0;
302 /* Read character from the parsebuffer instead of input. */
303 if (parseindex >= 0) {
304 c = parsebuf[parseindex++];
313 return (pushback_buffer[--pushback_index]);
316 c = getc(file->stream);
318 yyerror("reached end of file while parsing "
323 c = getc(file->stream);
325 next = getc(file->stream);
330 yylval.lineno = file->lineno;
332 c = getc(file->stream);
348 if (pushback_index < MAXPUSHBACK-1)
349 return (pushback_buffer[pushback_index++] = c);
361 /* Skip to either EOF or the first real EOL. */
364 c = pushback_buffer[--pushback_index];
380 unsigned char buf[8096];
381 unsigned char *p, *val;
388 while (c == ' ' || c == '\t')
389 c = lgetc(0); /* nothing */
391 yylval.lineno = file->lineno;
394 while (c != '\n' && c != EOF)
395 c = lgetc(0); /* nothing */
397 if (c == '$' && parsebuf == NULL) {
403 if (p + 1 >= buf + sizeof(buf) - 1) {
404 yyerror("string too long");
407 if (isalnum(c) || c == '_') {
417 yyerror("macro '%s' not defined", buf);
436 } else if (c == '\\') {
437 next = lgetc(quotec);
440 if (next == quotec || c == ' ' || c == '\t')
442 else if (next == '\n') {
447 } else if (c == quotec) {
450 } else if (c == '\0') {
451 yyerror("syntax error");
454 if (p + 1 >= buf + sizeof(buf) - 1) {
455 yyerror("string too long");
460 yylval.v.string = strdup(buf);
461 if (yylval.v.string == NULL)
462 err(1, "yylex: strdup");
466 #define allowed_to_end_number(x) \
467 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
469 if (c == '-' || isdigit(c)) {
472 if ((unsigned)(p-buf) >= sizeof(buf)) {
473 yyerror("string too long");
477 } while (c != EOF && isdigit(c));
479 if (p == buf + 1 && buf[0] == '-')
481 if (c == EOF || allowed_to_end_number(c)) {
482 const char *errstr = NULL;
485 yylval.v.number = strtonum(buf, LLONG_MIN,
488 yyerror("\"%s\" invalid number: %s",
503 #define allowed_in_string(x) \
504 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
505 x != '{' && x != '}' && \
506 x != '!' && x != '=' && x != '#' && \
509 if (isalnum(c) || c == ':' || c == '_') {
512 if ((unsigned)(p-buf) >= sizeof(buf)) {
513 yyerror("string too long");
517 } while (c != EOF && (allowed_in_string(c)));
521 if (token == STRING) {
522 yylval.v.string = strdup(buf);
523 if (yylval.v.string == NULL)
524 err(1, "yylex: strdup");
529 yylval.lineno = file->lineno;
538 check_file_secrecy(int fd, const char *fname)
542 if (fstat(fd, &st)) {
543 log_warn("cannot stat %s", fname);
546 if (st.st_uid != 0 && st.st_uid != getuid()) {
547 log_warnx("%s: owner not root or current user", fname);
550 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
551 log_warnx("%s: group writable or world read/writable", fname);
558 newfile(const char *name, int secret)
562 nfile = calloc(1, sizeof(struct file));
567 nfile->name = strdup(name);
568 if (nfile->name == NULL) {
573 nfile->stream = fopen(nfile->name, "r");
574 if (nfile->stream == NULL) {
575 /* no warning, we don't require a conf file */
580 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
581 fclose(nfile->stream);
591 closefile(struct file *xfile)
593 fclose(xfile->stream);
599 parse_config(const char *filename, enum gotd_procid proc_id,
602 struct sym *sym, *next;
604 memset(env, 0, sizeof(*env));
607 gotd_proc_id = proc_id;
608 TAILQ_INIT(&gotd->repos);
610 /* Apply default values. */
611 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
612 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
613 fprintf(stderr, "%s: unix socket path too long", __func__);
616 if (strlcpy(gotd->unix_group_name, GOTD_UNIX_GROUP,
617 sizeof(gotd->unix_group_name)) >= sizeof(gotd->unix_group_name)) {
618 fprintf(stderr, "%s: unix group name too long", __func__);
621 if (strlcpy(gotd->user_name, GOTD_USER,
622 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
623 fprintf(stderr, "%s: user name too long", __func__);
627 file = newfile(filename, 0);
629 /* just return, as we don't require a conf file */
634 errors = file->errors;
637 /* Free macros and check which have not been used. */
638 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
639 if ((gotd->verbosity > 1) && !sym->used)
640 fprintf(stderr, "warning: macro '%s' not used\n",
645 TAILQ_REMOVE(&symhead, sym, entry);
656 static struct gotd_repo *
657 conf_new_repo(const char *name)
659 struct gotd_repo *repo;
661 if (strchr(name, '\n') != NULL) {
662 fatalx("%s: repository names must not contain linefeeds: %s",
663 getprogname(), name);
666 repo = calloc(1, sizeof(*repo));
668 fatalx("%s: calloc", __func__);
670 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
672 fatalx("%s: strlcpy", __func__);
674 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
681 symset(const char *nam, const char *val, int persist)
685 TAILQ_FOREACH(sym, &symhead, entry) {
686 if (strcmp(nam, sym->nam) == 0)
691 if (sym->persist == 1)
696 TAILQ_REMOVE(&symhead, sym, entry);
700 sym = calloc(1, sizeof(*sym));
704 sym->nam = strdup(nam);
705 if (sym->nam == NULL) {
709 sym->val = strdup(val);
710 if (sym->val == NULL) {
716 sym->persist = persist;
717 TAILQ_INSERT_TAIL(&symhead, sym, entry);
722 symget(const char *nam)
726 TAILQ_FOREACH(sym, &symhead, entry) {
727 if (strcmp(nam, sym->nam) == 0) {