blob: 3434550a387cfbda2b1ff99907911f43eb4443ef [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>
Willy Tarreau35b51c62018-09-10 15:38:55 +020039#include <proto/h1.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 {
Willy Tarreau83061a82018-07-13 11:56:34 +020051 struct buffer sep1; /* first pid separator */
52 struct buffer sep2; /* second pid separator */
Dragan Dosen43885c72015-10-01 13:18:13 +020053 } pid;
54};
55
56static const struct log_fmt log_formats[LOG_FORMATS] = {
57 [LOG_FORMAT_RFC3164] = {
58 .name = "rfc3164",
59 .pid = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020060 .sep1 = { .area = "[", .data = 1 },
61 .sep2 = { .area = "]: ", .data = 3 }
Dragan Dosen43885c72015-10-01 13:18:13 +020062 }
63 },
64 [LOG_FORMAT_RFC5424] = {
65 .name = "rfc5424",
66 .pid = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020067 .sep1 = { .area = " ", .data = 1 },
68 .sep2 = { .area = " - ", .data = 3 }
Dragan Dosen43885c72015-10-01 13:18:13 +020069 }
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))];
Willy Tarreaue10cd482018-09-10 18:16:53 +020083fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
84fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
85fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Dragan Dosen835b9212016-02-12 13:23:03 +010086
87#else
88#error "Check if your OS uses bitfields for fd_sets"
89#endif
90
Willy Tarreaubaaee002006-06-26 02:48:02 +020091const char *log_facilities[NB_LOG_FACILITIES] = {
92 "kern", "user", "mail", "daemon",
93 "auth", "syslog", "lpr", "news",
94 "uucp", "cron", "auth2", "ftp",
95 "ntp", "audit", "alert", "cron2",
96 "local0", "local1", "local2", "local3",
97 "local4", "local5", "local6", "local7"
98};
99
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100const char *log_levels[NB_LOG_LEVELS] = {
101 "emerg", "alert", "crit", "err",
102 "warning", "notice", "info", "debug"
103};
104
Willy Tarreau570f2212013-06-10 16:42:09 +0200105const 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 +0200106const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107
William Lallemand723b73a2012-02-08 16:37:49 +0100108
109/* log_format */
110struct logformat_type {
111 char *name;
112 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100113 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200114 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100115 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100116 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100117};
118
William Lallemandb7ff6a32012-03-02 14:35:21 +0100119int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
120
William Lallemand723b73a2012-02-08 16:37:49 +0100121/* log_format variable names */
122static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200123 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100124
125 /* please keep these lines sorted ! */
126 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
127 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
128 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
129 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
130 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200131 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200132 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200133 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100134 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200135 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
136 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
137 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
138 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
139 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
140 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200141 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100142 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200143 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +0100144 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
145 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200146 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100147 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200148 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100149 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
150 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200151 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200152 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
153 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100154 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
155 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200156 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
157 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100158 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200159 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
160 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
161 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
162 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000163 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
164 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000165 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000166 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
167 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200168 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100169 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200170 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100171 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
172 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100173 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100174 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
175 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
176 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
177 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
178 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200179 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
180 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100181 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200182 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
183 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
184 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100185 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
186 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
187
188 /* The following tags are deprecated and will be removed soon */
189 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
190 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200191 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
192 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
193 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
194 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100195 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
196 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
197 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
198 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
199 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200200 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100201};
202
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200203char 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
204char 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 +0100205char 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 +0100206char *log_format = NULL;
207
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200208/* Default string used for structured-data part in RFC5424 formatted
209 * syslog messages.
210 */
211char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200212
Willy Tarreau13ef7732018-11-12 07:25:28 +0100213/* total number of dropped logs */
214unsigned int dropped_logs = 0;
215
Dragan Dosen1322d092015-09-22 16:05:32 +0200216/* This is a global syslog header, common to all outgoing messages in
217 * RFC3164 format. It begins with time-based part and is updated by
218 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200219 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200220THREAD_LOCAL char *logheader = NULL;
Dragan Dosen59cee972015-09-19 22:09:02 +0200221
Dragan Dosen1322d092015-09-22 16:05:32 +0200222/* This is a global syslog header for messages in RFC5424 format. It is
223 * updated by update_log_hdr_rfc5424().
224 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200225THREAD_LOCAL char *logheader_rfc5424 = NULL;
Dragan Dosen1322d092015-09-22 16:05:32 +0200226
Dragan Dosen59cee972015-09-19 22:09:02 +0200227/* This is a global syslog message buffer, common to all outgoing
228 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100229 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200230THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100231
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200232/* A global syslog message buffer, common to all RFC5424 syslog messages.
233 * Currently, it is used for generating the structured-data part.
234 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200235THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200236
Christopher Fauletd4696382017-10-24 11:44:05 +0200237/* A global buffer used to store all startup alerts/warnings. It will then be
238 * retrieve on the CLI. */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200239static THREAD_LOCAL char *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200240
William Lallemand723b73a2012-02-08 16:37:49 +0100241struct logformat_var_args {
242 char *name;
243 int mask;
244};
245
246struct logformat_var_args var_args_list[] = {
247// global
248 { "M", LOG_OPT_MANDATORY },
249 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200250 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100251 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100252 { 0, 0 }
253};
254
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200255/* return the name of the directive used in the current proxy for which we're
256 * currently parsing a header, when it is known.
257 */
258static inline const char *fmt_directive(const struct proxy *curproxy)
259{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100260 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200261 case ARGC_ACL:
262 return "acl";
263 case ARGC_STK:
264 return "stick";
265 case ARGC_TRK:
266 return "track-sc";
267 case ARGC_LOG:
268 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200269 case ARGC_LOGSD:
270 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100271 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100272 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100273 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100274 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200275 case ARGC_UIF:
276 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100277 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200278 return "redirect";
279 case ARGC_CAP:
280 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200281 case ARGC_SRV:
282 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200283 case ARGC_SPOE:
284 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100285 case ARGC_UBK:
286 return "use_backend";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100287 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200288 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100289 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200290}
291
William Lallemand723b73a2012-02-08 16:37:49 +0100292/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100293 * callback used to configure addr source retrieval
294 */
295int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
296{
297 curproxy->options2 |= PR_O2_SRC_ADDR;
298
299 return 0;
300}
301
302
303/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100304 * Parse args in a logformat_var. Returns 0 in error
305 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100306 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100307int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100308{
309 int i = 0;
310 int end = 0;
311 int flags = 0; // 1 = + 2 = -
312 char *sp = NULL; // start pointer
313
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100314 if (args == NULL) {
315 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100316 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100317 }
William Lallemand723b73a2012-02-08 16:37:49 +0100318
319 while (1) {
320 if (*args == '\0')
321 end = 1;
322
323 if (*args == '+') {
324 // add flag
325 sp = args + 1;
326 flags = 1;
327 }
328 if (*args == '-') {
329 // delete flag
330 sp = args + 1;
331 flags = 2;
332 }
333
334 if (*args == '\0' || *args == ',') {
335 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100336 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100337 if (strcmp(sp, var_args_list[i].name) == 0) {
338 if (flags == 1) {
339 node->options |= var_args_list[i].mask;
340 break;
341 } else if (flags == 2) {
342 node->options &= ~var_args_list[i].mask;
343 break;
344 }
345 }
346 }
347 sp = NULL;
348 if (end)
349 break;
350 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100351 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100352 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100353 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100354}
355
356/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100357 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
358 * must pass the args part in the <arg> pointer with its length in <arg_len>,
359 * and varname with its length in <var> and <var_len> respectively. <arg> is
360 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100361 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100362 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100363int 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 +0100364{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100365 int j;
366 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100367
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100368 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
369 if (strlen(logformat_keywords[j].name) == var_len &&
370 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
371 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200372 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100373 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100374 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100375 return 0;
376 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100377 node->type = logformat_keywords[j].type;
378 node->options = *defoptions;
379 if (arg_len) {
380 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100381 if (!parse_logformat_var_args(node->arg, node, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100382 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100383 }
384 if (node->type == LOG_FMT_GLOBAL) {
385 *defoptions = node->options;
386 free(node->arg);
387 free(node);
388 } else {
389 if (logformat_keywords[j].config_callback &&
390 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100391 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100392 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100393 curproxy->to_log |= logformat_keywords[j].lw;
394 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100395 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100396 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100397 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
398 curproxy->conf.args.file, curproxy->conf.args.line,
399 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100400 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100401 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100402 memprintf(err, "format variable '%s' is reserved for HTTP mode",
403 logformat_keywords[j].name);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100404 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100405 }
William Lallemand723b73a2012-02-08 16:37:49 +0100406 }
407 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100408
409 j = var[var_len];
410 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100411 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 +0100412 var[var_len] = j;
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100413 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100414}
415
416/*
417 * push to the logformat linked list
418 *
419 * start: start pointer
420 * end: end text pointer
421 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100422 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100423 *
424 * LOG_TEXT: copy chars from start to end excluding end.
425 *
426*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100427int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100428{
429 char *str;
430
Willy Tarreaua3571662012-12-20 21:59:12 +0100431 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200432 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100433 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100434 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100435 return 0;
436 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200437 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100438 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100439 str[end - start] = '\0';
440 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100441 node->type = LOG_FMT_TEXT; // type string
442 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100443 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200444 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100445 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100446 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100447 return 0;
448 }
William Lallemand1d705562012-03-12 12:46:41 +0100449 node->type = LOG_FMT_SEPARATOR;
450 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100451 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100452 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100453}
454
455/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100456 * Parse the sample fetch expression <text> and add a node to <list_format> upon
457 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200458 * should work. The curpx->conf.args.ctx must be set by the caller.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100459 *
460 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100461 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100462int 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 +0100463{
464 char *cmd[2];
465 struct sample_expr *expr;
466 struct logformat_node *node;
467 int cmd_arg;
468
469 cmd[0] = text;
470 cmd[1] = "";
471 cmd_arg = 0;
472
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100473 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 +0100474 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100475 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100476 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100477 }
478
Vincent Bernat02779b62016-04-03 13:48:43 +0200479 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100480 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100481 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100482 return 0;
483 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100484 node->type = LOG_FMT_EXPR;
485 node->expr = expr;
486 node->options = options;
487
488 if (arg_len) {
489 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100490 if (!parse_logformat_var_args(node->arg, node, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100491 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100492 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100493 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100494 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
495
Willy Tarreau434c57c2013-01-08 01:10:24 +0100496 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100497 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
498
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100499 if (!(expr->fetch->val & cap)) {
David Carlier93e8b882017-09-21 14:36:43 +0000500 free(node);
501 node = NULL;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100502 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
503 text, sample_src_names(expr->fetch->use));
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100504 return 0;
505 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100506
Willy Tarreauc8368452012-12-21 00:09:23 +0100507 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
508 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100509 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100510
William Lallemand65ad6e12014-01-31 15:08:02 +0100511 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
512 * needed with some sample fetches (eg: ssl*). We always set it for
513 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100514 */
515 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100516 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100517 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100518 return 1;
Willy Tarreauc8368452012-12-21 00:09:23 +0100519}
520
521/*
William Lallemand723b73a2012-02-08 16:37:49 +0100522 * Parse the log_format string and fill a linked list.
523 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200524 * You can set arguments using { } : %{many arguments}varname.
525 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100526 *
527 * str: the string to parse
528 * curproxy: the proxy affected
529 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100530 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100531 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100532 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100533 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100534 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100535int 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 +0100536{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100537 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100538 char *arg = NULL; /* start pointer for args */
539 char *var = NULL; /* start pointer for vars */
540 int arg_len = 0;
541 int var_len = 0;
542 int cformat; /* current token format */
543 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100544 struct logformat_node *tmplf, *back;
545
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100546 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100547 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100548 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100549 return 0;
550 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200551 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200552
William Lallemand723b73a2012-02-08 16:37:49 +0100553 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100554 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100555 LIST_DEL(&tmplf->list);
556 free(tmplf);
557 }
558
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100559 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100560 pformat = cformat;
561
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100562 if (!*str)
563 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100564
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100565 /* The prinicple of the two-step state machine below is to first detect a change, and
566 * second have all common paths processed at one place. The common paths are the ones
567 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
568 * We use the common LF_INIT state to dispatch to the different final states.
569 */
570 switch (pformat) {
571 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100572 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100573 arg_len = var_len = 0;
574 if (*str == '{') { // optional argument
575 cformat = LF_STARG;
576 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100577 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100578 else if (*str == '[') {
579 cformat = LF_STEXPR;
580 var = str + 1; // store expr in variable name
581 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100582 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100583 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100584 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100585 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100586 else if (*str == '%')
587 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100588 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100589 /* single '%' followed by blank or digit, send them both */
590 cformat = LF_TEXT;
591 pformat = LF_TEXT; /* finally we include the previous char as well */
592 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600593 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 +0100594 *str, (int)(str - backfmt), fmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100595 return 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100596
597 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100598 else
599 cformat = LF_INIT; // handle other cases of litterals
600 break;
601
602 case LF_STARG: // text immediately following '%{'
603 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100604 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100605 arg_len = str - arg;
606 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100607 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100608 break;
609
610 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100611 if (*str == '[') {
612 cformat = LF_STEXPR;
613 var = str + 1; // store expr in variable name
614 break;
615 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100616 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100617 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100618 var = str;
619 break;
620 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100621 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100622 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100623
Willy Tarreauc8368452012-12-21 00:09:23 +0100624 case LF_STEXPR: // text immediately following '%['
625 if (*str == ']') { // end of arg
626 cformat = LF_EDEXPR;
627 var_len = str - var;
628 *str = 0; // needed for parsing the expression
629 }
630 break;
631
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100632 case LF_VAR: // text part of a variable name
633 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100634 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100635 cformat = LF_INIT; // not variable name anymore
636 break;
637
Willy Tarreauc8368452012-12-21 00:09:23 +0100638 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100639 cformat = LF_INIT;
640 }
641
642 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
643 switch (*str) {
644 case '%': cformat = LF_STARTVAR; break;
645 case ' ': cformat = LF_SEPARATOR; break;
646 case 0 : cformat = LF_END; break;
647 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100648 }
649 }
650
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100651 if (cformat != pformat || pformat == LF_SEPARATOR) {
652 switch (pformat) {
653 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100654 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100655 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100656 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100657 case LF_STEXPR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100658 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 +0100659 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100660 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100661 case LF_TEXT:
662 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100663 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100664 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100665 break;
666 }
667 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100668 }
669 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100670
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100671 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100672 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100673 return 0;
674 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100675 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100676
677 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100678}
679
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200680/*
681 * Parse "log" keyword and update <logsrvs> list accordingly.
682 *
683 * When <do_del> is set, it means the "no log" line was parsed, so all log
684 * servers in <logsrvs> are released.
685 *
686 * Otherwise, we try to parse the "log" line. First of all, when the list is not
687 * the global one, we look for the parameter "global". If we find it,
688 * global.logsrvs is copied. Else we parse each arguments.
689 *
690 * The function returns 1 in success case, otherwise, it returns 0 and err is
691 * filled.
692 */
693int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
694{
695 struct sockaddr_storage *sk;
696 struct logsrv *logsrv = NULL;
697 int port1, port2;
698 int cur_arg;
699
700 /*
701 * "no log": delete previous herited or defined syslog
702 * servers.
703 */
704 if (do_del) {
705 struct logsrv *back;
706
707 if (*(args[1]) != 0) {
708 memprintf(err, "'no log' does not expect arguments");
709 goto error;
710 }
711
712 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
713 LIST_DEL(&logsrv->list);
714 free(logsrv);
715 }
716 return 1;
717 }
718
719 /*
720 * "log global": copy global.logrsvs linked list to the end of logsrvs
721 * list. But first, we check (logsrvs != global.logsrvs).
722 */
723 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
724 if (logsrvs == &global.logsrvs) {
725 memprintf(err, "'global' is not supported for a global syslog server");
726 goto error;
727 }
728 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200729 struct logsrv *node;
730
731 list_for_each_entry(node, logsrvs, list) {
732 if (node->ref == logsrv)
733 goto skip_logsrv;
734 }
735
736 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200737 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200738 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200739 LIST_INIT(&node->list);
740 LIST_ADDQ(logsrvs, &node->list);
Christopher Faulet28ac0992018-03-26 16:09:19 +0200741
742 skip_logsrv:
743 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200744 }
745 return 1;
746 }
747
748 /*
749 * "log <address> ...: parse a syslog server line
750 */
751 if (*(args[1]) == 0 || *(args[2]) == 0) {
752 memprintf(err, "expects <address> and <facility> %s as arguments",
753 ((logsrvs == &global.logsrvs) ? "" : "or global"));
754 goto error;
755 }
756
757 logsrv = calloc(1, sizeof(*logsrv));
758 if (!logsrv) {
759 memprintf(err, "out of memory");
760 goto error;
761 }
762
763 /* skip address for now, it will be parsed at the end */
764 cur_arg = 2;
765
766 /* just after the address, a length may be specified */
767 logsrv->maxlen = MAX_SYSLOG_LEN;
768 if (strcmp(args[cur_arg], "len") == 0) {
769 int len = atoi(args[cur_arg+1]);
770 if (len < 80 || len > 65535) {
771 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
772 args[cur_arg+1]);
773 goto error;
774 }
775 logsrv->maxlen = len;
776 cur_arg += 2;
777 }
778 if (logsrv->maxlen > global.max_syslog_len)
779 global.max_syslog_len = logsrv->maxlen;
780
781 /* after the length, a format may be specified */
782 if (strcmp(args[cur_arg], "format") == 0) {
783 logsrv->format = get_log_format(args[cur_arg+1]);
784 if (logsrv->format < 0) {
785 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
786 goto error;
787 }
788 cur_arg += 2;
789 }
790
791 /* parse the facility */
792 logsrv->facility = get_log_facility(args[cur_arg]);
793 if (logsrv->facility < 0) {
794 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
795 goto error;
796 }
797 cur_arg++;
798
799 /* parse the max syslog level (default: debug) */
800 logsrv->level = 7;
801 if (*(args[cur_arg])) {
802 logsrv->level = get_log_level(args[cur_arg]);
803 if (logsrv->level < 0) {
804 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
805 goto error;
806 }
807 cur_arg++;
808 }
809
810 /* parse the limit syslog level (default: emerg) */
811 logsrv->minlvl = 0;
812 if (*(args[cur_arg])) {
813 logsrv->minlvl = get_log_level(args[cur_arg]);
814 if (logsrv->minlvl < 0) {
815 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
816 goto error;
817 }
818 cur_arg++;
819 }
820
821 /* Too many args */
822 if (*(args[cur_arg])) {
823 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
824 goto error;
825 }
826
827 /* now, back to the address */
828 sk = str2sa_range(args[1], NULL, &port1, &port2, err, NULL, NULL, 1);
829 if (!sk)
830 goto error;
831 logsrv->addr = *sk;
832
833 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
834 if (port1 != port2) {
835 memprintf(err, "port ranges and offsets are not allowed in '%s'", args[1]);
836 goto error;
837 }
838 logsrv->addr = *sk;
839 if (!port1)
840 set_host_port(&logsrv->addr, SYSLOG_PORT);
841 }
842 LIST_ADDQ(logsrvs, &logsrv->list);
843 return 1;
844
845 error:
846 free(logsrv);
847 return 0;
848}
849
850
Christopher Fauletd4696382017-10-24 11:44:05 +0200851/* Generic function to display messages prefixed by a label */
852static void print_message(const char *label, const char *fmt, va_list argp)
853{
854 struct tm tm;
855 char *head, *msg;
856
857 head = msg = NULL;
858
859 get_localtime(date.tv_sec, &tm);
860 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
861 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
862 memvprintf(&msg, fmt, argp);
863
864 if (global.mode & MODE_STARTING)
865 memprintf(&startup_logs, "%s%s%s", (startup_logs ? startup_logs : ""), head, msg);
866
867 fprintf(stderr, "%s%s", head, msg);
868 fflush(stderr);
869
870 free(head);
871 free(msg);
872}
873
Willy Tarreaubaaee002006-06-26 02:48:02 +0200874/*
875 * Displays the message on stderr with the date and pid. Overrides the quiet
876 * mode during startup.
877 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100878void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200879{
880 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200881
882 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
883 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +0200884 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200885 va_end(argp);
886 }
887}
888
889
890/*
891 * Displays the message on stderr with the date and pid.
892 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100893void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200894{
895 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200896
897 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
898 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +0200899 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200900 va_end(argp);
901 }
902}
903
904/*
905 * Displays the message on <out> only if quiet mode is not set.
906 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200907void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200908{
909 va_list argp;
910
911 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
912 va_start(argp, fmt);
913 vfprintf(out, fmt, argp);
914 fflush(out);
915 va_end(argp);
916 }
917}
918
919/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200920 * returns log format for <fmt> or -1 if not found.
921 */
922int get_log_format(const char *fmt)
923{
924 int format;
925
926 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200927 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200928 format--;
929
930 return format;
931}
932
933/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934 * returns log level for <lev> or -1 if not found.
935 */
936int get_log_level(const char *lev)
937{
938 int level;
939
940 level = NB_LOG_LEVELS - 1;
941 while (level >= 0 && strcmp(log_levels[level], lev))
942 level--;
943
944 return level;
945}
946
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947/*
948 * returns log facility for <fac> or -1 if not found.
949 */
950int get_log_facility(const char *fac)
951{
952 int facility;
953
954 facility = NB_LOG_FACILITIES - 1;
955 while (facility >= 0 && strcmp(log_facilities[facility], fac))
956 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100957
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958 return facility;
959}
960
William Lallemanda1cc3812012-02-08 16:38:44 +0100961/*
Dragan Dosen835b9212016-02-12 13:23:03 +0100962 * Encode the string.
963 *
964 * When using the +E log format option, it will try to escape '"\]'
965 * characters with '\' as prefix. The same prefix should not be used as
966 * <escape>.
967 */
968static char *lf_encode_string(char *start, char *stop,
969 const char escape, const fd_set *map,
970 const char *string,
971 struct logformat_node *node)
972{
973 if (node->options & LOG_OPT_ESC) {
974 if (start < stop) {
975 stop--; /* reserve one byte for the final '\0' */
976 while (start < stop && *string != '\0') {
977 if (!FD_ISSET((unsigned char)(*string), map)) {
978 if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map))
979 *start++ = *string;
980 else {
981 if (start + 2 >= stop)
982 break;
983 *start++ = '\\';
984 *start++ = *string;
985 }
986 }
987 else {
988 if (start + 3 >= stop)
989 break;
990 *start++ = escape;
991 *start++ = hextab[(*string >> 4) & 15];
992 *start++ = hextab[*string & 15];
993 }
994 string++;
995 }
996 *start = '\0';
997 }
998 }
999 else {
1000 return encode_string(start, stop, escape, map, string);
1001 }
1002
1003 return start;
1004}
1005
1006/*
1007 * Encode the chunk.
1008 *
1009 * When using the +E log format option, it will try to escape '"\]'
1010 * characters with '\' as prefix. The same prefix should not be used as
1011 * <escape>.
1012 */
1013static char *lf_encode_chunk(char *start, char *stop,
1014 const char escape, const fd_set *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001015 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001016 struct logformat_node *node)
1017{
1018 char *str, *end;
1019
1020 if (node->options & LOG_OPT_ESC) {
1021 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001022 str = chunk->area;
1023 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001024
1025 stop--; /* reserve one byte for the final '\0' */
1026 while (start < stop && str < end) {
1027 if (!FD_ISSET((unsigned char)(*str), map)) {
1028 if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map))
1029 *start++ = *str;
1030 else {
1031 if (start + 2 >= stop)
1032 break;
1033 *start++ = '\\';
1034 *start++ = *str;
1035 }
1036 }
1037 else {
1038 if (start + 3 >= stop)
1039 break;
1040 *start++ = escape;
1041 *start++ = hextab[(*str >> 4) & 15];
1042 *start++ = hextab[*str & 15];
1043 }
1044 str++;
1045 }
1046 *start = '\0';
1047 }
1048 }
1049 else {
1050 return encode_chunk(start, stop, escape, map, chunk);
1051 }
1052
1053 return start;
1054}
1055
1056/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001057 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001058 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001059 *
1060 * Return the adress of the \0 character, or NULL on error
1061 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001062char *lf_text_len(char *dst, const char *src, size_t len, size_t size, const struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +01001063{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001064 if (size < 2)
1065 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001066
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001067 if (node->options & LOG_OPT_QUOTE) {
1068 *(dst++) = '"';
1069 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001070 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001071
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001072 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001073 if (++len > size)
1074 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001075 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001076 char *ret;
1077
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001078 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001079 if (ret == NULL || *ret != '\0')
1080 return NULL;
1081 len = ret - dst;
1082 }
1083 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001084 len = strlcpy2(dst, src, len);
1085 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001086
1087 size -= len;
1088 dst += len;
1089 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001090 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1091 if (size < 2)
1092 return NULL;
1093 *(dst++) = '-';
1094 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001095
1096 if (node->options & LOG_OPT_QUOTE) {
1097 if (size < 2)
1098 return NULL;
1099 *(dst++) = '"';
1100 }
1101
1102 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001103 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001104}
1105
Willy Tarreau26ffa852018-09-05 15:23:10 +02001106static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001107{
1108 return lf_text_len(dst, src, size, size, node);
1109}
1110
William Lallemand5f232402012-04-05 18:02:55 +02001111/*
1112 * Write a IP adress to the log string
1113 * +X option write in hexadecimal notation, most signifant byte on the left
1114 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001115char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001116{
1117 char *ret = dst;
1118 int iret;
1119 char pn[INET6_ADDRSTRLEN];
1120
1121 if (node->options & LOG_OPT_HEXA) {
1122 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1123 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1124 if (iret < 0 || iret > size)
1125 return NULL;
1126 ret += iret;
1127 } else {
1128 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1129 ret = lf_text(dst, pn, size, node);
1130 if (ret == NULL)
1131 return NULL;
1132 }
1133 return ret;
1134}
1135
1136/*
1137 * Write a port to the log
1138 * +X option write in hexadecimal notation, most signifant byte on the left
1139 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001140char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001141{
1142 char *ret = dst;
1143 int iret;
1144
1145 if (node->options & LOG_OPT_HEXA) {
1146 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1147 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1148 if (iret < 0 || iret > size)
1149 return NULL;
1150 ret += iret;
1151 } else {
1152 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1153 if (ret == NULL)
1154 return NULL;
1155 }
1156 return ret;
1157}
1158
Dragan Dosen1322d092015-09-22 16:05:32 +02001159/* Re-generate time-based part of the syslog header in RFC3164 format at
1160 * the beginning of logheader once a second and return the pointer to the
1161 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001162 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001163static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001165 static THREAD_LOCAL long tvsec;
1166 static THREAD_LOCAL char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreau83061a82018-07-13 11:56:34 +02001167 static THREAD_LOCAL struct buffer host = { };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001168 static THREAD_LOCAL int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001169
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001170 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001171 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +02001172 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001173 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +02001174
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001175 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +02001176 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001177
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001178 if (unlikely(global.log_send_hostname != host.area)) {
1179 host.area = global.log_send_hostname;
1180 host.data = host.area ? strlen(host.area) : 0;
1181 sep = host.data ? 1 : 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001182 }
1183
Dragan Dosen59cee972015-09-19 22:09:02 +02001184 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +02001185 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +02001186 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +02001187 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001188 (int)host.data, host.area, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001189 /* WARNING: depending upon implementations, snprintf may return
1190 * either -1 or the number of bytes that would be needed to store
1191 * the total message. In both cases, we must adjust it.
1192 */
Willy Tarreau18324f52014-06-27 18:10:07 +02001193 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1194 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001195
Dragan Dosen59cee972015-09-19 22:09:02 +02001196 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001197 }
1198
Willy Tarreau094af4e2015-01-07 15:03:42 +01001199 dataptr[0] = 0; // ensure we get rid of any previous attempt
1200
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001201 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001202}
1203
Dragan Dosen1322d092015-09-22 16:05:32 +02001204/* Re-generate time-based part of the syslog header in RFC5424 format at
1205 * the beginning of logheader_rfc5424 once a second and return the pointer
1206 * to the first character after it.
1207 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001208static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +02001209{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001210 static THREAD_LOCAL long tvsec;
1211 static THREAD_LOCAL char *dataptr = NULL; /* backup of last end of header, NULL first time */
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001212 const char *gmt_offset;
Dragan Dosen1322d092015-09-22 16:05:32 +02001213
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001214 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +02001215 /* this string is rebuild only once a second */
1216 struct tm tm;
1217 int hdr_len;
1218
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001219 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +02001220 get_localtime(tvsec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001221 gmt_offset = get_gmt_offset(time, &tm);
Dragan Dosen1322d092015-09-22 16:05:32 +02001222
1223 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +02001224 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +02001225 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +02001226 tm.tm_hour, tm.tm_min, tm.tm_sec,
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001227 gmt_offset, gmt_offset+3,
Dragan Dosen43885c72015-10-01 13:18:13 +02001228 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +02001229 /* WARNING: depending upon implementations, snprintf may return
1230 * either -1 or the number of bytes that would be needed to store
1231 * the total message. In both cases, we must adjust it.
1232 */
1233 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1234 hdr_len = global.max_syslog_len;
1235
1236 dataptr = logheader_rfc5424 + hdr_len;
1237 }
1238
1239 dataptr[0] = 0; // ensure we get rid of any previous attempt
1240
1241 return dataptr;
1242}
1243
William Lallemand2a4a44f2012-02-06 16:00:33 +01001244/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001245 * This function sends the syslog message using a printf format string. It
1246 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001247 */
1248void send_log(struct proxy *p, int level, const char *format, ...)
1249{
1250 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001251 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001252
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001253 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001254 return;
1255
William Lallemand2a4a44f2012-02-06 16:00:33 +01001256 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001257 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001258 if (data_len < 0 || data_len > global.max_syslog_len)
1259 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001260 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001261
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001262 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001263}
1264
1265/*
1266 * This function sends a syslog message.
1267 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001268 * It overrides the last byte of the message vector with an LF character.
1269 * The arguments <sd> and <sd_size> are used for the structured-data part
1270 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001271 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001272void __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 +01001273{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001274 static THREAD_LOCAL struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1275 static THREAD_LOCAL struct msghdr msghdr = {
1276 //.msg_iov = iovec,
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001277 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1278 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001279 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1280 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
1281 static THREAD_LOCAL char *dataptr = NULL;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001282 int fac_level;
1283 struct list *logsrvs = NULL;
1284 struct logsrv *tmp = NULL;
1285 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +02001286 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001287 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001288 time_t time = date.tv_sec;
Willy Tarreau83061a82018-07-13 11:56:34 +02001289 struct buffer *tag = &global.log_tag;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001290 static THREAD_LOCAL int curr_pid;
1291 static THREAD_LOCAL char pidstr[100];
Willy Tarreau83061a82018-07-13 11:56:34 +02001292 static THREAD_LOCAL struct buffer pid;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001293
1294 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001295
1296 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297
1298 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +02001299 if (!LIST_ISEMPTY(&global.logsrvs)) {
1300 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001301 }
1302 } else {
William Lallemand0f99e342011-10-12 17:50:54 +02001303 if (!LIST_ISEMPTY(&p->logsrvs)) {
1304 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001305 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001306 if (p->log_tag.area) {
Dragan Dosen43885c72015-10-01 13:18:13 +02001307 tag = &p->log_tag;
1308 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001309 }
1310
William Lallemand0f99e342011-10-12 17:50:54 +02001311 if (!logsrvs)
1312 return;
1313
Dragan Dosen43885c72015-10-01 13:18:13 +02001314 if (unlikely(curr_pid != getpid())) {
1315 curr_pid = getpid();
1316 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1317 chunk_initstr(&pid, pidstr);
1318 }
1319
Robert Tsai81ae1952007-12-05 10:47:29 +01001320 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +02001321 nblogger = 0;
1322 list_for_each_entry(tmp, logsrvs, list) {
1323 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +01001324 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +01001325 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +02001326 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +01001327 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001328 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +02001329 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001330 int tag_max = 0;
1331 int pid_sep1_max = 0;
1332 int pid_max = 0;
1333 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001334 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001335 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +01001336
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001337 nblogger++;
1338
Willy Tarreaubaaee002006-06-26 02:48:02 +02001339 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +02001340 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001341 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001342
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001343 if (unlikely(*plogfd < 0)) {
1344 /* socket not successfully initialized yet */
1345 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
1346
1347 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001348 static char once;
1349
1350 if (!once) {
1351 once = 1; /* note: no need for atomic ops here */
Willy Tarreau251fe342018-11-12 07:00:11 +01001352 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001353 nblogger, strerror(errno), errno);
1354 }
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001355 continue;
1356 }
1357 /* we don't want to receive anything on this socket */
1358 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1359 /* does nothing under Linux, maybe needed for others */
1360 shutdown(*plogfd, SHUT_RD);
Christopher Faulet78969172017-12-19 10:35:53 +01001361 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001362 }
1363
Dragan Dosen1322d092015-09-22 16:05:32 +02001364 switch (logsrv->format) {
1365 case LOG_FORMAT_RFC3164:
1366 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001367 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +02001368 break;
1369
1370 case LOG_FORMAT_RFC5424:
1371 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001372 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001373 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001374 break;
1375
1376 default:
1377 continue; /* must never happen */
1378 }
1379
1380 hdr_size = hdr_ptr - hdr;
1381
Willy Tarreaubaaee002006-06-26 02:48:02 +02001382 /* For each target, we may have a different facility.
1383 * We can also have a different log level for each message.
1384 * This induces variations in the message header length.
1385 * Since we don't want to recompute it each time, nor copy it every
1386 * time, we only change the facility in the pre-computed header,
1387 * and we change the pointer to the header accordingly.
1388 */
William Lallemand0f99e342011-10-12 17:50:54 +02001389 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001390 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001391 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001392 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001393 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001394 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001395 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001396 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001397
Dragan Dosen1322d092015-09-22 16:05:32 +02001398 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001399
Dragan Dosen43885c72015-10-01 13:18:13 +02001400 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001401 if (unlikely(hdr_size >= logsrv->maxlen)) {
1402 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001403 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001404 goto send;
1405 }
1406
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001407 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001408
Dragan Dosen43885c72015-10-01 13:18:13 +02001409 /* tag */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001410 tag_max = tag->data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001411 if (unlikely(tag_max >= maxlen)) {
1412 tag_max = maxlen - 1;
1413 sd_max = 0;
1414 goto send;
1415 }
1416
1417 maxlen -= tag_max;
1418
1419 /* first pid separator */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001420 pid_sep1_max = log_formats[logsrv->format].pid.sep1.data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001421 if (unlikely(pid_sep1_max >= maxlen)) {
1422 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001423 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001424 goto send;
1425 }
1426
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001427 pid_sep1 = log_formats[logsrv->format].pid.sep1.area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001428 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001429
Dragan Dosen43885c72015-10-01 13:18:13 +02001430 /* pid */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001431 pid_max = pid.data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001432 if (unlikely(pid_max >= maxlen)) {
1433 pid_max = maxlen - 1;
1434 sd_max = 0;
1435 goto send;
1436 }
1437
1438 maxlen -= pid_max;
1439
1440 /* second pid separator */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001441 pid_sep2_max = log_formats[logsrv->format].pid.sep2.data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001442 if (unlikely(pid_sep2_max >= maxlen)) {
1443 pid_sep2_max = maxlen - 1;
1444 sd_max = 0;
1445 goto send;
1446 }
1447
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001448 pid_sep2 = log_formats[logsrv->format].pid.sep2.area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001449 maxlen -= pid_sep2_max;
1450
1451 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001452 if (sd_max >= maxlen) {
1453 sd_max = maxlen - 1;
1454 goto send;
1455 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001456
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001457 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001458send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001459 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001460 iovec[0].iov_len = hdr_max;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001461 iovec[1].iov_base = tag->area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001462 iovec[1].iov_len = tag_max;
1463 iovec[2].iov_base = pid_sep1;
1464 iovec[2].iov_len = pid_sep1_max;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001465 iovec[3].iov_base = pid.area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001466 iovec[3].iov_len = pid_max;
1467 iovec[4].iov_base = pid_sep2;
1468 iovec[4].iov_len = pid_sep2_max;
1469 iovec[5].iov_base = sd;
1470 iovec[5].iov_len = sd_max;
1471 iovec[6].iov_base = dataptr;
1472 iovec[6].iov_len = max;
1473 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1474 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001475
1476 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1477 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1478
1479 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001480
Robert Tsai81ae1952007-12-05 10:47:29 +01001481 if (sent < 0) {
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001482 static char once;
1483
Willy Tarreau13ef7732018-11-12 07:25:28 +01001484 if (errno == EAGAIN)
1485 HA_ATOMIC_ADD(&dropped_logs, 1);
1486 else if (!once) {
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001487 once = 1; /* note: no need for atomic ops here */
Willy Tarreau251fe342018-11-12 07:00:11 +01001488 ha_alert("sendmsg() failed in logger #%d: %s (errno=%d)\n",
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001489 nblogger, strerror(errno), errno);
1490 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001491 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001492 }
1493}
1494
William Lallemandbddd4fd2012-02-27 11:23:10 +01001495extern fd_set hdr_encode_map[];
1496extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001497extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001498
Willy Tarreaubaaee002006-06-26 02:48:02 +02001499
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001500const 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 +01001501const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1502 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1503 Set-cookie Updated, unknown, unknown */
1504
William Lallemand1d705562012-03-12 12:46:41 +01001505/*
1506 * try to write a character if there is enough space, or goto out
1507 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001508#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001509 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001510 *(tmplog++) = (x); \
1511 } else { \
1512 goto out; \
1513 } \
1514 } while(0)
1515
Dragan Dosen835b9212016-02-12 13:23:03 +01001516
1517/* Initializes some log data.
1518 */
1519void init_log()
1520{
1521 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001522 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001523
1524 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1525 * inside PARAM-VALUE should be escaped with '\' as prefix.
1526 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1527 * details.
1528 */
1529 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1530
1531 tmp = "\"\\]";
1532 while (*tmp) {
1533 FD_SET(*tmp, rfc5424_escape_map);
1534 tmp++;
1535 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001536
1537 /* initialize the log header encoding map : '{|}"#' should be encoded with
1538 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1539 * URL encoding only requires '"', '#' to be encoded as well as non-
1540 * printable characters above.
1541 */
1542 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1543 memset(url_encode_map, 0, sizeof(url_encode_map));
1544 for (i = 0; i < 32; i++) {
1545 FD_SET(i, hdr_encode_map);
1546 FD_SET(i, url_encode_map);
1547 }
1548 for (i = 127; i < 256; i++) {
1549 FD_SET(i, hdr_encode_map);
1550 FD_SET(i, url_encode_map);
1551 }
1552
1553 tmp = "\"#{|}";
1554 while (*tmp) {
1555 FD_SET(*tmp, hdr_encode_map);
1556 tmp++;
1557 }
1558
1559 tmp = "\"#";
1560 while (*tmp) {
1561 FD_SET(*tmp, url_encode_map);
1562 tmp++;
1563 }
1564
1565 /* initialize the http header encoding map. The draft httpbis define the
1566 * header content as:
1567 *
1568 * HTTP-message = start-line
1569 * *( header-field CRLF )
1570 * CRLF
1571 * [ message-body ]
1572 * header-field = field-name ":" OWS field-value OWS
1573 * field-value = *( field-content / obs-fold )
1574 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1575 * obs-fold = CRLF 1*( SP / HTAB )
1576 * field-vchar = VCHAR / obs-text
1577 * VCHAR = %x21-7E
1578 * obs-text = %x80-FF
1579 *
1580 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1581 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
1582 * "obs-fold" is volontary forgotten because haproxy remove this.
1583 */
1584 memset(http_encode_map, 0, sizeof(http_encode_map));
1585 for (i = 0x00; i <= 0x08; i++)
1586 FD_SET(i, http_encode_map);
1587 for (i = 0x0a; i <= 0x1f; i++)
1588 FD_SET(i, http_encode_map);
1589 FD_SET(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001590}
William Lallemand1d705562012-03-12 12:46:41 +01001591
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001592static int init_log_buffers_per_thread()
1593{
1594 return init_log_buffers();
1595}
1596
1597static void deinit_log_buffers_per_thread()
1598{
1599 deinit_log_buffers();
1600}
1601
Christopher Faulet0132d062017-07-26 15:33:35 +02001602/* Initialize log buffers used for syslog messages */
1603int init_log_buffers()
1604{
1605 logheader = my_realloc2(logheader, global.max_syslog_len + 1);
1606 logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1);
1607 logline = my_realloc2(logline, global.max_syslog_len + 1);
1608 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
1609 if (!logheader || !logline_rfc5424 || !logline || !logline_rfc5424)
1610 return 0;
1611 return 1;
1612}
1613
1614/* Deinitialize log buffers used for syslog messages */
1615void deinit_log_buffers()
1616{
1617 free(logheader);
1618 free(logheader_rfc5424);
1619 free(logline);
1620 free(logline_rfc5424);
Christopher Fauletd4696382017-10-24 11:44:05 +02001621 free(startup_logs);
Christopher Faulet0132d062017-07-26 15:33:35 +02001622 logheader = NULL;
1623 logheader_rfc5424 = NULL;
1624 logline = NULL;
1625 logline_rfc5424 = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +02001626 startup_logs = NULL;
Christopher Faulet0132d062017-07-26 15:33:35 +02001627}
1628
Willy Tarreaudf974472012-12-28 02:44:01 +01001629/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1630 * <maxsize> characters. Returns the size of the output string in characters,
1631 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001632 * is not zero. It requires a valid session and optionally a stream. If the
1633 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01001634 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001635int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001636{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001637 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001638 struct proxy *be;
1639 struct http_txn *txn;
1640 const struct strm_logs *logs;
1641 const struct connection *be_conn;
1642 unsigned int s_flags;
1643 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02001644 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001645 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001646 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001647 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001648 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001649 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001650 int t_request;
1651 int hdr;
1652 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001653 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001654 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001655 char *ret;
1656 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001657 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001658 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001659 struct strm_logs tmp_strm_log;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001660
William Lallemandbddd4fd2012-02-27 11:23:10 +01001661 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001662
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001663 if (likely(s)) {
1664 be = s->be;
1665 txn = s->txn;
1666 be_conn = cs_conn(objt_cs(s->si[1].end));
1667 s_flags = s->flags;
1668 uniq_id = s->uniq_id;
1669 logs = &s->logs;
1670 } else {
1671 /* we have no stream so we first need to initialize a few
1672 * things that are needed later. We do increment the request
1673 * ID so that it's uniquely assigned to this request just as
1674 * if the request had reached the point of being processed.
1675 * A request error is reported as it's the only element we have
1676 * here and which justifies emitting such a log.
1677 */
1678 be = fe;
1679 txn = NULL;
1680 be_conn = NULL;
1681 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
1682 uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
1683
1684 /* prepare a valid log structure */
1685 tmp_strm_log.tv_accept = sess->tv_accept;
1686 tmp_strm_log.accept_date = sess->accept_date;
1687 tmp_strm_log.t_handshake = sess->t_handshake;
1688 tmp_strm_log.t_idle = tv_ms_elapsed(&sess->tv_accept, &now) - sess->t_handshake;
1689 tv_zero(&tmp_strm_log.tv_request);
1690 tmp_strm_log.t_queue = -1;
1691 tmp_strm_log.t_connect = -1;
1692 tmp_strm_log.t_data = -1;
1693 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
1694 tmp_strm_log.bytes_in = 0;
1695 tmp_strm_log.bytes_out = 0;
1696 tmp_strm_log.prx_queue_pos = 0;
1697 tmp_strm_log.srv_queue_pos = 0;
1698
1699 logs = &tmp_strm_log;
1700 }
1701
William Lallemandbddd4fd2012-02-27 11:23:10 +01001702 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001703 if (tv_isge(&logs->tv_request, &logs->tv_accept))
1704 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01001705
William Lallemand1d705562012-03-12 12:46:41 +01001706 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001707
William Lallemandbddd4fd2012-02-27 11:23:10 +01001708 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001709 if (LIST_ISEMPTY(list_format))
1710 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001711
William Lallemand1d705562012-03-12 12:46:41 +01001712 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001713 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001714 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001715 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02001716 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001717
Willy Tarreauc8368452012-12-21 00:09:23 +01001718 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001719 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001720 if (!last_isspace) {
1721 LOGCHAR(' ');
1722 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001723 }
1724 break;
1725
William Lallemand1d705562012-03-12 12:46:41 +01001726 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001727 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001728 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001729 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001730 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001731 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001732 last_isspace = 0;
1733 break;
1734
Willy Tarreauc8368452012-12-21 00:09:23 +01001735 case LOG_FMT_EXPR: // sample expression, may be request or response
1736 key = NULL;
1737 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001738 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 +01001739 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001740 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 +01001741 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01001742 ret = lf_encode_chunk(tmplog, dst + maxsize,
1743 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001744 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001745 ret = lf_text_len(tmplog,
1746 key ? key->data.u.str.area : NULL,
1747 key ? key->data.u.str.data : 0,
1748 dst + maxsize - tmplog,
1749 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01001750 if (ret == 0)
1751 goto out;
1752 tmplog = ret;
1753 last_isspace = 0;
1754 break;
1755
Willy Tarreau2beef582012-12-20 17:22:52 +01001756 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001757 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001758 if (conn)
1759 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1760 else
1761 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001762 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001763 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001764 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001765 last_isspace = 0;
1766 break;
1767
Willy Tarreau2beef582012-12-20 17:22:52 +01001768 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001769 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001770 if (conn) {
1771 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001772 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001773 } else {
1774 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1775 dst + maxsize - tmplog, tmp);
1776 }
William Lallemand5f232402012-04-05 18:02:55 +02001777 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001778 else
1779 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1780
William Lallemand5f232402012-04-05 18:02:55 +02001781 if (ret == NULL)
1782 goto out;
1783 tmplog = ret;
1784 last_isspace = 0;
1785 break;
1786
Willy Tarreau2beef582012-12-20 17:22:52 +01001787 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001788 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001789 if (conn) {
1790 conn_get_to_addr(conn);
1791 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1792 }
1793 else
1794 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1795
William Lallemand1d705562012-03-12 12:46:41 +01001796 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001797 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001798 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001799 last_isspace = 0;
1800 break;
1801
Willy Tarreau2beef582012-12-20 17:22:52 +01001802 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001803 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001804 if (conn) {
1805 conn_get_to_addr(conn);
1806 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001807 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001808 else
1809 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001810 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001811 else
1812 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1813
William Lallemand5f232402012-04-05 18:02:55 +02001814 if (ret == NULL)
1815 goto out;
1816 tmplog = ret;
1817 last_isspace = 0;
1818 break;
1819
Willy Tarreau2beef582012-12-20 17:22:52 +01001820 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau2393c5b2018-09-05 15:24:56 +02001821 if (be_conn)
1822 ret = lf_ip(tmplog, (const struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001823 else
1824 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1825
William Lallemand1d705562012-03-12 12:46:41 +01001826 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001827 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001828 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001829 last_isspace = 0;
1830 break;
1831
Willy Tarreau2beef582012-12-20 17:22:52 +01001832 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau2393c5b2018-09-05 15:24:56 +02001833 if (be_conn)
1834 ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001835 else
1836 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1837
William Lallemand5f232402012-04-05 18:02:55 +02001838 if (ret == NULL)
1839 goto out;
1840 tmplog = ret;
1841 last_isspace = 0;
1842 break;
1843
Willy Tarreau2beef582012-12-20 17:22:52 +01001844 case LOG_FMT_SERVERIP: // %si
Willy Tarreau2393c5b2018-09-05 15:24:56 +02001845 if (be_conn)
1846 ret = lf_ip(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001847 else
1848 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1849
William Lallemand5f232402012-04-05 18:02:55 +02001850 if (ret == NULL)
1851 goto out;
1852 tmplog = ret;
1853 last_isspace = 0;
1854 break;
1855
Willy Tarreau2beef582012-12-20 17:22:52 +01001856 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau2393c5b2018-09-05 15:24:56 +02001857 if (be_conn)
1858 ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001859 else
1860 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1861
William Lallemand1d705562012-03-12 12:46:41 +01001862 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001863 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001864 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001865 last_isspace = 0;
1866 break;
1867
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001868 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001869 get_localtime(logs->accept_date.tv_sec, &tm);
1870 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001871 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001872 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001873 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001874 last_isspace = 0;
1875 break;
1876
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001877 case LOG_FMT_tr: // %tr = start of request date
1878 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001879 tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001880 get_localtime(tv.tv_sec, &tm);
1881 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
1882 if (ret == NULL)
1883 goto out;
1884 tmplog = ret;
1885 last_isspace = 0;
1886 break;
1887
1888 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001889 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001890 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001891 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001892 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001893 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001894 last_isspace = 0;
1895 break;
1896
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001897 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001898 tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001899 get_gmtime(tv.tv_sec, &tm);
1900 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
1901 if (ret == NULL)
1902 goto out;
1903 tmplog = ret;
1904 last_isspace = 0;
1905 break;
1906
1907 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001908 get_localtime(logs->accept_date.tv_sec, &tm);
1909 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08001910 if (ret == NULL)
1911 goto out;
1912 tmplog = ret;
1913 last_isspace = 0;
1914 break;
1915
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001916 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001917 tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001918 get_localtime(tv.tv_sec, &tm);
1919 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
1920 if (ret == NULL)
1921 goto out;
1922 tmplog = ret;
1923 last_isspace = 0;
1924 break;
1925
William Lallemand5f232402012-04-05 18:02:55 +02001926 case LOG_FMT_TS: // %Ts
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001927 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001928 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001929 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02001930 if (iret < 0 || iret > dst + maxsize - tmplog)
1931 goto out;
1932 last_isspace = 0;
1933 tmplog += iret;
1934 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001935 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001936 if (ret == NULL)
1937 goto out;
1938 tmplog = ret;
1939 last_isspace = 0;
1940 }
1941 break;
1942
William Lallemand1d705562012-03-12 12:46:41 +01001943 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001944 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001945 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02001946 if (iret < 0 || iret > dst + maxsize - tmplog)
1947 goto out;
1948 last_isspace = 0;
1949 tmplog += iret;
1950 } else {
1951 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001952 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001953 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001954 tmplog, 4);
1955 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001956 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001957 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001958 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001959 }
1960 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001961
William Lallemand1d705562012-03-12 12:46:41 +01001962 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001963 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001964 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001965 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001966 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001967 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001968 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001969 break;
1970
Willy Tarreau773d65f2012-10-12 14:56:11 +02001971 case LOG_FMT_FRONTEND_XPRT: // %ft
1972 src = fe->id;
1973 if (tmp->options & LOG_OPT_QUOTE)
1974 LOGCHAR('"');
1975 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1976 if (iret == 0)
1977 goto out;
1978 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001979 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02001980 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02001981 if (tmp->options & LOG_OPT_QUOTE)
1982 LOGCHAR('"');
1983 last_isspace = 0;
1984 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001985#ifdef USE_OPENSSL
1986 case LOG_FMT_SSL_CIPHER: // %sslc
1987 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001988 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001989 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001990 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001991 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001992 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1993 if (ret == NULL)
1994 goto out;
1995 tmplog = ret;
1996 last_isspace = 0;
1997 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001998
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001999 case LOG_FMT_SSL_VERSION: // %sslv
2000 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002001 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002002 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002003 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002004 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002005 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2006 if (ret == NULL)
2007 goto out;
2008 tmplog = ret;
2009 last_isspace = 0;
2010 break;
2011#endif
William Lallemand1d705562012-03-12 12:46:41 +01002012 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002013 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002014 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002015 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002016 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002017 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002018 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002019 break;
2020
William Lallemand1d705562012-03-12 12:46:41 +01002021 case LOG_FMT_SERVER: // %s
Willy Tarreaue1809df2018-09-05 15:30:16 +02002022 switch (obj_type(s ? s->target : NULL)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002023 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002024 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002025 break;
2026 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002027 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002028 break;
2029 default:
2030 src = "<NOSRV>";
2031 break;
2032 }
William Lallemand5f232402012-04-05 18:02:55 +02002033 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002034 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002035 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002036 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002037 last_isspace = 0;
2038 break;
2039
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002040 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002041 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002042 if (ret == NULL)
2043 goto out;
2044 tmplog = ret;
2045 last_isspace = 0;
2046 break;
2047
2048 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002049 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002050 if (ret == NULL)
2051 goto out;
2052 tmplog = ret;
2053 last_isspace = 0;
2054 break;
2055
2056 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002057 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002058 tmplog, dst + maxsize - tmplog);
2059 if (ret == NULL)
2060 goto out;
2061 tmplog = ret;
2062 last_isspace = 0;
2063 break;
2064
2065 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002066 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002067 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002068 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002069 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002070 last_isspace = 0;
2071 break;
2072
William Lallemand1d705562012-03-12 12:46:41 +01002073 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002074 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002075 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002076 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002077 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002078 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002079 last_isspace = 0;
2080 break;
2081
William Lallemand1d705562012-03-12 12:46:41 +01002082 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002083 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002084 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002085 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002086 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002087 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002088 last_isspace = 0;
2089 break;
2090
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002091 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002092 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002093 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002094 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002095 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002096 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002097 last_isspace = 0;
2098 break;
2099
Willy Tarreau27b639d2016-05-17 17:55:27 +02002100 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002101 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002102 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002103 tmplog, dst + maxsize - tmplog);
2104 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002105 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002106 tmplog, dst + maxsize - tmplog);
2107 if (ret == NULL)
2108 goto out;
2109 tmplog = ret;
2110 last_isspace = 0;
2111 break;
2112
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002113 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2114 if (!(fe->to_log & LW_BYTES))
2115 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002116 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0),
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002117 tmplog, dst + maxsize - tmplog);
2118 if (ret == NULL)
2119 goto out;
2120 tmplog = ret;
2121 last_isspace = 0;
2122 break;
2123
2124 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002125 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002126 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002127 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002128 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002129 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002130 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002131 last_isspace = 0;
2132 break;
2133
Willy Tarreau2beef582012-12-20 17:22:52 +01002134 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02002135 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002136 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002137 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002138 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002139 last_isspace = 0;
2140 break;
2141
William Lallemand1d705562012-03-12 12:46:41 +01002142 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002143 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002144 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002145 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002146 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002147 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002148 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002149 last_isspace = 0;
2150 break;
2151
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002152 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002153 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002154 if (ret == NULL)
2155 goto out;
2156 tmplog = ret;
2157 last_isspace = 0;
2158 break;
2159
Willy Tarreau2beef582012-12-20 17:22:52 +01002160 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002161 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002162 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002163 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002164 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002165 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002166 last_isspace = 0;
2167 break;
2168
Willy Tarreau2beef582012-12-20 17:22:52 +01002169 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002170 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002171 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002172 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002173 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002174 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002175 last_isspace = 0;
2176 break;
2177
William Lallemand1d705562012-03-12 12:46:41 +01002178 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002179 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2180 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002181 *tmplog = '\0';
2182 last_isspace = 0;
2183 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002184
William Lallemand1d705562012-03-12 12:46:41 +01002185 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002186 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2187 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002188 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2189 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 +01002190 last_isspace = 0;
2191 break;
2192
William Lallemand1d705562012-03-12 12:46:41 +01002193 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002194 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002195 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002196 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002197 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002198 last_isspace = 0;
2199 break;
2200
William Lallemand1d705562012-03-12 12:46:41 +01002201 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002202 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002203 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002204 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002205 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002206 last_isspace = 0;
2207 break;
2208
William Lallemand1d705562012-03-12 12:46:41 +01002209 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002210 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002211 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002212 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002213 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002214 last_isspace = 0;
2215 break;
2216
William Lallemand1d705562012-03-12 12:46:41 +01002217 case LOG_FMT_SRVCONN: // %sc
Willy Tarreaue1809df2018-09-05 15:30:16 +02002218 ret = ultoa_o(objt_server(s ? s->target : NULL) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002219 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002220 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002221 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002222 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002223 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002224 last_isspace = 0;
2225 break;
2226
William Lallemand1d705562012-03-12 12:46:41 +01002227 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002228 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002229 LOGCHAR('+');
Willy Tarreauabd71a52018-09-04 19:21:44 +02002230 ret = ltoa_o((s && s->si[1].conn_retries > 0) ?
Willy Tarreau350f4872014-11-28 14:42:25 +01002231 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002232 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002233 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002234 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002235 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002236 last_isspace = 0;
2237 break;
2238
William Lallemand1d705562012-03-12 12:46:41 +01002239 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002240 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002241 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002242 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002243 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002244 last_isspace = 0;
2245 break;
2246
William Lallemand1d705562012-03-12 12:46:41 +01002247 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002248 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002249 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002250 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002251 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002252 last_isspace = 0;
2253 break;
2254
William Lallemand1d705562012-03-12 12:46:41 +01002255 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002256 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002257 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002258 if (tmp->options & LOG_OPT_QUOTE)
2259 LOGCHAR('"');
2260 LOGCHAR('{');
2261 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2262 if (hdr)
2263 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002264 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002265 ret = lf_encode_string(tmplog, dst + maxsize,
2266 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002267 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002268 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002269 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002270 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002271 }
2272 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002273 if (tmp->options & LOG_OPT_QUOTE)
2274 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002275 last_isspace = 0;
2276 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002277 break;
2278
William Lallemand1d705562012-03-12 12:46:41 +01002279 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002280 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002281 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002282 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2283 if (hdr > 0)
2284 LOGCHAR(' ');
2285 if (tmp->options & LOG_OPT_QUOTE)
2286 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002287 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002288 ret = lf_encode_string(tmplog, dst + maxsize,
2289 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002290 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002291 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002292 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002293 } else if (!(tmp->options & LOG_OPT_QUOTE))
2294 LOGCHAR('-');
2295 if (tmp->options & LOG_OPT_QUOTE)
2296 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002297 last_isspace = 0;
2298 }
2299 }
2300 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002301
William Lallemand1d705562012-03-12 12:46:41 +01002302
2303 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002304 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002305 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002306 if (tmp->options & LOG_OPT_QUOTE)
2307 LOGCHAR('"');
2308 LOGCHAR('{');
2309 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2310 if (hdr)
2311 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002312 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002313 ret = lf_encode_string(tmplog, dst + maxsize,
2314 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002315 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002316 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002317 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002318 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002319 }
2320 LOGCHAR('}');
2321 last_isspace = 0;
2322 if (tmp->options & LOG_OPT_QUOTE)
2323 LOGCHAR('"');
2324 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002325 break;
2326
William Lallemand1d705562012-03-12 12:46:41 +01002327 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002328 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002329 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002330 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2331 if (hdr > 0)
2332 LOGCHAR(' ');
2333 if (tmp->options & LOG_OPT_QUOTE)
2334 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002335 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002336 ret = lf_encode_string(tmplog, dst + maxsize,
2337 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002338 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002339 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002340 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002341 } else if (!(tmp->options & LOG_OPT_QUOTE))
2342 LOGCHAR('-');
2343 if (tmp->options & LOG_OPT_QUOTE)
2344 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002345 last_isspace = 0;
2346 }
2347 }
2348 break;
2349
William Lallemand1d705562012-03-12 12:46:41 +01002350 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002351 /* Request */
2352 if (tmp->options & LOG_OPT_QUOTE)
2353 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002354 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002355 ret = lf_encode_string(tmplog, dst + maxsize,
2356 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002357 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002358 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002359 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002360 if (tmp->options & LOG_OPT_QUOTE)
2361 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002362 last_isspace = 0;
2363 break;
William Lallemand5f232402012-04-05 18:02:55 +02002364
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002365 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002366 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002367
Willy Tarreaub7636d12015-06-17 19:58:02 +02002368 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002369 LOGCHAR('"');
2370
2371 end = uri + strlen(uri);
2372 // look for the first whitespace character
2373 while (uri < end && !HTTP_IS_SPHT(*uri))
2374 uri++;
2375
2376 // keep advancing past multiple spaces
2377 while (uri < end && HTTP_IS_SPHT(*uri)) {
2378 uri++; nspaces++;
2379 }
2380
2381 // look for first space or question mark after url
2382 spc = uri;
2383 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2384 spc++;
2385
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002386 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002387 chunk.area = "<BADREQ>";
2388 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002389 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002390 chunk.area = uri;
2391 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002392 }
2393
Dragan Dosen835b9212016-02-12 13:23:03 +01002394 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002395 if (ret == NULL || *ret != '\0')
2396 goto out;
2397
2398 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002399 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002400 LOGCHAR('"');
2401
2402 last_isspace = 0;
2403 break;
2404
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002405 case LOG_FMT_HTTP_QUERY: // %HQ
2406 if (tmp->options & LOG_OPT_QUOTE)
2407 LOGCHAR('"');
2408
Willy Tarreau57bc8912016-04-25 17:09:40 +02002409 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002410 chunk.area = "<BADREQ>";
2411 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002412 } else {
2413 uri = txn->uri;
2414 end = uri + strlen(uri);
2415 // look for the first question mark
2416 while (uri < end && *uri != '?')
2417 uri++;
2418
2419 qmark = uri;
2420 // look for first space or question mark after url
2421 while (uri < end && !HTTP_IS_SPHT(*uri))
2422 uri++;
2423
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002424 chunk.area = qmark;
2425 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002426 }
2427
Dragan Dosen835b9212016-02-12 13:23:03 +01002428 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002429 if (ret == NULL || *ret != '\0')
2430 goto out;
2431
2432 tmplog = ret;
2433 if (tmp->options & LOG_OPT_QUOTE)
2434 LOGCHAR('"');
2435
2436 last_isspace = 0;
2437 break;
2438
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002439 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002440 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002441
Willy Tarreaub7636d12015-06-17 19:58:02 +02002442 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002443 LOGCHAR('"');
2444
2445 end = uri + strlen(uri);
2446 // look for the first whitespace character
2447 while (uri < end && !HTTP_IS_SPHT(*uri))
2448 uri++;
2449
2450 // keep advancing past multiple spaces
2451 while (uri < end && HTTP_IS_SPHT(*uri)) {
2452 uri++; nspaces++;
2453 }
2454
2455 // look for first space after url
2456 spc = uri;
2457 while (spc < end && !HTTP_IS_SPHT(*spc))
2458 spc++;
2459
Willy Tarreau57bc8912016-04-25 17:09:40 +02002460 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002461 chunk.area = "<BADREQ>";
2462 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002463 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002464 chunk.area = uri;
2465 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002466 }
2467
Dragan Dosen835b9212016-02-12 13:23:03 +01002468 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002469 if (ret == NULL || *ret != '\0')
2470 goto out;
2471
2472 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002473 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002474 LOGCHAR('"');
2475
2476 last_isspace = 0;
2477 break;
2478
2479 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002480 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002481 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002482 LOGCHAR('"');
2483
2484 end = uri + strlen(uri);
2485 // look for the first whitespace character
2486 spc = uri;
2487 while (spc < end && !HTTP_IS_SPHT(*spc))
2488 spc++;
2489
2490 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002491 chunk.area = "<BADREQ>";
2492 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002493 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002494 chunk.area = uri;
2495 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002496 }
2497
Dragan Dosen835b9212016-02-12 13:23:03 +01002498 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002499 if (ret == NULL || *ret != '\0')
2500 goto out;
2501
2502 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002503 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002504 LOGCHAR('"');
2505
2506 last_isspace = 0;
2507 break;
2508
2509 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002510 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002511 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002512 LOGCHAR('"');
2513
2514 end = uri + strlen(uri);
2515 // look for the first whitespace character
2516 while (uri < end && !HTTP_IS_SPHT(*uri))
2517 uri++;
2518
2519 // keep advancing past multiple spaces
2520 while (uri < end && HTTP_IS_SPHT(*uri)) {
2521 uri++; nspaces++;
2522 }
2523
2524 // look for the next whitespace character
2525 while (uri < end && !HTTP_IS_SPHT(*uri))
2526 uri++;
2527
2528 // keep advancing past multiple spaces
2529 while (uri < end && HTTP_IS_SPHT(*uri))
2530 uri++;
2531
Willy Tarreau57bc8912016-04-25 17:09:40 +02002532 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002533 chunk.area = "<BADREQ>";
2534 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002535 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002536 chunk.area = "HTTP/0.9";
2537 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002538 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002539 chunk.area = uri;
2540 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002541 }
2542
Dragan Dosen835b9212016-02-12 13:23:03 +01002543 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002544 if (ret == NULL || *ret != '\0')
2545 goto out;
2546
2547 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002548 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002549 LOGCHAR('"');
2550
2551 last_isspace = 0;
2552 break;
2553
William Lallemand5f232402012-04-05 18:02:55 +02002554 case LOG_FMT_COUNTER: // %rt
2555 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002556 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002557 if (iret < 0 || iret > dst + maxsize - tmplog)
2558 goto out;
2559 last_isspace = 0;
2560 tmplog += iret;
2561 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002562 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002563 if (ret == NULL)
2564 goto out;
2565 tmplog = ret;
2566 last_isspace = 0;
2567 }
2568 break;
2569
Willy Tarreau7346acb2014-08-28 15:03:15 +02002570 case LOG_FMT_LOGCNT: // %lc
2571 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002572 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002573 if (iret < 0 || iret > dst + maxsize - tmplog)
2574 goto out;
2575 last_isspace = 0;
2576 tmplog += iret;
2577 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002578 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002579 if (ret == NULL)
2580 goto out;
2581 tmplog = ret;
2582 last_isspace = 0;
2583 }
2584 break;
2585
William Lallemand5f232402012-04-05 18:02:55 +02002586 case LOG_FMT_HOSTNAME: // %H
2587 src = hostname;
2588 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2589 if (ret == NULL)
2590 goto out;
2591 tmplog = ret;
2592 last_isspace = 0;
2593 break;
2594
2595 case LOG_FMT_PID: // %pid
2596 if (tmp->options & LOG_OPT_HEXA) {
2597 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2598 if (iret < 0 || iret > dst + maxsize - tmplog)
2599 goto out;
2600 last_isspace = 0;
2601 tmplog += iret;
2602 } else {
2603 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2604 if (ret == NULL)
2605 goto out;
2606 tmplog = ret;
2607 last_isspace = 0;
2608 }
2609 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002610
2611 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002612 ret = NULL;
Willy Tarreau02fdf4f2018-09-05 15:49:01 +02002613 src = s ? s->unique_id : NULL;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002614 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002615 if (ret == NULL)
2616 goto out;
2617 tmplog = ret;
2618 last_isspace = 0;
2619 break;
2620
William Lallemandbddd4fd2012-02-27 11:23:10 +01002621 }
2622 }
2623
2624out:
William Lallemand1d705562012-03-12 12:46:41 +01002625 /* *tmplog is a unused character */
2626 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002627 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002628
Willy Tarreaubaaee002006-06-26 02:48:02 +02002629}
2630
William Lallemand1d705562012-03-12 12:46:41 +01002631/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002632 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002633 * Will not log if the frontend has no log defined.
2634 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002635void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002636{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002637 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002638 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002639 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002640
2641 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002642 err = (s->flags & SF_REDISP) ||
2643 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2644 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002645 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002646 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002647
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002648 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002649 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002650
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002651 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002652 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002653
Willy Tarreauabcd5142013-06-11 17:18:02 +02002654 if (s->logs.level) { /* loglevel was overridden */
2655 if (s->logs.level == -1) {
2656 s->logs.logwait = 0; /* logs disabled */
2657 return;
2658 }
2659 level = s->logs.level - 1;
2660 }
2661 else {
2662 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002663 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002664 level = LOG_ERR;
2665 }
William Lallemand1d705562012-03-12 12:46:41 +01002666
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002667 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002668 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002669 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002670 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002671 }
2672
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002673 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2674 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2675 &sess->fe->logformat_sd);
2676 }
2677
Dragan Dosen59cee972015-09-19 22:09:02 +02002678 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002679 if (size > 0) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002680 HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002681 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002682 s->logs.logwait = 0;
2683 }
2684}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002685
Willy Tarreau53839352018-09-05 19:51:10 +02002686/*
2687 * send a minimalist log for the session. Will not log if the frontend has no
2688 * log defined. It is assumed that this is only used to report anomalies that
2689 * cannot lead to the creation of a regular stream. Because of this the log
2690 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
2691 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02002692 * function to report unimportant events. It is safe to call this function with
2693 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02002694 */
2695void sess_log(struct session *sess)
2696{
2697 int size, level;
2698 int sd_size = 0;
2699
Willy Tarreau9fa267d2018-10-05 10:22:27 +02002700 if (!sess)
2701 return;
2702
Willy Tarreau53839352018-09-05 19:51:10 +02002703 if (LIST_ISEMPTY(&sess->fe->logsrvs))
2704 return;
2705
2706 level = LOG_INFO;
2707 if (sess->fe->options2 & PR_O2_LOGERRORS)
2708 level = LOG_ERR;
2709
2710 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2711 sd_size = sess_build_logline(sess, NULL,
2712 logline_rfc5424, global.max_syslog_len,
2713 &sess->fe->logformat_sd);
2714 }
2715
2716 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
2717 if (size > 0) {
2718 HA_ATOMIC_ADD(&sess->fe->log_count, 1);
2719 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
2720 }
2721}
2722
Christopher Fauletc1b730a2017-10-24 12:00:51 +02002723static int cli_io_handler_show_startup_logs(struct appctx *appctx)
2724{
2725 struct stream_interface *si = appctx->owner;
2726 const char *msg = (startup_logs ? startup_logs : "No startup alerts/warnings.\n");
2727
2728 if (ci_putstr(si_ic(si), msg) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002729 si_cant_put(si);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02002730 return 0;
2731 }
2732 return 1;
2733}
2734
2735/* register cli keywords */
2736static struct cli_kw_list cli_kws = {{ },{
2737 { { "show", "startup-logs", NULL },
2738 "show startup-logs : report logs emitted during HAProxy startup",
2739 NULL, cli_io_handler_show_startup_logs },
2740 {{},}
2741}};
2742
2743__attribute__((constructor))
2744static void __log_init(void)
2745{
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002746 hap_register_per_thread_init(init_log_buffers_per_thread);
2747 hap_register_per_thread_deinit(deinit_log_buffers_per_thread);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02002748 cli_register_kw(&cli_kws);
2749}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002750/*
2751 * Local variables:
2752 * c-indent-level: 8
2753 * c-basic-offset: 8
2754 * End:
2755 */