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>
27 #include <netdb.h>
29 #include <ctype.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "got_compat.h"
40 #include "got_error.h"
41 #include "gotconfig.h"
43 static struct file {
44 FILE *stream;
45 const char *name;
46 size_t ungetpos;
47 size_t ungetsize;
48 u_char *ungetbuf;
49 int eof_reached;
50 int lineno;
51 } *file;
52 static const struct got_error* newfile(struct file**, const char *, int *);
53 static void closefile(struct file *);
54 int yyparse(void);
55 int yylex(void);
56 int yyerror(const char *, ...)
57 __attribute__((__format__ (printf, 1, 2)))
58 __attribute__((__nonnull__ (1)));
59 int kw_cmp(const void *, const void *);
60 int lookup(char *);
61 int igetc(void);
62 int lgetc(int);
63 void lungetc(int);
64 int findeol(void);
65 static int parseport(char *, long long *);
67 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
68 struct sym {
69 TAILQ_ENTRY(sym) entry;
70 int used;
71 int persist;
72 char *nam;
73 char *val;
74 };
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 **);
89 typedef struct {
90 union {
91 long long number;
92 char *string;
93 struct node_branch *branch;
94 struct node_ref *ref;
95 } v;
96 int lineno;
97 } YYSTYPE;
99 #if defined(__APPLE__) && !defined(YYSTYPE)
100 #warning "Setting YYSTYPE - is GNU Bison installed?"
101 #define YYSTYPE YYSTYPE
102 #endif
103 %}
105 %token ERROR
106 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
107 %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS FETCH_ALL_BRANCHES REFERENCE
108 %token 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
116 %%
118 grammar : /* empty */
119 | grammar '\n'
120 | grammar author '\n'
121 | grammar remote '\n'
122 | grammar allowed_signers '\n'
124 boolean : STRING {
125 if (strcasecmp($1, "true") == 0 ||
126 strcasecmp($1, "yes") == 0)
127 $$ = 1;
128 else if (strcasecmp($1, "false") == 0 ||
129 strcasecmp($1, "no") == 0)
130 $$ = 0;
131 else {
132 yyerror("invalid boolean value '%s'", $1);
133 free($1);
134 YYERROR;
136 free($1);
139 numberstring : NUMBER {
140 char *s;
141 if (asprintf(&s, "%lld", $1) == -1) {
142 yyerror("string: asprintf");
143 YYERROR;
145 $$ = s;
147 | STRING
149 portplain : numberstring {
150 if (parseport($1, &$$) == -1) {
151 free($1);
152 YYERROR;
154 free($1);
157 branch : /* empty */ { $$ = NULL; }
158 | xbranch { $$ = $1; }
159 | '{' optnl branch_list '}' { $$ = $3; }
161 xbranch : STRING {
162 $$ = calloc(1, sizeof(struct node_branch));
163 if ($$ == NULL) {
164 yyerror("calloc");
165 YYERROR;
167 $$->branch_name = $1;
168 $$->tail = $$;
171 branch_list : xbranch optnl { $$ = $1; }
172 | branch_list comma xbranch optnl {
173 $1->tail->next = $3;
174 $1->tail = $3;
175 $$ = $1;
178 ref : /* empty */ { $$ = NULL; }
179 | xref { $$ = $1; }
180 | '{' optnl ref_list '}' { $$ = $3; }
182 xref : STRING {
183 $$ = calloc(1, sizeof(struct node_ref));
184 if ($$ == NULL) {
185 yyerror("calloc");
186 YYERROR;
188 $$->ref_name = $1;
189 $$->tail = $$;
192 ref_list : xref optnl { $$ = $1; }
193 | ref_list comma xref optnl {
194 $1->tail->next = $3;
195 $1->tail = $3;
196 $$ = $1;
199 remoteopts2 : remoteopts2 remoteopts1 nl
200 | remoteopts1 optnl
202 remoteopts1 : REPOSITORY STRING {
203 remote->repository = $2;
205 | SERVER STRING {
206 remote->server = $2;
208 | PROTOCOL STRING {
209 remote->protocol = $2;
211 | MIRROR_REFERENCES boolean {
212 remote->mirror_references = $2;
214 | FETCH_ALL_BRANCHES boolean {
215 remote->fetch_all_branches = $2;
217 | PORT portplain {
218 remote->port = $2;
220 | BRANCH branch {
221 remote->branch = $2;
223 | REFERENCE ref {
224 remote->fetch_ref = $2;
226 | FETCH {
227 static const struct got_error* error;
229 if (remote->fetch_config != NULL) {
230 yyerror("fetch block already exists");
231 YYERROR;
233 error = new_fetch_config(&remote->fetch_config);
234 if (error) {
235 yyerror("%s", error->msg);
236 YYERROR;
238 } '{' optnl fetchempty '}'
239 | SEND {
240 static const struct got_error* error;
242 if (remote->send_config != NULL) {
243 yyerror("send block already exists");
244 YYERROR;
246 error = new_send_config(&remote->send_config);
247 if (error) {
248 yyerror("%s", error->msg);
249 YYERROR;
251 } '{' optnl sendempty '}'
253 fetchempty : /* empty */
254 | fetchopts2
256 fetchopts2 : fetchopts2 fetchopts1 nl
257 | fetchopts1 optnl
259 fetchopts1 : REPOSITORY STRING {
260 remote->fetch_config->repository = $2;
262 | SERVER STRING {
263 remote->fetch_config->server = $2;
265 | PROTOCOL STRING {
266 remote->fetch_config->protocol = $2;
268 | PORT portplain {
269 remote->fetch_config->port = $2;
271 | BRANCH branch {
272 remote->fetch_config->branch = $2;
275 sendempty : /* empty */
276 | sendopts2
278 sendopts2 : sendopts2 sendopts1 nl
279 | sendopts1 optnl
281 sendopts1 : REPOSITORY STRING {
282 remote->send_config->repository = $2;
284 | SERVER STRING {
285 remote->send_config->server = $2;
287 | PROTOCOL STRING {
288 remote->send_config->protocol = $2;
290 | PORT portplain {
291 remote->send_config->port = $2;
293 | BRANCH branch {
294 remote->send_config->branch = $2;
297 remote : REMOTE STRING {
298 static const struct got_error* error;
300 error = new_remote(&remote);
301 if (error) {
302 free($2);
303 yyerror("%s", error->msg);
304 YYERROR;
306 remote->name = $2;
307 } '{' optnl remoteopts2 '}' {
308 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
309 gotconfig.nremotes++;
312 author : AUTHOR STRING {
313 gotconfig.author = $2;
316 allowed_signers : ALLOWED_SIGNERS STRING {
317 gotconfig.allowed_signers_file = $2;
320 revoked_signers : REVOKED_SIGNERS STRING {
321 gotconfig.revoked_signers_file = $2;
324 optnl : '\n' optnl
325 | /* empty */
327 nl : '\n' optnl
329 comma : ','
330 | /* empty */
332 %%
334 struct keywords {
335 const char *k_name;
336 int k_val;
337 };
339 int
340 yyerror(const char *fmt, ...)
342 va_list ap;
343 char *msg;
344 char *err = NULL;
346 va_start(ap, fmt);
347 if (vasprintf(&msg, fmt, ap) == -1) {
348 gerror = got_error_from_errno("vasprintf");
349 return 0;
351 va_end(ap);
352 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
353 msg) == -1) {
354 gerror = got_error_from_errno("asprintf");
355 return(0);
357 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
358 free(msg);
359 return(0);
361 int
362 kw_cmp(const void *k, const void *e)
364 return (strcmp(k, ((const struct keywords *)e)->k_name));
367 int
368 lookup(char *s)
370 /* This has to be sorted always. */
371 static const struct keywords keywords[] = {
372 {"allowed_signers", ALLOWED_SIGNERS},
373 {"author", AUTHOR},
374 {"branch", BRANCH},
375 {"fetch", FETCH},
376 {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
377 {"fetch_all_branches", FETCH_ALL_BRANCHES},
378 {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
379 {"mirror_references", MIRROR_REFERENCES},
380 {"port", PORT},
381 {"protocol", PROTOCOL},
382 {"reference", REFERENCE},
383 {"remote", REMOTE},
384 {"repository", REPOSITORY},
385 {"revoked_signers", REVOKED_SIGNERS},
386 {"send", SEND},
387 {"server", SERVER},
388 };
389 const struct keywords *p;
391 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
392 sizeof(keywords[0]), kw_cmp);
394 if (p)
395 return (p->k_val);
396 else
397 return (STRING);
400 #define START_EXPAND 1
401 #define DONE_EXPAND 2
403 static int expanding;
405 int
406 igetc(void)
408 int c;
410 while (1) {
411 if (file->ungetpos > 0)
412 c = file->ungetbuf[--file->ungetpos];
413 else
414 c = getc(file->stream);
416 if (c == START_EXPAND)
417 expanding = 1;
418 else if (c == DONE_EXPAND)
419 expanding = 0;
420 else
421 break;
423 return (c);
426 int
427 lgetc(int quotec)
429 int c, next;
431 if (quotec) {
432 c = igetc();
433 if (c == EOF) {
434 yyerror("reached end of file while parsing "
435 "quoted string");
437 return (c);
440 c = igetc();
441 while (c == '\\') {
442 next = igetc();
443 if (next != '\n') {
444 c = next;
445 break;
447 yylval.lineno = file->lineno;
448 file->lineno++;
451 return (c);
454 void
455 lungetc(int c)
457 if (c == EOF)
458 return;
460 if (file->ungetpos >= file->ungetsize) {
461 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
462 if (p == NULL)
463 err(1, "%s", __func__);
464 file->ungetbuf = p;
465 file->ungetsize *= 2;
467 file->ungetbuf[file->ungetpos++] = c;
470 int
471 findeol(void)
473 int c;
475 /* Skip to either EOF or the first real EOL. */
476 while (1) {
477 c = lgetc(0);
478 if (c == '\n') {
479 file->lineno++;
480 break;
482 if (c == EOF)
483 break;
485 return (ERROR);
488 static long long
489 getservice(char *n)
491 struct servent *s;
492 u_long ulval;
494 if (atoul(n, &ulval) == 0) {
495 if (ulval == 0 || ulval > 65535) {
496 yyerror("illegal port value %lu", ulval);
497 return (-1);
499 return ulval;
500 } else {
501 s = getservbyname(n, "tcp");
502 if (s == NULL)
503 s = getservbyname(n, "udp");
504 if (s == NULL) {
505 yyerror("unknown port %s", n);
506 return (-1);
508 return (s->s_port);
512 static int
513 parseport(char *port, long long *pn)
515 if ((*pn = getservice(port)) == -1) {
516 *pn = 0LL;
517 return (-1);
519 return (0);
523 int
524 yylex(void)
526 char buf[8096];
527 char *p, *val;
528 int quotec, next, c;
529 int token;
531 top:
532 p = buf;
533 c = lgetc(0);
534 while (c == ' ' || c == '\t')
535 c = lgetc(0); /* nothing */
537 yylval.lineno = file->lineno;
538 if (c == '#') {
539 c = lgetc(0);
540 while (c != '\n' && c != EOF)
541 c = lgetc(0); /* nothing */
543 if (c == '$' && !expanding) {
544 while (1) {
545 c = lgetc(0);
546 if (c == EOF)
547 return (0);
549 if (p + 1 >= buf + sizeof(buf) - 1) {
550 yyerror("string too long");
551 return (findeol());
553 if (isalnum(c) || c == '_') {
554 *p++ = c;
555 continue;
557 *p = '\0';
558 lungetc(c);
559 break;
561 val = symget(buf);
562 if (val == NULL) {
563 yyerror("macro '%s' not defined", buf);
564 return (findeol());
566 p = val + strlen(val) - 1;
567 lungetc(DONE_EXPAND);
568 while (p >= val) {
569 lungetc((unsigned char)*p);
570 p--;
572 lungetc(START_EXPAND);
573 goto top;
576 switch (c) {
577 case '\'':
578 case '"':
579 quotec = c;
580 while (1) {
581 c = lgetc(quotec);
582 if (c == EOF)
583 return (0);
584 if (c == '\n') {
585 file->lineno++;
586 continue;
587 } else if (c == '\\') {
588 next = lgetc(quotec);
589 if (next == EOF)
590 return (0);
591 if (next == quotec || c == ' ' || c == '\t')
592 c = next;
593 else if (next == '\n') {
594 file->lineno++;
595 continue;
596 } else
597 lungetc(next);
598 } else if (c == quotec) {
599 *p = '\0';
600 break;
601 } else if (c == '\0') {
602 yyerror("syntax error");
603 return (findeol());
605 if (p + 1 >= buf + sizeof(buf) - 1) {
606 yyerror("string too long");
607 return (findeol());
609 *p++ = c;
611 yylval.v.string = strdup(buf);
612 if (yylval.v.string == NULL)
613 err(1, "%s", __func__);
614 return (STRING);
617 #define allowed_to_end_number(x) \
618 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
620 if (c == '-' || isdigit(c)) {
621 do {
622 *p++ = c;
623 if ((size_t)(p-buf) >= sizeof(buf)) {
624 yyerror("string too long");
625 return (findeol());
627 c = lgetc(0);
628 } while (c != EOF && isdigit(c));
629 lungetc(c);
630 if (p == buf + 1 && buf[0] == '-')
631 goto nodigits;
632 if (c == EOF || allowed_to_end_number(c)) {
633 const char *errstr = NULL;
635 *p = '\0';
636 yylval.v.number = strtonum(buf, LLONG_MIN,
637 LLONG_MAX, &errstr);
638 if (errstr) {
639 yyerror("\"%s\" invalid number: %s",
640 buf, errstr);
641 return (findeol());
643 return (NUMBER);
644 } else {
645 nodigits:
646 while (p > buf + 1)
647 lungetc((unsigned char)*--p);
648 c = (unsigned char)*--p;
649 if (c == '-')
650 return (c);
654 #define allowed_in_string(x) \
655 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
656 x != '{' && x != '}' && \
657 x != '!' && x != '=' && x != '#' && \
658 x != ','))
660 if (isalnum(c) || c == ':' || c == '_') {
661 do {
662 *p++ = c;
663 if ((size_t)(p-buf) >= sizeof(buf)) {
664 yyerror("string too long");
665 return (findeol());
667 c = lgetc(0);
668 } while (c != EOF && (allowed_in_string(c)));
669 lungetc(c);
670 *p = '\0';
671 token = lookup(buf);
672 if (token == STRING) {
673 yylval.v.string = strdup(buf);
674 if (yylval.v.string == NULL)
675 err(1, "%s", __func__);
677 return (token);
679 if (c == '\n') {
680 yylval.lineno = file->lineno;
681 file->lineno++;
683 if (c == EOF)
684 return (0);
685 return (c);
688 static const struct got_error*
689 newfile(struct file **nfile, const char *filename, int *fd)
691 const struct got_error* error = NULL;
693 (*nfile) = calloc(1, sizeof(struct file));
694 if ((*nfile) == NULL)
695 return got_error_from_errno("calloc");
696 (*nfile)->stream = fdopen(*fd, "r");
697 if ((*nfile)->stream == NULL) {
698 error = got_error_from_errno("fdopen");
699 free((*nfile));
700 return error;
702 *fd = -1; /* Stream owns the file descriptor now. */
703 (*nfile)->name = filename;
704 (*nfile)->lineno = 1;
705 (*nfile)->ungetsize = 16;
706 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
707 if ((*nfile)->ungetbuf == NULL) {
708 error = got_error_from_errno("malloc");
709 fclose((*nfile)->stream);
710 free((*nfile));
711 return error;
713 return NULL;
716 static const struct got_error*
717 new_remote(struct gotconfig_remote_repo **remote)
719 const struct got_error *error = NULL;
721 *remote = calloc(1, sizeof(**remote));
722 if (*remote == NULL)
723 error = got_error_from_errno("calloc");
724 return error;
727 static const struct got_error*
728 new_fetch_config(struct fetch_config **fetch_config)
730 const struct got_error *error = NULL;
732 *fetch_config = calloc(1, sizeof(**fetch_config));
733 if (*fetch_config == NULL)
734 error = got_error_from_errno("calloc");
735 return error;
738 static const struct got_error*
739 new_send_config(struct send_config **send_config)
741 const struct got_error *error = NULL;
743 *send_config = calloc(1, sizeof(**send_config));
744 if (*send_config == NULL)
745 error = got_error_from_errno("calloc");
746 return error;
749 static void
750 closefile(struct file *file)
752 fclose(file->stream);
753 free(file->ungetbuf);
754 free(file);
757 const struct got_error *
758 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
760 const struct got_error *err = NULL;
761 struct sym *sym, *next;
763 *conf = NULL;
765 err = newfile(&file, filename, fd);
766 if (err)
767 return err;
769 TAILQ_INIT(&gotconfig.remotes);
771 yyparse();
772 closefile(file);
774 /* Free macros and check which have not been used. */
775 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
776 if (!sym->persist) {
777 free(sym->nam);
778 free(sym->val);
779 TAILQ_REMOVE(&symhead, sym, entry);
780 free(sym);
784 if (gerror == NULL)
785 *conf = &gotconfig;
786 return gerror;
789 static void
790 free_fetch_config(struct fetch_config *fetch_config)
792 free(remote->fetch_config->repository);
793 free(remote->fetch_config->server);
794 free(remote->fetch_config->protocol);
795 free(remote->fetch_config);
798 static void
799 free_send_config(struct send_config *send_config)
801 free(remote->send_config->repository);
802 free(remote->send_config->server);
803 free(remote->send_config->protocol);
804 free(remote->send_config);
807 void
808 gotconfig_free(struct gotconfig *conf)
810 struct gotconfig_remote_repo *remote;
812 free(conf->author);
813 free(conf->allowed_signers_file);
814 free(conf->revoked_signers_file);
815 while (!TAILQ_EMPTY(&conf->remotes)) {
816 remote = TAILQ_FIRST(&conf->remotes);
817 TAILQ_REMOVE(&conf->remotes, remote, entry);
818 if (remote->fetch_config != NULL)
819 free_fetch_config(remote->fetch_config);
820 if (remote->send_config != NULL)
821 free_send_config(remote->send_config);
822 free(remote->name);
823 free(remote->repository);
824 free(remote->server);
825 free(remote->protocol);
826 free(remote);
830 int
831 symset(const char *nam, const char *val, int persist)
833 struct sym *sym;
835 TAILQ_FOREACH(sym, &symhead, entry) {
836 if (strcmp(nam, sym->nam) == 0)
837 break;
840 if (sym != NULL) {
841 if (sym->persist == 1)
842 return (0);
843 else {
844 free(sym->nam);
845 free(sym->val);
846 TAILQ_REMOVE(&symhead, sym, entry);
847 free(sym);
850 sym = calloc(1, sizeof(*sym));
851 if (sym == NULL)
852 return (-1);
854 sym->nam = strdup(nam);
855 if (sym->nam == NULL) {
856 free(sym);
857 return (-1);
859 sym->val = strdup(val);
860 if (sym->val == NULL) {
861 free(sym->nam);
862 free(sym);
863 return (-1);
865 sym->used = 0;
866 sym->persist = persist;
867 TAILQ_INSERT_TAIL(&symhead, sym, entry);
868 return (0);
871 int
872 cmdline_symset(char *s)
874 char *sym, *val;
875 int ret;
876 size_t len;
878 val = strrchr(s, '=');
879 if (val == NULL)
880 return (-1);
882 len = strlen(s) - strlen(val) + 1;
883 sym = malloc(len);
884 if (sym == NULL)
885 errx(1, "cmdline_symset: malloc");
887 strlcpy(sym, s, len);
889 ret = symset(sym, val + 1, 1);
890 free(sym);
892 return (ret);
895 char *
896 symget(const char *nam)
898 struct sym *sym;
900 TAILQ_FOREACH(sym, &symhead, entry) {
901 if (strcmp(nam, sym->nam) == 0) {
902 sym->used = 1;
903 return (sym->val);
906 return (NULL);
909 static int
910 atoul(char *s, u_long *ulvalp)
912 u_long ulval;
913 char *ep;
915 errno = 0;
916 ulval = strtoul(s, &ep, 0);
917 if (s[0] == '\0' || *ep != '\0')
918 return (-1);
919 if (errno == ERANGE && ulval == ULONG_MAX)
920 return (-1);
921 *ulvalp = ulval;
922 return (0);