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 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
111 %%
113 grammar : /* empty */
114 | grammar '\n'
115 | grammar author '\n'
116 | grammar remote '\n'
117 | grammar allowed_signers '\n'
118 | grammar revoked_signers '\n'
119 | grammar signer_id '\n'
121 boolean : STRING {
122 if (strcasecmp($1, "true") == 0 ||
123 strcasecmp($1, "yes") == 0)
124 $$ = 1;
125 else if (strcasecmp($1, "false") == 0 ||
126 strcasecmp($1, "no") == 0)
127 $$ = 0;
128 else {
129 yyerror("invalid boolean value '%s'", $1);
130 free($1);
131 YYERROR;
133 free($1);
136 numberstring : NUMBER {
137 char *s;
138 if (asprintf(&s, "%lld", $1) == -1) {
139 yyerror("string: asprintf");
140 YYERROR;
142 $$ = s;
144 | STRING
146 portplain : numberstring {
147 if (parseport($1, &$$) == -1) {
148 free($1);
149 YYERROR;
151 free($1);
154 branch : /* empty */ { $$ = NULL; }
155 | xbranch { $$ = $1; }
156 | '{' optnl branch_list '}' { $$ = $3; }
158 xbranch : STRING {
159 $$ = calloc(1, sizeof(struct node_branch));
160 if ($$ == NULL) {
161 yyerror("calloc");
162 YYERROR;
164 $$->branch_name = $1;
165 $$->tail = $$;
168 branch_list : xbranch optnl { $$ = $1; }
169 | branch_list comma xbranch optnl {
170 $1->tail->next = $3;
171 $1->tail = $3;
172 $$ = $1;
175 ref : /* empty */ { $$ = NULL; }
176 | xref { $$ = $1; }
177 | '{' optnl ref_list '}' { $$ = $3; }
179 xref : STRING {
180 $$ = calloc(1, sizeof(struct node_ref));
181 if ($$ == NULL) {
182 yyerror("calloc");
183 YYERROR;
185 $$->ref_name = $1;
186 $$->tail = $$;
189 ref_list : xref optnl { $$ = $1; }
190 | ref_list comma xref optnl {
191 $1->tail->next = $3;
192 $1->tail = $3;
193 $$ = $1;
196 remoteopts2 : remoteopts2 remoteopts1 nl
197 | remoteopts1 optnl
199 remoteopts1 : REPOSITORY STRING {
200 remote->repository = $2;
202 | SERVER STRING {
203 remote->server = $2;
205 | PROTOCOL STRING {
206 remote->protocol = $2;
208 | MIRROR_REFERENCES boolean {
209 remote->mirror_references = $2;
211 | FETCH_ALL_BRANCHES boolean {
212 remote->fetch_all_branches = $2;
214 | PORT portplain {
215 remote->port = $2;
217 | BRANCH branch {
218 remote->branch = $2;
220 | REFERENCE ref {
221 remote->fetch_ref = $2;
223 | FETCH {
224 static const struct got_error* error;
226 if (remote->fetch_config != NULL) {
227 yyerror("fetch block already exists");
228 YYERROR;
230 error = new_fetch_config(&remote->fetch_config);
231 if (error) {
232 yyerror("%s", error->msg);
233 YYERROR;
235 } '{' optnl fetchempty '}'
236 | SEND {
237 static const struct got_error* error;
239 if (remote->send_config != NULL) {
240 yyerror("send block already exists");
241 YYERROR;
243 error = new_send_config(&remote->send_config);
244 if (error) {
245 yyerror("%s", error->msg);
246 YYERROR;
248 } '{' optnl sendempty '}'
250 fetchempty : /* empty */
251 | fetchopts2
253 fetchopts2 : fetchopts2 fetchopts1 nl
254 | fetchopts1 optnl
256 fetchopts1 : REPOSITORY STRING {
257 remote->fetch_config->repository = $2;
259 | SERVER STRING {
260 remote->fetch_config->server = $2;
262 | PROTOCOL STRING {
263 remote->fetch_config->protocol = $2;
265 | PORT portplain {
266 remote->fetch_config->port = $2;
268 | BRANCH branch {
269 remote->fetch_config->branch = $2;
272 sendempty : /* empty */
273 | sendopts2
275 sendopts2 : sendopts2 sendopts1 nl
276 | sendopts1 optnl
278 sendopts1 : REPOSITORY STRING {
279 remote->send_config->repository = $2;
281 | SERVER STRING {
282 remote->send_config->server = $2;
284 | PROTOCOL STRING {
285 remote->send_config->protocol = $2;
287 | PORT portplain {
288 remote->send_config->port = $2;
290 | BRANCH branch {
291 remote->send_config->branch = $2;
294 remote : REMOTE STRING {
295 static const struct got_error* error;
297 error = new_remote(&remote);
298 if (error) {
299 free($2);
300 yyerror("%s", error->msg);
301 YYERROR;
303 remote->name = $2;
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;
325 optnl : '\n' optnl
326 | /* empty */
328 nl : '\n' optnl
330 comma : ','
331 | /* empty */
333 %%
335 struct keywords {
336 const char *k_name;
337 int k_val;
338 };
340 int
341 yyerror(const char *fmt, ...)
343 va_list ap;
344 char *msg;
345 char *err = NULL;
347 va_start(ap, fmt);
348 if (vasprintf(&msg, fmt, ap) == -1) {
349 gerror = got_error_from_errno("vasprintf");
350 return 0;
352 va_end(ap);
353 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
354 msg) == -1) {
355 gerror = got_error_from_errno("asprintf");
356 return(0);
358 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
359 free(msg);
360 return(0);
362 int
363 kw_cmp(const void *k, const void *e)
365 return (strcmp(k, ((const struct keywords *)e)->k_name));
368 int
369 lookup(char *s)
371 /* This has to be sorted always. */
372 static const struct keywords keywords[] = {
373 {"allowed_signers", ALLOWED_SIGNERS},
374 {"author", AUTHOR},
375 {"branch", BRANCH},
376 {"fetch", FETCH},
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},
381 {"port", PORT},
382 {"protocol", PROTOCOL},
383 {"reference", REFERENCE},
384 {"remote", REMOTE},
385 {"repository", REPOSITORY},
386 {"revoked_signers", REVOKED_SIGNERS},
387 {"send", SEND},
388 {"server", SERVER},
389 {"signer_id", SIGNER_ID},
390 };
391 const struct keywords *p;
393 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
394 sizeof(keywords[0]), kw_cmp);
396 if (p)
397 return (p->k_val);
398 else
399 return (STRING);
402 #define START_EXPAND 1
403 #define DONE_EXPAND 2
405 static int expanding;
407 int
408 igetc(void)
410 int c;
412 while (1) {
413 if (file->ungetpos > 0)
414 c = file->ungetbuf[--file->ungetpos];
415 else
416 c = getc(file->stream);
418 if (c == START_EXPAND)
419 expanding = 1;
420 else if (c == DONE_EXPAND)
421 expanding = 0;
422 else
423 break;
425 return (c);
428 int
429 lgetc(int quotec)
431 int c, next;
433 if (quotec) {
434 c = igetc();
435 if (c == EOF) {
436 yyerror("reached end of file while parsing "
437 "quoted string");
439 return (c);
442 c = igetc();
443 while (c == '\\') {
444 next = igetc();
445 if (next != '\n') {
446 c = next;
447 break;
449 yylval.lineno = file->lineno;
450 file->lineno++;
453 return (c);
456 void
457 lungetc(int c)
459 if (c == EOF)
460 return;
462 if (file->ungetpos >= file->ungetsize) {
463 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
464 if (p == NULL)
465 err(1, "%s", __func__);
466 file->ungetbuf = p;
467 file->ungetsize *= 2;
469 file->ungetbuf[file->ungetpos++] = c;
472 int
473 findeol(void)
475 int c;
477 /* Skip to either EOF or the first real EOL. */
478 while (1) {
479 c = lgetc(0);
480 if (c == '\n') {
481 file->lineno++;
482 break;
484 if (c == EOF)
485 break;
487 return (ERROR);
490 static long long
491 getservice(char *n)
493 struct servent *s;
494 u_long ulval;
496 if (atoul(n, &ulval) == 0) {
497 if (ulval == 0 || ulval > 65535) {
498 yyerror("illegal port value %lu", ulval);
499 return (-1);
501 return ulval;
502 } else {
503 s = getservbyname(n, "tcp");
504 if (s == NULL)
505 s = getservbyname(n, "udp");
506 if (s == NULL) {
507 yyerror("unknown port %s", n);
508 return (-1);
510 return (s->s_port);
514 static int
515 parseport(char *port, long long *pn)
517 if ((*pn = getservice(port)) == -1) {
518 *pn = 0LL;
519 return (-1);
521 return (0);
525 int
526 yylex(void)
528 char buf[8096];
529 char *p, *val;
530 int quotec, next, c;
531 int token;
533 top:
534 p = buf;
535 c = lgetc(0);
536 while (c == ' ' || c == '\t')
537 c = lgetc(0); /* nothing */
539 yylval.lineno = file->lineno;
540 if (c == '#') {
541 c = lgetc(0);
542 while (c != '\n' && c != EOF)
543 c = lgetc(0); /* nothing */
545 if (c == '$' && !expanding) {
546 while (1) {
547 c = lgetc(0);
548 if (c == EOF)
549 return (0);
551 if (p + 1 >= buf + sizeof(buf) - 1) {
552 yyerror("string too long");
553 return (findeol());
555 if (isalnum(c) || c == '_') {
556 *p++ = c;
557 continue;
559 *p = '\0';
560 lungetc(c);
561 break;
563 val = symget(buf);
564 if (val == NULL) {
565 yyerror("macro '%s' not defined", buf);
566 return (findeol());
568 p = val + strlen(val) - 1;
569 lungetc(DONE_EXPAND);
570 while (p >= val) {
571 lungetc((unsigned char)*p);
572 p--;
574 lungetc(START_EXPAND);
575 goto top;
578 switch (c) {
579 case '\'':
580 case '"':
581 quotec = c;
582 while (1) {
583 c = lgetc(quotec);
584 if (c == EOF)
585 return (0);
586 if (c == '\n') {
587 file->lineno++;
588 continue;
589 } else if (c == '\\') {
590 next = lgetc(quotec);
591 if (next == EOF)
592 return (0);
593 if (next == quotec || c == ' ' || c == '\t')
594 c = next;
595 else if (next == '\n') {
596 file->lineno++;
597 continue;
598 } else
599 lungetc(next);
600 } else if (c == quotec) {
601 *p = '\0';
602 break;
603 } else if (c == '\0') {
604 yyerror("syntax error");
605 return (findeol());
607 if (p + 1 >= buf + sizeof(buf) - 1) {
608 yyerror("string too long");
609 return (findeol());
611 *p++ = c;
613 yylval.v.string = strdup(buf);
614 if (yylval.v.string == NULL)
615 err(1, "%s", __func__);
616 return (STRING);
619 #define allowed_to_end_number(x) \
620 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
622 if (c == '-' || isdigit(c)) {
623 do {
624 *p++ = c;
625 if ((size_t)(p-buf) >= sizeof(buf)) {
626 yyerror("string too long");
627 return (findeol());
629 c = lgetc(0);
630 } while (c != EOF && isdigit(c));
631 lungetc(c);
632 if (p == buf + 1 && buf[0] == '-')
633 goto nodigits;
634 if (c == EOF || allowed_to_end_number(c)) {
635 const char *errstr = NULL;
637 *p = '\0';
638 yylval.v.number = strtonum(buf, LLONG_MIN,
639 LLONG_MAX, &errstr);
640 if (errstr) {
641 yyerror("\"%s\" invalid number: %s",
642 buf, errstr);
643 return (findeol());
645 return (NUMBER);
646 } else {
647 nodigits:
648 while (p > buf + 1)
649 lungetc((unsigned char)*--p);
650 c = (unsigned char)*--p;
651 if (c == '-')
652 return (c);
656 #define allowed_in_string(x) \
657 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
658 x != '{' && x != '}' && \
659 x != '!' && x != '=' && x != '#' && \
660 x != ','))
662 if (isalnum(c) || c == ':' || c == '_') {
663 do {
664 *p++ = c;
665 if ((size_t)(p-buf) >= sizeof(buf)) {
666 yyerror("string too long");
667 return (findeol());
669 c = lgetc(0);
670 } while (c != EOF && (allowed_in_string(c)));
671 lungetc(c);
672 *p = '\0';
673 token = lookup(buf);
674 if (token == STRING) {
675 yylval.v.string = strdup(buf);
676 if (yylval.v.string == NULL)
677 err(1, "%s", __func__);
679 return (token);
681 if (c == '\n') {
682 yylval.lineno = file->lineno;
683 file->lineno++;
685 if (c == EOF)
686 return (0);
687 return (c);
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");
701 free((*nfile));
702 return error;
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);
712 free((*nfile));
713 return error;
715 return NULL;
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));
724 if (*remote == NULL)
725 error = got_error_from_errno("calloc");
726 return error;
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");
737 return error;
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");
748 return error;
751 static void
752 closefile(struct file *file)
754 fclose(file->stream);
755 free(file->ungetbuf);
756 free(file);
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;
765 *conf = NULL;
767 err = newfile(&file, filename, fd);
768 if (err)
769 return err;
771 TAILQ_INIT(&gotconfig.remotes);
773 yyparse();
774 closefile(file);
776 /* Free macros and check which have not been used. */
777 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
778 if (!sym->persist) {
779 free(sym->nam);
780 free(sym->val);
781 TAILQ_REMOVE(&symhead, sym, entry);
782 free(sym);
786 if (gerror == NULL)
787 *conf = &gotconfig;
788 return gerror;
791 static void
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);
800 static void
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);
809 void
810 gotconfig_free(struct gotconfig *conf)
812 struct gotconfig_remote_repo *remote;
814 free(conf->author);
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);
825 free(remote->name);
826 free(remote->repository);
827 free(remote->server);
828 free(remote->protocol);
829 free(remote);
833 int
834 symset(const char *nam, const char *val, int persist)
836 struct sym *sym;
838 TAILQ_FOREACH(sym, &symhead, entry) {
839 if (strcmp(nam, sym->nam) == 0)
840 break;
843 if (sym != NULL) {
844 if (sym->persist == 1)
845 return (0);
846 else {
847 free(sym->nam);
848 free(sym->val);
849 TAILQ_REMOVE(&symhead, sym, entry);
850 free(sym);
853 sym = calloc(1, sizeof(*sym));
854 if (sym == NULL)
855 return (-1);
857 sym->nam = strdup(nam);
858 if (sym->nam == NULL) {
859 free(sym);
860 return (-1);
862 sym->val = strdup(val);
863 if (sym->val == NULL) {
864 free(sym->nam);
865 free(sym);
866 return (-1);
868 sym->used = 0;
869 sym->persist = persist;
870 TAILQ_INSERT_TAIL(&symhead, sym, entry);
871 return (0);
874 int
875 cmdline_symset(char *s)
877 char *sym, *val;
878 int ret;
879 size_t len;
881 val = strrchr(s, '=');
882 if (val == NULL)
883 return (-1);
885 len = strlen(s) - strlen(val) + 1;
886 sym = malloc(len);
887 if (sym == NULL)
888 errx(1, "cmdline_symset: malloc");
890 strlcpy(sym, s, len);
892 ret = symset(sym, val + 1, 1);
893 free(sym);
895 return (ret);
898 char *
899 symget(const char *nam)
901 struct sym *sym;
903 TAILQ_FOREACH(sym, &symhead, entry) {
904 if (strcmp(nam, sym->nam) == 0) {
905 sym->used = 1;
906 return (sym->val);
909 return (NULL);
912 static int
913 atoul(char *s, u_long *ulvalp)
915 u_long ulval;
916 char *ep;
918 errno = 0;
919 ulval = strtoul(s, &ep, 0);
920 if (s[0] == '\0' || *ep != '\0')
921 return (-1);
922 if (errno == ERANGE && ulval == ULONG_MAX)
923 return (-1);
924 *ulvalp = ulval;
925 return (0);