varnish-cache/vmod/vmod_debug_filters.c
0
/*-
1
 * Copyright (c) 2012-2019 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <stdlib.h>
33
#include <stdio.h>
34
#include <string.h>
35
#include <sys/socket.h>
36
#include <unistd.h>
37
38
#include "cache/cache_varnishd.h"
39
#include "cache/cache_filter.h"
40
41
#include "vgz.h"
42
#include "vsha256.h"
43
#include "vtim.h"
44
#include "vcc_debug_if.h"
45
46
#include "vmod_debug.h"
47
48
/**********************************************************************/
49
50
static enum vfp_status v_matchproto_(vfp_pull_f)
51 360
xyzzy_vfp_rot13_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
52
    ssize_t *lp)
53
{
54
        enum vfp_status vp;
55
        char *q;
56
        ssize_t l;
57
58 360
        (void)vfe;
59 360
        vp = VFP_Suck(vc, p, lp);
60 360
        if (vp == VFP_ERROR)
61 0
                return (vp);
62 360
        q = p;
63 10440
        for (l = 0; l < *lp; l++, q++) {
64 10080
                if (*q >= 'A' && *q <= 'Z')
65 1440
                        *q = (((*q - 'A') + 13) % 26) + 'A';
66 10080
                if (*q >= 'a' && *q <= 'z')
67 6840
                        *q = (((*q - 'a') + 13) % 26) + 'a';
68 10080
        }
69 360
        return (vp);
70 360
}
71
72
static const struct vfp xyzzy_vfp_rot13 = {
73
        .name = "rot13",
74
        .pull = xyzzy_vfp_rot13_pull,
75
};
76
77
/**********************************************************************/
78
79
// deliberately fragmenting the stream to make testing more interesting
80
#define ROT13_BUFSZ 8
81
82
static int v_matchproto_(vdp_init_f)
83 560
xyzzy_vdp_rot13_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
84
{
85
86 560
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
87 560
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
88 560
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
89 560
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
90 560
        AN(vdc->clen);
91
92 560
        AN(priv);
93
94 560
        *priv = malloc(ROT13_BUFSZ);
95 560
        if (*priv == NULL)
96 0
                return (-1);
97
98 560
        return (0);
99 560
}
100
101
static int v_matchproto_(vdp_bytes_f)
102 1080
xyzzy_vdp_rot13_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
103
    const void *ptr, ssize_t len)
104
{
105
        char *q;
106
        const char *pp;
107 1080
        int i, j, retval = 0;
108
109 1080
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
110 1080
        AN(priv);
111 1080
        AN(*priv);
112 1080
        if (len <= 0)
113 240
                return (VDP_bytes(vdc, act, ptr, len));
114 840
        AN(ptr);
115 840
        if (act != VDP_END)
116 400
                act = VDP_FLUSH;
117 840
        q = *priv;
118 840
        pp = ptr;
119
120 22120
        for (i = 0, j = 0; j < len; i++, j++) {
121 21280
                if (pp[j] >= 'A' && pp[j] <= 'Z')
122 1840
                        q[i] = (((pp[j] - 'A') + 13) % 26) + 'A';
123 19440
                else if (pp[j] >= 'a' && pp[j] <= 'z')
124 13320
                        q[i] = (((pp[j] - 'a') + 13) % 26) + 'a';
125
                else
126 6120
                        q[i] = pp[j];
127 21280
                if (i == ROT13_BUFSZ - 1 && j < len - 1) {
128 2200
                        retval = VDP_bytes(vdc, VDP_FLUSH, q, ROT13_BUFSZ);
129 2200
                        if (retval != 0)
130 0
                                return (retval);
131 2200
                        i = -1;
132 2200
                }
133 21280
        }
134 840
        if (i >= 0)
135 840
                retval = VDP_bytes(vdc, act, q, i);
136 840
        return (retval);
137 1080
}
138
139
static int v_matchproto_(vdp_fini_f)
140 560
xyzzy_vdp_rot13_fini(struct vdp_ctx *vdc, void **priv)
141
{
142 560
        (void)vdc;
143 560
        AN(priv);
144 560
        free(*priv);
145 560
        *priv = NULL;
146 560
        return (0);
147
}
148
149
static const struct vdp xyzzy_vdp_rot13 = {
150
        .name  = "rot13",
151
        .init  = xyzzy_vdp_rot13_init,
152
        .bytes = xyzzy_vdp_rot13_bytes,
153
        .fini  = xyzzy_vdp_rot13_fini,
154
};
155
156
VCL_VOID v_matchproto_(td_debug_rot104)
157 40
xyzzy_rot104(VRT_CTX)
158
{
159
160 40
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
161
        // This should fail
162 40
        AN(VRT_AddFilter(ctx, &xyzzy_vfp_rot13, &xyzzy_vdp_rot13));
163 40
}
164
165
/**********************************************************************
166
 * vdp debug_chunked: force http1 chunked encoding by removing the
167
 * Content-Length header
168
 *
169
 * this happens in a VDP because cnt_transmit() runs after VCL and
170
 * restores it
171
 */
172
173
static int v_matchproto_(vdp_init_f)
174 720
xyzzy_vdp_chunked_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
175
{
176
177 720
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
178 720
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
179 720
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
180 720
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
181 720
        AN(vdc->clen);
182 720
        AN(priv);
183
184 720
        http_Unset(vdc->hp, H_Content_Length);
185 720
        *vdc->clen = -1;
186
187 720
        return (1);
188
}
189
190
static const struct vdp xyzzy_vdp_chunked = {
191
        .name  = "debug.chunked",
192
        .init  = xyzzy_vdp_chunked_init,
193
};
194
195
/**********************************************************************
196
 * pedantic tests of the VDP API:
197
 * - assert that we see a VDP_END
198
 * - assert that _fini gets called before the task ends
199
 *
200
 * note:
201
 * we could lookup our own vdpe in _fini and check for vdpe->end == VDP_END
202
 * yet that would cross the API
203
 */
204
205
enum vdp_state_e {
206
        VDPS_NULL = 0,
207
        VDPS_INIT,      // _init called
208
        VDPS_BYTES,     // _bytes called act != VDP_END
209
        VDPS_END,       // _bytes called act == VDP_END
210
        VDPS_FINI       // _fini called
211
};
212
213
struct vdp_state_s {
214
        unsigned                magic;
215
#define VDP_STATE_MAGIC 0x57c8d309
216
        enum vdp_state_e        state;
217
};
218
219
static void v_matchproto_(vmod_priv_fini_f)
220 7560
priv_pedantic_fini(VRT_CTX, void *priv)
221
{
222
        struct vdp_state_s *vdps;
223
224 7560
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
225 7560
        CAST_OBJ_NOTNULL(vdps, priv, VDP_STATE_MAGIC);
226
227 7560
        assert(vdps->state == VDPS_FINI);
228 7560
}
229
230
static const struct vmod_priv_methods priv_pedantic_methods[1] = {{
231
        .magic = VMOD_PRIV_METHODS_MAGIC,
232
        .type = "debug_vdp_pedantic",
233
        .fini = priv_pedantic_fini
234
}};
235
236
static int v_matchproto_(vdp_init_f)
237 7880
xyzzy_pedantic_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
238
{
239
        struct vdp_state_s *vdps;
240
        struct vmod_priv *p;
241
242 7880
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
243 7880
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
244 7880
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
245 7880
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
246 7880
        AN(vdc->clen);
247 7880
        AN(priv);
248
249 15440
        WS_TASK_ALLOC_OBJ(ctx, vdps, VDP_STATE_MAGIC);
250 7880
        if (vdps == NULL)
251 320
                return (-1);
252 7560
        assert(vdps->state == VDPS_NULL);
253
254 7560
        p = VRT_priv_task(ctx, (void *)vdc);
255 7560
        if (p == NULL)
256 0
                return (-1);
257 7560
        p->priv = vdps;
258 7560
        p->methods = priv_pedantic_methods;
259
260 7560
        *priv = vdps;
261
262 7560
        vdps->state = VDPS_INIT;
263
264 7560
        return (0);
265 7880
}
266
267
static int v_matchproto_(vdp_bytes_f)
268 9398
xyzzy_pedantic_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
269
    const void *ptr, ssize_t len)
270
{
271
        struct vdp_state_s *vdps;
272
273 9398
        CAST_OBJ_NOTNULL(vdps, *priv, VDP_STATE_MAGIC);
274 9398
        assert(vdps->state >= VDPS_INIT);
275 9398
        assert(vdps->state < VDPS_END);
276
277 9398
        if (act == VDP_END)
278 2440
                vdps->state = VDPS_END;
279
        else
280 6958
                vdps->state = VDPS_BYTES;
281
282 9398
        return (VDP_bytes(vdc, act, ptr, len));
283
}
284
285
static int v_matchproto_(vdp_fini_f)
286 7879
xyzzy_pedantic_fini(struct vdp_ctx *vdc, void **priv)
287
{
288
        struct vdp_state_s *vdps;
289
290 7879
        (void) vdc;
291 7879
        AN(priv);
292 7879
        if (*priv == NULL)
293 320
                return (0);
294 7559
        TAKE_OBJ_NOTNULL(vdps, priv, VDP_STATE_MAGIC);
295 7559
        assert(vdps->state == VDPS_INIT || vdps->state == VDPS_END);
296 7559
        vdps->state = VDPS_FINI;
297
298 7559
        return (0);
299 7879
}
300
301
static const struct vdp xyzzy_vdp_pedantic = {
302
        .name  = "debug.pedantic",
303
        .init  = xyzzy_pedantic_init,
304
        .bytes = xyzzy_pedantic_bytes,
305
        .fini  = xyzzy_pedantic_fini,
306
};
307
308
/**********************************************************************
309
 *
310
 * this trivial copy/paste/edit filter (of rot13) was specifically made for
311
 * someone who added a DBG_SLOW_BEREQ debug flag. It should actually be turned
312
 * in a proper "bandwidth control" filter, but that exceeds an evening's work,
313
 * so it's kept for later
314
 */
315
316
static enum vfp_status v_matchproto_(vfp_pull_f)
317 0
xyzzy_vfp_slow_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
318
    ssize_t *lp)
319
{
320
321 0
        (void)vfe;
322 0
        VTIM_sleep(1.0);
323 0
        return (VFP_Suck(vc, p, lp));
324
}
325
326
static const struct vfp xyzzy_vfp_slow = {
327
        .name = "debug.slow",
328
        .pull = xyzzy_vfp_slow_pull,
329
};
330
331
/**********************************************************************/
332
333
static int v_matchproto_(vdp_bytes_f)
334 160
xyzzy_vdp_slow_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
335
    const void *ptr, ssize_t len)
336
{
337
338 160
        (void)priv;
339 160
        VTIM_sleep(1.0);
340 160
        return (VDP_bytes(vdc, act, ptr, len));
341
}
342
343
static const struct vdp xyzzy_vdp_slow = {
344
        .name  = "debug.slow",
345
        .bytes = xyzzy_vdp_slow_bytes
346
};
347
348
/*
349
 * checksum VDP:
350
 * test that the stream of bytes has a certain checksum and either log
351
 * or panic
352
 *
353
 * The sha256 and crc32 variants are basically identical, but the amount of
354
 * code does not justify generalizing. (slink)
355
 */
356
357
enum vdp_chk_mode_e {
358
        //lint -esym(749, vdp_chk_mode_e::VDP_CHK_INVAL) deliberately not referenced
359
        VDP_CHK_INVAL = 0,
360
        VDP_CHK_LOG,
361
        VDP_CHK_PANIC,
362
        VDP_CHK_PANIC_UNLESS_ERROR
363
};
364
365
struct vdp_chksha256_cfg_s {
366
        unsigned                        magic;
367
#define VDP_CHKSHA256_CFG_MAGIC         0x624f5b32
368
        enum vdp_chk_mode_e             mode;
369
        unsigned char                   expected[VSHA256_DIGEST_LENGTH];
370
};
371
372
struct vdp_chkcrc32_cfg_s {
373
        unsigned                        magic;
374
#define VDP_CHKCRC32_CFG_MAGIC          0x5a7a835c
375
        enum vdp_chk_mode_e             mode;
376
        uint32_t                        expected;
377
};
378
379
struct vdp_chksha256_s {
380
        unsigned                        magic;
381
#define VDP_CHKSHA256_MAGIC             0x6856e913
382
        unsigned                        called;
383
        size_t                          bytes;
384
        struct VSHA256Context           cx[1];
385
        struct vdp_chksha256_cfg_s      *cfg;
386
};
387
388
struct vdp_chkcrc32_s {
389
        unsigned                        magic;
390
#define VDP_CHKCRC32_MAGIC              0x15c03d3c
391
        unsigned                        called;
392
        size_t                          bytes;
393
        uint32_t                        crc;
394
        struct vdp_chkcrc32_cfg_s       *cfg;
395
};
396
397
static const void * const chksha256_priv_id = &chksha256_priv_id;
398
static const void * const chkcrc32_priv_id = &chkcrc32_priv_id;
399
400
static int v_matchproto_(vdp_init_f)
401 0
xyzzy_chksha256_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
402
{
403
        struct vdp_chksha256_s *vdps;
404
        struct vmod_priv *p;
405
406 0
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
407 0
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
408 0
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
409 0
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
410 0
        AN(vdc->clen);
411 0
        AN(priv);
412
413 0
        WS_TASK_ALLOC_OBJ(ctx, vdps, VDP_CHKSHA256_MAGIC);
414 0
        if (vdps == NULL)
415 0
                return (-1);
416 0
        VSHA256_Init(vdps->cx);
417
418 0
        p = VRT_priv_task_get(ctx, chksha256_priv_id);
419 0
        if (p == NULL)
420 0
                return (-1);
421
422 0
        assert(p->len == sizeof(struct vdp_chksha256_cfg_s));
423 0
        CAST_OBJ_NOTNULL(vdps->cfg, p->priv, VDP_CHKSHA256_CFG_MAGIC);
424 0
        *priv = vdps;
425
426 0
        return (0);
427 0
}
428
429
static int v_matchproto_(vdp_init_f)
430 0
xyzzy_chkcrc32_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
431
{
432
        struct vdp_chkcrc32_s *vdps;
433
        struct vmod_priv *p;
434
435 0
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
436 0
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
437 0
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
438 0
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
439 0
        AN(vdc->clen);
440 0
        AN(priv);
441
442 0
        WS_TASK_ALLOC_OBJ(ctx, vdps, VDP_CHKCRC32_MAGIC);
443 0
        if (vdps == NULL)
444 0
                return (-1);
445 0
        vdps->crc = crc32(0L, Z_NULL, 0);
446
447 0
        p = VRT_priv_task_get(ctx, chkcrc32_priv_id);
448 0
        if (p == NULL)
449 0
                return (-1);
450
451 0
        assert(p->len == sizeof(struct vdp_chkcrc32_cfg_s));
452 0
        CAST_OBJ_NOTNULL(vdps->cfg, p->priv, VDP_CHKCRC32_CFG_MAGIC);
453 0
        *priv = vdps;
454
455 0
        return (0);
456 0
}
457
458
static int v_matchproto_(vdp_bytes_f)
459 0
xyzzy_chksha256_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
460
    const void *ptr, ssize_t len)
461
{
462
        struct vdp_chksha256_s *vdps;
463
464 0
        CAST_OBJ_NOTNULL(vdps, *priv, VDP_CHKSHA256_MAGIC);
465 0
        if (len != 0)
466 0
                VSHA256_Update(vdps->cx, ptr, len);
467 0
        vdps->called++;
468 0
        vdps->bytes += len;
469 0
        return (VDP_bytes(vdc, act, ptr, len));
470
}
471
472
static int v_matchproto_(vdp_bytes_f)
473 0
xyzzy_chkcrc32_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
474
    const void *ptr, ssize_t len)
475
{
476
        struct vdp_chkcrc32_s *vdps;
477
478 0
        CAST_OBJ_NOTNULL(vdps, *priv, VDP_CHKCRC32_MAGIC);
479 0
        if (len > 0)
480 0
                vdps->crc = crc32(vdps->crc, ptr, len);
481 0
        vdps->called++;
482 0
        vdps->bytes += len;
483 0
        return (VDP_bytes(vdc, act, ptr, len));
484
}
485
486
static int v_matchproto_(vdp_fini_f)
487 0
xyzzy_chksha256_fini(struct vdp_ctx *vdc, void **priv)
488
{
489
        unsigned char digest[VSHA256_DIGEST_LENGTH];
490
        enum vdp_chk_mode_e mode;
491
        struct vdp_chksha256_s *vdps;
492
        struct vsb *vsb;
493
        int r;
494
495 0
        (void) vdc;
496 0
        AN(priv);
497 0
        if (*priv == NULL)
498 0
                return (0);
499 0
        TAKE_OBJ_NOTNULL(vdps, priv, VDP_CHKSHA256_MAGIC);
500
501 0
        VSHA256_Final(digest, vdps->cx);
502 0
        r = memcmp(digest, vdps->cfg->expected, sizeof digest);
503 0
        if (r == 0)
504 0
                return (0);
505
506 0
        mode = vdps->cfg->mode;
507 0
        if (mode == VDP_CHK_PANIC_UNLESS_ERROR)
508 0
                mode = (vdps->called == 0 || vdc->retval != 0) ? VDP_CHK_LOG : VDP_CHK_PANIC;
509
510 0
        if (mode == VDP_CHK_LOG) {
511 0
                VSLb(vdc->vsl, SLT_Debug, "sha256 checksum mismatch");
512
513 0
                vsb = VSB_new_auto();
514 0
                AN(vsb);
515 0
                VSB_quote(vsb, digest, sizeof digest, VSB_QUOTE_HEX);
516 0
                AZ(VSB_finish(vsb));
517 0
                VSLb(vdc->vsl, SLT_Debug, "got: %s", VSB_data(vsb));
518
519 0
                VSB_clear(vsb);
520 0
                VSB_quote(vsb, vdps->cfg->expected, sizeof digest, VSB_QUOTE_HEX);
521 0
                AZ(VSB_finish(vsb));
522 0
                VSLb(vdc->vsl, SLT_Debug, "exp: %s", VSB_data(vsb));
523 0
                VSB_destroy(&vsb);
524 0
        }
525 0
        else if (mode == VDP_CHK_PANIC)
526 0
                WRONG("body checksum");
527
        else
528 0
                WRONG("mode");
529
530 0
        return (0);
531 0
}
532
533
static int v_matchproto_(vdp_fini_f)
534 0
xyzzy_chkcrc32_fini(struct vdp_ctx *vdc, void **priv)
535
{
536
        enum vdp_chk_mode_e mode;
537
        struct vdp_chkcrc32_s *vdps;
538
539 0
        (void) vdc;
540 0
        AN(priv);
541 0
        if (*priv == NULL)
542 0
                return (0);
543 0
        TAKE_OBJ_NOTNULL(vdps, priv, VDP_CHKCRC32_MAGIC);
544
545 0
        if (vdps->crc == vdps->cfg->expected)
546 0
                return (0);
547
548 0
        mode = vdps->cfg->mode;
549 0
        if (mode == VDP_CHK_PANIC_UNLESS_ERROR)
550 0
                mode = (vdps->called == 0 || vdc->retval != 0) ? VDP_CHK_LOG : VDP_CHK_PANIC;
551
552 0
        if (mode == VDP_CHK_LOG) {
553 0
                VSLb(vdc->vsl, SLT_Debug, "crc32 checksum mismatch");
554 0
                VSLb(vdc->vsl, SLT_Debug, "got: %08x", vdps->crc);
555 0
                VSLb(vdc->vsl, SLT_Debug, "exp: %08x", vdps->cfg->expected);
556 0
        }
557 0
        else if (mode == VDP_CHK_PANIC)
558 0
                WRONG("body checksum");
559
        else
560 0
                WRONG("mode");
561
562 0
        return (0);
563 0
}
564
565
static const struct vdp xyzzy_vdp_chksha256 = {
566
        .name  = "debug.chksha256",
567
        .init  = xyzzy_chksha256_init,
568
        .bytes = xyzzy_chksha256_bytes,
569
        .fini  = xyzzy_chksha256_fini,
570
};
571
572
static const struct vdp xyzzy_vdp_chkcrc32 = {
573
        .name  = "debug.chkcrc32",
574
        .init  = xyzzy_chkcrc32_init,
575
        .bytes = xyzzy_chkcrc32_bytes,
576
        .fini  = xyzzy_chkcrc32_fini,
577
};
578
579
#define chkcfg(ws, cfg, magic, id, mode_e) do {                                                 \
580
        struct vmod_priv *p = VRT_priv_task(ctx, id);                                           \
581
                                                                                                \
582
        XXXAN(p);                                                                               \
583
        if (p->priv == NULL) {                                                                  \
584
                p->priv = WS_Alloc(ws, sizeof *cfg);                                            \
585
                p->len = sizeof *cfg;                                                           \
586
        }                                                                                       \
587
        AN(p->priv);                                                                            \
588
        cfg = p->priv;                                                                          \
589
        INIT_OBJ(cfg, magic);                                                                   \
590
        if (mode_e == VENUM(log))                                                               \
591
                cfg->mode = VDP_CHK_LOG;                                                        \
592
        else if (mode_e == VENUM(panic))                                                        \
593
                cfg->mode = VDP_CHK_PANIC;                                                      \
594
        else if (mode_e == VENUM(panic_unless_error))                                           \
595
                cfg->mode = VDP_CHK_PANIC_UNLESS_ERROR;                                         \
596
        else                                                                                    \
597
                WRONG("mode");                                                                  \
598
} while(0)
599
600
VCL_VOID v_matchproto_(td_xyzzy_debug_chksha256)
601 0
xyzzy_chksha256(VRT_CTX, VCL_BLOB blob, VCL_ENUM mode_e)
602
{
603
        struct vdp_chksha256_cfg_s *cfg;
604
        size_t l;
605
606 0
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
607 0
        AN(blob);
608 0
        XXXAN(blob->blob);
609 0
        XXXAN(blob->len);
610
611 0
        chkcfg(ctx->ws, cfg, VDP_CHKSHA256_CFG_MAGIC, chksha256_priv_id, mode_e);
612
613 0
        l = blob->len;
614 0
        if (l > sizeof cfg->expected)
615 0
                l = sizeof cfg->expected;
616 0
        memcpy(cfg->expected, blob->blob, l);
617
618 0
}
619
620
VCL_VOID v_matchproto_(td_xyzzy_debug_chkcrc32)
621 0
xyzzy_chkcrc32(VRT_CTX, VCL_INT expected, VCL_ENUM mode_e)
622
{
623
        struct vdp_chkcrc32_cfg_s *cfg;
624
625 0
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
626
627 0
        chkcfg(ctx->ws, cfg, VDP_CHKCRC32_CFG_MAGIC, chkcrc32_priv_id, mode_e);
628
629 0
        if (expected < 0)
630 0
                expected = 0;
631 0
        cfg->expected = (uintmax_t)expected % UINT32_MAX;
632 0
}
633
634
/**********************************************************************
635
 * reserve thread_workspace
636
 */
637
638
static int v_matchproto_(vdp_init_f)
639 40
xyzzy_awshog_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
640
{
641
        struct ws *aws;
642
        unsigned u;
643
644 40
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
645 40
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
646 40
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
647 40
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
648 40
        AN(vdc->clen);
649 40
        AN(priv);
650
651 40
        if (ctx->req != NULL)
652 0
                aws = ctx->req->wrk->aws;
653 40
        else if (ctx->bo != NULL)
654 40
                aws = ctx->bo->wrk->aws;
655
        else
656 0
                WRONG("neither req nor bo");
657
658 40
        u = WS_ReserveAll(aws);
659 40
        WS_Release(aws, 0);
660 40
        (void) WS_Alloc(aws, u);
661 40
        return (1);
662
}
663
664
static const struct vdp xyzzy_vdp_awshog = {
665
        .name  = "debug.awshog",
666
        .init  = xyzzy_awshog_init
667
};
668
669
void
670 3920
debug_add_filters(VRT_CTX)
671
{
672 3920
        AZ(VRT_AddFilter(ctx, &xyzzy_vfp_rot13, &xyzzy_vdp_rot13));
673 3920
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_pedantic));
674 3920
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_chunked));
675 3920
        AZ(VRT_AddFilter(ctx, &xyzzy_vfp_slow, &xyzzy_vdp_slow));
676 3920
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_chksha256));
677 3920
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_chkcrc32));
678 3920
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_awshog));
679 3920
}
680
681
void
682 920
debug_remove_filters(VRT_CTX)
683
{
684 920
        VRT_RemoveFilter(ctx, &xyzzy_vfp_slow, &xyzzy_vdp_slow);
685 920
        VRT_RemoveFilter(ctx, &xyzzy_vfp_rot13, &xyzzy_vdp_rot13);
686 920
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_pedantic);
687 920
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_chunked);
688 920
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_chksha256);
689 920
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_chkcrc32);
690 920
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_awshog);
691 920
}