blob: 2613940e7ec797c2acd62d41bb91bfeb3fa02ef8 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
Willy Tarreau077edcb2016-08-10 18:30:56 +020025#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020028#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020030#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Christopher Fauletc1b730a2017-10-24 12:00:51 +020032#include <types/cli.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010034#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020035
Christopher Fauletc1b730a2017-10-24 12:00:51 +020036#include <proto/applet.h>
37#include <proto/cli.h>
William Lallemand5f232402012-04-05 18:02:55 +020038#include <proto/frontend.h>
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +000039#include <proto/proto_http.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020040#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010041#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020042#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010043#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020044#ifdef USE_OPENSSL
45#include <proto/ssl_sock.h>
46#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Dragan Dosen43885c72015-10-01 13:18:13 +020048struct log_fmt {
49 char *name;
50 struct {
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))];
83
84#else
85#error "Check if your OS uses bitfields for fd_sets"
86#endif
87
Willy Tarreaubaaee002006-06-26 02:48:02 +020088const char *log_facilities[NB_LOG_FACILITIES] = {
89 "kern", "user", "mail", "daemon",
90 "auth", "syslog", "lpr", "news",
91 "uucp", "cron", "auth2", "ftp",
92 "ntp", "audit", "alert", "cron2",
93 "local0", "local1", "local2", "local3",
94 "local4", "local5", "local6", "local7"
95};
96
Willy Tarreaubaaee002006-06-26 02:48:02 +020097const char *log_levels[NB_LOG_LEVELS] = {
98 "emerg", "alert", "crit", "err",
99 "warning", "notice", "info", "debug"
100};
101
Willy Tarreau570f2212013-06-10 16:42:09 +0200102const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200103const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104
William Lallemand723b73a2012-02-08 16:37:49 +0100105
106/* log_format */
107struct logformat_type {
108 char *name;
109 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100110 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200111 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100112 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100113 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100114};
115
William Lallemandb7ff6a32012-03-02 14:35:21 +0100116int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
117
William Lallemand723b73a2012-02-08 16:37:49 +0100118/* log_format variable names */
119static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200120 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100121
122 /* please keep these lines sorted ! */
123 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
124 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
125 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
126 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
127 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200128 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200129 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200130 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100131 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200132 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
133 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
134 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
135 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
136 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
137 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200138 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100139 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200140 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +0100141 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
142 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200143 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100144 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200145 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100146 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
147 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200148 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200149 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
150 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100151 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
152 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200153 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
154 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100155 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200156 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
157 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
158 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
159 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000160 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
161 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000162 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000163 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
164 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200165 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100166 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200167 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100168 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
169 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100170 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100171 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
172 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
173 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
174 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
175 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200176 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
177 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100178 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200179 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
180 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
181 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100182 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
183 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
184
185 /* The following tags are deprecated and will be removed soon */
186 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
187 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200188 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
189 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
190 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
191 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100192 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
193 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
194 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
195 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
196 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200197 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100198};
199
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200200char default_http_log_format[] = "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
201char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%trg] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl";
Willy Tarreau2beef582012-12-20 17:22:52 +0100202char default_tcp_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq";
William Lallemand723b73a2012-02-08 16:37:49 +0100203char *log_format = NULL;
204
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200205/* Default string used for structured-data part in RFC5424 formatted
206 * syslog messages.
207 */
208char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200209
210/* This is a global syslog header, common to all outgoing messages in
211 * RFC3164 format. It begins with time-based part and is updated by
212 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200213 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200214THREAD_LOCAL char *logheader = NULL;
Dragan Dosen59cee972015-09-19 22:09:02 +0200215
Dragan Dosen1322d092015-09-22 16:05:32 +0200216/* This is a global syslog header for messages in RFC5424 format. It is
217 * updated by update_log_hdr_rfc5424().
218 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200219THREAD_LOCAL char *logheader_rfc5424 = NULL;
Dragan Dosen1322d092015-09-22 16:05:32 +0200220
Dragan Dosen59cee972015-09-19 22:09:02 +0200221/* This is a global syslog message buffer, common to all outgoing
222 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100223 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200224THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100225
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200226/* A global syslog message buffer, common to all RFC5424 syslog messages.
227 * Currently, it is used for generating the structured-data part.
228 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200229THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200230
Christopher Fauletd4696382017-10-24 11:44:05 +0200231/* A global buffer used to store all startup alerts/warnings. It will then be
232 * retrieve on the CLI. */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200233static THREAD_LOCAL char *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200234
William Lallemand723b73a2012-02-08 16:37:49 +0100235struct logformat_var_args {
236 char *name;
237 int mask;
238};
239
240struct logformat_var_args var_args_list[] = {
241// global
242 { "M", LOG_OPT_MANDATORY },
243 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200244 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100245 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100246 { 0, 0 }
247};
248
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200249/* return the name of the directive used in the current proxy for which we're
250 * currently parsing a header, when it is known.
251 */
252static inline const char *fmt_directive(const struct proxy *curproxy)
253{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100254 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200255 case ARGC_ACL:
256 return "acl";
257 case ARGC_STK:
258 return "stick";
259 case ARGC_TRK:
260 return "track-sc";
261 case ARGC_LOG:
262 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200263 case ARGC_LOGSD:
264 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100265 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100266 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100267 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100268 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200269 case ARGC_UIF:
270 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100271 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200272 return "redirect";
273 case ARGC_CAP:
274 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200275 case ARGC_SRV:
276 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200277 case ARGC_SPOE:
278 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100279 case ARGC_UBK:
280 return "use_backend";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100281 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200282 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100283 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200284}
285
William Lallemand723b73a2012-02-08 16:37:49 +0100286/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100287 * callback used to configure addr source retrieval
288 */
289int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
290{
291 curproxy->options2 |= PR_O2_SRC_ADDR;
292
293 return 0;
294}
295
296
297/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100298 * Parse args in a logformat_var. Returns 0 in error
299 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100300 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100301int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100302{
303 int i = 0;
304 int end = 0;
305 int flags = 0; // 1 = + 2 = -
306 char *sp = NULL; // start pointer
307
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100308 if (args == NULL) {
309 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100310 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100311 }
William Lallemand723b73a2012-02-08 16:37:49 +0100312
313 while (1) {
314 if (*args == '\0')
315 end = 1;
316
317 if (*args == '+') {
318 // add flag
319 sp = args + 1;
320 flags = 1;
321 }
322 if (*args == '-') {
323 // delete flag
324 sp = args + 1;
325 flags = 2;
326 }
327
328 if (*args == '\0' || *args == ',') {
329 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100330 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100331 if (strcmp(sp, var_args_list[i].name) == 0) {
332 if (flags == 1) {
333 node->options |= var_args_list[i].mask;
334 break;
335 } else if (flags == 2) {
336 node->options &= ~var_args_list[i].mask;
337 break;
338 }
339 }
340 }
341 sp = NULL;
342 if (end)
343 break;
344 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100345 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100346 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100347 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100348}
349
350/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100351 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
352 * must pass the args part in the <arg> pointer with its length in <arg_len>,
353 * and varname with its length in <var> and <var_len> respectively. <arg> is
354 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100355 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100356 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100357int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100358{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100359 int j;
360 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100361
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100362 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
363 if (strlen(logformat_keywords[j].name) == var_len &&
364 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
365 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200366 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100367 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100368 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100369 return 0;
370 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100371 node->type = logformat_keywords[j].type;
372 node->options = *defoptions;
373 if (arg_len) {
374 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100375 if (!parse_logformat_var_args(node->arg, node, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100376 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100377 }
378 if (node->type == LOG_FMT_GLOBAL) {
379 *defoptions = node->options;
380 free(node->arg);
381 free(node);
382 } else {
383 if (logformat_keywords[j].config_callback &&
384 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100385 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100386 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100387 curproxy->to_log |= logformat_keywords[j].lw;
388 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100389 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100390 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100391 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
392 curproxy->conf.args.file, curproxy->conf.args.line,
393 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100394 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100395 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100396 memprintf(err, "format variable '%s' is reserved for HTTP mode",
397 logformat_keywords[j].name);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100398 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100399 }
William Lallemand723b73a2012-02-08 16:37:49 +0100400 }
401 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100402
403 j = var[var_len];
404 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100405 memprintf(err, "no such format variable '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%'", var);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100406 var[var_len] = j;
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100407 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100408}
409
410/*
411 * push to the logformat linked list
412 *
413 * start: start pointer
414 * end: end text pointer
415 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100416 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100417 *
418 * LOG_TEXT: copy chars from start to end excluding end.
419 *
420*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100421int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100422{
423 char *str;
424
Willy Tarreaua3571662012-12-20 21:59:12 +0100425 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200426 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100427 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100428 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100429 return 0;
430 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200431 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100432 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100433 str[end - start] = '\0';
434 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100435 node->type = LOG_FMT_TEXT; // type string
436 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100437 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200438 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100439 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100440 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100441 return 0;
442 }
William Lallemand1d705562012-03-12 12:46:41 +0100443 node->type = LOG_FMT_SEPARATOR;
444 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100445 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100446 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100447}
448
449/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100450 * Parse the sample fetch expression <text> and add a node to <list_format> upon
451 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200452 * should work. The curpx->conf.args.ctx must be set by the caller.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100453 *
454 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100455 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100456int add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, char **err)
Willy Tarreauc8368452012-12-21 00:09:23 +0100457{
458 char *cmd[2];
459 struct sample_expr *expr;
460 struct logformat_node *node;
461 int cmd_arg;
462
463 cmd[0] = text;
464 cmd[1] = "";
465 cmd_arg = 0;
466
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100467 expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100468 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100469 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100470 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100471 }
472
Vincent Bernat02779b62016-04-03 13:48:43 +0200473 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100474 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100475 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100476 return 0;
477 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100478 node->type = LOG_FMT_EXPR;
479 node->expr = expr;
480 node->options = options;
481
482 if (arg_len) {
483 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100484 if (!parse_logformat_var_args(node->arg, node, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100485 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100486 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100487 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100488 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
489
Willy Tarreau434c57c2013-01-08 01:10:24 +0100490 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100491 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
492
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100493 if (!(expr->fetch->val & cap)) {
David Carlier93e8b882017-09-21 14:36:43 +0000494 free(node);
495 node = NULL;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100496 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
497 text, sample_src_names(expr->fetch->use));
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100498 return 0;
499 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100500
Willy Tarreauc8368452012-12-21 00:09:23 +0100501 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
502 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100503 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100504
William Lallemand65ad6e12014-01-31 15:08:02 +0100505 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
506 * needed with some sample fetches (eg: ssl*). We always set it for
507 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100508 */
509 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100510 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100511 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100512 return 1;
Willy Tarreauc8368452012-12-21 00:09:23 +0100513}
514
515/*
William Lallemand723b73a2012-02-08 16:37:49 +0100516 * Parse the log_format string and fill a linked list.
517 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200518 * You can set arguments using { } : %{many arguments}varname.
519 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100520 *
521 * str: the string to parse
522 * curproxy: the proxy affected
523 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100524 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100525 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100526 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100527 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100528 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100529int parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100530{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100531 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100532 char *arg = NULL; /* start pointer for args */
533 char *var = NULL; /* start pointer for vars */
534 int arg_len = 0;
535 int var_len = 0;
536 int cformat; /* current token format */
537 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100538 struct logformat_node *tmplf, *back;
539
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100540 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100541 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100542 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100543 return 0;
544 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200545 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200546
William Lallemand723b73a2012-02-08 16:37:49 +0100547 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100548 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100549 LIST_DEL(&tmplf->list);
550 free(tmplf);
551 }
552
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100553 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100554 pformat = cformat;
555
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100556 if (!*str)
557 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100558
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100559 /* The prinicple of the two-step state machine below is to first detect a change, and
560 * second have all common paths processed at one place. The common paths are the ones
561 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
562 * We use the common LF_INIT state to dispatch to the different final states.
563 */
564 switch (pformat) {
565 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100566 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100567 arg_len = var_len = 0;
568 if (*str == '{') { // optional argument
569 cformat = LF_STARG;
570 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100571 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100572 else if (*str == '[') {
573 cformat = LF_STEXPR;
574 var = str + 1; // store expr in variable name
575 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100576 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100577 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100578 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100579 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100580 else if (*str == '%')
581 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100582 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100583 /* single '%' followed by blank or digit, send them both */
584 cformat = LF_TEXT;
585 pformat = LF_TEXT; /* finally we include the previous char as well */
586 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600587 memprintf(err, "unexpected variable name near '%c' at position %d line : '%s'. Maybe you want to write a single '%%', use the syntax '%%%%'",
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100588 *str, (int)(str - backfmt), fmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100589 return 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100590
591 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100592 else
593 cformat = LF_INIT; // handle other cases of litterals
594 break;
595
596 case LF_STARG: // text immediately following '%{'
597 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100598 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100599 arg_len = str - arg;
600 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100601 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100602 break;
603
604 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100605 if (*str == '[') {
606 cformat = LF_STEXPR;
607 var = str + 1; // store expr in variable name
608 break;
609 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100610 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100611 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100612 var = str;
613 break;
614 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100615 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100616 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100617
Willy Tarreauc8368452012-12-21 00:09:23 +0100618 case LF_STEXPR: // text immediately following '%['
619 if (*str == ']') { // end of arg
620 cformat = LF_EDEXPR;
621 var_len = str - var;
622 *str = 0; // needed for parsing the expression
623 }
624 break;
625
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100626 case LF_VAR: // text part of a variable name
627 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100628 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100629 cformat = LF_INIT; // not variable name anymore
630 break;
631
Willy Tarreauc8368452012-12-21 00:09:23 +0100632 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100633 cformat = LF_INIT;
634 }
635
636 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
637 switch (*str) {
638 case '%': cformat = LF_STARTVAR; break;
639 case ' ': cformat = LF_SEPARATOR; break;
640 case 0 : cformat = LF_END; break;
641 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100642 }
643 }
644
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100645 if (cformat != pformat || pformat == LF_SEPARATOR) {
646 switch (pformat) {
647 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100648 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100649 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100650 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100651 case LF_STEXPR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100652 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100653 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100654 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100655 case LF_TEXT:
656 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100657 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100658 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100659 break;
660 }
661 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100662 }
663 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100664
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100665 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100666 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100667 return 0;
668 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100669 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100670
671 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100672}
673
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200674/*
675 * Parse "log" keyword and update <logsrvs> list accordingly.
676 *
677 * When <do_del> is set, it means the "no log" line was parsed, so all log
678 * servers in <logsrvs> are released.
679 *
680 * Otherwise, we try to parse the "log" line. First of all, when the list is not
681 * the global one, we look for the parameter "global". If we find it,
682 * global.logsrvs is copied. Else we parse each arguments.
683 *
684 * The function returns 1 in success case, otherwise, it returns 0 and err is
685 * filled.
686 */
687int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
688{
689 struct sockaddr_storage *sk;
690 struct logsrv *logsrv = NULL;
691 int port1, port2;
692 int cur_arg;
693
694 /*
695 * "no log": delete previous herited or defined syslog
696 * servers.
697 */
698 if (do_del) {
699 struct logsrv *back;
700
701 if (*(args[1]) != 0) {
702 memprintf(err, "'no log' does not expect arguments");
703 goto error;
704 }
705
706 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
707 LIST_DEL(&logsrv->list);
708 free(logsrv);
709 }
710 return 1;
711 }
712
713 /*
714 * "log global": copy global.logrsvs linked list to the end of logsrvs
715 * list. But first, we check (logsrvs != global.logsrvs).
716 */
717 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
718 if (logsrvs == &global.logsrvs) {
719 memprintf(err, "'global' is not supported for a global syslog server");
720 goto error;
721 }
722 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200723 struct logsrv *node;
724
725 list_for_each_entry(node, logsrvs, list) {
726 if (node->ref == logsrv)
727 goto skip_logsrv;
728 }
729
730 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200731 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200732 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200733 LIST_INIT(&node->list);
734 LIST_ADDQ(logsrvs, &node->list);
Christopher Faulet28ac0992018-03-26 16:09:19 +0200735
736 skip_logsrv:
737 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200738 }
739 return 1;
740 }
741
742 /*
743 * "log <address> ...: parse a syslog server line
744 */
745 if (*(args[1]) == 0 || *(args[2]) == 0) {
746 memprintf(err, "expects <address> and <facility> %s as arguments",
747 ((logsrvs == &global.logsrvs) ? "" : "or global"));
748 goto error;
749 }
750
751 logsrv = calloc(1, sizeof(*logsrv));
752 if (!logsrv) {
753 memprintf(err, "out of memory");
754 goto error;
755 }
756
757 /* skip address for now, it will be parsed at the end */
758 cur_arg = 2;
759
760 /* just after the address, a length may be specified */
761 logsrv->maxlen = MAX_SYSLOG_LEN;
762 if (strcmp(args[cur_arg], "len") == 0) {
763 int len = atoi(args[cur_arg+1]);
764 if (len < 80 || len > 65535) {
765 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
766 args[cur_arg+1]);
767 goto error;
768 }
769 logsrv->maxlen = len;
770 cur_arg += 2;
771 }
772 if (logsrv->maxlen > global.max_syslog_len)
773 global.max_syslog_len = logsrv->maxlen;
774
775 /* after the length, a format may be specified */
776 if (strcmp(args[cur_arg], "format") == 0) {
777 logsrv->format = get_log_format(args[cur_arg+1]);
778 if (logsrv->format < 0) {
779 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
780 goto error;
781 }
782 cur_arg += 2;
783 }
784
785 /* parse the facility */
786 logsrv->facility = get_log_facility(args[cur_arg]);
787 if (logsrv->facility < 0) {
788 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
789 goto error;
790 }
791 cur_arg++;
792
793 /* parse the max syslog level (default: debug) */
794 logsrv->level = 7;
795 if (*(args[cur_arg])) {
796 logsrv->level = get_log_level(args[cur_arg]);
797 if (logsrv->level < 0) {
798 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
799 goto error;
800 }
801 cur_arg++;
802 }
803
804 /* parse the limit syslog level (default: emerg) */
805 logsrv->minlvl = 0;
806 if (*(args[cur_arg])) {
807 logsrv->minlvl = get_log_level(args[cur_arg]);
808 if (logsrv->minlvl < 0) {
809 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
810 goto error;
811 }
812 cur_arg++;
813 }
814
815 /* Too many args */
816 if (*(args[cur_arg])) {
817 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
818 goto error;
819 }
820
821 /* now, back to the address */
822 sk = str2sa_range(args[1], NULL, &port1, &port2, err, NULL, NULL, 1);
823 if (!sk)
824 goto error;
825 logsrv->addr = *sk;
826
827 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
828 if (port1 != port2) {
829 memprintf(err, "port ranges and offsets are not allowed in '%s'", args[1]);
830 goto error;
831 }
832 logsrv->addr = *sk;
833 if (!port1)
834 set_host_port(&logsrv->addr, SYSLOG_PORT);
835 }
836 LIST_ADDQ(logsrvs, &logsrv->list);
837 return 1;
838
839 error:
840 free(logsrv);
841 return 0;
842}
843
844
Christopher Fauletd4696382017-10-24 11:44:05 +0200845/* Generic function to display messages prefixed by a label */
846static void print_message(const char *label, const char *fmt, va_list argp)
847{
848 struct tm tm;
849 char *head, *msg;
850
851 head = msg = NULL;
852
853 get_localtime(date.tv_sec, &tm);
854 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
855 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
856 memvprintf(&msg, fmt, argp);
857
858 if (global.mode & MODE_STARTING)
859 memprintf(&startup_logs, "%s%s%s", (startup_logs ? startup_logs : ""), head, msg);
860
861 fprintf(stderr, "%s%s", head, msg);
862 fflush(stderr);
863
864 free(head);
865 free(msg);
866}
867
Willy Tarreaubaaee002006-06-26 02:48:02 +0200868/*
869 * Displays the message on stderr with the date and pid. Overrides the quiet
870 * mode during startup.
871 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100872void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873{
874 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200875
876 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
877 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +0200878 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200879 va_end(argp);
880 }
881}
882
883
884/*
885 * Displays the message on stderr with the date and pid.
886 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100887void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200888{
889 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200890
891 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
892 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +0200893 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200894 va_end(argp);
895 }
896}
897
898/*
899 * Displays the message on <out> only if quiet mode is not set.
900 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200901void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200902{
903 va_list argp;
904
905 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
906 va_start(argp, fmt);
907 vfprintf(out, fmt, argp);
908 fflush(out);
909 va_end(argp);
910 }
911}
912
913/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200914 * returns log format for <fmt> or -1 if not found.
915 */
916int get_log_format(const char *fmt)
917{
918 int format;
919
920 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200921 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200922 format--;
923
924 return format;
925}
926
927/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928 * returns log level for <lev> or -1 if not found.
929 */
930int get_log_level(const char *lev)
931{
932 int level;
933
934 level = NB_LOG_LEVELS - 1;
935 while (level >= 0 && strcmp(log_levels[level], lev))
936 level--;
937
938 return level;
939}
940
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941/*
942 * returns log facility for <fac> or -1 if not found.
943 */
944int get_log_facility(const char *fac)
945{
946 int facility;
947
948 facility = NB_LOG_FACILITIES - 1;
949 while (facility >= 0 && strcmp(log_facilities[facility], fac))
950 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100951
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952 return facility;
953}
954
William Lallemanda1cc3812012-02-08 16:38:44 +0100955/*
Dragan Dosen835b9212016-02-12 13:23:03 +0100956 * Encode the string.
957 *
958 * When using the +E log format option, it will try to escape '"\]'
959 * characters with '\' as prefix. The same prefix should not be used as
960 * <escape>.
961 */
962static char *lf_encode_string(char *start, char *stop,
963 const char escape, const fd_set *map,
964 const char *string,
965 struct logformat_node *node)
966{
967 if (node->options & LOG_OPT_ESC) {
968 if (start < stop) {
969 stop--; /* reserve one byte for the final '\0' */
970 while (start < stop && *string != '\0') {
971 if (!FD_ISSET((unsigned char)(*string), map)) {
972 if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map))
973 *start++ = *string;
974 else {
975 if (start + 2 >= stop)
976 break;
977 *start++ = '\\';
978 *start++ = *string;
979 }
980 }
981 else {
982 if (start + 3 >= stop)
983 break;
984 *start++ = escape;
985 *start++ = hextab[(*string >> 4) & 15];
986 *start++ = hextab[*string & 15];
987 }
988 string++;
989 }
990 *start = '\0';
991 }
992 }
993 else {
994 return encode_string(start, stop, escape, map, string);
995 }
996
997 return start;
998}
999
1000/*
1001 * Encode the chunk.
1002 *
1003 * When using the +E log format option, it will try to escape '"\]'
1004 * characters with '\' as prefix. The same prefix should not be used as
1005 * <escape>.
1006 */
1007static char *lf_encode_chunk(char *start, char *stop,
1008 const char escape, const fd_set *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001009 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001010 struct logformat_node *node)
1011{
1012 char *str, *end;
1013
1014 if (node->options & LOG_OPT_ESC) {
1015 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001016 str = chunk->area;
1017 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001018
1019 stop--; /* reserve one byte for the final '\0' */
1020 while (start < stop && str < end) {
1021 if (!FD_ISSET((unsigned char)(*str), map)) {
1022 if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map))
1023 *start++ = *str;
1024 else {
1025 if (start + 2 >= stop)
1026 break;
1027 *start++ = '\\';
1028 *start++ = *str;
1029 }
1030 }
1031 else {
1032 if (start + 3 >= stop)
1033 break;
1034 *start++ = escape;
1035 *start++ = hextab[(*str >> 4) & 15];
1036 *start++ = hextab[*str & 15];
1037 }
1038 str++;
1039 }
1040 *start = '\0';
1041 }
1042 }
1043 else {
1044 return encode_chunk(start, stop, escape, map, chunk);
1045 }
1046
1047 return start;
1048}
1049
1050/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001051 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001052 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001053 *
1054 * Return the adress of the \0 character, or NULL on error
1055 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001056char *lf_text_len(char *dst, const char *src, size_t len, size_t size, struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +01001057{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001058 if (size < 2)
1059 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001060
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001061 if (node->options & LOG_OPT_QUOTE) {
1062 *(dst++) = '"';
1063 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001064 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001065
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001066 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001067 if (++len > size)
1068 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001069 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001070 char *ret;
1071
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001072 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001073 if (ret == NULL || *ret != '\0')
1074 return NULL;
1075 len = ret - dst;
1076 }
1077 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001078 len = strlcpy2(dst, src, len);
1079 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001080
1081 size -= len;
1082 dst += len;
1083 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001084 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1085 if (size < 2)
1086 return NULL;
1087 *(dst++) = '-';
1088 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001089
1090 if (node->options & LOG_OPT_QUOTE) {
1091 if (size < 2)
1092 return NULL;
1093 *(dst++) = '"';
1094 }
1095
1096 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001097 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001098}
1099
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001100static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
1101{
1102 return lf_text_len(dst, src, size, size, node);
1103}
1104
William Lallemand5f232402012-04-05 18:02:55 +02001105/*
1106 * Write a IP adress to the log string
1107 * +X option write in hexadecimal notation, most signifant byte on the left
1108 */
1109char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
1110{
1111 char *ret = dst;
1112 int iret;
1113 char pn[INET6_ADDRSTRLEN];
1114
1115 if (node->options & LOG_OPT_HEXA) {
1116 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1117 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1118 if (iret < 0 || iret > size)
1119 return NULL;
1120 ret += iret;
1121 } else {
1122 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1123 ret = lf_text(dst, pn, size, node);
1124 if (ret == NULL)
1125 return NULL;
1126 }
1127 return ret;
1128}
1129
1130/*
1131 * Write a port to the log
1132 * +X option write in hexadecimal notation, most signifant byte on the left
1133 */
1134char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
1135{
1136 char *ret = dst;
1137 int iret;
1138
1139 if (node->options & LOG_OPT_HEXA) {
1140 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1141 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1142 if (iret < 0 || iret > size)
1143 return NULL;
1144 ret += iret;
1145 } else {
1146 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1147 if (ret == NULL)
1148 return NULL;
1149 }
1150 return ret;
1151}
1152
Dragan Dosen1322d092015-09-22 16:05:32 +02001153/* Re-generate time-based part of the syslog header in RFC3164 format at
1154 * the beginning of logheader once a second and return the pointer to the
1155 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001156 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001157static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001158{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001159 static THREAD_LOCAL long tvsec;
1160 static THREAD_LOCAL char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreau83061a82018-07-13 11:56:34 +02001161 static THREAD_LOCAL struct buffer host = { };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001162 static THREAD_LOCAL int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001163
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001164 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +02001166 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001167 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +02001168
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001169 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +02001170 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001171
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001172 if (unlikely(global.log_send_hostname != host.area)) {
1173 host.area = global.log_send_hostname;
1174 host.data = host.area ? strlen(host.area) : 0;
1175 sep = host.data ? 1 : 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001176 }
1177
Dragan Dosen59cee972015-09-19 22:09:02 +02001178 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +02001179 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +02001180 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +02001181 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001182 (int)host.data, host.area, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001183 /* WARNING: depending upon implementations, snprintf may return
1184 * either -1 or the number of bytes that would be needed to store
1185 * the total message. In both cases, we must adjust it.
1186 */
Willy Tarreau18324f52014-06-27 18:10:07 +02001187 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1188 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001189
Dragan Dosen59cee972015-09-19 22:09:02 +02001190 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001191 }
1192
Willy Tarreau094af4e2015-01-07 15:03:42 +01001193 dataptr[0] = 0; // ensure we get rid of any previous attempt
1194
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001195 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001196}
1197
Dragan Dosen1322d092015-09-22 16:05:32 +02001198/* Re-generate time-based part of the syslog header in RFC5424 format at
1199 * the beginning of logheader_rfc5424 once a second and return the pointer
1200 * to the first character after it.
1201 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001202static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +02001203{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001204 static THREAD_LOCAL long tvsec;
1205 static THREAD_LOCAL char *dataptr = NULL; /* backup of last end of header, NULL first time */
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001206 const char *gmt_offset;
Dragan Dosen1322d092015-09-22 16:05:32 +02001207
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001208 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +02001209 /* this string is rebuild only once a second */
1210 struct tm tm;
1211 int hdr_len;
1212
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001213 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +02001214 get_localtime(tvsec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001215 gmt_offset = get_gmt_offset(time, &tm);
Dragan Dosen1322d092015-09-22 16:05:32 +02001216
1217 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +02001218 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +02001219 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +02001220 tm.tm_hour, tm.tm_min, tm.tm_sec,
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001221 gmt_offset, gmt_offset+3,
Dragan Dosen43885c72015-10-01 13:18:13 +02001222 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +02001223 /* WARNING: depending upon implementations, snprintf may return
1224 * either -1 or the number of bytes that would be needed to store
1225 * the total message. In both cases, we must adjust it.
1226 */
1227 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1228 hdr_len = global.max_syslog_len;
1229
1230 dataptr = logheader_rfc5424 + hdr_len;
1231 }
1232
1233 dataptr[0] = 0; // ensure we get rid of any previous attempt
1234
1235 return dataptr;
1236}
1237
William Lallemand2a4a44f2012-02-06 16:00:33 +01001238/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001239 * This function sends the syslog message using a printf format string. It
1240 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001241 */
1242void send_log(struct proxy *p, int level, const char *format, ...)
1243{
1244 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001245 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001246
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001247 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001248 return;
1249
William Lallemand2a4a44f2012-02-06 16:00:33 +01001250 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001251 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001252 if (data_len < 0 || data_len > global.max_syslog_len)
1253 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001254 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001255
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001256 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001257}
1258
1259/*
1260 * This function sends a syslog message.
1261 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001262 * It overrides the last byte of the message vector with an LF character.
1263 * The arguments <sd> and <sd_size> are used for the structured-data part
1264 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001265 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001266void __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 +01001267{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001268 static THREAD_LOCAL struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1269 static THREAD_LOCAL struct msghdr msghdr = {
1270 //.msg_iov = iovec,
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001271 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1272 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001273 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1274 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
1275 static THREAD_LOCAL char *dataptr = NULL;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001276 int fac_level;
1277 struct list *logsrvs = NULL;
1278 struct logsrv *tmp = NULL;
1279 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +02001280 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001281 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001282 time_t time = date.tv_sec;
Willy Tarreau83061a82018-07-13 11:56:34 +02001283 struct buffer *tag = &global.log_tag;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001284 static THREAD_LOCAL int curr_pid;
1285 static THREAD_LOCAL char pidstr[100];
Willy Tarreau83061a82018-07-13 11:56:34 +02001286 static THREAD_LOCAL struct buffer pid;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001287
1288 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001289
1290 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001291
1292 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +02001293 if (!LIST_ISEMPTY(&global.logsrvs)) {
1294 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001295 }
1296 } else {
William Lallemand0f99e342011-10-12 17:50:54 +02001297 if (!LIST_ISEMPTY(&p->logsrvs)) {
1298 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001299 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001300 if (p->log_tag.area) {
Dragan Dosen43885c72015-10-01 13:18:13 +02001301 tag = &p->log_tag;
1302 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001303 }
1304
William Lallemand0f99e342011-10-12 17:50:54 +02001305 if (!logsrvs)
1306 return;
1307
Dragan Dosen43885c72015-10-01 13:18:13 +02001308 if (unlikely(curr_pid != getpid())) {
1309 curr_pid = getpid();
1310 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1311 chunk_initstr(&pid, pidstr);
1312 }
1313
Robert Tsai81ae1952007-12-05 10:47:29 +01001314 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +02001315 nblogger = 0;
1316 list_for_each_entry(tmp, logsrvs, list) {
1317 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +01001318 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +01001319 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +02001320 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +01001321 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001322 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +02001323 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001324 int tag_max = 0;
1325 int pid_sep1_max = 0;
1326 int pid_max = 0;
1327 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001328 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001329 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +01001330
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001331 nblogger++;
1332
Willy Tarreaubaaee002006-06-26 02:48:02 +02001333 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +02001334 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001335 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001336
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001337 if (unlikely(*plogfd < 0)) {
1338 /* socket not successfully initialized yet */
1339 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
1340
1341 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001342 static char once;
1343
1344 if (!once) {
1345 once = 1; /* note: no need for atomic ops here */
1346 ha_alert("socket for logger #%d failed: %s (errno=%d)\n",
1347 nblogger, strerror(errno), errno);
1348 }
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001349 continue;
1350 }
1351 /* we don't want to receive anything on this socket */
1352 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1353 /* does nothing under Linux, maybe needed for others */
1354 shutdown(*plogfd, SHUT_RD);
Christopher Faulet78969172017-12-19 10:35:53 +01001355 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001356 }
1357
Dragan Dosen1322d092015-09-22 16:05:32 +02001358 switch (logsrv->format) {
1359 case LOG_FORMAT_RFC3164:
1360 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001361 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +02001362 break;
1363
1364 case LOG_FORMAT_RFC5424:
1365 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001366 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001367 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001368 break;
1369
1370 default:
1371 continue; /* must never happen */
1372 }
1373
1374 hdr_size = hdr_ptr - hdr;
1375
Willy Tarreaubaaee002006-06-26 02:48:02 +02001376 /* For each target, we may have a different facility.
1377 * We can also have a different log level for each message.
1378 * This induces variations in the message header length.
1379 * Since we don't want to recompute it each time, nor copy it every
1380 * time, we only change the facility in the pre-computed header,
1381 * and we change the pointer to the header accordingly.
1382 */
William Lallemand0f99e342011-10-12 17:50:54 +02001383 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001384 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001385 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001386 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001387 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001388 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001389 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001390 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001391
Dragan Dosen1322d092015-09-22 16:05:32 +02001392 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001393
Dragan Dosen43885c72015-10-01 13:18:13 +02001394 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001395 if (unlikely(hdr_size >= logsrv->maxlen)) {
1396 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001397 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001398 goto send;
1399 }
1400
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001401 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001402
Dragan Dosen43885c72015-10-01 13:18:13 +02001403 /* tag */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001404 tag_max = tag->data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001405 if (unlikely(tag_max >= maxlen)) {
1406 tag_max = maxlen - 1;
1407 sd_max = 0;
1408 goto send;
1409 }
1410
1411 maxlen -= tag_max;
1412
1413 /* first pid separator */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001414 pid_sep1_max = log_formats[logsrv->format].pid.sep1.data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001415 if (unlikely(pid_sep1_max >= maxlen)) {
1416 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001417 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001418 goto send;
1419 }
1420
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001421 pid_sep1 = log_formats[logsrv->format].pid.sep1.area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001422 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001423
Dragan Dosen43885c72015-10-01 13:18:13 +02001424 /* pid */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001425 pid_max = pid.data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001426 if (unlikely(pid_max >= maxlen)) {
1427 pid_max = maxlen - 1;
1428 sd_max = 0;
1429 goto send;
1430 }
1431
1432 maxlen -= pid_max;
1433
1434 /* second pid separator */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001435 pid_sep2_max = log_formats[logsrv->format].pid.sep2.data;
Dragan Dosen43885c72015-10-01 13:18:13 +02001436 if (unlikely(pid_sep2_max >= maxlen)) {
1437 pid_sep2_max = maxlen - 1;
1438 sd_max = 0;
1439 goto send;
1440 }
1441
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001442 pid_sep2 = log_formats[logsrv->format].pid.sep2.area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001443 maxlen -= pid_sep2_max;
1444
1445 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001446 if (sd_max >= maxlen) {
1447 sd_max = maxlen - 1;
1448 goto send;
1449 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001450
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001451 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001452send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001453 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001454 iovec[0].iov_len = hdr_max;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001455 iovec[1].iov_base = tag->area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001456 iovec[1].iov_len = tag_max;
1457 iovec[2].iov_base = pid_sep1;
1458 iovec[2].iov_len = pid_sep1_max;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001459 iovec[3].iov_base = pid.area;
Dragan Dosen43885c72015-10-01 13:18:13 +02001460 iovec[3].iov_len = pid_max;
1461 iovec[4].iov_base = pid_sep2;
1462 iovec[4].iov_len = pid_sep2_max;
1463 iovec[5].iov_base = sd;
1464 iovec[5].iov_len = sd_max;
1465 iovec[6].iov_base = dataptr;
1466 iovec[6].iov_len = max;
1467 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1468 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001469
1470 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1471 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1472
1473 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001474
Robert Tsai81ae1952007-12-05 10:47:29 +01001475 if (sent < 0) {
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001476 static char once;
1477
1478 if (!once) {
1479 once = 1; /* note: no need for atomic ops here */
1480 ha_alert("sendmsg logger #%d failed: %s (errno=%d)\n",
1481 nblogger, strerror(errno), errno);
1482 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001483 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001484 }
1485}
1486
William Lallemandbddd4fd2012-02-27 11:23:10 +01001487extern fd_set hdr_encode_map[];
1488extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001489extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001490
Willy Tarreaubaaee002006-06-26 02:48:02 +02001491
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001492const 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 +01001493const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1494 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1495 Set-cookie Updated, unknown, unknown */
1496
William Lallemand1d705562012-03-12 12:46:41 +01001497/*
1498 * try to write a character if there is enough space, or goto out
1499 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001500#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001501 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001502 *(tmplog++) = (x); \
1503 } else { \
1504 goto out; \
1505 } \
1506 } while(0)
1507
Dragan Dosen835b9212016-02-12 13:23:03 +01001508
1509/* Initializes some log data.
1510 */
1511void init_log()
1512{
1513 char *tmp;
1514
1515 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1516 * inside PARAM-VALUE should be escaped with '\' as prefix.
1517 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1518 * details.
1519 */
1520 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1521
1522 tmp = "\"\\]";
1523 while (*tmp) {
1524 FD_SET(*tmp, rfc5424_escape_map);
1525 tmp++;
1526 }
1527}
William Lallemand1d705562012-03-12 12:46:41 +01001528
Christopher Fauletcd7879a2017-10-27 13:53:47 +02001529static int init_log_buffers_per_thread()
1530{
1531 return init_log_buffers();
1532}
1533
1534static void deinit_log_buffers_per_thread()
1535{
1536 deinit_log_buffers();
1537}
1538
Christopher Faulet0132d062017-07-26 15:33:35 +02001539/* Initialize log buffers used for syslog messages */
1540int init_log_buffers()
1541{
1542 logheader = my_realloc2(logheader, global.max_syslog_len + 1);
1543 logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1);
1544 logline = my_realloc2(logline, global.max_syslog_len + 1);
1545 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
1546 if (!logheader || !logline_rfc5424 || !logline || !logline_rfc5424)
1547 return 0;
1548 return 1;
1549}
1550
1551/* Deinitialize log buffers used for syslog messages */
1552void deinit_log_buffers()
1553{
1554 free(logheader);
1555 free(logheader_rfc5424);
1556 free(logline);
1557 free(logline_rfc5424);
Christopher Fauletd4696382017-10-24 11:44:05 +02001558 free(startup_logs);
Christopher Faulet0132d062017-07-26 15:33:35 +02001559 logheader = NULL;
1560 logheader_rfc5424 = NULL;
1561 logline = NULL;
1562 logline_rfc5424 = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +02001563 startup_logs = NULL;
Christopher Faulet0132d062017-07-26 15:33:35 +02001564}
1565
Willy Tarreaudf974472012-12-28 02:44:01 +01001566/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1567 * <maxsize> characters. Returns the size of the output string in characters,
1568 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau43c538e2018-09-05 14:58:15 +02001569 * is not zero. It requires a session and a stream.
Willy Tarreaudf974472012-12-28 02:44:01 +01001570 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001571int 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 +02001572{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001573 struct proxy *fe = sess->fe;
Willy Tarreaua21c0e62018-09-05 15:07:15 +02001574 struct proxy *be = s ? s->be : fe;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001575 struct http_txn *txn = s->txn;
Willy Tarreau83061a82018-07-13 11:56:34 +02001576 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001577 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001578 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001579 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001580 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001581 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001582 int t_request;
1583 int hdr;
1584 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001585 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001586 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001587 char *ret;
1588 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001589 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001590 struct timeval tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001591
William Lallemandbddd4fd2012-02-27 11:23:10 +01001592 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001593
William Lallemandbddd4fd2012-02-27 11:23:10 +01001594 t_request = -1;
1595 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1596 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1597
William Lallemand1d705562012-03-12 12:46:41 +01001598 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001599
William Lallemandbddd4fd2012-02-27 11:23:10 +01001600 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001601 if (LIST_ISEMPTY(list_format))
1602 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001603
William Lallemand1d705562012-03-12 12:46:41 +01001604 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001605 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001606 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001607 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02001608 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001609
Willy Tarreauc8368452012-12-21 00:09:23 +01001610 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001611 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001612 if (!last_isspace) {
1613 LOGCHAR(' ');
1614 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001615 }
1616 break;
1617
William Lallemand1d705562012-03-12 12:46:41 +01001618 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001619 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001620 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001621 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001622 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001623 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001624 last_isspace = 0;
1625 break;
1626
Willy Tarreauc8368452012-12-21 00:09:23 +01001627 case LOG_FMT_EXPR: // sample expression, may be request or response
1628 key = NULL;
1629 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001630 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 +01001631 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001632 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 +01001633 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01001634 ret = lf_encode_chunk(tmplog, dst + maxsize,
1635 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001636 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001637 ret = lf_text_len(tmplog,
1638 key ? key->data.u.str.area : NULL,
1639 key ? key->data.u.str.data : 0,
1640 dst + maxsize - tmplog,
1641 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01001642 if (ret == 0)
1643 goto out;
1644 tmplog = ret;
1645 last_isspace = 0;
1646 break;
1647
Willy Tarreau2beef582012-12-20 17:22:52 +01001648 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001649 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001650 if (conn)
1651 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1652 else
1653 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001654 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001655 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001656 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001657 last_isspace = 0;
1658 break;
1659
Willy Tarreau2beef582012-12-20 17:22:52 +01001660 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001661 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001662 if (conn) {
1663 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001664 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001665 } else {
1666 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1667 dst + maxsize - tmplog, tmp);
1668 }
William Lallemand5f232402012-04-05 18:02:55 +02001669 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001670 else
1671 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1672
William Lallemand5f232402012-04-05 18:02:55 +02001673 if (ret == NULL)
1674 goto out;
1675 tmplog = ret;
1676 last_isspace = 0;
1677 break;
1678
Willy Tarreau2beef582012-12-20 17:22:52 +01001679 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001680 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001681 if (conn) {
1682 conn_get_to_addr(conn);
1683 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1684 }
1685 else
1686 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1687
William Lallemand1d705562012-03-12 12:46:41 +01001688 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001689 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001690 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001691 last_isspace = 0;
1692 break;
1693
Willy Tarreau2beef582012-12-20 17:22:52 +01001694 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001695 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001696 if (conn) {
1697 conn_get_to_addr(conn);
1698 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001699 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001700 else
1701 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001702 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001703 else
1704 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1705
William Lallemand5f232402012-04-05 18:02:55 +02001706 if (ret == NULL)
1707 goto out;
1708 tmplog = ret;
1709 last_isspace = 0;
1710 break;
1711
Willy Tarreau2beef582012-12-20 17:22:52 +01001712 case LOG_FMT_BACKENDIP: // %bi
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001713 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001714 if (conn)
1715 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1716 else
1717 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1718
William Lallemand1d705562012-03-12 12:46:41 +01001719 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001720 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001721 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001722 last_isspace = 0;
1723 break;
1724
Willy Tarreau2beef582012-12-20 17:22:52 +01001725 case LOG_FMT_BACKENDPORT: // %bp
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001726 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001727 if (conn)
1728 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1729 else
1730 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1731
William Lallemand5f232402012-04-05 18:02:55 +02001732 if (ret == NULL)
1733 goto out;
1734 tmplog = ret;
1735 last_isspace = 0;
1736 break;
1737
Willy Tarreau2beef582012-12-20 17:22:52 +01001738 case LOG_FMT_SERVERIP: // %si
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001739 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001740 if (conn)
1741 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1742 else
1743 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1744
William Lallemand5f232402012-04-05 18:02:55 +02001745 if (ret == NULL)
1746 goto out;
1747 tmplog = ret;
1748 last_isspace = 0;
1749 break;
1750
Willy Tarreau2beef582012-12-20 17:22:52 +01001751 case LOG_FMT_SERVERPORT: // %sp
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001752 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001753 if (conn)
1754 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1755 else
1756 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1757
William Lallemand1d705562012-03-12 12:46:41 +01001758 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001759 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001760 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001761 last_isspace = 0;
1762 break;
1763
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001764 case LOG_FMT_DATE: // %t = accept date
William Lallemandbddd4fd2012-02-27 11:23:10 +01001765 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001766 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1767 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001768 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001769 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001770 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001771 last_isspace = 0;
1772 break;
1773
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001774 case LOG_FMT_tr: // %tr = start of request date
1775 /* Note that the timers are valid if we get here */
1776 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1777 get_localtime(tv.tv_sec, &tm);
1778 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
1779 if (ret == NULL)
1780 goto out;
1781 tmplog = ret;
1782 last_isspace = 0;
1783 break;
1784
1785 case LOG_FMT_DATEGMT: // %T = accept date, GMT
William Lallemandbddd4fd2012-02-27 11:23:10 +01001786 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001787 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001788 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001789 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001790 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001791 last_isspace = 0;
1792 break;
1793
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001794 case LOG_FMT_trg: // %trg = start of request date, GMT
1795 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1796 get_gmtime(tv.tv_sec, &tm);
1797 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
1798 if (ret == NULL)
1799 goto out;
1800 tmplog = ret;
1801 last_isspace = 0;
1802 break;
1803
1804 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Yuxans Yao4e25b012012-10-19 10:36:09 +08001805 get_localtime(s->logs.accept_date.tv_sec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001806 ret = localdate2str_log(tmplog, s->logs.accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08001807 if (ret == NULL)
1808 goto out;
1809 tmplog = ret;
1810 last_isspace = 0;
1811 break;
1812
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001813 case LOG_FMT_trl: // %trl = start of request date, local
1814 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1815 get_localtime(tv.tv_sec, &tm);
1816 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
1817 if (ret == NULL)
1818 goto out;
1819 tmplog = ret;
1820 last_isspace = 0;
1821 break;
1822
William Lallemand5f232402012-04-05 18:02:55 +02001823 case LOG_FMT_TS: // %Ts
1824 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1825 if (tmp->options & LOG_OPT_HEXA) {
1826 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1827 if (iret < 0 || iret > dst + maxsize - tmplog)
1828 goto out;
1829 last_isspace = 0;
1830 tmplog += iret;
1831 } else {
1832 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1833 if (ret == NULL)
1834 goto out;
1835 tmplog = ret;
1836 last_isspace = 0;
1837 }
1838 break;
1839
William Lallemand1d705562012-03-12 12:46:41 +01001840 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001841 if (tmp->options & LOG_OPT_HEXA) {
1842 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1843 if (iret < 0 || iret > dst + maxsize - tmplog)
1844 goto out;
1845 last_isspace = 0;
1846 tmplog += iret;
1847 } else {
1848 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001849 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001850 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1851 tmplog, 4);
1852 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001853 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001854 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001855 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001856 }
1857 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001858
William Lallemand1d705562012-03-12 12:46:41 +01001859 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001860 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001861 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001862 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001863 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001864 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001865 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001866 break;
1867
Willy Tarreau773d65f2012-10-12 14:56:11 +02001868 case LOG_FMT_FRONTEND_XPRT: // %ft
1869 src = fe->id;
1870 if (tmp->options & LOG_OPT_QUOTE)
1871 LOGCHAR('"');
1872 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1873 if (iret == 0)
1874 goto out;
1875 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001876 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02001877 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02001878 if (tmp->options & LOG_OPT_QUOTE)
1879 LOGCHAR('"');
1880 last_isspace = 0;
1881 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001882#ifdef USE_OPENSSL
1883 case LOG_FMT_SSL_CIPHER: // %sslc
1884 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001885 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001886 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001887 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001888 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001889 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1890 if (ret == NULL)
1891 goto out;
1892 tmplog = ret;
1893 last_isspace = 0;
1894 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001895
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001896 case LOG_FMT_SSL_VERSION: // %sslv
1897 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001898 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001899 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001900 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001901 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001902 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1903 if (ret == NULL)
1904 goto out;
1905 tmplog = ret;
1906 last_isspace = 0;
1907 break;
1908#endif
William Lallemand1d705562012-03-12 12:46:41 +01001909 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001910 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001911 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001912 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001913 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001914 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001915 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001916 break;
1917
William Lallemand1d705562012-03-12 12:46:41 +01001918 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001919 switch (obj_type(s->target)) {
1920 case OBJ_TYPE_SERVER:
1921 src = objt_server(s->target)->id;
1922 break;
1923 case OBJ_TYPE_APPLET:
1924 src = objt_applet(s->target)->name;
1925 break;
1926 default:
1927 src = "<NOSRV>";
1928 break;
1929 }
William Lallemand5f232402012-04-05 18:02:55 +02001930 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001931 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001932 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001933 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001934 last_isspace = 0;
1935 break;
1936
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001937 case LOG_FMT_Th: // %Th = handshake time
1938 ret = ltoa_o(s->logs.t_handshake, tmplog, dst + maxsize - tmplog);
1939 if (ret == NULL)
1940 goto out;
1941 tmplog = ret;
1942 last_isspace = 0;
1943 break;
1944
1945 case LOG_FMT_Ti: // %Ti = HTTP idle time
1946 ret = ltoa_o(s->logs.t_idle, tmplog, dst + maxsize - tmplog);
1947 if (ret == NULL)
1948 goto out;
1949 tmplog = ret;
1950 last_isspace = 0;
1951 break;
1952
1953 case LOG_FMT_TR: // %TR = HTTP request time
1954 ret = ltoa_o((t_request >= 0) ? t_request - s->logs.t_idle - s->logs.t_handshake : -1,
1955 tmplog, dst + maxsize - tmplog);
1956 if (ret == NULL)
1957 goto out;
1958 tmplog = ret;
1959 last_isspace = 0;
1960 break;
1961
1962 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02001963 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001964 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001965 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001966 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001967 last_isspace = 0;
1968 break;
1969
William Lallemand1d705562012-03-12 12:46:41 +01001970 case LOG_FMT_TW: // %Tw
1971 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001972 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001973 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001974 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001975 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001976 last_isspace = 0;
1977 break;
1978
William Lallemand1d705562012-03-12 12:46:41 +01001979 case LOG_FMT_TC: // %Tc
1980 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001981 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001982 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001983 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001984 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001985 last_isspace = 0;
1986 break;
1987
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001988 case LOG_FMT_Tr: // %Tr
William Lallemand1d705562012-03-12 12:46:41 +01001989 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001990 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001991 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001992 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001993 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001994 last_isspace = 0;
1995 break;
1996
Willy Tarreau27b639d2016-05-17 17:55:27 +02001997 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02001998 if (be->mode == PR_MODE_HTTP)
Willy Tarreau27b639d2016-05-17 17:55:27 +02001999 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_close - s->logs.t_data : -1,
2000 tmplog, dst + maxsize - tmplog);
2001 else
2002 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_close - s->logs.t_connect : -1,
2003 tmplog, dst + maxsize - tmplog);
2004 if (ret == NULL)
2005 goto out;
2006 tmplog = ret;
2007 last_isspace = 0;
2008 break;
2009
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002010 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2011 if (!(fe->to_log & LW_BYTES))
2012 LOGCHAR('+');
2013 ret = ltoa_o(s->logs.t_close - (s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0),
2014 tmplog, dst + maxsize - tmplog);
2015 if (ret == NULL)
2016 goto out;
2017 tmplog = ret;
2018 last_isspace = 0;
2019 break;
2020
2021 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002022 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002023 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02002024 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002025 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002026 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002027 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002028 last_isspace = 0;
2029 break;
2030
Willy Tarreau2beef582012-12-20 17:22:52 +01002031 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02002032 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002033 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002034 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002035 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002036 last_isspace = 0;
2037 break;
2038
William Lallemand1d705562012-03-12 12:46:41 +01002039 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002040 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002041 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02002042 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002043 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002044 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002045 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002046 last_isspace = 0;
2047 break;
2048
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002049 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002050 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
2051 if (ret == NULL)
2052 goto out;
2053 tmplog = ret;
2054 last_isspace = 0;
2055 break;
2056
Willy Tarreau2beef582012-12-20 17:22:52 +01002057 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002058 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002059 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002060 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002061 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002062 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002063 last_isspace = 0;
2064 break;
2065
Willy Tarreau2beef582012-12-20 17:22:52 +01002066 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002067 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002068 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002069 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002070 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002071 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002072 last_isspace = 0;
2073 break;
2074
William Lallemand1d705562012-03-12 12:46:41 +01002075 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02002076 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2077 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002078 *tmplog = '\0';
2079 last_isspace = 0;
2080 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002081
William Lallemand1d705562012-03-12 12:46:41 +01002082 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002083 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2084 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002085 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2086 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 +01002087 last_isspace = 0;
2088 break;
2089
William Lallemand1d705562012-03-12 12:46:41 +01002090 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002091 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002092 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002093 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002094 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002095 last_isspace = 0;
2096 break;
2097
William Lallemand1d705562012-03-12 12:46:41 +01002098 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002099 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002100 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002101 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002102 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002103 last_isspace = 0;
2104 break;
2105
William Lallemand1d705562012-03-12 12:46:41 +01002106 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002107 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002108 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002109 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002110 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002111 last_isspace = 0;
2112 break;
2113
William Lallemand1d705562012-03-12 12:46:41 +01002114 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01002115 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002116 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002117 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002118 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002119 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002120 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002121 last_isspace = 0;
2122 break;
2123
William Lallemand1d705562012-03-12 12:46:41 +01002124 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02002125 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002126 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01002127 ret = ltoa_o((s->si[1].conn_retries>0) ?
2128 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002129 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002130 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002131 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002132 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002133 last_isspace = 0;
2134 break;
2135
William Lallemand1d705562012-03-12 12:46:41 +01002136 case LOG_FMT_SRVQUEUE: // %sq
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04002137 ret = ltoa_o(s->logs.srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002138 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002139 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002140 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002141 last_isspace = 0;
2142 break;
2143
William Lallemand1d705562012-03-12 12:46:41 +01002144 case LOG_FMT_BCKQUEUE: // %bq
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04002145 ret = ltoa_o(s->logs.prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002146 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002147 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002148 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002149 last_isspace = 0;
2150 break;
2151
William Lallemand1d705562012-03-12 12:46:41 +01002152 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002153 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002154 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002155 if (tmp->options & LOG_OPT_QUOTE)
2156 LOGCHAR('"');
2157 LOGCHAR('{');
2158 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2159 if (hdr)
2160 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002161 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002162 ret = lf_encode_string(tmplog, dst + maxsize,
2163 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002164 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002165 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002166 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002167 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002168 }
2169 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002170 if (tmp->options & LOG_OPT_QUOTE)
2171 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002172 last_isspace = 0;
2173 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002174 break;
2175
William Lallemand1d705562012-03-12 12:46:41 +01002176 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002177 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002178 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002179 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2180 if (hdr > 0)
2181 LOGCHAR(' ');
2182 if (tmp->options & LOG_OPT_QUOTE)
2183 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002184 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002185 ret = lf_encode_string(tmplog, dst + maxsize,
2186 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002187 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002188 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002189 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002190 } else if (!(tmp->options & LOG_OPT_QUOTE))
2191 LOGCHAR('-');
2192 if (tmp->options & LOG_OPT_QUOTE)
2193 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002194 last_isspace = 0;
2195 }
2196 }
2197 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198
William Lallemand1d705562012-03-12 12:46:41 +01002199
2200 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002201 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002202 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002203 if (tmp->options & LOG_OPT_QUOTE)
2204 LOGCHAR('"');
2205 LOGCHAR('{');
2206 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2207 if (hdr)
2208 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002209 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002210 ret = lf_encode_string(tmplog, dst + maxsize,
2211 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002212 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002213 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002214 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002215 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002216 }
2217 LOGCHAR('}');
2218 last_isspace = 0;
2219 if (tmp->options & LOG_OPT_QUOTE)
2220 LOGCHAR('"');
2221 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002222 break;
2223
William Lallemand1d705562012-03-12 12:46:41 +01002224 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002225 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002226 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002227 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2228 if (hdr > 0)
2229 LOGCHAR(' ');
2230 if (tmp->options & LOG_OPT_QUOTE)
2231 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002232 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002233 ret = lf_encode_string(tmplog, dst + maxsize,
2234 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002235 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002236 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002237 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002238 } else if (!(tmp->options & LOG_OPT_QUOTE))
2239 LOGCHAR('-');
2240 if (tmp->options & LOG_OPT_QUOTE)
2241 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002242 last_isspace = 0;
2243 }
2244 }
2245 break;
2246
William Lallemand1d705562012-03-12 12:46:41 +01002247 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002248 /* Request */
2249 if (tmp->options & LOG_OPT_QUOTE)
2250 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002251 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002252 ret = lf_encode_string(tmplog, dst + maxsize,
2253 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002254 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002255 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002256 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002257 if (tmp->options & LOG_OPT_QUOTE)
2258 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002259 last_isspace = 0;
2260 break;
William Lallemand5f232402012-04-05 18:02:55 +02002261
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002262 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002263 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002264
Willy Tarreaub7636d12015-06-17 19:58:02 +02002265 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002266 LOGCHAR('"');
2267
2268 end = uri + strlen(uri);
2269 // look for the first whitespace character
2270 while (uri < end && !HTTP_IS_SPHT(*uri))
2271 uri++;
2272
2273 // keep advancing past multiple spaces
2274 while (uri < end && HTTP_IS_SPHT(*uri)) {
2275 uri++; nspaces++;
2276 }
2277
2278 // look for first space or question mark after url
2279 spc = uri;
2280 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2281 spc++;
2282
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002283 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002284 chunk.area = "<BADREQ>";
2285 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002286 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002287 chunk.area = uri;
2288 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002289 }
2290
Dragan Dosen835b9212016-02-12 13:23:03 +01002291 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002292 if (ret == NULL || *ret != '\0')
2293 goto out;
2294
2295 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002296 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002297 LOGCHAR('"');
2298
2299 last_isspace = 0;
2300 break;
2301
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002302 case LOG_FMT_HTTP_QUERY: // %HQ
2303 if (tmp->options & LOG_OPT_QUOTE)
2304 LOGCHAR('"');
2305
Willy Tarreau57bc8912016-04-25 17:09:40 +02002306 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002307 chunk.area = "<BADREQ>";
2308 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002309 } else {
2310 uri = txn->uri;
2311 end = uri + strlen(uri);
2312 // look for the first question mark
2313 while (uri < end && *uri != '?')
2314 uri++;
2315
2316 qmark = uri;
2317 // look for first space or question mark after url
2318 while (uri < end && !HTTP_IS_SPHT(*uri))
2319 uri++;
2320
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002321 chunk.area = qmark;
2322 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002323 }
2324
Dragan Dosen835b9212016-02-12 13:23:03 +01002325 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002326 if (ret == NULL || *ret != '\0')
2327 goto out;
2328
2329 tmplog = ret;
2330 if (tmp->options & LOG_OPT_QUOTE)
2331 LOGCHAR('"');
2332
2333 last_isspace = 0;
2334 break;
2335
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002336 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002337 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002338
Willy Tarreaub7636d12015-06-17 19:58:02 +02002339 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002340 LOGCHAR('"');
2341
2342 end = uri + strlen(uri);
2343 // look for the first whitespace character
2344 while (uri < end && !HTTP_IS_SPHT(*uri))
2345 uri++;
2346
2347 // keep advancing past multiple spaces
2348 while (uri < end && HTTP_IS_SPHT(*uri)) {
2349 uri++; nspaces++;
2350 }
2351
2352 // look for first space after url
2353 spc = uri;
2354 while (spc < end && !HTTP_IS_SPHT(*spc))
2355 spc++;
2356
Willy Tarreau57bc8912016-04-25 17:09:40 +02002357 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002358 chunk.area = "<BADREQ>";
2359 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002360 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002361 chunk.area = uri;
2362 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002363 }
2364
Dragan Dosen835b9212016-02-12 13:23:03 +01002365 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002366 if (ret == NULL || *ret != '\0')
2367 goto out;
2368
2369 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002370 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002371 LOGCHAR('"');
2372
2373 last_isspace = 0;
2374 break;
2375
2376 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002377 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002378 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002379 LOGCHAR('"');
2380
2381 end = uri + strlen(uri);
2382 // look for the first whitespace character
2383 spc = uri;
2384 while (spc < end && !HTTP_IS_SPHT(*spc))
2385 spc++;
2386
2387 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002388 chunk.area = "<BADREQ>";
2389 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002390 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002391 chunk.area = uri;
2392 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002393 }
2394
Dragan Dosen835b9212016-02-12 13:23:03 +01002395 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002396 if (ret == NULL || *ret != '\0')
2397 goto out;
2398
2399 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002400 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002401 LOGCHAR('"');
2402
2403 last_isspace = 0;
2404 break;
2405
2406 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002407 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002408 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002409 LOGCHAR('"');
2410
2411 end = uri + strlen(uri);
2412 // look for the first whitespace character
2413 while (uri < end && !HTTP_IS_SPHT(*uri))
2414 uri++;
2415
2416 // keep advancing past multiple spaces
2417 while (uri < end && HTTP_IS_SPHT(*uri)) {
2418 uri++; nspaces++;
2419 }
2420
2421 // look for the next whitespace character
2422 while (uri < end && !HTTP_IS_SPHT(*uri))
2423 uri++;
2424
2425 // keep advancing past multiple spaces
2426 while (uri < end && HTTP_IS_SPHT(*uri))
2427 uri++;
2428
Willy Tarreau57bc8912016-04-25 17:09:40 +02002429 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002430 chunk.area = "<BADREQ>";
2431 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002432 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002433 chunk.area = "HTTP/0.9";
2434 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002435 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002436 chunk.area = uri;
2437 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002438 }
2439
Dragan Dosen835b9212016-02-12 13:23:03 +01002440 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002441 if (ret == NULL || *ret != '\0')
2442 goto out;
2443
2444 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002445 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002446 LOGCHAR('"');
2447
2448 last_isspace = 0;
2449 break;
2450
William Lallemand5f232402012-04-05 18:02:55 +02002451 case LOG_FMT_COUNTER: // %rt
2452 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002453 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002454 if (iret < 0 || iret > dst + maxsize - tmplog)
2455 goto out;
2456 last_isspace = 0;
2457 tmplog += iret;
2458 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002459 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002460 if (ret == NULL)
2461 goto out;
2462 tmplog = ret;
2463 last_isspace = 0;
2464 }
2465 break;
2466
Willy Tarreau7346acb2014-08-28 15:03:15 +02002467 case LOG_FMT_LOGCNT: // %lc
2468 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002469 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002470 if (iret < 0 || iret > dst + maxsize - tmplog)
2471 goto out;
2472 last_isspace = 0;
2473 tmplog += iret;
2474 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002475 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002476 if (ret == NULL)
2477 goto out;
2478 tmplog = ret;
2479 last_isspace = 0;
2480 }
2481 break;
2482
William Lallemand5f232402012-04-05 18:02:55 +02002483 case LOG_FMT_HOSTNAME: // %H
2484 src = hostname;
2485 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2486 if (ret == NULL)
2487 goto out;
2488 tmplog = ret;
2489 last_isspace = 0;
2490 break;
2491
2492 case LOG_FMT_PID: // %pid
2493 if (tmp->options & LOG_OPT_HEXA) {
2494 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2495 if (iret < 0 || iret > dst + maxsize - tmplog)
2496 goto out;
2497 last_isspace = 0;
2498 tmplog += iret;
2499 } else {
2500 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2501 if (ret == NULL)
2502 goto out;
2503 tmplog = ret;
2504 last_isspace = 0;
2505 }
2506 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002507
2508 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002509 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01002510 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002511 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002512 if (ret == NULL)
2513 goto out;
2514 tmplog = ret;
2515 last_isspace = 0;
2516 break;
2517
William Lallemandbddd4fd2012-02-27 11:23:10 +01002518 }
2519 }
2520
2521out:
William Lallemand1d705562012-03-12 12:46:41 +01002522 /* *tmplog is a unused character */
2523 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002524 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002525
Willy Tarreaubaaee002006-06-26 02:48:02 +02002526}
2527
William Lallemand1d705562012-03-12 12:46:41 +01002528/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002529 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002530 * Will not log if the frontend has no log defined.
2531 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002532void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002533{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002534 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002535 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002536 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002537
2538 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002539 err = (s->flags & SF_REDISP) ||
2540 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2541 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002542 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002543 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002544
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002545 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002546 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002547
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002548 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002549 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002550
Willy Tarreauabcd5142013-06-11 17:18:02 +02002551 if (s->logs.level) { /* loglevel was overridden */
2552 if (s->logs.level == -1) {
2553 s->logs.logwait = 0; /* logs disabled */
2554 return;
2555 }
2556 level = s->logs.level - 1;
2557 }
2558 else {
2559 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002560 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002561 level = LOG_ERR;
2562 }
William Lallemand1d705562012-03-12 12:46:41 +01002563
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002564 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002565 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002566 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002567 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002568 }
2569
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002570 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2571 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2572 &sess->fe->logformat_sd);
2573 }
2574
Dragan Dosen59cee972015-09-19 22:09:02 +02002575 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002576 if (size > 0) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002577 HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002578 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002579 s->logs.logwait = 0;
2580 }
2581}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002582
Christopher Fauletc1b730a2017-10-24 12:00:51 +02002583static int cli_io_handler_show_startup_logs(struct appctx *appctx)
2584{
2585 struct stream_interface *si = appctx->owner;
2586 const char *msg = (startup_logs ? startup_logs : "No startup alerts/warnings.\n");
2587
2588 if (ci_putstr(si_ic(si), msg) == -1) {
2589 si_applet_cant_put(si);
2590 return 0;
2591 }
2592 return 1;
2593}
2594
2595/* register cli keywords */
2596static struct cli_kw_list cli_kws = {{ },{
2597 { { "show", "startup-logs", NULL },
2598 "show startup-logs : report logs emitted during HAProxy startup",
2599 NULL, cli_io_handler_show_startup_logs },
2600 {{},}
2601}};
2602
2603__attribute__((constructor))
2604static void __log_init(void)
2605{
Christopher Fauletcd7879a2017-10-27 13:53:47 +02002606 hap_register_per_thread_init(init_log_buffers_per_thread);
2607 hap_register_per_thread_deinit(deinit_log_buffers_per_thread);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02002608 cli_register_kw(&cli_kws);
2609}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002610/*
2611 * Local variables:
2612 * c-indent-level: 8
2613 * c-basic-offset: 8
2614 * End:
2615 */