blob: 0d6e7fd33fcf0dcfc249e456f20bbf46aba3be23 [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
Christopher Fauletc1b730a2017-10-24 12:00:51 +020032#include <types/cli.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010034#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020035
Christopher Fauletc1b730a2017-10-24 12:00:51 +020036#include <proto/applet.h>
37#include <proto/cli.h>
William Lallemand5f232402012-04-05 18:02:55 +020038#include <proto/frontend.h>
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +000039#include <proto/proto_http.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020040#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010041#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020042#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010043#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020044#ifdef USE_OPENSSL
45#include <proto/ssl_sock.h>
46#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Dragan Dosen43885c72015-10-01 13:18:13 +020048struct log_fmt {
49 char *name;
50 struct {
51 struct chunk sep1; /* first pid separator */
52 struct chunk sep2; /* second pid separator */
53 } pid;
54};
55
56static const struct log_fmt log_formats[LOG_FORMATS] = {
57 [LOG_FORMAT_RFC3164] = {
58 .name = "rfc3164",
59 .pid = {
60 .sep1 = { .str = "[", .len = 1 },
61 .sep2 = { .str = "]: ", .len = 3 }
62 }
63 },
64 [LOG_FORMAT_RFC5424] = {
65 .name = "rfc5424",
66 .pid = {
67 .sep1 = { .str = " ", .len = 1 },
68 .sep2 = { .str = " - ", .len = 3 }
69 }
70 }
Dragan Dosen1322d092015-09-22 16:05:32 +020071};
72
Dragan Dosen835b9212016-02-12 13:23:03 +010073#define FD_SETS_ARE_BITFIELDS
74#ifdef FD_SETS_ARE_BITFIELDS
75/*
76 * This map is used with all the FD_* macros to check whether a particular bit
77 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
78 * which should be escaped. When FD_ISSET() returns non-zero, it means that the
79 * byte should be escaped. Be careful to always pass bytes from 0 to 255
80 * exclusively to the macros.
81 */
82fd_set rfc5424_escape_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
83
84#else
85#error "Check if your OS uses bitfields for fd_sets"
86#endif
87
Willy Tarreaubaaee002006-06-26 02:48:02 +020088const char *log_facilities[NB_LOG_FACILITIES] = {
89 "kern", "user", "mail", "daemon",
90 "auth", "syslog", "lpr", "news",
91 "uucp", "cron", "auth2", "ftp",
92 "ntp", "audit", "alert", "cron2",
93 "local0", "local1", "local2", "local3",
94 "local4", "local5", "local6", "local7"
95};
96
Willy Tarreaubaaee002006-06-26 02:48:02 +020097const char *log_levels[NB_LOG_LEVELS] = {
98 "emerg", "alert", "crit", "err",
99 "warning", "notice", "info", "debug"
100};
101
Willy Tarreau570f2212013-06-10 16:42:09 +0200102const 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 +0200103const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104
William Lallemand723b73a2012-02-08 16:37:49 +0100105
106/* log_format */
107struct logformat_type {
108 char *name;
109 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100110 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200111 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100112 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100113 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100114};
115
William Lallemandb7ff6a32012-03-02 14:35:21 +0100116int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
117
William Lallemand723b73a2012-02-08 16:37:49 +0100118/* log_format variable names */
119static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200120 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100121
122 /* please keep these lines sorted ! */
123 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
124 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
125 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
126 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
127 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200128 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200129 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200130 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100131 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200132 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
133 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
134 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
135 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
136 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
137 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200138 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100139 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200140 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +0100141 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
142 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200143 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100144 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200145 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100146 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
147 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200148 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200149 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
150 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100151 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
152 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200153 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
154 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100155 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200156 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
157 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
158 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
159 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000160 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
161 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000162 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000163 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
164 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200165 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100166 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200167 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100168 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
169 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100170 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100171 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
172 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
173 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
174 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
175 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200176 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
177 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100178 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200179 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
180 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
181 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100182 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
183 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
184
185 /* The following tags are deprecated and will be removed soon */
186 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
187 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200188 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
189 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
190 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
191 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100192 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
193 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
194 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
195 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
196 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200197 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100198};
199
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200200char 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
201char 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 +0100202char 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 +0100203char *log_format = NULL;
204
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200205/* Default string used for structured-data part in RFC5424 formatted
206 * syslog messages.
207 */
208char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200209
210/* This is a global syslog header, common to all outgoing messages in
211 * RFC3164 format. It begins with time-based part and is updated by
212 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200213 */
214char *logheader = NULL;
215
Dragan Dosen1322d092015-09-22 16:05:32 +0200216/* This is a global syslog header for messages in RFC5424 format. It is
217 * updated by update_log_hdr_rfc5424().
218 */
219char *logheader_rfc5424 = NULL;
220
Dragan Dosen59cee972015-09-19 22:09:02 +0200221/* This is a global syslog message buffer, common to all outgoing
222 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100223 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200224char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100225
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200226/* A global syslog message buffer, common to all RFC5424 syslog messages.
227 * Currently, it is used for generating the structured-data part.
228 */
229char *logline_rfc5424 = NULL;
230
Christopher Fauletd4696382017-10-24 11:44:05 +0200231/* A global buffer used to store all startup alerts/warnings. It will then be
232 * retrieve on the CLI. */
233static char *startup_logs = NULL;
234
William Lallemand723b73a2012-02-08 16:37:49 +0100235struct logformat_var_args {
236 char *name;
237 int mask;
238};
239
240struct logformat_var_args var_args_list[] = {
241// global
242 { "M", LOG_OPT_MANDATORY },
243 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200244 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100245 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100246 { 0, 0 }
247};
248
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200249/* return the name of the directive used in the current proxy for which we're
250 * currently parsing a header, when it is known.
251 */
252static inline const char *fmt_directive(const struct proxy *curproxy)
253{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100254 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200255 case ARGC_ACL:
256 return "acl";
257 case ARGC_STK:
258 return "stick";
259 case ARGC_TRK:
260 return "track-sc";
261 case ARGC_LOG:
262 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200263 case ARGC_LOGSD:
264 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100265 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100266 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100267 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100268 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200269 case ARGC_UIF:
270 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100271 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200272 return "redirect";
273 case ARGC_CAP:
274 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200275 case ARGC_SRV:
276 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200277 case ARGC_SPOE:
278 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100279 case ARGC_UBK:
280 return "use_backend";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100281 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200282 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100283 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200284}
285
William Lallemand723b73a2012-02-08 16:37:49 +0100286/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100287 * callback used to configure addr source retrieval
288 */
289int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
290{
291 curproxy->options2 |= PR_O2_SRC_ADDR;
292
293 return 0;
294}
295
296
297/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100298 * Parse args in a logformat_var. Returns 0 in error
299 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100300 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100301int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100302{
303 int i = 0;
304 int end = 0;
305 int flags = 0; // 1 = + 2 = -
306 char *sp = NULL; // start pointer
307
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100308 if (args == NULL) {
309 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100310 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100311 }
William Lallemand723b73a2012-02-08 16:37:49 +0100312
313 while (1) {
314 if (*args == '\0')
315 end = 1;
316
317 if (*args == '+') {
318 // add flag
319 sp = args + 1;
320 flags = 1;
321 }
322 if (*args == '-') {
323 // delete flag
324 sp = args + 1;
325 flags = 2;
326 }
327
328 if (*args == '\0' || *args == ',') {
329 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100330 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100331 if (strcmp(sp, var_args_list[i].name) == 0) {
332 if (flags == 1) {
333 node->options |= var_args_list[i].mask;
334 break;
335 } else if (flags == 2) {
336 node->options &= ~var_args_list[i].mask;
337 break;
338 }
339 }
340 }
341 sp = NULL;
342 if (end)
343 break;
344 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100345 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100346 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100347 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100348}
349
350/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100351 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
352 * must pass the args part in the <arg> pointer with its length in <arg_len>,
353 * and varname with its length in <var> and <var_len> respectively. <arg> is
354 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100355 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100356 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100357int 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 +0100358{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100359 int j;
360 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100361
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100362 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
363 if (strlen(logformat_keywords[j].name) == var_len &&
364 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
365 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200366 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100367 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100368 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100369 return 0;
370 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100371 node->type = logformat_keywords[j].type;
372 node->options = *defoptions;
373 if (arg_len) {
374 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100375 if (!parse_logformat_var_args(node->arg, node, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100376 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100377 }
378 if (node->type == LOG_FMT_GLOBAL) {
379 *defoptions = node->options;
380 free(node->arg);
381 free(node);
382 } else {
383 if (logformat_keywords[j].config_callback &&
384 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100385 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100386 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100387 curproxy->to_log |= logformat_keywords[j].lw;
388 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100389 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100390 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100391 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200392 curproxy->conf.args.file, curproxy->conf.args.line,
393 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100394 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100395 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100396 memprintf(err, "format variable '%s' is reserved for HTTP mode",
397 logformat_keywords[j].name);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100398 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100399 }
William Lallemand723b73a2012-02-08 16:37:49 +0100400 }
401 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100402
403 j = var[var_len];
404 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100405 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 +0100406 var[var_len] = j;
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100407 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100408}
409
410/*
411 * push to the logformat linked list
412 *
413 * start: start pointer
414 * end: end text pointer
415 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100416 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100417 *
418 * LOG_TEXT: copy chars from start to end excluding end.
419 *
420*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100421int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100422{
423 char *str;
424
Willy Tarreaua3571662012-12-20 21:59:12 +0100425 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200426 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100427 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100428 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100429 return 0;
430 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200431 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100432 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100433 str[end - start] = '\0';
434 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100435 node->type = LOG_FMT_TEXT; // type string
436 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100437 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200438 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100439 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100440 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100441 return 0;
442 }
William Lallemand1d705562012-03-12 12:46:41 +0100443 node->type = LOG_FMT_SEPARATOR;
444 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100445 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100446 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100447}
448
449/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100450 * Parse the sample fetch expression <text> and add a node to <list_format> upon
451 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200452 * should work. The curpx->conf.args.ctx must be set by the caller.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100453 *
454 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100455 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100456int 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 +0100457{
458 char *cmd[2];
459 struct sample_expr *expr;
460 struct logformat_node *node;
461 int cmd_arg;
462
463 cmd[0] = text;
464 cmd[1] = "";
465 cmd_arg = 0;
466
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100467 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 +0100468 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100469 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100470 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100471 }
472
Vincent Bernat02779b62016-04-03 13:48:43 +0200473 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100474 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100475 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100476 return 0;
477 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100478 node->type = LOG_FMT_EXPR;
479 node->expr = expr;
480 node->options = options;
481
482 if (arg_len) {
483 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100484 if (!parse_logformat_var_args(node->arg, node, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100485 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100486 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100487 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100488 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
489
Willy Tarreau434c57c2013-01-08 01:10:24 +0100490 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100491 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
492
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100493 if (!(expr->fetch->val & cap)) {
David Carlier93e8b882017-09-21 14:36:43 +0000494 free(node);
495 node = NULL;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100496 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
497 text, sample_src_names(expr->fetch->use));
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100498 return 0;
499 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100500
Willy Tarreauc8368452012-12-21 00:09:23 +0100501 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
502 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100503 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100504
William Lallemand65ad6e12014-01-31 15:08:02 +0100505 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
506 * needed with some sample fetches (eg: ssl*). We always set it for
507 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100508 */
509 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100510 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100511 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100512 return 1;
Willy Tarreauc8368452012-12-21 00:09:23 +0100513}
514
515/*
William Lallemand723b73a2012-02-08 16:37:49 +0100516 * Parse the log_format string and fill a linked list.
517 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200518 * You can set arguments using { } : %{many arguments}varname.
519 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100520 *
521 * str: the string to parse
522 * curproxy: the proxy affected
523 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100524 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100525 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100526 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100527 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100528 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100529int 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 +0100530{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100531 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100532 char *arg = NULL; /* start pointer for args */
533 char *var = NULL; /* start pointer for vars */
534 int arg_len = 0;
535 int var_len = 0;
536 int cformat; /* current token format */
537 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100538 struct logformat_node *tmplf, *back;
539
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100540 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100541 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100542 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100543 return 0;
544 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200545 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200546
William Lallemand723b73a2012-02-08 16:37:49 +0100547 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100548 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100549 LIST_DEL(&tmplf->list);
550 free(tmplf);
551 }
552
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100553 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100554 pformat = cformat;
555
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100556 if (!*str)
557 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100558
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100559 /* The prinicple of the two-step state machine below is to first detect a change, and
560 * second have all common paths processed at one place. The common paths are the ones
561 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
562 * We use the common LF_INIT state to dispatch to the different final states.
563 */
564 switch (pformat) {
565 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100566 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100567 arg_len = var_len = 0;
568 if (*str == '{') { // optional argument
569 cformat = LF_STARG;
570 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100571 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100572 else if (*str == '[') {
573 cformat = LF_STEXPR;
574 var = str + 1; // store expr in variable name
575 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100576 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100577 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100578 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100579 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100580 else if (*str == '%')
581 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100582 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100583 /* single '%' followed by blank or digit, send them both */
584 cformat = LF_TEXT;
585 pformat = LF_TEXT; /* finally we include the previous char as well */
586 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600587 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 +0100588 *str, (int)(str - backfmt), fmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100589 return 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100590
591 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100592 else
593 cformat = LF_INIT; // handle other cases of litterals
594 break;
595
596 case LF_STARG: // text immediately following '%{'
597 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100598 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100599 arg_len = str - arg;
600 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100601 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100602 break;
603
604 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100605 if (*str == '[') {
606 cformat = LF_STEXPR;
607 var = str + 1; // store expr in variable name
608 break;
609 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100610 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100611 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100612 var = str;
613 break;
614 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100615 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100616 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100617
Willy Tarreauc8368452012-12-21 00:09:23 +0100618 case LF_STEXPR: // text immediately following '%['
619 if (*str == ']') { // end of arg
620 cformat = LF_EDEXPR;
621 var_len = str - var;
622 *str = 0; // needed for parsing the expression
623 }
624 break;
625
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100626 case LF_VAR: // text part of a variable name
627 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100628 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100629 cformat = LF_INIT; // not variable name anymore
630 break;
631
Willy Tarreauc8368452012-12-21 00:09:23 +0100632 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100633 cformat = LF_INIT;
634 }
635
636 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
637 switch (*str) {
638 case '%': cformat = LF_STARTVAR; break;
639 case ' ': cformat = LF_SEPARATOR; break;
640 case 0 : cformat = LF_END; break;
641 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100642 }
643 }
644
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100645 if (cformat != pformat || pformat == LF_SEPARATOR) {
646 switch (pformat) {
647 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100648 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100649 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100650 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100651 case LF_STEXPR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100652 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 +0100653 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100654 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100655 case LF_TEXT:
656 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100657 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100658 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100659 break;
660 }
661 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100662 }
663 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100664
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100665 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100666 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100667 return 0;
668 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100669 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100670
671 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100672}
673
Christopher Fauletd4696382017-10-24 11:44:05 +0200674/* Generic function to display messages prefixed by a label */
675static void print_message(const char *label, const char *fmt, va_list argp)
676{
677 struct tm tm;
678 char *head, *msg;
679
680 head = msg = NULL;
681
682 get_localtime(date.tv_sec, &tm);
683 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
684 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
685 memvprintf(&msg, fmt, argp);
686
687 if (global.mode & MODE_STARTING)
688 memprintf(&startup_logs, "%s%s%s", (startup_logs ? startup_logs : ""), head, msg);
689
690 fprintf(stderr, "%s%s", head, msg);
691 fflush(stderr);
692
693 free(head);
694 free(msg);
695}
696
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697/*
698 * Displays the message on stderr with the date and pid. Overrides the quiet
699 * mode during startup.
700 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200701void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200702{
703 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200704
705 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
706 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +0200707 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708 va_end(argp);
709 }
710}
711
712
713/*
714 * Displays the message on stderr with the date and pid.
715 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200716void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717{
718 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719
720 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
721 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +0200722 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 va_end(argp);
724 }
725}
726
727/*
728 * Displays the message on <out> only if quiet mode is not set.
729 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200730void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731{
732 va_list argp;
733
734 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
735 va_start(argp, fmt);
736 vfprintf(out, fmt, argp);
737 fflush(out);
738 va_end(argp);
739 }
740}
741
742/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200743 * returns log format for <fmt> or -1 if not found.
744 */
745int get_log_format(const char *fmt)
746{
747 int format;
748
749 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200750 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200751 format--;
752
753 return format;
754}
755
756/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 * returns log level for <lev> or -1 if not found.
758 */
759int get_log_level(const char *lev)
760{
761 int level;
762
763 level = NB_LOG_LEVELS - 1;
764 while (level >= 0 && strcmp(log_levels[level], lev))
765 level--;
766
767 return level;
768}
769
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770/*
771 * returns log facility for <fac> or -1 if not found.
772 */
773int get_log_facility(const char *fac)
774{
775 int facility;
776
777 facility = NB_LOG_FACILITIES - 1;
778 while (facility >= 0 && strcmp(log_facilities[facility], fac))
779 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100780
Willy Tarreaubaaee002006-06-26 02:48:02 +0200781 return facility;
782}
783
William Lallemanda1cc3812012-02-08 16:38:44 +0100784/*
Dragan Dosen835b9212016-02-12 13:23:03 +0100785 * Encode the string.
786 *
787 * When using the +E log format option, it will try to escape '"\]'
788 * characters with '\' as prefix. The same prefix should not be used as
789 * <escape>.
790 */
791static char *lf_encode_string(char *start, char *stop,
792 const char escape, const fd_set *map,
793 const char *string,
794 struct logformat_node *node)
795{
796 if (node->options & LOG_OPT_ESC) {
797 if (start < stop) {
798 stop--; /* reserve one byte for the final '\0' */
799 while (start < stop && *string != '\0') {
800 if (!FD_ISSET((unsigned char)(*string), map)) {
801 if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map))
802 *start++ = *string;
803 else {
804 if (start + 2 >= stop)
805 break;
806 *start++ = '\\';
807 *start++ = *string;
808 }
809 }
810 else {
811 if (start + 3 >= stop)
812 break;
813 *start++ = escape;
814 *start++ = hextab[(*string >> 4) & 15];
815 *start++ = hextab[*string & 15];
816 }
817 string++;
818 }
819 *start = '\0';
820 }
821 }
822 else {
823 return encode_string(start, stop, escape, map, string);
824 }
825
826 return start;
827}
828
829/*
830 * Encode the chunk.
831 *
832 * When using the +E log format option, it will try to escape '"\]'
833 * characters with '\' as prefix. The same prefix should not be used as
834 * <escape>.
835 */
836static char *lf_encode_chunk(char *start, char *stop,
837 const char escape, const fd_set *map,
838 const struct chunk *chunk,
839 struct logformat_node *node)
840{
841 char *str, *end;
842
843 if (node->options & LOG_OPT_ESC) {
844 if (start < stop) {
845 str = chunk->str;
846 end = chunk->str + chunk->len;
847
848 stop--; /* reserve one byte for the final '\0' */
849 while (start < stop && str < end) {
850 if (!FD_ISSET((unsigned char)(*str), map)) {
851 if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map))
852 *start++ = *str;
853 else {
854 if (start + 2 >= stop)
855 break;
856 *start++ = '\\';
857 *start++ = *str;
858 }
859 }
860 else {
861 if (start + 3 >= stop)
862 break;
863 *start++ = escape;
864 *start++ = hextab[(*str >> 4) & 15];
865 *start++ = hextab[*str & 15];
866 }
867 str++;
868 }
869 *start = '\0';
870 }
871 }
872 else {
873 return encode_chunk(start, stop, escape, map, chunk);
874 }
875
876 return start;
877}
878
879/*
William Lallemanda1cc3812012-02-08 16:38:44 +0100880 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +0100881 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +0100882 *
883 * Return the adress of the \0 character, or NULL on error
884 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100885char *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 +0100886{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100887 if (size < 2)
888 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100889
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100890 if (node->options & LOG_OPT_QUOTE) {
891 *(dst++) = '"';
892 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100893 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100894
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100895 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +0200896 if (++len > size)
897 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +0100898 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +0100899 char *ret;
900
Dragan Dosendb1b6f92016-07-25 11:35:02 +0200901 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +0100902 if (ret == NULL || *ret != '\0')
903 return NULL;
904 len = ret - dst;
905 }
906 else {
Dragan Dosen835b9212016-02-12 13:23:03 +0100907 len = strlcpy2(dst, src, len);
908 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100909
910 size -= len;
911 dst += len;
912 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100913 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
914 if (size < 2)
915 return NULL;
916 *(dst++) = '-';
917 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100918
919 if (node->options & LOG_OPT_QUOTE) {
920 if (size < 2)
921 return NULL;
922 *(dst++) = '"';
923 }
924
925 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100926 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100927}
928
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100929static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
930{
931 return lf_text_len(dst, src, size, size, node);
932}
933
William Lallemand5f232402012-04-05 18:02:55 +0200934/*
935 * Write a IP adress to the log string
936 * +X option write in hexadecimal notation, most signifant byte on the left
937 */
938char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
939{
940 char *ret = dst;
941 int iret;
942 char pn[INET6_ADDRSTRLEN];
943
944 if (node->options & LOG_OPT_HEXA) {
945 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
946 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
947 if (iret < 0 || iret > size)
948 return NULL;
949 ret += iret;
950 } else {
951 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
952 ret = lf_text(dst, pn, size, node);
953 if (ret == NULL)
954 return NULL;
955 }
956 return ret;
957}
958
959/*
960 * Write a port to the log
961 * +X option write in hexadecimal notation, most signifant byte on the left
962 */
963char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
964{
965 char *ret = dst;
966 int iret;
967
968 if (node->options & LOG_OPT_HEXA) {
969 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
970 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
971 if (iret < 0 || iret > size)
972 return NULL;
973 ret += iret;
974 } else {
975 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
976 if (ret == NULL)
977 return NULL;
978 }
979 return ret;
980}
981
Dragan Dosen1322d092015-09-22 16:05:32 +0200982/* Re-generate time-based part of the syslog header in RFC3164 format at
983 * the beginning of logheader once a second and return the pointer to the
984 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100985 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200986static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200987{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100988 static long tvsec;
989 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Dragan Dosen43885c72015-10-01 13:18:13 +0200990 static struct chunk host = { NULL, 0, 0 };
991 static int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200993 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200995 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100996 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200997
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200998 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +0200999 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001000
Dragan Dosen43885c72015-10-01 13:18:13 +02001001 if (unlikely(global.log_send_hostname != host.str)) {
1002 host.str = global.log_send_hostname;
1003 host.len = host.str ? strlen(host.str) : 0;
1004 sep = host.len ? 1 : 0;
1005 }
1006
Dragan Dosen59cee972015-09-19 22:09:02 +02001007 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +02001008 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +02001009 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +02001010 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
1011 host.len, host.str, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012 /* WARNING: depending upon implementations, snprintf may return
1013 * either -1 or the number of bytes that would be needed to store
1014 * the total message. In both cases, we must adjust it.
1015 */
Willy Tarreau18324f52014-06-27 18:10:07 +02001016 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1017 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018
Dragan Dosen59cee972015-09-19 22:09:02 +02001019 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001020 }
1021
Willy Tarreau094af4e2015-01-07 15:03:42 +01001022 dataptr[0] = 0; // ensure we get rid of any previous attempt
1023
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001024 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001025}
1026
Dragan Dosen1322d092015-09-22 16:05:32 +02001027/* Re-generate time-based part of the syslog header in RFC5424 format at
1028 * the beginning of logheader_rfc5424 once a second and return the pointer
1029 * to the first character after it.
1030 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001031static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +02001032{
1033 static long tvsec;
1034 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001035 const char *gmt_offset;
Dragan Dosen1322d092015-09-22 16:05:32 +02001036
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001037 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +02001038 /* this string is rebuild only once a second */
1039 struct tm tm;
1040 int hdr_len;
1041
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001042 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +02001043 get_localtime(tvsec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001044 gmt_offset = get_gmt_offset(time, &tm);
Dragan Dosen1322d092015-09-22 16:05:32 +02001045
1046 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +02001047 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +02001048 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +02001049 tm.tm_hour, tm.tm_min, tm.tm_sec,
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001050 gmt_offset, gmt_offset+3,
Dragan Dosen43885c72015-10-01 13:18:13 +02001051 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +02001052 /* WARNING: depending upon implementations, snprintf may return
1053 * either -1 or the number of bytes that would be needed to store
1054 * the total message. In both cases, we must adjust it.
1055 */
1056 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1057 hdr_len = global.max_syslog_len;
1058
1059 dataptr = logheader_rfc5424 + hdr_len;
1060 }
1061
1062 dataptr[0] = 0; // ensure we get rid of any previous attempt
1063
1064 return dataptr;
1065}
1066
William Lallemand2a4a44f2012-02-06 16:00:33 +01001067/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001068 * This function sends the syslog message using a printf format string. It
1069 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001070 */
1071void send_log(struct proxy *p, int level, const char *format, ...)
1072{
1073 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001074 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001075
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001076 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001077 return;
1078
William Lallemand2a4a44f2012-02-06 16:00:33 +01001079 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001080 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001081 if (data_len < 0 || data_len > global.max_syslog_len)
1082 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001083 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001084
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001085 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001086}
1087
1088/*
1089 * This function sends a syslog message.
1090 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001091 * It overrides the last byte of the message vector with an LF character.
1092 * The arguments <sd> and <sd_size> are used for the structured-data part
1093 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001094 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001095void __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 +01001096{
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001097 static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1098 static struct msghdr msghdr = {
1099 .msg_iov = iovec,
1100 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1101 };
William Lallemand2a4a44f2012-02-06 16:00:33 +01001102 static int logfdunix = -1; /* syslog to AF_UNIX socket */
1103 static int logfdinet = -1; /* syslog to AF_INET socket */
1104 static char *dataptr = NULL;
1105 int fac_level;
1106 struct list *logsrvs = NULL;
1107 struct logsrv *tmp = NULL;
1108 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +02001109 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001110 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001111 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001112 struct chunk *tag = &global.log_tag;
1113 static int curr_pid;
1114 static char pidstr[100];
1115 static struct chunk pid;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001116
1117 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001118
1119 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +02001120 if (!LIST_ISEMPTY(&global.logsrvs)) {
1121 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001122 }
1123 } else {
William Lallemand0f99e342011-10-12 17:50:54 +02001124 if (!LIST_ISEMPTY(&p->logsrvs)) {
1125 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001126 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001127 if (p->log_tag.str) {
1128 tag = &p->log_tag;
1129 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001130 }
1131
William Lallemand0f99e342011-10-12 17:50:54 +02001132 if (!logsrvs)
1133 return;
1134
Dragan Dosen43885c72015-10-01 13:18:13 +02001135 if (unlikely(curr_pid != getpid())) {
1136 curr_pid = getpid();
1137 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1138 chunk_initstr(&pid, pidstr);
1139 }
1140
Robert Tsai81ae1952007-12-05 10:47:29 +01001141 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +02001142 nblogger = 0;
1143 list_for_each_entry(tmp, logsrvs, list) {
1144 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +01001145 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +01001146 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +02001147 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +01001148 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001149 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +02001150 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001151 int tag_max = 0;
1152 int pid_sep1_max = 0;
1153 int pid_max = 0;
1154 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001155 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001156 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +01001157
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001158 nblogger++;
1159
Willy Tarreaubaaee002006-06-26 02:48:02 +02001160 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +02001161 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001162 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001163
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001164 if (unlikely(*plogfd < 0)) {
1165 /* socket not successfully initialized yet */
1166 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
1167
1168 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
1169 Alert("socket for logger #%d failed: %s (errno=%d)\n",
1170 nblogger, strerror(errno), errno);
1171 continue;
1172 }
1173 /* we don't want to receive anything on this socket */
1174 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1175 /* does nothing under Linux, maybe needed for others */
1176 shutdown(*plogfd, SHUT_RD);
1177 }
1178
Dragan Dosen1322d092015-09-22 16:05:32 +02001179 switch (logsrv->format) {
1180 case LOG_FORMAT_RFC3164:
1181 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001182 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +02001183 break;
1184
1185 case LOG_FORMAT_RFC5424:
1186 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001187 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001188 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001189 break;
1190
1191 default:
1192 continue; /* must never happen */
1193 }
1194
1195 hdr_size = hdr_ptr - hdr;
1196
Willy Tarreaubaaee002006-06-26 02:48:02 +02001197 /* For each target, we may have a different facility.
1198 * We can also have a different log level for each message.
1199 * This induces variations in the message header length.
1200 * Since we don't want to recompute it each time, nor copy it every
1201 * time, we only change the facility in the pre-computed header,
1202 * and we change the pointer to the header accordingly.
1203 */
William Lallemand0f99e342011-10-12 17:50:54 +02001204 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001205 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001206 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001207 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001208 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001209 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001210 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001211 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001212
Dragan Dosen1322d092015-09-22 16:05:32 +02001213 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001214
Dragan Dosen43885c72015-10-01 13:18:13 +02001215 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001216 if (unlikely(hdr_size >= logsrv->maxlen)) {
1217 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001218 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001219 goto send;
1220 }
1221
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001222 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001223
Dragan Dosen43885c72015-10-01 13:18:13 +02001224 /* tag */
1225 tag_max = tag->len;
1226 if (unlikely(tag_max >= maxlen)) {
1227 tag_max = maxlen - 1;
1228 sd_max = 0;
1229 goto send;
1230 }
1231
1232 maxlen -= tag_max;
1233
1234 /* first pid separator */
1235 pid_sep1_max = log_formats[logsrv->format].pid.sep1.len;
1236 if (unlikely(pid_sep1_max >= maxlen)) {
1237 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001238 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001239 goto send;
1240 }
1241
Dragan Dosen43885c72015-10-01 13:18:13 +02001242 pid_sep1 = log_formats[logsrv->format].pid.sep1.str;
1243 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001244
Dragan Dosen43885c72015-10-01 13:18:13 +02001245 /* pid */
1246 pid_max = pid.len;
1247 if (unlikely(pid_max >= maxlen)) {
1248 pid_max = maxlen - 1;
1249 sd_max = 0;
1250 goto send;
1251 }
1252
1253 maxlen -= pid_max;
1254
1255 /* second pid separator */
1256 pid_sep2_max = log_formats[logsrv->format].pid.sep2.len;
1257 if (unlikely(pid_sep2_max >= maxlen)) {
1258 pid_sep2_max = maxlen - 1;
1259 sd_max = 0;
1260 goto send;
1261 }
1262
1263 pid_sep2 = log_formats[logsrv->format].pid.sep2.str;
1264 maxlen -= pid_sep2_max;
1265
1266 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001267 if (sd_max >= maxlen) {
1268 sd_max = maxlen - 1;
1269 goto send;
1270 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001271
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001272 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001273send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001274 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001275 iovec[0].iov_len = hdr_max;
1276 iovec[1].iov_base = tag->str;
1277 iovec[1].iov_len = tag_max;
1278 iovec[2].iov_base = pid_sep1;
1279 iovec[2].iov_len = pid_sep1_max;
1280 iovec[3].iov_base = pid.str;
1281 iovec[3].iov_len = pid_max;
1282 iovec[4].iov_base = pid_sep2;
1283 iovec[4].iov_len = pid_sep2_max;
1284 iovec[5].iov_base = sd;
1285 iovec[5].iov_len = sd_max;
1286 iovec[6].iov_base = dataptr;
1287 iovec[6].iov_len = max;
1288 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1289 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001290
1291 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1292 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1293
1294 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001295
Robert Tsai81ae1952007-12-05 10:47:29 +01001296 if (sent < 0) {
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001297 Alert("sendmsg logger #%d failed: %s (errno=%d)\n",
Robert Tsai81ae1952007-12-05 10:47:29 +01001298 nblogger, strerror(errno), errno);
1299 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001300 }
1301}
1302
William Lallemandbddd4fd2012-02-27 11:23:10 +01001303extern fd_set hdr_encode_map[];
1304extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001305extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001306
Willy Tarreaubaaee002006-06-26 02:48:02 +02001307
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001308const 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 +01001309const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1310 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1311 Set-cookie Updated, unknown, unknown */
1312
William Lallemand1d705562012-03-12 12:46:41 +01001313/*
1314 * try to write a character if there is enough space, or goto out
1315 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001316#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001317 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001318 *(tmplog++) = (x); \
1319 } else { \
1320 goto out; \
1321 } \
1322 } while(0)
1323
Dragan Dosen835b9212016-02-12 13:23:03 +01001324
1325/* Initializes some log data.
1326 */
1327void init_log()
1328{
1329 char *tmp;
1330
1331 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1332 * inside PARAM-VALUE should be escaped with '\' as prefix.
1333 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1334 * details.
1335 */
1336 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1337
1338 tmp = "\"\\]";
1339 while (*tmp) {
1340 FD_SET(*tmp, rfc5424_escape_map);
1341 tmp++;
1342 }
1343}
William Lallemand1d705562012-03-12 12:46:41 +01001344
Christopher Faulet0132d062017-07-26 15:33:35 +02001345/* Initialize log buffers used for syslog messages */
1346int init_log_buffers()
1347{
1348 logheader = my_realloc2(logheader, global.max_syslog_len + 1);
1349 logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1);
1350 logline = my_realloc2(logline, global.max_syslog_len + 1);
1351 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
1352 if (!logheader || !logline_rfc5424 || !logline || !logline_rfc5424)
1353 return 0;
1354 return 1;
1355}
1356
1357/* Deinitialize log buffers used for syslog messages */
1358void deinit_log_buffers()
1359{
1360 free(logheader);
1361 free(logheader_rfc5424);
1362 free(logline);
1363 free(logline_rfc5424);
Christopher Fauletd4696382017-10-24 11:44:05 +02001364 free(startup_logs);
Christopher Faulet0132d062017-07-26 15:33:35 +02001365 logheader = NULL;
1366 logheader_rfc5424 = NULL;
1367 logline = NULL;
1368 logline_rfc5424 = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +02001369 startup_logs = NULL;
Christopher Faulet0132d062017-07-26 15:33:35 +02001370}
1371
Willy Tarreaudf974472012-12-28 02:44:01 +01001372/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1373 * <maxsize> characters. Returns the size of the output string in characters,
1374 * not counting the trailing zero which is always added if the resulting size
1375 * is not zero.
1376 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001377int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001378{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001379 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001380 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +01001381 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001382 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001383 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001384 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001385 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001386 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001387 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001388 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001389 int t_request;
1390 int hdr;
1391 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001392 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001393 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001394 char *ret;
1395 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001396 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001397 struct timeval tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001398
William Lallemandbddd4fd2012-02-27 11:23:10 +01001399 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001400
William Lallemandbddd4fd2012-02-27 11:23:10 +01001401 t_request = -1;
1402 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1403 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1404
William Lallemand1d705562012-03-12 12:46:41 +01001405 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001406
William Lallemandbddd4fd2012-02-27 11:23:10 +01001407 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001408 if (LIST_ISEMPTY(list_format))
1409 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001410
William Lallemand1d705562012-03-12 12:46:41 +01001411 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001412 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001413 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001414 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001415 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001416
Willy Tarreauc8368452012-12-21 00:09:23 +01001417 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001418 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001419 if (!last_isspace) {
1420 LOGCHAR(' ');
1421 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001422 }
1423 break;
1424
William Lallemand1d705562012-03-12 12:46:41 +01001425 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001426 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001427 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001428 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001429 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001430 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001431 last_isspace = 0;
1432 break;
1433
Willy Tarreauc8368452012-12-21 00:09:23 +01001434 case LOG_FMT_EXPR: // sample expression, may be request or response
1435 key = NULL;
1436 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001437 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 +01001438 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001439 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 +01001440 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01001441 ret = lf_encode_chunk(tmplog, dst + maxsize,
1442 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001443 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001444 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 +01001445 if (ret == 0)
1446 goto out;
1447 tmplog = ret;
1448 last_isspace = 0;
1449 break;
1450
Willy Tarreau2beef582012-12-20 17:22:52 +01001451 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001452 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001453 if (conn)
1454 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1455 else
1456 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001457 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001458 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001459 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001460 last_isspace = 0;
1461 break;
1462
Willy Tarreau2beef582012-12-20 17:22:52 +01001463 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001464 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001465 if (conn) {
1466 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001467 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001468 } else {
1469 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1470 dst + maxsize - tmplog, tmp);
1471 }
William Lallemand5f232402012-04-05 18:02:55 +02001472 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001473 else
1474 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1475
William Lallemand5f232402012-04-05 18:02:55 +02001476 if (ret == NULL)
1477 goto out;
1478 tmplog = ret;
1479 last_isspace = 0;
1480 break;
1481
Willy Tarreau2beef582012-12-20 17:22:52 +01001482 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001483 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001484 if (conn) {
1485 conn_get_to_addr(conn);
1486 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1487 }
1488 else
1489 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1490
William Lallemand1d705562012-03-12 12:46:41 +01001491 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001492 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001493 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001494 last_isspace = 0;
1495 break;
1496
Willy Tarreau2beef582012-12-20 17:22:52 +01001497 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001498 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001499 if (conn) {
1500 conn_get_to_addr(conn);
1501 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001502 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001503 else
1504 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001505 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001506 else
1507 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1508
William Lallemand5f232402012-04-05 18:02:55 +02001509 if (ret == NULL)
1510 goto out;
1511 tmplog = ret;
1512 last_isspace = 0;
1513 break;
1514
Willy Tarreau2beef582012-12-20 17:22:52 +01001515 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001516 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001517 if (conn)
1518 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1519 else
1520 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1521
William Lallemand1d705562012-03-12 12:46:41 +01001522 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001523 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001524 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001525 last_isspace = 0;
1526 break;
1527
Willy Tarreau2beef582012-12-20 17:22:52 +01001528 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001529 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001530 if (conn)
1531 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1532 else
1533 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1534
William Lallemand5f232402012-04-05 18:02:55 +02001535 if (ret == NULL)
1536 goto out;
1537 tmplog = ret;
1538 last_isspace = 0;
1539 break;
1540
Willy Tarreau2beef582012-12-20 17:22:52 +01001541 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001542 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001543 if (conn)
1544 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1545 else
1546 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1547
William Lallemand5f232402012-04-05 18:02:55 +02001548 if (ret == NULL)
1549 goto out;
1550 tmplog = ret;
1551 last_isspace = 0;
1552 break;
1553
Willy Tarreau2beef582012-12-20 17:22:52 +01001554 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001555 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001556 if (conn)
1557 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1558 else
1559 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1560
William Lallemand1d705562012-03-12 12:46:41 +01001561 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001562 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001563 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001564 last_isspace = 0;
1565 break;
1566
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001567 case LOG_FMT_DATE: // %t = accept date
William Lallemandbddd4fd2012-02-27 11:23:10 +01001568 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001569 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1570 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_tr: // %tr = start of request date
1578 /* Note that the timers are valid if we get here */
1579 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1580 get_localtime(tv.tv_sec, &tm);
1581 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
1582 if (ret == NULL)
1583 goto out;
1584 tmplog = ret;
1585 last_isspace = 0;
1586 break;
1587
1588 case LOG_FMT_DATEGMT: // %T = accept date, GMT
William Lallemandbddd4fd2012-02-27 11:23:10 +01001589 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001590 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001591 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001592 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001593 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001594 last_isspace = 0;
1595 break;
1596
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001597 case LOG_FMT_trg: // %trg = start of request date, GMT
1598 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1599 get_gmtime(tv.tv_sec, &tm);
1600 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
1601 if (ret == NULL)
1602 goto out;
1603 tmplog = ret;
1604 last_isspace = 0;
1605 break;
1606
1607 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Yuxans Yao4e25b012012-10-19 10:36:09 +08001608 get_localtime(s->logs.accept_date.tv_sec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001609 ret = localdate2str_log(tmplog, s->logs.accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08001610 if (ret == NULL)
1611 goto out;
1612 tmplog = ret;
1613 last_isspace = 0;
1614 break;
1615
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001616 case LOG_FMT_trl: // %trl = start of request date, local
1617 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1618 get_localtime(tv.tv_sec, &tm);
1619 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
1620 if (ret == NULL)
1621 goto out;
1622 tmplog = ret;
1623 last_isspace = 0;
1624 break;
1625
William Lallemand5f232402012-04-05 18:02:55 +02001626 case LOG_FMT_TS: // %Ts
1627 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1628 if (tmp->options & LOG_OPT_HEXA) {
1629 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1630 if (iret < 0 || iret > dst + maxsize - tmplog)
1631 goto out;
1632 last_isspace = 0;
1633 tmplog += iret;
1634 } else {
1635 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1636 if (ret == NULL)
1637 goto out;
1638 tmplog = ret;
1639 last_isspace = 0;
1640 }
1641 break;
1642
William Lallemand1d705562012-03-12 12:46:41 +01001643 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001644 if (tmp->options & LOG_OPT_HEXA) {
1645 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1646 if (iret < 0 || iret > dst + maxsize - tmplog)
1647 goto out;
1648 last_isspace = 0;
1649 tmplog += iret;
1650 } else {
1651 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001652 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001653 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1654 tmplog, 4);
1655 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001656 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001657 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001658 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001659 }
1660 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001661
William Lallemand1d705562012-03-12 12:46:41 +01001662 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001663 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001664 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001665 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001666 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001667 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001668 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001669 break;
1670
Willy Tarreau773d65f2012-10-12 14:56:11 +02001671 case LOG_FMT_FRONTEND_XPRT: // %ft
1672 src = fe->id;
1673 if (tmp->options & LOG_OPT_QUOTE)
1674 LOGCHAR('"');
1675 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1676 if (iret == 0)
1677 goto out;
1678 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001679 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02001680 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02001681 if (tmp->options & LOG_OPT_QUOTE)
1682 LOGCHAR('"');
1683 last_isspace = 0;
1684 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001685#ifdef USE_OPENSSL
1686 case LOG_FMT_SSL_CIPHER: // %sslc
1687 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001688 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001689 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001690 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001691 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001692 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1693 if (ret == NULL)
1694 goto out;
1695 tmplog = ret;
1696 last_isspace = 0;
1697 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001698
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001699 case LOG_FMT_SSL_VERSION: // %sslv
1700 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001701 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001702 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001703 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001704 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001705 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1706 if (ret == NULL)
1707 goto out;
1708 tmplog = ret;
1709 last_isspace = 0;
1710 break;
1711#endif
William Lallemand1d705562012-03-12 12:46:41 +01001712 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001713 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001714 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001715 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001716 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001717 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001718 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001719 break;
1720
William Lallemand1d705562012-03-12 12:46:41 +01001721 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001722 switch (obj_type(s->target)) {
1723 case OBJ_TYPE_SERVER:
1724 src = objt_server(s->target)->id;
1725 break;
1726 case OBJ_TYPE_APPLET:
1727 src = objt_applet(s->target)->name;
1728 break;
1729 default:
1730 src = "<NOSRV>";
1731 break;
1732 }
William Lallemand5f232402012-04-05 18:02:55 +02001733 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001734 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001735 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001736 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001737 last_isspace = 0;
1738 break;
1739
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001740 case LOG_FMT_Th: // %Th = handshake time
1741 ret = ltoa_o(s->logs.t_handshake, tmplog, dst + maxsize - tmplog);
1742 if (ret == NULL)
1743 goto out;
1744 tmplog = ret;
1745 last_isspace = 0;
1746 break;
1747
1748 case LOG_FMT_Ti: // %Ti = HTTP idle time
1749 ret = ltoa_o(s->logs.t_idle, tmplog, dst + maxsize - tmplog);
1750 if (ret == NULL)
1751 goto out;
1752 tmplog = ret;
1753 last_isspace = 0;
1754 break;
1755
1756 case LOG_FMT_TR: // %TR = HTTP request time
1757 ret = ltoa_o((t_request >= 0) ? t_request - s->logs.t_idle - s->logs.t_handshake : -1,
1758 tmplog, dst + maxsize - tmplog);
1759 if (ret == NULL)
1760 goto out;
1761 tmplog = ret;
1762 last_isspace = 0;
1763 break;
1764
1765 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02001766 ret = ltoa_o(t_request, 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
William Lallemand1d705562012-03-12 12:46:41 +01001773 case LOG_FMT_TW: // %Tw
1774 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -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
William Lallemand1d705562012-03-12 12:46:41 +01001782 case LOG_FMT_TC: // %Tc
1783 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001784 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001785 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001786 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001787 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001788 last_isspace = 0;
1789 break;
1790
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001791 case LOG_FMT_Tr: // %Tr
William Lallemand1d705562012-03-12 12:46:41 +01001792 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001793 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001794 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001795 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001796 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001797 last_isspace = 0;
1798 break;
1799
Willy Tarreau27b639d2016-05-17 17:55:27 +02001800 case LOG_FMT_TD: // %Td
1801 if (s->be->mode == PR_MODE_HTTP)
1802 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_close - s->logs.t_data : -1,
1803 tmplog, dst + maxsize - tmplog);
1804 else
1805 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_close - s->logs.t_connect : -1,
1806 tmplog, dst + maxsize - tmplog);
1807 if (ret == NULL)
1808 goto out;
1809 tmplog = ret;
1810 last_isspace = 0;
1811 break;
1812
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001813 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
1814 if (!(fe->to_log & LW_BYTES))
1815 LOGCHAR('+');
1816 ret = ltoa_o(s->logs.t_close - (s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0),
1817 tmplog, dst + maxsize - tmplog);
1818 if (ret == NULL)
1819 goto out;
1820 tmplog = ret;
1821 last_isspace = 0;
1822 break;
1823
1824 case LOG_FMT_TT: // %Tt = total time
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 = ltoa_o(s->logs.t_close, 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 Tarreau2beef582012-12-20 17:22:52 +01001834 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02001835 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001836 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001837 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001838 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001839 last_isspace = 0;
1840 break;
1841
William Lallemand1d705562012-03-12 12:46:41 +01001842 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001843 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001844 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001845 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001846 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001847 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001848 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001849 last_isspace = 0;
1850 break;
1851
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001852 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001853 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1854 if (ret == NULL)
1855 goto out;
1856 tmplog = ret;
1857 last_isspace = 0;
1858 break;
1859
Willy Tarreau2beef582012-12-20 17:22:52 +01001860 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02001861 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02001862 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001863 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001864 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001865 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001866 last_isspace = 0;
1867 break;
1868
Willy Tarreau2beef582012-12-20 17:22:52 +01001869 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02001870 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02001871 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001872 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001873 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001874 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001875 last_isspace = 0;
1876 break;
1877
William Lallemand1d705562012-03-12 12:46:41 +01001878 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001879 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1880 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001881 *tmplog = '\0';
1882 last_isspace = 0;
1883 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001884
William Lallemand1d705562012-03-12 12:46:41 +01001885 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001886 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1887 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02001888 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1889 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 +01001890 last_isspace = 0;
1891 break;
1892
William Lallemand1d705562012-03-12 12:46:41 +01001893 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001894 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001895 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001896 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001897 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001898 last_isspace = 0;
1899 break;
1900
William Lallemand1d705562012-03-12 12:46:41 +01001901 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001902 ret = ltoa_o(fe->feconn, 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_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001910 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001911 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001912 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001913 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001914 last_isspace = 0;
1915 break;
1916
William Lallemand1d705562012-03-12 12:46:41 +01001917 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001918 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001919 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001920 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001921 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001922 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001923 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001924 last_isspace = 0;
1925 break;
1926
William Lallemand1d705562012-03-12 12:46:41 +01001927 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001928 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001929 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001930 ret = ltoa_o((s->si[1].conn_retries>0) ?
1931 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001932 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001933 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001934 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001935 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001936 last_isspace = 0;
1937 break;
1938
William Lallemand1d705562012-03-12 12:46:41 +01001939 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001940 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001941 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001942 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001943 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001944 last_isspace = 0;
1945 break;
1946
William Lallemand1d705562012-03-12 12:46:41 +01001947 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001948 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001949 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001950 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001951 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001952 last_isspace = 0;
1953 break;
1954
William Lallemand1d705562012-03-12 12:46:41 +01001955 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001956 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001957 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001958 if (tmp->options & LOG_OPT_QUOTE)
1959 LOGCHAR('"');
1960 LOGCHAR('{');
1961 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1962 if (hdr)
1963 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001964 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001965 ret = lf_encode_string(tmplog, dst + maxsize,
1966 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001967 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001968 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001969 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001970 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001971 }
1972 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001973 if (tmp->options & LOG_OPT_QUOTE)
1974 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001975 last_isspace = 0;
1976 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001977 break;
1978
William Lallemand1d705562012-03-12 12:46:41 +01001979 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001980 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001981 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001982 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1983 if (hdr > 0)
1984 LOGCHAR(' ');
1985 if (tmp->options & LOG_OPT_QUOTE)
1986 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001987 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001988 ret = lf_encode_string(tmplog, dst + maxsize,
1989 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001990 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001991 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001992 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001993 } else if (!(tmp->options & LOG_OPT_QUOTE))
1994 LOGCHAR('-');
1995 if (tmp->options & LOG_OPT_QUOTE)
1996 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001997 last_isspace = 0;
1998 }
1999 }
2000 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002001
William Lallemand1d705562012-03-12 12:46:41 +01002002
2003 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002004 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002005 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002006 if (tmp->options & LOG_OPT_QUOTE)
2007 LOGCHAR('"');
2008 LOGCHAR('{');
2009 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2010 if (hdr)
2011 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002012 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002013 ret = lf_encode_string(tmplog, dst + maxsize,
2014 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002015 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002016 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002017 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002018 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002019 }
2020 LOGCHAR('}');
2021 last_isspace = 0;
2022 if (tmp->options & LOG_OPT_QUOTE)
2023 LOGCHAR('"');
2024 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002025 break;
2026
William Lallemand1d705562012-03-12 12:46:41 +01002027 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002028 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002029 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002030 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2031 if (hdr > 0)
2032 LOGCHAR(' ');
2033 if (tmp->options & LOG_OPT_QUOTE)
2034 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002035 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002036 ret = lf_encode_string(tmplog, dst + maxsize,
2037 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002038 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002039 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002040 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002041 } else if (!(tmp->options & LOG_OPT_QUOTE))
2042 LOGCHAR('-');
2043 if (tmp->options & LOG_OPT_QUOTE)
2044 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002045 last_isspace = 0;
2046 }
2047 }
2048 break;
2049
William Lallemand1d705562012-03-12 12:46:41 +01002050 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002051 /* Request */
2052 if (tmp->options & LOG_OPT_QUOTE)
2053 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002054 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002055 ret = lf_encode_string(tmplog, dst + maxsize,
2056 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002057 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002058 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002059 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002060 if (tmp->options & LOG_OPT_QUOTE)
2061 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002062 last_isspace = 0;
2063 break;
William Lallemand5f232402012-04-05 18:02:55 +02002064
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002065 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002066 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002067
Willy Tarreaub7636d12015-06-17 19:58:02 +02002068 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002069 LOGCHAR('"');
2070
2071 end = uri + strlen(uri);
2072 // look for the first whitespace character
2073 while (uri < end && !HTTP_IS_SPHT(*uri))
2074 uri++;
2075
2076 // keep advancing past multiple spaces
2077 while (uri < end && HTTP_IS_SPHT(*uri)) {
2078 uri++; nspaces++;
2079 }
2080
2081 // look for first space or question mark after url
2082 spc = uri;
2083 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2084 spc++;
2085
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002086 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002087 chunk.str = "<BADREQ>";
2088 chunk.len = strlen("<BADREQ>");
2089 } else {
2090 chunk.str = uri;
2091 chunk.len = spc - uri;
2092 }
2093
Dragan Dosen835b9212016-02-12 13:23:03 +01002094 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002095 if (ret == NULL || *ret != '\0')
2096 goto out;
2097
2098 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002099 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002100 LOGCHAR('"');
2101
2102 last_isspace = 0;
2103 break;
2104
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002105 case LOG_FMT_HTTP_QUERY: // %HQ
2106 if (tmp->options & LOG_OPT_QUOTE)
2107 LOGCHAR('"');
2108
Willy Tarreau57bc8912016-04-25 17:09:40 +02002109 if (!txn || !txn->uri) {
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002110 chunk.str = "<BADREQ>";
2111 chunk.len = strlen("<BADREQ>");
2112 } else {
2113 uri = txn->uri;
2114 end = uri + strlen(uri);
2115 // look for the first question mark
2116 while (uri < end && *uri != '?')
2117 uri++;
2118
2119 qmark = uri;
2120 // look for first space or question mark after url
2121 while (uri < end && !HTTP_IS_SPHT(*uri))
2122 uri++;
2123
2124 chunk.str = qmark;
2125 chunk.len = uri - qmark;
2126 }
2127
Dragan Dosen835b9212016-02-12 13:23:03 +01002128 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002129 if (ret == NULL || *ret != '\0')
2130 goto out;
2131
2132 tmplog = ret;
2133 if (tmp->options & LOG_OPT_QUOTE)
2134 LOGCHAR('"');
2135
2136 last_isspace = 0;
2137 break;
2138
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002139 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002140 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002141
Willy Tarreaub7636d12015-06-17 19:58:02 +02002142 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002143 LOGCHAR('"');
2144
2145 end = uri + strlen(uri);
2146 // look for the first whitespace character
2147 while (uri < end && !HTTP_IS_SPHT(*uri))
2148 uri++;
2149
2150 // keep advancing past multiple spaces
2151 while (uri < end && HTTP_IS_SPHT(*uri)) {
2152 uri++; nspaces++;
2153 }
2154
2155 // look for first space after url
2156 spc = uri;
2157 while (spc < end && !HTTP_IS_SPHT(*spc))
2158 spc++;
2159
Willy Tarreau57bc8912016-04-25 17:09:40 +02002160 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002161 chunk.str = "<BADREQ>";
2162 chunk.len = strlen("<BADREQ>");
2163 } else {
2164 chunk.str = uri;
2165 chunk.len = spc - uri;
2166 }
2167
Dragan Dosen835b9212016-02-12 13:23:03 +01002168 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002169 if (ret == NULL || *ret != '\0')
2170 goto out;
2171
2172 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002173 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002174 LOGCHAR('"');
2175
2176 last_isspace = 0;
2177 break;
2178
2179 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002180 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002181 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002182 LOGCHAR('"');
2183
2184 end = uri + strlen(uri);
2185 // look for the first whitespace character
2186 spc = uri;
2187 while (spc < end && !HTTP_IS_SPHT(*spc))
2188 spc++;
2189
2190 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
2191 chunk.str = "<BADREQ>";
2192 chunk.len = strlen("<BADREQ>");
2193 } else {
2194 chunk.str = uri;
2195 chunk.len = spc - uri;
2196 }
2197
Dragan Dosen835b9212016-02-12 13:23:03 +01002198 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002199 if (ret == NULL || *ret != '\0')
2200 goto out;
2201
2202 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002203 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002204 LOGCHAR('"');
2205
2206 last_isspace = 0;
2207 break;
2208
2209 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002210 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002211 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002212 LOGCHAR('"');
2213
2214 end = uri + strlen(uri);
2215 // look for the first whitespace character
2216 while (uri < end && !HTTP_IS_SPHT(*uri))
2217 uri++;
2218
2219 // keep advancing past multiple spaces
2220 while (uri < end && HTTP_IS_SPHT(*uri)) {
2221 uri++; nspaces++;
2222 }
2223
2224 // look for the next whitespace character
2225 while (uri < end && !HTTP_IS_SPHT(*uri))
2226 uri++;
2227
2228 // keep advancing past multiple spaces
2229 while (uri < end && HTTP_IS_SPHT(*uri))
2230 uri++;
2231
Willy Tarreau57bc8912016-04-25 17:09:40 +02002232 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002233 chunk.str = "<BADREQ>";
2234 chunk.len = strlen("<BADREQ>");
2235 } else if (uri == end) {
2236 chunk.str = "HTTP/0.9";
2237 chunk.len = strlen("HTTP/0.9");
2238 } else {
2239 chunk.str = uri;
2240 chunk.len = end - uri;
2241 }
2242
Dragan Dosen835b9212016-02-12 13:23:03 +01002243 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002244 if (ret == NULL || *ret != '\0')
2245 goto out;
2246
2247 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002248 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002249 LOGCHAR('"');
2250
2251 last_isspace = 0;
2252 break;
2253
William Lallemand5f232402012-04-05 18:02:55 +02002254 case LOG_FMT_COUNTER: // %rt
2255 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002256 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002257 if (iret < 0 || iret > dst + maxsize - tmplog)
2258 goto out;
2259 last_isspace = 0;
2260 tmplog += iret;
2261 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002262 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002263 if (ret == NULL)
2264 goto out;
2265 tmplog = ret;
2266 last_isspace = 0;
2267 }
2268 break;
2269
Willy Tarreau7346acb2014-08-28 15:03:15 +02002270 case LOG_FMT_LOGCNT: // %lc
2271 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002272 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002273 if (iret < 0 || iret > dst + maxsize - tmplog)
2274 goto out;
2275 last_isspace = 0;
2276 tmplog += iret;
2277 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002278 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002279 if (ret == NULL)
2280 goto out;
2281 tmplog = ret;
2282 last_isspace = 0;
2283 }
2284 break;
2285
William Lallemand5f232402012-04-05 18:02:55 +02002286 case LOG_FMT_HOSTNAME: // %H
2287 src = hostname;
2288 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2289 if (ret == NULL)
2290 goto out;
2291 tmplog = ret;
2292 last_isspace = 0;
2293 break;
2294
2295 case LOG_FMT_PID: // %pid
2296 if (tmp->options & LOG_OPT_HEXA) {
2297 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2298 if (iret < 0 || iret > dst + maxsize - tmplog)
2299 goto out;
2300 last_isspace = 0;
2301 tmplog += iret;
2302 } else {
2303 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2304 if (ret == NULL)
2305 goto out;
2306 tmplog = ret;
2307 last_isspace = 0;
2308 }
2309 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002310
2311 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002312 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01002313 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002314 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002315 if (ret == NULL)
2316 goto out;
2317 tmplog = ret;
2318 last_isspace = 0;
2319 break;
2320
William Lallemandbddd4fd2012-02-27 11:23:10 +01002321 }
2322 }
2323
2324out:
William Lallemand1d705562012-03-12 12:46:41 +01002325 /* *tmplog is a unused character */
2326 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002327 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002328
Willy Tarreaubaaee002006-06-26 02:48:02 +02002329}
2330
William Lallemand1d705562012-03-12 12:46:41 +01002331/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002332 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002333 * Will not log if the frontend has no log defined.
2334 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002335void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002336{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002337 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002338 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002339 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002340
2341 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002342 err = (s->flags & SF_REDISP) ||
2343 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2344 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002345 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002346 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002347
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002348 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002349 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002350
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002351 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002352 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002353
Willy Tarreauabcd5142013-06-11 17:18:02 +02002354 if (s->logs.level) { /* loglevel was overridden */
2355 if (s->logs.level == -1) {
2356 s->logs.logwait = 0; /* logs disabled */
2357 return;
2358 }
2359 level = s->logs.level - 1;
2360 }
2361 else {
2362 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002363 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002364 level = LOG_ERR;
2365 }
William Lallemand1d705562012-03-12 12:46:41 +01002366
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002367 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002368 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002369 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002370 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002371 }
2372
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002373 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2374 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2375 &sess->fe->logformat_sd);
2376 }
2377
Dragan Dosen59cee972015-09-19 22:09:02 +02002378 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002379 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002380 sess->fe->log_count++;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002381 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002382 s->logs.logwait = 0;
2383 }
2384}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002385
Christopher Fauletc1b730a2017-10-24 12:00:51 +02002386static int cli_io_handler_show_startup_logs(struct appctx *appctx)
2387{
2388 struct stream_interface *si = appctx->owner;
2389 const char *msg = (startup_logs ? startup_logs : "No startup alerts/warnings.\n");
2390
2391 if (ci_putstr(si_ic(si), msg) == -1) {
2392 si_applet_cant_put(si);
2393 return 0;
2394 }
2395 return 1;
2396}
2397
2398/* register cli keywords */
2399static struct cli_kw_list cli_kws = {{ },{
2400 { { "show", "startup-logs", NULL },
2401 "show startup-logs : report logs emitted during HAProxy startup",
2402 NULL, cli_io_handler_show_startup_logs },
2403 {{},}
2404}};
2405
2406__attribute__((constructor))
2407static void __log_init(void)
2408{
2409 cli_register_kw(&cli_kws);
2410}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002411/*
2412 * Local variables:
2413 * c-indent-level: 8
2414 * c-basic-offset: 8
2415 * End:
2416 */