portable: release 0.101
[got-portable.git] / gotd / parse.y
blobe54d1cdb100569c2221f6253b0e72a1f14710991
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.
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.
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.
25 #include "got_compat.h"
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/queue.h>
30 #include <sys/stat.h>
32 #include <ctype.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <event.h>
36 #include <imsg.h>
37 #include <limits.h>
38 #include <pwd.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <syslog.h>
44 #include <unistd.h>
46 #include "got_error.h"
47 #include "got_object.h"
48 #include "got_path.h"
49 #include "got_reference.h"
51 #include "log.h"
52 #include "gotd.h"
53 #include "auth.h"
54 #include "listen.h"
56 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
57 static struct file {
58 TAILQ_ENTRY(file) entry;
59 FILE *stream;
60 char *name;
61 int lineno;
62 int errors;
63 } *file;
64 struct file *newfile(const char *, int, int);
65 static void closefile(struct file *);
66 int check_file_secrecy(int, const char *);
67 int yyparse(void);
68 int yylex(void);
69 int yyerror(const char *, ...)
70 __attribute__((__format__ (printf, 1, 2)))
71 __attribute__((__nonnull__ (1)));
72 int kw_cmp(const void *, const void *);
73 int lookup(char *);
74 int lgetc(int);
75 int lungetc(int);
76 int findeol(void);
77 static char *port_sprintf(int);
79 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
80 struct sym {
81 TAILQ_ENTRY(sym) entry;
82 int used;
83 int persist;
84 char *nam;
85 char *val;
88 int symset(const char *, const char *, int);
89 char *symget(const char *);
91 static int errors;
93 static struct gotd *gotd;
94 static struct gotd_repo *new_repo;
95 static int conf_limit_user_connections(const char *, int);
96 static struct gotd_repo *conf_new_repo(const char *);
97 static void conf_new_access_rule(struct gotd_repo *,
98 enum gotd_access, int, char *);
99 static int conf_protect_ref_namespace(char **,
100 struct got_pathlist_head *, char *);
101 static int conf_protect_tag_namespace(struct gotd_repo *,
102 char *);
103 static int conf_protect_branch_namespace(
104 struct gotd_repo *, char *);
105 static int conf_protect_branch(struct gotd_repo *,
106 char *);
107 static int conf_notify_branch(struct gotd_repo *,
108 char *);
109 static int conf_notify_ref_namespace(struct gotd_repo *,
110 char *);
111 static int conf_notify_email(struct gotd_repo *,
112 char *, char *, char *, char *, char *);
113 static int conf_notify_http(struct gotd_repo *,
114 char *, char *, char *, int);
115 static enum gotd_procid gotd_proc_id;
117 typedef struct {
118 union {
119 long long number;
120 char *string;
121 struct timeval tv;
122 } v;
123 int lineno;
124 } YYSTYPE;
128 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
129 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
130 %token PROTECT NAMESPACE BRANCH TAG REFERENCE RELAY PORT
131 %token NOTIFY EMAIL FROM REPLY TO URL PASSWORD INSECURE
133 %token <v.string> STRING
134 %token <v.number> NUMBER
135 %type <v.tv> timeout
139 grammar :
140 | grammar '\n'
141 | grammar varset '\n'
142 | grammar main '\n'
143 | grammar repository '\n'
146 varset : STRING '=' STRING {
147 char *s = $1;
148 while (*s++) {
149 if (isspace((unsigned char)*s)) {
150 yyerror("macro name cannot contain "
151 "whitespace");
152 free($1);
153 free($3);
154 YYERROR;
157 if (symset($1, $3, 0) == -1)
158 fatal("cannot store variable");
159 free($1);
160 free($3);
164 timeout : NUMBER {
165 if ($1 < 0) {
166 yyerror("invalid timeout: %lld", $1);
167 YYERROR;
169 $$.tv_sec = $1;
170 $$.tv_usec = 0;
172 | STRING {
173 const char *errstr;
174 const char *type = "seconds";
175 size_t len;
176 int mul = 1;
178 if (*$1 == '\0') {
179 yyerror("invalid number of seconds: %s", $1);
180 free($1);
181 YYERROR;
184 len = strlen($1);
185 switch ($1[len - 1]) {
186 case 'S':
187 case 's':
188 $1[len - 1] = '\0';
189 break;
190 case 'M':
191 case 'm':
192 type = "minutes";
193 mul = 60;
194 $1[len - 1] = '\0';
195 break;
196 case 'H':
197 case 'h':
198 type = "hours";
199 mul = 60 * 60;
200 $1[len - 1] = '\0';
201 break;
204 $$.tv_usec = 0;
205 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
206 if (errstr) {
207 yyerror("number of %s is %s: %s", type,
208 errstr, $1);
209 free($1);
210 YYERROR;
213 $$.tv_sec *= mul;
214 free($1);
218 main : LISTEN ON STRING {
219 if (!got_path_is_absolute($3))
220 yyerror("bad unix socket path \"%s\": "
221 "must be an absolute path", $3);
223 if (gotd_proc_id == PROC_LISTEN) {
224 if (strlcpy(gotd->unix_socket_path, $3,
225 sizeof(gotd->unix_socket_path)) >=
226 sizeof(gotd->unix_socket_path)) {
227 yyerror("%s: unix socket path too long",
228 __func__);
229 free($3);
230 YYERROR;
233 free($3);
235 | USER STRING {
236 if (strlcpy(gotd->user_name, $2,
237 sizeof(gotd->user_name)) >=
238 sizeof(gotd->user_name)) {
239 yyerror("%s: user name too long", __func__);
240 free($2);
241 YYERROR;
243 free($2);
245 | connection
248 connection : CONNECTION '{' optnl conflags_l '}'
249 | CONNECTION conflags
251 conflags_l : conflags optnl conflags_l
252 | conflags optnl
255 conflags : REQUEST TIMEOUT timeout {
256 if ($3.tv_sec <= 0) {
257 yyerror("invalid timeout: %lld",
258 (long long)$3.tv_sec);
259 YYERROR;
261 memcpy(&gotd->request_timeout, &$3,
262 sizeof(gotd->request_timeout));
264 | LIMIT USER STRING NUMBER {
265 if (gotd_proc_id == PROC_LISTEN &&
266 conf_limit_user_connections($3, $4) == -1) {
267 free($3);
268 YYERROR;
270 free($3);
274 protect : PROTECT '{' optnl protectflags_l '}'
275 | PROTECT protectflags
277 protectflags_l : protectflags optnl protectflags_l
278 | protectflags optnl
281 protectflags : TAG NAMESPACE STRING {
282 if (gotd_proc_id == PROC_GOTD ||
283 gotd_proc_id == PROC_REPO_WRITE) {
284 if (conf_protect_tag_namespace(new_repo, $3)) {
285 free($3);
286 YYERROR;
289 free($3);
291 | BRANCH NAMESPACE STRING {
292 if (gotd_proc_id == PROC_GOTD ||
293 gotd_proc_id == PROC_REPO_WRITE) {
294 if (conf_protect_branch_namespace(new_repo,
295 $3)) {
296 free($3);
297 YYERROR;
300 free($3);
302 | BRANCH STRING {
303 if (gotd_proc_id == PROC_GOTD ||
304 gotd_proc_id == PROC_REPO_WRITE) {
305 if (conf_protect_branch(new_repo, $2)) {
306 free($2);
307 YYERROR;
310 free($2);
314 notify : NOTIFY '{' optnl notifyflags_l '}'
315 | NOTIFY notifyflags
317 notifyflags_l : notifyflags optnl notifyflags_l
318 | notifyflags optnl
321 notifyflags : BRANCH STRING {
322 if (gotd_proc_id == PROC_GOTD ||
323 gotd_proc_id == PROC_SESSION_WRITE ||
324 gotd_proc_id == PROC_NOTIFY) {
325 if (conf_notify_branch(new_repo, $2)) {
326 free($2);
327 YYERROR;
330 free($2);
332 | REFERENCE NAMESPACE STRING {
333 if (gotd_proc_id == PROC_GOTD ||
334 gotd_proc_id == PROC_SESSION_WRITE ||
335 gotd_proc_id == PROC_NOTIFY) {
336 if (conf_notify_ref_namespace(new_repo, $3)) {
337 free($3);
338 YYERROR;
341 free($3);
343 | EMAIL TO STRING {
344 if (gotd_proc_id == PROC_GOTD ||
345 gotd_proc_id == PROC_SESSION_WRITE ||
346 gotd_proc_id == PROC_NOTIFY) {
347 if (conf_notify_email(new_repo, NULL, $3,
348 NULL, NULL, NULL)) {
349 free($3);
350 YYERROR;
353 free($3);
355 | EMAIL FROM STRING TO STRING {
356 if (gotd_proc_id == PROC_GOTD ||
357 gotd_proc_id == PROC_SESSION_WRITE ||
358 gotd_proc_id == PROC_NOTIFY) {
359 if (conf_notify_email(new_repo, $3, $5,
360 NULL, NULL, NULL)) {
361 free($3);
362 free($5);
363 YYERROR;
366 free($3);
367 free($5);
369 | EMAIL TO STRING REPLY TO STRING {
370 if (gotd_proc_id == PROC_GOTD ||
371 gotd_proc_id == PROC_SESSION_WRITE ||
372 gotd_proc_id == PROC_NOTIFY) {
373 if (conf_notify_email(new_repo, NULL, $3,
374 $6, NULL, NULL)) {
375 free($3);
376 free($6);
377 YYERROR;
380 free($3);
381 free($6);
383 | EMAIL FROM STRING TO STRING REPLY TO STRING {
384 if (gotd_proc_id == PROC_GOTD ||
385 gotd_proc_id == PROC_SESSION_WRITE ||
386 gotd_proc_id == PROC_NOTIFY) {
387 if (conf_notify_email(new_repo, $3, $5,
388 $8, NULL, NULL)) {
389 free($3);
390 free($5);
391 free($8);
392 YYERROR;
395 free($3);
396 free($5);
397 free($8);
399 | EMAIL TO STRING RELAY STRING {
400 if (gotd_proc_id == PROC_GOTD ||
401 gotd_proc_id == PROC_SESSION_WRITE ||
402 gotd_proc_id == PROC_NOTIFY) {
403 if (conf_notify_email(new_repo, NULL, $3,
404 NULL, $5, NULL)) {
405 free($3);
406 free($5);
407 YYERROR;
410 free($3);
411 free($5);
413 | EMAIL FROM STRING TO STRING RELAY STRING {
414 if (gotd_proc_id == PROC_GOTD ||
415 gotd_proc_id == PROC_SESSION_WRITE ||
416 gotd_proc_id == PROC_NOTIFY) {
417 if (conf_notify_email(new_repo, $3, $5,
418 NULL, $7, NULL)) {
419 free($3);
420 free($5);
421 free($7);
422 YYERROR;
425 free($3);
426 free($5);
427 free($7);
429 | EMAIL TO STRING REPLY TO STRING RELAY STRING {
430 if (gotd_proc_id == PROC_GOTD ||
431 gotd_proc_id == PROC_SESSION_WRITE ||
432 gotd_proc_id == PROC_NOTIFY) {
433 if (conf_notify_email(new_repo, NULL, $3,
434 $6, $8, NULL)) {
435 free($3);
436 free($6);
437 free($8);
438 YYERROR;
441 free($3);
442 free($6);
443 free($8);
445 | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING {
446 if (gotd_proc_id == PROC_GOTD ||
447 gotd_proc_id == PROC_SESSION_WRITE ||
448 gotd_proc_id == PROC_NOTIFY) {
449 if (conf_notify_email(new_repo, $3, $5,
450 $8, $10, NULL)) {
451 free($3);
452 free($5);
453 free($8);
454 free($10);
455 YYERROR;
458 free($3);
459 free($5);
460 free($8);
461 free($10);
463 | EMAIL TO STRING RELAY STRING PORT STRING {
464 if (gotd_proc_id == PROC_GOTD ||
465 gotd_proc_id == PROC_SESSION_WRITE ||
466 gotd_proc_id == PROC_NOTIFY) {
467 if (conf_notify_email(new_repo, NULL, $3,
468 NULL, $5, $7)) {
469 free($3);
470 free($5);
471 free($7);
472 YYERROR;
475 free($3);
476 free($5);
477 free($7);
479 | EMAIL FROM STRING TO STRING RELAY STRING PORT STRING {
480 if (gotd_proc_id == PROC_GOTD ||
481 gotd_proc_id == PROC_SESSION_WRITE ||
482 gotd_proc_id == PROC_NOTIFY) {
483 if (conf_notify_email(new_repo, $3, $5,
484 NULL, $7, $9)) {
485 free($3);
486 free($5);
487 free($7);
488 free($9);
489 YYERROR;
492 free($3);
493 free($5);
494 free($7);
495 free($9);
497 | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT STRING {
498 if (gotd_proc_id == PROC_GOTD ||
499 gotd_proc_id == PROC_SESSION_WRITE ||
500 gotd_proc_id == PROC_NOTIFY) {
501 if (conf_notify_email(new_repo, NULL, $3,
502 $6, $8, $10)) {
503 free($3);
504 free($6);
505 free($8);
506 free($10);
507 YYERROR;
510 free($3);
511 free($6);
512 free($8);
513 free($10);
515 | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT STRING {
516 if (gotd_proc_id == PROC_GOTD ||
517 gotd_proc_id == PROC_SESSION_WRITE ||
518 gotd_proc_id == PROC_NOTIFY) {
519 if (conf_notify_email(new_repo, $3, $5,
520 $8, $10, $12)) {
521 free($3);
522 free($5);
523 free($8);
524 free($10);
525 free($12);
526 YYERROR;
529 free($3);
530 free($5);
531 free($8);
532 free($10);
533 free($12);
535 | EMAIL TO STRING RELAY STRING PORT NUMBER {
536 if (gotd_proc_id == PROC_GOTD ||
537 gotd_proc_id == PROC_SESSION_WRITE ||
538 gotd_proc_id == PROC_NOTIFY) {
539 if (conf_notify_email(new_repo, NULL, $3,
540 NULL, $5, port_sprintf($7))) {
541 free($3);
542 free($5);
543 YYERROR;
546 free($3);
547 free($5);
549 | EMAIL FROM STRING TO STRING RELAY STRING PORT NUMBER {
550 if (gotd_proc_id == PROC_GOTD ||
551 gotd_proc_id == PROC_SESSION_WRITE ||
552 gotd_proc_id == PROC_NOTIFY) {
553 if (conf_notify_email(new_repo, $3, $5,
554 NULL, $7, port_sprintf($9))) {
555 free($3);
556 free($5);
557 free($7);
558 YYERROR;
561 free($3);
562 free($5);
563 free($7);
565 | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
566 if (gotd_proc_id == PROC_GOTD ||
567 gotd_proc_id == PROC_SESSION_WRITE ||
568 gotd_proc_id == PROC_NOTIFY) {
569 if (conf_notify_email(new_repo, NULL, $3,
570 $6, $8, port_sprintf($10))) {
571 free($3);
572 free($6);
573 free($8);
574 YYERROR;
577 free($3);
578 free($6);
579 free($8);
581 | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
582 if (gotd_proc_id == PROC_GOTD ||
583 gotd_proc_id == PROC_SESSION_WRITE ||
584 gotd_proc_id == PROC_NOTIFY) {
585 if (conf_notify_email(new_repo, $3, $5,
586 $8, $10, port_sprintf($12))) {
587 free($3);
588 free($5);
589 free($8);
590 free($10);
591 YYERROR;
594 free($3);
595 free($5);
596 free($8);
597 free($10);
599 | URL STRING {
600 if (gotd_proc_id == PROC_GOTD ||
601 gotd_proc_id == PROC_SESSION_WRITE ||
602 gotd_proc_id == PROC_NOTIFY) {
603 if (conf_notify_http(new_repo, $2, NULL,
604 NULL, 0)) {
605 free($2);
606 YYERROR;
609 free($2);
611 | URL STRING USER STRING PASSWORD STRING {
612 if (gotd_proc_id == PROC_GOTD ||
613 gotd_proc_id == PROC_SESSION_WRITE ||
614 gotd_proc_id == PROC_NOTIFY) {
615 if (conf_notify_http(new_repo, $2, $4, $6, 0)) {
616 free($2);
617 free($4);
618 free($6);
619 YYERROR;
622 free($2);
623 free($4);
624 free($6);
626 | URL STRING USER STRING PASSWORD STRING INSECURE {
627 if (gotd_proc_id == PROC_GOTD ||
628 gotd_proc_id == PROC_SESSION_WRITE ||
629 gotd_proc_id == PROC_NOTIFY) {
630 if (conf_notify_http(new_repo, $2, $4, $6, 1)) {
631 free($2);
632 free($4);
633 free($6);
634 YYERROR;
637 free($2);
638 free($4);
639 free($6);
643 repository : REPOSITORY STRING {
644 struct gotd_repo *repo;
646 TAILQ_FOREACH(repo, &gotd->repos, entry) {
647 if (strcmp(repo->name, $2) == 0) {
648 yyerror("duplicate repository '%s'", $2);
649 free($2);
650 YYERROR;
654 if (gotd_proc_id == PROC_GOTD ||
655 gotd_proc_id == PROC_AUTH ||
656 gotd_proc_id == PROC_REPO_WRITE ||
657 gotd_proc_id == PROC_SESSION_WRITE ||
658 gotd_proc_id == PROC_GITWRAPPER |
659 gotd_proc_id == PROC_NOTIFY) {
660 new_repo = conf_new_repo($2);
662 free($2);
663 } '{' optnl repoopts2 '}' {
667 repoopts1 : PATH STRING {
668 if (gotd_proc_id == PROC_GOTD ||
669 gotd_proc_id == PROC_AUTH ||
670 gotd_proc_id == PROC_REPO_WRITE ||
671 gotd_proc_id == PROC_SESSION_WRITE ||
672 gotd_proc_id == PROC_GITWRAPPER ||
673 gotd_proc_id == PROC_NOTIFY) {
674 if (!got_path_is_absolute($2)) {
675 yyerror("%s: path %s is not absolute",
676 __func__, $2);
677 free($2);
678 YYERROR;
680 if (realpath($2, new_repo->path) == NULL) {
682 * To give admins a chance to create
683 * missing repositories at run-time
684 * we only warn about ENOENT here.
686 * And ignore 'permission denied' when
687 * running in gitwrapper. Users may be
688 * able to access this repository via
689 * gotd regardless.
691 if (errno == ENOENT) {
692 yyerror("realpath %s: %s", $2,
693 strerror(errno));
694 } else if (errno != EACCES ||
695 gotd_proc_id != PROC_GITWRAPPER) {
696 yyerror("realpath %s: %s", $2,
697 strerror(errno));
698 free($2);
699 YYERROR;
702 if (strlcpy(new_repo->path, $2,
703 sizeof(new_repo->path)) >=
704 sizeof(new_repo->path))
705 yyerror("path too long");
708 free($2);
710 | PERMIT RO STRING {
711 if (gotd_proc_id == PROC_AUTH) {
712 conf_new_access_rule(new_repo,
713 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
714 } else
715 free($3);
717 | PERMIT RW STRING {
718 if (gotd_proc_id == PROC_AUTH) {
719 conf_new_access_rule(new_repo,
720 GOTD_ACCESS_PERMITTED,
721 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
722 } else
723 free($3);
725 | DENY STRING {
726 if (gotd_proc_id == PROC_AUTH) {
727 conf_new_access_rule(new_repo,
728 GOTD_ACCESS_DENIED, 0, $2);
729 } else
730 free($2);
732 | protect
733 | notify
736 repoopts2 : repoopts2 repoopts1 nl
737 | repoopts1 optnl
740 nl : '\n' optnl
743 optnl : '\n' optnl /* zero or more newlines */
744 | /* empty */
749 struct keywords {
750 const char *k_name;
751 int k_val;
755 yyerror(const char *fmt, ...)
757 va_list ap;
758 char *msg;
760 file->errors++;
761 va_start(ap, fmt);
762 if (vasprintf(&msg, fmt, ap) == -1)
763 fatalx("yyerror vasprintf");
764 va_end(ap);
765 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
766 free(msg);
767 return (0);
771 kw_cmp(const void *k, const void *e)
773 return (strcmp(k, ((const struct keywords *)e)->k_name));
777 lookup(char *s)
779 /* This has to be sorted always. */
780 static const struct keywords keywords[] = {
781 { "branch", BRANCH },
782 { "connection", CONNECTION },
783 { "deny", DENY },
784 { "email", EMAIL },
785 { "from", FROM },
786 { "insecure", INSECURE },
787 { "limit", LIMIT },
788 { "listen", LISTEN },
789 { "namespace", NAMESPACE },
790 { "notify", NOTIFY },
791 { "on", ON },
792 { "password", PASSWORD },
793 { "path", PATH },
794 { "permit", PERMIT },
795 { "port", PORT },
796 { "protect", PROTECT },
797 { "reference", REFERENCE },
798 { "relay", RELAY },
799 { "reply", REPLY },
800 { "repository", REPOSITORY },
801 { "request", REQUEST },
802 { "ro", RO },
803 { "rw", RW },
804 { "tag", TAG },
805 { "timeout", TIMEOUT },
806 { "to", TO },
807 { "url", URL },
808 { "user", USER },
810 const struct keywords *p;
812 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
813 sizeof(keywords[0]), kw_cmp);
815 if (p)
816 return (p->k_val);
817 else
818 return (STRING);
821 #define MAXPUSHBACK 128
823 unsigned char *parsebuf;
824 int parseindex;
825 unsigned char pushback_buffer[MAXPUSHBACK];
826 int pushback_index = 0;
829 lgetc(int quotec)
831 int c, next;
833 if (parsebuf) {
834 /* Read character from the parsebuffer instead of input. */
835 if (parseindex >= 0) {
836 c = parsebuf[parseindex++];
837 if (c != '\0')
838 return (c);
839 parsebuf = NULL;
840 } else
841 parseindex++;
844 if (pushback_index)
845 return (pushback_buffer[--pushback_index]);
847 if (quotec) {
848 c = getc(file->stream);
849 if (c == EOF)
850 yyerror("reached end of file while parsing "
851 "quoted string");
852 return (c);
855 c = getc(file->stream);
856 while (c == '\\') {
857 next = getc(file->stream);
858 if (next != '\n') {
859 c = next;
860 break;
862 yylval.lineno = file->lineno;
863 file->lineno++;
864 c = getc(file->stream);
867 return (c);
871 lungetc(int c)
873 if (c == EOF)
874 return (EOF);
875 if (parsebuf) {
876 parseindex--;
877 if (parseindex >= 0)
878 return (c);
880 if (pushback_index < MAXPUSHBACK-1)
881 return (pushback_buffer[pushback_index++] = c);
882 else
883 return (EOF);
887 findeol(void)
889 int c;
891 parsebuf = NULL;
893 /* Skip to either EOF or the first real EOL. */
894 while (1) {
895 if (pushback_index)
896 c = pushback_buffer[--pushback_index];
897 else
898 c = lgetc(0);
899 if (c == '\n') {
900 file->lineno++;
901 break;
903 if (c == EOF)
904 break;
906 return (ERROR);
910 yylex(void)
912 unsigned char buf[8096];
913 unsigned char *p, *val;
914 int quotec, next, c;
915 int token;
917 top:
918 p = buf;
919 c = lgetc(0);
920 while (c == ' ' || c == '\t')
921 c = lgetc(0); /* nothing */
923 yylval.lineno = file->lineno;
924 if (c == '#') {
925 c = lgetc(0);
926 while (c != '\n' && c != EOF)
927 c = lgetc(0); /* nothing */
929 if (c == '$' && parsebuf == NULL) {
930 while (1) {
931 c = lgetc(0);
932 if (c == EOF)
933 return (0);
935 if (p + 1 >= buf + sizeof(buf) - 1) {
936 yyerror("string too long");
937 return (findeol());
939 if (isalnum(c) || c == '_') {
940 *p++ = c;
941 continue;
943 *p = '\0';
944 lungetc(c);
945 break;
947 val = symget(buf);
948 if (val == NULL) {
949 yyerror("macro '%s' not defined", buf);
950 return (findeol());
952 parsebuf = val;
953 parseindex = 0;
954 goto top;
957 switch (c) {
958 case '\'':
959 case '"':
960 quotec = c;
961 while (1) {
962 c = lgetc(quotec);
963 if (c == EOF)
964 return (0);
965 if (c == '\n') {
966 file->lineno++;
967 continue;
968 } else if (c == '\\') {
969 next = lgetc(quotec);
970 if (next == EOF)
971 return (0);
972 if (next == quotec || c == ' ' || c == '\t')
973 c = next;
974 else if (next == '\n') {
975 file->lineno++;
976 continue;
977 } else
978 lungetc(next);
979 } else if (c == quotec) {
980 *p = '\0';
981 break;
982 } else if (c == '\0') {
983 yyerror("syntax error");
984 return (findeol());
986 if (p + 1 >= buf + sizeof(buf) - 1) {
987 yyerror("string too long");
988 return (findeol());
990 *p++ = c;
992 yylval.v.string = strdup(buf);
993 if (yylval.v.string == NULL)
994 err(1, "yylex: strdup");
995 return (STRING);
998 #define allowed_to_end_number(x) \
999 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
1001 if (c == '-' || isdigit(c)) {
1002 do {
1003 *p++ = c;
1004 if ((unsigned)(p-buf) >= sizeof(buf)) {
1005 yyerror("string too long");
1006 return (findeol());
1008 c = lgetc(0);
1009 } while (c != EOF && isdigit(c));
1010 lungetc(c);
1011 if (p == buf + 1 && buf[0] == '-')
1012 goto nodigits;
1013 if (c == EOF || allowed_to_end_number(c)) {
1014 const char *errstr = NULL;
1016 *p = '\0';
1017 yylval.v.number = strtonum(buf, LLONG_MIN,
1018 LLONG_MAX, &errstr);
1019 if (errstr) {
1020 yyerror("\"%s\" invalid number: %s",
1021 buf, errstr);
1022 return (findeol());
1024 return (NUMBER);
1025 } else {
1026 nodigits:
1027 while (p > buf + 1)
1028 lungetc(*--p);
1029 c = *--p;
1030 if (c == '-')
1031 return (c);
1035 #define allowed_in_string(x) \
1036 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1037 x != '{' && x != '}' && \
1038 x != '!' && x != '=' && x != '#' && \
1039 x != ','))
1041 if (isalnum(c) || c == ':' || c == '_') {
1042 do {
1043 *p++ = c;
1044 if ((unsigned)(p-buf) >= sizeof(buf)) {
1045 yyerror("string too long");
1046 return (findeol());
1048 c = lgetc(0);
1049 } while (c != EOF && (allowed_in_string(c)));
1050 lungetc(c);
1051 *p = '\0';
1052 token = lookup(buf);
1053 if (token == STRING) {
1054 yylval.v.string = strdup(buf);
1055 if (yylval.v.string == NULL)
1056 err(1, "yylex: strdup");
1058 return (token);
1060 if (c == '\n') {
1061 yylval.lineno = file->lineno;
1062 file->lineno++;
1064 if (c == EOF)
1065 return (0);
1066 return (c);
1070 check_file_secrecy(int fd, const char *fname)
1072 struct stat st;
1074 if (fstat(fd, &st)) {
1075 log_warn("cannot stat %s", fname);
1076 return (-1);
1078 if (st.st_uid != 0 && st.st_uid != getuid()) {
1079 log_warnx("%s: owner not root or current user", fname);
1080 return (-1);
1082 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
1083 log_warnx("%s: group writable or world read/writable", fname);
1084 return (-1);
1086 return (0);
1089 struct file *
1090 newfile(const char *name, int secret, int required)
1092 struct file *nfile;
1094 nfile = calloc(1, sizeof(struct file));
1095 if (nfile == NULL) {
1096 log_warn("calloc");
1097 return (NULL);
1099 nfile->name = strdup(name);
1100 if (nfile->name == NULL) {
1101 log_warn("strdup");
1102 free(nfile);
1103 return (NULL);
1105 nfile->stream = fopen(nfile->name, "r");
1106 if (nfile->stream == NULL) {
1107 if (required)
1108 log_warn("open %s", nfile->name);
1109 free(nfile->name);
1110 free(nfile);
1111 return (NULL);
1112 } else if (secret &&
1113 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
1114 fclose(nfile->stream);
1115 free(nfile->name);
1116 free(nfile);
1117 return (NULL);
1119 nfile->lineno = 1;
1120 return (nfile);
1123 static void
1124 closefile(struct file *xfile)
1126 fclose(xfile->stream);
1127 free(xfile->name);
1128 free(xfile);
1132 parse_config(const char *filename, enum gotd_procid proc_id,
1133 struct gotd *env)
1135 struct sym *sym, *next;
1136 struct gotd_repo *repo;
1137 int require_config_file = (proc_id != PROC_GITWRAPPER);
1139 memset(env, 0, sizeof(*env));
1141 gotd = env;
1142 gotd_proc_id = proc_id;
1143 TAILQ_INIT(&gotd->repos);
1145 /* Apply default values. */
1146 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
1147 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
1148 fprintf(stderr, "%s: unix socket path too long", __func__);
1149 return -1;
1151 if (strlcpy(gotd->user_name, GOTD_USER,
1152 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
1153 fprintf(stderr, "%s: user name too long", __func__);
1154 return -1;
1157 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
1158 gotd->request_timeout.tv_usec = 0;
1160 file = newfile(filename, 0, require_config_file);
1161 if (file == NULL)
1162 return require_config_file ? -1 : 0;
1164 yyparse();
1165 errors = file->errors;
1166 closefile(file);
1168 /* Free macros and check which have not been used. */
1169 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1170 if ((gotd->verbosity > 1) && !sym->used)
1171 fprintf(stderr, "warning: macro '%s' not used\n",
1172 sym->nam);
1173 if (!sym->persist) {
1174 free(sym->nam);
1175 free(sym->val);
1176 TAILQ_REMOVE(&symhead, sym, entry);
1177 free(sym);
1181 if (errors)
1182 return (-1);
1184 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1185 if (repo->path[0] == '\0') {
1186 log_warnx("repository \"%s\": no path provided in "
1187 "configuration file", repo->name);
1188 return (-1);
1192 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
1193 log_warnx("no repository defined in configuration file");
1194 return (-1);
1197 return (0);
1200 static int
1201 uid_connection_limit_cmp(const void *pa, const void *pb)
1203 const struct gotd_uid_connection_limit *a = pa, *b = pb;
1205 if (a->uid < b->uid)
1206 return -1;
1207 else if (a->uid > b->uid);
1208 return 1;
1210 return 0;
1213 static int
1214 conf_limit_user_connections(const char *user, int maximum)
1216 uid_t uid;
1217 struct gotd_uid_connection_limit *limit;
1218 size_t nlimits;
1220 if (maximum < 1) {
1221 yyerror("max connections cannot be smaller 1");
1222 return -1;
1224 if (maximum > GOTD_MAXCLIENTS) {
1225 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
1226 return -1;
1229 if (gotd_parseuid(user, &uid) == -1) {
1230 yyerror("%s: no such user", user);
1231 return -1;
1234 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
1235 gotd->nconnection_limits, uid);
1236 if (limit) {
1237 limit->max_connections = maximum;
1238 return 0;
1241 limit = gotd->connection_limits;
1242 nlimits = gotd->nconnection_limits + 1;
1243 limit = reallocarray(limit, nlimits, sizeof(*limit));
1244 if (limit == NULL)
1245 fatal("reallocarray");
1247 limit[nlimits - 1].uid = uid;
1248 limit[nlimits - 1].max_connections = maximum;
1250 gotd->connection_limits = limit;
1251 gotd->nconnection_limits = nlimits;
1252 qsort(gotd->connection_limits, gotd->nconnection_limits,
1253 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
1255 return 0;
1258 static struct gotd_repo *
1259 conf_new_repo(const char *name)
1261 struct gotd_repo *repo;
1263 if (name[0] == '\0') {
1264 fatalx("syntax error: empty repository name found in %s",
1265 file->name);
1268 if (strchr(name, '\n') != NULL)
1269 fatalx("repository names must not contain linefeeds: %s", name);
1271 repo = calloc(1, sizeof(*repo));
1272 if (repo == NULL)
1273 fatalx("%s: calloc", __func__);
1275 STAILQ_INIT(&repo->rules);
1276 TAILQ_INIT(&repo->protected_tag_namespaces);
1277 TAILQ_INIT(&repo->protected_branch_namespaces);
1278 TAILQ_INIT(&repo->protected_branches);
1279 TAILQ_INIT(&repo->protected_branches);
1280 TAILQ_INIT(&repo->notification_refs);
1281 TAILQ_INIT(&repo->notification_ref_namespaces);
1282 STAILQ_INIT(&repo->notification_targets);
1284 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
1285 sizeof(repo->name))
1286 fatalx("%s: strlcpy", __func__);
1288 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
1289 gotd->nrepos++;
1291 return repo;
1294 static void
1295 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
1296 int authorization, char *identifier)
1298 struct gotd_access_rule *rule;
1300 rule = calloc(1, sizeof(*rule));
1301 if (rule == NULL)
1302 fatal("calloc");
1304 rule->access = access;
1305 rule->authorization = authorization;
1306 rule->identifier = identifier;
1308 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
1311 static int
1312 refname_is_valid(char *refname)
1314 if (strncmp(refname, "refs/", 5) != 0) {
1315 yyerror("reference name must begin with \"refs/\": %s",
1316 refname);
1317 return 0;
1320 if (!got_ref_name_is_valid(refname)) {
1321 yyerror("invalid reference name: %s", refname);
1322 return 0;
1325 return 1;
1328 static int
1329 conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
1330 char *namespace)
1332 const struct got_error *error;
1333 struct got_pathlist_entry *pe;
1334 char *s;
1336 *new = NULL;
1338 got_path_strip_trailing_slashes(namespace);
1339 if (!refname_is_valid(namespace))
1340 return -1;
1341 if (asprintf(&s, "%s/", namespace) == -1) {
1342 yyerror("asprintf: %s", strerror(errno));
1343 return -1;
1346 error = got_pathlist_insert(&pe, refs, s, NULL);
1347 if (error || pe == NULL) {
1348 free(s);
1349 if (error)
1350 yyerror("got_pathlist_insert: %s", error->msg);
1351 else
1352 yyerror("duplicate protected namespace %s", namespace);
1353 return -1;
1356 *new = s;
1357 return 0;
1360 static int
1361 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
1363 struct got_pathlist_entry *pe;
1364 char *new;
1366 if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
1367 namespace) == -1)
1368 return -1;
1370 TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
1371 if (strcmp(pe->path, new) == 0) {
1372 yyerror("duplicate protected namespace %s", namespace);
1373 return -1;
1377 return 0;
1380 static int
1381 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
1383 struct got_pathlist_entry *pe;
1384 char *new;
1386 if (conf_protect_ref_namespace(&new,
1387 &repo->protected_branch_namespaces, namespace) == -1)
1388 return -1;
1390 TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
1391 if (strcmp(pe->path, new) == 0) {
1392 yyerror("duplicate protected namespace %s", namespace);
1393 return -1;
1397 return 0;
1400 static int
1401 conf_protect_branch(struct gotd_repo *repo, char *branchname)
1403 const struct got_error *error;
1404 struct got_pathlist_entry *new;
1405 char *refname;
1407 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1408 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1409 yyerror("asprintf: %s", strerror(errno));
1410 return -1;
1412 } else {
1413 refname = strdup(branchname);
1414 if (refname == NULL) {
1415 yyerror("strdup: %s", strerror(errno));
1416 return -1;
1420 if (!refname_is_valid(refname)) {
1421 free(refname);
1422 return -1;
1425 error = got_pathlist_insert(&new, &repo->protected_branches,
1426 refname, NULL);
1427 if (error || new == NULL) {
1428 free(refname);
1429 if (error)
1430 yyerror("got_pathlist_insert: %s", error->msg);
1431 else
1432 yyerror("duplicate protect branch %s", branchname);
1433 return -1;
1436 return 0;
1439 static int
1440 conf_notify_branch(struct gotd_repo *repo, char *branchname)
1442 const struct got_error *error;
1443 struct got_pathlist_entry *pe;
1444 char *refname;
1446 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1447 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1448 yyerror("asprintf: %s", strerror(errno));
1449 return -1;
1451 } else {
1452 refname = strdup(branchname);
1453 if (refname == NULL) {
1454 yyerror("strdup: %s", strerror(errno));
1455 return -1;
1459 if (!refname_is_valid(refname)) {
1460 free(refname);
1461 return -1;
1464 error = got_pathlist_insert(&pe, &repo->notification_refs,
1465 refname, NULL);
1466 if (error) {
1467 free(refname);
1468 yyerror("got_pathlist_insert: %s", error->msg);
1469 return -1;
1471 if (pe == NULL)
1472 free(refname);
1474 return 0;
1477 static int
1478 conf_notify_ref_namespace(struct gotd_repo *repo, char *namespace)
1480 const struct got_error *error;
1481 struct got_pathlist_entry *pe;
1482 char *s;
1484 got_path_strip_trailing_slashes(namespace);
1485 if (!refname_is_valid(namespace))
1486 return -1;
1488 if (asprintf(&s, "%s/", namespace) == -1) {
1489 yyerror("asprintf: %s", strerror(errno));
1490 return -1;
1493 error = got_pathlist_insert(&pe, &repo->notification_ref_namespaces,
1494 s, NULL);
1495 if (error) {
1496 free(s);
1497 yyerror("got_pathlist_insert: %s", error->msg);
1498 return -1;
1500 if (pe == NULL)
1501 free(s);
1503 return 0;
1506 static int
1507 conf_notify_email(struct gotd_repo *repo, char *sender, char *recipient,
1508 char *responder, char *hostname, char *port)
1510 struct gotd_notification_target *target;
1512 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1513 if (target->type != GOTD_NOTIFICATION_VIA_EMAIL)
1514 continue;
1515 if (strcmp(target->conf.email.recipient, recipient) == 0) {
1516 yyerror("duplicate email notification for '%s' in "
1517 "repository '%s'", recipient, repo->name);
1518 return -1;
1522 target = calloc(1, sizeof(*target));
1523 if (target == NULL)
1524 fatal("calloc");
1525 target->type = GOTD_NOTIFICATION_VIA_EMAIL;
1526 if (sender) {
1527 target->conf.email.sender = strdup(sender);
1528 if (target->conf.email.sender == NULL)
1529 fatal("strdup");
1531 target->conf.email.recipient = strdup(recipient);
1532 if (target->conf.email.recipient == NULL)
1533 fatal("strdup");
1534 if (responder) {
1535 target->conf.email.responder = strdup(responder);
1536 if (target->conf.email.responder == NULL)
1537 fatal("strdup");
1539 if (hostname) {
1540 target->conf.email.hostname = strdup(hostname);
1541 if (target->conf.email.hostname == NULL)
1542 fatal("strdup");
1544 if (port) {
1545 target->conf.email.port = strdup(port);
1546 if (target->conf.email.port == NULL)
1547 fatal("strdup");
1550 STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1551 return 0;
1554 static int
1555 conf_notify_http(struct gotd_repo *repo, char *url, char *user, char *password,
1556 int insecure)
1558 const struct got_error *error;
1559 struct gotd_notification_target *target;
1560 char *proto, *hostname, *port, *path;
1561 int tls = 0, ret = 0;
1563 error = gotd_parse_url(&proto, &hostname, &port, &path, url);
1564 if (error) {
1565 yyerror("invalid HTTP notification URL '%s' in "
1566 "repository '%s': %s", url, repo->name, error->msg);
1567 return -1;
1570 tls = !strcmp(proto, "https");
1572 if (strcmp(proto, "http") != 0 && strcmp(proto, "https") != 0) {
1573 yyerror("invalid protocol '%s' in notification URL '%s' in "
1574 "repository '%s", proto, url, repo->name);
1575 ret = -1;
1576 goto done;
1579 if (port == NULL) {
1580 if (strcmp(proto, "http") == 0)
1581 port = strdup("80");
1582 if (strcmp(proto, "https") == 0)
1583 port = strdup("443");
1584 if (port == NULL) {
1585 error = got_error_from_errno("strdup");
1586 ret = -1;
1587 goto done;
1591 if ((user != NULL && password == NULL) ||
1592 (user == NULL && password != NULL)) {
1593 yyerror("missing username or password");
1594 ret = -1;
1595 goto done;
1598 if (!insecure && strcmp(proto, "http") == 0 &&
1599 (user != NULL || password != NULL)) {
1600 yyerror("%s: HTTP notifications with basic authentication "
1601 "over plaintext HTTP will leak credentials; add the "
1602 "'insecure' config keyword if this is intentional", url);
1603 ret = -1;
1604 goto done;
1607 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1608 if (target->type != GOTD_NOTIFICATION_VIA_HTTP)
1609 continue;
1610 if (target->conf.http.tls == tls &&
1611 !strcmp(target->conf.http.hostname, hostname) &&
1612 !strcmp(target->conf.http.port, port) &&
1613 !strcmp(target->conf.http.path, path)) {
1614 yyerror("duplicate notification for URL '%s' in "
1615 "repository '%s'", url, repo->name);
1616 ret = -1;
1617 goto done;
1621 target = calloc(1, sizeof(*target));
1622 if (target == NULL)
1623 fatal("calloc");
1624 target->type = GOTD_NOTIFICATION_VIA_HTTP;
1625 target->conf.http.tls = tls;
1626 target->conf.http.hostname = hostname;
1627 target->conf.http.port = port;
1628 target->conf.http.path = path;
1629 hostname = port = path = NULL;
1631 if (user) {
1632 target->conf.http.user = strdup(user);
1633 if (target->conf.http.user == NULL)
1634 fatal("strdup");
1635 target->conf.http.password = strdup(password);
1636 if (target->conf.http.password == NULL)
1637 fatal("strdup");
1640 STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1641 done:
1642 free(proto);
1643 free(hostname);
1644 free(port);
1645 free(path);
1646 return ret;
1650 symset(const char *nam, const char *val, int persist)
1652 struct sym *sym;
1654 TAILQ_FOREACH(sym, &symhead, entry) {
1655 if (strcmp(nam, sym->nam) == 0)
1656 break;
1659 if (sym != NULL) {
1660 if (sym->persist == 1)
1661 return (0);
1662 else {
1663 free(sym->nam);
1664 free(sym->val);
1665 TAILQ_REMOVE(&symhead, sym, entry);
1666 free(sym);
1669 sym = calloc(1, sizeof(*sym));
1670 if (sym == NULL)
1671 return (-1);
1673 sym->nam = strdup(nam);
1674 if (sym->nam == NULL) {
1675 free(sym);
1676 return (-1);
1678 sym->val = strdup(val);
1679 if (sym->val == NULL) {
1680 free(sym->nam);
1681 free(sym);
1682 return (-1);
1684 sym->used = 0;
1685 sym->persist = persist;
1686 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1687 return (0);
1690 char *
1691 symget(const char *nam)
1693 struct sym *sym;
1695 TAILQ_FOREACH(sym, &symhead, entry) {
1696 if (strcmp(nam, sym->nam) == 0) {
1697 sym->used = 1;
1698 return (sym->val);
1701 return (NULL);
1704 struct gotd_repo *
1705 gotd_find_repo_by_name(const char *repo_name, struct gotd_repolist *repos)
1707 struct gotd_repo *repo;
1708 size_t namelen;
1710 TAILQ_FOREACH(repo, repos, entry) {
1711 namelen = strlen(repo->name);
1712 if (strncmp(repo->name, repo_name, namelen) != 0)
1713 continue;
1714 if (repo_name[namelen] == '\0' ||
1715 strcmp(&repo_name[namelen], ".git") == 0)
1716 return repo;
1719 return NULL;
1722 struct gotd_repo *
1723 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1725 struct gotd_repo *repo;
1727 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1728 if (strcmp(repo->path, repo_path) == 0)
1729 return repo;
1732 return NULL;
1735 struct gotd_uid_connection_limit *
1736 gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
1737 size_t nlimits, uid_t uid)
1739 /* This array is always sorted to allow for binary search. */
1740 int i, left = 0, right = nlimits - 1;
1742 while (left <= right) {
1743 i = ((left + right) / 2);
1744 if (limits[i].uid == uid)
1745 return &limits[i];
1746 if (limits[i].uid > uid)
1747 left = i + 1;
1748 else
1749 right = i - 1;
1752 return NULL;
1756 gotd_parseuid(const char *s, uid_t *uid)
1758 struct passwd *pw;
1759 const char *errstr;
1761 if ((pw = getpwnam(s)) != NULL) {
1762 *uid = pw->pw_uid;
1763 if (*uid == UID_MAX)
1764 return -1;
1765 return 0;
1767 *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1768 if (errstr)
1769 return -1;
1770 return 0;
1773 const struct got_error *
1774 gotd_parse_url(char **proto, char **host, char **port,
1775 char **request_path, const char *url)
1777 const struct got_error *err = NULL;
1778 char *s, *p, *q;
1780 *proto = *host = *port = *request_path = NULL;
1782 p = strstr(url, "://");
1783 if (!p)
1784 return got_error(GOT_ERR_PARSE_URI);
1786 *proto = strndup(url, p - url);
1787 if (*proto == NULL) {
1788 err = got_error_from_errno("strndup");
1789 goto done;
1791 s = p + 3;
1793 p = strstr(s, "/");
1794 if (p == NULL) {
1795 err = got_error(GOT_ERR_PARSE_URI);
1796 goto done;
1799 q = memchr(s, ':', p - s);
1800 if (q) {
1801 *host = strndup(s, q - s);
1802 if (*host == NULL) {
1803 err = got_error_from_errno("strndup");
1804 goto done;
1806 if ((*host)[0] == '\0') {
1807 err = got_error(GOT_ERR_PARSE_URI);
1808 goto done;
1810 *port = strndup(q + 1, p - (q + 1));
1811 if (*port == NULL) {
1812 err = got_error_from_errno("strndup");
1813 goto done;
1815 if ((*port)[0] == '\0') {
1816 err = got_error(GOT_ERR_PARSE_URI);
1817 goto done;
1819 } else {
1820 *host = strndup(s, p - s);
1821 if (*host == NULL) {
1822 err = got_error_from_errno("strndup");
1823 goto done;
1825 if ((*host)[0] == '\0') {
1826 err = got_error(GOT_ERR_PARSE_URI);
1827 goto done;
1831 while (p[0] == '/' && p[1] == '/')
1832 p++;
1833 *request_path = strdup(p);
1834 if (*request_path == NULL) {
1835 err = got_error_from_errno("strdup");
1836 goto done;
1838 if ((*request_path)[0] == '\0') {
1839 err = got_error(GOT_ERR_PARSE_URI);
1840 goto done;
1842 done:
1843 if (err) {
1844 free(*proto);
1845 *proto = NULL;
1846 free(*host);
1847 *host = NULL;
1848 free(*port);
1849 *port = NULL;
1850 free(*request_path);
1851 *request_path = NULL;
1853 return err;
1856 static char *
1857 port_sprintf(int p)
1859 static char portno[32];
1860 int n;
1862 n = snprintf(portno, sizeof(portno), "%lld", (long long)p);
1863 if (n < 0 || (size_t)n >= sizeof(portno))
1864 fatalx("port number too long: %lld", (long long)p);
1866 return portno;