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