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 (!got_path_is_absolute($3))
199 yyerror("bad unix socket path \"%s\": "
200 "must be an absolute path", $3);
202 if (gotd_proc_id == PROC_LISTEN) {
203 if (strlcpy(gotd->unix_socket_path, $3,
204 sizeof(gotd->unix_socket_path)) >=
205 sizeof(gotd->unix_socket_path)) {
206 yyerror("%s: unix socket path too long",
207 __func__);
208 free($3);
209 YYERROR;
212 free($3);
214 | USER STRING {
215 if (strlcpy(gotd->user_name, $2,
216 sizeof(gotd->user_name)) >=
217 sizeof(gotd->user_name)) {
218 yyerror("%s: user name too long", __func__);
219 free($2);
220 YYERROR;
222 free($2);
224 | connection
227 connection : CONNECTION '{' optnl conflags_l '}'
228 | CONNECTION conflags
230 conflags_l : conflags optnl conflags_l
231 | conflags optnl
234 conflags : REQUEST TIMEOUT timeout {
235 if ($3.tv_sec <= 0) {
236 yyerror("invalid timeout: %lld", $3.tv_sec);
237 YYERROR;
239 memcpy(&gotd->request_timeout, &$3,
240 sizeof(gotd->request_timeout));
242 | LIMIT USER STRING NUMBER {
243 if (gotd_proc_id == PROC_LISTEN &&
244 conf_limit_user_connections($3, $4) == -1) {
245 free($3);
246 YYERROR;
248 free($3);
252 repository : REPOSITORY STRING {
253 struct gotd_repo *repo;
255 TAILQ_FOREACH(repo, &gotd->repos, entry) {
256 if (strcmp(repo->name, $2) == 0) {
257 yyerror("duplicate repository '%s'", $2);
258 free($2);
259 YYERROR;
263 if (gotd_proc_id == PROC_GOTD ||
264 gotd_proc_id == PROC_AUTH) {
265 new_repo = conf_new_repo($2);
267 free($2);
268 } '{' optnl repoopts2 '}' {
272 repoopts1 : PATH STRING {
273 if (gotd_proc_id == PROC_GOTD ||
274 gotd_proc_id == PROC_AUTH) {
275 if (!got_path_is_absolute($2)) {
276 yyerror("%s: path %s is not absolute",
277 __func__, $2);
278 free($2);
279 YYERROR;
281 if (realpath($2, new_repo->path) == NULL) {
282 yyerror("realpath %s: %s", $2, strerror(errno));
283 free($2);
284 YYERROR;
287 free($2);
289 | PERMIT RO STRING {
290 if (gotd_proc_id == PROC_AUTH) {
291 conf_new_access_rule(new_repo,
292 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
295 | PERMIT RW STRING {
296 if (gotd_proc_id == PROC_AUTH) {
297 conf_new_access_rule(new_repo,
298 GOTD_ACCESS_PERMITTED,
299 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
302 | DENY STRING {
303 if (gotd_proc_id == PROC_AUTH) {
304 conf_new_access_rule(new_repo,
305 GOTD_ACCESS_DENIED, 0, $2);
310 repoopts2 : repoopts2 repoopts1 nl
311 | repoopts1 optnl
314 nl : '\n' optnl
317 optnl : '\n' optnl /* zero or more newlines */
318 | /* empty */
321 %%
323 struct keywords {
324 const char *k_name;
325 int k_val;
326 };
328 int
329 yyerror(const char *fmt, ...)
331 va_list ap;
332 char *msg;
334 file->errors++;
335 va_start(ap, fmt);
336 if (vasprintf(&msg, fmt, ap) == -1)
337 fatalx("yyerror vasprintf");
338 va_end(ap);
339 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
340 free(msg);
341 return (0);
344 int
345 kw_cmp(const void *k, const void *e)
347 return (strcmp(k, ((const struct keywords *)e)->k_name));
350 int
351 lookup(char *s)
353 /* This has to be sorted always. */
354 static const struct keywords keywords[] = {
355 { "connection", CONNECTION },
356 { "deny", DENY },
357 { "limit", LIMIT },
358 { "listen", LISTEN },
359 { "on", ON },
360 { "path", PATH },
361 { "permit", PERMIT },
362 { "repository", REPOSITORY },
363 { "request", REQUEST },
364 { "ro", RO },
365 { "rw", RW },
366 { "timeout", TIMEOUT },
367 { "user", USER },
368 };
369 const struct keywords *p;
371 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
372 sizeof(keywords[0]), kw_cmp);
374 if (p)
375 return (p->k_val);
376 else
377 return (STRING);
380 #define MAXPUSHBACK 128
382 unsigned char *parsebuf;
383 int parseindex;
384 unsigned char pushback_buffer[MAXPUSHBACK];
385 int pushback_index = 0;
387 int
388 lgetc(int quotec)
390 int c, next;
392 if (parsebuf) {
393 /* Read character from the parsebuffer instead of input. */
394 if (parseindex >= 0) {
395 c = parsebuf[parseindex++];
396 if (c != '\0')
397 return (c);
398 parsebuf = NULL;
399 } else
400 parseindex++;
403 if (pushback_index)
404 return (pushback_buffer[--pushback_index]);
406 if (quotec) {
407 c = getc(file->stream);
408 if (c == EOF)
409 yyerror("reached end of file while parsing "
410 "quoted string");
411 return (c);
414 c = getc(file->stream);
415 while (c == '\\') {
416 next = getc(file->stream);
417 if (next != '\n') {
418 c = next;
419 break;
421 yylval.lineno = file->lineno;
422 file->lineno++;
423 c = getc(file->stream);
426 return (c);
429 int
430 lungetc(int c)
432 if (c == EOF)
433 return (EOF);
434 if (parsebuf) {
435 parseindex--;
436 if (parseindex >= 0)
437 return (c);
439 if (pushback_index < MAXPUSHBACK-1)
440 return (pushback_buffer[pushback_index++] = c);
441 else
442 return (EOF);
445 int
446 findeol(void)
448 int c;
450 parsebuf = NULL;
452 /* Skip to either EOF or the first real EOL. */
453 while (1) {
454 if (pushback_index)
455 c = pushback_buffer[--pushback_index];
456 else
457 c = lgetc(0);
458 if (c == '\n') {
459 file->lineno++;
460 break;
462 if (c == EOF)
463 break;
465 return (ERROR);
468 int
469 yylex(void)
471 unsigned char buf[8096];
472 unsigned char *p, *val;
473 int quotec, next, c;
474 int token;
476 top:
477 p = buf;
478 c = lgetc(0);
479 while (c == ' ' || c == '\t')
480 c = lgetc(0); /* nothing */
482 yylval.lineno = file->lineno;
483 if (c == '#') {
484 c = lgetc(0);
485 while (c != '\n' && c != EOF)
486 c = lgetc(0); /* nothing */
488 if (c == '$' && parsebuf == NULL) {
489 while (1) {
490 c = lgetc(0);
491 if (c == EOF)
492 return (0);
494 if (p + 1 >= buf + sizeof(buf) - 1) {
495 yyerror("string too long");
496 return (findeol());
498 if (isalnum(c) || c == '_') {
499 *p++ = c;
500 continue;
502 *p = '\0';
503 lungetc(c);
504 break;
506 val = symget(buf);
507 if (val == NULL) {
508 yyerror("macro '%s' not defined", buf);
509 return (findeol());
511 parsebuf = val;
512 parseindex = 0;
513 goto top;
516 switch (c) {
517 case '\'':
518 case '"':
519 quotec = c;
520 while (1) {
521 c = lgetc(quotec);
522 if (c == EOF)
523 return (0);
524 if (c == '\n') {
525 file->lineno++;
526 continue;
527 } else if (c == '\\') {
528 next = lgetc(quotec);
529 if (next == EOF)
530 return (0);
531 if (next == quotec || c == ' ' || c == '\t')
532 c = next;
533 else if (next == '\n') {
534 file->lineno++;
535 continue;
536 } else
537 lungetc(next);
538 } else if (c == quotec) {
539 *p = '\0';
540 break;
541 } else if (c == '\0') {
542 yyerror("syntax error");
543 return (findeol());
545 if (p + 1 >= buf + sizeof(buf) - 1) {
546 yyerror("string too long");
547 return (findeol());
549 *p++ = c;
551 yylval.v.string = strdup(buf);
552 if (yylval.v.string == NULL)
553 err(1, "yylex: strdup");
554 return (STRING);
557 #define allowed_to_end_number(x) \
558 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
560 if (c == '-' || isdigit(c)) {
561 do {
562 *p++ = c;
563 if ((unsigned)(p-buf) >= sizeof(buf)) {
564 yyerror("string too long");
565 return (findeol());
567 c = lgetc(0);
568 } while (c != EOF && isdigit(c));
569 lungetc(c);
570 if (p == buf + 1 && buf[0] == '-')
571 goto nodigits;
572 if (c == EOF || allowed_to_end_number(c)) {
573 const char *errstr = NULL;
575 *p = '\0';
576 yylval.v.number = strtonum(buf, LLONG_MIN,
577 LLONG_MAX, &errstr);
578 if (errstr) {
579 yyerror("\"%s\" invalid number: %s",
580 buf, errstr);
581 return (findeol());
583 return (NUMBER);
584 } else {
585 nodigits:
586 while (p > buf + 1)
587 lungetc(*--p);
588 c = *--p;
589 if (c == '-')
590 return (c);
594 #define allowed_in_string(x) \
595 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
596 x != '{' && x != '}' && \
597 x != '!' && x != '=' && x != '#' && \
598 x != ','))
600 if (isalnum(c) || c == ':' || c == '_') {
601 do {
602 *p++ = c;
603 if ((unsigned)(p-buf) >= sizeof(buf)) {
604 yyerror("string too long");
605 return (findeol());
607 c = lgetc(0);
608 } while (c != EOF && (allowed_in_string(c)));
609 lungetc(c);
610 *p = '\0';
611 token = lookup(buf);
612 if (token == STRING) {
613 yylval.v.string = strdup(buf);
614 if (yylval.v.string == NULL)
615 err(1, "yylex: strdup");
617 return (token);
619 if (c == '\n') {
620 yylval.lineno = file->lineno;
621 file->lineno++;
623 if (c == EOF)
624 return (0);
625 return (c);
628 int
629 check_file_secrecy(int fd, const char *fname)
631 struct stat st;
633 if (fstat(fd, &st)) {
634 log_warn("cannot stat %s", fname);
635 return (-1);
637 if (st.st_uid != 0 && st.st_uid != getuid()) {
638 log_warnx("%s: owner not root or current user", fname);
639 return (-1);
641 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
642 log_warnx("%s: group writable or world read/writable", fname);
643 return (-1);
645 return (0);
648 struct file *
649 newfile(const char *name, int secret)
651 struct file *nfile;
653 nfile = calloc(1, sizeof(struct file));
654 if (nfile == NULL) {
655 log_warn("calloc");
656 return (NULL);
658 nfile->name = strdup(name);
659 if (nfile->name == NULL) {
660 log_warn("strdup");
661 free(nfile);
662 return (NULL);
664 nfile->stream = fopen(nfile->name, "r");
665 if (nfile->stream == NULL) {
666 /* no warning, we don't require a conf file */
667 free(nfile->name);
668 free(nfile);
669 return (NULL);
670 } else if (secret &&
671 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
672 fclose(nfile->stream);
673 free(nfile->name);
674 free(nfile);
675 return (NULL);
677 nfile->lineno = 1;
678 return (nfile);
681 static void
682 closefile(struct file *xfile)
684 fclose(xfile->stream);
685 free(xfile->name);
686 free(xfile);
689 int
690 parse_config(const char *filename, enum gotd_procid proc_id,
691 struct gotd *env)
693 struct sym *sym, *next;
694 struct gotd_repo *repo;
696 memset(env, 0, sizeof(*env));
698 gotd = env;
699 gotd_proc_id = proc_id;
700 TAILQ_INIT(&gotd->repos);
702 /* Apply default values. */
703 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
704 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
705 fprintf(stderr, "%s: unix socket path too long", __func__);
706 return -1;
708 if (strlcpy(gotd->user_name, GOTD_USER,
709 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
710 fprintf(stderr, "%s: user name too long", __func__);
711 return -1;
714 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
715 gotd->request_timeout.tv_usec = 0;
717 file = newfile(filename, 0);
718 if (file == NULL) {
719 /* just return, as we don't require a conf file */
720 return (0);
723 yyparse();
724 errors = file->errors;
725 closefile(file);
727 /* Free macros and check which have not been used. */
728 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
729 if ((gotd->verbosity > 1) && !sym->used)
730 fprintf(stderr, "warning: macro '%s' not used\n",
731 sym->nam);
732 if (!sym->persist) {
733 free(sym->nam);
734 free(sym->val);
735 TAILQ_REMOVE(&symhead, sym, entry);
736 free(sym);
740 if (errors)
741 return (-1);
743 TAILQ_FOREACH(repo, &gotd->repos, entry) {
744 if (repo->path[0] == '\0') {
745 log_warnx("repository \"%s\": no path provided in "
746 "configuration file", repo->name);
747 return (-1);
751 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
752 log_warnx("no repository defined in configuration file");
753 return (-1);
756 return (0);
759 static int
760 uid_connection_limit_cmp(const void *pa, const void *pb)
762 const struct gotd_uid_connection_limit *a = pa, *b = pb;
764 if (a->uid < b->uid)
765 return -1;
766 else if (a->uid > b->uid);
767 return 1;
769 return 0;
772 static int
773 conf_limit_user_connections(const char *user, int maximum)
775 uid_t uid;
776 struct gotd_uid_connection_limit *limit;
777 size_t nlimits;
779 if (maximum < 1) {
780 yyerror("max connections cannot be smaller 1");
781 return -1;
783 if (maximum > GOTD_MAXCLIENTS) {
784 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
785 return -1;
788 if (gotd_auth_parseuid(user, &uid) == -1) {
789 yyerror("%s: no such user", user);
790 return -1;
793 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
794 gotd->nconnection_limits, uid);
795 if (limit) {
796 limit->max_connections = maximum;
797 return 0;
800 limit = gotd->connection_limits;
801 nlimits = gotd->nconnection_limits + 1;
802 limit = reallocarray(limit, nlimits, sizeof(*limit));
803 if (limit == NULL)
804 fatal("reallocarray");
806 limit[nlimits - 1].uid = uid;
807 limit[nlimits - 1].max_connections = maximum;
809 gotd->connection_limits = limit;
810 gotd->nconnection_limits = nlimits;
811 qsort(gotd->connection_limits, gotd->nconnection_limits,
812 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
814 return 0;
817 static struct gotd_repo *
818 conf_new_repo(const char *name)
820 struct gotd_repo *repo;
822 if (name[0] == '\0') {
823 fatalx("syntax error: empty repository name found in %s",
824 file->name);
827 if (strchr(name, '\n') != NULL)
828 fatalx("repository names must not contain linefeeds: %s", name);
830 repo = calloc(1, sizeof(*repo));
831 if (repo == NULL)
832 fatalx("%s: calloc", __func__);
834 STAILQ_INIT(&repo->rules);
836 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
837 sizeof(repo->name))
838 fatalx("%s: strlcpy", __func__);
840 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
841 gotd->nrepos++;
843 return repo;
844 };
846 static void
847 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
848 int authorization, char *identifier)
850 struct gotd_access_rule *rule;
852 rule = calloc(1, sizeof(*rule));
853 if (rule == NULL)
854 fatal("calloc");
856 rule->access = access;
857 rule->authorization = authorization;
858 rule->identifier = identifier;
860 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
863 int
864 symset(const char *nam, const char *val, int persist)
866 struct sym *sym;
868 TAILQ_FOREACH(sym, &symhead, entry) {
869 if (strcmp(nam, sym->nam) == 0)
870 break;
873 if (sym != NULL) {
874 if (sym->persist == 1)
875 return (0);
876 else {
877 free(sym->nam);
878 free(sym->val);
879 TAILQ_REMOVE(&symhead, sym, entry);
880 free(sym);
883 sym = calloc(1, sizeof(*sym));
884 if (sym == NULL)
885 return (-1);
887 sym->nam = strdup(nam);
888 if (sym->nam == NULL) {
889 free(sym);
890 return (-1);
892 sym->val = strdup(val);
893 if (sym->val == NULL) {
894 free(sym->nam);
895 free(sym);
896 return (-1);
898 sym->used = 0;
899 sym->persist = persist;
900 TAILQ_INSERT_TAIL(&symhead, sym, entry);
901 return (0);
904 char *
905 symget(const char *nam)
907 struct sym *sym;
909 TAILQ_FOREACH(sym, &symhead, entry) {
910 if (strcmp(nam, sym->nam) == 0) {
911 sym->used = 1;
912 return (sym->val);
915 return (NULL);