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 <pwd.h>
37 #include <sha1.h>
38 #include <sha2.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_path.h"
48 #include "got_reference.h"
50 #include "log.h"
51 #include "gotd.h"
52 #include "auth.h"
53 #include "listen.h"
55 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
56 static struct file {
57 TAILQ_ENTRY(file) entry;
58 FILE *stream;
59 char *name;
60 int lineno;
61 int errors;
62 } *file;
63 struct file *newfile(const char *, int, int);
64 static void closefile(struct file *);
65 int check_file_secrecy(int, const char *);
66 int yyparse(void);
67 int yylex(void);
68 int yyerror(const char *, ...)
69 __attribute__((__format__ (printf, 1, 2)))
70 __attribute__((__nonnull__ (1)));
71 int kw_cmp(const void *, const void *);
72 int lookup(char *);
73 int lgetc(int);
74 int lungetc(int);
75 int findeol(void);
76 static char *port_sprintf(int);
78 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
79 struct sym {
80 TAILQ_ENTRY(sym) entry;
81 int used;
82 int persist;
83 char *nam;
84 char *val;
85 };
87 int symset(const char *, const char *, int);
88 char *symget(const char *);
90 static int errors;
92 static struct gotd *gotd;
93 static struct gotd_repo *new_repo;
94 static int conf_limit_user_connections(const char *, int);
95 static struct gotd_repo *conf_new_repo(const char *);
96 static void conf_new_access_rule(struct gotd_repo *,
97 enum gotd_access, int, char *);
98 static int conf_protect_ref_namespace(char **,
99 struct got_pathlist_head *, char *);
100 static int conf_protect_tag_namespace(struct gotd_repo *,
101 char *);
102 static int conf_protect_branch_namespace(
103 struct gotd_repo *, char *);
104 static int conf_protect_branch(struct gotd_repo *,
105 char *);
106 static int conf_notify_branch(struct gotd_repo *,
107 char *);
108 static int conf_notify_ref_namespace(struct gotd_repo *,
109 char *);
110 static int conf_notify_email(struct gotd_repo *,
111 char *, char *, char *, char *, char *);
112 static int conf_notify_http(struct gotd_repo *,
113 char *, char *, char *);
114 static enum gotd_procid gotd_proc_id;
116 typedef struct {
117 union {
118 long long number;
119 char *string;
120 struct timeval tv;
121 } v;
122 int lineno;
123 } YYSTYPE;
125 %}
127 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
128 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
129 %token PROTECT NAMESPACE BRANCH TAG REFERENCE RELAY PORT
130 %token NOTIFY EMAIL FROM REPLY TO URL PASSWORD
132 %token <v.string> STRING
133 %token <v.number> NUMBER
134 %type <v.tv> timeout
136 %%
138 grammar :
139 | grammar '\n'
140 | grammar varset '\n'
141 | grammar main '\n'
142 | grammar repository '\n'
145 varset : STRING '=' STRING {
146 char *s = $1;
147 while (*s++) {
148 if (isspace((unsigned char)*s)) {
149 yyerror("macro name cannot contain "
150 "whitespace");
151 free($1);
152 free($3);
153 YYERROR;
156 if (symset($1, $3, 0) == -1)
157 fatal("cannot store variable");
158 free($1);
159 free($3);
163 timeout : NUMBER {
164 if ($1 < 0) {
165 yyerror("invalid timeout: %lld", $1);
166 YYERROR;
168 $$.tv_sec = $1;
169 $$.tv_usec = 0;
171 | STRING {
172 const char *errstr;
173 const char *type = "seconds";
174 size_t len;
175 int mul = 1;
177 if (*$1 == '\0') {
178 yyerror("invalid number of seconds: %s", $1);
179 free($1);
180 YYERROR;
183 len = strlen($1);
184 switch ($1[len - 1]) {
185 case 'S':
186 case 's':
187 $1[len - 1] = '\0';
188 break;
189 case 'M':
190 case 'm':
191 type = "minutes";
192 mul = 60;
193 $1[len - 1] = '\0';
194 break;
195 case 'H':
196 case 'h':
197 type = "hours";
198 mul = 60 * 60;
199 $1[len - 1] = '\0';
200 break;
203 $$.tv_usec = 0;
204 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
205 if (errstr) {
206 yyerror("number of %s is %s: %s", type,
207 errstr, $1);
208 free($1);
209 YYERROR;
212 $$.tv_sec *= mul;
213 free($1);
217 main : LISTEN ON STRING {
218 if (!got_path_is_absolute($3))
219 yyerror("bad unix socket path \"%s\": "
220 "must be an absolute path", $3);
222 if (gotd_proc_id == PROC_LISTEN) {
223 if (strlcpy(gotd->unix_socket_path, $3,
224 sizeof(gotd->unix_socket_path)) >=
225 sizeof(gotd->unix_socket_path)) {
226 yyerror("%s: unix socket path too long",
227 __func__);
228 free($3);
229 YYERROR;
232 free($3);
234 | USER STRING {
235 if (strlcpy(gotd->user_name, $2,
236 sizeof(gotd->user_name)) >=
237 sizeof(gotd->user_name)) {
238 yyerror("%s: user name too long", __func__);
239 free($2);
240 YYERROR;
242 free($2);
244 | connection
247 connection : CONNECTION '{' optnl conflags_l '}'
248 | CONNECTION conflags
250 conflags_l : conflags optnl conflags_l
251 | conflags optnl
254 conflags : REQUEST TIMEOUT timeout {
255 if ($3.tv_sec <= 0) {
256 yyerror("invalid timeout: %lld", $3.tv_sec);
257 YYERROR;
259 memcpy(&gotd->request_timeout, &$3,
260 sizeof(gotd->request_timeout));
262 | LIMIT USER STRING NUMBER {
263 if (gotd_proc_id == PROC_LISTEN &&
264 conf_limit_user_connections($3, $4) == -1) {
265 free($3);
266 YYERROR;
268 free($3);
272 protect : PROTECT '{' optnl protectflags_l '}'
273 | PROTECT protectflags
275 protectflags_l : protectflags optnl protectflags_l
276 | protectflags optnl
279 protectflags : TAG NAMESPACE STRING {
280 if (gotd_proc_id == PROC_GOTD ||
281 gotd_proc_id == PROC_REPO_WRITE) {
282 if (conf_protect_tag_namespace(new_repo, $3)) {
283 free($3);
284 YYERROR;
287 free($3);
289 | BRANCH NAMESPACE STRING {
290 if (gotd_proc_id == PROC_GOTD ||
291 gotd_proc_id == PROC_REPO_WRITE) {
292 if (conf_protect_branch_namespace(new_repo,
293 $3)) {
294 free($3);
295 YYERROR;
298 free($3);
300 | BRANCH STRING {
301 if (gotd_proc_id == PROC_GOTD ||
302 gotd_proc_id == PROC_REPO_WRITE) {
303 if (conf_protect_branch(new_repo, $2)) {
304 free($2);
305 YYERROR;
308 free($2);
312 notify : NOTIFY '{' optnl notifyflags_l '}'
313 | NOTIFY notifyflags
315 notifyflags_l : notifyflags optnl notifyflags_l
316 | notifyflags optnl
319 notifyflags : BRANCH STRING {
320 if (gotd_proc_id == PROC_GOTD ||
321 gotd_proc_id == PROC_SESSION_WRITE ||
322 gotd_proc_id == PROC_NOTIFY) {
323 if (conf_notify_branch(new_repo, $2)) {
324 free($2);
325 YYERROR;
328 free($2);
330 | REFERENCE NAMESPACE STRING {
331 if (gotd_proc_id == PROC_GOTD ||
332 gotd_proc_id == PROC_SESSION_WRITE ||
333 gotd_proc_id == PROC_NOTIFY) {
334 if (conf_notify_ref_namespace(new_repo, $3)) {
335 free($3);
336 YYERROR;
339 free($3);
341 | EMAIL TO STRING {
342 if (gotd_proc_id == PROC_GOTD ||
343 gotd_proc_id == PROC_SESSION_WRITE ||
344 gotd_proc_id == PROC_NOTIFY) {
345 if (conf_notify_email(new_repo, NULL, $3,
346 NULL, NULL, NULL)) {
347 free($3);
348 YYERROR;
351 free($3);
353 | EMAIL FROM STRING TO STRING {
354 if (gotd_proc_id == PROC_GOTD ||
355 gotd_proc_id == PROC_SESSION_WRITE ||
356 gotd_proc_id == PROC_NOTIFY) {
357 if (conf_notify_email(new_repo, $3, $5,
358 NULL, NULL, NULL)) {
359 free($3);
360 free($5);
361 YYERROR;
364 free($3);
365 free($5);
367 | EMAIL TO STRING REPLY TO STRING {
368 if (gotd_proc_id == PROC_GOTD ||
369 gotd_proc_id == PROC_SESSION_WRITE ||
370 gotd_proc_id == PROC_NOTIFY) {
371 if (conf_notify_email(new_repo, NULL, $3,
372 $6, NULL, NULL)) {
373 free($3);
374 free($6);
375 YYERROR;
378 free($3);
379 free($6);
381 | EMAIL FROM STRING TO STRING REPLY TO STRING {
382 if (gotd_proc_id == PROC_GOTD ||
383 gotd_proc_id == PROC_SESSION_WRITE ||
384 gotd_proc_id == PROC_NOTIFY) {
385 if (conf_notify_email(new_repo, $3, $5,
386 $8, NULL, NULL)) {
387 free($3);
388 free($5);
389 free($8);
390 YYERROR;
393 free($3);
394 free($5);
395 free($8);
397 | EMAIL TO STRING RELAY STRING {
398 if (gotd_proc_id == PROC_GOTD ||
399 gotd_proc_id == PROC_SESSION_WRITE ||
400 gotd_proc_id == PROC_NOTIFY) {
401 if (conf_notify_email(new_repo, NULL, $3,
402 NULL, $5, NULL)) {
403 free($3);
404 free($5);
405 YYERROR;
408 free($3);
409 free($5);
411 | EMAIL FROM STRING TO STRING RELAY STRING {
412 if (gotd_proc_id == PROC_GOTD ||
413 gotd_proc_id == PROC_SESSION_WRITE ||
414 gotd_proc_id == PROC_NOTIFY) {
415 if (conf_notify_email(new_repo, $3, $5,
416 NULL, $7, NULL)) {
417 free($3);
418 free($5);
419 free($7);
420 YYERROR;
423 free($3);
424 free($5);
425 free($7);
427 | EMAIL TO STRING REPLY TO STRING RELAY STRING {
428 if (gotd_proc_id == PROC_GOTD ||
429 gotd_proc_id == PROC_SESSION_WRITE ||
430 gotd_proc_id == PROC_NOTIFY) {
431 if (conf_notify_email(new_repo, NULL, $3,
432 $6, $8, NULL)) {
433 free($3);
434 free($6);
435 free($8);
436 YYERROR;
439 free($3);
440 free($6);
441 free($8);
443 | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING {
444 if (gotd_proc_id == PROC_GOTD ||
445 gotd_proc_id == PROC_SESSION_WRITE ||
446 gotd_proc_id == PROC_NOTIFY) {
447 if (conf_notify_email(new_repo, $3, $5,
448 $8, $10, NULL)) {
449 free($3);
450 free($5);
451 free($8);
452 free($10);
453 YYERROR;
456 free($3);
457 free($5);
458 free($8);
459 free($10);
461 | EMAIL TO STRING RELAY STRING PORT STRING {
462 if (gotd_proc_id == PROC_GOTD ||
463 gotd_proc_id == PROC_SESSION_WRITE ||
464 gotd_proc_id == PROC_NOTIFY) {
465 if (conf_notify_email(new_repo, NULL, $3,
466 NULL, $5, $7)) {
467 free($3);
468 free($5);
469 free($7);
470 YYERROR;
473 free($3);
474 free($5);
475 free($7);
477 | EMAIL FROM STRING TO STRING RELAY STRING PORT STRING {
478 if (gotd_proc_id == PROC_GOTD ||
479 gotd_proc_id == PROC_SESSION_WRITE ||
480 gotd_proc_id == PROC_NOTIFY) {
481 if (conf_notify_email(new_repo, $3, $5,
482 NULL, $7, $9)) {
483 free($3);
484 free($5);
485 free($7);
486 free($9);
487 YYERROR;
490 free($3);
491 free($5);
492 free($7);
493 free($9);
495 | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT STRING {
496 if (gotd_proc_id == PROC_GOTD ||
497 gotd_proc_id == PROC_SESSION_WRITE ||
498 gotd_proc_id == PROC_NOTIFY) {
499 if (conf_notify_email(new_repo, NULL, $3,
500 $6, $8, $10)) {
501 free($3);
502 free($6);
503 free($8);
504 free($10);
505 YYERROR;
508 free($3);
509 free($6);
510 free($8);
511 free($10);
513 | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT STRING {
514 if (gotd_proc_id == PROC_GOTD ||
515 gotd_proc_id == PROC_SESSION_WRITE ||
516 gotd_proc_id == PROC_NOTIFY) {
517 if (conf_notify_email(new_repo, $3, $5,
518 $8, $10, $12)) {
519 free($3);
520 free($5);
521 free($8);
522 free($10);
523 free($12);
524 YYERROR;
527 free($3);
528 free($5);
529 free($8);
530 free($10);
531 free($12);
533 | EMAIL TO STRING RELAY STRING PORT NUMBER {
534 if (gotd_proc_id == PROC_GOTD ||
535 gotd_proc_id == PROC_SESSION_WRITE ||
536 gotd_proc_id == PROC_NOTIFY) {
537 if (conf_notify_email(new_repo, NULL, $3,
538 NULL, $5, port_sprintf($7))) {
539 free($3);
540 free($5);
541 YYERROR;
544 free($3);
545 free($5);
547 | EMAIL FROM STRING TO STRING RELAY STRING PORT NUMBER {
548 if (gotd_proc_id == PROC_GOTD ||
549 gotd_proc_id == PROC_SESSION_WRITE ||
550 gotd_proc_id == PROC_NOTIFY) {
551 if (conf_notify_email(new_repo, $3, $5,
552 NULL, $7, port_sprintf($9))) {
553 free($3);
554 free($5);
555 free($7);
556 YYERROR;
559 free($3);
560 free($5);
561 free($7);
563 | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
564 if (gotd_proc_id == PROC_GOTD ||
565 gotd_proc_id == PROC_SESSION_WRITE ||
566 gotd_proc_id == PROC_NOTIFY) {
567 if (conf_notify_email(new_repo, NULL, $3,
568 $6, $8, port_sprintf($10))) {
569 free($3);
570 free($6);
571 free($8);
572 YYERROR;
575 free($3);
576 free($6);
577 free($8);
579 | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
580 if (gotd_proc_id == PROC_GOTD ||
581 gotd_proc_id == PROC_SESSION_WRITE ||
582 gotd_proc_id == PROC_NOTIFY) {
583 if (conf_notify_email(new_repo, $3, $5,
584 $8, $10, port_sprintf($12))) {
585 free($3);
586 free($5);
587 free($8);
588 free($10);
589 YYERROR;
592 free($3);
593 free($5);
594 free($8);
595 free($10);
597 | URL STRING {
598 if (gotd_proc_id == PROC_GOTD ||
599 gotd_proc_id == PROC_SESSION_WRITE ||
600 gotd_proc_id == PROC_NOTIFY) {
601 if (conf_notify_http(new_repo, $2, NULL,
602 NULL)) {
603 free($2);
604 YYERROR;
607 free($2);
609 | URL STRING USER STRING PASSWORD STRING {
610 if (gotd_proc_id == PROC_GOTD ||
611 gotd_proc_id == PROC_SESSION_WRITE ||
612 gotd_proc_id == PROC_NOTIFY) {
613 if (conf_notify_http(new_repo, $2, $4, $6)) {
614 free($2);
615 free($4);
616 free($6);
617 YYERROR;
620 free($2);
621 free($4);
622 free($6);
626 repository : REPOSITORY STRING {
627 struct gotd_repo *repo;
629 TAILQ_FOREACH(repo, &gotd->repos, entry) {
630 if (strcmp(repo->name, $2) == 0) {
631 yyerror("duplicate repository '%s'", $2);
632 free($2);
633 YYERROR;
637 if (gotd_proc_id == PROC_GOTD ||
638 gotd_proc_id == PROC_AUTH ||
639 gotd_proc_id == PROC_REPO_WRITE ||
640 gotd_proc_id == PROC_SESSION_WRITE ||
641 gotd_proc_id == PROC_GITWRAPPER |
642 gotd_proc_id == PROC_NOTIFY) {
643 new_repo = conf_new_repo($2);
645 free($2);
646 } '{' optnl repoopts2 '}' {
650 repoopts1 : PATH STRING {
651 if (gotd_proc_id == PROC_GOTD ||
652 gotd_proc_id == PROC_AUTH ||
653 gotd_proc_id == PROC_REPO_WRITE ||
654 gotd_proc_id == PROC_SESSION_WRITE ||
655 gotd_proc_id == PROC_GITWRAPPER ||
656 gotd_proc_id == PROC_NOTIFY) {
657 if (!got_path_is_absolute($2)) {
658 yyerror("%s: path %s is not absolute",
659 __func__, $2);
660 free($2);
661 YYERROR;
663 if (realpath($2, new_repo->path) == NULL) {
664 /*
665 * To give admins a chance to create
666 * missing repositories at run-time
667 * we only warn about ENOENT here.
669 * And ignore 'permission denied' when
670 * running in gitwrapper. Users may be
671 * able to access this repository via
672 * gotd regardless.
673 */
674 if (errno == ENOENT) {
675 yyerror("realpath %s: %s", $2,
676 strerror(errno));
677 } else if (errno != EACCES ||
678 gotd_proc_id != PROC_GITWRAPPER) {
679 yyerror("realpath %s: %s", $2,
680 strerror(errno));
681 free($2);
682 YYERROR;
685 if (strlcpy(new_repo->path, $2,
686 sizeof(new_repo->path)) >=
687 sizeof(new_repo->path))
688 yyerror("path too long");
691 free($2);
693 | PERMIT RO STRING {
694 if (gotd_proc_id == PROC_AUTH) {
695 conf_new_access_rule(new_repo,
696 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
697 } else
698 free($3);
700 | PERMIT RW STRING {
701 if (gotd_proc_id == PROC_AUTH) {
702 conf_new_access_rule(new_repo,
703 GOTD_ACCESS_PERMITTED,
704 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
705 } else
706 free($3);
708 | DENY STRING {
709 if (gotd_proc_id == PROC_AUTH) {
710 conf_new_access_rule(new_repo,
711 GOTD_ACCESS_DENIED, 0, $2);
712 } else
713 free($2);
715 | protect
716 | notify
719 repoopts2 : repoopts2 repoopts1 nl
720 | repoopts1 optnl
723 nl : '\n' optnl
726 optnl : '\n' optnl /* zero or more newlines */
727 | /* empty */
730 %%
732 struct keywords {
733 const char *k_name;
734 int k_val;
735 };
737 int
738 yyerror(const char *fmt, ...)
740 va_list ap;
741 char *msg;
743 file->errors++;
744 va_start(ap, fmt);
745 if (vasprintf(&msg, fmt, ap) == -1)
746 fatalx("yyerror vasprintf");
747 va_end(ap);
748 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
749 free(msg);
750 return (0);
753 int
754 kw_cmp(const void *k, const void *e)
756 return (strcmp(k, ((const struct keywords *)e)->k_name));
759 int
760 lookup(char *s)
762 /* This has to be sorted always. */
763 static const struct keywords keywords[] = {
764 { "branch", BRANCH },
765 { "connection", CONNECTION },
766 { "deny", DENY },
767 { "email", EMAIL },
768 { "from", FROM },
769 { "limit", LIMIT },
770 { "listen", LISTEN },
771 { "namespace", NAMESPACE },
772 { "notify", NOTIFY },
773 { "on", ON },
774 { "password", PASSWORD },
775 { "path", PATH },
776 { "permit", PERMIT },
777 { "port", PORT },
778 { "protect", PROTECT },
779 { "reference", REFERENCE },
780 { "relay", RELAY },
781 { "reply", REPLY },
782 { "repository", REPOSITORY },
783 { "request", REQUEST },
784 { "ro", RO },
785 { "rw", RW },
786 { "tag", TAG },
787 { "timeout", TIMEOUT },
788 { "to", TO },
789 { "url", URL },
790 { "user", USER },
791 };
792 const struct keywords *p;
794 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
795 sizeof(keywords[0]), kw_cmp);
797 if (p)
798 return (p->k_val);
799 else
800 return (STRING);
803 #define MAXPUSHBACK 128
805 unsigned char *parsebuf;
806 int parseindex;
807 unsigned char pushback_buffer[MAXPUSHBACK];
808 int pushback_index = 0;
810 int
811 lgetc(int quotec)
813 int c, next;
815 if (parsebuf) {
816 /* Read character from the parsebuffer instead of input. */
817 if (parseindex >= 0) {
818 c = parsebuf[parseindex++];
819 if (c != '\0')
820 return (c);
821 parsebuf = NULL;
822 } else
823 parseindex++;
826 if (pushback_index)
827 return (pushback_buffer[--pushback_index]);
829 if (quotec) {
830 c = getc(file->stream);
831 if (c == EOF)
832 yyerror("reached end of file while parsing "
833 "quoted string");
834 return (c);
837 c = getc(file->stream);
838 while (c == '\\') {
839 next = getc(file->stream);
840 if (next != '\n') {
841 c = next;
842 break;
844 yylval.lineno = file->lineno;
845 file->lineno++;
846 c = getc(file->stream);
849 return (c);
852 int
853 lungetc(int c)
855 if (c == EOF)
856 return (EOF);
857 if (parsebuf) {
858 parseindex--;
859 if (parseindex >= 0)
860 return (c);
862 if (pushback_index < MAXPUSHBACK-1)
863 return (pushback_buffer[pushback_index++] = c);
864 else
865 return (EOF);
868 int
869 findeol(void)
871 int c;
873 parsebuf = NULL;
875 /* Skip to either EOF or the first real EOL. */
876 while (1) {
877 if (pushback_index)
878 c = pushback_buffer[--pushback_index];
879 else
880 c = lgetc(0);
881 if (c == '\n') {
882 file->lineno++;
883 break;
885 if (c == EOF)
886 break;
888 return (ERROR);
891 int
892 yylex(void)
894 unsigned char buf[8096];
895 unsigned char *p, *val;
896 int quotec, next, c;
897 int token;
899 top:
900 p = buf;
901 c = lgetc(0);
902 while (c == ' ' || c == '\t')
903 c = lgetc(0); /* nothing */
905 yylval.lineno = file->lineno;
906 if (c == '#') {
907 c = lgetc(0);
908 while (c != '\n' && c != EOF)
909 c = lgetc(0); /* nothing */
911 if (c == '$' && parsebuf == NULL) {
912 while (1) {
913 c = lgetc(0);
914 if (c == EOF)
915 return (0);
917 if (p + 1 >= buf + sizeof(buf) - 1) {
918 yyerror("string too long");
919 return (findeol());
921 if (isalnum(c) || c == '_') {
922 *p++ = c;
923 continue;
925 *p = '\0';
926 lungetc(c);
927 break;
929 val = symget(buf);
930 if (val == NULL) {
931 yyerror("macro '%s' not defined", buf);
932 return (findeol());
934 parsebuf = val;
935 parseindex = 0;
936 goto top;
939 switch (c) {
940 case '\'':
941 case '"':
942 quotec = c;
943 while (1) {
944 c = lgetc(quotec);
945 if (c == EOF)
946 return (0);
947 if (c == '\n') {
948 file->lineno++;
949 continue;
950 } else if (c == '\\') {
951 next = lgetc(quotec);
952 if (next == EOF)
953 return (0);
954 if (next == quotec || c == ' ' || c == '\t')
955 c = next;
956 else if (next == '\n') {
957 file->lineno++;
958 continue;
959 } else
960 lungetc(next);
961 } else if (c == quotec) {
962 *p = '\0';
963 break;
964 } else if (c == '\0') {
965 yyerror("syntax error");
966 return (findeol());
968 if (p + 1 >= buf + sizeof(buf) - 1) {
969 yyerror("string too long");
970 return (findeol());
972 *p++ = c;
974 yylval.v.string = strdup(buf);
975 if (yylval.v.string == NULL)
976 err(1, "yylex: strdup");
977 return (STRING);
980 #define allowed_to_end_number(x) \
981 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
983 if (c == '-' || isdigit(c)) {
984 do {
985 *p++ = c;
986 if ((unsigned)(p-buf) >= sizeof(buf)) {
987 yyerror("string too long");
988 return (findeol());
990 c = lgetc(0);
991 } while (c != EOF && isdigit(c));
992 lungetc(c);
993 if (p == buf + 1 && buf[0] == '-')
994 goto nodigits;
995 if (c == EOF || allowed_to_end_number(c)) {
996 const char *errstr = NULL;
998 *p = '\0';
999 yylval.v.number = strtonum(buf, LLONG_MIN,
1000 LLONG_MAX, &errstr);
1001 if (errstr) {
1002 yyerror("\"%s\" invalid number: %s",
1003 buf, errstr);
1004 return (findeol());
1006 return (NUMBER);
1007 } else {
1008 nodigits:
1009 while (p > buf + 1)
1010 lungetc(*--p);
1011 c = *--p;
1012 if (c == '-')
1013 return (c);
1017 #define allowed_in_string(x) \
1018 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1019 x != '{' && x != '}' && \
1020 x != '!' && x != '=' && x != '#' && \
1021 x != ','))
1023 if (isalnum(c) || c == ':' || c == '_') {
1024 do {
1025 *p++ = c;
1026 if ((unsigned)(p-buf) >= sizeof(buf)) {
1027 yyerror("string too long");
1028 return (findeol());
1030 c = lgetc(0);
1031 } while (c != EOF && (allowed_in_string(c)));
1032 lungetc(c);
1033 *p = '\0';
1034 token = lookup(buf);
1035 if (token == STRING) {
1036 yylval.v.string = strdup(buf);
1037 if (yylval.v.string == NULL)
1038 err(1, "yylex: strdup");
1040 return (token);
1042 if (c == '\n') {
1043 yylval.lineno = file->lineno;
1044 file->lineno++;
1046 if (c == EOF)
1047 return (0);
1048 return (c);
1051 int
1052 check_file_secrecy(int fd, const char *fname)
1054 struct stat st;
1056 if (fstat(fd, &st)) {
1057 log_warn("cannot stat %s", fname);
1058 return (-1);
1060 if (st.st_uid != 0 && st.st_uid != getuid()) {
1061 log_warnx("%s: owner not root or current user", fname);
1062 return (-1);
1064 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
1065 log_warnx("%s: group writable or world read/writable", fname);
1066 return (-1);
1068 return (0);
1071 struct file *
1072 newfile(const char *name, int secret, int required)
1074 struct file *nfile;
1076 nfile = calloc(1, sizeof(struct file));
1077 if (nfile == NULL) {
1078 log_warn("calloc");
1079 return (NULL);
1081 nfile->name = strdup(name);
1082 if (nfile->name == NULL) {
1083 log_warn("strdup");
1084 free(nfile);
1085 return (NULL);
1087 nfile->stream = fopen(nfile->name, "r");
1088 if (nfile->stream == NULL) {
1089 if (required)
1090 log_warn("open %s", nfile->name);
1091 free(nfile->name);
1092 free(nfile);
1093 return (NULL);
1094 } else if (secret &&
1095 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
1096 fclose(nfile->stream);
1097 free(nfile->name);
1098 free(nfile);
1099 return (NULL);
1101 nfile->lineno = 1;
1102 return (nfile);
1105 static void
1106 closefile(struct file *xfile)
1108 fclose(xfile->stream);
1109 free(xfile->name);
1110 free(xfile);
1113 int
1114 parse_config(const char *filename, enum gotd_procid proc_id,
1115 struct gotd *env)
1117 struct sym *sym, *next;
1118 struct gotd_repo *repo;
1119 int require_config_file = (proc_id != PROC_GITWRAPPER);
1121 memset(env, 0, sizeof(*env));
1123 gotd = env;
1124 gotd_proc_id = proc_id;
1125 TAILQ_INIT(&gotd->repos);
1127 /* Apply default values. */
1128 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
1129 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
1130 fprintf(stderr, "%s: unix socket path too long", __func__);
1131 return -1;
1133 if (strlcpy(gotd->user_name, GOTD_USER,
1134 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
1135 fprintf(stderr, "%s: user name too long", __func__);
1136 return -1;
1139 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
1140 gotd->request_timeout.tv_usec = 0;
1142 file = newfile(filename, 0, require_config_file);
1143 if (file == NULL)
1144 return require_config_file ? -1 : 0;
1146 yyparse();
1147 errors = file->errors;
1148 closefile(file);
1150 /* Free macros and check which have not been used. */
1151 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1152 if ((gotd->verbosity > 1) && !sym->used)
1153 fprintf(stderr, "warning: macro '%s' not used\n",
1154 sym->nam);
1155 if (!sym->persist) {
1156 free(sym->nam);
1157 free(sym->val);
1158 TAILQ_REMOVE(&symhead, sym, entry);
1159 free(sym);
1163 if (errors)
1164 return (-1);
1166 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1167 if (repo->path[0] == '\0') {
1168 log_warnx("repository \"%s\": no path provided in "
1169 "configuration file", repo->name);
1170 return (-1);
1174 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
1175 log_warnx("no repository defined in configuration file");
1176 return (-1);
1179 return (0);
1182 static int
1183 uid_connection_limit_cmp(const void *pa, const void *pb)
1185 const struct gotd_uid_connection_limit *a = pa, *b = pb;
1187 if (a->uid < b->uid)
1188 return -1;
1189 else if (a->uid > b->uid);
1190 return 1;
1192 return 0;
1195 static int
1196 conf_limit_user_connections(const char *user, int maximum)
1198 uid_t uid;
1199 struct gotd_uid_connection_limit *limit;
1200 size_t nlimits;
1202 if (maximum < 1) {
1203 yyerror("max connections cannot be smaller 1");
1204 return -1;
1206 if (maximum > GOTD_MAXCLIENTS) {
1207 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
1208 return -1;
1211 if (gotd_parseuid(user, &uid) == -1) {
1212 yyerror("%s: no such user", user);
1213 return -1;
1216 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
1217 gotd->nconnection_limits, uid);
1218 if (limit) {
1219 limit->max_connections = maximum;
1220 return 0;
1223 limit = gotd->connection_limits;
1224 nlimits = gotd->nconnection_limits + 1;
1225 limit = reallocarray(limit, nlimits, sizeof(*limit));
1226 if (limit == NULL)
1227 fatal("reallocarray");
1229 limit[nlimits - 1].uid = uid;
1230 limit[nlimits - 1].max_connections = maximum;
1232 gotd->connection_limits = limit;
1233 gotd->nconnection_limits = nlimits;
1234 qsort(gotd->connection_limits, gotd->nconnection_limits,
1235 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
1237 return 0;
1240 static struct gotd_repo *
1241 conf_new_repo(const char *name)
1243 struct gotd_repo *repo;
1245 if (name[0] == '\0') {
1246 fatalx("syntax error: empty repository name found in %s",
1247 file->name);
1250 if (strchr(name, '\n') != NULL)
1251 fatalx("repository names must not contain linefeeds: %s", name);
1253 repo = calloc(1, sizeof(*repo));
1254 if (repo == NULL)
1255 fatalx("%s: calloc", __func__);
1257 STAILQ_INIT(&repo->rules);
1258 TAILQ_INIT(&repo->protected_tag_namespaces);
1259 TAILQ_INIT(&repo->protected_branch_namespaces);
1260 TAILQ_INIT(&repo->protected_branches);
1261 TAILQ_INIT(&repo->protected_branches);
1262 TAILQ_INIT(&repo->notification_refs);
1263 TAILQ_INIT(&repo->notification_ref_namespaces);
1264 STAILQ_INIT(&repo->notification_targets);
1266 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
1267 sizeof(repo->name))
1268 fatalx("%s: strlcpy", __func__);
1270 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
1271 gotd->nrepos++;
1273 return repo;
1276 static void
1277 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
1278 int authorization, char *identifier)
1280 struct gotd_access_rule *rule;
1282 rule = calloc(1, sizeof(*rule));
1283 if (rule == NULL)
1284 fatal("calloc");
1286 rule->access = access;
1287 rule->authorization = authorization;
1288 rule->identifier = identifier;
1290 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
1293 static int
1294 refname_is_valid(char *refname)
1296 if (strncmp(refname, "refs/", 5) != 0) {
1297 yyerror("reference name must begin with \"refs/\": %s",
1298 refname);
1299 return 0;
1302 if (!got_ref_name_is_valid(refname)) {
1303 yyerror("invalid reference name: %s", refname);
1304 return 0;
1307 return 1;
1310 static int
1311 conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
1312 char *namespace)
1314 const struct got_error *error;
1315 struct got_pathlist_entry *pe;
1316 char *s;
1318 *new = NULL;
1320 got_path_strip_trailing_slashes(namespace);
1321 if (!refname_is_valid(namespace))
1322 return -1;
1323 if (asprintf(&s, "%s/", namespace) == -1) {
1324 yyerror("asprintf: %s", strerror(errno));
1325 return -1;
1328 error = got_pathlist_insert(&pe, refs, s, NULL);
1329 if (error || pe == NULL) {
1330 free(s);
1331 if (error)
1332 yyerror("got_pathlist_insert: %s", error->msg);
1333 else
1334 yyerror("duplicate protected namespace %s", namespace);
1335 return -1;
1338 *new = s;
1339 return 0;
1342 static int
1343 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
1345 struct got_pathlist_entry *pe;
1346 char *new;
1348 if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
1349 namespace) == -1)
1350 return -1;
1352 TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
1353 if (strcmp(pe->path, new) == 0) {
1354 yyerror("duplicate protected namespace %s", namespace);
1355 return -1;
1359 return 0;
1362 static int
1363 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
1365 struct got_pathlist_entry *pe;
1366 char *new;
1368 if (conf_protect_ref_namespace(&new,
1369 &repo->protected_branch_namespaces, namespace) == -1)
1370 return -1;
1372 TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
1373 if (strcmp(pe->path, new) == 0) {
1374 yyerror("duplicate protected namespace %s", namespace);
1375 return -1;
1379 return 0;
1382 static int
1383 conf_protect_branch(struct gotd_repo *repo, char *branchname)
1385 const struct got_error *error;
1386 struct got_pathlist_entry *new;
1387 char *refname;
1389 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1390 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1391 yyerror("asprintf: %s", strerror(errno));
1392 return -1;
1394 } else {
1395 refname = strdup(branchname);
1396 if (refname == NULL) {
1397 yyerror("strdup: %s", strerror(errno));
1398 return -1;
1402 if (!refname_is_valid(refname)) {
1403 free(refname);
1404 return -1;
1407 error = got_pathlist_insert(&new, &repo->protected_branches,
1408 refname, NULL);
1409 if (error || new == NULL) {
1410 free(refname);
1411 if (error)
1412 yyerror("got_pathlist_insert: %s", error->msg);
1413 else
1414 yyerror("duplicate protect branch %s", branchname);
1415 return -1;
1418 return 0;
1421 static int
1422 conf_notify_branch(struct gotd_repo *repo, char *branchname)
1424 const struct got_error *error;
1425 struct got_pathlist_entry *pe;
1426 char *refname;
1428 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1429 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1430 yyerror("asprintf: %s", strerror(errno));
1431 return -1;
1433 } else {
1434 refname = strdup(branchname);
1435 if (refname == NULL) {
1436 yyerror("strdup: %s", strerror(errno));
1437 return -1;
1441 if (!refname_is_valid(refname)) {
1442 free(refname);
1443 return -1;
1446 error = got_pathlist_insert(&pe, &repo->notification_refs,
1447 refname, NULL);
1448 if (error) {
1449 free(refname);
1450 yyerror("got_pathlist_insert: %s", error->msg);
1451 return -1;
1453 if (pe == NULL)
1454 free(refname);
1456 return 0;
1459 static int
1460 conf_notify_ref_namespace(struct gotd_repo *repo, char *namespace)
1462 const struct got_error *error;
1463 struct got_pathlist_entry *pe;
1464 char *s;
1466 got_path_strip_trailing_slashes(namespace);
1467 if (!refname_is_valid(namespace))
1468 return -1;
1470 if (asprintf(&s, "%s/", namespace) == -1) {
1471 yyerror("asprintf: %s", strerror(errno));
1472 return -1;
1475 error = got_pathlist_insert(&pe, &repo->notification_ref_namespaces,
1476 s, NULL);
1477 if (error) {
1478 free(s);
1479 yyerror("got_pathlist_insert: %s", error->msg);
1480 return -1;
1482 if (pe == NULL)
1483 free(s);
1485 return 0;
1488 static int
1489 conf_notify_email(struct gotd_repo *repo, char *sender, char *recipient,
1490 char *responder, char *hostname, char *port)
1492 struct gotd_notification_target *target;
1494 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1495 if (target->type != GOTD_NOTIFICATION_VIA_EMAIL)
1496 continue;
1497 if (strcmp(target->conf.email.recipient, recipient) == 0) {
1498 yyerror("duplicate email notification for '%s' in "
1499 "repository '%s'", recipient, repo->name);
1500 return -1;
1504 target = calloc(1, sizeof(*target));
1505 if (target == NULL)
1506 fatal("calloc");
1507 target->type = GOTD_NOTIFICATION_VIA_EMAIL;
1508 if (sender) {
1509 target->conf.email.sender = strdup(sender);
1510 if (target->conf.email.sender == NULL)
1511 fatal("strdup");
1513 target->conf.email.recipient = strdup(recipient);
1514 if (target->conf.email.recipient == NULL)
1515 fatal("strdup");
1516 if (responder) {
1517 target->conf.email.responder = strdup(responder);
1518 if (target->conf.email.responder == NULL)
1519 fatal("strdup");
1521 if (hostname) {
1522 target->conf.email.hostname = strdup(hostname);
1523 if (target->conf.email.hostname == NULL)
1524 fatal("strdup");
1526 if (port) {
1527 target->conf.email.port = strdup(port);
1528 if (target->conf.email.port == NULL)
1529 fatal("strdup");
1532 STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1533 return 0;
1536 static int
1537 conf_notify_http(struct gotd_repo *repo, char *url, char *user, char *password)
1539 const struct got_error *error;
1540 struct gotd_notification_target *target;
1541 char *proto, *hostname, *port, *path;
1542 int tls = 0, ret = 0;
1544 error = gotd_parse_url(&proto, &hostname, &port, &path, url);
1545 if (error) {
1546 yyerror("invalid HTTP notification URL '%s' in "
1547 "repository '%s': %s", url, repo->name, error->msg);
1548 return -1;
1551 tls = !strcmp(proto, "https");
1553 if (strcmp(proto, "http") != 0 && strcmp(proto, "https") != 0) {
1554 yyerror("invalid protocol '%s' in notification URL '%s' in "
1555 "repository '%s", proto, url, repo->name);
1556 ret = -1;
1557 goto done;
1560 if ((user != NULL && password == NULL) ||
1561 (user == NULL && password != NULL)) {
1562 yyerror("missing username or password");
1563 ret = -1;
1564 goto done;
1567 if (strcmp(proto, "http") == 0 && (user != NULL || password != NULL)) {
1568 log_warnx("%s: WARNING: Using basic authentication over "
1569 "plaintext http:// will leak credentials; https:// is "
1570 "recommended for URL '%s'", getprogname(), url);
1573 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1574 if (target->type != GOTD_NOTIFICATION_VIA_HTTP)
1575 continue;
1576 if (target->conf.http.tls == tls &&
1577 !strcmp(target->conf.http.hostname, hostname) &&
1578 !strcmp(target->conf.http.port, port) &&
1579 !strcmp(target->conf.http.path, path)) {
1580 yyerror("duplicate notification for URL '%s' in "
1581 "repository '%s'", url, repo->name);
1582 ret = -1;
1583 goto done;
1587 target = calloc(1, sizeof(*target));
1588 if (target == NULL)
1589 fatal("calloc");
1590 target->type = GOTD_NOTIFICATION_VIA_HTTP;
1591 target->conf.http.tls = tls;
1592 target->conf.http.hostname = hostname;
1593 target->conf.http.port = port;
1594 target->conf.http.path = path;
1595 hostname = port = path = NULL;
1597 if (user) {
1598 target->conf.http.user = strdup(user);
1599 if (target->conf.http.user == NULL)
1600 fatal("strdup");
1601 target->conf.http.password = strdup(password);
1602 if (target->conf.http.password == NULL)
1603 fatal("strdup");
1606 STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1607 done:
1608 free(proto);
1609 free(hostname);
1610 free(port);
1611 free(path);
1612 return ret;
1615 int
1616 symset(const char *nam, const char *val, int persist)
1618 struct sym *sym;
1620 TAILQ_FOREACH(sym, &symhead, entry) {
1621 if (strcmp(nam, sym->nam) == 0)
1622 break;
1625 if (sym != NULL) {
1626 if (sym->persist == 1)
1627 return (0);
1628 else {
1629 free(sym->nam);
1630 free(sym->val);
1631 TAILQ_REMOVE(&symhead, sym, entry);
1632 free(sym);
1635 sym = calloc(1, sizeof(*sym));
1636 if (sym == NULL)
1637 return (-1);
1639 sym->nam = strdup(nam);
1640 if (sym->nam == NULL) {
1641 free(sym);
1642 return (-1);
1644 sym->val = strdup(val);
1645 if (sym->val == NULL) {
1646 free(sym->nam);
1647 free(sym);
1648 return (-1);
1650 sym->used = 0;
1651 sym->persist = persist;
1652 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1653 return (0);
1656 char *
1657 symget(const char *nam)
1659 struct sym *sym;
1661 TAILQ_FOREACH(sym, &symhead, entry) {
1662 if (strcmp(nam, sym->nam) == 0) {
1663 sym->used = 1;
1664 return (sym->val);
1667 return (NULL);
1670 struct gotd_repo *
1671 gotd_find_repo_by_name(const char *repo_name, struct gotd_repolist *repos)
1673 struct gotd_repo *repo;
1674 size_t namelen;
1676 TAILQ_FOREACH(repo, repos, entry) {
1677 namelen = strlen(repo->name);
1678 if (strncmp(repo->name, repo_name, namelen) != 0)
1679 continue;
1680 if (repo_name[namelen] == '\0' ||
1681 strcmp(&repo_name[namelen], ".git") == 0)
1682 return repo;
1685 return NULL;
1688 struct gotd_repo *
1689 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1691 struct gotd_repo *repo;
1693 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1694 if (strcmp(repo->path, repo_path) == 0)
1695 return repo;
1698 return NULL;
1701 struct gotd_uid_connection_limit *
1702 gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
1703 size_t nlimits, uid_t uid)
1705 /* This array is always sorted to allow for binary search. */
1706 int i, left = 0, right = nlimits - 1;
1708 while (left <= right) {
1709 i = ((left + right) / 2);
1710 if (limits[i].uid == uid)
1711 return &limits[i];
1712 if (limits[i].uid > uid)
1713 left = i + 1;
1714 else
1715 right = i - 1;
1718 return NULL;
1721 int
1722 gotd_parseuid(const char *s, uid_t *uid)
1724 struct passwd *pw;
1725 const char *errstr;
1727 if ((pw = getpwnam(s)) != NULL) {
1728 *uid = pw->pw_uid;
1729 if (*uid == UID_MAX)
1730 return -1;
1731 return 0;
1733 *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1734 if (errstr)
1735 return -1;
1736 return 0;
1739 const struct got_error *
1740 gotd_parse_url(char **proto, char **host, char **port,
1741 char **request_path, const char *url)
1743 const struct got_error *err = NULL;
1744 char *s, *p, *q;
1746 *proto = *host = *port = *request_path = NULL;
1748 p = strstr(url, "://");
1749 if (!p)
1750 return got_error(GOT_ERR_PARSE_URI);
1752 *proto = strndup(url, p - url);
1753 if (*proto == NULL) {
1754 err = got_error_from_errno("strndup");
1755 goto done;
1757 s = p + 3;
1759 p = strstr(s, "/");
1760 if (p == NULL) {
1761 err = got_error(GOT_ERR_PARSE_URI);
1762 goto done;
1765 q = memchr(s, ':', p - s);
1766 if (q) {
1767 *host = strndup(s, q - s);
1768 if (*host == NULL) {
1769 err = got_error_from_errno("strndup");
1770 goto done;
1772 if ((*host)[0] == '\0') {
1773 err = got_error(GOT_ERR_PARSE_URI);
1774 goto done;
1776 *port = strndup(q + 1, p - (q + 1));
1777 if (*port == NULL) {
1778 err = got_error_from_errno("strndup");
1779 goto done;
1781 if ((*port)[0] == '\0') {
1782 err = got_error(GOT_ERR_PARSE_URI);
1783 goto done;
1785 } else {
1786 *host = strndup(s, p - s);
1787 if (*host == NULL) {
1788 err = got_error_from_errno("strndup");
1789 goto done;
1791 if ((*host)[0] == '\0') {
1792 err = got_error(GOT_ERR_PARSE_URI);
1793 goto done;
1797 while (p[0] == '/' && p[1] == '/')
1798 p++;
1799 *request_path = strdup(p);
1800 if (*request_path == NULL) {
1801 err = got_error_from_errno("strdup");
1802 goto done;
1804 if ((*request_path)[0] == '\0') {
1805 err = got_error(GOT_ERR_PARSE_URI);
1806 goto done;
1808 done:
1809 if (err) {
1810 free(*proto);
1811 *proto = NULL;
1812 free(*host);
1813 *host = NULL;
1814 free(*port);
1815 *port = NULL;
1816 free(*request_path);
1817 *request_path = NULL;
1819 return err;
1822 static char *
1823 port_sprintf(int p)
1825 static char portno[32];
1826 int n;
1828 n = snprintf(portno, sizeof(portno), "%lld", (long long)p);
1829 if (n < 0 || (size_t)n >= sizeof(portno))
1830 fatalx("port number too long: %lld", (long long)p);
1832 return portno;