blob: 3e25bc79d612ed347ad9cc2f40c21fe059fbfd82 [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
Dragan Dosen835b9212016-02-12 13:23:03 +010069#define FD_SETS_ARE_BITFIELDS
70#ifdef FD_SETS_ARE_BITFIELDS
71/*
72 * This map is used with all the FD_* macros to check whether a particular bit
73 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
74 * which should be escaped. When FD_ISSET() returns non-zero, it means that the
75 * byte should be escaped. Be careful to always pass bytes from 0 to 255
76 * exclusively to the macros.
77 */
78fd_set rfc5424_escape_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
79
80#else
81#error "Check if your OS uses bitfields for fd_sets"
82#endif
83
Willy Tarreaubaaee002006-06-26 02:48:02 +020084const char *log_facilities[NB_LOG_FACILITIES] = {
85 "kern", "user", "mail", "daemon",
86 "auth", "syslog", "lpr", "news",
87 "uucp", "cron", "auth2", "ftp",
88 "ntp", "audit", "alert", "cron2",
89 "local0", "local1", "local2", "local3",
90 "local4", "local5", "local6", "local7"
91};
92
Willy Tarreaubaaee002006-06-26 02:48:02 +020093const char *log_levels[NB_LOG_LEVELS] = {
94 "emerg", "alert", "crit", "err",
95 "warning", "notice", "info", "debug"
96};
97
Willy Tarreau570f2212013-06-10 16:42:09 +020098const 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 +020099const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100
William Lallemand723b73a2012-02-08 16:37:49 +0100101
102/* log_format */
103struct logformat_type {
104 char *name;
105 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100106 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200107 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100108 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100109 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100110};
111
William Lallemandb7ff6a32012-03-02 14:35:21 +0100112int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
113
William Lallemand723b73a2012-02-08 16:37:49 +0100114/* log_format variable names */
115static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200116 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100117
118 /* please keep these lines sorted ! */
119 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
120 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
121 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
122 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
123 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200124 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200125 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +0100126 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +0800127 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +0200128 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +0200129 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +0100130 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200131 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +0100132 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
133 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200134 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100135 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200136 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100137 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
138 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200139 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +0100140 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
141 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
142 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
143 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
144 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
145 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
146 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200147 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
148 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
149 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
150 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000151 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
152 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000153 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000154 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
155 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200156 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100157 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200158 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100159 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
160 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100161 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100162 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
163 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
164 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
165 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
166 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200167 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
168 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100169 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
170 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
171 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
172
173 /* The following tags are deprecated and will be removed soon */
174 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
175 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
176 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
177 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
178 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
179 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
180 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
181 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
182 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
183 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
184 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200185 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100186};
187
Willy Tarreau2beef582012-12-20 17:22:52 +0100188char 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
189char 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";
190char 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 +0100191char *log_format = NULL;
192
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200193/* Default string used for structured-data part in RFC5424 formatted
194 * syslog messages.
195 */
196char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200197
198/* This is a global syslog header, common to all outgoing messages in
199 * RFC3164 format. It begins with time-based part and is updated by
200 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200201 */
202char *logheader = NULL;
203
Dragan Dosen1322d092015-09-22 16:05:32 +0200204/* This is a global syslog header for messages in RFC5424 format. It is
205 * updated by update_log_hdr_rfc5424().
206 */
207char *logheader_rfc5424 = NULL;
208
Dragan Dosen59cee972015-09-19 22:09:02 +0200209/* This is a global syslog message buffer, common to all outgoing
210 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100211 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200212char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100213
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200214/* A global syslog message buffer, common to all RFC5424 syslog messages.
215 * Currently, it is used for generating the structured-data part.
216 */
217char *logline_rfc5424 = NULL;
218
William Lallemand723b73a2012-02-08 16:37:49 +0100219struct logformat_var_args {
220 char *name;
221 int mask;
222};
223
224struct logformat_var_args var_args_list[] = {
225// global
226 { "M", LOG_OPT_MANDATORY },
227 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200228 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100229 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100230 { 0, 0 }
231};
232
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200233/* return the name of the directive used in the current proxy for which we're
234 * currently parsing a header, when it is known.
235 */
236static inline const char *fmt_directive(const struct proxy *curproxy)
237{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100238 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200239 case ARGC_ACL:
240 return "acl";
241 case ARGC_STK:
242 return "stick";
243 case ARGC_TRK:
244 return "track-sc";
245 case ARGC_LOG:
246 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200247 case ARGC_LOGSD:
248 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100249 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100250 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100251 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100252 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200253 case ARGC_UIF:
254 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100255 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200256 return "redirect";
257 case ARGC_CAP:
258 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200259 case ARGC_SRV:
260 return "server";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100261 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200262 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100263 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200264}
265
William Lallemand723b73a2012-02-08 16:37:49 +0100266/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100267 * callback used to configure addr source retrieval
268 */
269int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
270{
271 curproxy->options2 |= PR_O2_SRC_ADDR;
272
273 return 0;
274}
275
276
277/*
William Lallemand723b73a2012-02-08 16:37:49 +0100278 * Parse args in a logformat_var
279 */
280int parse_logformat_var_args(char *args, struct logformat_node *node)
281{
282 int i = 0;
283 int end = 0;
284 int flags = 0; // 1 = + 2 = -
285 char *sp = NULL; // start pointer
286
287 if (args == NULL)
288 return 1;
289
290 while (1) {
291 if (*args == '\0')
292 end = 1;
293
294 if (*args == '+') {
295 // add flag
296 sp = args + 1;
297 flags = 1;
298 }
299 if (*args == '-') {
300 // delete flag
301 sp = args + 1;
302 flags = 2;
303 }
304
305 if (*args == '\0' || *args == ',') {
306 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100307 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100308 if (strcmp(sp, var_args_list[i].name) == 0) {
309 if (flags == 1) {
310 node->options |= var_args_list[i].mask;
311 break;
312 } else if (flags == 2) {
313 node->options &= ~var_args_list[i].mask;
314 break;
315 }
316 }
317 }
318 sp = NULL;
319 if (end)
320 break;
321 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100322 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100323 }
324 return 0;
325}
326
327/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100328 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
329 * must pass the args part in the <arg> pointer with its length in <arg_len>,
330 * and varname with its length in <var> and <var_len> respectively. <arg> is
331 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100332 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100333int 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 +0100334{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100335 int j;
336 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100337
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100338 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
339 if (strlen(logformat_keywords[j].name) == var_len &&
340 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
341 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
342 node = calloc(1, sizeof(struct logformat_node));
343 node->type = logformat_keywords[j].type;
344 node->options = *defoptions;
345 if (arg_len) {
346 node->arg = my_strndup(arg, arg_len);
347 parse_logformat_var_args(node->arg, node);
348 }
349 if (node->type == LOG_FMT_GLOBAL) {
350 *defoptions = node->options;
351 free(node->arg);
352 free(node);
353 } else {
354 if (logformat_keywords[j].config_callback &&
355 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100356 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100357 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100358 curproxy->to_log |= logformat_keywords[j].lw;
359 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100360 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100361 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100362 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200363 curproxy->conf.args.file, curproxy->conf.args.line,
364 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100365 return 0;
366 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100367 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200368 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
369 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100370 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100371 }
William Lallemand723b73a2012-02-08 16:37:49 +0100372 }
373 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100374
375 j = var[var_len];
376 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100377 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 +0200378 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100379 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100380 return -1;
381}
382
383/*
384 * push to the logformat linked list
385 *
386 * start: start pointer
387 * end: end text pointer
388 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100389 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100390 *
391 * LOG_TEXT: copy chars from start to end excluding end.
392 *
393*/
William Lallemand1d705562012-03-12 12:46:41 +0100394void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100395{
396 char *str;
397
Willy Tarreaua3571662012-12-20 21:59:12 +0100398 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100399 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100400 str = calloc(end - start + 1, 1);
401 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100402 str[end - start] = '\0';
403 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100404 node->type = LOG_FMT_TEXT; // type string
405 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100406 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100407 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100408 node->type = LOG_FMT_SEPARATOR;
409 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100410 }
411}
412
413/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100414 * Parse the sample fetch expression <text> and add a node to <list_format> upon
415 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200416 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100417 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100418void 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 +0100419{
420 char *cmd[2];
421 struct sample_expr *expr;
422 struct logformat_node *node;
423 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100424 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100425
426 cmd[0] = text;
427 cmd[1] = "";
428 cmd_arg = 0;
429
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100430 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100431 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200432 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
433 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100434 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100435 return;
436 }
437
438 node = calloc(1, sizeof(struct logformat_node));
439 node->type = LOG_FMT_EXPR;
440 node->expr = expr;
441 node->options = options;
442
443 if (arg_len) {
444 node->arg = my_strndup(arg, arg_len);
445 parse_logformat_var_args(node->arg, node);
446 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100447 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100448 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
449
Willy Tarreau434c57c2013-01-08 01:10:24 +0100450 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100451 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
452
Willy Tarreau434c57c2013-01-08 01:10:24 +0100453 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200454 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
455 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
456 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100457
Willy Tarreauc8368452012-12-21 00:09:23 +0100458 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
459 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100460 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100461
William Lallemand65ad6e12014-01-31 15:08:02 +0100462 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
463 * needed with some sample fetches (eg: ssl*). We always set it for
464 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100465 */
466 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100467 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100468 LIST_ADDQ(list_format, &node->list);
469}
470
471/*
William Lallemand723b73a2012-02-08 16:37:49 +0100472 * Parse the log_format string and fill a linked list.
473 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200474 * You can set arguments using { } : %{many arguments}varname.
475 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100476 *
477 * str: the string to parse
478 * curproxy: the proxy affected
479 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100480 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100481 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100482 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100483void 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 +0100484{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100485 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100486 char *arg = NULL; /* start pointer for args */
487 char *var = NULL; /* start pointer for vars */
488 int arg_len = 0;
489 int var_len = 0;
490 int cformat; /* current token format */
491 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100492 struct logformat_node *tmplf, *back;
493
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100494 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200495 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200496
William Lallemand723b73a2012-02-08 16:37:49 +0100497 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100498 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100499 LIST_DEL(&tmplf->list);
500 free(tmplf);
501 }
502
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100503 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100504 pformat = cformat;
505
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100506 if (!*str)
507 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100508
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100509 /* The prinicple of the two-step state machine below is to first detect a change, and
510 * second have all common paths processed at one place. The common paths are the ones
511 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
512 * We use the common LF_INIT state to dispatch to the different final states.
513 */
514 switch (pformat) {
515 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100516 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100517 arg_len = var_len = 0;
518 if (*str == '{') { // optional argument
519 cformat = LF_STARG;
520 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100521 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100522 else if (*str == '[') {
523 cformat = LF_STEXPR;
524 var = str + 1; // store expr in variable name
525 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100526 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100527 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100528 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100529 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100530 else if (*str == '%')
531 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100532 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100533 /* single '%' followed by blank or digit, send them both */
534 cformat = LF_TEXT;
535 pformat = LF_TEXT; /* finally we include the previous char as well */
536 sp = str - 1; /* send both the '%' and the current char */
537 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",
538 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
539
540 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100541 else
542 cformat = LF_INIT; // handle other cases of litterals
543 break;
544
545 case LF_STARG: // text immediately following '%{'
546 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100547 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100548 arg_len = str - arg;
549 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100550 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100551 break;
552
553 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100554 if (*str == '[') {
555 cformat = LF_STEXPR;
556 var = str + 1; // store expr in variable name
557 break;
558 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100559 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100560 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100561 var = str;
562 break;
563 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200564 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
565 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100566 cformat = LF_INIT;
567 break;
568
Willy Tarreauc8368452012-12-21 00:09:23 +0100569 case LF_STEXPR: // text immediately following '%['
570 if (*str == ']') { // end of arg
571 cformat = LF_EDEXPR;
572 var_len = str - var;
573 *str = 0; // needed for parsing the expression
574 }
575 break;
576
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100577 case LF_VAR: // text part of a variable name
578 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100579 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100580 cformat = LF_INIT; // not variable name anymore
581 break;
582
Willy Tarreauc8368452012-12-21 00:09:23 +0100583 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100584 cformat = LF_INIT;
585 }
586
587 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
588 switch (*str) {
589 case '%': cformat = LF_STARTVAR; break;
590 case ' ': cformat = LF_SEPARATOR; break;
591 case 0 : cformat = LF_END; break;
592 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100593 }
594 }
595
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100596 if (cformat != pformat || pformat == LF_SEPARATOR) {
597 switch (pformat) {
598 case LF_VAR:
599 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
600 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100601 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100602 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100603 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100604 case LF_TEXT:
605 case LF_SEPARATOR:
606 add_to_logformat_list(sp, str, pformat, list_format);
607 break;
608 }
609 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100610 }
611 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100612
Willy Tarreauc8368452012-12-21 00:09:23 +0100613 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200614 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
615 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
616 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100617
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100618 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100619}
620
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621/*
622 * Displays the message on stderr with the date and pid. Overrides the quiet
623 * mode during startup.
624 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200625void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200626{
627 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200628 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629
630 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
631 va_start(argp, fmt);
632
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200633 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200635 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636 vfprintf(stderr, fmt, argp);
637 fflush(stderr);
638 va_end(argp);
639 }
640}
641
642
643/*
644 * Displays the message on stderr with the date and pid.
645 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200646void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647{
648 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200649 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650
651 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
652 va_start(argp, fmt);
653
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200654 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200656 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657 vfprintf(stderr, fmt, argp);
658 fflush(stderr);
659 va_end(argp);
660 }
661}
662
663/*
664 * Displays the message on <out> only if quiet mode is not set.
665 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200666void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667{
668 va_list argp;
669
670 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
671 va_start(argp, fmt);
672 vfprintf(out, fmt, argp);
673 fflush(out);
674 va_end(argp);
675 }
676}
677
678/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200679 * returns log format for <fmt> or -1 if not found.
680 */
681int get_log_format(const char *fmt)
682{
683 int format;
684
685 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200686 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200687 format--;
688
689 return format;
690}
691
692/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693 * returns log level for <lev> or -1 if not found.
694 */
695int get_log_level(const char *lev)
696{
697 int level;
698
699 level = NB_LOG_LEVELS - 1;
700 while (level >= 0 && strcmp(log_levels[level], lev))
701 level--;
702
703 return level;
704}
705
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706/*
707 * returns log facility for <fac> or -1 if not found.
708 */
709int get_log_facility(const char *fac)
710{
711 int facility;
712
713 facility = NB_LOG_FACILITIES - 1;
714 while (facility >= 0 && strcmp(log_facilities[facility], fac))
715 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100716
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717 return facility;
718}
719
William Lallemanda1cc3812012-02-08 16:38:44 +0100720/*
Dragan Dosen835b9212016-02-12 13:23:03 +0100721 * Encode the string.
722 *
723 * When using the +E log format option, it will try to escape '"\]'
724 * characters with '\' as prefix. The same prefix should not be used as
725 * <escape>.
726 */
727static char *lf_encode_string(char *start, char *stop,
728 const char escape, const fd_set *map,
729 const char *string,
730 struct logformat_node *node)
731{
732 if (node->options & LOG_OPT_ESC) {
733 if (start < stop) {
734 stop--; /* reserve one byte for the final '\0' */
735 while (start < stop && *string != '\0') {
736 if (!FD_ISSET((unsigned char)(*string), map)) {
737 if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map))
738 *start++ = *string;
739 else {
740 if (start + 2 >= stop)
741 break;
742 *start++ = '\\';
743 *start++ = *string;
744 }
745 }
746 else {
747 if (start + 3 >= stop)
748 break;
749 *start++ = escape;
750 *start++ = hextab[(*string >> 4) & 15];
751 *start++ = hextab[*string & 15];
752 }
753 string++;
754 }
755 *start = '\0';
756 }
757 }
758 else {
759 return encode_string(start, stop, escape, map, string);
760 }
761
762 return start;
763}
764
765/*
766 * Encode the chunk.
767 *
768 * When using the +E log format option, it will try to escape '"\]'
769 * characters with '\' as prefix. The same prefix should not be used as
770 * <escape>.
771 */
772static char *lf_encode_chunk(char *start, char *stop,
773 const char escape, const fd_set *map,
774 const struct chunk *chunk,
775 struct logformat_node *node)
776{
777 char *str, *end;
778
779 if (node->options & LOG_OPT_ESC) {
780 if (start < stop) {
781 str = chunk->str;
782 end = chunk->str + chunk->len;
783
784 stop--; /* reserve one byte for the final '\0' */
785 while (start < stop && str < end) {
786 if (!FD_ISSET((unsigned char)(*str), map)) {
787 if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map))
788 *start++ = *str;
789 else {
790 if (start + 2 >= stop)
791 break;
792 *start++ = '\\';
793 *start++ = *str;
794 }
795 }
796 else {
797 if (start + 3 >= stop)
798 break;
799 *start++ = escape;
800 *start++ = hextab[(*str >> 4) & 15];
801 *start++ = hextab[*str & 15];
802 }
803 str++;
804 }
805 *start = '\0';
806 }
807 }
808 else {
809 return encode_chunk(start, stop, escape, map, chunk);
810 }
811
812 return start;
813}
814
815/*
William Lallemanda1cc3812012-02-08 16:38:44 +0100816 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +0100817 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +0100818 *
819 * Return the adress of the \0 character, or NULL on error
820 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100821char *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 +0100822{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100823 if (size < 2)
824 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100825
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100826 if (node->options & LOG_OPT_QUOTE) {
827 *(dst++) = '"';
828 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100829 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100830
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100831 if (src && len) {
Dragan Dosen835b9212016-02-12 13:23:03 +0100832 if (node->options & LOG_OPT_ESC) {
833 struct chunk chunk;
834 char *ret;
835
836 chunk_initlen(&chunk, (char *)src, 0, len);
837 ret = escape_chunk(dst, dst + size, '\\', rfc5424_escape_map, &chunk);
838 if (ret == NULL || *ret != '\0')
839 return NULL;
840 len = ret - dst;
841 }
842 else {
843 if (++len > size)
844 len = size;
845 len = strlcpy2(dst, src, len);
846 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100847
848 size -= len;
849 dst += len;
850 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100851 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
852 if (size < 2)
853 return NULL;
854 *(dst++) = '-';
855 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100856
857 if (node->options & LOG_OPT_QUOTE) {
858 if (size < 2)
859 return NULL;
860 *(dst++) = '"';
861 }
862
863 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100864 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100865}
866
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100867static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
868{
869 return lf_text_len(dst, src, size, size, node);
870}
871
William Lallemand5f232402012-04-05 18:02:55 +0200872/*
873 * Write a IP adress to the log string
874 * +X option write in hexadecimal notation, most signifant byte on the left
875 */
876char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
877{
878 char *ret = dst;
879 int iret;
880 char pn[INET6_ADDRSTRLEN];
881
882 if (node->options & LOG_OPT_HEXA) {
883 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
884 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
885 if (iret < 0 || iret > size)
886 return NULL;
887 ret += iret;
888 } else {
889 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
890 ret = lf_text(dst, pn, size, node);
891 if (ret == NULL)
892 return NULL;
893 }
894 return ret;
895}
896
897/*
898 * Write a port to the log
899 * +X option write in hexadecimal notation, most signifant byte on the left
900 */
901char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
902{
903 char *ret = dst;
904 int iret;
905
906 if (node->options & LOG_OPT_HEXA) {
907 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
908 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
909 if (iret < 0 || iret > size)
910 return NULL;
911 ret += iret;
912 } else {
913 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
914 if (ret == NULL)
915 return NULL;
916 }
917 return ret;
918}
919
Dragan Dosen1322d092015-09-22 16:05:32 +0200920/* Re-generate time-based part of the syslog header in RFC3164 format at
921 * the beginning of logheader once a second and return the pointer to the
922 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100923 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200924static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100926 static long tvsec;
927 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Dragan Dosen43885c72015-10-01 13:18:13 +0200928 static struct chunk host = { NULL, 0, 0 };
929 static int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200931 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200933 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100934 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200935
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200936 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +0200937 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938
Dragan Dosen43885c72015-10-01 13:18:13 +0200939 if (unlikely(global.log_send_hostname != host.str)) {
940 host.str = global.log_send_hostname;
941 host.len = host.str ? strlen(host.str) : 0;
942 sep = host.len ? 1 : 0;
943 }
944
Dragan Dosen59cee972015-09-19 22:09:02 +0200945 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +0200946 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200947 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +0200948 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
949 host.len, host.str, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950 /* WARNING: depending upon implementations, snprintf may return
951 * either -1 or the number of bytes that would be needed to store
952 * the total message. In both cases, we must adjust it.
953 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200954 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
955 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200956
Dragan Dosen59cee972015-09-19 22:09:02 +0200957 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958 }
959
Willy Tarreau094af4e2015-01-07 15:03:42 +0100960 dataptr[0] = 0; // ensure we get rid of any previous attempt
961
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200962 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100963}
964
Dragan Dosen1322d092015-09-22 16:05:32 +0200965/* Re-generate time-based part of the syslog header in RFC5424 format at
966 * the beginning of logheader_rfc5424 once a second and return the pointer
967 * to the first character after it.
968 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200969static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +0200970{
971 static long tvsec;
972 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
973
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200974 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +0200975 /* this string is rebuild only once a second */
976 struct tm tm;
977 int hdr_len;
978
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200979 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +0200980 get_localtime(tvsec, &tm);
981
982 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +0200983 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +0200984 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +0200985 tm.tm_hour, tm.tm_min, tm.tm_sec,
986 localtimezone, localtimezone+3,
Dragan Dosen43885c72015-10-01 13:18:13 +0200987 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +0200988 /* WARNING: depending upon implementations, snprintf may return
989 * either -1 or the number of bytes that would be needed to store
990 * the total message. In both cases, we must adjust it.
991 */
992 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
993 hdr_len = global.max_syslog_len;
994
995 dataptr = logheader_rfc5424 + hdr_len;
996 }
997
998 dataptr[0] = 0; // ensure we get rid of any previous attempt
999
1000 return dataptr;
1001}
1002
William Lallemand2a4a44f2012-02-06 16:00:33 +01001003/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001004 * This function sends the syslog message using a printf format string. It
1005 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001006 */
1007void send_log(struct proxy *p, int level, const char *format, ...)
1008{
1009 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001010 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001011
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001012 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001013 return;
1014
William Lallemand2a4a44f2012-02-06 16:00:33 +01001015 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001016 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001017 if (data_len < 0 || data_len > global.max_syslog_len)
1018 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001020
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001021 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001022}
1023
1024/*
1025 * This function sends a syslog message.
1026 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001027 * It overrides the last byte of the message vector with an LF character.
1028 * The arguments <sd> and <sd_size> are used for the structured-data part
1029 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001030 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001031void __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 +01001032{
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001033 static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1034 static struct msghdr msghdr = {
1035 .msg_iov = iovec,
1036 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1037 };
William Lallemand2a4a44f2012-02-06 16:00:33 +01001038 static int logfdunix = -1; /* syslog to AF_UNIX socket */
1039 static int logfdinet = -1; /* syslog to AF_INET socket */
1040 static char *dataptr = NULL;
1041 int fac_level;
1042 struct list *logsrvs = NULL;
1043 struct logsrv *tmp = NULL;
1044 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +02001045 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001046 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001047 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001048 struct chunk *tag = &global.log_tag;
1049 static int curr_pid;
1050 static char pidstr[100];
1051 static struct chunk pid;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001052
1053 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001054
1055 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +02001056 if (!LIST_ISEMPTY(&global.logsrvs)) {
1057 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001058 }
1059 } else {
William Lallemand0f99e342011-10-12 17:50:54 +02001060 if (!LIST_ISEMPTY(&p->logsrvs)) {
1061 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001063 if (p->log_tag.str) {
1064 tag = &p->log_tag;
1065 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001066 }
1067
William Lallemand0f99e342011-10-12 17:50:54 +02001068 if (!logsrvs)
1069 return;
1070
Dragan Dosen43885c72015-10-01 13:18:13 +02001071 if (unlikely(curr_pid != getpid())) {
1072 curr_pid = getpid();
1073 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1074 chunk_initstr(&pid, pidstr);
1075 }
1076
Robert Tsai81ae1952007-12-05 10:47:29 +01001077 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +02001078 nblogger = 0;
1079 list_for_each_entry(tmp, logsrvs, list) {
1080 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +01001081 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +01001082 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +02001083 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +01001084 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001085 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +02001086 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001087 int tag_max = 0;
1088 int pid_sep1_max = 0;
1089 int pid_max = 0;
1090 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001091 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001092 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +01001093
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001094 nblogger++;
1095
Willy Tarreaubaaee002006-06-26 02:48:02 +02001096 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +02001097 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001099
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001100 if (unlikely(*plogfd < 0)) {
1101 /* socket not successfully initialized yet */
1102 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
1103
1104 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
1105 Alert("socket for logger #%d failed: %s (errno=%d)\n",
1106 nblogger, strerror(errno), errno);
1107 continue;
1108 }
1109 /* we don't want to receive anything on this socket */
1110 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1111 /* does nothing under Linux, maybe needed for others */
1112 shutdown(*plogfd, SHUT_RD);
1113 }
1114
Dragan Dosen1322d092015-09-22 16:05:32 +02001115 switch (logsrv->format) {
1116 case LOG_FORMAT_RFC3164:
1117 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001118 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +02001119 break;
1120
1121 case LOG_FORMAT_RFC5424:
1122 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001123 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001124 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001125 break;
1126
1127 default:
1128 continue; /* must never happen */
1129 }
1130
1131 hdr_size = hdr_ptr - hdr;
1132
Willy Tarreaubaaee002006-06-26 02:48:02 +02001133 /* For each target, we may have a different facility.
1134 * We can also have a different log level for each message.
1135 * This induces variations in the message header length.
1136 * Since we don't want to recompute it each time, nor copy it every
1137 * time, we only change the facility in the pre-computed header,
1138 * and we change the pointer to the header accordingly.
1139 */
William Lallemand0f99e342011-10-12 17:50:54 +02001140 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001141 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001142 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001143 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001144 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001145 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001146 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001147 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001148
Dragan Dosen1322d092015-09-22 16:05:32 +02001149 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001150
Dragan Dosen43885c72015-10-01 13:18:13 +02001151 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001152 if (unlikely(hdr_size >= logsrv->maxlen)) {
1153 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001154 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001155 goto send;
1156 }
1157
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001158 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001159
Dragan Dosen43885c72015-10-01 13:18:13 +02001160 /* tag */
1161 tag_max = tag->len;
1162 if (unlikely(tag_max >= maxlen)) {
1163 tag_max = maxlen - 1;
1164 sd_max = 0;
1165 goto send;
1166 }
1167
1168 maxlen -= tag_max;
1169
1170 /* first pid separator */
1171 pid_sep1_max = log_formats[logsrv->format].pid.sep1.len;
1172 if (unlikely(pid_sep1_max >= maxlen)) {
1173 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001174 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001175 goto send;
1176 }
1177
Dragan Dosen43885c72015-10-01 13:18:13 +02001178 pid_sep1 = log_formats[logsrv->format].pid.sep1.str;
1179 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001180
Dragan Dosen43885c72015-10-01 13:18:13 +02001181 /* pid */
1182 pid_max = pid.len;
1183 if (unlikely(pid_max >= maxlen)) {
1184 pid_max = maxlen - 1;
1185 sd_max = 0;
1186 goto send;
1187 }
1188
1189 maxlen -= pid_max;
1190
1191 /* second pid separator */
1192 pid_sep2_max = log_formats[logsrv->format].pid.sep2.len;
1193 if (unlikely(pid_sep2_max >= maxlen)) {
1194 pid_sep2_max = maxlen - 1;
1195 sd_max = 0;
1196 goto send;
1197 }
1198
1199 pid_sep2 = log_formats[logsrv->format].pid.sep2.str;
1200 maxlen -= pid_sep2_max;
1201
1202 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001203 if (sd_max >= maxlen) {
1204 sd_max = maxlen - 1;
1205 goto send;
1206 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001207
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001208 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001209send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001210 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001211 iovec[0].iov_len = hdr_max;
1212 iovec[1].iov_base = tag->str;
1213 iovec[1].iov_len = tag_max;
1214 iovec[2].iov_base = pid_sep1;
1215 iovec[2].iov_len = pid_sep1_max;
1216 iovec[3].iov_base = pid.str;
1217 iovec[3].iov_len = pid_max;
1218 iovec[4].iov_base = pid_sep2;
1219 iovec[4].iov_len = pid_sep2_max;
1220 iovec[5].iov_base = sd;
1221 iovec[5].iov_len = sd_max;
1222 iovec[6].iov_base = dataptr;
1223 iovec[6].iov_len = max;
1224 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1225 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001226
1227 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1228 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1229
1230 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001231
Robert Tsai81ae1952007-12-05 10:47:29 +01001232 if (sent < 0) {
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001233 Alert("sendmsg logger #%d failed: %s (errno=%d)\n",
Robert Tsai81ae1952007-12-05 10:47:29 +01001234 nblogger, strerror(errno), errno);
1235 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001236 }
1237}
1238
William Lallemandbddd4fd2012-02-27 11:23:10 +01001239extern fd_set hdr_encode_map[];
1240extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001241extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001242
Willy Tarreaubaaee002006-06-26 02:48:02 +02001243
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001244const 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 +01001245const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1246 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1247 Set-cookie Updated, unknown, unknown */
1248
William Lallemand1d705562012-03-12 12:46:41 +01001249/*
1250 * try to write a character if there is enough space, or goto out
1251 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001252#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001253 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001254 *(tmplog++) = (x); \
1255 } else { \
1256 goto out; \
1257 } \
1258 } while(0)
1259
Dragan Dosen835b9212016-02-12 13:23:03 +01001260
1261/* Initializes some log data.
1262 */
1263void init_log()
1264{
1265 char *tmp;
1266
1267 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1268 * inside PARAM-VALUE should be escaped with '\' as prefix.
1269 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1270 * details.
1271 */
1272 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1273
1274 tmp = "\"\\]";
1275 while (*tmp) {
1276 FD_SET(*tmp, rfc5424_escape_map);
1277 tmp++;
1278 }
1279}
William Lallemand1d705562012-03-12 12:46:41 +01001280
Willy Tarreaudf974472012-12-28 02:44:01 +01001281/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1282 * <maxsize> characters. Returns the size of the output string in characters,
1283 * not counting the trailing zero which is always added if the resulting size
1284 * is not zero.
1285 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001286int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001287{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001288 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001289 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +01001290 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001291 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001292 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001293 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001294 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001295 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001296 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001297 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001298 int t_request;
1299 int hdr;
1300 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001301 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001302 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001303 char *ret;
1304 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001305 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001306
William Lallemandbddd4fd2012-02-27 11:23:10 +01001307 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001308
William Lallemandbddd4fd2012-02-27 11:23:10 +01001309 t_request = -1;
1310 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1311 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1312
William Lallemand1d705562012-03-12 12:46:41 +01001313 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001314
William Lallemandbddd4fd2012-02-27 11:23:10 +01001315 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001316 if (LIST_ISEMPTY(list_format))
1317 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001318
William Lallemand1d705562012-03-12 12:46:41 +01001319 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001320 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001321 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001322 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001323 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001324
Willy Tarreauc8368452012-12-21 00:09:23 +01001325 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001326 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001327 if (!last_isspace) {
1328 LOGCHAR(' ');
1329 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001330 }
1331 break;
1332
William Lallemand1d705562012-03-12 12:46:41 +01001333 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001334 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001335 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001336 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001337 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001338 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001339 last_isspace = 0;
1340 break;
1341
Willy Tarreauc8368452012-12-21 00:09:23 +01001342 case LOG_FMT_EXPR: // sample expression, may be request or response
1343 key = NULL;
1344 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001345 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 +01001346 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001347 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 +01001348 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01001349 ret = lf_encode_chunk(tmplog, dst + maxsize,
1350 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001351 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001352 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 +01001353 if (ret == 0)
1354 goto out;
1355 tmplog = ret;
1356 last_isspace = 0;
1357 break;
1358
Willy Tarreau2beef582012-12-20 17:22:52 +01001359 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001360 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001361 if (conn)
1362 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1363 else
1364 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001365 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001366 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001367 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001368 last_isspace = 0;
1369 break;
1370
Willy Tarreau2beef582012-12-20 17:22:52 +01001371 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001372 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001373 if (conn) {
1374 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001375 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001376 } else {
1377 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1378 dst + maxsize - tmplog, tmp);
1379 }
William Lallemand5f232402012-04-05 18:02:55 +02001380 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001381 else
1382 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1383
William Lallemand5f232402012-04-05 18:02:55 +02001384 if (ret == NULL)
1385 goto out;
1386 tmplog = ret;
1387 last_isspace = 0;
1388 break;
1389
Willy Tarreau2beef582012-12-20 17:22:52 +01001390 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001391 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001392 if (conn) {
1393 conn_get_to_addr(conn);
1394 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1395 }
1396 else
1397 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1398
William Lallemand1d705562012-03-12 12:46:41 +01001399 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001400 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001401 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001402 last_isspace = 0;
1403 break;
1404
Willy Tarreau2beef582012-12-20 17:22:52 +01001405 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001406 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001407 if (conn) {
1408 conn_get_to_addr(conn);
1409 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001410 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001411 else
1412 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001413 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001414 else
1415 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1416
William Lallemand5f232402012-04-05 18:02:55 +02001417 if (ret == NULL)
1418 goto out;
1419 tmplog = ret;
1420 last_isspace = 0;
1421 break;
1422
Willy Tarreau2beef582012-12-20 17:22:52 +01001423 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001424 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001425 if (conn)
1426 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1427 else
1428 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1429
William Lallemand1d705562012-03-12 12:46:41 +01001430 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001431 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001432 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001433 last_isspace = 0;
1434 break;
1435
Willy Tarreau2beef582012-12-20 17:22:52 +01001436 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001437 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001438 if (conn)
1439 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1440 else
1441 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1442
William Lallemand5f232402012-04-05 18:02:55 +02001443 if (ret == NULL)
1444 goto out;
1445 tmplog = ret;
1446 last_isspace = 0;
1447 break;
1448
Willy Tarreau2beef582012-12-20 17:22:52 +01001449 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001450 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001451 if (conn)
1452 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1453 else
1454 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1455
William Lallemand5f232402012-04-05 18:02:55 +02001456 if (ret == NULL)
1457 goto out;
1458 tmplog = ret;
1459 last_isspace = 0;
1460 break;
1461
Willy Tarreau2beef582012-12-20 17:22:52 +01001462 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001463 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001464 if (conn)
1465 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1466 else
1467 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1468
William Lallemand1d705562012-03-12 12:46:41 +01001469 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001470 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001471 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001472 last_isspace = 0;
1473 break;
1474
William Lallemand1d705562012-03-12 12:46:41 +01001475 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001476 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001477 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1478 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_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001486 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001487 ret = gmt2str_log(tmplog, &tm, 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
Yuxans Yao4e25b012012-10-19 10:36:09 +08001494 case LOG_FMT_DATELOCAL: // %Tl
1495 get_localtime(s->logs.accept_date.tv_sec, &tm);
1496 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1497 if (ret == NULL)
1498 goto out;
1499 tmplog = ret;
1500 last_isspace = 0;
1501 break;
1502
William Lallemand5f232402012-04-05 18:02:55 +02001503 case LOG_FMT_TS: // %Ts
1504 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1505 if (tmp->options & LOG_OPT_HEXA) {
1506 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1507 if (iret < 0 || iret > dst + maxsize - tmplog)
1508 goto out;
1509 last_isspace = 0;
1510 tmplog += iret;
1511 } else {
1512 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1513 if (ret == NULL)
1514 goto out;
1515 tmplog = ret;
1516 last_isspace = 0;
1517 }
1518 break;
1519
William Lallemand1d705562012-03-12 12:46:41 +01001520 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001521 if (tmp->options & LOG_OPT_HEXA) {
1522 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1523 if (iret < 0 || iret > dst + maxsize - tmplog)
1524 goto out;
1525 last_isspace = 0;
1526 tmplog += iret;
1527 } else {
1528 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001529 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001530 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1531 tmplog, 4);
1532 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001533 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001534 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001535 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001536 }
1537 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001538
William Lallemand1d705562012-03-12 12:46:41 +01001539 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001540 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001541 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001542 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001543 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001544 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001545 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001546 break;
1547
Willy Tarreau773d65f2012-10-12 14:56:11 +02001548 case LOG_FMT_FRONTEND_XPRT: // %ft
1549 src = fe->id;
1550 if (tmp->options & LOG_OPT_QUOTE)
1551 LOGCHAR('"');
1552 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1553 if (iret == 0)
1554 goto out;
1555 tmplog += iret;
1556#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001557 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001558 LOGCHAR('~');
1559#endif
1560 if (tmp->options & LOG_OPT_QUOTE)
1561 LOGCHAR('"');
1562 last_isspace = 0;
1563 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001564#ifdef USE_OPENSSL
1565 case LOG_FMT_SSL_CIPHER: // %sslc
1566 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001567 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001568 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001569 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001570 src = ssl_sock_get_cipher_name(conn);
1571 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001572 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1573 if (ret == NULL)
1574 goto out;
1575 tmplog = ret;
1576 last_isspace = 0;
1577 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001578
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001579 case LOG_FMT_SSL_VERSION: // %sslv
1580 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001581 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001582 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001583 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001584 src = ssl_sock_get_proto_version(conn);
1585 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001586 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1587 if (ret == NULL)
1588 goto out;
1589 tmplog = ret;
1590 last_isspace = 0;
1591 break;
1592#endif
William Lallemand1d705562012-03-12 12:46:41 +01001593 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001594 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001595 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001596 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001597 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001598 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001599 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001600 break;
1601
William Lallemand1d705562012-03-12 12:46:41 +01001602 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001603 switch (obj_type(s->target)) {
1604 case OBJ_TYPE_SERVER:
1605 src = objt_server(s->target)->id;
1606 break;
1607 case OBJ_TYPE_APPLET:
1608 src = objt_applet(s->target)->name;
1609 break;
1610 default:
1611 src = "<NOSRV>";
1612 break;
1613 }
William Lallemand5f232402012-04-05 18:02:55 +02001614 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001615 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001616 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001617 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001618 last_isspace = 0;
1619 break;
1620
William Lallemand1d705562012-03-12 12:46:41 +01001621 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001622 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001623 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001624 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001625 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001626 last_isspace = 0;
1627 break;
1628
William Lallemand1d705562012-03-12 12:46:41 +01001629 case LOG_FMT_TW: // %Tw
1630 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001631 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001632 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001633 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001634 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001635 last_isspace = 0;
1636 break;
1637
William Lallemand1d705562012-03-12 12:46:41 +01001638 case LOG_FMT_TC: // %Tc
1639 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001640 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001641 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001642 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001643 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001644 last_isspace = 0;
1645 break;
1646
William Lallemand1d705562012-03-12 12:46:41 +01001647 case LOG_FMT_TR: // %Tr
1648 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001649 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001650 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001651 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001652 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001653 last_isspace = 0;
1654 break;
1655
William Lallemand1d705562012-03-12 12:46:41 +01001656 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001657 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001658 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001659 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001660 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001661 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001662 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001663 last_isspace = 0;
1664 break;
1665
Willy Tarreau2beef582012-12-20 17:22:52 +01001666 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001667 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001668 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001669 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001670 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001671 last_isspace = 0;
1672 break;
1673
William Lallemand1d705562012-03-12 12:46:41 +01001674 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001675 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001676 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001677 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001678 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001679 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001680 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001681 last_isspace = 0;
1682 break;
1683
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001684 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001685 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1686 if (ret == NULL)
1687 goto out;
1688 tmplog = ret;
1689 last_isspace = 0;
1690 break;
1691
Willy Tarreau2beef582012-12-20 17:22:52 +01001692 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001693 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001694 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001695 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001696 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001697 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001698 last_isspace = 0;
1699 break;
1700
Willy Tarreau2beef582012-12-20 17:22:52 +01001701 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001702 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001703 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001704 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001705 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001706 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001707 last_isspace = 0;
1708 break;
1709
William Lallemand1d705562012-03-12 12:46:41 +01001710 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001711 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1712 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001713 *tmplog = '\0';
1714 last_isspace = 0;
1715 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001716
William Lallemand1d705562012-03-12 12:46:41 +01001717 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001718 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1719 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001720 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1721 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 +01001722 last_isspace = 0;
1723 break;
1724
William Lallemand1d705562012-03-12 12:46:41 +01001725 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001726 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001727 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001728 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001729 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001730 last_isspace = 0;
1731 break;
1732
William Lallemand1d705562012-03-12 12:46:41 +01001733 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001734 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001735 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001736 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001737 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001738 last_isspace = 0;
1739 break;
1740
William Lallemand1d705562012-03-12 12:46:41 +01001741 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001742 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001743 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001744 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001745 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001746 last_isspace = 0;
1747 break;
1748
William Lallemand1d705562012-03-12 12:46:41 +01001749 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001750 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001751 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001752 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001753 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001754 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001755 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001756 last_isspace = 0;
1757 break;
1758
William Lallemand1d705562012-03-12 12:46:41 +01001759 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001760 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001761 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001762 ret = ltoa_o((s->si[1].conn_retries>0) ?
1763 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001764 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001765 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001766 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001767 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001768 last_isspace = 0;
1769 break;
1770
William Lallemand1d705562012-03-12 12:46:41 +01001771 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001772 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001773 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001774 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001775 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001776 last_isspace = 0;
1777 break;
1778
William Lallemand1d705562012-03-12 12:46:41 +01001779 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001780 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001781 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001782 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001783 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001784 last_isspace = 0;
1785 break;
1786
William Lallemand1d705562012-03-12 12:46:41 +01001787 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001788 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001789 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001790 if (tmp->options & LOG_OPT_QUOTE)
1791 LOGCHAR('"');
1792 LOGCHAR('{');
1793 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1794 if (hdr)
1795 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001796 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001797 ret = lf_encode_string(tmplog, dst + maxsize,
1798 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001799 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001800 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001801 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001802 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001803 }
1804 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001805 if (tmp->options & LOG_OPT_QUOTE)
1806 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001807 last_isspace = 0;
1808 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001809 break;
1810
William Lallemand1d705562012-03-12 12:46:41 +01001811 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001812 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001813 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001814 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1815 if (hdr > 0)
1816 LOGCHAR(' ');
1817 if (tmp->options & LOG_OPT_QUOTE)
1818 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001819 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001820 ret = lf_encode_string(tmplog, dst + maxsize,
1821 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001822 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001823 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001824 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001825 } else if (!(tmp->options & LOG_OPT_QUOTE))
1826 LOGCHAR('-');
1827 if (tmp->options & LOG_OPT_QUOTE)
1828 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001829 last_isspace = 0;
1830 }
1831 }
1832 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001833
William Lallemand1d705562012-03-12 12:46:41 +01001834
1835 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001836 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001837 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001838 if (tmp->options & LOG_OPT_QUOTE)
1839 LOGCHAR('"');
1840 LOGCHAR('{');
1841 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1842 if (hdr)
1843 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001844 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001845 ret = lf_encode_string(tmplog, dst + maxsize,
1846 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001847 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001848 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001849 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001850 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001851 }
1852 LOGCHAR('}');
1853 last_isspace = 0;
1854 if (tmp->options & LOG_OPT_QUOTE)
1855 LOGCHAR('"');
1856 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001857 break;
1858
William Lallemand1d705562012-03-12 12:46:41 +01001859 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001860 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001861 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001862 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1863 if (hdr > 0)
1864 LOGCHAR(' ');
1865 if (tmp->options & LOG_OPT_QUOTE)
1866 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001867 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001868 ret = lf_encode_string(tmplog, dst + maxsize,
1869 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001870 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001871 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001872 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001873 } else if (!(tmp->options & LOG_OPT_QUOTE))
1874 LOGCHAR('-');
1875 if (tmp->options & LOG_OPT_QUOTE)
1876 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001877 last_isspace = 0;
1878 }
1879 }
1880 break;
1881
William Lallemand1d705562012-03-12 12:46:41 +01001882 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001883 /* Request */
1884 if (tmp->options & LOG_OPT_QUOTE)
1885 LOGCHAR('"');
1886 uri = txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01001887 ret = lf_encode_string(tmplog, dst + maxsize,
1888 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001889 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001890 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001891 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001892 if (tmp->options & LOG_OPT_QUOTE)
1893 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001894 last_isspace = 0;
1895 break;
William Lallemand5f232402012-04-05 18:02:55 +02001896
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001897 case LOG_FMT_HTTP_PATH: // %HP
1898 uri = txn->uri ? txn->uri : "<BADREQ>";
1899
Willy Tarreaub7636d12015-06-17 19:58:02 +02001900 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001901 LOGCHAR('"');
1902
1903 end = uri + strlen(uri);
1904 // look for the first whitespace character
1905 while (uri < end && !HTTP_IS_SPHT(*uri))
1906 uri++;
1907
1908 // keep advancing past multiple spaces
1909 while (uri < end && HTTP_IS_SPHT(*uri)) {
1910 uri++; nspaces++;
1911 }
1912
1913 // look for first space or question mark after url
1914 spc = uri;
1915 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
1916 spc++;
1917
1918 if (!txn->uri || nspaces == 0) {
1919 chunk.str = "<BADREQ>";
1920 chunk.len = strlen("<BADREQ>");
1921 } else {
1922 chunk.str = uri;
1923 chunk.len = spc - uri;
1924 }
1925
Dragan Dosen835b9212016-02-12 13:23:03 +01001926 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001927 if (ret == NULL || *ret != '\0')
1928 goto out;
1929
1930 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001931 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001932 LOGCHAR('"');
1933
1934 last_isspace = 0;
1935 break;
1936
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001937 case LOG_FMT_HTTP_QUERY: // %HQ
1938 if (tmp->options & LOG_OPT_QUOTE)
1939 LOGCHAR('"');
1940
1941 if (!txn->uri) {
1942 chunk.str = "<BADREQ>";
1943 chunk.len = strlen("<BADREQ>");
1944 } else {
1945 uri = txn->uri;
1946 end = uri + strlen(uri);
1947 // look for the first question mark
1948 while (uri < end && *uri != '?')
1949 uri++;
1950
1951 qmark = uri;
1952 // look for first space or question mark after url
1953 while (uri < end && !HTTP_IS_SPHT(*uri))
1954 uri++;
1955
1956 chunk.str = qmark;
1957 chunk.len = uri - qmark;
1958 }
1959
Dragan Dosen835b9212016-02-12 13:23:03 +01001960 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001961 if (ret == NULL || *ret != '\0')
1962 goto out;
1963
1964 tmplog = ret;
1965 if (tmp->options & LOG_OPT_QUOTE)
1966 LOGCHAR('"');
1967
1968 last_isspace = 0;
1969 break;
1970
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001971 case LOG_FMT_HTTP_URI: // %HU
1972 uri = txn->uri ? txn->uri : "<BADREQ>";
1973
Willy Tarreaub7636d12015-06-17 19:58:02 +02001974 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001975 LOGCHAR('"');
1976
1977 end = uri + strlen(uri);
1978 // look for the first whitespace character
1979 while (uri < end && !HTTP_IS_SPHT(*uri))
1980 uri++;
1981
1982 // keep advancing past multiple spaces
1983 while (uri < end && HTTP_IS_SPHT(*uri)) {
1984 uri++; nspaces++;
1985 }
1986
1987 // look for first space after url
1988 spc = uri;
1989 while (spc < end && !HTTP_IS_SPHT(*spc))
1990 spc++;
1991
1992 if (!txn->uri || nspaces == 0) {
1993 chunk.str = "<BADREQ>";
1994 chunk.len = strlen("<BADREQ>");
1995 } else {
1996 chunk.str = uri;
1997 chunk.len = spc - uri;
1998 }
1999
Dragan Dosen835b9212016-02-12 13:23:03 +01002000 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002001 if (ret == NULL || *ret != '\0')
2002 goto out;
2003
2004 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002005 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002006 LOGCHAR('"');
2007
2008 last_isspace = 0;
2009 break;
2010
2011 case LOG_FMT_HTTP_METHOD: // %HM
2012 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002013 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002014 LOGCHAR('"');
2015
2016 end = uri + strlen(uri);
2017 // look for the first whitespace character
2018 spc = uri;
2019 while (spc < end && !HTTP_IS_SPHT(*spc))
2020 spc++;
2021
2022 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
2023 chunk.str = "<BADREQ>";
2024 chunk.len = strlen("<BADREQ>");
2025 } else {
2026 chunk.str = uri;
2027 chunk.len = spc - uri;
2028 }
2029
Dragan Dosen835b9212016-02-12 13:23:03 +01002030 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002031 if (ret == NULL || *ret != '\0')
2032 goto out;
2033
2034 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002035 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002036 LOGCHAR('"');
2037
2038 last_isspace = 0;
2039 break;
2040
2041 case LOG_FMT_HTTP_VERSION: // %HV
2042 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002043 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002044 LOGCHAR('"');
2045
2046 end = uri + strlen(uri);
2047 // look for the first whitespace character
2048 while (uri < end && !HTTP_IS_SPHT(*uri))
2049 uri++;
2050
2051 // keep advancing past multiple spaces
2052 while (uri < end && HTTP_IS_SPHT(*uri)) {
2053 uri++; nspaces++;
2054 }
2055
2056 // look for the next whitespace character
2057 while (uri < end && !HTTP_IS_SPHT(*uri))
2058 uri++;
2059
2060 // keep advancing past multiple spaces
2061 while (uri < end && HTTP_IS_SPHT(*uri))
2062 uri++;
2063
2064 if (!txn->uri || nspaces == 0) {
2065 chunk.str = "<BADREQ>";
2066 chunk.len = strlen("<BADREQ>");
2067 } else if (uri == end) {
2068 chunk.str = "HTTP/0.9";
2069 chunk.len = strlen("HTTP/0.9");
2070 } else {
2071 chunk.str = uri;
2072 chunk.len = end - uri;
2073 }
2074
Dragan Dosen835b9212016-02-12 13:23:03 +01002075 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002076 if (ret == NULL || *ret != '\0')
2077 goto out;
2078
2079 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002080 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002081 LOGCHAR('"');
2082
2083 last_isspace = 0;
2084 break;
2085
William Lallemand5f232402012-04-05 18:02:55 +02002086 case LOG_FMT_COUNTER: // %rt
2087 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002088 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002089 if (iret < 0 || iret > dst + maxsize - tmplog)
2090 goto out;
2091 last_isspace = 0;
2092 tmplog += iret;
2093 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002094 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002095 if (ret == NULL)
2096 goto out;
2097 tmplog = ret;
2098 last_isspace = 0;
2099 }
2100 break;
2101
Willy Tarreau7346acb2014-08-28 15:03:15 +02002102 case LOG_FMT_LOGCNT: // %lc
2103 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002104 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002105 if (iret < 0 || iret > dst + maxsize - tmplog)
2106 goto out;
2107 last_isspace = 0;
2108 tmplog += iret;
2109 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002110 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002111 if (ret == NULL)
2112 goto out;
2113 tmplog = ret;
2114 last_isspace = 0;
2115 }
2116 break;
2117
William Lallemand5f232402012-04-05 18:02:55 +02002118 case LOG_FMT_HOSTNAME: // %H
2119 src = hostname;
2120 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2121 if (ret == NULL)
2122 goto out;
2123 tmplog = ret;
2124 last_isspace = 0;
2125 break;
2126
2127 case LOG_FMT_PID: // %pid
2128 if (tmp->options & LOG_OPT_HEXA) {
2129 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2130 if (iret < 0 || iret > dst + maxsize - tmplog)
2131 goto out;
2132 last_isspace = 0;
2133 tmplog += iret;
2134 } else {
2135 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2136 if (ret == NULL)
2137 goto out;
2138 tmplog = ret;
2139 last_isspace = 0;
2140 }
2141 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002142
2143 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002144 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01002145 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002146 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002147 if (ret == NULL)
2148 goto out;
2149 tmplog = ret;
2150 last_isspace = 0;
2151 break;
2152
William Lallemandbddd4fd2012-02-27 11:23:10 +01002153 }
2154 }
2155
2156out:
William Lallemand1d705562012-03-12 12:46:41 +01002157 /* *tmplog is a unused character */
2158 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002159 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002160
Willy Tarreaubaaee002006-06-26 02:48:02 +02002161}
2162
William Lallemand1d705562012-03-12 12:46:41 +01002163/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002164 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002165 * Will not log if the frontend has no log defined.
2166 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002167void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002168{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002169 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002170 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002171 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002172
2173 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002174 err = (s->flags & SF_REDISP) ||
2175 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2176 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002177 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002178 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002179
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002180 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002181 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002182
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002183 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002184 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002185
Willy Tarreauabcd5142013-06-11 17:18:02 +02002186 if (s->logs.level) { /* loglevel was overridden */
2187 if (s->logs.level == -1) {
2188 s->logs.logwait = 0; /* logs disabled */
2189 return;
2190 }
2191 level = s->logs.level - 1;
2192 }
2193 else {
2194 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002195 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002196 level = LOG_ERR;
2197 }
William Lallemand1d705562012-03-12 12:46:41 +01002198
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002199 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002200 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002201 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002202 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002203 }
2204
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002205 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2206 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2207 &sess->fe->logformat_sd);
2208 }
2209
Dragan Dosen59cee972015-09-19 22:09:02 +02002210 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002211 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002212 sess->fe->log_count++;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002213 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002214 s->logs.logwait = 0;
2215 }
2216}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002217
Willy Tarreaubaaee002006-06-26 02:48:02 +02002218/*
2219 * Local variables:
2220 * c-indent-level: 8
2221 * c-basic-offset: 8
2222 * End:
2223 */