2 * Copyright (c) 2020, 2021 Tracey Emery <tracey@openbsd.org>
3 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
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.
25 #include <sys/types.h>
26 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "gotconfig.h"
51 static const struct got_error* newfile(struct file**, const char *, int *);
52 static void closefile(struct file *);
55 int yyerror(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
64 static int parseport(char *, long long *);
66 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
68 TAILQ_ENTRY(sym) entry;
75 int symset(const char *, const char *, int);
76 int cmdline_symset(char *);
77 char *symget(const char *);
79 static int atoul(char *, u_long *);
81 static const struct got_error* gerror;
82 static struct gotconfig_remote_repo *remote;
83 static struct gotconfig gotconfig;
84 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
85 static const struct got_error* new_fetch_config(struct fetch_config **);
86 static const struct got_error* new_send_config(struct send_config **);
92 struct node_branch *branch;
101 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
102 %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS FETCH_ALL_BRANCHES REFERENCE
104 %token <v.string> STRING
105 %token <v.number> NUMBER
106 %type <v.number> boolean portplain
107 %type <v.string> numberstring
108 %type <v.branch> branch xbranch branch_list
109 %type <v.ref> ref xref ref_list
113 grammar : /* empty */
115 | grammar author '\n'
116 | grammar remote '\n'
117 | grammar allowed_signers '\n'
120 if (strcasecmp($1, "true") == 0 ||
121 strcasecmp($1, "yes") == 0)
123 else if (strcasecmp($1, "false") == 0 ||
124 strcasecmp($1, "no") == 0)
127 yyerror("invalid boolean value '%s'", $1);
134 numberstring : NUMBER {
136 if (asprintf(&s, "%lld", $1) == -1) {
137 yyerror("string: asprintf");
144 portplain : numberstring {
145 if (parseport($1, &$$) == -1) {
152 branch : /* empty */ { $$ = NULL; }
153 | xbranch { $$ = $1; }
154 | '{' optnl branch_list '}' { $$ = $3; }
157 $$ = calloc(1, sizeof(struct node_branch));
162 $$->branch_name = $1;
166 branch_list : xbranch optnl { $$ = $1; }
167 | branch_list comma xbranch optnl {
173 ref : /* empty */ { $$ = NULL; }
175 | '{' optnl ref_list '}' { $$ = $3; }
178 $$ = calloc(1, sizeof(struct node_ref));
187 ref_list : xref optnl { $$ = $1; }
188 | ref_list comma xref optnl {
194 remoteopts2 : remoteopts2 remoteopts1 nl
197 remoteopts1 : REPOSITORY STRING {
198 remote->repository = $2;
204 remote->protocol = $2;
206 | MIRROR_REFERENCES boolean {
207 remote->mirror_references = $2;
209 | FETCH_ALL_BRANCHES boolean {
210 remote->fetch_all_branches = $2;
219 remote->fetch_ref = $2;
222 static const struct got_error* error;
224 if (remote->fetch_config != NULL) {
225 yyerror("fetch block already exists");
228 error = new_fetch_config(&remote->fetch_config);
230 yyerror("%s", error->msg);
233 } '{' optnl fetchempty '}'
235 static const struct got_error* error;
237 if (remote->send_config != NULL) {
238 yyerror("send block already exists");
241 error = new_send_config(&remote->send_config);
243 yyerror("%s", error->msg);
246 } '{' optnl sendempty '}'
248 fetchempty : /* empty */
251 fetchopts2 : fetchopts2 fetchopts1 nl
254 fetchopts1 : REPOSITORY STRING {
255 remote->fetch_config->repository = $2;
258 remote->fetch_config->server = $2;
261 remote->fetch_config->protocol = $2;
264 remote->fetch_config->port = $2;
267 remote->fetch_config->branch = $2;
270 sendempty : /* empty */
273 sendopts2 : sendopts2 sendopts1 nl
276 sendopts1 : REPOSITORY STRING {
277 remote->send_config->repository = $2;
280 remote->send_config->server = $2;
283 remote->send_config->protocol = $2;
286 remote->send_config->port = $2;
289 remote->send_config->branch = $2;
292 remote : REMOTE STRING {
293 static const struct got_error* error;
295 error = new_remote(&remote);
298 yyerror("%s", error->msg);
302 } '{' optnl remoteopts2 '}' {
303 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
304 gotconfig.nremotes++;
307 author : AUTHOR STRING {
308 gotconfig.author = $2;
311 allowed_signers : ALLOWED_SIGNERS STRING {
312 gotconfig.allowed_signers_file = $2;
315 revoked_signers : REVOKED_SIGNERS STRING {
316 gotconfig.revoked_signers_file = $2;
335 yyerror(const char *fmt, ...)
342 if (vasprintf(&msg, fmt, ap) == -1) {
343 gerror = got_error_from_errno("vasprintf");
347 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
349 gerror = got_error_from_errno("asprintf");
352 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
357 kw_cmp(const void *k, const void *e)
359 return (strcmp(k, ((const struct keywords *)e)->k_name));
365 /* This has to be sorted always. */
366 static const struct keywords keywords[] = {
367 {"allowed_signers", ALLOWED_SIGNERS},
371 {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
372 {"fetch_all_branches", FETCH_ALL_BRANCHES},
373 {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
374 {"mirror_references", MIRROR_REFERENCES},
376 {"protocol", PROTOCOL},
377 {"reference", REFERENCE},
379 {"repository", REPOSITORY},
380 {"revoked_signers", REVOKED_SIGNERS},
384 const struct keywords *p;
386 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
387 sizeof(keywords[0]), kw_cmp);
395 #define START_EXPAND 1
396 #define DONE_EXPAND 2
398 static int expanding;
406 if (file->ungetpos > 0)
407 c = file->ungetbuf[--file->ungetpos];
409 c = getc(file->stream);
411 if (c == START_EXPAND)
413 else if (c == DONE_EXPAND)
429 yyerror("reached end of file while parsing "
442 yylval.lineno = file->lineno;
455 if (file->ungetpos >= file->ungetsize) {
456 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
458 err(1, "%s", __func__);
460 file->ungetsize *= 2;
462 file->ungetbuf[file->ungetpos++] = c;
470 /* Skip to either EOF or the first real EOL. */
489 if (atoul(n, &ulval) == 0) {
490 if (ulval == 0 || ulval > 65535) {
491 yyerror("illegal port value %lu", ulval);
496 s = getservbyname(n, "tcp");
498 s = getservbyname(n, "udp");
500 yyerror("unknown port %s", n);
508 parseport(char *port, long long *pn)
510 if ((*pn = getservice(port)) == -1) {
529 while (c == ' ' || c == '\t')
530 c = lgetc(0); /* nothing */
532 yylval.lineno = file->lineno;
535 while (c != '\n' && c != EOF)
536 c = lgetc(0); /* nothing */
538 if (c == '$' && !expanding) {
544 if (p + 1 >= buf + sizeof(buf) - 1) {
545 yyerror("string too long");
548 if (isalnum(c) || c == '_') {
558 yyerror("macro '%s' not defined", buf);
561 p = val + strlen(val) - 1;
562 lungetc(DONE_EXPAND);
564 lungetc((unsigned char)*p);
567 lungetc(START_EXPAND);
582 } else if (c == '\\') {
583 next = lgetc(quotec);
586 if (next == quotec || c == ' ' || c == '\t')
588 else if (next == '\n') {
593 } else if (c == quotec) {
596 } else if (c == '\0') {
597 yyerror("syntax error");
600 if (p + 1 >= buf + sizeof(buf) - 1) {
601 yyerror("string too long");
606 yylval.v.string = strdup(buf);
607 if (yylval.v.string == NULL)
608 err(1, "%s", __func__);
612 #define allowed_to_end_number(x) \
613 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
615 if (c == '-' || isdigit(c)) {
618 if ((size_t)(p-buf) >= sizeof(buf)) {
619 yyerror("string too long");
623 } while (c != EOF && isdigit(c));
625 if (p == buf + 1 && buf[0] == '-')
627 if (c == EOF || allowed_to_end_number(c)) {
628 const char *errstr = NULL;
631 yylval.v.number = strtonum(buf, LLONG_MIN,
634 yyerror("\"%s\" invalid number: %s",
642 lungetc((unsigned char)*--p);
643 c = (unsigned char)*--p;
649 #define allowed_in_string(x) \
650 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
651 x != '{' && x != '}' && \
652 x != '!' && x != '=' && x != '#' && \
655 if (isalnum(c) || c == ':' || c == '_') {
658 if ((size_t)(p-buf) >= sizeof(buf)) {
659 yyerror("string too long");
663 } while (c != EOF && (allowed_in_string(c)));
667 if (token == STRING) {
668 yylval.v.string = strdup(buf);
669 if (yylval.v.string == NULL)
670 err(1, "%s", __func__);
675 yylval.lineno = file->lineno;
683 static const struct got_error*
684 newfile(struct file **nfile, const char *filename, int *fd)
686 const struct got_error* error = NULL;
688 (*nfile) = calloc(1, sizeof(struct file));
689 if ((*nfile) == NULL)
690 return got_error_from_errno("calloc");
691 (*nfile)->stream = fdopen(*fd, "r");
692 if ((*nfile)->stream == NULL) {
693 error = got_error_from_errno("fdopen");
697 *fd = -1; /* Stream owns the file descriptor now. */
698 (*nfile)->name = filename;
699 (*nfile)->lineno = 1;
700 (*nfile)->ungetsize = 16;
701 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
702 if ((*nfile)->ungetbuf == NULL) {
703 error = got_error_from_errno("malloc");
704 fclose((*nfile)->stream);
711 static const struct got_error*
712 new_remote(struct gotconfig_remote_repo **remote)
714 const struct got_error *error = NULL;
716 *remote = calloc(1, sizeof(**remote));
718 error = got_error_from_errno("calloc");
722 static const struct got_error*
723 new_fetch_config(struct fetch_config **fetch_config)
725 const struct got_error *error = NULL;
727 *fetch_config = calloc(1, sizeof(**fetch_config));
728 if (*fetch_config == NULL)
729 error = got_error_from_errno("calloc");
733 static const struct got_error*
734 new_send_config(struct send_config **send_config)
736 const struct got_error *error = NULL;
738 *send_config = calloc(1, sizeof(**send_config));
739 if (*send_config == NULL)
740 error = got_error_from_errno("calloc");
745 closefile(struct file *file)
747 fclose(file->stream);
748 free(file->ungetbuf);
752 const struct got_error *
753 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
755 const struct got_error *err = NULL;
756 struct sym *sym, *next;
760 err = newfile(&file, filename, fd);
764 TAILQ_INIT(&gotconfig.remotes);
769 /* Free macros and check which have not been used. */
770 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
774 TAILQ_REMOVE(&symhead, sym, entry);
785 free_fetch_config(struct fetch_config *fetch_config)
787 free(remote->fetch_config->repository);
788 free(remote->fetch_config->server);
789 free(remote->fetch_config->protocol);
790 free(remote->fetch_config);
794 free_send_config(struct send_config *send_config)
796 free(remote->send_config->repository);
797 free(remote->send_config->server);
798 free(remote->send_config->protocol);
799 free(remote->send_config);
803 gotconfig_free(struct gotconfig *conf)
805 struct gotconfig_remote_repo *remote;
808 free(conf->allowed_signers_file);
809 free(conf->revoked_signers_file);
810 while (!TAILQ_EMPTY(&conf->remotes)) {
811 remote = TAILQ_FIRST(&conf->remotes);
812 TAILQ_REMOVE(&conf->remotes, remote, entry);
813 if (remote->fetch_config != NULL)
814 free_fetch_config(remote->fetch_config);
815 if (remote->send_config != NULL)
816 free_send_config(remote->send_config);
818 free(remote->repository);
819 free(remote->server);
820 free(remote->protocol);
826 symset(const char *nam, const char *val, int persist)
830 TAILQ_FOREACH(sym, &symhead, entry) {
831 if (strcmp(nam, sym->nam) == 0)
836 if (sym->persist == 1)
841 TAILQ_REMOVE(&symhead, sym, entry);
845 sym = calloc(1, sizeof(*sym));
849 sym->nam = strdup(nam);
850 if (sym->nam == NULL) {
854 sym->val = strdup(val);
855 if (sym->val == NULL) {
861 sym->persist = persist;
862 TAILQ_INSERT_TAIL(&symhead, sym, entry);
867 cmdline_symset(char *s)
873 val = strrchr(s, '=');
877 len = strlen(s) - strlen(val) + 1;
880 errx(1, "cmdline_symset: malloc");
882 strlcpy(sym, s, len);
884 ret = symset(sym, val + 1, 1);
891 symget(const char *nam)
895 TAILQ_FOREACH(sym, &symhead, entry) {
896 if (strcmp(nam, sym->nam) == 0) {
905 atoul(char *s, u_long *ulvalp)
911 ulval = strtoul(s, &ep, 0);
912 if (s[0] == '\0' || *ep != '\0')
914 if (errno == ERANGE && ulval == ULONG_MAX)