blob: 520263a718dc104c6939af6469e4a20d78ee351f [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>
Willy Tarreau077edcb2016-08-10 18:30:56 +020025#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020028#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020030#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010033#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020034
William Lallemand5f232402012-04-05 18:02:55 +020035#include <proto/frontend.h>
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +000036#include <proto/proto_http.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020037#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010038#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020039#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010040#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020041#ifdef USE_OPENSSL
42#include <proto/ssl_sock.h>
43#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Dragan Dosen43885c72015-10-01 13:18:13 +020045struct log_fmt {
46 char *name;
47 struct {
48 struct chunk sep1; /* first pid separator */
49 struct chunk sep2; /* second pid separator */
50 } pid;
51};
52
53static const struct log_fmt log_formats[LOG_FORMATS] = {
54 [LOG_FORMAT_RFC3164] = {
55 .name = "rfc3164",
56 .pid = {
57 .sep1 = { .str = "[", .len = 1 },
58 .sep2 = { .str = "]: ", .len = 3 }
59 }
60 },
61 [LOG_FORMAT_RFC5424] = {
62 .name = "rfc5424",
63 .pid = {
64 .sep1 = { .str = " ", .len = 1 },
65 .sep2 = { .str = " - ", .len = 3 }
66 }
67 }
Dragan Dosen1322d092015-09-22 16:05:32 +020068};
69
Dragan Dosen835b9212016-02-12 13:23:03 +010070#define FD_SETS_ARE_BITFIELDS
71#ifdef FD_SETS_ARE_BITFIELDS
72/*
73 * This map is used with all the FD_* macros to check whether a particular bit
74 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
75 * which should be escaped. When FD_ISSET() returns non-zero, it means that the
76 * byte should be escaped. Be careful to always pass bytes from 0 to 255
77 * exclusively to the macros.
78 */
79fd_set rfc5424_escape_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
80
81#else
82#error "Check if your OS uses bitfields for fd_sets"
83#endif
84
Willy Tarreaubaaee002006-06-26 02:48:02 +020085const char *log_facilities[NB_LOG_FACILITIES] = {
86 "kern", "user", "mail", "daemon",
87 "auth", "syslog", "lpr", "news",
88 "uucp", "cron", "auth2", "ftp",
89 "ntp", "audit", "alert", "cron2",
90 "local0", "local1", "local2", "local3",
91 "local4", "local5", "local6", "local7"
92};
93
Willy Tarreaubaaee002006-06-26 02:48:02 +020094const char *log_levels[NB_LOG_LEVELS] = {
95 "emerg", "alert", "crit", "err",
96 "warning", "notice", "info", "debug"
97};
98
Willy Tarreau570f2212013-06-10 16:42:09 +020099const 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 +0200100const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101
William Lallemand723b73a2012-02-08 16:37:49 +0100102
103/* log_format */
104struct logformat_type {
105 char *name;
106 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100107 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200108 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100109 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100110 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100111};
112
William Lallemandb7ff6a32012-03-02 14:35:21 +0100113int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
114
William Lallemand723b73a2012-02-08 16:37:49 +0100115/* log_format variable names */
116static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200117 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100118
119 /* please keep these lines sorted ! */
120 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
121 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
122 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
123 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
124 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200125 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200126 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200127 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100128 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200129 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
130 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
131 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
132 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
133 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
134 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200135 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100136 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200137 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +0100138 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
139 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200140 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100141 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200142 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100143 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
144 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200145 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200146 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
147 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100148 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
149 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200150 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
151 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100152 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200153 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
154 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
155 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
156 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000157 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
158 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000159 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000160 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
161 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200162 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100163 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200164 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100165 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
166 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100167 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100168 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
169 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
170 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
171 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
172 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200173 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
174 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100175 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200176 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
177 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
178 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100179 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
180 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
181
182 /* The following tags are deprecated and will be removed soon */
183 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
184 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200185 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
186 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
187 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
188 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100189 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
190 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
191 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
192 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
193 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200194 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100195};
196
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200197char default_http_log_format[] = "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
198char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%trg] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl";
Willy Tarreau2beef582012-12-20 17:22:52 +0100199char 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 +0100200char *log_format = NULL;
201
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200202/* Default string used for structured-data part in RFC5424 formatted
203 * syslog messages.
204 */
205char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200206
207/* This is a global syslog header, common to all outgoing messages in
208 * RFC3164 format. It begins with time-based part and is updated by
209 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200210 */
211char *logheader = NULL;
212
Dragan Dosen1322d092015-09-22 16:05:32 +0200213/* This is a global syslog header for messages in RFC5424 format. It is
214 * updated by update_log_hdr_rfc5424().
215 */
216char *logheader_rfc5424 = NULL;
217
Dragan Dosen59cee972015-09-19 22:09:02 +0200218/* This is a global syslog message buffer, common to all outgoing
219 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100220 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200221char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100222
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200223/* A global syslog message buffer, common to all RFC5424 syslog messages.
224 * Currently, it is used for generating the structured-data part.
225 */
226char *logline_rfc5424 = NULL;
227
William Lallemand723b73a2012-02-08 16:37:49 +0100228struct logformat_var_args {
229 char *name;
230 int mask;
231};
232
233struct logformat_var_args var_args_list[] = {
234// global
235 { "M", LOG_OPT_MANDATORY },
236 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200237 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100238 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100239 { 0, 0 }
240};
241
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200242/* return the name of the directive used in the current proxy for which we're
243 * currently parsing a header, when it is known.
244 */
245static inline const char *fmt_directive(const struct proxy *curproxy)
246{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100247 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200248 case ARGC_ACL:
249 return "acl";
250 case ARGC_STK:
251 return "stick";
252 case ARGC_TRK:
253 return "track-sc";
254 case ARGC_LOG:
255 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200256 case ARGC_LOGSD:
257 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100258 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100259 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100260 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100261 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200262 case ARGC_UIF:
263 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100264 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200265 return "redirect";
266 case ARGC_CAP:
267 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200268 case ARGC_SRV:
269 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200270 case ARGC_SPOE:
271 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100272 case ARGC_UBK:
273 return "use_backend";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100274 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200275 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100276 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200277}
278
William Lallemand723b73a2012-02-08 16:37:49 +0100279/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100280 * callback used to configure addr source retrieval
281 */
282int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
283{
284 curproxy->options2 |= PR_O2_SRC_ADDR;
285
286 return 0;
287}
288
289
290/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100291 * Parse args in a logformat_var. Returns 0 in error
292 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100293 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100294int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100295{
296 int i = 0;
297 int end = 0;
298 int flags = 0; // 1 = + 2 = -
299 char *sp = NULL; // start pointer
300
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100301 if (args == NULL) {
302 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100303 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100304 }
William Lallemand723b73a2012-02-08 16:37:49 +0100305
306 while (1) {
307 if (*args == '\0')
308 end = 1;
309
310 if (*args == '+') {
311 // add flag
312 sp = args + 1;
313 flags = 1;
314 }
315 if (*args == '-') {
316 // delete flag
317 sp = args + 1;
318 flags = 2;
319 }
320
321 if (*args == '\0' || *args == ',') {
322 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100323 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100324 if (strcmp(sp, var_args_list[i].name) == 0) {
325 if (flags == 1) {
326 node->options |= var_args_list[i].mask;
327 break;
328 } else if (flags == 2) {
329 node->options &= ~var_args_list[i].mask;
330 break;
331 }
332 }
333 }
334 sp = NULL;
335 if (end)
336 break;
337 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100338 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100339 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100340 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100341}
342
343/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100344 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
345 * must pass the args part in the <arg> pointer with its length in <arg_len>,
346 * and varname with its length in <var> and <var_len> respectively. <arg> is
347 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100348 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100349 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100350int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100351{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100352 int j;
353 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100354
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100355 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
356 if (strlen(logformat_keywords[j].name) == var_len &&
357 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
358 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200359 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100360 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100361 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100362 return 0;
363 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100364 node->type = logformat_keywords[j].type;
365 node->options = *defoptions;
366 if (arg_len) {
367 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100368 if (!parse_logformat_var_args(node->arg, node, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100369 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100370 }
371 if (node->type == LOG_FMT_GLOBAL) {
372 *defoptions = node->options;
373 free(node->arg);
374 free(node);
375 } else {
376 if (logformat_keywords[j].config_callback &&
377 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100378 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100379 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100380 curproxy->to_log |= logformat_keywords[j].lw;
381 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100382 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100383 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100384 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200385 curproxy->conf.args.file, curproxy->conf.args.line,
386 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100387 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100388 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100389 memprintf(err, "format variable '%s' is reserved for HTTP mode",
390 logformat_keywords[j].name);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100391 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100392 }
William Lallemand723b73a2012-02-08 16:37:49 +0100393 }
394 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100395
396 j = var[var_len];
397 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100398 memprintf(err, "no such format variable '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%'", var);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100399 var[var_len] = j;
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100400 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100401}
402
403/*
404 * push to the logformat linked list
405 *
406 * start: start pointer
407 * end: end text pointer
408 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100409 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100410 *
411 * LOG_TEXT: copy chars from start to end excluding end.
412 *
413*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100414int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100415{
416 char *str;
417
Willy Tarreaua3571662012-12-20 21:59:12 +0100418 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200419 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100420 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100421 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100422 return 0;
423 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200424 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100425 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100426 str[end - start] = '\0';
427 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100428 node->type = LOG_FMT_TEXT; // type string
429 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100430 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200431 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100432 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100433 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100434 return 0;
435 }
William Lallemand1d705562012-03-12 12:46:41 +0100436 node->type = LOG_FMT_SEPARATOR;
437 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100438 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100439 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100440}
441
442/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100443 * Parse the sample fetch expression <text> and add a node to <list_format> upon
444 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200445 * should work. The curpx->conf.args.ctx must be set by the caller.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100446 *
447 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100448 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100449int add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, char **err)
Willy Tarreauc8368452012-12-21 00:09:23 +0100450{
451 char *cmd[2];
452 struct sample_expr *expr;
453 struct logformat_node *node;
454 int cmd_arg;
455
456 cmd[0] = text;
457 cmd[1] = "";
458 cmd_arg = 0;
459
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100460 expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100461 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100462 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100463 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100464 }
465
Vincent Bernat02779b62016-04-03 13:48:43 +0200466 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100467 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100468 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100469 return 0;
470 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100471 node->type = LOG_FMT_EXPR;
472 node->expr = expr;
473 node->options = options;
474
475 if (arg_len) {
476 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100477 if (!parse_logformat_var_args(node->arg, node, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100478 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100479 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100480 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100481 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
482
Willy Tarreau434c57c2013-01-08 01:10:24 +0100483 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100484 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
485
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100486 if (!(expr->fetch->val & cap)) {
David Carlier93e8b882017-09-21 14:36:43 +0000487 free(node);
488 node = NULL;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100489 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
490 text, sample_src_names(expr->fetch->use));
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100491 return 0;
492 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100493
Willy Tarreauc8368452012-12-21 00:09:23 +0100494 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
495 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100496 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100497
William Lallemand65ad6e12014-01-31 15:08:02 +0100498 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
499 * needed with some sample fetches (eg: ssl*). We always set it for
500 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100501 */
502 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100503 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100504 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100505 return 1;
Willy Tarreauc8368452012-12-21 00:09:23 +0100506}
507
508/*
William Lallemand723b73a2012-02-08 16:37:49 +0100509 * Parse the log_format string and fill a linked list.
510 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200511 * You can set arguments using { } : %{many arguments}varname.
512 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100513 *
514 * str: the string to parse
515 * curproxy: the proxy affected
516 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100517 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100518 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100519 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100520 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100521 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100522int parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100523{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100524 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100525 char *arg = NULL; /* start pointer for args */
526 char *var = NULL; /* start pointer for vars */
527 int arg_len = 0;
528 int var_len = 0;
529 int cformat; /* current token format */
530 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100531 struct logformat_node *tmplf, *back;
532
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100533 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100534 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100535 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100536 return 0;
537 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200538 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200539
William Lallemand723b73a2012-02-08 16:37:49 +0100540 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100541 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100542 LIST_DEL(&tmplf->list);
543 free(tmplf);
544 }
545
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100546 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100547 pformat = cformat;
548
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100549 if (!*str)
550 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100551
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100552 /* The prinicple of the two-step state machine below is to first detect a change, and
553 * second have all common paths processed at one place. The common paths are the ones
554 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
555 * We use the common LF_INIT state to dispatch to the different final states.
556 */
557 switch (pformat) {
558 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100559 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100560 arg_len = var_len = 0;
561 if (*str == '{') { // optional argument
562 cformat = LF_STARG;
563 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100564 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100565 else if (*str == '[') {
566 cformat = LF_STEXPR;
567 var = str + 1; // store expr in variable name
568 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100569 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100570 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100571 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100572 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100573 else if (*str == '%')
574 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100575 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100576 /* single '%' followed by blank or digit, send them both */
577 cformat = LF_TEXT;
578 pformat = LF_TEXT; /* finally we include the previous char as well */
579 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600580 memprintf(err, "unexpected variable name near '%c' at position %d line : '%s'. Maybe you want to write a single '%%', use the syntax '%%%%'",
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100581 *str, (int)(str - backfmt), fmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100582 return 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100583
584 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100585 else
586 cformat = LF_INIT; // handle other cases of litterals
587 break;
588
589 case LF_STARG: // text immediately following '%{'
590 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100591 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100592 arg_len = str - arg;
593 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100594 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100595 break;
596
597 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100598 if (*str == '[') {
599 cformat = LF_STEXPR;
600 var = str + 1; // store expr in variable name
601 break;
602 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100603 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100604 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100605 var = str;
606 break;
607 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100608 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100609 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100610
Willy Tarreauc8368452012-12-21 00:09:23 +0100611 case LF_STEXPR: // text immediately following '%['
612 if (*str == ']') { // end of arg
613 cformat = LF_EDEXPR;
614 var_len = str - var;
615 *str = 0; // needed for parsing the expression
616 }
617 break;
618
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100619 case LF_VAR: // text part of a variable name
620 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100621 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100622 cformat = LF_INIT; // not variable name anymore
623 break;
624
Willy Tarreauc8368452012-12-21 00:09:23 +0100625 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100626 cformat = LF_INIT;
627 }
628
629 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
630 switch (*str) {
631 case '%': cformat = LF_STARTVAR; break;
632 case ' ': cformat = LF_SEPARATOR; break;
633 case 0 : cformat = LF_END; break;
634 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100635 }
636 }
637
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100638 if (cformat != pformat || pformat == LF_SEPARATOR) {
639 switch (pformat) {
640 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100641 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100642 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100643 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100644 case LF_STEXPR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100645 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100646 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100647 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100648 case LF_TEXT:
649 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100650 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100651 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100652 break;
653 }
654 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100655 }
656 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100657
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100658 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100659 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100660 return 0;
661 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100662 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100663
664 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100665}
666
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667/*
668 * Displays the message on stderr with the date and pid. Overrides the quiet
669 * mode during startup.
670 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200671void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672{
673 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200674 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675
676 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
677 va_start(argp, fmt);
678
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200679 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200681 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682 vfprintf(stderr, fmt, argp);
683 fflush(stderr);
684 va_end(argp);
685 }
686}
687
688
689/*
690 * Displays the message on stderr with the date and pid.
691 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200692void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693{
694 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200695 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696
697 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
698 va_start(argp, fmt);
699
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200700 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200702 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200703 vfprintf(stderr, fmt, argp);
704 fflush(stderr);
705 va_end(argp);
706 }
707}
708
709/*
710 * Displays the message on <out> only if quiet mode is not set.
711 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200712void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200713{
714 va_list argp;
715
716 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
717 va_start(argp, fmt);
718 vfprintf(out, fmt, argp);
719 fflush(out);
720 va_end(argp);
721 }
722}
723
724/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200725 * returns log format for <fmt> or -1 if not found.
726 */
727int get_log_format(const char *fmt)
728{
729 int format;
730
731 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200732 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200733 format--;
734
735 return format;
736}
737
738/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200739 * returns log level for <lev> or -1 if not found.
740 */
741int get_log_level(const char *lev)
742{
743 int level;
744
745 level = NB_LOG_LEVELS - 1;
746 while (level >= 0 && strcmp(log_levels[level], lev))
747 level--;
748
749 return level;
750}
751
Willy Tarreaubaaee002006-06-26 02:48:02 +0200752/*
753 * returns log facility for <fac> or -1 if not found.
754 */
755int get_log_facility(const char *fac)
756{
757 int facility;
758
759 facility = NB_LOG_FACILITIES - 1;
760 while (facility >= 0 && strcmp(log_facilities[facility], fac))
761 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100762
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 return facility;
764}
765
William Lallemanda1cc3812012-02-08 16:38:44 +0100766/*
Dragan Dosen835b9212016-02-12 13:23:03 +0100767 * Encode the string.
768 *
769 * When using the +E log format option, it will try to escape '"\]'
770 * characters with '\' as prefix. The same prefix should not be used as
771 * <escape>.
772 */
773static char *lf_encode_string(char *start, char *stop,
774 const char escape, const fd_set *map,
775 const char *string,
776 struct logformat_node *node)
777{
778 if (node->options & LOG_OPT_ESC) {
779 if (start < stop) {
780 stop--; /* reserve one byte for the final '\0' */
781 while (start < stop && *string != '\0') {
782 if (!FD_ISSET((unsigned char)(*string), map)) {
783 if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map))
784 *start++ = *string;
785 else {
786 if (start + 2 >= stop)
787 break;
788 *start++ = '\\';
789 *start++ = *string;
790 }
791 }
792 else {
793 if (start + 3 >= stop)
794 break;
795 *start++ = escape;
796 *start++ = hextab[(*string >> 4) & 15];
797 *start++ = hextab[*string & 15];
798 }
799 string++;
800 }
801 *start = '\0';
802 }
803 }
804 else {
805 return encode_string(start, stop, escape, map, string);
806 }
807
808 return start;
809}
810
811/*
812 * Encode the chunk.
813 *
814 * When using the +E log format option, it will try to escape '"\]'
815 * characters with '\' as prefix. The same prefix should not be used as
816 * <escape>.
817 */
818static char *lf_encode_chunk(char *start, char *stop,
819 const char escape, const fd_set *map,
820 const struct chunk *chunk,
821 struct logformat_node *node)
822{
823 char *str, *end;
824
825 if (node->options & LOG_OPT_ESC) {
826 if (start < stop) {
827 str = chunk->str;
828 end = chunk->str + chunk->len;
829
830 stop--; /* reserve one byte for the final '\0' */
831 while (start < stop && str < end) {
832 if (!FD_ISSET((unsigned char)(*str), map)) {
833 if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map))
834 *start++ = *str;
835 else {
836 if (start + 2 >= stop)
837 break;
838 *start++ = '\\';
839 *start++ = *str;
840 }
841 }
842 else {
843 if (start + 3 >= stop)
844 break;
845 *start++ = escape;
846 *start++ = hextab[(*str >> 4) & 15];
847 *start++ = hextab[*str & 15];
848 }
849 str++;
850 }
851 *start = '\0';
852 }
853 }
854 else {
855 return encode_chunk(start, stop, escape, map, chunk);
856 }
857
858 return start;
859}
860
861/*
William Lallemanda1cc3812012-02-08 16:38:44 +0100862 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +0100863 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +0100864 *
865 * Return the adress of the \0 character, or NULL on error
866 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100867char *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 +0100868{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100869 if (size < 2)
870 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100871
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100872 if (node->options & LOG_OPT_QUOTE) {
873 *(dst++) = '"';
874 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100875 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100876
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100877 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +0200878 if (++len > size)
879 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +0100880 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +0100881 char *ret;
882
Dragan Dosendb1b6f92016-07-25 11:35:02 +0200883 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +0100884 if (ret == NULL || *ret != '\0')
885 return NULL;
886 len = ret - dst;
887 }
888 else {
Dragan Dosen835b9212016-02-12 13:23:03 +0100889 len = strlcpy2(dst, src, len);
890 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100891
892 size -= len;
893 dst += len;
894 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100895 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
896 if (size < 2)
897 return NULL;
898 *(dst++) = '-';
899 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100900
901 if (node->options & LOG_OPT_QUOTE) {
902 if (size < 2)
903 return NULL;
904 *(dst++) = '"';
905 }
906
907 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100908 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100909}
910
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100911static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
912{
913 return lf_text_len(dst, src, size, size, node);
914}
915
William Lallemand5f232402012-04-05 18:02:55 +0200916/*
917 * Write a IP adress to the log string
918 * +X option write in hexadecimal notation, most signifant byte on the left
919 */
920char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
921{
922 char *ret = dst;
923 int iret;
924 char pn[INET6_ADDRSTRLEN];
925
926 if (node->options & LOG_OPT_HEXA) {
927 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
928 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
929 if (iret < 0 || iret > size)
930 return NULL;
931 ret += iret;
932 } else {
933 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
934 ret = lf_text(dst, pn, size, node);
935 if (ret == NULL)
936 return NULL;
937 }
938 return ret;
939}
940
941/*
942 * Write a port to the log
943 * +X option write in hexadecimal notation, most signifant byte on the left
944 */
945char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
946{
947 char *ret = dst;
948 int iret;
949
950 if (node->options & LOG_OPT_HEXA) {
951 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
952 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
953 if (iret < 0 || iret > size)
954 return NULL;
955 ret += iret;
956 } else {
957 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
958 if (ret == NULL)
959 return NULL;
960 }
961 return ret;
962}
963
Dragan Dosen1322d092015-09-22 16:05:32 +0200964/* Re-generate time-based part of the syslog header in RFC3164 format at
965 * the beginning of logheader once a second and return the pointer to the
966 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100967 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200968static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200969{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100970 static long tvsec;
971 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Dragan Dosen43885c72015-10-01 13:18:13 +0200972 static struct chunk host = { NULL, 0, 0 };
973 static int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200974
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200975 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200976 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200977 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100978 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200979
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200980 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +0200981 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200982
Dragan Dosen43885c72015-10-01 13:18:13 +0200983 if (unlikely(global.log_send_hostname != host.str)) {
984 host.str = global.log_send_hostname;
985 host.len = host.str ? strlen(host.str) : 0;
986 sep = host.len ? 1 : 0;
987 }
988
Dragan Dosen59cee972015-09-19 22:09:02 +0200989 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +0200990 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200991 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +0200992 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
993 host.len, host.str, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994 /* WARNING: depending upon implementations, snprintf may return
995 * either -1 or the number of bytes that would be needed to store
996 * the total message. In both cases, we must adjust it.
997 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200998 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
999 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001000
Dragan Dosen59cee972015-09-19 22:09:02 +02001001 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001002 }
1003
Willy Tarreau094af4e2015-01-07 15:03:42 +01001004 dataptr[0] = 0; // ensure we get rid of any previous attempt
1005
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001006 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001007}
1008
Dragan Dosen1322d092015-09-22 16:05:32 +02001009/* Re-generate time-based part of the syslog header in RFC5424 format at
1010 * the beginning of logheader_rfc5424 once a second and return the pointer
1011 * to the first character after it.
1012 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001013static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +02001014{
1015 static long tvsec;
1016 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001017 const char *gmt_offset;
Dragan Dosen1322d092015-09-22 16:05:32 +02001018
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001019 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +02001020 /* this string is rebuild only once a second */
1021 struct tm tm;
1022 int hdr_len;
1023
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001024 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +02001025 get_localtime(tvsec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001026 gmt_offset = get_gmt_offset(time, &tm);
Dragan Dosen1322d092015-09-22 16:05:32 +02001027
1028 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +02001029 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +02001030 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +02001031 tm.tm_hour, tm.tm_min, tm.tm_sec,
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001032 gmt_offset, gmt_offset+3,
Dragan Dosen43885c72015-10-01 13:18:13 +02001033 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +02001034 /* WARNING: depending upon implementations, snprintf may return
1035 * either -1 or the number of bytes that would be needed to store
1036 * the total message. In both cases, we must adjust it.
1037 */
1038 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1039 hdr_len = global.max_syslog_len;
1040
1041 dataptr = logheader_rfc5424 + hdr_len;
1042 }
1043
1044 dataptr[0] = 0; // ensure we get rid of any previous attempt
1045
1046 return dataptr;
1047}
1048
William Lallemand2a4a44f2012-02-06 16:00:33 +01001049/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001050 * This function sends the syslog message using a printf format string. It
1051 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001052 */
1053void send_log(struct proxy *p, int level, const char *format, ...)
1054{
1055 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001056 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001057
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001058 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001059 return;
1060
William Lallemand2a4a44f2012-02-06 16:00:33 +01001061 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001062 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001063 if (data_len < 0 || data_len > global.max_syslog_len)
1064 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001065 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001066
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001067 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001068}
1069
1070/*
1071 * This function sends a syslog message.
1072 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001073 * It overrides the last byte of the message vector with an LF character.
1074 * The arguments <sd> and <sd_size> are used for the structured-data part
1075 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001076 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001077void __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 +01001078{
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001079 static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1080 static struct msghdr msghdr = {
1081 .msg_iov = iovec,
1082 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1083 };
William Lallemand2a4a44f2012-02-06 16:00:33 +01001084 static int logfdunix = -1; /* syslog to AF_UNIX socket */
1085 static int logfdinet = -1; /* syslog to AF_INET socket */
1086 static char *dataptr = NULL;
1087 int fac_level;
1088 struct list *logsrvs = NULL;
1089 struct logsrv *tmp = NULL;
1090 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +02001091 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001092 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001093 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001094 struct chunk *tag = &global.log_tag;
1095 static int curr_pid;
1096 static char pidstr[100];
1097 static struct chunk pid;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001098
1099 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100
1101 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +02001102 if (!LIST_ISEMPTY(&global.logsrvs)) {
1103 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104 }
1105 } else {
William Lallemand0f99e342011-10-12 17:50:54 +02001106 if (!LIST_ISEMPTY(&p->logsrvs)) {
1107 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001108 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001109 if (p->log_tag.str) {
1110 tag = &p->log_tag;
1111 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001112 }
1113
William Lallemand0f99e342011-10-12 17:50:54 +02001114 if (!logsrvs)
1115 return;
1116
Dragan Dosen43885c72015-10-01 13:18:13 +02001117 if (unlikely(curr_pid != getpid())) {
1118 curr_pid = getpid();
1119 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1120 chunk_initstr(&pid, pidstr);
1121 }
1122
Robert Tsai81ae1952007-12-05 10:47:29 +01001123 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +02001124 nblogger = 0;
1125 list_for_each_entry(tmp, logsrvs, list) {
1126 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +01001127 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +01001128 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +02001129 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +01001130 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001131 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +02001132 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001133 int tag_max = 0;
1134 int pid_sep1_max = 0;
1135 int pid_max = 0;
1136 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001137 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001138 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +01001139
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001140 nblogger++;
1141
Willy Tarreaubaaee002006-06-26 02:48:02 +02001142 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +02001143 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001144 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001145
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001146 if (unlikely(*plogfd < 0)) {
1147 /* socket not successfully initialized yet */
1148 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
1149
1150 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
1151 Alert("socket for logger #%d failed: %s (errno=%d)\n",
1152 nblogger, strerror(errno), errno);
1153 continue;
1154 }
1155 /* we don't want to receive anything on this socket */
1156 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1157 /* does nothing under Linux, maybe needed for others */
1158 shutdown(*plogfd, SHUT_RD);
1159 }
1160
Dragan Dosen1322d092015-09-22 16:05:32 +02001161 switch (logsrv->format) {
1162 case LOG_FORMAT_RFC3164:
1163 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001164 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +02001165 break;
1166
1167 case LOG_FORMAT_RFC5424:
1168 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001169 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001170 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001171 break;
1172
1173 default:
1174 continue; /* must never happen */
1175 }
1176
1177 hdr_size = hdr_ptr - hdr;
1178
Willy Tarreaubaaee002006-06-26 02:48:02 +02001179 /* For each target, we may have a different facility.
1180 * We can also have a different log level for each message.
1181 * This induces variations in the message header length.
1182 * Since we don't want to recompute it each time, nor copy it every
1183 * time, we only change the facility in the pre-computed header,
1184 * and we change the pointer to the header accordingly.
1185 */
William Lallemand0f99e342011-10-12 17:50:54 +02001186 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001187 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001188 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001189 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001190 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001191 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001192 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001193 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001194
Dragan Dosen1322d092015-09-22 16:05:32 +02001195 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001196
Dragan Dosen43885c72015-10-01 13:18:13 +02001197 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001198 if (unlikely(hdr_size >= logsrv->maxlen)) {
1199 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001200 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001201 goto send;
1202 }
1203
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001204 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001205
Dragan Dosen43885c72015-10-01 13:18:13 +02001206 /* tag */
1207 tag_max = tag->len;
1208 if (unlikely(tag_max >= maxlen)) {
1209 tag_max = maxlen - 1;
1210 sd_max = 0;
1211 goto send;
1212 }
1213
1214 maxlen -= tag_max;
1215
1216 /* first pid separator */
1217 pid_sep1_max = log_formats[logsrv->format].pid.sep1.len;
1218 if (unlikely(pid_sep1_max >= maxlen)) {
1219 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001220 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001221 goto send;
1222 }
1223
Dragan Dosen43885c72015-10-01 13:18:13 +02001224 pid_sep1 = log_formats[logsrv->format].pid.sep1.str;
1225 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001226
Dragan Dosen43885c72015-10-01 13:18:13 +02001227 /* pid */
1228 pid_max = pid.len;
1229 if (unlikely(pid_max >= maxlen)) {
1230 pid_max = maxlen - 1;
1231 sd_max = 0;
1232 goto send;
1233 }
1234
1235 maxlen -= pid_max;
1236
1237 /* second pid separator */
1238 pid_sep2_max = log_formats[logsrv->format].pid.sep2.len;
1239 if (unlikely(pid_sep2_max >= maxlen)) {
1240 pid_sep2_max = maxlen - 1;
1241 sd_max = 0;
1242 goto send;
1243 }
1244
1245 pid_sep2 = log_formats[logsrv->format].pid.sep2.str;
1246 maxlen -= pid_sep2_max;
1247
1248 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001249 if (sd_max >= maxlen) {
1250 sd_max = maxlen - 1;
1251 goto send;
1252 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001253
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001254 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001255send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001256 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001257 iovec[0].iov_len = hdr_max;
1258 iovec[1].iov_base = tag->str;
1259 iovec[1].iov_len = tag_max;
1260 iovec[2].iov_base = pid_sep1;
1261 iovec[2].iov_len = pid_sep1_max;
1262 iovec[3].iov_base = pid.str;
1263 iovec[3].iov_len = pid_max;
1264 iovec[4].iov_base = pid_sep2;
1265 iovec[4].iov_len = pid_sep2_max;
1266 iovec[5].iov_base = sd;
1267 iovec[5].iov_len = sd_max;
1268 iovec[6].iov_base = dataptr;
1269 iovec[6].iov_len = max;
1270 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1271 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001272
1273 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1274 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1275
1276 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001277
Robert Tsai81ae1952007-12-05 10:47:29 +01001278 if (sent < 0) {
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001279 Alert("sendmsg logger #%d failed: %s (errno=%d)\n",
Robert Tsai81ae1952007-12-05 10:47:29 +01001280 nblogger, strerror(errno), errno);
1281 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001282 }
1283}
1284
William Lallemandbddd4fd2012-02-27 11:23:10 +01001285extern fd_set hdr_encode_map[];
1286extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001287extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001288
Willy Tarreaubaaee002006-06-26 02:48:02 +02001289
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001290const 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 +01001291const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1292 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1293 Set-cookie Updated, unknown, unknown */
1294
William Lallemand1d705562012-03-12 12:46:41 +01001295/*
1296 * try to write a character if there is enough space, or goto out
1297 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001298#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001299 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001300 *(tmplog++) = (x); \
1301 } else { \
1302 goto out; \
1303 } \
1304 } while(0)
1305
Dragan Dosen835b9212016-02-12 13:23:03 +01001306
1307/* Initializes some log data.
1308 */
1309void init_log()
1310{
1311 char *tmp;
1312
1313 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1314 * inside PARAM-VALUE should be escaped with '\' as prefix.
1315 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1316 * details.
1317 */
1318 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1319
1320 tmp = "\"\\]";
1321 while (*tmp) {
1322 FD_SET(*tmp, rfc5424_escape_map);
1323 tmp++;
1324 }
1325}
William Lallemand1d705562012-03-12 12:46:41 +01001326
Christopher Faulet0132d062017-07-26 15:33:35 +02001327/* Initialize log buffers used for syslog messages */
1328int init_log_buffers()
1329{
1330 logheader = my_realloc2(logheader, global.max_syslog_len + 1);
1331 logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1);
1332 logline = my_realloc2(logline, global.max_syslog_len + 1);
1333 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
1334 if (!logheader || !logline_rfc5424 || !logline || !logline_rfc5424)
1335 return 0;
1336 return 1;
1337}
1338
1339/* Deinitialize log buffers used for syslog messages */
1340void deinit_log_buffers()
1341{
1342 free(logheader);
1343 free(logheader_rfc5424);
1344 free(logline);
1345 free(logline_rfc5424);
1346 logheader = NULL;
1347 logheader_rfc5424 = NULL;
1348 logline = NULL;
1349 logline_rfc5424 = NULL;
1350}
1351
Willy Tarreaudf974472012-12-28 02:44:01 +01001352/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1353 * <maxsize> characters. Returns the size of the output string in characters,
1354 * not counting the trailing zero which is always added if the resulting size
1355 * is not zero.
1356 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001357int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001358{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001359 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001360 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +01001361 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001362 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001363 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001364 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001365 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001366 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001367 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001368 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001369 int t_request;
1370 int hdr;
1371 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001372 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001373 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001374 char *ret;
1375 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001376 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001377 struct timeval tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001378
William Lallemandbddd4fd2012-02-27 11:23:10 +01001379 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001380
William Lallemandbddd4fd2012-02-27 11:23:10 +01001381 t_request = -1;
1382 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1383 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1384
William Lallemand1d705562012-03-12 12:46:41 +01001385 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001386
William Lallemandbddd4fd2012-02-27 11:23:10 +01001387 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001388 if (LIST_ISEMPTY(list_format))
1389 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001390
William Lallemand1d705562012-03-12 12:46:41 +01001391 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001392 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001393 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001394 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001395 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001396
Willy Tarreauc8368452012-12-21 00:09:23 +01001397 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001398 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001399 if (!last_isspace) {
1400 LOGCHAR(' ');
1401 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001402 }
1403 break;
1404
William Lallemand1d705562012-03-12 12:46:41 +01001405 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001406 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001407 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001408 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001409 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001410 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001411 last_isspace = 0;
1412 break;
1413
Willy Tarreauc8368452012-12-21 00:09:23 +01001414 case LOG_FMT_EXPR: // sample expression, may be request or response
1415 key = NULL;
1416 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001417 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 +01001418 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001419 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 +01001420 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01001421 ret = lf_encode_chunk(tmplog, dst + maxsize,
1422 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001423 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001424 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 +01001425 if (ret == 0)
1426 goto out;
1427 tmplog = ret;
1428 last_isspace = 0;
1429 break;
1430
Willy Tarreau2beef582012-12-20 17:22:52 +01001431 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001432 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001433 if (conn)
1434 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1435 else
1436 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001437 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001438 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001439 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001440 last_isspace = 0;
1441 break;
1442
Willy Tarreau2beef582012-12-20 17:22:52 +01001443 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001444 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001445 if (conn) {
1446 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001447 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001448 } else {
1449 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1450 dst + maxsize - tmplog, tmp);
1451 }
William Lallemand5f232402012-04-05 18:02:55 +02001452 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001453 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_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001463 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001464 if (conn) {
1465 conn_get_to_addr(conn);
1466 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1467 }
1468 else
1469 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1470
William Lallemand1d705562012-03-12 12:46:41 +01001471 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001472 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001473 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001474 last_isspace = 0;
1475 break;
1476
Willy Tarreau2beef582012-12-20 17:22:52 +01001477 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001478 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001479 if (conn) {
1480 conn_get_to_addr(conn);
1481 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001482 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001483 else
1484 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001485 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001486 else
1487 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1488
William Lallemand5f232402012-04-05 18:02:55 +02001489 if (ret == NULL)
1490 goto out;
1491 tmplog = ret;
1492 last_isspace = 0;
1493 break;
1494
Willy Tarreau2beef582012-12-20 17:22:52 +01001495 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001496 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001497 if (conn)
1498 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1499 else
1500 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1501
William Lallemand1d705562012-03-12 12:46:41 +01001502 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001503 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001504 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001505 last_isspace = 0;
1506 break;
1507
Willy Tarreau2beef582012-12-20 17:22:52 +01001508 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001509 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001510 if (conn)
1511 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1512 else
1513 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1514
William Lallemand5f232402012-04-05 18:02:55 +02001515 if (ret == NULL)
1516 goto out;
1517 tmplog = ret;
1518 last_isspace = 0;
1519 break;
1520
Willy Tarreau2beef582012-12-20 17:22:52 +01001521 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001522 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001523 if (conn)
1524 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1525 else
1526 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1527
William Lallemand5f232402012-04-05 18:02:55 +02001528 if (ret == NULL)
1529 goto out;
1530 tmplog = ret;
1531 last_isspace = 0;
1532 break;
1533
Willy Tarreau2beef582012-12-20 17:22:52 +01001534 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001535 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001536 if (conn)
1537 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1538 else
1539 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1540
William Lallemand1d705562012-03-12 12:46:41 +01001541 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001542 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001543 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001544 last_isspace = 0;
1545 break;
1546
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001547 case LOG_FMT_DATE: // %t = accept date
William Lallemandbddd4fd2012-02-27 11:23:10 +01001548 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001549 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1550 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001551 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001552 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001553 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001554 last_isspace = 0;
1555 break;
1556
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001557 case LOG_FMT_tr: // %tr = start of request date
1558 /* Note that the timers are valid if we get here */
1559 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1560 get_localtime(tv.tv_sec, &tm);
1561 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
1562 if (ret == NULL)
1563 goto out;
1564 tmplog = ret;
1565 last_isspace = 0;
1566 break;
1567
1568 case LOG_FMT_DATEGMT: // %T = accept date, GMT
William Lallemandbddd4fd2012-02-27 11:23:10 +01001569 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001570 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001571 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001572 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001573 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001574 last_isspace = 0;
1575 break;
1576
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001577 case LOG_FMT_trg: // %trg = start of request date, GMT
1578 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1579 get_gmtime(tv.tv_sec, &tm);
1580 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
1581 if (ret == NULL)
1582 goto out;
1583 tmplog = ret;
1584 last_isspace = 0;
1585 break;
1586
1587 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Yuxans Yao4e25b012012-10-19 10:36:09 +08001588 get_localtime(s->logs.accept_date.tv_sec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001589 ret = localdate2str_log(tmplog, s->logs.accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08001590 if (ret == NULL)
1591 goto out;
1592 tmplog = ret;
1593 last_isspace = 0;
1594 break;
1595
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001596 case LOG_FMT_trl: // %trl = start of request date, local
1597 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1598 get_localtime(tv.tv_sec, &tm);
1599 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
1600 if (ret == NULL)
1601 goto out;
1602 tmplog = ret;
1603 last_isspace = 0;
1604 break;
1605
William Lallemand5f232402012-04-05 18:02:55 +02001606 case LOG_FMT_TS: // %Ts
1607 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1608 if (tmp->options & LOG_OPT_HEXA) {
1609 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1610 if (iret < 0 || iret > dst + maxsize - tmplog)
1611 goto out;
1612 last_isspace = 0;
1613 tmplog += iret;
1614 } else {
1615 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1616 if (ret == NULL)
1617 goto out;
1618 tmplog = ret;
1619 last_isspace = 0;
1620 }
1621 break;
1622
William Lallemand1d705562012-03-12 12:46:41 +01001623 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001624 if (tmp->options & LOG_OPT_HEXA) {
1625 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1626 if (iret < 0 || iret > dst + maxsize - tmplog)
1627 goto out;
1628 last_isspace = 0;
1629 tmplog += iret;
1630 } else {
1631 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001632 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001633 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1634 tmplog, 4);
1635 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001636 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001637 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001638 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001639 }
1640 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001641
William Lallemand1d705562012-03-12 12:46:41 +01001642 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001643 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001644 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001645 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001646 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001647 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001648 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001649 break;
1650
Willy Tarreau773d65f2012-10-12 14:56:11 +02001651 case LOG_FMT_FRONTEND_XPRT: // %ft
1652 src = fe->id;
1653 if (tmp->options & LOG_OPT_QUOTE)
1654 LOGCHAR('"');
1655 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1656 if (iret == 0)
1657 goto out;
1658 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001659 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02001660 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02001661 if (tmp->options & LOG_OPT_QUOTE)
1662 LOGCHAR('"');
1663 last_isspace = 0;
1664 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001665#ifdef USE_OPENSSL
1666 case LOG_FMT_SSL_CIPHER: // %sslc
1667 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001668 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001669 if (conn) {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001670 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001671 src = ssl_sock_get_cipher_name(conn);
1672 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001673 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1674 if (ret == NULL)
1675 goto out;
1676 tmplog = ret;
1677 last_isspace = 0;
1678 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001679
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001680 case LOG_FMT_SSL_VERSION: // %sslv
1681 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001682 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001683 if (conn) {
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001684 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001685 src = ssl_sock_get_proto_version(conn);
1686 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001687 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1688 if (ret == NULL)
1689 goto out;
1690 tmplog = ret;
1691 last_isspace = 0;
1692 break;
1693#endif
William Lallemand1d705562012-03-12 12:46:41 +01001694 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001695 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001696 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001697 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001698 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001699 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001700 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001701 break;
1702
William Lallemand1d705562012-03-12 12:46:41 +01001703 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001704 switch (obj_type(s->target)) {
1705 case OBJ_TYPE_SERVER:
1706 src = objt_server(s->target)->id;
1707 break;
1708 case OBJ_TYPE_APPLET:
1709 src = objt_applet(s->target)->name;
1710 break;
1711 default:
1712 src = "<NOSRV>";
1713 break;
1714 }
William Lallemand5f232402012-04-05 18:02:55 +02001715 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001716 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001717 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001718 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001719 last_isspace = 0;
1720 break;
1721
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001722 case LOG_FMT_Th: // %Th = handshake time
1723 ret = ltoa_o(s->logs.t_handshake, tmplog, dst + maxsize - tmplog);
1724 if (ret == NULL)
1725 goto out;
1726 tmplog = ret;
1727 last_isspace = 0;
1728 break;
1729
1730 case LOG_FMT_Ti: // %Ti = HTTP idle time
1731 ret = ltoa_o(s->logs.t_idle, tmplog, dst + maxsize - tmplog);
1732 if (ret == NULL)
1733 goto out;
1734 tmplog = ret;
1735 last_isspace = 0;
1736 break;
1737
1738 case LOG_FMT_TR: // %TR = HTTP request time
1739 ret = ltoa_o((t_request >= 0) ? t_request - s->logs.t_idle - s->logs.t_handshake : -1,
1740 tmplog, dst + maxsize - tmplog);
1741 if (ret == NULL)
1742 goto out;
1743 tmplog = ret;
1744 last_isspace = 0;
1745 break;
1746
1747 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02001748 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001749 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001750 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001751 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001752 last_isspace = 0;
1753 break;
1754
William Lallemand1d705562012-03-12 12:46:41 +01001755 case LOG_FMT_TW: // %Tw
1756 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001757 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001758 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001759 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001760 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001761 last_isspace = 0;
1762 break;
1763
William Lallemand1d705562012-03-12 12:46:41 +01001764 case LOG_FMT_TC: // %Tc
1765 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001766 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001767 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001768 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001769 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001770 last_isspace = 0;
1771 break;
1772
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001773 case LOG_FMT_Tr: // %Tr
William Lallemand1d705562012-03-12 12:46:41 +01001774 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001775 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001776 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001777 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001778 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001779 last_isspace = 0;
1780 break;
1781
Willy Tarreau27b639d2016-05-17 17:55:27 +02001782 case LOG_FMT_TD: // %Td
1783 if (s->be->mode == PR_MODE_HTTP)
1784 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_close - s->logs.t_data : -1,
1785 tmplog, dst + maxsize - tmplog);
1786 else
1787 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_close - s->logs.t_connect : -1,
1788 tmplog, dst + maxsize - tmplog);
1789 if (ret == NULL)
1790 goto out;
1791 tmplog = ret;
1792 last_isspace = 0;
1793 break;
1794
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001795 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
1796 if (!(fe->to_log & LW_BYTES))
1797 LOGCHAR('+');
1798 ret = ltoa_o(s->logs.t_close - (s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0),
1799 tmplog, dst + maxsize - tmplog);
1800 if (ret == NULL)
1801 goto out;
1802 tmplog = ret;
1803 last_isspace = 0;
1804 break;
1805
1806 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001807 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001808 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001809 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001810 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001811 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001812 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001813 last_isspace = 0;
1814 break;
1815
Willy Tarreau2beef582012-12-20 17:22:52 +01001816 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02001817 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001818 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001819 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001820 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001821 last_isspace = 0;
1822 break;
1823
William Lallemand1d705562012-03-12 12:46:41 +01001824 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001825 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001826 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001827 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001828 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001829 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001830 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001831 last_isspace = 0;
1832 break;
1833
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001834 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001835 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1836 if (ret == NULL)
1837 goto out;
1838 tmplog = ret;
1839 last_isspace = 0;
1840 break;
1841
Willy Tarreau2beef582012-12-20 17:22:52 +01001842 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02001843 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02001844 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001845 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001846 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001847 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001848 last_isspace = 0;
1849 break;
1850
Willy Tarreau2beef582012-12-20 17:22:52 +01001851 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02001852 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02001853 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001854 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001855 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001856 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001857 last_isspace = 0;
1858 break;
1859
William Lallemand1d705562012-03-12 12:46:41 +01001860 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001861 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1862 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001863 *tmplog = '\0';
1864 last_isspace = 0;
1865 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001866
William Lallemand1d705562012-03-12 12:46:41 +01001867 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001868 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1869 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02001870 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1871 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001872 last_isspace = 0;
1873 break;
1874
William Lallemand1d705562012-03-12 12:46:41 +01001875 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001876 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001877 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001878 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001879 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001880 last_isspace = 0;
1881 break;
1882
William Lallemand1d705562012-03-12 12:46:41 +01001883 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001884 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001885 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001886 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001887 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001888 last_isspace = 0;
1889 break;
1890
William Lallemand1d705562012-03-12 12:46:41 +01001891 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001892 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001893 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001894 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001895 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001896 last_isspace = 0;
1897 break;
1898
William Lallemand1d705562012-03-12 12:46:41 +01001899 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001900 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001901 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001902 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001903 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001904 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001905 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001906 last_isspace = 0;
1907 break;
1908
William Lallemand1d705562012-03-12 12:46:41 +01001909 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001910 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001911 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001912 ret = ltoa_o((s->si[1].conn_retries>0) ?
1913 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001914 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001915 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001916 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001917 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001918 last_isspace = 0;
1919 break;
1920
William Lallemand1d705562012-03-12 12:46:41 +01001921 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001922 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001923 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001924 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001925 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001926 last_isspace = 0;
1927 break;
1928
William Lallemand1d705562012-03-12 12:46:41 +01001929 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001930 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001931 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001932 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001933 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001934 last_isspace = 0;
1935 break;
1936
William Lallemand1d705562012-03-12 12:46:41 +01001937 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001938 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001939 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001940 if (tmp->options & LOG_OPT_QUOTE)
1941 LOGCHAR('"');
1942 LOGCHAR('{');
1943 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1944 if (hdr)
1945 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001946 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001947 ret = lf_encode_string(tmplog, dst + maxsize,
1948 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001949 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001950 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001951 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001952 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001953 }
1954 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001955 if (tmp->options & LOG_OPT_QUOTE)
1956 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001957 last_isspace = 0;
1958 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001959 break;
1960
William Lallemand1d705562012-03-12 12:46:41 +01001961 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001962 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001963 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001964 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1965 if (hdr > 0)
1966 LOGCHAR(' ');
1967 if (tmp->options & LOG_OPT_QUOTE)
1968 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001969 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001970 ret = lf_encode_string(tmplog, dst + maxsize,
1971 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001972 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001973 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001974 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001975 } else if (!(tmp->options & LOG_OPT_QUOTE))
1976 LOGCHAR('-');
1977 if (tmp->options & LOG_OPT_QUOTE)
1978 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001979 last_isspace = 0;
1980 }
1981 }
1982 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001983
William Lallemand1d705562012-03-12 12:46:41 +01001984
1985 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001986 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001987 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001988 if (tmp->options & LOG_OPT_QUOTE)
1989 LOGCHAR('"');
1990 LOGCHAR('{');
1991 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1992 if (hdr)
1993 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001994 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001995 ret = lf_encode_string(tmplog, dst + maxsize,
1996 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001997 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001998 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001999 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002000 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002001 }
2002 LOGCHAR('}');
2003 last_isspace = 0;
2004 if (tmp->options & LOG_OPT_QUOTE)
2005 LOGCHAR('"');
2006 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002007 break;
2008
William Lallemand1d705562012-03-12 12:46:41 +01002009 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002010 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002011 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002012 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2013 if (hdr > 0)
2014 LOGCHAR(' ');
2015 if (tmp->options & LOG_OPT_QUOTE)
2016 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002017 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002018 ret = lf_encode_string(tmplog, dst + maxsize,
2019 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002020 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002021 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002022 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002023 } else if (!(tmp->options & LOG_OPT_QUOTE))
2024 LOGCHAR('-');
2025 if (tmp->options & LOG_OPT_QUOTE)
2026 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002027 last_isspace = 0;
2028 }
2029 }
2030 break;
2031
William Lallemand1d705562012-03-12 12:46:41 +01002032 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002033 /* Request */
2034 if (tmp->options & LOG_OPT_QUOTE)
2035 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002036 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002037 ret = lf_encode_string(tmplog, dst + maxsize,
2038 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002039 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002040 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002041 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002042 if (tmp->options & LOG_OPT_QUOTE)
2043 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002044 last_isspace = 0;
2045 break;
William Lallemand5f232402012-04-05 18:02:55 +02002046
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002047 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002048 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002049
Willy Tarreaub7636d12015-06-17 19:58:02 +02002050 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002051 LOGCHAR('"');
2052
2053 end = uri + strlen(uri);
2054 // look for the first whitespace character
2055 while (uri < end && !HTTP_IS_SPHT(*uri))
2056 uri++;
2057
2058 // keep advancing past multiple spaces
2059 while (uri < end && HTTP_IS_SPHT(*uri)) {
2060 uri++; nspaces++;
2061 }
2062
2063 // look for first space or question mark after url
2064 spc = uri;
2065 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2066 spc++;
2067
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002068 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002069 chunk.str = "<BADREQ>";
2070 chunk.len = strlen("<BADREQ>");
2071 } else {
2072 chunk.str = uri;
2073 chunk.len = spc - uri;
2074 }
2075
Dragan Dosen835b9212016-02-12 13:23:03 +01002076 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002077 if (ret == NULL || *ret != '\0')
2078 goto out;
2079
2080 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002081 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002082 LOGCHAR('"');
2083
2084 last_isspace = 0;
2085 break;
2086
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002087 case LOG_FMT_HTTP_QUERY: // %HQ
2088 if (tmp->options & LOG_OPT_QUOTE)
2089 LOGCHAR('"');
2090
Willy Tarreau57bc8912016-04-25 17:09:40 +02002091 if (!txn || !txn->uri) {
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002092 chunk.str = "<BADREQ>";
2093 chunk.len = strlen("<BADREQ>");
2094 } else {
2095 uri = txn->uri;
2096 end = uri + strlen(uri);
2097 // look for the first question mark
2098 while (uri < end && *uri != '?')
2099 uri++;
2100
2101 qmark = uri;
2102 // look for first space or question mark after url
2103 while (uri < end && !HTTP_IS_SPHT(*uri))
2104 uri++;
2105
2106 chunk.str = qmark;
2107 chunk.len = uri - qmark;
2108 }
2109
Dragan Dosen835b9212016-02-12 13:23:03 +01002110 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002111 if (ret == NULL || *ret != '\0')
2112 goto out;
2113
2114 tmplog = ret;
2115 if (tmp->options & LOG_OPT_QUOTE)
2116 LOGCHAR('"');
2117
2118 last_isspace = 0;
2119 break;
2120
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002121 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002122 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002123
Willy Tarreaub7636d12015-06-17 19:58:02 +02002124 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002125 LOGCHAR('"');
2126
2127 end = uri + strlen(uri);
2128 // look for the first whitespace character
2129 while (uri < end && !HTTP_IS_SPHT(*uri))
2130 uri++;
2131
2132 // keep advancing past multiple spaces
2133 while (uri < end && HTTP_IS_SPHT(*uri)) {
2134 uri++; nspaces++;
2135 }
2136
2137 // look for first space after url
2138 spc = uri;
2139 while (spc < end && !HTTP_IS_SPHT(*spc))
2140 spc++;
2141
Willy Tarreau57bc8912016-04-25 17:09:40 +02002142 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002143 chunk.str = "<BADREQ>";
2144 chunk.len = strlen("<BADREQ>");
2145 } else {
2146 chunk.str = uri;
2147 chunk.len = spc - uri;
2148 }
2149
Dragan Dosen835b9212016-02-12 13:23:03 +01002150 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002151 if (ret == NULL || *ret != '\0')
2152 goto out;
2153
2154 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002155 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002156 LOGCHAR('"');
2157
2158 last_isspace = 0;
2159 break;
2160
2161 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002162 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002163 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002164 LOGCHAR('"');
2165
2166 end = uri + strlen(uri);
2167 // look for the first whitespace character
2168 spc = uri;
2169 while (spc < end && !HTTP_IS_SPHT(*spc))
2170 spc++;
2171
2172 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
2173 chunk.str = "<BADREQ>";
2174 chunk.len = strlen("<BADREQ>");
2175 } else {
2176 chunk.str = uri;
2177 chunk.len = spc - uri;
2178 }
2179
Dragan Dosen835b9212016-02-12 13:23:03 +01002180 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002181 if (ret == NULL || *ret != '\0')
2182 goto out;
2183
2184 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002185 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002186 LOGCHAR('"');
2187
2188 last_isspace = 0;
2189 break;
2190
2191 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002192 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002193 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002194 LOGCHAR('"');
2195
2196 end = uri + strlen(uri);
2197 // look for the first whitespace character
2198 while (uri < end && !HTTP_IS_SPHT(*uri))
2199 uri++;
2200
2201 // keep advancing past multiple spaces
2202 while (uri < end && HTTP_IS_SPHT(*uri)) {
2203 uri++; nspaces++;
2204 }
2205
2206 // look for the next whitespace character
2207 while (uri < end && !HTTP_IS_SPHT(*uri))
2208 uri++;
2209
2210 // keep advancing past multiple spaces
2211 while (uri < end && HTTP_IS_SPHT(*uri))
2212 uri++;
2213
Willy Tarreau57bc8912016-04-25 17:09:40 +02002214 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002215 chunk.str = "<BADREQ>";
2216 chunk.len = strlen("<BADREQ>");
2217 } else if (uri == end) {
2218 chunk.str = "HTTP/0.9";
2219 chunk.len = strlen("HTTP/0.9");
2220 } else {
2221 chunk.str = uri;
2222 chunk.len = end - uri;
2223 }
2224
Dragan Dosen835b9212016-02-12 13:23:03 +01002225 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002226 if (ret == NULL || *ret != '\0')
2227 goto out;
2228
2229 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002230 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002231 LOGCHAR('"');
2232
2233 last_isspace = 0;
2234 break;
2235
William Lallemand5f232402012-04-05 18:02:55 +02002236 case LOG_FMT_COUNTER: // %rt
2237 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002238 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002239 if (iret < 0 || iret > dst + maxsize - tmplog)
2240 goto out;
2241 last_isspace = 0;
2242 tmplog += iret;
2243 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002244 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002245 if (ret == NULL)
2246 goto out;
2247 tmplog = ret;
2248 last_isspace = 0;
2249 }
2250 break;
2251
Willy Tarreau7346acb2014-08-28 15:03:15 +02002252 case LOG_FMT_LOGCNT: // %lc
2253 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002254 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002255 if (iret < 0 || iret > dst + maxsize - tmplog)
2256 goto out;
2257 last_isspace = 0;
2258 tmplog += iret;
2259 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002260 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002261 if (ret == NULL)
2262 goto out;
2263 tmplog = ret;
2264 last_isspace = 0;
2265 }
2266 break;
2267
William Lallemand5f232402012-04-05 18:02:55 +02002268 case LOG_FMT_HOSTNAME: // %H
2269 src = hostname;
2270 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2271 if (ret == NULL)
2272 goto out;
2273 tmplog = ret;
2274 last_isspace = 0;
2275 break;
2276
2277 case LOG_FMT_PID: // %pid
2278 if (tmp->options & LOG_OPT_HEXA) {
2279 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2280 if (iret < 0 || iret > dst + maxsize - tmplog)
2281 goto out;
2282 last_isspace = 0;
2283 tmplog += iret;
2284 } else {
2285 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2286 if (ret == NULL)
2287 goto out;
2288 tmplog = ret;
2289 last_isspace = 0;
2290 }
2291 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002292
2293 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002294 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01002295 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002296 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002297 if (ret == NULL)
2298 goto out;
2299 tmplog = ret;
2300 last_isspace = 0;
2301 break;
2302
William Lallemandbddd4fd2012-02-27 11:23:10 +01002303 }
2304 }
2305
2306out:
William Lallemand1d705562012-03-12 12:46:41 +01002307 /* *tmplog is a unused character */
2308 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002309 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002310
Willy Tarreaubaaee002006-06-26 02:48:02 +02002311}
2312
William Lallemand1d705562012-03-12 12:46:41 +01002313/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002314 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002315 * Will not log if the frontend has no log defined.
2316 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002317void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002318{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002319 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002320 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002321 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002322
2323 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002324 err = (s->flags & SF_REDISP) ||
2325 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2326 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002327 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002328 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002329
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002330 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002331 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002332
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002333 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002334 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002335
Willy Tarreauabcd5142013-06-11 17:18:02 +02002336 if (s->logs.level) { /* loglevel was overridden */
2337 if (s->logs.level == -1) {
2338 s->logs.logwait = 0; /* logs disabled */
2339 return;
2340 }
2341 level = s->logs.level - 1;
2342 }
2343 else {
2344 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002345 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002346 level = LOG_ERR;
2347 }
William Lallemand1d705562012-03-12 12:46:41 +01002348
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002349 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002350 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002351 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002352 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002353 }
2354
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002355 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2356 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2357 &sess->fe->logformat_sd);
2358 }
2359
Dragan Dosen59cee972015-09-19 22:09:02 +02002360 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002361 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002362 sess->fe->log_count++;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002363 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002364 s->logs.logwait = 0;
2365 }
2366}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002367
Willy Tarreaubaaee002006-06-26 02:48:02 +02002368/*
2369 * Local variables:
2370 * c-indent-level: 8
2371 * c-basic-offset: 8
2372 * End:
2373 */