Blame


1 3efd8e31 2022-10-23 thomas /*
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.
10 3efd8e31 2022-10-23 thomas *
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.
14 3efd8e31 2022-10-23 thomas *
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.
22 3efd8e31 2022-10-23 thomas */
23 3efd8e31 2022-10-23 thomas
24 3efd8e31 2022-10-23 thomas %{
25 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
26 4efc8dcb 2023-08-29 thomas
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>
31 3efd8e31 2022-10-23 thomas
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>
45 3efd8e31 2022-10-23 thomas
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"
49 3efd8e31 2022-10-23 thomas
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"
54 3efd8e31 2022-10-23 thomas
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;
62 3efd8e31 2022-10-23 thomas } *file;
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);
77 3efd8e31 2022-10-23 thomas
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;
81 3efd8e31 2022-10-23 thomas int used;
82 3efd8e31 2022-10-23 thomas int persist;
83 3efd8e31 2022-10-23 thomas char *nam;
84 3efd8e31 2022-10-23 thomas char *val;
85 3efd8e31 2022-10-23 thomas };
86 3efd8e31 2022-10-23 thomas
87 3efd8e31 2022-10-23 thomas int symset(const char *, const char *, int);
88 3efd8e31 2022-10-23 thomas char *symget(const char *);
89 3efd8e31 2022-10-23 thomas
90 3efd8e31 2022-10-23 thomas static int errors;
91 3efd8e31 2022-10-23 thomas
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 *,
101 6d7eb4f7 2023-04-04 thomas char *);
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 *,
105 6d7eb4f7 2023-04-04 thomas char *);
106 ce1bfad9 2024-03-30 thomas static int conf_notify_branch(struct gotd_repo *,
107 ce1bfad9 2024-03-30 thomas char *);
108 ce1bfad9 2024-03-30 thomas static int conf_notify_ref_namespace(struct gotd_repo *,
109 ce1bfad9 2024-03-30 thomas char *);
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;
115 3efd8e31 2022-10-23 thomas
116 3efd8e31 2022-10-23 thomas typedef struct {
117 3efd8e31 2022-10-23 thomas union {
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;
121 3efd8e31 2022-10-23 thomas } v;
122 3efd8e31 2022-10-23 thomas int lineno;
123 3efd8e31 2022-10-23 thomas } YYSTYPE;
124 3efd8e31 2022-10-23 thomas
125 3efd8e31 2022-10-23 thomas %}
126 3efd8e31 2022-10-23 thomas
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
131 3efd8e31 2022-10-23 thomas
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
135 3efd8e31 2022-10-23 thomas
136 3efd8e31 2022-10-23 thomas %%
137 3efd8e31 2022-10-23 thomas
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'
143 3efd8e31 2022-10-23 thomas ;
144 3efd8e31 2022-10-23 thomas
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);
153 07202819 2023-11-06 thomas YYERROR;
154 07202819 2023-11-06 thomas }
155 07202819 2023-11-06 thomas }
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);
160 07202819 2023-11-06 thomas }
161 07202819 2023-11-06 thomas ;
162 07202819 2023-11-06 thomas
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);
166 0781db0e 2023-01-06 thomas YYERROR;
167 0781db0e 2023-01-06 thomas }
168 0781db0e 2023-01-06 thomas $$.tv_sec = $1;
169 0781db0e 2023-01-06 thomas $$.tv_usec = 0;
170 0781db0e 2023-01-06 thomas }
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;
176 77d755e8 2023-01-06 thomas
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);
180 77d755e8 2023-01-06 thomas YYERROR;
181 77d755e8 2023-01-06 thomas }
182 77d755e8 2023-01-06 thomas
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';
188 77d755e8 2023-01-06 thomas break;
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';
194 77d755e8 2023-01-06 thomas break;
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';
200 77d755e8 2023-01-06 thomas break;
201 77d755e8 2023-01-06 thomas }
202 77d755e8 2023-01-06 thomas
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);
209 77d755e8 2023-01-06 thomas YYERROR;
210 77d755e8 2023-01-06 thomas }
211 77d755e8 2023-01-06 thomas
212 77d755e8 2023-01-06 thomas $$.tv_sec *= mul;
213 77d755e8 2023-01-06 thomas free($1);
214 77d755e8 2023-01-06 thomas }
215 0781db0e 2023-01-06 thomas ;
216 0781db0e 2023-01-06 thomas
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);
221 7348ded8 2023-01-19 thomas
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);
229 3efd8e31 2022-10-23 thomas YYERROR;
230 3efd8e31 2022-10-23 thomas }
231 3efd8e31 2022-10-23 thomas }
232 f9a4feb6 2023-01-06 thomas free($3);
233 3efd8e31 2022-10-23 thomas }
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);
240 3efd8e31 2022-10-23 thomas YYERROR;
241 3efd8e31 2022-10-23 thomas }
242 3efd8e31 2022-10-23 thomas free($2);
243 3efd8e31 2022-10-23 thomas }
244 0781db0e 2023-01-06 thomas | connection
245 3efd8e31 2022-10-23 thomas ;
246 3efd8e31 2022-10-23 thomas
247 0781db0e 2023-01-06 thomas connection : CONNECTION '{' optnl conflags_l '}'
248 0781db0e 2023-01-06 thomas | CONNECTION conflags
249 0781db0e 2023-01-06 thomas
250 0781db0e 2023-01-06 thomas conflags_l : conflags optnl conflags_l
251 0781db0e 2023-01-06 thomas | conflags optnl
252 0781db0e 2023-01-06 thomas ;
253 0781db0e 2023-01-06 thomas
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);
258 912db690 2023-01-06 thomas YYERROR;
259 912db690 2023-01-06 thomas }
260 0781db0e 2023-01-06 thomas memcpy(&gotd->request_timeout, &$3,
261 0781db0e 2023-01-06 thomas sizeof(gotd->request_timeout));
262 0781db0e 2023-01-06 thomas }
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);
267 0781db0e 2023-01-06 thomas YYERROR;
268 0781db0e 2023-01-06 thomas }
269 0781db0e 2023-01-06 thomas free($3);
270 0781db0e 2023-01-06 thomas }
271 0781db0e 2023-01-06 thomas ;
272 0781db0e 2023-01-06 thomas
273 6d7eb4f7 2023-04-04 thomas protect : PROTECT '{' optnl protectflags_l '}'
274 6d7eb4f7 2023-04-04 thomas | PROTECT protectflags
275 6d7eb4f7 2023-04-04 thomas
276 6d7eb4f7 2023-04-04 thomas protectflags_l : protectflags optnl protectflags_l
277 6d7eb4f7 2023-04-04 thomas | protectflags optnl
278 6d7eb4f7 2023-04-04 thomas ;
279 6d7eb4f7 2023-04-04 thomas
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);
285 6d7eb4f7 2023-04-04 thomas YYERROR;
286 6d7eb4f7 2023-04-04 thomas }
287 6d7eb4f7 2023-04-04 thomas }
288 ffc797f3 2023-04-05 thomas free($3);
289 6d7eb4f7 2023-04-04 thomas }
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,
294 6d7eb4f7 2023-04-04 thomas $3)) {
295 6d7eb4f7 2023-04-04 thomas free($3);
296 6d7eb4f7 2023-04-04 thomas YYERROR;
297 6d7eb4f7 2023-04-04 thomas }
298 6d7eb4f7 2023-04-04 thomas }
299 ffc797f3 2023-04-05 thomas free($3);
300 6d7eb4f7 2023-04-04 thomas }
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);
306 6d7eb4f7 2023-04-04 thomas YYERROR;
307 6d7eb4f7 2023-04-04 thomas }
308 6d7eb4f7 2023-04-04 thomas }
309 ffc797f3 2023-04-05 thomas free($2);
310 6d7eb4f7 2023-04-04 thomas }
311 6d7eb4f7 2023-04-04 thomas ;
312 6d7eb4f7 2023-04-04 thomas
313 ce1bfad9 2024-03-30 thomas notify : NOTIFY '{' optnl notifyflags_l '}'
314 ce1bfad9 2024-03-30 thomas | NOTIFY notifyflags
315 ce1bfad9 2024-03-30 thomas
316 ce1bfad9 2024-03-30 thomas notifyflags_l : notifyflags optnl notifyflags_l
317 ce1bfad9 2024-03-30 thomas | notifyflags optnl
318 ce1bfad9 2024-03-30 thomas ;
319 ce1bfad9 2024-03-30 thomas
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);
326 ce1bfad9 2024-03-30 thomas YYERROR;
327 ce1bfad9 2024-03-30 thomas }
328 ce1bfad9 2024-03-30 thomas }
329 0a89e570 2024-03-30 thomas free($2);
330 ce1bfad9 2024-03-30 thomas }
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);
337 ce1bfad9 2024-03-30 thomas YYERROR;
338 ce1bfad9 2024-03-30 thomas }
339 ce1bfad9 2024-03-30 thomas }
340 0a89e570 2024-03-30 thomas free($3);
341 ce1bfad9 2024-03-30 thomas }
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);
349 ce1bfad9 2024-03-30 thomas YYERROR;
350 ce1bfad9 2024-03-30 thomas }
351 ce1bfad9 2024-03-30 thomas }
352 0a89e570 2024-03-30 thomas free($3);
353 ce1bfad9 2024-03-30 thomas }
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);
362 ce1bfad9 2024-03-30 thomas YYERROR;
363 ce1bfad9 2024-03-30 thomas }
364 0a89e570 2024-03-30 thomas }
365 0a89e570 2024-03-30 thomas free($3);
366 0a89e570 2024-03-30 thomas free($5);
367 ce1bfad9 2024-03-30 thomas }
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);
376 ce1bfad9 2024-03-30 thomas YYERROR;
377 ce1bfad9 2024-03-30 thomas }
378 0a89e570 2024-03-30 thomas }
379 0a89e570 2024-03-30 thomas free($3);
380 0a89e570 2024-03-30 thomas free($6);
381 ce1bfad9 2024-03-30 thomas }
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);
391 ce1bfad9 2024-03-30 thomas YYERROR;
392 ce1bfad9 2024-03-30 thomas }
393 ce1bfad9 2024-03-30 thomas }
394 0a89e570 2024-03-30 thomas free($3);
395 0a89e570 2024-03-30 thomas free($5);
396 0a89e570 2024-03-30 thomas free($8);
397 ce1bfad9 2024-03-30 thomas }
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);
406 ce1bfad9 2024-03-30 thomas YYERROR;
407 ce1bfad9 2024-03-30 thomas }
408 ce1bfad9 2024-03-30 thomas }
409 0a89e570 2024-03-30 thomas free($3);
410 0a89e570 2024-03-30 thomas free($5);
411 ce1bfad9 2024-03-30 thomas }
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);
421 ce1bfad9 2024-03-30 thomas YYERROR;
422 ce1bfad9 2024-03-30 thomas }
423 ce1bfad9 2024-03-30 thomas }
424 0a89e570 2024-03-30 thomas free($3);
425 0a89e570 2024-03-30 thomas free($5);
426 0a89e570 2024-03-30 thomas free($7);
427 ce1bfad9 2024-03-30 thomas }
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);
437 ce1bfad9 2024-03-30 thomas YYERROR;
438 ce1bfad9 2024-03-30 thomas }
439 ce1bfad9 2024-03-30 thomas }
440 0a89e570 2024-03-30 thomas free($3);
441 0a89e570 2024-03-30 thomas free($6);
442 0a89e570 2024-03-30 thomas free($8);
443 ce1bfad9 2024-03-30 thomas }
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);
454 ce1bfad9 2024-03-30 thomas YYERROR;
455 ce1bfad9 2024-03-30 thomas }
456 ce1bfad9 2024-03-30 thomas }
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);
461 ce1bfad9 2024-03-30 thomas }
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);
471 ce1bfad9 2024-03-30 thomas YYERROR;
472 ce1bfad9 2024-03-30 thomas }
473 ce1bfad9 2024-03-30 thomas }
474 0a89e570 2024-03-30 thomas free($3);
475 0a89e570 2024-03-30 thomas free($5);
476 0a89e570 2024-03-30 thomas free($7);
477 ce1bfad9 2024-03-30 thomas }
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);
488 ce1bfad9 2024-03-30 thomas YYERROR;
489 ce1bfad9 2024-03-30 thomas }
490 ce1bfad9 2024-03-30 thomas }
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);
495 ce1bfad9 2024-03-30 thomas }
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);
506 ce1bfad9 2024-03-30 thomas YYERROR;
507 ce1bfad9 2024-03-30 thomas }
508 0a89e570 2024-03-30 thomas }
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);
513 ce1bfad9 2024-03-30 thomas }
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);
525 ce1bfad9 2024-03-30 thomas YYERROR;
526 ce1bfad9 2024-03-30 thomas }
527 ce1bfad9 2024-03-30 thomas }
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);
533 ce1bfad9 2024-03-30 thomas }
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);
542 ce1bfad9 2024-03-30 thomas YYERROR;
543 ce1bfad9 2024-03-30 thomas }
544 ce1bfad9 2024-03-30 thomas }
545 0a89e570 2024-03-30 thomas free($3);
546 0a89e570 2024-03-30 thomas free($5);
547 ce1bfad9 2024-03-30 thomas }
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);
557 ce1bfad9 2024-03-30 thomas YYERROR;
558 ce1bfad9 2024-03-30 thomas }
559 ce1bfad9 2024-03-30 thomas }
560 0a89e570 2024-03-30 thomas free($3);
561 0a89e570 2024-03-30 thomas free($5);
562 0a89e570 2024-03-30 thomas free($7);
563 ce1bfad9 2024-03-30 thomas }
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);
573 ce1bfad9 2024-03-30 thomas YYERROR;
574 ce1bfad9 2024-03-30 thomas }
575 ce1bfad9 2024-03-30 thomas }
576 0a89e570 2024-03-30 thomas free($3);
577 0a89e570 2024-03-30 thomas free($6);
578 0a89e570 2024-03-30 thomas free($8);
579 ce1bfad9 2024-03-30 thomas }
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);
590 ce1bfad9 2024-03-30 thomas YYERROR;
591 ce1bfad9 2024-03-30 thomas }
592 ce1bfad9 2024-03-30 thomas }
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);
597 ce1bfad9 2024-03-30 thomas }
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);
605 ce1bfad9 2024-03-30 thomas YYERROR;
606 ce1bfad9 2024-03-30 thomas }
607 ce1bfad9 2024-03-30 thomas }
608 0a89e570 2024-03-30 thomas free($2);
609 ce1bfad9 2024-03-30 thomas }
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;
619 2ae445d7 2024-04-25 thomas.ad }
620 2ae445d7 2024-04-25 thomas.ad }
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);
624 2ae445d7 2024-04-25 thomas.ad }
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);
633 ce1bfad9 2024-03-30 thomas YYERROR;
634 ce1bfad9 2024-03-30 thomas }
635 ce1bfad9 2024-03-30 thomas }
636 0a89e570 2024-03-30 thomas free($2);
637 0a89e570 2024-03-30 thomas free($4);
638 0a89e570 2024-03-30 thomas free($6);
639 ce1bfad9 2024-03-30 thomas }
640 ce1bfad9 2024-03-30 thomas ;
641 ce1bfad9 2024-03-30 thomas
642 3efd8e31 2022-10-23 thomas repository : REPOSITORY STRING {
643 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
644 3efd8e31 2022-10-23 thomas
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);
649 3efd8e31 2022-10-23 thomas YYERROR;
650 3efd8e31 2022-10-23 thomas }
651 3efd8e31 2022-10-23 thomas }
652 3efd8e31 2022-10-23 thomas
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);
660 3efd8e31 2022-10-23 thomas }
661 3efd8e31 2022-10-23 thomas free($2);
662 3efd8e31 2022-10-23 thomas } '{' optnl repoopts2 '}' {
663 3efd8e31 2022-10-23 thomas }
664 3efd8e31 2022-10-23 thomas ;
665 3efd8e31 2022-10-23 thomas
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);
677 3efd8e31 2022-10-23 thomas YYERROR;
678 3efd8e31 2022-10-23 thomas }
679 fe6a8988 2023-01-08 thomas if (realpath($2, new_repo->path) == NULL) {
680 d95864cd 2023-04-14 thomas /*
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.
684 f3807fe5 2023-07-10 thomas *
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.
689 d95864cd 2023-04-14 thomas */
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);
698 d95864cd 2023-04-14 thomas YYERROR;
699 f3807fe5 2023-07-10 thomas }
700 f3807fe5 2023-07-10 thomas
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");
705 3efd8e31 2022-10-23 thomas }
706 3efd8e31 2022-10-23 thomas }
707 3efd8e31 2022-10-23 thomas free($2);
708 729a7e24 2022-11-17 thomas }
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);
713 ffc797f3 2023-04-05 thomas } else
714 ffc797f3 2023-04-05 thomas free($3);
715 729a7e24 2022-11-17 thomas }
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);
721 ffc797f3 2023-04-05 thomas } else
722 ffc797f3 2023-04-05 thomas free($3);
723 3efd8e31 2022-10-23 thomas }
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);
728 ffc797f3 2023-04-05 thomas } else
729 ffc797f3 2023-04-05 thomas free($2);
730 729a7e24 2022-11-17 thomas }
731 6d7eb4f7 2023-04-04 thomas | protect
732 ce1bfad9 2024-03-30 thomas | notify
733 3efd8e31 2022-10-23 thomas ;
734 3efd8e31 2022-10-23 thomas
735 3efd8e31 2022-10-23 thomas repoopts2 : repoopts2 repoopts1 nl
736 3efd8e31 2022-10-23 thomas | repoopts1 optnl
737 3efd8e31 2022-10-23 thomas ;
738 3efd8e31 2022-10-23 thomas
739 3efd8e31 2022-10-23 thomas nl : '\n' optnl
740 3efd8e31 2022-10-23 thomas ;
741 3efd8e31 2022-10-23 thomas
742 3efd8e31 2022-10-23 thomas optnl : '\n' optnl /* zero or more newlines */
743 3efd8e31 2022-10-23 thomas | /* empty */
744 3efd8e31 2022-10-23 thomas ;
745 3efd8e31 2022-10-23 thomas
746 3efd8e31 2022-10-23 thomas %%
747 3efd8e31 2022-10-23 thomas
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;
751 3efd8e31 2022-10-23 thomas };
752 3efd8e31 2022-10-23 thomas
753 3efd8e31 2022-10-23 thomas int
754 3efd8e31 2022-10-23 thomas yyerror(const char *fmt, ...)
755 3efd8e31 2022-10-23 thomas {
756 3efd8e31 2022-10-23 thomas va_list ap;
757 3efd8e31 2022-10-23 thomas char *msg;
758 3efd8e31 2022-10-23 thomas
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);
767 3efd8e31 2022-10-23 thomas }
768 3efd8e31 2022-10-23 thomas
769 3efd8e31 2022-10-23 thomas int
770 3efd8e31 2022-10-23 thomas kw_cmp(const void *k, const void *e)
771 3efd8e31 2022-10-23 thomas {
772 3efd8e31 2022-10-23 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
773 3efd8e31 2022-10-23 thomas }
774 3efd8e31 2022-10-23 thomas
775 3efd8e31 2022-10-23 thomas int
776 3efd8e31 2022-10-23 thomas lookup(char *s)
777 3efd8e31 2022-10-23 thomas {
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 },
808 3efd8e31 2022-10-23 thomas };
809 3efd8e31 2022-10-23 thomas const struct keywords *p;
810 3efd8e31 2022-10-23 thomas
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);
813 3efd8e31 2022-10-23 thomas
814 3efd8e31 2022-10-23 thomas if (p)
815 3efd8e31 2022-10-23 thomas return (p->k_val);
816 3efd8e31 2022-10-23 thomas else
817 3efd8e31 2022-10-23 thomas return (STRING);
818 3efd8e31 2022-10-23 thomas }
819 3efd8e31 2022-10-23 thomas
820 3efd8e31 2022-10-23 thomas #define MAXPUSHBACK 128
821 3efd8e31 2022-10-23 thomas
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;
826 3efd8e31 2022-10-23 thomas
827 3efd8e31 2022-10-23 thomas int
828 3efd8e31 2022-10-23 thomas lgetc(int quotec)
829 3efd8e31 2022-10-23 thomas {
830 3efd8e31 2022-10-23 thomas int c, next;
831 3efd8e31 2022-10-23 thomas
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;
839 3efd8e31 2022-10-23 thomas } else
840 3efd8e31 2022-10-23 thomas parseindex++;
841 3efd8e31 2022-10-23 thomas }
842 3efd8e31 2022-10-23 thomas
843 3efd8e31 2022-10-23 thomas if (pushback_index)
844 3efd8e31 2022-10-23 thomas return (pushback_buffer[--pushback_index]);
845 3efd8e31 2022-10-23 thomas
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);
852 3efd8e31 2022-10-23 thomas }
853 3efd8e31 2022-10-23 thomas
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;
859 3efd8e31 2022-10-23 thomas break;
860 3efd8e31 2022-10-23 thomas }
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);
864 3efd8e31 2022-10-23 thomas }
865 3efd8e31 2022-10-23 thomas
866 3efd8e31 2022-10-23 thomas return (c);
867 3efd8e31 2022-10-23 thomas }
868 3efd8e31 2022-10-23 thomas
869 3efd8e31 2022-10-23 thomas int
870 3efd8e31 2022-10-23 thomas lungetc(int c)
871 3efd8e31 2022-10-23 thomas {
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);
878 3efd8e31 2022-10-23 thomas }
879 3efd8e31 2022-10-23 thomas if (pushback_index < MAXPUSHBACK-1)
880 3efd8e31 2022-10-23 thomas return (pushback_buffer[pushback_index++] = c);
881 3efd8e31 2022-10-23 thomas else
882 3efd8e31 2022-10-23 thomas return (EOF);
883 3efd8e31 2022-10-23 thomas }
884 3efd8e31 2022-10-23 thomas
885 3efd8e31 2022-10-23 thomas int
886 3efd8e31 2022-10-23 thomas findeol(void)
887 3efd8e31 2022-10-23 thomas {
888 3efd8e31 2022-10-23 thomas int c;
889 3efd8e31 2022-10-23 thomas
890 3efd8e31 2022-10-23 thomas parsebuf = NULL;
891 3efd8e31 2022-10-23 thomas
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];
896 3efd8e31 2022-10-23 thomas else
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++;
900 3efd8e31 2022-10-23 thomas break;
901 3efd8e31 2022-10-23 thomas }
902 3efd8e31 2022-10-23 thomas if (c == EOF)
903 3efd8e31 2022-10-23 thomas break;
904 3efd8e31 2022-10-23 thomas }
905 3efd8e31 2022-10-23 thomas return (ERROR);
906 3efd8e31 2022-10-23 thomas }
907 3efd8e31 2022-10-23 thomas
908 3efd8e31 2022-10-23 thomas int
909 3efd8e31 2022-10-23 thomas yylex(void)
910 3efd8e31 2022-10-23 thomas {
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;
915 3efd8e31 2022-10-23 thomas
916 3efd8e31 2022-10-23 thomas top:
917 3efd8e31 2022-10-23 thomas p = buf;
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 */
921 3efd8e31 2022-10-23 thomas
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 */
927 3efd8e31 2022-10-23 thomas }
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);
933 3efd8e31 2022-10-23 thomas
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());
937 3efd8e31 2022-10-23 thomas }
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;
941 3efd8e31 2022-10-23 thomas }
942 3efd8e31 2022-10-23 thomas *p = '\0';
943 3efd8e31 2022-10-23 thomas lungetc(c);
944 3efd8e31 2022-10-23 thomas break;
945 3efd8e31 2022-10-23 thomas }
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());
950 3efd8e31 2022-10-23 thomas }
951 3efd8e31 2022-10-23 thomas parsebuf = val;
952 3efd8e31 2022-10-23 thomas parseindex = 0;
953 3efd8e31 2022-10-23 thomas goto top;
954 3efd8e31 2022-10-23 thomas }
955 3efd8e31 2022-10-23 thomas
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;
976 3efd8e31 2022-10-23 thomas } else
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';
980 3efd8e31 2022-10-23 thomas break;
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());
984 3efd8e31 2022-10-23 thomas }
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());
988 3efd8e31 2022-10-23 thomas }
989 3efd8e31 2022-10-23 thomas *p++ = c;
990 3efd8e31 2022-10-23 thomas }
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);
995 3efd8e31 2022-10-23 thomas }
996 3efd8e31 2022-10-23 thomas
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 == '=')
999 3efd8e31 2022-10-23 thomas
1000 3efd8e31 2022-10-23 thomas if (c == '-' || isdigit(c)) {
1001 3efd8e31 2022-10-23 thomas do {
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());
1006 3efd8e31 2022-10-23 thomas }
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;
1014 3efd8e31 2022-10-23 thomas
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());
1022 3efd8e31 2022-10-23 thomas }
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);
1031 3efd8e31 2022-10-23 thomas }
1032 3efd8e31 2022-10-23 thomas }
1033 3efd8e31 2022-10-23 thomas
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 != ','))
1039 3efd8e31 2022-10-23 thomas
1040 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == ':' || c == '_') {
1041 3efd8e31 2022-10-23 thomas do {
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());
1046 3efd8e31 2022-10-23 thomas }
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");
1056 3efd8e31 2022-10-23 thomas }
1057 3efd8e31 2022-10-23 thomas return (token);
1058 3efd8e31 2022-10-23 thomas }
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++;
1062 3efd8e31 2022-10-23 thomas }
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);
1066 3efd8e31 2022-10-23 thomas }
1067 3efd8e31 2022-10-23 thomas
1068 3efd8e31 2022-10-23 thomas int
1069 3efd8e31 2022-10-23 thomas check_file_secrecy(int fd, const char *fname)
1070 3efd8e31 2022-10-23 thomas {
1071 3efd8e31 2022-10-23 thomas struct stat st;
1072 3efd8e31 2022-10-23 thomas
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);
1076 3efd8e31 2022-10-23 thomas }
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);
1080 3efd8e31 2022-10-23 thomas }
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);
1084 3efd8e31 2022-10-23 thomas }
1085 3efd8e31 2022-10-23 thomas return (0);
1086 3efd8e31 2022-10-23 thomas }
1087 3efd8e31 2022-10-23 thomas
1088 3efd8e31 2022-10-23 thomas struct file *
1089 7554713a 2023-04-01 thomas newfile(const char *name, int secret, int required)
1090 3efd8e31 2022-10-23 thomas {
1091 3efd8e31 2022-10-23 thomas struct file *nfile;
1092 3efd8e31 2022-10-23 thomas
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);
1097 3efd8e31 2022-10-23 thomas }
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);
1103 3efd8e31 2022-10-23 thomas }
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);
1117 3efd8e31 2022-10-23 thomas }
1118 3efd8e31 2022-10-23 thomas nfile->lineno = 1;
1119 3efd8e31 2022-10-23 thomas return (nfile);
1120 3efd8e31 2022-10-23 thomas }
1121 3efd8e31 2022-10-23 thomas
1122 3efd8e31 2022-10-23 thomas static void
1123 3efd8e31 2022-10-23 thomas closefile(struct file *xfile)
1124 3efd8e31 2022-10-23 thomas {
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);
1128 3efd8e31 2022-10-23 thomas }
1129 3efd8e31 2022-10-23 thomas
1130 3efd8e31 2022-10-23 thomas int
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)
1133 3efd8e31 2022-10-23 thomas {
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);
1137 3efd8e31 2022-10-23 thomas
1138 3efd8e31 2022-10-23 thomas memset(env, 0, sizeof(*env));
1139 3efd8e31 2022-10-23 thomas
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);
1143 3efd8e31 2022-10-23 thomas
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;
1149 3efd8e31 2022-10-23 thomas }
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;
1154 3efd8e31 2022-10-23 thomas }
1155 0781db0e 2023-01-06 thomas
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;
1158 3efd8e31 2022-10-23 thomas
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;
1162 3efd8e31 2022-10-23 thomas
1163 3efd8e31 2022-10-23 thomas yyparse();
1164 3efd8e31 2022-10-23 thomas errors = file->errors;
1165 3efd8e31 2022-10-23 thomas closefile(file);
1166 3efd8e31 2022-10-23 thomas
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);
1177 3efd8e31 2022-10-23 thomas }
1178 3efd8e31 2022-10-23 thomas }
1179 3efd8e31 2022-10-23 thomas
1180 3efd8e31 2022-10-23 thomas if (errors)
1181 3efd8e31 2022-10-23 thomas return (-1);
1182 3efd8e31 2022-10-23 thomas
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);
1188 88f1bb6d 2023-01-02 thomas }
1189 88f1bb6d 2023-01-02 thomas }
1190 88f1bb6d 2023-01-02 thomas
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);
1194 4d24b1fd 2023-01-19 thomas }
1195 4d24b1fd 2023-01-19 thomas
1196 3efd8e31 2022-10-23 thomas return (0);
1197 3efd8e31 2022-10-23 thomas }
1198 3efd8e31 2022-10-23 thomas
1199 0781db0e 2023-01-06 thomas static int
1200 0781db0e 2023-01-06 thomas uid_connection_limit_cmp(const void *pa, const void *pb)
1201 0781db0e 2023-01-06 thomas {
1202 0781db0e 2023-01-06 thomas const struct gotd_uid_connection_limit *a = pa, *b = pb;
1203 0781db0e 2023-01-06 thomas
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;
1208 0781db0e 2023-01-06 thomas
1209 0781db0e 2023-01-06 thomas return 0;
1210 0781db0e 2023-01-06 thomas }
1211 0781db0e 2023-01-06 thomas
1212 0781db0e 2023-01-06 thomas static int
1213 0781db0e 2023-01-06 thomas conf_limit_user_connections(const char *user, int maximum)
1214 0781db0e 2023-01-06 thomas {
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;
1218 0781db0e 2023-01-06 thomas
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;
1222 0781db0e 2023-01-06 thomas }
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;
1226 0781db0e 2023-01-06 thomas }
1227 0781db0e 2023-01-06 thomas
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;
1231 0781db0e 2023-01-06 thomas }
1232 0781db0e 2023-01-06 thomas
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;
1238 0781db0e 2023-01-06 thomas }
1239 0781db0e 2023-01-06 thomas
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");
1245 0781db0e 2023-01-06 thomas
1246 0781db0e 2023-01-06 thomas limit[nlimits - 1].uid = uid;
1247 0781db0e 2023-01-06 thomas limit[nlimits - 1].max_connections = maximum;
1248 0781db0e 2023-01-06 thomas
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);
1253 0781db0e 2023-01-06 thomas
1254 0781db0e 2023-01-06 thomas return 0;
1255 0781db0e 2023-01-06 thomas }
1256 0781db0e 2023-01-06 thomas
1257 3efd8e31 2022-10-23 thomas static struct gotd_repo *
1258 3efd8e31 2022-10-23 thomas conf_new_repo(const char *name)
1259 3efd8e31 2022-10-23 thomas {
1260 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
1261 3efd8e31 2022-10-23 thomas
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);
1265 a42b418b 2023-01-02 thomas }
1266 a42b418b 2023-01-02 thomas
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);
1269 3efd8e31 2022-10-23 thomas
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__);
1273 3efd8e31 2022-10-23 thomas
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);
1282 729a7e24 2022-11-17 thomas
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__);
1286 3efd8e31 2022-10-23 thomas
1287 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
1288 3efd8e31 2022-10-23 thomas gotd->nrepos++;
1289 3efd8e31 2022-10-23 thomas
1290 3efd8e31 2022-10-23 thomas return repo;
1291 3efd8e31 2022-10-23 thomas };
1292 729a7e24 2022-11-17 thomas
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)
1296 729a7e24 2022-11-17 thomas {
1297 729a7e24 2022-11-17 thomas struct gotd_access_rule *rule;
1298 729a7e24 2022-11-17 thomas
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");
1302 3efd8e31 2022-10-23 thomas
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;
1306 729a7e24 2022-11-17 thomas
1307 729a7e24 2022-11-17 thomas STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
1308 729a7e24 2022-11-17 thomas }
1309 729a7e24 2022-11-17 thomas
1310 6d7eb4f7 2023-04-04 thomas static int
1311 6d7eb4f7 2023-04-04 thomas refname_is_valid(char *refname)
1312 6d7eb4f7 2023-04-04 thomas {
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;
1317 6d7eb4f7 2023-04-04 thomas }
1318 6d7eb4f7 2023-04-04 thomas
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;
1322 6d7eb4f7 2023-04-04 thomas }
1323 6d7eb4f7 2023-04-04 thomas
1324 6d7eb4f7 2023-04-04 thomas return 1;
1325 6d7eb4f7 2023-04-04 thomas }
1326 6d7eb4f7 2023-04-04 thomas
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)
1330 6d7eb4f7 2023-04-04 thomas {
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;
1334 6d7eb4f7 2023-04-04 thomas
1335 57b6056a 2023-04-05 thomas *new = NULL;
1336 57b6056a 2023-04-05 thomas
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;
1343 6d7eb4f7 2023-04-04 thomas }
1344 6d7eb4f7 2023-04-04 thomas
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);
1350 a4435ef0 2023-04-05 thomas else
1351 3f0853b8 2023-04-07 thomas yyerror("duplicate protected namespace %s", namespace);
1352 6d7eb4f7 2023-04-04 thomas return -1;
1353 6d7eb4f7 2023-04-04 thomas }
1354 6d7eb4f7 2023-04-04 thomas
1355 57b6056a 2023-04-05 thomas *new = s;
1356 6d7eb4f7 2023-04-04 thomas return 0;
1357 6d7eb4f7 2023-04-04 thomas }
1358 6d7eb4f7 2023-04-04 thomas
1359 6d7eb4f7 2023-04-04 thomas static int
1360 6d7eb4f7 2023-04-04 thomas conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
1361 6d7eb4f7 2023-04-04 thomas {
1362 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
1363 57b6056a 2023-04-05 thomas char *new;
1364 57b6056a 2023-04-05 thomas
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;
1368 57b6056a 2023-04-05 thomas
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;
1373 57b6056a 2023-04-05 thomas }
1374 57b6056a 2023-04-05 thomas }
1375 57b6056a 2023-04-05 thomas
1376 57b6056a 2023-04-05 thomas return 0;
1377 6d7eb4f7 2023-04-04 thomas }
1378 6d7eb4f7 2023-04-04 thomas
1379 6d7eb4f7 2023-04-04 thomas static int
1380 6d7eb4f7 2023-04-04 thomas conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
1381 6d7eb4f7 2023-04-04 thomas {
1382 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
1383 57b6056a 2023-04-05 thomas char *new;
1384 57b6056a 2023-04-05 thomas
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;
1388 57b6056a 2023-04-05 thomas
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;
1393 57b6056a 2023-04-05 thomas }
1394 57b6056a 2023-04-05 thomas }
1395 57b6056a 2023-04-05 thomas
1396 57b6056a 2023-04-05 thomas return 0;
1397 6d7eb4f7 2023-04-04 thomas }
1398 6d7eb4f7 2023-04-04 thomas
1399 6d7eb4f7 2023-04-04 thomas static int
1400 6d7eb4f7 2023-04-04 thomas conf_protect_branch(struct gotd_repo *repo, char *branchname)
1401 6d7eb4f7 2023-04-04 thomas {
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;
1405 6d7eb4f7 2023-04-04 thomas
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;
1410 6d7eb4f7 2023-04-04 thomas }
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;
1416 6d7eb4f7 2023-04-04 thomas }
1417 6d7eb4f7 2023-04-04 thomas }
1418 6d7eb4f7 2023-04-04 thomas
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;
1422 6d7eb4f7 2023-04-04 thomas }
1423 6d7eb4f7 2023-04-04 thomas
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);
1430 a4435ef0 2023-04-05 thomas else
1431 a4435ef0 2023-04-05 thomas yyerror("duplicate protect branch %s", branchname);
1432 ce1bfad9 2024-03-30 thomas return -1;
1433 ce1bfad9 2024-03-30 thomas }
1434 ce1bfad9 2024-03-30 thomas
1435 ce1bfad9 2024-03-30 thomas return 0;
1436 ce1bfad9 2024-03-30 thomas }
1437 ce1bfad9 2024-03-30 thomas
1438 ce1bfad9 2024-03-30 thomas static int
1439 ce1bfad9 2024-03-30 thomas conf_notify_branch(struct gotd_repo *repo, char *branchname)
1440 ce1bfad9 2024-03-30 thomas {
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;
1444 ce1bfad9 2024-03-30 thomas
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;
1449 ce1bfad9 2024-03-30 thomas }
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;
1455 ce1bfad9 2024-03-30 thomas }
1456 ce1bfad9 2024-03-30 thomas }
1457 ce1bfad9 2024-03-30 thomas
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;
1461 6d7eb4f7 2023-04-04 thomas }
1462 6d7eb4f7 2023-04-04 thomas
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;
1469 ce1bfad9 2024-03-30 thomas }
1470 ce1bfad9 2024-03-30 thomas if (pe == NULL)
1471 ce1bfad9 2024-03-30 thomas free(refname);
1472 ce1bfad9 2024-03-30 thomas
1473 6d7eb4f7 2023-04-04 thomas return 0;
1474 6d7eb4f7 2023-04-04 thomas }
1475 6d7eb4f7 2023-04-04 thomas
1476 ce1bfad9 2024-03-30 thomas static int
1477 ce1bfad9 2024-03-30 thomas conf_notify_ref_namespace(struct gotd_repo *repo, char *namespace)
1478 ce1bfad9 2024-03-30 thomas {
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;
1482 ce1bfad9 2024-03-30 thomas
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;
1486 ce1bfad9 2024-03-30 thomas
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;
1490 ce1bfad9 2024-03-30 thomas }
1491 ce1bfad9 2024-03-30 thomas
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;
1498 ce1bfad9 2024-03-30 thomas }
1499 ce1bfad9 2024-03-30 thomas if (pe == NULL)
1500 ce1bfad9 2024-03-30 thomas free(s);
1501 ce1bfad9 2024-03-30 thomas
1502 ce1bfad9 2024-03-30 thomas return 0;
1503 ce1bfad9 2024-03-30 thomas }
1504 ce1bfad9 2024-03-30 thomas
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)
1508 ce1bfad9 2024-03-30 thomas {
1509 ce1bfad9 2024-03-30 thomas struct gotd_notification_target *target;
1510 ce1bfad9 2024-03-30 thomas
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;
1518 ce1bfad9 2024-03-30 thomas }
1519 ce1bfad9 2024-03-30 thomas }
1520 ce1bfad9 2024-03-30 thomas
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");
1529 ce1bfad9 2024-03-30 thomas }
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");
1537 ce1bfad9 2024-03-30 thomas }
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");
1542 ce1bfad9 2024-03-30 thomas }
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");
1547 ce1bfad9 2024-03-30 thomas }
1548 ce1bfad9 2024-03-30 thomas
1549 ce1bfad9 2024-03-30 thomas STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1550 ce1bfad9 2024-03-30 thomas return 0;
1551 ce1bfad9 2024-03-30 thomas }
1552 ce1bfad9 2024-03-30 thomas
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)
1556 ce1bfad9 2024-03-30 thomas {
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;
1561 ce1bfad9 2024-03-30 thomas
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;
1567 ce1bfad9 2024-03-30 thomas }
1568 ce1bfad9 2024-03-30 thomas
1569 94a3f4e9 2024-03-30 thomas tls = !strcmp(proto, "https");
1570 94a3f4e9 2024-03-30 thomas
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;
1576 b290a4cc 2024-04-25 thomas.ad }
1577 b290a4cc 2024-04-25 thomas.ad
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;
1587 b290a4cc 2024-04-25 thomas.ad }
1588 94a3f4e9 2024-03-30 thomas }
1589 94a3f4e9 2024-03-30 thomas
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;
1595 ce1bfad9 2024-03-30 thomas }
1596 ce1bfad9 2024-03-30 thomas
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;
1604 ce1bfad9 2024-03-30 thomas }
1605 ce1bfad9 2024-03-30 thomas
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;
1617 ce1bfad9 2024-03-30 thomas }
1618 ce1bfad9 2024-03-30 thomas }
1619 ce1bfad9 2024-03-30 thomas
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;
1629 94a3f4e9 2024-03-30 thomas
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");
1637 ce1bfad9 2024-03-30 thomas }
1638 ce1bfad9 2024-03-30 thomas
1639 ce1bfad9 2024-03-30 thomas STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1640 ce1bfad9 2024-03-30 thomas done:
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;
1646 ce1bfad9 2024-03-30 thomas }
1647 ce1bfad9 2024-03-30 thomas
1648 3efd8e31 2022-10-23 thomas int
1649 3efd8e31 2022-10-23 thomas symset(const char *nam, const char *val, int persist)
1650 3efd8e31 2022-10-23 thomas {
1651 3efd8e31 2022-10-23 thomas struct sym *sym;
1652 3efd8e31 2022-10-23 thomas
1653 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1654 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0)
1655 3efd8e31 2022-10-23 thomas break;
1656 3efd8e31 2022-10-23 thomas }
1657 3efd8e31 2022-10-23 thomas
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);
1661 3efd8e31 2022-10-23 thomas else {
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);
1666 3efd8e31 2022-10-23 thomas }
1667 3efd8e31 2022-10-23 thomas }
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);
1671 3efd8e31 2022-10-23 thomas
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);
1676 3efd8e31 2022-10-23 thomas }
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);
1682 3efd8e31 2022-10-23 thomas }
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);
1687 3efd8e31 2022-10-23 thomas }
1688 3efd8e31 2022-10-23 thomas
1689 3efd8e31 2022-10-23 thomas char *
1690 3efd8e31 2022-10-23 thomas symget(const char *nam)
1691 3efd8e31 2022-10-23 thomas {
1692 3efd8e31 2022-10-23 thomas struct sym *sym;
1693 3efd8e31 2022-10-23 thomas
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);
1698 3efd8e31 2022-10-23 thomas }
1699 3efd8e31 2022-10-23 thomas }
1700 3efd8e31 2022-10-23 thomas return (NULL);
1701 5dcb3a43 2023-04-01 thomas }
1702 5dcb3a43 2023-04-01 thomas
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)
1705 5dcb3a43 2023-04-01 thomas {
1706 5dcb3a43 2023-04-01 thomas struct gotd_repo *repo;
1707 5dcb3a43 2023-04-01 thomas size_t namelen;
1708 5dcb3a43 2023-04-01 thomas
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;
1716 5dcb3a43 2023-04-01 thomas }
1717 5dcb3a43 2023-04-01 thomas
1718 5dcb3a43 2023-04-01 thomas return NULL;
1719 3efd8e31 2022-10-23 thomas }
1720 6d7eb4f7 2023-04-04 thomas
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)
1723 6d7eb4f7 2023-04-04 thomas {
1724 6d7eb4f7 2023-04-04 thomas struct gotd_repo *repo;
1725 6d7eb4f7 2023-04-04 thomas
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;
1729 65a36f17 2023-04-22 thomas }
1730 65a36f17 2023-04-22 thomas
1731 65a36f17 2023-04-22 thomas return NULL;
1732 65a36f17 2023-04-22 thomas }
1733 65a36f17 2023-04-22 thomas
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)
1737 65a36f17 2023-04-22 thomas {
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;
1740 65a36f17 2023-04-22 thomas
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;
1747 65a36f17 2023-04-22 thomas else
1748 65a36f17 2023-04-22 thomas right = i - 1;
1749 6d7eb4f7 2023-04-04 thomas }
1750 6d7eb4f7 2023-04-04 thomas
1751 6d7eb4f7 2023-04-04 thomas return NULL;
1752 6d7eb4f7 2023-04-04 thomas }
1753 48488136 2023-04-22 thomas
1754 48488136 2023-04-22 thomas int
1755 48488136 2023-04-22 thomas gotd_parseuid(const char *s, uid_t *uid)
1756 48488136 2023-04-22 thomas {
1757 48488136 2023-04-22 thomas struct passwd *pw;
1758 48488136 2023-04-22 thomas const char *errstr;
1759 48488136 2023-04-22 thomas
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;
1765 48488136 2023-04-22 thomas }
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;
1770 48488136 2023-04-22 thomas }
1771 ce1bfad9 2024-03-30 thomas
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)
1775 ce1bfad9 2024-03-30 thomas {
1776 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
1777 ce1bfad9 2024-03-30 thomas char *s, *p, *q;
1778 ce1bfad9 2024-03-30 thomas
1779 ce1bfad9 2024-03-30 thomas *proto = *host = *port = *request_path = NULL;
1780 ce1bfad9 2024-03-30 thomas
1781 ce1bfad9 2024-03-30 thomas p = strstr(url, "://");
1782 ce1bfad9 2024-03-30 thomas if (!p)
1783 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PARSE_URI);
1784 ce1bfad9 2024-03-30 thomas
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;
1789 ce1bfad9 2024-03-30 thomas }
1790 ce1bfad9 2024-03-30 thomas s = p + 3;
1791 ce1bfad9 2024-03-30 thomas
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;
1796 ce1bfad9 2024-03-30 thomas }
1797 ce1bfad9 2024-03-30 thomas
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;
1804 ce1bfad9 2024-03-30 thomas }
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;
1808 ce1bfad9 2024-03-30 thomas }
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;
1813 ce1bfad9 2024-03-30 thomas }
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;
1817 ce1bfad9 2024-03-30 thomas }
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;
1823 ce1bfad9 2024-03-30 thomas }
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;
1827 ce1bfad9 2024-03-30 thomas }
1828 ce1bfad9 2024-03-30 thomas }
1829 ce1bfad9 2024-03-30 thomas
1830 ce1bfad9 2024-03-30 thomas while (p[0] == '/' && p[1] == '/')
1831 ce1bfad9 2024-03-30 thomas p++;
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;
1836 ce1bfad9 2024-03-30 thomas }
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;
1840 ce1bfad9 2024-03-30 thomas }
1841 ce1bfad9 2024-03-30 thomas 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;
1851 ce1bfad9 2024-03-30 thomas }
1852 ce1bfad9 2024-03-30 thomas return err;
1853 ce1bfad9 2024-03-30 thomas }
1854 ce1bfad9 2024-03-30 thomas
1855 ce1bfad9 2024-03-30 thomas static char *
1856 ce1bfad9 2024-03-30 thomas port_sprintf(int p)
1857 ce1bfad9 2024-03-30 thomas {
1858 ce1bfad9 2024-03-30 thomas static char portno[32];
1859 ce1bfad9 2024-03-30 thomas int n;
1860 ce1bfad9 2024-03-30 thomas
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);
1864 ce1bfad9 2024-03-30 thomas
1865 ce1bfad9 2024-03-30 thomas return portno;
1866 ce1bfad9 2024-03-30 thomas }