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>
38 #include "got_compat.h"
40 #include "got_error.h"
41 #include "gotconfig.h"
52 static const struct got_error* newfile(struct file**, const char *, int *);
53 static void closefile(struct file *);
56 int yyerror(const char *, ...)
57 __attribute__((__format__ (printf, 1, 2)))
58 __attribute__((__nonnull__ (1)));
59 int kw_cmp(const void *, const void *);
65 static int parseport(char *, long long *);
67 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
69 TAILQ_ENTRY(sym) entry;
76 int symset(const char *, const char *, int);
77 int cmdline_symset(char *);
78 char *symget(const char *);
80 static int atoul(char *, u_long *);
82 static const struct got_error* gerror;
83 static struct gotconfig_remote_repo *remote;
84 static struct gotconfig gotconfig;
85 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
86 static const struct got_error* new_fetch_config(struct fetch_config **);
87 static const struct got_error* new_send_config(struct send_config **);
93 struct node_branch *branch;
99 #if defined(__APPLE__) && !defined(YYSTYPE)
100 #warning "Setting YYSTYPE - is GNU Bison installed?"
101 #define YYSTYPE YYSTYPE
106 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
107 %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS SIGNER_ID FETCH_ALL_BRANCHES
108 %token REFERENCE FETCH SEND
109 %token <v.string> STRING
110 %token <v.number> NUMBER
111 %type <v.number> boolean portplain
112 %type <v.string> numberstring
113 %type <v.branch> branch xbranch branch_list
114 %type <v.ref> ref xref ref_list
118 grammar : /* empty */
120 | grammar author '\n'
121 | grammar remote '\n'
122 | grammar allowed_signers '\n'
123 | grammar revoked_signers '\n'
124 | grammar signer_id '\n'
127 if (strcasecmp($1, "true") == 0 ||
128 strcasecmp($1, "yes") == 0)
130 else if (strcasecmp($1, "false") == 0 ||
131 strcasecmp($1, "no") == 0)
134 yyerror("invalid boolean value '%s'", $1);
141 numberstring : NUMBER {
143 if (asprintf(&s, "%lld", $1) == -1) {
144 yyerror("string: asprintf");
151 portplain : numberstring {
152 if (parseport($1, &$$) == -1) {
159 branch : /* empty */ { $$ = NULL; }
160 | xbranch { $$ = $1; }
161 | '{' optnl branch_list '}' { $$ = $3; }
164 $$ = calloc(1, sizeof(struct node_branch));
169 $$->branch_name = $1;
173 branch_list : xbranch optnl { $$ = $1; }
174 | branch_list comma xbranch optnl {
180 ref : /* empty */ { $$ = NULL; }
182 | '{' optnl ref_list '}' { $$ = $3; }
185 $$ = calloc(1, sizeof(struct node_ref));
194 ref_list : xref optnl { $$ = $1; }
195 | ref_list comma xref optnl {
201 remoteopts2 : remoteopts2 remoteopts1 nl
204 remoteopts1 : REPOSITORY STRING {
205 remote->repository = $2;
211 remote->protocol = $2;
213 | MIRROR_REFERENCES boolean {
214 remote->mirror_references = $2;
216 | FETCH_ALL_BRANCHES boolean {
217 remote->fetch_all_branches = $2;
226 remote->fetch_ref = $2;
229 static const struct got_error* error;
231 if (remote->fetch_config != NULL) {
232 yyerror("fetch block already exists");
235 error = new_fetch_config(&remote->fetch_config);
237 yyerror("%s", error->msg);
240 } '{' optnl fetchempty '}'
242 static const struct got_error* error;
244 if (remote->send_config != NULL) {
245 yyerror("send block already exists");
248 error = new_send_config(&remote->send_config);
250 yyerror("%s", error->msg);
253 } '{' optnl sendempty '}'
255 fetchempty : /* empty */
258 fetchopts2 : fetchopts2 fetchopts1 nl
261 fetchopts1 : REPOSITORY STRING {
262 remote->fetch_config->repository = $2;
265 remote->fetch_config->server = $2;
268 remote->fetch_config->protocol = $2;
271 remote->fetch_config->port = $2;
274 remote->fetch_config->branch = $2;
277 sendempty : /* empty */
280 sendopts2 : sendopts2 sendopts1 nl
283 sendopts1 : REPOSITORY STRING {
284 remote->send_config->repository = $2;
287 remote->send_config->server = $2;
290 remote->send_config->protocol = $2;
293 remote->send_config->port = $2;
296 remote->send_config->branch = $2;
299 remote : REMOTE STRING {
300 static const struct got_error* error;
302 error = new_remote(&remote);
305 yyerror("%s", error->msg);
309 } '{' optnl remoteopts2 '}' {
310 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
311 gotconfig.nremotes++;
314 author : AUTHOR STRING {
315 gotconfig.author = $2;
318 allowed_signers : ALLOWED_SIGNERS STRING {
319 gotconfig.allowed_signers_file = $2;
322 revoked_signers : REVOKED_SIGNERS STRING {
323 gotconfig.revoked_signers_file = $2;
326 signer_id : SIGNER_ID STRING {
327 gotconfig.signer_id = $2;
346 yyerror(const char *fmt, ...)
353 if (vasprintf(&msg, fmt, ap) == -1) {
354 gerror = got_error_from_errno("vasprintf");
358 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
360 gerror = got_error_from_errno("asprintf");
363 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
368 kw_cmp(const void *k, const void *e)
370 return (strcmp(k, ((const struct keywords *)e)->k_name));
376 /* This has to be sorted always. */
377 static const struct keywords keywords[] = {
378 {"allowed_signers", ALLOWED_SIGNERS},
382 {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
383 {"fetch_all_branches", FETCH_ALL_BRANCHES},
384 {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
385 {"mirror_references", MIRROR_REFERENCES},
387 {"protocol", PROTOCOL},
388 {"reference", REFERENCE},
390 {"repository", REPOSITORY},
391 {"revoked_signers", REVOKED_SIGNERS},
394 {"signer_id", SIGNER_ID},
396 const struct keywords *p;
398 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
399 sizeof(keywords[0]), kw_cmp);
407 #define START_EXPAND 1
408 #define DONE_EXPAND 2
410 static int expanding;
418 if (file->ungetpos > 0)
419 c = file->ungetbuf[--file->ungetpos];
421 c = getc(file->stream);
423 if (c == START_EXPAND)
425 else if (c == DONE_EXPAND)
441 yyerror("reached end of file while parsing "
454 yylval.lineno = file->lineno;
467 if (file->ungetpos >= file->ungetsize) {
468 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
470 err(1, "%s", __func__);
472 file->ungetsize *= 2;
474 file->ungetbuf[file->ungetpos++] = c;
482 /* Skip to either EOF or the first real EOL. */
501 if (atoul(n, &ulval) == 0) {
502 if (ulval == 0 || ulval > 65535) {
503 yyerror("illegal port value %lu", ulval);
508 s = getservbyname(n, "tcp");
510 s = getservbyname(n, "udp");
512 yyerror("unknown port %s", n);
520 parseport(char *port, long long *pn)
522 if ((*pn = getservice(port)) == -1) {
541 while (c == ' ' || c == '\t')
542 c = lgetc(0); /* nothing */
544 yylval.lineno = file->lineno;
547 while (c != '\n' && c != EOF)
548 c = lgetc(0); /* nothing */
550 if (c == '$' && !expanding) {
556 if (p + 1 >= buf + sizeof(buf) - 1) {
557 yyerror("string too long");
560 if (isalnum(c) || c == '_') {
570 yyerror("macro '%s' not defined", buf);
573 p = val + strlen(val) - 1;
574 lungetc(DONE_EXPAND);
576 lungetc((unsigned char)*p);
579 lungetc(START_EXPAND);
594 } else if (c == '\\') {
595 next = lgetc(quotec);
598 if (next == quotec || c == ' ' || c == '\t')
600 else if (next == '\n') {
605 } else if (c == quotec) {
608 } else if (c == '\0') {
609 yyerror("syntax error");
612 if (p + 1 >= buf + sizeof(buf) - 1) {
613 yyerror("string too long");
618 yylval.v.string = strdup(buf);
619 if (yylval.v.string == NULL)
620 err(1, "%s", __func__);
624 #define allowed_to_end_number(x) \
625 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
627 if (c == '-' || isdigit(c)) {
630 if ((size_t)(p-buf) >= sizeof(buf)) {
631 yyerror("string too long");
635 } while (c != EOF && isdigit(c));
637 if (p == buf + 1 && buf[0] == '-')
639 if (c == EOF || allowed_to_end_number(c)) {
640 const char *errstr = NULL;
643 yylval.v.number = strtonum(buf, LLONG_MIN,
646 yyerror("\"%s\" invalid number: %s",
654 lungetc((unsigned char)*--p);
655 c = (unsigned char)*--p;
661 #define allowed_in_string(x) \
662 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
663 x != '{' && x != '}' && \
664 x != '!' && x != '=' && x != '#' && \
667 if (isalnum(c) || c == ':' || c == '_') {
670 if ((size_t)(p-buf) >= sizeof(buf)) {
671 yyerror("string too long");
675 } while (c != EOF && (allowed_in_string(c)));
679 if (token == STRING) {
680 yylval.v.string = strdup(buf);
681 if (yylval.v.string == NULL)
682 err(1, "%s", __func__);
687 yylval.lineno = file->lineno;
695 static const struct got_error*
696 newfile(struct file **nfile, const char *filename, int *fd)
698 const struct got_error* error = NULL;
700 (*nfile) = calloc(1, sizeof(struct file));
701 if ((*nfile) == NULL)
702 return got_error_from_errno("calloc");
703 (*nfile)->stream = fdopen(*fd, "r");
704 if ((*nfile)->stream == NULL) {
705 error = got_error_from_errno("fdopen");
709 *fd = -1; /* Stream owns the file descriptor now. */
710 (*nfile)->name = filename;
711 (*nfile)->lineno = 1;
712 (*nfile)->ungetsize = 16;
713 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
714 if ((*nfile)->ungetbuf == NULL) {
715 error = got_error_from_errno("malloc");
716 fclose((*nfile)->stream);
723 static const struct got_error*
724 new_remote(struct gotconfig_remote_repo **remote)
726 const struct got_error *error = NULL;
728 *remote = calloc(1, sizeof(**remote));
730 error = got_error_from_errno("calloc");
734 static const struct got_error*
735 new_fetch_config(struct fetch_config **fetch_config)
737 const struct got_error *error = NULL;
739 *fetch_config = calloc(1, sizeof(**fetch_config));
740 if (*fetch_config == NULL)
741 error = got_error_from_errno("calloc");
745 static const struct got_error*
746 new_send_config(struct send_config **send_config)
748 const struct got_error *error = NULL;
750 *send_config = calloc(1, sizeof(**send_config));
751 if (*send_config == NULL)
752 error = got_error_from_errno("calloc");
757 closefile(struct file *file)
759 fclose(file->stream);
760 free(file->ungetbuf);
764 const struct got_error *
765 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
767 const struct got_error *err = NULL;
768 struct sym *sym, *next;
772 err = newfile(&file, filename, fd);
776 TAILQ_INIT(&gotconfig.remotes);
781 /* Free macros and check which have not been used. */
782 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
786 TAILQ_REMOVE(&symhead, sym, entry);
797 free_fetch_config(struct fetch_config *fetch_config)
799 free(remote->fetch_config->repository);
800 free(remote->fetch_config->server);
801 free(remote->fetch_config->protocol);
802 free(remote->fetch_config);
806 free_send_config(struct send_config *send_config)
808 free(remote->send_config->repository);
809 free(remote->send_config->server);
810 free(remote->send_config->protocol);
811 free(remote->send_config);
815 gotconfig_free(struct gotconfig *conf)
817 struct gotconfig_remote_repo *remote;
820 free(conf->allowed_signers_file);
821 free(conf->revoked_signers_file);
822 free(conf->signer_id);
823 while (!TAILQ_EMPTY(&conf->remotes)) {
824 remote = TAILQ_FIRST(&conf->remotes);
825 TAILQ_REMOVE(&conf->remotes, remote, entry);
826 if (remote->fetch_config != NULL)
827 free_fetch_config(remote->fetch_config);
828 if (remote->send_config != NULL)
829 free_send_config(remote->send_config);
831 free(remote->repository);
832 free(remote->server);
833 free(remote->protocol);
839 symset(const char *nam, const char *val, int persist)
843 TAILQ_FOREACH(sym, &symhead, entry) {
844 if (strcmp(nam, sym->nam) == 0)
849 if (sym->persist == 1)
854 TAILQ_REMOVE(&symhead, sym, entry);
858 sym = calloc(1, sizeof(*sym));
862 sym->nam = strdup(nam);
863 if (sym->nam == NULL) {
867 sym->val = strdup(val);
868 if (sym->val == NULL) {
874 sym->persist = persist;
875 TAILQ_INSERT_TAIL(&symhead, sym, entry);
880 cmdline_symset(char *s)
886 val = strrchr(s, '=');
890 len = strlen(s) - strlen(val) + 1;
893 errx(1, "cmdline_symset: malloc");
895 strlcpy(sym, s, len);
897 ret = symset(sym, val + 1, 1);
904 symget(const char *nam)
908 TAILQ_FOREACH(sym, &symhead, entry) {
909 if (strcmp(nam, sym->nam) == 0) {
918 atoul(char *s, u_long *ulvalp)
924 ulval = strtoul(s, &ep, 0);
925 if (s[0] == '\0' || *ep != '\0')
927 if (errno == ERANGE && ulval == ULONG_MAX)