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.tv> timeout
114 %%
116 grammar :
117 | grammar '\n'
118 | grammar main '\n'
119 | grammar repository '\n'
122 timeout : NUMBER {
123 if ($1 < 0) {
124 yyerror("invalid timeout: %lld", $1);
125 YYERROR;
127 $$.tv_sec = $1;
128 $$.tv_usec = 0;
130 | STRING {
131 const char *errstr;
132 const char *type = "seconds";
133 size_t len;
134 int mul = 1;
136 if (*$1 == '\0') {
137 yyerror("invalid number of seconds: %s", $1);
138 free($1);
139 YYERROR;
142 len = strlen($1);
143 switch ($1[len - 1]) {
144 case 'S':
145 case 's':
146 $1[len - 1] = '\0';
147 break;
148 case 'M':
149 case 'm':
150 type = "minutes";
151 mul = 60;
152 $1[len - 1] = '\0';
153 break;
154 case 'H':
155 case 'h':
156 type = "hours";
157 mul = 60 * 60;
158 $1[len - 1] = '\0';
159 break;
162 $$.tv_usec = 0;
163 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
164 if (errstr) {
165 yyerror("number of %s is %s: %s", type,
166 errstr, $1);
167 free($1);
168 YYERROR;
171 $$.tv_sec *= mul;
172 free($1);
176 main : LISTEN ON STRING {
177 if (!got_path_is_absolute($3))
178 yyerror("bad unix socket path \"%s\": "
179 "must be an absolute path", $3);
181 if (gotd_proc_id == PROC_LISTEN) {
182 if (strlcpy(gotd->unix_socket_path, $3,
183 sizeof(gotd->unix_socket_path)) >=
184 sizeof(gotd->unix_socket_path)) {
185 yyerror("%s: unix socket path too long",
186 __func__);
187 free($3);
188 YYERROR;
191 free($3);
193 | USER STRING {
194 if (strlcpy(gotd->user_name, $2,
195 sizeof(gotd->user_name)) >=
196 sizeof(gotd->user_name)) {
197 yyerror("%s: user name too long", __func__);
198 free($2);
199 YYERROR;
201 free($2);
203 | connection
206 connection : CONNECTION '{' optnl conflags_l '}'
207 | CONNECTION conflags
209 conflags_l : conflags optnl conflags_l
210 | conflags optnl
213 conflags : REQUEST TIMEOUT timeout {
214 if ($3.tv_sec <= 0) {
215 yyerror("invalid timeout: %lld", $3.tv_sec);
216 YYERROR;
218 memcpy(&gotd->request_timeout, &$3,
219 sizeof(gotd->request_timeout));
221 | LIMIT USER STRING NUMBER {
222 if (gotd_proc_id == PROC_LISTEN &&
223 conf_limit_user_connections($3, $4) == -1) {
224 free($3);
225 YYERROR;
227 free($3);
231 repository : REPOSITORY STRING {
232 struct gotd_repo *repo;
234 TAILQ_FOREACH(repo, &gotd->repos, entry) {
235 if (strcmp(repo->name, $2) == 0) {
236 yyerror("duplicate repository '%s'", $2);
237 free($2);
238 YYERROR;
242 if (gotd_proc_id == PROC_GOTD ||
243 gotd_proc_id == PROC_AUTH) {
244 new_repo = conf_new_repo($2);
246 free($2);
247 } '{' optnl repoopts2 '}' {
251 repoopts1 : PATH STRING {
252 if (gotd_proc_id == PROC_GOTD ||
253 gotd_proc_id == PROC_AUTH) {
254 if (!got_path_is_absolute($2)) {
255 yyerror("%s: path %s is not absolute",
256 __func__, $2);
257 free($2);
258 YYERROR;
260 if (realpath($2, new_repo->path) == NULL) {
261 yyerror("realpath %s: %s", $2, strerror(errno));
262 free($2);
263 YYERROR;
266 free($2);
268 | PERMIT RO STRING {
269 if (gotd_proc_id == PROC_AUTH) {
270 conf_new_access_rule(new_repo,
271 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
274 | PERMIT RW STRING {
275 if (gotd_proc_id == PROC_AUTH) {
276 conf_new_access_rule(new_repo,
277 GOTD_ACCESS_PERMITTED,
278 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
281 | DENY STRING {
282 if (gotd_proc_id == PROC_AUTH) {
283 conf_new_access_rule(new_repo,
284 GOTD_ACCESS_DENIED, 0, $2);
289 repoopts2 : repoopts2 repoopts1 nl
290 | repoopts1 optnl
293 nl : '\n' optnl
296 optnl : '\n' optnl /* zero or more newlines */
297 | /* empty */
300 %%
302 struct keywords {
303 const char *k_name;
304 int k_val;
305 };
307 int
308 yyerror(const char *fmt, ...)
310 va_list ap;
311 char *msg;
313 file->errors++;
314 va_start(ap, fmt);
315 if (vasprintf(&msg, fmt, ap) == -1)
316 fatalx("yyerror vasprintf");
317 va_end(ap);
318 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
319 free(msg);
320 return (0);
323 int
324 kw_cmp(const void *k, const void *e)
326 return (strcmp(k, ((const struct keywords *)e)->k_name));
329 int
330 lookup(char *s)
332 /* This has to be sorted always. */
333 static const struct keywords keywords[] = {
334 { "connection", CONNECTION },
335 { "deny", DENY },
336 { "limit", LIMIT },
337 { "listen", LISTEN },
338 { "on", ON },
339 { "path", PATH },
340 { "permit", PERMIT },
341 { "repository", REPOSITORY },
342 { "request", REQUEST },
343 { "ro", RO },
344 { "rw", RW },
345 { "timeout", TIMEOUT },
346 { "user", USER },
347 };
348 const struct keywords *p;
350 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
351 sizeof(keywords[0]), kw_cmp);
353 if (p)
354 return (p->k_val);
355 else
356 return (STRING);
359 #define MAXPUSHBACK 128
361 unsigned char *parsebuf;
362 int parseindex;
363 unsigned char pushback_buffer[MAXPUSHBACK];
364 int pushback_index = 0;
366 int
367 lgetc(int quotec)
369 int c, next;
371 if (parsebuf) {
372 /* Read character from the parsebuffer instead of input. */
373 if (parseindex >= 0) {
374 c = parsebuf[parseindex++];
375 if (c != '\0')
376 return (c);
377 parsebuf = NULL;
378 } else
379 parseindex++;
382 if (pushback_index)
383 return (pushback_buffer[--pushback_index]);
385 if (quotec) {
386 c = getc(file->stream);
387 if (c == EOF)
388 yyerror("reached end of file while parsing "
389 "quoted string");
390 return (c);
393 c = getc(file->stream);
394 while (c == '\\') {
395 next = getc(file->stream);
396 if (next != '\n') {
397 c = next;
398 break;
400 yylval.lineno = file->lineno;
401 file->lineno++;
402 c = getc(file->stream);
405 return (c);
408 int
409 lungetc(int c)
411 if (c == EOF)
412 return (EOF);
413 if (parsebuf) {
414 parseindex--;
415 if (parseindex >= 0)
416 return (c);
418 if (pushback_index < MAXPUSHBACK-1)
419 return (pushback_buffer[pushback_index++] = c);
420 else
421 return (EOF);
424 int
425 findeol(void)
427 int c;
429 parsebuf = NULL;
431 /* Skip to either EOF or the first real EOL. */
432 while (1) {
433 if (pushback_index)
434 c = pushback_buffer[--pushback_index];
435 else
436 c = lgetc(0);
437 if (c == '\n') {
438 file->lineno++;
439 break;
441 if (c == EOF)
442 break;
444 return (ERROR);
447 int
448 yylex(void)
450 unsigned char buf[8096];
451 unsigned char *p, *val;
452 int quotec, next, c;
453 int token;
455 top:
456 p = buf;
457 c = lgetc(0);
458 while (c == ' ' || c == '\t')
459 c = lgetc(0); /* nothing */
461 yylval.lineno = file->lineno;
462 if (c == '#') {
463 c = lgetc(0);
464 while (c != '\n' && c != EOF)
465 c = lgetc(0); /* nothing */
467 if (c == '$' && parsebuf == NULL) {
468 while (1) {
469 c = lgetc(0);
470 if (c == EOF)
471 return (0);
473 if (p + 1 >= buf + sizeof(buf) - 1) {
474 yyerror("string too long");
475 return (findeol());
477 if (isalnum(c) || c == '_') {
478 *p++ = c;
479 continue;
481 *p = '\0';
482 lungetc(c);
483 break;
485 val = symget(buf);
486 if (val == NULL) {
487 yyerror("macro '%s' not defined", buf);
488 return (findeol());
490 parsebuf = val;
491 parseindex = 0;
492 goto top;
495 switch (c) {
496 case '\'':
497 case '"':
498 quotec = c;
499 while (1) {
500 c = lgetc(quotec);
501 if (c == EOF)
502 return (0);
503 if (c == '\n') {
504 file->lineno++;
505 continue;
506 } else if (c == '\\') {
507 next = lgetc(quotec);
508 if (next == EOF)
509 return (0);
510 if (next == quotec || c == ' ' || c == '\t')
511 c = next;
512 else if (next == '\n') {
513 file->lineno++;
514 continue;
515 } else
516 lungetc(next);
517 } else if (c == quotec) {
518 *p = '\0';
519 break;
520 } else if (c == '\0') {
521 yyerror("syntax error");
522 return (findeol());
524 if (p + 1 >= buf + sizeof(buf) - 1) {
525 yyerror("string too long");
526 return (findeol());
528 *p++ = c;
530 yylval.v.string = strdup(buf);
531 if (yylval.v.string == NULL)
532 err(1, "yylex: strdup");
533 return (STRING);
536 #define allowed_to_end_number(x) \
537 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
539 if (c == '-' || isdigit(c)) {
540 do {
541 *p++ = c;
542 if ((unsigned)(p-buf) >= sizeof(buf)) {
543 yyerror("string too long");
544 return (findeol());
546 c = lgetc(0);
547 } while (c != EOF && isdigit(c));
548 lungetc(c);
549 if (p == buf + 1 && buf[0] == '-')
550 goto nodigits;
551 if (c == EOF || allowed_to_end_number(c)) {
552 const char *errstr = NULL;
554 *p = '\0';
555 yylval.v.number = strtonum(buf, LLONG_MIN,
556 LLONG_MAX, &errstr);
557 if (errstr) {
558 yyerror("\"%s\" invalid number: %s",
559 buf, errstr);
560 return (findeol());
562 return (NUMBER);
563 } else {
564 nodigits:
565 while (p > buf + 1)
566 lungetc(*--p);
567 c = *--p;
568 if (c == '-')
569 return (c);
573 #define allowed_in_string(x) \
574 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
575 x != '{' && x != '}' && \
576 x != '!' && x != '=' && x != '#' && \
577 x != ','))
579 if (isalnum(c) || c == ':' || c == '_') {
580 do {
581 *p++ = c;
582 if ((unsigned)(p-buf) >= sizeof(buf)) {
583 yyerror("string too long");
584 return (findeol());
586 c = lgetc(0);
587 } while (c != EOF && (allowed_in_string(c)));
588 lungetc(c);
589 *p = '\0';
590 token = lookup(buf);
591 if (token == STRING) {
592 yylval.v.string = strdup(buf);
593 if (yylval.v.string == NULL)
594 err(1, "yylex: strdup");
596 return (token);
598 if (c == '\n') {
599 yylval.lineno = file->lineno;
600 file->lineno++;
602 if (c == EOF)
603 return (0);
604 return (c);
607 int
608 check_file_secrecy(int fd, const char *fname)
610 struct stat st;
612 if (fstat(fd, &st)) {
613 log_warn("cannot stat %s", fname);
614 return (-1);
616 if (st.st_uid != 0 && st.st_uid != getuid()) {
617 log_warnx("%s: owner not root or current user", fname);
618 return (-1);
620 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
621 log_warnx("%s: group writable or world read/writable", fname);
622 return (-1);
624 return (0);
627 struct file *
628 newfile(const char *name, int secret)
630 struct file *nfile;
632 nfile = calloc(1, sizeof(struct file));
633 if (nfile == NULL) {
634 log_warn("calloc");
635 return (NULL);
637 nfile->name = strdup(name);
638 if (nfile->name == NULL) {
639 log_warn("strdup");
640 free(nfile);
641 return (NULL);
643 nfile->stream = fopen(nfile->name, "r");
644 if (nfile->stream == NULL) {
645 /* no warning, we don't require a conf file */
646 free(nfile->name);
647 free(nfile);
648 return (NULL);
649 } else if (secret &&
650 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
651 fclose(nfile->stream);
652 free(nfile->name);
653 free(nfile);
654 return (NULL);
656 nfile->lineno = 1;
657 return (nfile);
660 static void
661 closefile(struct file *xfile)
663 fclose(xfile->stream);
664 free(xfile->name);
665 free(xfile);
668 int
669 parse_config(const char *filename, enum gotd_procid proc_id,
670 struct gotd *env)
672 struct sym *sym, *next;
673 struct gotd_repo *repo;
675 memset(env, 0, sizeof(*env));
677 gotd = env;
678 gotd_proc_id = proc_id;
679 TAILQ_INIT(&gotd->repos);
681 /* Apply default values. */
682 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
683 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
684 fprintf(stderr, "%s: unix socket path too long", __func__);
685 return -1;
687 if (strlcpy(gotd->user_name, GOTD_USER,
688 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
689 fprintf(stderr, "%s: user name too long", __func__);
690 return -1;
693 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
694 gotd->request_timeout.tv_usec = 0;
696 file = newfile(filename, 0);
697 if (file == NULL) {
698 /* just return, as we don't require a conf file */
699 return (0);
702 yyparse();
703 errors = file->errors;
704 closefile(file);
706 /* Free macros and check which have not been used. */
707 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
708 if ((gotd->verbosity > 1) && !sym->used)
709 fprintf(stderr, "warning: macro '%s' not used\n",
710 sym->nam);
711 if (!sym->persist) {
712 free(sym->nam);
713 free(sym->val);
714 TAILQ_REMOVE(&symhead, sym, entry);
715 free(sym);
719 if (errors)
720 return (-1);
722 TAILQ_FOREACH(repo, &gotd->repos, entry) {
723 if (repo->path[0] == '\0') {
724 log_warnx("repository \"%s\": no path provided in "
725 "configuration file", repo->name);
726 return (-1);
730 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
731 log_warnx("no repository defined in configuration file");
732 return (-1);
735 return (0);
738 static int
739 uid_connection_limit_cmp(const void *pa, const void *pb)
741 const struct gotd_uid_connection_limit *a = pa, *b = pb;
743 if (a->uid < b->uid)
744 return -1;
745 else if (a->uid > b->uid);
746 return 1;
748 return 0;
751 static int
752 conf_limit_user_connections(const char *user, int maximum)
754 uid_t uid;
755 struct gotd_uid_connection_limit *limit;
756 size_t nlimits;
758 if (maximum < 1) {
759 yyerror("max connections cannot be smaller 1");
760 return -1;
762 if (maximum > GOTD_MAXCLIENTS) {
763 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
764 return -1;
767 if (gotd_auth_parseuid(user, &uid) == -1) {
768 yyerror("%s: no such user", user);
769 return -1;
772 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
773 gotd->nconnection_limits, uid);
774 if (limit) {
775 limit->max_connections = maximum;
776 return 0;
779 limit = gotd->connection_limits;
780 nlimits = gotd->nconnection_limits + 1;
781 limit = reallocarray(limit, nlimits, sizeof(*limit));
782 if (limit == NULL)
783 fatal("reallocarray");
785 limit[nlimits - 1].uid = uid;
786 limit[nlimits - 1].max_connections = maximum;
788 gotd->connection_limits = limit;
789 gotd->nconnection_limits = nlimits;
790 qsort(gotd->connection_limits, gotd->nconnection_limits,
791 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
793 return 0;
796 static struct gotd_repo *
797 conf_new_repo(const char *name)
799 struct gotd_repo *repo;
801 if (name[0] == '\0') {
802 fatalx("syntax error: empty repository name found in %s",
803 file->name);
806 if (strchr(name, '\n') != NULL)
807 fatalx("repository names must not contain linefeeds: %s", name);
809 repo = calloc(1, sizeof(*repo));
810 if (repo == NULL)
811 fatalx("%s: calloc", __func__);
813 STAILQ_INIT(&repo->rules);
815 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
816 sizeof(repo->name))
817 fatalx("%s: strlcpy", __func__);
819 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
820 gotd->nrepos++;
822 return repo;
823 };
825 static void
826 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
827 int authorization, char *identifier)
829 struct gotd_access_rule *rule;
831 rule = calloc(1, sizeof(*rule));
832 if (rule == NULL)
833 fatal("calloc");
835 rule->access = access;
836 rule->authorization = authorization;
837 rule->identifier = identifier;
839 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
842 int
843 symset(const char *nam, const char *val, int persist)
845 struct sym *sym;
847 TAILQ_FOREACH(sym, &symhead, entry) {
848 if (strcmp(nam, sym->nam) == 0)
849 break;
852 if (sym != NULL) {
853 if (sym->persist == 1)
854 return (0);
855 else {
856 free(sym->nam);
857 free(sym->val);
858 TAILQ_REMOVE(&symhead, sym, entry);
859 free(sym);
862 sym = calloc(1, sizeof(*sym));
863 if (sym == NULL)
864 return (-1);
866 sym->nam = strdup(nam);
867 if (sym->nam == NULL) {
868 free(sym);
869 return (-1);
871 sym->val = strdup(val);
872 if (sym->val == NULL) {
873 free(sym->nam);
874 free(sym);
875 return (-1);
877 sym->used = 0;
878 sym->persist = persist;
879 TAILQ_INSERT_TAIL(&symhead, sym, entry);
880 return (0);
883 char *
884 symget(const char *nam)
886 struct sym *sym;
888 TAILQ_FOREACH(sym, &symhead, entry) {
889 if (strcmp(nam, sym->nam) == 0) {
890 sym->used = 1;
891 return (sym->val);
894 return (NULL);