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