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 ce1bfad9 2024-03-30 thomas char *, char *, char *);
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 ce1bfad9 2024-03-30 thomas %token NOTIFY EMAIL FROM REPLY TO URL PASSWORD
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 912db690 2023-01-06 thomas yyerror("invalid timeout: %lld", $3.tv_sec);
257 912db690 2023-01-06 thomas YYERROR;
258 912db690 2023-01-06 thomas }
259 0781db0e 2023-01-06 thomas memcpy(&gotd->request_timeout, &$3,
260 0781db0e 2023-01-06 thomas sizeof(gotd->request_timeout));
261 0781db0e 2023-01-06 thomas }
262 0781db0e 2023-01-06 thomas | LIMIT USER STRING NUMBER {
263 0781db0e 2023-01-06 thomas if (gotd_proc_id == PROC_LISTEN &&
264 0781db0e 2023-01-06 thomas conf_limit_user_connections($3, $4) == -1) {
265 0781db0e 2023-01-06 thomas free($3);
266 0781db0e 2023-01-06 thomas YYERROR;
267 0781db0e 2023-01-06 thomas }
268 0781db0e 2023-01-06 thomas free($3);
269 0781db0e 2023-01-06 thomas }
270 0781db0e 2023-01-06 thomas ;
271 0781db0e 2023-01-06 thomas
272 6d7eb4f7 2023-04-04 thomas protect : PROTECT '{' optnl protectflags_l '}'
273 6d7eb4f7 2023-04-04 thomas | PROTECT protectflags
274 6d7eb4f7 2023-04-04 thomas
275 6d7eb4f7 2023-04-04 thomas protectflags_l : protectflags optnl protectflags_l
276 6d7eb4f7 2023-04-04 thomas | protectflags optnl
277 6d7eb4f7 2023-04-04 thomas ;
278 6d7eb4f7 2023-04-04 thomas
279 6d7eb4f7 2023-04-04 thomas protectflags : TAG NAMESPACE STRING {
280 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
281 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
282 6d7eb4f7 2023-04-04 thomas if (conf_protect_tag_namespace(new_repo, $3)) {
283 6d7eb4f7 2023-04-04 thomas free($3);
284 6d7eb4f7 2023-04-04 thomas YYERROR;
285 6d7eb4f7 2023-04-04 thomas }
286 6d7eb4f7 2023-04-04 thomas }
287 ffc797f3 2023-04-05 thomas free($3);
288 6d7eb4f7 2023-04-04 thomas }
289 6d7eb4f7 2023-04-04 thomas | BRANCH NAMESPACE STRING {
290 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
291 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
292 6d7eb4f7 2023-04-04 thomas if (conf_protect_branch_namespace(new_repo,
293 6d7eb4f7 2023-04-04 thomas $3)) {
294 6d7eb4f7 2023-04-04 thomas free($3);
295 6d7eb4f7 2023-04-04 thomas YYERROR;
296 6d7eb4f7 2023-04-04 thomas }
297 6d7eb4f7 2023-04-04 thomas }
298 ffc797f3 2023-04-05 thomas free($3);
299 6d7eb4f7 2023-04-04 thomas }
300 6d7eb4f7 2023-04-04 thomas | BRANCH STRING {
301 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
302 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
303 6d7eb4f7 2023-04-04 thomas if (conf_protect_branch(new_repo, $2)) {
304 6d7eb4f7 2023-04-04 thomas free($2);
305 6d7eb4f7 2023-04-04 thomas YYERROR;
306 6d7eb4f7 2023-04-04 thomas }
307 6d7eb4f7 2023-04-04 thomas }
308 ffc797f3 2023-04-05 thomas free($2);
309 6d7eb4f7 2023-04-04 thomas }
310 6d7eb4f7 2023-04-04 thomas ;
311 6d7eb4f7 2023-04-04 thomas
312 ce1bfad9 2024-03-30 thomas notify : NOTIFY '{' optnl notifyflags_l '}'
313 ce1bfad9 2024-03-30 thomas | NOTIFY notifyflags
314 ce1bfad9 2024-03-30 thomas
315 ce1bfad9 2024-03-30 thomas notifyflags_l : notifyflags optnl notifyflags_l
316 ce1bfad9 2024-03-30 thomas | notifyflags optnl
317 ce1bfad9 2024-03-30 thomas ;
318 ce1bfad9 2024-03-30 thomas
319 ce1bfad9 2024-03-30 thomas notifyflags : BRANCH STRING {
320 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
321 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
322 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
323 ce1bfad9 2024-03-30 thomas if (conf_notify_branch(new_repo, $2)) {
324 ce1bfad9 2024-03-30 thomas free($2);
325 ce1bfad9 2024-03-30 thomas YYERROR;
326 ce1bfad9 2024-03-30 thomas }
327 ce1bfad9 2024-03-30 thomas }
328 0a89e570 2024-03-30 thomas free($2);
329 ce1bfad9 2024-03-30 thomas }
330 ce1bfad9 2024-03-30 thomas | REFERENCE NAMESPACE STRING {
331 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
332 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
333 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
334 ce1bfad9 2024-03-30 thomas if (conf_notify_ref_namespace(new_repo, $3)) {
335 ce1bfad9 2024-03-30 thomas free($3);
336 ce1bfad9 2024-03-30 thomas YYERROR;
337 ce1bfad9 2024-03-30 thomas }
338 ce1bfad9 2024-03-30 thomas }
339 0a89e570 2024-03-30 thomas free($3);
340 ce1bfad9 2024-03-30 thomas }
341 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING {
342 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
343 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
344 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
345 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
346 ce1bfad9 2024-03-30 thomas NULL, NULL, NULL)) {
347 ce1bfad9 2024-03-30 thomas free($3);
348 ce1bfad9 2024-03-30 thomas YYERROR;
349 ce1bfad9 2024-03-30 thomas }
350 ce1bfad9 2024-03-30 thomas }
351 0a89e570 2024-03-30 thomas free($3);
352 ce1bfad9 2024-03-30 thomas }
353 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING {
354 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
355 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
356 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
357 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
358 ce1bfad9 2024-03-30 thomas NULL, NULL, NULL)) {
359 ce1bfad9 2024-03-30 thomas free($3);
360 ce1bfad9 2024-03-30 thomas free($5);
361 ce1bfad9 2024-03-30 thomas YYERROR;
362 ce1bfad9 2024-03-30 thomas }
363 0a89e570 2024-03-30 thomas }
364 0a89e570 2024-03-30 thomas free($3);
365 0a89e570 2024-03-30 thomas free($5);
366 ce1bfad9 2024-03-30 thomas }
367 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING REPLY TO STRING {
368 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
369 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
370 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
371 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
372 ce1bfad9 2024-03-30 thomas $6, NULL, NULL)) {
373 ce1bfad9 2024-03-30 thomas free($3);
374 ce1bfad9 2024-03-30 thomas free($6);
375 ce1bfad9 2024-03-30 thomas YYERROR;
376 ce1bfad9 2024-03-30 thomas }
377 0a89e570 2024-03-30 thomas }
378 0a89e570 2024-03-30 thomas free($3);
379 0a89e570 2024-03-30 thomas free($6);
380 ce1bfad9 2024-03-30 thomas }
381 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING REPLY TO STRING {
382 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
383 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
384 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
385 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
386 ce1bfad9 2024-03-30 thomas $8, NULL, NULL)) {
387 ce1bfad9 2024-03-30 thomas free($3);
388 ce1bfad9 2024-03-30 thomas free($5);
389 ce1bfad9 2024-03-30 thomas free($8);
390 ce1bfad9 2024-03-30 thomas YYERROR;
391 ce1bfad9 2024-03-30 thomas }
392 ce1bfad9 2024-03-30 thomas }
393 0a89e570 2024-03-30 thomas free($3);
394 0a89e570 2024-03-30 thomas free($5);
395 0a89e570 2024-03-30 thomas free($8);
396 ce1bfad9 2024-03-30 thomas }
397 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING RELAY STRING {
398 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
399 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
400 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
401 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
402 ce1bfad9 2024-03-30 thomas NULL, $5, NULL)) {
403 ce1bfad9 2024-03-30 thomas free($3);
404 ce1bfad9 2024-03-30 thomas free($5);
405 ce1bfad9 2024-03-30 thomas YYERROR;
406 ce1bfad9 2024-03-30 thomas }
407 ce1bfad9 2024-03-30 thomas }
408 0a89e570 2024-03-30 thomas free($3);
409 0a89e570 2024-03-30 thomas free($5);
410 ce1bfad9 2024-03-30 thomas }
411 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING RELAY STRING {
412 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
413 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
414 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
415 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
416 ce1bfad9 2024-03-30 thomas NULL, $7, NULL)) {
417 ce1bfad9 2024-03-30 thomas free($3);
418 ce1bfad9 2024-03-30 thomas free($5);
419 ce1bfad9 2024-03-30 thomas free($7);
420 ce1bfad9 2024-03-30 thomas YYERROR;
421 ce1bfad9 2024-03-30 thomas }
422 ce1bfad9 2024-03-30 thomas }
423 0a89e570 2024-03-30 thomas free($3);
424 0a89e570 2024-03-30 thomas free($5);
425 0a89e570 2024-03-30 thomas free($7);
426 ce1bfad9 2024-03-30 thomas }
427 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING REPLY TO STRING RELAY STRING {
428 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
429 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
430 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
431 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
432 ce1bfad9 2024-03-30 thomas $6, $8, NULL)) {
433 ce1bfad9 2024-03-30 thomas free($3);
434 ce1bfad9 2024-03-30 thomas free($6);
435 ce1bfad9 2024-03-30 thomas free($8);
436 ce1bfad9 2024-03-30 thomas YYERROR;
437 ce1bfad9 2024-03-30 thomas }
438 ce1bfad9 2024-03-30 thomas }
439 0a89e570 2024-03-30 thomas free($3);
440 0a89e570 2024-03-30 thomas free($6);
441 0a89e570 2024-03-30 thomas free($8);
442 ce1bfad9 2024-03-30 thomas }
443 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING {
444 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
445 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
446 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
447 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
448 ce1bfad9 2024-03-30 thomas $8, $10, NULL)) {
449 ce1bfad9 2024-03-30 thomas free($3);
450 ce1bfad9 2024-03-30 thomas free($5);
451 ce1bfad9 2024-03-30 thomas free($8);
452 ce1bfad9 2024-03-30 thomas free($10);
453 ce1bfad9 2024-03-30 thomas YYERROR;
454 ce1bfad9 2024-03-30 thomas }
455 ce1bfad9 2024-03-30 thomas }
456 0a89e570 2024-03-30 thomas free($3);
457 0a89e570 2024-03-30 thomas free($5);
458 0a89e570 2024-03-30 thomas free($8);
459 0a89e570 2024-03-30 thomas free($10);
460 ce1bfad9 2024-03-30 thomas }
461 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING RELAY STRING PORT STRING {
462 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
463 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
464 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
465 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
466 ce1bfad9 2024-03-30 thomas NULL, $5, $7)) {
467 ce1bfad9 2024-03-30 thomas free($3);
468 ce1bfad9 2024-03-30 thomas free($5);
469 ce1bfad9 2024-03-30 thomas free($7);
470 ce1bfad9 2024-03-30 thomas YYERROR;
471 ce1bfad9 2024-03-30 thomas }
472 ce1bfad9 2024-03-30 thomas }
473 0a89e570 2024-03-30 thomas free($3);
474 0a89e570 2024-03-30 thomas free($5);
475 0a89e570 2024-03-30 thomas free($7);
476 ce1bfad9 2024-03-30 thomas }
477 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING RELAY STRING PORT STRING {
478 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
479 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
480 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
481 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
482 ce1bfad9 2024-03-30 thomas NULL, $7, $9)) {
483 ce1bfad9 2024-03-30 thomas free($3);
484 ce1bfad9 2024-03-30 thomas free($5);
485 ce1bfad9 2024-03-30 thomas free($7);
486 ce1bfad9 2024-03-30 thomas free($9);
487 ce1bfad9 2024-03-30 thomas YYERROR;
488 ce1bfad9 2024-03-30 thomas }
489 ce1bfad9 2024-03-30 thomas }
490 0a89e570 2024-03-30 thomas free($3);
491 0a89e570 2024-03-30 thomas free($5);
492 0a89e570 2024-03-30 thomas free($7);
493 0a89e570 2024-03-30 thomas free($9);
494 ce1bfad9 2024-03-30 thomas }
495 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT STRING {
496 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
497 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
498 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
499 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
500 ce1bfad9 2024-03-30 thomas $6, $8, $10)) {
501 ce1bfad9 2024-03-30 thomas free($3);
502 ce1bfad9 2024-03-30 thomas free($6);
503 ce1bfad9 2024-03-30 thomas free($8);
504 ce1bfad9 2024-03-30 thomas free($10);
505 ce1bfad9 2024-03-30 thomas YYERROR;
506 ce1bfad9 2024-03-30 thomas }
507 0a89e570 2024-03-30 thomas }
508 0a89e570 2024-03-30 thomas free($3);
509 0a89e570 2024-03-30 thomas free($6);
510 0a89e570 2024-03-30 thomas free($8);
511 0a89e570 2024-03-30 thomas free($10);
512 ce1bfad9 2024-03-30 thomas }
513 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT STRING {
514 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
515 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
516 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
517 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
518 ce1bfad9 2024-03-30 thomas $8, $10, $12)) {
519 ce1bfad9 2024-03-30 thomas free($3);
520 ce1bfad9 2024-03-30 thomas free($5);
521 ce1bfad9 2024-03-30 thomas free($8);
522 ce1bfad9 2024-03-30 thomas free($10);
523 ce1bfad9 2024-03-30 thomas free($12);
524 ce1bfad9 2024-03-30 thomas YYERROR;
525 ce1bfad9 2024-03-30 thomas }
526 ce1bfad9 2024-03-30 thomas }
527 0a89e570 2024-03-30 thomas free($3);
528 0a89e570 2024-03-30 thomas free($5);
529 0a89e570 2024-03-30 thomas free($8);
530 0a89e570 2024-03-30 thomas free($10);
531 0a89e570 2024-03-30 thomas free($12);
532 ce1bfad9 2024-03-30 thomas }
533 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING RELAY STRING PORT NUMBER {
534 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
535 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
536 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
537 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
538 ce1bfad9 2024-03-30 thomas NULL, $5, port_sprintf($7))) {
539 ce1bfad9 2024-03-30 thomas free($3);
540 ce1bfad9 2024-03-30 thomas free($5);
541 ce1bfad9 2024-03-30 thomas YYERROR;
542 ce1bfad9 2024-03-30 thomas }
543 ce1bfad9 2024-03-30 thomas }
544 0a89e570 2024-03-30 thomas free($3);
545 0a89e570 2024-03-30 thomas free($5);
546 ce1bfad9 2024-03-30 thomas }
547 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING RELAY STRING PORT NUMBER {
548 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
549 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
550 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
551 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
552 ce1bfad9 2024-03-30 thomas NULL, $7, port_sprintf($9))) {
553 ce1bfad9 2024-03-30 thomas free($3);
554 ce1bfad9 2024-03-30 thomas free($5);
555 ce1bfad9 2024-03-30 thomas free($7);
556 ce1bfad9 2024-03-30 thomas YYERROR;
557 ce1bfad9 2024-03-30 thomas }
558 ce1bfad9 2024-03-30 thomas }
559 0a89e570 2024-03-30 thomas free($3);
560 0a89e570 2024-03-30 thomas free($5);
561 0a89e570 2024-03-30 thomas free($7);
562 ce1bfad9 2024-03-30 thomas }
563 ce1bfad9 2024-03-30 thomas | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
564 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
565 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
566 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
567 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, NULL, $3,
568 ce1bfad9 2024-03-30 thomas $6, $8, port_sprintf($10))) {
569 ce1bfad9 2024-03-30 thomas free($3);
570 ce1bfad9 2024-03-30 thomas free($6);
571 ce1bfad9 2024-03-30 thomas free($8);
572 ce1bfad9 2024-03-30 thomas YYERROR;
573 ce1bfad9 2024-03-30 thomas }
574 ce1bfad9 2024-03-30 thomas }
575 0a89e570 2024-03-30 thomas free($3);
576 0a89e570 2024-03-30 thomas free($6);
577 0a89e570 2024-03-30 thomas free($8);
578 ce1bfad9 2024-03-30 thomas }
579 ce1bfad9 2024-03-30 thomas | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
580 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
581 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
582 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
583 ce1bfad9 2024-03-30 thomas if (conf_notify_email(new_repo, $3, $5,
584 ce1bfad9 2024-03-30 thomas $8, $10, port_sprintf($12))) {
585 ce1bfad9 2024-03-30 thomas free($3);
586 ce1bfad9 2024-03-30 thomas free($5);
587 ce1bfad9 2024-03-30 thomas free($8);
588 ce1bfad9 2024-03-30 thomas free($10);
589 ce1bfad9 2024-03-30 thomas YYERROR;
590 ce1bfad9 2024-03-30 thomas }
591 ce1bfad9 2024-03-30 thomas }
592 0a89e570 2024-03-30 thomas free($3);
593 0a89e570 2024-03-30 thomas free($5);
594 0a89e570 2024-03-30 thomas free($8);
595 0a89e570 2024-03-30 thomas free($10);
596 ce1bfad9 2024-03-30 thomas }
597 ce1bfad9 2024-03-30 thomas | URL STRING {
598 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
599 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
600 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
601 ce1bfad9 2024-03-30 thomas if (conf_notify_http(new_repo, $2, NULL,
602 ce1bfad9 2024-03-30 thomas NULL)) {
603 ce1bfad9 2024-03-30 thomas free($2);
604 ce1bfad9 2024-03-30 thomas YYERROR;
605 ce1bfad9 2024-03-30 thomas }
606 ce1bfad9 2024-03-30 thomas }
607 0a89e570 2024-03-30 thomas free($2);
608 ce1bfad9 2024-03-30 thomas }
609 ce1bfad9 2024-03-30 thomas | URL STRING USER STRING PASSWORD STRING {
610 ce1bfad9 2024-03-30 thomas if (gotd_proc_id == PROC_GOTD ||
611 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
612 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
613 ce1bfad9 2024-03-30 thomas if (conf_notify_http(new_repo, $2, $4, $6)) {
614 ce1bfad9 2024-03-30 thomas free($2);
615 ce1bfad9 2024-03-30 thomas free($4);
616 ce1bfad9 2024-03-30 thomas free($6);
617 ce1bfad9 2024-03-30 thomas YYERROR;
618 ce1bfad9 2024-03-30 thomas }
619 ce1bfad9 2024-03-30 thomas }
620 0a89e570 2024-03-30 thomas free($2);
621 0a89e570 2024-03-30 thomas free($4);
622 0a89e570 2024-03-30 thomas free($6);
623 ce1bfad9 2024-03-30 thomas }
624 ce1bfad9 2024-03-30 thomas ;
625 ce1bfad9 2024-03-30 thomas
626 3efd8e31 2022-10-23 thomas repository : REPOSITORY STRING {
627 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
628 3efd8e31 2022-10-23 thomas
629 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
630 3efd8e31 2022-10-23 thomas if (strcmp(repo->name, $2) == 0) {
631 3efd8e31 2022-10-23 thomas yyerror("duplicate repository '%s'", $2);
632 3efd8e31 2022-10-23 thomas free($2);
633 3efd8e31 2022-10-23 thomas YYERROR;
634 3efd8e31 2022-10-23 thomas }
635 3efd8e31 2022-10-23 thomas }
636 3efd8e31 2022-10-23 thomas
637 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
638 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_AUTH ||
639 f3807fe5 2023-07-10 thomas gotd_proc_id == PROC_REPO_WRITE ||
640 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
641 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_GITWRAPPER |
642 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
643 3efd8e31 2022-10-23 thomas new_repo = conf_new_repo($2);
644 3efd8e31 2022-10-23 thomas }
645 3efd8e31 2022-10-23 thomas free($2);
646 3efd8e31 2022-10-23 thomas } '{' optnl repoopts2 '}' {
647 3efd8e31 2022-10-23 thomas }
648 3efd8e31 2022-10-23 thomas ;
649 3efd8e31 2022-10-23 thomas
650 3efd8e31 2022-10-23 thomas repoopts1 : PATH STRING {
651 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
652 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_AUTH ||
653 f3807fe5 2023-07-10 thomas gotd_proc_id == PROC_REPO_WRITE ||
654 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_SESSION_WRITE ||
655 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_GITWRAPPER ||
656 ce1bfad9 2024-03-30 thomas gotd_proc_id == PROC_NOTIFY) {
657 3efd8e31 2022-10-23 thomas if (!got_path_is_absolute($2)) {
658 3efd8e31 2022-10-23 thomas yyerror("%s: path %s is not absolute",
659 3efd8e31 2022-10-23 thomas __func__, $2);
660 3efd8e31 2022-10-23 thomas free($2);
661 3efd8e31 2022-10-23 thomas YYERROR;
662 3efd8e31 2022-10-23 thomas }
663 fe6a8988 2023-01-08 thomas if (realpath($2, new_repo->path) == NULL) {
664 d95864cd 2023-04-14 thomas /*
665 f3807fe5 2023-07-10 thomas * To give admins a chance to create
666 f3807fe5 2023-07-10 thomas * missing repositories at run-time
667 f3807fe5 2023-07-10 thomas * we only warn about ENOENT here.
668 f3807fe5 2023-07-10 thomas *
669 f3807fe5 2023-07-10 thomas * And ignore 'permission denied' when
670 f3807fe5 2023-07-10 thomas * running in gitwrapper. Users may be
671 f3807fe5 2023-07-10 thomas * able to access this repository via
672 f3807fe5 2023-07-10 thomas * gotd regardless.
673 d95864cd 2023-04-14 thomas */
674 f3807fe5 2023-07-10 thomas if (errno == ENOENT) {
675 f3807fe5 2023-07-10 thomas yyerror("realpath %s: %s", $2,
676 f3807fe5 2023-07-10 thomas strerror(errno));
677 f3807fe5 2023-07-10 thomas } else if (errno != EACCES ||
678 f3807fe5 2023-07-10 thomas gotd_proc_id != PROC_GITWRAPPER) {
679 f3807fe5 2023-07-10 thomas yyerror("realpath %s: %s", $2,
680 f3807fe5 2023-07-10 thomas strerror(errno));
681 d95864cd 2023-04-14 thomas free($2);
682 d95864cd 2023-04-14 thomas YYERROR;
683 f3807fe5 2023-07-10 thomas }
684 f3807fe5 2023-07-10 thomas
685 f3807fe5 2023-07-10 thomas if (strlcpy(new_repo->path, $2,
686 d95864cd 2023-04-14 thomas sizeof(new_repo->path)) >=
687 d95864cd 2023-04-14 thomas sizeof(new_repo->path))
688 d95864cd 2023-04-14 thomas yyerror("path too long");
689 3efd8e31 2022-10-23 thomas }
690 3efd8e31 2022-10-23 thomas }
691 3efd8e31 2022-10-23 thomas free($2);
692 729a7e24 2022-11-17 thomas }
693 729a7e24 2022-11-17 thomas | PERMIT RO STRING {
694 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
695 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
696 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
697 ffc797f3 2023-04-05 thomas } else
698 ffc797f3 2023-04-05 thomas free($3);
699 729a7e24 2022-11-17 thomas }
700 729a7e24 2022-11-17 thomas | PERMIT RW STRING {
701 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
702 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
703 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED,
704 729a7e24 2022-11-17 thomas GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
705 ffc797f3 2023-04-05 thomas } else
706 ffc797f3 2023-04-05 thomas free($3);
707 3efd8e31 2022-10-23 thomas }
708 729a7e24 2022-11-17 thomas | DENY STRING {
709 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
710 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
711 729a7e24 2022-11-17 thomas GOTD_ACCESS_DENIED, 0, $2);
712 ffc797f3 2023-04-05 thomas } else
713 ffc797f3 2023-04-05 thomas free($2);
714 729a7e24 2022-11-17 thomas }
715 6d7eb4f7 2023-04-04 thomas | protect
716 ce1bfad9 2024-03-30 thomas | notify
717 3efd8e31 2022-10-23 thomas ;
718 3efd8e31 2022-10-23 thomas
719 3efd8e31 2022-10-23 thomas repoopts2 : repoopts2 repoopts1 nl
720 3efd8e31 2022-10-23 thomas | repoopts1 optnl
721 3efd8e31 2022-10-23 thomas ;
722 3efd8e31 2022-10-23 thomas
723 3efd8e31 2022-10-23 thomas nl : '\n' optnl
724 3efd8e31 2022-10-23 thomas ;
725 3efd8e31 2022-10-23 thomas
726 3efd8e31 2022-10-23 thomas optnl : '\n' optnl /* zero or more newlines */
727 3efd8e31 2022-10-23 thomas | /* empty */
728 3efd8e31 2022-10-23 thomas ;
729 3efd8e31 2022-10-23 thomas
730 3efd8e31 2022-10-23 thomas %%
731 3efd8e31 2022-10-23 thomas
732 3efd8e31 2022-10-23 thomas struct keywords {
733 3efd8e31 2022-10-23 thomas const char *k_name;
734 3efd8e31 2022-10-23 thomas int k_val;
735 3efd8e31 2022-10-23 thomas };
736 3efd8e31 2022-10-23 thomas
737 3efd8e31 2022-10-23 thomas int
738 3efd8e31 2022-10-23 thomas yyerror(const char *fmt, ...)
739 3efd8e31 2022-10-23 thomas {
740 3efd8e31 2022-10-23 thomas va_list ap;
741 3efd8e31 2022-10-23 thomas char *msg;
742 3efd8e31 2022-10-23 thomas
743 3efd8e31 2022-10-23 thomas file->errors++;
744 3efd8e31 2022-10-23 thomas va_start(ap, fmt);
745 3efd8e31 2022-10-23 thomas if (vasprintf(&msg, fmt, ap) == -1)
746 3efd8e31 2022-10-23 thomas fatalx("yyerror vasprintf");
747 3efd8e31 2022-10-23 thomas va_end(ap);
748 3efd8e31 2022-10-23 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
749 3efd8e31 2022-10-23 thomas free(msg);
750 3efd8e31 2022-10-23 thomas return (0);
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 kw_cmp(const void *k, const void *e)
755 3efd8e31 2022-10-23 thomas {
756 3efd8e31 2022-10-23 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
757 3efd8e31 2022-10-23 thomas }
758 3efd8e31 2022-10-23 thomas
759 3efd8e31 2022-10-23 thomas int
760 3efd8e31 2022-10-23 thomas lookup(char *s)
761 3efd8e31 2022-10-23 thomas {
762 3efd8e31 2022-10-23 thomas /* This has to be sorted always. */
763 3efd8e31 2022-10-23 thomas static const struct keywords keywords[] = {
764 6d7eb4f7 2023-04-04 thomas { "branch", BRANCH },
765 0781db0e 2023-01-06 thomas { "connection", CONNECTION },
766 729a7e24 2022-11-17 thomas { "deny", DENY },
767 ce1bfad9 2024-03-30 thomas { "email", EMAIL },
768 ce1bfad9 2024-03-30 thomas { "from", FROM },
769 0781db0e 2023-01-06 thomas { "limit", LIMIT },
770 f9a4feb6 2023-01-06 thomas { "listen", LISTEN },
771 6d7eb4f7 2023-04-04 thomas { "namespace", NAMESPACE },
772 ce1bfad9 2024-03-30 thomas { "notify", NOTIFY },
773 3efd8e31 2022-10-23 thomas { "on", ON },
774 ce1bfad9 2024-03-30 thomas { "password", PASSWORD },
775 3efd8e31 2022-10-23 thomas { "path", PATH },
776 729a7e24 2022-11-17 thomas { "permit", PERMIT },
777 ce1bfad9 2024-03-30 thomas { "port", PORT },
778 6d7eb4f7 2023-04-04 thomas { "protect", PROTECT },
779 ce1bfad9 2024-03-30 thomas { "reference", REFERENCE },
780 ce1bfad9 2024-03-30 thomas { "relay", RELAY },
781 ce1bfad9 2024-03-30 thomas { "reply", REPLY },
782 3efd8e31 2022-10-23 thomas { "repository", REPOSITORY },
783 0781db0e 2023-01-06 thomas { "request", REQUEST },
784 729a7e24 2022-11-17 thomas { "ro", RO },
785 729a7e24 2022-11-17 thomas { "rw", RW },
786 6d7eb4f7 2023-04-04 thomas { "tag", TAG },
787 0781db0e 2023-01-06 thomas { "timeout", TIMEOUT },
788 ce1bfad9 2024-03-30 thomas { "to", TO },
789 ce1bfad9 2024-03-30 thomas { "url", URL },
790 3efd8e31 2022-10-23 thomas { "user", USER },
791 3efd8e31 2022-10-23 thomas };
792 3efd8e31 2022-10-23 thomas const struct keywords *p;
793 3efd8e31 2022-10-23 thomas
794 3efd8e31 2022-10-23 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
795 3efd8e31 2022-10-23 thomas sizeof(keywords[0]), kw_cmp);
796 3efd8e31 2022-10-23 thomas
797 3efd8e31 2022-10-23 thomas if (p)
798 3efd8e31 2022-10-23 thomas return (p->k_val);
799 3efd8e31 2022-10-23 thomas else
800 3efd8e31 2022-10-23 thomas return (STRING);
801 3efd8e31 2022-10-23 thomas }
802 3efd8e31 2022-10-23 thomas
803 3efd8e31 2022-10-23 thomas #define MAXPUSHBACK 128
804 3efd8e31 2022-10-23 thomas
805 3efd8e31 2022-10-23 thomas unsigned char *parsebuf;
806 3efd8e31 2022-10-23 thomas int parseindex;
807 3efd8e31 2022-10-23 thomas unsigned char pushback_buffer[MAXPUSHBACK];
808 3efd8e31 2022-10-23 thomas int pushback_index = 0;
809 3efd8e31 2022-10-23 thomas
810 3efd8e31 2022-10-23 thomas int
811 3efd8e31 2022-10-23 thomas lgetc(int quotec)
812 3efd8e31 2022-10-23 thomas {
813 3efd8e31 2022-10-23 thomas int c, next;
814 3efd8e31 2022-10-23 thomas
815 3efd8e31 2022-10-23 thomas if (parsebuf) {
816 3efd8e31 2022-10-23 thomas /* Read character from the parsebuffer instead of input. */
817 3efd8e31 2022-10-23 thomas if (parseindex >= 0) {
818 3efd8e31 2022-10-23 thomas c = parsebuf[parseindex++];
819 3efd8e31 2022-10-23 thomas if (c != '\0')
820 3efd8e31 2022-10-23 thomas return (c);
821 3efd8e31 2022-10-23 thomas parsebuf = NULL;
822 3efd8e31 2022-10-23 thomas } else
823 3efd8e31 2022-10-23 thomas parseindex++;
824 3efd8e31 2022-10-23 thomas }
825 3efd8e31 2022-10-23 thomas
826 3efd8e31 2022-10-23 thomas if (pushback_index)
827 3efd8e31 2022-10-23 thomas return (pushback_buffer[--pushback_index]);
828 3efd8e31 2022-10-23 thomas
829 3efd8e31 2022-10-23 thomas if (quotec) {
830 3efd8e31 2022-10-23 thomas c = getc(file->stream);
831 3efd8e31 2022-10-23 thomas if (c == EOF)
832 3efd8e31 2022-10-23 thomas yyerror("reached end of file while parsing "
833 3efd8e31 2022-10-23 thomas "quoted string");
834 3efd8e31 2022-10-23 thomas return (c);
835 3efd8e31 2022-10-23 thomas }
836 3efd8e31 2022-10-23 thomas
837 3efd8e31 2022-10-23 thomas c = getc(file->stream);
838 3efd8e31 2022-10-23 thomas while (c == '\\') {
839 3efd8e31 2022-10-23 thomas next = getc(file->stream);
840 3efd8e31 2022-10-23 thomas if (next != '\n') {
841 3efd8e31 2022-10-23 thomas c = next;
842 3efd8e31 2022-10-23 thomas break;
843 3efd8e31 2022-10-23 thomas }
844 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
845 3efd8e31 2022-10-23 thomas file->lineno++;
846 3efd8e31 2022-10-23 thomas c = getc(file->stream);
847 3efd8e31 2022-10-23 thomas }
848 3efd8e31 2022-10-23 thomas
849 3efd8e31 2022-10-23 thomas return (c);
850 3efd8e31 2022-10-23 thomas }
851 3efd8e31 2022-10-23 thomas
852 3efd8e31 2022-10-23 thomas int
853 3efd8e31 2022-10-23 thomas lungetc(int c)
854 3efd8e31 2022-10-23 thomas {
855 3efd8e31 2022-10-23 thomas if (c == EOF)
856 3efd8e31 2022-10-23 thomas return (EOF);
857 3efd8e31 2022-10-23 thomas if (parsebuf) {
858 3efd8e31 2022-10-23 thomas parseindex--;
859 3efd8e31 2022-10-23 thomas if (parseindex >= 0)
860 3efd8e31 2022-10-23 thomas return (c);
861 3efd8e31 2022-10-23 thomas }
862 3efd8e31 2022-10-23 thomas if (pushback_index < MAXPUSHBACK-1)
863 3efd8e31 2022-10-23 thomas return (pushback_buffer[pushback_index++] = c);
864 3efd8e31 2022-10-23 thomas else
865 3efd8e31 2022-10-23 thomas return (EOF);
866 3efd8e31 2022-10-23 thomas }
867 3efd8e31 2022-10-23 thomas
868 3efd8e31 2022-10-23 thomas int
869 3efd8e31 2022-10-23 thomas findeol(void)
870 3efd8e31 2022-10-23 thomas {
871 3efd8e31 2022-10-23 thomas int c;
872 3efd8e31 2022-10-23 thomas
873 3efd8e31 2022-10-23 thomas parsebuf = NULL;
874 3efd8e31 2022-10-23 thomas
875 3efd8e31 2022-10-23 thomas /* Skip to either EOF or the first real EOL. */
876 3efd8e31 2022-10-23 thomas while (1) {
877 3efd8e31 2022-10-23 thomas if (pushback_index)
878 3efd8e31 2022-10-23 thomas c = pushback_buffer[--pushback_index];
879 3efd8e31 2022-10-23 thomas else
880 3efd8e31 2022-10-23 thomas c = lgetc(0);
881 3efd8e31 2022-10-23 thomas if (c == '\n') {
882 3efd8e31 2022-10-23 thomas file->lineno++;
883 3efd8e31 2022-10-23 thomas break;
884 3efd8e31 2022-10-23 thomas }
885 3efd8e31 2022-10-23 thomas if (c == EOF)
886 3efd8e31 2022-10-23 thomas break;
887 3efd8e31 2022-10-23 thomas }
888 3efd8e31 2022-10-23 thomas return (ERROR);
889 3efd8e31 2022-10-23 thomas }
890 3efd8e31 2022-10-23 thomas
891 3efd8e31 2022-10-23 thomas int
892 3efd8e31 2022-10-23 thomas yylex(void)
893 3efd8e31 2022-10-23 thomas {
894 3efd8e31 2022-10-23 thomas unsigned char buf[8096];
895 3efd8e31 2022-10-23 thomas unsigned char *p, *val;
896 3efd8e31 2022-10-23 thomas int quotec, next, c;
897 3efd8e31 2022-10-23 thomas int token;
898 3efd8e31 2022-10-23 thomas
899 3efd8e31 2022-10-23 thomas top:
900 3efd8e31 2022-10-23 thomas p = buf;
901 3efd8e31 2022-10-23 thomas c = lgetc(0);
902 3efd8e31 2022-10-23 thomas while (c == ' ' || c == '\t')
903 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
904 3efd8e31 2022-10-23 thomas
905 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
906 3efd8e31 2022-10-23 thomas if (c == '#') {
907 3efd8e31 2022-10-23 thomas c = lgetc(0);
908 3efd8e31 2022-10-23 thomas while (c != '\n' && c != EOF)
909 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
910 3efd8e31 2022-10-23 thomas }
911 3efd8e31 2022-10-23 thomas if (c == '$' && parsebuf == NULL) {
912 3efd8e31 2022-10-23 thomas while (1) {
913 3efd8e31 2022-10-23 thomas c = lgetc(0);
914 3efd8e31 2022-10-23 thomas if (c == EOF)
915 3efd8e31 2022-10-23 thomas return (0);
916 3efd8e31 2022-10-23 thomas
917 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
918 3efd8e31 2022-10-23 thomas yyerror("string too long");
919 3efd8e31 2022-10-23 thomas return (findeol());
920 3efd8e31 2022-10-23 thomas }
921 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == '_') {
922 3efd8e31 2022-10-23 thomas *p++ = c;
923 3efd8e31 2022-10-23 thomas continue;
924 3efd8e31 2022-10-23 thomas }
925 3efd8e31 2022-10-23 thomas *p = '\0';
926 3efd8e31 2022-10-23 thomas lungetc(c);
927 3efd8e31 2022-10-23 thomas break;
928 3efd8e31 2022-10-23 thomas }
929 3efd8e31 2022-10-23 thomas val = symget(buf);
930 3efd8e31 2022-10-23 thomas if (val == NULL) {
931 3efd8e31 2022-10-23 thomas yyerror("macro '%s' not defined", buf);
932 3efd8e31 2022-10-23 thomas return (findeol());
933 3efd8e31 2022-10-23 thomas }
934 3efd8e31 2022-10-23 thomas parsebuf = val;
935 3efd8e31 2022-10-23 thomas parseindex = 0;
936 3efd8e31 2022-10-23 thomas goto top;
937 3efd8e31 2022-10-23 thomas }
938 3efd8e31 2022-10-23 thomas
939 3efd8e31 2022-10-23 thomas switch (c) {
940 3efd8e31 2022-10-23 thomas case '\'':
941 3efd8e31 2022-10-23 thomas case '"':
942 3efd8e31 2022-10-23 thomas quotec = c;
943 3efd8e31 2022-10-23 thomas while (1) {
944 3efd8e31 2022-10-23 thomas c = lgetc(quotec);
945 3efd8e31 2022-10-23 thomas if (c == EOF)
946 3efd8e31 2022-10-23 thomas return (0);
947 3efd8e31 2022-10-23 thomas if (c == '\n') {
948 3efd8e31 2022-10-23 thomas file->lineno++;
949 3efd8e31 2022-10-23 thomas continue;
950 3efd8e31 2022-10-23 thomas } else if (c == '\\') {
951 3efd8e31 2022-10-23 thomas next = lgetc(quotec);
952 3efd8e31 2022-10-23 thomas if (next == EOF)
953 3efd8e31 2022-10-23 thomas return (0);
954 3efd8e31 2022-10-23 thomas if (next == quotec || c == ' ' || c == '\t')
955 3efd8e31 2022-10-23 thomas c = next;
956 3efd8e31 2022-10-23 thomas else if (next == '\n') {
957 3efd8e31 2022-10-23 thomas file->lineno++;
958 3efd8e31 2022-10-23 thomas continue;
959 3efd8e31 2022-10-23 thomas } else
960 3efd8e31 2022-10-23 thomas lungetc(next);
961 3efd8e31 2022-10-23 thomas } else if (c == quotec) {
962 3efd8e31 2022-10-23 thomas *p = '\0';
963 3efd8e31 2022-10-23 thomas break;
964 3efd8e31 2022-10-23 thomas } else if (c == '\0') {
965 3efd8e31 2022-10-23 thomas yyerror("syntax error");
966 3efd8e31 2022-10-23 thomas return (findeol());
967 3efd8e31 2022-10-23 thomas }
968 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
969 3efd8e31 2022-10-23 thomas yyerror("string too long");
970 3efd8e31 2022-10-23 thomas return (findeol());
971 3efd8e31 2022-10-23 thomas }
972 3efd8e31 2022-10-23 thomas *p++ = c;
973 3efd8e31 2022-10-23 thomas }
974 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
975 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
976 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
977 3efd8e31 2022-10-23 thomas return (STRING);
978 3efd8e31 2022-10-23 thomas }
979 3efd8e31 2022-10-23 thomas
980 3efd8e31 2022-10-23 thomas #define allowed_to_end_number(x) \
981 3efd8e31 2022-10-23 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
982 3efd8e31 2022-10-23 thomas
983 3efd8e31 2022-10-23 thomas if (c == '-' || isdigit(c)) {
984 3efd8e31 2022-10-23 thomas do {
985 3efd8e31 2022-10-23 thomas *p++ = c;
986 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
987 3efd8e31 2022-10-23 thomas yyerror("string too long");
988 3efd8e31 2022-10-23 thomas return (findeol());
989 3efd8e31 2022-10-23 thomas }
990 3efd8e31 2022-10-23 thomas c = lgetc(0);
991 3efd8e31 2022-10-23 thomas } while (c != EOF && isdigit(c));
992 3efd8e31 2022-10-23 thomas lungetc(c);
993 3efd8e31 2022-10-23 thomas if (p == buf + 1 && buf[0] == '-')
994 3efd8e31 2022-10-23 thomas goto nodigits;
995 3efd8e31 2022-10-23 thomas if (c == EOF || allowed_to_end_number(c)) {
996 3efd8e31 2022-10-23 thomas const char *errstr = NULL;
997 3efd8e31 2022-10-23 thomas
998 3efd8e31 2022-10-23 thomas *p = '\0';
999 3efd8e31 2022-10-23 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
1000 3efd8e31 2022-10-23 thomas LLONG_MAX, &errstr);
1001 3efd8e31 2022-10-23 thomas if (errstr) {
1002 3efd8e31 2022-10-23 thomas yyerror("\"%s\" invalid number: %s",
1003 3efd8e31 2022-10-23 thomas buf, errstr);
1004 3efd8e31 2022-10-23 thomas return (findeol());
1005 3efd8e31 2022-10-23 thomas }
1006 3efd8e31 2022-10-23 thomas return (NUMBER);
1007 3efd8e31 2022-10-23 thomas } else {
1008 3efd8e31 2022-10-23 thomas nodigits:
1009 3efd8e31 2022-10-23 thomas while (p > buf + 1)
1010 3efd8e31 2022-10-23 thomas lungetc(*--p);
1011 3efd8e31 2022-10-23 thomas c = *--p;
1012 3efd8e31 2022-10-23 thomas if (c == '-')
1013 3efd8e31 2022-10-23 thomas return (c);
1014 3efd8e31 2022-10-23 thomas }
1015 3efd8e31 2022-10-23 thomas }
1016 3efd8e31 2022-10-23 thomas
1017 3efd8e31 2022-10-23 thomas #define allowed_in_string(x) \
1018 3efd8e31 2022-10-23 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1019 3efd8e31 2022-10-23 thomas x != '{' && x != '}' && \
1020 3efd8e31 2022-10-23 thomas x != '!' && x != '=' && x != '#' && \
1021 3efd8e31 2022-10-23 thomas x != ','))
1022 3efd8e31 2022-10-23 thomas
1023 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == ':' || c == '_') {
1024 3efd8e31 2022-10-23 thomas do {
1025 3efd8e31 2022-10-23 thomas *p++ = c;
1026 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
1027 3efd8e31 2022-10-23 thomas yyerror("string too long");
1028 3efd8e31 2022-10-23 thomas return (findeol());
1029 3efd8e31 2022-10-23 thomas }
1030 3efd8e31 2022-10-23 thomas c = lgetc(0);
1031 3efd8e31 2022-10-23 thomas } while (c != EOF && (allowed_in_string(c)));
1032 3efd8e31 2022-10-23 thomas lungetc(c);
1033 3efd8e31 2022-10-23 thomas *p = '\0';
1034 3efd8e31 2022-10-23 thomas token = lookup(buf);
1035 3efd8e31 2022-10-23 thomas if (token == STRING) {
1036 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
1037 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
1038 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
1039 3efd8e31 2022-10-23 thomas }
1040 3efd8e31 2022-10-23 thomas return (token);
1041 3efd8e31 2022-10-23 thomas }
1042 3efd8e31 2022-10-23 thomas if (c == '\n') {
1043 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
1044 3efd8e31 2022-10-23 thomas file->lineno++;
1045 3efd8e31 2022-10-23 thomas }
1046 3efd8e31 2022-10-23 thomas if (c == EOF)
1047 3efd8e31 2022-10-23 thomas return (0);
1048 3efd8e31 2022-10-23 thomas return (c);
1049 3efd8e31 2022-10-23 thomas }
1050 3efd8e31 2022-10-23 thomas
1051 3efd8e31 2022-10-23 thomas int
1052 3efd8e31 2022-10-23 thomas check_file_secrecy(int fd, const char *fname)
1053 3efd8e31 2022-10-23 thomas {
1054 3efd8e31 2022-10-23 thomas struct stat st;
1055 3efd8e31 2022-10-23 thomas
1056 3efd8e31 2022-10-23 thomas if (fstat(fd, &st)) {
1057 3efd8e31 2022-10-23 thomas log_warn("cannot stat %s", fname);
1058 3efd8e31 2022-10-23 thomas return (-1);
1059 3efd8e31 2022-10-23 thomas }
1060 3efd8e31 2022-10-23 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
1061 3efd8e31 2022-10-23 thomas log_warnx("%s: owner not root or current user", fname);
1062 3efd8e31 2022-10-23 thomas return (-1);
1063 3efd8e31 2022-10-23 thomas }
1064 3efd8e31 2022-10-23 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
1065 3efd8e31 2022-10-23 thomas log_warnx("%s: group writable or world read/writable", fname);
1066 3efd8e31 2022-10-23 thomas return (-1);
1067 3efd8e31 2022-10-23 thomas }
1068 3efd8e31 2022-10-23 thomas return (0);
1069 3efd8e31 2022-10-23 thomas }
1070 3efd8e31 2022-10-23 thomas
1071 3efd8e31 2022-10-23 thomas struct file *
1072 7554713a 2023-04-01 thomas newfile(const char *name, int secret, int required)
1073 3efd8e31 2022-10-23 thomas {
1074 3efd8e31 2022-10-23 thomas struct file *nfile;
1075 3efd8e31 2022-10-23 thomas
1076 3efd8e31 2022-10-23 thomas nfile = calloc(1, sizeof(struct file));
1077 3efd8e31 2022-10-23 thomas if (nfile == NULL) {
1078 3efd8e31 2022-10-23 thomas log_warn("calloc");
1079 3efd8e31 2022-10-23 thomas return (NULL);
1080 3efd8e31 2022-10-23 thomas }
1081 3efd8e31 2022-10-23 thomas nfile->name = strdup(name);
1082 3efd8e31 2022-10-23 thomas if (nfile->name == NULL) {
1083 3efd8e31 2022-10-23 thomas log_warn("strdup");
1084 3efd8e31 2022-10-23 thomas free(nfile);
1085 3efd8e31 2022-10-23 thomas return (NULL);
1086 3efd8e31 2022-10-23 thomas }
1087 3efd8e31 2022-10-23 thomas nfile->stream = fopen(nfile->name, "r");
1088 3efd8e31 2022-10-23 thomas if (nfile->stream == NULL) {
1089 7554713a 2023-04-01 thomas if (required)
1090 7554713a 2023-04-01 thomas log_warn("open %s", nfile->name);
1091 3efd8e31 2022-10-23 thomas free(nfile->name);
1092 3efd8e31 2022-10-23 thomas free(nfile);
1093 3efd8e31 2022-10-23 thomas return (NULL);
1094 3efd8e31 2022-10-23 thomas } else if (secret &&
1095 3efd8e31 2022-10-23 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
1096 3efd8e31 2022-10-23 thomas fclose(nfile->stream);
1097 3efd8e31 2022-10-23 thomas free(nfile->name);
1098 3efd8e31 2022-10-23 thomas free(nfile);
1099 3efd8e31 2022-10-23 thomas return (NULL);
1100 3efd8e31 2022-10-23 thomas }
1101 3efd8e31 2022-10-23 thomas nfile->lineno = 1;
1102 3efd8e31 2022-10-23 thomas return (nfile);
1103 3efd8e31 2022-10-23 thomas }
1104 3efd8e31 2022-10-23 thomas
1105 3efd8e31 2022-10-23 thomas static void
1106 3efd8e31 2022-10-23 thomas closefile(struct file *xfile)
1107 3efd8e31 2022-10-23 thomas {
1108 3efd8e31 2022-10-23 thomas fclose(xfile->stream);
1109 3efd8e31 2022-10-23 thomas free(xfile->name);
1110 3efd8e31 2022-10-23 thomas free(xfile);
1111 3efd8e31 2022-10-23 thomas }
1112 3efd8e31 2022-10-23 thomas
1113 3efd8e31 2022-10-23 thomas int
1114 3efd8e31 2022-10-23 thomas parse_config(const char *filename, enum gotd_procid proc_id,
1115 f3807fe5 2023-07-10 thomas struct gotd *env)
1116 3efd8e31 2022-10-23 thomas {
1117 3efd8e31 2022-10-23 thomas struct sym *sym, *next;
1118 88f1bb6d 2023-01-02 thomas struct gotd_repo *repo;
1119 f3807fe5 2023-07-10 thomas int require_config_file = (proc_id != PROC_GITWRAPPER);
1120 3efd8e31 2022-10-23 thomas
1121 3efd8e31 2022-10-23 thomas memset(env, 0, sizeof(*env));
1122 3efd8e31 2022-10-23 thomas
1123 3efd8e31 2022-10-23 thomas gotd = env;
1124 3efd8e31 2022-10-23 thomas gotd_proc_id = proc_id;
1125 3efd8e31 2022-10-23 thomas TAILQ_INIT(&gotd->repos);
1126 3efd8e31 2022-10-23 thomas
1127 3efd8e31 2022-10-23 thomas /* Apply default values. */
1128 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
1129 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
1130 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: unix socket path too long", __func__);
1131 3efd8e31 2022-10-23 thomas return -1;
1132 3efd8e31 2022-10-23 thomas }
1133 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, GOTD_USER,
1134 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
1135 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: user name too long", __func__);
1136 3efd8e31 2022-10-23 thomas return -1;
1137 3efd8e31 2022-10-23 thomas }
1138 0781db0e 2023-01-06 thomas
1139 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
1140 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_usec = 0;
1141 3efd8e31 2022-10-23 thomas
1142 7554713a 2023-04-01 thomas file = newfile(filename, 0, require_config_file);
1143 f3296add 2023-03-01 thomas if (file == NULL)
1144 7554713a 2023-04-01 thomas return require_config_file ? -1 : 0;
1145 3efd8e31 2022-10-23 thomas
1146 3efd8e31 2022-10-23 thomas yyparse();
1147 3efd8e31 2022-10-23 thomas errors = file->errors;
1148 3efd8e31 2022-10-23 thomas closefile(file);
1149 3efd8e31 2022-10-23 thomas
1150 3efd8e31 2022-10-23 thomas /* Free macros and check which have not been used. */
1151 3efd8e31 2022-10-23 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1152 3efd8e31 2022-10-23 thomas if ((gotd->verbosity > 1) && !sym->used)
1153 3efd8e31 2022-10-23 thomas fprintf(stderr, "warning: macro '%s' not used\n",
1154 3efd8e31 2022-10-23 thomas sym->nam);
1155 3efd8e31 2022-10-23 thomas if (!sym->persist) {
1156 3efd8e31 2022-10-23 thomas free(sym->nam);
1157 3efd8e31 2022-10-23 thomas free(sym->val);
1158 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
1159 3efd8e31 2022-10-23 thomas free(sym);
1160 3efd8e31 2022-10-23 thomas }
1161 3efd8e31 2022-10-23 thomas }
1162 3efd8e31 2022-10-23 thomas
1163 3efd8e31 2022-10-23 thomas if (errors)
1164 3efd8e31 2022-10-23 thomas return (-1);
1165 3efd8e31 2022-10-23 thomas
1166 88f1bb6d 2023-01-02 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
1167 88f1bb6d 2023-01-02 thomas if (repo->path[0] == '\0') {
1168 0cbf8de7 2023-01-02 thomas log_warnx("repository \"%s\": no path provided in "
1169 0cbf8de7 2023-01-02 thomas "configuration file", repo->name);
1170 88f1bb6d 2023-01-02 thomas return (-1);
1171 88f1bb6d 2023-01-02 thomas }
1172 88f1bb6d 2023-01-02 thomas }
1173 88f1bb6d 2023-01-02 thomas
1174 4d24b1fd 2023-01-19 thomas if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
1175 4d24b1fd 2023-01-19 thomas log_warnx("no repository defined in configuration file");
1176 4d24b1fd 2023-01-19 thomas return (-1);
1177 4d24b1fd 2023-01-19 thomas }
1178 4d24b1fd 2023-01-19 thomas
1179 3efd8e31 2022-10-23 thomas return (0);
1180 3efd8e31 2022-10-23 thomas }
1181 3efd8e31 2022-10-23 thomas
1182 0781db0e 2023-01-06 thomas static int
1183 0781db0e 2023-01-06 thomas uid_connection_limit_cmp(const void *pa, const void *pb)
1184 0781db0e 2023-01-06 thomas {
1185 0781db0e 2023-01-06 thomas const struct gotd_uid_connection_limit *a = pa, *b = pb;
1186 0781db0e 2023-01-06 thomas
1187 0781db0e 2023-01-06 thomas if (a->uid < b->uid)
1188 0781db0e 2023-01-06 thomas return -1;
1189 0781db0e 2023-01-06 thomas else if (a->uid > b->uid);
1190 0781db0e 2023-01-06 thomas return 1;
1191 0781db0e 2023-01-06 thomas
1192 0781db0e 2023-01-06 thomas return 0;
1193 0781db0e 2023-01-06 thomas }
1194 0781db0e 2023-01-06 thomas
1195 0781db0e 2023-01-06 thomas static int
1196 0781db0e 2023-01-06 thomas conf_limit_user_connections(const char *user, int maximum)
1197 0781db0e 2023-01-06 thomas {
1198 0781db0e 2023-01-06 thomas uid_t uid;
1199 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *limit;
1200 0781db0e 2023-01-06 thomas size_t nlimits;
1201 0781db0e 2023-01-06 thomas
1202 0781db0e 2023-01-06 thomas if (maximum < 1) {
1203 0781db0e 2023-01-06 thomas yyerror("max connections cannot be smaller 1");
1204 0781db0e 2023-01-06 thomas return -1;
1205 0781db0e 2023-01-06 thomas }
1206 0781db0e 2023-01-06 thomas if (maximum > GOTD_MAXCLIENTS) {
1207 0781db0e 2023-01-06 thomas yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
1208 0781db0e 2023-01-06 thomas return -1;
1209 0781db0e 2023-01-06 thomas }
1210 0781db0e 2023-01-06 thomas
1211 48488136 2023-04-22 thomas if (gotd_parseuid(user, &uid) == -1) {
1212 0781db0e 2023-01-06 thomas yyerror("%s: no such user", user);
1213 0781db0e 2023-01-06 thomas return -1;
1214 0781db0e 2023-01-06 thomas }
1215 0781db0e 2023-01-06 thomas
1216 0781db0e 2023-01-06 thomas limit = gotd_find_uid_connection_limit(gotd->connection_limits,
1217 0781db0e 2023-01-06 thomas gotd->nconnection_limits, uid);
1218 0781db0e 2023-01-06 thomas if (limit) {
1219 0781db0e 2023-01-06 thomas limit->max_connections = maximum;
1220 0781db0e 2023-01-06 thomas return 0;
1221 0781db0e 2023-01-06 thomas }
1222 0781db0e 2023-01-06 thomas
1223 0781db0e 2023-01-06 thomas limit = gotd->connection_limits;
1224 0781db0e 2023-01-06 thomas nlimits = gotd->nconnection_limits + 1;
1225 0781db0e 2023-01-06 thomas limit = reallocarray(limit, nlimits, sizeof(*limit));
1226 0781db0e 2023-01-06 thomas if (limit == NULL)
1227 0781db0e 2023-01-06 thomas fatal("reallocarray");
1228 0781db0e 2023-01-06 thomas
1229 0781db0e 2023-01-06 thomas limit[nlimits - 1].uid = uid;
1230 0781db0e 2023-01-06 thomas limit[nlimits - 1].max_connections = maximum;
1231 0781db0e 2023-01-06 thomas
1232 0781db0e 2023-01-06 thomas gotd->connection_limits = limit;
1233 0781db0e 2023-01-06 thomas gotd->nconnection_limits = nlimits;
1234 0781db0e 2023-01-06 thomas qsort(gotd->connection_limits, gotd->nconnection_limits,
1235 0781db0e 2023-01-06 thomas sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
1236 0781db0e 2023-01-06 thomas
1237 0781db0e 2023-01-06 thomas return 0;
1238 0781db0e 2023-01-06 thomas }
1239 0781db0e 2023-01-06 thomas
1240 3efd8e31 2022-10-23 thomas static struct gotd_repo *
1241 3efd8e31 2022-10-23 thomas conf_new_repo(const char *name)
1242 3efd8e31 2022-10-23 thomas {
1243 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
1244 3efd8e31 2022-10-23 thomas
1245 a42b418b 2023-01-02 thomas if (name[0] == '\0') {
1246 a42b418b 2023-01-02 thomas fatalx("syntax error: empty repository name found in %s",
1247 a42b418b 2023-01-02 thomas file->name);
1248 a42b418b 2023-01-02 thomas }
1249 a42b418b 2023-01-02 thomas
1250 0cbf8de7 2023-01-02 thomas if (strchr(name, '\n') != NULL)
1251 0cbf8de7 2023-01-02 thomas fatalx("repository names must not contain linefeeds: %s", name);
1252 3efd8e31 2022-10-23 thomas
1253 3efd8e31 2022-10-23 thomas repo = calloc(1, sizeof(*repo));
1254 3efd8e31 2022-10-23 thomas if (repo == NULL)
1255 3efd8e31 2022-10-23 thomas fatalx("%s: calloc", __func__);
1256 3efd8e31 2022-10-23 thomas
1257 729a7e24 2022-11-17 thomas STAILQ_INIT(&repo->rules);
1258 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_tag_namespaces);
1259 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_branch_namespaces);
1260 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_branches);
1261 ce1bfad9 2024-03-30 thomas TAILQ_INIT(&repo->protected_branches);
1262 ce1bfad9 2024-03-30 thomas TAILQ_INIT(&repo->notification_refs);
1263 ce1bfad9 2024-03-30 thomas TAILQ_INIT(&repo->notification_ref_namespaces);
1264 ce1bfad9 2024-03-30 thomas STAILQ_INIT(&repo->notification_targets);
1265 729a7e24 2022-11-17 thomas
1266 3efd8e31 2022-10-23 thomas if (strlcpy(repo->name, name, sizeof(repo->name)) >=
1267 3efd8e31 2022-10-23 thomas sizeof(repo->name))
1268 3efd8e31 2022-10-23 thomas fatalx("%s: strlcpy", __func__);
1269 3efd8e31 2022-10-23 thomas
1270 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
1271 3efd8e31 2022-10-23 thomas gotd->nrepos++;
1272 3efd8e31 2022-10-23 thomas
1273 3efd8e31 2022-10-23 thomas return repo;
1274 3efd8e31 2022-10-23 thomas };
1275 729a7e24 2022-11-17 thomas
1276 729a7e24 2022-11-17 thomas static void
1277 729a7e24 2022-11-17 thomas conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
1278 729a7e24 2022-11-17 thomas int authorization, char *identifier)
1279 729a7e24 2022-11-17 thomas {
1280 729a7e24 2022-11-17 thomas struct gotd_access_rule *rule;
1281 729a7e24 2022-11-17 thomas
1282 729a7e24 2022-11-17 thomas rule = calloc(1, sizeof(*rule));
1283 729a7e24 2022-11-17 thomas if (rule == NULL)
1284 729a7e24 2022-11-17 thomas fatal("calloc");
1285 3efd8e31 2022-10-23 thomas
1286 729a7e24 2022-11-17 thomas rule->access = access;
1287 729a7e24 2022-11-17 thomas rule->authorization = authorization;
1288 729a7e24 2022-11-17 thomas rule->identifier = identifier;
1289 729a7e24 2022-11-17 thomas
1290 729a7e24 2022-11-17 thomas STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
1291 729a7e24 2022-11-17 thomas }
1292 729a7e24 2022-11-17 thomas
1293 6d7eb4f7 2023-04-04 thomas static int
1294 6d7eb4f7 2023-04-04 thomas refname_is_valid(char *refname)
1295 6d7eb4f7 2023-04-04 thomas {
1296 ef44e136 2023-06-15 thomas if (strncmp(refname, "refs/", 5) != 0) {
1297 6d7eb4f7 2023-04-04 thomas yyerror("reference name must begin with \"refs/\": %s",
1298 6d7eb4f7 2023-04-04 thomas refname);
1299 6d7eb4f7 2023-04-04 thomas return 0;
1300 6d7eb4f7 2023-04-04 thomas }
1301 6d7eb4f7 2023-04-04 thomas
1302 6d7eb4f7 2023-04-04 thomas if (!got_ref_name_is_valid(refname)) {
1303 6d7eb4f7 2023-04-04 thomas yyerror("invalid reference name: %s", refname);
1304 6d7eb4f7 2023-04-04 thomas return 0;
1305 6d7eb4f7 2023-04-04 thomas }
1306 6d7eb4f7 2023-04-04 thomas
1307 6d7eb4f7 2023-04-04 thomas return 1;
1308 6d7eb4f7 2023-04-04 thomas }
1309 6d7eb4f7 2023-04-04 thomas
1310 6d7eb4f7 2023-04-04 thomas static int
1311 57b6056a 2023-04-05 thomas conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
1312 57b6056a 2023-04-05 thomas char *namespace)
1313 6d7eb4f7 2023-04-04 thomas {
1314 6d7eb4f7 2023-04-04 thomas const struct got_error *error;
1315 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
1316 6d7eb4f7 2023-04-04 thomas char *s;
1317 6d7eb4f7 2023-04-04 thomas
1318 57b6056a 2023-04-05 thomas *new = NULL;
1319 57b6056a 2023-04-05 thomas
1320 6d7eb4f7 2023-04-04 thomas got_path_strip_trailing_slashes(namespace);
1321 6d7eb4f7 2023-04-04 thomas if (!refname_is_valid(namespace))
1322 6d7eb4f7 2023-04-04 thomas return -1;
1323 6d7eb4f7 2023-04-04 thomas if (asprintf(&s, "%s/", namespace) == -1) {
1324 6d7eb4f7 2023-04-04 thomas yyerror("asprintf: %s", strerror(errno));
1325 6d7eb4f7 2023-04-04 thomas return -1;
1326 6d7eb4f7 2023-04-04 thomas }
1327 6d7eb4f7 2023-04-04 thomas
1328 57b6056a 2023-04-05 thomas error = got_pathlist_insert(&pe, refs, s, NULL);
1329 57b6056a 2023-04-05 thomas if (error || pe == NULL) {
1330 a4435ef0 2023-04-05 thomas free(s);
1331 a4435ef0 2023-04-05 thomas if (error)
1332 a4435ef0 2023-04-05 thomas yyerror("got_pathlist_insert: %s", error->msg);
1333 a4435ef0 2023-04-05 thomas else
1334 3f0853b8 2023-04-07 thomas yyerror("duplicate protected namespace %s", namespace);
1335 6d7eb4f7 2023-04-04 thomas return -1;
1336 6d7eb4f7 2023-04-04 thomas }
1337 6d7eb4f7 2023-04-04 thomas
1338 57b6056a 2023-04-05 thomas *new = s;
1339 6d7eb4f7 2023-04-04 thomas return 0;
1340 6d7eb4f7 2023-04-04 thomas }
1341 6d7eb4f7 2023-04-04 thomas
1342 6d7eb4f7 2023-04-04 thomas static int
1343 6d7eb4f7 2023-04-04 thomas conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
1344 6d7eb4f7 2023-04-04 thomas {
1345 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
1346 57b6056a 2023-04-05 thomas char *new;
1347 57b6056a 2023-04-05 thomas
1348 57b6056a 2023-04-05 thomas if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
1349 57b6056a 2023-04-05 thomas namespace) == -1)
1350 57b6056a 2023-04-05 thomas return -1;
1351 57b6056a 2023-04-05 thomas
1352 57b6056a 2023-04-05 thomas TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
1353 57b6056a 2023-04-05 thomas if (strcmp(pe->path, new) == 0) {
1354 3f0853b8 2023-04-07 thomas yyerror("duplicate protected namespace %s", namespace);
1355 57b6056a 2023-04-05 thomas return -1;
1356 57b6056a 2023-04-05 thomas }
1357 57b6056a 2023-04-05 thomas }
1358 57b6056a 2023-04-05 thomas
1359 57b6056a 2023-04-05 thomas return 0;
1360 6d7eb4f7 2023-04-04 thomas }
1361 6d7eb4f7 2023-04-04 thomas
1362 6d7eb4f7 2023-04-04 thomas static int
1363 6d7eb4f7 2023-04-04 thomas conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
1364 6d7eb4f7 2023-04-04 thomas {
1365 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
1366 57b6056a 2023-04-05 thomas char *new;
1367 57b6056a 2023-04-05 thomas
1368 57b6056a 2023-04-05 thomas if (conf_protect_ref_namespace(&new,
1369 57b6056a 2023-04-05 thomas &repo->protected_branch_namespaces, namespace) == -1)
1370 57b6056a 2023-04-05 thomas return -1;
1371 57b6056a 2023-04-05 thomas
1372 57b6056a 2023-04-05 thomas TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
1373 57b6056a 2023-04-05 thomas if (strcmp(pe->path, new) == 0) {
1374 3f0853b8 2023-04-07 thomas yyerror("duplicate protected namespace %s", namespace);
1375 57b6056a 2023-04-05 thomas return -1;
1376 57b6056a 2023-04-05 thomas }
1377 57b6056a 2023-04-05 thomas }
1378 57b6056a 2023-04-05 thomas
1379 57b6056a 2023-04-05 thomas return 0;
1380 6d7eb4f7 2023-04-04 thomas }
1381 6d7eb4f7 2023-04-04 thomas
1382 6d7eb4f7 2023-04-04 thomas static int
1383 6d7eb4f7 2023-04-04 thomas conf_protect_branch(struct gotd_repo *repo, char *branchname)
1384 6d7eb4f7 2023-04-04 thomas {
1385 6d7eb4f7 2023-04-04 thomas const struct got_error *error;
1386 a4435ef0 2023-04-05 thomas struct got_pathlist_entry *new;
1387 6d7eb4f7 2023-04-04 thomas char *refname;
1388 6d7eb4f7 2023-04-04 thomas
1389 6d7eb4f7 2023-04-04 thomas if (strncmp(branchname, "refs/heads/", 11) != 0) {
1390 6d7eb4f7 2023-04-04 thomas if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1391 6d7eb4f7 2023-04-04 thomas yyerror("asprintf: %s", strerror(errno));
1392 6d7eb4f7 2023-04-04 thomas return -1;
1393 6d7eb4f7 2023-04-04 thomas }
1394 6d7eb4f7 2023-04-04 thomas } else {
1395 6d7eb4f7 2023-04-04 thomas refname = strdup(branchname);
1396 6d7eb4f7 2023-04-04 thomas if (refname == NULL) {
1397 6d7eb4f7 2023-04-04 thomas yyerror("strdup: %s", strerror(errno));
1398 6d7eb4f7 2023-04-04 thomas return -1;
1399 6d7eb4f7 2023-04-04 thomas }
1400 6d7eb4f7 2023-04-04 thomas }
1401 6d7eb4f7 2023-04-04 thomas
1402 6d7eb4f7 2023-04-04 thomas if (!refname_is_valid(refname)) {
1403 6d7eb4f7 2023-04-04 thomas free(refname);
1404 6d7eb4f7 2023-04-04 thomas return -1;
1405 6d7eb4f7 2023-04-04 thomas }
1406 6d7eb4f7 2023-04-04 thomas
1407 a4435ef0 2023-04-05 thomas error = got_pathlist_insert(&new, &repo->protected_branches,
1408 6d7eb4f7 2023-04-04 thomas refname, NULL);
1409 a4435ef0 2023-04-05 thomas if (error || new == NULL) {
1410 a4435ef0 2023-04-05 thomas free(refname);
1411 a4435ef0 2023-04-05 thomas if (error)
1412 a4435ef0 2023-04-05 thomas yyerror("got_pathlist_insert: %s", error->msg);
1413 a4435ef0 2023-04-05 thomas else
1414 a4435ef0 2023-04-05 thomas yyerror("duplicate protect branch %s", branchname);
1415 ce1bfad9 2024-03-30 thomas return -1;
1416 ce1bfad9 2024-03-30 thomas }
1417 ce1bfad9 2024-03-30 thomas
1418 ce1bfad9 2024-03-30 thomas return 0;
1419 ce1bfad9 2024-03-30 thomas }
1420 ce1bfad9 2024-03-30 thomas
1421 ce1bfad9 2024-03-30 thomas static int
1422 ce1bfad9 2024-03-30 thomas conf_notify_branch(struct gotd_repo *repo, char *branchname)
1423 ce1bfad9 2024-03-30 thomas {
1424 ce1bfad9 2024-03-30 thomas const struct got_error *error;
1425 ce1bfad9 2024-03-30 thomas struct got_pathlist_entry *pe;
1426 ce1bfad9 2024-03-30 thomas char *refname;
1427 ce1bfad9 2024-03-30 thomas
1428 ce1bfad9 2024-03-30 thomas if (strncmp(branchname, "refs/heads/", 11) != 0) {
1429 ce1bfad9 2024-03-30 thomas if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1430 ce1bfad9 2024-03-30 thomas yyerror("asprintf: %s", strerror(errno));
1431 ce1bfad9 2024-03-30 thomas return -1;
1432 ce1bfad9 2024-03-30 thomas }
1433 ce1bfad9 2024-03-30 thomas } else {
1434 ce1bfad9 2024-03-30 thomas refname = strdup(branchname);
1435 ce1bfad9 2024-03-30 thomas if (refname == NULL) {
1436 ce1bfad9 2024-03-30 thomas yyerror("strdup: %s", strerror(errno));
1437 ce1bfad9 2024-03-30 thomas return -1;
1438 ce1bfad9 2024-03-30 thomas }
1439 ce1bfad9 2024-03-30 thomas }
1440 ce1bfad9 2024-03-30 thomas
1441 ce1bfad9 2024-03-30 thomas if (!refname_is_valid(refname)) {
1442 ce1bfad9 2024-03-30 thomas free(refname);
1443 6d7eb4f7 2023-04-04 thomas return -1;
1444 6d7eb4f7 2023-04-04 thomas }
1445 6d7eb4f7 2023-04-04 thomas
1446 ce1bfad9 2024-03-30 thomas error = got_pathlist_insert(&pe, &repo->notification_refs,
1447 ce1bfad9 2024-03-30 thomas refname, NULL);
1448 ce1bfad9 2024-03-30 thomas if (error) {
1449 ce1bfad9 2024-03-30 thomas free(refname);
1450 ce1bfad9 2024-03-30 thomas yyerror("got_pathlist_insert: %s", error->msg);
1451 ce1bfad9 2024-03-30 thomas return -1;
1452 ce1bfad9 2024-03-30 thomas }
1453 ce1bfad9 2024-03-30 thomas if (pe == NULL)
1454 ce1bfad9 2024-03-30 thomas free(refname);
1455 ce1bfad9 2024-03-30 thomas
1456 6d7eb4f7 2023-04-04 thomas return 0;
1457 6d7eb4f7 2023-04-04 thomas }
1458 6d7eb4f7 2023-04-04 thomas
1459 ce1bfad9 2024-03-30 thomas static int
1460 ce1bfad9 2024-03-30 thomas conf_notify_ref_namespace(struct gotd_repo *repo, char *namespace)
1461 ce1bfad9 2024-03-30 thomas {
1462 ce1bfad9 2024-03-30 thomas const struct got_error *error;
1463 ce1bfad9 2024-03-30 thomas struct got_pathlist_entry *pe;
1464 ce1bfad9 2024-03-30 thomas char *s;
1465 ce1bfad9 2024-03-30 thomas
1466 ce1bfad9 2024-03-30 thomas got_path_strip_trailing_slashes(namespace);
1467 ce1bfad9 2024-03-30 thomas if (!refname_is_valid(namespace))
1468 ce1bfad9 2024-03-30 thomas return -1;
1469 ce1bfad9 2024-03-30 thomas
1470 ce1bfad9 2024-03-30 thomas if (asprintf(&s, "%s/", namespace) == -1) {
1471 ce1bfad9 2024-03-30 thomas yyerror("asprintf: %s", strerror(errno));
1472 ce1bfad9 2024-03-30 thomas return -1;
1473 ce1bfad9 2024-03-30 thomas }
1474 ce1bfad9 2024-03-30 thomas
1475 ce1bfad9 2024-03-30 thomas error = got_pathlist_insert(&pe, &repo->notification_ref_namespaces,
1476 ce1bfad9 2024-03-30 thomas s, NULL);
1477 ce1bfad9 2024-03-30 thomas if (error) {
1478 ce1bfad9 2024-03-30 thomas free(s);
1479 ce1bfad9 2024-03-30 thomas yyerror("got_pathlist_insert: %s", error->msg);
1480 ce1bfad9 2024-03-30 thomas return -1;
1481 ce1bfad9 2024-03-30 thomas }
1482 ce1bfad9 2024-03-30 thomas if (pe == NULL)
1483 ce1bfad9 2024-03-30 thomas free(s);
1484 ce1bfad9 2024-03-30 thomas
1485 ce1bfad9 2024-03-30 thomas return 0;
1486 ce1bfad9 2024-03-30 thomas }
1487 ce1bfad9 2024-03-30 thomas
1488 ce1bfad9 2024-03-30 thomas static int
1489 ce1bfad9 2024-03-30 thomas conf_notify_email(struct gotd_repo *repo, char *sender, char *recipient,
1490 ce1bfad9 2024-03-30 thomas char *responder, char *hostname, char *port)
1491 ce1bfad9 2024-03-30 thomas {
1492 ce1bfad9 2024-03-30 thomas struct gotd_notification_target *target;
1493 ce1bfad9 2024-03-30 thomas
1494 ce1bfad9 2024-03-30 thomas STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1495 ce1bfad9 2024-03-30 thomas if (target->type != GOTD_NOTIFICATION_VIA_EMAIL)
1496 ce1bfad9 2024-03-30 thomas continue;
1497 ce1bfad9 2024-03-30 thomas if (strcmp(target->conf.email.recipient, recipient) == 0) {
1498 ce1bfad9 2024-03-30 thomas yyerror("duplicate email notification for '%s' in "
1499 ce1bfad9 2024-03-30 thomas "repository '%s'", recipient, repo->name);
1500 ce1bfad9 2024-03-30 thomas return -1;
1501 ce1bfad9 2024-03-30 thomas }
1502 ce1bfad9 2024-03-30 thomas }
1503 ce1bfad9 2024-03-30 thomas
1504 ce1bfad9 2024-03-30 thomas target = calloc(1, sizeof(*target));
1505 ce1bfad9 2024-03-30 thomas if (target == NULL)
1506 ce1bfad9 2024-03-30 thomas fatal("calloc");
1507 ce1bfad9 2024-03-30 thomas target->type = GOTD_NOTIFICATION_VIA_EMAIL;
1508 ce1bfad9 2024-03-30 thomas if (sender) {
1509 ce1bfad9 2024-03-30 thomas target->conf.email.sender = strdup(sender);
1510 ce1bfad9 2024-03-30 thomas if (target->conf.email.sender == NULL)
1511 ce1bfad9 2024-03-30 thomas fatal("strdup");
1512 ce1bfad9 2024-03-30 thomas }
1513 ce1bfad9 2024-03-30 thomas target->conf.email.recipient = strdup(recipient);
1514 ce1bfad9 2024-03-30 thomas if (target->conf.email.recipient == NULL)
1515 ce1bfad9 2024-03-30 thomas fatal("strdup");
1516 ce1bfad9 2024-03-30 thomas if (responder) {
1517 ce1bfad9 2024-03-30 thomas target->conf.email.responder = strdup(responder);
1518 ce1bfad9 2024-03-30 thomas if (target->conf.email.responder == NULL)
1519 ce1bfad9 2024-03-30 thomas fatal("strdup");
1520 ce1bfad9 2024-03-30 thomas }
1521 ce1bfad9 2024-03-30 thomas if (hostname) {
1522 ce1bfad9 2024-03-30 thomas target->conf.email.hostname = strdup(hostname);
1523 ce1bfad9 2024-03-30 thomas if (target->conf.email.hostname == NULL)
1524 ce1bfad9 2024-03-30 thomas fatal("strdup");
1525 ce1bfad9 2024-03-30 thomas }
1526 ce1bfad9 2024-03-30 thomas if (port) {
1527 ce1bfad9 2024-03-30 thomas target->conf.email.port = strdup(port);
1528 ce1bfad9 2024-03-30 thomas if (target->conf.email.port == NULL)
1529 ce1bfad9 2024-03-30 thomas fatal("strdup");
1530 ce1bfad9 2024-03-30 thomas }
1531 ce1bfad9 2024-03-30 thomas
1532 ce1bfad9 2024-03-30 thomas STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1533 ce1bfad9 2024-03-30 thomas return 0;
1534 ce1bfad9 2024-03-30 thomas }
1535 ce1bfad9 2024-03-30 thomas
1536 ce1bfad9 2024-03-30 thomas static int
1537 ce1bfad9 2024-03-30 thomas conf_notify_http(struct gotd_repo *repo, char *url, char *user, char *password)
1538 ce1bfad9 2024-03-30 thomas {
1539 ce1bfad9 2024-03-30 thomas const struct got_error *error;
1540 ce1bfad9 2024-03-30 thomas struct gotd_notification_target *target;
1541 94a3f4e9 2024-03-30 thomas char *proto, *hostname, *port, *path;
1542 94a3f4e9 2024-03-30 thomas int tls = 0, ret = 0;
1543 ce1bfad9 2024-03-30 thomas
1544 94a3f4e9 2024-03-30 thomas error = gotd_parse_url(&proto, &hostname, &port, &path, url);
1545 ce1bfad9 2024-03-30 thomas if (error) {
1546 ce1bfad9 2024-03-30 thomas yyerror("invalid HTTP notification URL '%s' in "
1547 ce1bfad9 2024-03-30 thomas "repository '%s': %s", url, repo->name, error->msg);
1548 ce1bfad9 2024-03-30 thomas return -1;
1549 ce1bfad9 2024-03-30 thomas }
1550 ce1bfad9 2024-03-30 thomas
1551 94a3f4e9 2024-03-30 thomas tls = !strcmp(proto, "https");
1552 94a3f4e9 2024-03-30 thomas
1553 ce1bfad9 2024-03-30 thomas if (strcmp(proto, "http") != 0 && strcmp(proto, "https") != 0) {
1554 ce1bfad9 2024-03-30 thomas yyerror("invalid protocol '%s' in notification URL '%s' in "
1555 ce1bfad9 2024-03-30 thomas "repository '%s", proto, url, repo->name);
1556 94a3f4e9 2024-03-30 thomas ret = -1;
1557 94a3f4e9 2024-03-30 thomas goto done;
1558 94a3f4e9 2024-03-30 thomas }
1559 94a3f4e9 2024-03-30 thomas
1560 94a3f4e9 2024-03-30 thomas if ((user != NULL && password == NULL) ||
1561 94a3f4e9 2024-03-30 thomas (user == NULL && password != NULL)) {
1562 94a3f4e9 2024-03-30 thomas yyerror("missing username or password");
1563 ce1bfad9 2024-03-30 thomas ret = -1;
1564 ce1bfad9 2024-03-30 thomas goto done;
1565 ce1bfad9 2024-03-30 thomas }
1566 ce1bfad9 2024-03-30 thomas
1567 ce1bfad9 2024-03-30 thomas if (strcmp(proto, "http") == 0 && (user != NULL || password != NULL)) {
1568 ce1bfad9 2024-03-30 thomas log_warnx("%s: WARNING: Using basic authentication over "
1569 ce1bfad9 2024-03-30 thomas "plaintext http:// will leak credentials; https:// is "
1570 ce1bfad9 2024-03-30 thomas "recommended for URL '%s'", getprogname(), url);
1571 ce1bfad9 2024-03-30 thomas }
1572 ce1bfad9 2024-03-30 thomas
1573 ce1bfad9 2024-03-30 thomas STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1574 ce1bfad9 2024-03-30 thomas if (target->type != GOTD_NOTIFICATION_VIA_HTTP)
1575 ce1bfad9 2024-03-30 thomas continue;
1576 94a3f4e9 2024-03-30 thomas if (target->conf.http.tls == tls &&
1577 94a3f4e9 2024-03-30 thomas !strcmp(target->conf.http.hostname, hostname) &&
1578 94a3f4e9 2024-03-30 thomas !strcmp(target->conf.http.port, port) &&
1579 94a3f4e9 2024-03-30 thomas !strcmp(target->conf.http.path, path)) {
1580 ce1bfad9 2024-03-30 thomas yyerror("duplicate notification for URL '%s' in "
1581 ce1bfad9 2024-03-30 thomas "repository '%s'", url, repo->name);
1582 ce1bfad9 2024-03-30 thomas ret = -1;
1583 ce1bfad9 2024-03-30 thomas goto done;
1584 ce1bfad9 2024-03-30 thomas }
1585 ce1bfad9 2024-03-30 thomas }
1586 ce1bfad9 2024-03-30 thomas
1587 ce1bfad9 2024-03-30 thomas target = calloc(1, sizeof(*target));
1588 ce1bfad9 2024-03-30 thomas if (target == NULL)
1589 ce1bfad9 2024-03-30 thomas fatal("calloc");
1590 ce1bfad9 2024-03-30 thomas target->type = GOTD_NOTIFICATION_VIA_HTTP;
1591 94a3f4e9 2024-03-30 thomas target->conf.http.tls = tls;
1592 94a3f4e9 2024-03-30 thomas target->conf.http.hostname = hostname;
1593 94a3f4e9 2024-03-30 thomas target->conf.http.port = port;
1594 94a3f4e9 2024-03-30 thomas target->conf.http.path = path;
1595 94a3f4e9 2024-03-30 thomas hostname = port = path = NULL;
1596 94a3f4e9 2024-03-30 thomas
1597 ce1bfad9 2024-03-30 thomas if (user) {
1598 ce1bfad9 2024-03-30 thomas target->conf.http.user = strdup(user);
1599 ce1bfad9 2024-03-30 thomas if (target->conf.http.user == NULL)
1600 94a3f4e9 2024-03-30 thomas fatal("strdup");
1601 ce1bfad9 2024-03-30 thomas target->conf.http.password = strdup(password);
1602 ce1bfad9 2024-03-30 thomas if (target->conf.http.password == NULL)
1603 94a3f4e9 2024-03-30 thomas fatal("strdup");
1604 ce1bfad9 2024-03-30 thomas }
1605 ce1bfad9 2024-03-30 thomas
1606 ce1bfad9 2024-03-30 thomas STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1607 ce1bfad9 2024-03-30 thomas done:
1608 ce1bfad9 2024-03-30 thomas free(proto);
1609 94a3f4e9 2024-03-30 thomas free(hostname);
1610 ce1bfad9 2024-03-30 thomas free(port);
1611 94a3f4e9 2024-03-30 thomas free(path);
1612 ce1bfad9 2024-03-30 thomas return ret;
1613 ce1bfad9 2024-03-30 thomas }
1614 ce1bfad9 2024-03-30 thomas
1615 3efd8e31 2022-10-23 thomas int
1616 3efd8e31 2022-10-23 thomas symset(const char *nam, const char *val, int persist)
1617 3efd8e31 2022-10-23 thomas {
1618 3efd8e31 2022-10-23 thomas struct sym *sym;
1619 3efd8e31 2022-10-23 thomas
1620 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1621 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0)
1622 3efd8e31 2022-10-23 thomas break;
1623 3efd8e31 2022-10-23 thomas }
1624 3efd8e31 2022-10-23 thomas
1625 3efd8e31 2022-10-23 thomas if (sym != NULL) {
1626 3efd8e31 2022-10-23 thomas if (sym->persist == 1)
1627 3efd8e31 2022-10-23 thomas return (0);
1628 3efd8e31 2022-10-23 thomas else {
1629 3efd8e31 2022-10-23 thomas free(sym->nam);
1630 3efd8e31 2022-10-23 thomas free(sym->val);
1631 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
1632 3efd8e31 2022-10-23 thomas free(sym);
1633 3efd8e31 2022-10-23 thomas }
1634 3efd8e31 2022-10-23 thomas }
1635 3efd8e31 2022-10-23 thomas sym = calloc(1, sizeof(*sym));
1636 3efd8e31 2022-10-23 thomas if (sym == NULL)
1637 3efd8e31 2022-10-23 thomas return (-1);
1638 3efd8e31 2022-10-23 thomas
1639 3efd8e31 2022-10-23 thomas sym->nam = strdup(nam);
1640 3efd8e31 2022-10-23 thomas if (sym->nam == NULL) {
1641 3efd8e31 2022-10-23 thomas free(sym);
1642 3efd8e31 2022-10-23 thomas return (-1);
1643 3efd8e31 2022-10-23 thomas }
1644 3efd8e31 2022-10-23 thomas sym->val = strdup(val);
1645 3efd8e31 2022-10-23 thomas if (sym->val == NULL) {
1646 3efd8e31 2022-10-23 thomas free(sym->nam);
1647 3efd8e31 2022-10-23 thomas free(sym);
1648 3efd8e31 2022-10-23 thomas return (-1);
1649 3efd8e31 2022-10-23 thomas }
1650 3efd8e31 2022-10-23 thomas sym->used = 0;
1651 3efd8e31 2022-10-23 thomas sym->persist = persist;
1652 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
1653 3efd8e31 2022-10-23 thomas return (0);
1654 3efd8e31 2022-10-23 thomas }
1655 3efd8e31 2022-10-23 thomas
1656 3efd8e31 2022-10-23 thomas char *
1657 3efd8e31 2022-10-23 thomas symget(const char *nam)
1658 3efd8e31 2022-10-23 thomas {
1659 3efd8e31 2022-10-23 thomas struct sym *sym;
1660 3efd8e31 2022-10-23 thomas
1661 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1662 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0) {
1663 3efd8e31 2022-10-23 thomas sym->used = 1;
1664 3efd8e31 2022-10-23 thomas return (sym->val);
1665 3efd8e31 2022-10-23 thomas }
1666 3efd8e31 2022-10-23 thomas }
1667 3efd8e31 2022-10-23 thomas return (NULL);
1668 5dcb3a43 2023-04-01 thomas }
1669 5dcb3a43 2023-04-01 thomas
1670 5dcb3a43 2023-04-01 thomas struct gotd_repo *
1671 ce1bfad9 2024-03-30 thomas gotd_find_repo_by_name(const char *repo_name, struct gotd_repolist *repos)
1672 5dcb3a43 2023-04-01 thomas {
1673 5dcb3a43 2023-04-01 thomas struct gotd_repo *repo;
1674 5dcb3a43 2023-04-01 thomas size_t namelen;
1675 5dcb3a43 2023-04-01 thomas
1676 ce1bfad9 2024-03-30 thomas TAILQ_FOREACH(repo, repos, entry) {
1677 5dcb3a43 2023-04-01 thomas namelen = strlen(repo->name);
1678 5dcb3a43 2023-04-01 thomas if (strncmp(repo->name, repo_name, namelen) != 0)
1679 5dcb3a43 2023-04-01 thomas continue;
1680 5dcb3a43 2023-04-01 thomas if (repo_name[namelen] == '\0' ||
1681 5dcb3a43 2023-04-01 thomas strcmp(&repo_name[namelen], ".git") == 0)
1682 5dcb3a43 2023-04-01 thomas return repo;
1683 5dcb3a43 2023-04-01 thomas }
1684 5dcb3a43 2023-04-01 thomas
1685 5dcb3a43 2023-04-01 thomas return NULL;
1686 3efd8e31 2022-10-23 thomas }
1687 6d7eb4f7 2023-04-04 thomas
1688 6d7eb4f7 2023-04-04 thomas struct gotd_repo *
1689 6d7eb4f7 2023-04-04 thomas gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1690 6d7eb4f7 2023-04-04 thomas {
1691 6d7eb4f7 2023-04-04 thomas struct gotd_repo *repo;
1692 6d7eb4f7 2023-04-04 thomas
1693 6d7eb4f7 2023-04-04 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
1694 6d7eb4f7 2023-04-04 thomas if (strcmp(repo->path, repo_path) == 0)
1695 6d7eb4f7 2023-04-04 thomas return repo;
1696 65a36f17 2023-04-22 thomas }
1697 65a36f17 2023-04-22 thomas
1698 65a36f17 2023-04-22 thomas return NULL;
1699 65a36f17 2023-04-22 thomas }
1700 65a36f17 2023-04-22 thomas
1701 65a36f17 2023-04-22 thomas struct gotd_uid_connection_limit *
1702 65a36f17 2023-04-22 thomas gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
1703 65a36f17 2023-04-22 thomas size_t nlimits, uid_t uid)
1704 65a36f17 2023-04-22 thomas {
1705 65a36f17 2023-04-22 thomas /* This array is always sorted to allow for binary search. */
1706 65a36f17 2023-04-22 thomas int i, left = 0, right = nlimits - 1;
1707 65a36f17 2023-04-22 thomas
1708 65a36f17 2023-04-22 thomas while (left <= right) {
1709 65a36f17 2023-04-22 thomas i = ((left + right) / 2);
1710 65a36f17 2023-04-22 thomas if (limits[i].uid == uid)
1711 65a36f17 2023-04-22 thomas return &limits[i];
1712 65a36f17 2023-04-22 thomas if (limits[i].uid > uid)
1713 65a36f17 2023-04-22 thomas left = i + 1;
1714 65a36f17 2023-04-22 thomas else
1715 65a36f17 2023-04-22 thomas right = i - 1;
1716 6d7eb4f7 2023-04-04 thomas }
1717 6d7eb4f7 2023-04-04 thomas
1718 6d7eb4f7 2023-04-04 thomas return NULL;
1719 6d7eb4f7 2023-04-04 thomas }
1720 48488136 2023-04-22 thomas
1721 48488136 2023-04-22 thomas int
1722 48488136 2023-04-22 thomas gotd_parseuid(const char *s, uid_t *uid)
1723 48488136 2023-04-22 thomas {
1724 48488136 2023-04-22 thomas struct passwd *pw;
1725 48488136 2023-04-22 thomas const char *errstr;
1726 48488136 2023-04-22 thomas
1727 48488136 2023-04-22 thomas if ((pw = getpwnam(s)) != NULL) {
1728 48488136 2023-04-22 thomas *uid = pw->pw_uid;
1729 48488136 2023-04-22 thomas if (*uid == UID_MAX)
1730 48488136 2023-04-22 thomas return -1;
1731 48488136 2023-04-22 thomas return 0;
1732 48488136 2023-04-22 thomas }
1733 48488136 2023-04-22 thomas *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1734 48488136 2023-04-22 thomas if (errstr)
1735 48488136 2023-04-22 thomas return -1;
1736 48488136 2023-04-22 thomas return 0;
1737 48488136 2023-04-22 thomas }
1738 ce1bfad9 2024-03-30 thomas
1739 ce1bfad9 2024-03-30 thomas const struct got_error *
1740 ce1bfad9 2024-03-30 thomas gotd_parse_url(char **proto, char **host, char **port,
1741 ce1bfad9 2024-03-30 thomas char **request_path, const char *url)
1742 ce1bfad9 2024-03-30 thomas {
1743 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
1744 ce1bfad9 2024-03-30 thomas char *s, *p, *q;
1745 ce1bfad9 2024-03-30 thomas
1746 ce1bfad9 2024-03-30 thomas *proto = *host = *port = *request_path = NULL;
1747 ce1bfad9 2024-03-30 thomas
1748 ce1bfad9 2024-03-30 thomas p = strstr(url, "://");
1749 ce1bfad9 2024-03-30 thomas if (!p)
1750 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PARSE_URI);
1751 ce1bfad9 2024-03-30 thomas
1752 ce1bfad9 2024-03-30 thomas *proto = strndup(url, p - url);
1753 ce1bfad9 2024-03-30 thomas if (*proto == NULL) {
1754 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strndup");
1755 ce1bfad9 2024-03-30 thomas goto done;
1756 ce1bfad9 2024-03-30 thomas }
1757 ce1bfad9 2024-03-30 thomas s = p + 3;
1758 ce1bfad9 2024-03-30 thomas
1759 ce1bfad9 2024-03-30 thomas p = strstr(s, "/");
1760 bc0cdda1 2024-03-30 thomas if (p == NULL) {
1761 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PARSE_URI);
1762 ce1bfad9 2024-03-30 thomas goto done;
1763 ce1bfad9 2024-03-30 thomas }
1764 ce1bfad9 2024-03-30 thomas
1765 ce1bfad9 2024-03-30 thomas q = memchr(s, ':', p - s);
1766 ce1bfad9 2024-03-30 thomas if (q) {
1767 ce1bfad9 2024-03-30 thomas *host = strndup(s, q - s);
1768 ce1bfad9 2024-03-30 thomas if (*host == NULL) {
1769 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strndup");
1770 ce1bfad9 2024-03-30 thomas goto done;
1771 ce1bfad9 2024-03-30 thomas }
1772 ce1bfad9 2024-03-30 thomas if ((*host)[0] == '\0') {
1773 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PARSE_URI);
1774 ce1bfad9 2024-03-30 thomas goto done;
1775 ce1bfad9 2024-03-30 thomas }
1776 ce1bfad9 2024-03-30 thomas *port = strndup(q + 1, p - (q + 1));
1777 ce1bfad9 2024-03-30 thomas if (*port == NULL) {
1778 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strndup");
1779 ce1bfad9 2024-03-30 thomas goto done;
1780 ce1bfad9 2024-03-30 thomas }
1781 ce1bfad9 2024-03-30 thomas if ((*port)[0] == '\0') {
1782 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PARSE_URI);
1783 ce1bfad9 2024-03-30 thomas goto done;
1784 ce1bfad9 2024-03-30 thomas }
1785 ce1bfad9 2024-03-30 thomas } else {
1786 ce1bfad9 2024-03-30 thomas *host = strndup(s, p - s);
1787 ce1bfad9 2024-03-30 thomas if (*host == NULL) {
1788 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strndup");
1789 ce1bfad9 2024-03-30 thomas goto done;
1790 ce1bfad9 2024-03-30 thomas }
1791 ce1bfad9 2024-03-30 thomas if ((*host)[0] == '\0') {
1792 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PARSE_URI);
1793 ce1bfad9 2024-03-30 thomas goto done;
1794 ce1bfad9 2024-03-30 thomas }
1795 ce1bfad9 2024-03-30 thomas }
1796 ce1bfad9 2024-03-30 thomas
1797 ce1bfad9 2024-03-30 thomas while (p[0] == '/' && p[1] == '/')
1798 ce1bfad9 2024-03-30 thomas p++;
1799 ce1bfad9 2024-03-30 thomas *request_path = strdup(p);
1800 ce1bfad9 2024-03-30 thomas if (*request_path == NULL) {
1801 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strdup");
1802 ce1bfad9 2024-03-30 thomas goto done;
1803 ce1bfad9 2024-03-30 thomas }
1804 ce1bfad9 2024-03-30 thomas if ((*request_path)[0] == '\0') {
1805 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PARSE_URI);
1806 ce1bfad9 2024-03-30 thomas goto done;
1807 ce1bfad9 2024-03-30 thomas }
1808 ce1bfad9 2024-03-30 thomas done:
1809 ce1bfad9 2024-03-30 thomas if (err) {
1810 ce1bfad9 2024-03-30 thomas free(*proto);
1811 ce1bfad9 2024-03-30 thomas *proto = NULL;
1812 ce1bfad9 2024-03-30 thomas free(*host);
1813 ce1bfad9 2024-03-30 thomas *host = NULL;
1814 ce1bfad9 2024-03-30 thomas free(*port);
1815 ce1bfad9 2024-03-30 thomas *port = NULL;
1816 ce1bfad9 2024-03-30 thomas free(*request_path);
1817 ce1bfad9 2024-03-30 thomas *request_path = NULL;
1818 ce1bfad9 2024-03-30 thomas }
1819 ce1bfad9 2024-03-30 thomas return err;
1820 ce1bfad9 2024-03-30 thomas }
1821 ce1bfad9 2024-03-30 thomas
1822 ce1bfad9 2024-03-30 thomas static char *
1823 ce1bfad9 2024-03-30 thomas port_sprintf(int p)
1824 ce1bfad9 2024-03-30 thomas {
1825 ce1bfad9 2024-03-30 thomas static char portno[32];
1826 ce1bfad9 2024-03-30 thomas int n;
1827 ce1bfad9 2024-03-30 thomas
1828 ce1bfad9 2024-03-30 thomas n = snprintf(portno, sizeof(portno), "%lld", (long long)p);
1829 ce1bfad9 2024-03-30 thomas if (n < 0 || (size_t)n >= sizeof(portno))
1830 ce1bfad9 2024-03-30 thomas fatalx("port number too long: %lld", (long long)p);
1831 ce1bfad9 2024-03-30 thomas
1832 ce1bfad9 2024-03-30 thomas return portno;
1833 ce1bfad9 2024-03-30 thomas }