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