Code Coverage: vinyl-cache/vmod/vmod_std.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2010-2017 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 <sys/stat.h>
33
34
#include <netinet/in.h>
35
36
#include <ctype.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <syslog.h>
40
#include <sys/socket.h>
41
#include <fnmatch.h>
42
43
#include "cache/cache.h"
44
45
#include "vrnd.h"
46
#include "vtcp.h"
47
#include "vsa.h"
48
#include "vtim.h"
49
#include "vcl.h"
50
51
#include "vcc_std_if.h"
52
53
VCL_VOID v_matchproto_(td_std_set_ip_tos)
54
vmod_set_ip_tos(VRT_CTX, VCL_INT tos)
55 2
{
56 2
        struct suckaddr *sa;
57 2
        int fam, itos = tos;
58
59 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
60 2
        AZ(SES_Get_local_addr(ctx->req->sp, &sa));
61
        /* Silently ignore for non-IP addresses. */
62 2
        if (VSA_Compare(sa, bogo_ip) == 0)
63 1
                return;
64 1
        fam = VSA_Get_Proto(sa);
65 1
        switch (fam) {
66 1
        case PF_INET:
67 1
                VTCP_Assert(setsockopt(ctx->req->sp->fd,
68 1
                    IPPROTO_IP, IP_TOS, &itos, sizeof(itos)));
69 1
                break;
70 1
        case PF_INET6:
71 0
                VTCP_Assert(setsockopt(ctx->req->sp->fd,
72 0
                    IPPROTO_IPV6, IPV6_TCLASS, &itos, sizeof(itos)));
73 0
                break;
74 0
        default:
75 0
                INCOMPL();
76 1
        }
77 1
}
78
79
static const char *
80
vmod_updown(VRT_CTX, int up, VCL_STRANDS s)
81 12
{
82 12
        unsigned u;
83 12
        char *b, *e;
84 12
        const char *p;
85 12
        int i;
86
87 12
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
88 12
        CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC);
89 12
        u = WS_ReserveAll(ctx->ws);
90 24
        e = b = WS_Reservation(ctx->ws);
91 0
        e += u;
92 24
        for (i = 0; i < s->n && b < e; i++) {
93 12
                p = s->p[i];
94 2194
                while (p != NULL && *p != '\0' && b < e) {
95 2182
                        if (up)
96 1082
                                *b++ = (char)toupper(*p++);
97 1100
                        else
98 1100
                                *b++ = (char)tolower(*p++);
99 2182
                }
100 12
        }
101 24
        if (b < e)
102 12
                *b = '\0';
103 24
        b++;
104 24
        if (b > e) {
105 0
                WS_MarkOverflow(ctx->ws);
106 0
                WS_Release(ctx->ws, 0);
107 0
                return (NULL);
108 12
        } else {
109 12
                e = b;
110 12
                b = WS_Reservation(ctx->ws);
111 0
                WS_Release(ctx->ws, e - b);
112 12
                return (b);
113 12
        }
114 24
}
115
116
VCL_STRING v_matchproto_(td_std_toupper)
117
vmod_toupper(VRT_CTX, VCL_STRANDS s)
118 4
{
119
120 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
121 4
        return (vmod_updown(ctx, 1, s));
122 4
}
123
124
VCL_STRING v_matchproto_(td_std_tolower)
125
vmod_tolower(VRT_CTX, VCL_STRANDS s)
126 8
{
127
128 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
129 8
        return (vmod_updown(ctx, 0, s));
130 8
}
131
132
VCL_REAL v_matchproto_(td_std_random)
133
vmod_random(VRT_CTX, VCL_REAL lo, VCL_REAL hi)
134 6
{
135 6
        double a;
136
137 6
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
138 6
        a = VRND_RandomTestableDouble();
139 6
        a *= hi - lo;
140 6
        a += lo;
141 6
        return (a);
142 6
}
143
144
VCL_VOID v_matchproto_(td_std_log)
145
vmod_log(VRT_CTX, VCL_STRANDS s)
146 239
{
147 239
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
148
149 239
        if (ctx->vsl != NULL)
150 206
                VSLbs(ctx->vsl, SLT_VCL_Log, s);
151 33
        else
152 33
                VSLs(SLT_VCL_Log, NO_VXID, s);
153 239
}
154
155
/* XXX use vsyslog() ? */
156
VCL_VOID v_matchproto_(td_std_syslog)
157
vmod_syslog(VRT_CTX, VCL_INT fac, VCL_STRANDS s)
158 5
{
159 5
        const char *p;
160 5
        uintptr_t sn;
161
162 5
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
163 5
        sn = WS_Snapshot(ctx->ws);
164 5
        p = VRT_StrandsWS(ctx->ws, NULL, s);
165 5
        if (p != NULL)
166 4
                syslog((int)fac, "%s", p);
167 5
        WS_Reset(ctx->ws, sn);
168 5
}
169
170
VCL_BOOL v_matchproto_(td_std_file_exists)
171
vmod_file_exists(VRT_CTX, VCL_STRING file_name)
172 2
{
173 2
        struct stat st;
174
175 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
176 2
        return (stat(file_name, &st) == 0);
177 2
}
178
179
VCL_VOID v_matchproto_(td_std_collect)
180
vmod_collect(VRT_CTX, VCL_HEADER hdr, VCL_STRING sep)
181 13
{
182 13
        struct http *hp;
183
184 13
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
185 13
        if (hdr == NULL) {
186 0
                VRT_fail(ctx, "std.collect(): header argument is NULL");
187 0
                return;
188 0
        }
189 13
        hp = VRT_selecthttp(ctx, hdr->where);
190 13
        if (hp == NULL) {
191 1
                VRT_fail(ctx, "std.collect(): header argument "
192 1
                    "cannot be used here");
193 1
                return;
194 1
        }
195 12
        http_CollectHdrSep(hp, hdr->what, sep);
196 12
}
197
198
VCL_VOID v_matchproto_(td_std_collect_all)
199
vmod_collect_all(VRT_CTX, VCL_HTTP hp)
200 2
{
201
202 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
203 2
        if (hp == NULL) {
204 0
                VRT_fail(ctx, "std.collect_all(): header argument "
205 0
                    "cannot be used here");
206 0
                return;
207 0
        }
208 2
        http_CollectAllHdrs(hp);
209 2
}
210
211
VCL_BOOL v_matchproto_(td_std_healthy)
212
vmod_healthy(VRT_CTX, VCL_BACKEND be)
213 37
{
214 37
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
215 37
        CHECK_OBJ_ORNULL(be, DIRECTOR_MAGIC);
216 37
        return (VRT_Healthy(ctx, be, NULL));
217 37
}
218
219
VCL_INT v_matchproto_(td_std_port)
220
vmod_port(VRT_CTX, VCL_IP ip)
221 112
{
222 112
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
223 112
        if (ip == NULL)
224 0
                return (0);
225 112
        return (VSA_Port(ip));
226 112
}
227
228
VCL_VOID v_matchproto_(td_std_rollback)
229
vmod_rollback(VRT_CTX, VCL_HTTP hp)
230 25
{
231
232 25
        (void)hp;
233 25
        VRT_Rollback(ctx);
234 25
}
235
236
VCL_VOID v_matchproto_(td_std_timestamp)
237
vmod_timestamp(VRT_CTX, VCL_STRING label)
238 21
{
239
240 21
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
241 21
        if (label == NULL)
242 0
                return;
243 21
        if (*label == '\0')
244 0
                return;
245 21
        if (ctx->bo != NULL && ctx->req == NULL) {
246
                /* Called from backend vcl methods */
247 1
                CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
248 1
                VSLb_ts_busyobj(ctx->bo, label, VTIM_real());
249 20
        } else if (ctx->req != NULL) {
250
                /* Called from request vcl methods */
251 19
                CHECK_OBJ(ctx->req, REQ_MAGIC);
252 19
                VSLb_ts_req(ctx->req, label, VTIM_real());
253 19
        }
254 21
}
255
256
VCL_BOOL v_matchproto_(td_std_cache_req_body)
257
vmod_cache_req_body(VRT_CTX, VCL_BYTES size)
258 42
{
259 42
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
260 42
        size = vmax_t(VCL_BYTES, size, 0);
261 42
        if (VRT_CacheReqBody(ctx, (size_t)size) < 0)
262 6
                return (0);
263 36
        return (1);
264 42
}
265
266
VCL_STRING v_matchproto_(td_std_strstr)
267
vmod_strstr(VRT_CTX, VCL_STRING s1, VCL_STRING s2)
268 3
{
269 3
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
270 3
        if (s1 == NULL || s2 == NULL)
271 1
                return (NULL);
272 2
        return (strstr(s1, s2));
273 3
}
274
275
VCL_STRING v_matchproto_(td_std_getenv)
276
vmod_getenv(VRT_CTX, VCL_STRING name, VCL_STRING fallback)
277 5
{
278 5
        const char *val;
279
280 5
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
281 5
        if (name == NULL || *name == '\0')
282 1
                return (NULL);
283
284 4
        val = getenv(name);
285 4
        if (val == NULL)
286 2
                return (fallback);
287 2
        return (val);
288 4
}
289
290
VCL_VOID v_matchproto_(td_std_late_100_continue)
291
vmod_late_100_continue(VRT_CTX, VCL_BOOL late)
292 5
{
293 5
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
294 5
        assert(ctx->method == VCL_MET_RECV);
295 5
        CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
296 5
        if (ctx->req->want100cont)
297 5
                ctx->req->late100cont = late;
298 5
}
299
300
VCL_BOOL v_matchproto_(td_std_syntax)
301
vmod_syntax(VRT_CTX, VCL_REAL r)
302 6
{
303
304 6
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
305 6
        assert(ctx->syntax == 40 || ctx->syntax == 41);
306
        /*
307
         * We need to be careful because non-integer numbers have imprecise
308
         * IEE754 representation (4.1 is 0x1.0666666666666p+2 = 4.09999...)
309
         * By scaling up and rounding, this is taken care of.
310
         */
311 6
        return (round(r * 10) <= ctx->syntax);
312 6
}
313
314
VCL_BOOL v_matchproto_(td_std_fnmatch)
315
vmod_fnmatch(VRT_CTX, VCL_STRING pattern, VCL_STRING subject,
316
             VCL_BOOL pathname, VCL_BOOL noescape, VCL_BOOL period)
317 62
{
318 62
        int flags = 0;
319
320 62
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
321 62
        if (pattern == NULL) {
322 1
                VRT_fail(ctx, "std.fnmatch(): pattern is NULL");
323 1
                return (0);
324 1
        }
325 61
        if (subject == NULL) {
326 1
                VRT_fail(ctx, "std.fnmatch(): subject is NULL");
327 1
                return (0);
328 1
        }
329
330 60
        if (pathname)
331 45
                flags |= FNM_PATHNAME;
332 60
        if (noescape)
333 15
                flags |= FNM_NOESCAPE;
334 60
        if (period)
335 15
                flags |= FNM_PERIOD;
336 60
        return (fnmatch(pattern, subject, flags) != FNM_NOMATCH);
337 61
}
338
339
static const void * const priv_task_id_ban = &priv_task_id_ban;
340
341
VCL_BOOL v_matchproto_(td_std_ban)
342
vmod_ban(VRT_CTX, VCL_STRING s)
343 22
{
344 22
        struct vmod_priv *priv_task;
345 22
        VCL_STRING r;
346
347 22
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
348
349 22
        r = VRT_ban_string(ctx, s);
350 22
        priv_task = VRT_priv_task_get(ctx, priv_task_id_ban);
351
352 22
        if (r == NULL && priv_task == NULL)
353 10
                return (1);
354
355 12
        if (priv_task == NULL)
356 2
                priv_task = VRT_priv_task(ctx, priv_task_id_ban);
357
358 12
        if (priv_task == NULL) {
359 0
                VRT_fail(ctx, "std.ban(): no priv_task (out of workspace?)");
360 0
                return (0);
361 0
        }
362
363
        /*
364
         * TRUST_ME: the ban error is const. We save it in the un-const priv
365
         * pointer, but promise to only ever return it as a (const) VCL_STRING
366
         */
367 12
        priv_task->priv = TRUST_ME(r);
368
369 12
        return (r == NULL);
370 12
}
371
372
VCL_STRING v_matchproto_(td_std_ban_error)
373
vmod_ban_error(VRT_CTX)
374 12
{
375 12
        struct vmod_priv *priv_task;
376 12
        VCL_STRING r;
377
378 12
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
379
380 12
        priv_task = VRT_priv_task_get(ctx, priv_task_id_ban);
381 12
        if (priv_task == NULL)
382 0
                return ("");
383
384 12
        r = priv_task->priv;
385 12
        if (r == NULL)
386 1
                r = "";
387 12
        return (r);
388 12
}
389
390
VCL_TIME v_matchproto_(td_std_now)
391
vmod_now(VRT_CTX)
392 2
{
393
394 2
        (void) ctx;
395 2
        return (VTIM_real());
396 2
}
397
398
VCL_DURATION v_matchproto_(td_std_timed_call)
399
vmod_timed_call(VRT_CTX, VCL_SUB sub)
400 1
{
401 1
        vtim_mono b;
402
403 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
404 1
        b = VTIM_mono();
405 1
        VRT_call(ctx, sub);
406 1
        return (VTIM_mono() - b);
407 1
}
408
409
VCL_VOID v_matchproto_(td_std_rfc_ttl)
410
vmod_rfc_ttl(VRT_CTX)
411 2
{
412 2
        struct busyobj *bo;
413 2
        struct objcore *oc;
414
415 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
416
        // $Restrict guarantees
417 2
        bo = ctx->bo;
418 2
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
419 2
        oc = bo->fetch_objcore;
420 2
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
421
422 2
        RFC2616_Ttl(bo, ctx->now,
423 2
             &oc->t_origin,
424 2
             &oc->ttl,
425 2
             &oc->grace,
426 2
             &oc->keep);
427 2
}