Blame


1 fb1a36c0 2022-01-09 op /*
2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 fb1a36c0 2022-01-09 op * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
4 fb1a36c0 2022-01-09 op * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 fb1a36c0 2022-01-09 op * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 fb1a36c0 2022-01-09 op * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 fb1a36c0 2022-01-09 op * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 fb1a36c0 2022-01-09 op * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 fb1a36c0 2022-01-09 op * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 fb1a36c0 2022-01-09 op *
11 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
12 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
13 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
14 fb1a36c0 2022-01-09 op *
15 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 fb1a36c0 2022-01-09 op */
23 fb1a36c0 2022-01-09 op
24 fb1a36c0 2022-01-09 op %{
25 fb1a36c0 2022-01-09 op
26 fb1a36c0 2022-01-09 op #include <sys/types.h>
27 fb1a36c0 2022-01-09 op #include <sys/queue.h>
28 fb1a36c0 2022-01-09 op #include <sys/stat.h>
29 fb1a36c0 2022-01-09 op
30 fb1a36c0 2022-01-09 op #include <ctype.h>
31 fb1a36c0 2022-01-09 op #include <err.h>
32 fb1a36c0 2022-01-09 op #include <errno.h>
33 fb1a36c0 2022-01-09 op #include <event.h>
34 fb1a36c0 2022-01-09 op #include <inttypes.h>
35 fb1a36c0 2022-01-09 op #include <limits.h>
36 fb1a36c0 2022-01-09 op #include <stdarg.h>
37 fb1a36c0 2022-01-09 op #include <stdio.h>
38 fb1a36c0 2022-01-09 op #include <stdlib.h>
39 fb1a36c0 2022-01-09 op #include <string.h>
40 fb1a36c0 2022-01-09 op #include <syslog.h>
41 fb1a36c0 2022-01-09 op #include <unistd.h>
42 fb1a36c0 2022-01-09 op #include <imsg.h>
43 fb1a36c0 2022-01-09 op
44 fb1a36c0 2022-01-09 op #include "log.h"
45 fb1a36c0 2022-01-09 op #include "kamid.h"
46 fb1a36c0 2022-01-09 op #include "table.h"
47 fb1a36c0 2022-01-09 op #include "utils.h"
48 fb1a36c0 2022-01-09 op
49 fb1a36c0 2022-01-09 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
50 fb1a36c0 2022-01-09 op static struct file {
51 fb1a36c0 2022-01-09 op TAILQ_ENTRY(file) entry;
52 fb1a36c0 2022-01-09 op FILE *stream;
53 fb1a36c0 2022-01-09 op char *name;
54 fb1a36c0 2022-01-09 op size_t ungetpos;
55 fb1a36c0 2022-01-09 op size_t ungetsize;
56 fb1a36c0 2022-01-09 op u_char *ungetbuf;
57 fb1a36c0 2022-01-09 op int eof_reached;
58 fb1a36c0 2022-01-09 op int lineno;
59 fb1a36c0 2022-01-09 op int errors;
60 fb1a36c0 2022-01-09 op } *file, *topfile;
61 fb1a36c0 2022-01-09 op struct file *pushfile(const char *, int);
62 fb1a36c0 2022-01-09 op int popfile(void);
63 fb1a36c0 2022-01-09 op int check_file_secrecy(int, const char *);
64 fb1a36c0 2022-01-09 op int yyparse(void);
65 fb1a36c0 2022-01-09 op int yylex(void);
66 fb1a36c0 2022-01-09 op int yyerror(const char *, ...)
67 fb1a36c0 2022-01-09 op __attribute__((__format__ (printf, 1, 2)))
68 fb1a36c0 2022-01-09 op __attribute__((__nonnull__ (1)));
69 fb1a36c0 2022-01-09 op int kw_cmp(const void *, const void *);
70 fb1a36c0 2022-01-09 op int lookup(char *);
71 fb1a36c0 2022-01-09 op int igetc(void);
72 fb1a36c0 2022-01-09 op int lgetc(int);
73 fb1a36c0 2022-01-09 op void lungetc(int);
74 fb1a36c0 2022-01-09 op int findeol(void);
75 fb1a36c0 2022-01-09 op
76 fb1a36c0 2022-01-09 op TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
77 fb1a36c0 2022-01-09 op struct sym {
78 fb1a36c0 2022-01-09 op TAILQ_ENTRY(sym) entry;
79 fb1a36c0 2022-01-09 op int used;
80 fb1a36c0 2022-01-09 op int persist;
81 fb1a36c0 2022-01-09 op char *nam;
82 fb1a36c0 2022-01-09 op char *val;
83 fb1a36c0 2022-01-09 op };
84 fb1a36c0 2022-01-09 op
85 fb1a36c0 2022-01-09 op int symset(const char *, const char *, int);
86 fb1a36c0 2022-01-09 op char *symget(const char *);
87 fb1a36c0 2022-01-09 op
88 fb1a36c0 2022-01-09 op void clear_config(struct kd_conf *xconf);
89 fb1a36c0 2022-01-09 op
90 fb1a36c0 2022-01-09 op static void add_table(const char *, const char *, const char *);
91 fb1a36c0 2022-01-09 op static struct table *findtable(const char *name);
92 fb1a36c0 2022-01-09 op static void add_cert(const char *, const char *);
93 fb1a36c0 2022-01-09 op static void add_key(const char *, const char *);
94 fb1a36c0 2022-01-09 op static struct kd_listen_conf *listen_new(void);
95 fb1a36c0 2022-01-09 op
96 fb1a36c0 2022-01-09 op static uint32_t counter;
97 fb1a36c0 2022-01-09 op static struct table *table;
98 fb1a36c0 2022-01-09 op static struct kd_listen_conf *listener;
99 fb1a36c0 2022-01-09 op static struct kd_conf *conf;
100 fb1a36c0 2022-01-09 op static int errors;
101 fb1a36c0 2022-01-09 op
102 fb1a36c0 2022-01-09 op typedef struct {
103 fb1a36c0 2022-01-09 op union {
104 fb1a36c0 2022-01-09 op int64_t number;
105 fb1a36c0 2022-01-09 op char *string;
106 fb1a36c0 2022-01-09 op struct table *table;
107 fb1a36c0 2022-01-09 op } v;
108 fb1a36c0 2022-01-09 op int lineno;
109 fb1a36c0 2022-01-09 op } YYSTYPE;
110 fb1a36c0 2022-01-09 op
111 fb1a36c0 2022-01-09 op %}
112 fb1a36c0 2022-01-09 op
113 fb1a36c0 2022-01-09 op %token AUTH
114 fb1a36c0 2022-01-09 op %token CERT
115 fb1a36c0 2022-01-09 op %token ERROR
116 fb1a36c0 2022-01-09 op %token INCLUDE
117 fb1a36c0 2022-01-09 op %token KEY
118 fb1a36c0 2022-01-09 op %token LISTEN
119 fb1a36c0 2022-01-09 op %token NO
120 fb1a36c0 2022-01-09 op %token ON
121 fb1a36c0 2022-01-09 op %token PKI PORT
122 fb1a36c0 2022-01-09 op %token TABLE TLS
123 fb1a36c0 2022-01-09 op %token USERDATA
124 fb1a36c0 2022-01-09 op %token VIRTUAL
125 fb1a36c0 2022-01-09 op %token YES
126 fb1a36c0 2022-01-09 op
127 fb1a36c0 2022-01-09 op %token <v.string> STRING
128 fb1a36c0 2022-01-09 op %token <v.number> NUMBER
129 fb1a36c0 2022-01-09 op %type <v.number> yesno
130 fb1a36c0 2022-01-09 op %type <v.string> string
131 fb1a36c0 2022-01-09 op %type <v.table> tableref
132 fb1a36c0 2022-01-09 op
133 fb1a36c0 2022-01-09 op %%
134 fb1a36c0 2022-01-09 op
135 fb1a36c0 2022-01-09 op grammar : /* empty */
136 fb1a36c0 2022-01-09 op | grammar include '\n'
137 fb1a36c0 2022-01-09 op | grammar '\n'
138 fb1a36c0 2022-01-09 op | grammar table '\n'
139 fb1a36c0 2022-01-09 op | grammar pki '\n'
140 fb1a36c0 2022-01-09 op | grammar listen '\n'
141 fb1a36c0 2022-01-09 op | grammar varset '\n'
142 fb1a36c0 2022-01-09 op | grammar error '\n' { file->errors++; }
143 fb1a36c0 2022-01-09 op ;
144 fb1a36c0 2022-01-09 op
145 fb1a36c0 2022-01-09 op include : INCLUDE STRING {
146 fb1a36c0 2022-01-09 op struct file *nfile;
147 fb1a36c0 2022-01-09 op
148 fb1a36c0 2022-01-09 op if ((nfile = pushfile($2, 0)) == NULL) {
149 fb1a36c0 2022-01-09 op yyerror("failed to include file %s", $2);
150 fb1a36c0 2022-01-09 op free($2);
151 fb1a36c0 2022-01-09 op YYERROR;
152 fb1a36c0 2022-01-09 op }
153 fb1a36c0 2022-01-09 op free($2);
154 fb1a36c0 2022-01-09 op
155 fb1a36c0 2022-01-09 op file = nfile;
156 fb1a36c0 2022-01-09 op lungetc('\n');
157 fb1a36c0 2022-01-09 op }
158 fb1a36c0 2022-01-09 op ;
159 fb1a36c0 2022-01-09 op
160 fb1a36c0 2022-01-09 op string : string STRING {
161 fb1a36c0 2022-01-09 op if (asprintf(&$$, "%s %s", $1, $2) == -1) {
162 fb1a36c0 2022-01-09 op free($1);
163 fb1a36c0 2022-01-09 op free($2);
164 fb1a36c0 2022-01-09 op yyerror("string: asprintf");
165 fb1a36c0 2022-01-09 op YYERROR;
166 fb1a36c0 2022-01-09 op }
167 fb1a36c0 2022-01-09 op free($1);
168 fb1a36c0 2022-01-09 op free($2);
169 fb1a36c0 2022-01-09 op }
170 fb1a36c0 2022-01-09 op | STRING
171 fb1a36c0 2022-01-09 op ;
172 fb1a36c0 2022-01-09 op
173 fb1a36c0 2022-01-09 op yesno : YES { $$ = 1; }
174 fb1a36c0 2022-01-09 op | NO { $$ = 0; }
175 fb1a36c0 2022-01-09 op ;
176 fb1a36c0 2022-01-09 op
177 fb1a36c0 2022-01-09 op optnl : '\n' optnl /* zero or more newlines */
178 fb1a36c0 2022-01-09 op | /*empty*/
179 fb1a36c0 2022-01-09 op ;
180 fb1a36c0 2022-01-09 op
181 fb1a36c0 2022-01-09 op nl : '\n' optnl /* one or more newlines */
182 fb1a36c0 2022-01-09 op ;
183 fb1a36c0 2022-01-09 op
184 fb1a36c0 2022-01-09 op arrow : '=' '>' ;
185 fb1a36c0 2022-01-09 op
186 fb1a36c0 2022-01-09 op comma : ',' optnl
187 fb1a36c0 2022-01-09 op ;
188 fb1a36c0 2022-01-09 op
189 fb1a36c0 2022-01-09 op varset : STRING '=' string {
190 fb1a36c0 2022-01-09 op char *s = $1;
191 fb1a36c0 2022-01-09 op if (verbose)
192 fb1a36c0 2022-01-09 op printf("%s = \"%s\"\n", $1, $3);
193 fb1a36c0 2022-01-09 op while (*s++) {
194 fb1a36c0 2022-01-09 op if (isspace((unsigned char)*s)) {
195 fb1a36c0 2022-01-09 op yyerror("macro name cannot contain "
196 fb1a36c0 2022-01-09 op "whitespace");
197 fb1a36c0 2022-01-09 op free($1);
198 fb1a36c0 2022-01-09 op free($3);
199 fb1a36c0 2022-01-09 op YYERROR;
200 fb1a36c0 2022-01-09 op }
201 fb1a36c0 2022-01-09 op }
202 fb1a36c0 2022-01-09 op if (symset($1, $3, 0) == -1)
203 fb1a36c0 2022-01-09 op fatal("cannot store variable");
204 fb1a36c0 2022-01-09 op free($1);
205 fb1a36c0 2022-01-09 op free($3);
206 fb1a36c0 2022-01-09 op }
207 fb1a36c0 2022-01-09 op ;
208 fb1a36c0 2022-01-09 op
209 fb1a36c0 2022-01-09 op pki : PKI STRING CERT STRING { add_cert($2, $4); }
210 fb1a36c0 2022-01-09 op | PKI STRING KEY STRING { add_key($2, $4); }
211 fb1a36c0 2022-01-09 op ;
212 fb1a36c0 2022-01-09 op
213 fb1a36c0 2022-01-09 op table_kp : string arrow string optnl {
214 fb1a36c0 2022-01-09 op if (table_add(table, $1, $3) == -1)
215 fb1a36c0 2022-01-09 op yyerror("can't add to table %s",
216 fb1a36c0 2022-01-09 op table->t_name);
217 fb1a36c0 2022-01-09 op free($1);
218 fb1a36c0 2022-01-09 op free($3);
219 fb1a36c0 2022-01-09 op }
220 fb1a36c0 2022-01-09 op ;
221 fb1a36c0 2022-01-09 op
222 fb1a36c0 2022-01-09 op table_kps : table_kp
223 fb1a36c0 2022-01-09 op | table_kp comma table_kps
224 fb1a36c0 2022-01-09 op ;
225 fb1a36c0 2022-01-09 op
226 fb1a36c0 2022-01-09 op stringel : STRING {
227 fb1a36c0 2022-01-09 op if (table_add(table, $1, NULL) == -1)
228 fb1a36c0 2022-01-09 op yyerror("can't add to table %s",
229 fb1a36c0 2022-01-09 op table->t_name);
230 fb1a36c0 2022-01-09 op free($1);
231 fb1a36c0 2022-01-09 op }
232 fb1a36c0 2022-01-09 op ;
233 fb1a36c0 2022-01-09 op
234 fb1a36c0 2022-01-09 op string_list : stringel
235 fb1a36c0 2022-01-09 op | stringel comma string_list
236 fb1a36c0 2022-01-09 op ;
237 fb1a36c0 2022-01-09 op
238 fb1a36c0 2022-01-09 op table_vals : table_kps
239 fb1a36c0 2022-01-09 op | string_list
240 fb1a36c0 2022-01-09 op ;
241 fb1a36c0 2022-01-09 op
242 fb1a36c0 2022-01-09 op table : TABLE STRING STRING {
243 fb1a36c0 2022-01-09 op char *p;
244 fb1a36c0 2022-01-09 op
245 fb1a36c0 2022-01-09 op if ((p = strchr($3, ':')) == NULL) {
246 fb1a36c0 2022-01-09 op yyerror("invalid table %s", $2);
247 fb1a36c0 2022-01-09 op YYERROR;
248 fb1a36c0 2022-01-09 op }
249 fb1a36c0 2022-01-09 op
250 fb1a36c0 2022-01-09 op *p = '\0';
251 fb1a36c0 2022-01-09 op add_table($2, $3, p+1);
252 fb1a36c0 2022-01-09 op free($2);
253 fb1a36c0 2022-01-09 op free($3);
254 fb1a36c0 2022-01-09 op }
255 fb1a36c0 2022-01-09 op | TABLE STRING {
256 fb1a36c0 2022-01-09 op add_table($2, "static", NULL);
257 fb1a36c0 2022-01-09 op } '{' optnl table_vals '}' {
258 fb1a36c0 2022-01-09 op table = NULL;
259 fb1a36c0 2022-01-09 op }
260 fb1a36c0 2022-01-09 op ;
261 fb1a36c0 2022-01-09 op
262 fb1a36c0 2022-01-09 op tableref : '<' STRING '>' {
263 fb1a36c0 2022-01-09 op struct table *t;
264 fb1a36c0 2022-01-09 op
265 fb1a36c0 2022-01-09 op t = findtable($2);
266 fb1a36c0 2022-01-09 op free($2);
267 fb1a36c0 2022-01-09 op if (t == NULL)
268 fb1a36c0 2022-01-09 op YYERROR;
269 fb1a36c0 2022-01-09 op $$ = t;
270 fb1a36c0 2022-01-09 op }
271 fb1a36c0 2022-01-09 op ;
272 fb1a36c0 2022-01-09 op
273 fb1a36c0 2022-01-09 op listen : LISTEN { listener = listen_new(); }
274 fb1a36c0 2022-01-09 op listen_opts {
275 fb1a36c0 2022-01-09 op if (listener->auth_table == NULL)
276 fb1a36c0 2022-01-09 op yyerror("missing auth table");
277 fb1a36c0 2022-01-09 op if (!(listener->flags & L_TLS))
278 fb1a36c0 2022-01-09 op yyerror("can't define a non-tls listener");
279 fb1a36c0 2022-01-09 op listener = NULL;
280 fb1a36c0 2022-01-09 op }
281 fb1a36c0 2022-01-09 op ;
282 fb1a36c0 2022-01-09 op
283 fb1a36c0 2022-01-09 op listen_opts : listen_opt
284 fb1a36c0 2022-01-09 op | listen_opt listen_opts
285 fb1a36c0 2022-01-09 op ;
286 fb1a36c0 2022-01-09 op
287 fb1a36c0 2022-01-09 op listen_opt : ON STRING PORT NUMBER {
288 fb1a36c0 2022-01-09 op if (*listener->iface != '\0')
289 fb1a36c0 2022-01-09 op yyerror("listen address and port already"
290 fb1a36c0 2022-01-09 op " defined");
291 fb1a36c0 2022-01-09 op strlcpy(listener->iface, $2, sizeof(listener->iface));
292 fb1a36c0 2022-01-09 op listener->port = $4;
293 fb1a36c0 2022-01-09 op }
294 fb1a36c0 2022-01-09 op | TLS PKI STRING {
295 fb1a36c0 2022-01-09 op if (*listener->pki != '\0')
296 fb1a36c0 2022-01-09 op yyerror("listen tls pki already defined");
297 fb1a36c0 2022-01-09 op listener->flags |= L_TLS;
298 fb1a36c0 2022-01-09 op strlcpy(listener->pki, $3, sizeof(listener->pki));
299 fb1a36c0 2022-01-09 op }
300 fb1a36c0 2022-01-09 op | AUTH tableref {
301 fb1a36c0 2022-01-09 op if (listener->auth_table != NULL)
302 fb1a36c0 2022-01-09 op yyerror("listen auth already defined");
303 fb1a36c0 2022-01-09 op listener->auth_table = $2;
304 fb1a36c0 2022-01-09 op }
305 fb1a36c0 2022-01-09 op | USERDATA tableref {
306 fb1a36c0 2022-01-09 op if (listener->userdata_table != NULL)
307 fb1a36c0 2022-01-09 op yyerror("userdata table already defined");
308 fb1a36c0 2022-01-09 op listener->userdata_table = $2;
309 fb1a36c0 2022-01-09 op }
310 fb1a36c0 2022-01-09 op | VIRTUAL tableref {
311 fb1a36c0 2022-01-09 op if (listener->virtual_table != NULL)
312 fb1a36c0 2022-01-09 op yyerror("virtual table already defined");
313 fb1a36c0 2022-01-09 op listener->virtual_table = $2;
314 fb1a36c0 2022-01-09 op }
315 fb1a36c0 2022-01-09 op ;
316 fb1a36c0 2022-01-09 op
317 fb1a36c0 2022-01-09 op %%
318 fb1a36c0 2022-01-09 op
319 fb1a36c0 2022-01-09 op struct keywords {
320 fb1a36c0 2022-01-09 op const char *k_name;
321 fb1a36c0 2022-01-09 op int k_val;
322 fb1a36c0 2022-01-09 op };
323 fb1a36c0 2022-01-09 op
324 fb1a36c0 2022-01-09 op int
325 fb1a36c0 2022-01-09 op yyerror(const char *fmt, ...)
326 fb1a36c0 2022-01-09 op {
327 fb1a36c0 2022-01-09 op va_list ap;
328 fb1a36c0 2022-01-09 op char *msg;
329 fb1a36c0 2022-01-09 op
330 fb1a36c0 2022-01-09 op file->errors++;
331 fb1a36c0 2022-01-09 op va_start(ap, fmt);
332 fb1a36c0 2022-01-09 op if (vasprintf(&msg, fmt, ap) == -1)
333 fb1a36c0 2022-01-09 op fatalx("yyerror vasprintf");
334 fb1a36c0 2022-01-09 op va_end(ap);
335 fb1a36c0 2022-01-09 op logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
336 fb1a36c0 2022-01-09 op free(msg);
337 fb1a36c0 2022-01-09 op return 0;
338 fb1a36c0 2022-01-09 op }
339 fb1a36c0 2022-01-09 op
340 fb1a36c0 2022-01-09 op int
341 fb1a36c0 2022-01-09 op kw_cmp(const void *k, const void *e)
342 fb1a36c0 2022-01-09 op {
343 fb1a36c0 2022-01-09 op return strcmp(k, ((const struct keywords *)e)->k_name);
344 fb1a36c0 2022-01-09 op }
345 fb1a36c0 2022-01-09 op
346 fb1a36c0 2022-01-09 op int
347 fb1a36c0 2022-01-09 op lookup(char *s)
348 fb1a36c0 2022-01-09 op {
349 fb1a36c0 2022-01-09 op /* This has to be sorted always. */
350 fb1a36c0 2022-01-09 op static const struct keywords keywords[] = {
351 fb1a36c0 2022-01-09 op {"auth", AUTH},
352 fb1a36c0 2022-01-09 op {"cert", CERT},
353 fb1a36c0 2022-01-09 op {"include", INCLUDE},
354 fb1a36c0 2022-01-09 op {"key", KEY},
355 fb1a36c0 2022-01-09 op {"listen", LISTEN},
356 fb1a36c0 2022-01-09 op {"no", NO},
357 fb1a36c0 2022-01-09 op {"on", ON},
358 fb1a36c0 2022-01-09 op {"pki", PKI},
359 fb1a36c0 2022-01-09 op {"port", PORT},
360 fb1a36c0 2022-01-09 op {"table", TABLE},
361 fb1a36c0 2022-01-09 op {"tls", TLS},
362 fb1a36c0 2022-01-09 op {"userdata", USERDATA},
363 fb1a36c0 2022-01-09 op {"virtual", VIRTUAL},
364 fb1a36c0 2022-01-09 op {"yes", YES},
365 fb1a36c0 2022-01-09 op };
366 fb1a36c0 2022-01-09 op const struct keywords *p;
367 fb1a36c0 2022-01-09 op
368 fb1a36c0 2022-01-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
369 fb1a36c0 2022-01-09 op sizeof(keywords[0]), kw_cmp);
370 fb1a36c0 2022-01-09 op
371 fb1a36c0 2022-01-09 op if (p)
372 fb1a36c0 2022-01-09 op return p->k_val;
373 fb1a36c0 2022-01-09 op else
374 fb1a36c0 2022-01-09 op return STRING;
375 fb1a36c0 2022-01-09 op }
376 fb1a36c0 2022-01-09 op
377 fb1a36c0 2022-01-09 op #define START_EXPAND 1
378 fb1a36c0 2022-01-09 op #define DONE_EXPAND 2
379 fb1a36c0 2022-01-09 op
380 fb1a36c0 2022-01-09 op static int expanding;
381 fb1a36c0 2022-01-09 op
382 fb1a36c0 2022-01-09 op int
383 fb1a36c0 2022-01-09 op igetc(void)
384 fb1a36c0 2022-01-09 op {
385 fb1a36c0 2022-01-09 op int c;
386 fb1a36c0 2022-01-09 op
387 fb1a36c0 2022-01-09 op while (1) {
388 fb1a36c0 2022-01-09 op if (file->ungetpos > 0)
389 fb1a36c0 2022-01-09 op c = file->ungetbuf[--file->ungetpos];
390 fb1a36c0 2022-01-09 op else
391 fb1a36c0 2022-01-09 op c = getc(file->stream);
392 fb1a36c0 2022-01-09 op
393 fb1a36c0 2022-01-09 op if (c == START_EXPAND)
394 fb1a36c0 2022-01-09 op expanding = 1;
395 fb1a36c0 2022-01-09 op else if (c == DONE_EXPAND)
396 fb1a36c0 2022-01-09 op expanding = 0;
397 fb1a36c0 2022-01-09 op else
398 fb1a36c0 2022-01-09 op break;
399 fb1a36c0 2022-01-09 op }
400 fb1a36c0 2022-01-09 op return c;
401 fb1a36c0 2022-01-09 op }
402 fb1a36c0 2022-01-09 op
403 fb1a36c0 2022-01-09 op int
404 fb1a36c0 2022-01-09 op lgetc(int quotec)
405 fb1a36c0 2022-01-09 op {
406 fb1a36c0 2022-01-09 op int c, next;
407 fb1a36c0 2022-01-09 op
408 fb1a36c0 2022-01-09 op if (quotec) {
409 fb1a36c0 2022-01-09 op if ((c = igetc()) == EOF) {
410 fb1a36c0 2022-01-09 op yyerror("reached end of file while parsing "
411 fb1a36c0 2022-01-09 op "quoted string");
412 fb1a36c0 2022-01-09 op if (file == topfile || popfile() == EOF)
413 fb1a36c0 2022-01-09 op return EOF;
414 fb1a36c0 2022-01-09 op return quotec;
415 fb1a36c0 2022-01-09 op }
416 fb1a36c0 2022-01-09 op return c;
417 fb1a36c0 2022-01-09 op }
418 fb1a36c0 2022-01-09 op
419 fb1a36c0 2022-01-09 op while ((c = igetc()) == '\\') {
420 fb1a36c0 2022-01-09 op next = igetc();
421 fb1a36c0 2022-01-09 op if (next != '\n') {
422 fb1a36c0 2022-01-09 op c = next;
423 fb1a36c0 2022-01-09 op break;
424 fb1a36c0 2022-01-09 op }
425 fb1a36c0 2022-01-09 op yylval.lineno = file->lineno;
426 fb1a36c0 2022-01-09 op file->lineno++;
427 fb1a36c0 2022-01-09 op }
428 fb1a36c0 2022-01-09 op
429 fb1a36c0 2022-01-09 op if (c == EOF) {
430 fb1a36c0 2022-01-09 op /*
431 fb1a36c0 2022-01-09 op * Fake EOL when hit EOF for the first time. This gets line
432 fb1a36c0 2022-01-09 op * count right if last line in included file is syntactically
433 fb1a36c0 2022-01-09 op * invalid and has no newline.
434 fb1a36c0 2022-01-09 op */
435 fb1a36c0 2022-01-09 op if (file->eof_reached == 0) {
436 fb1a36c0 2022-01-09 op file->eof_reached = 1;
437 fb1a36c0 2022-01-09 op return '\n';
438 fb1a36c0 2022-01-09 op }
439 fb1a36c0 2022-01-09 op while (c == EOF) {
440 fb1a36c0 2022-01-09 op if (file == topfile || popfile() == EOF)
441 fb1a36c0 2022-01-09 op return EOF;
442 fb1a36c0 2022-01-09 op c = igetc();
443 fb1a36c0 2022-01-09 op }
444 fb1a36c0 2022-01-09 op }
445 fb1a36c0 2022-01-09 op return c;
446 fb1a36c0 2022-01-09 op }
447 fb1a36c0 2022-01-09 op
448 fb1a36c0 2022-01-09 op void
449 fb1a36c0 2022-01-09 op lungetc(int c)
450 fb1a36c0 2022-01-09 op {
451 fb1a36c0 2022-01-09 op if (c == EOF)
452 fb1a36c0 2022-01-09 op return;
453 fb1a36c0 2022-01-09 op
454 fb1a36c0 2022-01-09 op if (file->ungetpos >= file->ungetsize) {
455 fb1a36c0 2022-01-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
456 fb1a36c0 2022-01-09 op if (p == NULL)
457 fb1a36c0 2022-01-09 op err(1, "lungetc");
458 fb1a36c0 2022-01-09 op file->ungetbuf = p;
459 fb1a36c0 2022-01-09 op file->ungetsize *= 2;
460 fb1a36c0 2022-01-09 op }
461 fb1a36c0 2022-01-09 op file->ungetbuf[file->ungetpos++] = c;
462 fb1a36c0 2022-01-09 op }
463 fb1a36c0 2022-01-09 op
464 fb1a36c0 2022-01-09 op int
465 fb1a36c0 2022-01-09 op findeol(void)
466 fb1a36c0 2022-01-09 op {
467 fb1a36c0 2022-01-09 op int c;
468 fb1a36c0 2022-01-09 op
469 fb1a36c0 2022-01-09 op /* Skip to either EOF or the first real EOL. */
470 fb1a36c0 2022-01-09 op while (1) {
471 fb1a36c0 2022-01-09 op c = lgetc(0);
472 fb1a36c0 2022-01-09 op if (c == '\n') {
473 fb1a36c0 2022-01-09 op file->lineno++;
474 fb1a36c0 2022-01-09 op break;
475 fb1a36c0 2022-01-09 op }
476 fb1a36c0 2022-01-09 op if (c == EOF)
477 fb1a36c0 2022-01-09 op break;
478 fb1a36c0 2022-01-09 op }
479 fb1a36c0 2022-01-09 op return ERROR;
480 fb1a36c0 2022-01-09 op }
481 fb1a36c0 2022-01-09 op
482 fb1a36c0 2022-01-09 op #if 0
483 fb1a36c0 2022-01-09 op int my_yylex(void);
484 fb1a36c0 2022-01-09 op
485 fb1a36c0 2022-01-09 op int
486 fb1a36c0 2022-01-09 op yylex(void)
487 fb1a36c0 2022-01-09 op {
488 fb1a36c0 2022-01-09 op int x;
489 fb1a36c0 2022-01-09 op
490 fb1a36c0 2022-01-09 op switch (x = my_yylex()) {
491 fb1a36c0 2022-01-09 op case AUTH:
492 fb1a36c0 2022-01-09 op puts("auth");
493 fb1a36c0 2022-01-09 op break;
494 fb1a36c0 2022-01-09 op case CERT:
495 fb1a36c0 2022-01-09 op puts("cert");
496 fb1a36c0 2022-01-09 op break;
497 fb1a36c0 2022-01-09 op case ERROR:
498 fb1a36c0 2022-01-09 op puts("error");
499 fb1a36c0 2022-01-09 op break;
500 fb1a36c0 2022-01-09 op case INCLUDE:
501 fb1a36c0 2022-01-09 op puts("include");
502 fb1a36c0 2022-01-09 op break;
503 fb1a36c0 2022-01-09 op case KEY:
504 fb1a36c0 2022-01-09 op puts("key");
505 fb1a36c0 2022-01-09 op break;
506 fb1a36c0 2022-01-09 op case LISTEN:
507 fb1a36c0 2022-01-09 op puts("listen");
508 fb1a36c0 2022-01-09 op break;
509 fb1a36c0 2022-01-09 op case NO:
510 fb1a36c0 2022-01-09 op puts("no");
511 fb1a36c0 2022-01-09 op break;
512 fb1a36c0 2022-01-09 op case ON:
513 fb1a36c0 2022-01-09 op puts("on");
514 fb1a36c0 2022-01-09 op break;
515 fb1a36c0 2022-01-09 op case PKI:
516 fb1a36c0 2022-01-09 op puts("pki");
517 fb1a36c0 2022-01-09 op break;
518 fb1a36c0 2022-01-09 op case PORT:
519 fb1a36c0 2022-01-09 op puts("port");
520 fb1a36c0 2022-01-09 op break;
521 fb1a36c0 2022-01-09 op case TABLE:
522 fb1a36c0 2022-01-09 op puts("table");
523 fb1a36c0 2022-01-09 op break;
524 fb1a36c0 2022-01-09 op case TLS:
525 fb1a36c0 2022-01-09 op puts("tls");
526 fb1a36c0 2022-01-09 op break;
527 fb1a36c0 2022-01-09 op case YES:
528 fb1a36c0 2022-01-09 op puts("yes");
529 fb1a36c0 2022-01-09 op break;
530 fb1a36c0 2022-01-09 op case STRING:
531 fb1a36c0 2022-01-09 op printf("string \"%s\"\n", yylval.v.string);
532 fb1a36c0 2022-01-09 op break;
533 fb1a36c0 2022-01-09 op case NUMBER:
534 fb1a36c0 2022-01-09 op printf("number %"PRIi64"\n", yylval.v.number);
535 fb1a36c0 2022-01-09 op default:
536 fb1a36c0 2022-01-09 op printf("character ");
537 fb1a36c0 2022-01-09 op if (x == '\n')
538 fb1a36c0 2022-01-09 op printf("\\n");
539 fb1a36c0 2022-01-09 op else
540 fb1a36c0 2022-01-09 op printf("%c", x);
541 fb1a36c0 2022-01-09 op printf(" [0x%x]", x);
542 fb1a36c0 2022-01-09 op printf("\n");
543 fb1a36c0 2022-01-09 op break;
544 fb1a36c0 2022-01-09 op }
545 fb1a36c0 2022-01-09 op
546 fb1a36c0 2022-01-09 op return x;
547 fb1a36c0 2022-01-09 op }
548 fb1a36c0 2022-01-09 op
549 fb1a36c0 2022-01-09 op int
550 fb1a36c0 2022-01-09 op my_yylex(void)
551 fb1a36c0 2022-01-09 op #else
552 fb1a36c0 2022-01-09 op int
553 fb1a36c0 2022-01-09 op yylex(void)
554 fb1a36c0 2022-01-09 op #endif
555 fb1a36c0 2022-01-09 op {
556 fb1a36c0 2022-01-09 op char buf[8096];
557 fb1a36c0 2022-01-09 op char *p, *val;
558 fb1a36c0 2022-01-09 op int quotec, next, c;
559 fb1a36c0 2022-01-09 op int token;
560 fb1a36c0 2022-01-09 op
561 fb1a36c0 2022-01-09 op top:
562 fb1a36c0 2022-01-09 op p = buf;
563 fb1a36c0 2022-01-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
564 fb1a36c0 2022-01-09 op ; /* nothing */
565 fb1a36c0 2022-01-09 op
566 fb1a36c0 2022-01-09 op yylval.lineno = file->lineno;
567 fb1a36c0 2022-01-09 op if (c == '#')
568 fb1a36c0 2022-01-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
569 fb1a36c0 2022-01-09 op ; /* nothing */
570 fb1a36c0 2022-01-09 op if (c == '$' && !expanding) {
571 fb1a36c0 2022-01-09 op while (1) {
572 fb1a36c0 2022-01-09 op if ((c = lgetc(0)) == EOF)
573 fb1a36c0 2022-01-09 op return 0;
574 fb1a36c0 2022-01-09 op
575 fb1a36c0 2022-01-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
576 fb1a36c0 2022-01-09 op yyerror("string too long");
577 fb1a36c0 2022-01-09 op return findeol();
578 fb1a36c0 2022-01-09 op }
579 fb1a36c0 2022-01-09 op if (isalnum(c) || c == '_') {
580 fb1a36c0 2022-01-09 op *p++ = c;
581 fb1a36c0 2022-01-09 op continue;
582 fb1a36c0 2022-01-09 op }
583 fb1a36c0 2022-01-09 op *p = '\0';
584 fb1a36c0 2022-01-09 op lungetc(c);
585 fb1a36c0 2022-01-09 op break;
586 fb1a36c0 2022-01-09 op }
587 fb1a36c0 2022-01-09 op val = symget(buf);
588 fb1a36c0 2022-01-09 op if (val == NULL) {
589 fb1a36c0 2022-01-09 op yyerror("macro '%s' not defined", buf);
590 fb1a36c0 2022-01-09 op return findeol();
591 fb1a36c0 2022-01-09 op }
592 fb1a36c0 2022-01-09 op p = val + strlen(val) - 1;
593 fb1a36c0 2022-01-09 op lungetc(DONE_EXPAND);
594 fb1a36c0 2022-01-09 op while (p >= val) {
595 fb1a36c0 2022-01-09 op lungetc((unsigned char)*p);
596 fb1a36c0 2022-01-09 op p--;
597 fb1a36c0 2022-01-09 op }
598 fb1a36c0 2022-01-09 op lungetc(START_EXPAND);
599 fb1a36c0 2022-01-09 op goto top;
600 fb1a36c0 2022-01-09 op }
601 fb1a36c0 2022-01-09 op
602 fb1a36c0 2022-01-09 op switch (c) {
603 fb1a36c0 2022-01-09 op case '\'':
604 fb1a36c0 2022-01-09 op case '"':
605 fb1a36c0 2022-01-09 op quotec = c;
606 fb1a36c0 2022-01-09 op while (1) {
607 fb1a36c0 2022-01-09 op if ((c = lgetc(quotec)) == EOF)
608 fb1a36c0 2022-01-09 op return 0;
609 fb1a36c0 2022-01-09 op if (c == '\n') {
610 fb1a36c0 2022-01-09 op file->lineno++;
611 fb1a36c0 2022-01-09 op continue;
612 fb1a36c0 2022-01-09 op } else if (c == '\\') {
613 fb1a36c0 2022-01-09 op if ((next = lgetc(quotec)) == EOF)
614 fb1a36c0 2022-01-09 op return (0);
615 fb1a36c0 2022-01-09 op if (next == quotec || next == ' ' ||
616 fb1a36c0 2022-01-09 op next == '\t')
617 fb1a36c0 2022-01-09 op c = next;
618 fb1a36c0 2022-01-09 op else if (next == '\n') {
619 fb1a36c0 2022-01-09 op file->lineno++;
620 fb1a36c0 2022-01-09 op continue;
621 fb1a36c0 2022-01-09 op } else
622 fb1a36c0 2022-01-09 op lungetc(next);
623 fb1a36c0 2022-01-09 op } else if (c == quotec) {
624 fb1a36c0 2022-01-09 op *p = '\0';
625 fb1a36c0 2022-01-09 op break;
626 fb1a36c0 2022-01-09 op } else if (c == '\0') {
627 fb1a36c0 2022-01-09 op yyerror("syntax error");
628 fb1a36c0 2022-01-09 op return findeol();
629 fb1a36c0 2022-01-09 op }
630 fb1a36c0 2022-01-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
631 fb1a36c0 2022-01-09 op yyerror("string too long");
632 fb1a36c0 2022-01-09 op return findeol();
633 fb1a36c0 2022-01-09 op }
634 fb1a36c0 2022-01-09 op *p++ = c;
635 fb1a36c0 2022-01-09 op }
636 fb1a36c0 2022-01-09 op yylval.v.string = strdup(buf);
637 fb1a36c0 2022-01-09 op if (yylval.v.string == NULL)
638 fb1a36c0 2022-01-09 op err(1, "yylex: strdup");
639 fb1a36c0 2022-01-09 op return STRING;
640 fb1a36c0 2022-01-09 op }
641 fb1a36c0 2022-01-09 op
642 fb1a36c0 2022-01-09 op #define allowed_to_end_number(x) \
643 fb1a36c0 2022-01-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
644 fb1a36c0 2022-01-09 op
645 fb1a36c0 2022-01-09 op if (c == '-' || isdigit(c)) {
646 fb1a36c0 2022-01-09 op do {
647 fb1a36c0 2022-01-09 op *p++ = c;
648 fb1a36c0 2022-01-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
649 fb1a36c0 2022-01-09 op yyerror("string too long");
650 fb1a36c0 2022-01-09 op return findeol();
651 fb1a36c0 2022-01-09 op }
652 fb1a36c0 2022-01-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
653 fb1a36c0 2022-01-09 op lungetc(c);
654 fb1a36c0 2022-01-09 op if (p == buf + 1 && buf[0] == '-')
655 fb1a36c0 2022-01-09 op goto nodigits;
656 fb1a36c0 2022-01-09 op if (c == EOF || allowed_to_end_number(c)) {
657 fb1a36c0 2022-01-09 op const char *errstr = NULL;
658 fb1a36c0 2022-01-09 op
659 fb1a36c0 2022-01-09 op *p = '\0';
660 fb1a36c0 2022-01-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
661 fb1a36c0 2022-01-09 op LLONG_MAX, &errstr);
662 fb1a36c0 2022-01-09 op if (errstr) {
663 fb1a36c0 2022-01-09 op yyerror("\"%s\" invalid number: %s",
664 fb1a36c0 2022-01-09 op buf, errstr);
665 fb1a36c0 2022-01-09 op return findeol();
666 fb1a36c0 2022-01-09 op }
667 fb1a36c0 2022-01-09 op return NUMBER;
668 fb1a36c0 2022-01-09 op } else {
669 fb1a36c0 2022-01-09 op nodigits:
670 fb1a36c0 2022-01-09 op while (p > buf + 1)
671 fb1a36c0 2022-01-09 op lungetc((unsigned char)*--p);
672 fb1a36c0 2022-01-09 op c = (unsigned char)*--p;
673 fb1a36c0 2022-01-09 op if (c == '-')
674 fb1a36c0 2022-01-09 op return c;
675 fb1a36c0 2022-01-09 op }
676 fb1a36c0 2022-01-09 op }
677 fb1a36c0 2022-01-09 op
678 fb1a36c0 2022-01-09 op #define allowed_in_string(x) \
679 fb1a36c0 2022-01-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
680 fb1a36c0 2022-01-09 op x != '{' && x != '}' && \
681 fb1a36c0 2022-01-09 op x != '!' && x != '=' && x != '#' && \
682 fb1a36c0 2022-01-09 op x != ',' && x != '>'))
683 fb1a36c0 2022-01-09 op
684 fb1a36c0 2022-01-09 op if (isalnum(c) || c == ':' || c == '_') {
685 fb1a36c0 2022-01-09 op do {
686 fb1a36c0 2022-01-09 op *p++ = c;
687 fb1a36c0 2022-01-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
688 fb1a36c0 2022-01-09 op yyerror("string too long");
689 fb1a36c0 2022-01-09 op return findeol();
690 fb1a36c0 2022-01-09 op }
691 fb1a36c0 2022-01-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
692 fb1a36c0 2022-01-09 op lungetc(c);
693 fb1a36c0 2022-01-09 op *p = '\0';
694 fb1a36c0 2022-01-09 op if ((token = lookup(buf)) == STRING)
695 fb1a36c0 2022-01-09 op if ((yylval.v.string = strdup(buf)) == NULL)
696 fb1a36c0 2022-01-09 op err(1, "yylex: strdup");
697 fb1a36c0 2022-01-09 op return token;
698 fb1a36c0 2022-01-09 op }
699 fb1a36c0 2022-01-09 op if (c == '\n') {
700 fb1a36c0 2022-01-09 op yylval.lineno = file->lineno;
701 fb1a36c0 2022-01-09 op file->lineno++;
702 fb1a36c0 2022-01-09 op }
703 fb1a36c0 2022-01-09 op if (c == EOF)
704 fb1a36c0 2022-01-09 op return 0;
705 fb1a36c0 2022-01-09 op return c;
706 fb1a36c0 2022-01-09 op }
707 fb1a36c0 2022-01-09 op
708 fb1a36c0 2022-01-09 op int
709 fb1a36c0 2022-01-09 op check_file_secrecy(int fd, const char *fname)
710 fb1a36c0 2022-01-09 op {
711 fb1a36c0 2022-01-09 op struct stat st;
712 fb1a36c0 2022-01-09 op
713 fb1a36c0 2022-01-09 op if (fstat(fd, &st)) {
714 fb1a36c0 2022-01-09 op log_warn("cannot stat %s", fname);
715 fb1a36c0 2022-01-09 op return -1;
716 fb1a36c0 2022-01-09 op }
717 fb1a36c0 2022-01-09 op if (st.st_uid != 0 && st.st_uid != getuid()) {
718 fb1a36c0 2022-01-09 op log_warnx("%s: owner not root or current user", fname);
719 fb1a36c0 2022-01-09 op return -1;
720 fb1a36c0 2022-01-09 op }
721 fb1a36c0 2022-01-09 op if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
722 fb1a36c0 2022-01-09 op log_warnx("%s: group writable or world read/writable", fname);
723 fb1a36c0 2022-01-09 op return -1;
724 fb1a36c0 2022-01-09 op }
725 fb1a36c0 2022-01-09 op return 0;
726 fb1a36c0 2022-01-09 op }
727 fb1a36c0 2022-01-09 op
728 fb1a36c0 2022-01-09 op struct file *
729 fb1a36c0 2022-01-09 op pushfile(const char *name, int secret)
730 fb1a36c0 2022-01-09 op {
731 fb1a36c0 2022-01-09 op struct file *nfile;
732 fb1a36c0 2022-01-09 op
733 fb1a36c0 2022-01-09 op if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
734 fb1a36c0 2022-01-09 op log_warn("calloc");
735 fb1a36c0 2022-01-09 op return NULL;
736 fb1a36c0 2022-01-09 op }
737 fb1a36c0 2022-01-09 op if ((nfile->name = strdup(name)) == NULL) {
738 fb1a36c0 2022-01-09 op log_warn("strdup");
739 fb1a36c0 2022-01-09 op free(nfile);
740 fb1a36c0 2022-01-09 op return NULL;
741 fb1a36c0 2022-01-09 op }
742 fb1a36c0 2022-01-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
743 fb1a36c0 2022-01-09 op log_warn("%s", nfile->name);
744 fb1a36c0 2022-01-09 op free(nfile->name);
745 fb1a36c0 2022-01-09 op free(nfile);
746 fb1a36c0 2022-01-09 op return NULL;
747 fb1a36c0 2022-01-09 op } else if (secret &&
748 fb1a36c0 2022-01-09 op check_file_secrecy(fileno(nfile->stream), nfile->name)) {
749 fb1a36c0 2022-01-09 op fclose(nfile->stream);
750 fb1a36c0 2022-01-09 op free(nfile->name);
751 fb1a36c0 2022-01-09 op free(nfile);
752 fb1a36c0 2022-01-09 op return NULL;
753 fb1a36c0 2022-01-09 op }
754 fb1a36c0 2022-01-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
755 fb1a36c0 2022-01-09 op nfile->ungetsize = 16;
756 fb1a36c0 2022-01-09 op nfile->ungetbuf = malloc(nfile->ungetsize);
757 fb1a36c0 2022-01-09 op if (nfile->ungetbuf == NULL) {
758 fb1a36c0 2022-01-09 op log_warn("malloc");
759 fb1a36c0 2022-01-09 op fclose(nfile->stream);
760 fb1a36c0 2022-01-09 op free(nfile->name);
761 fb1a36c0 2022-01-09 op free(nfile);
762 fb1a36c0 2022-01-09 op return NULL;
763 fb1a36c0 2022-01-09 op }
764 fb1a36c0 2022-01-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
765 fb1a36c0 2022-01-09 op return nfile;
766 fb1a36c0 2022-01-09 op }
767 fb1a36c0 2022-01-09 op
768 fb1a36c0 2022-01-09 op int
769 fb1a36c0 2022-01-09 op popfile(void)
770 fb1a36c0 2022-01-09 op {
771 fb1a36c0 2022-01-09 op struct file *prev;
772 fb1a36c0 2022-01-09 op
773 fb1a36c0 2022-01-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
774 fb1a36c0 2022-01-09 op prev->errors += file->errors;
775 fb1a36c0 2022-01-09 op
776 fb1a36c0 2022-01-09 op TAILQ_REMOVE(&files, file, entry);
777 fb1a36c0 2022-01-09 op fclose(file->stream);
778 fb1a36c0 2022-01-09 op free(file->name);
779 fb1a36c0 2022-01-09 op free(file->ungetbuf);
780 fb1a36c0 2022-01-09 op free(file);
781 fb1a36c0 2022-01-09 op file = prev;
782 fb1a36c0 2022-01-09 op return file ? 0 : EOF;
783 fb1a36c0 2022-01-09 op }
784 fb1a36c0 2022-01-09 op
785 fb1a36c0 2022-01-09 op struct kd_conf *
786 fb1a36c0 2022-01-09 op parse_config(const char *filename)
787 fb1a36c0 2022-01-09 op {
788 fb1a36c0 2022-01-09 op struct sym *sym, *next;
789 fb1a36c0 2022-01-09 op
790 fb1a36c0 2022-01-09 op counter = 0;
791 fb1a36c0 2022-01-09 op conf = config_new_empty();
792 fb1a36c0 2022-01-09 op
793 fb1a36c0 2022-01-09 op file = pushfile(filename, 0);
794 fb1a36c0 2022-01-09 op if (file == NULL) {
795 fb1a36c0 2022-01-09 op free(conf);
796 fb1a36c0 2022-01-09 op return NULL;
797 fb1a36c0 2022-01-09 op }
798 fb1a36c0 2022-01-09 op topfile = file;
799 fb1a36c0 2022-01-09 op
800 fb1a36c0 2022-01-09 op yyparse();
801 fb1a36c0 2022-01-09 op errors = file->errors;
802 fb1a36c0 2022-01-09 op popfile();
803 fb1a36c0 2022-01-09 op
804 fb1a36c0 2022-01-09 op /* Free macros and check which have not been used. */
805 fb1a36c0 2022-01-09 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
806 fb1a36c0 2022-01-09 op if (verbose && !sym->used)
807 fb1a36c0 2022-01-09 op fprintf(stderr, "warning: macro '%s' not used\n",
808 fb1a36c0 2022-01-09 op sym->nam);
809 fb1a36c0 2022-01-09 op if (!sym->persist) {
810 fb1a36c0 2022-01-09 op free(sym->nam);
811 fb1a36c0 2022-01-09 op free(sym->val);
812 fb1a36c0 2022-01-09 op TAILQ_REMOVE(&symhead, sym, entry);
813 fb1a36c0 2022-01-09 op free(sym);
814 fb1a36c0 2022-01-09 op }
815 fb1a36c0 2022-01-09 op }
816 fb1a36c0 2022-01-09 op
817 fb1a36c0 2022-01-09 op if (errors) {
818 fb1a36c0 2022-01-09 op clear_config(conf);
819 fb1a36c0 2022-01-09 op return NULL;
820 fb1a36c0 2022-01-09 op }
821 fb1a36c0 2022-01-09 op
822 fb1a36c0 2022-01-09 op return conf;
823 fb1a36c0 2022-01-09 op }
824 fb1a36c0 2022-01-09 op
825 fb1a36c0 2022-01-09 op int
826 fb1a36c0 2022-01-09 op symset(const char *nam, const char *val, int persist)
827 fb1a36c0 2022-01-09 op {
828 fb1a36c0 2022-01-09 op struct sym *sym;
829 fb1a36c0 2022-01-09 op
830 fb1a36c0 2022-01-09 op TAILQ_FOREACH(sym, &symhead, entry) {
831 fb1a36c0 2022-01-09 op if (strcmp(nam, sym->nam) == 0)
832 fb1a36c0 2022-01-09 op break;
833 fb1a36c0 2022-01-09 op }
834 fb1a36c0 2022-01-09 op
835 fb1a36c0 2022-01-09 op if (sym != NULL) {
836 fb1a36c0 2022-01-09 op if (sym->persist == 1)
837 fb1a36c0 2022-01-09 op return 0;
838 fb1a36c0 2022-01-09 op else {
839 fb1a36c0 2022-01-09 op free(sym->nam);
840 fb1a36c0 2022-01-09 op free(sym->val);
841 fb1a36c0 2022-01-09 op TAILQ_REMOVE(&symhead, sym, entry);
842 fb1a36c0 2022-01-09 op free(sym);
843 fb1a36c0 2022-01-09 op }
844 fb1a36c0 2022-01-09 op }
845 fb1a36c0 2022-01-09 op if ((sym = calloc(1, sizeof(*sym))) == NULL)
846 fb1a36c0 2022-01-09 op return -1;
847 fb1a36c0 2022-01-09 op
848 fb1a36c0 2022-01-09 op sym->nam = strdup(nam);
849 fb1a36c0 2022-01-09 op if (sym->nam == NULL) {
850 fb1a36c0 2022-01-09 op free(sym);
851 fb1a36c0 2022-01-09 op return -1;
852 fb1a36c0 2022-01-09 op }
853 fb1a36c0 2022-01-09 op sym->val = strdup(val);
854 fb1a36c0 2022-01-09 op if (sym->val == NULL) {
855 fb1a36c0 2022-01-09 op free(sym->nam);
856 fb1a36c0 2022-01-09 op free(sym);
857 fb1a36c0 2022-01-09 op return -1;
858 fb1a36c0 2022-01-09 op }
859 fb1a36c0 2022-01-09 op sym->used = 0;
860 fb1a36c0 2022-01-09 op sym->persist = persist;
861 fb1a36c0 2022-01-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
862 fb1a36c0 2022-01-09 op return 0;
863 fb1a36c0 2022-01-09 op }
864 fb1a36c0 2022-01-09 op
865 fb1a36c0 2022-01-09 op int
866 fb1a36c0 2022-01-09 op cmdline_symset(char *s)
867 fb1a36c0 2022-01-09 op {
868 fb1a36c0 2022-01-09 op char *sym, *val;
869 fb1a36c0 2022-01-09 op int ret;
870 fb1a36c0 2022-01-09 op
871 fb1a36c0 2022-01-09 op if ((val = strrchr(s, '=')) == NULL)
872 fb1a36c0 2022-01-09 op return -1;
873 fb1a36c0 2022-01-09 op sym = strndup(s, val - s);
874 fb1a36c0 2022-01-09 op if (sym == NULL)
875 fb1a36c0 2022-01-09 op errx(1, "%s: strndup", __func__);
876 fb1a36c0 2022-01-09 op ret = symset(sym, val + 1, 1);
877 fb1a36c0 2022-01-09 op free(sym);
878 fb1a36c0 2022-01-09 op
879 fb1a36c0 2022-01-09 op return ret;
880 fb1a36c0 2022-01-09 op }
881 fb1a36c0 2022-01-09 op
882 fb1a36c0 2022-01-09 op char *
883 fb1a36c0 2022-01-09 op symget(const char *nam)
884 fb1a36c0 2022-01-09 op {
885 fb1a36c0 2022-01-09 op struct sym *sym;
886 fb1a36c0 2022-01-09 op
887 fb1a36c0 2022-01-09 op TAILQ_FOREACH(sym, &symhead, entry) {
888 fb1a36c0 2022-01-09 op if (strcmp(nam, sym->nam) == 0) {
889 fb1a36c0 2022-01-09 op sym->used = 1;
890 fb1a36c0 2022-01-09 op return sym->val;
891 fb1a36c0 2022-01-09 op }
892 fb1a36c0 2022-01-09 op }
893 fb1a36c0 2022-01-09 op return NULL;
894 fb1a36c0 2022-01-09 op }
895 fb1a36c0 2022-01-09 op
896 fb1a36c0 2022-01-09 op void
897 fb1a36c0 2022-01-09 op clear_config(struct kd_conf *xconf)
898 fb1a36c0 2022-01-09 op {
899 fb1a36c0 2022-01-09 op /* free stuff? */
900 fb1a36c0 2022-01-09 op
901 fb1a36c0 2022-01-09 op free(xconf);
902 fb1a36c0 2022-01-09 op }
903 fb1a36c0 2022-01-09 op
904 fb1a36c0 2022-01-09 op static void
905 fb1a36c0 2022-01-09 op add_table(const char *name, const char *type, const char *path)
906 fb1a36c0 2022-01-09 op {
907 fb1a36c0 2022-01-09 op if (table_open(conf, name, type, path) == -1)
908 fb1a36c0 2022-01-09 op yyerror("can't initialize table %s", name);
909 fb1a36c0 2022-01-09 op table = STAILQ_FIRST(&conf->table_head)->table;
910 fb1a36c0 2022-01-09 op }
911 fb1a36c0 2022-01-09 op
912 fb1a36c0 2022-01-09 op static struct table *
913 fb1a36c0 2022-01-09 op findtable(const char *name)
914 fb1a36c0 2022-01-09 op {
915 fb1a36c0 2022-01-09 op struct kd_tables_conf *i;
916 fb1a36c0 2022-01-09 op
917 fb1a36c0 2022-01-09 op STAILQ_FOREACH(i, &conf->table_head, entry) {
918 fb1a36c0 2022-01-09 op if (!strcmp(i->table->t_name, name))
919 fb1a36c0 2022-01-09 op return i->table;
920 fb1a36c0 2022-01-09 op }
921 fb1a36c0 2022-01-09 op
922 fb1a36c0 2022-01-09 op yyerror("unknown table %s", name);
923 fb1a36c0 2022-01-09 op return NULL;
924 fb1a36c0 2022-01-09 op }
925 fb1a36c0 2022-01-09 op
926 fb1a36c0 2022-01-09 op static void
927 fb1a36c0 2022-01-09 op add_cert(const char *name, const char *path)
928 fb1a36c0 2022-01-09 op {
929 fb1a36c0 2022-01-09 op struct kd_pki_conf *pki;
930 fb1a36c0 2022-01-09 op
931 fb1a36c0 2022-01-09 op STAILQ_FOREACH(pki, &conf->pki_head, entry) {
932 fb1a36c0 2022-01-09 op if (strcmp(name, pki->name) != 0)
933 fb1a36c0 2022-01-09 op continue;
934 fb1a36c0 2022-01-09 op
935 fb1a36c0 2022-01-09 op if (pki->cert != NULL) {
936 fb1a36c0 2022-01-09 op yyerror("duplicate `pki %s cert'", name);
937 fb1a36c0 2022-01-09 op return;
938 fb1a36c0 2022-01-09 op }
939 fb1a36c0 2022-01-09 op
940 fb1a36c0 2022-01-09 op goto set;
941 fb1a36c0 2022-01-09 op }
942 fb1a36c0 2022-01-09 op
943 fb1a36c0 2022-01-09 op pki = xcalloc(1, sizeof(*pki));
944 fb1a36c0 2022-01-09 op strlcpy(pki->name, name, sizeof(pki->name));
945 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&conf->pki_head, pki, entry);
946 fb1a36c0 2022-01-09 op
947 fb1a36c0 2022-01-09 op set:
948 fb1a36c0 2022-01-09 op if ((pki->cert = tls_load_file(path, &pki->certlen, NULL)) == NULL)
949 fb1a36c0 2022-01-09 op fatal(NULL);
950 fb1a36c0 2022-01-09 op }
951 fb1a36c0 2022-01-09 op
952 fb1a36c0 2022-01-09 op static void
953 fb1a36c0 2022-01-09 op add_key(const char *name, const char *path)
954 fb1a36c0 2022-01-09 op {
955 fb1a36c0 2022-01-09 op struct kd_pki_conf *pki;
956 fb1a36c0 2022-01-09 op
957 fb1a36c0 2022-01-09 op STAILQ_FOREACH(pki, &conf->pki_head, entry) {
958 fb1a36c0 2022-01-09 op if (strcmp(name, pki->name) != 0)
959 fb1a36c0 2022-01-09 op continue;
960 fb1a36c0 2022-01-09 op
961 fb1a36c0 2022-01-09 op if (pki->key != NULL) {
962 fb1a36c0 2022-01-09 op yyerror("duplicate `pki %s key'", name);
963 fb1a36c0 2022-01-09 op return;
964 fb1a36c0 2022-01-09 op }
965 fb1a36c0 2022-01-09 op
966 fb1a36c0 2022-01-09 op goto set;
967 fb1a36c0 2022-01-09 op }
968 fb1a36c0 2022-01-09 op
969 fb1a36c0 2022-01-09 op pki = xcalloc(1, sizeof(*pki));
970 fb1a36c0 2022-01-09 op strlcpy(pki->name, name, sizeof(pki->name));
971 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&conf->pki_head, pki, entry);
972 fb1a36c0 2022-01-09 op
973 fb1a36c0 2022-01-09 op set:
974 fb1a36c0 2022-01-09 op if ((pki->key = tls_load_file(path, &pki->keylen, NULL)) == NULL)
975 fb1a36c0 2022-01-09 op fatal(NULL);
976 fb1a36c0 2022-01-09 op }
977 fb1a36c0 2022-01-09 op
978 fb1a36c0 2022-01-09 op static struct kd_listen_conf *
979 fb1a36c0 2022-01-09 op listen_new(void)
980 fb1a36c0 2022-01-09 op {
981 fb1a36c0 2022-01-09 op struct kd_listen_conf *l;
982 fb1a36c0 2022-01-09 op
983 fb1a36c0 2022-01-09 op l = xcalloc(1, sizeof(*l));
984 fb1a36c0 2022-01-09 op l->id = counter++;
985 fb1a36c0 2022-01-09 op l->fd = -1;
986 fb1a36c0 2022-01-09 op
987 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&conf->listen_head, l, entry);
988 fb1a36c0 2022-01-09 op return l;
989 fb1a36c0 2022-01-09 op }