| | 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 |
18 |
vcl_acl_free(struct acl_e **aep) |
111 |
|
{ |
112 |
|
struct acl_e *a; |
113 |
|
|
114 |
18 |
TAKE_OBJ_NOTNULL(a, aep, VCC_ACL_E_MAGIC); |
115 |
18 |
free(a->addr); |
116 |
18 |
FREE_OBJ(a); |
117 |
18 |
} |
118 |
|
|
119 |
|
static enum acl_cmp_e |
120 |
1479 |
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 |
1479 |
CHECK_OBJ_NOTNULL(ae1, VCC_ACL_E_MAGIC); |
127 |
1479 |
CHECK_OBJ_NOTNULL(ae2, VCC_ACL_E_MAGIC); |
128 |
|
|
129 |
1479 |
p1 = ae1->data; |
130 |
1479 |
p2 = ae2->data; |
131 |
1479 |
m = vmin_t(unsigned, ae1->mask, ae2->mask); |
132 |
7713 |
for (; m >= 8; m -= 8) { |
133 |
7047 |
if (m == 8 && ae1->mask == ae2->mask) |
134 |
117 |
CMPA(*p1, *p2); |
135 |
13254 |
CMP(ACL_GT, *p1, *p2); |
136 |
6234 |
p1++; |
137 |
6234 |
p2++; |
138 |
6234 |
} |
139 |
666 |
if (m) { |
140 |
636 |
assert (m < 8); |
141 |
636 |
a1 = *p1 >> (8 - m); |
142 |
636 |
a2 = *p2 >> (8 - m); |
143 |
636 |
if (ae1->mask == ae2->mask) |
144 |
204 |
CMPA(a1, a2); |
145 |
852 |
CMP(ACL_GT, a1, a2); |
146 |
294 |
} else if (ae1->mask == ae2->mask) { |
147 |
30 |
CMPA(*p1, *p2); |
148 |
15 |
} |
149 |
|
/* Long mask is less than short mask */ |
150 |
312 |
CMP(ACL_CONTAINS, ae2->mask, ae1->mask); |
151 |
|
|
152 |
18 |
return (ACL_EQ); |
153 |
1479 |
} |
154 |
|
|
155 |
|
static int |
156 |
345 |
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 |
345 |
CHECK_OBJ_NOTNULL(ae1, VCC_ACL_E_MAGIC); |
162 |
345 |
CHECK_OBJ_NOTNULL(ae2, VCC_ACL_E_MAGIC); |
163 |
|
|
164 |
345 |
p1 = ae1->data; |
165 |
345 |
p2 = ae2->data; |
166 |
345 |
m = vmin_t(unsigned, ae1->mask, ae2->mask); |
167 |
2646 |
for (; m >= 8; m -= 8) { |
168 |
4686 |
CMP(ACL_GT, *p1, *p2); |
169 |
2301 |
p1++; |
170 |
2301 |
p2++; |
171 |
2301 |
} |
172 |
261 |
if (m) { |
173 |
240 |
m = 0xff00 >> m; |
174 |
240 |
m &= 0xff; |
175 |
453 |
CMP(ACL_GT, *p1 & m, *p2 & m); |
176 |
213 |
} |
177 |
234 |
return (0); |
178 |
345 |
} |
179 |
|
|
180 |
1053 |
VRBT_GENERATE_INSERT_COLOR(acl_tree, acl_e, branch, static) |
181 |
507 |
VRBT_GENERATE_INSERT_FINISH(acl_tree, acl_e, branch, static) |
182 |
1836 |
VRBT_GENERATE_INSERT(acl_tree, acl_e, branch, vcl_acl_cmp, static) |
183 |
87 |
VRBT_GENERATE_REMOVE_COLOR(acl_tree, acl_e, branch, static) |
184 |
147 |
VRBT_GENERATE_REMOVE(acl_tree, acl_e, branch, static) |
185 |
498 |
VRBT_GENERATE_MINMAX(acl_tree, acl_e, branch, static) |
186 |
1326 |
VRBT_GENERATE_NEXT(acl_tree, acl_e, branch, static) |
187 |
825 |
VRBT_GENERATE_PREV(acl_tree, acl_e, branch, static) |
188 |
|
|
189 |
|
static char * |
190 |
498 |
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 |
498 |
char s[vsa_suckaddr_len]; |
197 |
498 |
char *r = NULL; |
198 |
|
const struct suckaddr *sa; |
199 |
|
unsigned m; |
200 |
498 |
int ll, ret = 0; |
201 |
|
|
202 |
498 |
u = p; |
203 |
498 |
ll = l; |
204 |
498 |
m = ae->mask; |
205 |
|
|
206 |
498 |
p += m / 8; |
207 |
498 |
ll -= m / 8; |
208 |
498 |
assert (ll >= 0); |
209 |
498 |
m %= 8; |
210 |
|
|
211 |
498 |
if (m && ((unsigned)*p << m & 0xff) != 0) { |
212 |
12 |
ret = 1; |
213 |
12 |
m = 0xff00 >> m; |
214 |
12 |
*p &= m; |
215 |
12 |
} |
216 |
498 |
if (m) { |
217 |
285 |
p++; |
218 |
285 |
ll--; |
219 |
285 |
} |
220 |
|
|
221 |
942 |
for ( ; ll > 0; p++, ll--) { |
222 |
444 |
if (*p == 0) |
223 |
429 |
continue; |
224 |
15 |
ret = 1; |
225 |
15 |
*p = 0; |
226 |
15 |
} |
227 |
498 |
if (ret == 0) |
228 |
471 |
return (NULL); |
229 |
|
|
230 |
27 |
sa = VSA_BuildFAP(s, fam, u, l, NULL, 0); |
231 |
27 |
AN(sa); |
232 |
27 |
VTCP_name(sa, h, sizeof h, NULL, 0); |
233 |
27 |
bprintf(t, "%s/%d", h, ae->mask); |
234 |
27 |
if (tl->acl->flag_pedantic != 0) { |
235 |
6 |
VSB_cat(tl->sb, "Non-zero bits in masked part, "); |
236 |
6 |
VSB_printf(tl->sb, "(maybe use %s ?)\n", t); |
237 |
6 |
vcc_ErrWhere(tl, ae->t_addr); |
238 |
6 |
} |
239 |
27 |
REPLACE(r, t); |
240 |
27 |
return (r); |
241 |
498 |
} |
242 |
|
|
243 |
|
static void |
244 |
132 |
vcl_acl_fold(struct vcc *tl, struct acl_e **l, struct acl_e **r) |
245 |
|
{ |
246 |
|
enum acl_cmp_e cmp; |
247 |
|
|
248 |
132 |
AN(l); |
249 |
132 |
AN(r); |
250 |
132 |
CHECK_OBJ_NOTNULL(*l, VCC_ACL_E_MAGIC); |
251 |
132 |
CHECK_OBJ_NOTNULL(*r, VCC_ACL_E_MAGIC); |
252 |
|
|
253 |
132 |
if ((*l)->not || (*r)->not) |
254 |
0 |
return; |
255 |
|
|
256 |
132 |
cmp = vcl_acl_cmp(*l, *r); |
257 |
|
|
258 |
132 |
assert(cmp < 0); |
259 |
132 |
if (cmp == ACL_LT) |
260 |
87 |
return; |
261 |
|
|
262 |
45 |
do { |
263 |
48 |
switch (cmp) { |
264 |
|
case ACL_CONTAINED: |
265 |
21 |
VSB_cat(tl->sb, "ACL entry:\n"); |
266 |
21 |
vcc_ErrWhere(tl, (*r)->t_addr); |
267 |
21 |
VSB_cat(tl->sb, "supersedes / removes:\n"); |
268 |
21 |
vcc_ErrWhere(tl, (*l)->t_addr); |
269 |
21 |
vcc_Warn(tl); |
270 |
21 |
VRBT_REMOVE(acl_tree, &tl->acl->acl_tree, *l); |
271 |
21 |
FREE_OBJ(*l); |
272 |
21 |
*l = VRBT_PREV(acl_tree, &tl->acl->acl_tree, *r); |
273 |
21 |
break; |
274 |
|
case ACL_LEFT: |
275 |
27 |
(*l)->mask--; |
276 |
27 |
(*l)->fixed = "folded"; |
277 |
27 |
VSB_cat(tl->sb, "ACL entry:\n"); |
278 |
27 |
vcc_ErrWhere(tl, (*l)->t_addr); |
279 |
27 |
VSB_cat(tl->sb, "left of:\n"); |
280 |
27 |
vcc_ErrWhere(tl, (*r)->t_addr); |
281 |
54 |
VSB_printf(tl->sb, "removing the latter and expanding " |
282 |
|
"mask of the former by one to /%u\n", |
283 |
27 |
(*l)->mask - 8); |
284 |
27 |
vcc_Warn(tl); |
285 |
27 |
VRBT_REMOVE(acl_tree, &tl->acl->acl_tree, *r); |
286 |
27 |
FREE_OBJ(*r); |
287 |
27 |
VRBT_REMOVE(acl_tree, &tl->acl->acl_tree, *l); |
288 |
27 |
vcc_acl_insert_entry(tl, l); |
289 |
27 |
return; |
290 |
|
default: |
291 |
0 |
INCOMPL(); |
292 |
0 |
} |
293 |
21 |
if (*l == NULL || *r == NULL) |
294 |
3 |
break; |
295 |
18 |
cmp = vcl_acl_cmp(*l, *r); |
296 |
18 |
} while (cmp != ACL_LT); |
297 |
132 |
} |
298 |
|
|
299 |
|
static void |
300 |
525 |
vcc_acl_insert_entry(struct vcc *tl, struct acl_e **aenp) |
301 |
|
{ |
302 |
|
struct acl_e *ae2, *l, *r; |
303 |
|
|
304 |
525 |
CHECK_OBJ_NOTNULL(*aenp, VCC_ACL_E_MAGIC); |
305 |
525 |
ae2 = VRBT_INSERT(acl_tree, &tl->acl->acl_tree, *aenp); |
306 |
525 |
if (ae2 != NULL) { |
307 |
18 |
if (ae2->not != (*aenp)->not) { |
308 |
3 |
VSB_cat(tl->sb, "Conflicting ACL entries:\n"); |
309 |
3 |
vcc_ErrWhere(tl, ae2->t_addr); |
310 |
3 |
VSB_cat(tl->sb, "vs:\n"); |
311 |
3 |
vcc_ErrWhere(tl, (*aenp)->t_addr); |
312 |
3 |
} |
313 |
18 |
return; |
314 |
|
} |
315 |
|
|
316 |
507 |
r = *aenp; |
317 |
507 |
*aenp = NULL; |
318 |
|
|
319 |
507 |
if (tl->acl->flag_fold == 0) |
320 |
396 |
return; |
321 |
|
|
322 |
111 |
l = VRBT_PREV(acl_tree, &tl->acl->acl_tree, r); |
323 |
111 |
if (l != NULL) { |
324 |
105 |
vcl_acl_fold(tl, &l, &r); |
325 |
105 |
} |
326 |
111 |
if (r == NULL) |
327 |
24 |
return; |
328 |
87 |
l = r; |
329 |
87 |
r = VRBT_NEXT(acl_tree, &tl->acl->acl_tree, l); |
330 |
87 |
if (r == NULL) |
331 |
60 |
return; |
332 |
27 |
vcl_acl_fold(tl, &l, &r); |
333 |
525 |
} |
334 |
|
|
335 |
|
static void |
336 |
504 |
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 |
504 |
if (fam == PF_INET && ae->mask > 32) { |
342 |
6 |
VSB_printf(tl->sb, |
343 |
3 |
"Too wide mask (/%u) for IPv4 address\n", ae->mask); |
344 |
3 |
if (ae->t_mask != NULL) |
345 |
3 |
vcc_ErrWhere(tl, ae->t_mask); |
346 |
|
else |
347 |
0 |
vcc_ErrWhere(tl, ae->t_addr); |
348 |
3 |
return; |
349 |
|
} |
350 |
501 |
if (fam == PF_INET6 && ae->mask > 128) { |
351 |
6 |
VSB_printf(tl->sb, |
352 |
3 |
"Too wide mask (/%u) for IPv6 address\n", ae->mask); |
353 |
3 |
vcc_ErrWhere(tl, ae->t_mask); |
354 |
3 |
return; |
355 |
|
} |
356 |
|
|
357 |
|
/* Make a copy from the template */ |
358 |
498 |
ALLOC_OBJ(aen, VCC_ACL_E_MAGIC); |
359 |
498 |
AN(aen); |
360 |
498 |
*aen = *ae; |
361 |
498 |
aen->addr = strdup(ae->addr); |
362 |
498 |
AN(aen->addr); |
363 |
|
|
364 |
498 |
aen->fixed = vcc_acl_chk(tl, ae, l, u, fam); |
365 |
|
|
366 |
|
/* We treat family as part of address, it saves code */ |
367 |
498 |
assert(fam <= 0xff); |
368 |
498 |
aen->data[0] = fam & 0xff; |
369 |
498 |
aen->mask += 8; |
370 |
|
|
371 |
498 |
assert(l + 1UL <= sizeof aen->data); |
372 |
498 |
memcpy(aen->data + 1L, u, l); |
373 |
|
|
374 |
498 |
vcc_acl_insert_entry(tl, &aen); |
375 |
498 |
if (aen != NULL) |
376 |
18 |
vcl_acl_free(&aen); |
377 |
504 |
} |
378 |
|
|
379 |
|
static void |
380 |
165 |
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 |
165 |
CHECK_OBJ_NOTNULL(ae, VCC_ACL_E_MAGIC); |
389 |
165 |
memset(&hint, 0, sizeof hint); |
390 |
165 |
hint.ai_family = PF_UNSPEC; |
391 |
165 |
hint.ai_socktype = SOCK_STREAM; |
392 |
165 |
error = getaddrinfo(ae->addr, "0", &hint, &res0); |
393 |
165 |
if (error) { |
394 |
12 |
if (ae->para) { |
395 |
12 |
VSB_printf(tl->sb, |
396 |
|
"Warning: %s ignored\n -- %s\n", |
397 |
6 |
ae->addr, gai_strerror(error)); |
398 |
12 |
Fh(tl, 1, "/* Ignored ACL entry: %s%s", |
399 |
6 |
ae->para ? "\"(\" " : "", ae->not ? "\"!\" " : ""); |
400 |
6 |
EncToken(tl->fh, ae->t_addr); |
401 |
6 |
if (ae->t_mask) |
402 |
3 |
Fh(tl, 0, "/%u", ae->mask); |
403 |
6 |
Fh(tl, 0, "%s\n", ae->para ? " \")\"" : ""); |
404 |
12 |
Fh(tl, 1, " * getaddrinfo: %s */\n", |
405 |
6 |
gai_strerror(error)); |
406 |
6 |
} else { |
407 |
12 |
VSB_printf(tl->sb, |
408 |
|
"DNS lookup(%s): %s\n", |
409 |
6 |
ae->addr, gai_strerror(error)); |
410 |
6 |
vcc_ErrWhere(tl, ae->t_addr); |
411 |
|
} |
412 |
12 |
return; |
413 |
|
} |
414 |
|
|
415 |
153 |
i4 = i6 = 0; |
416 |
312 |
for (res = res0; res != NULL; res = res->ai_next) { |
417 |
159 |
switch (res->ai_family) { |
418 |
|
case PF_INET: |
419 |
6 |
i4++; |
420 |
6 |
break; |
421 |
|
case PF_INET6: |
422 |
153 |
i6++; |
423 |
153 |
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 |
159 |
} |
431 |
|
|
432 |
153 |
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 |
306 |
for (res = res0; res != NULL; res = res->ai_next) { |
442 |
159 |
switch (res->ai_family) { |
443 |
|
case PF_INET: |
444 |
6 |
assert(PF_INET < 256); |
445 |
6 |
sin4 = (void*)res->ai_addr; |
446 |
6 |
assert(sizeof(sin4->sin_addr) == 4); |
447 |
6 |
u = (void*)&sin4->sin_addr; |
448 |
6 |
if (ae->t_mask == NULL) |
449 |
6 |
ae->mask = 32; |
450 |
6 |
vcc_acl_add_entry(tl, ae, 4, u, res->ai_family); |
451 |
6 |
break; |
452 |
|
case PF_INET6: |
453 |
153 |
assert(PF_INET6 < 256); |
454 |
153 |
sin6 = (void*)res->ai_addr; |
455 |
153 |
assert(sizeof(sin6->sin6_addr) == 16); |
456 |
153 |
u = (void*)&sin6->sin6_addr; |
457 |
153 |
if (ae->t_mask == NULL) |
458 |
18 |
ae->mask = 128; |
459 |
153 |
vcc_acl_add_entry(tl, ae, 16, u, res->ai_family); |
460 |
153 |
break; |
461 |
|
default: |
462 |
0 |
continue; |
463 |
|
} |
464 |
159 |
if (tl->err) |
465 |
6 |
freeaddrinfo(res0); |
466 |
159 |
ERRCHK(tl); |
467 |
153 |
} |
468 |
147 |
freeaddrinfo(res0); |
469 |
|
|
470 |
165 |
} |
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 |
510 |
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 |
510 |
CHECK_OBJ_NOTNULL(ae, VCC_ACL_E_MAGIC); |
488 |
510 |
memset(b, 0, sizeof b); |
489 |
510 |
p = ae->addr; |
490 |
1488 |
for (i = 0; i < 4; i++) { |
491 |
1488 |
j = sscanf(p, "%u%n", &u, &k); |
492 |
1488 |
if (j != 1) |
493 |
156 |
return (0); |
494 |
1332 |
if (u & ~0xff) |
495 |
3 |
return (0); |
496 |
1329 |
b[i] = (unsigned char)u; |
497 |
1329 |
if (p[k] == '\0') |
498 |
345 |
break; |
499 |
984 |
if (p[k] != '.') |
500 |
6 |
return (0); |
501 |
978 |
p += k + 1; |
502 |
978 |
} |
503 |
345 |
if (ae->t_mask == NULL) |
504 |
78 |
ae->mask = 8 + 8 * i; |
505 |
345 |
vcc_acl_add_entry(tl, ae, 4, b, AF_INET); |
506 |
345 |
return (1); |
507 |
510 |
} |
508 |
|
|
509 |
|
static void |
510 |
522 |
vcc_acl_entry(struct vcc *tl) |
511 |
|
{ |
512 |
|
struct acl_e ae[1]; |
513 |
|
char *sl, *e; |
514 |
|
|
515 |
522 |
INIT_OBJ(ae, VCC_ACL_E_MAGIC); |
516 |
|
|
517 |
522 |
if (tl->t->tok == '!') { |
518 |
120 |
ae->not = 1; |
519 |
120 |
vcc_NextToken(tl); |
520 |
120 |
} |
521 |
|
|
522 |
522 |
if (tl->t->tok == '(') { |
523 |
9 |
ae->para = 1; |
524 |
9 |
vcc_NextToken(tl); |
525 |
9 |
} |
526 |
|
|
527 |
522 |
if (!ae->not && tl->t->tok == '!') { |
528 |
3 |
ae->not = 1; |
529 |
3 |
vcc_NextToken(tl); |
530 |
3 |
} |
531 |
|
|
532 |
522 |
ExpectErr(tl, CSTR); |
533 |
522 |
ae->t_addr = tl->t; |
534 |
522 |
ae->addr = ae->t_addr->dec; |
535 |
522 |
vcc_NextToken(tl); |
536 |
|
|
537 |
522 |
if (strchr(ae->t_addr->dec, '/') != NULL) { |
538 |
9 |
sl = strchr(ae->addr, '/'); |
539 |
9 |
AN(sl); |
540 |
9 |
*sl++ = '\0'; |
541 |
9 |
e = NULL; |
542 |
9 |
ae->mask = strtoul(sl, &e, 10); |
543 |
9 |
if (*e != '\0') { |
544 |
6 |
VSB_cat(tl->sb, ".../mask is not numeric.\n"); |
545 |
6 |
vcc_ErrWhere(tl, ae->t_addr); |
546 |
6 |
return; |
547 |
|
} |
548 |
3 |
ae->t_mask = ae->t_addr; |
549 |
3 |
if (tl->t->tok == '/') { |
550 |
3 |
VSB_cat(tl->sb, "/mask only allowed once.\n"); |
551 |
3 |
vcc_ErrWhere(tl, tl->t); |
552 |
3 |
return; |
553 |
|
} |
554 |
513 |
} else if (tl->t->tok == '/') { |
555 |
405 |
vcc_NextToken(tl); |
556 |
405 |
ae->t_mask = tl->t; |
557 |
405 |
ExpectErr(tl, CNUM); |
558 |
405 |
ae->mask = vcc_UintVal(tl); |
559 |
405 |
} |
560 |
|
|
561 |
513 |
if (ae->para) |
562 |
9 |
SkipToken(tl, ')'); |
563 |
|
|
564 |
510 |
if (!vcc_acl_try_netnotation(tl, ae)) { |
565 |
165 |
ERRCHK(tl); |
566 |
165 |
vcc_acl_try_getaddrinfo(tl, ae); |
567 |
165 |
} |
568 |
510 |
ERRCHK(tl); |
569 |
522 |
} |
570 |
|
|
571 |
|
/********************************************************************* |
572 |
|
* Emit the tokens making up an entry as C-strings |
573 |
|
*/ |
574 |
|
|
575 |
|
static void |
576 |
153 |
vcc_acl_emit_tokens(const struct vcc *tl, const struct acl_e *ae) |
577 |
|
{ |
578 |
|
struct token *t; |
579 |
153 |
const char *sep = ""; |
580 |
|
|
581 |
153 |
CHECK_OBJ_NOTNULL(ae, VCC_ACL_E_MAGIC); |
582 |
153 |
t = ae->t_addr; |
583 |
153 |
do { |
584 |
447 |
if (t->tok == CSTR) { |
585 |
153 |
Fh(tl, 0, "%s\"\\\"\" ", sep); |
586 |
153 |
EncToken(tl->fh, t); |
587 |
153 |
Fh(tl, 0, " \"\\\"\""); |
588 |
447 |
} else if (t == ae->t_mask) { |
589 |
147 |
Fh(tl, 0, " \"%u\"", ae->mask - 8); |
590 |
147 |
} else { |
591 |
147 |
Fh(tl, 0, "%s\"%.*s\"", sep, PF(t)); |
592 |
|
} |
593 |
447 |
if (t == ae->t_mask) |
594 |
147 |
break; |
595 |
300 |
t = vcc_PeekTokenFrom(tl, t); |
596 |
300 |
AN(t); |
597 |
300 |
sep = " "; |
598 |
300 |
} while (ae->t_mask != NULL); |
599 |
153 |
if (ae->fixed) |
600 |
30 |
Fh(tl, 0, "\" fixed: %s\"", ae->fixed); |
601 |
153 |
} |
602 |
|
|
603 |
|
/********************************************************************* |
604 |
|
* Emit ACL on table format |
605 |
|
*/ |
606 |
|
|
607 |
|
static unsigned |
608 |
6 |
vcc_acl_emit_tables(const struct vcc *tl, unsigned n, const char *name) |
609 |
|
{ |
610 |
|
struct acl_e *ae; |
611 |
6 |
unsigned rv = sizeof(ae->data) + 3; |
612 |
6 |
unsigned nn = 0; |
613 |
|
size_t sz; |
614 |
|
|
615 |
12 |
Fh(tl, 0, "\nstatic unsigned char acl_tbl_%s[%u*%u] = {\n", |
616 |
6 |
name, n, rv); |
617 |
150 |
VRBT_FOREACH(ae, acl_tree, &tl->acl->acl_tree) { |
618 |
144 |
if (ae->overlapped) |
619 |
120 |
continue; |
620 |
24 |
Fh(tl, 0, "\t0x%02x,", ae->not ? 0 : 1); |
621 |
24 |
Fh(tl, 0, "0x%02x,", (ae->mask >> 3) - 1); |
622 |
24 |
Fh(tl, 0, "0x%02x,", (0xff00 >> (ae->mask & 7)) & 0xff); |
623 |
432 |
for (sz = 0; sz < sizeof(ae->data); sz++) |
624 |
408 |
Fh(tl, 0, "0x%02x,", ae->data[sz]); |
625 |
24 |
for (; sz < rv - 3; sz++) |
626 |
0 |
Fh(tl, 0, "0,"); |
627 |
24 |
Fh(tl, 0, "\n"); |
628 |
24 |
nn++; |
629 |
24 |
} |
630 |
6 |
assert(n == nn); |
631 |
6 |
Fh(tl, 0, "};\n"); |
632 |
6 |
if (tl->acl->flag_log) { |
633 |
6 |
Fh(tl, 0, "\nstatic const char *acl_str_%s[%d] = {\n", |
634 |
3 |
name, n); |
635 |
75 |
VRBT_FOREACH(ae, acl_tree, &tl->acl->acl_tree) { |
636 |
72 |
if (ae->overlapped) |
637 |
60 |
continue; |
638 |
12 |
Fh(tl, 0, "\t"); |
639 |
24 |
Fh(tl, 0, "\"%sMATCH %s \" ", |
640 |
12 |
ae->not ? "NEG_" : "", name); |
641 |
12 |
vcc_acl_emit_tokens(tl, ae); |
642 |
12 |
Fh(tl, 0, ",\n"); |
643 |
12 |
} |
644 |
3 |
Fh(tl, 0, "};\n"); |
645 |
3 |
} |
646 |
6 |
return (rv); |
647 |
|
} |
648 |
|
|
649 |
|
/********************************************************************* |
650 |
|
* Emit a function to match the ACL we have collected |
651 |
|
*/ |
652 |
|
|
653 |
|
static void |
654 |
78 |
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 |
78 |
struct inifin *ifp = NULL; |
660 |
|
struct vsb *func; |
661 |
78 |
unsigned n, no, nw = 0; |
662 |
|
|
663 |
78 |
func = VSB_new_auto(); |
664 |
78 |
AN(func); |
665 |
78 |
VSB_cat(func, "match_acl_"); |
666 |
78 |
VCC_PrintCName(func, sym->name, NULL); |
667 |
78 |
AZ(VSB_finish(func)); |
668 |
|
|
669 |
78 |
depth = -1; |
670 |
78 |
at[0] = 256; |
671 |
78 |
ae2 = NULL; |
672 |
78 |
n = no = 0; |
673 |
498 |
VRBT_FOREACH_REVERSE(ae, acl_tree, &tl->acl->acl_tree) { |
674 |
420 |
n++; |
675 |
420 |
if (ae2 == NULL) { |
676 |
75 |
ae2 = ae; |
677 |
420 |
} else if (vcl_acl_disjoint(ae, ae2)) { |
678 |
111 |
ae2 = ae; |
679 |
111 |
} else { |
680 |
234 |
no++; |
681 |
234 |
ae->overlapped = 1; |
682 |
|
} |
683 |
420 |
} |
684 |
|
|
685 |
78 |
Fh(tl, 0, "/* acl_n_%s n %u no %u */\n", sym->name, n, no); |
686 |
78 |
if (n - no < (1<<1)) |
687 |
45 |
no = n; |
688 |
33 |
else if (!tl->acl->flag_table) |
689 |
27 |
no = n; |
690 |
|
|
691 |
78 |
if (no < n) |
692 |
6 |
nw = vcc_acl_emit_tables(tl, n - no, sym->name); |
693 |
|
|
694 |
|
|
695 |
78 |
Fh(tl, 0, "\nstatic int v_matchproto_(acl_match_f)\n"); |
696 |
78 |
Fh(tl, 0, "%s(VRT_CTX, const VCL_IP p)\n", VSB_data(func)); |
697 |
78 |
Fh(tl, 0, "{\n"); |
698 |
78 |
Fh(tl, 0, "\tconst unsigned char *a;\n"); |
699 |
78 |
Fh(tl, 0, "\tint fam;\n"); |
700 |
78 |
Fh(tl, 0, "\n"); |
701 |
78 |
Fh(tl, 0, "\tfam = VRT_VSA_GetPtr(ctx, p, &a);\n"); |
702 |
78 |
Fh(tl, 0, "\tif (fam < 0) {\n"); |
703 |
78 |
Fh(tl, 0, "\t\tVRT_fail(ctx,"); |
704 |
78 |
Fh(tl, 0, " \"ACL %s: no protocol family\");\n", sym->name); |
705 |
78 |
Fh(tl, 0, "\t\treturn(0);\n"); |
706 |
78 |
Fh(tl, 0, "\t}\n\n"); |
707 |
78 |
if (!tl->err_unref) { |
708 |
6 |
ifp = New_IniFin(tl); |
709 |
12 |
VSB_printf(ifp->ini, |
710 |
6 |
"\t(void)%s;\n", VSB_data(func)); |
711 |
6 |
} |
712 |
|
|
713 |
498 |
VRBT_FOREACH(ae, acl_tree, &tl->acl->acl_tree) { |
714 |
|
|
715 |
420 |
if (no < n && !ae->overlapped) |
716 |
24 |
continue; |
717 |
|
|
718 |
|
/* Find how much common prefix we have */ |
719 |
2472 |
for (l = 0; l <= depth && l * 8 < (int)ae->mask - 7; l++) { |
720 |
2214 |
assert(l >= 0); |
721 |
2214 |
if (ae->data[l] != at[l]) |
722 |
138 |
break; |
723 |
2076 |
} |
724 |
|
|
725 |
|
/* Back down, if necessary */ |
726 |
900 |
while (l <= depth) { |
727 |
504 |
Fh(tl, 0, "\t%*s}\n", -depth, ""); |
728 |
504 |
depth--; |
729 |
|
} |
730 |
|
|
731 |
396 |
m = (int)ae->mask; |
732 |
396 |
assert(m >= l*8); |
733 |
396 |
m -= l * 8; |
734 |
|
|
735 |
|
/* Do whole byte compares */ |
736 |
1302 |
for (i = l; m >= 8; m -= 8, i++) { |
737 |
906 |
if (i == 0) |
738 |
210 |
Fh(tl, 0, "\t%*s%sif (fam == %d) {\n", |
739 |
105 |
-i, "", "", ae->data[i]); |
740 |
|
else |
741 |
1602 |
Fh(tl, 0, "\t%*s%sif (a[%d] == %d) {\n", |
742 |
801 |
-i, "", "", i - 1, ae->data[i]); |
743 |
906 |
at[i] = ae->data[i]; |
744 |
906 |
depth = i; |
745 |
906 |
} |
746 |
|
|
747 |
396 |
if (m > 0) { |
748 |
|
// XXX can remove masking due to fixup |
749 |
|
/* Do fractional byte compares */ |
750 |
456 |
Fh(tl, 0, "\t%*s%sif ((a[%d] & 0x%x) == %d) {\n", |
751 |
228 |
-i, "", "", i - 1, (0xff00 >> m) & 0xff, |
752 |
228 |
ae->data[i] & ((0xff00 >> m) & 0xff)); |
753 |
228 |
at[i] = 256; |
754 |
228 |
depth = i; |
755 |
228 |
} |
756 |
|
|
757 |
396 |
i = ((int)ae->mask + 7) / 8; |
758 |
|
|
759 |
396 |
if (tl->acl->flag_log) { |
760 |
282 |
Fh(tl, 0, "\t%*sVPI_acl_log(ctx, \"%sMATCH %s \" ", |
761 |
141 |
-i, "", ae->not ? "NEG_" : "", sym->name); |
762 |
141 |
vcc_acl_emit_tokens(tl, ae); |
763 |
141 |
Fh(tl, 0, ");\n"); |
764 |
141 |
} |
765 |
|
|
766 |
396 |
Fh(tl, 0, "\t%*sreturn (%d);\n", -i, "", ae->not ? 0 : 1); |
767 |
396 |
} |
768 |
|
|
769 |
|
/* Unwind */ |
770 |
708 |
for (; 0 <= depth; depth--) |
771 |
630 |
Fh(tl, 0, "\t%*.*s}\n", depth, depth, ""); |
772 |
|
|
773 |
78 |
if (no < n) { |
774 |
6 |
Fh(tl, 0, "\treturn(\n\t VPI_acl_table(ctx,\n"); |
775 |
6 |
Fh(tl, 0, "\t\tp,\n"); |
776 |
6 |
Fh(tl, 0, "\t\t%u, %u,\n", n - no, nw); |
777 |
6 |
Fh(tl, 0, "\t\tacl_tbl_%s,\n", sym->name); |
778 |
6 |
if (tl->acl->flag_log) |
779 |
3 |
Fh(tl, 0, "\t\tacl_str_%s,\n", sym->name); |
780 |
|
else |
781 |
3 |
Fh(tl, 0, "\t\tNULL,\n"); |
782 |
6 |
Fh(tl, 0, "\t\t\"NO MATCH %s\"\n\t )\n\t);\n", sym->name); |
783 |
6 |
} else { |
784 |
|
/* Deny by default */ |
785 |
72 |
if (tl->acl->flag_log) |
786 |
18 |
Fh(tl, 0, "\tVPI_acl_log(ctx, \"NO_MATCH %s\");\n", |
787 |
9 |
sym->name); |
788 |
72 |
Fh(tl, 0, "\treturn(0);\n"); |
789 |
|
} |
790 |
78 |
Fh(tl, 0, "}\n"); |
791 |
|
|
792 |
|
/* Emit the struct that will be referenced */ |
793 |
78 |
Fh(tl, 0, "\nstatic const struct vrt_acl %s[] = {{\n", sym->rname); |
794 |
78 |
Fh(tl, 0, "\t.magic = VRT_ACL_MAGIC,\n"); |
795 |
78 |
Fh(tl, 0, "\t.match = &%s,\n", VSB_data(func)); |
796 |
78 |
Fh(tl, 0, "\t.name = \"%s\",\n", sym->name); |
797 |
78 |
Fh(tl, 0, "}};\n\n"); |
798 |
78 |
if (!tl->err_unref) { |
799 |
6 |
AN(ifp); |
800 |
6 |
VSB_printf(ifp->ini, "\t(void)%s;", sym->rname); |
801 |
6 |
} |
802 |
78 |
VSB_destroy(&func); |
803 |
78 |
} |
804 |
|
|
805 |
|
void |
806 |
123 |
vcc_ParseAcl(struct vcc *tl) |
807 |
|
{ |
808 |
|
struct symbol *sym; |
809 |
|
int sign; |
810 |
|
struct acl acl[1]; |
811 |
|
|
812 |
123 |
INIT_OBJ(acl, VCC_ACL_MAGIC); |
813 |
123 |
tl->acl = acl; |
814 |
123 |
acl->flag_pedantic = 1; |
815 |
123 |
vcc_NextToken(tl); |
816 |
123 |
VRBT_INIT(&acl->acl_tree); |
817 |
|
|
818 |
123 |
vcc_ExpectVid(tl, "ACL"); |
819 |
123 |
ERRCHK(tl); |
820 |
120 |
sym = VCC_HandleSymbol(tl, ACL); |
821 |
120 |
ERRCHK(tl); |
822 |
120 |
AN(sym); |
823 |
|
|
824 |
165 |
while (1) { |
825 |
165 |
sign = vcc_IsFlag(tl); |
826 |
165 |
if (tl->err) { |
827 |
3 |
VSB_cat(tl->sb, |
828 |
|
"Valid ACL flags are `log` and `table`:\n"); |
829 |
3 |
return; |
830 |
|
} |
831 |
162 |
if (sign < 0) |
832 |
114 |
break; |
833 |
48 |
if (vcc_IdIs(tl->t, "log")) { |
834 |
12 |
acl->flag_log = sign; |
835 |
12 |
vcc_NextToken(tl); |
836 |
48 |
} else if (vcc_IdIs(tl->t, "fold")) { |
837 |
3 |
acl->flag_fold = sign; |
838 |
3 |
vcc_NextToken(tl); |
839 |
36 |
} else if (vcc_IdIs(tl->t, "pedantic")) { |
840 |
24 |
acl->flag_pedantic = sign; |
841 |
24 |
vcc_NextToken(tl); |
842 |
33 |
} else if (vcc_IdIs(tl->t, "table")) { |
843 |
6 |
acl->flag_table = sign; |
844 |
6 |
vcc_NextToken(tl); |
845 |
6 |
} else { |
846 |
3 |
VSB_cat(tl->sb, "Unknown ACL flag:\n"); |
847 |
3 |
vcc_ErrWhere(tl, tl->t); |
848 |
3 |
return; |
849 |
|
} |
850 |
|
} |
851 |
|
|
852 |
114 |
SkipToken(tl, '{'); |
853 |
|
|
854 |
600 |
while (tl->t->tok != '}') { |
855 |
522 |
vcc_acl_entry(tl); |
856 |
522 |
ERRCHK(tl); |
857 |
489 |
SkipToken(tl, ';'); |
858 |
|
} |
859 |
78 |
SkipToken(tl, '}'); |
860 |
|
|
861 |
78 |
vcc_acl_emit(tl, sym); |
862 |
123 |
} |