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 1866
VSL_Name2Tag(const char *name, int l)
63
{
64
        int i, n;
65
66 1866
        if (l == -1)
67 177
                l = strlen(name);
68 1866
        n = -1;
69 85317
        for (i = 0; i < SLT__MAX; i++) {
70 85301
                if (VSL_tags[i] != NULL &&
71 80843
                    !strncasecmp(name, VSL_tags[i], l)) {
72 1884
                        if (strlen(VSL_tags[i]) == l) {
73
                                /* Exact match */
74 1850
                                return (i);
75
                        }
76 34
                        if (n == -1)
77 4
                                n = i;
78
                        else
79 30
                                n = -2;
80 34
                }
81 83451
        }
82 16
        return (n);
83 1866
}
84
85
int
86 235
VSL_Glob2Tags(const char *glob, int l, VSL_tagfind_f *func, void *priv)
87
{
88 235
        const char *p1 = NULL;
89 235
        const char *p2 = NULL;
90
        const char *e, *p;
91 235
        int i, l1 = 0, l2 = 0, r = 0;
92
93 235
        AN(glob);
94 235
        if (l >= 0)
95 59
                e = glob + l;
96
        else
97 176
                e = strchr(glob, '\0');
98 235
        if (glob == e)
99 2
                return (-1);            // Empty pattern cannot match
100
101 1900
        for (p = glob; p < e; p++)
102 1688
                if (*p == '*')
103 21
                        break;
104
105 233
        if (p == e) {                   // No wildcard
106 212
                i = VSL_Name2Tag(glob, l);
107 212
                if (i < 0)
108 9
                        return (i);
109 203
                if (func != NULL)
110 203
                        (func)(i, priv);
111 203
                return (1);
112
        }
113
114 21
        if (p != glob) {                // Prefix match
115 14
                p1 = glob;
116 14
                l1 = p - p1;
117 14
        }
118
119 21
        if (p != e - 1) {               // Postfix match
120 10
                p2 = p + 1;
121 10
                l2 = e - p2;
122 10
        }
123
124 40
        for (p++; p < e; p++)
125 25
                if (*p == '*')
126 6
                        return (-3);    // More than one wildcard
127
128 3855
        for (i = 0; i < SLT__MAX; i++) {
129 3840
                p = VSL_tags[i];
130 3840
                if (p == NULL)
131 2445
                        continue;
132 1395
                e = strchr(p, '\0');
133 1395
                if ((e - p) - l1 < l2)
134 98
                        continue;
135 1297
                if (p1 != NULL && strncasecmp(p, p1, l1))
136 941
                        continue;
137 356
                if (p2 != NULL && strncasecmp(e - l2, p2, l2))
138 160
                        continue;
139 196
                if (func != NULL)
140 196
                        (func)(i, priv);
141 196
                r++;
142 196
        }
143 15
        if (r == 0)
144 1
                return (-1);
145 14
        return (r);
146 235
}
147
148
int
149 46
VSL_List2Tags(const char *list, int l, VSL_tagfind_f *func, void *priv)
150
{
151
        const char *p, *b, *e;
152 46
        int r, t = 0;
153
154 46
        p = list;
155 46
        if (l >= 0)
156 3
                e = p + l;
157
        else
158 43
                e = strchr(p, '\0');
159 96
        while (p < e) {
160 75
                while (p < e && *p == ',')
161 16
                        p++;
162 59
                if (p == e)
163 1
                        break;
164 58
                b = p;
165 384
                while (p < e && *p != ',')
166 326
                        p++;
167 58
                r = VSL_Glob2Tags(b, p - b, func, priv);
168 58
                if (r < 0)
169 8
                        return (r);
170 50
                t += r;
171
        }
172 38
        if (t == 0)
173 1
                return (-1);
174 37
        return (t);
175 46
}
176
177
const char *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 222
VSLQ_Name2Grouping(const char *name, int l)
186
{
187
        int i, n;
188
189 222
        AN(name);
190 222
        if (l == -1)
191 14
                l = strlen(name);
192 222
        n = -1;
193 398
        for (i = 0; i < VSL_g__MAX; i++) {
194 396
                if (!strncasecmp(name, VSLQ_grouping[i], l)) {
195 222
                        if (strlen(VSLQ_grouping[i]) == l) {
196
                                /* Exact match */
197 220
                                return (i);
198
                        }
199 2
                        if (n == -1)
200 1
                                n = i;
201
                        else
202 1
                                n = -2;
203 2
                }
204 176
        }
205 2
        return (n);
206 222
}
207
208
void v_matchproto_(VSL_tagfind_f)
209 182
vsl_vbm_bitset(int bit, void *priv)
210
{
211
212 182
        vbit_set((struct vbitmap *)priv, bit);
213 182
}
214
215
void v_matchproto_(VSL_tagfind_f)
216 42
vsl_vbm_bitclr(int bit, void *priv)
217
{
218
219 42
        vbit_clr((struct vbitmap *)priv, bit);
220 42
}
221
222
static int
223 38
vsl_ix_arg(struct VSL_data *vsl, int opt, const char *arg)
224
{
225
        int i;
226
227 38
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
228 38
        vsl->flags |= F_SEEN_ixIX;
229
230 76
        i = VSL_List2Tags(arg, -1, opt == 'x' ? vsl_vbm_bitset : vsl_vbm_bitclr,
231 38
            vsl->vbm_suppress);
232 38
        if (i == -1)
233 2
                return (vsl_diag(vsl, "-%c: \"%s\" matches zero tags",
234 1
                    (char)opt, arg));
235 37
        else if (i == -2)
236 2
                return (vsl_diag(vsl, "-%c: \"%s\" is ambiguous",
237 1
                    (char)opt, arg));
238 36
        else if (i == -3)
239 4
                return (vsl_diag(vsl, "-%c: Syntax error in \"%s\"",
240 2
                    (char)opt, arg));
241
242 34
        return (1);
243 38
}
244
245
static int
246 8
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 8
        struct vbitmap *tags = NULL;
254
        char errbuf[VRE_ERROR_LEN];
255
256
257 8
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
258 8
        AN(arg);
259 8
        vsl->flags |= F_SEEN_ixIX;
260
261 8
        b = arg;
262 8
        e = strchr(b, ':');
263 8
        if (e) {
264 3
                tags = vbit_new(SLT__MAX);
265 3
                AN(tags);
266 3
                l = e - b;
267 3
                i = VSL_List2Tags(b, l, vsl_vbm_bitset, tags);
268 3
                if (i < 0)
269 3
                        vbit_destroy(tags);
270 3
                if (i == -1)
271 2
                        return (vsl_diag(vsl,
272
                            "-%c: \"%*.*s\" matches zero tags",
273 1
                            (char)opt, l, l, b));
274 2
                else if (i == -2)
275 2
                        return (vsl_diag(vsl,
276
                            "-%c: \"%*.*s\" is ambiguous",
277 1
                            (char)opt, l, l, b));
278 1
                else if (i <= -3)
279 2
                        return (vsl_diag(vsl,
280
                            "-%c: Syntax error in \"%*.*s\"",
281 1
                            (char)opt, l, l, b));
282 0
                b = e + 1;
283 0
        }
284
285 5
        vre = VRE_compile(b, vsl->C_opt ? VRE_CASELESS : 0, &err, &off, 1);
286 5
        if (vre == NULL) {
287 1
                if (tags)
288 0
                        vbit_destroy(tags);
289 1
                AN(VSB_init(vsb, errbuf, sizeof errbuf));
290 1
                AZ(VRE_error(vsb, err));
291 1
                AZ(VSB_finish(vsb));
292 1
                VSB_fini(vsb);
293 2
                return (vsl_diag(vsl, "-%c: Regex error at position %d (%s)",
294 1
                    (char)opt, off, errbuf));
295
        }
296
297 4
        ALLOC_OBJ(vslf, VSLF_MAGIC);
298 4
        AN(vslf);
299 4
        vslf->tags = tags;
300 4
        vslf->vre = vre;
301
302 4
        if (opt == 'I')
303 2
                VTAILQ_INSERT_TAIL(&vsl->vslf_select, vslf, list);
304
        else {
305 2
                assert(opt == 'X');
306 2
                VTAILQ_INSERT_TAIL(&vsl->vslf_suppress, vslf, list);
307
        }
308
309 4
        return (1);
310 8
}
311
312
static int
313 11
vsl_R_arg(struct VSL_data *vsl, const char *arg)
314
{
315 11
        char buf[32] = "";
316
        char *p;
317
        long l;
318
319 11
        AN(arg);
320 11
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
321
322 11
        errno = 0;
323 11
        l = strtol(arg, &p, 0);
324 11
        if ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE)
325 0
                return (vsl_diag(vsl, "-R: Range error"));
326 11
        if (l <= 0 || l > INT_MAX)
327 3
                return (vsl_diag(vsl, "-R: Range error"));
328 8
        vsl->R_opt_l = l;
329 8
        assert(p != arg);
330 8
        AN(p);
331 8
        if (*p == '\0') {
332 0
                vsl->R_opt_p = 1.0;
333 0
                return (1);
334
        }
335 8
        if (*p != '/' || p[1] == '\0')
336 1
                return (vsl_diag(vsl, "-R: Syntax error"));
337 7
        p++;
338 7
        if (strlen(p) > sizeof(buf) - 2)
339 1
                return (vsl_diag(vsl, "-R: Syntax error"));
340 6
        if (!isdigit(*p))
341 3
                strcat(buf, "1");
342 6
        strcat(buf, p);
343 6
        vsl->R_opt_p = VNUM_duration(buf);
344 6
        if (isnan(vsl->R_opt_p) || vsl->R_opt_p <= 0.0)
345 4
                return (vsl_diag(vsl, "-R: Syntax error: Invalid duration"));
346 2
        return (1);
347 11
}
348
349
int
350 158
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 158
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
358
        /* If first option is 'i', set all bits for suppression */
359 158
        if ((opt == 'i' || opt == 'I') && !(vsl->flags & F_SEEN_ixIX))
360 6939
                for (i = 0; i < SLT__MAX; i++)
361 6939
                        vbit_set(vsl->vbm_suppress, i);
362
363 158
        switch (opt) {
364 15
        case 'b': vsl->b_opt = 1; return (1);
365 70
        case 'c': vsl->c_opt = 1; return (1);
366
        case 'C':
367
                /* Caseless regular expressions */
368 2
                vsl->C_opt = 1;
369 2
                return (1);
370
        case 'E':
371 9
                vsl->E_opt = 1;
372 9
                vsl->c_opt = 1;
373 9
                return (1);
374 38
        case 'i': case 'x': return (vsl_ix_arg(vsl, opt, arg));
375 8
        case 'I': case 'X': return (vsl_IX_arg(vsl, opt, arg));
376
        case 'L':
377 1
                AN(arg);
378 1
                l = strtol(arg, &p, 0);
379 1
                while (isspace(*p))
380 0
                        p++;
381 1
                if (*p != '\0')
382 0
                        return (vsl_diag(vsl, "-L: Syntax error"));
383 1
                if (l <= 0 || l > INT_MAX)
384 1
                        return (vsl_diag(vsl, "-L: Range error"));
385 0
                vsl->L_opt = (int)l;
386 0
                return (1);
387
        case 'R':
388 11
                return (vsl_R_arg(vsl, arg));
389
        case 'T':
390 1
                AN(arg);
391 1
                d = VNUM(arg);
392 1
                if (isnan(d))
393 0
                        return (vsl_diag(vsl, "-T: Syntax error"));
394 1
                if (d < 0.)
395 0
                        return (vsl_diag(vsl, "-T: Range error"));
396 1
                vsl->T_opt = d;
397 1
                return (1);
398 1
        case 'v': vsl->v_opt = 1; return (1);
399
        default:
400 2
                return (0);
401
        }
402 158
}