blob: cd9ff01b996b5db7a9bb6b6bc7855a58cbbfc7ff [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
213/* This is a global syslog header, common to all outgoing messages in
214 * RFC3164 format. It begins with time-based part and is updated by
215 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200216 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200217THREAD_LOCAL char *logheader = NULL;
Dragan Dosen59cee972015-09-19 22:09:02 +0200218
Dragan Dosen1322d092015-09-22 16:05:32 +0200219/* This is a global syslog header for messages in RFC5424 format. It is
220 * updated by update_log_hdr_rfc5424().
221 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200222THREAD_LOCAL char *logheader_rfc5424 = NULL;
Dragan Dosen1322d092015-09-22 16:05:32 +0200223
Dragan Dosen59cee972015-09-19 22:09:02 +0200224/* This is a global syslog message buffer, common to all outgoing
225 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100226 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200227THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100228
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200229/* A global syslog message buffer, common to all RFC5424 syslog messages.
230 * Currently, it is used for generating the structured-data part.
231 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200232THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200233
Christopher Fauletd4696382017-10-24 11:44:05 +0200234/* A global buffer used to store all startup alerts/warnings. It will then be
235 * retrieve on the CLI. */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200236static THREAD_LOCAL char *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200237
William Lallemand723b73a2012-02-08 16:37:49 +0100238struct logformat_var_args {
239 char *name;
240 int mask;
241};
242
243struct logformat_var_args var_args_list[] = {
244// global
245 { "M", LOG_OPT_MANDATORY },
246 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200247 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100248 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100249 { 0, 0 }
250};
251
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200252/* return the name of the directive used in the current proxy for which we're
253 * currently parsing a header, when it is known.
254 */
255static inline const char *fmt_directive(const struct proxy *curproxy)
256{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100257 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200258 case ARGC_ACL:
259 return "acl";
260 case ARGC_STK:
261 return "stick";
262 case ARGC_TRK:
263 return "track-sc";
264 case ARGC_LOG:
265 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200266 case ARGC_LOGSD:
267 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100268 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100269 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100270 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100271 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200272 case ARGC_UIF:
273 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100274 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200275 return "redirect";
276 case ARGC_CAP:
277 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200278 case ARGC_SRV:
279 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200280 case ARGC_SPOE:
281 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100282 case ARGC_UBK:
283 return "use_backend";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100284 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200285 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100286 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200287}
288
William Lallemand723b73a2012-02-08 16:37:49 +0100289/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100290 * callback used to configure addr source retrieval
291 */
292int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
293{
294 curproxy->options2 |= PR_O2_SRC_ADDR;
295
296 return 0;
297}
298
299
300/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100301 * Parse args in a logformat_var. Returns 0 in error
302 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100303 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100304int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100305{
306 int i = 0;
307 int end = 0;
308 int flags = 0; // 1 = + 2 = -
309 char *sp = NULL; // start pointer
310
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100311 if (args == NULL) {
312 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100313 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100314 }
William Lallemand723b73a2012-02-08 16:37:49 +0100315
316 while (1) {
317 if (*args == '\0')
318 end = 1;
319
320 if (*args == '+') {
321 // add flag
322 sp = args + 1;
323 flags = 1;
324 }
325 if (*args == '-') {
326 // delete flag
327 sp = args + 1;
328 flags = 2;
329 }
330
331 if (*args == '\0' || *args == ',') {
332 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100333 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100334 if (strcmp(sp, var_args_list[i].name) == 0) {
335 if (flags == 1) {
336 node->options |= var_args_list[i].mask;
337 break;
338 } else if (flags == 2) {
339 node->options &= ~var_args_list[i].mask;
340 break;
341 }
342 }
343 }
344 sp = NULL;
345 if (end)
346 break;
347 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100348 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100349 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100350 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100351}
352
353/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100354 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
355 * must pass the args part in the <arg> pointer with its length in <arg_len>,
356 * and varname with its length in <var> and <var_len> respectively. <arg> is
357 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100358 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100359 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100360int 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 +0100361{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100362 int j;
363 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100364
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100365 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
366 if (strlen(logformat_keywords[j].name) == var_len &&
367 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
368 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200369 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100370 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100371 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100372 return 0;
373 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100374 node->type = logformat_keywords[j].type;
375 node->options = *defoptions;
376 if (arg_len) {
377 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100378 if (!parse_logformat_var_args(node->arg, node, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100379 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100380 }
381 if (node->type == LOG_FMT_GLOBAL) {
382 *defoptions = node->options;
383 free(node->arg);
384 free(node);
385 } else {
386 if (logformat_keywords[j].config_callback &&
387 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100388 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100389 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100390 curproxy->to_log |= logformat_keywords[j].lw;
391 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100392 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100393 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100394 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
395 curproxy->conf.args.file, curproxy->conf.args.line,
396 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100397 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100398 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100399 memprintf(err, "format variable '%s' is reserved for HTTP mode",
400 logformat_keywords[j].name);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100401 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100402 }
William Lallemand723b73a2012-02-08 16:37:49 +0100403 }
404 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100405
406 j = var[var_len];
407 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100408 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 +0100409 var[var_len] = j;
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100410 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100411}
412
413/*
414 * push to the logformat linked list
415 *
416 * start: start pointer
417 * end: end text pointer
418 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100419 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100420 *
421 * LOG_TEXT: copy chars from start to end excluding end.
422 *
423*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100424int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100425{
426 char *str;
427
Willy Tarreaua3571662012-12-20 21:59:12 +0100428 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200429 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100430 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100431 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100432 return 0;
433 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200434 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100435 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100436 str[end - start] = '\0';
437 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100438 node->type = LOG_FMT_TEXT; // type string
439 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100440 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200441 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100442 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100443 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100444 return 0;
445 }
William Lallemand1d705562012-03-12 12:46:41 +0100446 node->type = LOG_FMT_SEPARATOR;
447 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100448 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100449 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100450}
451
452/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100453 * Parse the sample fetch expression <text> and add a node to <list_format> upon
454 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200455 * should work. The curpx->conf.args.ctx must be set by the caller.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100456 *
457 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100458 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100459int 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 +0100460{
461 char *cmd[2];
462 struct sample_expr *expr;
463 struct logformat_node *node;
464 int cmd_arg;
465
466 cmd[0] = text;
467 cmd[1] = "";
468 cmd_arg = 0;
469
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100470 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 +0100471 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100472 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100473 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100474 }
475
Vincent Bernat02779b62016-04-03 13:48:43 +0200476 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100477 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100478 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100479 return 0;
480 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100481 node->type = LOG_FMT_EXPR;
482 node->expr = expr;
483 node->options = options;
484
485 if (arg_len) {
486 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100487 if (!parse_logformat_var_args(node->arg, node, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100488 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100489 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100490 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100491 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
492
Willy Tarreau434c57c2013-01-08 01:10:24 +0100493 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100494 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
495
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100496 if (!(expr->fetch->val & cap)) {
David Carlier93e8b882017-09-21 14:36:43 +0000497 free(node);
498 node = NULL;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100499 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
500 text, sample_src_names(expr->fetch->use));
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100501 return 0;
502 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100503
Willy Tarreauc8368452012-12-21 00:09:23 +0100504 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
505 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100506 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100507
William Lallemand65ad6e12014-01-31 15:08:02 +0100508 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
509 * needed with some sample fetches (eg: ssl*). We always set it for
510 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100511 */
512 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100513 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100514 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100515 return 1;
Willy Tarreauc8368452012-12-21 00:09:23 +0100516}
517
518/*
William Lallemand723b73a2012-02-08 16:37:49 +0100519 * Parse the log_format string and fill a linked list.
520 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200521 * You can set arguments using { } : %{many arguments}varname.
522 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100523 *
524 * str: the string to parse
525 * curproxy: the proxy affected
526 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100527 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100528 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100529 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100530 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100531 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100532int 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 +0100533{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100534 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100535 char *arg = NULL; /* start pointer for args */
536 char *var = NULL; /* start pointer for vars */
537 int arg_len = 0;
538 int var_len = 0;
539 int cformat; /* current token format */
540 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100541 struct logformat_node *tmplf, *back;
542
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100543 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100544 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100545 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100546 return 0;
547 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200548 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200549
William Lallemand723b73a2012-02-08 16:37:49 +0100550 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100551 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100552 LIST_DEL(&tmplf->list);
553 free(tmplf);
554 }
555
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100556 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100557 pformat = cformat;
558
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100559 if (!*str)
560 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100561
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100562 /* The prinicple of the two-step state machine below is to first detect a change, and
563 * second have all common paths processed at one place. The common paths are the ones
564 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
565 * We use the common LF_INIT state to dispatch to the different final states.
566 */
567 switch (pformat) {
568 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100569 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100570 arg_len = var_len = 0;
571 if (*str == '{') { // optional argument
572 cformat = LF_STARG;
573 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100574 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100575 else if (*str == '[') {
576 cformat = LF_STEXPR;
577 var = str + 1; // store expr in variable name
578 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100579 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100580 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100581 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100582 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100583 else if (*str == '%')
584 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100585 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100586 /* single '%' followed by blank or digit, send them both */
587 cformat = LF_TEXT;
588 pformat = LF_TEXT; /* finally we include the previous char as well */
589 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600590 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 +0100591 *str, (int)(str - backfmt), fmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100592 return 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100593
594 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100595 else
596 cformat = LF_INIT; // handle other cases of litterals
597 break;
598
599 case LF_STARG: // text immediately following '%{'
600 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100601 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100602 arg_len = str - arg;
603 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100604 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100605 break;
606
607 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100608 if (*str == '[') {
609 cformat = LF_STEXPR;
610 var = str + 1; // store expr in variable name
611 break;
612 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100613 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100614 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100615 var = str;
616 break;
617 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100618 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100619 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100620
Willy Tarreauc8368452012-12-21 00:09:23 +0100621 case LF_STEXPR: // text immediately following '%['
622 if (*str == ']') { // end of arg
623 cformat = LF_EDEXPR;
624 var_len = str - var;
625 *str = 0; // needed for parsing the expression
626 }
627 break;
628
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100629 case LF_VAR: // text part of a variable name
630 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100631 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100632 cformat = LF_INIT; // not variable name anymore
633 break;
634
Willy Tarreauc8368452012-12-21 00:09:23 +0100635 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100636 cformat = LF_INIT;
637 }
638
639 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
640 switch (*str) {
641 case '%': cformat = LF_STARTVAR; break;
642 case ' ': cformat = LF_SEPARATOR; break;
643 case 0 : cformat = LF_END; break;
644 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100645 }
646 }
647
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100648 if (cformat != pformat || pformat == LF_SEPARATOR) {
649 switch (pformat) {
650 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100651 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100652 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100653 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100654 case LF_STEXPR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100655 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 +0100656 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100657 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100658 case LF_TEXT:
659 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100660 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100661 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100662 break;
663 }
664 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100665 }
666 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100667
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100668 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100669 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100670 return 0;
671 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100672 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100673
674 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100675}
676
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200677/*
678 * Parse "log" keyword and update <logsrvs> list accordingly.
679 *
680 * When <do_del> is set, it means the "no log" line was parsed, so all log
681 * servers in <logsrvs> are released.
682 *
683 * Otherwise, we try to parse the "log" line. First of all, when the list is not
684 * the global one, we look for the parameter "global". If we find it,
685 * global.logsrvs is copied. Else we parse each arguments.
686 *
687 * The function returns 1 in success case, otherwise, it returns 0 and err is
688 * filled.
689 */
690int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
691{
692 struct sockaddr_storage *sk;
693 struct logsrv *logsrv = NULL;
694 int port1, port2;
695 int cur_arg;
696
697 /*
698 * "no log": delete previous herited or defined syslog
699 * servers.
700 */
701 if (do_del) {
702 struct logsrv *back;
703
704 if (*(args[1]) != 0) {
705 memprintf(err, "'no log' does not expect arguments");
706 goto error;
707 }
708
709 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
710 LIST_DEL(&logsrv->list);
711 free(logsrv);
712 }
713 return 1;
714 }
715
716 /*
717 * "log global": copy global.logrsvs linked list to the end of logsrvs
718 * list. But first, we check (logsrvs != global.logsrvs).
719 */
720 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
721 if (logsrvs == &global.logsrvs) {
722 memprintf(err, "'global' is not supported for a global syslog server");
723 goto error;
724 }
725 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200726 struct logsrv *node;
727
728 list_for_each_entry(node, logsrvs, list) {
729 if (node->ref == logsrv)
730 goto skip_logsrv;
731 }
732
733 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200734 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200735 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200736 LIST_INIT(&node->list);
737 LIST_ADDQ(logsrvs, &node->list);
Christopher Faulet28ac0992018-03-26 16:09:19 +0200738
739 skip_logsrv:
740 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200741 }
742 return 1;
743 }
744
745 /*
746 * "log <address> ...: parse a syslog server line
747 */
748 if (*(args[1]) == 0 || *(args[2]) == 0) {
749 memprintf(err, "expects <address> and <facility> %s as arguments",
750 ((logsrvs == &global.logsrvs) ? "" : "or global"));
751 goto error;
752 }
753
754 logsrv = calloc(1, sizeof(*logsrv));
755 if (!logsrv) {
756 memprintf(err, "out of memory");
757 goto error;
758 }
759
760 /* skip address for now, it will be parsed at the end */
761 cur_arg = 2;
762
763 /* just after the address, a length may be specified */
764 logsrv->maxlen = MAX_SYSLOG_LEN;
765 if (strcmp(args[cur_arg], "len") == 0) {
766 int len = atoi(args[cur_arg+1]);
767 if (len < 80 || len > 65535) {
768 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
769 args[cur_arg+1]);
770 goto error;
771 }
772 logsrv->maxlen = len;
773 cur_arg += 2;
774 }
775 if (logsrv->maxlen > global.max_syslog_len)
776 global.max_syslog_len = logsrv->maxlen;
777
778 /* after the length, a format may be specified */
779 if (strcmp(args[cur_arg], "format") == 0) {
780 logsrv->format = get_log_format(args[cur_arg+1]);
781 if (logsrv->format < 0) {
782 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
783 goto error;
784 }
785 cur_arg += 2;
786 }
787
788 /* parse the facility */
789 logsrv->facility = get_log_facility(args[cur_arg]);
790 if (logsrv->facility < 0) {
791 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
792 goto error;
793 }
794 cur_arg++;
795
796 /* parse the max syslog level (default: debug) */
797 logsrv->level = 7;
798 if (*(args[cur_arg])) {
799 logsrv->level = get_log_level(args[cur_arg]);
800 if (logsrv->level < 0) {
801 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
802 goto error;
803 }
804 cur_arg++;
805 }
806
807 /* parse the limit syslog level (default: emerg) */
808 logsrv->minlvl = 0;
809 if (*(args[cur_arg])) {
810 logsrv->minlvl = get_log_level(args[cur_arg]);
811 if (logsrv->minlvl < 0) {
812 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
813 goto error;
814 }
815 cur_arg++;
816 }
817
818 /* Too many args */
819 if (*(args[cur_arg])) {
820 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
821 goto error;
822 }
823
824 /* now, back to the address */
825 sk = str2sa_range(args[1], NULL, &port1, &port2, err, NULL, NULL, 1);
826 if (!sk)
827 goto error;
828 logsrv->addr = *sk;
829
830 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
831 if (port1 != port2) {
832 memprintf(err, "port ranges and offsets are not allowed in '%s'", args[1]);
833 goto error;
834 }
835 logsrv->addr = *sk;
836 if (!port1)
837 set_host_port(&logsrv->addr, SYSLOG_PORT);
838 }
839 LIST_ADDQ(logsrvs, &logsrv->list);
840 return 1;
841
842 error:
843 free(logsrv);
844 return 0;
845}
846
847
Christopher Fauletd4696382017-10-24 11:44:05 +0200848/* Generic function to display messages prefixed by a label */
849static void print_message(const char *label, const char *fmt, va_list argp)
850{
851 struct tm tm;
852 char *head, *msg;
853
854 head = msg = NULL;
855
856 get_localtime(date.tv_sec, &tm);
857 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
858 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
859 memvprintf(&msg, fmt, argp);
860
861 if (global.mode & MODE_STARTING)
862 memprintf(&startup_logs, "%s%s%s", (startup_logs ? startup_logs : ""), head, msg);
863
864 fprintf(stderr, "%s%s", head, msg);
865 fflush(stderr);
866
867 free(head);
868 free(msg);
869}
870
Willy Tarreaubaaee002006-06-26 02:48:02 +0200871/*
872 * Displays the message on stderr with the date and pid. Overrides the quiet
873 * mode during startup.
874 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100875void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200876{
877 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200878
879 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
880 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +0200881 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200882 va_end(argp);
883 }
884}
885
886
887/*
888 * Displays the message on stderr with the date and pid.
889 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100890void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200891{
892 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893
894 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
895 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +0200896 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200897 va_end(argp);
898 }
899}
900
901/*
902 * Displays the message on <out> only if quiet mode is not set.
903 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200904void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200905{
906 va_list argp;
907
908 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
909 va_start(argp, fmt);
910 vfprintf(out, fmt, argp);
911 fflush(out);
912 va_end(argp);
913 }
914}
915
916/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200917 * returns log format for <fmt> or -1 if not found.
918 */
919int get_log_format(const char *fmt)
920{
921 int format;
922
923 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200924 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200925 format--;
926
927 return format;
928}
929
930/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931 * returns log level for <lev> or -1 if not found.
932 */
933int get_log_level(const char *lev)
934{
935 int level;
936
937 level = NB_LOG_LEVELS - 1;
938 while (level >= 0 && strcmp(log_levels[level], lev))
939 level--;
940
941 return level;
942}
943
Willy Tarreaubaaee002006-06-26 02:48:02 +0200944/*
945 * returns log facility for <fac> or -1 if not found.
946 */
947int get_log_facility(const char *fac)
948{
949 int facility;
950
951 facility = NB_LOG_FACILITIES - 1;
952 while (facility >= 0 && strcmp(log_facilities[facility], fac))
953 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100954
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955 return facility;
956}
957
William Lallemanda1cc3812012-02-08 16:38:44 +0100958/*
Dragan Dosen835b9212016-02-12 13:23:03 +0100959 * Encode the string.
960 *
961 * When using the +E log format option, it will try to escape '"\]'
962 * characters with '\' as prefix. The same prefix should not be used as
963 * <escape>.
964 */
965static char *lf_encode_string(char *start, char *stop,
966 const char escape, const fd_set *map,
967 const char *string,
968 struct logformat_node *node)
969{
970 if (node->options & LOG_OPT_ESC) {
971 if (start < stop) {
972 stop--; /* reserve one byte for the final '\0' */
973 while (start < stop && *string != '\0') {
974 if (!FD_ISSET((unsigned char)(*string), map)) {
975 if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map))
976 *start++ = *string;
977 else {
978 if (start + 2 >= stop)
979 break;
980 *start++ = '\\';
981 *start++ = *string;
982 }
983 }
984 else {
985 if (start + 3 >= stop)
986 break;
987 *start++ = escape;
988 *start++ = hextab[(*string >> 4) & 15];
989 *start++ = hextab[*string & 15];
990 }
991 string++;
992 }
993 *start = '\0';
994 }
995 }
996 else {
997 return encode_string(start, stop, escape, map, string);
998 }
999
1000 return start;
1001}
1002
1003/*
1004 * Encode the chunk.
1005 *
1006 * When using the +E log format option, it will try to escape '"\]'
1007 * characters with '\' as prefix. The same prefix should not be used as
1008 * <escape>.
1009 */
1010static char *lf_encode_chunk(char *start, char *stop,
1011 const char escape, const fd_set *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001012 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001013 struct logformat_node *node)
1014{
1015 char *str, *end;
1016
1017 if (node->options & LOG_OPT_ESC) {
1018 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001019 str = chunk->area;
1020 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001021
1022 stop--; /* reserve one byte for the final '\0' */
1023 while (start < stop && str < end) {
1024 if (!FD_ISSET((unsigned char)(*str), map)) {
1025 if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map))
1026 *start++ = *str;
1027 else {
1028 if (start + 2 >= stop)
1029 break;
1030 *start++ = '\\';
1031 *start++ = *str;
1032 }
1033 }
1034 else {
1035 if (start + 3 >= stop)
1036 break;
1037 *start++ = escape;
1038 *start++ = hextab[(*str >> 4) & 15];
1039 *start++ = hextab[*str & 15];
1040 }
1041 str++;
1042 }
1043 *start = '\0';
1044 }
1045 }
1046 else {
1047 return encode_chunk(start, stop, escape, map, chunk);
1048 }
1049
1050 return start;
1051}
1052
1053/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001054 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001055 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001056 *
1057 * Return the adress of the \0 character, or NULL on error
1058 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001059char *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 +01001060{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001061 if (size < 2)
1062 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001063
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001064 if (node->options & LOG_OPT_QUOTE) {
1065 *(dst++) = '"';
1066 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001067 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001068
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001069 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001070 if (++len > size)
1071 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001072 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001073 char *ret;
1074
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001075 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001076 if (ret == NULL || *ret != '\0')
1077 return NULL;
1078 len = ret - dst;
1079 }
1080 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001081 len = strlcpy2(dst, src, len);
1082 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001083
1084 size -= len;
1085 dst += len;
1086 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001087 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1088 if (size < 2)
1089 return NULL;
1090 *(dst++) = '-';
1091 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001092
1093 if (node->options & LOG_OPT_QUOTE) {
1094 if (size < 2)
1095 return NULL;
1096 *(dst++) = '"';
1097 }
1098
1099 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001100 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001101}
1102
Willy Tarreau26ffa852018-09-05 15:23:10 +02001103static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001104{
1105 return lf_text_len(dst, src, size, size, node);
1106}
1107
William Lallemand5f232402012-04-05 18:02:55 +02001108/*
1109 * Write a IP adress to the log string
1110 * +X option write in hexadecimal notation, most signifant byte on the left
1111 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001112char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001113{
1114 char *ret = dst;
1115 int iret;
1116 char pn[INET6_ADDRSTRLEN];
1117
1118 if (node->options & LOG_OPT_HEXA) {
1119 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1120 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1121 if (iret < 0 || iret > size)
1122 return NULL;
1123 ret += iret;
1124 } else {
1125 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1126 ret = lf_text(dst, pn, size, node);
1127 if (ret == NULL)
1128 return NULL;
1129 }
1130 return ret;
1131}
1132
1133/*
1134 * Write a port to the log
1135 * +X option write in hexadecimal notation, most signifant byte on the left
1136 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001137char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001138{
1139 char *ret = dst;
1140 int iret;
1141
1142 if (node->options & LOG_OPT_HEXA) {
1143 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1144 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1145 if (iret < 0 || iret > size)
1146 return NULL;
1147 ret += iret;
1148 } else {
1149 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1150 if (ret == NULL)
1151 return NULL;
1152 }
1153 return ret;
1154}
1155
Dragan Dosen1322d092015-09-22 16:05:32 +02001156/* Re-generate time-based part of the syslog header in RFC3164 format at
1157 * the beginning of logheader once a second and return the pointer to the
1158 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001159 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001160static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001161{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001162 static THREAD_LOCAL long tvsec;
1163 static THREAD_LOCAL char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreau83061a82018-07-13 11:56:34 +02001164 static THREAD_LOCAL struct buffer host = { };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001165 static THREAD_LOCAL int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001166
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001167 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001168 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +02001169 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001170 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +02001171
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001172 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +02001173 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001174
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001175 if (unlikely(global.log_send_hostname != host.area)) {
1176 host.area = global.log_send_hostname;
1177 host.data = host.area ? strlen(host.area) : 0;
1178 sep = host.data ? 1 : 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001179 }
1180
Dragan Dosen59cee972015-09-19 22:09:02 +02001181 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +02001182 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +02001183 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +02001184 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001185 (int)host.data, host.area, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001186 /* WARNING: depending upon implementations, snprintf may return
1187 * either -1 or the number of bytes that would be needed to store
1188 * the total message. In both cases, we must adjust it.
1189 */
Willy Tarreau18324f52014-06-27 18:10:07 +02001190 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1191 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001192
Dragan Dosen59cee972015-09-19 22:09:02 +02001193 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001194 }
1195
Willy Tarreau094af4e2015-01-07 15:03:42 +01001196 dataptr[0] = 0; // ensure we get rid of any previous attempt
1197
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001198 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001199}
1200
Dragan Dosen1322d092015-09-22 16:05:32 +02001201/* Re-generate time-based part of the syslog header in RFC5424 format at
1202 * the beginning of logheader_rfc5424 once a second and return the pointer
1203 * to the first character after it.
1204 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001205static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +02001206{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001207 static THREAD_LOCAL long tvsec;
1208 static THREAD_LOCAL char *dataptr = NULL; /* backup of last end of header, NULL first time */
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001209 const char *gmt_offset;
Dragan Dosen1322d092015-09-22 16:05:32 +02001210
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001211 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +02001212 /* this string is rebuild only once a second */
1213 struct tm tm;
1214 int hdr_len;
1215
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001216 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +02001217 get_localtime(tvsec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001218 gmt_offset = get_gmt_offset(time, &tm);
Dragan Dosen1322d092015-09-22 16:05:32 +02001219
1220 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +02001221 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +02001222 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +02001223 tm.tm_hour, tm.tm_min, tm.tm_sec,
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001224 gmt_offset, gmt_offset+3,
Dragan Dosen43885c72015-10-01 13:18:13 +02001225 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +02001226 /* WARNING: depending upon implementations, snprintf may return
1227 * either -1 or the number of bytes that would be needed to store
1228 * the total message. In both cases, we must adjust it.
1229 */
1230 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1231 hdr_len = global.max_syslog_len;
1232
1233 dataptr = logheader_rfc5424 + hdr_len;
1234 }
1235
1236 dataptr[0] = 0; // ensure we get rid of any previous attempt
1237
1238 return dataptr;
1239}
1240
William Lallemand2a4a44f2012-02-06 16:00:33 +01001241/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001242 * This function sends the syslog message using a printf format string. It
1243 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001244 */
1245void send_log(struct proxy *p, int level, const char *format, ...)
1246{
1247 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001248 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001249
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001250 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001251 return;
1252
William Lallemand2a4a44f2012-02-06 16:00:33 +01001253 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001254 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001255 if (data_len < 0 || data_len > global.max_syslog_len)
1256 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001257 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001258
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001259 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001260}
1261
1262/*
1263 * This function sends a syslog message.
1264 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001265 * It overrides the last byte of the message vector with an LF character.
1266 * The arguments <sd> and <sd_size> are used for the structured-data part
1267 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001268 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001269void __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 +01001270{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001271 static THREAD_LOCAL struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1272 static THREAD_LOCAL struct msghdr msghdr = {
1273 //.msg_iov = iovec,
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001274 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1275 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001276 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1277 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
1278 static THREAD_LOCAL char *dataptr = NULL;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001279 int fac_level;
1280 struct list *logsrvs = NULL;
1281 struct logsrv *tmp = NULL;
1282 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +02001283 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001284 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001285 time_t time = date.tv_sec;
Willy Tarreau83061a82018-07-13 11:56:34 +02001286 struct buffer *tag = &global.log_tag;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001287 static THREAD_LOCAL int curr_pid;
1288 static THREAD_LOCAL char pidstr[100];
Willy Tarreau83061a82018-07-13 11:56:34 +02001289 static THREAD_LOCAL struct buffer pid;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001290
1291 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001292
1293 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001294
1295 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +02001296 if (!LIST_ISEMPTY(&global.logsrvs)) {
1297 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001298 }
1299 } else {
William Lallemand0f99e342011-10-12 17:50:54 +02001300 if (!LIST_ISEMPTY(&p->logsrvs)) {
1301 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001302 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001303 if (p->log_tag.area) {
Dragan Dosen43885c72015-10-01 13:18:13 +02001304 tag = &p->log_tag;
1305 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001306 }
1307
William Lallemand0f99e342011-10-12 17:50:54 +02001308 if (!logsrvs)
1309 return;
1310
Dragan Dosen43885c72015-10-01 13:18:13 +02001311 if (unlikely(curr_pid != getpid())) {
1312 curr_pid = getpid();
1313 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1314 chunk_initstr(&pid, pidstr);
1315 }
1316
Robert Tsai81ae1952007-12-05 10:47:29 +01001317 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +02001318 nblogger = 0;
1319 list_for_each_entry(tmp, logsrvs, list) {
1320 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +01001321 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +01001322 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +02001323 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +01001324 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001325 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +02001326 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001327 int tag_max = 0;
1328 int pid_sep1_max = 0;
1329 int pid_max = 0;
1330 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001331 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001332 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +01001333
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001334 nblogger++;
1335
Willy Tarreaubaaee002006-06-26 02:48:02 +02001336 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +02001337 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001338 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001339
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001340 if (unlikely(*plogfd < 0)) {
1341 /* socket not successfully initialized yet */
1342 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
1343
1344 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001345 static char once;
1346
1347 if (!once) {
1348 once = 1; /* note: no need for atomic ops here */
1349 ha_alert("socket for logger #%d failed: %s (errno=%d)\n",
1350 nblogger, strerror(errno), errno);
1351 }
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001352 continue;
1353 }
1354 /* we don't want to receive anything on this socket */
1355 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1356 /* does nothing under Linux, maybe needed for others */
1357 shutdown(*plogfd, SHUT_RD);
Christopher Faulet78969172017-12-19 10:35:53 +01001358 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001359 }
1360
Dragan Dosen1322d092015-09-22 16:05:32 +02001361 switch (logsrv->format) {
1362 case LOG_FORMAT_RFC3164:
1363 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001364 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +02001365 break;
1366
1367 case LOG_FORMAT_RFC5424:
1368 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001369 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001370 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001371 break;
1372
1373 default:
1374 continue; /* must never happen */
1375 }
1376
1377 hdr_size = hdr_ptr - hdr;
1378
Willy Tarreaubaaee002006-06-26 02:48:02 +02001379 /* For each target, we may have a different facility.
1380 * We can also have a different log level for each message.
1381 * This induces variations in the message header length.
1382 * Since we don't want to recompute it each time, nor copy it every
1383 * time, we only change the facility in the pre-computed header,
1384 * and we change the pointer to the header accordingly.
1385 */
William Lallemand0f99e342011-10-12 17:50:54 +02001386 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001387 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001388 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001389 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001390 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001391 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001392 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001393 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001394
Dragan Dosen1322d092015-09-22 16:05:32 +02001395 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001396
Dragan Dosen43885c72015-10-01 13:18:13 +02001397 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001398 if (unlikely(hdr_size >= logsrv->maxlen)) {
1399 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001400 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001401 goto send;
1402 }
1403
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001404 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001405
Dragan Dosen43885c72015-10-01 13:18:13 +02001406 /* tag */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001407 tag_max = tag->data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001408 if (unlikely(tag_max >= maxlen)) {
1409 tag_max = maxlen - 1;
1410 sd_max = 0;
1411 goto send;
1412 }
1413
1414 maxlen -= tag_max;
1415
1416 /* first pid separator */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001417 pid_sep1_max = log_formats[logsrv->format].pid.sep1.data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001418 if (unlikely(pid_sep1_max >= maxlen)) {
1419 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001420 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001421 goto send;
1422 }
1423
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001424 pid_sep1 = log_formats[logsrv->format].pid.sep1.area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001425 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001426
Dragan Dosen43885c72015-10-01 13:18:13 +02001427 /* pid */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001428 pid_max = pid.data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001429 if (unlikely(pid_max >= maxlen)) {
1430 pid_max = maxlen - 1;
1431 sd_max = 0;
1432 goto send;
1433 }
1434
1435 maxlen -= pid_max;
1436
1437 /* second pid separator */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001438 pid_sep2_max = log_formats[logsrv->format].pid.sep2.data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001439 if (unlikely(pid_sep2_max >= maxlen)) {
1440 pid_sep2_max = maxlen - 1;
1441 sd_max = 0;
1442 goto send;
1443 }
1444
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001445 pid_sep2 = log_formats[logsrv->format].pid.sep2.area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001446 maxlen -= pid_sep2_max;
1447
1448 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001449 if (sd_max >= maxlen) {
1450 sd_max = maxlen - 1;
1451 goto send;
1452 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001453
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001454 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001455send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001456 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001457 iovec[0].iov_len = hdr_max;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001458 iovec[1].iov_base = tag->area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001459 iovec[1].iov_len = tag_max;
1460 iovec[2].iov_base = pid_sep1;
1461 iovec[2].iov_len = pid_sep1_max;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001462 iovec[3].iov_base = pid.area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001463 iovec[3].iov_len = pid_max;
1464 iovec[4].iov_base = pid_sep2;
1465 iovec[4].iov_len = pid_sep2_max;
1466 iovec[5].iov_base = sd;
1467 iovec[5].iov_len = sd_max;
1468 iovec[6].iov_base = dataptr;
1469 iovec[6].iov_len = max;
1470 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1471 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001472
1473 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1474 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1475
1476 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001477
Robert Tsai81ae1952007-12-05 10:47:29 +01001478 if (sent < 0) {
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001479 static char once;
1480
1481 if (!once) {
1482 once = 1; /* note: no need for atomic ops here */
1483 ha_alert("sendmsg logger #%d failed: %s (errno=%d)\n",
1484 nblogger, strerror(errno), errno);
1485 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001486 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001487 }
1488}
1489
William Lallemandbddd4fd2012-02-27 11:23:10 +01001490extern fd_set hdr_encode_map[];
1491extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001492extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001493
Willy Tarreaubaaee002006-06-26 02:48:02 +02001494
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001495const 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 +01001496const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1497 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1498 Set-cookie Updated, unknown, unknown */
1499
William Lallemand1d705562012-03-12 12:46:41 +01001500/*
1501 * try to write a character if there is enough space, or goto out
1502 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001503#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001504 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001505 *(tmplog++) = (x); \
1506 } else { \
1507 goto out; \
1508 } \
1509 } while(0)
1510
Dragan Dosen835b9212016-02-12 13:23:03 +01001511
1512/* Initializes some log data.
1513 */
1514void init_log()
1515{
1516 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001517 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001518
1519 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1520 * inside PARAM-VALUE should be escaped with '\' as prefix.
1521 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1522 * details.
1523 */
1524 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1525
1526 tmp = "\"\\]";
1527 while (*tmp) {
1528 FD_SET(*tmp, rfc5424_escape_map);
1529 tmp++;
1530 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001531
1532 /* initialize the log header encoding map : '{|}"#' should be encoded with
1533 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1534 * URL encoding only requires '"', '#' to be encoded as well as non-
1535 * printable characters above.
1536 */
1537 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1538 memset(url_encode_map, 0, sizeof(url_encode_map));
1539 for (i = 0; i < 32; i++) {
1540 FD_SET(i, hdr_encode_map);
1541 FD_SET(i, url_encode_map);
1542 }
1543 for (i = 127; i < 256; i++) {
1544 FD_SET(i, hdr_encode_map);
1545 FD_SET(i, url_encode_map);
1546 }
1547
1548 tmp = "\"#{|}";
1549 while (*tmp) {
1550 FD_SET(*tmp, hdr_encode_map);
1551 tmp++;
1552 }
1553
1554 tmp = "\"#";
1555 while (*tmp) {
1556 FD_SET(*tmp, url_encode_map);
1557 tmp++;
1558 }
1559
1560 /* initialize the http header encoding map. The draft httpbis define the
1561 * header content as:
1562 *
1563 * HTTP-message = start-line
1564 * *( header-field CRLF )
1565 * CRLF
1566 * [ message-body ]
1567 * header-field = field-name ":" OWS field-value OWS
1568 * field-value = *( field-content / obs-fold )
1569 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1570 * obs-fold = CRLF 1*( SP / HTAB )
1571 * field-vchar = VCHAR / obs-text
1572 * VCHAR = %x21-7E
1573 * obs-text = %x80-FF
1574 *
1575 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1576 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
1577 * "obs-fold" is volontary forgotten because haproxy remove this.
1578 */
1579 memset(http_encode_map, 0, sizeof(http_encode_map));
1580 for (i = 0x00; i <= 0x08; i++)
1581 FD_SET(i, http_encode_map);
1582 for (i = 0x0a; i <= 0x1f; i++)
1583 FD_SET(i, http_encode_map);
1584 FD_SET(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001585}
William Lallemand1d705562012-03-12 12:46:41 +01001586
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001587static int init_log_buffers_per_thread()
1588{
1589 return init_log_buffers();
1590}
1591
1592static void deinit_log_buffers_per_thread()
1593{
1594 deinit_log_buffers();
1595}
1596
Christopher Faulet0132d062017-07-26 15:33:35 +02001597/* Initialize log buffers used for syslog messages */
1598int init_log_buffers()
1599{
1600 logheader = my_realloc2(logheader, global.max_syslog_len + 1);
1601 logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1);
1602 logline = my_realloc2(logline, global.max_syslog_len + 1);
1603 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
1604 if (!logheader || !logline_rfc5424 || !logline || !logline_rfc5424)
1605 return 0;
1606 return 1;
1607}
1608
1609/* Deinitialize log buffers used for syslog messages */
1610void deinit_log_buffers()
1611{
1612 free(logheader);
1613 free(logheader_rfc5424);
1614 free(logline);
1615 free(logline_rfc5424);
Christopher Fauletd4696382017-10-24 11:44:05 +02001616 free(startup_logs);
Christopher Faulet0132d062017-07-26 15:33:35 +02001617 logheader = NULL;
1618 logheader_rfc5424 = NULL;
1619 logline = NULL;
1620 logline_rfc5424 = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +02001621 startup_logs = NULL;
Christopher Faulet0132d062017-07-26 15:33:35 +02001622}
1623
Willy Tarreaudf974472012-12-28 02:44:01 +01001624/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1625 * <maxsize> characters. Returns the size of the output string in characters,
1626 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001627 * is not zero. It requires a valid session and optionally a stream. If the
1628 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01001629 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001630int 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 +02001631{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001632 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001633 struct proxy *be;
1634 struct http_txn *txn;
1635 const struct strm_logs *logs;
1636 const struct connection *be_conn;
1637 unsigned int s_flags;
1638 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02001639 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001640 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001641 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001642 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001643 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001644 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001645 int t_request;
1646 int hdr;
1647 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001648 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001649 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001650 char *ret;
1651 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001652 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001653 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001654 struct strm_logs tmp_strm_log;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001655
William Lallemandbddd4fd2012-02-27 11:23:10 +01001656 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001657
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001658 if (likely(s)) {
1659 be = s->be;
1660 txn = s->txn;
1661 be_conn = cs_conn(objt_cs(s->si[1].end));
1662 s_flags = s->flags;
1663 uniq_id = s->uniq_id;
1664 logs = &s->logs;
1665 } else {
1666 /* we have no stream so we first need to initialize a few
1667 * things that are needed later. We do increment the request
1668 * ID so that it's uniquely assigned to this request just as
1669 * if the request had reached the point of being processed.
1670 * A request error is reported as it's the only element we have
1671 * here and which justifies emitting such a log.
1672 */
1673 be = fe;
1674 txn = NULL;
1675 be_conn = NULL;
1676 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
1677 uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
1678
1679 /* prepare a valid log structure */
1680 tmp_strm_log.tv_accept = sess->tv_accept;
1681 tmp_strm_log.accept_date = sess->accept_date;
1682 tmp_strm_log.t_handshake = sess->t_handshake;
1683 tmp_strm_log.t_idle = tv_ms_elapsed(&sess->tv_accept, &now) - sess->t_handshake;
1684 tv_zero(&tmp_strm_log.tv_request);
1685 tmp_strm_log.t_queue = -1;
1686 tmp_strm_log.t_connect = -1;
1687 tmp_strm_log.t_data = -1;
1688 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
1689 tmp_strm_log.bytes_in = 0;
1690 tmp_strm_log.bytes_out = 0;
1691 tmp_strm_log.prx_queue_pos = 0;
1692 tmp_strm_log.srv_queue_pos = 0;
1693
1694 logs = &tmp_strm_log;
1695 }
1696
William Lallemandbddd4fd2012-02-27 11:23:10 +01001697 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001698 if (tv_isge(&logs->tv_request, &logs->tv_accept))
1699 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01001700
William Lallemand1d705562012-03-12 12:46:41 +01001701 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001702
William Lallemandbddd4fd2012-02-27 11:23:10 +01001703 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001704 if (LIST_ISEMPTY(list_format))
1705 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001706
William Lallemand1d705562012-03-12 12:46:41 +01001707 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001708 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001709 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001710 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02001711 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001712
Willy Tarreauc8368452012-12-21 00:09:23 +01001713 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001714 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001715 if (!last_isspace) {
1716 LOGCHAR(' ');
1717 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001718 }
1719 break;
1720
William Lallemand1d705562012-03-12 12:46:41 +01001721 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001722 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001723 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001724 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001725 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001726 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001727 last_isspace = 0;
1728 break;
1729
Willy Tarreauc8368452012-12-21 00:09:23 +01001730 case LOG_FMT_EXPR: // sample expression, may be request or response
1731 key = NULL;
1732 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001733 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 +01001734 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001735 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 +01001736 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01001737 ret = lf_encode_chunk(tmplog, dst + maxsize,
1738 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001739 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001740 ret = lf_text_len(tmplog,
1741 key ? key->data.u.str.area : NULL,
1742 key ? key->data.u.str.data : 0,
1743 dst + maxsize - tmplog,
1744 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01001745 if (ret == 0)
1746 goto out;
1747 tmplog = ret;
1748 last_isspace = 0;
1749 break;
1750
Willy Tarreau2beef582012-12-20 17:22:52 +01001751 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001752 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001753 if (conn)
1754 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1755 else
1756 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001757 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001758 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001759 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001760 last_isspace = 0;
1761 break;
1762
Willy Tarreau2beef582012-12-20 17:22:52 +01001763 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001764 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001765 if (conn) {
1766 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001767 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001768 } else {
1769 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1770 dst + maxsize - tmplog, tmp);
1771 }
William Lallemand5f232402012-04-05 18:02:55 +02001772 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001773 else
1774 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1775
William Lallemand5f232402012-04-05 18:02:55 +02001776 if (ret == NULL)
1777 goto out;
1778 tmplog = ret;
1779 last_isspace = 0;
1780 break;
1781
Willy Tarreau2beef582012-12-20 17:22:52 +01001782 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001783 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001784 if (conn) {
1785 conn_get_to_addr(conn);
1786 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1787 }
1788 else
1789 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1790
William Lallemand1d705562012-03-12 12:46:41 +01001791 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001792 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001793 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001794 last_isspace = 0;
1795 break;
1796
Willy Tarreau2beef582012-12-20 17:22:52 +01001797 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001798 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001799 if (conn) {
1800 conn_get_to_addr(conn);
1801 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001802 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001803 else
1804 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001805 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001806 else
1807 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1808
William Lallemand5f232402012-04-05 18:02:55 +02001809 if (ret == NULL)
1810 goto out;
1811 tmplog = ret;
1812 last_isspace = 0;
1813 break;
1814
Willy Tarreau2beef582012-12-20 17:22:52 +01001815 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau2393c5b2018-09-05 15:24:56 +02001816 if (be_conn)
1817 ret = lf_ip(tmplog, (const struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001818 else
1819 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1820
William Lallemand1d705562012-03-12 12:46:41 +01001821 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001822 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001823 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001824 last_isspace = 0;
1825 break;
1826
Willy Tarreau2beef582012-12-20 17:22:52 +01001827 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau2393c5b2018-09-05 15:24:56 +02001828 if (be_conn)
1829 ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001830 else
1831 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1832
William Lallemand5f232402012-04-05 18:02:55 +02001833 if (ret == NULL)
1834 goto out;
1835 tmplog = ret;
1836 last_isspace = 0;
1837 break;
1838
Willy Tarreau2beef582012-12-20 17:22:52 +01001839 case LOG_FMT_SERVERIP: // %si
Willy Tarreau2393c5b2018-09-05 15:24:56 +02001840 if (be_conn)
1841 ret = lf_ip(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001842 else
1843 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1844
William Lallemand5f232402012-04-05 18:02:55 +02001845 if (ret == NULL)
1846 goto out;
1847 tmplog = ret;
1848 last_isspace = 0;
1849 break;
1850
Willy Tarreau2beef582012-12-20 17:22:52 +01001851 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau2393c5b2018-09-05 15:24:56 +02001852 if (be_conn)
1853 ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001854 else
1855 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1856
William Lallemand1d705562012-03-12 12:46:41 +01001857 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001858 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001859 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001860 last_isspace = 0;
1861 break;
1862
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001863 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001864 get_localtime(logs->accept_date.tv_sec, &tm);
1865 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001866 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001867 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001868 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001869 last_isspace = 0;
1870 break;
1871
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001872 case LOG_FMT_tr: // %tr = start of request date
1873 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001874 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 +02001875 get_localtime(tv.tv_sec, &tm);
1876 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
1877 if (ret == NULL)
1878 goto out;
1879 tmplog = ret;
1880 last_isspace = 0;
1881 break;
1882
1883 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001884 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001885 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001886 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001887 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001888 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001889 last_isspace = 0;
1890 break;
1891
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001892 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001893 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 +02001894 get_gmtime(tv.tv_sec, &tm);
1895 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
1896 if (ret == NULL)
1897 goto out;
1898 tmplog = ret;
1899 last_isspace = 0;
1900 break;
1901
1902 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001903 get_localtime(logs->accept_date.tv_sec, &tm);
1904 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08001905 if (ret == NULL)
1906 goto out;
1907 tmplog = ret;
1908 last_isspace = 0;
1909 break;
1910
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001911 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001912 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 +02001913 get_localtime(tv.tv_sec, &tm);
1914 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
1915 if (ret == NULL)
1916 goto out;
1917 tmplog = ret;
1918 last_isspace = 0;
1919 break;
1920
William Lallemand5f232402012-04-05 18:02:55 +02001921 case LOG_FMT_TS: // %Ts
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001922 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001923 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001924 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02001925 if (iret < 0 || iret > dst + maxsize - tmplog)
1926 goto out;
1927 last_isspace = 0;
1928 tmplog += iret;
1929 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001930 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001931 if (ret == NULL)
1932 goto out;
1933 tmplog = ret;
1934 last_isspace = 0;
1935 }
1936 break;
1937
William Lallemand1d705562012-03-12 12:46:41 +01001938 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001939 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001940 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02001941 if (iret < 0 || iret > dst + maxsize - tmplog)
1942 goto out;
1943 last_isspace = 0;
1944 tmplog += iret;
1945 } else {
1946 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001947 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001948 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001949 tmplog, 4);
1950 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001951 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001952 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001953 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001954 }
1955 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001956
William Lallemand1d705562012-03-12 12:46:41 +01001957 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001958 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001959 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001960 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001961 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001962 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001963 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001964 break;
1965
Willy Tarreau773d65f2012-10-12 14:56:11 +02001966 case LOG_FMT_FRONTEND_XPRT: // %ft
1967 src = fe->id;
1968 if (tmp->options & LOG_OPT_QUOTE)
1969 LOGCHAR('"');
1970 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1971 if (iret == 0)
1972 goto out;
1973 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001974 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02001975 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02001976 if (tmp->options & LOG_OPT_QUOTE)
1977 LOGCHAR('"');
1978 last_isspace = 0;
1979 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001980#ifdef USE_OPENSSL
1981 case LOG_FMT_SSL_CIPHER: // %sslc
1982 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001983 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001984 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001985 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001986 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001987 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1988 if (ret == NULL)
1989 goto out;
1990 tmplog = ret;
1991 last_isspace = 0;
1992 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001993
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001994 case LOG_FMT_SSL_VERSION: // %sslv
1995 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001996 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001997 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001998 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001999 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002000 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2001 if (ret == NULL)
2002 goto out;
2003 tmplog = ret;
2004 last_isspace = 0;
2005 break;
2006#endif
William Lallemand1d705562012-03-12 12:46:41 +01002007 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002008 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002009 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002010 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002011 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002012 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002013 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002014 break;
2015
William Lallemand1d705562012-03-12 12:46:41 +01002016 case LOG_FMT_SERVER: // %s
Willy Tarreaue1809df2018-09-05 15:30:16 +02002017 switch (obj_type(s ? s->target : NULL)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002018 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002019 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002020 break;
2021 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002022 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002023 break;
2024 default:
2025 src = "<NOSRV>";
2026 break;
2027 }
William Lallemand5f232402012-04-05 18:02:55 +02002028 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002029 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002030 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002031 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002032 last_isspace = 0;
2033 break;
2034
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002035 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002036 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002037 if (ret == NULL)
2038 goto out;
2039 tmplog = ret;
2040 last_isspace = 0;
2041 break;
2042
2043 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002044 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002045 if (ret == NULL)
2046 goto out;
2047 tmplog = ret;
2048 last_isspace = 0;
2049 break;
2050
2051 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002052 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002053 tmplog, dst + maxsize - tmplog);
2054 if (ret == NULL)
2055 goto out;
2056 tmplog = ret;
2057 last_isspace = 0;
2058 break;
2059
2060 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002061 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002062 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002063 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002064 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002065 last_isspace = 0;
2066 break;
2067
William Lallemand1d705562012-03-12 12:46:41 +01002068 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002069 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002070 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002071 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002072 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002073 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002074 last_isspace = 0;
2075 break;
2076
William Lallemand1d705562012-03-12 12:46:41 +01002077 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002078 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002079 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002080 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002081 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002082 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002083 last_isspace = 0;
2084 break;
2085
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002086 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002087 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002088 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002089 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002090 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002091 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002092 last_isspace = 0;
2093 break;
2094
Willy Tarreau27b639d2016-05-17 17:55:27 +02002095 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002096 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002097 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002098 tmplog, dst + maxsize - tmplog);
2099 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002100 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002101 tmplog, dst + maxsize - tmplog);
2102 if (ret == NULL)
2103 goto out;
2104 tmplog = ret;
2105 last_isspace = 0;
2106 break;
2107
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002108 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2109 if (!(fe->to_log & LW_BYTES))
2110 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002111 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 +02002112 tmplog, dst + maxsize - tmplog);
2113 if (ret == NULL)
2114 goto out;
2115 tmplog = ret;
2116 last_isspace = 0;
2117 break;
2118
2119 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002120 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002121 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002122 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002123 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002124 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002125 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002126 last_isspace = 0;
2127 break;
2128
Willy Tarreau2beef582012-12-20 17:22:52 +01002129 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02002130 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002131 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002132 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002133 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002134 last_isspace = 0;
2135 break;
2136
William Lallemand1d705562012-03-12 12:46:41 +01002137 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002138 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002139 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002140 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002141 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002142 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002143 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002144 last_isspace = 0;
2145 break;
2146
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002147 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002148 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002149 if (ret == NULL)
2150 goto out;
2151 tmplog = ret;
2152 last_isspace = 0;
2153 break;
2154
Willy Tarreau2beef582012-12-20 17:22:52 +01002155 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002156 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002157 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002158 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002159 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002160 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002161 last_isspace = 0;
2162 break;
2163
Willy Tarreau2beef582012-12-20 17:22:52 +01002164 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002165 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002166 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002167 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002168 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002169 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002170 last_isspace = 0;
2171 break;
2172
William Lallemand1d705562012-03-12 12:46:41 +01002173 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002174 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2175 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002176 *tmplog = '\0';
2177 last_isspace = 0;
2178 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002179
William Lallemand1d705562012-03-12 12:46:41 +01002180 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002181 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2182 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002183 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2184 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 +01002185 last_isspace = 0;
2186 break;
2187
William Lallemand1d705562012-03-12 12:46:41 +01002188 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002189 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002190 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002191 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002192 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002193 last_isspace = 0;
2194 break;
2195
William Lallemand1d705562012-03-12 12:46:41 +01002196 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002197 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002198 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002199 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002200 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002201 last_isspace = 0;
2202 break;
2203
William Lallemand1d705562012-03-12 12:46:41 +01002204 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002205 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002206 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002207 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002208 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002209 last_isspace = 0;
2210 break;
2211
William Lallemand1d705562012-03-12 12:46:41 +01002212 case LOG_FMT_SRVCONN: // %sc
Willy Tarreaue1809df2018-09-05 15:30:16 +02002213 ret = ultoa_o(objt_server(s ? s->target : NULL) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002214 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002215 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002216 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002217 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002218 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002219 last_isspace = 0;
2220 break;
2221
William Lallemand1d705562012-03-12 12:46:41 +01002222 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002223 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002224 LOGCHAR('+');
Willy Tarreauabd71a52018-09-04 19:21:44 +02002225 ret = ltoa_o((s && s->si[1].conn_retries > 0) ?
Willy Tarreau350f4872014-11-28 14:42:25 +01002226 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002227 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002228 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002229 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002230 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002231 last_isspace = 0;
2232 break;
2233
William Lallemand1d705562012-03-12 12:46:41 +01002234 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002235 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002236 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002237 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002238 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002239 last_isspace = 0;
2240 break;
2241
William Lallemand1d705562012-03-12 12:46:41 +01002242 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002243 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002244 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002245 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002246 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002247 last_isspace = 0;
2248 break;
2249
William Lallemand1d705562012-03-12 12:46:41 +01002250 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002251 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002252 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002253 if (tmp->options & LOG_OPT_QUOTE)
2254 LOGCHAR('"');
2255 LOGCHAR('{');
2256 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2257 if (hdr)
2258 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002259 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002260 ret = lf_encode_string(tmplog, dst + maxsize,
2261 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002262 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002263 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002264 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002265 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002266 }
2267 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002268 if (tmp->options & LOG_OPT_QUOTE)
2269 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002270 last_isspace = 0;
2271 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002272 break;
2273
William Lallemand1d705562012-03-12 12:46:41 +01002274 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002275 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002276 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002277 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2278 if (hdr > 0)
2279 LOGCHAR(' ');
2280 if (tmp->options & LOG_OPT_QUOTE)
2281 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002282 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002283 ret = lf_encode_string(tmplog, dst + maxsize,
2284 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002285 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002286 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002287 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002288 } else if (!(tmp->options & LOG_OPT_QUOTE))
2289 LOGCHAR('-');
2290 if (tmp->options & LOG_OPT_QUOTE)
2291 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002292 last_isspace = 0;
2293 }
2294 }
2295 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002296
William Lallemand1d705562012-03-12 12:46:41 +01002297
2298 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002299 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002300 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002301 if (tmp->options & LOG_OPT_QUOTE)
2302 LOGCHAR('"');
2303 LOGCHAR('{');
2304 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2305 if (hdr)
2306 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002307 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002308 ret = lf_encode_string(tmplog, dst + maxsize,
2309 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002310 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002311 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002312 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002313 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002314 }
2315 LOGCHAR('}');
2316 last_isspace = 0;
2317 if (tmp->options & LOG_OPT_QUOTE)
2318 LOGCHAR('"');
2319 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002320 break;
2321
William Lallemand1d705562012-03-12 12:46:41 +01002322 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002323 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002324 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002325 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2326 if (hdr > 0)
2327 LOGCHAR(' ');
2328 if (tmp->options & LOG_OPT_QUOTE)
2329 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002330 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002331 ret = lf_encode_string(tmplog, dst + maxsize,
2332 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002333 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002334 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002335 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002336 } else if (!(tmp->options & LOG_OPT_QUOTE))
2337 LOGCHAR('-');
2338 if (tmp->options & LOG_OPT_QUOTE)
2339 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002340 last_isspace = 0;
2341 }
2342 }
2343 break;
2344
William Lallemand1d705562012-03-12 12:46:41 +01002345 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002346 /* Request */
2347 if (tmp->options & LOG_OPT_QUOTE)
2348 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002349 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002350 ret = lf_encode_string(tmplog, dst + maxsize,
2351 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002352 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002353 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002354 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002355 if (tmp->options & LOG_OPT_QUOTE)
2356 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002357 last_isspace = 0;
2358 break;
William Lallemand5f232402012-04-05 18:02:55 +02002359
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002360 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002361 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002362
Willy Tarreaub7636d12015-06-17 19:58:02 +02002363 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002364 LOGCHAR('"');
2365
2366 end = uri + strlen(uri);
2367 // look for the first whitespace character
2368 while (uri < end && !HTTP_IS_SPHT(*uri))
2369 uri++;
2370
2371 // keep advancing past multiple spaces
2372 while (uri < end && HTTP_IS_SPHT(*uri)) {
2373 uri++; nspaces++;
2374 }
2375
2376 // look for first space or question mark after url
2377 spc = uri;
2378 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2379 spc++;
2380
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002381 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002382 chunk.area = "<BADREQ>";
2383 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002384 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002385 chunk.area = uri;
2386 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002387 }
2388
Dragan Dosen835b9212016-02-12 13:23:03 +01002389 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002390 if (ret == NULL || *ret != '\0')
2391 goto out;
2392
2393 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002394 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002395 LOGCHAR('"');
2396
2397 last_isspace = 0;
2398 break;
2399
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002400 case LOG_FMT_HTTP_QUERY: // %HQ
2401 if (tmp->options & LOG_OPT_QUOTE)
2402 LOGCHAR('"');
2403
Willy Tarreau57bc8912016-04-25 17:09:40 +02002404 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002405 chunk.area = "<BADREQ>";
2406 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002407 } else {
2408 uri = txn->uri;
2409 end = uri + strlen(uri);
2410 // look for the first question mark
2411 while (uri < end && *uri != '?')
2412 uri++;
2413
2414 qmark = uri;
2415 // look for first space or question mark after url
2416 while (uri < end && !HTTP_IS_SPHT(*uri))
2417 uri++;
2418
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002419 chunk.area = qmark;
2420 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002421 }
2422
Dragan Dosen835b9212016-02-12 13:23:03 +01002423 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002424 if (ret == NULL || *ret != '\0')
2425 goto out;
2426
2427 tmplog = ret;
2428 if (tmp->options & LOG_OPT_QUOTE)
2429 LOGCHAR('"');
2430
2431 last_isspace = 0;
2432 break;
2433
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002434 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002435 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002436
Willy Tarreaub7636d12015-06-17 19:58:02 +02002437 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002438 LOGCHAR('"');
2439
2440 end = uri + strlen(uri);
2441 // look for the first whitespace character
2442 while (uri < end && !HTTP_IS_SPHT(*uri))
2443 uri++;
2444
2445 // keep advancing past multiple spaces
2446 while (uri < end && HTTP_IS_SPHT(*uri)) {
2447 uri++; nspaces++;
2448 }
2449
2450 // look for first space after url
2451 spc = uri;
2452 while (spc < end && !HTTP_IS_SPHT(*spc))
2453 spc++;
2454
Willy Tarreau57bc8912016-04-25 17:09:40 +02002455 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002456 chunk.area = "<BADREQ>";
2457 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002458 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002459 chunk.area = uri;
2460 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002461 }
2462
Dragan Dosen835b9212016-02-12 13:23:03 +01002463 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002464 if (ret == NULL || *ret != '\0')
2465 goto out;
2466
2467 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002468 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002469 LOGCHAR('"');
2470
2471 last_isspace = 0;
2472 break;
2473
2474 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002475 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002476 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002477 LOGCHAR('"');
2478
2479 end = uri + strlen(uri);
2480 // look for the first whitespace character
2481 spc = uri;
2482 while (spc < end && !HTTP_IS_SPHT(*spc))
2483 spc++;
2484
2485 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002486 chunk.area = "<BADREQ>";
2487 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002488 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002489 chunk.area = uri;
2490 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002491 }
2492
Dragan Dosen835b9212016-02-12 13:23:03 +01002493 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002494 if (ret == NULL || *ret != '\0')
2495 goto out;
2496
2497 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002498 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002499 LOGCHAR('"');
2500
2501 last_isspace = 0;
2502 break;
2503
2504 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002505 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002506 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002507 LOGCHAR('"');
2508
2509 end = uri + strlen(uri);
2510 // look for the first whitespace character
2511 while (uri < end && !HTTP_IS_SPHT(*uri))
2512 uri++;
2513
2514 // keep advancing past multiple spaces
2515 while (uri < end && HTTP_IS_SPHT(*uri)) {
2516 uri++; nspaces++;
2517 }
2518
2519 // look for the next whitespace character
2520 while (uri < end && !HTTP_IS_SPHT(*uri))
2521 uri++;
2522
2523 // keep advancing past multiple spaces
2524 while (uri < end && HTTP_IS_SPHT(*uri))
2525 uri++;
2526
Willy Tarreau57bc8912016-04-25 17:09:40 +02002527 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002528 chunk.area = "<BADREQ>";
2529 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002530 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002531 chunk.area = "HTTP/0.9";
2532 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002533 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002534 chunk.area = uri;
2535 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002536 }
2537
Dragan Dosen835b9212016-02-12 13:23:03 +01002538 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002539 if (ret == NULL || *ret != '\0')
2540 goto out;
2541
2542 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002543 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002544 LOGCHAR('"');
2545
2546 last_isspace = 0;
2547 break;
2548
William Lallemand5f232402012-04-05 18:02:55 +02002549 case LOG_FMT_COUNTER: // %rt
2550 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002551 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002552 if (iret < 0 || iret > dst + maxsize - tmplog)
2553 goto out;
2554 last_isspace = 0;
2555 tmplog += iret;
2556 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002557 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002558 if (ret == NULL)
2559 goto out;
2560 tmplog = ret;
2561 last_isspace = 0;
2562 }
2563 break;
2564
Willy Tarreau7346acb2014-08-28 15:03:15 +02002565 case LOG_FMT_LOGCNT: // %lc
2566 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002567 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002568 if (iret < 0 || iret > dst + maxsize - tmplog)
2569 goto out;
2570 last_isspace = 0;
2571 tmplog += iret;
2572 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002573 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002574 if (ret == NULL)
2575 goto out;
2576 tmplog = ret;
2577 last_isspace = 0;
2578 }
2579 break;
2580
William Lallemand5f232402012-04-05 18:02:55 +02002581 case LOG_FMT_HOSTNAME: // %H
2582 src = hostname;
2583 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2584 if (ret == NULL)
2585 goto out;
2586 tmplog = ret;
2587 last_isspace = 0;
2588 break;
2589
2590 case LOG_FMT_PID: // %pid
2591 if (tmp->options & LOG_OPT_HEXA) {
2592 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2593 if (iret < 0 || iret > dst + maxsize - tmplog)
2594 goto out;
2595 last_isspace = 0;
2596 tmplog += iret;
2597 } else {
2598 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2599 if (ret == NULL)
2600 goto out;
2601 tmplog = ret;
2602 last_isspace = 0;
2603 }
2604 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002605
2606 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002607 ret = NULL;
Willy Tarreau02fdf4f2018-09-05 15:49:01 +02002608 src = s ? s->unique_id : NULL;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002609 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002610 if (ret == NULL)
2611 goto out;
2612 tmplog = ret;
2613 last_isspace = 0;
2614 break;
2615
William Lallemandbddd4fd2012-02-27 11:23:10 +01002616 }
2617 }
2618
2619out:
William Lallemand1d705562012-03-12 12:46:41 +01002620 /* *tmplog is a unused character */
2621 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002622 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002623
Willy Tarreaubaaee002006-06-26 02:48:02 +02002624}
2625
William Lallemand1d705562012-03-12 12:46:41 +01002626/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002627 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002628 * Will not log if the frontend has no log defined.
2629 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002630void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002631{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002632 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002633 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002634 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002635
2636 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002637 err = (s->flags & SF_REDISP) ||
2638 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2639 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002640 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002641 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002642
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002643 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002644 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002645
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002646 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002647 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002648
Willy Tarreauabcd5142013-06-11 17:18:02 +02002649 if (s->logs.level) { /* loglevel was overridden */
2650 if (s->logs.level == -1) {
2651 s->logs.logwait = 0; /* logs disabled */
2652 return;
2653 }
2654 level = s->logs.level - 1;
2655 }
2656 else {
2657 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002658 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002659 level = LOG_ERR;
2660 }
William Lallemand1d705562012-03-12 12:46:41 +01002661
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002662 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002663 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002664 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002665 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002666 }
2667
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002668 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2669 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2670 &sess->fe->logformat_sd);
2671 }
2672
Dragan Dosen59cee972015-09-19 22:09:02 +02002673 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002674 if (size > 0) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002675 HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002676 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002677 s->logs.logwait = 0;
2678 }
2679}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002680
Willy Tarreau53839352018-09-05 19:51:10 +02002681/*
2682 * send a minimalist log for the session. Will not log if the frontend has no
2683 * log defined. It is assumed that this is only used to report anomalies that
2684 * cannot lead to the creation of a regular stream. Because of this the log
2685 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
2686 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02002687 * function to report unimportant events. It is safe to call this function with
2688 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02002689 */
2690void sess_log(struct session *sess)
2691{
2692 int size, level;
2693 int sd_size = 0;
2694
Willy Tarreau9fa267d2018-10-05 10:22:27 +02002695 if (!sess)
2696 return;
2697
Willy Tarreau53839352018-09-05 19:51:10 +02002698 if (LIST_ISEMPTY(&sess->fe->logsrvs))
2699 return;
2700
2701 level = LOG_INFO;
2702 if (sess->fe->options2 & PR_O2_LOGERRORS)
2703 level = LOG_ERR;
2704
2705 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2706 sd_size = sess_build_logline(sess, NULL,
2707 logline_rfc5424, global.max_syslog_len,
2708 &sess->fe->logformat_sd);
2709 }
2710
2711 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
2712 if (size > 0) {
2713 HA_ATOMIC_ADD(&sess->fe->log_count, 1);
2714 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
2715 }
2716}
2717
Christopher Fauletc1b730a2017-10-24 12:00:51 +02002718static int cli_io_handler_show_startup_logs(struct appctx *appctx)
2719{
2720 struct stream_interface *si = appctx->owner;
2721 const char *msg = (startup_logs ? startup_logs : "No startup alerts/warnings.\n");
2722
2723 if (ci_putstr(si_ic(si), msg) == -1) {
2724 si_applet_cant_put(si);
2725 return 0;
2726 }
2727 return 1;
2728}
2729
2730/* register cli keywords */
2731static struct cli_kw_list cli_kws = {{ },{
2732 { { "show", "startup-logs", NULL },
2733 "show startup-logs : report logs emitted during HAProxy startup",
2734 NULL, cli_io_handler_show_startup_logs },
2735 {{},}
2736}};
2737
2738__attribute__((constructor))
2739static void __log_init(void)
2740{
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002741 hap_register_per_thread_init(init_log_buffers_per_thread);
2742 hap_register_per_thread_deinit(deinit_log_buffers_per_thread);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02002743 cli_register_kw(&cli_kws);
2744}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002745/*
2746 * Local variables:
2747 * c-indent-level: 8
2748 * c-basic-offset: 8
2749 * End:
2750 */