blob: 6556ab6fb169f26bd4204f78c4952a8c51c3ef1a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
25
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020027#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020029#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010032#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020033
William Lallemand5f232402012-04-05 18:02:55 +020034#include <proto/frontend.h>
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +000035#include <proto/proto_http.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020036#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010037#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020038#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010039#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020040#ifdef USE_OPENSSL
41#include <proto/ssl_sock.h>
42#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Dragan Dosen43885c72015-10-01 13:18:13 +020044struct log_fmt {
45 char *name;
46 struct {
47 struct chunk sep1; /* first pid separator */
48 struct chunk sep2; /* second pid separator */
49 } pid;
50};
51
52static const struct log_fmt log_formats[LOG_FORMATS] = {
53 [LOG_FORMAT_RFC3164] = {
54 .name = "rfc3164",
55 .pid = {
56 .sep1 = { .str = "[", .len = 1 },
57 .sep2 = { .str = "]: ", .len = 3 }
58 }
59 },
60 [LOG_FORMAT_RFC5424] = {
61 .name = "rfc5424",
62 .pid = {
63 .sep1 = { .str = " ", .len = 1 },
64 .sep2 = { .str = " - ", .len = 3 }
65 }
66 }
Dragan Dosen1322d092015-09-22 16:05:32 +020067};
68
Willy Tarreaubaaee002006-06-26 02:48:02 +020069const char *log_facilities[NB_LOG_FACILITIES] = {
70 "kern", "user", "mail", "daemon",
71 "auth", "syslog", "lpr", "news",
72 "uucp", "cron", "auth2", "ftp",
73 "ntp", "audit", "alert", "cron2",
74 "local0", "local1", "local2", "local3",
75 "local4", "local5", "local6", "local7"
76};
77
Willy Tarreaubaaee002006-06-26 02:48:02 +020078const char *log_levels[NB_LOG_LEVELS] = {
79 "emerg", "alert", "crit", "err",
80 "warning", "notice", "info", "debug"
81};
82
Willy Tarreau570f2212013-06-10 16:42:09 +020083const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +020084const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020085
William Lallemand723b73a2012-02-08 16:37:49 +010086
87/* log_format */
88struct logformat_type {
89 char *name;
90 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010091 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020092 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010093 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +010094 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +010095};
96
William Lallemandb7ff6a32012-03-02 14:35:21 +010097int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
98
William Lallemand723b73a2012-02-08 16:37:49 +010099/* log_format variable names */
100static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200101 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100102
103 /* please keep these lines sorted ! */
104 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
105 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
106 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
107 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
108 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200109 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200110 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +0100111 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +0800112 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +0200113 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +0200114 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +0100115 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200116 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +0100117 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
118 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200119 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100120 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200121 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100122 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
123 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200124 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +0100125 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
126 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
127 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
128 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
129 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
130 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
131 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200132 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
133 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
134 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
135 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000136 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
137 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000138 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000139 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
140 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200141 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100142 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200143 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100144 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
145 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100146 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100147 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
148 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
149 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
150 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
151 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200152 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
153 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100154 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
155 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
156 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
157
158 /* The following tags are deprecated and will be removed soon */
159 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
160 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
161 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
162 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
163 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
164 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
165 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
166 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
167 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
168 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
169 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200170 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100171};
172
Willy Tarreau2beef582012-12-20 17:22:52 +0100173char default_http_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
174char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%T] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %Tq %Tw %Tc %Tr %Tt %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl";
175char default_tcp_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq";
William Lallemand723b73a2012-02-08 16:37:49 +0100176char *log_format = NULL;
177
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200178/* Default string used for structured-data part in RFC5424 formatted
179 * syslog messages.
180 */
181char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200182
183/* This is a global syslog header, common to all outgoing messages in
184 * RFC3164 format. It begins with time-based part and is updated by
185 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200186 */
187char *logheader = NULL;
188
Dragan Dosen1322d092015-09-22 16:05:32 +0200189/* This is a global syslog header for messages in RFC5424 format. It is
190 * updated by update_log_hdr_rfc5424().
191 */
192char *logheader_rfc5424 = NULL;
193
Dragan Dosen59cee972015-09-19 22:09:02 +0200194/* This is a global syslog message buffer, common to all outgoing
195 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100196 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200197char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100198
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200199/* A global syslog message buffer, common to all RFC5424 syslog messages.
200 * Currently, it is used for generating the structured-data part.
201 */
202char *logline_rfc5424 = NULL;
203
William Lallemand723b73a2012-02-08 16:37:49 +0100204struct logformat_var_args {
205 char *name;
206 int mask;
207};
208
209struct logformat_var_args var_args_list[] = {
210// global
211 { "M", LOG_OPT_MANDATORY },
212 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200213 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100214 { 0, 0 }
215};
216
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200217/* return the name of the directive used in the current proxy for which we're
218 * currently parsing a header, when it is known.
219 */
220static inline const char *fmt_directive(const struct proxy *curproxy)
221{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100222 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200223 case ARGC_ACL:
224 return "acl";
225 case ARGC_STK:
226 return "stick";
227 case ARGC_TRK:
228 return "track-sc";
229 case ARGC_LOG:
230 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200231 case ARGC_LOGSD:
232 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100233 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100234 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100235 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100236 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200237 case ARGC_UIF:
238 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100239 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200240 return "redirect";
241 case ARGC_CAP:
242 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200243 case ARGC_SRV:
244 return "server";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100245 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200246 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100247 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200248}
249
William Lallemand723b73a2012-02-08 16:37:49 +0100250/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100251 * callback used to configure addr source retrieval
252 */
253int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
254{
255 curproxy->options2 |= PR_O2_SRC_ADDR;
256
257 return 0;
258}
259
260
261/*
William Lallemand723b73a2012-02-08 16:37:49 +0100262 * Parse args in a logformat_var
263 */
264int parse_logformat_var_args(char *args, struct logformat_node *node)
265{
266 int i = 0;
267 int end = 0;
268 int flags = 0; // 1 = + 2 = -
269 char *sp = NULL; // start pointer
270
271 if (args == NULL)
272 return 1;
273
274 while (1) {
275 if (*args == '\0')
276 end = 1;
277
278 if (*args == '+') {
279 // add flag
280 sp = args + 1;
281 flags = 1;
282 }
283 if (*args == '-') {
284 // delete flag
285 sp = args + 1;
286 flags = 2;
287 }
288
289 if (*args == '\0' || *args == ',') {
290 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100291 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100292 if (strcmp(sp, var_args_list[i].name) == 0) {
293 if (flags == 1) {
294 node->options |= var_args_list[i].mask;
295 break;
296 } else if (flags == 2) {
297 node->options &= ~var_args_list[i].mask;
298 break;
299 }
300 }
301 }
302 sp = NULL;
303 if (end)
304 break;
305 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100306 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100307 }
308 return 0;
309}
310
311/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100312 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
313 * must pass the args part in the <arg> pointer with its length in <arg_len>,
314 * and varname with its length in <var> and <var_len> respectively. <arg> is
315 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100316 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100317int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions)
William Lallemand723b73a2012-02-08 16:37:49 +0100318{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100319 int j;
320 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100321
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100322 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
323 if (strlen(logformat_keywords[j].name) == var_len &&
324 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
325 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
326 node = calloc(1, sizeof(struct logformat_node));
327 node->type = logformat_keywords[j].type;
328 node->options = *defoptions;
329 if (arg_len) {
330 node->arg = my_strndup(arg, arg_len);
331 parse_logformat_var_args(node->arg, node);
332 }
333 if (node->type == LOG_FMT_GLOBAL) {
334 *defoptions = node->options;
335 free(node->arg);
336 free(node);
337 } else {
338 if (logformat_keywords[j].config_callback &&
339 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100340 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100341 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100342 curproxy->to_log |= logformat_keywords[j].lw;
343 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100344 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100345 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100346 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200347 curproxy->conf.args.file, curproxy->conf.args.line,
348 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100349 return 0;
350 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100351 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200352 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
353 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100354 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100355 }
William Lallemand723b73a2012-02-08 16:37:49 +0100356 }
357 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100358
359 j = var[var_len];
360 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100361 Warning("parsing [%s:%d] : no such format variable '%s' in '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%' in log-format expressions.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200362 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100363 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100364 return -1;
365}
366
367/*
368 * push to the logformat linked list
369 *
370 * start: start pointer
371 * end: end text pointer
372 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100373 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100374 *
375 * LOG_TEXT: copy chars from start to end excluding end.
376 *
377*/
William Lallemand1d705562012-03-12 12:46:41 +0100378void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100379{
380 char *str;
381
Willy Tarreaua3571662012-12-20 21:59:12 +0100382 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100383 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100384 str = calloc(end - start + 1, 1);
385 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100386 str[end - start] = '\0';
387 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100388 node->type = LOG_FMT_TEXT; // type string
389 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100390 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100391 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100392 node->type = LOG_FMT_SEPARATOR;
393 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100394 }
395}
396
397/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100398 * Parse the sample fetch expression <text> and add a node to <list_format> upon
399 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200400 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100401 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100402void add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, const char *file, int line)
Willy Tarreauc8368452012-12-21 00:09:23 +0100403{
404 char *cmd[2];
405 struct sample_expr *expr;
406 struct logformat_node *node;
407 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100408 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100409
410 cmd[0] = text;
411 cmd[1] = "";
412 cmd_arg = 0;
413
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100414 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100415 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200416 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
417 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100418 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100419 return;
420 }
421
422 node = calloc(1, sizeof(struct logformat_node));
423 node->type = LOG_FMT_EXPR;
424 node->expr = expr;
425 node->options = options;
426
427 if (arg_len) {
428 node->arg = my_strndup(arg, arg_len);
429 parse_logformat_var_args(node->arg, node);
430 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100431 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100432 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
433
Willy Tarreau434c57c2013-01-08 01:10:24 +0100434 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100435 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
436
Willy Tarreau434c57c2013-01-08 01:10:24 +0100437 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200438 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
439 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
440 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100441
Willy Tarreauc8368452012-12-21 00:09:23 +0100442 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
443 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100444 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100445
William Lallemand65ad6e12014-01-31 15:08:02 +0100446 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
447 * needed with some sample fetches (eg: ssl*). We always set it for
448 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100449 */
450 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100451 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100452 LIST_ADDQ(list_format, &node->list);
453}
454
455/*
William Lallemand723b73a2012-02-08 16:37:49 +0100456 * Parse the log_format string and fill a linked list.
457 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200458 * You can set arguments using { } : %{many arguments}varname.
459 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100460 *
461 * str: the string to parse
462 * curproxy: the proxy affected
463 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100464 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100465 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100466 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100467void parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, const char *file, int line)
William Lallemand723b73a2012-02-08 16:37:49 +0100468{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100469 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100470 char *arg = NULL; /* start pointer for args */
471 char *var = NULL; /* start pointer for vars */
472 int arg_len = 0;
473 int var_len = 0;
474 int cformat; /* current token format */
475 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100476 struct logformat_node *tmplf, *back;
477
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100478 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200479 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200480
William Lallemand723b73a2012-02-08 16:37:49 +0100481 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100482 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100483 LIST_DEL(&tmplf->list);
484 free(tmplf);
485 }
486
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100487 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100488 pformat = cformat;
489
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100490 if (!*str)
491 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100492
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100493 /* The prinicple of the two-step state machine below is to first detect a change, and
494 * second have all common paths processed at one place. The common paths are the ones
495 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
496 * We use the common LF_INIT state to dispatch to the different final states.
497 */
498 switch (pformat) {
499 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100500 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100501 arg_len = var_len = 0;
502 if (*str == '{') { // optional argument
503 cformat = LF_STARG;
504 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100505 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100506 else if (*str == '[') {
507 cformat = LF_STEXPR;
508 var = str + 1; // store expr in variable name
509 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100510 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100511 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100512 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100513 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100514 else if (*str == '%')
515 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100516 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100517 /* single '%' followed by blank or digit, send them both */
518 cformat = LF_TEXT;
519 pformat = LF_TEXT; /* finally we include the previous char as well */
520 sp = str - 1; /* send both the '%' and the current char */
521 Warning("parsing [%s:%d] : Fixed missing '%%' before '%c' at position %d in %s line : '%s'. Please use '%%%%' when you need the '%%' character in a log-format expression.\n",
522 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
523
524 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100525 else
526 cformat = LF_INIT; // handle other cases of litterals
527 break;
528
529 case LF_STARG: // text immediately following '%{'
530 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100531 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100532 arg_len = str - arg;
533 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100534 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100535 break;
536
537 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100538 if (*str == '[') {
539 cformat = LF_STEXPR;
540 var = str + 1; // store expr in variable name
541 break;
542 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100543 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100544 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100545 var = str;
546 break;
547 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200548 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
549 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100550 cformat = LF_INIT;
551 break;
552
Willy Tarreauc8368452012-12-21 00:09:23 +0100553 case LF_STEXPR: // text immediately following '%['
554 if (*str == ']') { // end of arg
555 cformat = LF_EDEXPR;
556 var_len = str - var;
557 *str = 0; // needed for parsing the expression
558 }
559 break;
560
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100561 case LF_VAR: // text part of a variable name
562 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100563 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100564 cformat = LF_INIT; // not variable name anymore
565 break;
566
Willy Tarreauc8368452012-12-21 00:09:23 +0100567 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100568 cformat = LF_INIT;
569 }
570
571 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
572 switch (*str) {
573 case '%': cformat = LF_STARTVAR; break;
574 case ' ': cformat = LF_SEPARATOR; break;
575 case 0 : cformat = LF_END; break;
576 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100577 }
578 }
579
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100580 if (cformat != pformat || pformat == LF_SEPARATOR) {
581 switch (pformat) {
582 case LF_VAR:
583 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
584 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100585 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100586 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100587 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100588 case LF_TEXT:
589 case LF_SEPARATOR:
590 add_to_logformat_list(sp, str, pformat, list_format);
591 break;
592 }
593 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100594 }
595 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100596
Willy Tarreauc8368452012-12-21 00:09:23 +0100597 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200598 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
599 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
600 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100601
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100602 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100603}
604
Willy Tarreaubaaee002006-06-26 02:48:02 +0200605/*
606 * Displays the message on stderr with the date and pid. Overrides the quiet
607 * mode during startup.
608 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200609void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610{
611 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200612 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613
614 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
615 va_start(argp, fmt);
616
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200617 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200619 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200620 vfprintf(stderr, fmt, argp);
621 fflush(stderr);
622 va_end(argp);
623 }
624}
625
626
627/*
628 * Displays the message on stderr with the date and pid.
629 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200630void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200631{
632 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200633 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634
635 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
636 va_start(argp, fmt);
637
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200638 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200640 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200641 vfprintf(stderr, fmt, argp);
642 fflush(stderr);
643 va_end(argp);
644 }
645}
646
647/*
648 * Displays the message on <out> only if quiet mode is not set.
649 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200650void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651{
652 va_list argp;
653
654 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
655 va_start(argp, fmt);
656 vfprintf(out, fmt, argp);
657 fflush(out);
658 va_end(argp);
659 }
660}
661
662/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200663 * returns log format for <fmt> or -1 if not found.
664 */
665int get_log_format(const char *fmt)
666{
667 int format;
668
669 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200670 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200671 format--;
672
673 return format;
674}
675
676/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 * returns log level for <lev> or -1 if not found.
678 */
679int get_log_level(const char *lev)
680{
681 int level;
682
683 level = NB_LOG_LEVELS - 1;
684 while (level >= 0 && strcmp(log_levels[level], lev))
685 level--;
686
687 return level;
688}
689
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690/*
691 * returns log facility for <fac> or -1 if not found.
692 */
693int get_log_facility(const char *fac)
694{
695 int facility;
696
697 facility = NB_LOG_FACILITIES - 1;
698 while (facility >= 0 && strcmp(log_facilities[facility], fac))
699 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100700
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701 return facility;
702}
703
William Lallemanda1cc3812012-02-08 16:38:44 +0100704/*
705 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200706 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100707 *
708 * Return the adress of the \0 character, or NULL on error
709 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100710char *lf_text_len(char *dst, const char *src, size_t len, size_t size, struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +0100711{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100712 if (size < 2)
713 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100714
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100715 if (node->options & LOG_OPT_QUOTE) {
716 *(dst++) = '"';
717 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100718 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100719
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100720 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100721 if (++len > size)
722 len = size;
723 len = strlcpy2(dst, src, len);
724
725 size -= len;
726 dst += len;
727 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100728 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
729 if (size < 2)
730 return NULL;
731 *(dst++) = '-';
732 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100733
734 if (node->options & LOG_OPT_QUOTE) {
735 if (size < 2)
736 return NULL;
737 *(dst++) = '"';
738 }
739
740 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100741 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100742}
743
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100744static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
745{
746 return lf_text_len(dst, src, size, size, node);
747}
748
William Lallemand5f232402012-04-05 18:02:55 +0200749/*
750 * Write a IP adress to the log string
751 * +X option write in hexadecimal notation, most signifant byte on the left
752 */
753char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
754{
755 char *ret = dst;
756 int iret;
757 char pn[INET6_ADDRSTRLEN];
758
759 if (node->options & LOG_OPT_HEXA) {
760 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
761 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
762 if (iret < 0 || iret > size)
763 return NULL;
764 ret += iret;
765 } else {
766 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
767 ret = lf_text(dst, pn, size, node);
768 if (ret == NULL)
769 return NULL;
770 }
771 return ret;
772}
773
774/*
775 * Write a port to the log
776 * +X option write in hexadecimal notation, most signifant byte on the left
777 */
778char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
779{
780 char *ret = dst;
781 int iret;
782
783 if (node->options & LOG_OPT_HEXA) {
784 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
785 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
786 if (iret < 0 || iret > size)
787 return NULL;
788 ret += iret;
789 } else {
790 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
791 if (ret == NULL)
792 return NULL;
793 }
794 return ret;
795}
796
Dragan Dosen1322d092015-09-22 16:05:32 +0200797/* Re-generate time-based part of the syslog header in RFC3164 format at
798 * the beginning of logheader once a second and return the pointer to the
799 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100800 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200801static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100803 static long tvsec;
804 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Dragan Dosen43885c72015-10-01 13:18:13 +0200805 static struct chunk host = { NULL, 0, 0 };
806 static int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200808 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200810 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100811 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200812
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200813 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +0200814 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815
Dragan Dosen43885c72015-10-01 13:18:13 +0200816 if (unlikely(global.log_send_hostname != host.str)) {
817 host.str = global.log_send_hostname;
818 host.len = host.str ? strlen(host.str) : 0;
819 sep = host.len ? 1 : 0;
820 }
821
Dragan Dosen59cee972015-09-19 22:09:02 +0200822 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +0200823 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200824 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +0200825 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
826 host.len, host.str, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200827 /* WARNING: depending upon implementations, snprintf may return
828 * either -1 or the number of bytes that would be needed to store
829 * the total message. In both cases, we must adjust it.
830 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200831 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
832 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833
Dragan Dosen59cee972015-09-19 22:09:02 +0200834 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835 }
836
Willy Tarreau094af4e2015-01-07 15:03:42 +0100837 dataptr[0] = 0; // ensure we get rid of any previous attempt
838
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200839 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100840}
841
Dragan Dosen1322d092015-09-22 16:05:32 +0200842/* Re-generate time-based part of the syslog header in RFC5424 format at
843 * the beginning of logheader_rfc5424 once a second and return the pointer
844 * to the first character after it.
845 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200846static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +0200847{
848 static long tvsec;
849 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
850
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200851 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +0200852 /* this string is rebuild only once a second */
853 struct tm tm;
854 int hdr_len;
855
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200856 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +0200857 get_localtime(tvsec, &tm);
858
859 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +0200860 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +0200861 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen43885c72015-10-01 13:18:13 +0200862 tm.tm_hour, tm.tm_min, tm.tm_sec, localtimezone,
863 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +0200864 /* WARNING: depending upon implementations, snprintf may return
865 * either -1 or the number of bytes that would be needed to store
866 * the total message. In both cases, we must adjust it.
867 */
868 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
869 hdr_len = global.max_syslog_len;
870
871 dataptr = logheader_rfc5424 + hdr_len;
872 }
873
874 dataptr[0] = 0; // ensure we get rid of any previous attempt
875
876 return dataptr;
877}
878
William Lallemand2a4a44f2012-02-06 16:00:33 +0100879/*
Dragan Dosen59cee972015-09-19 22:09:02 +0200880 * This function sends the syslog message using a printf format string. It
881 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100882 */
883void send_log(struct proxy *p, int level, const char *format, ...)
884{
885 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100886 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100887
Willy Tarreau8c97ab52015-01-15 16:29:53 +0100888 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100889 return;
890
William Lallemand2a4a44f2012-02-06 16:00:33 +0100891 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +0200892 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +0200893 if (data_len < 0 || data_len > global.max_syslog_len)
894 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100896
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200897 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100898}
899
900/*
901 * This function sends a syslog message.
902 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200903 * It overrides the last byte of the message vector with an LF character.
904 * The arguments <sd> and <sd_size> are used for the structured-data part
905 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100906 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200907void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100908{
Dragan Dosen609ac2a2015-09-16 18:25:42 +0200909 static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
910 static struct msghdr msghdr = {
911 .msg_iov = iovec,
912 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
913 };
William Lallemand2a4a44f2012-02-06 16:00:33 +0100914 static int logfdunix = -1; /* syslog to AF_UNIX socket */
915 static int logfdinet = -1; /* syslog to AF_INET socket */
916 static char *dataptr = NULL;
917 int fac_level;
918 struct list *logsrvs = NULL;
919 struct logsrv *tmp = NULL;
920 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +0200921 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +0200922 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200923 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +0200924 struct chunk *tag = &global.log_tag;
925 static int curr_pid;
926 static char pidstr[100];
927 static struct chunk pid;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100928
929 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930
931 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200932 if (!LIST_ISEMPTY(&global.logsrvs)) {
933 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934 }
935 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200936 if (!LIST_ISEMPTY(&p->logsrvs)) {
937 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938 }
Dragan Dosen43885c72015-10-01 13:18:13 +0200939 if (p->log_tag.str) {
940 tag = &p->log_tag;
941 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100942 }
943
William Lallemand0f99e342011-10-12 17:50:54 +0200944 if (!logsrvs)
945 return;
946
Dragan Dosen43885c72015-10-01 13:18:13 +0200947 if (unlikely(curr_pid != getpid())) {
948 curr_pid = getpid();
949 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
950 chunk_initstr(&pid, pidstr);
951 }
952
Robert Tsai81ae1952007-12-05 10:47:29 +0100953 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200954 nblogger = 0;
955 list_for_each_entry(tmp, logsrvs, list) {
956 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100957 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100958 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +0200959 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +0100960 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200961 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +0200962 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +0200963 int tag_max = 0;
964 int pid_sep1_max = 0;
965 int pid_max = 0;
966 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200967 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +0200968 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +0100969
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200970 nblogger++;
971
Willy Tarreaubaaee002006-06-26 02:48:02 +0200972 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200973 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200974 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100975
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200976 if (unlikely(*plogfd < 0)) {
977 /* socket not successfully initialized yet */
978 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
979
980 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
981 Alert("socket for logger #%d failed: %s (errno=%d)\n",
982 nblogger, strerror(errno), errno);
983 continue;
984 }
985 /* we don't want to receive anything on this socket */
986 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
987 /* does nothing under Linux, maybe needed for others */
988 shutdown(*plogfd, SHUT_RD);
989 }
990
Dragan Dosen1322d092015-09-22 16:05:32 +0200991 switch (logsrv->format) {
992 case LOG_FORMAT_RFC3164:
993 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200994 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +0200995 break;
996
997 case LOG_FORMAT_RFC5424:
998 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200999 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001000 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001001 break;
1002
1003 default:
1004 continue; /* must never happen */
1005 }
1006
1007 hdr_size = hdr_ptr - hdr;
1008
Willy Tarreaubaaee002006-06-26 02:48:02 +02001009 /* For each target, we may have a different facility.
1010 * We can also have a different log level for each message.
1011 * This induces variations in the message header length.
1012 * Since we don't want to recompute it each time, nor copy it every
1013 * time, we only change the facility in the pre-computed header,
1014 * and we change the pointer to the header accordingly.
1015 */
William Lallemand0f99e342011-10-12 17:50:54 +02001016 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001017 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001019 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001020 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001021 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001022 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001023 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001024
Dragan Dosen1322d092015-09-22 16:05:32 +02001025 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001026
Dragan Dosen43885c72015-10-01 13:18:13 +02001027 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001028 if (unlikely(hdr_size >= logsrv->maxlen)) {
1029 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001030 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001031 goto send;
1032 }
1033
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001034 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001035
Dragan Dosen43885c72015-10-01 13:18:13 +02001036 /* tag */
1037 tag_max = tag->len;
1038 if (unlikely(tag_max >= maxlen)) {
1039 tag_max = maxlen - 1;
1040 sd_max = 0;
1041 goto send;
1042 }
1043
1044 maxlen -= tag_max;
1045
1046 /* first pid separator */
1047 pid_sep1_max = log_formats[logsrv->format].pid.sep1.len;
1048 if (unlikely(pid_sep1_max >= maxlen)) {
1049 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001050 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001051 goto send;
1052 }
1053
Dragan Dosen43885c72015-10-01 13:18:13 +02001054 pid_sep1 = log_formats[logsrv->format].pid.sep1.str;
1055 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001056
Dragan Dosen43885c72015-10-01 13:18:13 +02001057 /* pid */
1058 pid_max = pid.len;
1059 if (unlikely(pid_max >= maxlen)) {
1060 pid_max = maxlen - 1;
1061 sd_max = 0;
1062 goto send;
1063 }
1064
1065 maxlen -= pid_max;
1066
1067 /* second pid separator */
1068 pid_sep2_max = log_formats[logsrv->format].pid.sep2.len;
1069 if (unlikely(pid_sep2_max >= maxlen)) {
1070 pid_sep2_max = maxlen - 1;
1071 sd_max = 0;
1072 goto send;
1073 }
1074
1075 pid_sep2 = log_formats[logsrv->format].pid.sep2.str;
1076 maxlen -= pid_sep2_max;
1077
1078 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001079 if (sd_max >= maxlen) {
1080 sd_max = maxlen - 1;
1081 goto send;
1082 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001083
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001084 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001085send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001086 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001087 iovec[0].iov_len = hdr_max;
1088 iovec[1].iov_base = tag->str;
1089 iovec[1].iov_len = tag_max;
1090 iovec[2].iov_base = pid_sep1;
1091 iovec[2].iov_len = pid_sep1_max;
1092 iovec[3].iov_base = pid.str;
1093 iovec[3].iov_len = pid_max;
1094 iovec[4].iov_base = pid_sep2;
1095 iovec[4].iov_len = pid_sep2_max;
1096 iovec[5].iov_base = sd;
1097 iovec[5].iov_len = sd_max;
1098 iovec[6].iov_base = dataptr;
1099 iovec[6].iov_len = max;
1100 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1101 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001102
1103 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1104 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1105
1106 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001107
Robert Tsai81ae1952007-12-05 10:47:29 +01001108 if (sent < 0) {
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001109 Alert("sendmsg logger #%d failed: %s (errno=%d)\n",
Robert Tsai81ae1952007-12-05 10:47:29 +01001110 nblogger, strerror(errno), errno);
1111 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001112 }
1113}
1114
William Lallemandbddd4fd2012-02-27 11:23:10 +01001115extern fd_set hdr_encode_map[];
1116extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001117extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001118
Willy Tarreaubaaee002006-06-26 02:48:02 +02001119
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001120const char sess_cookie[8] = "NIDVEOU7"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, Unused, unknown */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001121const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1122 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1123 Set-cookie Updated, unknown, unknown */
1124
William Lallemand1d705562012-03-12 12:46:41 +01001125/*
1126 * try to write a character if there is enough space, or goto out
1127 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001128#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001129 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001130 *(tmplog++) = (x); \
1131 } else { \
1132 goto out; \
1133 } \
1134 } while(0)
1135
William Lallemand1d705562012-03-12 12:46:41 +01001136
Willy Tarreaudf974472012-12-28 02:44:01 +01001137/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1138 * <maxsize> characters. Returns the size of the output string in characters,
1139 * not counting the trailing zero which is always added if the resulting size
1140 * is not zero.
1141 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001142int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001143{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001144 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001145 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +01001146 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001147 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001148 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001149 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001150 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001151 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001152 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001153 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001154 int t_request;
1155 int hdr;
1156 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001157 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001158 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001159 char *ret;
1160 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001161 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001162
William Lallemandbddd4fd2012-02-27 11:23:10 +01001163 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164
William Lallemandbddd4fd2012-02-27 11:23:10 +01001165 t_request = -1;
1166 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1167 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1168
William Lallemand1d705562012-03-12 12:46:41 +01001169 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001170
William Lallemandbddd4fd2012-02-27 11:23:10 +01001171 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001172 if (LIST_ISEMPTY(list_format))
1173 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001174
William Lallemand1d705562012-03-12 12:46:41 +01001175 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001176 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001177 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001178 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001179 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001180
Willy Tarreauc8368452012-12-21 00:09:23 +01001181 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001182 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001183 if (!last_isspace) {
1184 LOGCHAR(' ');
1185 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001186 }
1187 break;
1188
William Lallemand1d705562012-03-12 12:46:41 +01001189 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001190 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001191 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001192 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001193 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001194 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001195 last_isspace = 0;
1196 break;
1197
Willy Tarreauc8368452012-12-21 00:09:23 +01001198 case LOG_FMT_EXPR: // sample expression, may be request or response
1199 key = NULL;
1200 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001201 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Willy Tarreauc8368452012-12-21 00:09:23 +01001202 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001203 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001204 if (tmp->options & LOG_OPT_HTTP)
1205 ret = encode_chunk(tmplog, dst + maxsize,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001206 '%', http_encode_map, key ? &key->data.u.str : &empty);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001207 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001208 ret = lf_text_len(tmplog, key ? key->data.u.str.str : NULL, key ? key->data.u.str.len : 0, dst + maxsize - tmplog, tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01001209 if (ret == 0)
1210 goto out;
1211 tmplog = ret;
1212 last_isspace = 0;
1213 break;
1214
Willy Tarreau2beef582012-12-20 17:22:52 +01001215 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001216 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001217 if (conn)
1218 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1219 else
1220 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001221 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001222 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001223 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001224 last_isspace = 0;
1225 break;
1226
Willy Tarreau2beef582012-12-20 17:22:52 +01001227 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001228 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001229 if (conn) {
1230 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001231 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001232 } else {
1233 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1234 dst + maxsize - tmplog, tmp);
1235 }
William Lallemand5f232402012-04-05 18:02:55 +02001236 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001237 else
1238 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1239
William Lallemand5f232402012-04-05 18:02:55 +02001240 if (ret == NULL)
1241 goto out;
1242 tmplog = ret;
1243 last_isspace = 0;
1244 break;
1245
Willy Tarreau2beef582012-12-20 17:22:52 +01001246 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001247 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001248 if (conn) {
1249 conn_get_to_addr(conn);
1250 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1251 }
1252 else
1253 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1254
William Lallemand1d705562012-03-12 12:46:41 +01001255 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001256 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001257 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001258 last_isspace = 0;
1259 break;
1260
Willy Tarreau2beef582012-12-20 17:22:52 +01001261 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001262 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001263 if (conn) {
1264 conn_get_to_addr(conn);
1265 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001266 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001267 else
1268 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001269 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001270 else
1271 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1272
William Lallemand5f232402012-04-05 18:02:55 +02001273 if (ret == NULL)
1274 goto out;
1275 tmplog = ret;
1276 last_isspace = 0;
1277 break;
1278
Willy Tarreau2beef582012-12-20 17:22:52 +01001279 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001280 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001281 if (conn)
1282 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1283 else
1284 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1285
William Lallemand1d705562012-03-12 12:46:41 +01001286 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001287 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001288 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001289 last_isspace = 0;
1290 break;
1291
Willy Tarreau2beef582012-12-20 17:22:52 +01001292 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001293 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001294 if (conn)
1295 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1296 else
1297 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1298
William Lallemand5f232402012-04-05 18:02:55 +02001299 if (ret == NULL)
1300 goto out;
1301 tmplog = ret;
1302 last_isspace = 0;
1303 break;
1304
Willy Tarreau2beef582012-12-20 17:22:52 +01001305 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001306 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001307 if (conn)
1308 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1309 else
1310 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1311
William Lallemand5f232402012-04-05 18:02:55 +02001312 if (ret == NULL)
1313 goto out;
1314 tmplog = ret;
1315 last_isspace = 0;
1316 break;
1317
Willy Tarreau2beef582012-12-20 17:22:52 +01001318 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001319 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001320 if (conn)
1321 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1322 else
1323 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1324
William Lallemand1d705562012-03-12 12:46:41 +01001325 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001326 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001327 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001328 last_isspace = 0;
1329 break;
1330
William Lallemand1d705562012-03-12 12:46:41 +01001331 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001332 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001333 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1334 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001335 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001336 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001337 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001338 last_isspace = 0;
1339 break;
1340
William Lallemand1d705562012-03-12 12:46:41 +01001341 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001342 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001343 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001344 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001345 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001346 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001347 last_isspace = 0;
1348 break;
1349
Yuxans Yao4e25b012012-10-19 10:36:09 +08001350 case LOG_FMT_DATELOCAL: // %Tl
1351 get_localtime(s->logs.accept_date.tv_sec, &tm);
1352 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1353 if (ret == NULL)
1354 goto out;
1355 tmplog = ret;
1356 last_isspace = 0;
1357 break;
1358
William Lallemand5f232402012-04-05 18:02:55 +02001359 case LOG_FMT_TS: // %Ts
1360 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1361 if (tmp->options & LOG_OPT_HEXA) {
1362 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1363 if (iret < 0 || iret > dst + maxsize - tmplog)
1364 goto out;
1365 last_isspace = 0;
1366 tmplog += iret;
1367 } else {
1368 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1369 if (ret == NULL)
1370 goto out;
1371 tmplog = ret;
1372 last_isspace = 0;
1373 }
1374 break;
1375
William Lallemand1d705562012-03-12 12:46:41 +01001376 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001377 if (tmp->options & LOG_OPT_HEXA) {
1378 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1379 if (iret < 0 || iret > dst + maxsize - tmplog)
1380 goto out;
1381 last_isspace = 0;
1382 tmplog += iret;
1383 } else {
1384 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001385 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001386 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1387 tmplog, 4);
1388 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001389 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001390 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001391 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001392 }
1393 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001394
William Lallemand1d705562012-03-12 12:46:41 +01001395 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001396 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001397 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001398 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001399 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001400 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001401 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001402 break;
1403
Willy Tarreau773d65f2012-10-12 14:56:11 +02001404 case LOG_FMT_FRONTEND_XPRT: // %ft
1405 src = fe->id;
1406 if (tmp->options & LOG_OPT_QUOTE)
1407 LOGCHAR('"');
1408 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1409 if (iret == 0)
1410 goto out;
1411 tmplog += iret;
1412#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001413 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001414 LOGCHAR('~');
1415#endif
1416 if (tmp->options & LOG_OPT_QUOTE)
1417 LOGCHAR('"');
1418 last_isspace = 0;
1419 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001420#ifdef USE_OPENSSL
1421 case LOG_FMT_SSL_CIPHER: // %sslc
1422 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001423 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001424 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001425 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001426 src = ssl_sock_get_cipher_name(conn);
1427 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001428 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1429 if (ret == NULL)
1430 goto out;
1431 tmplog = ret;
1432 last_isspace = 0;
1433 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001434
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001435 case LOG_FMT_SSL_VERSION: // %sslv
1436 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001437 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001438 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001439 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001440 src = ssl_sock_get_proto_version(conn);
1441 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001442 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1443 if (ret == NULL)
1444 goto out;
1445 tmplog = ret;
1446 last_isspace = 0;
1447 break;
1448#endif
William Lallemand1d705562012-03-12 12:46:41 +01001449 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001450 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001451 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001452 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001453 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001454 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001455 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001456 break;
1457
William Lallemand1d705562012-03-12 12:46:41 +01001458 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001459 switch (obj_type(s->target)) {
1460 case OBJ_TYPE_SERVER:
1461 src = objt_server(s->target)->id;
1462 break;
1463 case OBJ_TYPE_APPLET:
1464 src = objt_applet(s->target)->name;
1465 break;
1466 default:
1467 src = "<NOSRV>";
1468 break;
1469 }
William Lallemand5f232402012-04-05 18:02:55 +02001470 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001471 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001472 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001473 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001474 last_isspace = 0;
1475 break;
1476
William Lallemand1d705562012-03-12 12:46:41 +01001477 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001478 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001479 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001480 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001481 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001482 last_isspace = 0;
1483 break;
1484
William Lallemand1d705562012-03-12 12:46:41 +01001485 case LOG_FMT_TW: // %Tw
1486 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001487 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001488 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001489 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001490 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001491 last_isspace = 0;
1492 break;
1493
William Lallemand1d705562012-03-12 12:46:41 +01001494 case LOG_FMT_TC: // %Tc
1495 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001496 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001497 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001498 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001499 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001500 last_isspace = 0;
1501 break;
1502
William Lallemand1d705562012-03-12 12:46:41 +01001503 case LOG_FMT_TR: // %Tr
1504 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001505 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001506 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001507 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001508 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001509 last_isspace = 0;
1510 break;
1511
William Lallemand1d705562012-03-12 12:46:41 +01001512 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001513 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001514 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001515 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001516 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001517 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001518 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001519 last_isspace = 0;
1520 break;
1521
Willy Tarreau2beef582012-12-20 17:22:52 +01001522 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001523 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001524 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001525 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001526 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001527 last_isspace = 0;
1528 break;
1529
William Lallemand1d705562012-03-12 12:46:41 +01001530 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001531 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001532 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001533 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001534 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001535 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001536 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001537 last_isspace = 0;
1538 break;
1539
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001540 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001541 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1542 if (ret == NULL)
1543 goto out;
1544 tmplog = ret;
1545 last_isspace = 0;
1546 break;
1547
Willy Tarreau2beef582012-12-20 17:22:52 +01001548 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001549 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001550 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001551 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001552 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001553 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001554 last_isspace = 0;
1555 break;
1556
Willy Tarreau2beef582012-12-20 17:22:52 +01001557 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001558 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001559 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001560 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001561 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001562 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001563 last_isspace = 0;
1564 break;
1565
William Lallemand1d705562012-03-12 12:46:41 +01001566 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001567 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1568 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001569 *tmplog = '\0';
1570 last_isspace = 0;
1571 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001572
William Lallemand1d705562012-03-12 12:46:41 +01001573 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001574 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1575 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001576 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1577 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001578 last_isspace = 0;
1579 break;
1580
William Lallemand1d705562012-03-12 12:46:41 +01001581 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001582 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001583 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001584 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001585 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001586 last_isspace = 0;
1587 break;
1588
William Lallemand1d705562012-03-12 12:46:41 +01001589 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001590 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001591 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001592 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001593 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001594 last_isspace = 0;
1595 break;
1596
William Lallemand1d705562012-03-12 12:46:41 +01001597 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001598 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001599 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001600 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001601 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001602 last_isspace = 0;
1603 break;
1604
William Lallemand1d705562012-03-12 12:46:41 +01001605 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001606 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001607 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001608 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001609 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001610 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001611 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001612 last_isspace = 0;
1613 break;
1614
William Lallemand1d705562012-03-12 12:46:41 +01001615 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001616 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001617 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001618 ret = ltoa_o((s->si[1].conn_retries>0) ?
1619 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001620 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001621 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001622 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001623 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001624 last_isspace = 0;
1625 break;
1626
William Lallemand1d705562012-03-12 12:46:41 +01001627 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001628 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001629 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001630 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001631 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001632 last_isspace = 0;
1633 break;
1634
William Lallemand1d705562012-03-12 12:46:41 +01001635 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001636 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001637 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001638 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001639 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001640 last_isspace = 0;
1641 break;
1642
William Lallemand1d705562012-03-12 12:46:41 +01001643 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001644 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001645 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001646 if (tmp->options & LOG_OPT_QUOTE)
1647 LOGCHAR('"');
1648 LOGCHAR('{');
1649 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1650 if (hdr)
1651 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001652 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001653 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001654 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001655 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001656 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001657 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001658 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001659 }
1660 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001661 if (tmp->options & LOG_OPT_QUOTE)
1662 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001663 last_isspace = 0;
1664 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001665 break;
1666
William Lallemand1d705562012-03-12 12:46:41 +01001667 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001668 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001669 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001670 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1671 if (hdr > 0)
1672 LOGCHAR(' ');
1673 if (tmp->options & LOG_OPT_QUOTE)
1674 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001675 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001676 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001677 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001678 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001679 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001680 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001681 } else if (!(tmp->options & LOG_OPT_QUOTE))
1682 LOGCHAR('-');
1683 if (tmp->options & LOG_OPT_QUOTE)
1684 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001685 last_isspace = 0;
1686 }
1687 }
1688 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001689
William Lallemand1d705562012-03-12 12:46:41 +01001690
1691 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001692 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001693 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001694 if (tmp->options & LOG_OPT_QUOTE)
1695 LOGCHAR('"');
1696 LOGCHAR('{');
1697 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1698 if (hdr)
1699 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001700 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001701 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001702 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001703 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001704 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001705 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001706 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001707 }
1708 LOGCHAR('}');
1709 last_isspace = 0;
1710 if (tmp->options & LOG_OPT_QUOTE)
1711 LOGCHAR('"');
1712 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001713 break;
1714
William Lallemand1d705562012-03-12 12:46:41 +01001715 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001716 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001717 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001718 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1719 if (hdr > 0)
1720 LOGCHAR(' ');
1721 if (tmp->options & LOG_OPT_QUOTE)
1722 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001723 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001724 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001725 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001726 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001727 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001728 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001729 } else if (!(tmp->options & LOG_OPT_QUOTE))
1730 LOGCHAR('-');
1731 if (tmp->options & LOG_OPT_QUOTE)
1732 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001733 last_isspace = 0;
1734 }
1735 }
1736 break;
1737
William Lallemand1d705562012-03-12 12:46:41 +01001738 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001739 /* Request */
1740 if (tmp->options & LOG_OPT_QUOTE)
1741 LOGCHAR('"');
1742 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001743 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001744 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001745 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001746 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001747 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001748 if (tmp->options & LOG_OPT_QUOTE)
1749 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001750 last_isspace = 0;
1751 break;
William Lallemand5f232402012-04-05 18:02:55 +02001752
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001753 case LOG_FMT_HTTP_PATH: // %HP
1754 uri = txn->uri ? txn->uri : "<BADREQ>";
1755
Willy Tarreaub7636d12015-06-17 19:58:02 +02001756 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001757 LOGCHAR('"');
1758
1759 end = uri + strlen(uri);
1760 // look for the first whitespace character
1761 while (uri < end && !HTTP_IS_SPHT(*uri))
1762 uri++;
1763
1764 // keep advancing past multiple spaces
1765 while (uri < end && HTTP_IS_SPHT(*uri)) {
1766 uri++; nspaces++;
1767 }
1768
1769 // look for first space or question mark after url
1770 spc = uri;
1771 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
1772 spc++;
1773
1774 if (!txn->uri || nspaces == 0) {
1775 chunk.str = "<BADREQ>";
1776 chunk.len = strlen("<BADREQ>");
1777 } else {
1778 chunk.str = uri;
1779 chunk.len = spc - uri;
1780 }
1781
1782 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1783 if (ret == NULL || *ret != '\0')
1784 goto out;
1785
1786 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001787 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001788 LOGCHAR('"');
1789
1790 last_isspace = 0;
1791 break;
1792
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001793 case LOG_FMT_HTTP_QUERY: // %HQ
1794 if (tmp->options & LOG_OPT_QUOTE)
1795 LOGCHAR('"');
1796
1797 if (!txn->uri) {
1798 chunk.str = "<BADREQ>";
1799 chunk.len = strlen("<BADREQ>");
1800 } else {
1801 uri = txn->uri;
1802 end = uri + strlen(uri);
1803 // look for the first question mark
1804 while (uri < end && *uri != '?')
1805 uri++;
1806
1807 qmark = uri;
1808 // look for first space or question mark after url
1809 while (uri < end && !HTTP_IS_SPHT(*uri))
1810 uri++;
1811
1812 chunk.str = qmark;
1813 chunk.len = uri - qmark;
1814 }
1815
1816 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1817 if (ret == NULL || *ret != '\0')
1818 goto out;
1819
1820 tmplog = ret;
1821 if (tmp->options & LOG_OPT_QUOTE)
1822 LOGCHAR('"');
1823
1824 last_isspace = 0;
1825 break;
1826
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001827 case LOG_FMT_HTTP_URI: // %HU
1828 uri = txn->uri ? txn->uri : "<BADREQ>";
1829
Willy Tarreaub7636d12015-06-17 19:58:02 +02001830 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001831 LOGCHAR('"');
1832
1833 end = uri + strlen(uri);
1834 // look for the first whitespace character
1835 while (uri < end && !HTTP_IS_SPHT(*uri))
1836 uri++;
1837
1838 // keep advancing past multiple spaces
1839 while (uri < end && HTTP_IS_SPHT(*uri)) {
1840 uri++; nspaces++;
1841 }
1842
1843 // look for first space after url
1844 spc = uri;
1845 while (spc < end && !HTTP_IS_SPHT(*spc))
1846 spc++;
1847
1848 if (!txn->uri || nspaces == 0) {
1849 chunk.str = "<BADREQ>";
1850 chunk.len = strlen("<BADREQ>");
1851 } else {
1852 chunk.str = uri;
1853 chunk.len = spc - uri;
1854 }
1855
1856 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1857 if (ret == NULL || *ret != '\0')
1858 goto out;
1859
1860 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001861 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001862 LOGCHAR('"');
1863
1864 last_isspace = 0;
1865 break;
1866
1867 case LOG_FMT_HTTP_METHOD: // %HM
1868 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001869 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001870 LOGCHAR('"');
1871
1872 end = uri + strlen(uri);
1873 // look for the first whitespace character
1874 spc = uri;
1875 while (spc < end && !HTTP_IS_SPHT(*spc))
1876 spc++;
1877
1878 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
1879 chunk.str = "<BADREQ>";
1880 chunk.len = strlen("<BADREQ>");
1881 } else {
1882 chunk.str = uri;
1883 chunk.len = spc - uri;
1884 }
1885
1886 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1887 if (ret == NULL || *ret != '\0')
1888 goto out;
1889
1890 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001891 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001892 LOGCHAR('"');
1893
1894 last_isspace = 0;
1895 break;
1896
1897 case LOG_FMT_HTTP_VERSION: // %HV
1898 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001899 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001900 LOGCHAR('"');
1901
1902 end = uri + strlen(uri);
1903 // look for the first whitespace character
1904 while (uri < end && !HTTP_IS_SPHT(*uri))
1905 uri++;
1906
1907 // keep advancing past multiple spaces
1908 while (uri < end && HTTP_IS_SPHT(*uri)) {
1909 uri++; nspaces++;
1910 }
1911
1912 // look for the next whitespace character
1913 while (uri < end && !HTTP_IS_SPHT(*uri))
1914 uri++;
1915
1916 // keep advancing past multiple spaces
1917 while (uri < end && HTTP_IS_SPHT(*uri))
1918 uri++;
1919
1920 if (!txn->uri || nspaces == 0) {
1921 chunk.str = "<BADREQ>";
1922 chunk.len = strlen("<BADREQ>");
1923 } else if (uri == end) {
1924 chunk.str = "HTTP/0.9";
1925 chunk.len = strlen("HTTP/0.9");
1926 } else {
1927 chunk.str = uri;
1928 chunk.len = end - uri;
1929 }
1930
1931 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1932 if (ret == NULL || *ret != '\0')
1933 goto out;
1934
1935 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001936 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001937 LOGCHAR('"');
1938
1939 last_isspace = 0;
1940 break;
1941
William Lallemand5f232402012-04-05 18:02:55 +02001942 case LOG_FMT_COUNTER: // %rt
1943 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001944 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001945 if (iret < 0 || iret > dst + maxsize - tmplog)
1946 goto out;
1947 last_isspace = 0;
1948 tmplog += iret;
1949 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001950 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001951 if (ret == NULL)
1952 goto out;
1953 tmplog = ret;
1954 last_isspace = 0;
1955 }
1956 break;
1957
Willy Tarreau7346acb2014-08-28 15:03:15 +02001958 case LOG_FMT_LOGCNT: // %lc
1959 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001960 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001961 if (iret < 0 || iret > dst + maxsize - tmplog)
1962 goto out;
1963 last_isspace = 0;
1964 tmplog += iret;
1965 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001966 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001967 if (ret == NULL)
1968 goto out;
1969 tmplog = ret;
1970 last_isspace = 0;
1971 }
1972 break;
1973
William Lallemand5f232402012-04-05 18:02:55 +02001974 case LOG_FMT_HOSTNAME: // %H
1975 src = hostname;
1976 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1977 if (ret == NULL)
1978 goto out;
1979 tmplog = ret;
1980 last_isspace = 0;
1981 break;
1982
1983 case LOG_FMT_PID: // %pid
1984 if (tmp->options & LOG_OPT_HEXA) {
1985 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1986 if (iret < 0 || iret > dst + maxsize - tmplog)
1987 goto out;
1988 last_isspace = 0;
1989 tmplog += iret;
1990 } else {
1991 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1992 if (ret == NULL)
1993 goto out;
1994 tmplog = ret;
1995 last_isspace = 0;
1996 }
1997 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001998
1999 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002000 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01002001 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002002 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002003 if (ret == NULL)
2004 goto out;
2005 tmplog = ret;
2006 last_isspace = 0;
2007 break;
2008
William Lallemandbddd4fd2012-02-27 11:23:10 +01002009 }
2010 }
2011
2012out:
William Lallemand1d705562012-03-12 12:46:41 +01002013 /* *tmplog is a unused character */
2014 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002015 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002016
Willy Tarreaubaaee002006-06-26 02:48:02 +02002017}
2018
William Lallemand1d705562012-03-12 12:46:41 +01002019/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002020 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002021 * Will not log if the frontend has no log defined.
2022 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002023void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002024{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002025 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002026 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002027 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002028
2029 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002030 err = (s->flags & SF_REDISP) ||
2031 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2032 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002033 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002034 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002035
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002036 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002037 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002038
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002039 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002040 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002041
Willy Tarreauabcd5142013-06-11 17:18:02 +02002042 if (s->logs.level) { /* loglevel was overridden */
2043 if (s->logs.level == -1) {
2044 s->logs.logwait = 0; /* logs disabled */
2045 return;
2046 }
2047 level = s->logs.level - 1;
2048 }
2049 else {
2050 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002051 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002052 level = LOG_ERR;
2053 }
William Lallemand1d705562012-03-12 12:46:41 +01002054
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002055 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002056 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002057 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002058 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002059 }
2060
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002061 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2062 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2063 &sess->fe->logformat_sd);
2064 }
2065
Dragan Dosen59cee972015-09-19 22:09:02 +02002066 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002067 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002068 sess->fe->log_count++;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002069 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002070 s->logs.logwait = 0;
2071 }
2072}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002073
Willy Tarreaubaaee002006-06-26 02:48:02 +02002074/*
2075 * Local variables:
2076 * c-indent-level: 8
2077 * c-basic-offset: 8
2078 * End:
2079 */