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 (strlcpy(new_repo->path, $2,
278 sizeof(new_repo->path)) >=
279 sizeof(new_repo->path)) {
280 yyerror("%s: path truncated", __func__);
281 free($2);
282 YYERROR;
285 free($2);
287 | PERMIT RO STRING {
288 if (gotd_proc_id == PROC_AUTH) {
289 conf_new_access_rule(new_repo,
290 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
293 | PERMIT RW STRING {
294 if (gotd_proc_id == PROC_AUTH) {
295 conf_new_access_rule(new_repo,
296 GOTD_ACCESS_PERMITTED,
297 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
300 | DENY STRING {
301 if (gotd_proc_id == PROC_AUTH) {
302 conf_new_access_rule(new_repo,
303 GOTD_ACCESS_DENIED, 0, $2);
308 repoopts2 : repoopts2 repoopts1 nl
309 | repoopts1 optnl
312 nl : '\n' optnl
315 optnl : '\n' optnl /* zero or more newlines */
316 | /* empty */
319 %%
321 struct keywords {
322 const char *k_name;
323 int k_val;
324 };
326 int
327 yyerror(const char *fmt, ...)
329 va_list ap;
330 char *msg;
332 file->errors++;
333 va_start(ap, fmt);
334 if (vasprintf(&msg, fmt, ap) == -1)
335 fatalx("yyerror vasprintf");
336 va_end(ap);
337 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
338 free(msg);
339 return (0);
342 int
343 kw_cmp(const void *k, const void *e)
345 return (strcmp(k, ((const struct keywords *)e)->k_name));
348 int
349 lookup(char *s)
351 /* This has to be sorted always. */
352 static const struct keywords keywords[] = {
353 { "connection", CONNECTION },
354 { "deny", DENY },
355 { "limit", LIMIT },
356 { "listen", LISTEN },
357 { "on", ON },
358 { "path", PATH },
359 { "permit", PERMIT },
360 { "repository", REPOSITORY },
361 { "request", REQUEST },
362 { "ro", RO },
363 { "rw", RW },
364 { "timeout", TIMEOUT },
365 { "user", USER },
366 };
367 const struct keywords *p;
369 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
370 sizeof(keywords[0]), kw_cmp);
372 if (p)
373 return (p->k_val);
374 else
375 return (STRING);
378 #define MAXPUSHBACK 128
380 unsigned char *parsebuf;
381 int parseindex;
382 unsigned char pushback_buffer[MAXPUSHBACK];
383 int pushback_index = 0;
385 int
386 lgetc(int quotec)
388 int c, next;
390 if (parsebuf) {
391 /* Read character from the parsebuffer instead of input. */
392 if (parseindex >= 0) {
393 c = parsebuf[parseindex++];
394 if (c != '\0')
395 return (c);
396 parsebuf = NULL;
397 } else
398 parseindex++;
401 if (pushback_index)
402 return (pushback_buffer[--pushback_index]);
404 if (quotec) {
405 c = getc(file->stream);
406 if (c == EOF)
407 yyerror("reached end of file while parsing "
408 "quoted string");
409 return (c);
412 c = getc(file->stream);
413 while (c == '\\') {
414 next = getc(file->stream);
415 if (next != '\n') {
416 c = next;
417 break;
419 yylval.lineno = file->lineno;
420 file->lineno++;
421 c = getc(file->stream);
424 return (c);
427 int
428 lungetc(int c)
430 if (c == EOF)
431 return (EOF);
432 if (parsebuf) {
433 parseindex--;
434 if (parseindex >= 0)
435 return (c);
437 if (pushback_index < MAXPUSHBACK-1)
438 return (pushback_buffer[pushback_index++] = c);
439 else
440 return (EOF);
443 int
444 findeol(void)
446 int c;
448 parsebuf = NULL;
450 /* Skip to either EOF or the first real EOL. */
451 while (1) {
452 if (pushback_index)
453 c = pushback_buffer[--pushback_index];
454 else
455 c = lgetc(0);
456 if (c == '\n') {
457 file->lineno++;
458 break;
460 if (c == EOF)
461 break;
463 return (ERROR);
466 int
467 yylex(void)
469 unsigned char buf[8096];
470 unsigned char *p, *val;
471 int quotec, next, c;
472 int token;
474 top:
475 p = buf;
476 c = lgetc(0);
477 while (c == ' ' || c == '\t')
478 c = lgetc(0); /* nothing */
480 yylval.lineno = file->lineno;
481 if (c == '#') {
482 c = lgetc(0);
483 while (c != '\n' && c != EOF)
484 c = lgetc(0); /* nothing */
486 if (c == '$' && parsebuf == NULL) {
487 while (1) {
488 c = lgetc(0);
489 if (c == EOF)
490 return (0);
492 if (p + 1 >= buf + sizeof(buf) - 1) {
493 yyerror("string too long");
494 return (findeol());
496 if (isalnum(c) || c == '_') {
497 *p++ = c;
498 continue;
500 *p = '\0';
501 lungetc(c);
502 break;
504 val = symget(buf);
505 if (val == NULL) {
506 yyerror("macro '%s' not defined", buf);
507 return (findeol());
509 parsebuf = val;
510 parseindex = 0;
511 goto top;
514 switch (c) {
515 case '\'':
516 case '"':
517 quotec = c;
518 while (1) {
519 c = lgetc(quotec);
520 if (c == EOF)
521 return (0);
522 if (c == '\n') {
523 file->lineno++;
524 continue;
525 } else if (c == '\\') {
526 next = lgetc(quotec);
527 if (next == EOF)
528 return (0);
529 if (next == quotec || c == ' ' || c == '\t')
530 c = next;
531 else if (next == '\n') {
532 file->lineno++;
533 continue;
534 } else
535 lungetc(next);
536 } else if (c == quotec) {
537 *p = '\0';
538 break;
539 } else if (c == '\0') {
540 yyerror("syntax error");
541 return (findeol());
543 if (p + 1 >= buf + sizeof(buf) - 1) {
544 yyerror("string too long");
545 return (findeol());
547 *p++ = c;
549 yylval.v.string = strdup(buf);
550 if (yylval.v.string == NULL)
551 err(1, "yylex: strdup");
552 return (STRING);
555 #define allowed_to_end_number(x) \
556 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
558 if (c == '-' || isdigit(c)) {
559 do {
560 *p++ = c;
561 if ((unsigned)(p-buf) >= sizeof(buf)) {
562 yyerror("string too long");
563 return (findeol());
565 c = lgetc(0);
566 } while (c != EOF && isdigit(c));
567 lungetc(c);
568 if (p == buf + 1 && buf[0] == '-')
569 goto nodigits;
570 if (c == EOF || allowed_to_end_number(c)) {
571 const char *errstr = NULL;
573 *p = '\0';
574 yylval.v.number = strtonum(buf, LLONG_MIN,
575 LLONG_MAX, &errstr);
576 if (errstr) {
577 yyerror("\"%s\" invalid number: %s",
578 buf, errstr);
579 return (findeol());
581 return (NUMBER);
582 } else {
583 nodigits:
584 while (p > buf + 1)
585 lungetc(*--p);
586 c = *--p;
587 if (c == '-')
588 return (c);
592 #define allowed_in_string(x) \
593 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
594 x != '{' && x != '}' && \
595 x != '!' && x != '=' && x != '#' && \
596 x != ','))
598 if (isalnum(c) || c == ':' || c == '_') {
599 do {
600 *p++ = c;
601 if ((unsigned)(p-buf) >= sizeof(buf)) {
602 yyerror("string too long");
603 return (findeol());
605 c = lgetc(0);
606 } while (c != EOF && (allowed_in_string(c)));
607 lungetc(c);
608 *p = '\0';
609 token = lookup(buf);
610 if (token == STRING) {
611 yylval.v.string = strdup(buf);
612 if (yylval.v.string == NULL)
613 err(1, "yylex: strdup");
615 return (token);
617 if (c == '\n') {
618 yylval.lineno = file->lineno;
619 file->lineno++;
621 if (c == EOF)
622 return (0);
623 return (c);
626 int
627 check_file_secrecy(int fd, const char *fname)
629 struct stat st;
631 if (fstat(fd, &st)) {
632 log_warn("cannot stat %s", fname);
633 return (-1);
635 if (st.st_uid != 0 && st.st_uid != getuid()) {
636 log_warnx("%s: owner not root or current user", fname);
637 return (-1);
639 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
640 log_warnx("%s: group writable or world read/writable", fname);
641 return (-1);
643 return (0);
646 struct file *
647 newfile(const char *name, int secret)
649 struct file *nfile;
651 nfile = calloc(1, sizeof(struct file));
652 if (nfile == NULL) {
653 log_warn("calloc");
654 return (NULL);
656 nfile->name = strdup(name);
657 if (nfile->name == NULL) {
658 log_warn("strdup");
659 free(nfile);
660 return (NULL);
662 nfile->stream = fopen(nfile->name, "r");
663 if (nfile->stream == NULL) {
664 /* no warning, we don't require a conf file */
665 free(nfile->name);
666 free(nfile);
667 return (NULL);
668 } else if (secret &&
669 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
670 fclose(nfile->stream);
671 free(nfile->name);
672 free(nfile);
673 return (NULL);
675 nfile->lineno = 1;
676 return (nfile);
679 static void
680 closefile(struct file *xfile)
682 fclose(xfile->stream);
683 free(xfile->name);
684 free(xfile);
687 int
688 parse_config(const char *filename, enum gotd_procid proc_id,
689 struct gotd *env)
691 struct sym *sym, *next;
692 struct gotd_repo *repo;
694 memset(env, 0, sizeof(*env));
696 gotd = env;
697 gotd_proc_id = proc_id;
698 TAILQ_INIT(&gotd->repos);
700 /* Apply default values. */
701 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
702 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
703 fprintf(stderr, "%s: unix socket path too long", __func__);
704 return -1;
706 if (strlcpy(gotd->user_name, GOTD_USER,
707 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
708 fprintf(stderr, "%s: user name too long", __func__);
709 return -1;
712 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
713 gotd->request_timeout.tv_usec = 0;
715 file = newfile(filename, 0);
716 if (file == NULL) {
717 /* just return, as we don't require a conf file */
718 return (0);
721 yyparse();
722 errors = file->errors;
723 closefile(file);
725 /* Free macros and check which have not been used. */
726 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
727 if ((gotd->verbosity > 1) && !sym->used)
728 fprintf(stderr, "warning: macro '%s' not used\n",
729 sym->nam);
730 if (!sym->persist) {
731 free(sym->nam);
732 free(sym->val);
733 TAILQ_REMOVE(&symhead, sym, entry);
734 free(sym);
738 if (errors)
739 return (-1);
741 TAILQ_FOREACH(repo, &gotd->repos, entry) {
742 if (repo->path[0] == '\0') {
743 log_warnx("repository \"%s\": no path provided in "
744 "configuration file", repo->name);
745 return (-1);
749 return (0);
752 static int
753 uid_connection_limit_cmp(const void *pa, const void *pb)
755 const struct gotd_uid_connection_limit *a = pa, *b = pb;
757 if (a->uid < b->uid)
758 return -1;
759 else if (a->uid > b->uid);
760 return 1;
762 return 0;
765 static int
766 conf_limit_user_connections(const char *user, int maximum)
768 uid_t uid;
769 struct gotd_uid_connection_limit *limit;
770 size_t nlimits;
772 if (maximum < 1) {
773 yyerror("max connections cannot be smaller 1");
774 return -1;
776 if (maximum > GOTD_MAXCLIENTS) {
777 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
778 return -1;
781 if (gotd_auth_parseuid(user, &uid) == -1) {
782 yyerror("%s: no such user", user);
783 return -1;
786 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
787 gotd->nconnection_limits, uid);
788 if (limit) {
789 limit->max_connections = maximum;
790 return 0;
793 limit = gotd->connection_limits;
794 nlimits = gotd->nconnection_limits + 1;
795 limit = reallocarray(limit, nlimits, sizeof(*limit));
796 if (limit == NULL)
797 fatal("reallocarray");
799 limit[nlimits - 1].uid = uid;
800 limit[nlimits - 1].max_connections = maximum;
802 gotd->connection_limits = limit;
803 gotd->nconnection_limits = nlimits;
804 qsort(gotd->connection_limits, gotd->nconnection_limits,
805 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
807 return 0;
810 static struct gotd_repo *
811 conf_new_repo(const char *name)
813 struct gotd_repo *repo;
815 if (name[0] == '\0') {
816 fatalx("syntax error: empty repository name found in %s",
817 file->name);
820 if (strchr(name, '\n') != NULL)
821 fatalx("repository names must not contain linefeeds: %s", name);
823 repo = calloc(1, sizeof(*repo));
824 if (repo == NULL)
825 fatalx("%s: calloc", __func__);
827 STAILQ_INIT(&repo->rules);
829 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
830 sizeof(repo->name))
831 fatalx("%s: strlcpy", __func__);
833 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
834 gotd->nrepos++;
836 return repo;
837 };
839 static void
840 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
841 int authorization, char *identifier)
843 struct gotd_access_rule *rule;
845 rule = calloc(1, sizeof(*rule));
846 if (rule == NULL)
847 fatal("calloc");
849 rule->access = access;
850 rule->authorization = authorization;
851 rule->identifier = identifier;
853 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
856 int
857 symset(const char *nam, const char *val, int persist)
859 struct sym *sym;
861 TAILQ_FOREACH(sym, &symhead, entry) {
862 if (strcmp(nam, sym->nam) == 0)
863 break;
866 if (sym != NULL) {
867 if (sym->persist == 1)
868 return (0);
869 else {
870 free(sym->nam);
871 free(sym->val);
872 TAILQ_REMOVE(&symhead, sym, entry);
873 free(sym);
876 sym = calloc(1, sizeof(*sym));
877 if (sym == NULL)
878 return (-1);
880 sym->nam = strdup(nam);
881 if (sym->nam == NULL) {
882 free(sym);
883 return (-1);
885 sym->val = strdup(val);
886 if (sym->val == NULL) {
887 free(sym->nam);
888 free(sym);
889 return (-1);
891 sym->used = 0;
892 sym->persist = persist;
893 TAILQ_INSERT_TAIL(&symhead, sym, entry);
894 return (0);
897 char *
898 symget(const char *nam)
900 struct sym *sym;
902 TAILQ_FOREACH(sym, &symhead, entry) {
903 if (strcmp(nam, sym->nam) == 0) {
904 sym->used = 1;
905 return (sym->val);
908 return (NULL);