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