File: vmod_cookie.c

package info (click to toggle)
varnish 7.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,256 kB
  • sloc: ansic: 104,222; python: 2,679; makefile: 1,303; sh: 1,077; awk: 114; perl: 105; ruby: 41
file content (456 lines) | stat: -rw-r--r-- 10,707 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*-
 * Copyright (c) 2012-2020 Varnish Software AS
 *
 * Author: Lasse Karstensen <lasse.karstensen@gmail.com>
 * Author: Lasse Karstensen <lkarsten@varnish-software.com>
 * Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Cookie VMOD that simplifies handling of the Cookie request header.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>

#include <cache/cache.h>

#include <vsb.h>

#include "vcc_cookie_if.h"

enum filter_action {
	blacklist,
	whitelist
};

struct cookie {
	unsigned		magic;
#define VMOD_COOKIE_ENTRY_MAGIC	0x3BB41543
	const char		*name;
	const char		*value;
	VTAILQ_ENTRY(cookie)	list;
};

/* A structure to represent both whitelists and blacklists */
struct matchlist {
	char			*name;
	VTAILQ_ENTRY(matchlist)	list;
};

struct vmod_cookie {
	unsigned		magic;
#define VMOD_COOKIE_MAGIC	0x4EE5FB2E
	VTAILQ_HEAD(, cookie)	cookielist;
};

static void
cobj_free(VRT_CTX, void *p)
{
	struct vmod_cookie *vcp;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	CAST_OBJ_NOTNULL(vcp, p, VMOD_COOKIE_MAGIC);
	FREE_OBJ(vcp);
}

static const struct vmod_priv_methods cookie_cobj_priv_methods[1] = {{
	.magic = VMOD_PRIV_METHODS_MAGIC,
	.type = "vmod_cookie_cobj",
	.fini = cobj_free
}};

static struct vmod_cookie *
cobj_get(struct vmod_priv *priv)
{
	struct vmod_cookie *vcp;

	if (priv->priv == NULL) {
		ALLOC_OBJ(vcp, VMOD_COOKIE_MAGIC);
		AN(vcp);
		VTAILQ_INIT(&vcp->cookielist);
		priv->priv = vcp;
		priv->methods = cookie_cobj_priv_methods;
	} else
		CAST_OBJ_NOTNULL(vcp, priv->priv, VMOD_COOKIE_MAGIC);

	return (vcp);
}

VCL_VOID
vmod_parse(VRT_CTX, struct vmod_priv *priv, VCL_STRING cookieheader)
{
	struct vmod_cookie *vcp = cobj_get(priv);
	char *name, *value;
	const char *p, *sep;
	int i = 0;

	if (cookieheader == NULL || *cookieheader == '\0') {
		VSLb(ctx->vsl, SLT_Debug, "cookie: nothing to parse");
		return;
	}

	/* If called twice during the same request, clean out old state. */
	if (!VTAILQ_EMPTY(&vcp->cookielist))
		vmod_clean(ctx, priv);

	p = cookieheader;
	while (*p != '\0') {
		while (isspace(*p))
			p++;
		sep = strchr(p, '=');
		if (sep == NULL)
			break;
		name = strndup(p, pdiff(p, sep));
		p = sep + 1;

		sep = p;
		while (*sep != '\0' && *sep != ';')
			sep++;
		value = strndup(p, pdiff(p, sep));

		vmod_set(ctx, priv, name, value);
		free(name);
		free(value);
		i++;
		if (*sep == '\0')
			break;
		p = sep + 1;
	}

	VSLb(ctx->vsl, SLT_Debug, "cookie: parsed %i cookies.", i);
}

static struct cookie *
find_cookie(const struct vmod_cookie *vcp, VCL_STRING name)
{
	struct cookie *cookie;

	VTAILQ_FOREACH(cookie, &vcp->cookielist, list) {
		CHECK_OBJ_NOTNULL(cookie, VMOD_COOKIE_ENTRY_MAGIC);
		if (!strcmp(cookie->name, name))
			break;
	}
	return (cookie);
}

VCL_VOID
vmod_set(VRT_CTX, struct vmod_priv *priv, VCL_STRING name, VCL_STRING value)
{
	struct vmod_cookie *vcp = cobj_get(priv);
	struct cookie *cookie;
	const char *p;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);

	/* Empty cookies should be ignored. */
	if (name == NULL || *name == '\0')
		return;
	if (value == NULL || *value == '\0')
		return;

	cookie = find_cookie(vcp, name);
	if (cookie != NULL) {
		p = WS_Printf(ctx->ws, "%s", value);
		if (p == NULL) {
			VSLb(ctx->vsl, SLT_Error,
			    "cookie: Workspace overflow in set()");
		} else
			cookie->value = p;
		return;
	}

	WS_TASK_ALLOC_OBJ(ctx, cookie, VMOD_COOKIE_ENTRY_MAGIC);
	if (cookie == NULL)
		return;

	cookie->name = WS_Printf(ctx->ws, "%s", name);
	cookie->value = WS_Printf(ctx->ws, "%s", value);
	if (cookie->name == NULL || cookie->value == NULL) {
		VSLb(ctx->vsl, SLT_Error,
		    "cookie: unable to get storage for cookie");
		return;
	}
	VTAILQ_INSERT_TAIL(&vcp->cookielist, cookie, list);
}

VCL_BOOL
vmod_isset(VRT_CTX, struct vmod_priv *priv, const char *name)
{
	struct vmod_cookie *vcp = cobj_get(priv);
	struct cookie *cookie;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	if (name == NULL || *name == '\0')
		return (0);

	cookie = find_cookie(vcp, name);
	return (cookie ? 1 : 0);
}

VCL_STRING
vmod_get(VRT_CTX, struct vmod_priv *priv, VCL_STRING name)
{
	struct vmod_cookie *vcp = cobj_get(priv);
	struct cookie *cookie;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	if (name == NULL || *name == '\0')
		return (NULL);

	cookie = find_cookie(vcp, name);
	return (cookie ? cookie->value : NULL);
}


VCL_STRING
vmod_get_re(VRT_CTX, struct vmod_priv *priv, VCL_REGEX re)
{
	struct vmod_cookie *vcp = cobj_get(priv);
	struct cookie *cookie = NULL;
	struct cookie *current;
	unsigned match;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	AN(re);

	VTAILQ_FOREACH(current, &vcp->cookielist, list) {
		CHECK_OBJ_NOTNULL(current, VMOD_COOKIE_ENTRY_MAGIC);
		VSLb(ctx->vsl, SLT_Debug, "cookie: checking %s", current->name);
		match = VRT_re_match(ctx, current->name, re);
		if (!match)
			continue;
		cookie = current;
		break;
	}

	return (cookie ? cookie->value : NULL);
}

VCL_VOID
vmod_delete(VRT_CTX, struct vmod_priv *priv, VCL_STRING name)
{
	struct vmod_cookie *vcp = cobj_get(priv);
	struct cookie *cookie;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	if (name == NULL || *name == '\0')
		return;

	cookie = find_cookie(vcp, name);

	if (cookie != NULL)
		VTAILQ_REMOVE(&vcp->cookielist, cookie, list);
}

VCL_VOID
vmod_clean(VRT_CTX, struct vmod_priv *priv)
{
	struct vmod_cookie *vcp = cobj_get(priv);

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	AN(vcp);
	VTAILQ_INIT(&vcp->cookielist);
}

static void
filter_cookies(struct vmod_priv *priv, VCL_STRING list_s,
    enum filter_action mode)
{
	struct cookie *cookieptr, *safeptr;
	struct vmod_cookie *vcp = cobj_get(priv);
	struct matchlist *mlentry, *mlsafe;
	char const *p = list_s, *q;
	int matched = 0;
	VTAILQ_HEAD(, matchlist) matchlist_head;

	VTAILQ_INIT(&matchlist_head);

	/* Parse the supplied list. */
	while (p && *p != '\0') {
		while (isspace(*p))
			p++;
		if (*p == '\0')
			break;

		q = p;
		while (*q != '\0' && *q != ',')
			q++;

		if (q == p) {
			p++;
			continue;
		}

		/* XXX: can we reserve/release lumps of txt instead of
		 * malloc/free?
		 */
		mlentry = malloc(sizeof *mlentry);
		AN(mlentry);
		mlentry->name = strndup(p, q - p);
		AN(mlentry->name);

		VTAILQ_INSERT_TAIL(&matchlist_head, mlentry, list);

		p = q;
		if (*p != '\0')
			p++;
	}

	/* Filter existing cookies that either aren't in the whitelist or
	 * are in the blacklist (depending on the filter_action) */
	VTAILQ_FOREACH_SAFE(cookieptr, &vcp->cookielist, list, safeptr) {
		CHECK_OBJ_NOTNULL(cookieptr, VMOD_COOKIE_ENTRY_MAGIC);
		matched = 0;

		VTAILQ_FOREACH(mlentry, &matchlist_head, list) {
			if (strcmp(cookieptr->name, mlentry->name) == 0) {
				matched = 1;
				break;
			}
		}
		if (matched != mode)
			VTAILQ_REMOVE(&vcp->cookielist, cookieptr, list);
	}

	VTAILQ_FOREACH_SAFE(mlentry, &matchlist_head, list, mlsafe) {
		VTAILQ_REMOVE(&matchlist_head, mlentry, list);
		free(mlentry->name);
		free(mlentry);
	}
}

VCL_VOID
vmod_keep(VRT_CTX, struct vmod_priv *priv, VCL_STRING whitelist_s)
{

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	filter_cookies(priv, whitelist_s, whitelist);
}


VCL_VOID
vmod_filter(VRT_CTX, struct vmod_priv *priv, VCL_STRING blacklist_s)
{

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	filter_cookies(priv, blacklist_s, blacklist);
}

static VCL_VOID
re_filter(VRT_CTX, struct vmod_priv *priv, VCL_REGEX re, enum filter_action mode)
{
	struct vmod_cookie *vcp = cobj_get(priv);
	struct cookie *current, *safeptr;
	unsigned match;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	AN(re);

	VTAILQ_FOREACH_SAFE(current, &vcp->cookielist, list, safeptr) {
		CHECK_OBJ_NOTNULL(current, VMOD_COOKIE_ENTRY_MAGIC);

		match = VRT_re_match(ctx, current->name, re);

		switch (mode) {
		case blacklist:
			if (!match)
				continue;
			VSLb(ctx->vsl, SLT_Debug,
			    "Removing matching cookie %s (value: %s)",
			    current->name, current->value);
			VTAILQ_REMOVE(&vcp->cookielist, current, list);
			break;
		case whitelist:
			if (match)
				continue;

			VSLb(ctx->vsl, SLT_Debug,
			    "Removing cookie %s (value: %s)",
			    current->name, current->value);
			VTAILQ_REMOVE(&vcp->cookielist, current, list);
			break;
		default:
			WRONG("invalid mode");
		}
	}
}


VCL_VOID
vmod_keep_re(VRT_CTX, struct vmod_priv *priv, VCL_REGEX re)
{

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	re_filter(ctx, priv, re, whitelist);
}


VCL_VOID
vmod_filter_re(VRT_CTX, struct vmod_priv *priv, VCL_REGEX re)
{

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	re_filter(ctx, priv, re, blacklist);
}


VCL_STRING
vmod_get_string(VRT_CTX, struct vmod_priv *priv)
{
	struct cookie *curr;
	struct vsb output[1];
	struct vmod_cookie *vcp = cobj_get(priv);
	const char *sep = "", *res;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
	WS_VSB_new(output, ctx->ws);

	VTAILQ_FOREACH(curr, &vcp->cookielist, list) {
		CHECK_OBJ_NOTNULL(curr, VMOD_COOKIE_ENTRY_MAGIC);
		AN(curr->name);
		AN(curr->value);
		VSB_printf(output, "%s%s=%s", sep, curr->name, curr->value);
		sep = "; ";
	}
	res = WS_VSB_finish(output, ctx->ws, NULL);
	if (res == NULL)
		VSLb(ctx->vsl, SLT_Error, "cookie: Workspace overflow");
	return (res);
}

VCL_STRING
vmod_format_date(VRT_CTX, VCL_TIME ts, VCL_DURATION duration)
{

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	return (VRT_TIME_string(ctx, ts + duration));
}