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