varnish-cache/lib/libvarnishapi/vsl_arg.c
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
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in the
17
 *    documentation and/or other materials provided with the distribution.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 */
31
32
#include "config.h"
33
34
#include <ctype.h>
35
#include <limits.h>
36
#include <math.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
40
#include "vdef.h"
41
#include "vas.h"
42
#include "miniobj.h"
43
44
#include "vbm.h"
45
#include "vnum.h"
46
#include "vqueue.h"
47
#include "vre.h"
48
#include "vsb.h"
49
50
#include "vapi/vsl.h"
51
52
#include "vsl_api.h"
53
54
/*--------------------------------------------------------------------
55
 * Look up a tag
56
 *   0..255     tag number
57
 *   -1         no tag matches
58
 *   -2         multiple tags match
59
 */
60
61
int
62 3898
VSL_Name2Tag(const char *name, int l)
63
{
64
        int i, n;
65
66 3898
        if (l == -1)
67 370
                l = strlen(name);
68 3898
        n = -1;
69 175682
        for (i = 0; i < SLT__MAX; i++) {
70 175650
                if (VSL_tags[i] != NULL &&
71 166568
                    !strncasecmp(name, VSL_tags[i], l)) {
72 3934
                        if (strlen(VSL_tags[i]) == l) {
73
                                /* Exact match */
74 3866
                                return (i);
75
                        }
76 68
                        if (n == -1)
77 8
                                n = i;
78
                        else
79 60
                                n = -2;
80 68
                }
81 171784
        }
82 32
        return (n);
83 3898
}
84
85
int
86 490
VSL_Glob2Tags(const char *glob, int l, VSL_tagfind_f *func, void *priv)
87
{
88 490
        const char *p1 = NULL;
89 490
        const char *p2 = NULL;
90
        const char *e, *p;
91 490
        int i, l1 = 0, l2 = 0, r = 0;
92
93 490
        AN(glob);
94 490
        if (l >= 0)
95 122
                e = glob + l;
96
        else
97 368
                e = strchr(glob, '\0');
98 490
        if (glob == e)
99 4
                return (-1);            // Empty pattern cannot match
100
101 3934
        for (p = glob; p < e; p++)
102 3490
                if (*p == '*')
103 42
                        break;
104
105 486
        if (p == e) {                   // No wildcard
106 444
                i = VSL_Name2Tag(glob, l);
107 444
                if (i < 0)
108 18
                        return (i);
109 426
                if (func != NULL)
110 426
                        (func)(i, priv);
111 426
                return (1);
112
        }
113
114 42
        if (p != glob) {                // Prefix match
115 28
                p1 = glob;
116 28
                l1 = p - p1;
117 28
        }
118
119 42
        if (p != e - 1) {               // Postfix match
120 20
                p2 = p + 1;
121 20
                l2 = e - p2;
122 20
        }
123
124 80
        for (p++; p < e; p++)
125 50
                if (*p == '*')
126 12
                        return (-3);    // More than one wildcard
127
128 7710
        for (i = 0; i < SLT__MAX; i++) {
129 7680
                p = VSL_tags[i];
130 7680
                if (p == NULL)
131 4890
                        continue;
132 2790
                e = strchr(p, '\0');
133 2790
                if ((e - p) - l1 < l2)
134 196
                        continue;
135 2594
                if (p1 != NULL && strncasecmp(p, p1, l1))
136 1882
                        continue;
137 712
                if (p2 != NULL && strncasecmp(e - l2, p2, l2))
138 320
                        continue;
139 392
                if (func != NULL)
140 392
                        (func)(i, priv);
141 392
                r++;
142 392
        }
143 30
        if (r == 0)
144 2
                return (-1);
145 28
        return (r);
146 490
}
147
148
int
149 96
VSL_List2Tags(const char *list, int l, VSL_tagfind_f *func, void *priv)
150
{
151
        const char *p, *b, *e;
152 96
        int r, t = 0;
153
154 96
        p = list;
155 96
        if (l >= 0)
156 6
                e = p + l;
157
        else
158 90
                e = strchr(p, '\0');
159 200
        while (p < e) {
160 154
                while (p < e && *p == ',')
161 32
                        p++;
162 122
                if (p == e)
163 2
                        break;
164 120
                b = p;
165 792
                while (p < e && *p != ',')
166 672
                        p++;
167 120
                r = VSL_Glob2Tags(b, p - b, func, priv);
168 120
                if (r < 0)
169 16
                        return (r);
170 104
                t += r;
171
        }
172 80
        if (t == 0)
173 2
                return (-1);
174 78
        return (t);
175 96
}
176
177
const char *const VSLQ_grouping[VSL_g__MAX] = {
178
        [VSL_g_raw]     = "raw",
179
        [VSL_g_vxid]    = "vxid",
180
        [VSL_g_request] = "request",
181
        [VSL_g_session] = "session",
182
};
183
184
int
185 468
VSLQ_Name2Grouping(const char *name, int l)
186
{
187
        int i, n;
188
189 468
        AN(name);
190 468
        if (l == -1)
191 28
                l = strlen(name);
192 468
        n = -1;
193 832
        for (i = 0; i < VSL_g__MAX; i++) {
194 828
                if (!strncasecmp(name, VSLQ_grouping[i], l)) {
195 468
                        if (strlen(VSLQ_grouping[i]) == l) {
196
                                /* Exact match */
197 464
                                return (i);
198
                        }
199 4
                        if (n == -1)
200 2
                                n = i;
201
                        else
202 2
                                n = -2;
203 4
                }
204 364
        }
205 4
        return (n);
206 468
}
207
208
void v_matchproto_(VSL_tagfind_f)
209 384
vsl_vbm_bitset(int bit, void *priv)
210
{
211
212 384
        vbit_set((struct vbitmap *)priv, bit);
213 384
}
214
215
void v_matchproto_(VSL_tagfind_f)
216 84
vsl_vbm_bitclr(int bit, void *priv)
217
{
218
219 84
        vbit_clr((struct vbitmap *)priv, bit);
220 84
}
221
222
static int
223 80
vsl_ix_arg(struct VSL_data *vsl, int opt, const char *arg)
224
{
225
        int i;
226
227 80
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
228 80
        vsl->flags |= F_SEEN_ixIX;
229
230 160
        i = VSL_List2Tags(arg, -1, opt == 'x' ? vsl_vbm_bitset : vsl_vbm_bitclr,
231 80
            vsl->vbm_suppress);
232 80
        if (i == -1)
233 4
                return (vsl_diag(vsl, "-%c: \"%s\" matches zero tags",
234 2
                    (char)opt, arg));
235 78
        else if (i == -2)
236 4
                return (vsl_diag(vsl, "-%c: \"%s\" is ambiguous",
237 2
                    (char)opt, arg));
238 76
        else if (i == -3)
239 8
                return (vsl_diag(vsl, "-%c: Syntax error in \"%s\"",
240 4
                    (char)opt, arg));
241
242 72
        return (1);
243 80
}
244
245
static int
246 16
vsl_IX_arg(struct VSL_data *vsl, int opt, const char *arg)
247
{
248
        int i, l, off, err;
249
        const char *b, *e;
250
        vre_t *vre;
251
        struct vsb vsb[1];
252
        struct vslf *vslf;
253 16
        struct vbitmap *tags = NULL;
254
        char errbuf[VRE_ERROR_LEN];
255
256
257 16
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
258 16
        AN(arg);
259 16
        vsl->flags |= F_SEEN_ixIX;
260
261 16
        b = arg;
262 16
        e = strchr(b, ':');
263 16
        if (e) {
264 6
                tags = vbit_new(SLT__MAX);
265 6
                AN(tags);
266 6
                l = e - b;
267 6
                i = VSL_List2Tags(b, l, vsl_vbm_bitset, tags);
268 6
                if (i < 0)
269 6
                        vbit_destroy(tags);
270 6
                if (i == -1)
271 4
                        return (vsl_diag(vsl,
272
                            "-%c: \"%*.*s\" matches zero tags",
273 2
                            (char)opt, l, l, b));
274 4
                else if (i == -2)
275 4
                        return (vsl_diag(vsl,
276
                            "-%c: \"%*.*s\" is ambiguous",
277 2
                            (char)opt, l, l, b));
278 2
                else if (i <= -3)
279 4
                        return (vsl_diag(vsl,
280
                            "-%c: Syntax error in \"%*.*s\"",
281 2
                            (char)opt, l, l, b));
282 0
                b = e + 1;
283 0
        }
284
285 10
        vre = VRE_compile(b, vsl->C_opt ? VRE_CASELESS : 0, &err, &off, 1);
286 10
        if (vre == NULL) {
287 2
                if (tags)
288 0
                        vbit_destroy(tags);
289 2
                AN(VSB_init(vsb, errbuf, sizeof errbuf));
290 2
                AZ(VRE_error(vsb, err));
291 2
                AZ(VSB_finish(vsb));
292 2
                VSB_fini(vsb);
293 4
                return (vsl_diag(vsl, "-%c: Regex error at position %d (%s)",
294 2
                    (char)opt, off, errbuf));
295
        }
296
297 8
        ALLOC_OBJ(vslf, VSLF_MAGIC);
298 8
        AN(vslf);
299 8
        vslf->tags = tags;
300 8
        vslf->vre = vre;
301
302 8
        if (opt == 'I')
303 4
                VTAILQ_INSERT_TAIL(&vsl->vslf_select, vslf, list);
304
        else {
305 4
                assert(opt == 'X');
306 4
                VTAILQ_INSERT_TAIL(&vsl->vslf_suppress, vslf, list);
307
        }
308
309 8
        return (1);
310 16
}
311
312
static int
313 22
vsl_R_arg(struct VSL_data *vsl, const char *arg)
314
{
315 22
        char buf[32] = "";
316
        char *p;
317
        long l;
318
319 22
        AN(arg);
320 22
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
321
322 22
        errno = 0;
323 22
        l = strtol(arg, &p, 0);
324 22
        if ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE)
325 0
                return (vsl_diag(vsl, "-R: Range error"));
326 22
        if (l <= 0 || l > INT_MAX)
327 6
                return (vsl_diag(vsl, "-R: Range error"));
328 16
        vsl->R_opt_l = l;
329 16
        assert(p != arg);
330 16
        AN(p);
331 16
        if (*p == '\0') {
332 0
                vsl->R_opt_p = 1.0;
333 0
                return (1);
334
        }
335 16
        if (*p != '/' || p[1] == '\0')
336 2
                return (vsl_diag(vsl, "-R: Syntax error"));
337 14
        p++;
338 14
        if (strlen(p) > sizeof(buf) - 2)
339 2
                return (vsl_diag(vsl, "-R: Syntax error"));
340 12
        if (!isdigit(*p))
341 6
                strcat(buf, "1");
342 12
        strcat(buf, p);
343 12
        vsl->R_opt_p = VNUM_duration(buf);
344 12
        if (isnan(vsl->R_opt_p) || vsl->R_opt_p <= 0.0)
345 8
                return (vsl_diag(vsl, "-R: Syntax error: Invalid duration"));
346 4
        return (1);
347 22
}
348
349
int
350 330
VSL_Arg(struct VSL_data *vsl, int opt, const char *arg)
351
{
352
        int i;
353
        char *p;
354
        double d;
355
        long l;
356
357 330
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
358
        /* If first option is 'i', set all bits for suppression */
359 330
        if ((opt == 'i' || opt == 'I') && !(vsl->flags & F_SEEN_ixIX))
360 13878
                for (i = 0; i < SLT__MAX; i++)
361 13878
                        vbit_set(vsl->vbm_suppress, i);
362
363 330
        switch (opt) {
364 34
        case 'b': vsl->b_opt = 1; return (1);
365 146
        case 'c': vsl->c_opt = 1; return (1);
366
        case 'C':
367
                /* Caseless regular expressions */
368 4
                vsl->C_opt = 1;
369 4
                return (1);
370
        case 'E':
371 18
                vsl->E_opt = 1;
372 18
                vsl->c_opt = 1;
373 18
                return (1);
374 80
        case 'i': case 'x': return (vsl_ix_arg(vsl, opt, arg));
375 16
        case 'I': case 'X': return (vsl_IX_arg(vsl, opt, arg));
376
        case 'L':
377 2
                AN(arg);
378 2
                l = strtol(arg, &p, 0);
379 2
                while (isspace(*p))
380 0
                        p++;
381 2
                if (*p != '\0')
382 0
                        return (vsl_diag(vsl, "-L: Syntax error"));
383 2
                if (l <= 0 || l > INT_MAX)
384 2
                        return (vsl_diag(vsl, "-L: Range error"));
385 0
                vsl->L_opt = (int)l;
386 0
                return (1);
387
        case 'R':
388 22
                return (vsl_R_arg(vsl, arg));
389
        case 'T':
390 2
                AN(arg);
391 2
                d = VNUM(arg);
392 2
                if (isnan(d))
393 0
                        return (vsl_diag(vsl, "-T: Syntax error"));
394 2
                if (d < 0.)
395 0
                        return (vsl_diag(vsl, "-T: Range error"));
396 2
                vsl->T_opt = d;
397 2
                return (1);
398 2
        case 'v': vsl->v_opt = 1; return (1);
399
        default:
400 4
                return (0);
401
        }
402 330
}