varnish-cache/lib/libvarnishapi/vsl_arg.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2015 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
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 <ctype.h>
34
#include <limits.h>
35
#include <math.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
39
#include "vdef.h"
40
#include "vas.h"
41
#include "miniobj.h"
42
43
#include "vbm.h"
44
#include "vnum.h"
45
#include "vqueue.h"
46
#include "vre.h"
47
48
#include "vapi/vsl.h"
49
50
#include "vsl_api.h"
51
52
/*--------------------------------------------------------------------
53
 * Look up a tag
54
 *   0..255     tag number
55
 *   -1         no tag matches
56
 *   -2         multiple tags match
57
 */
58
59
int
60 962
VSL_Name2Tag(const char *name, int l)
61
{
62
        int i, n;
63
64 962
        if (l == -1)
65 68
                l = strlen(name);
66 962
        n = -1;
67 49702
        for (i = 0; i < SLT__MAX; i++) {
68 96627
                if (VSL_tags[i] != NULL &&
69 46936
                    !strncasecmp(name, VSL_tags[i], l)) {
70 977
                        if (strlen(VSL_tags[i]) == l) {
71
                                /* Exact match */
72 951
                                return (i);
73
                        }
74 26
                        if (n == -1)
75 3
                                n = i;
76
                        else
77 23
                                n = -2;
78
                }
79
        }
80 11
        return (n);
81
}
82
83
int
84 91
VSL_Glob2Tags(const char *glob, int l, VSL_tagfind_f *func, void *priv)
85
{
86 91
        const char *p1 = NULL;
87 91
        const char *p2 = NULL;
88
        const char *e, *p;
89 91
        int i, l1 = 0, l2 = 0, r = 0;
90
91 91
        AN(glob);
92 91
        if (l >= 0)
93 18
                e = glob + l;
94
        else
95 73
                e = strchr(glob, '\0');
96 91
        if (glob == e)
97 2
                return (-1);            // Empty pattern cannot match
98
99 716
        for (p = glob; p < e; p++)
100 642
                if (*p == '*')
101 15
                        break;
102
103 89
        if (p == e) {                   // No wildcard
104 74
                i = VSL_Name2Tag(glob, l);
105 74
                if (i < 0)
106 6
                        return (i);
107 68
                if (func != NULL)
108 68
                        (func)(i, priv);
109 68
                return (1);
110
        }
111
112 15
        if (p != glob) {                // Prefix match
113 10
                p1 = glob;
114 10
                l1 = p - p1;
115
        }
116
117 15
        if (p != e - 1) {               // Postfix match
118 8
                p2 = p + 1;
119 8
                l2 = e - p2;
120
        }
121
122 28
        for (p++; p < e; p++)
123 18
                if (*p == '*')
124 5
                        return (-3);    // More than one wildcard
125
126 2570
        for (i = 0; i < SLT__MAX; i++) {
127 2560
                p = VSL_tags[i];
128 2560
                if (p == NULL)
129 1640
                        continue;
130 920
                e = strchr(p, '\0');
131 920
                if (e - p < l1 + l2)
132 64
                        continue;
133 856
                if (p1 != NULL && strncasecmp(p, p1, l1))
134 623
                        continue;
135 233
                if (p2 != NULL && strncasecmp(e - l2, p2, l2))
136 79
                        continue;
137 154
                if (func != NULL)
138 154
                        (func)(i, priv);
139 154
                r++;
140
        }
141 10
        if (r == 0)
142 1
                return (-1);
143 9
        return (r);
144
}
145
146
int
147 17
VSL_List2Tags(const char *list, int l, VSL_tagfind_f *func, void *priv)
148
{
149
        const char *p, *b, *e;
150 17
        int r, t = 0;
151
152 17
        p = list;
153 17
        if (l >= 0)
154 3
                e = p + l;
155
        else
156 14
                e = strchr(p, '\0');
157 43
        while (p < e) {
158 40
                while (p < e && *p == ',')
159 4
                        p++;
160 18
                if (p == e)
161 1
                        break;
162 17
                b = p;
163 125
                while (p < e && *p != ',')
164 91
                        p++;
165 17
                r = VSL_Glob2Tags(b, p - b, func, priv);
166 17
                if (r < 0)
167 8
                        return (r);
168 9
                t += r;
169
        }
170 9
        if (t == 0)
171 1
                return (-1);
172 8
        return (t);
173
}
174
175
const char *VSLQ_grouping[VSL_g__MAX] = {
176
        [VSL_g_raw]     = "raw",
177
        [VSL_g_vxid]    = "vxid",
178
        [VSL_g_request] = "request",
179
        [VSL_g_session] = "session",
180
};
181
182
int
183 153
VSLQ_Name2Grouping(const char *name, int l)
184
{
185
        int i, n;
186
187 153
        AN(name);
188 153
        if (l == -1)
189 9
                l = strlen(name);
190 153
        n = -1;
191 286
        for (i = 0; i < VSL_g__MAX; i++) {
192 284
                if (!strncasecmp(name, VSLQ_grouping[i], l)) {
193 153
                        if (strlen(VSLQ_grouping[i]) == l) {
194
                                /* Exact match */
195 151
                                return (i);
196
                        }
197 2
                        if (n == -1)
198 1
                                n = i;
199
                        else
200 1
                                n = -2;
201
                }
202
        }
203 2
        return (n);
204
}
205
206
void v_matchproto_(VSL_tagfind_f)
207 78
vsl_vbm_bitset(int bit, void *priv)
208
{
209
210 78
        vbit_set((struct vbitmap *)priv, bit);
211 78
}
212
213
void v_matchproto_(VSL_tagfind_f)
214 6
vsl_vbm_bitclr(int bit, void *priv)
215
{
216
217 6
        vbit_clr((struct vbitmap *)priv, bit);
218 6
}
219
220
static int
221 11
vsl_ix_arg(struct VSL_data *vsl, int opt, const char *arg)
222
{
223
        int i;
224
225 11
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
226 11
        vsl->flags |= F_SEEN_ixIX;
227
228 11
        i = VSL_List2Tags(arg, -1, opt == 'x' ? vsl_vbm_bitset : vsl_vbm_bitclr,
229 11
            vsl->vbm_supress);
230 11
        if (i == -1)
231 1
                return (vsl_diag(vsl, "-%c: \"%s\" matches zero tags",
232 1
                    (char)opt, arg));
233 10
        else if (i == -2)
234 1
                return (vsl_diag(vsl, "-%c: \"%s\" is ambiguous",
235 1
                    (char)opt, arg));
236 9
        else if (i == -3)
237 2
                return (vsl_diag(vsl, "-%c: Syntax error in \"%s\"",
238 2
                    (char)opt, arg));
239
240 7
        return (1);
241
}
242
243
static int
244 8
vsl_IX_arg(struct VSL_data *vsl, int opt, const char *arg)
245
{
246
        int i, l, off;
247
        const char *b, *e, *err;
248
        vre_t *vre;
249
        struct vslf *vslf;
250 8
        struct vbitmap *tags = NULL;
251
252 8
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
253 8
        AN(arg);
254 8
        vsl->flags |= F_SEEN_ixIX;
255
256 8
        b = arg;
257 8
        e = strchr(b, ':');
258 8
        if (e) {
259 3
                tags = vbit_new(SLT__MAX);
260 3
                AN(tags);
261 3
                l = e - b;
262 3
                i = VSL_List2Tags(b, l, vsl_vbm_bitset, tags);
263 3
                if (i < 0)
264 3
                        vbit_destroy(tags);
265 3
                if (i == -1)
266 1
                        return (vsl_diag(vsl,
267
                            "-%c: \"%*.*s\" matches zero tags",
268 1
                            (char)opt, l, l, b));
269 2
                else if (i == -2)
270 1
                        return (vsl_diag(vsl,
271
                            "-%c: \"%*.*s\" is ambiguous",
272 1
                            (char)opt, l, l, b));
273 1
                else if (i <= -3)
274 1
                        return (vsl_diag(vsl,
275
                            "-%c: Syntax error in \"%*.*s\"",
276 1
                            (char)opt, l, l, b));
277 0
                b = e + 1;
278
        }
279
280 5
        vre = VRE_compile(b, vsl->C_opt ? VRE_CASELESS : 0, &err, &off);
281 5
        if (vre == NULL) {
282 1
                if (tags)
283 0
                        vbit_destroy(tags);
284 2
                return (vsl_diag(vsl, "-%c: Regex error at position %d (%s)",
285 1
                    (char)opt, off, err));
286
        }
287
288 4
        ALLOC_OBJ(vslf, VSLF_MAGIC);
289 4
        AN(vslf);
290 4
        vslf->tags = tags;
291 4
        vslf->vre = vre;
292
293 4
        if (opt == 'I')
294 3
                VTAILQ_INSERT_TAIL(&vsl->vslf_select, vslf, list);
295
        else {
296 1
                assert(opt == 'X');
297 1
                VTAILQ_INSERT_TAIL(&vsl->vslf_suppress, vslf, list);
298
        }
299
300 4
        return (1);
301
}
302
303
int
304 27
VSL_Arg(struct VSL_data *vsl, int opt, const char *arg)
305
{
306
        int i;
307
        char *p;
308
        double d;
309
        long l;
310
311 27
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
312
        /* If first option is 'i', set all bits for supression */
313 27
        if ((opt == 'i' || opt == 'I') && !(vsl->flags & F_SEEN_ixIX))
314 3855
                for (i = 0; i < SLT__MAX; i++)
315 3840
                        vbit_set(vsl->vbm_supress, i);
316
317 27
        switch (opt) {
318 2
        case 'b': vsl->b_opt = 1; return (1);
319 2
        case 'c': vsl->c_opt = 1; return (1);
320
        case 'C':
321
                /* Caseless regular expressions */
322 1
                vsl->C_opt = 1;
323 1
                return (1);
324 11
        case 'i': case 'x': return (vsl_ix_arg(vsl, opt, arg));
325 8
        case 'I': case 'X': return (vsl_IX_arg(vsl, opt, arg));
326
        case 'L':
327 1
                AN(arg);
328 1
                l = strtol(arg, &p, 0);
329 2
                while (isspace(*p))
330 0
                        p++;
331 1
                if (*p != '\0')
332 0
                        return (vsl_diag(vsl, "-L: Syntax error"));
333 1
                if (l <= 0 || l > INT_MAX)
334 1
                        return (vsl_diag(vsl, "-L: Range error"));
335 0
                vsl->L_opt = (int)l;
336 0
                return (1);
337
        case 'T':
338 1
                AN(arg);
339 1
                d = VNUM(arg);
340 1
                if (isnan(d))
341 0
                        return (vsl_diag(vsl, "-T: Syntax error"));
342 1
                if (d < 0.)
343 0
                        return (vsl_diag(vsl, "-T: Range error"));
344 1
                vsl->T_opt = d;
345 1
                return (1);
346 1
        case 'v': vsl->v_opt = 1; return (1);
347
        default:
348 0
                return (0);
349
        }
350
}