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