varnish-cache/bin/varnishncsa/varnishncsa.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2016 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Anders Berg <andersb@vgnett.no>
7
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
8
 * Author: Tollef Fog Heen <tfheen@varnish-software.com>
9
 * Author: Martin Blix Grydeland <mbgrydeland@varnish-software.com>
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 *
32
 * Obtain log data from the shared memory log, order it by session ID, and
33
 * display it in Apache / NCSA combined log format.
34
 *
35
 * See doc/sphinx/reference/varnishncsa.rst for the supported format
36
 * specifiers.
37
 *
38
 */
39
40
#include "config.h"
41
42
#define _WITH_GETLINE
43
44
#include <stdlib.h>
45
#include <stdio.h>
46
#include <unistd.h>
47
#include <string.h>
48
#include <errno.h>
49
#include <signal.h>
50
#include <stdarg.h>
51
#include <inttypes.h>
52
#include <limits.h>
53
#include <ctype.h>
54
#include <time.h>
55
#include <math.h>
56
57
#define VOPT_DEFINITION
58
#define VOPT_INC "varnishncsa_options.h"
59
60
#include "vdef.h"
61
62
#include "b64.h"
63
#include "vapi/vsl.h"
64
#include "vapi/voptget.h"
65
#include "vas.h"
66
#include "vsb.h"
67
#include "vut.h"
68
#include "vqueue.h"
69
#include "miniobj.h"
70
71
#define TIME_FMT "[%d/%b/%Y:%T %z]"
72
#define FORMAT "%h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\""
73
74
static struct VUT *vut;
75
76
struct format;
77
78
enum e_frag {
79
        F_H,                    /* %H Proto */
80
        F_U,                    /* %U URL path */
81
        F_q,                    /* %q Query string */
82
        F_b,                    /* %b Body bytes sent */
83
        F_h,                    /* %h Host name / IP Address */
84
        F_m,                    /* %m Method */
85
        F_s,                    /* %s Status */
86
        F_I,                    /* %I Bytes received */
87
        F_O,                    /* %O Bytes sent */
88
        F_tstart,               /* Time start */
89
        F_tend,                 /* Time end */
90
        F_ttfb,                 /* %{Varnish:time_firstbyte}x */
91
        F_host,                 /* Host header */
92
        F_auth,                 /* Authorization header */
93
        F__MAX,
94
};
95
96
struct fragment {
97
        unsigned                gen;
98
        const char              *b, *e;
99
};
100
101
typedef int format_f(const struct format *format);
102
103
struct format {
104
        unsigned                magic;
105
#define FORMAT_MAGIC            0xC3119CDA
106
107
        char                    time_type;
108
        VTAILQ_ENTRY(format)    list;
109
        format_f                *func;
110
        struct fragment         *frag;
111
        char                    *string;
112
        const char *const       *strptr;
113
        char                    *time_fmt;
114
        int32_t                 *int32;
115
};
116
117
struct watch {
118
        unsigned                magic;
119
#define WATCH_MAGIC             0xA7D4005C
120
121
        VTAILQ_ENTRY(watch)     list;
122
        char                    *key;
123
        int                     keylen;
124
        struct fragment         frag;
125
};
126
VTAILQ_HEAD(watch_head, watch);
127
128
struct vsl_watch {
129
        unsigned                magic;
130
#define VSL_WATCH_MAGIC         0xE3E27D23
131
132
        VTAILQ_ENTRY(vsl_watch) list;
133
        enum VSL_tag_e          tag;
134
        int                     idx;
135
        char                    *prefix;
136
        int                     prefixlen;
137
        struct fragment         frag;
138
};
139
VTAILQ_HEAD(vsl_watch_head, vsl_watch);
140
141
static struct ctx {
142
        /* Options */
143
        int                     a_opt;
144
        int                     b_opt;
145
        int                     c_opt;
146
        char                    *w_arg;
147
148
        FILE                    *fo;
149
        struct vsb              *vsb;
150
        unsigned                gen;
151
        VTAILQ_HEAD(,format)    format;
152
153
        /* State */
154
        struct watch_head       watch_vcl_log;
155
        struct watch_head       watch_reqhdr; /* also bereqhdr */
156
        struct watch_head       watch_resphdr; /* also beresphdr */
157
        struct vsl_watch_head   watch_vsl;
158
        struct fragment         frag[F__MAX];
159
        const char              *hitmiss;
160
        const char              *handling;
161
        const char              *side;
162
        int32_t                 vxid;
163
} CTX;
164
165
static void v_noreturn_
166 2
usage(int status)
167
{
168
        const char **opt;
169
170 2
        fprintf(stderr, "Usage: %s <options>\n\n", vut->progname);
171 2
        fprintf(stderr, "Options:\n");
172 38
        for (opt = vopt_spec.vopt_usage; *opt != NULL; opt += 2)
173 36
                fprintf(stderr, " %-25s %s\n", *opt, *(opt + 1));
174 2
        exit(status);
175
}
176
177
static void
178 3
openout(int append)
179
{
180
181 3
        AN(CTX.w_arg);
182 3
        CTX.fo = fopen(CTX.w_arg, append ? "a" : "w");
183 3
        if (CTX.fo == NULL)
184 1
                VUT_Error(vut, 1, "Can't open output file (%s)",
185 1
                    strerror(errno));
186 2
}
187
188
static int v_matchproto_(VUT_cb_f)
189 1
rotateout(struct VUT *v)
190
{
191
192 1
        assert(v == vut);
193 1
        AN(CTX.w_arg);
194 1
        AN(CTX.fo);
195 1
        fclose(CTX.fo);
196 1
        openout(1);
197 1
        AN(CTX.fo);
198 1
        return (0);
199
}
200
201
static int v_matchproto_(VUT_cb_f)
202 264
flushout(struct VUT *v)
203
{
204
205 264
        assert(v == vut);
206 264
        AN(CTX.fo);
207 264
        if (fflush(CTX.fo))
208 0
                return (-5);
209 264
        return (0);
210
}
211
212
static int
213 104
vsb_esc_cat(struct vsb *sb, const char *b, const char *e)
214
{
215 104
        AN(b);
216
217 561
        for (; b < e; b++) {
218 457
                if (isspace(*b)) {
219 10
                        switch (*b) {
220
                        case '\n':
221 0
                                VSB_cat(sb, "\\n");
222 0
                                break;
223
                        case '\t':
224 0
                                VSB_cat(sb, "\\t");
225 0
                                break;
226
                        case '\f':
227 0
                                VSB_cat(sb, "\\f");
228 0
                                break;
229
                        case '\r':
230 0
                                VSB_cat(sb, "\\r");
231 0
                                break;
232
                        case '\v':
233 0
                                VSB_cat(sb, "\\v");
234 0
                                break;
235
                        default:
236 10
                                VSB_putc(sb, *b);
237 10
                                break;
238
                        }
239 447
                } else if (isprint(*b)) {
240 447
                        switch (*b) {
241
                        case '"':
242 0
                                VSB_cat(sb, "\\\"");
243 0
                                break;
244
                        case '\\':
245 0
                                VSB_cat(sb, "\\\\");
246 0
                                break;
247
                        default:
248 447
                                VSB_putc(sb, *b);
249 447
                                break;
250
                        }
251
                } else
252 0
                        VSB_printf(sb, "\\x%02hhx", *b);
253
        }
254
255 104
        return (VSB_error(sb));
256
}
257
258
static inline int
259 71
vsb_fcat(struct vsb *vsb, const struct fragment *f, const char *dflt)
260
{
261 71
        if (f->gen == CTX.gen) {
262 71
                assert(f->b <= f->e);
263 71
                return (vsb_esc_cat(vsb, f->b, f->e));
264
        }
265 0
        if (dflt)
266 0
                return (vsb_esc_cat(vsb, dflt, dflt + strlen(dflt)));
267 0
        return (-1);
268
}
269
270
static int v_matchproto_(format_f)
271 108
format_string(const struct format *format)
272
{
273
274 108
        CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
275 108
        AN(format->string);
276 108
        AZ(VSB_cat(CTX.vsb, format->string));
277 108
        return (1);
278
}
279
280
static int v_matchproto_(format_f)
281 9
format_strptr(const struct format *format)
282
{
283
284 9
        CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
285 9
        AN(format->strptr);
286 9
        AN(*format->strptr);
287 9
        AZ(VSB_cat(CTX.vsb, *format->strptr));
288 9
        return (1);
289
}
290
291
static int v_matchproto_(format_f)
292 3
format_int32(const struct format *format)
293
{
294
295 3
        CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
296 3
        VSB_printf(CTX.vsb, "%" PRIi32, *format->int32);
297 3
        return (1);
298
}
299
300
static int v_matchproto_(format_f)
301 80
format_fragment(const struct format *format)
302
{
303
304 80
        CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
305 80
        AN(format->frag);
306
307 80
        if (format->frag->gen != CTX.gen) {
308 26
                if (format->string == NULL)
309 0
                        return (-1);
310 26
                AZ(vsb_esc_cat(CTX.vsb, format->string,
311
                    format->string + strlen(format->string)));
312 26
                return (0);
313
        }
314 54
        AZ(vsb_fcat(CTX.vsb, format->frag, NULL));
315 54
        return (1);
316
}
317
318
static int v_matchproto_(format_f)
319 15
format_time(const struct format *format)
320
{
321
        double t_start, t_end;
322
        char *p;
323
        char buf[64];
324
        time_t t;
325
        struct tm tm;
326
327 15
        CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
328 15
        if (CTX.frag[F_tstart].gen == CTX.gen) {
329 15
                t_start = strtod(CTX.frag[F_tstart].b, &p);
330 15
                if (p != CTX.frag[F_tstart].e)
331 0
                        t_start = NAN;
332
        } else
333 0
                t_start = NAN;
334 15
        if (isnan(t_start)) {
335
                /* Missing t_start is a no go */
336 0
                if (format->string == NULL)
337 0
                        return (-1);
338 0
                AZ(VSB_cat(CTX.vsb, format->string));
339 0
                return (0);
340
        }
341
342
        /* Missing t_end defaults to t_start */
343 15
        if (CTX.frag[F_tend].gen == CTX.gen) {
344 15
                t_end = strtod(CTX.frag[F_tend].b, &p);
345 15
                if (p != CTX.frag[F_tend].e)
346 0
                        t_end = t_start;
347
        } else
348 0
                t_end = t_start;
349
350 15
        switch (format->time_type) {
351
        case 'D':
352 3
                AZ(VSB_printf(CTX.vsb, "%d",
353
                    (int)((t_end - t_start) * 1e6)));
354 3
                break;
355
        case 't':
356 9
                AN(format->time_fmt);
357 9
                t = t_start;
358 9
                localtime_r(&t, &tm);
359 9
                strftime(buf, sizeof buf, format->time_fmt, &tm);
360 9
                AZ(VSB_cat(CTX.vsb, buf));
361 9
                break;
362
        case 'T':
363 3
                AZ(VSB_printf(CTX.vsb, "%d", (int)(t_end - t_start)));
364 3
                break;
365
        default:
366 0
                WRONG("Time format specifier");
367
        }
368
369 15
        return (1);
370
}
371
372
static int v_matchproto_(format_f)
373 4
format_requestline(const struct format *format)
374
{
375
376
        (void)format;
377 4
        AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_m], "-"));
378 4
        AZ(VSB_putc(CTX.vsb, ' '));
379 4
        if (CTX.frag[F_host].gen == CTX.gen) {
380 1
                if (strncmp(CTX.frag[F_host].b, "http://", 7))
381 1
                        AZ(VSB_cat(CTX.vsb, "http://"));
382 1
                AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_host], NULL));
383
        } else
384 3
                AZ(VSB_cat(CTX.vsb, "http://localhost"));
385 4
        AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_U], ""));
386 4
        AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_q], ""));
387 4
        AZ(VSB_putc(CTX.vsb, ' '));
388 4
        AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_H], "HTTP/1.0"));
389 4
        return (1);
390
}
391
392
static int v_matchproto_(format_f)
393 7
format_auth(const struct format *format)
394
{
395
        char buf[128];
396
        char *q;
397
398 10
        if (CTX.frag[F_auth].gen != CTX.gen ||
399 3
            VB64_decode(buf, sizeof buf, CTX.frag[F_auth].b,
400
            CTX.frag[F_auth].e)) {
401 4
                if (format->string == NULL)
402 0
                        return (-1);
403 4
                AZ(vsb_esc_cat(CTX.vsb, format->string,
404
                    format->string + strlen(format->string)));
405 4
                return (0);
406
        }
407 3
        q = strchr(buf, ':');
408 3
        if (q != NULL)
409 3
                *q = '\0';
410 3
        AZ(vsb_esc_cat(CTX.vsb, buf, buf + strlen(buf)));
411 3
        return (1);
412
}
413
414
static int
415 19
print(void)
416
{
417
        const struct format *f;
418 19
        int i, r = 1;
419
420 19
        VSB_clear(CTX.vsb);
421 245
        VTAILQ_FOREACH(f, &CTX.format, list) {
422 226
                CHECK_OBJ_NOTNULL(f, FORMAT_MAGIC);
423 226
                i = (f->func)(f);
424 226
                if (r > i)
425 15
                        r = i;
426
        }
427 19
        AZ(VSB_putc(CTX.vsb, '\n'));
428 19
        AZ(VSB_finish(CTX.vsb));
429 19
        if (r >= 0) {
430 19
                i = fwrite(VSB_data(CTX.vsb), 1, VSB_len(CTX.vsb), CTX.fo);
431 19
                if (i != VSB_len(CTX.vsb))
432 0
                        return (-5);
433
        }
434 19
        return (0);
435
}
436
437
static void
438 46
addf_string(const char *str)
439
{
440
        struct format *f;
441
442 46
        AN(str);
443 46
        ALLOC_OBJ(f, FORMAT_MAGIC);
444 46
        AN(f);
445 46
        f->func = format_string;
446 46
        f->string = strdup(str);
447 46
        AN(f->string);
448 46
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
449 46
}
450
451
static void
452 3
addf_strptr(const char *const *strptr)
453
{
454
        struct format *f;
455
456 3
        AN(strptr);
457 3
        ALLOC_OBJ(f, FORMAT_MAGIC);
458 3
        AN(f);
459 3
        f->func = format_strptr;
460 3
        f->strptr = strptr;
461 3
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
462 3
}
463
464
static void
465 23
addf_fragment(struct fragment *frag, const char *str)
466
{
467
        struct format *f;
468
469 23
        AN(frag);
470 23
        ALLOC_OBJ(f, FORMAT_MAGIC);
471 23
        AN(f);
472 23
        f->func = format_fragment;
473 23
        f->frag = frag;
474 23
        if (str != NULL) {
475 23
                f->string = strdup(str);
476 23
                AN(f->string);
477
        }
478 23
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
479 23
}
480
481
static void
482 1
addf_int32(int32_t *i)
483
{
484
        struct format *f;
485
486 1
        AN(i);
487 1
        ALLOC_OBJ(f, FORMAT_MAGIC);
488 1
        AN(f);
489 1
        f->func = format_int32;
490 1
        f->int32 = i;
491 1
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
492 1
}
493
494
static void
495 6
addf_time(char type, const char *fmt)
496
{
497
        struct format *f;
498
499 6
        ALLOC_OBJ(f, FORMAT_MAGIC);
500 6
        AN(f);
501 6
        f->func = format_time;
502 6
        f->time_type = type;
503 6
        if (fmt != NULL) {
504 4
                f->time_fmt = strdup(fmt);
505 4
                AN(f->time_fmt);
506
        }
507 6
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
508 6
}
509
510
static void
511 3
addf_requestline(void)
512
{
513
        struct format *f;
514
515 3
        ALLOC_OBJ(f, FORMAT_MAGIC);
516 3
        AN(f);
517 3
        f->func = format_requestline;
518 3
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
519 3
}
520
521
static void
522 1
addf_vcl_log(const char *key)
523
{
524
        struct watch *w;
525
        struct format *f;
526
527 1
        AN(key);
528 1
        ALLOC_OBJ(w, WATCH_MAGIC);
529 1
        AN(w);
530 1
        w->keylen = asprintf(&w->key, "%s:", key);
531 1
        assert(w->keylen > 0);
532 1
        VTAILQ_INSERT_TAIL(&CTX.watch_vcl_log, w, list);
533
534 1
        ALLOC_OBJ(f, FORMAT_MAGIC);
535 1
        AN(f);
536 1
        f->func = format_fragment;
537 1
        f->frag = &w->frag;
538 1
        f->string = strdup("");
539 1
        AN(f->string);
540 1
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
541 1
}
542
543
static void
544 9
addf_hdr(struct watch_head *head, const char *key)
545
{
546
        struct watch *w;
547
        struct format *f;
548
549 9
        AN(head);
550 9
        AN(key);
551 9
        ALLOC_OBJ(w, WATCH_MAGIC);
552 9
        AN(w);
553 9
        w->keylen = asprintf(&w->key, "%s:", key);
554 9
        assert(w->keylen > 0);
555 9
        VTAILQ_INSERT_TAIL(head, w, list);
556
557 9
        ALLOC_OBJ(f, FORMAT_MAGIC);
558 9
        AN(f);
559 9
        f->func = format_fragment;
560 9
        f->frag = &w->frag;
561 9
        f->string = strdup("-");
562 9
        AN(f->string);
563 9
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
564 9
}
565
566
static void
567 5
addf_vsl(enum VSL_tag_e tag, long i, const char *prefix)
568
{
569
        struct vsl_watch *w;
570
571 5
        ALLOC_OBJ(w, VSL_WATCH_MAGIC);
572 5
        AN(w);
573 5
        w->tag = tag;
574 5
        assert(i <= INT_MAX);
575 5
        w->idx = i;
576 5
        if (prefix != NULL) {
577 3
                w->prefixlen = asprintf(&w->prefix, "%s:", prefix);
578 3
                assert(w->prefixlen > 0);
579
        }
580 5
        VTAILQ_INSERT_TAIL(&CTX.watch_vsl, w, list);
581 5
        addf_fragment(&w->frag, "-");
582 5
}
583
584
static void
585 4
addf_auth(void)
586
{
587
        struct format *f;
588
589 4
        ALLOC_OBJ(f, FORMAT_MAGIC);
590 4
        AN(f);
591 4
        f->func = format_auth;
592 4
        f->string = strdup("-");
593 4
        AN(f->string);
594 4
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
595 4
}
596
597
static void
598 21
parse_x_format(char *buf)
599
{
600
        char *e, *r, *s;
601
        long lval;
602
        int slt;
603
604 21
        if (!strcmp(buf, "Varnish:time_firstbyte")) {
605 1
                addf_fragment(&CTX.frag[F_ttfb], "");
606 1
                return;
607
        }
608 20
        if (!strcmp(buf, "Varnish:hitmiss")) {
609 1
                addf_strptr(&CTX.hitmiss);
610 1
                return;
611
        }
612 19
        if (!strcmp(buf, "Varnish:handling")) {
613 1
                addf_strptr(&CTX.handling);
614 1
                return;
615
        }
616 18
        if (!strcmp(buf, "Varnish:side")) {
617 1
                addf_strptr(&CTX.side);
618 1
                return;
619
        }
620 17
        if (!strcmp(buf, "Varnish:vxid")) {
621 1
                addf_int32(&CTX.vxid);
622 1
                return;
623
        }
624 16
        if (!strncmp(buf, "VCL_Log:", 8)) {
625 1
                addf_vcl_log(buf + 8);
626 1
                return;
627
        }
628 15
        if (!strncmp(buf, "VSL:", 4)) {
629 14
                buf += 4;
630 14
                e = buf;
631 153
                while (*e != '\0')
632 125
                        e++;
633 14
                if (e == buf)
634 1
                        VUT_Error(vut, 1, "Missing tag in VSL:");
635 13
                if (e[-1] == ']') {
636 6
                        r = e - 1;
637 38
                        while (r > buf && *r != '[')
638 26
                                r--;
639 6
                        if (r == buf || r[1] == ']')
640 1
                                VUT_Error(vut, 1, "Syntax error: VSL:%s", buf);
641 5
                        e[-1] = '\0';
642 5
                        lval = strtol(r + 1, &s, 10);
643 5
                        if (s != e - 1)
644 1
                                VUT_Error(vut, 1, "Syntax error: VSL:%s]", buf);
645 4
                        if (lval <= 0 || lval > 255) {
646 2
                                VUT_Error(vut, 1,
647
                                    "Syntax error. Field specifier must be"
648
                                    " between 1 and 255: %s]",
649
                                    buf);
650
                        }
651 2
                        *r = '\0';
652
                } else
653 7
                        lval = 0;
654 9
                r = buf;
655 85
                while (r < e && *r != ':')
656 67
                        r++;
657 9
                if (r != e) {
658 3
                        slt = VSL_Name2Tag(buf, r - buf);
659 3
                        r++;
660
                } else {
661 6
                        slt = VSL_Name2Tag(buf, -1);
662 6
                        r = NULL;
663
                }
664 9
                if (slt == -2)
665 1
                        VUT_Error(vut, 1, "Tag not unique: %s", buf);
666 8
                if (slt == -1)
667 3
                        VUT_Error(vut, 1, "Unknown log tag: %s", buf);
668 5
                assert(slt >= 0);
669
670 5
                addf_vsl(slt, lval, r);
671 5
                return;
672
        }
673 1
        VUT_Error(vut, 1, "Unknown formatting extension: %s", buf);
674
}
675
676
static void
677 22
parse_format(const char *format)
678
{
679
        const char *p, *q;
680
        struct vsb *vsb;
681
        char buf[256];
682
683 22
        if (format == NULL)
684 2
                format = FORMAT;
685
686 22
        vsb = VSB_new_auto();
687 22
        AN(vsb);
688
689 131
        for (p = format; *p != '\0'; p++) {
690
691
                /* Allow the most essential escape sequences in format */
692 122
                if (*p == '\\') {
693 0
                        p++;
694 0
                        if (*p == 't')
695 0
                                AZ(VSB_putc(vsb, '\t'));
696 0
                        else if (*p == 'n')
697 0
                                AZ(VSB_putc(vsb, '\n'));
698 0
                        else if (*p != '\0')
699 0
                                AZ(VSB_putc(vsb, *p));
700 0
                        continue;
701
                }
702
703 122
                if (*p != '%') {
704 56
                        AZ(VSB_putc(vsb, *p));
705 56
                        continue;
706
                }
707
708 66
                if (VSB_len(vsb) > 0) {
709 44
                        AZ(VSB_finish(vsb));
710 44
                        addf_string(VSB_data(vsb));
711 44
                        VSB_clear(vsb);
712
                }
713
714 66
                p++;
715 66
                switch (*p) {
716
                case 'b':       /* Body bytes sent */
717 5
                        addf_fragment(&CTX.frag[F_b], "-");
718 5
                        break;
719
                case 'D':       /* Float request time */
720 1
                        addf_time(*p, NULL);
721 1
                        break;
722
                case 'h':       /* Client host name / IP Address */
723 3
                        addf_fragment(&CTX.frag[F_h], "-");
724 3
                        break;
725
                case 'H':       /* Protocol */
726 1
                        addf_fragment(&CTX.frag[F_H], "HTTP/1.0");
727 1
                        break;
728
                case 'I':       /* Bytes received */
729 1
                        addf_fragment(&CTX.frag[F_I], "-");
730 1
                        break;
731
                case 'l':       /* Client user ID (identd) always '-' */
732 3
                        AZ(VSB_putc(vsb, '-'));
733 3
                        break;
734
                case 'm':       /* Method */
735 1
                        addf_fragment(&CTX.frag[F_m], "-");
736 1
                        break;
737
                case 'O':       /* Bytes sent */
738 1
                        addf_fragment(&CTX.frag[F_O], "-");
739 1
                        break;
740
                case 'q':       /* Query string */
741 1
                        addf_fragment(&CTX.frag[F_q], "");
742 1
                        break;
743
                case 'r':       /* Request line */
744 3
                        addf_requestline();
745 3
                        break;
746
                case 's':       /* Status code */
747 3
                        addf_fragment(&CTX.frag[F_s], "-");
748 3
                        break;
749
                case 't':       /* strftime */
750 3
                        addf_time(*p, TIME_FMT);
751 3
                        break;
752
                case 'T':       /* Int request time */
753 1
                        addf_time(*p, NULL);
754 1
                        break;
755
                case 'u':       /* Remote user from auth */
756 4
                        addf_auth();
757 4
                        break;
758
                case 'U':       /* URL */
759 1
                        addf_fragment(&CTX.frag[F_U], "-");
760 1
                        break;
761
                case '{':
762 33
                        p++;
763 33
                        q = p;
764 399
                        while (*q && *q != '}')
765 333
                                q++;
766 33
                        if (!*q)
767 1
                                VUT_Error(vut, 1, "Unmatched bracket at: %s",
768
                                    p - 2);
769 32
                        assert(q - p < sizeof buf - 1);
770 32
                        strncpy(buf, p, q - p);
771 32
                        buf[q - p] = '\0';
772 32
                        q++;
773 32
                        switch (*q) {
774
                        case 'i':
775 8
                                addf_hdr(&CTX.watch_reqhdr, buf);
776 8
                                break;
777
                        case 'o':
778 1
                                addf_hdr(&CTX.watch_resphdr, buf);
779 1
                                break;
780
                        case 't':
781 1
                                addf_time(*q, buf);
782 1
                                break;
783
                        case 'x':
784 21
                                parse_x_format(buf);
785 11
                                break;
786
                        default:
787 1
                                VUT_Error(vut, 1,
788
                                    "Unknown format specifier at: %s",
789
                                    p - 2);
790
                        }
791 21
                        p = q;
792 21
                        break;
793
                default:
794 1
                        VUT_Error(vut, 1, "Unknown format specifier at: %s",
795
                            p - 1);
796
                }
797
        }
798
799 9
        if (VSB_len(vsb) > 0) {
800
                /* Add any remaining static */
801 2
                AZ(VSB_finish(vsb));
802 2
                addf_string(VSB_data(vsb));
803 2
                VSB_clear(vsb);
804
        }
805
806 9
        VSB_destroy(&vsb);
807 9
}
808
809
static int
810 502
isprefix(const char *prefix, size_t len, const char *b,
811
    const char *e, const char **next)
812
{
813 502
        assert(len > 0);
814 502
        if (e - b < len || strncasecmp(b, prefix, len))
815 421
                return (0);
816 81
        b += len;
817 81
        if (next) {
818 230
                while (b < e && *b == ' ')
819 68
                        b++;
820 81
                *next = b;
821
        }
822 81
        return (1);
823
}
824
825
static void
826 97
frag_fields(int force, const char *b, const char *e, ...)
827
{
828
        va_list ap;
829
        const char *p, *q;
830
        int n, field;
831
        struct fragment *frag;
832
833 97
        AN(b);
834 97
        AN(e);
835 97
        va_start(ap, e);
836
837 97
        field = va_arg(ap, int);
838 97
        frag = va_arg(ap, struct fragment *);
839 205
        for (n = 1, q = b; q < e; n++) {
840
                /* caller must sort the fields, or this loop will not work: */
841 205
                assert(field >= n);
842 205
                AN(frag);
843
844 205
                p = q;
845
                /* Skip WS */
846 518
                while (p < e && isspace(*p))
847 108
                        p++;
848 205
                q = p;
849
                /* Skip non-WS */
850 1958
                while (q < e && !isspace(*q))
851 1548
                        q++;
852
853 205
                if (field == n) {
854 130
                        if (frag->gen != CTX.gen || force) {
855
                                /* We only grab the same matching field once */
856 130
                                frag->gen = CTX.gen;
857 130
                                frag->b = p;
858 130
                                frag->e = q;
859
                        }
860 130
                        field = va_arg(ap, int);
861 130
                        if (field == 0)
862 97
                                break;
863 33
                        frag = va_arg(ap, struct fragment *);
864
                }
865
        }
866 97
        va_end(ap);
867 97
}
868
869
static void
870 110
frag_line(int force, const char *b, const char *e, struct fragment *f)
871
{
872
873 110
        if (f->gen == CTX.gen && !force)
874
                /* We only grab the same matching record once */
875 110
                return;
876
877
        /* Skip leading space */
878 220
        while (b < e && isspace(*b))
879 0
                ++b;
880
881
        /* Skip trailing space */
882 220
        while (e > b && isspace(e[-1]))
883 0
                --e;
884
885 110
        f->gen = CTX.gen;
886 110
        f->b = b;
887 110
        f->e = e;
888
}
889
890
static void
891 125
process_hdr(const struct watch_head *head, const char *b, const char *e)
892
{
893
        struct watch *w;
894
        const char *p;
895
896 170
        VTAILQ_FOREACH(w, head, list) {
897 45
                CHECK_OBJ_NOTNULL(w, WATCH_MAGIC);
898 45
                if (!isprefix(w->key, w->keylen, b, e, &p))
899 41
                        continue;
900 4
                frag_line(1, p, e, &w->frag);
901
        }
902 125
}
903
904
static void
905 544
process_vsl(const struct vsl_watch_head *head, enum VSL_tag_e tag,
906
    const char *b, const char *e)
907
{
908
        struct vsl_watch *w;
909
        const char *p;
910
911 969
        VTAILQ_FOREACH(w, head, list) {
912 425
                CHECK_OBJ_NOTNULL(w, VSL_WATCH_MAGIC);
913 425
                if (tag != w->tag)
914 380
                        continue;
915 45
                p = b;
916 84
                if (w->prefixlen > 0 &&
917 39
                    !isprefix(w->prefix, w->prefixlen, b, e, &p))
918 35
                        continue;
919 10
                if (w->idx == 0)
920 5
                        frag_line(0, p, e, &w->frag);
921
                else
922 5
                        frag_fields(0, p, e, w->idx, &w->frag, 0, NULL);
923
        }
924 544
}
925
926
static int v_matchproto_(VSLQ_dispatch_f)
927 48
dispatch_f(struct VSL_data *vsl, struct VSL_transaction * const pt[],
928
    void *priv)
929
{
930
        struct VSL_transaction *t;
931
        unsigned tag;
932
        const char *b, *e, *p;
933
        struct watch *w;
934
        int i, skip, be_mark;
935
936
        (void)vsl;
937
        (void)priv;
938
939
#define BACKEND_MARKER (INT_MAX / 2 + 1)
940
        assert(BACKEND_MARKER >= VSL_t__MAX);
941
942 96
        for (t = pt[0]; t != NULL; t = *++pt) {
943 48
                CTX.gen++;
944
945
                /* Consider client requests only if in client mode.
946
                   Consider backend requests only if in backend mode. */
947 48
                if (t->type == VSL_t_req && CTX.c_opt) {
948 17
                        CTX.side = "c";
949 17
                        be_mark = 0;
950 31
                } else if (t->type == VSL_t_bereq && CTX.b_opt) {
951 2
                        CTX.side = "b";
952 2
                        be_mark = BACKEND_MARKER;
953
                } else
954 29
                        continue;
955 19
                if (t->reason == VSL_r_esi)
956
                        /* Skip ESI requests */
957 0
                        continue;
958 19
                CTX.hitmiss = "-";
959 19
                CTX.handling = "-";
960 19
                CTX.vxid = t->vxid;
961 19
                skip = 0;
962 582
                while (skip == 0 && 1 == VSL_Next(t->c)) {
963 544
                        tag = VSL_TAG(t->c->rec.ptr);
964 544
                        b = VSL_CDATA(t->c->rec.ptr);
965 544
                        e = b + VSL_LEN(t->c->rec.ptr);
966 1632
                        while (e > b && e[-1] == '\0')
967 544
                                e--;
968
969 544
                        switch (tag + be_mark) {
970
                        case SLT_HttpGarbage + BACKEND_MARKER:
971
                        case SLT_HttpGarbage:
972 0
                                skip = 1;
973 0
                                break;
974
                        case SLT_PipeAcct:
975 5
                                frag_fields(0, b, e,
976
                                    3, &CTX.frag[F_I],
977
                                    4, &CTX.frag[F_O],
978
                                    0, NULL);
979 5
                                break;
980
                        case (SLT_BackendStart + BACKEND_MARKER):
981 2
                                frag_fields(1, b, e,
982
                                    1, &CTX.frag[F_h],
983
                                    0, NULL);
984 2
                                break;
985
                        case SLT_ReqStart:
986 17
                                frag_fields(0, b, e,
987
                                    1, &CTX.frag[F_h],
988
                                    0, NULL);
989 17
                                break;
990
                        case (SLT_BereqMethod + BACKEND_MARKER):
991
                        case SLT_ReqMethod:
992 19
                                frag_line(0, b, e, &CTX.frag[F_m]);
993 19
                                break;
994
                        case (SLT_BereqURL + BACKEND_MARKER):
995
                        case SLT_ReqURL:
996 19
                                p = memchr(b, '?', e - b);
997 19
                                if (p == NULL)
998 8
                                        p = e;
999 19
                                frag_line(0, b, p, &CTX.frag[F_U]);
1000 19
                                frag_line(0, p, e, &CTX.frag[F_q]);
1001 19
                                break;
1002
                        case (SLT_BereqProtocol + BACKEND_MARKER):
1003
                        case SLT_ReqProtocol:
1004 19
                                frag_line(0, b, e, &CTX.frag[F_H]);
1005 19
                                break;
1006
                        case (SLT_BerespStatus + BACKEND_MARKER):
1007
                        case SLT_RespStatus:
1008 13
                                frag_line(1, b, e, &CTX.frag[F_s]);
1009 13
                                break;
1010
                        case (SLT_BereqAcct + BACKEND_MARKER):
1011
                        case SLT_ReqAcct:
1012 14
                                frag_fields(0, b, e,
1013
                                    3, &CTX.frag[F_I],
1014
                                    5, &CTX.frag[F_b],
1015
                                    6, &CTX.frag[F_O],
1016
                                    0, NULL);
1017 14
                                break;
1018
                        case (SLT_Timestamp + BACKEND_MARKER):
1019
                        case SLT_Timestamp:
1020
#define ISPREFIX(a, b, c, d)    isprefix(a, strlen(a), b, c, d)
1021 80
                                if (ISPREFIX("Start:", b, e, &p)) {
1022 18
                                        frag_fields(0, p, e, 1,
1023
                                            &CTX.frag[F_tstart], 0, NULL);
1024
1025 112
                                } else if (ISPREFIX("Resp:", b, e, &p) ||
1026 95
                                    ISPREFIX("PipeSess:", b, e, &p) ||
1027 45
                                    ISPREFIX("BerespBody:", b, e, &p)) {
1028 18
                                        frag_fields(0, p, e, 1,
1029
                                            &CTX.frag[F_tend], 0, NULL);
1030
1031 76
                                } else if (ISPREFIX("Process:", b, e, &p) ||
1032 59
                                    ISPREFIX("Pipe:", b, e, &p) ||
1033 27
                                    ISPREFIX("Beresp:", b, e, &p)) {
1034 18
                                        frag_fields(0, p, e, 2,
1035
                                            &CTX.frag[F_ttfb], 0, NULL);
1036
                                }
1037 80
                                break;
1038
                        case (SLT_BereqHeader + BACKEND_MARKER):
1039
                        case SLT_ReqHeader:
1040 47
                                if (ISPREFIX("Authorization:", b, e, &p) &&
1041 8
                                    ISPREFIX("basic ", p, e, &p))
1042 8
                                        frag_line(0, p, e,
1043
                                            &CTX.frag[F_auth]);
1044 31
                                else if (ISPREFIX("Host:", b, e, &p))
1045 3
                                        frag_line(0, p, e,
1046
                                            &CTX.frag[F_host]);
1047
#undef ISPREFIX
1048 39
                                break;
1049
                        case SLT_VCL_call:
1050 58
                                if (!strcasecmp(b, "recv")) {
1051 17
                                        CTX.hitmiss = "-";
1052 17
                                        CTX.handling = "-";
1053 41
                                } else if (!strcasecmp(b, "hit")) {
1054 5
                                        CTX.hitmiss = "hit";
1055 5
                                        CTX.handling = "hit";
1056 36
                                } else if (!strcasecmp(b, "miss")) {
1057 5
                                        CTX.hitmiss = "miss";
1058 5
                                        CTX.handling = "miss";
1059 31
                                } else if (!strcasecmp(b, "pass")) {
1060 2
                                        CTX.hitmiss = "miss";
1061 2
                                        CTX.handling = "pass";
1062 29
                                } else if (!strcasecmp(b, "synth")) {
1063
                                        /* Arguably, synth isn't a hit or
1064
                                           a miss, but miss is less
1065
                                           wrong */
1066 5
                                        CTX.hitmiss = "miss";
1067 5
                                        CTX.handling = "synth";
1068
                                }
1069 58
                                break;
1070
                        case SLT_VCL_return:
1071 58
                                if (!strcasecmp(b, "pipe")) {
1072 5
                                        CTX.hitmiss = "miss";
1073 5
                                        CTX.handling = "pipe";
1074 53
                                } else if (!strcasecmp(b, "restart"))
1075 0
                                        skip = 1;
1076 58
                                break;
1077
                        case (SLT_VCL_Log + BACKEND_MARKER):
1078
                        case SLT_VCL_Log:
1079 6
                                VTAILQ_FOREACH(w, &CTX.watch_vcl_log, list) {
1080 1
                                        CHECK_OBJ_NOTNULL(w, WATCH_MAGIC);
1081 2
                                        if (e - b < w->keylen ||
1082 1
                                            strncmp(b, w->key, w->keylen))
1083 0
                                                continue;
1084 1
                                        p = b + w->keylen;
1085 1
                                        frag_line(0, p, e, &w->frag);
1086
                                }
1087 5
                                break;
1088
                        default:
1089 196
                                break;
1090
                        }
1091
1092 544
                        if ((tag == SLT_ReqHeader && CTX.c_opt) ||
1093 8
                            (tag == SLT_BereqHeader && CTX.b_opt))
1094 39
                                process_hdr(&CTX.watch_reqhdr, b, e);
1095 505
                        else if ((tag == SLT_RespHeader && CTX.c_opt) ||
1096 2
                            (tag == SLT_BerespHeader && CTX.b_opt))
1097 86
                                process_hdr(&CTX.watch_resphdr, b, e);
1098
1099 544
                        process_vsl(&CTX.watch_vsl, tag, b, e);
1100
                }
1101 19
                if (skip)
1102 0
                        continue;
1103 19
                i = print();
1104 19
                if (i)
1105 0
                        return (i);
1106
        }
1107
1108 48
        return (0);
1109
}
1110
1111
static int v_matchproto_(VUT_cb_f)
1112 0
sighup(struct VUT *v)
1113
{
1114 0
        assert(v == vut);
1115 0
        return (1);
1116
}
1117
1118
static void
1119 2
vut_sighandler(int sig)
1120
{
1121 2
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
1122 2
        VUT_Signaled(vut, sig);
1123 2
}
1124
1125
static char *
1126 3
read_format(const char *formatfile)
1127
{
1128
        FILE *fmtfile;
1129 3
        size_t len = 0;
1130
        int fmtlen;
1131 3
        char *fmt = NULL;
1132
1133 3
        fmtfile = fopen(formatfile, "r");
1134 3
        if (fmtfile == NULL)
1135 1
                VUT_Error(vut, 1, "Can't open format file (%s)",
1136 1
                    strerror(errno));
1137 2
        AN(fmtfile);
1138 2
        fmtlen = getline(&fmt, &len, fmtfile);
1139 2
        if (fmtlen == -1) {
1140 1
                free(fmt);
1141 1
                if (feof(fmtfile))
1142 1
                        VUT_Error(vut, 1, "Empty format file");
1143
                else
1144 0
                        VUT_Error(vut, 1, "Can't read format from file (%s)",
1145 0
                            strerror(errno));
1146
        }
1147 1
        fclose(fmtfile);
1148 1
        if (fmt[fmtlen - 1] == '\n')
1149 1
                fmt[fmtlen - 1] = '\0';
1150 1
        return (fmt);
1151
}
1152
1153
int
1154 31
main(int argc, char * const *argv)
1155
{
1156
        signed char opt;
1157 31
        char *format = NULL;
1158
1159 31
        vut = VUT_InitProg(argc, argv, &vopt_spec);
1160 29
        AN(vut);
1161 29
        memset(&CTX, 0, sizeof CTX);
1162 29
        VTAILQ_INIT(&CTX.format);
1163 29
        VTAILQ_INIT(&CTX.watch_vcl_log);
1164 29
        VTAILQ_INIT(&CTX.watch_reqhdr);
1165 29
        VTAILQ_INIT(&CTX.watch_resphdr);
1166 29
        VTAILQ_INIT(&CTX.watch_vsl);
1167 29
        CTX.vsb = VSB_new_auto();
1168 29
        AN(CTX.vsb);
1169 29
        VB64_init();
1170
1171 29
        while ((opt = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
1172 47
                switch (opt) {
1173
                case 'a':
1174
                        /* Append to file */
1175 0
                        CTX.a_opt = 1;
1176 0
                        break;
1177
                case 'b':
1178
                        /* backend mode */
1179 1
                        CTX.b_opt = 1;
1180 1
                        break;
1181
                case 'c':
1182
                        /* client mode */
1183 1
                        CTX.c_opt = 1;
1184 1
                        break;
1185
                case 'F':
1186
                        /* Format string */
1187 19
                        if (format != NULL)
1188 0
                                free(format);
1189 19
                        format = strdup(optarg);
1190 19
                        AN(format);
1191 19
                        break;
1192
                case 'f':
1193
                        /* Format string from file */
1194 3
                        if (format != NULL)
1195 0
                                free(format);
1196 3
                        format = read_format(optarg);
1197 1
                        AN(format);
1198 1
                        break;
1199
                case 'h':
1200
                        /* Usage help */
1201 1
                        usage(0);
1202
                        break;
1203
                case 'w':
1204
                        /* Write to file */
1205 2
                        REPLACE(CTX.w_arg, optarg);
1206 2
                        break;
1207
                default:
1208 20
                        if (!VUT_Arg(vut, opt, optarg))
1209 0
                                usage(1);
1210
                }
1211
        }
1212
        /* default is client mode: */
1213 25
        if (!CTX.b_opt)
1214 24
                CTX.c_opt = 1;
1215
1216 25
        if (optind != argc)
1217 1
                usage(1);
1218
1219 24
        if (vut->D_opt && !CTX.w_arg)
1220 1
                VUT_Error(vut, 1, "Missing -w option");
1221
1222
        /* Check for valid grouping mode */
1223 23
        assert(vut->g_arg < VSL_g__MAX);
1224 23
        if (vut->g_arg != VSL_g_vxid && vut->g_arg != VSL_g_request)
1225 1
                VUT_Error(vut, 1, "Invalid grouping mode: %s",
1226 1
                    VSLQ_grouping[vut->g_arg]);
1227
1228
        /* Prepare output format */
1229 22
        parse_format(format);
1230 9
        free(format);
1231 9
        format = NULL;
1232
1233
        /* Setup output */
1234 9
        vut->dispatch_f = dispatch_f;
1235 9
        vut->dispatch_priv = NULL;
1236 9
        vut->sighup_f = sighup;
1237 9
        if (CTX.w_arg) {
1238 2
                openout(CTX.a_opt);
1239 1
                AN(CTX.fo);
1240 1
                if (vut->D_opt)
1241 1
                        vut->sighup_f = rotateout;
1242
        } else
1243 7
                CTX.fo = stdout;
1244 8
        vut->idle_f = flushout;
1245
1246 8
        VUT_Signal(vut_sighandler);
1247 8
        VUT_Setup(vut);
1248 8
        VUT_Main(vut);
1249 8
        VUT_Fini(&vut);
1250
1251 8
        exit(0);
1252
}