blob: 3ce41cf5fe5a738418649d1b74e6d5e0d9195eb7 [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 Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/applet-t.h>
Emeric Brun12941c82020-07-07 14:19:42 +020029#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020031#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020033#include <haproxy/http.h>
Emeric Brun12941c82020-07-07 14:19:42 +020034#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020035#include <haproxy/log.h>
Emeric Brun12941c82020-07-07 14:19:42 +020036#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/sample.h>
38#include <haproxy/sink.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020039#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020040#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020041#include <haproxy/stream_interface.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020042#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020043#include <haproxy/tools.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Emeric Brun45c457a2020-07-09 23:23:34 +020045/* global recv logs counter */
46int cum_log_messages;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Emeric Brun12941c82020-07-07 14:19:42 +020048/* log forward proxy list */
49struct proxy *cfg_log_forward;
50
Emeric Brun54648852020-07-06 15:54:06 +020051struct log_fmt_st {
Dragan Dosen43885c72015-10-01 13:18:13 +020052 char *name;
Dragan Dosen43885c72015-10-01 13:18:13 +020053};
54
Emeric Brun54648852020-07-06 15:54:06 +020055static const struct log_fmt_st log_formats[LOG_FORMATS] = {
Emeric Brun0237c4e2020-11-27 16:24:34 +010056 [LOG_FORMAT_LOCAL] = {
57 .name = "local",
58 },
Dragan Dosen43885c72015-10-01 13:18:13 +020059 [LOG_FORMAT_RFC3164] = {
60 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020061 },
62 [LOG_FORMAT_RFC5424] = {
63 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020064 },
65 [LOG_FORMAT_PRIO] = {
66 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010067 },
68 [LOG_FORMAT_SHORT] = {
69 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020070 },
71 [LOG_FORMAT_TIMED] = {
72 .name = "timed",
73 },
74 [LOG_FORMAT_ISO] = {
75 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010076 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010077 [LOG_FORMAT_RAW] = {
78 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010079 },
Dragan Dosen1322d092015-09-22 16:05:32 +020080};
81
Dragan Dosen835b9212016-02-12 13:23:03 +010082/*
83 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020084 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
85 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
86 * that the byte should be escaped. Be careful to always pass bytes from 0 to
87 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010088 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020089long rfc5424_escape_map[(256/8) / sizeof(long)];
90long hdr_encode_map[(256/8) / sizeof(long)];
91long url_encode_map[(256/8) / sizeof(long)];
92long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010093
Dragan Dosen835b9212016-02-12 13:23:03 +010094
Willy Tarreaubaaee002006-06-26 02:48:02 +020095const char *log_facilities[NB_LOG_FACILITIES] = {
96 "kern", "user", "mail", "daemon",
97 "auth", "syslog", "lpr", "news",
98 "uucp", "cron", "auth2", "ftp",
99 "ntp", "audit", "alert", "cron2",
100 "local0", "local1", "local2", "local3",
101 "local4", "local5", "local6", "local7"
102};
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104const char *log_levels[NB_LOG_LEVELS] = {
105 "emerg", "alert", "crit", "err",
106 "warning", "notice", "info", "debug"
107};
108
Willy Tarreau570f2212013-06-10 16:42:09 +0200109const 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 +0200110const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111
William Lallemand723b73a2012-02-08 16:37:49 +0100112
113/* log_format */
114struct logformat_type {
115 char *name;
116 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100117 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200118 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100119 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100120 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100121};
122
William Lallemandb7ff6a32012-03-02 14:35:21 +0100123int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
124
William Lallemand723b73a2012-02-08 16:37:49 +0100125/* log_format variable names */
126static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200127 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100128
129 /* please keep these lines sorted ! */
130 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
131 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
132 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
133 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100134 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200135 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200136 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200137 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100138 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200139 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
140 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
141 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
142 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
143 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
144 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200145 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100146 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200147 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000148 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100149 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
150 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200151 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100152 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200153 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100154 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
155 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200156 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200157 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
158 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100159 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
160 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200161 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
162 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100163 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200164 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
165 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
166 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
167 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000168 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
Maciej Zdebfcdfd852020-11-30 18:27:47 +0000169 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP relative or absolute path */
170 { "HPO", LOG_FMT_HTTP_PATH_ONLY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path only (without host nor query string) */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000171 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000172 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
173 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200174 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100175 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200176 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100177 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
178 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100179 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100180 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
181 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
182 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
183 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
184 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200185 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
186 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100187 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200188 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
189 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
190 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100191 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
192 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
193
194 /* The following tags are deprecated and will be removed soon */
195 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
196 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200197 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
198 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
199 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
200 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100201 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
202 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
203 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
204 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
205 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200206 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100207};
208
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200209char 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
210char 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 +0100211char 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 +0100212char *log_format = NULL;
213
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200214/* Default string used for structured-data part in RFC5424 formatted
215 * syslog messages.
216 */
217char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200218
Willy Tarreau13ef7732018-11-12 07:25:28 +0100219/* total number of dropped logs */
220unsigned int dropped_logs = 0;
221
Dragan Dosen59cee972015-09-19 22:09:02 +0200222/* This is a global syslog message buffer, common to all outgoing
223 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100224 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200225THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100226
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200227/* A global syslog message buffer, common to all RFC5424 syslog messages.
228 * Currently, it is used for generating the structured-data part.
229 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200230THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200231
William Lallemand723b73a2012-02-08 16:37:49 +0100232struct logformat_var_args {
233 char *name;
234 int mask;
235};
236
237struct logformat_var_args var_args_list[] = {
238// global
239 { "M", LOG_OPT_MANDATORY },
240 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200241 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100242 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100243 { 0, 0 }
244};
245
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200246/* return the name of the directive used in the current proxy for which we're
247 * currently parsing a header, when it is known.
248 */
249static inline const char *fmt_directive(const struct proxy *curproxy)
250{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100251 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200252 case ARGC_ACL:
253 return "acl";
254 case ARGC_STK:
255 return "stick";
256 case ARGC_TRK:
257 return "track-sc";
258 case ARGC_LOG:
259 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200260 case ARGC_LOGSD:
261 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100262 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100263 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100264 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100265 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200266 case ARGC_UIF:
267 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100268 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200269 return "redirect";
270 case ARGC_CAP:
271 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200272 case ARGC_SRV:
273 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200274 case ARGC_SPOE:
275 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100276 case ARGC_UBK:
277 return "use_backend";
Christopher Faulet3b967c12020-05-15 15:47:44 +0200278 case ARGC_HERR:
279 return "http-error";
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +0100280 case ARGC_OT:
281 return "ot-scope";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100282 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200283 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100284 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200285}
286
William Lallemand723b73a2012-02-08 16:37:49 +0100287/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100288 * callback used to configure addr source retrieval
289 */
290int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
291{
292 curproxy->options2 |= PR_O2_SRC_ADDR;
293
294 return 0;
295}
296
297
298/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100299 * Parse args in a logformat_var. Returns 0 in error
300 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100301 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100302int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100303{
304 int i = 0;
305 int end = 0;
306 int flags = 0; // 1 = + 2 = -
307 char *sp = NULL; // start pointer
308
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100309 if (args == NULL) {
310 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100311 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100312 }
William Lallemand723b73a2012-02-08 16:37:49 +0100313
314 while (1) {
315 if (*args == '\0')
316 end = 1;
317
318 if (*args == '+') {
319 // add flag
320 sp = args + 1;
321 flags = 1;
322 }
323 if (*args == '-') {
324 // delete flag
325 sp = args + 1;
326 flags = 2;
327 }
328
329 if (*args == '\0' || *args == ',') {
330 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100331 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100332 if (strcmp(sp, var_args_list[i].name) == 0) {
333 if (flags == 1) {
334 node->options |= var_args_list[i].mask;
335 break;
336 } else if (flags == 2) {
337 node->options &= ~var_args_list[i].mask;
338 break;
339 }
340 }
341 }
342 sp = NULL;
343 if (end)
344 break;
345 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100346 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100347 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100348 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100349}
350
351/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100352 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
353 * must pass the args part in the <arg> pointer with its length in <arg_len>,
354 * and varname with its length in <var> and <var_len> respectively. <arg> is
355 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100356 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100357 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100358int 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 +0100359{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100360 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200361 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100362
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100363 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
364 if (strlen(logformat_keywords[j].name) == var_len &&
365 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
366 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200367 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100368 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100369 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200370 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100371 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100372 node->type = logformat_keywords[j].type;
373 node->options = *defoptions;
374 if (arg_len) {
375 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100376 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200377 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100378 }
379 if (node->type == LOG_FMT_GLOBAL) {
380 *defoptions = node->options;
381 free(node->arg);
382 free(node);
383 } else {
384 if (logformat_keywords[j].config_callback &&
385 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200386 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100387 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100388 curproxy->to_log |= logformat_keywords[j].lw;
Willy Tarreau2b718102021-04-21 07:32:39 +0200389 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100390 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100391 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100392 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
393 curproxy->conf.args.file, curproxy->conf.args.line,
394 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100395 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100396 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100397 memprintf(err, "format variable '%s' is reserved for HTTP mode",
398 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200399 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100400 }
William Lallemand723b73a2012-02-08 16:37:49 +0100401 }
402 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100403
404 j = var[var_len];
405 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100406 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 +0100407 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200408
409 error_free:
410 if (node) {
411 free(node->arg);
412 free(node);
413 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100414 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100415}
416
417/*
418 * push to the logformat linked list
419 *
420 * start: start pointer
421 * end: end text pointer
422 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100423 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100424 *
425 * LOG_TEXT: copy chars from start to end excluding end.
426 *
427*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100428int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100429{
430 char *str;
431
Willy Tarreaua3571662012-12-20 21:59:12 +0100432 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200433 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100434 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100435 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100436 return 0;
437 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200438 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100439 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100440 str[end - start] = '\0';
441 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100442 node->type = LOG_FMT_TEXT; // type string
Willy Tarreau2b718102021-04-21 07:32:39 +0200443 LIST_APPEND(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100444 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200445 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100446 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100447 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100448 return 0;
449 }
William Lallemand1d705562012-03-12 12:46:41 +0100450 node->type = LOG_FMT_SEPARATOR;
Willy Tarreau2b718102021-04-21 07:32:39 +0200451 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100452 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100453 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100454}
455
456/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100457 * Parse the sample fetch expression <text> and add a node to <list_format> upon
458 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100459 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
460 * is passed in <endptr>, it will be updated with the pointer to the first character
461 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100462 *
463 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100464 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100465int 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, char **endptr)
Willy Tarreauc8368452012-12-21 00:09:23 +0100466{
467 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200468 struct sample_expr *expr = NULL;
469 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100470 int cmd_arg;
471
472 cmd[0] = text;
473 cmd[1] = "";
474 cmd_arg = 0;
475
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100476 expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err, &curpx->conf.args, endptr);
Willy Tarreauc8368452012-12-21 00:09:23 +0100477 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100478 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200479 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100480 }
481
Vincent Bernat02779b62016-04-03 13:48:43 +0200482 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100483 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100484 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200485 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100486 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100487 node->type = LOG_FMT_EXPR;
488 node->expr = expr;
489 node->options = options;
490
491 if (arg_len) {
492 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100493 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200494 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100495 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100496 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100497 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
498
Willy Tarreau434c57c2013-01-08 01:10:24 +0100499 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100500 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
501
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100502 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100503 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
504 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200505 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100506 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100507
Christopher Faulet581db2b2021-03-26 10:02:46 +0100508 if ((options & LOG_OPT_HTTP) && (expr->fetch->use & (SMP_USE_L6REQ|SMP_USE_L6RES))) {
509 ha_warning("parsing [%s:%d] : L6 sample fetch <%s> ignored in HTTP log-format string.\n",
510 curpx->conf.args.file, curpx->conf.args.line, text);
511 }
512
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200513 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100514 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100515 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100516
William Lallemand65ad6e12014-01-31 15:08:02 +0100517 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
518 * needed with some sample fetches (eg: ssl*). We always set it for
519 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100520 */
521 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200522 if (curpx->http_needed)
523 curpx->to_log |= LW_REQ;
Willy Tarreau2b718102021-04-21 07:32:39 +0200524 LIST_APPEND(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100525 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200526
527 error_free:
528 release_sample_expr(expr);
529 if (node) {
530 free(node->arg);
531 free(node);
532 }
533 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100534}
535
536/*
William Lallemand723b73a2012-02-08 16:37:49 +0100537 * Parse the log_format string and fill a linked list.
538 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200539 * You can set arguments using { } : %{many arguments}varname.
540 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100541 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500542 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100543 * curproxy: the proxy affected
544 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100545 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100546 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100547 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100548 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100549 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100550int 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 +0100551{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100552 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100553 char *arg = NULL; /* start pointer for args */
554 char *var = NULL; /* start pointer for vars */
555 int arg_len = 0;
556 int var_len = 0;
557 int cformat; /* current token format */
558 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100559 struct logformat_node *tmplf, *back;
560
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100561 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100562 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100563 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100564 return 0;
565 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200566 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200567
William Lallemand723b73a2012-02-08 16:37:49 +0100568 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100569 list_for_each_entry_safe(tmplf, back, list_format, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200570 LIST_DELETE(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200571 release_sample_expr(tmplf->expr);
572 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100573 free(tmplf);
574 }
575
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100576 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100577 pformat = cformat;
578
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100579 if (!*str)
580 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100581
Joseph Herlant85b40592018-11-15 12:10:04 -0800582 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100583 * second have all common paths processed at one place. The common paths are the ones
584 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
585 * We use the common LF_INIT state to dispatch to the different final states.
586 */
587 switch (pformat) {
588 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100589 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100590 arg_len = var_len = 0;
591 if (*str == '{') { // optional argument
592 cformat = LF_STARG;
593 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100594 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100595 else if (*str == '[') {
596 cformat = LF_STEXPR;
597 var = str + 1; // store expr in variable name
598 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100599 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100600 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100601 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100602 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100603 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500604 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100605 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100606 /* single '%' followed by blank or digit, send them both */
607 cformat = LF_TEXT;
608 pformat = LF_TEXT; /* finally we include the previous char as well */
609 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600610 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 +0100611 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100612 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100613
614 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100615 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500616 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100617 break;
618
619 case LF_STARG: // text immediately following '%{'
620 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100621 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100622 arg_len = str - arg;
623 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100624 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100625 break;
626
627 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100628 if (*str == '[') {
629 cformat = LF_STEXPR;
630 var = str + 1; // store expr in variable name
631 break;
632 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100633 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100634 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100635 var = str;
636 break;
637 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100638 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100639 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100640
Willy Tarreauc8368452012-12-21 00:09:23 +0100641 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100642 /* the whole sample expression is parsed at once,
643 * returning the pointer to the first character not
644 * part of the expression, which MUST be the trailing
645 * angle bracket.
646 */
647 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
648 goto fail;
649
650 if (*str == ']') {
651 // end of arg, go on with next state
652 cformat = pformat = LF_EDEXPR;
653 sp = str;
654 }
655 else {
656 char c = *str;
657 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100658 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100659 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
660 else
661 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200662 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100663 }
664 break;
665
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100666 case LF_VAR: // text part of a variable name
667 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100668 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100669 cformat = LF_INIT; // not variable name anymore
670 break;
671
Willy Tarreauc8368452012-12-21 00:09:23 +0100672 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100673 cformat = LF_INIT;
674 }
675
676 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
677 switch (*str) {
678 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100679 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200680 case ' ':
681 if (options & LOG_OPT_MERGE_SPACES) {
682 cformat = LF_SEPARATOR;
683 break;
684 }
685 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100686 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100687 }
688 }
689
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100690 if (cformat != pformat || pformat == LF_SEPARATOR) {
691 switch (pformat) {
692 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100693 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100694 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100695 break;
696 case LF_TEXT:
697 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100698 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100699 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100700 break;
701 }
702 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100703 }
704 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100705
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100706 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100707 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100708 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100709 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100710 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100711
712 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100713 fail:
714 free(backfmt);
715 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100716}
717
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200718/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500719 * Parse the first range of indexes from a string made of a list of comma separated
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200720 * ranges of indexes. Note that an index may be considered as a particular range
721 * with a high limit to the low limit.
722 */
723int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
724{
725 char *end, *p;
726
727 *low = *high = 0;
728
729 p = *arg;
730 end = strchr(p, ',');
731 if (!end)
732 end = p + strlen(p);
733
734 *high = *low = read_uint((const char **)&p, end);
735 if (!*low || (p != end && *p != '-'))
736 goto err;
737
738 if (p == end)
739 goto done;
740
741 p++;
742 *high = read_uint((const char **)&p, end);
743 if (!*high || *high <= *low || p != end)
744 goto err;
745
746 done:
747 if (*end == ',')
748 end++;
749 *arg = end;
750 return 1;
751
752 err:
753 memprintf(err, "wrong sample range '%s'", *arg);
754 return 0;
755}
756
757/*
758 * Returns 1 if the range defined by <low> and <high> overlaps
759 * one of them in <rgs> array of ranges with <sz> the size of this
760 * array, 0 if not.
761 */
762int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
763 unsigned int low, unsigned int high, char **err)
764{
765 size_t i;
766
767 for (i = 0; i < sz; i++) {
768 if ((low >= rgs[i].low && low <= rgs[i].high) ||
769 (high >= rgs[i].low && high <= rgs[i].high)) {
770 memprintf(err, "ranges are overlapping");
771 return 1;
772 }
773 }
774
775 return 0;
776}
777
778int smp_log_range_cmp(const void *a, const void *b)
779{
780 const struct smp_log_range *rg_a = a;
781 const struct smp_log_range *rg_b = b;
782
783 if (rg_a->high < rg_b->low)
784 return -1;
785 else if (rg_a->low > rg_b->high)
786 return 1;
787
788 return 0;
789}
790
791/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200792 * Parse "log" keyword and update <logsrvs> list accordingly.
793 *
794 * When <do_del> is set, it means the "no log" line was parsed, so all log
795 * servers in <logsrvs> are released.
796 *
797 * Otherwise, we try to parse the "log" line. First of all, when the list is not
798 * the global one, we look for the parameter "global". If we find it,
799 * global.logsrvs is copied. Else we parse each arguments.
800 *
801 * The function returns 1 in success case, otherwise, it returns 0 and err is
802 * filled.
803 */
Emeric Brun9533a702021-04-02 10:13:43 +0200804int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200805{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100806 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200807 struct sockaddr_storage *sk;
Emeric Brun94aab062021-04-02 10:41:36 +0200808 struct protocol *proto;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200809 struct logsrv *logsrv = NULL;
810 int port1, port2;
811 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200812 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200813
814 /*
815 * "no log": delete previous herited or defined syslog
816 * servers.
817 */
818 if (do_del) {
819 struct logsrv *back;
820
821 if (*(args[1]) != 0) {
822 memprintf(err, "'no log' does not expect arguments");
823 goto error;
824 }
825
826 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200827 LIST_DELETE(&logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200828 free(logsrv);
829 }
830 return 1;
831 }
832
833 /*
834 * "log global": copy global.logrsvs linked list to the end of logsrvs
835 * list. But first, we check (logsrvs != global.logsrvs).
836 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100837 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200838 if (logsrvs == &global.logsrvs) {
839 memprintf(err, "'global' is not supported for a global syslog server");
840 goto error;
841 }
842 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200843 struct logsrv *node;
844
845 list_for_each_entry(node, logsrvs, list) {
846 if (node->ref == logsrv)
847 goto skip_logsrv;
848 }
849
850 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200851 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200852 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200853 LIST_INIT(&node->list);
Willy Tarreau2b718102021-04-21 07:32:39 +0200854 LIST_APPEND(logsrvs, &node->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200855 node->conf.file = strdup(file);
856 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200857
858 skip_logsrv:
859 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200860 }
861 return 1;
862 }
863
864 /*
865 * "log <address> ...: parse a syslog server line
866 */
867 if (*(args[1]) == 0 || *(args[2]) == 0) {
868 memprintf(err, "expects <address> and <facility> %s as arguments",
869 ((logsrvs == &global.logsrvs) ? "" : "or global"));
870 goto error;
871 }
872
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100873 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
874 if (strcmp(args[1], "stdout") == 0)
875 args[1] = "fd@1";
876 else if (strcmp(args[1], "stderr") == 0)
877 args[1] = "fd@2";
878
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200879 logsrv = calloc(1, sizeof(*logsrv));
880 if (!logsrv) {
881 memprintf(err, "out of memory");
882 goto error;
883 }
884
Emeric Brun9533a702021-04-02 10:13:43 +0200885 logsrv->conf.file = strdup(file);
886 logsrv->conf.line = linenum;
887
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200888 /* skip address for now, it will be parsed at the end */
889 cur_arg = 2;
890
891 /* just after the address, a length may be specified */
892 logsrv->maxlen = MAX_SYSLOG_LEN;
893 if (strcmp(args[cur_arg], "len") == 0) {
894 int len = atoi(args[cur_arg+1]);
895 if (len < 80 || len > 65535) {
896 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
897 args[cur_arg+1]);
898 goto error;
899 }
900 logsrv->maxlen = len;
901 cur_arg += 2;
902 }
903 if (logsrv->maxlen > global.max_syslog_len)
904 global.max_syslog_len = logsrv->maxlen;
905
906 /* after the length, a format may be specified */
907 if (strcmp(args[cur_arg], "format") == 0) {
908 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200909 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200910 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
911 goto error;
912 }
913 cur_arg += 2;
914 }
915
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200916 if (strcmp(args[cur_arg], "sample") == 0) {
917 unsigned low, high;
918 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200919 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
920
921 p = args[cur_arg+1];
922 smp_sz_str = strchr(p, ':');
923 if (!smp_sz_str) {
924 memprintf(err, "Missing sample size");
925 goto error;
926 }
927
928 *smp_sz_str++ = '\0';
929
930 end = p + strlen(p);
931
932 while (p != end) {
933 if (!get_logsrv_smp_range(&low, &high, &p, err))
934 goto error;
935
936 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
937 goto error;
938
939 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
940 if (!smp_rgs) {
941 memprintf(err, "out of memory error");
942 goto error;
943 }
944
945 smp_rgs[smp_rgs_sz].low = low;
946 smp_rgs[smp_rgs_sz].high = high;
947 smp_rgs[smp_rgs_sz].sz = high - low + 1;
948 smp_rgs[smp_rgs_sz].curr_idx = 0;
949 if (smp_rgs[smp_rgs_sz].high > smp_sz)
950 smp_sz = smp_rgs[smp_rgs_sz].high;
951 smp_rgs_sz++;
952 }
953
Tim Duesterhus21648002019-06-23 22:10:10 +0200954 if (smp_rgs == NULL) {
955 memprintf(err, "no sampling ranges given");
956 goto error;
957 }
958
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200959 beg = smp_sz_str;
960 end = beg + strlen(beg);
961 new_smp_sz = read_uint((const char **)&beg, end);
962 if (!new_smp_sz || beg != end) {
963 memprintf(err, "wrong sample size '%s' for sample range '%s'",
964 smp_sz_str, args[cur_arg+1]);
965 goto error;
966 }
967
968 if (new_smp_sz < smp_sz) {
969 memprintf(err, "sample size %zu should be greater or equal to "
970 "%zu the maximum of the high ranges limits",
971 new_smp_sz, smp_sz);
972 goto error;
973 }
974 smp_sz = new_smp_sz;
975
976 /* Let's order <smp_rgs> array. */
977 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
978
979 logsrv->lb.smp_rgs = smp_rgs;
980 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
981 logsrv->lb.smp_sz = smp_sz;
982
983 cur_arg += 2;
984 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200985 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200986 /* parse the facility */
987 logsrv->facility = get_log_facility(args[cur_arg]);
988 if (logsrv->facility < 0) {
989 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
990 goto error;
991 }
992 cur_arg++;
993
994 /* parse the max syslog level (default: debug) */
995 logsrv->level = 7;
996 if (*(args[cur_arg])) {
997 logsrv->level = get_log_level(args[cur_arg]);
998 if (logsrv->level < 0) {
999 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
1000 goto error;
1001 }
1002 cur_arg++;
1003 }
1004
1005 /* parse the limit syslog level (default: emerg) */
1006 logsrv->minlvl = 0;
1007 if (*(args[cur_arg])) {
1008 logsrv->minlvl = get_log_level(args[cur_arg]);
1009 if (logsrv->minlvl < 0) {
1010 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1011 goto error;
1012 }
1013 cur_arg++;
1014 }
1015
1016 /* Too many args */
1017 if (*(args[cur_arg])) {
1018 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1019 goto error;
1020 }
1021
1022 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001023 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001024 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001025 logsrv->addr.ss_family = AF_UNSPEC;
1026 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001027 logsrv->sink = NULL;
1028 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001029 goto done;
1030 }
1031
Emeric Brun94aab062021-04-02 10:41:36 +02001032 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
1033 err, NULL, NULL,
1034 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM | PA_O_STREAM | PA_O_DEFAULT_DGRAM);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001035 if (!sk)
1036 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001037
1038 if (fd != -1)
1039 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001040 logsrv->addr = *sk;
1041
1042 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001043 if (!port1)
1044 set_host_port(&logsrv->addr, SYSLOG_PORT);
1045 }
Emeric Brun9533a702021-04-02 10:13:43 +02001046
Emeric Brun26754902021-04-07 14:26:44 +02001047 if (proto && proto->ctrl_type == SOCK_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +02001048 static unsigned long ring_ids;
1049
1050 /* Implicit sink buffer will be
1051 * initialized in post_check
1052 */
1053 logsrv->type = LOG_TARGET_BUFFER;
1054 logsrv->sink = NULL;
1055 /* compute uniq name for the ring */
1056 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
1057 }
1058
Willy Tarreauc046d162019-08-30 15:24:59 +02001059 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001060 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001061 return 1;
1062
1063 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001064 free(smp_rgs);
Emeric Brun9533a702021-04-02 10:13:43 +02001065 if (logsrv) {
1066 free(logsrv->conf.file);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001067 free(logsrv->ring_name);
Emeric Brun9533a702021-04-02 10:13:43 +02001068 }
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001069 free(logsrv);
1070 return 0;
1071}
1072
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001073
1074/*
Emeric Brun54648852020-07-06 15:54:06 +02001075 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001076 */
Emeric Brun54648852020-07-06 15:54:06 +02001077enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001078{
Emeric Brun54648852020-07-06 15:54:06 +02001079 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001080
1081 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001082 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001083 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001084 format--;
1085
Emeric Brun54648852020-07-06 15:54:06 +02001086 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001087 return format;
1088}
1089
1090/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001091 * returns log level for <lev> or -1 if not found.
1092 */
1093int get_log_level(const char *lev)
1094{
1095 int level;
1096
1097 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001098 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001099 level--;
1100
1101 return level;
1102}
1103
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104/*
1105 * returns log facility for <fac> or -1 if not found.
1106 */
1107int get_log_facility(const char *fac)
1108{
1109 int facility;
1110
1111 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001112 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001114
Willy Tarreaubaaee002006-06-26 02:48:02 +02001115 return facility;
1116}
1117
William Lallemanda1cc3812012-02-08 16:38:44 +01001118/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001119 * Encode the string.
1120 *
1121 * When using the +E log format option, it will try to escape '"\]'
1122 * characters with '\' as prefix. The same prefix should not be used as
1123 * <escape>.
1124 */
1125static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001126 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001127 const char *string,
1128 struct logformat_node *node)
1129{
1130 if (node->options & LOG_OPT_ESC) {
1131 if (start < stop) {
1132 stop--; /* reserve one byte for the final '\0' */
1133 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001134 if (!ha_bit_test((unsigned char)(*string), map)) {
1135 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001136 *start++ = *string;
1137 else {
1138 if (start + 2 >= stop)
1139 break;
1140 *start++ = '\\';
1141 *start++ = *string;
1142 }
1143 }
1144 else {
1145 if (start + 3 >= stop)
1146 break;
1147 *start++ = escape;
1148 *start++ = hextab[(*string >> 4) & 15];
1149 *start++ = hextab[*string & 15];
1150 }
1151 string++;
1152 }
1153 *start = '\0';
1154 }
1155 }
1156 else {
1157 return encode_string(start, stop, escape, map, string);
1158 }
1159
1160 return start;
1161}
1162
1163/*
1164 * Encode the chunk.
1165 *
1166 * When using the +E log format option, it will try to escape '"\]'
1167 * characters with '\' as prefix. The same prefix should not be used as
1168 * <escape>.
1169 */
1170static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001171 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001172 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001173 struct logformat_node *node)
1174{
1175 char *str, *end;
1176
1177 if (node->options & LOG_OPT_ESC) {
1178 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001179 str = chunk->area;
1180 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001181
1182 stop--; /* reserve one byte for the final '\0' */
1183 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001184 if (!ha_bit_test((unsigned char)(*str), map)) {
1185 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001186 *start++ = *str;
1187 else {
1188 if (start + 2 >= stop)
1189 break;
1190 *start++ = '\\';
1191 *start++ = *str;
1192 }
1193 }
1194 else {
1195 if (start + 3 >= stop)
1196 break;
1197 *start++ = escape;
1198 *start++ = hextab[(*str >> 4) & 15];
1199 *start++ = hextab[*str & 15];
1200 }
1201 str++;
1202 }
1203 *start = '\0';
1204 }
1205 }
1206 else {
1207 return encode_chunk(start, stop, escape, map, chunk);
1208 }
1209
1210 return start;
1211}
1212
1213/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001214 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001215 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001216 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001217 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001218 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001219char *lf_text_len(char *dst, const char *src, size_t len, size_t size, const struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +01001220{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001221 if (size < 2)
1222 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001223
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001224 if (node->options & LOG_OPT_QUOTE) {
1225 *(dst++) = '"';
1226 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001227 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001228
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001229 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001230 if (++len > size)
1231 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001232 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001233 char *ret;
1234
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001235 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001236 if (ret == NULL || *ret != '\0')
1237 return NULL;
1238 len = ret - dst;
1239 }
1240 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001241 len = strlcpy2(dst, src, len);
1242 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001243
1244 size -= len;
1245 dst += len;
1246 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001247 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1248 if (size < 2)
1249 return NULL;
1250 *(dst++) = '-';
1251 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001252
1253 if (node->options & LOG_OPT_QUOTE) {
1254 if (size < 2)
1255 return NULL;
1256 *(dst++) = '"';
1257 }
1258
1259 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001260 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001261}
1262
Willy Tarreau26ffa852018-09-05 15:23:10 +02001263static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001264{
1265 return lf_text_len(dst, src, size, size, node);
1266}
1267
William Lallemand5f232402012-04-05 18:02:55 +02001268/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001269 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001270 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001271 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001272char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001273{
1274 char *ret = dst;
1275 int iret;
1276 char pn[INET6_ADDRSTRLEN];
1277
1278 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001279 unsigned char *addr = NULL;
1280 switch (sockaddr->sa_family) {
1281 case AF_INET:
1282 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1283 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1284 break;
1285 case AF_INET6:
1286 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1287 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1288 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1289 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1290 break;
1291 default:
1292 return NULL;
1293 }
William Lallemand5f232402012-04-05 18:02:55 +02001294 if (iret < 0 || iret > size)
1295 return NULL;
1296 ret += iret;
1297 } else {
1298 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1299 ret = lf_text(dst, pn, size, node);
1300 if (ret == NULL)
1301 return NULL;
1302 }
1303 return ret;
1304}
1305
1306/*
1307 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001308 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001309 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001310char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001311{
1312 char *ret = dst;
1313 int iret;
1314
1315 if (node->options & LOG_OPT_HEXA) {
1316 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1317 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1318 if (iret < 0 || iret > size)
1319 return NULL;
1320 ret += iret;
1321 } else {
1322 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1323 if (ret == NULL)
1324 return NULL;
1325 }
1326 return ret;
1327}
1328
Emeric Brun54648852020-07-06 15:54:06 +02001329
1330/*
1331 * This function sends the syslog message using a printf format string. It
1332 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001333 */
Emeric Brun54648852020-07-06 15:54:06 +02001334void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001335{
Emeric Brun54648852020-07-06 15:54:06 +02001336 va_list argp;
1337 int data_len;
1338
1339 if (level < 0 || format == NULL || logline == NULL)
1340 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001341
Emeric Brun54648852020-07-06 15:54:06 +02001342 va_start(argp, format);
1343 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1344 if (data_len < 0 || data_len > global.max_syslog_len)
1345 data_len = global.max_syslog_len;
1346 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001347
Emeric Brun54648852020-07-06 15:54:06 +02001348 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1349 logline, data_len, default_rfc5424_sd_log_format, 2);
1350}
1351/*
1352 * This function builds a log header of given format using given
1353 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1354 * to determine format based on given metadas. It is useful
1355 * for log-forwarding to be able to forward any format without
1356 * settings.
1357 * This function returns a struct ist array of elements of the header
1358 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001359 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001360 * elements.
1361 */
1362struct ist *build_log_header(enum log_fmt format, int level, int facility,
1363 struct ist *metadata, size_t *nbelem)
1364{
1365 static THREAD_LOCAL struct {
1366 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1367 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1368 time_t cur_legacy_time;
1369 char priority_buffer[6];
1370 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001371
Emeric Brun54648852020-07-06 15:54:06 +02001372 struct tm logtime;
1373 int len;
1374 int fac_level = 0;
1375 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001376
Emeric Brun54648852020-07-06 15:54:06 +02001377 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001378
Emeric Brun54648852020-07-06 15:54:06 +02001379
1380 if (format == LOG_FORMAT_UNSPEC) {
1381 format = LOG_FORMAT_RAW;
1382 if (metadata) {
1383 /* If a hostname is set, it appears we want to perform syslog
1384 * because only rfc5427 or rfc3164 support an hostname.
1385 */
1386 if (metadata[LOG_META_HOST].len) {
1387 /* If a rfc5424 compliant timestamp is used we consider
1388 * that output format is rfc5424, else legacy format
1389 * is used as specified default for local logs
1390 * in documentation.
1391 */
1392 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1393 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1394 format = LOG_FORMAT_RFC5424;
1395 else
1396 format = LOG_FORMAT_RFC3164;
1397 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001398 else if (metadata[LOG_META_TAG].len) {
1399 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001400 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001401 * in legacy format (analog to RFC3164 but
1402 * with stripped hostname).
1403 */
1404 format = LOG_FORMAT_LOCAL;
1405 }
Emeric Brun54648852020-07-06 15:54:06 +02001406 else if (metadata[LOG_META_PRIO].len) {
1407 /* the source seems a parsed message
1408 * offering a valid level/prio prefix
1409 * so we consider this format.
1410 */
1411 format = LOG_FORMAT_PRIO;
1412 }
1413 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001414 }
1415
Emeric Brun54648852020-07-06 15:54:06 +02001416 /* prepare priority, stored into 1 single elem */
1417 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001418 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001419 case LOG_FORMAT_RFC3164:
1420 case LOG_FORMAT_RFC5424:
1421 case LOG_FORMAT_PRIO:
1422 fac_level = facility << 3;
1423 /* further format ignore the facility */
1424 /* fall through */
1425 case LOG_FORMAT_TIMED:
1426 case LOG_FORMAT_SHORT:
1427 fac_level += level;
1428 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1429 do {
1430 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1431 fac_level /= 10;
1432 hdr_ctx.ist_vector[*nbelem].ptr--;
1433 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1434 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1435 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1436 break;
1437 case LOG_FORMAT_ISO:
1438 case LOG_FORMAT_RAW:
1439 break;
1440 case LOG_FORMAT_UNSPEC:
1441 case LOG_FORMATS:
1442 ABORT_NOW();
1443 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001444
William Lallemand2a4a44f2012-02-06 16:00:33 +01001445
Emeric Brun54648852020-07-06 15:54:06 +02001446 /* prepare timestamp, stored into a max of 4 elems */
1447 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001448 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001449 case LOG_FORMAT_RFC3164:
1450 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1451 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1452 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1453 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001454 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001455 break;
1456 }
1457 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1458 int month;
1459 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001460
Emeric Brun54648852020-07-06 15:54:06 +02001461 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001462
Emeric Brun54648852020-07-06 15:54:06 +02001463 /* compute month */
1464 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1465 if (month)
1466 month--;
1467 if (month <= 11) {
1468 /* builds log prefix ex: 'Jan 1 ' */
1469 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1470 "%s %c%c ", monthname[month],
1471 timestamp[8] != '0' ? timestamp[8] : ' ',
1472 timestamp[9]);
1473 /* we reused the timestamp_buffer, signal that it does not
1474 * contain local time anymore
1475 */
1476 hdr_ctx.cur_legacy_time = 0;
1477 if (len == 7) {
1478 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1479 /* adds 'HH:MM:SS' from iso time */
1480 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1481 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1482 /* we successfully reuse iso time, we can break */
1483 break;
1484 }
1485 }
1486 /* Failed to reuse isotime time, fallback to local legacy time */
1487 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001488
Emeric Brun54648852020-07-06 15:54:06 +02001489 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1490 /* re-builds timestamp from the current local time */
1491 get_localtime(time, &logtime);
1492
1493 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1494 "%s %2d %02d:%02d:%02d ",
1495 monthname[logtime.tm_mon],
1496 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1497 if (len != LOG_LEGACYTIME_LEN+1)
1498 hdr_ctx.cur_legacy_time = 0;
1499 else
1500 hdr_ctx.cur_legacy_time = time;
1501 }
1502 if (likely(hdr_ctx.cur_legacy_time))
1503 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1504 else
1505 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1506 break;
1507 case LOG_FORMAT_RFC5424:
1508 /* adds rfc5425 version prefix */
1509 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1510 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001511 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001512 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1513 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1514 break;
1515 }
1516 /* let continue as 'timed' and 'iso' format for usual timestamp */
1517 /* fall through */
1518 case LOG_FORMAT_TIMED:
1519 case LOG_FORMAT_ISO:
1520 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1521 * '1900:01:01T14:00:00+02:00'
1522 * '1900:01:01T10:00:00.123456-02:00'
1523 */
1524 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1525 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1526 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001527 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001528 break;
1529 }
1530 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1531 int month;
1532 char *timestamp = metadata[LOG_META_TIME].ptr;
1533
1534 for (month = 0; month < 12; month++)
1535 if (!memcmp(monthname[month], timestamp, 3))
1536 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001537
Emeric Brun54648852020-07-06 15:54:06 +02001538 if (month < 12) {
1539
1540 /* get local time to retrieve year */
1541 get_localtime(time, &logtime);
1542
1543 /* year seems changed since log */
1544 if (logtime.tm_mon < month)
1545 logtime.tm_year--;
1546
1547 /* builds rfc5424 prefix ex: '1900-01-01T' */
1548 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1549 "%4d-%02d-%c%cT",
1550 logtime.tm_year+1900, month+1,
1551 timestamp[4] != ' ' ? timestamp[4] : '0',
1552 timestamp[5]);
1553
1554 /* we reused the timestamp_buffer, signal that it does not
1555 * contain local time anymore
1556 */
1557 hdr_ctx.cur_legacy_time = 0;
1558 if (len == 11) {
1559 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1560 /* adds HH:MM:SS from legacy timestamp */
1561 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001562 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001563 /* according to rfc: -00:00 means we don't know the timezone */
1564 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1565 /* we successfully reuse legacy time, we can break */
1566 break;
1567 }
1568 }
1569 /* Failed to reuse legacy time, fallback to local iso time */
1570 }
1571 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1572 break;
1573 case LOG_FORMAT_PRIO:
1574 case LOG_FORMAT_SHORT:
1575 case LOG_FORMAT_RAW:
1576 break;
1577 case LOG_FORMAT_UNSPEC:
1578 case LOG_FORMATS:
1579 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001580 }
1581
Emeric Brun54648852020-07-06 15:54:06 +02001582 /* prepare other meta data, stored into a max of 10 elems */
1583 switch (format) {
1584 case LOG_FORMAT_RFC3164:
1585 if (metadata && metadata[LOG_META_HOST].len) {
1586 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1587 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1588 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001589 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001590 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001591 /* fall through */
1592 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001593 if (!metadata || !metadata[LOG_META_TAG].len)
1594 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001595
Emeric Brun54648852020-07-06 15:54:06 +02001596 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1597 if (metadata[LOG_META_PID].len) {
1598 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1599 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1600 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1601 }
1602 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1603 break;
1604 case LOG_FORMAT_RFC5424:
1605 if (metadata && metadata[LOG_META_HOST].len) {
1606 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1607 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1608 }
1609 else
1610 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001611
Emeric Brun54648852020-07-06 15:54:06 +02001612 if (metadata && metadata[LOG_META_TAG].len) {
1613 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1614 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1615 }
1616 else
1617 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001618
Emeric Brun54648852020-07-06 15:54:06 +02001619 if (metadata && metadata[LOG_META_PID].len) {
1620 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1621 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1622 }
1623 else
1624 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001625
Emeric Brun54648852020-07-06 15:54:06 +02001626 if (metadata && metadata[LOG_META_MSGID].len) {
1627 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1628 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1629 }
1630 else
1631 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001632
Emeric Brun54648852020-07-06 15:54:06 +02001633 if (metadata && metadata[LOG_META_STDATA].len) {
1634 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1635 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1636 }
1637 else
1638 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1639 break;
1640 case LOG_FORMAT_PRIO:
1641 case LOG_FORMAT_SHORT:
1642 case LOG_FORMAT_TIMED:
1643 case LOG_FORMAT_ISO:
1644 case LOG_FORMAT_RAW:
1645 break;
1646 case LOG_FORMAT_UNSPEC:
1647 case LOG_FORMATS:
1648 ABORT_NOW();
1649 }
1650
1651 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001652}
1653
1654/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001655 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001656 * The argument <metadata> MUST be an array of size
1657 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001658 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001659 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001660 */
Emeric Brun54648852020-07-06 15:54:06 +02001661static inline void __do_send_log(struct logsrv *logsrv, int nblogger, int level, int facility, struct ist *metadata, char *message, size_t size)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001662{
Emeric Brun54648852020-07-06 15:54:06 +02001663 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001664 static THREAD_LOCAL struct msghdr msghdr = {
1665 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001666 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001667 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001668 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1669 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001670 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001671 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001672 size_t nbelem;
1673 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001674
1675 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001676
Emeric Brunfa9d7802020-05-28 14:21:33 +02001677 /* historically some messages used to already contain the trailing LF
1678 * or Zero. Let's remove all trailing LF or Zero
1679 */
Emeric Brun54648852020-07-06 15:54:06 +02001680 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001681 size--;
1682
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001683 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001684 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001685 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001686 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001687 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1688 /* the socket's address is a file descriptor */
1689 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1690 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001691 else if (logsrv->addr.ss_family == AF_UNIX)
1692 plogfd = &logfdunix;
1693 else
1694 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001695
Willy Tarreauc046d162019-08-30 15:24:59 +02001696 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001697 /* socket not successfully initialized yet */
1698 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1699 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1700 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001701
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001702 if (!once) {
1703 once = 1; /* note: no need for atomic ops here */
1704 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1705 nblogger, strerror(errno), errno);
1706 }
1707 return;
1708 } else {
1709 /* we don't want to receive anything on this socket */
1710 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1711 /* does nothing under Linux, maybe needed for others */
1712 shutdown(*plogfd, SHUT_RD);
1713 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1714 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001715 }
1716
Emeric Brun54648852020-07-06 15:54:06 +02001717 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1718 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001719 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001720 struct ist msg;
1721
1722 msg = ist2(message, size);
1723 if (msg.len > logsrv->maxlen)
1724 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001725
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001726 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1727 }
1728 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1729 struct ist msg;
1730
1731 msg = ist2(message, size);
1732 if (msg.len > logsrv->maxlen)
1733 msg.len = logsrv->maxlen;
1734
1735 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001736 }
1737 else {
Emeric Brun54648852020-07-06 15:54:06 +02001738 int i = 0;
1739 int totlen = logsrv->maxlen;
1740
1741 for (i = 0 ; i < nbelem ; i++ ) {
1742 iovec[i].iov_base = msg_header[i].ptr;
1743 iovec[i].iov_len = msg_header[i].len;
1744 if (totlen <= iovec[i].iov_len) {
1745 iovec[i].iov_len = totlen;
1746 totlen = 0;
1747 break;
1748 }
1749 totlen -= iovec[i].iov_len;
1750 }
1751 if (totlen) {
1752 iovec[i].iov_base = message;
1753 iovec[i].iov_len = size;
1754 if (totlen <= iovec[i].iov_len)
1755 iovec[i].iov_len = totlen;
1756 i++;
1757 }
1758 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1759 iovec[i].iov_len = 1;
1760 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001761
Emeric Brun54648852020-07-06 15:54:06 +02001762 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001763 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1764 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001765
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001766 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1767 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001768
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001769 if (sent < 0) {
1770 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001771
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001772 if (errno == EAGAIN)
Willy Tarreau4781b152021-04-06 13:53:36 +02001773 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001774 else if (!once) {
1775 once = 1; /* note: no need for atomic ops here */
1776 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1777 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001778 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001779 }
1780}
Dragan Dosen59cee972015-09-19 22:09:02 +02001781
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001782/*
1783 * This function sends a syslog message.
1784 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001785 * The argument <metadata> MUST be an array of size
1786 * LOG_META_FIELDS*sizeof(struct ist) containing
1787 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001788 */
Emeric Brun54648852020-07-06 15:54:06 +02001789void process_send_log(struct list *logsrvs, int level, int facility,
1790 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001791{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001792 struct logsrv *logsrv;
1793 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001794
1795 /* Send log messages to syslog server. */
1796 nblogger = 0;
1797 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001798 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001799
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001800 /* we can filter the level of the messages that are sent to each logger */
1801 if (level > logsrv->level)
1802 continue;
1803
Frédéric Lécailled803e472019-04-25 07:42:09 +02001804 if (logsrv->lb.smp_rgs) {
1805 struct smp_log_range *curr_rg;
1806
1807 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1808 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1809 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1810 if (in_range) {
1811 /* Let's consume this range. */
1812 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1813 if (!curr_rg->curr_idx) {
1814 /* If consumed, let's select the next range. */
1815 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1816 }
1817 }
1818 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1819 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1820 }
1821 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001822 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1823 (facility == -1) ? logsrv->facility : facility,
1824 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001825 }
1826}
1827
Emeric Brun54648852020-07-06 15:54:06 +02001828/*
1829 * This function sends a syslog message.
1830 * It doesn't care about errors nor does it report them.
1831 * The arguments <sd> and <sd_size> are used for the structured-data part
1832 * in RFC5424 formatted syslog messages.
1833 */
1834void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1835 char *message, size_t size, char *sd, size_t sd_size)
1836{
1837 static THREAD_LOCAL pid_t curr_pid;
1838 static THREAD_LOCAL char pidstr[16];
1839 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1840
1841 if (logsrvs == NULL) {
1842 if (!LIST_ISEMPTY(&global.logsrvs)) {
1843 logsrvs = &global.logsrvs;
1844 }
1845 }
1846 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1847 return;
1848
1849 if (!metadata[LOG_META_HOST].len) {
1850 if (global.log_send_hostname)
1851 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02001852 }
1853
1854 if (!tagb || !tagb->area)
1855 tagb = &global.log_tag;
1856
1857 if (tagb)
1858 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1859
1860 if (unlikely(curr_pid != getpid()))
1861 metadata[LOG_META_PID].len = 0;
1862
1863 if (!metadata[LOG_META_PID].len) {
1864 curr_pid = getpid();
1865 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1866 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1867 }
1868
1869 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1870
1871 /* Remove trailing space of structured data */
1872 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1873 metadata[LOG_META_STDATA].len--;
1874
1875 return process_send_log(logsrvs, level, -1, metadata, message, size);
1876}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001877
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001878const 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 +01001879const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1880 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1881 Set-cookie Updated, unknown, unknown */
1882
William Lallemand1d705562012-03-12 12:46:41 +01001883/*
1884 * try to write a character if there is enough space, or goto out
1885 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001886#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001887 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001888 *(tmplog++) = (x); \
1889 } else { \
1890 goto out; \
1891 } \
1892 } while(0)
1893
Dragan Dosen835b9212016-02-12 13:23:03 +01001894
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001895/* Initializes some log data at boot */
1896static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001897{
1898 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001899 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001900
1901 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1902 * inside PARAM-VALUE should be escaped with '\' as prefix.
1903 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1904 * details.
1905 */
1906 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1907
1908 tmp = "\"\\]";
1909 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001910 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001911 tmp++;
1912 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001913
1914 /* initialize the log header encoding map : '{|}"#' should be encoded with
1915 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1916 * URL encoding only requires '"', '#' to be encoded as well as non-
1917 * printable characters above.
1918 */
1919 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1920 memset(url_encode_map, 0, sizeof(url_encode_map));
1921 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001922 ha_bit_set(i, hdr_encode_map);
1923 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001924 }
1925 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001926 ha_bit_set(i, hdr_encode_map);
1927 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001928 }
1929
1930 tmp = "\"#{|}";
1931 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001932 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001933 tmp++;
1934 }
1935
1936 tmp = "\"#";
1937 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001938 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001939 tmp++;
1940 }
1941
1942 /* initialize the http header encoding map. The draft httpbis define the
1943 * header content as:
1944 *
1945 * HTTP-message = start-line
1946 * *( header-field CRLF )
1947 * CRLF
1948 * [ message-body ]
1949 * header-field = field-name ":" OWS field-value OWS
1950 * field-value = *( field-content / obs-fold )
1951 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1952 * obs-fold = CRLF 1*( SP / HTAB )
1953 * field-vchar = VCHAR / obs-text
1954 * VCHAR = %x21-7E
1955 * obs-text = %x80-FF
1956 *
1957 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1958 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08001959 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02001960 */
1961 memset(http_encode_map, 0, sizeof(http_encode_map));
1962 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001963 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001964 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001965 ha_bit_set(i, http_encode_map);
1966 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001967}
William Lallemand1d705562012-03-12 12:46:41 +01001968
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001969INITCALL0(STG_PREPARE, init_log);
1970
Christopher Faulet0132d062017-07-26 15:33:35 +02001971/* Initialize log buffers used for syslog messages */
1972int init_log_buffers()
1973{
Christopher Faulet0132d062017-07-26 15:33:35 +02001974 logline = my_realloc2(logline, global.max_syslog_len + 1);
1975 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02001976 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02001977 return 0;
1978 return 1;
1979}
1980
1981/* Deinitialize log buffers used for syslog messages */
1982void deinit_log_buffers()
1983{
Christopher Faulet0132d062017-07-26 15:33:35 +02001984 free(logline);
1985 free(logline_rfc5424);
Christopher Faulet0132d062017-07-26 15:33:35 +02001986 logline = NULL;
1987 logline_rfc5424 = NULL;
1988}
1989
Willy Tarreaudf974472012-12-28 02:44:01 +01001990/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1991 * <maxsize> characters. Returns the size of the output string in characters,
1992 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001993 * is not zero. It requires a valid session and optionally a stream. If the
1994 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01001995 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001996int 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 +02001997{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001998 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001999 struct proxy *be;
2000 struct http_txn *txn;
2001 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002002 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002003 unsigned int s_flags;
2004 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002005 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002006 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002007 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002008 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002009 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002010 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002011 int t_request;
2012 int hdr;
2013 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002014 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002015 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002016 char *ret;
2017 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002018 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002019 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002020 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002021 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002022 struct ist path;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002023
William Lallemandbddd4fd2012-02-27 11:23:10 +01002024 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002025
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002026 if (likely(s)) {
2027 be = s->be;
2028 txn = s->txn;
2029 be_conn = cs_conn(objt_cs(s->si[1].end));
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002030 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002031 s_flags = s->flags;
2032 uniq_id = s->uniq_id;
2033 logs = &s->logs;
2034 } else {
2035 /* we have no stream so we first need to initialize a few
2036 * things that are needed later. We do increment the request
2037 * ID so that it's uniquely assigned to this request just as
2038 * if the request had reached the point of being processed.
2039 * A request error is reported as it's the only element we have
2040 * here and which justifies emitting such a log.
2041 */
Christopher Fauletfd818482021-04-14 14:01:41 +02002042 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002043 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002044 fe_conn = objt_conn(sess->origin);
Christopher Fauletfd818482021-04-14 14:01:41 +02002045 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? cs_conn(__objt_check(sess->origin)->cs) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002046 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002047 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02002048 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002049
2050 /* prepare a valid log structure */
2051 tmp_strm_log.tv_accept = sess->tv_accept;
2052 tmp_strm_log.accept_date = sess->accept_date;
2053 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02002054 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002055 tv_zero(&tmp_strm_log.tv_request);
2056 tmp_strm_log.t_queue = -1;
2057 tmp_strm_log.t_connect = -1;
2058 tmp_strm_log.t_data = -1;
2059 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2060 tmp_strm_log.bytes_in = 0;
2061 tmp_strm_log.bytes_out = 0;
2062 tmp_strm_log.prx_queue_pos = 0;
2063 tmp_strm_log.srv_queue_pos = 0;
2064
2065 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002066
2067 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
2068 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, NULL);
2069
2070 switch (es) {
2071 case MUX_ES_SUCCESS:
2072 break;
2073 case MUX_ES_INVALID_ERR:
2074 status = 400;
2075 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2076 s_flags = SF_ERR_CLICL | SF_FINST_R;
2077 else
2078 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2079 break;
2080 case MUX_ES_TOUT_ERR:
2081 status = 408;
2082 s_flags = SF_ERR_CLITO | SF_FINST_R;
2083 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002084 case MUX_ES_NOTIMPL_ERR:
2085 status = 501;
2086 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2087 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002088 case MUX_ES_INTERNAL_ERR:
2089 status = 500;
2090 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2091 break;
2092 default:
2093 break;
2094 }
2095 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002096 }
2097
William Lallemandbddd4fd2012-02-27 11:23:10 +01002098 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002099 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2100 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002101
William Lallemand1d705562012-03-12 12:46:41 +01002102 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002103
William Lallemandbddd4fd2012-02-27 11:23:10 +01002104 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002105 if (LIST_ISEMPTY(list_format))
2106 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002107
William Lallemand1d705562012-03-12 12:46:41 +01002108 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002109 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002110 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002111 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002112 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002113
Willy Tarreauc8368452012-12-21 00:09:23 +01002114 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002115 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002116 if (!last_isspace) {
2117 LOGCHAR(' ');
2118 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002119 }
2120 break;
2121
William Lallemand1d705562012-03-12 12:46:41 +01002122 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002123 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002124 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002125 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002126 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002127 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002128 last_isspace = 0;
2129 break;
2130
Willy Tarreauc8368452012-12-21 00:09:23 +01002131 case LOG_FMT_EXPR: // sample expression, may be request or response
2132 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002133 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002134 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Christopher Faulet5f940702020-04-06 10:40:02 +02002135 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002136 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 +01002137 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002138 ret = lf_encode_chunk(tmplog, dst + maxsize,
2139 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002140 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002141 ret = lf_text_len(tmplog,
2142 key ? key->data.u.str.area : NULL,
2143 key ? key->data.u.str.data : 0,
2144 dst + maxsize - tmplog,
2145 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002146 if (ret == 0)
2147 goto out;
2148 tmplog = ret;
2149 last_isspace = 0;
2150 break;
2151
Willy Tarreau2beef582012-12-20 17:22:52 +01002152 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002153 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002154 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002155 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002156 else
2157 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002158 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002159 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002160 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002161 last_isspace = 0;
2162 break;
2163
Willy Tarreau2beef582012-12-20 17:22:52 +01002164 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002165 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002166 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002167 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002168 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002169 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002170 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002171 dst + maxsize - tmplog, tmp);
2172 }
William Lallemand5f232402012-04-05 18:02:55 +02002173 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002174 else
2175 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2176
William Lallemand5f232402012-04-05 18:02:55 +02002177 if (ret == NULL)
2178 goto out;
2179 tmplog = ret;
2180 last_isspace = 0;
2181 break;
2182
Willy Tarreau2beef582012-12-20 17:22:52 +01002183 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002184 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002185 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002186 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002187 }
2188 else
2189 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2190
William Lallemand1d705562012-03-12 12:46:41 +01002191 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002192 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002193 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002194 last_isspace = 0;
2195 break;
2196
Willy Tarreau2beef582012-12-20 17:22:52 +01002197 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002198 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002199 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002200 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002201 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002202 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002203 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002204 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002205 else
2206 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2207
William Lallemand5f232402012-04-05 18:02:55 +02002208 if (ret == NULL)
2209 goto out;
2210 tmplog = ret;
2211 last_isspace = 0;
2212 break;
2213
Willy Tarreau2beef582012-12-20 17:22:52 +01002214 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002215 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002216 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002217 else
2218 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2219
William Lallemand1d705562012-03-12 12:46:41 +01002220 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002221 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002222 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002223 last_isspace = 0;
2224 break;
2225
Willy Tarreau2beef582012-12-20 17:22:52 +01002226 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002227 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002228 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002229 else
2230 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2231
William Lallemand5f232402012-04-05 18:02:55 +02002232 if (ret == NULL)
2233 goto out;
2234 tmplog = ret;
2235 last_isspace = 0;
2236 break;
2237
Willy Tarreau2beef582012-12-20 17:22:52 +01002238 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002239 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002240 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002241 else
2242 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2243
William Lallemand5f232402012-04-05 18:02:55 +02002244 if (ret == NULL)
2245 goto out;
2246 tmplog = ret;
2247 last_isspace = 0;
2248 break;
2249
Willy Tarreau2beef582012-12-20 17:22:52 +01002250 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002251 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002252 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002253 else
2254 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2255
William Lallemand1d705562012-03-12 12:46:41 +01002256 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002257 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002258 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002259 last_isspace = 0;
2260 break;
2261
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002262 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002263 get_localtime(logs->accept_date.tv_sec, &tm);
2264 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002265 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002266 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002267 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002268 last_isspace = 0;
2269 break;
2270
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002271 case LOG_FMT_tr: // %tr = start of request date
2272 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002273 tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002274 get_localtime(tv.tv_sec, &tm);
2275 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2276 if (ret == NULL)
2277 goto out;
2278 tmplog = ret;
2279 last_isspace = 0;
2280 break;
2281
2282 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002283 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002284 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002285 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002286 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002287 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002288 last_isspace = 0;
2289 break;
2290
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002291 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002292 tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002293 get_gmtime(tv.tv_sec, &tm);
2294 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2295 if (ret == NULL)
2296 goto out;
2297 tmplog = ret;
2298 last_isspace = 0;
2299 break;
2300
2301 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002302 get_localtime(logs->accept_date.tv_sec, &tm);
2303 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002304 if (ret == NULL)
2305 goto out;
2306 tmplog = ret;
2307 last_isspace = 0;
2308 break;
2309
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002310 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002311 tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002312 get_localtime(tv.tv_sec, &tm);
2313 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2314 if (ret == NULL)
2315 goto out;
2316 tmplog = ret;
2317 last_isspace = 0;
2318 break;
2319
William Lallemand5f232402012-04-05 18:02:55 +02002320 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002321 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002322 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002323 if (iret < 0 || iret > dst + maxsize - tmplog)
2324 goto out;
2325 last_isspace = 0;
2326 tmplog += iret;
2327 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002328 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002329 if (ret == NULL)
2330 goto out;
2331 tmplog = ret;
2332 last_isspace = 0;
2333 }
2334 break;
2335
William Lallemand1d705562012-03-12 12:46:41 +01002336 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002337 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002338 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002339 if (iret < 0 || iret > dst + maxsize - tmplog)
2340 goto out;
2341 last_isspace = 0;
2342 tmplog += iret;
2343 } else {
2344 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002345 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002346 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002347 tmplog, 4);
2348 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002349 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002350 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002351 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002352 }
2353 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002354
William Lallemand1d705562012-03-12 12:46:41 +01002355 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002356 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002357 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002358 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002359 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002360 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002361 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002362 break;
2363
Willy Tarreau773d65f2012-10-12 14:56:11 +02002364 case LOG_FMT_FRONTEND_XPRT: // %ft
2365 src = fe->id;
2366 if (tmp->options & LOG_OPT_QUOTE)
2367 LOGCHAR('"');
2368 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2369 if (iret == 0)
2370 goto out;
2371 tmplog += iret;
Christopher Fauletfd818482021-04-14 14:01:41 +02002372 if (sess->listener && sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002373 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002374 if (tmp->options & LOG_OPT_QUOTE)
2375 LOGCHAR('"');
2376 last_isspace = 0;
2377 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002378#ifdef USE_OPENSSL
2379 case LOG_FMT_SSL_CIPHER: // %sslc
2380 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002381 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002382 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002383 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002384 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002385 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2386 if (ret == NULL)
2387 goto out;
2388 tmplog = ret;
2389 last_isspace = 0;
2390 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002391
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002392 case LOG_FMT_SSL_VERSION: // %sslv
2393 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002394 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002395 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002396 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002397 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002398 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2399 if (ret == NULL)
2400 goto out;
2401 tmplog = ret;
2402 last_isspace = 0;
2403 break;
2404#endif
William Lallemand1d705562012-03-12 12:46:41 +01002405 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002406 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002407 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002408 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002409 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002410 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002411 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002412 break;
2413
William Lallemand1d705562012-03-12 12:46:41 +01002414 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002415 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002416 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002417 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002418 break;
2419 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002420 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002421 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002422 case OBJ_TYPE_CHECK:
2423 src = (__objt_check(sess->origin)->server
2424 ? __objt_check(sess->origin)->server->id
2425 : "<NOSRV>");
2426 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002427 default:
2428 src = "<NOSRV>";
2429 break;
2430 }
William Lallemand5f232402012-04-05 18:02:55 +02002431 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002432 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002433 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002434 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002435 last_isspace = 0;
2436 break;
2437
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002438 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002439 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002440 if (ret == NULL)
2441 goto out;
2442 tmplog = ret;
2443 last_isspace = 0;
2444 break;
2445
2446 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002447 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002448 if (ret == NULL)
2449 goto out;
2450 tmplog = ret;
2451 last_isspace = 0;
2452 break;
2453
2454 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002455 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002456 tmplog, dst + maxsize - tmplog);
2457 if (ret == NULL)
2458 goto out;
2459 tmplog = ret;
2460 last_isspace = 0;
2461 break;
2462
2463 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002464 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002465 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002466 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002467 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002468 last_isspace = 0;
2469 break;
2470
William Lallemand1d705562012-03-12 12:46:41 +01002471 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002472 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002473 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002474 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002475 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002476 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002477 last_isspace = 0;
2478 break;
2479
William Lallemand1d705562012-03-12 12:46:41 +01002480 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002481 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002482 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002483 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002484 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002485 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002486 last_isspace = 0;
2487 break;
2488
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002489 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002490 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002491 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002492 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002493 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002494 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002495 last_isspace = 0;
2496 break;
2497
Willy Tarreau27b639d2016-05-17 17:55:27 +02002498 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002499 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002500 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002501 tmplog, dst + maxsize - tmplog);
2502 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002503 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002504 tmplog, dst + maxsize - tmplog);
2505 if (ret == NULL)
2506 goto out;
2507 tmplog = ret;
2508 last_isspace = 0;
2509 break;
2510
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002511 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2512 if (!(fe->to_log & LW_BYTES))
2513 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002514 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0),
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002515 tmplog, dst + maxsize - tmplog);
2516 if (ret == NULL)
2517 goto out;
2518 tmplog = ret;
2519 last_isspace = 0;
2520 break;
2521
2522 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002523 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002524 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002525 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002526 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002527 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002528 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002529 last_isspace = 0;
2530 break;
2531
Damien Claisse57c8eb92020-04-28 12:09:19 +00002532 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2533 if (!(fe->to_log & LW_BYTES))
2534 LOGCHAR('+');
2535 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2536 tmplog, dst + maxsize - tmplog);
2537 if (ret == NULL)
2538 goto out;
2539 tmplog = ret;
2540 last_isspace = 0;
2541 break;
2542
Willy Tarreau2beef582012-12-20 17:22:52 +01002543 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002544 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002545 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002546 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002547 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002548 last_isspace = 0;
2549 break;
2550
William Lallemand1d705562012-03-12 12:46:41 +01002551 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002552 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002553 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002554 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002555 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002556 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002557 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002558 last_isspace = 0;
2559 break;
2560
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002561 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002562 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002563 if (ret == NULL)
2564 goto out;
2565 tmplog = ret;
2566 last_isspace = 0;
2567 break;
2568
Willy Tarreau2beef582012-12-20 17:22:52 +01002569 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002570 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002571 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002572 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002573 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002574 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002575 last_isspace = 0;
2576 break;
2577
Willy Tarreau2beef582012-12-20 17:22:52 +01002578 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002579 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002580 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002581 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002582 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002583 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002584 last_isspace = 0;
2585 break;
2586
William Lallemand1d705562012-03-12 12:46:41 +01002587 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002588 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2589 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002590 *tmplog = '\0';
2591 last_isspace = 0;
2592 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002593
William Lallemand1d705562012-03-12 12:46:41 +01002594 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002595 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2596 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002597 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2598 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 +01002599 last_isspace = 0;
2600 break;
2601
William Lallemand1d705562012-03-12 12:46:41 +01002602 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002603 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002604 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002605 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002606 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002607 last_isspace = 0;
2608 break;
2609
William Lallemand1d705562012-03-12 12:46:41 +01002610 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002611 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002612 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002613 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002614 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002615 last_isspace = 0;
2616 break;
2617
William Lallemand1d705562012-03-12 12:46:41 +01002618 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002619 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002620 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002621 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002622 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002623 last_isspace = 0;
2624 break;
2625
William Lallemand1d705562012-03-12 12:46:41 +01002626 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002627 switch (obj_type(s ? s->target : sess->origin)) {
2628 case OBJ_TYPE_SERVER:
2629 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2630 tmplog, dst + maxsize - tmplog);
2631 break;
2632 case OBJ_TYPE_CHECK:
2633 ret = ultoa_o(__objt_check(sess->origin)->server
2634 ? __objt_check(sess->origin)->server->cur_sess
2635 : 0, tmplog, dst + maxsize - tmplog);
2636 break;
2637 default:
2638 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2639 break;
2640 }
2641
William Lallemand1d705562012-03-12 12:46:41 +01002642 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002643 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002644 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002645 last_isspace = 0;
2646 break;
2647
William Lallemand1d705562012-03-12 12:46:41 +01002648 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002649 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002650 LOGCHAR('+');
Christopher Faulet1d26f222021-04-16 11:24:20 +02002651 ret = ltoa_o(((s && s->si[1].conn_retries > 0)
2652 ? (be->conn_retries - s->si[1].conn_retries)
2653 : ((s && s->si[1].state != SI_ST_INI) ? be->conn_retries : 0)),
2654 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002655 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002656 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002657 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002658 last_isspace = 0;
2659 break;
2660
William Lallemand1d705562012-03-12 12:46:41 +01002661 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002662 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002663 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002664 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002665 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002666 last_isspace = 0;
2667 break;
2668
William Lallemand1d705562012-03-12 12:46:41 +01002669 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002670 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002671 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002672 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002673 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002674 last_isspace = 0;
2675 break;
2676
William Lallemand1d705562012-03-12 12:46:41 +01002677 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002678 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002679 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002680 if (tmp->options & LOG_OPT_QUOTE)
2681 LOGCHAR('"');
2682 LOGCHAR('{');
2683 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2684 if (hdr)
2685 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002686 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002687 ret = lf_encode_string(tmplog, dst + maxsize,
2688 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002689 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002690 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002691 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002692 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002693 }
2694 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002695 if (tmp->options & LOG_OPT_QUOTE)
2696 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002697 last_isspace = 0;
2698 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002699 break;
2700
William Lallemand1d705562012-03-12 12:46:41 +01002701 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002702 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002703 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002704 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2705 if (hdr > 0)
2706 LOGCHAR(' ');
2707 if (tmp->options & LOG_OPT_QUOTE)
2708 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002709 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002710 ret = lf_encode_string(tmplog, dst + maxsize,
2711 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002712 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002713 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002714 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002715 } else if (!(tmp->options & LOG_OPT_QUOTE))
2716 LOGCHAR('-');
2717 if (tmp->options & LOG_OPT_QUOTE)
2718 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002719 last_isspace = 0;
2720 }
2721 }
2722 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002723
William Lallemand1d705562012-03-12 12:46:41 +01002724
2725 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002726 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002727 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002728 if (tmp->options & LOG_OPT_QUOTE)
2729 LOGCHAR('"');
2730 LOGCHAR('{');
2731 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2732 if (hdr)
2733 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002734 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002735 ret = lf_encode_string(tmplog, dst + maxsize,
2736 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002737 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002738 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002739 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002740 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002741 }
2742 LOGCHAR('}');
2743 last_isspace = 0;
2744 if (tmp->options & LOG_OPT_QUOTE)
2745 LOGCHAR('"');
2746 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002747 break;
2748
William Lallemand1d705562012-03-12 12:46:41 +01002749 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002750 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002751 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002752 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2753 if (hdr > 0)
2754 LOGCHAR(' ');
2755 if (tmp->options & LOG_OPT_QUOTE)
2756 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002757 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002758 ret = lf_encode_string(tmplog, dst + maxsize,
2759 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002760 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002761 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002762 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002763 } else if (!(tmp->options & LOG_OPT_QUOTE))
2764 LOGCHAR('-');
2765 if (tmp->options & LOG_OPT_QUOTE)
2766 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002767 last_isspace = 0;
2768 }
2769 }
2770 break;
2771
William Lallemand1d705562012-03-12 12:46:41 +01002772 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002773 /* Request */
2774 if (tmp->options & LOG_OPT_QUOTE)
2775 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002776 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002777 ret = lf_encode_string(tmplog, dst + maxsize,
2778 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002779 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002780 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002781 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002782 if (tmp->options & LOG_OPT_QUOTE)
2783 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002784 last_isspace = 0;
2785 break;
William Lallemand5f232402012-04-05 18:02:55 +02002786
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002787 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002788 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002789
Willy Tarreaub7636d12015-06-17 19:58:02 +02002790 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002791 LOGCHAR('"');
2792
2793 end = uri + strlen(uri);
2794 // look for the first whitespace character
2795 while (uri < end && !HTTP_IS_SPHT(*uri))
2796 uri++;
2797
2798 // keep advancing past multiple spaces
2799 while (uri < end && HTTP_IS_SPHT(*uri)) {
2800 uri++; nspaces++;
2801 }
2802
2803 // look for first space or question mark after url
2804 spc = uri;
2805 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2806 spc++;
2807
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002808 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002809 chunk.area = "<BADREQ>";
2810 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002811 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002812 chunk.area = uri;
2813 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002814 }
2815
Dragan Dosen835b9212016-02-12 13:23:03 +01002816 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002817 if (ret == NULL || *ret != '\0')
2818 goto out;
2819
2820 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002821 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002822 LOGCHAR('"');
2823
2824 last_isspace = 0;
2825 break;
2826
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002827 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2828 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2829
2830 if (tmp->options & LOG_OPT_QUOTE)
2831 LOGCHAR('"');
2832
2833 end = uri + strlen(uri);
2834
2835 // look for the first whitespace character
2836 while (uri < end && !HTTP_IS_SPHT(*uri))
2837 uri++;
2838
2839 // keep advancing past multiple spaces
2840 while (uri < end && HTTP_IS_SPHT(*uri)) {
2841 uri++; nspaces++;
2842 }
2843
2844 // look for first space after url
2845 spc = uri;
2846 while (spc < end && !HTTP_IS_SPHT(*spc))
2847 spc++;
2848
Tim Duesterhus92c696e2021-02-28 16:11:36 +01002849 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002850
2851 // extract relative path without query params from url
2852 path = iststop(http_get_path(path), '?');
2853 if (!txn || !txn->uri || nspaces == 0) {
2854 chunk.area = "<BADREQ>";
2855 chunk.data = strlen("<BADREQ>");
2856 } else {
2857 chunk.area = path.ptr;
2858 chunk.data = path.len;
2859 }
2860
2861 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2862 if (ret == NULL || *ret != '\0')
2863 goto out;
2864
2865 tmplog = ret;
2866 if (tmp->options & LOG_OPT_QUOTE)
2867 LOGCHAR('"');
2868
2869 last_isspace = 0;
2870 break;
2871
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002872 case LOG_FMT_HTTP_QUERY: // %HQ
2873 if (tmp->options & LOG_OPT_QUOTE)
2874 LOGCHAR('"');
2875
Willy Tarreau57bc8912016-04-25 17:09:40 +02002876 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002877 chunk.area = "<BADREQ>";
2878 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002879 } else {
2880 uri = txn->uri;
2881 end = uri + strlen(uri);
2882 // look for the first question mark
2883 while (uri < end && *uri != '?')
2884 uri++;
2885
2886 qmark = uri;
2887 // look for first space or question mark after url
2888 while (uri < end && !HTTP_IS_SPHT(*uri))
2889 uri++;
2890
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002891 chunk.area = qmark;
2892 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002893 }
2894
Dragan Dosen835b9212016-02-12 13:23:03 +01002895 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002896 if (ret == NULL || *ret != '\0')
2897 goto out;
2898
2899 tmplog = ret;
2900 if (tmp->options & LOG_OPT_QUOTE)
2901 LOGCHAR('"');
2902
2903 last_isspace = 0;
2904 break;
2905
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002906 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002907 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002908
Willy Tarreaub7636d12015-06-17 19:58:02 +02002909 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002910 LOGCHAR('"');
2911
2912 end = uri + strlen(uri);
2913 // look for the first whitespace character
2914 while (uri < end && !HTTP_IS_SPHT(*uri))
2915 uri++;
2916
2917 // keep advancing past multiple spaces
2918 while (uri < end && HTTP_IS_SPHT(*uri)) {
2919 uri++; nspaces++;
2920 }
2921
2922 // look for first space after url
2923 spc = uri;
2924 while (spc < end && !HTTP_IS_SPHT(*spc))
2925 spc++;
2926
Willy Tarreau57bc8912016-04-25 17:09:40 +02002927 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002928 chunk.area = "<BADREQ>";
2929 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002930 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002931 chunk.area = uri;
2932 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002933 }
2934
Dragan Dosen835b9212016-02-12 13:23:03 +01002935 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002936 if (ret == NULL || *ret != '\0')
2937 goto out;
2938
2939 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002940 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002941 LOGCHAR('"');
2942
2943 last_isspace = 0;
2944 break;
2945
2946 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002947 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002948 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002949 LOGCHAR('"');
2950
2951 end = uri + strlen(uri);
2952 // look for the first whitespace character
2953 spc = uri;
2954 while (spc < end && !HTTP_IS_SPHT(*spc))
2955 spc++;
2956
2957 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002958 chunk.area = "<BADREQ>";
2959 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002960 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002961 chunk.area = uri;
2962 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002963 }
2964
Dragan Dosen835b9212016-02-12 13:23:03 +01002965 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002966 if (ret == NULL || *ret != '\0')
2967 goto out;
2968
2969 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002970 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002971 LOGCHAR('"');
2972
2973 last_isspace = 0;
2974 break;
2975
2976 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002977 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002978 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002979 LOGCHAR('"');
2980
2981 end = uri + strlen(uri);
2982 // look for the first whitespace character
2983 while (uri < end && !HTTP_IS_SPHT(*uri))
2984 uri++;
2985
2986 // keep advancing past multiple spaces
2987 while (uri < end && HTTP_IS_SPHT(*uri)) {
2988 uri++; nspaces++;
2989 }
2990
2991 // look for the next whitespace character
2992 while (uri < end && !HTTP_IS_SPHT(*uri))
2993 uri++;
2994
2995 // keep advancing past multiple spaces
2996 while (uri < end && HTTP_IS_SPHT(*uri))
2997 uri++;
2998
Willy Tarreau57bc8912016-04-25 17:09:40 +02002999 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003000 chunk.area = "<BADREQ>";
3001 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003002 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003003 chunk.area = "HTTP/0.9";
3004 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003005 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003006 chunk.area = uri;
3007 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003008 }
3009
Dragan Dosen835b9212016-02-12 13:23:03 +01003010 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003011 if (ret == NULL || *ret != '\0')
3012 goto out;
3013
3014 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003015 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003016 LOGCHAR('"');
3017
3018 last_isspace = 0;
3019 break;
3020
William Lallemand5f232402012-04-05 18:02:55 +02003021 case LOG_FMT_COUNTER: // %rt
3022 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003023 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02003024 if (iret < 0 || iret > dst + maxsize - tmplog)
3025 goto out;
3026 last_isspace = 0;
3027 tmplog += iret;
3028 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003029 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003030 if (ret == NULL)
3031 goto out;
3032 tmplog = ret;
3033 last_isspace = 0;
3034 }
3035 break;
3036
Willy Tarreau7346acb2014-08-28 15:03:15 +02003037 case LOG_FMT_LOGCNT: // %lc
3038 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003039 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003040 if (iret < 0 || iret > dst + maxsize - tmplog)
3041 goto out;
3042 last_isspace = 0;
3043 tmplog += iret;
3044 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003045 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003046 if (ret == NULL)
3047 goto out;
3048 tmplog = ret;
3049 last_isspace = 0;
3050 }
3051 break;
3052
William Lallemand5f232402012-04-05 18:02:55 +02003053 case LOG_FMT_HOSTNAME: // %H
3054 src = hostname;
3055 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3056 if (ret == NULL)
3057 goto out;
3058 tmplog = ret;
3059 last_isspace = 0;
3060 break;
3061
3062 case LOG_FMT_PID: // %pid
3063 if (tmp->options & LOG_OPT_HEXA) {
3064 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3065 if (iret < 0 || iret > dst + maxsize - tmplog)
3066 goto out;
3067 last_isspace = 0;
3068 tmplog += iret;
3069 } else {
3070 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3071 if (ret == NULL)
3072 goto out;
3073 tmplog = ret;
3074 last_isspace = 0;
3075 }
3076 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003077
3078 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003079 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003080 if (s)
3081 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3082 else
3083 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003084 if (ret == NULL)
3085 goto out;
3086 tmplog = ret;
3087 last_isspace = 0;
3088 break;
3089
William Lallemandbddd4fd2012-02-27 11:23:10 +01003090 }
3091 }
3092
3093out:
William Lallemand1d705562012-03-12 12:46:41 +01003094 /* *tmplog is a unused character */
3095 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003096 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003097
Willy Tarreaubaaee002006-06-26 02:48:02 +02003098}
3099
William Lallemand1d705562012-03-12 12:46:41 +01003100/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003101 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003102 * Will not log if the frontend has no log defined.
3103 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003104void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003105{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003106 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003107 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003108 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003109
3110 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003111 err = (s->flags & SF_REDISP) ||
3112 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3113 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003114 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003115 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003116
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003117 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003118 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003119
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003120 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003121 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003122
Willy Tarreauabcd5142013-06-11 17:18:02 +02003123 if (s->logs.level) { /* loglevel was overridden */
3124 if (s->logs.level == -1) {
3125 s->logs.logwait = 0; /* logs disabled */
3126 return;
3127 }
3128 level = s->logs.level - 1;
3129 }
3130 else {
3131 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003132 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003133 level = LOG_ERR;
3134 }
William Lallemand1d705562012-03-12 12:46:41 +01003135
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003136 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003137 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003138 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003139 }
3140
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003141 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3142 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3143 &sess->fe->logformat_sd);
3144 }
3145
Dragan Dosen59cee972015-09-19 22:09:02 +02003146 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003147 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003148 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003149 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3150 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003151 s->logs.logwait = 0;
3152 }
3153}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003154
Willy Tarreau53839352018-09-05 19:51:10 +02003155/*
3156 * send a minimalist log for the session. Will not log if the frontend has no
3157 * log defined. It is assumed that this is only used to report anomalies that
3158 * cannot lead to the creation of a regular stream. Because of this the log
3159 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3160 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003161 * function to report unimportant events. It is safe to call this function with
3162 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003163 */
3164void sess_log(struct session *sess)
3165{
3166 int size, level;
3167 int sd_size = 0;
3168
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003169 if (!sess)
3170 return;
3171
Willy Tarreau53839352018-09-05 19:51:10 +02003172 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3173 return;
3174
3175 level = LOG_INFO;
3176 if (sess->fe->options2 & PR_O2_LOGERRORS)
3177 level = LOG_ERR;
3178
3179 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3180 sd_size = sess_build_logline(sess, NULL,
3181 logline_rfc5424, global.max_syslog_len,
3182 &sess->fe->logformat_sd);
3183 }
3184
3185 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3186 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003187 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003188 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3189 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003190 }
3191}
3192
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003193void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3194{
3195 va_list argp;
3196 int data_len;
3197
3198 if (level < 0 || format == NULL || logline == NULL)
3199 return;
3200
3201 va_start(argp, format);
3202 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3203 if (data_len < 0 || data_len > global.max_syslog_len)
3204 data_len = global.max_syslog_len;
3205 va_end(argp);
3206
3207 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3208}
Emeric Brun54932b42020-07-07 09:43:24 +02003209/*
3210 * This function parse a received log message <buf>, of size <buflen>
3211 * it fills <level>, <facility> and <metadata> depending of the detected
3212 * header format and message will point on remaining payload of <size>
3213 *
3214 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3215 * struct ist len will be set to 0 if field is not found
3216 * <level> and <facility> will be set to -1 if not found.
3217 */
3218void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3219 struct ist *metadata, char **message, size_t *size)
3220{
3221
3222 char *p;
3223 int fac_level = 0;
3224
3225 *level = *facility = -1;
3226
3227 *message = buf;
3228 *size = buflen;
3229
3230 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3231
3232 p = buf;
3233 if (*size < 2 || *p != '<')
3234 return;
3235
3236 p++;
3237 while (*p != '>') {
3238 if (*p > '9' || *p < '0')
3239 return;
3240 fac_level = 10*fac_level + (*p - '0');
3241 p++;
3242 if ((p - buf) > buflen)
3243 return;
3244 }
3245
3246 *facility = fac_level >> 3;
3247 *level = fac_level & 0x7;
3248 p++;
3249
3250 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3251
3252 buflen -= p - buf;
3253 buf = p;
3254
3255 *size = buflen;
3256 *message = buf;
3257
3258 /* for rfc5424, prio is always followed by '1' and ' ' */
3259 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3260 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3261 * followed by message.
3262 * Each header field can present NILVALUE: '-'
3263 */
3264
3265 p += 2;
3266 /* timestamp is NILVALUE '-' */
3267 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3268 metadata[LOG_META_TIME] = ist2(p, 1);
3269 p++;
3270 }
3271 else if (*size > LOG_ISOTIME_MINLEN) {
3272 metadata[LOG_META_TIME].ptr = p;
3273
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003274 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003275 * in timestamp.
3276 * possible format are:
3277 * ex: '1970-01-01T00:00:00.000000Z'
3278 * '1970-01-01T00:00:00.000000+00:00'
3279 * '1970-01-01T00:00:00.000000-00:00'
3280 * '1970-01-01T00:00:00Z'
3281 * '1970-01-01T00:00:00+00:00'
3282 * '1970-01-01T00:00:00-00:00'
3283 */
3284 p += 19;
3285 if (*p == '.') {
3286 p++;
3287 if ((p - buf) >= buflen)
3288 goto bad_format;
3289 while (*p != 'Z' && *p != '+' && *p != '-') {
3290 if ((unsigned char)(*p - '0') > 9)
3291 goto bad_format;
3292
3293 p++;
3294 if ((p - buf) >= buflen)
3295 goto bad_format;
3296 }
3297 }
3298
3299 if (*p == 'Z')
3300 p++;
3301 else
3302 p += 6; /* case of '+00:00 or '-00:00' */
3303
3304 if ((p - buf) >= buflen || *p != ' ')
3305 goto bad_format;
3306 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3307 }
3308 else
3309 goto bad_format;
3310
3311
3312 p++;
3313 if ((p - buf) >= buflen || *p == ' ')
3314 goto bad_format;
3315
3316 metadata[LOG_META_HOST].ptr = p;
3317 while (*p != ' ') {
3318 p++;
3319 if ((p - buf) >= buflen)
3320 goto bad_format;
3321 }
3322 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3323 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3324 metadata[LOG_META_HOST].len = 0;
3325
3326 p++;
3327 if ((p - buf) >= buflen || *p == ' ')
3328 goto bad_format;
3329
3330 metadata[LOG_META_TAG].ptr = p;
3331 while (*p != ' ') {
3332 p++;
3333 if ((p - buf) >= buflen)
3334 goto bad_format;
3335 }
3336 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3337 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3338 metadata[LOG_META_TAG].len = 0;
3339
3340 p++;
3341 if ((p - buf) >= buflen || *p == ' ')
3342 goto bad_format;
3343
3344 metadata[LOG_META_PID].ptr = p;
3345 while (*p != ' ') {
3346 p++;
3347 if ((p - buf) >= buflen)
3348 goto bad_format;
3349 }
3350 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3351 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3352 metadata[LOG_META_PID].len = 0;
3353
3354 p++;
3355 if ((p - buf) >= buflen || *p == ' ')
3356 goto bad_format;
3357
3358 metadata[LOG_META_MSGID].ptr = p;
3359 while (*p != ' ') {
3360 p++;
3361 if ((p - buf) >= buflen)
3362 goto bad_format;
3363 }
3364 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3365 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3366 metadata[LOG_META_MSGID].len = 0;
3367
3368 p++;
3369 if ((p - buf) >= buflen || *p == ' ')
3370 goto bad_format;
3371
3372 /* structured data format is:
3373 * ex:
3374 * '[key1=value1 key2=value2][key3=value3]'
3375 *
3376 * space is invalid outside [] because
3377 * considered as the end of structured data field
3378 */
3379 metadata[LOG_META_STDATA].ptr = p;
3380 if (*p == '[') {
3381 int elem = 0;
3382
3383 while (1) {
3384 if (elem) {
3385 /* according to rfc this char is escaped in param values */
3386 if (*p == ']' && *(p-1) != '\\')
3387 elem = 0;
3388 }
3389 else {
3390 if (*p == '[')
3391 elem = 1;
3392 else if (*p == ' ')
3393 break;
3394 else
3395 goto bad_format;
3396 }
3397 p++;
3398 if ((p - buf) >= buflen)
3399 goto bad_format;
3400 }
3401 }
3402 else if (*p == '-') {
3403 /* case of NILVALUE */
3404 p++;
3405 if ((p - buf) >= buflen || *p != ' ')
3406 goto bad_format;
3407 }
3408 else
3409 goto bad_format;
3410
3411 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3412 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3413 metadata[LOG_META_STDATA].len = 0;
3414
3415 p++;
3416
3417 buflen -= p - buf;
3418 buf = p;
3419
3420 *size = buflen;
3421 *message = p;
3422 }
3423 else if (*size > LOG_LEGACYTIME_LEN) {
3424 int m;
3425
3426 /* supported header format according to rfc3164.
3427 * ex:
3428 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3429 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3430 * or 'Jan 1 00:00:00 HOSTNAME '
3431 * Note: HOSTNAME is mandatory, and day
3432 * of month uses a single space prefix if
3433 * less than 10 to ensure hour offset is
3434 * always the same.
3435 */
3436
3437 /* Check month to see if it correspond to a rfc3164
3438 * header ex 'Jan 1 00:00:00' */
3439 for (m = 0; m < 12; m++)
3440 if (!memcmp(monthname[m], p, 3))
3441 break;
3442 /* Month not found */
3443 if (m == 12)
3444 goto bad_format;
3445
3446 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3447
3448 p += LOG_LEGACYTIME_LEN;
3449 if ((p - buf) >= buflen || *p != ' ')
3450 goto bad_format;
3451
3452 p++;
3453 if ((p - buf) >= buflen || *p == ' ')
3454 goto bad_format;
3455
3456 metadata[LOG_META_HOST].ptr = p;
3457 while (*p != ' ') {
3458 p++;
3459 if ((p - buf) >= buflen)
3460 goto bad_format;
3461 }
3462 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3463
3464 /* TAG seems to no be mandatory */
3465 p++;
3466
3467 buflen -= p - buf;
3468 buf = p;
3469
3470 *size = buflen;
3471 *message = buf;
3472
3473 if (!buflen)
3474 return;
3475
3476 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3477 p++;
3478
3479 /* a tag must present a trailing ':' */
3480 if (((p - buf) >= buflen) || *p != ':')
3481 return;
3482 p++;
3483 /* followed by a space */
3484 if (((p - buf) >= buflen) || *p != ' ')
3485 return;
3486
3487 /* rewind to parse tag and pid */
3488 p = buf;
3489 metadata[LOG_META_TAG].ptr = p;
3490 /* we have the guarantee that ':' will be reach before size limit */
3491 while (*p != ':') {
3492 if (*p == '[') {
3493 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3494 metadata[LOG_META_PID].ptr = p + 1;
3495 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003496 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003497 if (p[1] != ':')
3498 return;
3499 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3500 }
3501 p++;
3502 }
3503 if (!metadata[LOG_META_TAG].len)
3504 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3505
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003506 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003507 p += 2;
3508
3509 buflen -= p - buf;
3510 buf = p;
3511
3512 *size = buflen;
3513 *message = buf;
3514 }
3515
3516 return;
3517
3518bad_format:
3519 /* bad syslog format, we reset all parsed syslog fields
3520 * but priority is kept because we are able to re-build
3521 * this message using LOF_FORMAT_PRIO.
3522 */
3523 metadata[LOG_META_TIME].len = 0;
3524 metadata[LOG_META_HOST].len = 0;
3525 metadata[LOG_META_TAG].len = 0;
3526 metadata[LOG_META_PID].len = 0;
3527 metadata[LOG_META_MSGID].len = 0;
3528 metadata[LOG_META_STDATA].len = 0;
3529
3530 return;
3531}
3532
3533/*
3534 * UDP syslog fd handler
3535 */
3536void syslog_fd_handler(int fd)
3537{
3538 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3539 ssize_t ret = 0;
3540 struct buffer *buf = get_trash_chunk();
3541 size_t size;
3542 char *message;
3543 int level;
3544 int facility;
3545 struct listener *l = objt_listener(fdtab[fd].owner);
3546 int max_accept;
3547
3548 if(!l)
3549 ABORT_NOW();
3550
Willy Tarreauf5090652021-04-06 17:23:40 +02003551 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003552
3553 if (!fd_recv_ready(fd))
3554 return;
3555
3556 max_accept = l->maxaccept ? l->maxaccept : 1;
3557
3558 do {
3559 /* Source address */
3560 struct sockaddr_storage saddr = {0};
3561 socklen_t saddrlen;
3562
3563 saddrlen = sizeof(saddr);
3564
3565 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3566 if (ret < 0) {
3567 if (errno == EINTR)
3568 continue;
3569 if (errno == EAGAIN)
3570 fd_cant_recv(fd);
3571 goto out;
3572 }
3573 buf->data = ret;
3574
Emeric Brun45c457a2020-07-09 23:23:34 +02003575 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003576 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003577 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003578
Emeric Brun54932b42020-07-07 09:43:24 +02003579 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3580
3581 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3582
3583 } while (--max_accept);
3584 }
3585
3586out:
3587 return;
3588}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003589
Emeric Brun12941c82020-07-07 14:19:42 +02003590/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003591 * IO Handler to handle message exchange with a syslog tcp client
3592 */
3593static void syslog_io_handler(struct appctx *appctx)
3594{
3595 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3596 struct stream_interface *si = appctx->owner;
3597 struct stream *s = si_strm(si);
3598 struct proxy *frontend = strm_fe(s);
3599 struct listener *l = strm_li(s);
3600 struct buffer *buf = get_trash_chunk();
3601 int max_accept;
3602 int to_skip;
3603 int facility;
3604 int level;
3605 char *message;
3606 size_t size;
3607
3608 max_accept = l->maxaccept ? l->maxaccept : 1;
3609 while (co_data(si_oc(si))) {
3610 char c;
3611
3612 if (max_accept <= 0)
3613 goto missing_budget;
3614 max_accept--;
3615
3616 to_skip = co_getchar(si_oc(si), &c);
3617 if (!to_skip)
3618 goto missing_data;
3619 else if (to_skip < 0)
3620 goto cli_abort;
3621
3622 if (c == '<') {
3623 /* rfc-6587, Non-Transparent-Framing: messages separated by
3624 * a trailing LF or CR LF
3625 */
3626 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3627 if (!to_skip)
3628 goto missing_data;
3629 else if (to_skip < 0)
3630 goto cli_abort;
3631
3632 if (buf->area[to_skip - 1] != '\n')
3633 goto parse_error;
3634
3635 buf->data = to_skip - 1;
3636
3637 /* according to rfc-6587, some devices adds CR before LF */
3638 if (buf->data && buf->area[buf->data - 1] == '\r')
3639 buf->data--;
3640
3641 }
3642 else if ((unsigned char)(c - '1') <= 8) {
3643 /* rfc-6587, Octet-Counting: message length in ASCII
3644 * (first digit can not be ZERO), followed by a space
3645 * and message length
3646 */
3647 char *p = NULL;
3648 int msglen;
3649
3650 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3651 if (!to_skip)
3652 goto missing_data;
3653 else if (to_skip < 0)
3654 goto cli_abort;
3655
3656 if (buf->area[to_skip - 1] != ' ')
3657 goto parse_error;
3658
3659 msglen = strtol(trash.area, &p, 10);
3660 if (!msglen || p != &buf->area[to_skip - 1])
3661 goto parse_error;
3662
3663 /* message seems too large */
3664 if (msglen > buf->size)
3665 goto parse_error;
3666
3667 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3668 if (!msglen)
3669 goto missing_data;
3670 else if (msglen < 0)
3671 goto cli_abort;
3672
3673
3674 buf->data = msglen;
3675 to_skip += msglen;
3676 }
3677 else
3678 goto parse_error;
3679
3680 co_skip(si_oc(si), to_skip);
3681
3682 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003683 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003684 proxy_inc_fe_req_ctr(l, frontend);
3685
3686 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3687
3688 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3689
3690 }
3691
3692missing_data:
3693 /* we need more data to read */
3694 si_oc(si)->flags |= CF_READ_DONTWAIT;
3695
3696 return;
3697
3698missing_budget:
3699 /* it may remain some stuff to do, let's retry later */
3700 appctx_wakeup(appctx);
3701
3702 return;
3703
3704parse_error:
3705 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003706 _HA_ATOMIC_INC(&l->counters->failed_req);
3707 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003708
3709 goto close;
3710
3711cli_abort:
3712 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003713 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3714 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003715
3716close:
3717 si_shutw(si);
3718 si_shutr(si);
3719
3720 si_ic(si)->flags |= CF_READ_NULL;
3721
3722 return;
3723}
3724
3725static struct applet syslog_applet = {
3726 .obj_type = OBJ_TYPE_APPLET,
3727 .name = "<SYSLOG>", /* used for logging */
3728 .fct = syslog_io_handler,
3729 .release = NULL,
3730};
3731
3732/*
Emeric Brun12941c82020-07-07 14:19:42 +02003733 * Parse "log-forward" section and create corresponding sink buffer.
3734 *
3735 * The function returns 0 in success case, otherwise, it returns error
3736 * flags.
3737 */
3738int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3739{
3740 int err_code = 0;
3741 struct proxy *px;
3742 char *errmsg = NULL;
3743 const char *err = NULL;
3744
3745 if (strcmp(args[0], "log-forward") == 0) {
3746 if (!*args[1]) {
3747 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3748 err_code |= ERR_ALERT | ERR_ABORT;
3749 goto out;
3750 }
3751
3752 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3753 goto out;
3754
3755 err = invalid_char(args[1]);
3756 if (err) {
3757 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3758 file, linenum, *err, args[0], args[1]);
3759 err_code |= ERR_ALERT | ERR_ABORT;
3760 goto out;
3761 }
3762
Emeric Brunb0c331f2020-10-07 17:05:59 +02003763 px = log_forward_by_name(args[1]);
3764 if (px) {
3765 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3766 file, linenum, args[1], px->conf.file, px->conf.line);
3767 err_code |= ERR_ALERT | ERR_FATAL;
3768 }
3769
3770 px = proxy_find_by_name(args[1], 0, 0);
3771 if (px) {
3772 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3773 file, linenum, args[1], proxy_type_str(px),
3774 px->id, px->conf.file, px->conf.line);
3775 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003776 }
3777
3778 px = calloc(1, sizeof *px);
3779 if (!px) {
3780 err_code |= ERR_ALERT | ERR_FATAL;
3781 goto out;
3782 }
3783
3784 px->next = cfg_log_forward;
3785 cfg_log_forward = px;
3786
3787 init_new_proxy(px);
3788 px->conf.file = strdup(file);
3789 px->conf.line = linenum;
3790 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003791 px->last_change = now.tv_sec;
3792 px->cap = PR_CAP_FE;
3793 px->maxconn = 10;
3794 px->timeout.client = TICK_ETERNITY;
3795 px->accept = frontend_accept;
3796 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003797 px->id = strdup(args[1]);
3798
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003799 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003800 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003801 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3802 err_code |= ERR_WARN;
3803
3804 if (*(args[1]) == 0) {
3805 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3806 err_code |= ERR_ALERT | ERR_FATAL;
3807 goto out;
3808 }
3809 cfg_log_forward->maxconn = atol(args[1]);
3810 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3811 goto out;
3812 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003813 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003814 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3815 err_code |= ERR_WARN;
3816
3817 if (*(args[1]) == 0) {
3818 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3819 err_code |= ERR_ALERT | ERR_FATAL;
3820 goto out;
3821 }
3822 cfg_log_forward->backlog = atol(args[1]);
3823 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3824 goto out;
3825 }
3826 else if (strcmp(args[0], "bind") == 0) {
3827 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003828 struct bind_conf *bind_conf;
3829 struct bind_kw *kw;
3830 struct listener *l;
3831
3832 cur_arg = 1;
3833
3834 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3835 NULL, xprt_get(XPRT_RAW));
3836 if (!bind_conf) {
3837 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3838 err_code |= ERR_ALERT | ERR_FATAL;
3839 goto out;
3840 }
3841
3842 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3843 if (errmsg && *errmsg) {
3844 indent_msg(&errmsg, 2);
3845 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3846 }
3847 else {
3848 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3849 file, linenum, args[0], args[1], args[2]);
3850 err_code |= ERR_ALERT | ERR_FATAL;
3851 goto out;
3852 }
3853 }
3854 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau66161322021-02-19 15:50:27 +01003855 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003856 l->accept = session_accept_fd;
3857 l->analysers |= cfg_log_forward->fe_req_ana;
3858 l->default_target = cfg_log_forward->default_target;
3859 global.maxsock++;
3860 }
3861 cur_arg++;
3862
3863 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3864 int ret;
3865
3866 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3867 err_code |= ret;
3868 if (ret) {
3869 if (errmsg && *errmsg) {
3870 indent_msg(&errmsg, 2);
3871 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3872 }
3873 else
3874 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3875 file, linenum, args[cur_arg]);
3876 if (ret & ERR_FATAL)
3877 goto out;
3878 }
3879 cur_arg += 1 + kw->skip;
3880 }
3881 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003882 const char *best = bind_find_best_kw(args[cur_arg]);
3883 if (best)
3884 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3885 file, linenum, args[cur_arg], cursection, best);
3886 else
3887 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3888 file, linenum, args[cur_arg], cursection);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003889 err_code |= ERR_ALERT | ERR_FATAL;
3890 goto out;
3891 }
Emeric Brun12941c82020-07-07 14:19:42 +02003892 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003893 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003894 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02003895 struct bind_conf *bind_conf;
3896 struct bind_kw *kw;
3897 struct listener *l;
3898
3899 cur_arg = 1;
3900
3901 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3902 NULL, xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +02003903 if (!bind_conf) {
3904 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3905 err_code |= ERR_ALERT | ERR_FATAL;
3906 goto out;
3907 }
Emeric Brun12941c82020-07-07 14:19:42 +02003908
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003909 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003910 if (errmsg && *errmsg) {
3911 indent_msg(&errmsg, 2);
3912 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3913 }
3914 else {
3915 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3916 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003917 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003918 err_code |= ERR_ALERT | ERR_FATAL;
3919 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003920 }
3921 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003922 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreau66161322021-02-19 15:50:27 +01003923 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreaue140a692020-10-15 21:25:32 +02003924 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003925 global.maxsock++;
3926 }
3927 cur_arg++;
3928
3929 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3930 int ret;
3931
3932 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3933 err_code |= ret;
3934 if (ret) {
3935 if (errmsg && *errmsg) {
3936 indent_msg(&errmsg, 2);
3937 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3938 }
3939 else
3940 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3941 file, linenum, args[cur_arg]);
3942 if (ret & ERR_FATAL)
3943 goto out;
3944 }
3945 cur_arg += 1 + kw->skip;
3946 }
3947 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003948 const char *best = bind_find_best_kw(args[cur_arg]);
3949 if (best)
3950 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3951 file, linenum, args[cur_arg], cursection, best);
3952 else
3953 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3954 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02003955 err_code |= ERR_ALERT | ERR_FATAL;
3956 goto out;
3957 }
3958 }
3959 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02003960 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003961 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3962 err_code |= ERR_ALERT | ERR_FATAL;
3963 goto out;
3964 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003965 }
3966 else if (strcmp(args[0], "timeout") == 0) {
3967 const char *res;
3968 unsigned timeout;
3969
3970 if (strcmp(args[1], "client") != 0) {
3971 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
3972 err_code |= ERR_ALERT | ERR_FATAL;
3973 goto out;
3974 }
3975
3976 if (*args[2] == 0) {
3977 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
3978 err_code |= ERR_ALERT | ERR_FATAL;
3979 goto out;
3980 }
3981 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3982 if (res == PARSE_TIME_OVER) {
3983 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
3984 }
3985 else if (res == PARSE_TIME_UNDER) {
3986 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
3987 }
3988 else if (res) {
3989 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
3990 return -1;
3991 }
3992
3993 if (res) {
3994 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3995 err_code |= ERR_ALERT | ERR_FATAL;
3996 goto out;
3997 }
3998 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02003999 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02004000 else {
4001 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
4002 err_code |= ERR_ALERT | ERR_ABORT;
4003 goto out;
4004 }
Emeric Brun12941c82020-07-07 14:19:42 +02004005out:
4006 return err_code;
4007}
4008
Willy Tarreau0108d902018-11-25 19:14:37 +01004009
Emeric Brun12941c82020-07-07 14:19:42 +02004010/* config parsers for this section */
4011REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4012
Willy Tarreau082b6282019-05-22 14:42:12 +02004013REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4014REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004015
Willy Tarreaubaaee002006-06-26 02:48:02 +02004016/*
4017 * Local variables:
4018 * c-indent-level: 8
4019 * c-basic-offset: 8
4020 * End:
4021 */