blob: 6014bfc2d272ff17d4a263cc1fcb33c4aaa91dbb [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 Tarreau83487a82020-06-04 20:19:54 +020030#include <haproxy/cli.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020032#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020034#include <haproxy/http.h>
Emeric Brun12941c82020-07-07 14:19:42 +020035#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036#include <haproxy/log.h>
Emeric Brun12941c82020-07-07 14:19:42 +020037#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/ring.h>
39#include <haproxy/sample.h>
40#include <haproxy/sink.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020041#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020043#include <haproxy/stream_interface.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020044#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020045#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020046#include <haproxy/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Emeric Brun45c457a2020-07-09 23:23:34 +020048/* global recv logs counter */
49int cum_log_messages;
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Emeric Brun12941c82020-07-07 14:19:42 +020051/* log forward proxy list */
52struct proxy *cfg_log_forward;
53
Emeric Brun54648852020-07-06 15:54:06 +020054struct log_fmt_st {
Dragan Dosen43885c72015-10-01 13:18:13 +020055 char *name;
Dragan Dosen43885c72015-10-01 13:18:13 +020056};
57
Emeric Brun54648852020-07-06 15:54:06 +020058static const struct log_fmt_st log_formats[LOG_FORMATS] = {
Emeric Bruna0338b92020-11-27 16:24:34 +010059 [LOG_FORMAT_LOCAL] = {
60 .name = "local",
61 },
Dragan Dosen43885c72015-10-01 13:18:13 +020062 [LOG_FORMAT_RFC3164] = {
63 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020064 },
65 [LOG_FORMAT_RFC5424] = {
66 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020067 },
68 [LOG_FORMAT_PRIO] = {
69 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010070 },
71 [LOG_FORMAT_SHORT] = {
72 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020073 },
74 [LOG_FORMAT_TIMED] = {
75 .name = "timed",
76 },
77 [LOG_FORMAT_ISO] = {
78 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010079 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010080 [LOG_FORMAT_RAW] = {
81 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010082 },
Dragan Dosen1322d092015-09-22 16:05:32 +020083};
84
Dragan Dosen835b9212016-02-12 13:23:03 +010085/*
86 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020087 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
88 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
89 * that the byte should be escaped. Be careful to always pass bytes from 0 to
90 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010091 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020092long rfc5424_escape_map[(256/8) / sizeof(long)];
93long hdr_encode_map[(256/8) / sizeof(long)];
94long url_encode_map[(256/8) / sizeof(long)];
95long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010096
Dragan Dosen835b9212016-02-12 13:23:03 +010097
Willy Tarreaubaaee002006-06-26 02:48:02 +020098const char *log_facilities[NB_LOG_FACILITIES] = {
99 "kern", "user", "mail", "daemon",
100 "auth", "syslog", "lpr", "news",
101 "uucp", "cron", "auth2", "ftp",
102 "ntp", "audit", "alert", "cron2",
103 "local0", "local1", "local2", "local3",
104 "local4", "local5", "local6", "local7"
105};
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107const char *log_levels[NB_LOG_LEVELS] = {
108 "emerg", "alert", "crit", "err",
109 "warning", "notice", "info", "debug"
110};
111
Willy Tarreau570f2212013-06-10 16:42:09 +0200112const 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 +0200113const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
William Lallemand723b73a2012-02-08 16:37:49 +0100115
116/* log_format */
117struct logformat_type {
118 char *name;
119 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100120 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200121 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100122 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100123 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100124};
125
William Lallemandb7ff6a32012-03-02 14:35:21 +0100126int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
127
William Lallemand723b73a2012-02-08 16:37:49 +0100128/* log_format variable names */
129static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200130 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100131
132 /* please keep these lines sorted ! */
133 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
134 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
135 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
136 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100137 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200138 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200139 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200140 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100141 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200142 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
143 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
144 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
145 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
146 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
147 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200148 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100149 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200150 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000151 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100152 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
153 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200154 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100155 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200156 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100157 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
158 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200159 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200160 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
161 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100162 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
163 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200164 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
165 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100166 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200167 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
168 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
169 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
170 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000171 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
172 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000173 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000174 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
175 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200176 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100177 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200178 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100179 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
180 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100181 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100182 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
183 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
184 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
185 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
186 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200187 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
188 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100189 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200190 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
191 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
192 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100193 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
194 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
195
196 /* The following tags are deprecated and will be removed soon */
197 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
198 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200199 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
200 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
201 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
202 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100203 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
204 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
205 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
206 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
207 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200208 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100209};
210
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200211char 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
212char 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 +0100213char 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 +0100214char *log_format = NULL;
215
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200216/* Default string used for structured-data part in RFC5424 formatted
217 * syslog messages.
218 */
219char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200220
Willy Tarreau13ef7732018-11-12 07:25:28 +0100221/* total number of dropped logs */
222unsigned int dropped_logs = 0;
223
Dragan Dosen59cee972015-09-19 22:09:02 +0200224/* This is a global syslog message buffer, common to all outgoing
225 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100226 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200227THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100228
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200229/* A global syslog message buffer, common to all RFC5424 syslog messages.
230 * Currently, it is used for generating the structured-data part.
231 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200232THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200233
Christopher Fauletd4696382017-10-24 11:44:05 +0200234/* A global buffer used to store all startup alerts/warnings. It will then be
235 * retrieve on the CLI. */
Willy Tarreau869efd52019-11-15 15:16:57 +0100236static struct ring *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200237
William Lallemand723b73a2012-02-08 16:37:49 +0100238struct logformat_var_args {
239 char *name;
240 int mask;
241};
242
243struct logformat_var_args var_args_list[] = {
244// global
245 { "M", LOG_OPT_MANDATORY },
246 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200247 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100248 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100249 { 0, 0 }
250};
251
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200252/* return the name of the directive used in the current proxy for which we're
253 * currently parsing a header, when it is known.
254 */
255static inline const char *fmt_directive(const struct proxy *curproxy)
256{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100257 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200258 case ARGC_ACL:
259 return "acl";
260 case ARGC_STK:
261 return "stick";
262 case ARGC_TRK:
263 return "track-sc";
264 case ARGC_LOG:
265 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200266 case ARGC_LOGSD:
267 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100268 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100269 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100270 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100271 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200272 case ARGC_UIF:
273 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100274 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200275 return "redirect";
276 case ARGC_CAP:
277 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200278 case ARGC_SRV:
279 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200280 case ARGC_SPOE:
281 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100282 case ARGC_UBK:
283 return "use_backend";
Christopher Faulet3b967c12020-05-15 15:47:44 +0200284 case ARGC_HERR:
285 return "http-error";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100286 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200287 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100288 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200289}
290
William Lallemand723b73a2012-02-08 16:37:49 +0100291/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100292 * callback used to configure addr source retrieval
293 */
294int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
295{
296 curproxy->options2 |= PR_O2_SRC_ADDR;
297
298 return 0;
299}
300
301
302/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100303 * Parse args in a logformat_var. Returns 0 in error
304 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100305 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100306int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100307{
308 int i = 0;
309 int end = 0;
310 int flags = 0; // 1 = + 2 = -
311 char *sp = NULL; // start pointer
312
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100313 if (args == NULL) {
314 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100315 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100316 }
William Lallemand723b73a2012-02-08 16:37:49 +0100317
318 while (1) {
319 if (*args == '\0')
320 end = 1;
321
322 if (*args == '+') {
323 // add flag
324 sp = args + 1;
325 flags = 1;
326 }
327 if (*args == '-') {
328 // delete flag
329 sp = args + 1;
330 flags = 2;
331 }
332
333 if (*args == '\0' || *args == ',') {
334 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100335 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100336 if (strcmp(sp, var_args_list[i].name) == 0) {
337 if (flags == 1) {
338 node->options |= var_args_list[i].mask;
339 break;
340 } else if (flags == 2) {
341 node->options &= ~var_args_list[i].mask;
342 break;
343 }
344 }
345 }
346 sp = NULL;
347 if (end)
348 break;
349 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100350 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100351 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100352 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100353}
354
355/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100356 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
357 * must pass the args part in the <arg> pointer with its length in <arg_len>,
358 * and varname with its length in <var> and <var_len> respectively. <arg> is
359 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100360 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100361 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100362int 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 +0100363{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100364 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200365 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100366
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100367 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
368 if (strlen(logformat_keywords[j].name) == var_len &&
369 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
370 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200371 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100372 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100373 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200374 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100375 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100376 node->type = logformat_keywords[j].type;
377 node->options = *defoptions;
378 if (arg_len) {
379 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100380 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200381 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100382 }
383 if (node->type == LOG_FMT_GLOBAL) {
384 *defoptions = node->options;
385 free(node->arg);
386 free(node);
387 } else {
388 if (logformat_keywords[j].config_callback &&
389 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200390 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100391 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100392 curproxy->to_log |= logformat_keywords[j].lw;
393 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100394 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100395 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100396 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
397 curproxy->conf.args.file, curproxy->conf.args.line,
398 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100399 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100400 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100401 memprintf(err, "format variable '%s' is reserved for HTTP mode",
402 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200403 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100404 }
William Lallemand723b73a2012-02-08 16:37:49 +0100405 }
406 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100407
408 j = var[var_len];
409 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100410 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 +0100411 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200412
413 error_free:
414 if (node) {
415 free(node->arg);
416 free(node);
417 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100418 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100419}
420
421/*
422 * push to the logformat linked list
423 *
424 * start: start pointer
425 * end: end text pointer
426 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100427 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100428 *
429 * LOG_TEXT: copy chars from start to end excluding end.
430 *
431*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100432int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100433{
434 char *str;
435
Willy Tarreaua3571662012-12-20 21:59:12 +0100436 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200437 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100438 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100439 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100440 return 0;
441 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200442 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100443 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100444 str[end - start] = '\0';
445 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100446 node->type = LOG_FMT_TEXT; // type string
447 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100448 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200449 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100450 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100451 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100452 return 0;
453 }
William Lallemand1d705562012-03-12 12:46:41 +0100454 node->type = LOG_FMT_SEPARATOR;
455 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100456 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100457 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100458}
459
460/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100461 * Parse the sample fetch expression <text> and add a node to <list_format> upon
462 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100463 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
464 * is passed in <endptr>, it will be updated with the pointer to the first character
465 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100466 *
467 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100468 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100469int 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 +0100470{
471 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200472 struct sample_expr *expr = NULL;
473 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100474 int cmd_arg;
475
476 cmd[0] = text;
477 cmd[1] = "";
478 cmd_arg = 0;
479
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100480 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 +0100481 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100482 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200483 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100484 }
485
Vincent Bernat02779b62016-04-03 13:48:43 +0200486 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100487 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100488 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200489 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100490 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100491 node->type = LOG_FMT_EXPR;
492 node->expr = expr;
493 node->options = options;
494
495 if (arg_len) {
496 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100497 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200498 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100499 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100500 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100501 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
502
Willy Tarreau434c57c2013-01-08 01:10:24 +0100503 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100504 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
505
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100506 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100507 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
508 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200509 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100510 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100511
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200512 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100513 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100514 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100515
William Lallemand65ad6e12014-01-31 15:08:02 +0100516 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
517 * needed with some sample fetches (eg: ssl*). We always set it for
518 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100519 */
520 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200521 if (curpx->http_needed)
522 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100523 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100524 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200525
526 error_free:
527 release_sample_expr(expr);
528 if (node) {
529 free(node->arg);
530 free(node);
531 }
532 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100533}
534
535/*
William Lallemand723b73a2012-02-08 16:37:49 +0100536 * Parse the log_format string and fill a linked list.
537 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200538 * You can set arguments using { } : %{many arguments}varname.
539 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100540 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500541 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100542 * curproxy: the proxy affected
543 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100544 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100545 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100546 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100547 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100548 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100549int 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 +0100550{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100551 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100552 char *arg = NULL; /* start pointer for args */
553 char *var = NULL; /* start pointer for vars */
554 int arg_len = 0;
555 int var_len = 0;
556 int cformat; /* current token format */
557 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100558 struct logformat_node *tmplf, *back;
559
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100560 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100561 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100562 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100563 return 0;
564 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200565 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200566
William Lallemand723b73a2012-02-08 16:37:49 +0100567 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100568 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100569 LIST_DEL(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200570 release_sample_expr(tmplf->expr);
571 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100572 free(tmplf);
573 }
574
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100575 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100576 pformat = cformat;
577
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100578 if (!*str)
579 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100580
Joseph Herlant85b40592018-11-15 12:10:04 -0800581 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100582 * second have all common paths processed at one place. The common paths are the ones
583 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
584 * We use the common LF_INIT state to dispatch to the different final states.
585 */
586 switch (pformat) {
587 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100588 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100589 arg_len = var_len = 0;
590 if (*str == '{') { // optional argument
591 cformat = LF_STARG;
592 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100593 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100594 else if (*str == '[') {
595 cformat = LF_STEXPR;
596 var = str + 1; // store expr in variable name
597 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100598 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100599 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100600 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100601 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100602 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500603 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100604 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100605 /* single '%' followed by blank or digit, send them both */
606 cformat = LF_TEXT;
607 pformat = LF_TEXT; /* finally we include the previous char as well */
608 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600609 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 +0100610 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100611 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100612
613 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100614 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500615 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100616 break;
617
618 case LF_STARG: // text immediately following '%{'
619 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100620 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100621 arg_len = str - arg;
622 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100623 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100624 break;
625
626 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100627 if (*str == '[') {
628 cformat = LF_STEXPR;
629 var = str + 1; // store expr in variable name
630 break;
631 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100632 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100633 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100634 var = str;
635 break;
636 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100637 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100638 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100639
Willy Tarreauc8368452012-12-21 00:09:23 +0100640 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100641 /* the whole sample expression is parsed at once,
642 * returning the pointer to the first character not
643 * part of the expression, which MUST be the trailing
644 * angle bracket.
645 */
646 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
647 goto fail;
648
649 if (*str == ']') {
650 // end of arg, go on with next state
651 cformat = pformat = LF_EDEXPR;
652 sp = str;
653 }
654 else {
655 char c = *str;
656 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100657 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100658 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
659 else
660 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200661 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100662 }
663 break;
664
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100665 case LF_VAR: // text part of a variable name
666 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100667 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100668 cformat = LF_INIT; // not variable name anymore
669 break;
670
Willy Tarreauc8368452012-12-21 00:09:23 +0100671 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100672 cformat = LF_INIT;
673 }
674
675 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
676 switch (*str) {
677 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100678 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200679 case ' ':
680 if (options & LOG_OPT_MERGE_SPACES) {
681 cformat = LF_SEPARATOR;
682 break;
683 }
684 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100685 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100686 }
687 }
688
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100689 if (cformat != pformat || pformat == LF_SEPARATOR) {
690 switch (pformat) {
691 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100692 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100693 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100694 break;
695 case LF_TEXT:
696 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100697 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100698 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100699 break;
700 }
701 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100702 }
703 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100704
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100705 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100706 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100707 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100708 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100709 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100710
711 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100712 fail:
713 free(backfmt);
714 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100715}
716
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200717/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500718 * 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 +0200719 * ranges of indexes. Note that an index may be considered as a particular range
720 * with a high limit to the low limit.
721 */
722int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
723{
724 char *end, *p;
725
726 *low = *high = 0;
727
728 p = *arg;
729 end = strchr(p, ',');
730 if (!end)
731 end = p + strlen(p);
732
733 *high = *low = read_uint((const char **)&p, end);
734 if (!*low || (p != end && *p != '-'))
735 goto err;
736
737 if (p == end)
738 goto done;
739
740 p++;
741 *high = read_uint((const char **)&p, end);
742 if (!*high || *high <= *low || p != end)
743 goto err;
744
745 done:
746 if (*end == ',')
747 end++;
748 *arg = end;
749 return 1;
750
751 err:
752 memprintf(err, "wrong sample range '%s'", *arg);
753 return 0;
754}
755
756/*
757 * Returns 1 if the range defined by <low> and <high> overlaps
758 * one of them in <rgs> array of ranges with <sz> the size of this
759 * array, 0 if not.
760 */
761int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
762 unsigned int low, unsigned int high, char **err)
763{
764 size_t i;
765
766 for (i = 0; i < sz; i++) {
767 if ((low >= rgs[i].low && low <= rgs[i].high) ||
768 (high >= rgs[i].low && high <= rgs[i].high)) {
769 memprintf(err, "ranges are overlapping");
770 return 1;
771 }
772 }
773
774 return 0;
775}
776
777int smp_log_range_cmp(const void *a, const void *b)
778{
779 const struct smp_log_range *rg_a = a;
780 const struct smp_log_range *rg_b = b;
781
782 if (rg_a->high < rg_b->low)
783 return -1;
784 else if (rg_a->low > rg_b->high)
785 return 1;
786
787 return 0;
788}
789
790/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200791 * Parse "log" keyword and update <logsrvs> list accordingly.
792 *
793 * When <do_del> is set, it means the "no log" line was parsed, so all log
794 * servers in <logsrvs> are released.
795 *
796 * Otherwise, we try to parse the "log" line. First of all, when the list is not
797 * the global one, we look for the parameter "global". If we find it,
798 * global.logsrvs is copied. Else we parse each arguments.
799 *
800 * The function returns 1 in success case, otherwise, it returns 0 and err is
801 * filled.
802 */
803int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
804{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100805 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200806 struct sockaddr_storage *sk;
807 struct logsrv *logsrv = NULL;
808 int port1, port2;
809 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200810 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200811
812 /*
813 * "no log": delete previous herited or defined syslog
814 * servers.
815 */
816 if (do_del) {
817 struct logsrv *back;
818
819 if (*(args[1]) != 0) {
820 memprintf(err, "'no log' does not expect arguments");
821 goto error;
822 }
823
824 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
825 LIST_DEL(&logsrv->list);
826 free(logsrv);
827 }
828 return 1;
829 }
830
831 /*
832 * "log global": copy global.logrsvs linked list to the end of logsrvs
833 * list. But first, we check (logsrvs != global.logsrvs).
834 */
835 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
836 if (logsrvs == &global.logsrvs) {
837 memprintf(err, "'global' is not supported for a global syslog server");
838 goto error;
839 }
840 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200841 struct logsrv *node;
842
843 list_for_each_entry(node, logsrvs, list) {
844 if (node->ref == logsrv)
845 goto skip_logsrv;
846 }
847
848 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200849 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200850 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200851 LIST_INIT(&node->list);
852 LIST_ADDQ(logsrvs, &node->list);
Christopher Faulet28ac0992018-03-26 16:09:19 +0200853
854 skip_logsrv:
855 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200856 }
857 return 1;
858 }
859
860 /*
861 * "log <address> ...: parse a syslog server line
862 */
863 if (*(args[1]) == 0 || *(args[2]) == 0) {
864 memprintf(err, "expects <address> and <facility> %s as arguments",
865 ((logsrvs == &global.logsrvs) ? "" : "or global"));
866 goto error;
867 }
868
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100869 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
870 if (strcmp(args[1], "stdout") == 0)
871 args[1] = "fd@1";
872 else if (strcmp(args[1], "stderr") == 0)
873 args[1] = "fd@2";
874
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200875 logsrv = calloc(1, sizeof(*logsrv));
876 if (!logsrv) {
877 memprintf(err, "out of memory");
878 goto error;
879 }
880
881 /* skip address for now, it will be parsed at the end */
882 cur_arg = 2;
883
884 /* just after the address, a length may be specified */
885 logsrv->maxlen = MAX_SYSLOG_LEN;
886 if (strcmp(args[cur_arg], "len") == 0) {
887 int len = atoi(args[cur_arg+1]);
888 if (len < 80 || len > 65535) {
889 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
890 args[cur_arg+1]);
891 goto error;
892 }
893 logsrv->maxlen = len;
894 cur_arg += 2;
895 }
896 if (logsrv->maxlen > global.max_syslog_len)
897 global.max_syslog_len = logsrv->maxlen;
898
899 /* after the length, a format may be specified */
900 if (strcmp(args[cur_arg], "format") == 0) {
901 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200902 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200903 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
904 goto error;
905 }
906 cur_arg += 2;
907 }
908
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200909 if (strcmp(args[cur_arg], "sample") == 0) {
910 unsigned low, high;
911 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200912 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
913
914 p = args[cur_arg+1];
915 smp_sz_str = strchr(p, ':');
916 if (!smp_sz_str) {
917 memprintf(err, "Missing sample size");
918 goto error;
919 }
920
921 *smp_sz_str++ = '\0';
922
923 end = p + strlen(p);
924
925 while (p != end) {
926 if (!get_logsrv_smp_range(&low, &high, &p, err))
927 goto error;
928
929 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
930 goto error;
931
932 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
933 if (!smp_rgs) {
934 memprintf(err, "out of memory error");
935 goto error;
936 }
937
938 smp_rgs[smp_rgs_sz].low = low;
939 smp_rgs[smp_rgs_sz].high = high;
940 smp_rgs[smp_rgs_sz].sz = high - low + 1;
941 smp_rgs[smp_rgs_sz].curr_idx = 0;
942 if (smp_rgs[smp_rgs_sz].high > smp_sz)
943 smp_sz = smp_rgs[smp_rgs_sz].high;
944 smp_rgs_sz++;
945 }
946
Tim Duesterhus21648002019-06-23 22:10:10 +0200947 if (smp_rgs == NULL) {
948 memprintf(err, "no sampling ranges given");
949 goto error;
950 }
951
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200952 beg = smp_sz_str;
953 end = beg + strlen(beg);
954 new_smp_sz = read_uint((const char **)&beg, end);
955 if (!new_smp_sz || beg != end) {
956 memprintf(err, "wrong sample size '%s' for sample range '%s'",
957 smp_sz_str, args[cur_arg+1]);
958 goto error;
959 }
960
961 if (new_smp_sz < smp_sz) {
962 memprintf(err, "sample size %zu should be greater or equal to "
963 "%zu the maximum of the high ranges limits",
964 new_smp_sz, smp_sz);
965 goto error;
966 }
967 smp_sz = new_smp_sz;
968
969 /* Let's order <smp_rgs> array. */
970 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
971
972 logsrv->lb.smp_rgs = smp_rgs;
973 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
974 logsrv->lb.smp_sz = smp_sz;
975
976 cur_arg += 2;
977 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200978 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200979 /* parse the facility */
980 logsrv->facility = get_log_facility(args[cur_arg]);
981 if (logsrv->facility < 0) {
982 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
983 goto error;
984 }
985 cur_arg++;
986
987 /* parse the max syslog level (default: debug) */
988 logsrv->level = 7;
989 if (*(args[cur_arg])) {
990 logsrv->level = get_log_level(args[cur_arg]);
991 if (logsrv->level < 0) {
992 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
993 goto error;
994 }
995 cur_arg++;
996 }
997
998 /* parse the limit syslog level (default: emerg) */
999 logsrv->minlvl = 0;
1000 if (*(args[cur_arg])) {
1001 logsrv->minlvl = get_log_level(args[cur_arg]);
1002 if (logsrv->minlvl < 0) {
1003 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1004 goto error;
1005 }
1006 cur_arg++;
1007 }
1008
1009 /* Too many args */
1010 if (*(args[cur_arg])) {
1011 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1012 goto error;
1013 }
1014
1015 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001016 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001017 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001018 logsrv->addr.ss_family = AF_UNSPEC;
1019 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001020 logsrv->sink = NULL;
1021 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001022 goto done;
1023 }
1024
Willy Tarreau5fc93282020-09-16 18:25:03 +02001025 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, NULL,
Willy Tarreaua93e5c72020-09-15 14:01:16 +02001026 err, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001027 if (!sk)
1028 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001029
1030 if (fd != -1)
1031 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001032 logsrv->addr = *sk;
1033
1034 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001035 logsrv->addr = *sk;
1036 if (!port1)
1037 set_host_port(&logsrv->addr, SYSLOG_PORT);
1038 }
Willy Tarreauc046d162019-08-30 15:24:59 +02001039 done:
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001040 LIST_ADDQ(logsrvs, &logsrv->list);
1041 return 1;
1042
1043 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001044 free(smp_rgs);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001045 if (logsrv)
1046 free(logsrv->ring_name);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001047 free(logsrv);
1048 return 0;
1049}
1050
1051
Christopher Fauletd4696382017-10-24 11:44:05 +02001052/* Generic function to display messages prefixed by a label */
1053static void print_message(const char *label, const char *fmt, va_list argp)
1054{
1055 struct tm tm;
1056 char *head, *msg;
1057
1058 head = msg = NULL;
1059
1060 get_localtime(date.tv_sec, &tm);
1061 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
1062 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
1063 memvprintf(&msg, fmt, argp);
1064
Willy Tarreau869efd52019-11-15 15:16:57 +01001065 if (global.mode & MODE_STARTING) {
1066 if (unlikely(!startup_logs))
1067 startup_logs = ring_new(STARTUP_LOG_SIZE);
1068
1069 if (likely(startup_logs)) {
1070 struct ist m[2];
1071
1072 m[0] = ist(head);
1073 m[1] = ist(msg);
1074 /* trim the trailing '\n' */
1075 if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
1076 m[1].len--;
1077 ring_write(startup_logs, ~0, 0, 0, m, 2);
1078 }
1079 }
Christopher Fauletd4696382017-10-24 11:44:05 +02001080
1081 fprintf(stderr, "%s%s", head, msg);
1082 fflush(stderr);
1083
1084 free(head);
1085 free(msg);
1086}
1087
Willy Tarreaubaaee002006-06-26 02:48:02 +02001088/*
1089 * Displays the message on stderr with the date and pid. Overrides the quiet
1090 * mode during startup.
1091 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001092void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001093{
1094 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001095
1096 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaubb869862020-04-16 10:52:41 +02001097 if (!(warned & WARN_EXEC_PATH)) {
1098 const char *path = get_exec_path();
1099
1100 warned |= WARN_EXEC_PATH;
1101 ha_notice("haproxy version is %s\n", haproxy_version);
1102 if (path)
1103 ha_notice("path to executable is %s\n", path);
1104 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001105 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001106 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001107 va_end(argp);
1108 }
1109}
1110
1111
1112/*
1113 * Displays the message on stderr with the date and pid.
1114 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001115void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001116{
1117 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001118
Willy Tarreaubebd2122020-04-15 16:06:11 +02001119 warned |= WARN_ANY;
1120
Willy Tarreaubaaee002006-06-26 02:48:02 +02001121 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1122 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001123 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001124 va_end(argp);
1125 }
1126}
1127
1128/*
William Lallemand9c56a222018-11-21 18:04:52 +01001129 * Displays the message on stderr with the date and pid.
1130 */
1131void ha_notice(const char *fmt, ...)
1132{
1133 va_list argp;
1134
1135 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1136 va_start(argp, fmt);
1137 print_message("NOTICE", fmt, argp);
1138 va_end(argp);
1139 }
1140}
1141
1142/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001143 * Displays the message on <out> only if quiet mode is not set.
1144 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001145void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001146{
1147 va_list argp;
1148
1149 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1150 va_start(argp, fmt);
1151 vfprintf(out, fmt, argp);
1152 fflush(out);
1153 va_end(argp);
1154 }
1155}
1156
1157/*
Emeric Brun54648852020-07-06 15:54:06 +02001158 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001159 */
Emeric Brun54648852020-07-06 15:54:06 +02001160enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001161{
Emeric Brun54648852020-07-06 15:54:06 +02001162 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001163
1164 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001165 while (format > 0 && log_formats[format].name
1166 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +02001167 format--;
1168
Emeric Brun54648852020-07-06 15:54:06 +02001169 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001170 return format;
1171}
1172
1173/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001174 * returns log level for <lev> or -1 if not found.
1175 */
1176int get_log_level(const char *lev)
1177{
1178 int level;
1179
1180 level = NB_LOG_LEVELS - 1;
1181 while (level >= 0 && strcmp(log_levels[level], lev))
1182 level--;
1183
1184 return level;
1185}
1186
Willy Tarreaubaaee002006-06-26 02:48:02 +02001187/*
1188 * returns log facility for <fac> or -1 if not found.
1189 */
1190int get_log_facility(const char *fac)
1191{
1192 int facility;
1193
1194 facility = NB_LOG_FACILITIES - 1;
1195 while (facility >= 0 && strcmp(log_facilities[facility], fac))
1196 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001197
Willy Tarreaubaaee002006-06-26 02:48:02 +02001198 return facility;
1199}
1200
William Lallemanda1cc3812012-02-08 16:38:44 +01001201/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001202 * Encode the string.
1203 *
1204 * When using the +E log format option, it will try to escape '"\]'
1205 * characters with '\' as prefix. The same prefix should not be used as
1206 * <escape>.
1207 */
1208static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001209 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001210 const char *string,
1211 struct logformat_node *node)
1212{
1213 if (node->options & LOG_OPT_ESC) {
1214 if (start < stop) {
1215 stop--; /* reserve one byte for the final '\0' */
1216 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001217 if (!ha_bit_test((unsigned char)(*string), map)) {
1218 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001219 *start++ = *string;
1220 else {
1221 if (start + 2 >= stop)
1222 break;
1223 *start++ = '\\';
1224 *start++ = *string;
1225 }
1226 }
1227 else {
1228 if (start + 3 >= stop)
1229 break;
1230 *start++ = escape;
1231 *start++ = hextab[(*string >> 4) & 15];
1232 *start++ = hextab[*string & 15];
1233 }
1234 string++;
1235 }
1236 *start = '\0';
1237 }
1238 }
1239 else {
1240 return encode_string(start, stop, escape, map, string);
1241 }
1242
1243 return start;
1244}
1245
1246/*
1247 * Encode the chunk.
1248 *
1249 * When using the +E log format option, it will try to escape '"\]'
1250 * characters with '\' as prefix. The same prefix should not be used as
1251 * <escape>.
1252 */
1253static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001254 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001255 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001256 struct logformat_node *node)
1257{
1258 char *str, *end;
1259
1260 if (node->options & LOG_OPT_ESC) {
1261 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001262 str = chunk->area;
1263 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001264
1265 stop--; /* reserve one byte for the final '\0' */
1266 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001267 if (!ha_bit_test((unsigned char)(*str), map)) {
1268 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001269 *start++ = *str;
1270 else {
1271 if (start + 2 >= stop)
1272 break;
1273 *start++ = '\\';
1274 *start++ = *str;
1275 }
1276 }
1277 else {
1278 if (start + 3 >= stop)
1279 break;
1280 *start++ = escape;
1281 *start++ = hextab[(*str >> 4) & 15];
1282 *start++ = hextab[*str & 15];
1283 }
1284 str++;
1285 }
1286 *start = '\0';
1287 }
1288 }
1289 else {
1290 return encode_chunk(start, stop, escape, map, chunk);
1291 }
1292
1293 return start;
1294}
1295
1296/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001297 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001298 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001299 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001300 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001301 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001302char *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 +01001303{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001304 if (size < 2)
1305 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001306
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001307 if (node->options & LOG_OPT_QUOTE) {
1308 *(dst++) = '"';
1309 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001310 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001311
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001312 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001313 if (++len > size)
1314 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001315 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001316 char *ret;
1317
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001318 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001319 if (ret == NULL || *ret != '\0')
1320 return NULL;
1321 len = ret - dst;
1322 }
1323 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001324 len = strlcpy2(dst, src, len);
1325 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001326
1327 size -= len;
1328 dst += len;
1329 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001330 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1331 if (size < 2)
1332 return NULL;
1333 *(dst++) = '-';
1334 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001335
1336 if (node->options & LOG_OPT_QUOTE) {
1337 if (size < 2)
1338 return NULL;
1339 *(dst++) = '"';
1340 }
1341
1342 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001343 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001344}
1345
Willy Tarreau26ffa852018-09-05 15:23:10 +02001346static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001347{
1348 return lf_text_len(dst, src, size, size, node);
1349}
1350
William Lallemand5f232402012-04-05 18:02:55 +02001351/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001352 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001353 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001354 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001355char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001356{
1357 char *ret = dst;
1358 int iret;
1359 char pn[INET6_ADDRSTRLEN];
1360
1361 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001362 unsigned char *addr = NULL;
1363 switch (sockaddr->sa_family) {
1364 case AF_INET:
1365 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1366 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1367 break;
1368 case AF_INET6:
1369 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1370 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1371 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1372 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1373 break;
1374 default:
1375 return NULL;
1376 }
William Lallemand5f232402012-04-05 18:02:55 +02001377 if (iret < 0 || iret > size)
1378 return NULL;
1379 ret += iret;
1380 } else {
1381 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1382 ret = lf_text(dst, pn, size, node);
1383 if (ret == NULL)
1384 return NULL;
1385 }
1386 return ret;
1387}
1388
1389/*
1390 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001391 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001392 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001393char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001394{
1395 char *ret = dst;
1396 int iret;
1397
1398 if (node->options & LOG_OPT_HEXA) {
1399 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1400 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1401 if (iret < 0 || iret > size)
1402 return NULL;
1403 ret += iret;
1404 } else {
1405 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1406 if (ret == NULL)
1407 return NULL;
1408 }
1409 return ret;
1410}
1411
Emeric Brun54648852020-07-06 15:54:06 +02001412
1413/*
1414 * This function sends the syslog message using a printf format string. It
1415 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001416 */
Emeric Brun54648852020-07-06 15:54:06 +02001417void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001418{
Emeric Brun54648852020-07-06 15:54:06 +02001419 va_list argp;
1420 int data_len;
1421
1422 if (level < 0 || format == NULL || logline == NULL)
1423 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001424
Emeric Brun54648852020-07-06 15:54:06 +02001425 va_start(argp, format);
1426 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1427 if (data_len < 0 || data_len > global.max_syslog_len)
1428 data_len = global.max_syslog_len;
1429 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001430
Emeric Brun54648852020-07-06 15:54:06 +02001431 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1432 logline, data_len, default_rfc5424_sd_log_format, 2);
1433}
1434/*
1435 * This function builds a log header of given format using given
1436 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1437 * to determine format based on given metadas. It is useful
1438 * for log-forwarding to be able to forward any format without
1439 * settings.
1440 * This function returns a struct ist array of elements of the header
1441 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001442 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001443 * elements.
1444 */
1445struct ist *build_log_header(enum log_fmt format, int level, int facility,
1446 struct ist *metadata, size_t *nbelem)
1447{
1448 static THREAD_LOCAL struct {
1449 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1450 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1451 time_t cur_legacy_time;
1452 char priority_buffer[6];
1453 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001454
Emeric Brun54648852020-07-06 15:54:06 +02001455 struct tm logtime;
1456 int len;
1457 int fac_level = 0;
1458 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001459
Emeric Brun54648852020-07-06 15:54:06 +02001460 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001461
Emeric Brun54648852020-07-06 15:54:06 +02001462
1463 if (format == LOG_FORMAT_UNSPEC) {
1464 format = LOG_FORMAT_RAW;
1465 if (metadata) {
1466 /* If a hostname is set, it appears we want to perform syslog
1467 * because only rfc5427 or rfc3164 support an hostname.
1468 */
1469 if (metadata[LOG_META_HOST].len) {
1470 /* If a rfc5424 compliant timestamp is used we consider
1471 * that output format is rfc5424, else legacy format
1472 * is used as specified default for local logs
1473 * in documentation.
1474 */
1475 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1476 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1477 format = LOG_FORMAT_RFC5424;
1478 else
1479 format = LOG_FORMAT_RFC3164;
1480 }
Emeric Bruna0338b92020-11-27 16:24:34 +01001481 else if (metadata[LOG_META_TAG].len) {
1482 /* Tag is present but no hostname, we should
1483 * consider we try to emmit a local log
1484 * in legacy format (analog to RFC3164 but
1485 * with stripped hostname).
1486 */
1487 format = LOG_FORMAT_LOCAL;
1488 }
Emeric Brun54648852020-07-06 15:54:06 +02001489 else if (metadata[LOG_META_PRIO].len) {
1490 /* the source seems a parsed message
1491 * offering a valid level/prio prefix
1492 * so we consider this format.
1493 */
1494 format = LOG_FORMAT_PRIO;
1495 }
1496 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001497 }
1498
Emeric Brun54648852020-07-06 15:54:06 +02001499 /* prepare priority, stored into 1 single elem */
1500 switch (format) {
Emeric Bruna0338b92020-11-27 16:24:34 +01001501 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001502 case LOG_FORMAT_RFC3164:
1503 case LOG_FORMAT_RFC5424:
1504 case LOG_FORMAT_PRIO:
1505 fac_level = facility << 3;
1506 /* further format ignore the facility */
1507 /* fall through */
1508 case LOG_FORMAT_TIMED:
1509 case LOG_FORMAT_SHORT:
1510 fac_level += level;
1511 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1512 do {
1513 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1514 fac_level /= 10;
1515 hdr_ctx.ist_vector[*nbelem].ptr--;
1516 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1517 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1518 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1519 break;
1520 case LOG_FORMAT_ISO:
1521 case LOG_FORMAT_RAW:
1522 break;
1523 case LOG_FORMAT_UNSPEC:
1524 case LOG_FORMATS:
1525 ABORT_NOW();
1526 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001527
William Lallemand2a4a44f2012-02-06 16:00:33 +01001528
Emeric Brun54648852020-07-06 15:54:06 +02001529 /* prepare timestamp, stored into a max of 4 elems */
1530 switch (format) {
Emeric Bruna0338b92020-11-27 16:24:34 +01001531 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001532 case LOG_FORMAT_RFC3164:
1533 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1534 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1535 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1536 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001537 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001538 break;
1539 }
1540 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1541 int month;
1542 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001543
Emeric Brun54648852020-07-06 15:54:06 +02001544 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001545
Emeric Brun54648852020-07-06 15:54:06 +02001546 /* compute month */
1547 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1548 if (month)
1549 month--;
1550 if (month <= 11) {
1551 /* builds log prefix ex: 'Jan 1 ' */
1552 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1553 "%s %c%c ", monthname[month],
1554 timestamp[8] != '0' ? timestamp[8] : ' ',
1555 timestamp[9]);
1556 /* we reused the timestamp_buffer, signal that it does not
1557 * contain local time anymore
1558 */
1559 hdr_ctx.cur_legacy_time = 0;
1560 if (len == 7) {
1561 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1562 /* adds 'HH:MM:SS' from iso time */
1563 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1564 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1565 /* we successfully reuse iso time, we can break */
1566 break;
1567 }
1568 }
1569 /* Failed to reuse isotime time, fallback to local legacy time */
1570 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001571
Emeric Brun54648852020-07-06 15:54:06 +02001572 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1573 /* re-builds timestamp from the current local time */
1574 get_localtime(time, &logtime);
1575
1576 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1577 "%s %2d %02d:%02d:%02d ",
1578 monthname[logtime.tm_mon],
1579 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1580 if (len != LOG_LEGACYTIME_LEN+1)
1581 hdr_ctx.cur_legacy_time = 0;
1582 else
1583 hdr_ctx.cur_legacy_time = time;
1584 }
1585 if (likely(hdr_ctx.cur_legacy_time))
1586 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1587 else
1588 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1589 break;
1590 case LOG_FORMAT_RFC5424:
1591 /* adds rfc5425 version prefix */
1592 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1593 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001594 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001595 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1596 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1597 break;
1598 }
1599 /* let continue as 'timed' and 'iso' format for usual timestamp */
1600 /* fall through */
1601 case LOG_FORMAT_TIMED:
1602 case LOG_FORMAT_ISO:
1603 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1604 * '1900:01:01T14:00:00+02:00'
1605 * '1900:01:01T10:00:00.123456-02:00'
1606 */
1607 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1608 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1609 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001610 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001611 break;
1612 }
1613 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1614 int month;
1615 char *timestamp = metadata[LOG_META_TIME].ptr;
1616
1617 for (month = 0; month < 12; month++)
1618 if (!memcmp(monthname[month], timestamp, 3))
1619 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001620
Emeric Brun54648852020-07-06 15:54:06 +02001621 if (month < 12) {
1622
1623 /* get local time to retrieve year */
1624 get_localtime(time, &logtime);
1625
1626 /* year seems changed since log */
1627 if (logtime.tm_mon < month)
1628 logtime.tm_year--;
1629
1630 /* builds rfc5424 prefix ex: '1900-01-01T' */
1631 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1632 "%4d-%02d-%c%cT",
1633 logtime.tm_year+1900, month+1,
1634 timestamp[4] != ' ' ? timestamp[4] : '0',
1635 timestamp[5]);
1636
1637 /* we reused the timestamp_buffer, signal that it does not
1638 * contain local time anymore
1639 */
1640 hdr_ctx.cur_legacy_time = 0;
1641 if (len == 11) {
1642 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1643 /* adds HH:MM:SS from legacy timestamp */
1644 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001645 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001646 /* according to rfc: -00:00 means we don't know the timezone */
1647 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1648 /* we successfully reuse legacy time, we can break */
1649 break;
1650 }
1651 }
1652 /* Failed to reuse legacy time, fallback to local iso time */
1653 }
1654 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1655 break;
1656 case LOG_FORMAT_PRIO:
1657 case LOG_FORMAT_SHORT:
1658 case LOG_FORMAT_RAW:
1659 break;
1660 case LOG_FORMAT_UNSPEC:
1661 case LOG_FORMATS:
1662 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001663 }
1664
Emeric Brun54648852020-07-06 15:54:06 +02001665 /* prepare other meta data, stored into a max of 10 elems */
1666 switch (format) {
1667 case LOG_FORMAT_RFC3164:
1668 if (metadata && metadata[LOG_META_HOST].len) {
1669 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1670 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1671 }
Emeric Bruna0338b92020-11-27 16:24:34 +01001672 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001673 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Bruna0338b92020-11-27 16:24:34 +01001674 /* fall through */
1675 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001676 if (!metadata || !metadata[LOG_META_TAG].len)
1677 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001678
Emeric Brun54648852020-07-06 15:54:06 +02001679 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1680 if (metadata[LOG_META_PID].len) {
1681 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1682 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1683 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1684 }
1685 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1686 break;
1687 case LOG_FORMAT_RFC5424:
1688 if (metadata && metadata[LOG_META_HOST].len) {
1689 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1690 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1691 }
1692 else
1693 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001694
Emeric Brun54648852020-07-06 15:54:06 +02001695 if (metadata && metadata[LOG_META_TAG].len) {
1696 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1697 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1698 }
1699 else
1700 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001701
Emeric Brun54648852020-07-06 15:54:06 +02001702 if (metadata && metadata[LOG_META_PID].len) {
1703 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1704 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1705 }
1706 else
1707 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001708
Emeric Brun54648852020-07-06 15:54:06 +02001709 if (metadata && metadata[LOG_META_MSGID].len) {
1710 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1711 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1712 }
1713 else
1714 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001715
Emeric Brun54648852020-07-06 15:54:06 +02001716 if (metadata && metadata[LOG_META_STDATA].len) {
1717 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1718 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1719 }
1720 else
1721 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1722 break;
1723 case LOG_FORMAT_PRIO:
1724 case LOG_FORMAT_SHORT:
1725 case LOG_FORMAT_TIMED:
1726 case LOG_FORMAT_ISO:
1727 case LOG_FORMAT_RAW:
1728 break;
1729 case LOG_FORMAT_UNSPEC:
1730 case LOG_FORMATS:
1731 ABORT_NOW();
1732 }
1733
1734 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001735}
1736
1737/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001738 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001739 * The argument <metadata> MUST be an array of size
1740 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001741 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001742 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001743 */
Emeric Brun54648852020-07-06 15:54:06 +02001744static 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 +01001745{
Emeric Brun54648852020-07-06 15:54:06 +02001746 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001747 static THREAD_LOCAL struct msghdr msghdr = {
1748 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001749 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001750 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001751 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1752 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001753 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001754 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001755 size_t nbelem;
1756 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001757
1758 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001759
Emeric Brunfa9d7802020-05-28 14:21:33 +02001760 /* historically some messages used to already contain the trailing LF
1761 * or Zero. Let's remove all trailing LF or Zero
1762 */
Emeric Brun54648852020-07-06 15:54:06 +02001763 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001764 size--;
1765
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001766 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001767 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001768 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001769 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001770 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1771 /* the socket's address is a file descriptor */
1772 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1773 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001774 else if (logsrv->addr.ss_family == AF_UNIX)
1775 plogfd = &logfdunix;
1776 else
1777 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001778
Willy Tarreauc046d162019-08-30 15:24:59 +02001779 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001780 /* socket not successfully initialized yet */
1781 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1782 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1783 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001784
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001785 if (!once) {
1786 once = 1; /* note: no need for atomic ops here */
1787 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1788 nblogger, strerror(errno), errno);
1789 }
1790 return;
1791 } else {
1792 /* we don't want to receive anything on this socket */
1793 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1794 /* does nothing under Linux, maybe needed for others */
1795 shutdown(*plogfd, SHUT_RD);
1796 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1797 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001798 }
1799
Emeric Brun54648852020-07-06 15:54:06 +02001800 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1801 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001802 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001803 struct ist msg;
1804
1805 msg = ist2(message, size);
1806 if (msg.len > logsrv->maxlen)
1807 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001808
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001809 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1810 }
1811 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1812 struct ist msg;
1813
1814 msg = ist2(message, size);
1815 if (msg.len > logsrv->maxlen)
1816 msg.len = logsrv->maxlen;
1817
1818 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001819 }
1820 else {
Emeric Brun54648852020-07-06 15:54:06 +02001821 int i = 0;
1822 int totlen = logsrv->maxlen;
1823
1824 for (i = 0 ; i < nbelem ; i++ ) {
1825 iovec[i].iov_base = msg_header[i].ptr;
1826 iovec[i].iov_len = msg_header[i].len;
1827 if (totlen <= iovec[i].iov_len) {
1828 iovec[i].iov_len = totlen;
1829 totlen = 0;
1830 break;
1831 }
1832 totlen -= iovec[i].iov_len;
1833 }
1834 if (totlen) {
1835 iovec[i].iov_base = message;
1836 iovec[i].iov_len = size;
1837 if (totlen <= iovec[i].iov_len)
1838 iovec[i].iov_len = totlen;
1839 i++;
1840 }
1841 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1842 iovec[i].iov_len = 1;
1843 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001844
Emeric Brun54648852020-07-06 15:54:06 +02001845 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001846 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1847 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001848
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001849 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1850 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001851
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001852 if (sent < 0) {
1853 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001854
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001855 if (errno == EAGAIN)
1856 _HA_ATOMIC_ADD(&dropped_logs, 1);
1857 else if (!once) {
1858 once = 1; /* note: no need for atomic ops here */
1859 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1860 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001861 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001862 }
1863}
Dragan Dosen59cee972015-09-19 22:09:02 +02001864
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001865/*
1866 * This function sends a syslog message.
1867 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001868 * The argument <metadata> MUST be an array of size
1869 * LOG_META_FIELDS*sizeof(struct ist) containing
1870 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001871 */
Emeric Brun54648852020-07-06 15:54:06 +02001872void process_send_log(struct list *logsrvs, int level, int facility,
1873 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001874{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001875 struct logsrv *logsrv;
1876 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001877
1878 /* Send log messages to syslog server. */
1879 nblogger = 0;
1880 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001881 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001882
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001883 /* we can filter the level of the messages that are sent to each logger */
1884 if (level > logsrv->level)
1885 continue;
1886
Frédéric Lécailled803e472019-04-25 07:42:09 +02001887 if (logsrv->lb.smp_rgs) {
1888 struct smp_log_range *curr_rg;
1889
1890 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1891 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1892 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1893 if (in_range) {
1894 /* Let's consume this range. */
1895 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1896 if (!curr_rg->curr_idx) {
1897 /* If consumed, let's select the next range. */
1898 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1899 }
1900 }
1901 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1902 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1903 }
1904 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001905 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1906 (facility == -1) ? logsrv->facility : facility,
1907 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001908 }
1909}
1910
Emeric Brun54648852020-07-06 15:54:06 +02001911/*
1912 * This function sends a syslog message.
1913 * It doesn't care about errors nor does it report them.
1914 * The arguments <sd> and <sd_size> are used for the structured-data part
1915 * in RFC5424 formatted syslog messages.
1916 */
1917void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1918 char *message, size_t size, char *sd, size_t sd_size)
1919{
1920 static THREAD_LOCAL pid_t curr_pid;
1921 static THREAD_LOCAL char pidstr[16];
1922 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1923
1924 if (logsrvs == NULL) {
1925 if (!LIST_ISEMPTY(&global.logsrvs)) {
1926 logsrvs = &global.logsrvs;
1927 }
1928 }
1929 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1930 return;
1931
1932 if (!metadata[LOG_META_HOST].len) {
1933 if (global.log_send_hostname)
1934 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02001935 }
1936
1937 if (!tagb || !tagb->area)
1938 tagb = &global.log_tag;
1939
1940 if (tagb)
1941 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1942
1943 if (unlikely(curr_pid != getpid()))
1944 metadata[LOG_META_PID].len = 0;
1945
1946 if (!metadata[LOG_META_PID].len) {
1947 curr_pid = getpid();
1948 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1949 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1950 }
1951
1952 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1953
1954 /* Remove trailing space of structured data */
1955 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1956 metadata[LOG_META_STDATA].len--;
1957
1958 return process_send_log(logsrvs, level, -1, metadata, message, size);
1959}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001960
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001961const 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 +01001962const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1963 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1964 Set-cookie Updated, unknown, unknown */
1965
William Lallemand1d705562012-03-12 12:46:41 +01001966/*
1967 * try to write a character if there is enough space, or goto out
1968 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001969#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001970 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001971 *(tmplog++) = (x); \
1972 } else { \
1973 goto out; \
1974 } \
1975 } while(0)
1976
Dragan Dosen835b9212016-02-12 13:23:03 +01001977
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001978/* Initializes some log data at boot */
1979static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001980{
1981 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001982 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001983
1984 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1985 * inside PARAM-VALUE should be escaped with '\' as prefix.
1986 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1987 * details.
1988 */
1989 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1990
1991 tmp = "\"\\]";
1992 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001993 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001994 tmp++;
1995 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001996
1997 /* initialize the log header encoding map : '{|}"#' should be encoded with
1998 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1999 * URL encoding only requires '"', '#' to be encoded as well as non-
2000 * printable characters above.
2001 */
2002 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
2003 memset(url_encode_map, 0, sizeof(url_encode_map));
2004 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002005 ha_bit_set(i, hdr_encode_map);
2006 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002007 }
2008 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002009 ha_bit_set(i, hdr_encode_map);
2010 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002011 }
2012
2013 tmp = "\"#{|}";
2014 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002015 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002016 tmp++;
2017 }
2018
2019 tmp = "\"#";
2020 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002021 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002022 tmp++;
2023 }
2024
2025 /* initialize the http header encoding map. The draft httpbis define the
2026 * header content as:
2027 *
2028 * HTTP-message = start-line
2029 * *( header-field CRLF )
2030 * CRLF
2031 * [ message-body ]
2032 * header-field = field-name ":" OWS field-value OWS
2033 * field-value = *( field-content / obs-fold )
2034 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2035 * obs-fold = CRLF 1*( SP / HTAB )
2036 * field-vchar = VCHAR / obs-text
2037 * VCHAR = %x21-7E
2038 * obs-text = %x80-FF
2039 *
2040 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
2041 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08002042 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02002043 */
2044 memset(http_encode_map, 0, sizeof(http_encode_map));
2045 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002046 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002047 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002048 ha_bit_set(i, http_encode_map);
2049 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002050}
William Lallemand1d705562012-03-12 12:46:41 +01002051
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002052INITCALL0(STG_PREPARE, init_log);
2053
Christopher Faulet0132d062017-07-26 15:33:35 +02002054/* Initialize log buffers used for syslog messages */
2055int init_log_buffers()
2056{
Christopher Faulet0132d062017-07-26 15:33:35 +02002057 logline = my_realloc2(logline, global.max_syslog_len + 1);
2058 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02002059 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02002060 return 0;
2061 return 1;
2062}
2063
2064/* Deinitialize log buffers used for syslog messages */
2065void deinit_log_buffers()
2066{
Christopher Faulet0132d062017-07-26 15:33:35 +02002067 free(logline);
2068 free(logline_rfc5424);
Willy Tarreau869efd52019-11-15 15:16:57 +01002069 ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
Christopher Faulet0132d062017-07-26 15:33:35 +02002070 logline = NULL;
2071 logline_rfc5424 = NULL;
2072}
2073
Willy Tarreaudf974472012-12-28 02:44:01 +01002074/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2075 * <maxsize> characters. Returns the size of the output string in characters,
2076 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002077 * is not zero. It requires a valid session and optionally a stream. If the
2078 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002079 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002080int 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 +02002081{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002082 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002083 struct proxy *be;
2084 struct http_txn *txn;
2085 const struct strm_logs *logs;
Willy Tarreau8fa99842019-07-17 11:47:11 +02002086 struct connection *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002087 unsigned int s_flags;
2088 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002089 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002090 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002091 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002092 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002093 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002094 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002095 int t_request;
2096 int hdr;
2097 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002098 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002099 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002100 char *ret;
2101 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002102 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002103 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002104 struct strm_logs tmp_strm_log;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002105
William Lallemandbddd4fd2012-02-27 11:23:10 +01002106 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002107
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002108 if (likely(s)) {
2109 be = s->be;
2110 txn = s->txn;
2111 be_conn = cs_conn(objt_cs(s->si[1].end));
2112 s_flags = s->flags;
2113 uniq_id = s->uniq_id;
2114 logs = &s->logs;
2115 } else {
2116 /* we have no stream so we first need to initialize a few
2117 * things that are needed later. We do increment the request
2118 * ID so that it's uniquely assigned to this request just as
2119 * if the request had reached the point of being processed.
2120 * A request error is reported as it's the only element we have
2121 * here and which justifies emitting such a log.
2122 */
2123 be = fe;
2124 txn = NULL;
2125 be_conn = NULL;
2126 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01002127 uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002128
2129 /* prepare a valid log structure */
2130 tmp_strm_log.tv_accept = sess->tv_accept;
2131 tmp_strm_log.accept_date = sess->accept_date;
2132 tmp_strm_log.t_handshake = sess->t_handshake;
2133 tmp_strm_log.t_idle = tv_ms_elapsed(&sess->tv_accept, &now) - sess->t_handshake;
2134 tv_zero(&tmp_strm_log.tv_request);
2135 tmp_strm_log.t_queue = -1;
2136 tmp_strm_log.t_connect = -1;
2137 tmp_strm_log.t_data = -1;
2138 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2139 tmp_strm_log.bytes_in = 0;
2140 tmp_strm_log.bytes_out = 0;
2141 tmp_strm_log.prx_queue_pos = 0;
2142 tmp_strm_log.srv_queue_pos = 0;
2143
2144 logs = &tmp_strm_log;
2145 }
2146
William Lallemandbddd4fd2012-02-27 11:23:10 +01002147 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002148 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2149 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002150
William Lallemand1d705562012-03-12 12:46:41 +01002151 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002152
William Lallemandbddd4fd2012-02-27 11:23:10 +01002153 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002154 if (LIST_ISEMPTY(list_format))
2155 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002156
William Lallemand1d705562012-03-12 12:46:41 +01002157 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002158 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002159 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002160 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002161 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002162
Willy Tarreauc8368452012-12-21 00:09:23 +01002163 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002164 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002165 if (!last_isspace) {
2166 LOGCHAR(' ');
2167 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002168 }
2169 break;
2170
William Lallemand1d705562012-03-12 12:46:41 +01002171 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002172 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002173 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002174 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002175 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002176 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002177 last_isspace = 0;
2178 break;
2179
Willy Tarreauc8368452012-12-21 00:09:23 +01002180 case LOG_FMT_EXPR: // sample expression, may be request or response
2181 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002182 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002183 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 +02002184 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002185 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 +01002186 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002187 ret = lf_encode_chunk(tmplog, dst + maxsize,
2188 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002189 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002190 ret = lf_text_len(tmplog,
2191 key ? key->data.u.str.area : NULL,
2192 key ? key->data.u.str.data : 0,
2193 dst + maxsize - tmplog,
2194 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002195 if (ret == 0)
2196 goto out;
2197 tmplog = ret;
2198 last_isspace = 0;
2199 break;
2200
Willy Tarreau2beef582012-12-20 17:22:52 +01002201 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002202 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002203 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002204 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002205 else
2206 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002207 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002208 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002209 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002210 last_isspace = 0;
2211 break;
2212
Willy Tarreau2beef582012-12-20 17:22:52 +01002213 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002214 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002215 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002216 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002217 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002218 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002219 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002220 dst + maxsize - tmplog, tmp);
2221 }
William Lallemand5f232402012-04-05 18:02:55 +02002222 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002223 else
2224 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2225
William Lallemand5f232402012-04-05 18:02:55 +02002226 if (ret == NULL)
2227 goto out;
2228 tmplog = ret;
2229 last_isspace = 0;
2230 break;
2231
Willy Tarreau2beef582012-12-20 17:22:52 +01002232 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002233 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002234 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002235 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002236 }
2237 else
2238 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2239
William Lallemand1d705562012-03-12 12:46:41 +01002240 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002241 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002242 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002243 last_isspace = 0;
2244 break;
2245
Willy Tarreau2beef582012-12-20 17:22:52 +01002246 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002247 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002248 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002249 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002250 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002251 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002252 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002253 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002254 else
2255 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2256
William Lallemand5f232402012-04-05 18:02:55 +02002257 if (ret == NULL)
2258 goto out;
2259 tmplog = ret;
2260 last_isspace = 0;
2261 break;
2262
Willy Tarreau2beef582012-12-20 17:22:52 +01002263 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002264 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002265 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002266 else
2267 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2268
William Lallemand1d705562012-03-12 12:46:41 +01002269 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002270 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002271 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002272 last_isspace = 0;
2273 break;
2274
Willy Tarreau2beef582012-12-20 17:22:52 +01002275 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002276 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002277 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002278 else
2279 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2280
William Lallemand5f232402012-04-05 18:02:55 +02002281 if (ret == NULL)
2282 goto out;
2283 tmplog = ret;
2284 last_isspace = 0;
2285 break;
2286
Willy Tarreau2beef582012-12-20 17:22:52 +01002287 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002288 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002289 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002290 else
2291 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2292
William Lallemand5f232402012-04-05 18:02:55 +02002293 if (ret == NULL)
2294 goto out;
2295 tmplog = ret;
2296 last_isspace = 0;
2297 break;
2298
Willy Tarreau2beef582012-12-20 17:22:52 +01002299 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002300 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002301 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002302 else
2303 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2304
William Lallemand1d705562012-03-12 12:46:41 +01002305 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002306 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002307 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002308 last_isspace = 0;
2309 break;
2310
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002311 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002312 get_localtime(logs->accept_date.tv_sec, &tm);
2313 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002314 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002315 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002316 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002317 last_isspace = 0;
2318 break;
2319
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002320 case LOG_FMT_tr: // %tr = start of request date
2321 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002322 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 +02002323 get_localtime(tv.tv_sec, &tm);
2324 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2325 if (ret == NULL)
2326 goto out;
2327 tmplog = ret;
2328 last_isspace = 0;
2329 break;
2330
2331 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002332 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002333 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002334 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002335 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002336 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002337 last_isspace = 0;
2338 break;
2339
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002340 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002341 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 +02002342 get_gmtime(tv.tv_sec, &tm);
2343 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2344 if (ret == NULL)
2345 goto out;
2346 tmplog = ret;
2347 last_isspace = 0;
2348 break;
2349
2350 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002351 get_localtime(logs->accept_date.tv_sec, &tm);
2352 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002353 if (ret == NULL)
2354 goto out;
2355 tmplog = ret;
2356 last_isspace = 0;
2357 break;
2358
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002359 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002360 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 +02002361 get_localtime(tv.tv_sec, &tm);
2362 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2363 if (ret == NULL)
2364 goto out;
2365 tmplog = ret;
2366 last_isspace = 0;
2367 break;
2368
William Lallemand5f232402012-04-05 18:02:55 +02002369 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002370 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002371 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002372 if (iret < 0 || iret > dst + maxsize - tmplog)
2373 goto out;
2374 last_isspace = 0;
2375 tmplog += iret;
2376 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002377 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002378 if (ret == NULL)
2379 goto out;
2380 tmplog = ret;
2381 last_isspace = 0;
2382 }
2383 break;
2384
William Lallemand1d705562012-03-12 12:46:41 +01002385 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002386 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002387 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002388 if (iret < 0 || iret > dst + maxsize - tmplog)
2389 goto out;
2390 last_isspace = 0;
2391 tmplog += iret;
2392 } else {
2393 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002394 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002395 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002396 tmplog, 4);
2397 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002398 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002399 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002400 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002401 }
2402 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002403
William Lallemand1d705562012-03-12 12:46:41 +01002404 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002405 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002406 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002407 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002408 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002409 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002410 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002411 break;
2412
Willy Tarreau773d65f2012-10-12 14:56:11 +02002413 case LOG_FMT_FRONTEND_XPRT: // %ft
2414 src = fe->id;
2415 if (tmp->options & LOG_OPT_QUOTE)
2416 LOGCHAR('"');
2417 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2418 if (iret == 0)
2419 goto out;
2420 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002421 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002422 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002423 if (tmp->options & LOG_OPT_QUOTE)
2424 LOGCHAR('"');
2425 last_isspace = 0;
2426 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002427#ifdef USE_OPENSSL
2428 case LOG_FMT_SSL_CIPHER: // %sslc
2429 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002430 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002431 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002432 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002433 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002434 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2435 if (ret == NULL)
2436 goto out;
2437 tmplog = ret;
2438 last_isspace = 0;
2439 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002440
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002441 case LOG_FMT_SSL_VERSION: // %sslv
2442 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002443 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002444 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002445 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002446 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002447 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2448 if (ret == NULL)
2449 goto out;
2450 tmplog = ret;
2451 last_isspace = 0;
2452 break;
2453#endif
William Lallemand1d705562012-03-12 12:46:41 +01002454 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002455 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002456 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002457 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002458 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002459 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002460 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002461 break;
2462
William Lallemand1d705562012-03-12 12:46:41 +01002463 case LOG_FMT_SERVER: // %s
Willy Tarreaue1809df2018-09-05 15:30:16 +02002464 switch (obj_type(s ? s->target : NULL)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002465 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002466 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002467 break;
2468 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002469 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002470 break;
2471 default:
2472 src = "<NOSRV>";
2473 break;
2474 }
William Lallemand5f232402012-04-05 18:02:55 +02002475 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002476 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002477 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002478 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002479 last_isspace = 0;
2480 break;
2481
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002482 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002483 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002484 if (ret == NULL)
2485 goto out;
2486 tmplog = ret;
2487 last_isspace = 0;
2488 break;
2489
2490 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002491 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002492 if (ret == NULL)
2493 goto out;
2494 tmplog = ret;
2495 last_isspace = 0;
2496 break;
2497
2498 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002499 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002500 tmplog, dst + maxsize - tmplog);
2501 if (ret == NULL)
2502 goto out;
2503 tmplog = ret;
2504 last_isspace = 0;
2505 break;
2506
2507 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002508 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002509 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002510 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002511 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002512 last_isspace = 0;
2513 break;
2514
William Lallemand1d705562012-03-12 12:46:41 +01002515 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002516 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002517 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002518 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002519 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002520 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002521 last_isspace = 0;
2522 break;
2523
William Lallemand1d705562012-03-12 12:46:41 +01002524 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002525 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002526 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002527 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002528 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002529 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002530 last_isspace = 0;
2531 break;
2532
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002533 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002534 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002535 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002536 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002537 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002538 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002539 last_isspace = 0;
2540 break;
2541
Willy Tarreau27b639d2016-05-17 17:55:27 +02002542 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002543 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002544 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002545 tmplog, dst + maxsize - tmplog);
2546 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002547 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002548 tmplog, dst + maxsize - tmplog);
2549 if (ret == NULL)
2550 goto out;
2551 tmplog = ret;
2552 last_isspace = 0;
2553 break;
2554
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002555 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2556 if (!(fe->to_log & LW_BYTES))
2557 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002558 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 +02002559 tmplog, dst + maxsize - tmplog);
2560 if (ret == NULL)
2561 goto out;
2562 tmplog = ret;
2563 last_isspace = 0;
2564 break;
2565
2566 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002567 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002568 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002569 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002570 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002571 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002572 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002573 last_isspace = 0;
2574 break;
2575
Damien Claisse57c8eb92020-04-28 12:09:19 +00002576 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2577 if (!(fe->to_log & LW_BYTES))
2578 LOGCHAR('+');
2579 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2580 tmplog, dst + maxsize - tmplog);
2581 if (ret == NULL)
2582 goto out;
2583 tmplog = ret;
2584 last_isspace = 0;
2585 break;
2586
Willy Tarreau2beef582012-12-20 17:22:52 +01002587 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02002588 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002589 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002590 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002591 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002592 last_isspace = 0;
2593 break;
2594
William Lallemand1d705562012-03-12 12:46:41 +01002595 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002596 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002597 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002598 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002599 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002600 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002601 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002602 last_isspace = 0;
2603 break;
2604
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002605 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002606 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002607 if (ret == NULL)
2608 goto out;
2609 tmplog = ret;
2610 last_isspace = 0;
2611 break;
2612
Willy Tarreau2beef582012-12-20 17:22:52 +01002613 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002614 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002615 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002616 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002617 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002618 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002619 last_isspace = 0;
2620 break;
2621
Willy Tarreau2beef582012-12-20 17:22:52 +01002622 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002623 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002624 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002625 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002626 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002627 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002628 last_isspace = 0;
2629 break;
2630
William Lallemand1d705562012-03-12 12:46:41 +01002631 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002632 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2633 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002634 *tmplog = '\0';
2635 last_isspace = 0;
2636 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002637
William Lallemand1d705562012-03-12 12:46:41 +01002638 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002639 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2640 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002641 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2642 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 +01002643 last_isspace = 0;
2644 break;
2645
William Lallemand1d705562012-03-12 12:46:41 +01002646 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002647 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002648 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002649 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002650 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002651 last_isspace = 0;
2652 break;
2653
William Lallemand1d705562012-03-12 12:46:41 +01002654 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002655 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002656 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002657 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002658 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002659 last_isspace = 0;
2660 break;
2661
William Lallemand1d705562012-03-12 12:46:41 +01002662 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002663 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002664 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002665 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002666 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002667 last_isspace = 0;
2668 break;
2669
William Lallemand1d705562012-03-12 12:46:41 +01002670 case LOG_FMT_SRVCONN: // %sc
Willy Tarreaue1809df2018-09-05 15:30:16 +02002671 ret = ultoa_o(objt_server(s ? s->target : NULL) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002672 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002673 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002674 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002675 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002676 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002677 last_isspace = 0;
2678 break;
2679
William Lallemand1d705562012-03-12 12:46:41 +01002680 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002681 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002682 LOGCHAR('+');
Willy Tarreauabd71a52018-09-04 19:21:44 +02002683 ret = ltoa_o((s && s->si[1].conn_retries > 0) ?
Willy Tarreau350f4872014-11-28 14:42:25 +01002684 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002685 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002686 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002687 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002688 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002689 last_isspace = 0;
2690 break;
2691
William Lallemand1d705562012-03-12 12:46:41 +01002692 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002693 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002694 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002695 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002696 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002697 last_isspace = 0;
2698 break;
2699
William Lallemand1d705562012-03-12 12:46:41 +01002700 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002701 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002702 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002703 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002704 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002705 last_isspace = 0;
2706 break;
2707
William Lallemand1d705562012-03-12 12:46:41 +01002708 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002709 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002710 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002711 if (tmp->options & LOG_OPT_QUOTE)
2712 LOGCHAR('"');
2713 LOGCHAR('{');
2714 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2715 if (hdr)
2716 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002717 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002718 ret = lf_encode_string(tmplog, dst + maxsize,
2719 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002720 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002721 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002722 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002723 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002724 }
2725 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002726 if (tmp->options & LOG_OPT_QUOTE)
2727 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002728 last_isspace = 0;
2729 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002730 break;
2731
William Lallemand1d705562012-03-12 12:46:41 +01002732 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002733 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002734 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002735 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2736 if (hdr > 0)
2737 LOGCHAR(' ');
2738 if (tmp->options & LOG_OPT_QUOTE)
2739 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002740 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002741 ret = lf_encode_string(tmplog, dst + maxsize,
2742 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002743 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002744 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002745 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002746 } else if (!(tmp->options & LOG_OPT_QUOTE))
2747 LOGCHAR('-');
2748 if (tmp->options & LOG_OPT_QUOTE)
2749 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002750 last_isspace = 0;
2751 }
2752 }
2753 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002754
William Lallemand1d705562012-03-12 12:46:41 +01002755
2756 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002757 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002758 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002759 if (tmp->options & LOG_OPT_QUOTE)
2760 LOGCHAR('"');
2761 LOGCHAR('{');
2762 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2763 if (hdr)
2764 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002765 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002766 ret = lf_encode_string(tmplog, dst + maxsize,
2767 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002768 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002769 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002770 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002771 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002772 }
2773 LOGCHAR('}');
2774 last_isspace = 0;
2775 if (tmp->options & LOG_OPT_QUOTE)
2776 LOGCHAR('"');
2777 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002778 break;
2779
William Lallemand1d705562012-03-12 12:46:41 +01002780 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002781 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002782 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002783 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2784 if (hdr > 0)
2785 LOGCHAR(' ');
2786 if (tmp->options & LOG_OPT_QUOTE)
2787 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002788 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002789 ret = lf_encode_string(tmplog, dst + maxsize,
2790 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002791 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002792 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002793 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002794 } else if (!(tmp->options & LOG_OPT_QUOTE))
2795 LOGCHAR('-');
2796 if (tmp->options & LOG_OPT_QUOTE)
2797 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002798 last_isspace = 0;
2799 }
2800 }
2801 break;
2802
William Lallemand1d705562012-03-12 12:46:41 +01002803 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002804 /* Request */
2805 if (tmp->options & LOG_OPT_QUOTE)
2806 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002807 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002808 ret = lf_encode_string(tmplog, dst + maxsize,
2809 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002810 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002811 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002812 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002813 if (tmp->options & LOG_OPT_QUOTE)
2814 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002815 last_isspace = 0;
2816 break;
William Lallemand5f232402012-04-05 18:02:55 +02002817
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002818 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002819 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002820
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 end = uri + strlen(uri);
2825 // look for the first whitespace character
2826 while (uri < end && !HTTP_IS_SPHT(*uri))
2827 uri++;
2828
2829 // keep advancing past multiple spaces
2830 while (uri < end && HTTP_IS_SPHT(*uri)) {
2831 uri++; nspaces++;
2832 }
2833
2834 // look for first space or question mark after url
2835 spc = uri;
2836 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2837 spc++;
2838
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002839 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002840 chunk.area = "<BADREQ>";
2841 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002842 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002843 chunk.area = uri;
2844 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002845 }
2846
Dragan Dosen835b9212016-02-12 13:23:03 +01002847 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002848 if (ret == NULL || *ret != '\0')
2849 goto out;
2850
2851 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002852 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002853 LOGCHAR('"');
2854
2855 last_isspace = 0;
2856 break;
2857
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002858 case LOG_FMT_HTTP_QUERY: // %HQ
2859 if (tmp->options & LOG_OPT_QUOTE)
2860 LOGCHAR('"');
2861
Willy Tarreau57bc8912016-04-25 17:09:40 +02002862 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002863 chunk.area = "<BADREQ>";
2864 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002865 } else {
2866 uri = txn->uri;
2867 end = uri + strlen(uri);
2868 // look for the first question mark
2869 while (uri < end && *uri != '?')
2870 uri++;
2871
2872 qmark = uri;
2873 // look for first space or question mark after url
2874 while (uri < end && !HTTP_IS_SPHT(*uri))
2875 uri++;
2876
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002877 chunk.area = qmark;
2878 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002879 }
2880
Dragan Dosen835b9212016-02-12 13:23:03 +01002881 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002882 if (ret == NULL || *ret != '\0')
2883 goto out;
2884
2885 tmplog = ret;
2886 if (tmp->options & LOG_OPT_QUOTE)
2887 LOGCHAR('"');
2888
2889 last_isspace = 0;
2890 break;
2891
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002892 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002893 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002894
Willy Tarreaub7636d12015-06-17 19:58:02 +02002895 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002896 LOGCHAR('"');
2897
2898 end = uri + strlen(uri);
2899 // look for the first whitespace character
2900 while (uri < end && !HTTP_IS_SPHT(*uri))
2901 uri++;
2902
2903 // keep advancing past multiple spaces
2904 while (uri < end && HTTP_IS_SPHT(*uri)) {
2905 uri++; nspaces++;
2906 }
2907
2908 // look for first space after url
2909 spc = uri;
2910 while (spc < end && !HTTP_IS_SPHT(*spc))
2911 spc++;
2912
Willy Tarreau57bc8912016-04-25 17:09:40 +02002913 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002914 chunk.area = "<BADREQ>";
2915 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002916 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002917 chunk.area = uri;
2918 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002919 }
2920
Dragan Dosen835b9212016-02-12 13:23:03 +01002921 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002922 if (ret == NULL || *ret != '\0')
2923 goto out;
2924
2925 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002926 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002927 LOGCHAR('"');
2928
2929 last_isspace = 0;
2930 break;
2931
2932 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002933 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002934 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002935 LOGCHAR('"');
2936
2937 end = uri + strlen(uri);
2938 // look for the first whitespace character
2939 spc = uri;
2940 while (spc < end && !HTTP_IS_SPHT(*spc))
2941 spc++;
2942
2943 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002944 chunk.area = "<BADREQ>";
2945 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002946 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002947 chunk.area = uri;
2948 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002949 }
2950
Dragan Dosen835b9212016-02-12 13:23:03 +01002951 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002952 if (ret == NULL || *ret != '\0')
2953 goto out;
2954
2955 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002956 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002957 LOGCHAR('"');
2958
2959 last_isspace = 0;
2960 break;
2961
2962 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002963 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002964 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002965 LOGCHAR('"');
2966
2967 end = uri + strlen(uri);
2968 // look for the first whitespace character
2969 while (uri < end && !HTTP_IS_SPHT(*uri))
2970 uri++;
2971
2972 // keep advancing past multiple spaces
2973 while (uri < end && HTTP_IS_SPHT(*uri)) {
2974 uri++; nspaces++;
2975 }
2976
2977 // look for the next whitespace character
2978 while (uri < end && !HTTP_IS_SPHT(*uri))
2979 uri++;
2980
2981 // keep advancing past multiple spaces
2982 while (uri < end && HTTP_IS_SPHT(*uri))
2983 uri++;
2984
Willy Tarreau57bc8912016-04-25 17:09:40 +02002985 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002986 chunk.area = "<BADREQ>";
2987 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002988 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002989 chunk.area = "HTTP/0.9";
2990 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002991 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002992 chunk.area = uri;
2993 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002994 }
2995
Dragan Dosen835b9212016-02-12 13:23:03 +01002996 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002997 if (ret == NULL || *ret != '\0')
2998 goto out;
2999
3000 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003001 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003002 LOGCHAR('"');
3003
3004 last_isspace = 0;
3005 break;
3006
William Lallemand5f232402012-04-05 18:02:55 +02003007 case LOG_FMT_COUNTER: // %rt
3008 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003009 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02003010 if (iret < 0 || iret > dst + maxsize - tmplog)
3011 goto out;
3012 last_isspace = 0;
3013 tmplog += iret;
3014 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003015 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003016 if (ret == NULL)
3017 goto out;
3018 tmplog = ret;
3019 last_isspace = 0;
3020 }
3021 break;
3022
Willy Tarreau7346acb2014-08-28 15:03:15 +02003023 case LOG_FMT_LOGCNT: // %lc
3024 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003025 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003026 if (iret < 0 || iret > dst + maxsize - tmplog)
3027 goto out;
3028 last_isspace = 0;
3029 tmplog += iret;
3030 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003031 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003032 if (ret == NULL)
3033 goto out;
3034 tmplog = ret;
3035 last_isspace = 0;
3036 }
3037 break;
3038
William Lallemand5f232402012-04-05 18:02:55 +02003039 case LOG_FMT_HOSTNAME: // %H
3040 src = hostname;
3041 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3042 if (ret == NULL)
3043 goto out;
3044 tmplog = ret;
3045 last_isspace = 0;
3046 break;
3047
3048 case LOG_FMT_PID: // %pid
3049 if (tmp->options & LOG_OPT_HEXA) {
3050 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3051 if (iret < 0 || iret > dst + maxsize - tmplog)
3052 goto out;
3053 last_isspace = 0;
3054 tmplog += iret;
3055 } else {
3056 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3057 if (ret == NULL)
3058 goto out;
3059 tmplog = ret;
3060 last_isspace = 0;
3061 }
3062 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003063
3064 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003065 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003066 if (s)
3067 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3068 else
3069 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003070 if (ret == NULL)
3071 goto out;
3072 tmplog = ret;
3073 last_isspace = 0;
3074 break;
3075
William Lallemandbddd4fd2012-02-27 11:23:10 +01003076 }
3077 }
3078
3079out:
William Lallemand1d705562012-03-12 12:46:41 +01003080 /* *tmplog is a unused character */
3081 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003082 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003083
Willy Tarreaubaaee002006-06-26 02:48:02 +02003084}
3085
William Lallemand1d705562012-03-12 12:46:41 +01003086/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003087 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003088 * Will not log if the frontend has no log defined.
3089 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003090void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003091{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003092 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003093 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003094 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003095
3096 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003097 err = (s->flags & SF_REDISP) ||
3098 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3099 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003100 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003101 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003102
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003103 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003104 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003105
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003106 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003107 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003108
Willy Tarreauabcd5142013-06-11 17:18:02 +02003109 if (s->logs.level) { /* loglevel was overridden */
3110 if (s->logs.level == -1) {
3111 s->logs.logwait = 0; /* logs disabled */
3112 return;
3113 }
3114 level = s->logs.level - 1;
3115 }
3116 else {
3117 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003118 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003119 level = LOG_ERR;
3120 }
William Lallemand1d705562012-03-12 12:46:41 +01003121
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003122 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003123 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003124 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003125 }
3126
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003127 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3128 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3129 &sess->fe->logformat_sd);
3130 }
3131
Dragan Dosen59cee972015-09-19 22:09:02 +02003132 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003133 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003134 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003135 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3136 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003137 s->logs.logwait = 0;
3138 }
3139}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003140
Willy Tarreau53839352018-09-05 19:51:10 +02003141/*
3142 * send a minimalist log for the session. Will not log if the frontend has no
3143 * log defined. It is assumed that this is only used to report anomalies that
3144 * cannot lead to the creation of a regular stream. Because of this the log
3145 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3146 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003147 * function to report unimportant events. It is safe to call this function with
3148 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003149 */
3150void sess_log(struct session *sess)
3151{
3152 int size, level;
3153 int sd_size = 0;
3154
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003155 if (!sess)
3156 return;
3157
Willy Tarreau53839352018-09-05 19:51:10 +02003158 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3159 return;
3160
3161 level = LOG_INFO;
3162 if (sess->fe->options2 & PR_O2_LOGERRORS)
3163 level = LOG_ERR;
3164
3165 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3166 sd_size = sess_build_logline(sess, NULL,
3167 logline_rfc5424, global.max_syslog_len,
3168 &sess->fe->logformat_sd);
3169 }
3170
3171 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3172 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003173 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003174 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3175 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003176 }
3177}
3178
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003179void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3180{
3181 va_list argp;
3182 int data_len;
3183
3184 if (level < 0 || format == NULL || logline == NULL)
3185 return;
3186
3187 va_start(argp, format);
3188 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3189 if (data_len < 0 || data_len > global.max_syslog_len)
3190 data_len = global.max_syslog_len;
3191 va_end(argp);
3192
3193 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3194}
Emeric Brun54932b42020-07-07 09:43:24 +02003195/*
3196 * This function parse a received log message <buf>, of size <buflen>
3197 * it fills <level>, <facility> and <metadata> depending of the detected
3198 * header format and message will point on remaining payload of <size>
3199 *
3200 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3201 * struct ist len will be set to 0 if field is not found
3202 * <level> and <facility> will be set to -1 if not found.
3203 */
3204void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3205 struct ist *metadata, char **message, size_t *size)
3206{
3207
3208 char *p;
3209 int fac_level = 0;
3210
3211 *level = *facility = -1;
3212
3213 *message = buf;
3214 *size = buflen;
3215
3216 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3217
3218 p = buf;
3219 if (*size < 2 || *p != '<')
3220 return;
3221
3222 p++;
3223 while (*p != '>') {
3224 if (*p > '9' || *p < '0')
3225 return;
3226 fac_level = 10*fac_level + (*p - '0');
3227 p++;
3228 if ((p - buf) > buflen)
3229 return;
3230 }
3231
3232 *facility = fac_level >> 3;
3233 *level = fac_level & 0x7;
3234 p++;
3235
3236 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3237
3238 buflen -= p - buf;
3239 buf = p;
3240
3241 *size = buflen;
3242 *message = buf;
3243
3244 /* for rfc5424, prio is always followed by '1' and ' ' */
3245 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3246 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3247 * followed by message.
3248 * Each header field can present NILVALUE: '-'
3249 */
3250
3251 p += 2;
3252 /* timestamp is NILVALUE '-' */
3253 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3254 metadata[LOG_META_TIME] = ist2(p, 1);
3255 p++;
3256 }
3257 else if (*size > LOG_ISOTIME_MINLEN) {
3258 metadata[LOG_META_TIME].ptr = p;
3259
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003260 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003261 * in timestamp.
3262 * possible format are:
3263 * ex: '1970-01-01T00:00:00.000000Z'
3264 * '1970-01-01T00:00:00.000000+00:00'
3265 * '1970-01-01T00:00:00.000000-00:00'
3266 * '1970-01-01T00:00:00Z'
3267 * '1970-01-01T00:00:00+00:00'
3268 * '1970-01-01T00:00:00-00:00'
3269 */
3270 p += 19;
3271 if (*p == '.') {
3272 p++;
3273 if ((p - buf) >= buflen)
3274 goto bad_format;
3275 while (*p != 'Z' && *p != '+' && *p != '-') {
3276 if ((unsigned char)(*p - '0') > 9)
3277 goto bad_format;
3278
3279 p++;
3280 if ((p - buf) >= buflen)
3281 goto bad_format;
3282 }
3283 }
3284
3285 if (*p == 'Z')
3286 p++;
3287 else
3288 p += 6; /* case of '+00:00 or '-00:00' */
3289
3290 if ((p - buf) >= buflen || *p != ' ')
3291 goto bad_format;
3292 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3293 }
3294 else
3295 goto bad_format;
3296
3297
3298 p++;
3299 if ((p - buf) >= buflen || *p == ' ')
3300 goto bad_format;
3301
3302 metadata[LOG_META_HOST].ptr = p;
3303 while (*p != ' ') {
3304 p++;
3305 if ((p - buf) >= buflen)
3306 goto bad_format;
3307 }
3308 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3309 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3310 metadata[LOG_META_HOST].len = 0;
3311
3312 p++;
3313 if ((p - buf) >= buflen || *p == ' ')
3314 goto bad_format;
3315
3316 metadata[LOG_META_TAG].ptr = p;
3317 while (*p != ' ') {
3318 p++;
3319 if ((p - buf) >= buflen)
3320 goto bad_format;
3321 }
3322 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3323 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3324 metadata[LOG_META_TAG].len = 0;
3325
3326 p++;
3327 if ((p - buf) >= buflen || *p == ' ')
3328 goto bad_format;
3329
3330 metadata[LOG_META_PID].ptr = p;
3331 while (*p != ' ') {
3332 p++;
3333 if ((p - buf) >= buflen)
3334 goto bad_format;
3335 }
3336 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3337 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3338 metadata[LOG_META_PID].len = 0;
3339
3340 p++;
3341 if ((p - buf) >= buflen || *p == ' ')
3342 goto bad_format;
3343
3344 metadata[LOG_META_MSGID].ptr = p;
3345 while (*p != ' ') {
3346 p++;
3347 if ((p - buf) >= buflen)
3348 goto bad_format;
3349 }
3350 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3351 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3352 metadata[LOG_META_MSGID].len = 0;
3353
3354 p++;
3355 if ((p - buf) >= buflen || *p == ' ')
3356 goto bad_format;
3357
3358 /* structured data format is:
3359 * ex:
3360 * '[key1=value1 key2=value2][key3=value3]'
3361 *
3362 * space is invalid outside [] because
3363 * considered as the end of structured data field
3364 */
3365 metadata[LOG_META_STDATA].ptr = p;
3366 if (*p == '[') {
3367 int elem = 0;
3368
3369 while (1) {
3370 if (elem) {
3371 /* according to rfc this char is escaped in param values */
3372 if (*p == ']' && *(p-1) != '\\')
3373 elem = 0;
3374 }
3375 else {
3376 if (*p == '[')
3377 elem = 1;
3378 else if (*p == ' ')
3379 break;
3380 else
3381 goto bad_format;
3382 }
3383 p++;
3384 if ((p - buf) >= buflen)
3385 goto bad_format;
3386 }
3387 }
3388 else if (*p == '-') {
3389 /* case of NILVALUE */
3390 p++;
3391 if ((p - buf) >= buflen || *p != ' ')
3392 goto bad_format;
3393 }
3394 else
3395 goto bad_format;
3396
3397 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3398 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3399 metadata[LOG_META_STDATA].len = 0;
3400
3401 p++;
3402
3403 buflen -= p - buf;
3404 buf = p;
3405
3406 *size = buflen;
3407 *message = p;
3408 }
3409 else if (*size > LOG_LEGACYTIME_LEN) {
3410 int m;
3411
3412 /* supported header format according to rfc3164.
3413 * ex:
3414 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3415 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3416 * or 'Jan 1 00:00:00 HOSTNAME '
3417 * Note: HOSTNAME is mandatory, and day
3418 * of month uses a single space prefix if
3419 * less than 10 to ensure hour offset is
3420 * always the same.
3421 */
3422
3423 /* Check month to see if it correspond to a rfc3164
3424 * header ex 'Jan 1 00:00:00' */
3425 for (m = 0; m < 12; m++)
3426 if (!memcmp(monthname[m], p, 3))
3427 break;
3428 /* Month not found */
3429 if (m == 12)
3430 goto bad_format;
3431
3432 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3433
3434 p += LOG_LEGACYTIME_LEN;
3435 if ((p - buf) >= buflen || *p != ' ')
3436 goto bad_format;
3437
3438 p++;
3439 if ((p - buf) >= buflen || *p == ' ')
3440 goto bad_format;
3441
3442 metadata[LOG_META_HOST].ptr = p;
3443 while (*p != ' ') {
3444 p++;
3445 if ((p - buf) >= buflen)
3446 goto bad_format;
3447 }
3448 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3449
3450 /* TAG seems to no be mandatory */
3451 p++;
3452
3453 buflen -= p - buf;
3454 buf = p;
3455
3456 *size = buflen;
3457 *message = buf;
3458
3459 if (!buflen)
3460 return;
3461
3462 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3463 p++;
3464
3465 /* a tag must present a trailing ':' */
3466 if (((p - buf) >= buflen) || *p != ':')
3467 return;
3468 p++;
3469 /* followed by a space */
3470 if (((p - buf) >= buflen) || *p != ' ')
3471 return;
3472
3473 /* rewind to parse tag and pid */
3474 p = buf;
3475 metadata[LOG_META_TAG].ptr = p;
3476 /* we have the guarantee that ':' will be reach before size limit */
3477 while (*p != ':') {
3478 if (*p == '[') {
3479 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3480 metadata[LOG_META_PID].ptr = p + 1;
3481 }
3482 else if (*p == ']' && metadata[LOG_META_PID].ptr) {
3483 if (p[1] != ':')
3484 return;
3485 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3486 }
3487 p++;
3488 }
3489 if (!metadata[LOG_META_TAG].len)
3490 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3491
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003492 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003493 p += 2;
3494
3495 buflen -= p - buf;
3496 buf = p;
3497
3498 *size = buflen;
3499 *message = buf;
3500 }
3501
3502 return;
3503
3504bad_format:
3505 /* bad syslog format, we reset all parsed syslog fields
3506 * but priority is kept because we are able to re-build
3507 * this message using LOF_FORMAT_PRIO.
3508 */
3509 metadata[LOG_META_TIME].len = 0;
3510 metadata[LOG_META_HOST].len = 0;
3511 metadata[LOG_META_TAG].len = 0;
3512 metadata[LOG_META_PID].len = 0;
3513 metadata[LOG_META_MSGID].len = 0;
3514 metadata[LOG_META_STDATA].len = 0;
3515
3516 return;
3517}
3518
3519/*
3520 * UDP syslog fd handler
3521 */
3522void syslog_fd_handler(int fd)
3523{
3524 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3525 ssize_t ret = 0;
3526 struct buffer *buf = get_trash_chunk();
3527 size_t size;
3528 char *message;
3529 int level;
3530 int facility;
3531 struct listener *l = objt_listener(fdtab[fd].owner);
3532 int max_accept;
3533
3534 if(!l)
3535 ABORT_NOW();
3536
3537 if (fdtab[fd].ev & FD_POLL_IN) {
3538
3539 if (!fd_recv_ready(fd))
3540 return;
3541
3542 max_accept = l->maxaccept ? l->maxaccept : 1;
3543
3544 do {
3545 /* Source address */
3546 struct sockaddr_storage saddr = {0};
3547 socklen_t saddrlen;
3548
3549 saddrlen = sizeof(saddr);
3550
3551 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3552 if (ret < 0) {
3553 if (errno == EINTR)
3554 continue;
3555 if (errno == EAGAIN)
3556 fd_cant_recv(fd);
3557 goto out;
3558 }
3559 buf->data = ret;
3560
Emeric Brun45c457a2020-07-09 23:23:34 +02003561 /* update counters */
3562 _HA_ATOMIC_ADD(&cum_log_messages, 1);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003563 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003564
Emeric Brun54932b42020-07-07 09:43:24 +02003565 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3566
3567 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3568
3569 } while (--max_accept);
3570 }
3571
3572out:
3573 return;
3574}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003575
Emeric Brun12941c82020-07-07 14:19:42 +02003576/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003577 * IO Handler to handle message exchange with a syslog tcp client
3578 */
3579static void syslog_io_handler(struct appctx *appctx)
3580{
3581 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3582 struct stream_interface *si = appctx->owner;
3583 struct stream *s = si_strm(si);
3584 struct proxy *frontend = strm_fe(s);
3585 struct listener *l = strm_li(s);
3586 struct buffer *buf = get_trash_chunk();
3587 int max_accept;
3588 int to_skip;
3589 int facility;
3590 int level;
3591 char *message;
3592 size_t size;
3593
3594 max_accept = l->maxaccept ? l->maxaccept : 1;
3595 while (co_data(si_oc(si))) {
3596 char c;
3597
3598 if (max_accept <= 0)
3599 goto missing_budget;
3600 max_accept--;
3601
3602 to_skip = co_getchar(si_oc(si), &c);
3603 if (!to_skip)
3604 goto missing_data;
3605 else if (to_skip < 0)
3606 goto cli_abort;
3607
3608 if (c == '<') {
3609 /* rfc-6587, Non-Transparent-Framing: messages separated by
3610 * a trailing LF or CR LF
3611 */
3612 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3613 if (!to_skip)
3614 goto missing_data;
3615 else if (to_skip < 0)
3616 goto cli_abort;
3617
3618 if (buf->area[to_skip - 1] != '\n')
3619 goto parse_error;
3620
3621 buf->data = to_skip - 1;
3622
3623 /* according to rfc-6587, some devices adds CR before LF */
3624 if (buf->data && buf->area[buf->data - 1] == '\r')
3625 buf->data--;
3626
3627 }
3628 else if ((unsigned char)(c - '1') <= 8) {
3629 /* rfc-6587, Octet-Counting: message length in ASCII
3630 * (first digit can not be ZERO), followed by a space
3631 * and message length
3632 */
3633 char *p = NULL;
3634 int msglen;
3635
3636 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3637 if (!to_skip)
3638 goto missing_data;
3639 else if (to_skip < 0)
3640 goto cli_abort;
3641
3642 if (buf->area[to_skip - 1] != ' ')
3643 goto parse_error;
3644
3645 msglen = strtol(trash.area, &p, 10);
3646 if (!msglen || p != &buf->area[to_skip - 1])
3647 goto parse_error;
3648
3649 /* message seems too large */
3650 if (msglen > buf->size)
3651 goto parse_error;
3652
3653 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3654 if (!msglen)
3655 goto missing_data;
3656 else if (msglen < 0)
3657 goto cli_abort;
3658
3659
3660 buf->data = msglen;
3661 to_skip += msglen;
3662 }
3663 else
3664 goto parse_error;
3665
3666 co_skip(si_oc(si), to_skip);
3667
3668 /* update counters */
3669 _HA_ATOMIC_ADD(&cum_log_messages, 1);
3670 proxy_inc_fe_req_ctr(l, frontend);
3671
3672 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3673
3674 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3675
3676 }
3677
3678missing_data:
3679 /* we need more data to read */
3680 si_oc(si)->flags |= CF_READ_DONTWAIT;
3681
3682 return;
3683
3684missing_budget:
3685 /* it may remain some stuff to do, let's retry later */
3686 appctx_wakeup(appctx);
3687
3688 return;
3689
3690parse_error:
3691 if (l->counters)
3692 _HA_ATOMIC_ADD(&l->counters->failed_req, 1);
3693 _HA_ATOMIC_ADD(&frontend->fe_counters.failed_req, 1);
3694
3695 goto close;
3696
3697cli_abort:
3698 if (l->counters)
3699 _HA_ATOMIC_ADD(&l->counters->cli_aborts, 1);
3700 _HA_ATOMIC_ADD(&frontend->fe_counters.cli_aborts, 1);
3701
3702close:
3703 si_shutw(si);
3704 si_shutr(si);
3705
3706 si_ic(si)->flags |= CF_READ_NULL;
3707
3708 return;
3709}
3710
3711static struct applet syslog_applet = {
3712 .obj_type = OBJ_TYPE_APPLET,
3713 .name = "<SYSLOG>", /* used for logging */
3714 .fct = syslog_io_handler,
3715 .release = NULL,
3716};
3717
3718/*
Emeric Brun12941c82020-07-07 14:19:42 +02003719 * Parse "log-forward" section and create corresponding sink buffer.
3720 *
3721 * The function returns 0 in success case, otherwise, it returns error
3722 * flags.
3723 */
3724int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3725{
3726 int err_code = 0;
3727 struct proxy *px;
3728 char *errmsg = NULL;
3729 const char *err = NULL;
3730
3731 if (strcmp(args[0], "log-forward") == 0) {
3732 if (!*args[1]) {
3733 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3734 err_code |= ERR_ALERT | ERR_ABORT;
3735 goto out;
3736 }
3737
3738 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3739 goto out;
3740
3741 err = invalid_char(args[1]);
3742 if (err) {
3743 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3744 file, linenum, *err, args[0], args[1]);
3745 err_code |= ERR_ALERT | ERR_ABORT;
3746 goto out;
3747 }
3748
Emeric Brunb0c331f2020-10-07 17:05:59 +02003749 px = log_forward_by_name(args[1]);
3750 if (px) {
3751 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3752 file, linenum, args[1], px->conf.file, px->conf.line);
3753 err_code |= ERR_ALERT | ERR_FATAL;
3754 }
3755
3756 px = proxy_find_by_name(args[1], 0, 0);
3757 if (px) {
3758 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3759 file, linenum, args[1], proxy_type_str(px),
3760 px->id, px->conf.file, px->conf.line);
3761 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003762 }
3763
3764 px = calloc(1, sizeof *px);
3765 if (!px) {
3766 err_code |= ERR_ALERT | ERR_FATAL;
3767 goto out;
3768 }
3769
3770 px->next = cfg_log_forward;
3771 cfg_log_forward = px;
3772
3773 init_new_proxy(px);
3774 px->conf.file = strdup(file);
3775 px->conf.line = linenum;
3776 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003777 px->last_change = now.tv_sec;
3778 px->cap = PR_CAP_FE;
3779 px->maxconn = 10;
3780 px->timeout.client = TICK_ETERNITY;
3781 px->accept = frontend_accept;
3782 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003783 px->id = strdup(args[1]);
3784
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003785 }
3786 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
3787 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3788 err_code |= ERR_WARN;
3789
3790 if (*(args[1]) == 0) {
3791 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3792 err_code |= ERR_ALERT | ERR_FATAL;
3793 goto out;
3794 }
3795 cfg_log_forward->maxconn = atol(args[1]);
3796 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3797 goto out;
3798 }
3799 else if (!strcmp(args[0], "backlog")) { /* backlog */
3800 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3801 err_code |= ERR_WARN;
3802
3803 if (*(args[1]) == 0) {
3804 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3805 err_code |= ERR_ALERT | ERR_FATAL;
3806 goto out;
3807 }
3808 cfg_log_forward->backlog = atol(args[1]);
3809 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3810 goto out;
3811 }
3812 else if (strcmp(args[0], "bind") == 0) {
3813 int cur_arg;
3814 static int kws_dumped;
3815 struct bind_conf *bind_conf;
3816 struct bind_kw *kw;
3817 struct listener *l;
3818
3819 cur_arg = 1;
3820
3821 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3822 NULL, xprt_get(XPRT_RAW));
3823 if (!bind_conf) {
3824 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3825 err_code |= ERR_ALERT | ERR_FATAL;
3826 goto out;
3827 }
3828
3829 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3830 if (errmsg && *errmsg) {
3831 indent_msg(&errmsg, 2);
3832 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3833 }
3834 else {
3835 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3836 file, linenum, args[0], args[1], args[2]);
3837 err_code |= ERR_ALERT | ERR_FATAL;
3838 goto out;
3839 }
3840 }
3841 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
3842 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : 64;
3843 l->accept = session_accept_fd;
3844 l->analysers |= cfg_log_forward->fe_req_ana;
3845 l->default_target = cfg_log_forward->default_target;
3846 global.maxsock++;
3847 }
3848 cur_arg++;
3849
3850 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3851 int ret;
3852
3853 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3854 err_code |= ret;
3855 if (ret) {
3856 if (errmsg && *errmsg) {
3857 indent_msg(&errmsg, 2);
3858 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3859 }
3860 else
3861 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3862 file, linenum, args[cur_arg]);
3863 if (ret & ERR_FATAL)
3864 goto out;
3865 }
3866 cur_arg += 1 + kw->skip;
3867 }
3868 if (*args[cur_arg] != 0) {
3869 char *kws = NULL;
3870
3871 if (!kws_dumped) {
3872 kws_dumped = 1;
3873 bind_dump_kws(&kws);
3874 indent_msg(&kws, 4);
3875 }
3876 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.%s%s\n",
3877 file, linenum, args[cur_arg], cursection,
3878 kws ? " Registered keywords :" : "", kws ? kws: "");
3879 free(kws);
3880 err_code |= ERR_ALERT | ERR_FATAL;
3881 goto out;
3882 }
Emeric Brun12941c82020-07-07 14:19:42 +02003883 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003884 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003885 int cur_arg;
3886 static int kws_dumped;
3887 struct bind_conf *bind_conf;
3888 struct bind_kw *kw;
3889 struct listener *l;
3890
3891 cur_arg = 1;
3892
3893 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3894 NULL, xprt_get(XPRT_RAW));
3895
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003896 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003897 if (errmsg && *errmsg) {
3898 indent_msg(&errmsg, 2);
3899 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3900 }
3901 else {
3902 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3903 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003904 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003905 err_code |= ERR_ALERT | ERR_FATAL;
3906 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003907 }
3908 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003909 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Emeric Brun12941c82020-07-07 14:19:42 +02003910 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : 64;
Willy Tarreaue140a692020-10-15 21:25:32 +02003911 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003912 global.maxsock++;
3913 }
3914 cur_arg++;
3915
3916 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3917 int ret;
3918
3919 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3920 err_code |= ret;
3921 if (ret) {
3922 if (errmsg && *errmsg) {
3923 indent_msg(&errmsg, 2);
3924 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3925 }
3926 else
3927 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3928 file, linenum, args[cur_arg]);
3929 if (ret & ERR_FATAL)
3930 goto out;
3931 }
3932 cur_arg += 1 + kw->skip;
3933 }
3934 if (*args[cur_arg] != 0) {
3935 char *kws = NULL;
3936
3937 if (!kws_dumped) {
3938 kws_dumped = 1;
3939 bind_dump_kws(&kws);
3940 indent_msg(&kws, 4);
3941 }
3942 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.%s%s\n",
3943 file, linenum, args[cur_arg], cursection,
3944 kws ? " Registered keywords :" : "", kws ? kws: "");
3945 free(kws);
3946 err_code |= ERR_ALERT | ERR_FATAL;
3947 goto out;
3948 }
3949 }
3950 else if (strcmp(args[0], "log") == 0) {
3951 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), &errmsg)) {
3952 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3953 err_code |= ERR_ALERT | ERR_FATAL;
3954 goto out;
3955 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003956 }
3957 else if (strcmp(args[0], "timeout") == 0) {
3958 const char *res;
3959 unsigned timeout;
3960
3961 if (strcmp(args[1], "client") != 0) {
3962 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
3963 err_code |= ERR_ALERT | ERR_FATAL;
3964 goto out;
3965 }
3966
3967 if (*args[2] == 0) {
3968 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
3969 err_code |= ERR_ALERT | ERR_FATAL;
3970 goto out;
3971 }
3972 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3973 if (res == PARSE_TIME_OVER) {
3974 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
3975 }
3976 else if (res == PARSE_TIME_UNDER) {
3977 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
3978 }
3979 else if (res) {
3980 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
3981 return -1;
3982 }
3983
3984 if (res) {
3985 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3986 err_code |= ERR_ALERT | ERR_FATAL;
3987 goto out;
3988 }
3989 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02003990 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02003991 else {
3992 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
3993 err_code |= ERR_ALERT | ERR_ABORT;
3994 goto out;
3995 }
Emeric Brun12941c82020-07-07 14:19:42 +02003996out:
3997 return err_code;
3998}
3999
4000
Willy Tarreau869efd52019-11-15 15:16:57 +01004001/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
4002static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004003{
Willy Tarreau869efd52019-11-15 15:16:57 +01004004 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4005 return 1;
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004006
Willy Tarreau869efd52019-11-15 15:16:57 +01004007 if (!startup_logs)
4008 return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
4009
4010 return ring_attach_cli(startup_logs, appctx);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004011}
4012
4013/* register cli keywords */
4014static struct cli_kw_list cli_kws = {{ },{
4015 { { "show", "startup-logs", NULL },
Willy Tarreau869efd52019-11-15 15:16:57 +01004016 "show startup-logs : report logs emitted during HAProxy startup", cli_parse_show_startup_logs, NULL, NULL },
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004017 {{},}
4018}};
4019
Willy Tarreau0108d902018-11-25 19:14:37 +01004020INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4021
Emeric Brun12941c82020-07-07 14:19:42 +02004022/* config parsers for this section */
4023REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4024
Willy Tarreau082b6282019-05-22 14:42:12 +02004025REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4026REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004027
Willy Tarreaubaaee002006-06-26 02:48:02 +02004028/*
4029 * Local variables:
4030 * c-indent-level: 8
4031 * c-basic-offset: 8
4032 * End:
4033 */