Blob


1 /*
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.
10 *
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.
14 *
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.
22 */
24 %{
25 #include <sys/types.h>
26 #include <sys/queue.h>
28 #include <netdb.h>
30 #include <ctype.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "got_error.h"
40 #include "gotconfig.h"
42 static struct file {
43 FILE *stream;
44 const char *name;
45 size_t ungetpos;
46 size_t ungetsize;
47 u_char *ungetbuf;
48 int eof_reached;
49 int lineno;
50 } *file;
51 static const struct got_error* newfile(struct file**, const char *, int *);
52 static void closefile(struct file *);
53 int yyparse(void);
54 int yylex(void);
55 int yyerror(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
59 int lookup(char *);
60 int igetc(void);
61 int lgetc(int);
62 void lungetc(int);
63 int findeol(void);
64 static int parseport(char *, long long *);
66 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
67 struct sym {
68 TAILQ_ENTRY(sym) entry;
69 int used;
70 int persist;
71 char *nam;
72 char *val;
73 };
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 **);
88 typedef struct {
89 union {
90 long long number;
91 char *string;
92 struct node_branch *branch;
93 struct node_ref *ref;
94 } v;
95 int lineno;
96 } YYSTYPE;
98 %}
100 %token ERROR
101 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
102 %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS FETCH_ALL_BRANCHES REFERENCE
103 %token 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
111 %%
113 grammar : /* empty */
114 | grammar '\n'
115 | grammar author '\n'
116 | grammar remote '\n'
117 | grammar allowed_signers '\n'
119 boolean : STRING {
120 if (strcasecmp($1, "true") == 0 ||
121 strcasecmp($1, "yes") == 0)
122 $$ = 1;
123 else if (strcasecmp($1, "false") == 0 ||
124 strcasecmp($1, "no") == 0)
125 $$ = 0;
126 else {
127 yyerror("invalid boolean value '%s'", $1);
128 free($1);
129 YYERROR;
131 free($1);
134 numberstring : NUMBER {
135 char *s;
136 if (asprintf(&s, "%lld", $1) == -1) {
137 yyerror("string: asprintf");
138 YYERROR;
140 $$ = s;
142 | STRING
144 portplain : numberstring {
145 if (parseport($1, &$$) == -1) {
146 free($1);
147 YYERROR;
149 free($1);
152 branch : /* empty */ { $$ = NULL; }
153 | xbranch { $$ = $1; }
154 | '{' optnl branch_list '}' { $$ = $3; }
156 xbranch : STRING {
157 $$ = calloc(1, sizeof(struct node_branch));
158 if ($$ == NULL) {
159 yyerror("calloc");
160 YYERROR;
162 $$->branch_name = $1;
163 $$->tail = $$;
166 branch_list : xbranch optnl { $$ = $1; }
167 | branch_list comma xbranch optnl {
168 $1->tail->next = $3;
169 $1->tail = $3;
170 $$ = $1;
173 ref : /* empty */ { $$ = NULL; }
174 | xref { $$ = $1; }
175 | '{' optnl ref_list '}' { $$ = $3; }
177 xref : STRING {
178 $$ = calloc(1, sizeof(struct node_ref));
179 if ($$ == NULL) {
180 yyerror("calloc");
181 YYERROR;
183 $$->ref_name = $1;
184 $$->tail = $$;
187 ref_list : xref optnl { $$ = $1; }
188 | ref_list comma xref optnl {
189 $1->tail->next = $3;
190 $1->tail = $3;
191 $$ = $1;
194 remoteopts2 : remoteopts2 remoteopts1 nl
195 | remoteopts1 optnl
197 remoteopts1 : REPOSITORY STRING {
198 remote->repository = $2;
200 | SERVER STRING {
201 remote->server = $2;
203 | PROTOCOL STRING {
204 remote->protocol = $2;
206 | MIRROR_REFERENCES boolean {
207 remote->mirror_references = $2;
209 | FETCH_ALL_BRANCHES boolean {
210 remote->fetch_all_branches = $2;
212 | PORT portplain {
213 remote->port = $2;
215 | BRANCH branch {
216 remote->branch = $2;
218 | REFERENCE ref {
219 remote->fetch_ref = $2;
221 | FETCH {
222 static const struct got_error* error;
224 if (remote->fetch_config != NULL) {
225 yyerror("fetch block already exists");
226 YYERROR;
228 error = new_fetch_config(&remote->fetch_config);
229 if (error) {
230 yyerror("%s", error->msg);
231 YYERROR;
233 } '{' optnl fetchempty '}'
234 | SEND {
235 static const struct got_error* error;
237 if (remote->send_config != NULL) {
238 yyerror("send block already exists");
239 YYERROR;
241 error = new_send_config(&remote->send_config);
242 if (error) {
243 yyerror("%s", error->msg);
244 YYERROR;
246 } '{' optnl sendempty '}'
248 fetchempty : /* empty */
249 | fetchopts2
251 fetchopts2 : fetchopts2 fetchopts1 nl
252 | fetchopts1 optnl
254 fetchopts1 : REPOSITORY STRING {
255 remote->fetch_config->repository = $2;
257 | SERVER STRING {
258 remote->fetch_config->server = $2;
260 | PROTOCOL STRING {
261 remote->fetch_config->protocol = $2;
263 | PORT portplain {
264 remote->fetch_config->port = $2;
266 | BRANCH branch {
267 remote->fetch_config->branch = $2;
270 sendempty : /* empty */
271 | sendopts2
273 sendopts2 : sendopts2 sendopts1 nl
274 | sendopts1 optnl
276 sendopts1 : REPOSITORY STRING {
277 remote->send_config->repository = $2;
279 | SERVER STRING {
280 remote->send_config->server = $2;
282 | PROTOCOL STRING {
283 remote->send_config->protocol = $2;
285 | PORT portplain {
286 remote->send_config->port = $2;
288 | BRANCH branch {
289 remote->send_config->branch = $2;
292 remote : REMOTE STRING {
293 static const struct got_error* error;
295 error = new_remote(&remote);
296 if (error) {
297 free($2);
298 yyerror("%s", error->msg);
299 YYERROR;
301 remote->name = $2;
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;
319 optnl : '\n' optnl
320 | /* empty */
322 nl : '\n' optnl
324 comma : ','
325 | /* empty */
327 %%
329 struct keywords {
330 const char *k_name;
331 int k_val;
332 };
334 int
335 yyerror(const char *fmt, ...)
337 va_list ap;
338 char *msg;
339 char *err = NULL;
341 va_start(ap, fmt);
342 if (vasprintf(&msg, fmt, ap) == -1) {
343 gerror = got_error_from_errno("vasprintf");
344 return 0;
346 va_end(ap);
347 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
348 msg) == -1) {
349 gerror = got_error_from_errno("asprintf");
350 return(0);
352 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
353 free(msg);
354 return(0);
356 int
357 kw_cmp(const void *k, const void *e)
359 return (strcmp(k, ((const struct keywords *)e)->k_name));
362 int
363 lookup(char *s)
365 /* This has to be sorted always. */
366 static const struct keywords keywords[] = {
367 {"allowed_signers", ALLOWED_SIGNERS},
368 {"author", AUTHOR},
369 {"branch", BRANCH},
370 {"fetch", FETCH},
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},
375 {"port", PORT},
376 {"protocol", PROTOCOL},
377 {"reference", REFERENCE},
378 {"remote", REMOTE},
379 {"repository", REPOSITORY},
380 {"revoked_signers", REVOKED_SIGNERS},
381 {"send", SEND},
382 {"server", SERVER},
383 };
384 const struct keywords *p;
386 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
387 sizeof(keywords[0]), kw_cmp);
389 if (p)
390 return (p->k_val);
391 else
392 return (STRING);
395 #define START_EXPAND 1
396 #define DONE_EXPAND 2
398 static int expanding;
400 int
401 igetc(void)
403 int c;
405 while (1) {
406 if (file->ungetpos > 0)
407 c = file->ungetbuf[--file->ungetpos];
408 else
409 c = getc(file->stream);
411 if (c == START_EXPAND)
412 expanding = 1;
413 else if (c == DONE_EXPAND)
414 expanding = 0;
415 else
416 break;
418 return (c);
421 int
422 lgetc(int quotec)
424 int c, next;
426 if (quotec) {
427 c = igetc();
428 if (c == EOF) {
429 yyerror("reached end of file while parsing "
430 "quoted string");
432 return (c);
435 c = igetc();
436 while (c == '\\') {
437 next = igetc();
438 if (next != '\n') {
439 c = next;
440 break;
442 yylval.lineno = file->lineno;
443 file->lineno++;
446 return (c);
449 void
450 lungetc(int c)
452 if (c == EOF)
453 return;
455 if (file->ungetpos >= file->ungetsize) {
456 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
457 if (p == NULL)
458 err(1, "%s", __func__);
459 file->ungetbuf = p;
460 file->ungetsize *= 2;
462 file->ungetbuf[file->ungetpos++] = c;
465 int
466 findeol(void)
468 int c;
470 /* Skip to either EOF or the first real EOL. */
471 while (1) {
472 c = lgetc(0);
473 if (c == '\n') {
474 file->lineno++;
475 break;
477 if (c == EOF)
478 break;
480 return (ERROR);
483 static long long
484 getservice(char *n)
486 struct servent *s;
487 u_long ulval;
489 if (atoul(n, &ulval) == 0) {
490 if (ulval == 0 || ulval > 65535) {
491 yyerror("illegal port value %lu", ulval);
492 return (-1);
494 return ulval;
495 } else {
496 s = getservbyname(n, "tcp");
497 if (s == NULL)
498 s = getservbyname(n, "udp");
499 if (s == NULL) {
500 yyerror("unknown port %s", n);
501 return (-1);
503 return (s->s_port);
507 static int
508 parseport(char *port, long long *pn)
510 if ((*pn = getservice(port)) == -1) {
511 *pn = 0LL;
512 return (-1);
514 return (0);
518 int
519 yylex(void)
521 char buf[8096];
522 char *p, *val;
523 int quotec, next, c;
524 int token;
526 top:
527 p = buf;
528 c = lgetc(0);
529 while (c == ' ' || c == '\t')
530 c = lgetc(0); /* nothing */
532 yylval.lineno = file->lineno;
533 if (c == '#') {
534 c = lgetc(0);
535 while (c != '\n' && c != EOF)
536 c = lgetc(0); /* nothing */
538 if (c == '$' && !expanding) {
539 while (1) {
540 c = lgetc(0);
541 if (c == EOF)
542 return (0);
544 if (p + 1 >= buf + sizeof(buf) - 1) {
545 yyerror("string too long");
546 return (findeol());
548 if (isalnum(c) || c == '_') {
549 *p++ = c;
550 continue;
552 *p = '\0';
553 lungetc(c);
554 break;
556 val = symget(buf);
557 if (val == NULL) {
558 yyerror("macro '%s' not defined", buf);
559 return (findeol());
561 p = val + strlen(val) - 1;
562 lungetc(DONE_EXPAND);
563 while (p >= val) {
564 lungetc((unsigned char)*p);
565 p--;
567 lungetc(START_EXPAND);
568 goto top;
571 switch (c) {
572 case '\'':
573 case '"':
574 quotec = c;
575 while (1) {
576 c = lgetc(quotec);
577 if (c == EOF)
578 return (0);
579 if (c == '\n') {
580 file->lineno++;
581 continue;
582 } else if (c == '\\') {
583 next = lgetc(quotec);
584 if (next == EOF)
585 return (0);
586 if (next == quotec || c == ' ' || c == '\t')
587 c = next;
588 else if (next == '\n') {
589 file->lineno++;
590 continue;
591 } else
592 lungetc(next);
593 } else if (c == quotec) {
594 *p = '\0';
595 break;
596 } else if (c == '\0') {
597 yyerror("syntax error");
598 return (findeol());
600 if (p + 1 >= buf + sizeof(buf) - 1) {
601 yyerror("string too long");
602 return (findeol());
604 *p++ = c;
606 yylval.v.string = strdup(buf);
607 if (yylval.v.string == NULL)
608 err(1, "%s", __func__);
609 return (STRING);
612 #define allowed_to_end_number(x) \
613 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
615 if (c == '-' || isdigit(c)) {
616 do {
617 *p++ = c;
618 if ((size_t)(p-buf) >= sizeof(buf)) {
619 yyerror("string too long");
620 return (findeol());
622 c = lgetc(0);
623 } while (c != EOF && isdigit(c));
624 lungetc(c);
625 if (p == buf + 1 && buf[0] == '-')
626 goto nodigits;
627 if (c == EOF || allowed_to_end_number(c)) {
628 const char *errstr = NULL;
630 *p = '\0';
631 yylval.v.number = strtonum(buf, LLONG_MIN,
632 LLONG_MAX, &errstr);
633 if (errstr) {
634 yyerror("\"%s\" invalid number: %s",
635 buf, errstr);
636 return (findeol());
638 return (NUMBER);
639 } else {
640 nodigits:
641 while (p > buf + 1)
642 lungetc((unsigned char)*--p);
643 c = (unsigned char)*--p;
644 if (c == '-')
645 return (c);
649 #define allowed_in_string(x) \
650 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
651 x != '{' && x != '}' && \
652 x != '!' && x != '=' && x != '#' && \
653 x != ','))
655 if (isalnum(c) || c == ':' || c == '_') {
656 do {
657 *p++ = c;
658 if ((size_t)(p-buf) >= sizeof(buf)) {
659 yyerror("string too long");
660 return (findeol());
662 c = lgetc(0);
663 } while (c != EOF && (allowed_in_string(c)));
664 lungetc(c);
665 *p = '\0';
666 token = lookup(buf);
667 if (token == STRING) {
668 yylval.v.string = strdup(buf);
669 if (yylval.v.string == NULL)
670 err(1, "%s", __func__);
672 return (token);
674 if (c == '\n') {
675 yylval.lineno = file->lineno;
676 file->lineno++;
678 if (c == EOF)
679 return (0);
680 return (c);
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");
694 free((*nfile));
695 return error;
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);
705 free((*nfile));
706 return error;
708 return NULL;
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));
717 if (*remote == NULL)
718 error = got_error_from_errno("calloc");
719 return error;
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");
730 return error;
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");
741 return error;
744 static void
745 closefile(struct file *file)
747 fclose(file->stream);
748 free(file->ungetbuf);
749 free(file);
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;
758 *conf = NULL;
760 err = newfile(&file, filename, fd);
761 if (err)
762 return err;
764 TAILQ_INIT(&gotconfig.remotes);
766 yyparse();
767 closefile(file);
769 /* Free macros and check which have not been used. */
770 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
771 if (!sym->persist) {
772 free(sym->nam);
773 free(sym->val);
774 TAILQ_REMOVE(&symhead, sym, entry);
775 free(sym);
779 if (gerror == NULL)
780 *conf = &gotconfig;
781 return gerror;
784 static void
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);
793 static void
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);
802 void
803 gotconfig_free(struct gotconfig *conf)
805 struct gotconfig_remote_repo *remote;
807 free(conf->author);
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);
817 free(remote->name);
818 free(remote->repository);
819 free(remote->server);
820 free(remote->protocol);
821 free(remote);
825 int
826 symset(const char *nam, const char *val, int persist)
828 struct sym *sym;
830 TAILQ_FOREACH(sym, &symhead, entry) {
831 if (strcmp(nam, sym->nam) == 0)
832 break;
835 if (sym != NULL) {
836 if (sym->persist == 1)
837 return (0);
838 else {
839 free(sym->nam);
840 free(sym->val);
841 TAILQ_REMOVE(&symhead, sym, entry);
842 free(sym);
845 sym = calloc(1, sizeof(*sym));
846 if (sym == NULL)
847 return (-1);
849 sym->nam = strdup(nam);
850 if (sym->nam == NULL) {
851 free(sym);
852 return (-1);
854 sym->val = strdup(val);
855 if (sym->val == NULL) {
856 free(sym->nam);
857 free(sym);
858 return (-1);
860 sym->used = 0;
861 sym->persist = persist;
862 TAILQ_INSERT_TAIL(&symhead, sym, entry);
863 return (0);
866 int
867 cmdline_symset(char *s)
869 char *sym, *val;
870 int ret;
871 size_t len;
873 val = strrchr(s, '=');
874 if (val == NULL)
875 return (-1);
877 len = strlen(s) - strlen(val) + 1;
878 sym = malloc(len);
879 if (sym == NULL)
880 errx(1, "cmdline_symset: malloc");
882 strlcpy(sym, s, len);
884 ret = symset(sym, val + 1, 1);
885 free(sym);
887 return (ret);
890 char *
891 symget(const char *nam)
893 struct sym *sym;
895 TAILQ_FOREACH(sym, &symhead, entry) {
896 if (strcmp(nam, sym->nam) == 0) {
897 sym->used = 1;
898 return (sym->val);
901 return (NULL);
904 static int
905 atoul(char *s, u_long *ulvalp)
907 u_long ulval;
908 char *ep;
910 errno = 0;
911 ulval = strtoul(s, &ep, 0);
912 if (s[0] == '\0' || *ep != '\0')
913 return (-1);
914 if (errno == ERANGE && ulval == ULONG_MAX)
915 return (-1);
916 *ulvalp = ulval;
917 return (0);