Blame


1 13b2bc37 2022-10-23 stsp /*
2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 13b2bc37 2022-10-23 stsp * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
4 13b2bc37 2022-10-23 stsp * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 13b2bc37 2022-10-23 stsp * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 13b2bc37 2022-10-23 stsp * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 13b2bc37 2022-10-23 stsp * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 13b2bc37 2022-10-23 stsp * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 13b2bc37 2022-10-23 stsp * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 13b2bc37 2022-10-23 stsp *
11 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
12 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
13 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
14 13b2bc37 2022-10-23 stsp *
15 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 13b2bc37 2022-10-23 stsp */
23 13b2bc37 2022-10-23 stsp
24 13b2bc37 2022-10-23 stsp %{
25 13b2bc37 2022-10-23 stsp #include <sys/time.h>
26 13b2bc37 2022-10-23 stsp #include <sys/types.h>
27 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
28 13b2bc37 2022-10-23 stsp #include <sys/stat.h>
29 13b2bc37 2022-10-23 stsp
30 13b2bc37 2022-10-23 stsp #include <ctype.h>
31 13b2bc37 2022-10-23 stsp #include <err.h>
32 13b2bc37 2022-10-23 stsp #include <errno.h>
33 13b2bc37 2022-10-23 stsp #include <event.h>
34 13b2bc37 2022-10-23 stsp #include <imsg.h>
35 13b2bc37 2022-10-23 stsp #include <limits.h>
36 1963be61 2023-04-14 stsp #include <pwd.h>
37 13b2bc37 2022-10-23 stsp #include <sha1.h>
38 5822e79e 2023-02-23 op #include <sha2.h>
39 13b2bc37 2022-10-23 stsp #include <stdarg.h>
40 13b2bc37 2022-10-23 stsp #include <stdlib.h>
41 13b2bc37 2022-10-23 stsp #include <stdio.h>
42 13b2bc37 2022-10-23 stsp #include <string.h>
43 13b2bc37 2022-10-23 stsp #include <syslog.h>
44 13b2bc37 2022-10-23 stsp #include <unistd.h>
45 13b2bc37 2022-10-23 stsp
46 13b2bc37 2022-10-23 stsp #include "got_error.h"
47 13b2bc37 2022-10-23 stsp #include "got_path.h"
48 9afa3de2 2023-04-04 stsp #include "got_reference.h"
49 13b2bc37 2022-10-23 stsp
50 13b2bc37 2022-10-23 stsp #include "log.h"
51 13b2bc37 2022-10-23 stsp #include "gotd.h"
52 40b85cca 2023-01-03 stsp #include "auth.h"
53 40b85cca 2023-01-03 stsp #include "listen.h"
54 13b2bc37 2022-10-23 stsp
55 13b2bc37 2022-10-23 stsp TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
56 13b2bc37 2022-10-23 stsp static struct file {
57 13b2bc37 2022-10-23 stsp TAILQ_ENTRY(file) entry;
58 13b2bc37 2022-10-23 stsp FILE *stream;
59 13b2bc37 2022-10-23 stsp char *name;
60 13b2bc37 2022-10-23 stsp int lineno;
61 13b2bc37 2022-10-23 stsp int errors;
62 13b2bc37 2022-10-23 stsp } *file;
63 e9e0377f 2023-03-29 stsp struct file *newfile(const char *, int, int);
64 13b2bc37 2022-10-23 stsp static void closefile(struct file *);
65 13b2bc37 2022-10-23 stsp int check_file_secrecy(int, const char *);
66 13b2bc37 2022-10-23 stsp int yyparse(void);
67 13b2bc37 2022-10-23 stsp int yylex(void);
68 13b2bc37 2022-10-23 stsp int yyerror(const char *, ...)
69 13b2bc37 2022-10-23 stsp __attribute__((__format__ (printf, 1, 2)))
70 13b2bc37 2022-10-23 stsp __attribute__((__nonnull__ (1)));
71 13b2bc37 2022-10-23 stsp int kw_cmp(const void *, const void *);
72 13b2bc37 2022-10-23 stsp int lookup(char *);
73 13b2bc37 2022-10-23 stsp int lgetc(int);
74 13b2bc37 2022-10-23 stsp int lungetc(int);
75 13b2bc37 2022-10-23 stsp int findeol(void);
76 13b2bc37 2022-10-23 stsp
77 13b2bc37 2022-10-23 stsp TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
78 13b2bc37 2022-10-23 stsp struct sym {
79 13b2bc37 2022-10-23 stsp TAILQ_ENTRY(sym) entry;
80 13b2bc37 2022-10-23 stsp int used;
81 13b2bc37 2022-10-23 stsp int persist;
82 13b2bc37 2022-10-23 stsp char *nam;
83 13b2bc37 2022-10-23 stsp char *val;
84 13b2bc37 2022-10-23 stsp };
85 13b2bc37 2022-10-23 stsp
86 13b2bc37 2022-10-23 stsp int symset(const char *, const char *, int);
87 13b2bc37 2022-10-23 stsp char *symget(const char *);
88 13b2bc37 2022-10-23 stsp
89 13b2bc37 2022-10-23 stsp static int errors;
90 13b2bc37 2022-10-23 stsp
91 13b2bc37 2022-10-23 stsp static struct gotd *gotd;
92 13b2bc37 2022-10-23 stsp static struct gotd_repo *new_repo;
93 fc89c900 2023-01-03 op static int conf_limit_user_connections(const char *, int);
94 13b2bc37 2022-10-23 stsp static struct gotd_repo *conf_new_repo(const char *);
95 533abaaa 2022-11-18 op static void conf_new_access_rule(struct gotd_repo *,
96 533abaaa 2022-11-18 op enum gotd_access, int, char *);
97 f850236e 2023-04-05 stsp static int conf_protect_ref_namespace(char **,
98 9afa3de2 2023-04-04 stsp struct got_pathlist_head *, char *);
99 9afa3de2 2023-04-04 stsp static int conf_protect_tag_namespace(struct gotd_repo *,
100 9afa3de2 2023-04-04 stsp char *);
101 9afa3de2 2023-04-04 stsp static int conf_protect_branch_namespace(
102 9afa3de2 2023-04-04 stsp struct gotd_repo *, char *);
103 9afa3de2 2023-04-04 stsp static int conf_protect_branch(struct gotd_repo *,
104 9afa3de2 2023-04-04 stsp char *);
105 13b2bc37 2022-10-23 stsp static enum gotd_procid gotd_proc_id;
106 13b2bc37 2022-10-23 stsp
107 13b2bc37 2022-10-23 stsp typedef struct {
108 13b2bc37 2022-10-23 stsp union {
109 13b2bc37 2022-10-23 stsp long long number;
110 13b2bc37 2022-10-23 stsp char *string;
111 40b85cca 2023-01-03 stsp struct timeval tv;
112 13b2bc37 2022-10-23 stsp } v;
113 13b2bc37 2022-10-23 stsp int lineno;
114 13b2bc37 2022-10-23 stsp } YYSTYPE;
115 13b2bc37 2022-10-23 stsp
116 13b2bc37 2022-10-23 stsp %}
117 13b2bc37 2022-10-23 stsp
118 83577462 2023-01-05 stsp %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
119 40b85cca 2023-01-03 stsp %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
120 9afa3de2 2023-04-04 stsp %token PROTECT NAMESPACE BRANCH TAG
121 13b2bc37 2022-10-23 stsp
122 13b2bc37 2022-10-23 stsp %token <v.string> STRING
123 13b2bc37 2022-10-23 stsp %token <v.number> NUMBER
124 40b85cca 2023-01-03 stsp %type <v.tv> timeout
125 13b2bc37 2022-10-23 stsp
126 13b2bc37 2022-10-23 stsp %%
127 13b2bc37 2022-10-23 stsp
128 13b2bc37 2022-10-23 stsp grammar :
129 13b2bc37 2022-10-23 stsp | grammar '\n'
130 13b2bc37 2022-10-23 stsp | grammar main '\n'
131 13b2bc37 2022-10-23 stsp | grammar repository '\n'
132 13b2bc37 2022-10-23 stsp ;
133 13b2bc37 2022-10-23 stsp
134 40b85cca 2023-01-03 stsp timeout : NUMBER {
135 40b85cca 2023-01-03 stsp if ($1 < 0) {
136 40b85cca 2023-01-03 stsp yyerror("invalid timeout: %lld", $1);
137 40b85cca 2023-01-03 stsp YYERROR;
138 40b85cca 2023-01-03 stsp }
139 40b85cca 2023-01-03 stsp $$.tv_sec = $1;
140 40b85cca 2023-01-03 stsp $$.tv_usec = 0;
141 40b85cca 2023-01-03 stsp }
142 2be11cde 2023-01-03 op | STRING {
143 2be11cde 2023-01-03 op const char *errstr;
144 2be11cde 2023-01-03 op const char *type = "seconds";
145 2be11cde 2023-01-03 op size_t len;
146 2be11cde 2023-01-03 op int mul = 1;
147 2be11cde 2023-01-03 op
148 2be11cde 2023-01-03 op if (*$1 == '\0') {
149 2be11cde 2023-01-03 op yyerror("invalid number of seconds: %s", $1);
150 2be11cde 2023-01-03 op free($1);
151 2be11cde 2023-01-03 op YYERROR;
152 2be11cde 2023-01-03 op }
153 2be11cde 2023-01-03 op
154 2be11cde 2023-01-03 op len = strlen($1);
155 2be11cde 2023-01-03 op switch ($1[len - 1]) {
156 2be11cde 2023-01-03 op case 'S':
157 2be11cde 2023-01-03 op case 's':
158 2be11cde 2023-01-03 op $1[len - 1] = '\0';
159 2be11cde 2023-01-03 op break;
160 2be11cde 2023-01-03 op case 'M':
161 2be11cde 2023-01-03 op case 'm':
162 2be11cde 2023-01-03 op type = "minutes";
163 2be11cde 2023-01-03 op mul = 60;
164 2be11cde 2023-01-03 op $1[len - 1] = '\0';
165 2be11cde 2023-01-03 op break;
166 2be11cde 2023-01-03 op case 'H':
167 2be11cde 2023-01-03 op case 'h':
168 2be11cde 2023-01-03 op type = "hours";
169 2be11cde 2023-01-03 op mul = 60 * 60;
170 2be11cde 2023-01-03 op $1[len - 1] = '\0';
171 2be11cde 2023-01-03 op break;
172 2be11cde 2023-01-03 op }
173 2be11cde 2023-01-03 op
174 2be11cde 2023-01-03 op $$.tv_usec = 0;
175 71cd355c 2023-01-03 op $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
176 2be11cde 2023-01-03 op if (errstr) {
177 2be11cde 2023-01-03 op yyerror("number of %s is %s: %s", type,
178 2be11cde 2023-01-03 op errstr, $1);
179 2be11cde 2023-01-03 op free($1);
180 2be11cde 2023-01-03 op YYERROR;
181 2be11cde 2023-01-03 op }
182 2be11cde 2023-01-03 op
183 2be11cde 2023-01-03 op $$.tv_sec *= mul;
184 2be11cde 2023-01-03 op free($1);
185 2be11cde 2023-01-03 op }
186 40b85cca 2023-01-03 stsp ;
187 40b85cca 2023-01-03 stsp
188 83577462 2023-01-05 stsp main : LISTEN ON STRING {
189 abe844e2 2023-01-18 op if (!got_path_is_absolute($3))
190 abe844e2 2023-01-18 op yyerror("bad unix socket path \"%s\": "
191 abe844e2 2023-01-18 op "must be an absolute path", $3);
192 abe844e2 2023-01-18 op
193 d93ecf7d 2022-12-14 stsp if (gotd_proc_id == PROC_LISTEN) {
194 83577462 2023-01-05 stsp if (strlcpy(gotd->unix_socket_path, $3,
195 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) >=
196 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) {
197 13b2bc37 2022-10-23 stsp yyerror("%s: unix socket path too long",
198 13b2bc37 2022-10-23 stsp __func__);
199 83577462 2023-01-05 stsp free($3);
200 13b2bc37 2022-10-23 stsp YYERROR;
201 13b2bc37 2022-10-23 stsp }
202 13b2bc37 2022-10-23 stsp }
203 83577462 2023-01-05 stsp free($3);
204 13b2bc37 2022-10-23 stsp }
205 13b2bc37 2022-10-23 stsp | USER STRING {
206 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->user_name, $2,
207 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) >=
208 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) {
209 13b2bc37 2022-10-23 stsp yyerror("%s: user name too long", __func__);
210 13b2bc37 2022-10-23 stsp free($2);
211 13b2bc37 2022-10-23 stsp YYERROR;
212 13b2bc37 2022-10-23 stsp }
213 13b2bc37 2022-10-23 stsp free($2);
214 13b2bc37 2022-10-23 stsp }
215 40b85cca 2023-01-03 stsp | connection
216 13b2bc37 2022-10-23 stsp ;
217 13b2bc37 2022-10-23 stsp
218 40b85cca 2023-01-03 stsp connection : CONNECTION '{' optnl conflags_l '}'
219 40b85cca 2023-01-03 stsp | CONNECTION conflags
220 40b85cca 2023-01-03 stsp
221 40b85cca 2023-01-03 stsp conflags_l : conflags optnl conflags_l
222 40b85cca 2023-01-03 stsp | conflags optnl
223 40b85cca 2023-01-03 stsp ;
224 40b85cca 2023-01-03 stsp
225 40b85cca 2023-01-03 stsp conflags : REQUEST TIMEOUT timeout {
226 46e48ac7 2023-01-03 stsp if ($3.tv_sec <= 0) {
227 46e48ac7 2023-01-03 stsp yyerror("invalid timeout: %lld", $3.tv_sec);
228 46e48ac7 2023-01-03 stsp YYERROR;
229 46e48ac7 2023-01-03 stsp }
230 40b85cca 2023-01-03 stsp memcpy(&gotd->request_timeout, &$3,
231 40b85cca 2023-01-03 stsp sizeof(gotd->request_timeout));
232 40b85cca 2023-01-03 stsp }
233 40b85cca 2023-01-03 stsp | LIMIT USER STRING NUMBER {
234 40b85cca 2023-01-03 stsp if (gotd_proc_id == PROC_LISTEN &&
235 40b85cca 2023-01-03 stsp conf_limit_user_connections($3, $4) == -1) {
236 40b85cca 2023-01-03 stsp free($3);
237 40b85cca 2023-01-03 stsp YYERROR;
238 40b85cca 2023-01-03 stsp }
239 40b85cca 2023-01-03 stsp free($3);
240 40b85cca 2023-01-03 stsp }
241 40b85cca 2023-01-03 stsp ;
242 40b85cca 2023-01-03 stsp
243 9afa3de2 2023-04-04 stsp protect : PROTECT '{' optnl protectflags_l '}'
244 9afa3de2 2023-04-04 stsp | PROTECT protectflags
245 9afa3de2 2023-04-04 stsp
246 9afa3de2 2023-04-04 stsp protectflags_l : protectflags optnl protectflags_l
247 9afa3de2 2023-04-04 stsp | protectflags optnl
248 9afa3de2 2023-04-04 stsp ;
249 9afa3de2 2023-04-04 stsp
250 9afa3de2 2023-04-04 stsp protectflags : TAG NAMESPACE STRING {
251 9afa3de2 2023-04-04 stsp if (gotd_proc_id == PROC_GOTD ||
252 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
253 9afa3de2 2023-04-04 stsp if (conf_protect_tag_namespace(new_repo, $3)) {
254 9afa3de2 2023-04-04 stsp free($3);
255 9afa3de2 2023-04-04 stsp YYERROR;
256 9afa3de2 2023-04-04 stsp }
257 9afa3de2 2023-04-04 stsp }
258 584140c2 2023-04-05 op free($3);
259 9afa3de2 2023-04-04 stsp }
260 9afa3de2 2023-04-04 stsp | BRANCH NAMESPACE STRING {
261 9afa3de2 2023-04-04 stsp if (gotd_proc_id == PROC_GOTD ||
262 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
263 9afa3de2 2023-04-04 stsp if (conf_protect_branch_namespace(new_repo,
264 9afa3de2 2023-04-04 stsp $3)) {
265 9afa3de2 2023-04-04 stsp free($3);
266 9afa3de2 2023-04-04 stsp YYERROR;
267 9afa3de2 2023-04-04 stsp }
268 9afa3de2 2023-04-04 stsp }
269 584140c2 2023-04-05 op free($3);
270 9afa3de2 2023-04-04 stsp }
271 9afa3de2 2023-04-04 stsp | BRANCH STRING {
272 9afa3de2 2023-04-04 stsp if (gotd_proc_id == PROC_GOTD ||
273 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
274 9afa3de2 2023-04-04 stsp if (conf_protect_branch(new_repo, $2)) {
275 9afa3de2 2023-04-04 stsp free($2);
276 9afa3de2 2023-04-04 stsp YYERROR;
277 9afa3de2 2023-04-04 stsp }
278 9afa3de2 2023-04-04 stsp }
279 584140c2 2023-04-05 op free($2);
280 9afa3de2 2023-04-04 stsp }
281 9afa3de2 2023-04-04 stsp ;
282 9afa3de2 2023-04-04 stsp
283 13b2bc37 2022-10-23 stsp repository : REPOSITORY STRING {
284 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
285 13b2bc37 2022-10-23 stsp
286 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
287 13b2bc37 2022-10-23 stsp if (strcmp(repo->name, $2) == 0) {
288 13b2bc37 2022-10-23 stsp yyerror("duplicate repository '%s'", $2);
289 13b2bc37 2022-10-23 stsp free($2);
290 13b2bc37 2022-10-23 stsp YYERROR;
291 13b2bc37 2022-10-23 stsp }
292 13b2bc37 2022-10-23 stsp }
293 13b2bc37 2022-10-23 stsp
294 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_GOTD ||
295 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_AUTH ||
296 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
297 13b2bc37 2022-10-23 stsp new_repo = conf_new_repo($2);
298 13b2bc37 2022-10-23 stsp }
299 13b2bc37 2022-10-23 stsp free($2);
300 13b2bc37 2022-10-23 stsp } '{' optnl repoopts2 '}' {
301 13b2bc37 2022-10-23 stsp }
302 13b2bc37 2022-10-23 stsp ;
303 13b2bc37 2022-10-23 stsp
304 13b2bc37 2022-10-23 stsp repoopts1 : PATH STRING {
305 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_GOTD ||
306 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_AUTH ||
307 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
308 13b2bc37 2022-10-23 stsp if (!got_path_is_absolute($2)) {
309 13b2bc37 2022-10-23 stsp yyerror("%s: path %s is not absolute",
310 13b2bc37 2022-10-23 stsp __func__, $2);
311 13b2bc37 2022-10-23 stsp free($2);
312 13b2bc37 2022-10-23 stsp YYERROR;
313 13b2bc37 2022-10-23 stsp }
314 9b7f22a6 2023-01-08 stsp if (realpath($2, new_repo->path) == NULL) {
315 859316d0 2023-04-12 stsp yyerror("realpath %s: %s", $2,
316 859316d0 2023-04-12 stsp strerror(errno));
317 859316d0 2023-04-12 stsp /*
318 859316d0 2023-04-12 stsp * Give admin a chance to create
319 859316d0 2023-04-12 stsp * missing repositories at run-time.
320 859316d0 2023-04-12 stsp */
321 859316d0 2023-04-12 stsp if (errno != ENOENT) {
322 859316d0 2023-04-12 stsp free($2);
323 859316d0 2023-04-12 stsp YYERROR;
324 859316d0 2023-04-12 stsp } else if (strlcpy(new_repo->path, $2,
325 859316d0 2023-04-12 stsp sizeof(new_repo->path)) >=
326 859316d0 2023-04-12 stsp sizeof(new_repo->path))
327 859316d0 2023-04-12 stsp yyerror("path too long");
328 13b2bc37 2022-10-23 stsp }
329 13b2bc37 2022-10-23 stsp }
330 13b2bc37 2022-10-23 stsp free($2);
331 0ccf3acb 2022-11-16 stsp }
332 0ccf3acb 2022-11-16 stsp | PERMIT RO STRING {
333 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
334 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
335 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
336 584140c2 2023-04-05 op } else
337 584140c2 2023-04-05 op free($3);
338 0ccf3acb 2022-11-16 stsp }
339 0ccf3acb 2022-11-16 stsp | PERMIT RW STRING {
340 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
341 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
342 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_PERMITTED,
343 0ccf3acb 2022-11-16 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
344 584140c2 2023-04-05 op } else
345 584140c2 2023-04-05 op free($3);
346 13b2bc37 2022-10-23 stsp }
347 0ccf3acb 2022-11-16 stsp | DENY STRING {
348 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
349 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
350 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_DENIED, 0, $2);
351 584140c2 2023-04-05 op } else
352 584140c2 2023-04-05 op free($2);
353 0ccf3acb 2022-11-16 stsp }
354 9afa3de2 2023-04-04 stsp | protect
355 13b2bc37 2022-10-23 stsp ;
356 13b2bc37 2022-10-23 stsp
357 13b2bc37 2022-10-23 stsp repoopts2 : repoopts2 repoopts1 nl
358 13b2bc37 2022-10-23 stsp | repoopts1 optnl
359 13b2bc37 2022-10-23 stsp ;
360 13b2bc37 2022-10-23 stsp
361 13b2bc37 2022-10-23 stsp nl : '\n' optnl
362 13b2bc37 2022-10-23 stsp ;
363 13b2bc37 2022-10-23 stsp
364 13b2bc37 2022-10-23 stsp optnl : '\n' optnl /* zero or more newlines */
365 13b2bc37 2022-10-23 stsp | /* empty */
366 13b2bc37 2022-10-23 stsp ;
367 13b2bc37 2022-10-23 stsp
368 13b2bc37 2022-10-23 stsp %%
369 13b2bc37 2022-10-23 stsp
370 13b2bc37 2022-10-23 stsp struct keywords {
371 13b2bc37 2022-10-23 stsp const char *k_name;
372 13b2bc37 2022-10-23 stsp int k_val;
373 13b2bc37 2022-10-23 stsp };
374 13b2bc37 2022-10-23 stsp
375 13b2bc37 2022-10-23 stsp int
376 13b2bc37 2022-10-23 stsp yyerror(const char *fmt, ...)
377 13b2bc37 2022-10-23 stsp {
378 13b2bc37 2022-10-23 stsp va_list ap;
379 13b2bc37 2022-10-23 stsp char *msg;
380 13b2bc37 2022-10-23 stsp
381 13b2bc37 2022-10-23 stsp file->errors++;
382 13b2bc37 2022-10-23 stsp va_start(ap, fmt);
383 13b2bc37 2022-10-23 stsp if (vasprintf(&msg, fmt, ap) == -1)
384 13b2bc37 2022-10-23 stsp fatalx("yyerror vasprintf");
385 13b2bc37 2022-10-23 stsp va_end(ap);
386 13b2bc37 2022-10-23 stsp logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
387 13b2bc37 2022-10-23 stsp free(msg);
388 13b2bc37 2022-10-23 stsp return (0);
389 13b2bc37 2022-10-23 stsp }
390 13b2bc37 2022-10-23 stsp
391 13b2bc37 2022-10-23 stsp int
392 13b2bc37 2022-10-23 stsp kw_cmp(const void *k, const void *e)
393 13b2bc37 2022-10-23 stsp {
394 13b2bc37 2022-10-23 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
395 13b2bc37 2022-10-23 stsp }
396 13b2bc37 2022-10-23 stsp
397 13b2bc37 2022-10-23 stsp int
398 13b2bc37 2022-10-23 stsp lookup(char *s)
399 13b2bc37 2022-10-23 stsp {
400 13b2bc37 2022-10-23 stsp /* This has to be sorted always. */
401 13b2bc37 2022-10-23 stsp static const struct keywords keywords[] = {
402 9afa3de2 2023-04-04 stsp { "branch", BRANCH },
403 40b85cca 2023-01-03 stsp { "connection", CONNECTION },
404 0ccf3acb 2022-11-16 stsp { "deny", DENY },
405 40b85cca 2023-01-03 stsp { "limit", LIMIT },
406 83577462 2023-01-05 stsp { "listen", LISTEN },
407 9afa3de2 2023-04-04 stsp { "namespace", NAMESPACE },
408 13b2bc37 2022-10-23 stsp { "on", ON },
409 13b2bc37 2022-10-23 stsp { "path", PATH },
410 0ccf3acb 2022-11-16 stsp { "permit", PERMIT },
411 9afa3de2 2023-04-04 stsp { "protect", PROTECT },
412 13b2bc37 2022-10-23 stsp { "repository", REPOSITORY },
413 40b85cca 2023-01-03 stsp { "request", REQUEST },
414 0ccf3acb 2022-11-16 stsp { "ro", RO },
415 0ccf3acb 2022-11-16 stsp { "rw", RW },
416 9afa3de2 2023-04-04 stsp { "tag", TAG },
417 40b85cca 2023-01-03 stsp { "timeout", TIMEOUT },
418 13b2bc37 2022-10-23 stsp { "user", USER },
419 13b2bc37 2022-10-23 stsp };
420 13b2bc37 2022-10-23 stsp const struct keywords *p;
421 13b2bc37 2022-10-23 stsp
422 13b2bc37 2022-10-23 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
423 13b2bc37 2022-10-23 stsp sizeof(keywords[0]), kw_cmp);
424 13b2bc37 2022-10-23 stsp
425 13b2bc37 2022-10-23 stsp if (p)
426 13b2bc37 2022-10-23 stsp return (p->k_val);
427 13b2bc37 2022-10-23 stsp else
428 13b2bc37 2022-10-23 stsp return (STRING);
429 13b2bc37 2022-10-23 stsp }
430 13b2bc37 2022-10-23 stsp
431 13b2bc37 2022-10-23 stsp #define MAXPUSHBACK 128
432 13b2bc37 2022-10-23 stsp
433 13b2bc37 2022-10-23 stsp unsigned char *parsebuf;
434 13b2bc37 2022-10-23 stsp int parseindex;
435 13b2bc37 2022-10-23 stsp unsigned char pushback_buffer[MAXPUSHBACK];
436 13b2bc37 2022-10-23 stsp int pushback_index = 0;
437 13b2bc37 2022-10-23 stsp
438 13b2bc37 2022-10-23 stsp int
439 13b2bc37 2022-10-23 stsp lgetc(int quotec)
440 13b2bc37 2022-10-23 stsp {
441 13b2bc37 2022-10-23 stsp int c, next;
442 13b2bc37 2022-10-23 stsp
443 13b2bc37 2022-10-23 stsp if (parsebuf) {
444 13b2bc37 2022-10-23 stsp /* Read character from the parsebuffer instead of input. */
445 13b2bc37 2022-10-23 stsp if (parseindex >= 0) {
446 13b2bc37 2022-10-23 stsp c = parsebuf[parseindex++];
447 13b2bc37 2022-10-23 stsp if (c != '\0')
448 13b2bc37 2022-10-23 stsp return (c);
449 13b2bc37 2022-10-23 stsp parsebuf = NULL;
450 13b2bc37 2022-10-23 stsp } else
451 13b2bc37 2022-10-23 stsp parseindex++;
452 13b2bc37 2022-10-23 stsp }
453 13b2bc37 2022-10-23 stsp
454 13b2bc37 2022-10-23 stsp if (pushback_index)
455 13b2bc37 2022-10-23 stsp return (pushback_buffer[--pushback_index]);
456 13b2bc37 2022-10-23 stsp
457 13b2bc37 2022-10-23 stsp if (quotec) {
458 13b2bc37 2022-10-23 stsp c = getc(file->stream);
459 13b2bc37 2022-10-23 stsp if (c == EOF)
460 13b2bc37 2022-10-23 stsp yyerror("reached end of file while parsing "
461 13b2bc37 2022-10-23 stsp "quoted string");
462 13b2bc37 2022-10-23 stsp return (c);
463 13b2bc37 2022-10-23 stsp }
464 13b2bc37 2022-10-23 stsp
465 13b2bc37 2022-10-23 stsp c = getc(file->stream);
466 13b2bc37 2022-10-23 stsp while (c == '\\') {
467 13b2bc37 2022-10-23 stsp next = getc(file->stream);
468 13b2bc37 2022-10-23 stsp if (next != '\n') {
469 13b2bc37 2022-10-23 stsp c = next;
470 13b2bc37 2022-10-23 stsp break;
471 13b2bc37 2022-10-23 stsp }
472 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
473 13b2bc37 2022-10-23 stsp file->lineno++;
474 13b2bc37 2022-10-23 stsp c = getc(file->stream);
475 13b2bc37 2022-10-23 stsp }
476 13b2bc37 2022-10-23 stsp
477 13b2bc37 2022-10-23 stsp return (c);
478 13b2bc37 2022-10-23 stsp }
479 13b2bc37 2022-10-23 stsp
480 13b2bc37 2022-10-23 stsp int
481 13b2bc37 2022-10-23 stsp lungetc(int c)
482 13b2bc37 2022-10-23 stsp {
483 13b2bc37 2022-10-23 stsp if (c == EOF)
484 13b2bc37 2022-10-23 stsp return (EOF);
485 13b2bc37 2022-10-23 stsp if (parsebuf) {
486 13b2bc37 2022-10-23 stsp parseindex--;
487 13b2bc37 2022-10-23 stsp if (parseindex >= 0)
488 13b2bc37 2022-10-23 stsp return (c);
489 13b2bc37 2022-10-23 stsp }
490 13b2bc37 2022-10-23 stsp if (pushback_index < MAXPUSHBACK-1)
491 13b2bc37 2022-10-23 stsp return (pushback_buffer[pushback_index++] = c);
492 13b2bc37 2022-10-23 stsp else
493 13b2bc37 2022-10-23 stsp return (EOF);
494 13b2bc37 2022-10-23 stsp }
495 13b2bc37 2022-10-23 stsp
496 13b2bc37 2022-10-23 stsp int
497 13b2bc37 2022-10-23 stsp findeol(void)
498 13b2bc37 2022-10-23 stsp {
499 13b2bc37 2022-10-23 stsp int c;
500 13b2bc37 2022-10-23 stsp
501 13b2bc37 2022-10-23 stsp parsebuf = NULL;
502 13b2bc37 2022-10-23 stsp
503 13b2bc37 2022-10-23 stsp /* Skip to either EOF or the first real EOL. */
504 13b2bc37 2022-10-23 stsp while (1) {
505 13b2bc37 2022-10-23 stsp if (pushback_index)
506 13b2bc37 2022-10-23 stsp c = pushback_buffer[--pushback_index];
507 13b2bc37 2022-10-23 stsp else
508 13b2bc37 2022-10-23 stsp c = lgetc(0);
509 13b2bc37 2022-10-23 stsp if (c == '\n') {
510 13b2bc37 2022-10-23 stsp file->lineno++;
511 13b2bc37 2022-10-23 stsp break;
512 13b2bc37 2022-10-23 stsp }
513 13b2bc37 2022-10-23 stsp if (c == EOF)
514 13b2bc37 2022-10-23 stsp break;
515 13b2bc37 2022-10-23 stsp }
516 13b2bc37 2022-10-23 stsp return (ERROR);
517 13b2bc37 2022-10-23 stsp }
518 13b2bc37 2022-10-23 stsp
519 13b2bc37 2022-10-23 stsp int
520 13b2bc37 2022-10-23 stsp yylex(void)
521 13b2bc37 2022-10-23 stsp {
522 13b2bc37 2022-10-23 stsp unsigned char buf[8096];
523 13b2bc37 2022-10-23 stsp unsigned char *p, *val;
524 13b2bc37 2022-10-23 stsp int quotec, next, c;
525 13b2bc37 2022-10-23 stsp int token;
526 13b2bc37 2022-10-23 stsp
527 13b2bc37 2022-10-23 stsp top:
528 13b2bc37 2022-10-23 stsp p = buf;
529 13b2bc37 2022-10-23 stsp c = lgetc(0);
530 13b2bc37 2022-10-23 stsp while (c == ' ' || c == '\t')
531 13b2bc37 2022-10-23 stsp c = lgetc(0); /* nothing */
532 13b2bc37 2022-10-23 stsp
533 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
534 13b2bc37 2022-10-23 stsp if (c == '#') {
535 13b2bc37 2022-10-23 stsp c = lgetc(0);
536 13b2bc37 2022-10-23 stsp while (c != '\n' && c != EOF)
537 13b2bc37 2022-10-23 stsp c = lgetc(0); /* nothing */
538 13b2bc37 2022-10-23 stsp }
539 13b2bc37 2022-10-23 stsp if (c == '$' && parsebuf == NULL) {
540 13b2bc37 2022-10-23 stsp while (1) {
541 13b2bc37 2022-10-23 stsp c = lgetc(0);
542 13b2bc37 2022-10-23 stsp if (c == EOF)
543 13b2bc37 2022-10-23 stsp return (0);
544 13b2bc37 2022-10-23 stsp
545 13b2bc37 2022-10-23 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
546 13b2bc37 2022-10-23 stsp yyerror("string too long");
547 13b2bc37 2022-10-23 stsp return (findeol());
548 13b2bc37 2022-10-23 stsp }
549 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == '_') {
550 13b2bc37 2022-10-23 stsp *p++ = c;
551 13b2bc37 2022-10-23 stsp continue;
552 13b2bc37 2022-10-23 stsp }
553 13b2bc37 2022-10-23 stsp *p = '\0';
554 13b2bc37 2022-10-23 stsp lungetc(c);
555 13b2bc37 2022-10-23 stsp break;
556 13b2bc37 2022-10-23 stsp }
557 13b2bc37 2022-10-23 stsp val = symget(buf);
558 13b2bc37 2022-10-23 stsp if (val == NULL) {
559 13b2bc37 2022-10-23 stsp yyerror("macro '%s' not defined", buf);
560 13b2bc37 2022-10-23 stsp return (findeol());
561 13b2bc37 2022-10-23 stsp }
562 13b2bc37 2022-10-23 stsp parsebuf = val;
563 13b2bc37 2022-10-23 stsp parseindex = 0;
564 13b2bc37 2022-10-23 stsp goto top;
565 13b2bc37 2022-10-23 stsp }
566 13b2bc37 2022-10-23 stsp
567 13b2bc37 2022-10-23 stsp switch (c) {
568 13b2bc37 2022-10-23 stsp case '\'':
569 13b2bc37 2022-10-23 stsp case '"':
570 13b2bc37 2022-10-23 stsp quotec = c;
571 13b2bc37 2022-10-23 stsp while (1) {
572 13b2bc37 2022-10-23 stsp c = lgetc(quotec);
573 13b2bc37 2022-10-23 stsp if (c == EOF)
574 13b2bc37 2022-10-23 stsp return (0);
575 13b2bc37 2022-10-23 stsp if (c == '\n') {
576 13b2bc37 2022-10-23 stsp file->lineno++;
577 13b2bc37 2022-10-23 stsp continue;
578 13b2bc37 2022-10-23 stsp } else if (c == '\\') {
579 13b2bc37 2022-10-23 stsp next = lgetc(quotec);
580 13b2bc37 2022-10-23 stsp if (next == EOF)
581 13b2bc37 2022-10-23 stsp return (0);
582 13b2bc37 2022-10-23 stsp if (next == quotec || c == ' ' || c == '\t')
583 13b2bc37 2022-10-23 stsp c = next;
584 13b2bc37 2022-10-23 stsp else if (next == '\n') {
585 13b2bc37 2022-10-23 stsp file->lineno++;
586 13b2bc37 2022-10-23 stsp continue;
587 13b2bc37 2022-10-23 stsp } else
588 13b2bc37 2022-10-23 stsp lungetc(next);
589 13b2bc37 2022-10-23 stsp } else if (c == quotec) {
590 13b2bc37 2022-10-23 stsp *p = '\0';
591 13b2bc37 2022-10-23 stsp break;
592 13b2bc37 2022-10-23 stsp } else if (c == '\0') {
593 13b2bc37 2022-10-23 stsp yyerror("syntax error");
594 13b2bc37 2022-10-23 stsp return (findeol());
595 13b2bc37 2022-10-23 stsp }
596 13b2bc37 2022-10-23 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
597 13b2bc37 2022-10-23 stsp yyerror("string too long");
598 13b2bc37 2022-10-23 stsp return (findeol());
599 13b2bc37 2022-10-23 stsp }
600 13b2bc37 2022-10-23 stsp *p++ = c;
601 13b2bc37 2022-10-23 stsp }
602 13b2bc37 2022-10-23 stsp yylval.v.string = strdup(buf);
603 13b2bc37 2022-10-23 stsp if (yylval.v.string == NULL)
604 13b2bc37 2022-10-23 stsp err(1, "yylex: strdup");
605 13b2bc37 2022-10-23 stsp return (STRING);
606 13b2bc37 2022-10-23 stsp }
607 13b2bc37 2022-10-23 stsp
608 13b2bc37 2022-10-23 stsp #define allowed_to_end_number(x) \
609 13b2bc37 2022-10-23 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
610 13b2bc37 2022-10-23 stsp
611 13b2bc37 2022-10-23 stsp if (c == '-' || isdigit(c)) {
612 13b2bc37 2022-10-23 stsp do {
613 13b2bc37 2022-10-23 stsp *p++ = c;
614 13b2bc37 2022-10-23 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
615 13b2bc37 2022-10-23 stsp yyerror("string too long");
616 13b2bc37 2022-10-23 stsp return (findeol());
617 13b2bc37 2022-10-23 stsp }
618 13b2bc37 2022-10-23 stsp c = lgetc(0);
619 13b2bc37 2022-10-23 stsp } while (c != EOF && isdigit(c));
620 13b2bc37 2022-10-23 stsp lungetc(c);
621 13b2bc37 2022-10-23 stsp if (p == buf + 1 && buf[0] == '-')
622 13b2bc37 2022-10-23 stsp goto nodigits;
623 13b2bc37 2022-10-23 stsp if (c == EOF || allowed_to_end_number(c)) {
624 13b2bc37 2022-10-23 stsp const char *errstr = NULL;
625 13b2bc37 2022-10-23 stsp
626 13b2bc37 2022-10-23 stsp *p = '\0';
627 13b2bc37 2022-10-23 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
628 13b2bc37 2022-10-23 stsp LLONG_MAX, &errstr);
629 13b2bc37 2022-10-23 stsp if (errstr) {
630 13b2bc37 2022-10-23 stsp yyerror("\"%s\" invalid number: %s",
631 13b2bc37 2022-10-23 stsp buf, errstr);
632 13b2bc37 2022-10-23 stsp return (findeol());
633 13b2bc37 2022-10-23 stsp }
634 13b2bc37 2022-10-23 stsp return (NUMBER);
635 13b2bc37 2022-10-23 stsp } else {
636 13b2bc37 2022-10-23 stsp nodigits:
637 13b2bc37 2022-10-23 stsp while (p > buf + 1)
638 13b2bc37 2022-10-23 stsp lungetc(*--p);
639 13b2bc37 2022-10-23 stsp c = *--p;
640 13b2bc37 2022-10-23 stsp if (c == '-')
641 13b2bc37 2022-10-23 stsp return (c);
642 13b2bc37 2022-10-23 stsp }
643 13b2bc37 2022-10-23 stsp }
644 13b2bc37 2022-10-23 stsp
645 13b2bc37 2022-10-23 stsp #define allowed_in_string(x) \
646 13b2bc37 2022-10-23 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
647 13b2bc37 2022-10-23 stsp x != '{' && x != '}' && \
648 13b2bc37 2022-10-23 stsp x != '!' && x != '=' && x != '#' && \
649 13b2bc37 2022-10-23 stsp x != ','))
650 13b2bc37 2022-10-23 stsp
651 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == ':' || c == '_') {
652 13b2bc37 2022-10-23 stsp do {
653 13b2bc37 2022-10-23 stsp *p++ = c;
654 13b2bc37 2022-10-23 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
655 13b2bc37 2022-10-23 stsp yyerror("string too long");
656 13b2bc37 2022-10-23 stsp return (findeol());
657 13b2bc37 2022-10-23 stsp }
658 13b2bc37 2022-10-23 stsp c = lgetc(0);
659 13b2bc37 2022-10-23 stsp } while (c != EOF && (allowed_in_string(c)));
660 13b2bc37 2022-10-23 stsp lungetc(c);
661 13b2bc37 2022-10-23 stsp *p = '\0';
662 13b2bc37 2022-10-23 stsp token = lookup(buf);
663 13b2bc37 2022-10-23 stsp if (token == STRING) {
664 13b2bc37 2022-10-23 stsp yylval.v.string = strdup(buf);
665 13b2bc37 2022-10-23 stsp if (yylval.v.string == NULL)
666 13b2bc37 2022-10-23 stsp err(1, "yylex: strdup");
667 13b2bc37 2022-10-23 stsp }
668 13b2bc37 2022-10-23 stsp return (token);
669 13b2bc37 2022-10-23 stsp }
670 13b2bc37 2022-10-23 stsp if (c == '\n') {
671 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
672 13b2bc37 2022-10-23 stsp file->lineno++;
673 13b2bc37 2022-10-23 stsp }
674 13b2bc37 2022-10-23 stsp if (c == EOF)
675 13b2bc37 2022-10-23 stsp return (0);
676 13b2bc37 2022-10-23 stsp return (c);
677 13b2bc37 2022-10-23 stsp }
678 13b2bc37 2022-10-23 stsp
679 13b2bc37 2022-10-23 stsp int
680 13b2bc37 2022-10-23 stsp check_file_secrecy(int fd, const char *fname)
681 13b2bc37 2022-10-23 stsp {
682 13b2bc37 2022-10-23 stsp struct stat st;
683 13b2bc37 2022-10-23 stsp
684 13b2bc37 2022-10-23 stsp if (fstat(fd, &st)) {
685 13b2bc37 2022-10-23 stsp log_warn("cannot stat %s", fname);
686 13b2bc37 2022-10-23 stsp return (-1);
687 13b2bc37 2022-10-23 stsp }
688 13b2bc37 2022-10-23 stsp if (st.st_uid != 0 && st.st_uid != getuid()) {
689 13b2bc37 2022-10-23 stsp log_warnx("%s: owner not root or current user", fname);
690 13b2bc37 2022-10-23 stsp return (-1);
691 13b2bc37 2022-10-23 stsp }
692 13b2bc37 2022-10-23 stsp if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
693 13b2bc37 2022-10-23 stsp log_warnx("%s: group writable or world read/writable", fname);
694 13b2bc37 2022-10-23 stsp return (-1);
695 13b2bc37 2022-10-23 stsp }
696 13b2bc37 2022-10-23 stsp return (0);
697 13b2bc37 2022-10-23 stsp }
698 13b2bc37 2022-10-23 stsp
699 13b2bc37 2022-10-23 stsp struct file *
700 e9e0377f 2023-03-29 stsp newfile(const char *name, int secret, int required)
701 13b2bc37 2022-10-23 stsp {
702 13b2bc37 2022-10-23 stsp struct file *nfile;
703 13b2bc37 2022-10-23 stsp
704 13b2bc37 2022-10-23 stsp nfile = calloc(1, sizeof(struct file));
705 13b2bc37 2022-10-23 stsp if (nfile == NULL) {
706 13b2bc37 2022-10-23 stsp log_warn("calloc");
707 13b2bc37 2022-10-23 stsp return (NULL);
708 13b2bc37 2022-10-23 stsp }
709 13b2bc37 2022-10-23 stsp nfile->name = strdup(name);
710 13b2bc37 2022-10-23 stsp if (nfile->name == NULL) {
711 13b2bc37 2022-10-23 stsp log_warn("strdup");
712 13b2bc37 2022-10-23 stsp free(nfile);
713 13b2bc37 2022-10-23 stsp return (NULL);
714 13b2bc37 2022-10-23 stsp }
715 13b2bc37 2022-10-23 stsp nfile->stream = fopen(nfile->name, "r");
716 13b2bc37 2022-10-23 stsp if (nfile->stream == NULL) {
717 e9e0377f 2023-03-29 stsp if (required)
718 e9e0377f 2023-03-29 stsp log_warn("open %s", nfile->name);
719 13b2bc37 2022-10-23 stsp free(nfile->name);
720 13b2bc37 2022-10-23 stsp free(nfile);
721 13b2bc37 2022-10-23 stsp return (NULL);
722 13b2bc37 2022-10-23 stsp } else if (secret &&
723 13b2bc37 2022-10-23 stsp check_file_secrecy(fileno(nfile->stream), nfile->name)) {
724 13b2bc37 2022-10-23 stsp fclose(nfile->stream);
725 13b2bc37 2022-10-23 stsp free(nfile->name);
726 13b2bc37 2022-10-23 stsp free(nfile);
727 13b2bc37 2022-10-23 stsp return (NULL);
728 13b2bc37 2022-10-23 stsp }
729 13b2bc37 2022-10-23 stsp nfile->lineno = 1;
730 13b2bc37 2022-10-23 stsp return (nfile);
731 13b2bc37 2022-10-23 stsp }
732 13b2bc37 2022-10-23 stsp
733 13b2bc37 2022-10-23 stsp static void
734 13b2bc37 2022-10-23 stsp closefile(struct file *xfile)
735 13b2bc37 2022-10-23 stsp {
736 13b2bc37 2022-10-23 stsp fclose(xfile->stream);
737 13b2bc37 2022-10-23 stsp free(xfile->name);
738 13b2bc37 2022-10-23 stsp free(xfile);
739 13b2bc37 2022-10-23 stsp }
740 13b2bc37 2022-10-23 stsp
741 13b2bc37 2022-10-23 stsp int
742 13b2bc37 2022-10-23 stsp parse_config(const char *filename, enum gotd_procid proc_id,
743 e9e0377f 2023-03-29 stsp struct gotd *env, int require_config_file)
744 13b2bc37 2022-10-23 stsp {
745 13b2bc37 2022-10-23 stsp struct sym *sym, *next;
746 3b706203 2023-01-02 stsp struct gotd_repo *repo;
747 13b2bc37 2022-10-23 stsp
748 13b2bc37 2022-10-23 stsp memset(env, 0, sizeof(*env));
749 13b2bc37 2022-10-23 stsp
750 13b2bc37 2022-10-23 stsp gotd = env;
751 13b2bc37 2022-10-23 stsp gotd_proc_id = proc_id;
752 13b2bc37 2022-10-23 stsp TAILQ_INIT(&gotd->repos);
753 13b2bc37 2022-10-23 stsp
754 13b2bc37 2022-10-23 stsp /* Apply default values. */
755 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
756 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
757 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: unix socket path too long", __func__);
758 13b2bc37 2022-10-23 stsp return -1;
759 13b2bc37 2022-10-23 stsp }
760 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->user_name, GOTD_USER,
761 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
762 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: user name too long", __func__);
763 13b2bc37 2022-10-23 stsp return -1;
764 13b2bc37 2022-10-23 stsp }
765 40b85cca 2023-01-03 stsp
766 40b85cca 2023-01-03 stsp gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
767 40b85cca 2023-01-03 stsp gotd->request_timeout.tv_usec = 0;
768 13b2bc37 2022-10-23 stsp
769 e9e0377f 2023-03-29 stsp file = newfile(filename, 0, require_config_file);
770 3fa763e5 2023-03-01 stsp if (file == NULL)
771 e9e0377f 2023-03-29 stsp return require_config_file ? -1 : 0;
772 13b2bc37 2022-10-23 stsp
773 13b2bc37 2022-10-23 stsp yyparse();
774 13b2bc37 2022-10-23 stsp errors = file->errors;
775 13b2bc37 2022-10-23 stsp closefile(file);
776 13b2bc37 2022-10-23 stsp
777 13b2bc37 2022-10-23 stsp /* Free macros and check which have not been used. */
778 13b2bc37 2022-10-23 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
779 13b2bc37 2022-10-23 stsp if ((gotd->verbosity > 1) && !sym->used)
780 13b2bc37 2022-10-23 stsp fprintf(stderr, "warning: macro '%s' not used\n",
781 13b2bc37 2022-10-23 stsp sym->nam);
782 13b2bc37 2022-10-23 stsp if (!sym->persist) {
783 13b2bc37 2022-10-23 stsp free(sym->nam);
784 13b2bc37 2022-10-23 stsp free(sym->val);
785 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
786 13b2bc37 2022-10-23 stsp free(sym);
787 13b2bc37 2022-10-23 stsp }
788 13b2bc37 2022-10-23 stsp }
789 13b2bc37 2022-10-23 stsp
790 13b2bc37 2022-10-23 stsp if (errors)
791 13b2bc37 2022-10-23 stsp return (-1);
792 13b2bc37 2022-10-23 stsp
793 3b706203 2023-01-02 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
794 3b706203 2023-01-02 stsp if (repo->path[0] == '\0') {
795 2507ffb7 2023-01-02 stsp log_warnx("repository \"%s\": no path provided in "
796 2507ffb7 2023-01-02 stsp "configuration file", repo->name);
797 3b706203 2023-01-02 stsp return (-1);
798 3b706203 2023-01-02 stsp }
799 3b706203 2023-01-02 stsp }
800 3b706203 2023-01-02 stsp
801 809a54db 2023-01-18 op if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
802 809a54db 2023-01-18 op log_warnx("no repository defined in configuration file");
803 809a54db 2023-01-18 op return (-1);
804 809a54db 2023-01-18 op }
805 809a54db 2023-01-18 op
806 13b2bc37 2022-10-23 stsp return (0);
807 13b2bc37 2022-10-23 stsp }
808 13b2bc37 2022-10-23 stsp
809 40b85cca 2023-01-03 stsp static int
810 40b85cca 2023-01-03 stsp uid_connection_limit_cmp(const void *pa, const void *pb)
811 40b85cca 2023-01-03 stsp {
812 40b85cca 2023-01-03 stsp const struct gotd_uid_connection_limit *a = pa, *b = pb;
813 40b85cca 2023-01-03 stsp
814 40b85cca 2023-01-03 stsp if (a->uid < b->uid)
815 40b85cca 2023-01-03 stsp return -1;
816 40b85cca 2023-01-03 stsp else if (a->uid > b->uid);
817 40b85cca 2023-01-03 stsp return 1;
818 40b85cca 2023-01-03 stsp
819 40b85cca 2023-01-03 stsp return 0;
820 40b85cca 2023-01-03 stsp }
821 40b85cca 2023-01-03 stsp
822 40b85cca 2023-01-03 stsp static int
823 40b85cca 2023-01-03 stsp conf_limit_user_connections(const char *user, int maximum)
824 40b85cca 2023-01-03 stsp {
825 40b85cca 2023-01-03 stsp uid_t uid;
826 40b85cca 2023-01-03 stsp struct gotd_uid_connection_limit *limit;
827 40b85cca 2023-01-03 stsp size_t nlimits;
828 40b85cca 2023-01-03 stsp
829 40b85cca 2023-01-03 stsp if (maximum < 1) {
830 40b85cca 2023-01-03 stsp yyerror("max connections cannot be smaller 1");
831 40b85cca 2023-01-03 stsp return -1;
832 40b85cca 2023-01-03 stsp }
833 40b85cca 2023-01-03 stsp if (maximum > GOTD_MAXCLIENTS) {
834 40b85cca 2023-01-03 stsp yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
835 40b85cca 2023-01-03 stsp return -1;
836 40b85cca 2023-01-03 stsp }
837 40b85cca 2023-01-03 stsp
838 1963be61 2023-04-14 stsp if (gotd_parseuid(user, &uid) == -1) {
839 40b85cca 2023-01-03 stsp yyerror("%s: no such user", user);
840 40b85cca 2023-01-03 stsp return -1;
841 40b85cca 2023-01-03 stsp }
842 40b85cca 2023-01-03 stsp
843 40b85cca 2023-01-03 stsp limit = gotd_find_uid_connection_limit(gotd->connection_limits,
844 40b85cca 2023-01-03 stsp gotd->nconnection_limits, uid);
845 40b85cca 2023-01-03 stsp if (limit) {
846 40b85cca 2023-01-03 stsp limit->max_connections = maximum;
847 40b85cca 2023-01-03 stsp return 0;
848 40b85cca 2023-01-03 stsp }
849 40b85cca 2023-01-03 stsp
850 40b85cca 2023-01-03 stsp limit = gotd->connection_limits;
851 40b85cca 2023-01-03 stsp nlimits = gotd->nconnection_limits + 1;
852 40b85cca 2023-01-03 stsp limit = reallocarray(limit, nlimits, sizeof(*limit));
853 40b85cca 2023-01-03 stsp if (limit == NULL)
854 40b85cca 2023-01-03 stsp fatal("reallocarray");
855 40b85cca 2023-01-03 stsp
856 40b85cca 2023-01-03 stsp limit[nlimits - 1].uid = uid;
857 40b85cca 2023-01-03 stsp limit[nlimits - 1].max_connections = maximum;
858 40b85cca 2023-01-03 stsp
859 40b85cca 2023-01-03 stsp gotd->connection_limits = limit;
860 40b85cca 2023-01-03 stsp gotd->nconnection_limits = nlimits;
861 40b85cca 2023-01-03 stsp qsort(gotd->connection_limits, gotd->nconnection_limits,
862 40b85cca 2023-01-03 stsp sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
863 40b85cca 2023-01-03 stsp
864 40b85cca 2023-01-03 stsp return 0;
865 40b85cca 2023-01-03 stsp }
866 40b85cca 2023-01-03 stsp
867 13b2bc37 2022-10-23 stsp static struct gotd_repo *
868 13b2bc37 2022-10-23 stsp conf_new_repo(const char *name)
869 13b2bc37 2022-10-23 stsp {
870 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
871 13b2bc37 2022-10-23 stsp
872 7683f79a 2023-01-02 stsp if (name[0] == '\0') {
873 7683f79a 2023-01-02 stsp fatalx("syntax error: empty repository name found in %s",
874 7683f79a 2023-01-02 stsp file->name);
875 7683f79a 2023-01-02 stsp }
876 7683f79a 2023-01-02 stsp
877 2507ffb7 2023-01-02 stsp if (strchr(name, '\n') != NULL)
878 2507ffb7 2023-01-02 stsp fatalx("repository names must not contain linefeeds: %s", name);
879 13b2bc37 2022-10-23 stsp
880 13b2bc37 2022-10-23 stsp repo = calloc(1, sizeof(*repo));
881 13b2bc37 2022-10-23 stsp if (repo == NULL)
882 13b2bc37 2022-10-23 stsp fatalx("%s: calloc", __func__);
883 13b2bc37 2022-10-23 stsp
884 0ccf3acb 2022-11-16 stsp STAILQ_INIT(&repo->rules);
885 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_tag_namespaces);
886 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_branch_namespaces);
887 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_branches);
888 0ccf3acb 2022-11-16 stsp
889 13b2bc37 2022-10-23 stsp if (strlcpy(repo->name, name, sizeof(repo->name)) >=
890 13b2bc37 2022-10-23 stsp sizeof(repo->name))
891 13b2bc37 2022-10-23 stsp fatalx("%s: strlcpy", __func__);
892 13b2bc37 2022-10-23 stsp
893 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
894 13b2bc37 2022-10-23 stsp gotd->nrepos++;
895 13b2bc37 2022-10-23 stsp
896 13b2bc37 2022-10-23 stsp return repo;
897 13b2bc37 2022-10-23 stsp };
898 0ccf3acb 2022-11-16 stsp
899 0ccf3acb 2022-11-16 stsp static void
900 0ccf3acb 2022-11-16 stsp conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
901 0ccf3acb 2022-11-16 stsp int authorization, char *identifier)
902 0ccf3acb 2022-11-16 stsp {
903 0ccf3acb 2022-11-16 stsp struct gotd_access_rule *rule;
904 0ccf3acb 2022-11-16 stsp
905 0ccf3acb 2022-11-16 stsp rule = calloc(1, sizeof(*rule));
906 0ccf3acb 2022-11-16 stsp if (rule == NULL)
907 0ccf3acb 2022-11-16 stsp fatal("calloc");
908 13b2bc37 2022-10-23 stsp
909 0ccf3acb 2022-11-16 stsp rule->access = access;
910 0ccf3acb 2022-11-16 stsp rule->authorization = authorization;
911 0ccf3acb 2022-11-16 stsp rule->identifier = identifier;
912 0ccf3acb 2022-11-16 stsp
913 0ccf3acb 2022-11-16 stsp STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
914 0ccf3acb 2022-11-16 stsp }
915 0ccf3acb 2022-11-16 stsp
916 9afa3de2 2023-04-04 stsp static int
917 9afa3de2 2023-04-04 stsp refname_is_valid(char *refname)
918 9afa3de2 2023-04-04 stsp {
919 9afa3de2 2023-04-04 stsp if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
920 9afa3de2 2023-04-04 stsp yyerror("reference name must begin with \"refs/\": %s",
921 9afa3de2 2023-04-04 stsp refname);
922 9afa3de2 2023-04-04 stsp return 0;
923 9afa3de2 2023-04-04 stsp }
924 9afa3de2 2023-04-04 stsp
925 9afa3de2 2023-04-04 stsp if (!got_ref_name_is_valid(refname)) {
926 9afa3de2 2023-04-04 stsp yyerror("invalid reference name: %s", refname);
927 9afa3de2 2023-04-04 stsp return 0;
928 9afa3de2 2023-04-04 stsp }
929 9afa3de2 2023-04-04 stsp
930 9afa3de2 2023-04-04 stsp return 1;
931 9afa3de2 2023-04-04 stsp }
932 9afa3de2 2023-04-04 stsp
933 9afa3de2 2023-04-04 stsp static int
934 f850236e 2023-04-05 stsp conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
935 f850236e 2023-04-05 stsp char *namespace)
936 9afa3de2 2023-04-04 stsp {
937 9afa3de2 2023-04-04 stsp const struct got_error *error;
938 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
939 9afa3de2 2023-04-04 stsp char *s;
940 9afa3de2 2023-04-04 stsp
941 f850236e 2023-04-05 stsp *new = NULL;
942 f850236e 2023-04-05 stsp
943 9afa3de2 2023-04-04 stsp got_path_strip_trailing_slashes(namespace);
944 9afa3de2 2023-04-04 stsp if (!refname_is_valid(namespace))
945 9afa3de2 2023-04-04 stsp return -1;
946 9afa3de2 2023-04-04 stsp if (asprintf(&s, "%s/", namespace) == -1) {
947 9afa3de2 2023-04-04 stsp yyerror("asprintf: %s", strerror(errno));
948 9afa3de2 2023-04-04 stsp return -1;
949 9afa3de2 2023-04-04 stsp }
950 9afa3de2 2023-04-04 stsp
951 f850236e 2023-04-05 stsp error = got_pathlist_insert(&pe, refs, s, NULL);
952 f850236e 2023-04-05 stsp if (error || pe == NULL) {
953 f0426190 2023-04-05 op free(s);
954 f0426190 2023-04-05 op if (error)
955 f0426190 2023-04-05 op yyerror("got_pathlist_insert: %s", error->msg);
956 f0426190 2023-04-05 op else
957 6be067ce 2023-04-06 stsp yyerror("duplicate protected namespace %s", namespace);
958 9afa3de2 2023-04-04 stsp return -1;
959 9afa3de2 2023-04-04 stsp }
960 9afa3de2 2023-04-04 stsp
961 f850236e 2023-04-05 stsp *new = s;
962 9afa3de2 2023-04-04 stsp return 0;
963 9afa3de2 2023-04-04 stsp }
964 9afa3de2 2023-04-04 stsp
965 9afa3de2 2023-04-04 stsp static int
966 9afa3de2 2023-04-04 stsp conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
967 9afa3de2 2023-04-04 stsp {
968 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
969 f850236e 2023-04-05 stsp char *new;
970 f850236e 2023-04-05 stsp
971 f850236e 2023-04-05 stsp if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
972 f850236e 2023-04-05 stsp namespace) == -1)
973 f850236e 2023-04-05 stsp return -1;
974 f850236e 2023-04-05 stsp
975 f850236e 2023-04-05 stsp TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
976 f850236e 2023-04-05 stsp if (strcmp(pe->path, new) == 0) {
977 6be067ce 2023-04-06 stsp yyerror("duplicate protected namespace %s", namespace);
978 f850236e 2023-04-05 stsp return -1;
979 f850236e 2023-04-05 stsp }
980 f850236e 2023-04-05 stsp }
981 f850236e 2023-04-05 stsp
982 f850236e 2023-04-05 stsp return 0;
983 9afa3de2 2023-04-04 stsp }
984 9afa3de2 2023-04-04 stsp
985 9afa3de2 2023-04-04 stsp static int
986 9afa3de2 2023-04-04 stsp conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
987 9afa3de2 2023-04-04 stsp {
988 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
989 f850236e 2023-04-05 stsp char *new;
990 f850236e 2023-04-05 stsp
991 f850236e 2023-04-05 stsp if (conf_protect_ref_namespace(&new,
992 f850236e 2023-04-05 stsp &repo->protected_branch_namespaces, namespace) == -1)
993 f850236e 2023-04-05 stsp return -1;
994 f850236e 2023-04-05 stsp
995 f850236e 2023-04-05 stsp TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
996 f850236e 2023-04-05 stsp if (strcmp(pe->path, new) == 0) {
997 6be067ce 2023-04-06 stsp yyerror("duplicate protected namespace %s", namespace);
998 f850236e 2023-04-05 stsp return -1;
999 f850236e 2023-04-05 stsp }
1000 f850236e 2023-04-05 stsp }
1001 f850236e 2023-04-05 stsp
1002 f850236e 2023-04-05 stsp return 0;
1003 9afa3de2 2023-04-04 stsp }
1004 9afa3de2 2023-04-04 stsp
1005 9afa3de2 2023-04-04 stsp static int
1006 9afa3de2 2023-04-04 stsp conf_protect_branch(struct gotd_repo *repo, char *branchname)
1007 9afa3de2 2023-04-04 stsp {
1008 9afa3de2 2023-04-04 stsp const struct got_error *error;
1009 f0426190 2023-04-05 op struct got_pathlist_entry *new;
1010 9afa3de2 2023-04-04 stsp char *refname;
1011 9afa3de2 2023-04-04 stsp
1012 9afa3de2 2023-04-04 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
1013 9afa3de2 2023-04-04 stsp if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1014 9afa3de2 2023-04-04 stsp yyerror("asprintf: %s", strerror(errno));
1015 9afa3de2 2023-04-04 stsp return -1;
1016 9afa3de2 2023-04-04 stsp }
1017 9afa3de2 2023-04-04 stsp } else {
1018 9afa3de2 2023-04-04 stsp refname = strdup(branchname);
1019 9afa3de2 2023-04-04 stsp if (refname == NULL) {
1020 9afa3de2 2023-04-04 stsp yyerror("strdup: %s", strerror(errno));
1021 9afa3de2 2023-04-04 stsp return -1;
1022 9afa3de2 2023-04-04 stsp }
1023 9afa3de2 2023-04-04 stsp }
1024 9afa3de2 2023-04-04 stsp
1025 9afa3de2 2023-04-04 stsp if (!refname_is_valid(refname)) {
1026 9afa3de2 2023-04-04 stsp free(refname);
1027 9afa3de2 2023-04-04 stsp return -1;
1028 9afa3de2 2023-04-04 stsp }
1029 9afa3de2 2023-04-04 stsp
1030 f0426190 2023-04-05 op error = got_pathlist_insert(&new, &repo->protected_branches,
1031 9afa3de2 2023-04-04 stsp refname, NULL);
1032 f0426190 2023-04-05 op if (error || new == NULL) {
1033 f0426190 2023-04-05 op free(refname);
1034 f0426190 2023-04-05 op if (error)
1035 f0426190 2023-04-05 op yyerror("got_pathlist_insert: %s", error->msg);
1036 f0426190 2023-04-05 op else
1037 f0426190 2023-04-05 op yyerror("duplicate protect branch %s", branchname);
1038 9afa3de2 2023-04-04 stsp return -1;
1039 9afa3de2 2023-04-04 stsp }
1040 9afa3de2 2023-04-04 stsp
1041 9afa3de2 2023-04-04 stsp return 0;
1042 9afa3de2 2023-04-04 stsp }
1043 9afa3de2 2023-04-04 stsp
1044 13b2bc37 2022-10-23 stsp int
1045 13b2bc37 2022-10-23 stsp symset(const char *nam, const char *val, int persist)
1046 13b2bc37 2022-10-23 stsp {
1047 13b2bc37 2022-10-23 stsp struct sym *sym;
1048 13b2bc37 2022-10-23 stsp
1049 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
1050 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0)
1051 13b2bc37 2022-10-23 stsp break;
1052 13b2bc37 2022-10-23 stsp }
1053 13b2bc37 2022-10-23 stsp
1054 13b2bc37 2022-10-23 stsp if (sym != NULL) {
1055 13b2bc37 2022-10-23 stsp if (sym->persist == 1)
1056 13b2bc37 2022-10-23 stsp return (0);
1057 13b2bc37 2022-10-23 stsp else {
1058 13b2bc37 2022-10-23 stsp free(sym->nam);
1059 13b2bc37 2022-10-23 stsp free(sym->val);
1060 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
1061 13b2bc37 2022-10-23 stsp free(sym);
1062 13b2bc37 2022-10-23 stsp }
1063 13b2bc37 2022-10-23 stsp }
1064 13b2bc37 2022-10-23 stsp sym = calloc(1, sizeof(*sym));
1065 13b2bc37 2022-10-23 stsp if (sym == NULL)
1066 13b2bc37 2022-10-23 stsp return (-1);
1067 13b2bc37 2022-10-23 stsp
1068 13b2bc37 2022-10-23 stsp sym->nam = strdup(nam);
1069 13b2bc37 2022-10-23 stsp if (sym->nam == NULL) {
1070 13b2bc37 2022-10-23 stsp free(sym);
1071 13b2bc37 2022-10-23 stsp return (-1);
1072 13b2bc37 2022-10-23 stsp }
1073 13b2bc37 2022-10-23 stsp sym->val = strdup(val);
1074 13b2bc37 2022-10-23 stsp if (sym->val == NULL) {
1075 13b2bc37 2022-10-23 stsp free(sym->nam);
1076 13b2bc37 2022-10-23 stsp free(sym);
1077 13b2bc37 2022-10-23 stsp return (-1);
1078 13b2bc37 2022-10-23 stsp }
1079 13b2bc37 2022-10-23 stsp sym->used = 0;
1080 13b2bc37 2022-10-23 stsp sym->persist = persist;
1081 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
1082 13b2bc37 2022-10-23 stsp return (0);
1083 13b2bc37 2022-10-23 stsp }
1084 13b2bc37 2022-10-23 stsp
1085 13b2bc37 2022-10-23 stsp char *
1086 13b2bc37 2022-10-23 stsp symget(const char *nam)
1087 13b2bc37 2022-10-23 stsp {
1088 13b2bc37 2022-10-23 stsp struct sym *sym;
1089 13b2bc37 2022-10-23 stsp
1090 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
1091 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0) {
1092 13b2bc37 2022-10-23 stsp sym->used = 1;
1093 13b2bc37 2022-10-23 stsp return (sym->val);
1094 13b2bc37 2022-10-23 stsp }
1095 13b2bc37 2022-10-23 stsp }
1096 13b2bc37 2022-10-23 stsp return (NULL);
1097 b09c1279 2023-03-28 stsp }
1098 b09c1279 2023-03-28 stsp
1099 b09c1279 2023-03-28 stsp struct gotd_repo *
1100 b09c1279 2023-03-28 stsp gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1101 b09c1279 2023-03-28 stsp {
1102 b09c1279 2023-03-28 stsp struct gotd_repo *repo;
1103 b09c1279 2023-03-28 stsp size_t namelen;
1104 b09c1279 2023-03-28 stsp
1105 b09c1279 2023-03-28 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
1106 b09c1279 2023-03-28 stsp namelen = strlen(repo->name);
1107 b09c1279 2023-03-28 stsp if (strncmp(repo->name, repo_name, namelen) != 0)
1108 b09c1279 2023-03-28 stsp continue;
1109 b09c1279 2023-03-28 stsp if (repo_name[namelen] == '\0' ||
1110 b09c1279 2023-03-28 stsp strcmp(&repo_name[namelen], ".git") == 0)
1111 b09c1279 2023-03-28 stsp return repo;
1112 b09c1279 2023-03-28 stsp }
1113 b09c1279 2023-03-28 stsp
1114 b09c1279 2023-03-28 stsp return NULL;
1115 13b2bc37 2022-10-23 stsp }
1116 9afa3de2 2023-04-04 stsp
1117 9afa3de2 2023-04-04 stsp struct gotd_repo *
1118 9afa3de2 2023-04-04 stsp gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1119 9afa3de2 2023-04-04 stsp {
1120 9afa3de2 2023-04-04 stsp struct gotd_repo *repo;
1121 9afa3de2 2023-04-04 stsp
1122 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
1123 9afa3de2 2023-04-04 stsp if (strcmp(repo->path, repo_path) == 0)
1124 9afa3de2 2023-04-04 stsp return repo;
1125 eeb616b7 2023-04-14 stsp }
1126 eeb616b7 2023-04-14 stsp
1127 eeb616b7 2023-04-14 stsp return NULL;
1128 eeb616b7 2023-04-14 stsp }
1129 eeb616b7 2023-04-14 stsp
1130 eeb616b7 2023-04-14 stsp struct gotd_uid_connection_limit *
1131 eeb616b7 2023-04-14 stsp gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
1132 eeb616b7 2023-04-14 stsp size_t nlimits, uid_t uid)
1133 eeb616b7 2023-04-14 stsp {
1134 eeb616b7 2023-04-14 stsp /* This array is always sorted to allow for binary search. */
1135 eeb616b7 2023-04-14 stsp int i, left = 0, right = nlimits - 1;
1136 eeb616b7 2023-04-14 stsp
1137 eeb616b7 2023-04-14 stsp while (left <= right) {
1138 eeb616b7 2023-04-14 stsp i = ((left + right) / 2);
1139 eeb616b7 2023-04-14 stsp if (limits[i].uid == uid)
1140 eeb616b7 2023-04-14 stsp return &limits[i];
1141 eeb616b7 2023-04-14 stsp if (limits[i].uid > uid)
1142 eeb616b7 2023-04-14 stsp left = i + 1;
1143 eeb616b7 2023-04-14 stsp else
1144 eeb616b7 2023-04-14 stsp right = i - 1;
1145 9afa3de2 2023-04-04 stsp }
1146 9afa3de2 2023-04-04 stsp
1147 9afa3de2 2023-04-04 stsp return NULL;
1148 9afa3de2 2023-04-04 stsp }
1149 1963be61 2023-04-14 stsp
1150 1963be61 2023-04-14 stsp int
1151 1963be61 2023-04-14 stsp gotd_parseuid(const char *s, uid_t *uid)
1152 1963be61 2023-04-14 stsp {
1153 1963be61 2023-04-14 stsp struct passwd *pw;
1154 1963be61 2023-04-14 stsp const char *errstr;
1155 1963be61 2023-04-14 stsp
1156 1963be61 2023-04-14 stsp if ((pw = getpwnam(s)) != NULL) {
1157 1963be61 2023-04-14 stsp *uid = pw->pw_uid;
1158 1963be61 2023-04-14 stsp if (*uid == UID_MAX)
1159 1963be61 2023-04-14 stsp return -1;
1160 1963be61 2023-04-14 stsp return 0;
1161 1963be61 2023-04-14 stsp }
1162 1963be61 2023-04-14 stsp *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1163 1963be61 2023-04-14 stsp if (errstr)
1164 1963be61 2023-04-14 stsp return -1;
1165 1963be61 2023-04-14 stsp return 0;
1166 1963be61 2023-04-14 stsp }