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