varnish-cache/lib/libvcc/vcc_utils.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
 *
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 <math.h>
34
#include <string.h>
35
#include <stdlib.h>
36
#include <sys/socket.h>
37
#include <sys/types.h>
38
#include <unistd.h>
39
40
#include "vcc_compile.h"
41
42
#include "vre.h"
43
#include "vnum.h"
44
#include "vsa.h"
45
#include "vss.h"
46
#include "vtcp.h"
47
48
/*--------------------------------------------------------------------*/
49
50
static void
51 90057
vcc_cstrcat(struct vcc *tl, struct vsb *vsb)
52
{
53
        struct token *t1;
54
55 90057
        assert(tl->t->tok == CSTR);
56 90057
        VSB_cat(vsb, tl->t->dec);
57
58 90057
        t1 = vcc_PeekToken(tl);
59 90057
        AN(t1);
60
61 90101
        while (t1->tok == '+') {
62 55
                vcc_NextToken(tl);
63 55
                SkipToken(tl, '+');
64 55
                ExpectErr(tl, CSTR);
65 44
                VSB_cat(vsb, tl->t->dec);
66 44
                t1 = vcc_PeekToken(tl);
67 44
                AN(t1);
68
        }
69 90057
}
70
71
void
72 90057
vcc_regexp(struct vcc *tl, struct vsb *vgc_name)
73
{
74
        struct vsb *pattern;
75
        struct token *t0;
76
        char buf[BUFSIZ];
77
        vre_t *t;
78
        int error, erroroffset;
79
        struct inifin *ifp;
80
81 90057
        t0 = tl->t;
82 90057
        pattern = VSB_new_auto();
83 90057
        AN(pattern);
84 90057
        vcc_cstrcat(tl, pattern);
85 90057
        AZ(VSB_finish(pattern));
86
87 90057
        t = VRE_compile(VSB_data(pattern), 0, &error, &erroroffset, 0);
88 90057
        if (t == NULL) {
89 22
                VSB_cat(tl->sb, "Regexp compilation error:\n\n");
90 22
                AZ(VRE_error(tl->sb, error));
91 22
                VSB_cat(tl->sb, "\n\n");
92 22
                vcc_ErrWhere2(tl, t0, tl->t);
93 22
                VSB_destroy(&pattern);
94 22
                return;
95
        }
96 90035
        VRE_free(&t);
97 90035
        bprintf(buf, "VGC_re_%u", tl->unique++);
98 90035
        if (vgc_name)
99 90035
                VSB_cat(vgc_name, buf);
100
101 90035
        Fh(tl, 0, "static struct vre *%s;\n", buf);
102 90035
        ifp = New_IniFin(tl);
103 90035
        VSB_printf(ifp->ini, "\tVPI_re_init(&%s, ",buf);
104 90035
        VSB_quote(ifp->ini, VSB_data(pattern), -1, VSB_QUOTE_CSTR);
105 90035
        VSB_cat(ifp->ini, ");");
106 90035
        VSB_printf(ifp->fin, "\t\tVPI_re_fini(%s);", buf);
107 90035
        VSB_destroy(&pattern);
108 90057
}
109
110
/*
111
 * The IPv6 crew royally screwed up the entire idea behind
112
 * struct sockaddr, see libvarnish/vsa.c for blow-by-blow account.
113
 *
114
 * There is no sane or even remotely portable way to initialize
115
 * a sockaddr for random protocols at compile time.
116
 *
117
 * In our case it is slightly more tricky than that, because we don't
118
 * even want to #include the struct sockaddr* definitions.
119
 *
120
 * Instead we make sure the sockaddr is sane (for our values of
121
 * sane) and dump it as our own "struct suckaddr" type, in binary,
122
 * using the widest integertype, hoping that this will ensure sufficient
123
 * alignment.
124
 */
125
126
static void
127 14729
vcc_suckaddr(struct vcc *tl, const char *host, const struct suckaddr *vsa,
128
    const char **ip, const char **ip_ascii, const char **p_ascii)
129
{
130
        char a[VTCP_ADDRBUFSIZE];
131
        char p[VTCP_PORTBUFSIZE];
132 14729
        const int sz = sizeof(unsigned long long);
133 14729
        const unsigned n = (vsa_suckaddr_len + sz - 1) / sz;
134 14729
        unsigned long long b[n];
135
        unsigned len;
136
        char *q;
137
138 14729
        VTCP_name(vsa, a, sizeof a, p, sizeof p);
139 14729
        Fh(tl, 0, "\n/* \"%s\" -> %s */\n", host, a);
140 14729
        if (ip_ascii != NULL)
141 14432
                *ip_ascii = TlDup(tl, a);
142 14729
        if (p_ascii != NULL && *p_ascii == NULL)
143 14355
                *p_ascii = TlDup(tl, p);
144
145 14729
        Fh(tl, 0, "static const unsigned long long");
146 14729
        Fh(tl, 0, " suckaddr_%u[%d] = {\n", tl->unique, n);
147 14729
        memcpy(b, vsa, vsa_suckaddr_len);
148 73645
        for (len = 0; len < n; len++)
149 117832
                Fh(tl, 0, "%s    0x%0*llxULL",
150 58916
                    len ? ",\n" : "", sz * 2, b[len]);
151 14729
        Fh(tl, 0, "\n};\n");
152
153 14729
        q = TlAlloc(tl, 40);
154 14729
        AN(q);
155 14729
        assert(snprintf(q, 40, "(const void*)suckaddr_%u", tl->unique) < 40);
156 14729
        *ip = q;
157 14729
        tl->unique++;
158 14729
}
159
160
/*--------------------------------------------------------------------
161
 * This routine is a monster, but at least we only have one such monster.
162
 * Look up a IP number, and return IPv4/IPv6 address as VGC produced names
163
 * and optionally ascii strings.
164
 *
165
 * For IP compile time constants we only want one IP#, but it can be
166
 * IPv4 or IPv6.
167
 *
168
 * For backends, we accept up to one IPv4 and one IPv6.
169
 */
170
171
struct rss {
172
        unsigned                magic;
173
#define RSS_MAGIC               0x11e966ab
174
175
        const struct suckaddr   *vsa4;
176
        const struct suckaddr   *vsa6;
177
        struct vsb              *vsb;
178
        int                     retval;
179
        int                     wrong;
180
};
181
182
static int v_matchproto_(vss_resolved_f)
183 14751
rs_callback(void *priv, const struct suckaddr *vsa)
184
{
185
        struct rss *rss;
186
        int v;
187
        char a[VTCP_ADDRBUFSIZE];
188
        char p[VTCP_PORTBUFSIZE];
189
190 14751
        CAST_OBJ_NOTNULL(rss, priv, RSS_MAGIC);
191 14751
        assert(VSA_Sane(vsa));
192
193 14751
        v = VSA_Get_Proto(vsa);
194 14751
        assert(v != AF_UNIX);
195 14751
        VTCP_name(vsa, a, sizeof a, p, sizeof p);
196 14751
        VSB_printf(rss->vsb, "\t%s:%s\n", a, p);
197 14751
        if (v == AF_INET) {
198 14520
                if (rss->vsa4 == NULL)
199 14498
                        rss->vsa4 = VSA_Clone(vsa);
200 22
                else if (VSA_Compare(vsa, rss->vsa4))
201 22
                        rss->wrong++;
202 14520
                rss->retval++;
203 14751
        } else if (v == AF_INET6) {
204 231
                if (rss->vsa6 == NULL)
205 231
                        rss->vsa6 = VSA_Clone(vsa);
206 0
                else if (VSA_Compare(vsa, rss->vsa6))
207 0
                        rss->wrong++;
208 231
                rss->retval++;
209 231
        }
210 14751
        return (0);
211
}
212
213
void
214 14696
Resolve_Sockaddr(struct vcc *tl,
215
    const char *host,
216
    const char *def_port,
217
    const char **ipv4,
218
    const char **ipv4_ascii,
219
    const char **ipv6,
220
    const char **ipv6_ascii,
221
    const char **p_ascii,
222
    int maxips,
223
    const struct token *t_err,
224
    const char *errid)
225
{
226
        int error;
227
        struct rss rss[1];
228
        const char *err;
229
230 14696
        *ipv4 = NULL;
231 14696
        *ipv6 = NULL;
232 14696
        if (p_ascii != NULL)
233 14377
                *p_ascii = NULL;
234
235 14696
        INIT_OBJ(rss, RSS_MAGIC);
236 14696
        rss->vsb = VSB_new_auto();
237 14696
        AN(rss->vsb);
238
239 14696
        error = VSS_resolver(host, def_port, rs_callback, rss, &err);
240 14696
        AZ(VSB_finish(rss->vsb));
241 14696
        if (err != NULL) {
242 88
                VSB_printf(tl->sb,
243
                    "%s '%.*s' could not be resolved to an IP address:\n"
244
                    "\t%s\n"
245
                    "(Sorry if that error message is gibberish.)\n",
246 44
                    errid, PF(t_err), err);
247 44
                vcc_ErrWhere(tl, t_err);
248 44
                if (rss->vsa4 != NULL)
249 0
                        VSA_free(&rss->vsa4);
250 44
                if (rss->vsa6 != NULL)
251 0
                        VSA_free(&rss->vsa6);
252 44
                VSB_destroy(&rss->vsb);
253 44
                ZERO_OBJ(rss, sizeof rss);
254 44
                return;
255
        }
256 14652
        AZ(error);
257 14652
        if (rss->vsa4 != NULL) {
258 14498
                vcc_suckaddr(tl, host, rss->vsa4, ipv4, ipv4_ascii, p_ascii);
259 14498
                VSA_free(&rss->vsa4);
260 14498
        }
261 14652
        if (rss->vsa6 != NULL) {
262 231
                vcc_suckaddr(tl, host, rss->vsa6, ipv6, ipv6_ascii, p_ascii);
263 231
                VSA_free(&rss->vsa6);
264 231
        }
265 14652
        if (rss->retval == 0) {
266 0
                VSB_printf(tl->sb,
267
                    "%s '%.*s': resolves to "
268
                    "neither IPv4 nor IPv6 addresses.\n",
269 0
                    errid, PF(t_err) );
270 0
                vcc_ErrWhere(tl, t_err);
271 0
        }
272 14652
        if (rss->wrong || rss->retval > maxips) {
273 22
                VSB_printf(tl->sb,
274
                    "%s %.*s: resolves to too many addresses.\n"
275
                    "Only one IPv4 %s IPv6 are allowed.\n"
276
                    "Please specify which exact address "
277
                    "you want to use, we found all of these:\n%s",
278 11
                    errid, PF(t_err),
279 11
                    maxips > 1 ? "and one" :  "or",
280 11
                    VSB_data(rss->vsb));
281 11
                vcc_ErrWhere(tl, t_err);
282 11
        }
283 14652
        VSB_destroy(&rss->vsb);
284 14652
        ZERO_OBJ(rss, sizeof rss);
285 14696
}
286
287
/*--------------------------------------------------------------------
288
* Recognize boolean const "true" or "false"
289
*/
290
291
uint8_t
292 44
vcc_BoolVal(struct vcc *tl)
293
{
294
        struct symbol* sym;
295
296 44
        if (tl->t->tok != ID) {
297 11
                VSB_cat(tl->sb, "Expected \"true\" or \"false\"\n");
298 11
                vcc_ErrWhere(tl, tl->t);
299 11
                return (0);
300
        }
301 33
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE, SYMTAB_NOERR, XREF_NONE);
302 33
        if (sym == NULL || sym->type != BOOL) {
303 11
                VSB_cat(tl->sb, "Expected \"true\" or \"false\"\n");
304 11
                vcc_ErrWhere(tl, tl->t);
305 11
                return (0);
306
        }
307 22
        return (sym->eval_priv != NULL);
308 44
}
309
310
/*--------------------------------------------------------------------
311
 * Recognize and convert units of duration, return seconds.
312
 */
313
314
double
315 61842
vcc_DurationUnit(struct vcc *tl)
316
{
317
        double sc;
318
319 61842
        assert(tl->t->tok == ID);
320 61842
        sc = VNUM_duration_unit(1.0, tl->t->b, tl->t->e);
321 61842
        if (!isnan(sc)) {
322 61831
                vcc_NextToken(tl);
323 61831
                return (sc);
324
        }
325 11
        VSB_cat(tl->sb, "Unknown duration unit ");
326 11
        vcc_ErrToken(tl, tl->t);
327 11
        VSB_printf(tl->sb, "\n%s\n", VNUM_LEGAL_DURATION);
328 11
        vcc_ErrWhere(tl, tl->t);
329 11
        return (1.0);
330 61842
}
331
332
/*--------------------------------------------------------------------
333
 * Recognize and convert { CNUM } to unsigned value
334
 * The tokenizer made sure we only get digits.
335
 */
336
337
uint64_t
338 2189
vcc_UintVal(struct vcc *tl)
339
{
340
        int64_t retval;
341
342 2189
        if (tl->t->tok != CNUM) {
343 0
                Expect(tl, CNUM);
344 0
                return (0);
345
        }
346 2189
        retval = (int64_t)round(tl->t->num);
347 2189
        if (retval < 0) {
348 0
                VSB_cat(tl->sb, "UINT cannot be negative\n");
349 0
                vcc_ErrWhere(tl, tl->t);
350 0
                return (0);
351
        }
352 2189
        vcc_NextToken(tl);
353 2189
        return (retval);
354 2189
}
355
356
static double
357 407
vcc_DoubleVal(struct vcc *tl)
358
{
359
        double retval;
360
361 407
        if (tl->t->tok != CNUM && tl->t->tok != FNUM) {
362 11
                Expect(tl, CNUM);
363 11
                return (0);
364
        }
365 396
        retval = tl->t->num;
366 396
        vcc_NextToken(tl);
367 396
        return (retval);
368 407
}
369
370
/*--------------------------------------------------------------------*/
371
372
void
373 407
vcc_Duration(struct vcc *tl, double *d)
374
{
375
        double v, sc;
376
377 407
        v = vcc_DoubleVal(tl);
378 407
        ERRCHK(tl);
379 396
        ExpectErr(tl, ID);
380 396
        sc = vcc_DurationUnit(tl);
381 396
        *d = v * sc;
382 407
}
383
384
/*--------------------------------------------------------------------*/
385
386
void
387 517
vcc_ByteVal(struct vcc *tl, VCL_INT *d)
388
{
389
        double v;
390
        VCL_INT retval;
391
        const char *errtxt;
392
393 517
        if (tl->t->tok != CNUM && tl->t->tok != FNUM) {
394 0
                Expect(tl, CNUM);
395 0
                return;
396
        }
397 517
        v = tl->t->num;
398 517
        vcc_NextToken(tl);
399 517
        if (tl->t->tok != ID) {
400 11
                VSB_cat(tl->sb, "Expected BYTES unit (B, KB, MB...) got ");
401 11
                vcc_ErrToken(tl, tl->t);
402 11
                VSB_cat(tl->sb, "\n");
403 11
                vcc_ErrWhere(tl, tl->t);
404 11
                return;
405
        }
406 506
        retval = VNUM_bytes_unit(v, tl->t->b, tl->t->e, 0, &errtxt);
407 506
        if (errno) {
408 11
                VSB_cat(tl->sb, errtxt);
409 11
                vcc_ErrToken(tl, tl->t);
410 11
                vcc_ErrWhere(tl, tl->t);
411 11
                return;
412
        }
413 495
        vcc_NextToken(tl);
414 495
        *d = retval;
415 517
}
416
417
/*--------------------------------------------------------------------*/
418
419
int
420 1144
vcc_IsFlagRaw(struct vcc *tl, const struct token *t1, const struct token *t2)
421
{
422
423 1144
        if (t1->tok != '-' && t1->tok != '+')
424 902
                return (-1);
425 242
        if (t2->b != t1->e) {
426 11
                VSB_cat(tl->sb, "Expected a flag at:\n");
427 11
                vcc_ErrWhere(tl, t1);
428 11
                return (-1);
429
        }
430 231
        return (t1->tok == '+' ? 1 : 0);
431 1144
}
432
433
int
434 605
vcc_IsFlag(struct vcc *tl)
435
{
436
        struct token *t;
437
        int retval;
438
439 605
        t = vcc_PeekToken(tl);
440 605
        if (t == NULL)
441 0
                return (-1);
442 605
        retval = vcc_IsFlagRaw(tl, tl->t, t);
443 605
        if (retval >= 0)
444 176
                vcc_NextToken(tl);
445 605
        return (retval);
446 605
}
447
448
char *
449 6692081
vcc_Dup_be(const char *b, const char *e)
450
{
451
        char *p;
452
453 6692081
        AN(b);
454 6692081
        if (e == NULL)
455 0
                e = strchr(b, '\0');
456 6692081
        AN(e);
457 6692081
        assert(e >= b);
458
459 6692081
        p = strndup(b, e - b);
460 6692081
        AN(p);
461 6692081
        return (p);
462
}
463
464
int
465 26070
vcc_Has_vcl_prefix(const char *b)
466
{
467 23870
        return (
468 26070
            (b[0] == 'v' || b[0] == 'V') &&
469 2233
            (b[1] == 'c' || b[1] == 'C') &&
470 26103
            (b[2] == 'l' || b[2] == 'L') &&
471 33
            (b[3] == '_')
472
        );
473
}