varnish-cache/bin/varnishd/cache/cache_vrt.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 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
 * Runtime support for compiled VCL programs
31
 */
32
33
#include "config.h"
34
35
#include <stdlib.h>
36
37
#include "cache_varnishd.h"
38
39
#include "cache_objhead.h"
40
#include "vav.h"
41
#include "vcl.h"
42
#include "vct.h"
43
#include "venc.h"
44
#include "vend.h"
45
#include "vrt_obj.h"
46
#include "vsa.h"
47
#include "vsha256.h"
48
#include "vtcp.h"
49
#include "vtim.h"
50
#include "vcc_interface.h"
51
52
#include "common/heritage.h"
53
#include "common/vsmw.h"
54
#include "proxy/cache_proxy.h"
55
56
// NOT using TOSTRANDS() to create a NULL pointer element despite n == 0
57
const struct strands *vrt_null_strands = &(struct strands){
58
        .n = 0,
59
        .p = (const char *[1]){NULL}
60
};
61
const struct vrt_blob *vrt_null_blob = &(struct vrt_blob){
62
        .type = VRT_NULL_BLOB_TYPE,
63
        .len = 0,
64
        .blob = "\0"
65
};
66
67
/*--------------------------------------------------------------------*/
68
69
VCL_VOID
70 1101
VRT_synth(VRT_CTX, VCL_INT code, VCL_STRING reason)
71
{
72
        const char *ret;
73
74 1101
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
75 1101
        assert(ctx->req != NULL || ctx->bo != NULL);
76
77 1101
        ret = ctx->req == NULL ? "error" : "synth";
78 1101
        if (code < 0) {
79 0
                VRT_fail(ctx, "return(%s()) status code (%jd) is negative",
80 0
                    ret, (intmax_t)code);
81 0
                return;
82
        }
83 1101
        if (code > 65535) {
84 0
                VRT_fail(ctx, "return(%s()) status code (%jd) > 65535",
85 0
                    ret, (intmax_t)code);
86 0
                return;
87
        }
88 1101
        if ((code % 1000) < 100) {
89 24
                VRT_fail(ctx,
90
                    "illegal return(%s()) status code (%jd) (..0##)",
91 12
                    ret, (intmax_t)code);
92 12
                return;
93
        }
94
95 1089
        if (reason && !WS_Allocated(ctx->ws, reason, -1)) {
96 132
                reason = WS_Copy(ctx->ws, reason, -1);
97 132
                if (!reason) {
98 0
                        VRT_fail(ctx, "Workspace overflow");
99 0
                        return;
100
                }
101 132
        }
102
103 1089
        if (ctx->req == NULL) {
104 36
                CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
105 36
                ctx->bo->err_code = (uint16_t)code;
106 36
                ctx->bo->err_reason = reason ? reason
107 27
                    : http_Status2Reason(ctx->bo->err_code % 1000, NULL);
108 36
                return;
109
        }
110
111 1053
        ctx->req->err_code = (uint16_t)code;
112 1053
        ctx->req->err_reason = reason ? reason
113 753
            : http_Status2Reason(ctx->req->err_code % 1000, NULL);
114 1101
}
115
116
/*--------------------------------------------------------------------*/
117
118
void
119 345
VPI_acl_log(VRT_CTX, const char *msg)
120
{
121
122 345
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
123 345
        AN(msg);
124 345
        if (ctx->vsl != NULL)
125 345
                VSLbs(ctx->vsl, SLT_VCL_acl, TOSTRAND(msg));
126
        else
127 0
                VSL(SLT_VCL_acl, NO_VXID, "%s", msg);
128 345
}
129
130
int
131 289470
VRT_acl_match(VRT_CTX, VCL_ACL acl, VCL_IP ip)
132
{
133
134 289470
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
135 289470
        if (acl == NULL || ip == NULL) {
136 18
                VRT_fail(ctx, "Cannot match a null %s",
137 9
                         acl == NULL ? "ACL" : "IP address");
138 9
                return (0);
139
        }
140 289461
        CHECK_OBJ(acl, VRT_ACL_MAGIC);
141 289461
        assert(VSA_Sane(ip));
142 289461
        return (acl->match(ctx, ip));
143 289470
}
144
145
static int
146 600
acl_tbl_cmp(int fam, const uint8_t *key, const uint8_t *b)
147
{
148
        int rv;
149
150 600
        rv = fam - (int)b[3];
151 600
        if (rv == 0 && b[1] > 0)
152 414
                rv = memcmp(key, b + 4, b[1]);
153 600
        if (rv == 0 && b[2])
154 336
                rv = (int)(key[b[1]] & b[2]) - (int)b[4 + b[1]];
155 600
        return (rv);
156
}
157
158
static const uint8_t *
159 276
bbsearch(int fam, const uint8_t *key, const uint8_t *base0,
160
    size_t nmemb, size_t size)
161
{
162 276
        const uint8_t *base = base0;
163
        size_t lim;
164
        int cmp;
165
        const uint8_t *p;
166
167 804
        for (lim = nmemb; lim != 0; lim >>= 1) {
168 600
                p = base + (lim >> 1) * size;
169 600
                cmp = acl_tbl_cmp(fam, key, p);
170 600
                if (cmp == 0)
171 72
                        return (p);
172 528
                if (cmp > 0) {
173
                        /* key > p: move right */
174 228
                        base = p + size;
175 228
                        lim--;
176 228
                } /* else move left */
177 528
        }
178 204
        return (NULL);
179 276
}
180
181
int
182 276
VPI_acl_table(VRT_CTX, VCL_IP p, unsigned n, unsigned m, const uint8_t *tbl,
183
    const char * const *str, const char *fin)
184
{
185
        int fam;
186
        const uint8_t *key;
187
        const uint8_t *ptr;
188
        size_t sz;
189
190 276
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
191 276
        AN(p);
192 276
        AN(n);
193 276
        assert(m == 20);
194 276
        AN(tbl);
195 276
        AN(fin);
196
197 276
        fam = VRT_VSA_GetPtr(ctx, p, &key);
198 276
        ptr = bbsearch(fam, key, tbl, n, m);
199
200 276
        if (ptr != NULL) {
201 72
                sz = ptr - tbl;
202 72
                AZ(sz % m);
203 72
                sz /= m;
204 72
                if (str != NULL)
205 36
                        VPI_acl_log(ctx, str[sz]);
206 72
                return (*ptr);
207
        }
208 204
        if (str != NULL)
209 102
                VPI_acl_log(ctx, fin);
210 204
        return (0);
211 276
}
212
213
/*--------------------------------------------------------------------*/
214
215
VCL_VOID
216 66
VRT_hit_for_pass(VRT_CTX, VCL_DURATION d)
217
{
218
        struct objcore *oc;
219
220 66
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
221 66
        if (ctx->bo == NULL) {
222 0
                VSLb(ctx->vsl, SLT_Error,
223
                    "Note: Ignoring DURATION argument to return(pass);");
224 0
                return;
225
        }
226 66
        CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
227 66
        oc = ctx->bo->fetch_objcore;
228 66
        oc->ttl = d;
229 66
        oc->grace = 0.0;
230 66
        oc->keep = 0.0;
231 132
        VSLb(ctx->vsl, SLT_TTL, "HFP %.0f %.0f %.0f %.0f uncacheable",
232 66
            oc->ttl, oc->grace, oc->keep, oc->t_origin);
233 66
}
234
235
/*--------------------------------------------------------------------*/
236
237
VCL_HTTP
238 96996
VRT_selecthttp(VRT_CTX, enum gethdr_e where)
239
{
240
        VCL_HTTP hp;
241
242 96996
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
243 96996
        switch (where) {
244
        case HDR_REQ:
245 58335
                hp = ctx->http_req;
246 58335
                break;
247
        case HDR_REQ_TOP:
248 21
                hp = ctx->http_req_top;
249 21
                break;
250
        case HDR_BEREQ:
251 1527
                hp = ctx->http_bereq;
252 1527
                break;
253
        case HDR_BERESP:
254 27147
                hp = ctx->http_beresp;
255 27147
                break;
256
        case HDR_RESP:
257 9963
                hp = ctx->http_resp;
258 9963
                break;
259
        case HDR_OBJ:
260 3
                hp = NULL;
261 3
                break;
262
        default:
263 0
                WRONG("VRT_selecthttp 'where' invalid");
264 0
        }
265 96996
        return (hp);
266
}
267
268
/*--------------------------------------------------------------------*/
269
270
VCL_STRING
271 76408
VRT_GetHdr(VRT_CTX, VCL_HEADER hs)
272
{
273
        VCL_HTTP hp;
274
        const char *p;
275
276 76408
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
277 76408
        if (hs->where == HDR_OBJ) {
278 42
                CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
279 42
                CHECK_OBJ_NOTNULL(ctx->req->objcore, OBJCORE_MAGIC);
280 84
                return (HTTP_GetHdrPack(ctx->req->wrk, ctx->req->objcore,
281 42
                    hs->what));
282
        }
283 76366
        hp = VRT_selecthttp(ctx, hs->where);
284 76366
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
285 76366
        if (!http_GetHdr(hp, hs->what, &p))
286 35346
                return (NULL);
287 41020
        return (p);
288 76408
}
289
290
/*--------------------------------------------------------------------
291
 * Alloc Strands with space for n elements on workspace
292
 *
293
 * Error handling is deliberately left to the caller
294
 */
295
296
struct strands *
297 0
VRT_AllocStrandsWS(struct ws *ws, int n)
298
{
299
        struct strands *s;
300
        const char **p;
301
302 0
        s = WS_Alloc(ws, sizeof *s);
303 0
        p = WS_Alloc(ws, n * sizeof *p);
304
305 0
        if (s == NULL || p == NULL)
306 0
                return (NULL);
307
308 0
        s->n = n;
309 0
        s->p = p;
310
311 0
        return (s);
312 0
}
313
314
/*--------------------------------------------------------------------
315
 * Compare two STRANDS
316
 */
317
318
int
319 555
VRT_CompareStrands(VCL_STRANDS a, VCL_STRANDS b)
320
{
321 555
        const char *pa = NULL, *pb = NULL;
322 555
        int na = 0, nb = 0;
323
324 3693
        while (1) {
325 3693
                if (pa != NULL && *pa == '\0')
326 330
                        pa = NULL;
327 3693
                if (pb != NULL && *pb == '\0')
328 441
                        pb = NULL;
329 3693
                if (pa == NULL && na < a->n)
330 756
                        pa = a->p[na++];
331 2937
                else if (pb == NULL && nb < b->n)
332 885
                        pb = b->p[nb++];
333 2052
                else if (pa == NULL && pb == NULL)
334 186
                        return (0);
335 1866
                else if (pa == NULL)
336 132
                        return (-1);
337 1734
                else if (pb == NULL)
338 36
                        return (1);
339 1698
                else if (*pa == '\0')
340 0
                        pa = NULL;
341 1698
                else if (*pb == '\0')
342 0
                        pb = NULL;
343 1698
                else if (*pa != *pb)
344 201
                        return (*pa - *pb);
345
                else {
346 1497
                        pa++;
347 1497
                        pb++;
348
                }
349
        }
350 555
}
351
352
/*--------------------------------------------------------------------
353
 * STRANDS to BOOL
354
 */
355
356
VCL_BOOL
357 41027
VRT_Strands2Bool(VCL_STRANDS s)
358
{
359
        int i;
360
361 41027
        AN(s);
362 64154
        for (i = 0; i < s->n; i++)
363 41026
                if (s->p[i] != NULL)
364 17899
                        return (1);
365 23128
        return (0);
366 41027
}
367
368
/*--------------------------------------------------------------------
369
 * Hash a STRANDS
370
 */
371
372
uint32_t
373 5178
VRT_HashStrands32(VCL_STRANDS s)
374
{
375
        struct VSHA256Context sha_ctx;
376
        unsigned char sha256[VSHA256_LEN];
377
        const char *p;
378
        int i;
379
380 5178
        AN(s);
381 5178
        VSHA256_Init(&sha_ctx);
382 15414
        for (i = 0; i < s->n; i++) {
383 10236
                p = s->p[i];
384 10236
                if (p != NULL && *p != '\0')
385 10224
                        VSHA256_Update(&sha_ctx, p, strlen(p));
386 10236
        }
387 5178
        VSHA256_Final(sha256, &sha_ctx);
388
389
        /* NB: for some reason vmod_director's shard director specifically
390
         * relied on little-endian decoding of the last 4 octets. In order
391
         * to maintain a stable hash function to share across consumers we
392
         * need to stick to that.
393
         */
394 5178
        return (vle32dec(sha256 + VSHA256_LEN - 4));
395
}
396
397
398
/*--------------------------------------------------------------------
399
 * Collapse STRANDS into the space provided, or return NULL
400
 */
401
402
char *
403 14229
VRT_Strands(char *d, size_t dl, VCL_STRANDS s)
404
{
405
        char *b;
406
        const char *e;
407
        unsigned x;
408
409 14229
        AN(d);
410 14229
        AN(s);
411 14229
        b = d;
412 14229
        e = b + dl;
413 28566
        for (int i = 0; i < s->n; i++)
414 28242
                if (s->p[i] != NULL && *s->p[i] != '\0') {
415 13905
                        x = strlen(s->p[i]);
416 13905
                        if (b + x >= e)
417 168
                                return (NULL);
418 13737
                        memcpy(b, s->p[i], x);
419 13737
                        b += x;
420 13737
                }
421 14061
        assert(b < e);
422 14061
        *b++ = '\0';
423 14061
        return (b);
424 14229
}
425
426
/*--------------------------------------------------------------------
427
 * Copy and merge STRANDS into a workspace.
428
 */
429
430
VCL_STRING
431 1407
VRT_StrandsWS(struct ws *ws, const char *h, VCL_STRANDS s)
432
{
433 1407
        const char *q = NULL;
434
        struct vsb vsb[1];
435
        int i;
436
437 1407
        WS_Assert(ws);
438 1407
        AN(s);
439
440 1488
        for (i = 0; i < s->n; i++) {
441 1461
                if (s->p[i] != NULL && *s->p[i] != '\0') {
442 1380
                        q = s->p[i];
443 1380
                        break;
444
                }
445 81
        }
446
447 1407
        if (q == NULL) {
448 27
                if (h == NULL)
449 24
                        return ("");
450 3
                if (WS_Allocated(ws, h, -1))
451 0
                        return (h);
452 1383
        } else if (h == NULL && WS_Allocated(ws, q, -1)) {
453 138
                for (i++; i < s->n; i++)
454 87
                        if (s->p[i] != NULL && *s->p[i] != '\0')
455 57
                                break;
456 108
                if (i == s->n)
457 51
                        return (q);
458 57
        }
459
460 1332
        WS_VSB_new(vsb, ws);
461 1332
        if (h != NULL)
462 798
                VSB_cat(vsb, h);
463 3057
        for (i = 0; i < s->n; i++) {
464 1725
                if (s->p[i] != NULL && *s->p[i] != '\0')
465 1620
                        VSB_cat(vsb, s->p[i]);
466 1725
        }
467 1332
        return (WS_VSB_finish(vsb, ws, NULL));
468 1407
}
469
470
/*--------------------------------------------------------------------
471
 * Copy and merge STRANDS on the current workspace
472
 */
473
474
VCL_STRING
475 231
VRT_STRANDS_string(VRT_CTX, VCL_STRANDS s)
476
{
477
        const char *b;
478
479 231
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
480 231
        CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
481 231
        b = VRT_StrandsWS(ctx->ws, NULL, s);
482 231
        if (b == NULL)
483 3
                VRT_fail(ctx, "Workspace overflow");
484 231
        return (b);
485
}
486
487
/*--------------------------------------------------------------------
488
 * upper/lower-case STRANDS (onto workspace)
489
 */
490
491
#include <stdio.h>
492
493
VCL_STRING
494 33
VRT_UpperLowerStrands(VRT_CTX, VCL_STRANDS s, int up)
495
{
496
        unsigned u;
497
        char *b, *e, *r;
498 33
        const char *p, *q = NULL;
499 33
        int i, copy = 0;
500
501 33
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
502 33
        CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
503 33
        AN(s);
504 33
        u = WS_ReserveAll(ctx->ws);
505 33
        r = b = WS_Reservation(ctx->ws);
506 33
        e = b + u;
507 78
        for (i = 0; i < s->n; i++) {
508 45
                if (s->p[i] == NULL || s->p[i][0] == '\0')
509 0
                        continue;
510 45
                if (q != NULL)
511 12
                        copy = 1;
512 252
                for(p = q = s->p[i]; *p != '\0'; p++) {
513 285
                        if ((up && vct_islower(*p)) ||
514 183
                            (!up && vct_isupper(*p))) {
515 102
                                *b++ = *p ^ 0x20;
516 102
                                copy = 1;
517 207
                        } else if (b < e) {
518 105
                                *b++ = *p;
519 105
                        }
520 207
                        if (copy && b == e)
521 0
                                break;
522 207
                }
523 45
                if (copy && b == e) {
524 0
                        WS_Release(ctx->ws, 0);
525 0
                        VRT_fail(ctx, "Workspace overflow");
526 0
                        return (NULL);
527
                }
528 45
        }
529 33
        assert(b <= e);
530 33
        if (!copy) {
531 9
                WS_Release(ctx->ws, 0);
532 9
                return (q);
533
        }
534 24
        assert(b < e);
535 24
        *b++ = '\0';
536 24
        assert(b <= e);
537 24
        WS_ReleaseP(ctx->ws, b);
538 24
        return (r);
539 33
}
540
541
542
// rfc9110,l,1585,1589
543
//     field-content  = field-vchar
544
//                      [ 1*( SP / HTAB / field-vchar ) field-vchar ]
545
//     field-vchar    = VCHAR / obs-text
546
//     obs-text       = %x80-FF
547
//
548
// This implementation is less strict, see #4221
549
static inline VCL_BOOL
550 14064
validhdr(const char *p)
551
{
552 14064
        AN(p);
553 561633
        for(;*p != '\0'; p++)
554 547575
                if (! vct_ishdrval(*p))
555 6
                        return (0);
556 14058
        return (1);
557 14064
}
558
559
/*--------------------------------------------------------------------*/
560
VCL_BOOL
561 6
VRT_ValidHdr(VRT_CTX, VCL_STRANDS s)
562
{
563
        int i;
564
565 6
        (void) ctx;
566
567 9
        for (i = 0; i < s->n; i++) {
568 6
                if (s->p[i] == NULL || s->p[i][0] == '\0')
569 0
                        continue;
570 6
                if (! validhdr(s->p[i]))
571 3
                        return (0);
572 3
        }
573
574 3
        return (1);
575 6
}
576
/*--------------------------------------------------------------------*/
577
578
VCL_VOID
579 5952
VRT_UnsetHdr(VRT_CTX, VCL_HEADER hs)
580
{
581
        VCL_HTTP hp;
582
583 5952
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
584 5952
        AN(hs);
585 5952
        AN(hs->what);
586 5952
        hp = VRT_selecthttp(ctx, hs->where);
587 5952
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
588 5952
        http_Unset(hp, hs->what);
589 5952
}
590
591
VCL_VOID
592 14616
VRT_SetHdr(VRT_CTX, VCL_HEADER hs, const char *pfx, VCL_STRANDS s)
593
{
594
        VCL_HTTP hp;
595
        unsigned u, l, pl;
596
        char *p, *b;
597
598 14616
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
599 14616
        AN(hs);
600 14616
        AN(hs->what);
601 14616
        hp = VRT_selecthttp(ctx, hs->where);
602 14616
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
603
604 14616
        u = WS_ReserveAll(hp->ws);
605 14616
        pl = (pfx == NULL) ? 0 : strlen(pfx);
606 14616
        l = hs->what[0] + 1 + pl;
607 14616
        if (u <= l) {
608 381
                WS_Release(hp->ws, 0);
609 381
                WS_MarkOverflow(hp->ws);
610 381
                VSLbs(ctx->vsl, SLT_LostHeader, TOSTRAND(hs->what + 1));
611 381
                return;
612
        }
613 14235
        b = WS_Reservation(hp->ws);
614 14235
        if (s != NULL) {
615 14211
                p = VRT_Strands(b + l, u - l, s);
616 14211
                if (p == NULL) {
617 168
                        WS_Release(hp->ws, 0);
618 168
                        WS_MarkOverflow(hp->ws);
619 336
                        VSLbs(ctx->vsl, SLT_LostHeader,
620 168
                            TOSTRAND(hs->what + 1));
621 168
                        return;
622
                }
623 14043
        } else {
624 24
                b[l] = '\0';
625
        }
626 14067
        p = b;
627 14067
        memcpy(p, hs->what + 1, hs->what[0]);
628 14067
        p += hs->what[0];
629 14067
        *p++ = ' ';
630 14067
        if (pfx != NULL)
631 30
                memcpy(p, pfx, pl);
632 14067
        p += pl;
633 14067
        if (FEATURE(FEATURE_VALIDATE_HEADERS) && !validhdr(b)) {
634 3
                VRT_fail(ctx, "Bad header %s", b);
635 3
                WS_Release(hp->ws, 0);
636 3
                return;
637
        }
638 14064
        WS_ReleaseP(hp->ws, strchr(p, '\0') + 1);
639 14064
        http_Unset(hp, hs->what);
640 14064
        http_SetHeader(hp, b);
641 14616
}
642
643
/*--------------------------------------------------------------------*/
644
645
VCL_VOID
646 60406
VRT_handling(VRT_CTX, unsigned hand)
647
{
648
649 60406
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
650 60406
        assert(hand != VCL_RET_FAIL);
651 60406
        AN(ctx->vpi);
652 60406
        AZ(ctx->vpi->handling);
653 60406
        assert(hand > 0);
654 60406
        assert(hand < VCL_RET_MAX);
655 60406
        ctx->vpi->handling = hand;
656 60406
}
657
658
unsigned
659 45
VRT_handled(VRT_CTX)
660
{
661 45
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
662 45
        AN(ctx->vpi);
663 45
        return (ctx->vpi->handling);
664
}
665
666
/* the trace value is cached in the VPI for efficiency */
667
VCL_VOID
668 3
VRT_trace(VRT_CTX, VCL_BOOL a)
669
{
670 3
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
671 3
        AN(ctx->vpi);
672 3
        ctx->vpi->trace = a;
673 3
}
674
675
/*--------------------------------------------------------------------*/
676
677
VCL_VOID
678 609
VRT_fail(VRT_CTX, const char *fmt, ...)
679
{
680
        va_list ap;
681
682 609
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
683 609
        assert(ctx->vsl != NULL || ctx->msg != NULL);
684 609
        AN(ctx->vpi);
685 609
        if (ctx->vpi->handling == VCL_RET_FAIL)
686 9
                return;
687 600
        AZ(ctx->vpi->handling);
688 600
        AN(fmt);
689 600
        AZ(strchr(fmt, '\n'));
690 600
        va_start(ap, fmt);
691 600
        if (ctx->vsl != NULL) {
692 477
                VSLbv(ctx->vsl, SLT_VCL_Error, fmt, ap);
693 477
        } else {
694 123
                AN(ctx->msg);
695 123
                VSB_vprintf(ctx->msg, fmt, ap);
696 123
                VSB_putc(ctx->msg, '\n');
697
        }
698 600
        va_end(ap);
699 600
        ctx->vpi->handling = VCL_RET_FAIL;
700 609
}
701
702
/*--------------------------------------------------------------------
703
 * Feed data into the hash calculation
704
 */
705
706
VCL_VOID
707 21744
VRT_hashdata(VRT_CTX, VCL_STRANDS s)
708
{
709
        int i;
710
711 21744
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
712 21744
        CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
713 21744
        AN(ctx->specific);
714 21744
        AN(s);
715 43498
        for (i = 0; i < s->n; i++)
716 21754
                HSH_AddString(ctx->req, ctx->specific, s->p[i]);
717
        /*
718
         * Add a 'field-separator' to make it more difficult to
719
         * manipulate the hash.
720
         */
721 21744
        HSH_AddString(ctx->req, ctx->specific, NULL);
722 21744
}
723
724
/*--------------------------------------------------------------------*/
725
726
VCL_TIME
727 480
VRT_r_now(VRT_CTX)
728
{
729 480
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
730 480
        return (ctx->now);
731
}
732
733
/*--------------------------------------------------------------------*/
734
735
VCL_STRING v_matchproto_()
736 742
VRT_IP_string(VRT_CTX, VCL_IP ip)
737
{
738
        char buf[VTCP_ADDRBUFSIZE];
739
740 742
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
741 742
        if (ip == NULL) {
742 0
                VRT_fail(ctx, "%s: Illegal IP", __func__);
743 0
                return (NULL);
744
        }
745 742
        VTCP_name(ip, buf, sizeof buf, NULL, 0);
746 742
        return (WS_Copy(ctx->ws, buf, -1));
747 742
}
748
749
int
750 8379
VRT_INT_is_valid(VCL_INT arg)
751
{
752 8379
        return (arg >= VRT_INTEGER_MIN && arg <= VRT_INTEGER_MAX);
753
}
754
755
756
VCL_STRING v_matchproto_()
757 8373
VRT_INT_string(VRT_CTX, VCL_INT num)
758
{
759
760 8373
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
761 8373
        if (!VRT_INT_is_valid(num))
762 6
                VRT_fail(ctx, "INT overflow converting to string (0x%jx)",
763 3
                    (intmax_t)num);
764 8373
        return (WS_Printf(ctx->ws, "%jd", (intmax_t)num));
765
}
766
767
int
768 690
VRT_REAL_is_valid(VCL_REAL arg)
769
{
770 690
        return (!isnan(arg) && arg >= VRT_DECIMAL_MIN && arg <= VRT_DECIMAL_MAX);
771
}
772
773
VCL_STRING v_matchproto_()
774 678
VRT_REAL_string(VRT_CTX, VCL_REAL num)
775
{
776
777 678
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
778 678
        if (!VRT_REAL_is_valid(num))
779 3
                VRT_fail(ctx, "REAL overflow converting to string (%e)", num);
780 678
        return (WS_Printf(ctx->ws, "%.3f", num));
781
}
782
783
VCL_STRING v_matchproto_()
784 228
VRT_TIME_string(VRT_CTX, VCL_TIME t)
785
{
786
        char *p;
787
788 228
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
789 228
        p = WS_Alloc(ctx->ws, VTIM_FORMAT_SIZE);
790 228
        if (p == NULL) {
791 0
                VRT_fail(ctx, "Workspace overflow");
792 0
                return (NULL);
793
        }
794 228
        VTIM_format(t, p);
795 228
        if (*p == '\0') {
796 3
                VRT_fail(ctx, "Unformattable VCL_TIME");
797 3
                return (NULL);
798
        }
799 225
        return (p);
800 228
}
801
802
VCL_STRING v_matchproto_()
803 13560
VRT_BACKEND_string(VCL_BACKEND d)
804
{
805 13560
        if (d == NULL)
806 0
                return (NULL);
807 13560
        CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
808 13560
        return (d->vcl_name);
809 13560
}
810
811
VCL_STRING v_matchproto_()
812 1611
VRT_BOOL_string(VCL_BOOL val)
813
{
814
815 1611
        return (val ? "true" : "false");
816
}
817
818
VCL_STRING v_matchproto_()
819 48
VRT_BLOB_string(VRT_CTX, VCL_BLOB val)
820
{
821
        struct vsb vsb[1];
822
        const char *s;
823
824 48
        if (val == NULL)
825 0
                return (NULL);
826 48
        WS_VSB_new(vsb, ctx->ws);
827 48
        VSB_putc(vsb, ':');
828 48
        VENC_Encode_Base64(vsb, val->blob, val->len);
829 48
        VSB_putc(vsb, ':');
830 48
        s = WS_VSB_finish(vsb, ctx->ws, NULL);
831 48
        return (s);
832 48
}
833
834
/*--------------------------------------------------------------------*/
835
836
VCL_VOID
837 69
VRT_Rollback(VRT_CTX, VCL_HTTP hp)
838
{
839
840 69
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
841 69
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
842 69
        if (ctx->method & VCL_MET_PIPE) {
843 0
                VRT_fail(ctx, "Cannot rollback in vcl_pipe {}");
844 0
                return;
845
        }
846 69
        if (hp == ctx->http_req) {
847 42
                CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
848 42
                Req_Rollback(ctx);
849 42
                if (ctx->method & VCL_MET_DELIVER)
850 27
                        XXXAZ(Resp_Setup_Deliver(ctx->req));
851 42
                if (ctx->method & VCL_MET_SYNTH)
852 6
                        Resp_Setup_Synth(ctx->req);
853 69
        } else if (hp == ctx->http_bereq) {
854 27
                Bereq_Rollback(ctx);
855 27
        } else
856 0
                WRONG("VRT_Rollback 'hp' invalid");
857 69
}
858
859
/*--------------------------------------------------------------------*/
860
861
VCL_VOID
862 15
VRT_synth_strands(VRT_CTX, VCL_STRANDS s)
863
{
864
        struct vsb *vsb;
865
        int i;
866
867 15
        CAST_OBJ_NOTNULL(vsb, ctx->specific, VSB_MAGIC);
868 15
        AN(s);
869 30
        for (i = 0; i < s->n; i++) {
870 15
                if (s->p[i] != NULL)
871 12
                        VSB_cat(vsb, s->p[i]);
872
                else
873 3
                        VSB_cat(vsb, "(null)");
874 15
        }
875 15
}
876
877
VCL_VOID
878 0
VRT_synth_blob(VRT_CTX, VCL_BLOB b)
879
{
880
        struct vsb *vsb;
881 0
        CAST_OBJ_NOTNULL(vsb, ctx->specific, VSB_MAGIC);
882
883 0
        if (b->len > 0 && b->blob != NULL)
884 0
                VSB_bcat(vsb, b->blob, b->len);
885 0
}
886
887
VCL_VOID
888 0
VRT_synth_page(VRT_CTX, VCL_STRANDS s)
889
{
890 0
        VRT_synth_strands(ctx, s);
891 0
}
892
893
/*--------------------------------------------------------------------*/
894
895
static VCL_STRING
896 63
vrt_ban_error(VRT_CTX, VCL_STRING err)
897
{
898 63
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
899 63
        AN(ctx->vsl);
900 63
        AN(err);
901
902 63
        VSLb(ctx->vsl, SLT_VCL_Error, "ban(): %s", err);
903 63
        return (err);
904
}
905
906
VCL_STRING
907 117
VRT_ban_string(VRT_CTX, VCL_STRING str)
908
{
909
        char *a1, *a2, *a3;
910
        char **av;
911
        struct ban_proto *bp;
912 117
        const char *err = NULL, *berr = NULL;
913
        int i;
914
915 117
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
916
917 117
        if (str == NULL)
918 6
                return (vrt_ban_error(ctx, "Null argument"));
919
920 111
        bp = BAN_Build();
921 111
        if (bp == NULL)
922 0
                return (vrt_ban_error(ctx, "Out of Memory"));
923
924 111
        av = VAV_Parse(str, NULL, ARGV_NOESC);
925 111
        AN(av);
926 111
        if (av[0] != NULL) {
927 0
                err = av[0];
928 0
                VAV_Free(av);
929 0
                BAN_Abandon(bp);
930 0
                return (vrt_ban_error(ctx, err));
931
        }
932 126
        for (i = 0; ;) {
933 126
                a1 = av[++i];
934 126
                if (a1 == NULL) {
935 6
                        err = "No ban conditions found.";
936 6
                        break;
937
                }
938 120
                a2 = av[++i];
939 120
                if (a2 == NULL) {
940 6
                        err = "Expected comparison operator.";
941 6
                        break;
942
                }
943 114
                a3 = av[++i];
944 114
                if (a3 == NULL) {
945 6
                        err = "Expected second operand.";
946 6
                        break;
947
                }
948 108
                berr = BAN_AddTest(bp, a1, a2, a3);
949 108
                if (berr != NULL)
950 33
                        break;
951
952 75
                if (av[++i] == NULL) {
953 54
                        berr = BAN_Commit(bp);
954 54
                        if (berr == NULL)
955 54
                                bp = NULL;
956 54
                        break;
957
                }
958 21
                if (strcmp(av[i], "&&")) {
959 12
                        err = WS_Printf(ctx->ws, "Expected && between "
960 6
                            "conditions, found \"%s\"", av[i]);
961 6
                        if (err == NULL)
962 0
                                err = "Expected && between conditions "
963
                                    "(workspace overflow)";
964 6
                        break;
965
                }
966
        }
967 111
        if (berr != NULL) {
968 33
                AZ(err);
969 33
                err = WS_Copy(ctx->ws, berr, -1);
970 33
                if (err == NULL)
971 0
                        err = "Unknown error (workspace overflow)";
972 33
                berr = NULL;
973 33
        }
974 111
        AZ(berr);
975 111
        if (bp != NULL)
976 57
                BAN_Abandon(bp);
977 111
        VAV_Free(av);
978 111
        if (err == NULL)
979 54
                return (NULL);
980 57
        return (vrt_ban_error(ctx, err));
981 117
}
982
983
VCL_BYTES
984 99
VRT_CacheReqBody(VRT_CTX, VCL_BYTES maxsize)
985
{
986 99
        const char * const err = "req.body can only be cached in vcl_recv{}";
987
988 99
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
989 99
        if (ctx->method != VCL_MET_RECV) {
990 0
                if (ctx->vsl != NULL) {
991 0
                        VSLbs(ctx->vsl, SLT_VCL_Error, TOSTRAND(err));
992 0
                } else {
993 0
                        AN(ctx->msg);
994 0
                        VSB_printf(ctx->msg, "%s\n", err);
995
                };
996 0
                return (-1);
997
        }
998 99
        CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
999 99
        return (VRB_Cache(ctx->req, maxsize));
1000 99
}
1001
1002
/*--------------------------------------------------------------------
1003
 * purges
1004
 */
1005
1006
VCL_INT
1007 42
VRT_purge(VRT_CTX, VCL_DURATION ttl, VCL_DURATION grace, VCL_DURATION keep)
1008
{
1009
1010 42
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1011
1012 42
        if ((ctx->method & (VCL_MET_HIT|VCL_MET_MISS)) == 0) {
1013 0
                VRT_fail(ctx,
1014
                    "purge can only happen in vcl_hit{} or vcl_miss{}");
1015 0
                return (0);
1016
        }
1017
1018 42
        CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
1019 42
        CHECK_OBJ_NOTNULL(ctx->req->wrk, WORKER_MAGIC);
1020 84
        return (HSH_Purge(ctx->req->wrk, ctx->req->objcore->objhead,
1021 42
            ctx->req->t_req, ttl, grace, keep));
1022 42
}
1023
1024
/*--------------------------------------------------------------------
1025
 */
1026
1027
struct vsmw_cluster * v_matchproto_()
1028 3609
VRT_VSM_Cluster_New(VRT_CTX, size_t sz)
1029
{
1030
        struct vsmw_cluster *vc;
1031
1032 3609
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1033 3609
        assert(sz > 0);
1034 3609
        AN(vsc_lock);
1035 3609
        AN(vsc_unlock);
1036 3609
        AN(heritage.proc_vsmw);
1037 3609
        vsc_lock();
1038 3609
        vc = VSMW_NewCluster(heritage.proc_vsmw, sz, "VSC_cluster");
1039 3609
        vsc_unlock();
1040 3609
        return (vc);
1041
}
1042
1043
void v_matchproto_()
1044 233
VRT_VSM_Cluster_Destroy(VRT_CTX, struct vsmw_cluster **vsmcp)
1045
{
1046
1047 233
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1048 233
        AN(vsmcp);
1049 233
        VSMW_DestroyCluster(heritage.proc_vsmw, vsmcp);
1050 233
}
1051
1052
/*--------------------------------------------------------------------
1053
 * Simple stuff
1054
 */
1055
1056
int
1057 39240
VRT_strcmp(const char *s1, const char *s2)
1058
{
1059 39240
        if (s1 == NULL || s2 == NULL)
1060 3504
                return (1);
1061 35736
        return (strcmp(s1, s2));
1062 39240
}
1063
1064
void
1065 0
VRT_memmove(void *dst, const void *src, unsigned len)
1066
{
1067
1068 0
        (void)memmove(dst, src, len);
1069 0
}
1070
1071
VCL_BOOL
1072 69
VRT_ipcmp(VRT_CTX, VCL_IP sua1, VCL_IP sua2)
1073
{
1074
1075 69
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1076
1077 69
        if (sua1 == NULL || sua2 == NULL) {
1078 0
                VRT_fail(ctx, "%s: Illegal IP", __func__);
1079 0
                return (1);
1080
        }
1081 69
        return (VSA_Compare_IP(sua1, sua2));
1082 69
}
1083
1084
/*
1085
 * the pointer passed as src must have at least VCL_TASK lifetime
1086
 */
1087
VCL_BLOB
1088 738
VRT_blob(VRT_CTX, const char *err, const void *src, size_t len, unsigned type)
1089
{
1090
        struct vrt_blob *p;
1091
1092 738
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1093 738
        CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
1094
1095 738
        if (src == NULL || len == 0)
1096 6
                return (vrt_null_blob);
1097
1098 732
        p = (void *)WS_Alloc(ctx->ws, sizeof *p);
1099 732
        if (p == NULL) {
1100 18
                VRT_fail(ctx, "Workspace overflow (%s)", err);
1101 18
                return (NULL);
1102
        }
1103
1104 714
        p->type = type;
1105 714
        p->len = len;
1106 714
        p->blob = src;
1107
1108 714
        return (p);
1109 738
}
1110
1111
int
1112 289737
VRT_VSA_GetPtr(VRT_CTX, const struct suckaddr *sua, const unsigned char ** dst)
1113
{
1114
1115 289737
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1116 289737
        AN(dst);
1117
1118 289737
        if (sua == NULL) {
1119 0
                VRT_fail(ctx, "%s: Illegal IP", __func__);
1120 0
                *dst = NULL;
1121 0
                return (-1);
1122
        }
1123 289737
        return (VSA_GetPtr(sua, dst));
1124 289737
}
1125
1126
void
1127 6
VRT_Format_Proxy(struct vsb *vsb, VCL_INT version, VCL_IP sac, VCL_IP sas,
1128
    VCL_STRING auth)
1129
{
1130 6
        VPX_Format_Proxy(vsb, (int)version, sac, sas, auth);
1131 6
}
1132
1133
/*
1134
 * Clone a struct vrt_endpoint in a single malloc() allocation
1135
 */
1136
1137
//lint -e{662}  Possible of out-of-bounds pointer (___ beyond end of data)
1138
//lint -e{826}  Suspicious pointer-to-pointer conversion (area to o small
1139
struct vrt_endpoint *
1140 7902
VRT_Endpoint_Clone(const struct vrt_endpoint * const vep)
1141
{
1142
        size_t sz;
1143
        struct vrt_endpoint *nvep;
1144 7902
        struct vrt_blob *blob = NULL;
1145
        struct suckaddr *sa;
1146 7902
        size_t uds_len = 0;
1147
        char *p, *e;
1148
1149 7902
        CHECK_OBJ_NOTNULL(vep, VRT_ENDPOINT_MAGIC);
1150 7902
        sz = sizeof *nvep;
1151 7902
        if (vep->ipv4)
1152 7605
                sz += vsa_suckaddr_len;
1153 7902
        if (vep->ipv6)
1154 24
                sz += vsa_suckaddr_len;
1155 7902
        if (vep->uds_path != NULL) {
1156 279
                uds_len = strlen(vep->uds_path) + 1;
1157 279
                sz += uds_len;
1158 279
        }
1159 7902
        if (vep->preamble != NULL && vep->preamble->len) {
1160 54
                sz += sizeof(*blob);
1161 54
                sz += vep->preamble->len;
1162 54
        }
1163 7902
        p = calloc(1, sz);
1164 7902
        AN(p);
1165 7902
        e = p + sz;
1166 7902
        nvep = (void*)p;
1167 7902
        p += sizeof *nvep;
1168 7902
        INIT_OBJ(nvep, VRT_ENDPOINT_MAGIC);
1169 7902
        if (vep->ipv4) {
1170 7605
                sa = (void*)p;
1171 7605
                memcpy(sa, vep->ipv4, vsa_suckaddr_len);
1172 7605
                nvep->ipv4 = sa;
1173 7605
                p += vsa_suckaddr_len;
1174 7605
        }
1175 7902
        if (vep->ipv6) {
1176 24
                sa = (void*)p;
1177 24
                memcpy(sa, vep->ipv6, vsa_suckaddr_len);
1178 24
                nvep->ipv6 = sa;
1179 24
                p += vsa_suckaddr_len;
1180 24
        }
1181 7902
        if (vep->preamble != NULL && vep->preamble->len) {
1182
                /* Before uds because we need p to be aligned still */
1183 54
                blob = (void*)p;
1184 54
                p += sizeof(*blob);
1185 54
                nvep->preamble = blob;
1186 54
                memcpy(p, vep->preamble->blob, vep->preamble->len);
1187 54
                blob->len = vep->preamble->len;
1188 54
                blob->blob = p;
1189 54
                p += vep->preamble->len;
1190 54
        }
1191 7902
        if (uds_len) {
1192 279
                memcpy(p, vep->uds_path, uds_len);
1193 279
                nvep->uds_path = p;
1194 279
                p += uds_len;
1195 279
        }
1196 7902
        assert(p == e);
1197 7902
        return (nvep);
1198
}