2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
4 3efd8e31 2022-10-23 thomas * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 3efd8e31 2022-10-23 thomas * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 3efd8e31 2022-10-23 thomas * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 3efd8e31 2022-10-23 thomas * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 3efd8e31 2022-10-23 thomas * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 3efd8e31 2022-10-23 thomas * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
12 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
13 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
15 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
27 3efd8e31 2022-10-23 thomas #include <sys/time.h>
28 3efd8e31 2022-10-23 thomas #include <sys/types.h>
29 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
30 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
32 3efd8e31 2022-10-23 thomas #include <ctype.h>
33 3efd8e31 2022-10-23 thomas #include <err.h>
34 3efd8e31 2022-10-23 thomas #include <errno.h>
35 3efd8e31 2022-10-23 thomas #include <event.h>
36 3efd8e31 2022-10-23 thomas #include <imsg.h>
37 3efd8e31 2022-10-23 thomas #include <limits.h>
38 48488136 2023-04-22 thomas #include <pwd.h>
39 3efd8e31 2022-10-23 thomas #include <stdarg.h>
40 3efd8e31 2022-10-23 thomas #include <stdlib.h>
41 3efd8e31 2022-10-23 thomas #include <stdio.h>
42 3efd8e31 2022-10-23 thomas #include <string.h>
43 3efd8e31 2022-10-23 thomas #include <syslog.h>
44 3efd8e31 2022-10-23 thomas #include <unistd.h>
46 3efd8e31 2022-10-23 thomas #include "got_error.h"
47 3efd8e31 2022-10-23 thomas #include "got_path.h"
48 6d7eb4f7 2023-04-04 thomas #include "got_reference.h"
50 3efd8e31 2022-10-23 thomas #include "log.h"
51 3efd8e31 2022-10-23 thomas #include "gotd.h"
52 0781db0e 2023-01-06 thomas #include "auth.h"
53 0781db0e 2023-01-06 thomas #include "listen.h"
55 3efd8e31 2022-10-23 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
56 3efd8e31 2022-10-23 thomas static struct file {
57 3efd8e31 2022-10-23 thomas TAILQ_ENTRY(file) entry;
58 3efd8e31 2022-10-23 thomas FILE *stream;
59 3efd8e31 2022-10-23 thomas char *name;
60 3efd8e31 2022-10-23 thomas int lineno;
61 3efd8e31 2022-10-23 thomas int errors;
63 7554713a 2023-04-01 thomas struct file *newfile(const char *, int, int);
64 3efd8e31 2022-10-23 thomas static void closefile(struct file *);
65 3efd8e31 2022-10-23 thomas int check_file_secrecy(int, const char *);
66 3efd8e31 2022-10-23 thomas int yyparse(void);
67 3efd8e31 2022-10-23 thomas int yylex(void);
68 3efd8e31 2022-10-23 thomas int yyerror(const char *, ...)
69 3efd8e31 2022-10-23 thomas __attribute__((__format__ (printf, 1, 2)))
70 3efd8e31 2022-10-23 thomas __attribute__((__nonnull__ (1)));
71 3efd8e31 2022-10-23 thomas int kw_cmp(const void *, const void *);
72 3efd8e31 2022-10-23 thomas int lookup(char *);
73 3efd8e31 2022-10-23 thomas int lgetc(int);
74 3efd8e31 2022-10-23 thomas int lungetc(int);
75 3efd8e31 2022-10-23 thomas int findeol(void);
76 ce1bfad9 2024-03-30 thomas static char *port_sprintf(int);
78 3efd8e31 2022-10-23 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
79 3efd8e31 2022-10-23 thomas struct sym {
80 3efd8e31 2022-10-23 thomas TAILQ_ENTRY(sym) entry;
82 3efd8e31 2022-10-23 thomas int persist;
83 3efd8e31 2022-10-23 thomas char *nam;
84 3efd8e31 2022-10-23 thomas char *val;
87 3efd8e31 2022-10-23 thomas int symset(const char *, const char *, int);
88 3efd8e31 2022-10-23 thomas char *symget(const char *);
90 3efd8e31 2022-10-23 thomas static int errors;
92 3efd8e31 2022-10-23 thomas static struct gotd *gotd;
93 3efd8e31 2022-10-23 thomas static struct gotd_repo *new_repo;
94 67f822ee 2023-01-06 thomas static int conf_limit_user_connections(const char *, int);
95 3efd8e31 2022-10-23 thomas static struct gotd_repo *conf_new_repo(const char *);
96 c3841c67 2022-11-18 thomas static void conf_new_access_rule(struct gotd_repo *,
97 c3841c67 2022-11-18 thomas enum gotd_access, int, char *);
98 57b6056a 2023-04-05 thomas static int conf_protect_ref_namespace(char **,
99 6d7eb4f7 2023-04-04 thomas struct got_pathlist_head *, char *);
100 6d7eb4f7 2023-04-04 thomas static int conf_protect_tag_namespace(struct gotd_repo *,
102 6d7eb4f7 2023-04-04 thomas static int conf_protect_branch_namespace(
103 6d7eb4f7 2023-04-04 thomas struct gotd_repo *, char *);
104 6d7eb4f7 2023-04-04 thomas static int conf_protect_branch(struct gotd_repo *,
106 ce1bfad9 2024-03-30 thomas static int conf_notify_branch(struct gotd_repo *,
108 ce1bfad9 2024-03-30 thomas static int conf_notify_ref_namespace(struct gotd_repo *,
110 ce1bfad9 2024-03-30 thomas static int conf_notify_email(struct gotd_repo *,
111 ce1bfad9 2024-03-30 thomas char *, char *, char *, char *, char *);
112 ce1bfad9 2024-03-30 thomas static int conf_notify_http(struct gotd_repo *,
113 2ae445d7 2024-04-25 thomas.ad char *, char *, char *, int);
114 3efd8e31 2022-10-23 thomas static enum gotd_procid gotd_proc_id;
116 3efd8e31 2022-10-23 thomas typedef struct {
118 3efd8e31 2022-10-23 thomas long long number;
119 3efd8e31 2022-10-23 thomas char *string;
120 0781db0e 2023-01-06 thomas struct timeval tv;
122 3efd8e31 2022-10-23 thomas int lineno;
123 3efd8e31 2022-10-23 thomas } YYSTYPE;
127 f9a4feb6 2023-01-06 thomas %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
128 0781db0e 2023-01-06 thomas %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
129 ce1bfad9 2024-03-30 thomas %token PROTECT NAMESPACE BRANCH TAG REFERENCE RELAY PORT
130 2ae445d7 2024-04-25 thomas.ad %token NOTIFY EMAIL FROM REPLY TO URL PASSWORD INSECURE
132 3efd8e31 2022-10-23 thomas %token <v.string> STRING
133 3efd8e31 2022-10-23 thomas %token <v.number> NUMBER
134 0781db0e 2023-01-06 thomas %type <v.tv> timeout
138 3efd8e31 2022-10-23 thomas grammar :
139 3efd8e31 2022-10-23 thomas | grammar '\n'
140 07202819 2023-11-06 thomas | grammar varset '\n'
141 3efd8e31 2022-10-23 thomas | grammar main '\n'
142 3efd8e31 2022-10-23 thomas | grammar repository '\n'
145 07202819 2023-11-06 thomas varset : STRING '=' STRING {
146 07202819 2023-11-06 thomas char *s = $1;
147 07202819 2023-11-06 thomas while (*s++) {
148 07202819 2023-11-06 thomas if (isspace((unsigned char)*s)) {
149 07202819 2023-11-06 thomas yyerror("macro name cannot contain "
150 07202819 2023-11-06 thomas "whitespace");
151 07202819 2023-11-06 thomas free($1);
152 07202819 2023-11-06 thomas free($3);
156 07202819 2023-11-06 thomas if (symset($1, $3, 0) == -1)
157 07202819 2023-11-06 thomas fatal("cannot store variable");
158 07202819 2023-11-06 thomas free($1);
159 07202819 2023-11-06 thomas free($3);
163 0781db0e 2023-01-06 thomas timeout : NUMBER {
164 0781db0e 2023-01-06 thomas if ($1 < 0) {
165 0781db0e 2023-01-06 thomas yyerror("invalid timeout: %lld", $1);
168 0781db0e 2023-01-06 thomas $$.tv_sec = $1;
169 0781db0e 2023-01-06 thomas $$.tv_usec = 0;
171 77d755e8 2023-01-06 thomas | STRING {
172 77d755e8 2023-01-06 thomas const char *errstr;
173 77d755e8 2023-01-06 thomas const char *type = "seconds";
174 77d755e8 2023-01-06 thomas size_t len;
175 77d755e8 2023-01-06 thomas int mul = 1;
177 77d755e8 2023-01-06 thomas if (*$1 == '\0') {
178 77d755e8 2023-01-06 thomas yyerror("invalid number of seconds: %s", $1);
179 77d755e8 2023-01-06 thomas free($1);
183 77d755e8 2023-01-06 thomas len = strlen($1);
184 77d755e8 2023-01-06 thomas switch ($1[len - 1]) {
185 77d755e8 2023-01-06 thomas case 'S':
186 77d755e8 2023-01-06 thomas case 's':
187 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
189 77d755e8 2023-01-06 thomas case 'M':
190 77d755e8 2023-01-06 thomas case 'm':
191 77d755e8 2023-01-06 thomas type = "minutes";
192 77d755e8 2023-01-06 thomas mul = 60;
193 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
195 77d755e8 2023-01-06 thomas case 'H':
196 77d755e8 2023-01-06 thomas case 'h':
197 77d755e8 2023-01-06 thomas type = "hours";
198 77d755e8 2023-01-06 thomas mul = 60 * 60;
199 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
203 77d755e8 2023-01-06 thomas $$.tv_usec = 0;
204 85bccbf7 2023-01-06 thomas $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
205 77d755e8 2023-01-06 thomas if (errstr) {
206 77d755e8 2023-01-06 thomas yyerror("number of %s is %s: %s", type,
207 77d755e8 2023-01-06 thomas errstr, $1);
208 77d755e8 2023-01-06 thomas free($1);
212 77d755e8 2023-01-06 thomas $$.tv_sec *= mul;
213 77d755e8 2023-01-06 thomas free($1);
217 f9a4feb6 2023-01-06 thomas main : LISTEN ON STRING {
218 7348ded8 2023-01-19 thomas if (!got_path_is_absolute($3))
219 7348ded8 2023-01-19 thomas yyerror("bad unix socket path \"%s\": "
220 7348ded8 2023-01-19 thomas "must be an absolute path", $3);
222 2b3d32a1 2022-12-30 thomas if (gotd_proc_id == PROC_LISTEN) {
223 f9a4feb6 2023-01-06 thomas if (strlcpy(gotd->unix_socket_path, $3,
224 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >=
225 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) {
226 3efd8e31 2022-10-23 thomas yyerror("%s: unix socket path too long",
227 3efd8e31 2022-10-23 thomas __func__);
228 f9a4feb6 2023-01-06 thomas free($3);
232 f9a4feb6 2023-01-06 thomas free($3);
234 3efd8e31 2022-10-23 thomas | USER STRING {
235 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, $2,
236 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >=
237 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) {
238 3efd8e31 2022-10-23 thomas yyerror("%s: user name too long", __func__);
239 3efd8e31 2022-10-23 thomas free($2);
242 3efd8e31 2022-10-23 thomas free($2);
244 0781db0e 2023-01-06 thomas | connection
247 0781db0e 2023-01-06 thomas connection : CONNECTION '{' optnl conflags_l '}'
248 0781db0e 2023-01-06 thomas | CONNECTION conflags
250 0781db0e 2023-01-06 thomas conflags_l : conflags optnl conflags_l
251 0781db0e 2023-01-06 thomas | conflags optnl
254 0781db0e 2023-01-06 thomas conflags : REQUEST TIMEOUT timeout {
255 912db690 2023-01-06 thomas if ($3.tv_sec <= 0) {
256 044f7af7 2024-04-09 thomas yyerror("invalid timeout: %lld",
257 044f7af7 2024-04-09 thomas (long long)$3.tv_sec);
260 0781db0e 2023-01-06 thomas memcpy(&gotd->request_timeout, &$3,
261 0781db0e 2023-01-06 thomas sizeof(gotd->request_timeout));
263 0781db0e 2023-01-06 thomas | LIMIT USER STRING NUMBER {
264 0781db0e 2023-01-06 thomas if (gotd_proc_id == PROC_LISTEN &&
265 0781db0e 2023-01-06 thomas conf_limit_user_connections($3, $4) == -1) {
266 0781db0e 2023-01-06 thomas free($3);
269 0781db0e 2023-01-06 thomas free($3);
273 6d7eb4f7 2023-04-04 thomas protect : PROTECT '{' optnl protectflags_l '}'
274 6d7eb4f7 2023-04-04 thomas | PROTECT protectflags
276 6d7eb4f7 2023-04-04 thomas protectflags_l : protectflags optnl protectflags_l
277 6d7eb4f7 2023-04-04 thomas | protectflags optnl
280 6d7eb4f7 2023-04-04 thomas protectflags : TAG NAMESPACE STRING {
281 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
282 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
283 6d7eb4f7 2023-04-04 thomas if (conf_protect_tag_namespace(new_repo, $3)) {
284 6d7eb4f7 2023-04-04 thomas free($3);
288 ffc797f3 2023-04-05 thomas free($3);
290 6d7eb4f7 2023-04-04 thomas | BRANCH NAMESPACE STRING {
291 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
292 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
293 6d7eb4f7 2023-04-04 thomas if (conf_protect_branch_namespace(new_repo,
295 6d7eb4f7 2023-04-04 thomas free($3);
299 ffc797f3 2023-04-05 thomas free($3);
301 6d7eb4f7 2023-04-04 thomas | BRANCH STRING {
302 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
303 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
304 6d7eb4f7 2023-04-04 thomas if (conf_protect_branch(new_repo, $2)) {
305 6d7eb4f7 2023-04-04 thomas free($2);
309 ffc797f3 2023-04-05 thomas free($2);
313 ce1bfad9 2024-03-30 thomas notify : NOTIFY '{' optnl notifyflags_l '}'
314 ce1bfad9 2024-03-30 thomas | NOTIFY notifyflags
316 ce1bfad9 2024-03-30 thomas notifyflags_l : notifyflags optnl notifyflags_l
317 ce1bfad9 2024-03-30 thomas | notifyflags optnl
320 ce1bfad9 2024-03-30 thomas notifyflags : BRANCH STRING {
321 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
322 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
323 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
324 ce1bfad9 2024-03-30 thomas if (conf_notify_branch(new_repo, $2)) {
325 ce1bfad9 2024-03-30 thomas free($2);
329 0a89e570 2024-03-30 thomas free($2);
331 ce1bfad9 2024-03-30 thomas | REFERENCE NAMESPACE STRING {
332 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
333 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
334 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
335 ce1bfad9 2024-03-30 thomas if (conf_notify_ref_namespace(new_repo, $3)) {
336 ce1bfad9 2024-03-30 thomas free($3);
340 0a89e570 2024-03-30 thomas free($3);
342 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING {
343 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
344 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
345 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
346 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
347 ce1bfad9 2024-03-30 thomas NULL, NULL, NULL)) {
348 ce1bfad9 2024-03-30 thomas free($3);
352 0a89e570 2024-03-30 thomas free($3);
354 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING {
355 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
356 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
357 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
358 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
359 ce1bfad9 2024-03-30 thomas NULL, NULL, NULL)) {
360 ce1bfad9 2024-03-30 thomas free($3);
361 ce1bfad9 2024-03-30 thomas free($5);
365 0a89e570 2024-03-30 thomas free($3);
366 0a89e570 2024-03-30 thomas free($5);
368 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING REPLY TO STRING {
369 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
370 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
371 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
372 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
373 ce1bfad9 2024-03-30 thomas $6, NULL, NULL)) {
374 ce1bfad9 2024-03-30 thomas free($3);
375 ce1bfad9 2024-03-30 thomas free($6);
379 0a89e570 2024-03-30 thomas free($3);
380 0a89e570 2024-03-30 thomas free($6);
382 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING REPLY TO STRING {
383 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
384 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
385 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
386 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
387 ce1bfad9 2024-03-30 thomas $8, NULL, NULL)) {
388 ce1bfad9 2024-03-30 thomas free($3);
389 ce1bfad9 2024-03-30 thomas free($5);
390 ce1bfad9 2024-03-30 thomas free($8);
394 0a89e570 2024-03-30 thomas free($3);
395 0a89e570 2024-03-30 thomas free($5);
396 0a89e570 2024-03-30 thomas free($8);
398 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING RELAY STRING {
399 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
400 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
401 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
402 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
403 ce1bfad9 2024-03-30 thomas NULL, $5, NULL)) {
404 ce1bfad9 2024-03-30 thomas free($3);
405 ce1bfad9 2024-03-30 thomas free($5);
409 0a89e570 2024-03-30 thomas free($3);
410 0a89e570 2024-03-30 thomas free($5);
412 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING RELAY STRING {
413 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
414 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
415 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
416 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
417 ce1bfad9 2024-03-30 thomas NULL, $7, NULL)) {
418 ce1bfad9 2024-03-30 thomas free($3);
419 ce1bfad9 2024-03-30 thomas free($5);
420 ce1bfad9 2024-03-30 thomas free($7);
424 0a89e570 2024-03-30 thomas free($3);
425 0a89e570 2024-03-30 thomas free($5);
426 0a89e570 2024-03-30 thomas free($7);
428 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING REPLY TO STRING RELAY STRING {
429 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
430 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
431 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
432 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
433 ce1bfad9 2024-03-30 thomas $6, $8, NULL)) {
434 ce1bfad9 2024-03-30 thomas free($3);
435 ce1bfad9 2024-03-30 thomas free($6);
436 ce1bfad9 2024-03-30 thomas free($8);
440 0a89e570 2024-03-30 thomas free($3);
441 0a89e570 2024-03-30 thomas free($6);
442 0a89e570 2024-03-30 thomas free($8);
444 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING {
445 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
446 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
447 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
448 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
449 ce1bfad9 2024-03-30 thomas $8, $10, NULL)) {
450 ce1bfad9 2024-03-30 thomas free($3);
451 ce1bfad9 2024-03-30 thomas free($5);
452 ce1bfad9 2024-03-30 thomas free($8);
453 ce1bfad9 2024-03-30 thomas free($10);
457 0a89e570 2024-03-30 thomas free($3);
458 0a89e570 2024-03-30 thomas free($5);
459 0a89e570 2024-03-30 thomas free($8);
460 0a89e570 2024-03-30 thomas free($10);
462 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING RELAY STRING PORT STRING {
463 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
464 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
465 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
466 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
467 ce1bfad9 2024-03-30 thomas NULL, $5, $7)) {
468 ce1bfad9 2024-03-30 thomas free($3);
469 ce1bfad9 2024-03-30 thomas free($5);
470 ce1bfad9 2024-03-30 thomas free($7);
474 0a89e570 2024-03-30 thomas free($3);
475 0a89e570 2024-03-30 thomas free($5);
476 0a89e570 2024-03-30 thomas free($7);
478 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING RELAY STRING PORT STRING {
479 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
480 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
481 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
482 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
483 ce1bfad9 2024-03-30 thomas NULL, $7, $9)) {
484 ce1bfad9 2024-03-30 thomas free($3);
485 ce1bfad9 2024-03-30 thomas free($5);
486 ce1bfad9 2024-03-30 thomas free($7);
487 ce1bfad9 2024-03-30 thomas free($9);
491 0a89e570 2024-03-30 thomas free($3);
492 0a89e570 2024-03-30 thomas free($5);
493 0a89e570 2024-03-30 thomas free($7);
494 0a89e570 2024-03-30 thomas free($9);
496 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT STRING {
497 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
498 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
499 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
500 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
501 ce1bfad9 2024-03-30 thomas $6, $8, $10)) {
502 ce1bfad9 2024-03-30 thomas free($3);
503 ce1bfad9 2024-03-30 thomas free($6);
504 ce1bfad9 2024-03-30 thomas free($8);
505 ce1bfad9 2024-03-30 thomas free($10);
509 0a89e570 2024-03-30 thomas free($3);
510 0a89e570 2024-03-30 thomas free($6);
511 0a89e570 2024-03-30 thomas free($8);
512 0a89e570 2024-03-30 thomas free($10);
514 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT STRING {
515 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
516 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
517 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
518 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
519 ce1bfad9 2024-03-30 thomas $8, $10, $12)) {
520 ce1bfad9 2024-03-30 thomas free($3);
521 ce1bfad9 2024-03-30 thomas free($5);
522 ce1bfad9 2024-03-30 thomas free($8);
523 ce1bfad9 2024-03-30 thomas free($10);
524 ce1bfad9 2024-03-30 thomas free($12);
528 0a89e570 2024-03-30 thomas free($3);
529 0a89e570 2024-03-30 thomas free($5);
530 0a89e570 2024-03-30 thomas free($8);
531 0a89e570 2024-03-30 thomas free($10);
532 0a89e570 2024-03-30 thomas free($12);
534 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING RELAY STRING PORT NUMBER {
535 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
536 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
537 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
538 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
539 ce1bfad9 2024-03-30 thomas NULL, $5, port_sprintf($7))) {
540 ce1bfad9 2024-03-30 thomas free($3);
541 ce1bfad9 2024-03-30 thomas free($5);
545 0a89e570 2024-03-30 thomas free($3);
546 0a89e570 2024-03-30 thomas free($5);
548 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING RELAY STRING PORT NUMBER {
549 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
550 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
551 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
552 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
553 ce1bfad9 2024-03-30 thomas NULL, $7, port_sprintf($9))) {
554 ce1bfad9 2024-03-30 thomas free($3);
555 ce1bfad9 2024-03-30 thomas free($5);
556 ce1bfad9 2024-03-30 thomas free($7);
560 0a89e570 2024-03-30 thomas free($3);
561 0a89e570 2024-03-30 thomas free($5);
562 0a89e570 2024-03-30 thomas free($7);
564 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
565 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
566 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
567 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
568 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
569 ce1bfad9 2024-03-30 thomas $6, $8, port_sprintf($10))) {
570 ce1bfad9 2024-03-30 thomas free($3);
571 ce1bfad9 2024-03-30 thomas free($6);
572 ce1bfad9 2024-03-30 thomas free($8);
576 0a89e570 2024-03-30 thomas free($3);
577 0a89e570 2024-03-30 thomas free($6);
578 0a89e570 2024-03-30 thomas free($8);
580 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
581 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
582 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
583 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
584 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
585 ce1bfad9 2024-03-30 thomas $8, $10, port_sprintf($12))) {
586 ce1bfad9 2024-03-30 thomas free($3);
587 ce1bfad9 2024-03-30 thomas free($5);
588 ce1bfad9 2024-03-30 thomas free($8);
589 ce1bfad9 2024-03-30 thomas free($10);
593 0a89e570 2024-03-30 thomas free($3);
594 0a89e570 2024-03-30 thomas free($5);
595 0a89e570 2024-03-30 thomas free($8);
596 0a89e570 2024-03-30 thomas free($10);
598 ce1bfad9 2024-03-30 thomas | URL STRING {
599 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
600 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
601 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
602 ce1bfad9 2024-03-30 thomas if (conf_notify_http(new_repo, $2, NULL,
603 2ae445d7 2024-04-25 thomas.ad NULL, 0)) {
604 ce1bfad9 2024-03-30 thomas free($2);
608 0a89e570 2024-03-30 thomas free($2);
610 ce1bfad9 2024-03-30 thomas | URL STRING USER STRING PASSWORD STRING {
611 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
612 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
613 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
614 2ae445d7 2024-04-25 thomas.ad if (conf_notify_http(new_repo, $2, $4, $6, 0)) {
615 2ae445d7 2024-04-25 thomas.ad free($2);
616 2ae445d7 2024-04-25 thomas.ad free($4);
617 2ae445d7 2024-04-25 thomas.ad free($6);
618 2ae445d7 2024-04-25 thomas.ad YYERROR;
621 2ae445d7 2024-04-25 thomas.ad free($2);
622 2ae445d7 2024-04-25 thomas.ad free($4);
623 2ae445d7 2024-04-25 thomas.ad free($6);
625 2ae445d7 2024-04-25 thomas.ad | URL STRING USER STRING PASSWORD STRING INSECURE {
626 2ae445d7 2024-04-25 thomas.ad if (gotd_proc_id == PROC_GOTD ||
627 2ae445d7 2024-04-25 thomas.ad gotd_proc_id == PROC_SESSION_WRITE ||
628 2ae445d7 2024-04-25 thomas.ad gotd_proc_id == PROC_NOTIFY) {
629 2ae445d7 2024-04-25 thomas.ad if (conf_notify_http(new_repo, $2, $4, $6, 1)) {
630 ce1bfad9 2024-03-30 thomas free($2);
631 ce1bfad9 2024-03-30 thomas free($4);
632 ce1bfad9 2024-03-30 thomas free($6);
636 0a89e570 2024-03-30 thomas free($2);
637 0a89e570 2024-03-30 thomas free($4);
638 0a89e570 2024-03-30 thomas free($6);
642 3efd8e31 2022-10-23 thomas repository : REPOSITORY STRING {
643 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
645 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
646 3efd8e31 2022-10-23 thomas if (strcmp(repo->name, $2) == 0) {
647 3efd8e31 2022-10-23 thomas yyerror("duplicate repository '%s'", $2);
648 3efd8e31 2022-10-23 thomas free($2);
653 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
654 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_AUTH ||
655 f3807fe5 2023-07-10 thomas gotd_proc_id == PROC_REPO_WRITE ||
656 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
657 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_GITWRAPPER |
658 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
659 3efd8e31 2022-10-23 thomas new_repo = conf_new_repo($2);
661 3efd8e31 2022-10-23 thomas free($2);
662 3efd8e31 2022-10-23 thomas } '{' optnl repoopts2 '}' {
666 3efd8e31 2022-10-23 thomas repoopts1 : PATH STRING {
667 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
668 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_AUTH ||
669 f3807fe5 2023-07-10 thomas gotd_proc_id == PROC_REPO_WRITE ||
670 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
671 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_GITWRAPPER ||
672 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
673 3efd8e31 2022-10-23 thomas if (!got_path_is_absolute($2)) {
674 3efd8e31 2022-10-23 thomas yyerror("%s: path %s is not absolute",
675 3efd8e31 2022-10-23 thomas __func__, $2);
676 3efd8e31 2022-10-23 thomas free($2);
679 fe6a8988 2023-01-08 thomas if (realpath($2, new_repo->path) == NULL) {
681 f3807fe5 2023-07-10 thomas * To give admins a chance to create
682 f3807fe5 2023-07-10 thomas * missing repositories at run-time
683 f3807fe5 2023-07-10 thomas * we only warn about ENOENT here.
685 f3807fe5 2023-07-10 thomas * And ignore 'permission denied' when
686 f3807fe5 2023-07-10 thomas * running in gitwrapper. Users may be
687 f3807fe5 2023-07-10 thomas * able to access this repository via
688 f3807fe5 2023-07-10 thomas * gotd regardless.
690 f3807fe5 2023-07-10 thomas if (errno == ENOENT) {
691 f3807fe5 2023-07-10 thomas yyerror("realpath %s: %s", $2,
692 f3807fe5 2023-07-10 thomas strerror(errno));
693 f3807fe5 2023-07-10 thomas } else if (errno != EACCES ||
694 f3807fe5 2023-07-10 thomas gotd_proc_id != PROC_GITWRAPPER) {
695 f3807fe5 2023-07-10 thomas yyerror("realpath %s: %s", $2,
696 f3807fe5 2023-07-10 thomas strerror(errno));
697 d95864cd 2023-04-14 thomas free($2);
701 f3807fe5 2023-07-10 thomas if (strlcpy(new_repo->path, $2,
702 d95864cd 2023-04-14 thomas sizeof(new_repo->path)) >=
703 d95864cd 2023-04-14 thomas sizeof(new_repo->path))
704 d95864cd 2023-04-14 thomas yyerror("path too long");
707 3efd8e31 2022-10-23 thomas free($2);
709 729a7e24 2022-11-17 thomas | PERMIT RO STRING {
710 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
711 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
712 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
714 ffc797f3 2023-04-05 thomas free($3);
716 729a7e24 2022-11-17 thomas | PERMIT RW STRING {
717 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
718 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
719 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED,
720 729a7e24 2022-11-17 thomas GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
722 ffc797f3 2023-04-05 thomas free($3);
724 729a7e24 2022-11-17 thomas | DENY STRING {
725 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
726 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
727 729a7e24 2022-11-17 thomas GOTD_ACCESS_DENIED, 0, $2);
729 ffc797f3 2023-04-05 thomas free($2);
731 6d7eb4f7 2023-04-04 thomas | protect
735 3efd8e31 2022-10-23 thomas repoopts2 : repoopts2 repoopts1 nl
736 3efd8e31 2022-10-23 thomas | repoopts1 optnl
739 3efd8e31 2022-10-23 thomas nl : '\n' optnl
742 3efd8e31 2022-10-23 thomas optnl : '\n' optnl /* zero or more newlines */
743 3efd8e31 2022-10-23 thomas | /* empty */
748 3efd8e31 2022-10-23 thomas struct keywords {
749 3efd8e31 2022-10-23 thomas const char *k_name;
750 3efd8e31 2022-10-23 thomas int k_val;
754 3efd8e31 2022-10-23 thomas yyerror(const char *fmt, ...)
756 3efd8e31 2022-10-23 thomas va_list ap;
757 3efd8e31 2022-10-23 thomas char *msg;
759 3efd8e31 2022-10-23 thomas file->errors++;
760 3efd8e31 2022-10-23 thomas va_start(ap, fmt);
761 3efd8e31 2022-10-23 thomas if (vasprintf(&msg, fmt, ap) == -1)
762 3efd8e31 2022-10-23 thomas fatalx("yyerror vasprintf");
763 3efd8e31 2022-10-23 thomas va_end(ap);
764 3efd8e31 2022-10-23 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
765 3efd8e31 2022-10-23 thomas free(msg);
766 3efd8e31 2022-10-23 thomas return (0);
770 3efd8e31 2022-10-23 thomas kw_cmp(const void *k, const void *e)
772 3efd8e31 2022-10-23 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
776 3efd8e31 2022-10-23 thomas lookup(char *s)
778 3efd8e31 2022-10-23 thomas /* This has to be sorted always. */
779 3efd8e31 2022-10-23 thomas static const struct keywords keywords[] = {
780 6d7eb4f7 2023-04-04 thomas { "branch", BRANCH },
781 0781db0e 2023-01-06 thomas { "connection", CONNECTION },
782 729a7e24 2022-11-17 thomas { "deny", DENY },
783 ce1bfad9 2024-03-30 thomas { "email", EMAIL },
784 ce1bfad9 2024-03-30 thomas { "from", FROM },
785 2ae445d7 2024-04-25 thomas.ad { "insecure", INSECURE },
786 0781db0e 2023-01-06 thomas { "limit", LIMIT },
787 f9a4feb6 2023-01-06 thomas { "listen", LISTEN },
788 6d7eb4f7 2023-04-04 thomas { "namespace", NAMESPACE },
789 ce1bfad9 2024-03-30 thomas { "notify", NOTIFY },
790 3efd8e31 2022-10-23 thomas { "on", ON },
791 ce1bfad9 2024-03-30 thomas { "password", PASSWORD },
792 3efd8e31 2022-10-23 thomas { "path", PATH },
793 729a7e24 2022-11-17 thomas { "permit", PERMIT },
794 ce1bfad9 2024-03-30 thomas { "port", PORT },
795 6d7eb4f7 2023-04-04 thomas { "protect", PROTECT },
796 ce1bfad9 2024-03-30 thomas { "reference", REFERENCE },
797 ce1bfad9 2024-03-30 thomas { "relay", RELAY },
798 ce1bfad9 2024-03-30 thomas { "reply", REPLY },
799 3efd8e31 2022-10-23 thomas { "repository", REPOSITORY },
800 0781db0e 2023-01-06 thomas { "request", REQUEST },
801 729a7e24 2022-11-17 thomas { "ro", RO },
802 729a7e24 2022-11-17 thomas { "rw", RW },
803 6d7eb4f7 2023-04-04 thomas { "tag", TAG },
804 0781db0e 2023-01-06 thomas { "timeout", TIMEOUT },
805 ce1bfad9 2024-03-30 thomas { "to", TO },
806 ce1bfad9 2024-03-30 thomas { "url", URL },
807 3efd8e31 2022-10-23 thomas { "user", USER },
809 3efd8e31 2022-10-23 thomas const struct keywords *p;
811 3efd8e31 2022-10-23 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
812 3efd8e31 2022-10-23 thomas sizeof(keywords[0]), kw_cmp);
815 3efd8e31 2022-10-23 thomas return (p->k_val);
817 3efd8e31 2022-10-23 thomas return (STRING);
820 3efd8e31 2022-10-23 thomas #define MAXPUSHBACK 128
822 3efd8e31 2022-10-23 thomas unsigned char *parsebuf;
823 3efd8e31 2022-10-23 thomas int parseindex;
824 3efd8e31 2022-10-23 thomas unsigned char pushback_buffer[MAXPUSHBACK];
825 3efd8e31 2022-10-23 thomas int pushback_index = 0;
828 3efd8e31 2022-10-23 thomas lgetc(int quotec)
830 3efd8e31 2022-10-23 thomas int c, next;
832 3efd8e31 2022-10-23 thomas if (parsebuf) {
833 3efd8e31 2022-10-23 thomas /* Read character from the parsebuffer instead of input. */
834 3efd8e31 2022-10-23 thomas if (parseindex >= 0) {
835 3efd8e31 2022-10-23 thomas c = parsebuf[parseindex++];
836 3efd8e31 2022-10-23 thomas if (c != '\0')
837 3efd8e31 2022-10-23 thomas return (c);
838 3efd8e31 2022-10-23 thomas parsebuf = NULL;
840 3efd8e31 2022-10-23 thomas parseindex++;
843 3efd8e31 2022-10-23 thomas if (pushback_index)
844 3efd8e31 2022-10-23 thomas return (pushback_buffer[--pushback_index]);
846 3efd8e31 2022-10-23 thomas if (quotec) {
847 3efd8e31 2022-10-23 thomas c = getc(file->stream);
848 3efd8e31 2022-10-23 thomas if (c == EOF)
849 3efd8e31 2022-10-23 thomas yyerror("reached end of file while parsing "
850 3efd8e31 2022-10-23 thomas "quoted string");
851 3efd8e31 2022-10-23 thomas return (c);
854 3efd8e31 2022-10-23 thomas c = getc(file->stream);
855 3efd8e31 2022-10-23 thomas while (c == '\\') {
856 3efd8e31 2022-10-23 thomas next = getc(file->stream);
857 3efd8e31 2022-10-23 thomas if (next != '\n') {
858 3efd8e31 2022-10-23 thomas c = next;
861 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
862 3efd8e31 2022-10-23 thomas file->lineno++;
863 3efd8e31 2022-10-23 thomas c = getc(file->stream);
866 3efd8e31 2022-10-23 thomas return (c);
870 3efd8e31 2022-10-23 thomas lungetc(int c)
872 3efd8e31 2022-10-23 thomas if (c == EOF)
873 3efd8e31 2022-10-23 thomas return (EOF);
874 3efd8e31 2022-10-23 thomas if (parsebuf) {
875 3efd8e31 2022-10-23 thomas parseindex--;
876 3efd8e31 2022-10-23 thomas if (parseindex >= 0)
877 3efd8e31 2022-10-23 thomas return (c);
879 3efd8e31 2022-10-23 thomas if (pushback_index < MAXPUSHBACK-1)
880 3efd8e31 2022-10-23 thomas return (pushback_buffer[pushback_index++] = c);
882 3efd8e31 2022-10-23 thomas return (EOF);
886 3efd8e31 2022-10-23 thomas findeol(void)
890 3efd8e31 2022-10-23 thomas parsebuf = NULL;
892 3efd8e31 2022-10-23 thomas /* Skip to either EOF or the first real EOL. */
893 3efd8e31 2022-10-23 thomas while (1) {
894 3efd8e31 2022-10-23 thomas if (pushback_index)
895 3efd8e31 2022-10-23 thomas c = pushback_buffer[--pushback_index];
897 3efd8e31 2022-10-23 thomas c = lgetc(0);
898 3efd8e31 2022-10-23 thomas if (c == '\n') {
899 3efd8e31 2022-10-23 thomas file->lineno++;
902 3efd8e31 2022-10-23 thomas if (c == EOF)
905 3efd8e31 2022-10-23 thomas return (ERROR);
909 3efd8e31 2022-10-23 thomas yylex(void)
911 3efd8e31 2022-10-23 thomas unsigned char buf[8096];
912 3efd8e31 2022-10-23 thomas unsigned char *p, *val;
913 3efd8e31 2022-10-23 thomas int quotec, next, c;
914 3efd8e31 2022-10-23 thomas int token;
918 3efd8e31 2022-10-23 thomas c = lgetc(0);
919 3efd8e31 2022-10-23 thomas while (c == ' ' || c == '\t')
920 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
922 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
923 3efd8e31 2022-10-23 thomas if (c == '#') {
924 3efd8e31 2022-10-23 thomas c = lgetc(0);
925 3efd8e31 2022-10-23 thomas while (c != '\n' && c != EOF)
926 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
928 3efd8e31 2022-10-23 thomas if (c == '$' && parsebuf == NULL) {
929 3efd8e31 2022-10-23 thomas while (1) {
930 3efd8e31 2022-10-23 thomas c = lgetc(0);
931 3efd8e31 2022-10-23 thomas if (c == EOF)
932 3efd8e31 2022-10-23 thomas return (0);
934 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
935 3efd8e31 2022-10-23 thomas yyerror("string too long");
936 3efd8e31 2022-10-23 thomas return (findeol());
938 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == '_') {
939 3efd8e31 2022-10-23 thomas *p++ = c;
940 3efd8e31 2022-10-23 thomas continue;
942 3efd8e31 2022-10-23 thomas *p = '\0';
943 3efd8e31 2022-10-23 thomas lungetc(c);
946 3efd8e31 2022-10-23 thomas val = symget(buf);
947 3efd8e31 2022-10-23 thomas if (val == NULL) {
948 3efd8e31 2022-10-23 thomas yyerror("macro '%s' not defined", buf);
949 3efd8e31 2022-10-23 thomas return (findeol());
951 3efd8e31 2022-10-23 thomas parsebuf = val;
952 3efd8e31 2022-10-23 thomas parseindex = 0;
953 3efd8e31 2022-10-23 thomas goto top;
956 3efd8e31 2022-10-23 thomas switch (c) {
957 3efd8e31 2022-10-23 thomas case '\'':
958 3efd8e31 2022-10-23 thomas case '"':
959 3efd8e31 2022-10-23 thomas quotec = c;
960 3efd8e31 2022-10-23 thomas while (1) {
961 3efd8e31 2022-10-23 thomas c = lgetc(quotec);
962 3efd8e31 2022-10-23 thomas if (c == EOF)
963 3efd8e31 2022-10-23 thomas return (0);
964 3efd8e31 2022-10-23 thomas if (c == '\n') {
965 3efd8e31 2022-10-23 thomas file->lineno++;
966 3efd8e31 2022-10-23 thomas continue;
967 3efd8e31 2022-10-23 thomas } else if (c == '\\') {
968 3efd8e31 2022-10-23 thomas next = lgetc(quotec);
969 3efd8e31 2022-10-23 thomas if (next == EOF)
970 3efd8e31 2022-10-23 thomas return (0);
971 3efd8e31 2022-10-23 thomas if (next == quotec || c == ' ' || c == '\t')
972 3efd8e31 2022-10-23 thomas c = next;
973 3efd8e31 2022-10-23 thomas else if (next == '\n') {
974 3efd8e31 2022-10-23 thomas file->lineno++;
975 3efd8e31 2022-10-23 thomas continue;
977 3efd8e31 2022-10-23 thomas lungetc(next);
978 3efd8e31 2022-10-23 thomas } else if (c == quotec) {
979 3efd8e31 2022-10-23 thomas *p = '\0';
981 3efd8e31 2022-10-23 thomas } else if (c == '\0') {
982 3efd8e31 2022-10-23 thomas yyerror("syntax error");
983 3efd8e31 2022-10-23 thomas return (findeol());
985 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
986 3efd8e31 2022-10-23 thomas yyerror("string too long");
987 3efd8e31 2022-10-23 thomas return (findeol());
989 3efd8e31 2022-10-23 thomas *p++ = c;
991 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
992 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
993 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
994 3efd8e31 2022-10-23 thomas return (STRING);
997 3efd8e31 2022-10-23 thomas #define allowed_to_end_number(x) \
998 3efd8e31 2022-10-23 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
1000 3efd8e31 2022-10-23 thomas if (c == '-' || isdigit(c)) {
1002 3efd8e31 2022-10-23 thomas *p++ = c;
1003 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
1004 3efd8e31 2022-10-23 thomas yyerror("string too long");
1005 3efd8e31 2022-10-23 thomas return (findeol());
1007 3efd8e31 2022-10-23 thomas c = lgetc(0);
1008 3efd8e31 2022-10-23 thomas } while (c != EOF && isdigit(c));
1009 3efd8e31 2022-10-23 thomas lungetc(c);
1010 3efd8e31 2022-10-23 thomas if (p == buf + 1 && buf[0] == '-')
1011 3efd8e31 2022-10-23 thomas goto nodigits;
1012 3efd8e31 2022-10-23 thomas if (c == EOF || allowed_to_end_number(c)) {
1013 3efd8e31 2022-10-23 thomas const char *errstr = NULL;
1015 3efd8e31 2022-10-23 thomas *p = '\0';
1016 3efd8e31 2022-10-23 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
1017 3efd8e31 2022-10-23 thomas LLONG_MAX, &errstr);
1018 3efd8e31 2022-10-23 thomas if (errstr) {
1019 3efd8e31 2022-10-23 thomas yyerror("\"%s\" invalid number: %s",
1020 3efd8e31 2022-10-23 thomas buf, errstr);
1021 3efd8e31 2022-10-23 thomas return (findeol());
1023 3efd8e31 2022-10-23 thomas return (NUMBER);
1024 3efd8e31 2022-10-23 thomas } else {
1025 3efd8e31 2022-10-23 thomas nodigits:
1026 3efd8e31 2022-10-23 thomas while (p > buf + 1)
1027 3efd8e31 2022-10-23 thomas lungetc(*--p);
1028 3efd8e31 2022-10-23 thomas c = *--p;
1029 3efd8e31 2022-10-23 thomas if (c == '-')
1030 3efd8e31 2022-10-23 thomas return (c);
1034 3efd8e31 2022-10-23 thomas #define allowed_in_string(x) \
1035 3efd8e31 2022-10-23 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1036 3efd8e31 2022-10-23 thomas x != '{' && x != '}' && \
1037 3efd8e31 2022-10-23 thomas x != '!' && x != '=' && x != '#' && \
1038 3efd8e31 2022-10-23 thomas x != ','))
1040 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == ':' || c == '_') {
1042 3efd8e31 2022-10-23 thomas *p++ = c;
1043 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
1044 3efd8e31 2022-10-23 thomas yyerror("string too long");
1045 3efd8e31 2022-10-23 thomas return (findeol());
1047 3efd8e31 2022-10-23 thomas c = lgetc(0);
1048 3efd8e31 2022-10-23 thomas } while (c != EOF && (allowed_in_string(c)));
1049 3efd8e31 2022-10-23 thomas lungetc(c);
1050 3efd8e31 2022-10-23 thomas *p = '\0';
1051 3efd8e31 2022-10-23 thomas token = lookup(buf);
1052 3efd8e31 2022-10-23 thomas if (token == STRING) {
1053 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
1054 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
1055 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
1057 3efd8e31 2022-10-23 thomas return (token);
1059 3efd8e31 2022-10-23 thomas if (c == '\n') {
1060 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
1061 3efd8e31 2022-10-23 thomas file->lineno++;
1063 3efd8e31 2022-10-23 thomas if (c == EOF)
1064 3efd8e31 2022-10-23 thomas return (0);
1065 3efd8e31 2022-10-23 thomas return (c);
1069 3efd8e31 2022-10-23 thomas check_file_secrecy(int fd, const char *fname)
1071 3efd8e31 2022-10-23 thomas struct stat st;
1073 3efd8e31 2022-10-23 thomas if (fstat(fd, &st)) {
1074 3efd8e31 2022-10-23 thomas log_warn("cannot stat %s", fname);
1075 3efd8e31 2022-10-23 thomas return (-1);
1077 3efd8e31 2022-10-23 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
1078 3efd8e31 2022-10-23 thomas log_warnx("%s: owner not root or current user", fname);
1079 3efd8e31 2022-10-23 thomas return (-1);
1081 3efd8e31 2022-10-23 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
1082 3efd8e31 2022-10-23 thomas log_warnx("%s: group writable or world read/writable", fname);
1083 3efd8e31 2022-10-23 thomas return (-1);
1085 3efd8e31 2022-10-23 thomas return (0);
1088 3efd8e31 2022-10-23 thomas struct file *
1089 7554713a 2023-04-01 thomas newfile(const char *name, int secret, int required)
1091 3efd8e31 2022-10-23 thomas struct file *nfile;
1093 3efd8e31 2022-10-23 thomas nfile = calloc(1, sizeof(struct file));
1094 3efd8e31 2022-10-23 thomas if (nfile == NULL) {
1095 3efd8e31 2022-10-23 thomas log_warn("calloc");
1096 3efd8e31 2022-10-23 thomas return (NULL);
1098 3efd8e31 2022-10-23 thomas nfile->name = strdup(name);
1099 3efd8e31 2022-10-23 thomas if (nfile->name == NULL) {
1100 3efd8e31 2022-10-23 thomas log_warn("strdup");
1101 3efd8e31 2022-10-23 thomas free(nfile);
1102 3efd8e31 2022-10-23 thomas return (NULL);
1104 3efd8e31 2022-10-23 thomas nfile->stream = fopen(nfile->name, "r");
1105 3efd8e31 2022-10-23 thomas if (nfile->stream == NULL) {
1106 7554713a 2023-04-01 thomas if (required)
1107 7554713a 2023-04-01 thomas log_warn("open %s", nfile->name);
1108 3efd8e31 2022-10-23 thomas free(nfile->name);
1109 3efd8e31 2022-10-23 thomas free(nfile);
1110 3efd8e31 2022-10-23 thomas return (NULL);
1111 3efd8e31 2022-10-23 thomas } else if (secret &&
1112 3efd8e31 2022-10-23 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
1113 3efd8e31 2022-10-23 thomas fclose(nfile->stream);
1114 3efd8e31 2022-10-23 thomas free(nfile->name);
1115 3efd8e31 2022-10-23 thomas free(nfile);
1116 3efd8e31 2022-10-23 thomas return (NULL);
1118 3efd8e31 2022-10-23 thomas nfile->lineno = 1;
1119 3efd8e31 2022-10-23 thomas return (nfile);
1122 3efd8e31 2022-10-23 thomas static void
1123 3efd8e31 2022-10-23 thomas closefile(struct file *xfile)
1125 3efd8e31 2022-10-23 thomas fclose(xfile->stream);
1126 3efd8e31 2022-10-23 thomas free(xfile->name);
1127 3efd8e31 2022-10-23 thomas free(xfile);
1131 3efd8e31 2022-10-23 thomas parse_config(const char *filename, enum gotd_procid proc_id,
1132 f3807fe5 2023-07-10 thomas struct gotd *env)
1134 3efd8e31 2022-10-23 thomas struct sym *sym, *next;
1135 88f1bb6d 2023-01-02 thomas struct gotd_repo *repo;
1136 f3807fe5 2023-07-10 thomas int require_config_file = (proc_id != PROC_GITWRAPPER);
1138 3efd8e31 2022-10-23 thomas memset(env, 0, sizeof(*env));
1140 3efd8e31 2022-10-23 thomas gotd = env;
1141 3efd8e31 2022-10-23 thomas gotd_proc_id = proc_id;
1142 3efd8e31 2022-10-23 thomas TAILQ_INIT(&gotd->repos);
1144 3efd8e31 2022-10-23 thomas /* Apply default values. */
1145 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
1146 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
1147 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: unix socket path too long", __func__);
1148 3efd8e31 2022-10-23 thomas return -1;
1150 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, GOTD_USER,
1151 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
1152 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: user name too long", __func__);
1153 3efd8e31 2022-10-23 thomas return -1;
1156 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
1157 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_usec = 0;
1159 7554713a 2023-04-01 thomas file = newfile(filename, 0, require_config_file);
1160 f3296add 2023-03-01 thomas if (file == NULL)
1161 7554713a 2023-04-01 thomas return require_config_file ? -1 : 0;
1163 3efd8e31 2022-10-23 thomas yyparse();
1164 3efd8e31 2022-10-23 thomas errors = file->errors;
1165 3efd8e31 2022-10-23 thomas closefile(file);
1167 3efd8e31 2022-10-23 thomas /* Free macros and check which have not been used. */
1168 3efd8e31 2022-10-23 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1169 3efd8e31 2022-10-23 thomas if ((gotd->verbosity > 1) && !sym->used)
1170 3efd8e31 2022-10-23 thomas fprintf(stderr, "warning: macro '%s' not used\n",
1171 3efd8e31 2022-10-23 thomas sym->nam);
1172 3efd8e31 2022-10-23 thomas if (!sym->persist) {
1173 3efd8e31 2022-10-23 thomas free(sym->nam);
1174 3efd8e31 2022-10-23 thomas free(sym->val);
1175 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
1176 3efd8e31 2022-10-23 thomas free(sym);
1180 3efd8e31 2022-10-23 thomas if (errors)
1181 3efd8e31 2022-10-23 thomas return (-1);
1183 88f1bb6d 2023-01-02 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
1184 88f1bb6d 2023-01-02 thomas if (repo->path[0] == '\0') {
1185 0cbf8de7 2023-01-02 thomas log_warnx("repository \"%s\": no path provided in "
1186 0cbf8de7 2023-01-02 thomas "configuration file", repo->name);
1187 88f1bb6d 2023-01-02 thomas return (-1);
1191 4d24b1fd 2023-01-19 thomas if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
1192 4d24b1fd 2023-01-19 thomas log_warnx("no repository defined in configuration file");
1193 4d24b1fd 2023-01-19 thomas return (-1);
1196 3efd8e31 2022-10-23 thomas return (0);
1199 0781db0e 2023-01-06 thomas static int
1200 0781db0e 2023-01-06 thomas uid_connection_limit_cmp(const void *pa, const void *pb)
1202 0781db0e 2023-01-06 thomas const struct gotd_uid_connection_limit *a = pa, *b = pb;
1204 0781db0e 2023-01-06 thomas if (a->uid < b->uid)
1205 0781db0e 2023-01-06 thomas return -1;
1206 0781db0e 2023-01-06 thomas else if (a->uid > b->uid);
1207 0781db0e 2023-01-06 thomas return 1;
1209 0781db0e 2023-01-06 thomas return 0;
1212 0781db0e 2023-01-06 thomas static int
1213 0781db0e 2023-01-06 thomas conf_limit_user_connections(const char *user, int maximum)
1215 0781db0e 2023-01-06 thomas uid_t uid;
1216 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *limit;
1217 0781db0e 2023-01-06 thomas size_t nlimits;
1219 0781db0e 2023-01-06 thomas if (maximum < 1) {
1220 0781db0e 2023-01-06 thomas yyerror("max connections cannot be smaller 1");
1221 0781db0e 2023-01-06 thomas return -1;
1223 0781db0e 2023-01-06 thomas if (maximum > GOTD_MAXCLIENTS) {
1224 0781db0e 2023-01-06 thomas yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
1225 0781db0e 2023-01-06 thomas return -1;
1228 48488136 2023-04-22 thomas if (gotd_parseuid(user, &uid) == -1) {
1229 0781db0e 2023-01-06 thomas yyerror("%s: no such user", user);
1230 0781db0e 2023-01-06 thomas return -1;
1233 0781db0e 2023-01-06 thomas limit = gotd_find_uid_connection_limit(gotd->connection_limits,
1234 0781db0e 2023-01-06 thomas gotd->nconnection_limits, uid);
1235 0781db0e 2023-01-06 thomas if (limit) {
1236 0781db0e 2023-01-06 thomas limit->max_connections = maximum;
1237 0781db0e 2023-01-06 thomas return 0;
1240 0781db0e 2023-01-06 thomas limit = gotd->connection_limits;
1241 0781db0e 2023-01-06 thomas nlimits = gotd->nconnection_limits + 1;
1242 0781db0e 2023-01-06 thomas limit = reallocarray(limit, nlimits, sizeof(*limit));
1243 0781db0e 2023-01-06 thomas if (limit == NULL)
1244 0781db0e 2023-01-06 thomas fatal("reallocarray");
1246 0781db0e 2023-01-06 thomas limit[nlimits - 1].uid = uid;
1247 0781db0e 2023-01-06 thomas limit[nlimits - 1].max_connections = maximum;
1249 0781db0e 2023-01-06 thomas gotd->connection_limits = limit;
1250 0781db0e 2023-01-06 thomas gotd->nconnection_limits = nlimits;
1251 0781db0e 2023-01-06 thomas qsort(gotd->connection_limits, gotd->nconnection_limits,
1252 0781db0e 2023-01-06 thomas sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
1254 0781db0e 2023-01-06 thomas return 0;
1257 3efd8e31 2022-10-23 thomas static struct gotd_repo *
1258 3efd8e31 2022-10-23 thomas conf_new_repo(const char *name)
1260 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
1262 a42b418b 2023-01-02 thomas if (name[0] == '\0') {
1263 a42b418b 2023-01-02 thomas fatalx("syntax error: empty repository name found in %s",
1264 a42b418b 2023-01-02 thomas file->name);
1267 0cbf8de7 2023-01-02 thomas if (strchr(name, '\n') != NULL)
1268 0cbf8de7 2023-01-02 thomas fatalx("repository names must not contain linefeeds: %s", name);
1270 3efd8e31 2022-10-23 thomas repo = calloc(1, sizeof(*repo));
1271 3efd8e31 2022-10-23 thomas if (repo == NULL)
1272 3efd8e31 2022-10-23 thomas fatalx("%s: calloc", __func__);
1274 729a7e24 2022-11-17 thomas STAILQ_INIT(&repo->rules);
1275 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_tag_namespaces);
1276 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_branch_namespaces);
1277 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_branches);
1278 ce1bfad9 2024-03-30 thomas TAILQ_INIT(&repo->protected_branches);
1279 ce1bfad9 2024-03-30 thomas TAILQ_INIT(&repo->notification_refs);
1280 ce1bfad9 2024-03-30 thomas TAILQ_INIT(&repo->notification_ref_namespaces);
1281 ce1bfad9 2024-03-30 thomas STAILQ_INIT(&repo->notification_targets);
1283 3efd8e31 2022-10-23 thomas if (strlcpy(repo->name, name, sizeof(repo->name)) >=
1284 3efd8e31 2022-10-23 thomas sizeof(repo->name))
1285 3efd8e31 2022-10-23 thomas fatalx("%s: strlcpy", __func__);
1287 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
1288 3efd8e31 2022-10-23 thomas gotd->nrepos++;
1290 3efd8e31 2022-10-23 thomas return repo;
1293 729a7e24 2022-11-17 thomas static void
1294 729a7e24 2022-11-17 thomas conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
1295 729a7e24 2022-11-17 thomas int authorization, char *identifier)
1297 729a7e24 2022-11-17 thomas struct gotd_access_rule *rule;
1299 729a7e24 2022-11-17 thomas rule = calloc(1, sizeof(*rule));
1300 729a7e24 2022-11-17 thomas if (rule == NULL)
1301 729a7e24 2022-11-17 thomas fatal("calloc");
1303 729a7e24 2022-11-17 thomas rule->access = access;
1304 729a7e24 2022-11-17 thomas rule->authorization = authorization;
1305 729a7e24 2022-11-17 thomas rule->identifier = identifier;
1307 729a7e24 2022-11-17 thomas STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
1310 6d7eb4f7 2023-04-04 thomas static int
1311 6d7eb4f7 2023-04-04 thomas refname_is_valid(char *refname)
1313 ef44e136 2023-06-15 thomas if (strncmp(refname, "refs/", 5) != 0) {
1314 6d7eb4f7 2023-04-04 thomas yyerror("reference name must begin with \"refs/\": %s",
1315 6d7eb4f7 2023-04-04 thomas refname);
1316 6d7eb4f7 2023-04-04 thomas return 0;
1319 6d7eb4f7 2023-04-04 thomas if (!got_ref_name_is_valid(refname)) {
1320 6d7eb4f7 2023-04-04 thomas yyerror("invalid reference name: %s", refname);
1321 6d7eb4f7 2023-04-04 thomas return 0;
1324 6d7eb4f7 2023-04-04 thomas return 1;
1327 6d7eb4f7 2023-04-04 thomas static int
1328 57b6056a 2023-04-05 thomas conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
1329 57b6056a 2023-04-05 thomas char *namespace)
1331 6d7eb4f7 2023-04-04 thomas const struct got_error *error;
1332 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
1333 6d7eb4f7 2023-04-04 thomas char *s;
1335 57b6056a 2023-04-05 thomas *new = NULL;
1337 6d7eb4f7 2023-04-04 thomas got_path_strip_trailing_slashes(namespace);
1338 6d7eb4f7 2023-04-04 thomas if (!refname_is_valid(namespace))
1339 6d7eb4f7 2023-04-04 thomas return -1;
1340 6d7eb4f7 2023-04-04 thomas if (asprintf(&s, "%s/", namespace) == -1) {
1341 6d7eb4f7 2023-04-04 thomas yyerror("asprintf: %s", strerror(errno));
1342 6d7eb4f7 2023-04-04 thomas return -1;
1345 57b6056a 2023-04-05 thomas error = got_pathlist_insert(&pe, refs, s, NULL);
1346 57b6056a 2023-04-05 thomas if (error || pe == NULL) {
1347 a4435ef0 2023-04-05 thomas free(s);
1348 a4435ef0 2023-04-05 thomas if (error)
1349 a4435ef0 2023-04-05 thomas yyerror("got_pathlist_insert: %s", error->msg);
1351 3f0853b8 2023-04-07 thomas yyerror("duplicate protected namespace %s", namespace);
1352 6d7eb4f7 2023-04-04 thomas return -1;
1355 57b6056a 2023-04-05 thomas *new = s;
1356 6d7eb4f7 2023-04-04 thomas return 0;
1359 6d7eb4f7 2023-04-04 thomas static int
1360 6d7eb4f7 2023-04-04 thomas conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
1362 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
1363 57b6056a 2023-04-05 thomas char *new;
1365 57b6056a 2023-04-05 thomas if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
1366 57b6056a 2023-04-05 thomas namespace) == -1)
1367 57b6056a 2023-04-05 thomas return -1;
1369 57b6056a 2023-04-05 thomas TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
1370 57b6056a 2023-04-05 thomas if (strcmp(pe->path, new) == 0) {
1371 3f0853b8 2023-04-07 thomas yyerror("duplicate protected namespace %s", namespace);
1372 57b6056a 2023-04-05 thomas return -1;
1376 57b6056a 2023-04-05 thomas return 0;
1379 6d7eb4f7 2023-04-04 thomas static int
1380 6d7eb4f7 2023-04-04 thomas conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
1382 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
1383 57b6056a 2023-04-05 thomas char *new;
1385 57b6056a 2023-04-05 thomas if (conf_protect_ref_namespace(&new,
1386 57b6056a 2023-04-05 thomas &repo->protected_branch_namespaces, namespace) == -1)
1387 57b6056a 2023-04-05 thomas return -1;
1389 57b6056a 2023-04-05 thomas TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
1390 57b6056a 2023-04-05 thomas if (strcmp(pe->path, new) == 0) {
1391 3f0853b8 2023-04-07 thomas yyerror("duplicate protected namespace %s", namespace);
1392 57b6056a 2023-04-05 thomas return -1;
1396 57b6056a 2023-04-05 thomas return 0;
1399 6d7eb4f7 2023-04-04 thomas static int
1400 6d7eb4f7 2023-04-04 thomas conf_protect_branch(struct gotd_repo *repo, char *branchname)
1402 6d7eb4f7 2023-04-04 thomas const struct got_error *error;
1403 a4435ef0 2023-04-05 thomas struct got_pathlist_entry *new;
1404 6d7eb4f7 2023-04-04 thomas char *refname;
1406 6d7eb4f7 2023-04-04 thomas if (strncmp(branchname, "refs/heads/", 11) != 0) {
1407 6d7eb4f7 2023-04-04 thomas if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1408 6d7eb4f7 2023-04-04 thomas yyerror("asprintf: %s", strerror(errno));
1409 6d7eb4f7 2023-04-04 thomas return -1;
1411 6d7eb4f7 2023-04-04 thomas } else {
1412 6d7eb4f7 2023-04-04 thomas refname = strdup(branchname);
1413 6d7eb4f7 2023-04-04 thomas if (refname == NULL) {
1414 6d7eb4f7 2023-04-04 thomas yyerror("strdup: %s", strerror(errno));
1415 6d7eb4f7 2023-04-04 thomas return -1;
1419 6d7eb4f7 2023-04-04 thomas if (!refname_is_valid(refname)) {
1420 6d7eb4f7 2023-04-04 thomas free(refname);
1421 6d7eb4f7 2023-04-04 thomas return -1;
1424 a4435ef0 2023-04-05 thomas error = got_pathlist_insert(&new, &repo->protected_branches,
1425 6d7eb4f7 2023-04-04 thomas refname, NULL);
1426 a4435ef0 2023-04-05 thomas if (error || new == NULL) {
1427 a4435ef0 2023-04-05 thomas free(refname);
1428 a4435ef0 2023-04-05 thomas if (error)
1429 a4435ef0 2023-04-05 thomas yyerror("got_pathlist_insert: %s", error->msg);
1431 a4435ef0 2023-04-05 thomas yyerror("duplicate protect branch %s", branchname);
1432 ce1bfad9 2024-03-30 thomas return -1;
1435 ce1bfad9 2024-03-30 thomas return 0;
1438 ce1bfad9 2024-03-30 thomas static int
1439 ce1bfad9 2024-03-30 thomas conf_notify_branch(struct gotd_repo *repo, char *branchname)
1441 ce1bfad9 2024-03-30 thomas const struct got_error *error;
1442 ce1bfad9 2024-03-30 thomas struct got_pathlist_entry *pe;
1443 ce1bfad9 2024-03-30 thomas char *refname;
1445 ce1bfad9 2024-03-30 thomas if (strncmp(branchname, "refs/heads/", 11) != 0) {
1446 ce1bfad9 2024-03-30 thomas if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1447 ce1bfad9 2024-03-30 thomas yyerror("asprintf: %s", strerror(errno));
1448 ce1bfad9 2024-03-30 thomas return -1;
1450 ce1bfad9 2024-03-30 thomas } else {
1451 ce1bfad9 2024-03-30 thomas refname = strdup(branchname);
1452 ce1bfad9 2024-03-30 thomas if (refname == NULL) {
1453 ce1bfad9 2024-03-30 thomas yyerror("strdup: %s", strerror(errno));
1454 ce1bfad9 2024-03-30 thomas return -1;
1458 ce1bfad9 2024-03-30 thomas if (!refname_is_valid(refname)) {
1459 ce1bfad9 2024-03-30 thomas free(refname);
1460 6d7eb4f7 2023-04-04 thomas return -1;
1463 ce1bfad9 2024-03-30 thomas error = got_pathlist_insert(&pe, &repo->notification_refs,
1464 ce1bfad9 2024-03-30 thomas refname, NULL);
1465 ce1bfad9 2024-03-30 thomas if (error) {
1466 ce1bfad9 2024-03-30 thomas free(refname);
1467 ce1bfad9 2024-03-30 thomas yyerror("got_pathlist_insert: %s", error->msg);
1468 ce1bfad9 2024-03-30 thomas return -1;
1470 ce1bfad9 2024-03-30 thomas if (pe == NULL)
1471 ce1bfad9 2024-03-30 thomas free(refname);
1473 6d7eb4f7 2023-04-04 thomas return 0;
1476 ce1bfad9 2024-03-30 thomas static int
1477 ce1bfad9 2024-03-30 thomas conf_notify_ref_namespace(struct gotd_repo *repo, char *namespace)
1479 ce1bfad9 2024-03-30 thomas const struct got_error *error;
1480 ce1bfad9 2024-03-30 thomas struct got_pathlist_entry *pe;
1481 ce1bfad9 2024-03-30 thomas char *s;
1483 ce1bfad9 2024-03-30 thomas got_path_strip_trailing_slashes(namespace);
1484 ce1bfad9 2024-03-30 thomas if (!refname_is_valid(namespace))
1485 ce1bfad9 2024-03-30 thomas return -1;
1487 ce1bfad9 2024-03-30 thomas if (asprintf(&s, "%s/", namespace) == -1) {
1488 ce1bfad9 2024-03-30 thomas yyerror("asprintf: %s", strerror(errno));
1489 ce1bfad9 2024-03-30 thomas return -1;
1492 ce1bfad9 2024-03-30 thomas error = got_pathlist_insert(&pe, &repo->notification_ref_namespaces,
1493 ce1bfad9 2024-03-30 thomas s, NULL);
1494 ce1bfad9 2024-03-30 thomas if (error) {
1495 ce1bfad9 2024-03-30 thomas free(s);
1496 ce1bfad9 2024-03-30 thomas yyerror("got_pathlist_insert: %s", error->msg);
1497 ce1bfad9 2024-03-30 thomas return -1;
1499 ce1bfad9 2024-03-30 thomas if (pe == NULL)
1500 ce1bfad9 2024-03-30 thomas free(s);
1502 ce1bfad9 2024-03-30 thomas return 0;
1505 ce1bfad9 2024-03-30 thomas static int
1506 ce1bfad9 2024-03-30 thomas conf_notify_email(struct gotd_repo *repo, char *sender, char *recipient,
1507 ce1bfad9 2024-03-30 thomas char *responder, char *hostname, char *port)
1509 ce1bfad9 2024-03-30 thomas struct gotd_notification_target *target;
1511 ce1bfad9 2024-03-30 thomas STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1512 ce1bfad9 2024-03-30 thomas if (target->type != GOTD_NOTIFICATION_VIA_EMAIL)
1513 ce1bfad9 2024-03-30 thomas continue;
1514 ce1bfad9 2024-03-30 thomas if (strcmp(target->conf.email.recipient, recipient) == 0) {
1515 ce1bfad9 2024-03-30 thomas yyerror("duplicate email notification for '%s' in "
1516 ce1bfad9 2024-03-30 thomas "repository '%s'", recipient, repo->name);
1517 ce1bfad9 2024-03-30 thomas return -1;
1521 ce1bfad9 2024-03-30 thomas target = calloc(1, sizeof(*target));
1522 ce1bfad9 2024-03-30 thomas if (target == NULL)
1523 ce1bfad9 2024-03-30 thomas fatal("calloc");
1524 ce1bfad9 2024-03-30 thomas target->type = GOTD_NOTIFICATION_VIA_EMAIL;
1525 ce1bfad9 2024-03-30 thomas if (sender) {
1526 ce1bfad9 2024-03-30 thomas target->conf.email.sender = strdup(sender);
1527 ce1bfad9 2024-03-30 thomas if (target->conf.email.sender == NULL)
1528 ce1bfad9 2024-03-30 thomas fatal("strdup");
1530 ce1bfad9 2024-03-30 thomas target->conf.email.recipient = strdup(recipient);
1531 ce1bfad9 2024-03-30 thomas if (target->conf.email.recipient == NULL)
1532 ce1bfad9 2024-03-30 thomas fatal("strdup");
1533 ce1bfad9 2024-03-30 thomas if (responder) {
1534 ce1bfad9 2024-03-30 thomas target->conf.email.responder = strdup(responder);
1535 ce1bfad9 2024-03-30 thomas if (target->conf.email.responder == NULL)
1536 ce1bfad9 2024-03-30 thomas fatal("strdup");
1538 ce1bfad9 2024-03-30 thomas if (hostname) {
1539 ce1bfad9 2024-03-30 thomas target->conf.email.hostname = strdup(hostname);
1540 ce1bfad9 2024-03-30 thomas if (target->conf.email.hostname == NULL)
1541 ce1bfad9 2024-03-30 thomas fatal("strdup");
1543 ce1bfad9 2024-03-30 thomas if (port) {
1544 ce1bfad9 2024-03-30 thomas target->conf.email.port = strdup(port);
1545 ce1bfad9 2024-03-30 thomas if (target->conf.email.port == NULL)
1546 ce1bfad9 2024-03-30 thomas fatal("strdup");
1549 ce1bfad9 2024-03-30 thomas STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1550 ce1bfad9 2024-03-30 thomas return 0;
1553 ce1bfad9 2024-03-30 thomas static int
1554 2ae445d7 2024-04-25 thomas.ad conf_notify_http(struct gotd_repo *repo, char *url, char *user, char *password,
1555 2ae445d7 2024-04-25 thomas.ad int insecure)
1557 ce1bfad9 2024-03-30 thomas const struct got_error *error;
1558 ce1bfad9 2024-03-30 thomas struct gotd_notification_target *target;
1559 94a3f4e9 2024-03-30 thomas char *proto, *hostname, *port, *path;
1560 94a3f4e9 2024-03-30 thomas int tls = 0, ret = 0;
1562 94a3f4e9 2024-03-30 thomas error = gotd_parse_url(&proto, &hostname, &port, &path, url);
1563 ce1bfad9 2024-03-30 thomas if (error) {
1564 ce1bfad9 2024-03-30 thomas yyerror("invalid HTTP notification URL '%s' in "
1565 ce1bfad9 2024-03-30 thomas "repository '%s': %s", url, repo->name, error->msg);
1566 ce1bfad9 2024-03-30 thomas return -1;
1569 94a3f4e9 2024-03-30 thomas tls = !strcmp(proto, "https");
1571 ce1bfad9 2024-03-30 thomas if (strcmp(proto, "http") != 0 && strcmp(proto, "https") != 0) {
1572 ce1bfad9 2024-03-30 thomas yyerror("invalid protocol '%s' in notification URL '%s' in "
1573 ce1bfad9 2024-03-30 thomas "repository '%s", proto, url, repo->name);
1574 94a3f4e9 2024-03-30 thomas ret = -1;
1575 94a3f4e9 2024-03-30 thomas goto done;
1578 b290a4cc 2024-04-25 thomas.ad if (port == NULL) {
1579 b290a4cc 2024-04-25 thomas.ad if (strcmp(proto, "http") == 0)
1580 b290a4cc 2024-04-25 thomas.ad port = strdup("80");
1581 b290a4cc 2024-04-25 thomas.ad if (strcmp(proto, "https") == 0)
1582 b290a4cc 2024-04-25 thomas.ad port = strdup("443");
1583 b290a4cc 2024-04-25 thomas.ad if (port == NULL) {
1584 b290a4cc 2024-04-25 thomas.ad error = got_error_from_errno("strdup");
1585 b290a4cc 2024-04-25 thomas.ad ret = -1;
1586 b290a4cc 2024-04-25 thomas.ad goto done;
1590 94a3f4e9 2024-03-30 thomas if ((user != NULL && password == NULL) ||
1591 94a3f4e9 2024-03-30 thomas (user == NULL && password != NULL)) {
1592 94a3f4e9 2024-03-30 thomas yyerror("missing username or password");
1593 ce1bfad9 2024-03-30 thomas ret = -1;
1594 ce1bfad9 2024-03-30 thomas goto done;
1597 2ae445d7 2024-04-25 thomas.ad if (!insecure && strcmp(proto, "http") == 0 &&
1598 2ae445d7 2024-04-25 thomas.ad (user != NULL || password != NULL)) {
1599 2ae445d7 2024-04-25 thomas.ad yyerror("%s: HTTP notifications with basic authentication "
1600 2ae445d7 2024-04-25 thomas.ad "over plaintext HTTP will leak credentials; add the "
1601 2ae445d7 2024-04-25 thomas.ad "'insecure' config keyword if this is intentional", url);
1602 2ae445d7 2024-04-25 thomas.ad ret = -1;
1603 2ae445d7 2024-04-25 thomas.ad goto done;
1606 ce1bfad9 2024-03-30 thomas STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1607 ce1bfad9 2024-03-30 thomas if (target->type != GOTD_NOTIFICATION_VIA_HTTP)
1608 ce1bfad9 2024-03-30 thomas continue;
1609 94a3f4e9 2024-03-30 thomas if (target->conf.http.tls == tls &&
1610 94a3f4e9 2024-03-30 thomas !strcmp(target->conf.http.hostname, hostname) &&
1611 94a3f4e9 2024-03-30 thomas !strcmp(target->conf.http.port, port) &&
1612 94a3f4e9 2024-03-30 thomas !strcmp(target->conf.http.path, path)) {
1613 ce1bfad9 2024-03-30 thomas yyerror("duplicate notification for URL '%s' in "
1614 ce1bfad9 2024-03-30 thomas "repository '%s'", url, repo->name);
1615 ce1bfad9 2024-03-30 thomas ret = -1;
1616 ce1bfad9 2024-03-30 thomas goto done;
1620 ce1bfad9 2024-03-30 thomas target = calloc(1, sizeof(*target));
1621 ce1bfad9 2024-03-30 thomas if (target == NULL)
1622 ce1bfad9 2024-03-30 thomas fatal("calloc");
1623 ce1bfad9 2024-03-30 thomas target->type = GOTD_NOTIFICATION_VIA_HTTP;
1624 94a3f4e9 2024-03-30 thomas target->conf.http.tls = tls;
1625 94a3f4e9 2024-03-30 thomas target->conf.http.hostname = hostname;
1626 94a3f4e9 2024-03-30 thomas target->conf.http.port = port;
1627 94a3f4e9 2024-03-30 thomas target->conf.http.path = path;
1628 94a3f4e9 2024-03-30 thomas hostname = port = path = NULL;
1630 ce1bfad9 2024-03-30 thomas if (user) {
1631 ce1bfad9 2024-03-30 thomas target->conf.http.user = strdup(user);
1632 ce1bfad9 2024-03-30 thomas if (target->conf.http.user == NULL)
1633 94a3f4e9 2024-03-30 thomas fatal("strdup");
1634 ce1bfad9 2024-03-30 thomas target->conf.http.password = strdup(password);
1635 ce1bfad9 2024-03-30 thomas if (target->conf.http.password == NULL)
1636 94a3f4e9 2024-03-30 thomas fatal("strdup");
1639 ce1bfad9 2024-03-30 thomas STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1641 ce1bfad9 2024-03-30 thomas free(proto);
1642 94a3f4e9 2024-03-30 thomas free(hostname);
1643 ce1bfad9 2024-03-30 thomas free(port);
1644 94a3f4e9 2024-03-30 thomas free(path);
1645 ce1bfad9 2024-03-30 thomas return ret;
1649 3efd8e31 2022-10-23 thomas symset(const char *nam, const char *val, int persist)
1651 3efd8e31 2022-10-23 thomas struct sym *sym;
1653 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1654 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0)
1658 3efd8e31 2022-10-23 thomas if (sym != NULL) {
1659 3efd8e31 2022-10-23 thomas if (sym->persist == 1)
1660 3efd8e31 2022-10-23 thomas return (0);
1662 3efd8e31 2022-10-23 thomas free(sym->nam);
1663 3efd8e31 2022-10-23 thomas free(sym->val);
1664 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
1665 3efd8e31 2022-10-23 thomas free(sym);
1668 3efd8e31 2022-10-23 thomas sym = calloc(1, sizeof(*sym));
1669 3efd8e31 2022-10-23 thomas if (sym == NULL)
1670 3efd8e31 2022-10-23 thomas return (-1);
1672 3efd8e31 2022-10-23 thomas sym->nam = strdup(nam);
1673 3efd8e31 2022-10-23 thomas if (sym->nam == NULL) {
1674 3efd8e31 2022-10-23 thomas free(sym);
1675 3efd8e31 2022-10-23 thomas return (-1);
1677 3efd8e31 2022-10-23 thomas sym->val = strdup(val);
1678 3efd8e31 2022-10-23 thomas if (sym->val == NULL) {
1679 3efd8e31 2022-10-23 thomas free(sym->nam);
1680 3efd8e31 2022-10-23 thomas free(sym);
1681 3efd8e31 2022-10-23 thomas return (-1);
1683 3efd8e31 2022-10-23 thomas sym->used = 0;
1684 3efd8e31 2022-10-23 thomas sym->persist = persist;
1685 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
1686 3efd8e31 2022-10-23 thomas return (0);
1690 3efd8e31 2022-10-23 thomas symget(const char *nam)
1692 3efd8e31 2022-10-23 thomas struct sym *sym;
1694 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1695 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0) {
1696 3efd8e31 2022-10-23 thomas sym->used = 1;
1697 3efd8e31 2022-10-23 thomas return (sym->val);
1700 3efd8e31 2022-10-23 thomas return (NULL);
1703 5dcb3a43 2023-04-01 thomas struct gotd_repo *
1704 ce1bfad9 2024-03-30 thomas gotd_find_repo_by_name(const char *repo_name, struct gotd_repolist *repos)
1706 5dcb3a43 2023-04-01 thomas struct gotd_repo *repo;
1707 5dcb3a43 2023-04-01 thomas size_t namelen;
1709 ce1bfad9 2024-03-30 thomas TAILQ_FOREACH(repo, repos, entry) {
1710 5dcb3a43 2023-04-01 thomas namelen = strlen(repo->name);
1711 5dcb3a43 2023-04-01 thomas if (strncmp(repo->name, repo_name, namelen) != 0)
1712 5dcb3a43 2023-04-01 thomas continue;
1713 5dcb3a43 2023-04-01 thomas if (repo_name[namelen] == '\0' ||
1714 5dcb3a43 2023-04-01 thomas strcmp(&repo_name[namelen], ".git") == 0)
1715 5dcb3a43 2023-04-01 thomas return repo;
1718 5dcb3a43 2023-04-01 thomas return NULL;
1721 6d7eb4f7 2023-04-04 thomas struct gotd_repo *
1722 6d7eb4f7 2023-04-04 thomas gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1724 6d7eb4f7 2023-04-04 thomas struct gotd_repo *repo;
1726 6d7eb4f7 2023-04-04 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
1727 6d7eb4f7 2023-04-04 thomas if (strcmp(repo->path, repo_path) == 0)
1728 6d7eb4f7 2023-04-04 thomas return repo;
1731 65a36f17 2023-04-22 thomas return NULL;
1734 65a36f17 2023-04-22 thomas struct gotd_uid_connection_limit *
1735 65a36f17 2023-04-22 thomas gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
1736 65a36f17 2023-04-22 thomas size_t nlimits, uid_t uid)
1738 65a36f17 2023-04-22 thomas /* This array is always sorted to allow for binary search. */
1739 65a36f17 2023-04-22 thomas int i, left = 0, right = nlimits - 1;
1741 65a36f17 2023-04-22 thomas while (left <= right) {
1742 65a36f17 2023-04-22 thomas i = ((left + right) / 2);
1743 65a36f17 2023-04-22 thomas if (limits[i].uid == uid)
1744 65a36f17 2023-04-22 thomas return &limits[i];
1745 65a36f17 2023-04-22 thomas if (limits[i].uid > uid)
1746 65a36f17 2023-04-22 thomas left = i + 1;
1748 65a36f17 2023-04-22 thomas right = i - 1;
1751 6d7eb4f7 2023-04-04 thomas return NULL;
1755 48488136 2023-04-22 thomas gotd_parseuid(const char *s, uid_t *uid)
1757 48488136 2023-04-22 thomas struct passwd *pw;
1758 48488136 2023-04-22 thomas const char *errstr;
1760 48488136 2023-04-22 thomas if ((pw = getpwnam(s)) != NULL) {
1761 48488136 2023-04-22 thomas *uid = pw->pw_uid;
1762 48488136 2023-04-22 thomas if (*uid == UID_MAX)
1763 48488136 2023-04-22 thomas return -1;
1764 48488136 2023-04-22 thomas return 0;
1766 48488136 2023-04-22 thomas *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1767 48488136 2023-04-22 thomas if (errstr)
1768 48488136 2023-04-22 thomas return -1;
1769 48488136 2023-04-22 thomas return 0;
1772 ce1bfad9 2024-03-30 thomas const struct got_error *
1773 ce1bfad9 2024-03-30 thomas gotd_parse_url(char **proto, char **host, char **port,
1774 ce1bfad9 2024-03-30 thomas char **request_path, const char *url)
1776 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
1777 ce1bfad9 2024-03-30 thomas char *s, *p, *q;
1779 ce1bfad9 2024-03-30 thomas *proto = *host = *port = *request_path = NULL;
1781 ce1bfad9 2024-03-30 thomas p = strstr(url, "://");
1783 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PARSE_URI);
1785 ce1bfad9 2024-03-30 thomas *proto = strndup(url, p - url);
1786 ce1bfad9 2024-03-30 thomas if (*proto == NULL) {
1787 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strndup");
1788 ce1bfad9 2024-03-30 thomas goto done;
1790 ce1bfad9 2024-03-30 thomas s = p + 3;
1792 ce1bfad9 2024-03-30 thomas p = strstr(s, "/");
1793 bc0cdda1 2024-03-30 thomas if (p == NULL) {
1794 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PARSE_URI);
1795 ce1bfad9 2024-03-30 thomas goto done;
1798 ce1bfad9 2024-03-30 thomas q = memchr(s, ':', p - s);
1799 ce1bfad9 2024-03-30 thomas if (q) {
1800 ce1bfad9 2024-03-30 thomas *host = strndup(s, q - s);
1801 ce1bfad9 2024-03-30 thomas if (*host == NULL) {
1802 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strndup");
1803 ce1bfad9 2024-03-30 thomas goto done;
1805 ce1bfad9 2024-03-30 thomas if ((*host)[0] == '\0') {
1806 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PARSE_URI);
1807 ce1bfad9 2024-03-30 thomas goto done;
1809 ce1bfad9 2024-03-30 thomas *port = strndup(q + 1, p - (q + 1));
1810 ce1bfad9 2024-03-30 thomas if (*port == NULL) {
1811 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strndup");
1812 ce1bfad9 2024-03-30 thomas goto done;
1814 ce1bfad9 2024-03-30 thomas if ((*port)[0] == '\0') {
1815 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PARSE_URI);
1816 ce1bfad9 2024-03-30 thomas goto done;
1818 ce1bfad9 2024-03-30 thomas } else {
1819 ce1bfad9 2024-03-30 thomas *host = strndup(s, p - s);
1820 ce1bfad9 2024-03-30 thomas if (*host == NULL) {
1821 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strndup");
1822 ce1bfad9 2024-03-30 thomas goto done;
1824 ce1bfad9 2024-03-30 thomas if ((*host)[0] == '\0') {
1825 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PARSE_URI);
1826 ce1bfad9 2024-03-30 thomas goto done;
1830 ce1bfad9 2024-03-30 thomas while (p[0] == '/' && p[1] == '/')
1832 ce1bfad9 2024-03-30 thomas *request_path = strdup(p);
1833 ce1bfad9 2024-03-30 thomas if (*request_path == NULL) {
1834 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strdup");
1835 ce1bfad9 2024-03-30 thomas goto done;
1837 ce1bfad9 2024-03-30 thomas if ((*request_path)[0] == '\0') {
1838 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PARSE_URI);
1839 ce1bfad9 2024-03-30 thomas goto done;
1842 ce1bfad9 2024-03-30 thomas if (err) {
1843 ce1bfad9 2024-03-30 thomas free(*proto);
1844 ce1bfad9 2024-03-30 thomas *proto = NULL;
1845 ce1bfad9 2024-03-30 thomas free(*host);
1846 ce1bfad9 2024-03-30 thomas *host = NULL;
1847 ce1bfad9 2024-03-30 thomas free(*port);
1848 ce1bfad9 2024-03-30 thomas *port = NULL;
1849 ce1bfad9 2024-03-30 thomas free(*request_path);
1850 ce1bfad9 2024-03-30 thomas *request_path = NULL;
1852 ce1bfad9 2024-03-30 thomas return err;
1855 ce1bfad9 2024-03-30 thomas static char *
1856 ce1bfad9 2024-03-30 thomas port_sprintf(int p)
1858 ce1bfad9 2024-03-30 thomas static char portno[32];
1861 ce1bfad9 2024-03-30 thomas n = snprintf(portno, sizeof(portno), "%lld", (long long)p);
1862 ce1bfad9 2024-03-30 thomas if (n < 0 || (size_t)n >= sizeof(portno))
1863 ce1bfad9 2024-03-30 thomas fatalx("port number too long: %lld", (long long)p);
1865 ce1bfad9 2024-03-30 thomas return portno;