Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
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/time.h>
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #include <sys/stat.h>
30 #include <ctype.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <event.h>
34 #include <imsg.h>
35 #include <limits.h>
36 #include <sha1.h>
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <unistd.h>
44 #include "got_error.h"
45 #include "got_path.h"
47 #include "log.h"
48 #include "gotd.h"
49 #include "auth.h"
50 #include "listen.h"
52 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
53 static struct file {
54 TAILQ_ENTRY(file) entry;
55 FILE *stream;
56 char *name;
57 int lineno;
58 int errors;
59 } *file;
60 struct file *newfile(const char *, int);
61 static void closefile(struct file *);
62 int check_file_secrecy(int, const char *);
63 int yyparse(void);
64 int yylex(void);
65 int yyerror(const char *, ...)
66 __attribute__((__format__ (printf, 1, 2)))
67 __attribute__((__nonnull__ (1)));
68 int kw_cmp(const void *, const void *);
69 int lookup(char *);
70 int lgetc(int);
71 int lungetc(int);
72 int findeol(void);
74 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 struct sym {
76 TAILQ_ENTRY(sym) entry;
77 int used;
78 int persist;
79 char *nam;
80 char *val;
81 };
83 int symset(const char *, const char *, int);
84 char *symget(const char *);
86 static int errors;
88 static struct gotd *gotd;
89 static struct gotd_repo *new_repo;
90 static int conf_limit_user_connections(const char *, int);
91 static struct gotd_repo *conf_new_repo(const char *);
92 static void conf_new_access_rule(struct gotd_repo *,
93 enum gotd_access, int, char *);
94 static enum gotd_procid gotd_proc_id;
96 typedef struct {
97 union {
98 long long number;
99 char *string;
100 struct timeval tv;
101 } v;
102 int lineno;
103 } YYSTYPE;
105 %}
107 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
108 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
110 %token <v.string> STRING
111 %token <v.number> NUMBER
112 %type <v.number> boolean
113 %type <v.tv> timeout
115 %%
117 grammar :
118 | grammar '\n'
119 | grammar main '\n'
120 | grammar repository '\n'
123 boolean : STRING {
124 if (strcasecmp($1, "1") == 0 ||
125 strcasecmp($1, "yes") == 0 ||
126 strcasecmp($1, "on") == 0)
127 $$ = 1;
128 else if (strcasecmp($1, "0") == 0 ||
129 strcasecmp($1, "off") == 0 ||
130 strcasecmp($1, "no") == 0)
131 $$ = 0;
132 else {
133 yyerror("invalid boolean value '%s'", $1);
134 free($1);
135 YYERROR;
137 free($1);
139 | ON { $$ = 1; }
140 | NUMBER { $$ = $1; }
143 timeout : NUMBER {
144 if ($1 < 0) {
145 yyerror("invalid timeout: %lld", $1);
146 YYERROR;
148 $$.tv_sec = $1;
149 $$.tv_usec = 0;
151 | STRING {
152 const char *errstr;
153 const char *type = "seconds";
154 size_t len;
155 int mul = 1;
157 if (*$1 == '\0') {
158 yyerror("invalid number of seconds: %s", $1);
159 free($1);
160 YYERROR;
163 len = strlen($1);
164 switch ($1[len - 1]) {
165 case 'S':
166 case 's':
167 $1[len - 1] = '\0';
168 break;
169 case 'M':
170 case 'm':
171 type = "minutes";
172 mul = 60;
173 $1[len - 1] = '\0';
174 break;
175 case 'H':
176 case 'h':
177 type = "hours";
178 mul = 60 * 60;
179 $1[len - 1] = '\0';
180 break;
183 $$.tv_usec = 0;
184 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
185 if (errstr) {
186 yyerror("number of %s is %s: %s", type,
187 errstr, $1);
188 free($1);
189 YYERROR;
192 $$.tv_sec *= mul;
193 free($1);
197 main : LISTEN ON STRING {
198 if (gotd_proc_id == PROC_LISTEN) {
199 if (strlcpy(gotd->unix_socket_path, $3,
200 sizeof(gotd->unix_socket_path)) >=
201 sizeof(gotd->unix_socket_path)) {
202 yyerror("%s: unix socket path too long",
203 __func__);
204 free($3);
205 YYERROR;
208 free($3);
210 | USER STRING {
211 if (strlcpy(gotd->user_name, $2,
212 sizeof(gotd->user_name)) >=
213 sizeof(gotd->user_name)) {
214 yyerror("%s: user name too long", __func__);
215 free($2);
216 YYERROR;
218 free($2);
220 | connection
223 connection : CONNECTION '{' optnl conflags_l '}'
224 | CONNECTION conflags
226 conflags_l : conflags optnl conflags_l
227 | conflags optnl
230 conflags : REQUEST TIMEOUT timeout {
231 if ($3.tv_sec <= 0) {
232 yyerror("invalid timeout: %lld", $3.tv_sec);
233 YYERROR;
235 memcpy(&gotd->request_timeout, &$3,
236 sizeof(gotd->request_timeout));
238 | LIMIT USER STRING NUMBER {
239 if (gotd_proc_id == PROC_LISTEN &&
240 conf_limit_user_connections($3, $4) == -1) {
241 free($3);
242 YYERROR;
244 free($3);
248 repository : REPOSITORY STRING {
249 struct gotd_repo *repo;
251 TAILQ_FOREACH(repo, &gotd->repos, entry) {
252 if (strcmp(repo->name, $2) == 0) {
253 yyerror("duplicate repository '%s'", $2);
254 free($2);
255 YYERROR;
259 if (gotd_proc_id == PROC_GOTD ||
260 gotd_proc_id == PROC_AUTH) {
261 new_repo = conf_new_repo($2);
263 free($2);
264 } '{' optnl repoopts2 '}' {
268 repoopts1 : PATH STRING {
269 if (gotd_proc_id == PROC_GOTD ||
270 gotd_proc_id == PROC_AUTH) {
271 if (!got_path_is_absolute($2)) {
272 yyerror("%s: path %s is not absolute",
273 __func__, $2);
274 free($2);
275 YYERROR;
277 if (realpath($2, new_repo->path) == NULL) {
278 yyerror("realpath %s: %s", $2, strerror(errno));
279 free($2);
280 YYERROR;
283 free($2);
285 | PERMIT RO STRING {
286 if (gotd_proc_id == PROC_AUTH) {
287 conf_new_access_rule(new_repo,
288 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
291 | PERMIT RW STRING {
292 if (gotd_proc_id == PROC_AUTH) {
293 conf_new_access_rule(new_repo,
294 GOTD_ACCESS_PERMITTED,
295 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
298 | DENY STRING {
299 if (gotd_proc_id == PROC_AUTH) {
300 conf_new_access_rule(new_repo,
301 GOTD_ACCESS_DENIED, 0, $2);
306 repoopts2 : repoopts2 repoopts1 nl
307 | repoopts1 optnl
310 nl : '\n' optnl
313 optnl : '\n' optnl /* zero or more newlines */
314 | /* empty */
317 %%
319 struct keywords {
320 const char *k_name;
321 int k_val;
322 };
324 int
325 yyerror(const char *fmt, ...)
327 va_list ap;
328 char *msg;
330 file->errors++;
331 va_start(ap, fmt);
332 if (vasprintf(&msg, fmt, ap) == -1)
333 fatalx("yyerror vasprintf");
334 va_end(ap);
335 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
336 free(msg);
337 return (0);
340 int
341 kw_cmp(const void *k, const void *e)
343 return (strcmp(k, ((const struct keywords *)e)->k_name));
346 int
347 lookup(char *s)
349 /* This has to be sorted always. */
350 static const struct keywords keywords[] = {
351 { "connection", CONNECTION },
352 { "deny", DENY },
353 { "limit", LIMIT },
354 { "listen", LISTEN },
355 { "on", ON },
356 { "path", PATH },
357 { "permit", PERMIT },
358 { "repository", REPOSITORY },
359 { "request", REQUEST },
360 { "ro", RO },
361 { "rw", RW },
362 { "timeout", TIMEOUT },
363 { "user", USER },
364 };
365 const struct keywords *p;
367 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
368 sizeof(keywords[0]), kw_cmp);
370 if (p)
371 return (p->k_val);
372 else
373 return (STRING);
376 #define MAXPUSHBACK 128
378 unsigned char *parsebuf;
379 int parseindex;
380 unsigned char pushback_buffer[MAXPUSHBACK];
381 int pushback_index = 0;
383 int
384 lgetc(int quotec)
386 int c, next;
388 if (parsebuf) {
389 /* Read character from the parsebuffer instead of input. */
390 if (parseindex >= 0) {
391 c = parsebuf[parseindex++];
392 if (c != '\0')
393 return (c);
394 parsebuf = NULL;
395 } else
396 parseindex++;
399 if (pushback_index)
400 return (pushback_buffer[--pushback_index]);
402 if (quotec) {
403 c = getc(file->stream);
404 if (c == EOF)
405 yyerror("reached end of file while parsing "
406 "quoted string");
407 return (c);
410 c = getc(file->stream);
411 while (c == '\\') {
412 next = getc(file->stream);
413 if (next != '\n') {
414 c = next;
415 break;
417 yylval.lineno = file->lineno;
418 file->lineno++;
419 c = getc(file->stream);
422 return (c);
425 int
426 lungetc(int c)
428 if (c == EOF)
429 return (EOF);
430 if (parsebuf) {
431 parseindex--;
432 if (parseindex >= 0)
433 return (c);
435 if (pushback_index < MAXPUSHBACK-1)
436 return (pushback_buffer[pushback_index++] = c);
437 else
438 return (EOF);
441 int
442 findeol(void)
444 int c;
446 parsebuf = NULL;
448 /* Skip to either EOF or the first real EOL. */
449 while (1) {
450 if (pushback_index)
451 c = pushback_buffer[--pushback_index];
452 else
453 c = lgetc(0);
454 if (c == '\n') {
455 file->lineno++;
456 break;
458 if (c == EOF)
459 break;
461 return (ERROR);
464 int
465 yylex(void)
467 unsigned char buf[8096];
468 unsigned char *p, *val;
469 int quotec, next, c;
470 int token;
472 top:
473 p = buf;
474 c = lgetc(0);
475 while (c == ' ' || c == '\t')
476 c = lgetc(0); /* nothing */
478 yylval.lineno = file->lineno;
479 if (c == '#') {
480 c = lgetc(0);
481 while (c != '\n' && c != EOF)
482 c = lgetc(0); /* nothing */
484 if (c == '$' && parsebuf == NULL) {
485 while (1) {
486 c = lgetc(0);
487 if (c == EOF)
488 return (0);
490 if (p + 1 >= buf + sizeof(buf) - 1) {
491 yyerror("string too long");
492 return (findeol());
494 if (isalnum(c) || c == '_') {
495 *p++ = c;
496 continue;
498 *p = '\0';
499 lungetc(c);
500 break;
502 val = symget(buf);
503 if (val == NULL) {
504 yyerror("macro '%s' not defined", buf);
505 return (findeol());
507 parsebuf = val;
508 parseindex = 0;
509 goto top;
512 switch (c) {
513 case '\'':
514 case '"':
515 quotec = c;
516 while (1) {
517 c = lgetc(quotec);
518 if (c == EOF)
519 return (0);
520 if (c == '\n') {
521 file->lineno++;
522 continue;
523 } else if (c == '\\') {
524 next = lgetc(quotec);
525 if (next == EOF)
526 return (0);
527 if (next == quotec || c == ' ' || c == '\t')
528 c = next;
529 else if (next == '\n') {
530 file->lineno++;
531 continue;
532 } else
533 lungetc(next);
534 } else if (c == quotec) {
535 *p = '\0';
536 break;
537 } else if (c == '\0') {
538 yyerror("syntax error");
539 return (findeol());
541 if (p + 1 >= buf + sizeof(buf) - 1) {
542 yyerror("string too long");
543 return (findeol());
545 *p++ = c;
547 yylval.v.string = strdup(buf);
548 if (yylval.v.string == NULL)
549 err(1, "yylex: strdup");
550 return (STRING);
553 #define allowed_to_end_number(x) \
554 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
556 if (c == '-' || isdigit(c)) {
557 do {
558 *p++ = c;
559 if ((unsigned)(p-buf) >= sizeof(buf)) {
560 yyerror("string too long");
561 return (findeol());
563 c = lgetc(0);
564 } while (c != EOF && isdigit(c));
565 lungetc(c);
566 if (p == buf + 1 && buf[0] == '-')
567 goto nodigits;
568 if (c == EOF || allowed_to_end_number(c)) {
569 const char *errstr = NULL;
571 *p = '\0';
572 yylval.v.number = strtonum(buf, LLONG_MIN,
573 LLONG_MAX, &errstr);
574 if (errstr) {
575 yyerror("\"%s\" invalid number: %s",
576 buf, errstr);
577 return (findeol());
579 return (NUMBER);
580 } else {
581 nodigits:
582 while (p > buf + 1)
583 lungetc(*--p);
584 c = *--p;
585 if (c == '-')
586 return (c);
590 #define allowed_in_string(x) \
591 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
592 x != '{' && x != '}' && \
593 x != '!' && x != '=' && x != '#' && \
594 x != ','))
596 if (isalnum(c) || c == ':' || c == '_') {
597 do {
598 *p++ = c;
599 if ((unsigned)(p-buf) >= sizeof(buf)) {
600 yyerror("string too long");
601 return (findeol());
603 c = lgetc(0);
604 } while (c != EOF && (allowed_in_string(c)));
605 lungetc(c);
606 *p = '\0';
607 token = lookup(buf);
608 if (token == STRING) {
609 yylval.v.string = strdup(buf);
610 if (yylval.v.string == NULL)
611 err(1, "yylex: strdup");
613 return (token);
615 if (c == '\n') {
616 yylval.lineno = file->lineno;
617 file->lineno++;
619 if (c == EOF)
620 return (0);
621 return (c);
624 int
625 check_file_secrecy(int fd, const char *fname)
627 struct stat st;
629 if (fstat(fd, &st)) {
630 log_warn("cannot stat %s", fname);
631 return (-1);
633 if (st.st_uid != 0 && st.st_uid != getuid()) {
634 log_warnx("%s: owner not root or current user", fname);
635 return (-1);
637 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
638 log_warnx("%s: group writable or world read/writable", fname);
639 return (-1);
641 return (0);
644 struct file *
645 newfile(const char *name, int secret)
647 struct file *nfile;
649 nfile = calloc(1, sizeof(struct file));
650 if (nfile == NULL) {
651 log_warn("calloc");
652 return (NULL);
654 nfile->name = strdup(name);
655 if (nfile->name == NULL) {
656 log_warn("strdup");
657 free(nfile);
658 return (NULL);
660 nfile->stream = fopen(nfile->name, "r");
661 if (nfile->stream == NULL) {
662 /* no warning, we don't require a conf file */
663 free(nfile->name);
664 free(nfile);
665 return (NULL);
666 } else if (secret &&
667 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
668 fclose(nfile->stream);
669 free(nfile->name);
670 free(nfile);
671 return (NULL);
673 nfile->lineno = 1;
674 return (nfile);
677 static void
678 closefile(struct file *xfile)
680 fclose(xfile->stream);
681 free(xfile->name);
682 free(xfile);
685 int
686 parse_config(const char *filename, enum gotd_procid proc_id,
687 struct gotd *env)
689 struct sym *sym, *next;
690 struct gotd_repo *repo;
692 memset(env, 0, sizeof(*env));
694 gotd = env;
695 gotd_proc_id = proc_id;
696 TAILQ_INIT(&gotd->repos);
698 /* Apply default values. */
699 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
700 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
701 fprintf(stderr, "%s: unix socket path too long", __func__);
702 return -1;
704 if (strlcpy(gotd->user_name, GOTD_USER,
705 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
706 fprintf(stderr, "%s: user name too long", __func__);
707 return -1;
710 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
711 gotd->request_timeout.tv_usec = 0;
713 file = newfile(filename, 0);
714 if (file == NULL) {
715 /* just return, as we don't require a conf file */
716 return (0);
719 yyparse();
720 errors = file->errors;
721 closefile(file);
723 /* Free macros and check which have not been used. */
724 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
725 if ((gotd->verbosity > 1) && !sym->used)
726 fprintf(stderr, "warning: macro '%s' not used\n",
727 sym->nam);
728 if (!sym->persist) {
729 free(sym->nam);
730 free(sym->val);
731 TAILQ_REMOVE(&symhead, sym, entry);
732 free(sym);
736 if (errors)
737 return (-1);
739 TAILQ_FOREACH(repo, &gotd->repos, entry) {
740 if (repo->path[0] == '\0') {
741 log_warnx("repository \"%s\": no path provided in "
742 "configuration file", repo->name);
743 return (-1);
747 return (0);
750 static int
751 uid_connection_limit_cmp(const void *pa, const void *pb)
753 const struct gotd_uid_connection_limit *a = pa, *b = pb;
755 if (a->uid < b->uid)
756 return -1;
757 else if (a->uid > b->uid);
758 return 1;
760 return 0;
763 static int
764 conf_limit_user_connections(const char *user, int maximum)
766 uid_t uid;
767 struct gotd_uid_connection_limit *limit;
768 size_t nlimits;
770 if (maximum < 1) {
771 yyerror("max connections cannot be smaller 1");
772 return -1;
774 if (maximum > GOTD_MAXCLIENTS) {
775 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
776 return -1;
779 if (gotd_auth_parseuid(user, &uid) == -1) {
780 yyerror("%s: no such user", user);
781 return -1;
784 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
785 gotd->nconnection_limits, uid);
786 if (limit) {
787 limit->max_connections = maximum;
788 return 0;
791 limit = gotd->connection_limits;
792 nlimits = gotd->nconnection_limits + 1;
793 limit = reallocarray(limit, nlimits, sizeof(*limit));
794 if (limit == NULL)
795 fatal("reallocarray");
797 limit[nlimits - 1].uid = uid;
798 limit[nlimits - 1].max_connections = maximum;
800 gotd->connection_limits = limit;
801 gotd->nconnection_limits = nlimits;
802 qsort(gotd->connection_limits, gotd->nconnection_limits,
803 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
805 return 0;
808 static struct gotd_repo *
809 conf_new_repo(const char *name)
811 struct gotd_repo *repo;
813 if (name[0] == '\0') {
814 fatalx("syntax error: empty repository name found in %s",
815 file->name);
818 if (strchr(name, '\n') != NULL)
819 fatalx("repository names must not contain linefeeds: %s", name);
821 repo = calloc(1, sizeof(*repo));
822 if (repo == NULL)
823 fatalx("%s: calloc", __func__);
825 STAILQ_INIT(&repo->rules);
827 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
828 sizeof(repo->name))
829 fatalx("%s: strlcpy", __func__);
831 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
832 gotd->nrepos++;
834 return repo;
835 };
837 static void
838 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
839 int authorization, char *identifier)
841 struct gotd_access_rule *rule;
843 rule = calloc(1, sizeof(*rule));
844 if (rule == NULL)
845 fatal("calloc");
847 rule->access = access;
848 rule->authorization = authorization;
849 rule->identifier = identifier;
851 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
854 int
855 symset(const char *nam, const char *val, int persist)
857 struct sym *sym;
859 TAILQ_FOREACH(sym, &symhead, entry) {
860 if (strcmp(nam, sym->nam) == 0)
861 break;
864 if (sym != NULL) {
865 if (sym->persist == 1)
866 return (0);
867 else {
868 free(sym->nam);
869 free(sym->val);
870 TAILQ_REMOVE(&symhead, sym, entry);
871 free(sym);
874 sym = calloc(1, sizeof(*sym));
875 if (sym == NULL)
876 return (-1);
878 sym->nam = strdup(nam);
879 if (sym->nam == NULL) {
880 free(sym);
881 return (-1);
883 sym->val = strdup(val);
884 if (sym->val == NULL) {
885 free(sym->nam);
886 free(sym);
887 return (-1);
889 sym->used = 0;
890 sym->persist = persist;
891 TAILQ_INSERT_TAIL(&symhead, sym, entry);
892 return (0);
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);