| | varnish-cache/lib/libvcc/vcc_acl.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2006 Verdens Gang AS |
2 |
|
* Copyright (c) 2006-2010 Varnish Software AS |
3 |
|
* All rights reserved. |
4 |
|
* |
5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
6 |
|
* |
7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
8 |
|
* |
9 |
|
* Redistribution and use in source and binary forms, with or without |
10 |
|
* modification, are permitted provided that the following conditions |
11 |
|
* are met: |
12 |
|
* 1. Redistributions of source code must retain the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
|
* notice, this list of conditions and the following disclaimer in the |
16 |
|
* documentation and/or other materials provided with the distribution. |
17 |
|
* |
18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
|
* SUCH DAMAGE. |
29 |
|
*/ |
30 |
|
|
31 |
|
#include "config.h" |
32 |
|
|
33 |
|
#include <sys/socket.h> |
34 |
|
|
35 |
|
#include <netinet/in.h> |
36 |
|
|
37 |
|
#include <netdb.h> |
38 |
|
#include <stdlib.h> |
39 |
|
#include <string.h> |
40 |
|
|
41 |
|
#include "vcc_compile.h" |
42 |
|
#include <vtcp.h> |
43 |
|
#include <vtree.h> |
44 |
|
#include <vsa.h> |
45 |
|
|
46 |
|
#define ACL_MAXADDR (sizeof(struct in6_addr) + 1) |
47 |
|
|
48 |
|
VRBT_HEAD(acl_tree, acl_e); |
49 |
|
|
50 |
|
struct acl { |
51 |
|
unsigned magic; |
52 |
|
#define VCC_ACL_MAGIC 0xb9fb3cd0 |
53 |
|
|
54 |
|
int flag_log; |
55 |
|
int flag_fold; |
56 |
|
int flag_pedantic; |
57 |
|
int flag_table; |
58 |
|
|
59 |
|
struct acl_tree acl_tree; |
60 |
|
}; |
61 |
|
|
62 |
|
struct acl_e { |
63 |
|
unsigned magic; |
64 |
|
#define VCC_ACL_E_MAGIC 0xcac81e23 |
65 |
|
VRBT_ENTRY(acl_e) branch; |
66 |
|
unsigned char data[ACL_MAXADDR]; |
67 |
|
unsigned mask; |
68 |
|
unsigned not; |
69 |
|
unsigned para; |
70 |
|
unsigned overlapped; |
71 |
|
char *addr; |
72 |
|
const char *fixed; |
73 |
|
struct token *t_addr; |
74 |
|
struct token *t_mask; |
75 |
|
}; |
76 |
|
|
77 |
|
enum acl_cmp_e { |
78 |
|
ACL_EQ = 0, |
79 |
|
ACL_LT = -1, // a < b |
80 |
|
ACL_GT = 1, // b > a |
81 |
|
ACL_CONTAINED = -2, // b contains a |
82 |
|
ACL_CONTAINS = 2, // a contains b |
83 |
|
ACL_LEFT = -3, // a + 1 == b |
84 |
|
ACL_RIGHT = 3 // a == b + 1 |
85 |
|
}; |
86 |
|
|
87 |
|
static void vcc_acl_insert_entry(struct vcc *, struct acl_e **); |
88 |
|
|
89 |
|
/* |
90 |
|
* Compare two acl rules for relation |
91 |
|
*/ |
92 |
|
|
93 |
|
#define CMP(n, a, b) \ |
94 |
|
do { \ |
95 |
|
if ((a) < (b)) \ |
96 |
|
return (enum acl_cmp_e)(-n); \ |
97 |
|
else if ((b) < (a)) \ |
98 |
|
return (n); \ |
99 |
|
} while (0) |
100 |
|
|
101 |
|
#define CMPA(a, b) \ |
102 |
|
do { \ |
103 |
|
if (((a) | 1) == (b)) \ |
104 |
|
return (ACL_LEFT); \ |
105 |
|
else if (((b) | 1) == (a)) \ |
106 |
|
return (ACL_RIGHT); \ |
107 |
|
} while (0) |
108 |
|
|
109 |
|
static void |
110 |
240 |
vcl_acl_free(struct acl_e **aep) |
111 |
|
{ |
112 |
|
struct acl_e *a; |
113 |
|
|
114 |
240 |
TAKE_OBJ_NOTNULL(a, aep, VCC_ACL_E_MAGIC); |
115 |
240 |
free(a->addr); |
116 |
240 |
FREE_OBJ(a); |
117 |
240 |
} |
118 |
|
|
119 |
|
static enum acl_cmp_e |
120 |
19720 |
vcl_acl_cmp(const struct acl_e *ae1, const struct acl_e *ae2) |
121 |
|
{ |
122 |
|
const unsigned char *p1, *p2; |
123 |
|
unsigned m; |
124 |
|
unsigned char a1, a2; |
125 |
|
|
126 |
19720 |
CHECK_OBJ_NOTNULL(ae1, VCC_ACL_E_MAGIC); |
127 |
19720 |
CHECK_OBJ_NOTNULL(ae2, VCC_ACL_E_MAGIC); |
128 |
|
|
129 |
19720 |
p1 = ae1->data; |
130 |
19720 |
p2 = ae2->data; |
131 |
19720 |
m = vmin_t(unsigned, ae1->mask, ae2->mask); |
132 |
102840 |
for (; m >= 8; m -= 8) { |
133 |
93960 |
if (m == 8 && ae1->mask == ae2->mask) |
134 |
1560 |
CMPA(*p1, *p2); |
135 |
176720 |
CMP(ACL_GT, *p1, *p2); |
136 |
83120 |
p1++; |
137 |
83120 |
p2++; |
138 |
83120 |
} |
139 |
8880 |
if (m) { |
140 |
8480 |
assert (m < 8); |
141 |
8480 |
a1 = *p1 >> (8 - m); |
142 |
8480 |
a2 = *p2 >> (8 - m); |
143 |
8480 |
if (ae1->mask == ae2->mask) |
144 |
2720 |
CMPA(a1, a2); |
145 |
11360 |
CMP(ACL_GT, a1, a2); |
146 |
3920 |
} else if (ae1->mask == ae2->mask) { |
147 |
400 |
CMPA(*p1, *p2); |
148 |
200 |
} |
149 |
|
/* Long mask is less than short mask */ |
150 |
4160 |
CMP(ACL_CONTAINS, ae2->mask, ae1->mask); |
151 |
|
|
152 |
240 |
return (ACL_EQ); |
153 |
19720 |
} |
154 |
|
|
155 |
|
static int |
156 |
4600 |
vcl_acl_disjoint(const struct acl_e *ae1, const struct acl_e *ae2) |
157 |
|
{ |
158 |
|
const unsigned char *p1, *p2; |
159 |
|
unsigned m; |
160 |
|
|
161 |
4600 |
CHECK_OBJ_NOTNULL(ae1, VCC_ACL_E_MAGIC); |
162 |
4600 |
CHECK_OBJ_NOTNULL(ae2, VCC_ACL_E_MAGIC); |
163 |
|
|
164 |
4600 |
p1 = ae1->data; |
165 |
4600 |
p2 = ae2->data; |
166 |
4600 |
m = vmin_t(unsigned, ae1->mask, ae2->mask); |
167 |
35280 |
for (; m >= 8; m -= 8) { |
168 |
62480 |
CMP(ACL_GT, *p1, *p2); |
169 |
30680 |
p1++; |
170 |
30680 |
p2++; |
171 |
30680 |
} |
172 |
3480 |
if (m) { |
173 |
3200 |
m = 0xff00 >> m; |
174 |
3200 |
m &= 0xff; |
175 |
6040 |
CMP(ACL_GT, *p1 & m, *p2 & m); |
176 |
2840 |
} |
177 |
3120 |
return (0); |
178 |
4600 |
} |
179 |
|
|
180 |
14040 |
VRBT_GENERATE_INSERT_COLOR(acl_tree, acl_e, branch, static) |
181 |
6760 |
VRBT_GENERATE_INSERT_FINISH(acl_tree, acl_e, branch, static) |
182 |
24480 |
VRBT_GENERATE_INSERT(acl_tree, acl_e, branch, vcl_acl_cmp, static) |
183 |
1160 |
VRBT_GENERATE_REMOVE_COLOR(acl_tree, acl_e, branch, static) |
184 |
1960 |
VRBT_GENERATE_REMOVE(acl_tree, acl_e, branch, static) |
185 |
6640 |
VRBT_GENERATE_MINMAX(acl_tree, acl_e, branch, static) |
186 |
17680 |
VRBT_GENERATE_NEXT(acl_tree, acl_e, branch, static) |
187 |
11000 |
VRBT_GENERATE_PREV(acl_tree, acl_e, branch, static) |
188 |
|
|
189 |
|
static char * |
190 |
6640 |
vcc_acl_chk(struct vcc *tl, const struct acl_e *ae, const int l, |
191 |
|
unsigned char *p, int fam) |
192 |
|
{ |
193 |
|
const unsigned char *u; |
194 |
|
char h[VTCP_ADDRBUFSIZE]; |
195 |
|
char t[VTCP_ADDRBUFSIZE + 10]; |
196 |
6640 |
char s[vsa_suckaddr_len]; |
197 |
6640 |
char *r = NULL; |
198 |
|
const struct suckaddr *sa; |
199 |
|
unsigned m; |
200 |
6640 |
int ll, ret = 0; |
201 |
|
|
202 |
6640 |
u = p; |
203 |
6640 |
ll = l; |
204 |
6640 |
m = ae->mask; |
205 |
|
|
206 |
6640 |
p += m / 8; |
207 |
6640 |
ll -= m / 8; |
208 |
6640 |
assert (ll >= 0); |
209 |
6640 |
m %= 8; |
210 |
|
|
211 |
6640 |
if (m && ((unsigned)*p << m & 0xff) != 0) { |
212 |
160 |
ret = 1; |
213 |
160 |
m = 0xff00 >> m; |
214 |
160 |
*p &= m; |
215 |
160 |
} |
216 |
6640 |
if (m) { |
217 |
3800 |
p++; |
218 |
3800 |
ll--; |
219 |
3800 |
} |
220 |
|
|
221 |
12560 |
for ( ; ll > 0; p++, ll--) { |
222 |
5920 |
if (*p == 0) |
223 |
5720 |
continue; |
224 |
200 |
ret = 1; |
225 |
200 |
*p = 0; |
226 |
200 |
} |
227 |
6640 |
if (ret == 0) |
228 |
6280 |
return (NULL); |
229 |
|
|
230 |
360 |
sa = VSA_BuildFAP(s, fam, u, l, NULL, 0); |
231 |
360 |
AN(sa); |
232 |
360 |
VTCP_name(sa, h, sizeof h, NULL, 0); |
233 |
360 |
bprintf(t, "%s/%d", h, ae->mask); |
234 |
360 |
if (tl->acl->flag_pedantic != 0) { |
235 |
80 |
VSB_cat(tl->sb, "Non-zero bits in masked part, "); |
236 |
80 |
VSB_printf(tl->sb, "(maybe use %s ?)\n", t); |
237 |
80 |
vcc_ErrWhere(tl, ae->t_addr); |
238 |
80 |
} |
239 |
360 |
REPLACE(r, t); |
240 |
360 |
return (r); |
241 |
6640 |
} |
242 |
|
|
243 |
|
static void |
244 |
1760 |
vcl_acl_fold(struct vcc *tl, struct acl_e **l, struct acl_e **r) |
245 |
|
{ |
246 |
|
enum acl_cmp_e cmp; |
247 |
|
|
248 |
1760 |
AN(l); |
249 |
1760 |
AN(r); |
250 |
1760 |
CHECK_OBJ_NOTNULL(*l, VCC_ACL_E_MAGIC); |
251 |
1760 |
CHECK_OBJ_NOTNULL(*r, VCC_ACL_E_MAGIC); |
252 |
|
|
253 |
1760 |
if ((*l)->not || (*r)->not) |
254 |
0 |
return; |
255 |
|
|
256 |
1760 |
cmp = vcl_acl_cmp(*l, *r); |
257 |
|
|
258 |
1760 |
assert(cmp < 0); |
259 |
1760 |
if (cmp == ACL_LT) |
260 |
1160 |
return; |
261 |
|
|
262 |
600 |
do { |
263 |
640 |
switch (cmp) { |
264 |
|
case ACL_CONTAINED: |
265 |
280 |
VSB_cat(tl->sb, "ACL entry:\n"); |
266 |
280 |
vcc_ErrWhere(tl, (*r)->t_addr); |
267 |
280 |
VSB_cat(tl->sb, "supersedes / removes:\n"); |
268 |
280 |
vcc_ErrWhere(tl, (*l)->t_addr); |
269 |
280 |
vcc_Warn(tl); |
270 |
280 |
VRBT_REMOVE(acl_tree, &tl->acl->acl_tree, *l); |
271 |
280 |
FREE_OBJ(*l); |
272 |
280 |
*l = VRBT_PREV(acl_tree, &tl->acl->acl_tree, *r); |
273 |
280 |
break; |
274 |
|
case ACL_LEFT: |
275 |
360 |
(*l)->mask--; |
276 |
360 |
(*l)->fixed = "folded"; |
277 |
360 |
VSB_cat(tl->sb, "ACL entry:\n"); |
278 |
360 |
vcc_ErrWhere(tl, (*l)->t_addr); |
279 |
360 |
VSB_cat(tl->sb, "left of:\n"); |
280 |
360 |
vcc_ErrWhere(tl, (*r)->t_addr); |
281 |
720 |
VSB_printf(tl->sb, "removing the latter and expanding " |
282 |
|
"mask of the former by one to /%u\n", |
283 |
360 |
(*l)->mask - 8); |
284 |
360 |
vcc_Warn(tl); |
285 |
360 |
VRBT_REMOVE(acl_tree, &tl->acl->acl_tree, *r); |
286 |
360 |
FREE_OBJ(*r); |
287 |
360 |
VRBT_REMOVE(acl_tree, &tl->acl->acl_tree, *l); |
288 |
360 |
vcc_acl_insert_entry(tl, l); |
289 |
360 |
return; |
290 |
|
default: |
291 |
0 |
INCOMPL(); |
292 |
0 |
} |
293 |
280 |
if (*l == NULL || *r == NULL) |
294 |
40 |
break; |
295 |
240 |
cmp = vcl_acl_cmp(*l, *r); |
296 |
240 |
} while (cmp != ACL_LT); |
297 |
1760 |
} |
298 |
|
|
299 |
|
static void |
300 |
7000 |
vcc_acl_insert_entry(struct vcc *tl, struct acl_e **aenp) |
301 |
|
{ |
302 |
|
struct acl_e *ae2, *l, *r; |
303 |
|
|
304 |
7000 |
CHECK_OBJ_NOTNULL(*aenp, VCC_ACL_E_MAGIC); |
305 |
7000 |
ae2 = VRBT_INSERT(acl_tree, &tl->acl->acl_tree, *aenp); |
306 |
7000 |
if (ae2 != NULL) { |
307 |
240 |
if (ae2->not != (*aenp)->not) { |
308 |
40 |
VSB_cat(tl->sb, "Conflicting ACL entries:\n"); |
309 |
40 |
vcc_ErrWhere(tl, ae2->t_addr); |
310 |
40 |
VSB_cat(tl->sb, "vs:\n"); |
311 |
40 |
vcc_ErrWhere(tl, (*aenp)->t_addr); |
312 |
40 |
} |
313 |
240 |
return; |
314 |
|
} |
315 |
|
|
316 |
6760 |
r = *aenp; |
317 |
6760 |
*aenp = NULL; |
318 |
|
|
319 |
6760 |
if (tl->acl->flag_fold == 0) |
320 |
5280 |
return; |
321 |
|
|
322 |
1480 |
l = VRBT_PREV(acl_tree, &tl->acl->acl_tree, r); |
323 |
1480 |
if (l != NULL) { |
324 |
1400 |
vcl_acl_fold(tl, &l, &r); |
325 |
1400 |
} |
326 |
1480 |
if (r == NULL) |
327 |
320 |
return; |
328 |
1160 |
l = r; |
329 |
1160 |
r = VRBT_NEXT(acl_tree, &tl->acl->acl_tree, l); |
330 |
1160 |
if (r == NULL) |
331 |
800 |
return; |
332 |
360 |
vcl_acl_fold(tl, &l, &r); |
333 |
7000 |
} |
334 |
|
|
335 |
|
static void |
336 |
6720 |
vcc_acl_add_entry(struct vcc *tl, const struct acl_e *ae, int l, |
337 |
|
unsigned char *u, int fam) |
338 |
|
{ |
339 |
|
struct acl_e *aen; |
340 |
|
|
341 |
6720 |
if (fam == PF_INET && ae->mask > 32) { |
342 |
80 |
VSB_printf(tl->sb, |
343 |
40 |
"Too wide mask (/%u) for IPv4 address\n", ae->mask); |
344 |
40 |
if (ae->t_mask != NULL) |
345 |
40 |
vcc_ErrWhere(tl, ae->t_mask); |
346 |
|
else |
347 |
0 |
vcc_ErrWhere(tl, ae->t_addr); |
348 |
40 |
return; |
349 |
|
} |
350 |
6680 |
if (fam == PF_INET6 && ae->mask > 128) { |
351 |
80 |
VSB_printf(tl->sb, |
352 |
40 |
"Too wide mask (/%u) for IPv6 address\n", ae->mask); |
353 |
40 |
vcc_ErrWhere(tl, ae->t_mask); |
354 |
40 |
return; |
355 |
|
} |
356 |
|
|
357 |
|
/* Make a copy from the template */ |
358 |
6640 |
ALLOC_OBJ(aen, VCC_ACL_E_MAGIC); |
359 |
6640 |
AN(aen); |
360 |
6640 |
*aen = *ae; |
361 |
6640 |
aen->addr = strdup(ae->addr); |
362 |
6640 |
AN(aen->addr); |
363 |
|
|
364 |
6640 |
aen->fixed = vcc_acl_chk(tl, ae, l, u, fam); |
365 |
|
|
366 |
|
/* We treat family as part of address, it saves code */ |
367 |
6640 |
assert(fam <= 0xff); |
368 |
6640 |
aen->data[0] = fam & 0xff; |
369 |
6640 |
aen->mask += 8; |
370 |
|
|
371 |
6640 |
assert(l + 1UL <= sizeof aen->data); |
372 |
6640 |
memcpy(aen->data + 1L, u, l); |
373 |
|
|
374 |
6640 |
vcc_acl_insert_entry(tl, &aen); |
375 |
6640 |
if (aen != NULL) |
376 |
240 |
vcl_acl_free(&aen); |
377 |
6720 |
} |
378 |
|
|
379 |
|
static void |
380 |
2200 |
vcc_acl_try_getaddrinfo(struct vcc *tl, struct acl_e *ae) |
381 |
|
{ |
382 |
|
struct addrinfo *res0, *res, hint; |
383 |
|
struct sockaddr_in *sin4; |
384 |
|
struct sockaddr_in6 *sin6; |
385 |
|
unsigned char *u, i4, i6; |
386 |
|
int error; |
387 |
|
|
388 |
2200 |
CHECK_OBJ_NOTNULL(ae, VCC_ACL_E_MAGIC); |
389 |
2200 |
memset(&hint, 0, sizeof hint); |
390 |
2200 |
hint.ai_family = PF_UNSPEC; |
391 |
2200 |
hint.ai_socktype = SOCK_STREAM; |
392 |
2200 |
error = getaddrinfo(ae->addr, "0", &hint, &res0); |
393 |
2200 |
if (error) { |
394 |
160 |
if (ae->para) { |
395 |
160 |
VSB_printf(tl->sb, |
396 |
|
"Warning: %s ignored\n -- %s\n", |
397 |
80 |
ae->addr, gai_strerror(error)); |
398 |
160 |
Fh(tl, 1, "/* Ignored ACL entry: %s%s", |
399 |
80 |
ae->para ? "\"(\" " : "", ae->not ? "\"!\" " : ""); |
400 |
80 |
EncToken(tl->fh, ae->t_addr); |
401 |
80 |
if (ae->t_mask) |
402 |
40 |
Fh(tl, 0, "/%u", ae->mask); |
403 |
80 |
Fh(tl, 0, "%s\n", ae->para ? " \")\"" : ""); |
404 |
160 |
Fh(tl, 1, " * getaddrinfo: %s */\n", |
405 |
80 |
gai_strerror(error)); |
406 |
80 |
} else { |
407 |
160 |
VSB_printf(tl->sb, |
408 |
|
"DNS lookup(%s): %s\n", |
409 |
80 |
ae->addr, gai_strerror(error)); |
410 |
80 |
vcc_ErrWhere(tl, ae->t_addr); |
411 |
|
} |
412 |
160 |
return; |
413 |
|
} |
414 |
|
|
415 |
2040 |
i4 = i6 = 0; |
416 |
4160 |
for (res = res0; res != NULL; res = res->ai_next) { |
417 |
2120 |
switch (res->ai_family) { |
418 |
|
case PF_INET: |
419 |
80 |
i4++; |
420 |
80 |
break; |
421 |
|
case PF_INET6: |
422 |
2040 |
i6++; |
423 |
2040 |
break; |
424 |
|
default: |
425 |
0 |
VSB_printf(tl->sb, |
426 |
|
"Ignoring unknown protocol family (%d) for %.*s\n", |
427 |
0 |
res->ai_family, PF(ae->t_addr)); |
428 |
0 |
continue; |
429 |
|
} |
430 |
2120 |
} |
431 |
|
|
432 |
2040 |
if (ae->t_mask != NULL && i4 > 0 && i6 > 0) { |
433 |
0 |
VSB_printf(tl->sb, |
434 |
|
"Mask (/%u) specified, but string resolves to" |
435 |
0 |
" both IPv4 and IPv6 addresses.\n", ae->mask); |
436 |
0 |
vcc_ErrWhere(tl, ae->t_mask); |
437 |
0 |
freeaddrinfo(res0); |
438 |
0 |
return; |
439 |
|
} |
440 |
|
|
441 |
4080 |
for (res = res0; res != NULL; res = res->ai_next) { |
442 |
2120 |
switch (res->ai_family) { |
443 |
|
case PF_INET: |
444 |
80 |
assert(PF_INET < 256); |
445 |
80 |
sin4 = (void*)res->ai_addr; |
446 |
80 |
assert(sizeof(sin4->sin_addr) == 4); |
447 |
80 |
u = (void*)&sin4->sin_addr; |
448 |
80 |
if (ae->t_mask == NULL) |
449 |
80 |
ae->mask = 32; |
450 |
80 |
vcc_acl_add_entry(tl, ae, 4, u, res->ai_family); |
451 |
80 |
break; |
452 |
|
case PF_INET6: |
453 |
2040 |
assert(PF_INET6 < 256); |
454 |
2040 |
sin6 = (void*)res->ai_addr; |
455 |
2040 |
assert(sizeof(sin6->sin6_addr) == 16); |
456 |
2040 |
u = (void*)&sin6->sin6_addr; |
457 |
2040 |
if (ae->t_mask == NULL) |
458 |
240 |
ae->mask = 128; |
459 |
2040 |
vcc_acl_add_entry(tl, ae, 16, u, res->ai_family); |
460 |
2040 |
break; |
461 |
|
default: |
462 |
0 |
continue; |
463 |
|
} |
464 |
2120 |
if (tl->err) |
465 |
80 |
freeaddrinfo(res0); |
466 |
2120 |
ERRCHK(tl); |
467 |
2040 |
} |
468 |
1960 |
freeaddrinfo(res0); |
469 |
|
|
470 |
2200 |
} |
471 |
|
|
472 |
|
/*-------------------------------------------------------------------- |
473 |
|
* Ancient stupidity on the part of X/Open and other standards orgs |
474 |
|
* dictate that "192.168" be translated to 192.0.0.168. Ever since |
475 |
|
* CIDR happened, "192.168/16" notation has been used, but apparently |
476 |
|
* no API supports parsing this, so roll our own. |
477 |
|
*/ |
478 |
|
|
479 |
|
static int |
480 |
6800 |
vcc_acl_try_netnotation(struct vcc *tl, struct acl_e *ae) |
481 |
|
{ |
482 |
|
unsigned char b[4]; |
483 |
|
int i, j, k; |
484 |
|
unsigned u; |
485 |
|
const char *p; |
486 |
|
|
487 |
6800 |
CHECK_OBJ_NOTNULL(ae, VCC_ACL_E_MAGIC); |
488 |
6800 |
memset(b, 0, sizeof b); |
489 |
6800 |
p = ae->addr; |
490 |
19840 |
for (i = 0; i < 4; i++) { |
491 |
19840 |
j = sscanf(p, "%u%n", &u, &k); |
492 |
19840 |
if (j != 1) |
493 |
2080 |
return (0); |
494 |
17760 |
if (u & ~0xff) |
495 |
40 |
return (0); |
496 |
17720 |
b[i] = (unsigned char)u; |
497 |
17720 |
if (p[k] == '\0') |
498 |
4600 |
break; |
499 |
13120 |
if (p[k] != '.') |
500 |
80 |
return (0); |
501 |
13040 |
p += k + 1; |
502 |
13040 |
} |
503 |
4600 |
if (ae->t_mask == NULL) |
504 |
1040 |
ae->mask = 8 + 8 * i; |
505 |
4600 |
vcc_acl_add_entry(tl, ae, 4, b, AF_INET); |
506 |
4600 |
return (1); |
507 |
6800 |
} |
508 |
|
|
509 |
|
static void |
510 |
6960 |
vcc_acl_entry(struct vcc *tl) |
511 |
|
{ |
512 |
|
struct acl_e ae[1]; |
513 |
|
char *sl, *e; |
514 |
|
|
515 |
6960 |
INIT_OBJ(ae, VCC_ACL_E_MAGIC); |
516 |
|
|
517 |
6960 |
if (tl->t->tok == '!') { |
518 |
1600 |
ae->not = 1; |
519 |
1600 |
vcc_NextToken(tl); |
520 |
1600 |
} |
521 |
|
|
522 |
6960 |
if (tl->t->tok == '(') { |
523 |
120 |
ae->para = 1; |
524 |
120 |
vcc_NextToken(tl); |
525 |
120 |
} |
526 |
|
|
527 |
6960 |
if (!ae->not && tl->t->tok == '!') { |
528 |
40 |
ae->not = 1; |
529 |
40 |
vcc_NextToken(tl); |
530 |
40 |
} |
531 |
|
|
532 |
6960 |
ExpectErr(tl, CSTR); |
533 |
6960 |
ae->t_addr = tl->t; |
534 |
6960 |
ae->addr = ae->t_addr->dec; |
535 |
6960 |
vcc_NextToken(tl); |
536 |
|
|
537 |
6960 |
if (strchr(ae->t_addr->dec, '/') != NULL) { |
538 |
120 |
sl = strchr(ae->addr, '/'); |
539 |
120 |
AN(sl); |
540 |
120 |
*sl++ = '\0'; |
541 |
120 |
e = NULL; |
542 |
120 |
ae->mask = strtoul(sl, &e, 10); |
543 |
120 |
if (*e != '\0') { |
544 |
80 |
VSB_cat(tl->sb, ".../mask is not numeric.\n"); |
545 |
80 |
vcc_ErrWhere(tl, ae->t_addr); |
546 |
80 |
return; |
547 |
|
} |
548 |
40 |
ae->t_mask = ae->t_addr; |
549 |
40 |
if (tl->t->tok == '/') { |
550 |
40 |
VSB_cat(tl->sb, "/mask only allowed once.\n"); |
551 |
40 |
vcc_ErrWhere(tl, tl->t); |
552 |
40 |
return; |
553 |
|
} |
554 |
6840 |
} else if (tl->t->tok == '/') { |
555 |
5400 |
vcc_NextToken(tl); |
556 |
5400 |
ae->t_mask = tl->t; |
557 |
5400 |
ExpectErr(tl, CNUM); |
558 |
5400 |
ae->mask = vcc_UintVal(tl); |
559 |
5400 |
} |
560 |
|
|
561 |
6840 |
if (ae->para) |
562 |
120 |
SkipToken(tl, ')'); |
563 |
|
|
564 |
6800 |
if (!vcc_acl_try_netnotation(tl, ae)) { |
565 |
2200 |
ERRCHK(tl); |
566 |
2200 |
vcc_acl_try_getaddrinfo(tl, ae); |
567 |
2200 |
} |
568 |
6800 |
ERRCHK(tl); |
569 |
6960 |
} |
570 |
|
|
571 |
|
/********************************************************************* |
572 |
|
* Emit the tokens making up an entry as C-strings |
573 |
|
*/ |
574 |
|
|
575 |
|
static void |
576 |
2040 |
vcc_acl_emit_tokens(const struct vcc *tl, const struct acl_e *ae) |
577 |
|
{ |
578 |
|
struct token *t; |
579 |
2040 |
const char *sep = ""; |
580 |
|
|
581 |
2040 |
CHECK_OBJ_NOTNULL(ae, VCC_ACL_E_MAGIC); |
582 |
2040 |
t = ae->t_addr; |
583 |
2040 |
do { |
584 |
5960 |
if (t->tok == CSTR) { |
585 |
2040 |
Fh(tl, 0, "%s\"\\\"\" ", sep); |
586 |
2040 |
EncToken(tl->fh, t); |
587 |
2040 |
Fh(tl, 0, " \"\\\"\""); |
588 |
5960 |
} else if (t == ae->t_mask) { |
589 |
1960 |
Fh(tl, 0, " \"%u\"", ae->mask - 8); |
590 |
1960 |
} else { |
591 |
1960 |
Fh(tl, 0, "%s\"%.*s\"", sep, PF(t)); |
592 |
|
} |
593 |
5960 |
if (t == ae->t_mask) |
594 |
1960 |
break; |
595 |
4000 |
t = vcc_PeekTokenFrom(tl, t); |
596 |
4000 |
AN(t); |
597 |
4000 |
sep = " "; |
598 |
4000 |
} while (ae->t_mask != NULL); |
599 |
2040 |
if (ae->fixed) |
600 |
400 |
Fh(tl, 0, "\" fixed: %s\"", ae->fixed); |
601 |
2040 |
} |
602 |
|
|
603 |
|
/********************************************************************* |
604 |
|
* Emit ACL on table format |
605 |
|
*/ |
606 |
|
|
607 |
|
static unsigned |
608 |
80 |
vcc_acl_emit_tables(const struct vcc *tl, unsigned n, const char *name) |
609 |
|
{ |
610 |
|
struct acl_e *ae; |
611 |
80 |
unsigned rv = sizeof(ae->data) + 3; |
612 |
80 |
unsigned nn = 0; |
613 |
|
size_t sz; |
614 |
|
|
615 |
160 |
Fh(tl, 0, "\nstatic unsigned char acl_tbl_%s[%u*%u] = {\n", |
616 |
80 |
name, n, rv); |
617 |
2000 |
VRBT_FOREACH(ae, acl_tree, &tl->acl->acl_tree) { |
618 |
1920 |
if (ae->overlapped) |
619 |
1600 |
continue; |
620 |
320 |
Fh(tl, 0, "\t0x%02x,", ae->not ? 0 : 1); |
621 |
320 |
Fh(tl, 0, "0x%02x,", (ae->mask >> 3) - 1); |
622 |
320 |
Fh(tl, 0, "0x%02x,", (0xff00 >> (ae->mask & 7)) & 0xff); |
623 |
5760 |
for (sz = 0; sz < sizeof(ae->data); sz++) |
624 |
5440 |
Fh(tl, 0, "0x%02x,", ae->data[sz]); |
625 |
320 |
for (; sz < rv - 3; sz++) |
626 |
0 |
Fh(tl, 0, "0,"); |
627 |
320 |
Fh(tl, 0, "\n"); |
628 |
320 |
nn++; |
629 |
320 |
} |
630 |
80 |
assert(n == nn); |
631 |
80 |
Fh(tl, 0, "};\n"); |
632 |
80 |
if (tl->acl->flag_log) { |
633 |
80 |
Fh(tl, 0, "\nstatic const char *acl_str_%s[%d] = {\n", |
634 |
40 |
name, n); |
635 |
1000 |
VRBT_FOREACH(ae, acl_tree, &tl->acl->acl_tree) { |
636 |
960 |
if (ae->overlapped) |
637 |
800 |
continue; |
638 |
160 |
Fh(tl, 0, "\t"); |
639 |
320 |
Fh(tl, 0, "\"%sMATCH %s \" ", |
640 |
160 |
ae->not ? "NEG_" : "", name); |
641 |
160 |
vcc_acl_emit_tokens(tl, ae); |
642 |
160 |
Fh(tl, 0, ",\n"); |
643 |
160 |
} |
644 |
40 |
Fh(tl, 0, "};\n"); |
645 |
40 |
} |
646 |
80 |
return (rv); |
647 |
|
} |
648 |
|
|
649 |
|
/********************************************************************* |
650 |
|
* Emit a function to match the ACL we have collected |
651 |
|
*/ |
652 |
|
|
653 |
|
static void |
654 |
1040 |
vcc_acl_emit(struct vcc *tl, const struct symbol *sym) |
655 |
|
{ |
656 |
|
struct acl_e *ae, *ae2; |
657 |
|
int depth, l, m, i; |
658 |
|
unsigned at[ACL_MAXADDR]; |
659 |
1040 |
struct inifin *ifp = NULL; |
660 |
|
struct vsb *func; |
661 |
1040 |
unsigned n, no, nw = 0; |
662 |
|
|
663 |
1040 |
func = VSB_new_auto(); |
664 |
1040 |
AN(func); |
665 |
1040 |
VSB_cat(func, "match_acl_"); |
666 |
1040 |
VCC_PrintCName(func, sym->name, NULL); |
667 |
1040 |
AZ(VSB_finish(func)); |
668 |
|
|
669 |
1040 |
depth = -1; |
670 |
1040 |
at[0] = 256; |
671 |
1040 |
ae2 = NULL; |
672 |
1040 |
n = no = 0; |
673 |
6640 |
VRBT_FOREACH_REVERSE(ae, acl_tree, &tl->acl->acl_tree) { |
674 |
5600 |
n++; |
675 |
5600 |
if (ae2 == NULL) { |
676 |
1000 |
ae2 = ae; |
677 |
5600 |
} else if (vcl_acl_disjoint(ae, ae2)) { |
678 |
1480 |
ae2 = ae; |
679 |
1480 |
} else { |
680 |
3120 |
no++; |
681 |
3120 |
ae->overlapped = 1; |
682 |
|
} |
683 |
5600 |
} |
684 |
|
|
685 |
1040 |
Fh(tl, 0, "/* acl_n_%s n %u no %u */\n", sym->name, n, no); |
686 |
1040 |
if (n - no < (1<<1)) |
687 |
600 |
no = n; |
688 |
440 |
else if (!tl->acl->flag_table) |
689 |
360 |
no = n; |
690 |
|
|
691 |
1040 |
if (no < n) |
692 |
80 |
nw = vcc_acl_emit_tables(tl, n - no, sym->name); |
693 |
|
|
694 |
|
|
695 |
1040 |
Fh(tl, 0, "\nstatic int v_matchproto_(acl_match_f)\n"); |
696 |
1040 |
Fh(tl, 0, "%s(VRT_CTX, const VCL_IP p)\n", VSB_data(func)); |
697 |
1040 |
Fh(tl, 0, "{\n"); |
698 |
1040 |
Fh(tl, 0, "\tconst unsigned char *a;\n"); |
699 |
1040 |
Fh(tl, 0, "\tint fam;\n"); |
700 |
1040 |
Fh(tl, 0, "\n"); |
701 |
1040 |
Fh(tl, 0, "\tfam = VRT_VSA_GetPtr(ctx, p, &a);\n"); |
702 |
1040 |
Fh(tl, 0, "\tif (fam < 0) {\n"); |
703 |
1040 |
Fh(tl, 0, "\t\tVRT_fail(ctx,"); |
704 |
1040 |
Fh(tl, 0, " \"ACL %s: no protocol family\");\n", sym->name); |
705 |
1040 |
Fh(tl, 0, "\t\treturn(0);\n"); |
706 |
1040 |
Fh(tl, 0, "\t}\n\n"); |
707 |
1040 |
if (!tl->err_unref) { |
708 |
80 |
ifp = New_IniFin(tl); |
709 |
160 |
VSB_printf(ifp->ini, |
710 |
80 |
"\t(void)%s;\n", VSB_data(func)); |
711 |
80 |
} |
712 |
|
|
713 |
6640 |
VRBT_FOREACH(ae, acl_tree, &tl->acl->acl_tree) { |
714 |
|
|
715 |
5600 |
if (no < n && !ae->overlapped) |
716 |
320 |
continue; |
717 |
|
|
718 |
|
/* Find how much common prefix we have */ |
719 |
32960 |
for (l = 0; l <= depth && l * 8 < (int)ae->mask - 7; l++) { |
720 |
29520 |
assert(l >= 0); |
721 |
29520 |
if (ae->data[l] != at[l]) |
722 |
1840 |
break; |
723 |
27680 |
} |
724 |
|
|
725 |
|
/* Back down, if necessary */ |
726 |
12000 |
while (l <= depth) { |
727 |
6720 |
Fh(tl, 0, "\t%*s}\n", -depth, ""); |
728 |
6720 |
depth--; |
729 |
|
} |
730 |
|
|
731 |
5280 |
m = (int)ae->mask; |
732 |
5280 |
assert(m >= l*8); |
733 |
5280 |
m -= l * 8; |
734 |
|
|
735 |
|
/* Do whole byte compares */ |
736 |
17360 |
for (i = l; m >= 8; m -= 8, i++) { |
737 |
12080 |
if (i == 0) |
738 |
2800 |
Fh(tl, 0, "\t%*s%sif (fam == %d) {\n", |
739 |
1400 |
-i, "", "", ae->data[i]); |
740 |
|
else |
741 |
21360 |
Fh(tl, 0, "\t%*s%sif (a[%d] == %d) {\n", |
742 |
10680 |
-i, "", "", i - 1, ae->data[i]); |
743 |
12080 |
at[i] = ae->data[i]; |
744 |
12080 |
depth = i; |
745 |
12080 |
} |
746 |
|
|
747 |
5280 |
if (m > 0) { |
748 |
|
// XXX can remove masking due to fixup |
749 |
|
/* Do fractional byte compares */ |
750 |
6080 |
Fh(tl, 0, "\t%*s%sif ((a[%d] & 0x%x) == %d) {\n", |
751 |
3040 |
-i, "", "", i - 1, (0xff00 >> m) & 0xff, |
752 |
3040 |
ae->data[i] & ((0xff00 >> m) & 0xff)); |
753 |
3040 |
at[i] = 256; |
754 |
3040 |
depth = i; |
755 |
3040 |
} |
756 |
|
|
757 |
5280 |
i = ((int)ae->mask + 7) / 8; |
758 |
|
|
759 |
5280 |
if (tl->acl->flag_log) { |
760 |
3760 |
Fh(tl, 0, "\t%*sVPI_acl_log(ctx, \"%sMATCH %s \" ", |
761 |
1880 |
-i, "", ae->not ? "NEG_" : "", sym->name); |
762 |
1880 |
vcc_acl_emit_tokens(tl, ae); |
763 |
1880 |
Fh(tl, 0, ");\n"); |
764 |
1880 |
} |
765 |
|
|
766 |
5280 |
Fh(tl, 0, "\t%*sreturn (%d);\n", -i, "", ae->not ? 0 : 1); |
767 |
5280 |
} |
768 |
|
|
769 |
|
/* Unwind */ |
770 |
9440 |
for (; 0 <= depth; depth--) |
771 |
8400 |
Fh(tl, 0, "\t%*.*s}\n", depth, depth, ""); |
772 |
|
|
773 |
1040 |
if (no < n) { |
774 |
80 |
Fh(tl, 0, "\treturn(\n\t VPI_acl_table(ctx,\n"); |
775 |
80 |
Fh(tl, 0, "\t\tp,\n"); |
776 |
80 |
Fh(tl, 0, "\t\t%u, %u,\n", n - no, nw); |
777 |
80 |
Fh(tl, 0, "\t\tacl_tbl_%s,\n", sym->name); |
778 |
80 |
if (tl->acl->flag_log) |
779 |
40 |
Fh(tl, 0, "\t\tacl_str_%s,\n", sym->name); |
780 |
|
else |
781 |
40 |
Fh(tl, 0, "\t\tNULL,\n"); |
782 |
80 |
Fh(tl, 0, "\t\t\"NO MATCH %s\"\n\t )\n\t);\n", sym->name); |
783 |
80 |
} else { |
784 |
|
/* Deny by default */ |
785 |
960 |
if (tl->acl->flag_log) |
786 |
240 |
Fh(tl, 0, "\tVPI_acl_log(ctx, \"NO_MATCH %s\");\n", |
787 |
120 |
sym->name); |
788 |
960 |
Fh(tl, 0, "\treturn(0);\n"); |
789 |
|
} |
790 |
1040 |
Fh(tl, 0, "}\n"); |
791 |
|
|
792 |
|
/* Emit the struct that will be referenced */ |
793 |
1040 |
Fh(tl, 0, "\nstatic const struct vrt_acl %s[] = {{\n", sym->rname); |
794 |
1040 |
Fh(tl, 0, "\t.magic = VRT_ACL_MAGIC,\n"); |
795 |
1040 |
Fh(tl, 0, "\t.match = &%s,\n", VSB_data(func)); |
796 |
1040 |
Fh(tl, 0, "\t.name = \"%s\",\n", sym->name); |
797 |
1040 |
Fh(tl, 0, "}};\n\n"); |
798 |
1040 |
if (!tl->err_unref) { |
799 |
80 |
AN(ifp); |
800 |
80 |
VSB_printf(ifp->ini, "\t(void)%s;", sym->rname); |
801 |
80 |
} |
802 |
1040 |
VSB_destroy(&func); |
803 |
1040 |
} |
804 |
|
|
805 |
|
void |
806 |
1640 |
vcc_ParseAcl(struct vcc *tl) |
807 |
|
{ |
808 |
|
struct symbol *sym; |
809 |
|
int sign; |
810 |
|
struct acl acl[1]; |
811 |
|
|
812 |
1640 |
INIT_OBJ(acl, VCC_ACL_MAGIC); |
813 |
1640 |
tl->acl = acl; |
814 |
1640 |
acl->flag_pedantic = 1; |
815 |
1640 |
vcc_NextToken(tl); |
816 |
1640 |
VRBT_INIT(&acl->acl_tree); |
817 |
|
|
818 |
1640 |
vcc_ExpectVid(tl, "ACL"); |
819 |
1640 |
ERRCHK(tl); |
820 |
1600 |
sym = VCC_HandleSymbol(tl, ACL); |
821 |
1600 |
ERRCHK(tl); |
822 |
1600 |
AN(sym); |
823 |
|
|
824 |
2200 |
while (1) { |
825 |
2200 |
sign = vcc_IsFlag(tl); |
826 |
2200 |
if (tl->err) { |
827 |
40 |
VSB_cat(tl->sb, |
828 |
|
"Valid ACL flags are `log` and `table`:\n"); |
829 |
40 |
return; |
830 |
|
} |
831 |
2160 |
if (sign < 0) |
832 |
1520 |
break; |
833 |
640 |
if (vcc_IdIs(tl->t, "log")) { |
834 |
160 |
acl->flag_log = sign; |
835 |
160 |
vcc_NextToken(tl); |
836 |
640 |
} else if (vcc_IdIs(tl->t, "fold")) { |
837 |
40 |
acl->flag_fold = sign; |
838 |
40 |
vcc_NextToken(tl); |
839 |
480 |
} else if (vcc_IdIs(tl->t, "pedantic")) { |
840 |
320 |
acl->flag_pedantic = sign; |
841 |
320 |
vcc_NextToken(tl); |
842 |
440 |
} else if (vcc_IdIs(tl->t, "table")) { |
843 |
80 |
acl->flag_table = sign; |
844 |
80 |
vcc_NextToken(tl); |
845 |
80 |
} else { |
846 |
40 |
VSB_cat(tl->sb, "Unknown ACL flag:\n"); |
847 |
40 |
vcc_ErrWhere(tl, tl->t); |
848 |
40 |
return; |
849 |
|
} |
850 |
|
} |
851 |
|
|
852 |
1520 |
SkipToken(tl, '{'); |
853 |
|
|
854 |
8000 |
while (tl->t->tok != '}') { |
855 |
6960 |
vcc_acl_entry(tl); |
856 |
6960 |
ERRCHK(tl); |
857 |
6520 |
SkipToken(tl, ';'); |
858 |
|
} |
859 |
1040 |
SkipToken(tl, '}'); |
860 |
|
|
861 |
1040 |
vcc_acl_emit(tl, sym); |
862 |
1640 |
} |