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 SIGNER_ID FETCH_ALL_BRANCHES
103 %token REFERENCE FETCH SEND
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'
118 | grammar revoked_signers '\n'
119 | grammar signer_id '\n'
122 if (strcasecmp($1, "true") == 0 ||
123 strcasecmp($1, "yes") == 0)
125 else if (strcasecmp($1, "false") == 0 ||
126 strcasecmp($1, "no") == 0)
129 yyerror("invalid boolean value '%s'", $1);
136 numberstring : NUMBER {
138 if (asprintf(&s, "%lld", $1) == -1) {
139 yyerror("string: asprintf");
146 portplain : numberstring {
147 if (parseport($1, &$$) == -1) {
154 branch : /* empty */ { $$ = NULL; }
155 | xbranch { $$ = $1; }
156 | '{' optnl branch_list '}' { $$ = $3; }
159 $$ = calloc(1, sizeof(struct node_branch));
164 $$->branch_name = $1;
168 branch_list : xbranch optnl { $$ = $1; }
169 | branch_list comma xbranch optnl {
175 ref : /* empty */ { $$ = NULL; }
177 | '{' optnl ref_list '}' { $$ = $3; }
180 $$ = calloc(1, sizeof(struct node_ref));
189 ref_list : xref optnl { $$ = $1; }
190 | ref_list comma xref optnl {
196 remoteopts2 : remoteopts2 remoteopts1 nl
199 remoteopts1 : REPOSITORY STRING {
200 remote->repository = $2;
206 remote->protocol = $2;
208 | MIRROR_REFERENCES boolean {
209 remote->mirror_references = $2;
211 | FETCH_ALL_BRANCHES boolean {
212 remote->fetch_all_branches = $2;
221 remote->fetch_ref = $2;
224 static const struct got_error* error;
226 if (remote->fetch_config != NULL) {
227 yyerror("fetch block already exists");
230 error = new_fetch_config(&remote->fetch_config);
232 yyerror("%s", error->msg);
235 } '{' optnl fetchempty '}'
237 static const struct got_error* error;
239 if (remote->send_config != NULL) {
240 yyerror("send block already exists");
243 error = new_send_config(&remote->send_config);
245 yyerror("%s", error->msg);
248 } '{' optnl sendempty '}'
250 fetchempty : /* empty */
253 fetchopts2 : fetchopts2 fetchopts1 nl
256 fetchopts1 : REPOSITORY STRING {
257 remote->fetch_config->repository = $2;
260 remote->fetch_config->server = $2;
263 remote->fetch_config->protocol = $2;
266 remote->fetch_config->port = $2;
269 remote->fetch_config->branch = $2;
272 sendempty : /* empty */
275 sendopts2 : sendopts2 sendopts1 nl
278 sendopts1 : REPOSITORY STRING {
279 remote->send_config->repository = $2;
282 remote->send_config->server = $2;
285 remote->send_config->protocol = $2;
288 remote->send_config->port = $2;
291 remote->send_config->branch = $2;
294 remote : REMOTE STRING {
295 static const struct got_error* error;
297 error = new_remote(&remote);
300 yyerror("%s", error->msg);
304 } '{' optnl remoteopts2 '}' {
305 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
306 gotconfig.nremotes++;
309 author : AUTHOR STRING {
310 gotconfig.author = $2;
313 allowed_signers : ALLOWED_SIGNERS STRING {
314 gotconfig.allowed_signers_file = $2;
317 revoked_signers : REVOKED_SIGNERS STRING {
318 gotconfig.revoked_signers_file = $2;
321 signer_id : SIGNER_ID STRING {
322 gotconfig.signer_id = $2;
341 yyerror(const char *fmt, ...)
348 if (vasprintf(&msg, fmt, ap) == -1) {
349 gerror = got_error_from_errno("vasprintf");
353 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
355 gerror = got_error_from_errno("asprintf");
358 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
363 kw_cmp(const void *k, const void *e)
365 return (strcmp(k, ((const struct keywords *)e)->k_name));
371 /* This has to be sorted always. */
372 static const struct keywords keywords[] = {
373 {"allowed_signers", ALLOWED_SIGNERS},
377 {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
378 {"fetch_all_branches", FETCH_ALL_BRANCHES},
379 {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
380 {"mirror_references", MIRROR_REFERENCES},
382 {"protocol", PROTOCOL},
383 {"reference", REFERENCE},
385 {"repository", REPOSITORY},
386 {"revoked_signers", REVOKED_SIGNERS},
389 {"signer_id", SIGNER_ID},
391 const struct keywords *p;
393 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
394 sizeof(keywords[0]), kw_cmp);
402 #define START_EXPAND 1
403 #define DONE_EXPAND 2
405 static int expanding;
413 if (file->ungetpos > 0)
414 c = file->ungetbuf[--file->ungetpos];
416 c = getc(file->stream);
418 if (c == START_EXPAND)
420 else if (c == DONE_EXPAND)
436 yyerror("reached end of file while parsing "
449 yylval.lineno = file->lineno;
462 if (file->ungetpos >= file->ungetsize) {
463 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
465 err(1, "%s", __func__);
467 file->ungetsize *= 2;
469 file->ungetbuf[file->ungetpos++] = c;
477 /* Skip to either EOF or the first real EOL. */
496 if (atoul(n, &ulval) == 0) {
497 if (ulval == 0 || ulval > 65535) {
498 yyerror("illegal port value %lu", ulval);
503 s = getservbyname(n, "tcp");
505 s = getservbyname(n, "udp");
507 yyerror("unknown port %s", n);
515 parseport(char *port, long long *pn)
517 if ((*pn = getservice(port)) == -1) {
536 while (c == ' ' || c == '\t')
537 c = lgetc(0); /* nothing */
539 yylval.lineno = file->lineno;
542 while (c != '\n' && c != EOF)
543 c = lgetc(0); /* nothing */
545 if (c == '$' && !expanding) {
551 if (p + 1 >= buf + sizeof(buf) - 1) {
552 yyerror("string too long");
555 if (isalnum(c) || c == '_') {
565 yyerror("macro '%s' not defined", buf);
568 p = val + strlen(val) - 1;
569 lungetc(DONE_EXPAND);
571 lungetc((unsigned char)*p);
574 lungetc(START_EXPAND);
589 } else if (c == '\\') {
590 next = lgetc(quotec);
593 if (next == quotec || c == ' ' || c == '\t')
595 else if (next == '\n') {
600 } else if (c == quotec) {
603 } else if (c == '\0') {
604 yyerror("syntax error");
607 if (p + 1 >= buf + sizeof(buf) - 1) {
608 yyerror("string too long");
613 yylval.v.string = strdup(buf);
614 if (yylval.v.string == NULL)
615 err(1, "%s", __func__);
619 #define allowed_to_end_number(x) \
620 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
622 if (c == '-' || isdigit(c)) {
625 if ((size_t)(p-buf) >= sizeof(buf)) {
626 yyerror("string too long");
630 } while (c != EOF && isdigit(c));
632 if (p == buf + 1 && buf[0] == '-')
634 if (c == EOF || allowed_to_end_number(c)) {
635 const char *errstr = NULL;
638 yylval.v.number = strtonum(buf, LLONG_MIN,
641 yyerror("\"%s\" invalid number: %s",
649 lungetc((unsigned char)*--p);
650 c = (unsigned char)*--p;
656 #define allowed_in_string(x) \
657 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
658 x != '{' && x != '}' && \
659 x != '!' && x != '=' && x != '#' && \
662 if (isalnum(c) || c == ':' || c == '_') {
665 if ((size_t)(p-buf) >= sizeof(buf)) {
666 yyerror("string too long");
670 } while (c != EOF && (allowed_in_string(c)));
674 if (token == STRING) {
675 yylval.v.string = strdup(buf);
676 if (yylval.v.string == NULL)
677 err(1, "%s", __func__);
682 yylval.lineno = file->lineno;
690 static const struct got_error*
691 newfile(struct file **nfile, const char *filename, int *fd)
693 const struct got_error* error = NULL;
695 (*nfile) = calloc(1, sizeof(struct file));
696 if ((*nfile) == NULL)
697 return got_error_from_errno("calloc");
698 (*nfile)->stream = fdopen(*fd, "r");
699 if ((*nfile)->stream == NULL) {
700 error = got_error_from_errno("fdopen");
704 *fd = -1; /* Stream owns the file descriptor now. */
705 (*nfile)->name = filename;
706 (*nfile)->lineno = 1;
707 (*nfile)->ungetsize = 16;
708 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
709 if ((*nfile)->ungetbuf == NULL) {
710 error = got_error_from_errno("malloc");
711 fclose((*nfile)->stream);
718 static const struct got_error*
719 new_remote(struct gotconfig_remote_repo **remote)
721 const struct got_error *error = NULL;
723 *remote = calloc(1, sizeof(**remote));
725 error = got_error_from_errno("calloc");
729 static const struct got_error*
730 new_fetch_config(struct fetch_config **fetch_config)
732 const struct got_error *error = NULL;
734 *fetch_config = calloc(1, sizeof(**fetch_config));
735 if (*fetch_config == NULL)
736 error = got_error_from_errno("calloc");
740 static const struct got_error*
741 new_send_config(struct send_config **send_config)
743 const struct got_error *error = NULL;
745 *send_config = calloc(1, sizeof(**send_config));
746 if (*send_config == NULL)
747 error = got_error_from_errno("calloc");
752 closefile(struct file *file)
754 fclose(file->stream);
755 free(file->ungetbuf);
759 const struct got_error *
760 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
762 const struct got_error *err = NULL;
763 struct sym *sym, *next;
767 err = newfile(&file, filename, fd);
771 TAILQ_INIT(&gotconfig.remotes);
776 /* Free macros and check which have not been used. */
777 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
781 TAILQ_REMOVE(&symhead, sym, entry);
792 free_fetch_config(struct fetch_config *fetch_config)
794 free(remote->fetch_config->repository);
795 free(remote->fetch_config->server);
796 free(remote->fetch_config->protocol);
797 free(remote->fetch_config);
801 free_send_config(struct send_config *send_config)
803 free(remote->send_config->repository);
804 free(remote->send_config->server);
805 free(remote->send_config->protocol);
806 free(remote->send_config);
810 gotconfig_free(struct gotconfig *conf)
812 struct gotconfig_remote_repo *remote;
815 free(conf->allowed_signers_file);
816 free(conf->revoked_signers_file);
817 free(conf->signer_id);
818 while (!TAILQ_EMPTY(&conf->remotes)) {
819 remote = TAILQ_FIRST(&conf->remotes);
820 TAILQ_REMOVE(&conf->remotes, remote, entry);
821 if (remote->fetch_config != NULL)
822 free_fetch_config(remote->fetch_config);
823 if (remote->send_config != NULL)
824 free_send_config(remote->send_config);
826 free(remote->repository);
827 free(remote->server);
828 free(remote->protocol);
834 symset(const char *nam, const char *val, int persist)
838 TAILQ_FOREACH(sym, &symhead, entry) {
839 if (strcmp(nam, sym->nam) == 0)
844 if (sym->persist == 1)
849 TAILQ_REMOVE(&symhead, sym, entry);
853 sym = calloc(1, sizeof(*sym));
857 sym->nam = strdup(nam);
858 if (sym->nam == NULL) {
862 sym->val = strdup(val);
863 if (sym->val == NULL) {
869 sym->persist = persist;
870 TAILQ_INSERT_TAIL(&symhead, sym, entry);
875 cmdline_symset(char *s)
881 val = strrchr(s, '=');
885 len = strlen(s) - strlen(val) + 1;
888 errx(1, "cmdline_symset: malloc");
890 strlcpy(sym, s, len);
892 ret = symset(sym, val + 1, 1);
899 symget(const char *nam)
903 TAILQ_FOREACH(sym, &symhead, entry) {
904 if (strcmp(nam, sym->nam) == 0) {
913 atoul(char *s, u_long *ulvalp)
919 ulval = strtoul(s, &ep, 0);
920 if (s[0] == '\0' || *ep != '\0')
922 if (errno == ERANGE && ulval == ULONG_MAX)