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 3efd8e31 2022-10-23 thomas #include <sys/time.h>
26 3efd8e31 2022-10-23 thomas #include <sys/types.h>
27 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
28 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
29 3efd8e31 2022-10-23 thomas
30 3efd8e31 2022-10-23 thomas #include <ctype.h>
31 3efd8e31 2022-10-23 thomas #include <err.h>
32 3efd8e31 2022-10-23 thomas #include <errno.h>
33 3efd8e31 2022-10-23 thomas #include <event.h>
34 3efd8e31 2022-10-23 thomas #include <imsg.h>
35 3efd8e31 2022-10-23 thomas #include <limits.h>
36 3efd8e31 2022-10-23 thomas #include <sha1.h>
37 3efd8e31 2022-10-23 thomas #include <stdarg.h>
38 3efd8e31 2022-10-23 thomas #include <stdlib.h>
39 3efd8e31 2022-10-23 thomas #include <stdio.h>
40 3efd8e31 2022-10-23 thomas #include <string.h>
41 3efd8e31 2022-10-23 thomas #include <syslog.h>
42 3efd8e31 2022-10-23 thomas #include <unistd.h>
43 3efd8e31 2022-10-23 thomas
44 3efd8e31 2022-10-23 thomas #include "got_error.h"
45 3efd8e31 2022-10-23 thomas #include "got_path.h"
46 3efd8e31 2022-10-23 thomas
47 3efd8e31 2022-10-23 thomas #include "log.h"
48 3efd8e31 2022-10-23 thomas #include "gotd.h"
49 0781db0e 2023-01-06 thomas #include "auth.h"
50 0781db0e 2023-01-06 thomas #include "listen.h"
51 3efd8e31 2022-10-23 thomas
52 3efd8e31 2022-10-23 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
53 3efd8e31 2022-10-23 thomas static struct file {
54 3efd8e31 2022-10-23 thomas TAILQ_ENTRY(file) entry;
55 3efd8e31 2022-10-23 thomas FILE *stream;
56 3efd8e31 2022-10-23 thomas char *name;
57 3efd8e31 2022-10-23 thomas int lineno;
58 3efd8e31 2022-10-23 thomas int errors;
59 3efd8e31 2022-10-23 thomas } *file;
60 3efd8e31 2022-10-23 thomas struct file *newfile(const char *, int);
61 3efd8e31 2022-10-23 thomas static void closefile(struct file *);
62 3efd8e31 2022-10-23 thomas int check_file_secrecy(int, const char *);
63 3efd8e31 2022-10-23 thomas int yyparse(void);
64 3efd8e31 2022-10-23 thomas int yylex(void);
65 3efd8e31 2022-10-23 thomas int yyerror(const char *, ...)
66 3efd8e31 2022-10-23 thomas __attribute__((__format__ (printf, 1, 2)))
67 3efd8e31 2022-10-23 thomas __attribute__((__nonnull__ (1)));
68 3efd8e31 2022-10-23 thomas int kw_cmp(const void *, const void *);
69 3efd8e31 2022-10-23 thomas int lookup(char *);
70 3efd8e31 2022-10-23 thomas int lgetc(int);
71 3efd8e31 2022-10-23 thomas int lungetc(int);
72 3efd8e31 2022-10-23 thomas int findeol(void);
73 3efd8e31 2022-10-23 thomas
74 3efd8e31 2022-10-23 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 3efd8e31 2022-10-23 thomas struct sym {
76 3efd8e31 2022-10-23 thomas TAILQ_ENTRY(sym) entry;
77 3efd8e31 2022-10-23 thomas int used;
78 3efd8e31 2022-10-23 thomas int persist;
79 3efd8e31 2022-10-23 thomas char *nam;
80 3efd8e31 2022-10-23 thomas char *val;
81 3efd8e31 2022-10-23 thomas };
82 3efd8e31 2022-10-23 thomas
83 3efd8e31 2022-10-23 thomas int symset(const char *, const char *, int);
84 3efd8e31 2022-10-23 thomas char *symget(const char *);
85 3efd8e31 2022-10-23 thomas
86 3efd8e31 2022-10-23 thomas static int errors;
87 3efd8e31 2022-10-23 thomas
88 3efd8e31 2022-10-23 thomas static struct gotd *gotd;
89 3efd8e31 2022-10-23 thomas static struct gotd_repo *new_repo;
90 67f822ee 2023-01-06 thomas static int conf_limit_user_connections(const char *, int);
91 3efd8e31 2022-10-23 thomas static struct gotd_repo *conf_new_repo(const char *);
92 c3841c67 2022-11-18 thomas static void conf_new_access_rule(struct gotd_repo *,
93 c3841c67 2022-11-18 thomas enum gotd_access, int, char *);
94 3efd8e31 2022-10-23 thomas static enum gotd_procid gotd_proc_id;
95 3efd8e31 2022-10-23 thomas
96 3efd8e31 2022-10-23 thomas typedef struct {
97 3efd8e31 2022-10-23 thomas union {
98 3efd8e31 2022-10-23 thomas long long number;
99 3efd8e31 2022-10-23 thomas char *string;
100 0781db0e 2023-01-06 thomas struct timeval tv;
101 3efd8e31 2022-10-23 thomas } v;
102 3efd8e31 2022-10-23 thomas int lineno;
103 3efd8e31 2022-10-23 thomas } YYSTYPE;
104 3efd8e31 2022-10-23 thomas
105 3efd8e31 2022-10-23 thomas %}
106 3efd8e31 2022-10-23 thomas
107 729a7e24 2022-11-17 thomas %token PATH ERROR ON UNIX_SOCKET UNIX_GROUP USER REPOSITORY PERMIT DENY
108 0781db0e 2023-01-06 thomas %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
109 3efd8e31 2022-10-23 thomas
110 3efd8e31 2022-10-23 thomas %token <v.string> STRING
111 3efd8e31 2022-10-23 thomas %token <v.number> NUMBER
112 3efd8e31 2022-10-23 thomas %type <v.number> boolean
113 0781db0e 2023-01-06 thomas %type <v.tv> timeout
114 3efd8e31 2022-10-23 thomas
115 3efd8e31 2022-10-23 thomas %%
116 3efd8e31 2022-10-23 thomas
117 3efd8e31 2022-10-23 thomas grammar :
118 3efd8e31 2022-10-23 thomas | grammar '\n'
119 3efd8e31 2022-10-23 thomas | grammar main '\n'
120 3efd8e31 2022-10-23 thomas | grammar repository '\n'
121 3efd8e31 2022-10-23 thomas ;
122 3efd8e31 2022-10-23 thomas
123 3efd8e31 2022-10-23 thomas boolean : STRING {
124 3efd8e31 2022-10-23 thomas if (strcasecmp($1, "1") == 0 ||
125 3efd8e31 2022-10-23 thomas strcasecmp($1, "yes") == 0 ||
126 3efd8e31 2022-10-23 thomas strcasecmp($1, "on") == 0)
127 3efd8e31 2022-10-23 thomas $$ = 1;
128 3efd8e31 2022-10-23 thomas else if (strcasecmp($1, "0") == 0 ||
129 3efd8e31 2022-10-23 thomas strcasecmp($1, "off") == 0 ||
130 3efd8e31 2022-10-23 thomas strcasecmp($1, "no") == 0)
131 3efd8e31 2022-10-23 thomas $$ = 0;
132 3efd8e31 2022-10-23 thomas else {
133 3efd8e31 2022-10-23 thomas yyerror("invalid boolean value '%s'", $1);
134 3efd8e31 2022-10-23 thomas free($1);
135 3efd8e31 2022-10-23 thomas YYERROR;
136 3efd8e31 2022-10-23 thomas }
137 3efd8e31 2022-10-23 thomas free($1);
138 3efd8e31 2022-10-23 thomas }
139 3efd8e31 2022-10-23 thomas | ON { $$ = 1; }
140 3efd8e31 2022-10-23 thomas | NUMBER { $$ = $1; }
141 3efd8e31 2022-10-23 thomas ;
142 3efd8e31 2022-10-23 thomas
143 0781db0e 2023-01-06 thomas timeout : NUMBER {
144 0781db0e 2023-01-06 thomas if ($1 < 0) {
145 0781db0e 2023-01-06 thomas yyerror("invalid timeout: %lld", $1);
146 0781db0e 2023-01-06 thomas YYERROR;
147 0781db0e 2023-01-06 thomas }
148 0781db0e 2023-01-06 thomas $$.tv_sec = $1;
149 0781db0e 2023-01-06 thomas $$.tv_usec = 0;
150 0781db0e 2023-01-06 thomas }
151 77d755e8 2023-01-06 thomas | STRING {
152 77d755e8 2023-01-06 thomas const char *errstr;
153 77d755e8 2023-01-06 thomas const char *type = "seconds";
154 77d755e8 2023-01-06 thomas size_t len;
155 77d755e8 2023-01-06 thomas int mul = 1;
156 77d755e8 2023-01-06 thomas
157 77d755e8 2023-01-06 thomas if (*$1 == '\0') {
158 77d755e8 2023-01-06 thomas yyerror("invalid number of seconds: %s", $1);
159 77d755e8 2023-01-06 thomas free($1);
160 77d755e8 2023-01-06 thomas YYERROR;
161 77d755e8 2023-01-06 thomas }
162 77d755e8 2023-01-06 thomas
163 77d755e8 2023-01-06 thomas len = strlen($1);
164 77d755e8 2023-01-06 thomas switch ($1[len - 1]) {
165 77d755e8 2023-01-06 thomas case 'S':
166 77d755e8 2023-01-06 thomas case 's':
167 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
168 77d755e8 2023-01-06 thomas break;
169 77d755e8 2023-01-06 thomas case 'M':
170 77d755e8 2023-01-06 thomas case 'm':
171 77d755e8 2023-01-06 thomas type = "minutes";
172 77d755e8 2023-01-06 thomas mul = 60;
173 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
174 77d755e8 2023-01-06 thomas break;
175 77d755e8 2023-01-06 thomas case 'H':
176 77d755e8 2023-01-06 thomas case 'h':
177 77d755e8 2023-01-06 thomas type = "hours";
178 77d755e8 2023-01-06 thomas mul = 60 * 60;
179 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
180 77d755e8 2023-01-06 thomas break;
181 77d755e8 2023-01-06 thomas }
182 77d755e8 2023-01-06 thomas
183 77d755e8 2023-01-06 thomas $$.tv_usec = 0;
184 85bccbf7 2023-01-06 thomas $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
185 77d755e8 2023-01-06 thomas if (errstr) {
186 77d755e8 2023-01-06 thomas yyerror("number of %s is %s: %s", type,
187 77d755e8 2023-01-06 thomas errstr, $1);
188 77d755e8 2023-01-06 thomas free($1);
189 77d755e8 2023-01-06 thomas YYERROR;
190 77d755e8 2023-01-06 thomas }
191 77d755e8 2023-01-06 thomas
192 77d755e8 2023-01-06 thomas $$.tv_sec *= mul;
193 77d755e8 2023-01-06 thomas free($1);
194 77d755e8 2023-01-06 thomas }
195 0781db0e 2023-01-06 thomas ;
196 0781db0e 2023-01-06 thomas
197 3efd8e31 2022-10-23 thomas main : UNIX_SOCKET STRING {
198 2b3d32a1 2022-12-30 thomas if (gotd_proc_id == PROC_LISTEN) {
199 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_socket_path, $2,
200 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >=
201 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) {
202 3efd8e31 2022-10-23 thomas yyerror("%s: unix socket path too long",
203 3efd8e31 2022-10-23 thomas __func__);
204 3efd8e31 2022-10-23 thomas free($2);
205 3efd8e31 2022-10-23 thomas YYERROR;
206 3efd8e31 2022-10-23 thomas }
207 3efd8e31 2022-10-23 thomas }
208 3efd8e31 2022-10-23 thomas free($2);
209 3efd8e31 2022-10-23 thomas }
210 3efd8e31 2022-10-23 thomas | UNIX_GROUP STRING {
211 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_group_name, $2,
212 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_group_name)) >=
213 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_group_name)) {
214 3efd8e31 2022-10-23 thomas yyerror("%s: unix group name too long",
215 3efd8e31 2022-10-23 thomas __func__);
216 3efd8e31 2022-10-23 thomas free($2);
217 3efd8e31 2022-10-23 thomas YYERROR;
218 3efd8e31 2022-10-23 thomas }
219 3efd8e31 2022-10-23 thomas free($2);
220 3efd8e31 2022-10-23 thomas }
221 3efd8e31 2022-10-23 thomas | USER STRING {
222 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, $2,
223 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >=
224 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) {
225 3efd8e31 2022-10-23 thomas yyerror("%s: user name too long", __func__);
226 3efd8e31 2022-10-23 thomas free($2);
227 3efd8e31 2022-10-23 thomas YYERROR;
228 3efd8e31 2022-10-23 thomas }
229 3efd8e31 2022-10-23 thomas free($2);
230 3efd8e31 2022-10-23 thomas }
231 0781db0e 2023-01-06 thomas | connection
232 3efd8e31 2022-10-23 thomas ;
233 3efd8e31 2022-10-23 thomas
234 0781db0e 2023-01-06 thomas connection : CONNECTION '{' optnl conflags_l '}'
235 0781db0e 2023-01-06 thomas | CONNECTION conflags
236 0781db0e 2023-01-06 thomas
237 0781db0e 2023-01-06 thomas conflags_l : conflags optnl conflags_l
238 0781db0e 2023-01-06 thomas | conflags optnl
239 0781db0e 2023-01-06 thomas ;
240 0781db0e 2023-01-06 thomas
241 0781db0e 2023-01-06 thomas conflags : REQUEST TIMEOUT timeout {
242 912db690 2023-01-06 thomas if ($3.tv_sec <= 0) {
243 912db690 2023-01-06 thomas yyerror("invalid timeout: %lld", $3.tv_sec);
244 912db690 2023-01-06 thomas YYERROR;
245 912db690 2023-01-06 thomas }
246 0781db0e 2023-01-06 thomas memcpy(&gotd->request_timeout, &$3,
247 0781db0e 2023-01-06 thomas sizeof(gotd->request_timeout));
248 0781db0e 2023-01-06 thomas }
249 0781db0e 2023-01-06 thomas | LIMIT USER STRING NUMBER {
250 0781db0e 2023-01-06 thomas if (gotd_proc_id == PROC_LISTEN &&
251 0781db0e 2023-01-06 thomas conf_limit_user_connections($3, $4) == -1) {
252 0781db0e 2023-01-06 thomas free($3);
253 0781db0e 2023-01-06 thomas YYERROR;
254 0781db0e 2023-01-06 thomas }
255 0781db0e 2023-01-06 thomas free($3);
256 0781db0e 2023-01-06 thomas }
257 0781db0e 2023-01-06 thomas ;
258 0781db0e 2023-01-06 thomas
259 3efd8e31 2022-10-23 thomas repository : REPOSITORY STRING {
260 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
261 3efd8e31 2022-10-23 thomas
262 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
263 3efd8e31 2022-10-23 thomas if (strcmp(repo->name, $2) == 0) {
264 3efd8e31 2022-10-23 thomas yyerror("duplicate repository '%s'", $2);
265 3efd8e31 2022-10-23 thomas free($2);
266 3efd8e31 2022-10-23 thomas YYERROR;
267 3efd8e31 2022-10-23 thomas }
268 3efd8e31 2022-10-23 thomas }
269 3efd8e31 2022-10-23 thomas
270 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
271 c669c489 2022-12-30 thomas gotd_proc_id == PROC_AUTH) {
272 3efd8e31 2022-10-23 thomas new_repo = conf_new_repo($2);
273 3efd8e31 2022-10-23 thomas }
274 3efd8e31 2022-10-23 thomas free($2);
275 3efd8e31 2022-10-23 thomas } '{' optnl repoopts2 '}' {
276 3efd8e31 2022-10-23 thomas }
277 3efd8e31 2022-10-23 thomas ;
278 3efd8e31 2022-10-23 thomas
279 3efd8e31 2022-10-23 thomas repoopts1 : PATH STRING {
280 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
281 c669c489 2022-12-30 thomas gotd_proc_id == PROC_AUTH) {
282 3efd8e31 2022-10-23 thomas if (!got_path_is_absolute($2)) {
283 3efd8e31 2022-10-23 thomas yyerror("%s: path %s is not absolute",
284 3efd8e31 2022-10-23 thomas __func__, $2);
285 3efd8e31 2022-10-23 thomas free($2);
286 3efd8e31 2022-10-23 thomas YYERROR;
287 3efd8e31 2022-10-23 thomas }
288 3efd8e31 2022-10-23 thomas if (strlcpy(new_repo->path, $2,
289 3efd8e31 2022-10-23 thomas sizeof(new_repo->path)) >=
290 3efd8e31 2022-10-23 thomas sizeof(new_repo->path)) {
291 3efd8e31 2022-10-23 thomas yyerror("%s: path truncated", __func__);
292 3efd8e31 2022-10-23 thomas free($2);
293 3efd8e31 2022-10-23 thomas YYERROR;
294 3efd8e31 2022-10-23 thomas }
295 3efd8e31 2022-10-23 thomas }
296 3efd8e31 2022-10-23 thomas free($2);
297 729a7e24 2022-11-17 thomas }
298 729a7e24 2022-11-17 thomas | PERMIT RO STRING {
299 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
300 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
301 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
302 729a7e24 2022-11-17 thomas }
303 729a7e24 2022-11-17 thomas }
304 729a7e24 2022-11-17 thomas | PERMIT RW STRING {
305 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
306 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
307 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED,
308 729a7e24 2022-11-17 thomas GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
309 729a7e24 2022-11-17 thomas }
310 3efd8e31 2022-10-23 thomas }
311 729a7e24 2022-11-17 thomas | DENY STRING {
312 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
313 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
314 729a7e24 2022-11-17 thomas GOTD_ACCESS_DENIED, 0, $2);
315 729a7e24 2022-11-17 thomas }
316 729a7e24 2022-11-17 thomas }
317 3efd8e31 2022-10-23 thomas ;
318 3efd8e31 2022-10-23 thomas
319 3efd8e31 2022-10-23 thomas repoopts2 : repoopts2 repoopts1 nl
320 3efd8e31 2022-10-23 thomas | repoopts1 optnl
321 3efd8e31 2022-10-23 thomas ;
322 3efd8e31 2022-10-23 thomas
323 3efd8e31 2022-10-23 thomas nl : '\n' optnl
324 3efd8e31 2022-10-23 thomas ;
325 3efd8e31 2022-10-23 thomas
326 3efd8e31 2022-10-23 thomas optnl : '\n' optnl /* zero or more newlines */
327 3efd8e31 2022-10-23 thomas | /* empty */
328 3efd8e31 2022-10-23 thomas ;
329 3efd8e31 2022-10-23 thomas
330 3efd8e31 2022-10-23 thomas %%
331 3efd8e31 2022-10-23 thomas
332 3efd8e31 2022-10-23 thomas struct keywords {
333 3efd8e31 2022-10-23 thomas const char *k_name;
334 3efd8e31 2022-10-23 thomas int k_val;
335 3efd8e31 2022-10-23 thomas };
336 3efd8e31 2022-10-23 thomas
337 3efd8e31 2022-10-23 thomas int
338 3efd8e31 2022-10-23 thomas yyerror(const char *fmt, ...)
339 3efd8e31 2022-10-23 thomas {
340 3efd8e31 2022-10-23 thomas va_list ap;
341 3efd8e31 2022-10-23 thomas char *msg;
342 3efd8e31 2022-10-23 thomas
343 3efd8e31 2022-10-23 thomas file->errors++;
344 3efd8e31 2022-10-23 thomas va_start(ap, fmt);
345 3efd8e31 2022-10-23 thomas if (vasprintf(&msg, fmt, ap) == -1)
346 3efd8e31 2022-10-23 thomas fatalx("yyerror vasprintf");
347 3efd8e31 2022-10-23 thomas va_end(ap);
348 3efd8e31 2022-10-23 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
349 3efd8e31 2022-10-23 thomas free(msg);
350 3efd8e31 2022-10-23 thomas return (0);
351 3efd8e31 2022-10-23 thomas }
352 3efd8e31 2022-10-23 thomas
353 3efd8e31 2022-10-23 thomas int
354 3efd8e31 2022-10-23 thomas kw_cmp(const void *k, const void *e)
355 3efd8e31 2022-10-23 thomas {
356 3efd8e31 2022-10-23 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
357 3efd8e31 2022-10-23 thomas }
358 3efd8e31 2022-10-23 thomas
359 3efd8e31 2022-10-23 thomas int
360 3efd8e31 2022-10-23 thomas lookup(char *s)
361 3efd8e31 2022-10-23 thomas {
362 3efd8e31 2022-10-23 thomas /* This has to be sorted always. */
363 3efd8e31 2022-10-23 thomas static const struct keywords keywords[] = {
364 0781db0e 2023-01-06 thomas { "connection", CONNECTION },
365 729a7e24 2022-11-17 thomas { "deny", DENY },
366 0781db0e 2023-01-06 thomas { "limit", LIMIT },
367 3efd8e31 2022-10-23 thomas { "on", ON },
368 3efd8e31 2022-10-23 thomas { "path", PATH },
369 729a7e24 2022-11-17 thomas { "permit", PERMIT },
370 3efd8e31 2022-10-23 thomas { "repository", REPOSITORY },
371 0781db0e 2023-01-06 thomas { "request", REQUEST },
372 729a7e24 2022-11-17 thomas { "ro", RO },
373 729a7e24 2022-11-17 thomas { "rw", RW },
374 0781db0e 2023-01-06 thomas { "timeout", TIMEOUT },
375 3efd8e31 2022-10-23 thomas { "unix_group", UNIX_GROUP },
376 3efd8e31 2022-10-23 thomas { "unix_socket", UNIX_SOCKET },
377 3efd8e31 2022-10-23 thomas { "user", USER },
378 3efd8e31 2022-10-23 thomas };
379 3efd8e31 2022-10-23 thomas const struct keywords *p;
380 3efd8e31 2022-10-23 thomas
381 3efd8e31 2022-10-23 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
382 3efd8e31 2022-10-23 thomas sizeof(keywords[0]), kw_cmp);
383 3efd8e31 2022-10-23 thomas
384 3efd8e31 2022-10-23 thomas if (p)
385 3efd8e31 2022-10-23 thomas return (p->k_val);
386 3efd8e31 2022-10-23 thomas else
387 3efd8e31 2022-10-23 thomas return (STRING);
388 3efd8e31 2022-10-23 thomas }
389 3efd8e31 2022-10-23 thomas
390 3efd8e31 2022-10-23 thomas #define MAXPUSHBACK 128
391 3efd8e31 2022-10-23 thomas
392 3efd8e31 2022-10-23 thomas unsigned char *parsebuf;
393 3efd8e31 2022-10-23 thomas int parseindex;
394 3efd8e31 2022-10-23 thomas unsigned char pushback_buffer[MAXPUSHBACK];
395 3efd8e31 2022-10-23 thomas int pushback_index = 0;
396 3efd8e31 2022-10-23 thomas
397 3efd8e31 2022-10-23 thomas int
398 3efd8e31 2022-10-23 thomas lgetc(int quotec)
399 3efd8e31 2022-10-23 thomas {
400 3efd8e31 2022-10-23 thomas int c, next;
401 3efd8e31 2022-10-23 thomas
402 3efd8e31 2022-10-23 thomas if (parsebuf) {
403 3efd8e31 2022-10-23 thomas /* Read character from the parsebuffer instead of input. */
404 3efd8e31 2022-10-23 thomas if (parseindex >= 0) {
405 3efd8e31 2022-10-23 thomas c = parsebuf[parseindex++];
406 3efd8e31 2022-10-23 thomas if (c != '\0')
407 3efd8e31 2022-10-23 thomas return (c);
408 3efd8e31 2022-10-23 thomas parsebuf = NULL;
409 3efd8e31 2022-10-23 thomas } else
410 3efd8e31 2022-10-23 thomas parseindex++;
411 3efd8e31 2022-10-23 thomas }
412 3efd8e31 2022-10-23 thomas
413 3efd8e31 2022-10-23 thomas if (pushback_index)
414 3efd8e31 2022-10-23 thomas return (pushback_buffer[--pushback_index]);
415 3efd8e31 2022-10-23 thomas
416 3efd8e31 2022-10-23 thomas if (quotec) {
417 3efd8e31 2022-10-23 thomas c = getc(file->stream);
418 3efd8e31 2022-10-23 thomas if (c == EOF)
419 3efd8e31 2022-10-23 thomas yyerror("reached end of file while parsing "
420 3efd8e31 2022-10-23 thomas "quoted string");
421 3efd8e31 2022-10-23 thomas return (c);
422 3efd8e31 2022-10-23 thomas }
423 3efd8e31 2022-10-23 thomas
424 3efd8e31 2022-10-23 thomas c = getc(file->stream);
425 3efd8e31 2022-10-23 thomas while (c == '\\') {
426 3efd8e31 2022-10-23 thomas next = getc(file->stream);
427 3efd8e31 2022-10-23 thomas if (next != '\n') {
428 3efd8e31 2022-10-23 thomas c = next;
429 3efd8e31 2022-10-23 thomas break;
430 3efd8e31 2022-10-23 thomas }
431 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
432 3efd8e31 2022-10-23 thomas file->lineno++;
433 3efd8e31 2022-10-23 thomas c = getc(file->stream);
434 3efd8e31 2022-10-23 thomas }
435 3efd8e31 2022-10-23 thomas
436 3efd8e31 2022-10-23 thomas return (c);
437 3efd8e31 2022-10-23 thomas }
438 3efd8e31 2022-10-23 thomas
439 3efd8e31 2022-10-23 thomas int
440 3efd8e31 2022-10-23 thomas lungetc(int c)
441 3efd8e31 2022-10-23 thomas {
442 3efd8e31 2022-10-23 thomas if (c == EOF)
443 3efd8e31 2022-10-23 thomas return (EOF);
444 3efd8e31 2022-10-23 thomas if (parsebuf) {
445 3efd8e31 2022-10-23 thomas parseindex--;
446 3efd8e31 2022-10-23 thomas if (parseindex >= 0)
447 3efd8e31 2022-10-23 thomas return (c);
448 3efd8e31 2022-10-23 thomas }
449 3efd8e31 2022-10-23 thomas if (pushback_index < MAXPUSHBACK-1)
450 3efd8e31 2022-10-23 thomas return (pushback_buffer[pushback_index++] = c);
451 3efd8e31 2022-10-23 thomas else
452 3efd8e31 2022-10-23 thomas return (EOF);
453 3efd8e31 2022-10-23 thomas }
454 3efd8e31 2022-10-23 thomas
455 3efd8e31 2022-10-23 thomas int
456 3efd8e31 2022-10-23 thomas findeol(void)
457 3efd8e31 2022-10-23 thomas {
458 3efd8e31 2022-10-23 thomas int c;
459 3efd8e31 2022-10-23 thomas
460 3efd8e31 2022-10-23 thomas parsebuf = NULL;
461 3efd8e31 2022-10-23 thomas
462 3efd8e31 2022-10-23 thomas /* Skip to either EOF or the first real EOL. */
463 3efd8e31 2022-10-23 thomas while (1) {
464 3efd8e31 2022-10-23 thomas if (pushback_index)
465 3efd8e31 2022-10-23 thomas c = pushback_buffer[--pushback_index];
466 3efd8e31 2022-10-23 thomas else
467 3efd8e31 2022-10-23 thomas c = lgetc(0);
468 3efd8e31 2022-10-23 thomas if (c == '\n') {
469 3efd8e31 2022-10-23 thomas file->lineno++;
470 3efd8e31 2022-10-23 thomas break;
471 3efd8e31 2022-10-23 thomas }
472 3efd8e31 2022-10-23 thomas if (c == EOF)
473 3efd8e31 2022-10-23 thomas break;
474 3efd8e31 2022-10-23 thomas }
475 3efd8e31 2022-10-23 thomas return (ERROR);
476 3efd8e31 2022-10-23 thomas }
477 3efd8e31 2022-10-23 thomas
478 3efd8e31 2022-10-23 thomas int
479 3efd8e31 2022-10-23 thomas yylex(void)
480 3efd8e31 2022-10-23 thomas {
481 3efd8e31 2022-10-23 thomas unsigned char buf[8096];
482 3efd8e31 2022-10-23 thomas unsigned char *p, *val;
483 3efd8e31 2022-10-23 thomas int quotec, next, c;
484 3efd8e31 2022-10-23 thomas int token;
485 3efd8e31 2022-10-23 thomas
486 3efd8e31 2022-10-23 thomas top:
487 3efd8e31 2022-10-23 thomas p = buf;
488 3efd8e31 2022-10-23 thomas c = lgetc(0);
489 3efd8e31 2022-10-23 thomas while (c == ' ' || c == '\t')
490 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
491 3efd8e31 2022-10-23 thomas
492 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
493 3efd8e31 2022-10-23 thomas if (c == '#') {
494 3efd8e31 2022-10-23 thomas c = lgetc(0);
495 3efd8e31 2022-10-23 thomas while (c != '\n' && c != EOF)
496 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
497 3efd8e31 2022-10-23 thomas }
498 3efd8e31 2022-10-23 thomas if (c == '$' && parsebuf == NULL) {
499 3efd8e31 2022-10-23 thomas while (1) {
500 3efd8e31 2022-10-23 thomas c = lgetc(0);
501 3efd8e31 2022-10-23 thomas if (c == EOF)
502 3efd8e31 2022-10-23 thomas return (0);
503 3efd8e31 2022-10-23 thomas
504 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
505 3efd8e31 2022-10-23 thomas yyerror("string too long");
506 3efd8e31 2022-10-23 thomas return (findeol());
507 3efd8e31 2022-10-23 thomas }
508 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == '_') {
509 3efd8e31 2022-10-23 thomas *p++ = c;
510 3efd8e31 2022-10-23 thomas continue;
511 3efd8e31 2022-10-23 thomas }
512 3efd8e31 2022-10-23 thomas *p = '\0';
513 3efd8e31 2022-10-23 thomas lungetc(c);
514 3efd8e31 2022-10-23 thomas break;
515 3efd8e31 2022-10-23 thomas }
516 3efd8e31 2022-10-23 thomas val = symget(buf);
517 3efd8e31 2022-10-23 thomas if (val == NULL) {
518 3efd8e31 2022-10-23 thomas yyerror("macro '%s' not defined", buf);
519 3efd8e31 2022-10-23 thomas return (findeol());
520 3efd8e31 2022-10-23 thomas }
521 3efd8e31 2022-10-23 thomas parsebuf = val;
522 3efd8e31 2022-10-23 thomas parseindex = 0;
523 3efd8e31 2022-10-23 thomas goto top;
524 3efd8e31 2022-10-23 thomas }
525 3efd8e31 2022-10-23 thomas
526 3efd8e31 2022-10-23 thomas switch (c) {
527 3efd8e31 2022-10-23 thomas case '\'':
528 3efd8e31 2022-10-23 thomas case '"':
529 3efd8e31 2022-10-23 thomas quotec = c;
530 3efd8e31 2022-10-23 thomas while (1) {
531 3efd8e31 2022-10-23 thomas c = lgetc(quotec);
532 3efd8e31 2022-10-23 thomas if (c == EOF)
533 3efd8e31 2022-10-23 thomas return (0);
534 3efd8e31 2022-10-23 thomas if (c == '\n') {
535 3efd8e31 2022-10-23 thomas file->lineno++;
536 3efd8e31 2022-10-23 thomas continue;
537 3efd8e31 2022-10-23 thomas } else if (c == '\\') {
538 3efd8e31 2022-10-23 thomas next = lgetc(quotec);
539 3efd8e31 2022-10-23 thomas if (next == EOF)
540 3efd8e31 2022-10-23 thomas return (0);
541 3efd8e31 2022-10-23 thomas if (next == quotec || c == ' ' || c == '\t')
542 3efd8e31 2022-10-23 thomas c = next;
543 3efd8e31 2022-10-23 thomas else if (next == '\n') {
544 3efd8e31 2022-10-23 thomas file->lineno++;
545 3efd8e31 2022-10-23 thomas continue;
546 3efd8e31 2022-10-23 thomas } else
547 3efd8e31 2022-10-23 thomas lungetc(next);
548 3efd8e31 2022-10-23 thomas } else if (c == quotec) {
549 3efd8e31 2022-10-23 thomas *p = '\0';
550 3efd8e31 2022-10-23 thomas break;
551 3efd8e31 2022-10-23 thomas } else if (c == '\0') {
552 3efd8e31 2022-10-23 thomas yyerror("syntax error");
553 3efd8e31 2022-10-23 thomas return (findeol());
554 3efd8e31 2022-10-23 thomas }
555 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
556 3efd8e31 2022-10-23 thomas yyerror("string too long");
557 3efd8e31 2022-10-23 thomas return (findeol());
558 3efd8e31 2022-10-23 thomas }
559 3efd8e31 2022-10-23 thomas *p++ = c;
560 3efd8e31 2022-10-23 thomas }
561 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
562 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
563 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
564 3efd8e31 2022-10-23 thomas return (STRING);
565 3efd8e31 2022-10-23 thomas }
566 3efd8e31 2022-10-23 thomas
567 3efd8e31 2022-10-23 thomas #define allowed_to_end_number(x) \
568 3efd8e31 2022-10-23 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
569 3efd8e31 2022-10-23 thomas
570 3efd8e31 2022-10-23 thomas if (c == '-' || isdigit(c)) {
571 3efd8e31 2022-10-23 thomas do {
572 3efd8e31 2022-10-23 thomas *p++ = c;
573 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
574 3efd8e31 2022-10-23 thomas yyerror("string too long");
575 3efd8e31 2022-10-23 thomas return (findeol());
576 3efd8e31 2022-10-23 thomas }
577 3efd8e31 2022-10-23 thomas c = lgetc(0);
578 3efd8e31 2022-10-23 thomas } while (c != EOF && isdigit(c));
579 3efd8e31 2022-10-23 thomas lungetc(c);
580 3efd8e31 2022-10-23 thomas if (p == buf + 1 && buf[0] == '-')
581 3efd8e31 2022-10-23 thomas goto nodigits;
582 3efd8e31 2022-10-23 thomas if (c == EOF || allowed_to_end_number(c)) {
583 3efd8e31 2022-10-23 thomas const char *errstr = NULL;
584 3efd8e31 2022-10-23 thomas
585 3efd8e31 2022-10-23 thomas *p = '\0';
586 3efd8e31 2022-10-23 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
587 3efd8e31 2022-10-23 thomas LLONG_MAX, &errstr);
588 3efd8e31 2022-10-23 thomas if (errstr) {
589 3efd8e31 2022-10-23 thomas yyerror("\"%s\" invalid number: %s",
590 3efd8e31 2022-10-23 thomas buf, errstr);
591 3efd8e31 2022-10-23 thomas return (findeol());
592 3efd8e31 2022-10-23 thomas }
593 3efd8e31 2022-10-23 thomas return (NUMBER);
594 3efd8e31 2022-10-23 thomas } else {
595 3efd8e31 2022-10-23 thomas nodigits:
596 3efd8e31 2022-10-23 thomas while (p > buf + 1)
597 3efd8e31 2022-10-23 thomas lungetc(*--p);
598 3efd8e31 2022-10-23 thomas c = *--p;
599 3efd8e31 2022-10-23 thomas if (c == '-')
600 3efd8e31 2022-10-23 thomas return (c);
601 3efd8e31 2022-10-23 thomas }
602 3efd8e31 2022-10-23 thomas }
603 3efd8e31 2022-10-23 thomas
604 3efd8e31 2022-10-23 thomas #define allowed_in_string(x) \
605 3efd8e31 2022-10-23 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
606 3efd8e31 2022-10-23 thomas x != '{' && x != '}' && \
607 3efd8e31 2022-10-23 thomas x != '!' && x != '=' && x != '#' && \
608 3efd8e31 2022-10-23 thomas x != ','))
609 3efd8e31 2022-10-23 thomas
610 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == ':' || c == '_') {
611 3efd8e31 2022-10-23 thomas do {
612 3efd8e31 2022-10-23 thomas *p++ = c;
613 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
614 3efd8e31 2022-10-23 thomas yyerror("string too long");
615 3efd8e31 2022-10-23 thomas return (findeol());
616 3efd8e31 2022-10-23 thomas }
617 3efd8e31 2022-10-23 thomas c = lgetc(0);
618 3efd8e31 2022-10-23 thomas } while (c != EOF && (allowed_in_string(c)));
619 3efd8e31 2022-10-23 thomas lungetc(c);
620 3efd8e31 2022-10-23 thomas *p = '\0';
621 3efd8e31 2022-10-23 thomas token = lookup(buf);
622 3efd8e31 2022-10-23 thomas if (token == STRING) {
623 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
624 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
625 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
626 3efd8e31 2022-10-23 thomas }
627 3efd8e31 2022-10-23 thomas return (token);
628 3efd8e31 2022-10-23 thomas }
629 3efd8e31 2022-10-23 thomas if (c == '\n') {
630 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
631 3efd8e31 2022-10-23 thomas file->lineno++;
632 3efd8e31 2022-10-23 thomas }
633 3efd8e31 2022-10-23 thomas if (c == EOF)
634 3efd8e31 2022-10-23 thomas return (0);
635 3efd8e31 2022-10-23 thomas return (c);
636 3efd8e31 2022-10-23 thomas }
637 3efd8e31 2022-10-23 thomas
638 3efd8e31 2022-10-23 thomas int
639 3efd8e31 2022-10-23 thomas check_file_secrecy(int fd, const char *fname)
640 3efd8e31 2022-10-23 thomas {
641 3efd8e31 2022-10-23 thomas struct stat st;
642 3efd8e31 2022-10-23 thomas
643 3efd8e31 2022-10-23 thomas if (fstat(fd, &st)) {
644 3efd8e31 2022-10-23 thomas log_warn("cannot stat %s", fname);
645 3efd8e31 2022-10-23 thomas return (-1);
646 3efd8e31 2022-10-23 thomas }
647 3efd8e31 2022-10-23 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
648 3efd8e31 2022-10-23 thomas log_warnx("%s: owner not root or current user", fname);
649 3efd8e31 2022-10-23 thomas return (-1);
650 3efd8e31 2022-10-23 thomas }
651 3efd8e31 2022-10-23 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
652 3efd8e31 2022-10-23 thomas log_warnx("%s: group writable or world read/writable", fname);
653 3efd8e31 2022-10-23 thomas return (-1);
654 3efd8e31 2022-10-23 thomas }
655 3efd8e31 2022-10-23 thomas return (0);
656 3efd8e31 2022-10-23 thomas }
657 3efd8e31 2022-10-23 thomas
658 3efd8e31 2022-10-23 thomas struct file *
659 3efd8e31 2022-10-23 thomas newfile(const char *name, int secret)
660 3efd8e31 2022-10-23 thomas {
661 3efd8e31 2022-10-23 thomas struct file *nfile;
662 3efd8e31 2022-10-23 thomas
663 3efd8e31 2022-10-23 thomas nfile = calloc(1, sizeof(struct file));
664 3efd8e31 2022-10-23 thomas if (nfile == NULL) {
665 3efd8e31 2022-10-23 thomas log_warn("calloc");
666 3efd8e31 2022-10-23 thomas return (NULL);
667 3efd8e31 2022-10-23 thomas }
668 3efd8e31 2022-10-23 thomas nfile->name = strdup(name);
669 3efd8e31 2022-10-23 thomas if (nfile->name == NULL) {
670 3efd8e31 2022-10-23 thomas log_warn("strdup");
671 3efd8e31 2022-10-23 thomas free(nfile);
672 3efd8e31 2022-10-23 thomas return (NULL);
673 3efd8e31 2022-10-23 thomas }
674 3efd8e31 2022-10-23 thomas nfile->stream = fopen(nfile->name, "r");
675 3efd8e31 2022-10-23 thomas if (nfile->stream == NULL) {
676 3efd8e31 2022-10-23 thomas /* no warning, we don't require a conf file */
677 3efd8e31 2022-10-23 thomas free(nfile->name);
678 3efd8e31 2022-10-23 thomas free(nfile);
679 3efd8e31 2022-10-23 thomas return (NULL);
680 3efd8e31 2022-10-23 thomas } else if (secret &&
681 3efd8e31 2022-10-23 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
682 3efd8e31 2022-10-23 thomas fclose(nfile->stream);
683 3efd8e31 2022-10-23 thomas free(nfile->name);
684 3efd8e31 2022-10-23 thomas free(nfile);
685 3efd8e31 2022-10-23 thomas return (NULL);
686 3efd8e31 2022-10-23 thomas }
687 3efd8e31 2022-10-23 thomas nfile->lineno = 1;
688 3efd8e31 2022-10-23 thomas return (nfile);
689 3efd8e31 2022-10-23 thomas }
690 3efd8e31 2022-10-23 thomas
691 3efd8e31 2022-10-23 thomas static void
692 3efd8e31 2022-10-23 thomas closefile(struct file *xfile)
693 3efd8e31 2022-10-23 thomas {
694 3efd8e31 2022-10-23 thomas fclose(xfile->stream);
695 3efd8e31 2022-10-23 thomas free(xfile->name);
696 3efd8e31 2022-10-23 thomas free(xfile);
697 3efd8e31 2022-10-23 thomas }
698 3efd8e31 2022-10-23 thomas
699 3efd8e31 2022-10-23 thomas int
700 3efd8e31 2022-10-23 thomas parse_config(const char *filename, enum gotd_procid proc_id,
701 3efd8e31 2022-10-23 thomas struct gotd *env)
702 3efd8e31 2022-10-23 thomas {
703 3efd8e31 2022-10-23 thomas struct sym *sym, *next;
704 88f1bb6d 2023-01-02 thomas struct gotd_repo *repo;
705 3efd8e31 2022-10-23 thomas
706 3efd8e31 2022-10-23 thomas memset(env, 0, sizeof(*env));
707 3efd8e31 2022-10-23 thomas
708 3efd8e31 2022-10-23 thomas gotd = env;
709 3efd8e31 2022-10-23 thomas gotd_proc_id = proc_id;
710 3efd8e31 2022-10-23 thomas TAILQ_INIT(&gotd->repos);
711 3efd8e31 2022-10-23 thomas
712 3efd8e31 2022-10-23 thomas /* Apply default values. */
713 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
714 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
715 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: unix socket path too long", __func__);
716 3efd8e31 2022-10-23 thomas return -1;
717 3efd8e31 2022-10-23 thomas }
718 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_group_name, GOTD_UNIX_GROUP,
719 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_group_name)) >= sizeof(gotd->unix_group_name)) {
720 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: unix group name too long", __func__);
721 3efd8e31 2022-10-23 thomas return -1;
722 3efd8e31 2022-10-23 thomas }
723 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, GOTD_USER,
724 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
725 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: user name too long", __func__);
726 3efd8e31 2022-10-23 thomas return -1;
727 3efd8e31 2022-10-23 thomas }
728 0781db0e 2023-01-06 thomas
729 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
730 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_usec = 0;
731 3efd8e31 2022-10-23 thomas
732 3efd8e31 2022-10-23 thomas file = newfile(filename, 0);
733 3efd8e31 2022-10-23 thomas if (file == NULL) {
734 3efd8e31 2022-10-23 thomas /* just return, as we don't require a conf file */
735 3efd8e31 2022-10-23 thomas return (0);
736 3efd8e31 2022-10-23 thomas }
737 3efd8e31 2022-10-23 thomas
738 3efd8e31 2022-10-23 thomas yyparse();
739 3efd8e31 2022-10-23 thomas errors = file->errors;
740 3efd8e31 2022-10-23 thomas closefile(file);
741 3efd8e31 2022-10-23 thomas
742 3efd8e31 2022-10-23 thomas /* Free macros and check which have not been used. */
743 3efd8e31 2022-10-23 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
744 3efd8e31 2022-10-23 thomas if ((gotd->verbosity > 1) && !sym->used)
745 3efd8e31 2022-10-23 thomas fprintf(stderr, "warning: macro '%s' not used\n",
746 3efd8e31 2022-10-23 thomas sym->nam);
747 3efd8e31 2022-10-23 thomas if (!sym->persist) {
748 3efd8e31 2022-10-23 thomas free(sym->nam);
749 3efd8e31 2022-10-23 thomas free(sym->val);
750 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
751 3efd8e31 2022-10-23 thomas free(sym);
752 3efd8e31 2022-10-23 thomas }
753 3efd8e31 2022-10-23 thomas }
754 3efd8e31 2022-10-23 thomas
755 3efd8e31 2022-10-23 thomas if (errors)
756 3efd8e31 2022-10-23 thomas return (-1);
757 3efd8e31 2022-10-23 thomas
758 88f1bb6d 2023-01-02 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
759 88f1bb6d 2023-01-02 thomas if (repo->path[0] == '\0') {
760 0cbf8de7 2023-01-02 thomas log_warnx("repository \"%s\": no path provided in "
761 0cbf8de7 2023-01-02 thomas "configuration file", repo->name);
762 88f1bb6d 2023-01-02 thomas return (-1);
763 88f1bb6d 2023-01-02 thomas }
764 88f1bb6d 2023-01-02 thomas }
765 88f1bb6d 2023-01-02 thomas
766 3efd8e31 2022-10-23 thomas return (0);
767 3efd8e31 2022-10-23 thomas }
768 3efd8e31 2022-10-23 thomas
769 0781db0e 2023-01-06 thomas static int
770 0781db0e 2023-01-06 thomas uid_connection_limit_cmp(const void *pa, const void *pb)
771 0781db0e 2023-01-06 thomas {
772 0781db0e 2023-01-06 thomas const struct gotd_uid_connection_limit *a = pa, *b = pb;
773 0781db0e 2023-01-06 thomas
774 0781db0e 2023-01-06 thomas if (a->uid < b->uid)
775 0781db0e 2023-01-06 thomas return -1;
776 0781db0e 2023-01-06 thomas else if (a->uid > b->uid);
777 0781db0e 2023-01-06 thomas return 1;
778 0781db0e 2023-01-06 thomas
779 0781db0e 2023-01-06 thomas return 0;
780 0781db0e 2023-01-06 thomas }
781 0781db0e 2023-01-06 thomas
782 0781db0e 2023-01-06 thomas static int
783 0781db0e 2023-01-06 thomas conf_limit_user_connections(const char *user, int maximum)
784 0781db0e 2023-01-06 thomas {
785 0781db0e 2023-01-06 thomas uid_t uid;
786 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *limit;
787 0781db0e 2023-01-06 thomas size_t nlimits;
788 0781db0e 2023-01-06 thomas
789 0781db0e 2023-01-06 thomas if (maximum < 1) {
790 0781db0e 2023-01-06 thomas yyerror("max connections cannot be smaller 1");
791 0781db0e 2023-01-06 thomas return -1;
792 0781db0e 2023-01-06 thomas }
793 0781db0e 2023-01-06 thomas if (maximum > GOTD_MAXCLIENTS) {
794 0781db0e 2023-01-06 thomas yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
795 0781db0e 2023-01-06 thomas return -1;
796 0781db0e 2023-01-06 thomas }
797 0781db0e 2023-01-06 thomas
798 0781db0e 2023-01-06 thomas if (gotd_auth_parseuid(user, &uid) == -1) {
799 0781db0e 2023-01-06 thomas yyerror("%s: no such user", user);
800 0781db0e 2023-01-06 thomas return -1;
801 0781db0e 2023-01-06 thomas }
802 0781db0e 2023-01-06 thomas
803 0781db0e 2023-01-06 thomas limit = gotd_find_uid_connection_limit(gotd->connection_limits,
804 0781db0e 2023-01-06 thomas gotd->nconnection_limits, uid);
805 0781db0e 2023-01-06 thomas if (limit) {
806 0781db0e 2023-01-06 thomas limit->max_connections = maximum;
807 0781db0e 2023-01-06 thomas return 0;
808 0781db0e 2023-01-06 thomas }
809 0781db0e 2023-01-06 thomas
810 0781db0e 2023-01-06 thomas limit = gotd->connection_limits;
811 0781db0e 2023-01-06 thomas nlimits = gotd->nconnection_limits + 1;
812 0781db0e 2023-01-06 thomas limit = reallocarray(limit, nlimits, sizeof(*limit));
813 0781db0e 2023-01-06 thomas if (limit == NULL)
814 0781db0e 2023-01-06 thomas fatal("reallocarray");
815 0781db0e 2023-01-06 thomas
816 0781db0e 2023-01-06 thomas limit[nlimits - 1].uid = uid;
817 0781db0e 2023-01-06 thomas limit[nlimits - 1].max_connections = maximum;
818 0781db0e 2023-01-06 thomas
819 0781db0e 2023-01-06 thomas gotd->connection_limits = limit;
820 0781db0e 2023-01-06 thomas gotd->nconnection_limits = nlimits;
821 0781db0e 2023-01-06 thomas qsort(gotd->connection_limits, gotd->nconnection_limits,
822 0781db0e 2023-01-06 thomas sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
823 0781db0e 2023-01-06 thomas
824 0781db0e 2023-01-06 thomas return 0;
825 0781db0e 2023-01-06 thomas }
826 0781db0e 2023-01-06 thomas
827 3efd8e31 2022-10-23 thomas static struct gotd_repo *
828 3efd8e31 2022-10-23 thomas conf_new_repo(const char *name)
829 3efd8e31 2022-10-23 thomas {
830 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
831 3efd8e31 2022-10-23 thomas
832 a42b418b 2023-01-02 thomas if (name[0] == '\0') {
833 a42b418b 2023-01-02 thomas fatalx("syntax error: empty repository name found in %s",
834 a42b418b 2023-01-02 thomas file->name);
835 a42b418b 2023-01-02 thomas }
836 a42b418b 2023-01-02 thomas
837 0cbf8de7 2023-01-02 thomas if (strchr(name, '\n') != NULL)
838 0cbf8de7 2023-01-02 thomas fatalx("repository names must not contain linefeeds: %s", name);
839 3efd8e31 2022-10-23 thomas
840 3efd8e31 2022-10-23 thomas repo = calloc(1, sizeof(*repo));
841 3efd8e31 2022-10-23 thomas if (repo == NULL)
842 3efd8e31 2022-10-23 thomas fatalx("%s: calloc", __func__);
843 3efd8e31 2022-10-23 thomas
844 729a7e24 2022-11-17 thomas STAILQ_INIT(&repo->rules);
845 729a7e24 2022-11-17 thomas
846 3efd8e31 2022-10-23 thomas if (strlcpy(repo->name, name, sizeof(repo->name)) >=
847 3efd8e31 2022-10-23 thomas sizeof(repo->name))
848 3efd8e31 2022-10-23 thomas fatalx("%s: strlcpy", __func__);
849 3efd8e31 2022-10-23 thomas
850 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
851 3efd8e31 2022-10-23 thomas gotd->nrepos++;
852 3efd8e31 2022-10-23 thomas
853 3efd8e31 2022-10-23 thomas return repo;
854 3efd8e31 2022-10-23 thomas };
855 729a7e24 2022-11-17 thomas
856 729a7e24 2022-11-17 thomas static void
857 729a7e24 2022-11-17 thomas conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
858 729a7e24 2022-11-17 thomas int authorization, char *identifier)
859 729a7e24 2022-11-17 thomas {
860 729a7e24 2022-11-17 thomas struct gotd_access_rule *rule;
861 729a7e24 2022-11-17 thomas
862 729a7e24 2022-11-17 thomas rule = calloc(1, sizeof(*rule));
863 729a7e24 2022-11-17 thomas if (rule == NULL)
864 729a7e24 2022-11-17 thomas fatal("calloc");
865 3efd8e31 2022-10-23 thomas
866 729a7e24 2022-11-17 thomas rule->access = access;
867 729a7e24 2022-11-17 thomas rule->authorization = authorization;
868 729a7e24 2022-11-17 thomas rule->identifier = identifier;
869 729a7e24 2022-11-17 thomas
870 729a7e24 2022-11-17 thomas STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
871 729a7e24 2022-11-17 thomas }
872 729a7e24 2022-11-17 thomas
873 3efd8e31 2022-10-23 thomas int
874 3efd8e31 2022-10-23 thomas symset(const char *nam, const char *val, int persist)
875 3efd8e31 2022-10-23 thomas {
876 3efd8e31 2022-10-23 thomas struct sym *sym;
877 3efd8e31 2022-10-23 thomas
878 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
879 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0)
880 3efd8e31 2022-10-23 thomas break;
881 3efd8e31 2022-10-23 thomas }
882 3efd8e31 2022-10-23 thomas
883 3efd8e31 2022-10-23 thomas if (sym != NULL) {
884 3efd8e31 2022-10-23 thomas if (sym->persist == 1)
885 3efd8e31 2022-10-23 thomas return (0);
886 3efd8e31 2022-10-23 thomas else {
887 3efd8e31 2022-10-23 thomas free(sym->nam);
888 3efd8e31 2022-10-23 thomas free(sym->val);
889 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
890 3efd8e31 2022-10-23 thomas free(sym);
891 3efd8e31 2022-10-23 thomas }
892 3efd8e31 2022-10-23 thomas }
893 3efd8e31 2022-10-23 thomas sym = calloc(1, sizeof(*sym));
894 3efd8e31 2022-10-23 thomas if (sym == NULL)
895 3efd8e31 2022-10-23 thomas return (-1);
896 3efd8e31 2022-10-23 thomas
897 3efd8e31 2022-10-23 thomas sym->nam = strdup(nam);
898 3efd8e31 2022-10-23 thomas if (sym->nam == NULL) {
899 3efd8e31 2022-10-23 thomas free(sym);
900 3efd8e31 2022-10-23 thomas return (-1);
901 3efd8e31 2022-10-23 thomas }
902 3efd8e31 2022-10-23 thomas sym->val = strdup(val);
903 3efd8e31 2022-10-23 thomas if (sym->val == NULL) {
904 3efd8e31 2022-10-23 thomas free(sym->nam);
905 3efd8e31 2022-10-23 thomas free(sym);
906 3efd8e31 2022-10-23 thomas return (-1);
907 3efd8e31 2022-10-23 thomas }
908 3efd8e31 2022-10-23 thomas sym->used = 0;
909 3efd8e31 2022-10-23 thomas sym->persist = persist;
910 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
911 3efd8e31 2022-10-23 thomas return (0);
912 3efd8e31 2022-10-23 thomas }
913 3efd8e31 2022-10-23 thomas
914 3efd8e31 2022-10-23 thomas char *
915 3efd8e31 2022-10-23 thomas symget(const char *nam)
916 3efd8e31 2022-10-23 thomas {
917 3efd8e31 2022-10-23 thomas struct sym *sym;
918 3efd8e31 2022-10-23 thomas
919 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
920 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0) {
921 3efd8e31 2022-10-23 thomas sym->used = 1;
922 3efd8e31 2022-10-23 thomas return (sym->val);
923 3efd8e31 2022-10-23 thomas }
924 3efd8e31 2022-10-23 thomas }
925 3efd8e31 2022-10-23 thomas return (NULL);
926 3efd8e31 2022-10-23 thomas }