blob: c6ba5e635da186f58dd772ed44bdf8d71ad58eb7 [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] = {
Dragan Dosen43885c72015-10-01 13:18:13 +020059 [LOG_FORMAT_RFC3164] = {
60 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020061 },
62 [LOG_FORMAT_RFC5424] = {
63 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020064 },
65 [LOG_FORMAT_PRIO] = {
66 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010067 },
68 [LOG_FORMAT_SHORT] = {
69 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020070 },
71 [LOG_FORMAT_TIMED] = {
72 .name = "timed",
73 },
74 [LOG_FORMAT_ISO] = {
75 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010076 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010077 [LOG_FORMAT_RAW] = {
78 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010079 },
Dragan Dosen1322d092015-09-22 16:05:32 +020080};
81
Dragan Dosen835b9212016-02-12 13:23:03 +010082/*
83 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020084 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
85 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
86 * that the byte should be escaped. Be careful to always pass bytes from 0 to
87 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010088 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020089long rfc5424_escape_map[(256/8) / sizeof(long)];
90long hdr_encode_map[(256/8) / sizeof(long)];
91long url_encode_map[(256/8) / sizeof(long)];
92long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010093
Dragan Dosen835b9212016-02-12 13:23:03 +010094
Willy Tarreaubaaee002006-06-26 02:48:02 +020095const char *log_facilities[NB_LOG_FACILITIES] = {
96 "kern", "user", "mail", "daemon",
97 "auth", "syslog", "lpr", "news",
98 "uucp", "cron", "auth2", "ftp",
99 "ntp", "audit", "alert", "cron2",
100 "local0", "local1", "local2", "local3",
101 "local4", "local5", "local6", "local7"
102};
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104const char *log_levels[NB_LOG_LEVELS] = {
105 "emerg", "alert", "crit", "err",
106 "warning", "notice", "info", "debug"
107};
108
Willy Tarreau570f2212013-06-10 16:42:09 +0200109const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200110const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111
William Lallemand723b73a2012-02-08 16:37:49 +0100112
113/* log_format */
114struct logformat_type {
115 char *name;
116 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100117 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200118 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100119 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100120 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100121};
122
William Lallemandb7ff6a32012-03-02 14:35:21 +0100123int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
124
William Lallemand723b73a2012-02-08 16:37:49 +0100125/* log_format variable names */
126static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200127 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100128
129 /* please keep these lines sorted ! */
130 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
131 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
132 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
133 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100134 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200135 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200136 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200137 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100138 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200139 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
140 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
141 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
142 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
143 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
144 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200145 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100146 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200147 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000148 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100149 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
150 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200151 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100152 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200153 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100154 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
155 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200156 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200157 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
158 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100159 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
160 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200161 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
162 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100163 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200164 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
165 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
166 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
167 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000168 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
169 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000170 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000171 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
172 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200173 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100174 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200175 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100176 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
177 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100178 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100179 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
180 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
181 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
182 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
183 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200184 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
185 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100186 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200187 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
188 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
189 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100190 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
191 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
192
193 /* The following tags are deprecated and will be removed soon */
194 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
195 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200196 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
197 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
198 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
199 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100200 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
201 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
202 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
203 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
204 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200205 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100206};
207
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200208char 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
209char 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 +0100210char 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 +0100211char *log_format = NULL;
212
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200213/* Default string used for structured-data part in RFC5424 formatted
214 * syslog messages.
215 */
216char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200217
Willy Tarreau13ef7732018-11-12 07:25:28 +0100218/* total number of dropped logs */
219unsigned int dropped_logs = 0;
220
Dragan Dosen59cee972015-09-19 22:09:02 +0200221/* This is a global syslog message buffer, common to all outgoing
222 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100223 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200224THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100225
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200226/* A global syslog message buffer, common to all RFC5424 syslog messages.
227 * Currently, it is used for generating the structured-data part.
228 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200229THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200230
Christopher Fauletd4696382017-10-24 11:44:05 +0200231/* A global buffer used to store all startup alerts/warnings. It will then be
232 * retrieve on the CLI. */
Willy Tarreau869efd52019-11-15 15:16:57 +0100233static struct ring *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200234
William Lallemand723b73a2012-02-08 16:37:49 +0100235struct logformat_var_args {
236 char *name;
237 int mask;
238};
239
240struct logformat_var_args var_args_list[] = {
241// global
242 { "M", LOG_OPT_MANDATORY },
243 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200244 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100245 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100246 { 0, 0 }
247};
248
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200249/* return the name of the directive used in the current proxy for which we're
250 * currently parsing a header, when it is known.
251 */
252static inline const char *fmt_directive(const struct proxy *curproxy)
253{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100254 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200255 case ARGC_ACL:
256 return "acl";
257 case ARGC_STK:
258 return "stick";
259 case ARGC_TRK:
260 return "track-sc";
261 case ARGC_LOG:
262 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200263 case ARGC_LOGSD:
264 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100265 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100266 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100267 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100268 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200269 case ARGC_UIF:
270 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100271 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200272 return "redirect";
273 case ARGC_CAP:
274 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200275 case ARGC_SRV:
276 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200277 case ARGC_SPOE:
278 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100279 case ARGC_UBK:
280 return "use_backend";
Christopher Faulet3b967c12020-05-15 15:47:44 +0200281 case ARGC_HERR:
282 return "http-error";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100283 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200284 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100285 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200286}
287
William Lallemand723b73a2012-02-08 16:37:49 +0100288/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100289 * callback used to configure addr source retrieval
290 */
291int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
292{
293 curproxy->options2 |= PR_O2_SRC_ADDR;
294
295 return 0;
296}
297
298
299/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100300 * Parse args in a logformat_var. Returns 0 in error
301 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100302 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100303int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100304{
305 int i = 0;
306 int end = 0;
307 int flags = 0; // 1 = + 2 = -
308 char *sp = NULL; // start pointer
309
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100310 if (args == NULL) {
311 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100312 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100313 }
William Lallemand723b73a2012-02-08 16:37:49 +0100314
315 while (1) {
316 if (*args == '\0')
317 end = 1;
318
319 if (*args == '+') {
320 // add flag
321 sp = args + 1;
322 flags = 1;
323 }
324 if (*args == '-') {
325 // delete flag
326 sp = args + 1;
327 flags = 2;
328 }
329
330 if (*args == '\0' || *args == ',') {
331 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100332 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100333 if (strcmp(sp, var_args_list[i].name) == 0) {
334 if (flags == 1) {
335 node->options |= var_args_list[i].mask;
336 break;
337 } else if (flags == 2) {
338 node->options &= ~var_args_list[i].mask;
339 break;
340 }
341 }
342 }
343 sp = NULL;
344 if (end)
345 break;
346 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100347 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100348 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100349 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100350}
351
352/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100353 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
354 * must pass the args part in the <arg> pointer with its length in <arg_len>,
355 * and varname with its length in <var> and <var_len> respectively. <arg> is
356 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100357 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100358 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100359int 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 +0100360{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100361 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200362 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100363
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100364 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
365 if (strlen(logformat_keywords[j].name) == var_len &&
366 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
367 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200368 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100369 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100370 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200371 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100372 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100373 node->type = logformat_keywords[j].type;
374 node->options = *defoptions;
375 if (arg_len) {
376 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100377 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200378 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100379 }
380 if (node->type == LOG_FMT_GLOBAL) {
381 *defoptions = node->options;
382 free(node->arg);
383 free(node);
384 } else {
385 if (logformat_keywords[j].config_callback &&
386 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200387 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100388 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100389 curproxy->to_log |= logformat_keywords[j].lw;
390 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100391 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100392 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100393 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
394 curproxy->conf.args.file, curproxy->conf.args.line,
395 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100396 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100397 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100398 memprintf(err, "format variable '%s' is reserved for HTTP mode",
399 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200400 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100401 }
William Lallemand723b73a2012-02-08 16:37:49 +0100402 }
403 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100404
405 j = var[var_len];
406 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100407 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 +0100408 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200409
410 error_free:
411 if (node) {
412 free(node->arg);
413 free(node);
414 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100415 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100416}
417
418/*
419 * push to the logformat linked list
420 *
421 * start: start pointer
422 * end: end text pointer
423 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100424 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100425 *
426 * LOG_TEXT: copy chars from start to end excluding end.
427 *
428*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100429int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100430{
431 char *str;
432
Willy Tarreaua3571662012-12-20 21:59:12 +0100433 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200434 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100435 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100436 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100437 return 0;
438 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200439 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100440 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100441 str[end - start] = '\0';
442 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100443 node->type = LOG_FMT_TEXT; // type string
444 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100445 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200446 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100447 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100448 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100449 return 0;
450 }
William Lallemand1d705562012-03-12 12:46:41 +0100451 node->type = LOG_FMT_SEPARATOR;
452 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100453 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100454 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100455}
456
457/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100458 * Parse the sample fetch expression <text> and add a node to <list_format> upon
459 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100460 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
461 * is passed in <endptr>, it will be updated with the pointer to the first character
462 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100463 *
464 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100465 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100466int 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 +0100467{
468 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200469 struct sample_expr *expr = NULL;
470 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100471 int cmd_arg;
472
473 cmd[0] = text;
474 cmd[1] = "";
475 cmd_arg = 0;
476
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100477 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 +0100478 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100479 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200480 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100481 }
482
Vincent Bernat02779b62016-04-03 13:48:43 +0200483 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100484 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100485 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200486 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100487 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100488 node->type = LOG_FMT_EXPR;
489 node->expr = expr;
490 node->options = options;
491
492 if (arg_len) {
493 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100494 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200495 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100496 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100497 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100498 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
499
Willy Tarreau434c57c2013-01-08 01:10:24 +0100500 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100501 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
502
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100503 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100504 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
505 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200506 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100507 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100508
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200509 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100510 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100511 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100512
William Lallemand65ad6e12014-01-31 15:08:02 +0100513 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
514 * needed with some sample fetches (eg: ssl*). We always set it for
515 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100516 */
517 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200518 if (curpx->http_needed)
519 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100520 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100521 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200522
523 error_free:
524 release_sample_expr(expr);
525 if (node) {
526 free(node->arg);
527 free(node);
528 }
529 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100530}
531
532/*
William Lallemand723b73a2012-02-08 16:37:49 +0100533 * Parse the log_format string and fill a linked list.
534 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200535 * You can set arguments using { } : %{many arguments}varname.
536 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100537 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500538 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100539 * curproxy: the proxy affected
540 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100541 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100542 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100543 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100544 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100545 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100546int 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 +0100547{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100548 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100549 char *arg = NULL; /* start pointer for args */
550 char *var = NULL; /* start pointer for vars */
551 int arg_len = 0;
552 int var_len = 0;
553 int cformat; /* current token format */
554 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100555 struct logformat_node *tmplf, *back;
556
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100557 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100558 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100559 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100560 return 0;
561 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200562 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200563
William Lallemand723b73a2012-02-08 16:37:49 +0100564 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100565 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100566 LIST_DEL(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200567 release_sample_expr(tmplf->expr);
568 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100569 free(tmplf);
570 }
571
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100572 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100573 pformat = cformat;
574
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100575 if (!*str)
576 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100577
Joseph Herlant85b40592018-11-15 12:10:04 -0800578 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100579 * second have all common paths processed at one place. The common paths are the ones
580 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
581 * We use the common LF_INIT state to dispatch to the different final states.
582 */
583 switch (pformat) {
584 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100585 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100586 arg_len = var_len = 0;
587 if (*str == '{') { // optional argument
588 cformat = LF_STARG;
589 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100590 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100591 else if (*str == '[') {
592 cformat = LF_STEXPR;
593 var = str + 1; // store expr in variable name
594 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100595 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100596 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100597 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100598 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100599 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500600 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100601 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100602 /* single '%' followed by blank or digit, send them both */
603 cformat = LF_TEXT;
604 pformat = LF_TEXT; /* finally we include the previous char as well */
605 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600606 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 +0100607 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100608 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100609
610 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100611 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500612 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100613 break;
614
615 case LF_STARG: // text immediately following '%{'
616 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100617 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100618 arg_len = str - arg;
619 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100620 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100621 break;
622
623 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100624 if (*str == '[') {
625 cformat = LF_STEXPR;
626 var = str + 1; // store expr in variable name
627 break;
628 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100629 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100630 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100631 var = str;
632 break;
633 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100634 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100635 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100636
Willy Tarreauc8368452012-12-21 00:09:23 +0100637 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100638 /* the whole sample expression is parsed at once,
639 * returning the pointer to the first character not
640 * part of the expression, which MUST be the trailing
641 * angle bracket.
642 */
643 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
644 goto fail;
645
646 if (*str == ']') {
647 // end of arg, go on with next state
648 cformat = pformat = LF_EDEXPR;
649 sp = str;
650 }
651 else {
652 char c = *str;
653 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100654 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100655 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
656 else
657 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200658 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100659 }
660 break;
661
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100662 case LF_VAR: // text part of a variable name
663 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100664 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100665 cformat = LF_INIT; // not variable name anymore
666 break;
667
Willy Tarreauc8368452012-12-21 00:09:23 +0100668 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100669 cformat = LF_INIT;
670 }
671
672 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
673 switch (*str) {
674 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100675 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200676 case ' ':
677 if (options & LOG_OPT_MERGE_SPACES) {
678 cformat = LF_SEPARATOR;
679 break;
680 }
681 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100682 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100683 }
684 }
685
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100686 if (cformat != pformat || pformat == LF_SEPARATOR) {
687 switch (pformat) {
688 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100689 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100690 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100691 break;
692 case LF_TEXT:
693 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100694 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100695 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100696 break;
697 }
698 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100699 }
700 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100701
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100702 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100703 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100704 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100705 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100706 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100707
708 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100709 fail:
710 free(backfmt);
711 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100712}
713
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200714/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500715 * 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 +0200716 * ranges of indexes. Note that an index may be considered as a particular range
717 * with a high limit to the low limit.
718 */
719int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
720{
721 char *end, *p;
722
723 *low = *high = 0;
724
725 p = *arg;
726 end = strchr(p, ',');
727 if (!end)
728 end = p + strlen(p);
729
730 *high = *low = read_uint((const char **)&p, end);
731 if (!*low || (p != end && *p != '-'))
732 goto err;
733
734 if (p == end)
735 goto done;
736
737 p++;
738 *high = read_uint((const char **)&p, end);
739 if (!*high || *high <= *low || p != end)
740 goto err;
741
742 done:
743 if (*end == ',')
744 end++;
745 *arg = end;
746 return 1;
747
748 err:
749 memprintf(err, "wrong sample range '%s'", *arg);
750 return 0;
751}
752
753/*
754 * Returns 1 if the range defined by <low> and <high> overlaps
755 * one of them in <rgs> array of ranges with <sz> the size of this
756 * array, 0 if not.
757 */
758int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
759 unsigned int low, unsigned int high, char **err)
760{
761 size_t i;
762
763 for (i = 0; i < sz; i++) {
764 if ((low >= rgs[i].low && low <= rgs[i].high) ||
765 (high >= rgs[i].low && high <= rgs[i].high)) {
766 memprintf(err, "ranges are overlapping");
767 return 1;
768 }
769 }
770
771 return 0;
772}
773
774int smp_log_range_cmp(const void *a, const void *b)
775{
776 const struct smp_log_range *rg_a = a;
777 const struct smp_log_range *rg_b = b;
778
779 if (rg_a->high < rg_b->low)
780 return -1;
781 else if (rg_a->low > rg_b->high)
782 return 1;
783
784 return 0;
785}
786
787/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200788 * Parse "log" keyword and update <logsrvs> list accordingly.
789 *
790 * When <do_del> is set, it means the "no log" line was parsed, so all log
791 * servers in <logsrvs> are released.
792 *
793 * Otherwise, we try to parse the "log" line. First of all, when the list is not
794 * the global one, we look for the parameter "global". If we find it,
795 * global.logsrvs is copied. Else we parse each arguments.
796 *
797 * The function returns 1 in success case, otherwise, it returns 0 and err is
798 * filled.
799 */
800int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
801{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100802 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200803 struct sockaddr_storage *sk;
804 struct logsrv *logsrv = NULL;
805 int port1, port2;
806 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200807 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200808
809 /*
810 * "no log": delete previous herited or defined syslog
811 * servers.
812 */
813 if (do_del) {
814 struct logsrv *back;
815
816 if (*(args[1]) != 0) {
817 memprintf(err, "'no log' does not expect arguments");
818 goto error;
819 }
820
821 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
822 LIST_DEL(&logsrv->list);
823 free(logsrv);
824 }
825 return 1;
826 }
827
828 /*
829 * "log global": copy global.logrsvs linked list to the end of logsrvs
830 * list. But first, we check (logsrvs != global.logsrvs).
831 */
832 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
833 if (logsrvs == &global.logsrvs) {
834 memprintf(err, "'global' is not supported for a global syslog server");
835 goto error;
836 }
837 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200838 struct logsrv *node;
839
840 list_for_each_entry(node, logsrvs, list) {
841 if (node->ref == logsrv)
842 goto skip_logsrv;
843 }
844
845 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200846 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200847 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200848 LIST_INIT(&node->list);
849 LIST_ADDQ(logsrvs, &node->list);
Christopher Faulet28ac0992018-03-26 16:09:19 +0200850
851 skip_logsrv:
852 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200853 }
854 return 1;
855 }
856
857 /*
858 * "log <address> ...: parse a syslog server line
859 */
860 if (*(args[1]) == 0 || *(args[2]) == 0) {
861 memprintf(err, "expects <address> and <facility> %s as arguments",
862 ((logsrvs == &global.logsrvs) ? "" : "or global"));
863 goto error;
864 }
865
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100866 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
867 if (strcmp(args[1], "stdout") == 0)
868 args[1] = "fd@1";
869 else if (strcmp(args[1], "stderr") == 0)
870 args[1] = "fd@2";
871
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200872 logsrv = calloc(1, sizeof(*logsrv));
873 if (!logsrv) {
874 memprintf(err, "out of memory");
875 goto error;
876 }
877
878 /* skip address for now, it will be parsed at the end */
879 cur_arg = 2;
880
881 /* just after the address, a length may be specified */
882 logsrv->maxlen = MAX_SYSLOG_LEN;
883 if (strcmp(args[cur_arg], "len") == 0) {
884 int len = atoi(args[cur_arg+1]);
885 if (len < 80 || len > 65535) {
886 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
887 args[cur_arg+1]);
888 goto error;
889 }
890 logsrv->maxlen = len;
891 cur_arg += 2;
892 }
893 if (logsrv->maxlen > global.max_syslog_len)
894 global.max_syslog_len = logsrv->maxlen;
895
896 /* after the length, a format may be specified */
897 if (strcmp(args[cur_arg], "format") == 0) {
898 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200899 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200900 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
901 goto error;
902 }
903 cur_arg += 2;
904 }
905
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200906 if (strcmp(args[cur_arg], "sample") == 0) {
907 unsigned low, high;
908 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200909 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
910
911 p = args[cur_arg+1];
912 smp_sz_str = strchr(p, ':');
913 if (!smp_sz_str) {
914 memprintf(err, "Missing sample size");
915 goto error;
916 }
917
918 *smp_sz_str++ = '\0';
919
920 end = p + strlen(p);
921
922 while (p != end) {
923 if (!get_logsrv_smp_range(&low, &high, &p, err))
924 goto error;
925
926 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
927 goto error;
928
929 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
930 if (!smp_rgs) {
931 memprintf(err, "out of memory error");
932 goto error;
933 }
934
935 smp_rgs[smp_rgs_sz].low = low;
936 smp_rgs[smp_rgs_sz].high = high;
937 smp_rgs[smp_rgs_sz].sz = high - low + 1;
938 smp_rgs[smp_rgs_sz].curr_idx = 0;
939 if (smp_rgs[smp_rgs_sz].high > smp_sz)
940 smp_sz = smp_rgs[smp_rgs_sz].high;
941 smp_rgs_sz++;
942 }
943
Tim Duesterhus21648002019-06-23 22:10:10 +0200944 if (smp_rgs == NULL) {
945 memprintf(err, "no sampling ranges given");
946 goto error;
947 }
948
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200949 beg = smp_sz_str;
950 end = beg + strlen(beg);
951 new_smp_sz = read_uint((const char **)&beg, end);
952 if (!new_smp_sz || beg != end) {
953 memprintf(err, "wrong sample size '%s' for sample range '%s'",
954 smp_sz_str, args[cur_arg+1]);
955 goto error;
956 }
957
958 if (new_smp_sz < smp_sz) {
959 memprintf(err, "sample size %zu should be greater or equal to "
960 "%zu the maximum of the high ranges limits",
961 new_smp_sz, smp_sz);
962 goto error;
963 }
964 smp_sz = new_smp_sz;
965
966 /* Let's order <smp_rgs> array. */
967 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
968
969 logsrv->lb.smp_rgs = smp_rgs;
970 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
971 logsrv->lb.smp_sz = smp_sz;
972
973 cur_arg += 2;
974 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200975 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200976 /* parse the facility */
977 logsrv->facility = get_log_facility(args[cur_arg]);
978 if (logsrv->facility < 0) {
979 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
980 goto error;
981 }
982 cur_arg++;
983
984 /* parse the max syslog level (default: debug) */
985 logsrv->level = 7;
986 if (*(args[cur_arg])) {
987 logsrv->level = get_log_level(args[cur_arg]);
988 if (logsrv->level < 0) {
989 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
990 goto error;
991 }
992 cur_arg++;
993 }
994
995 /* parse the limit syslog level (default: emerg) */
996 logsrv->minlvl = 0;
997 if (*(args[cur_arg])) {
998 logsrv->minlvl = get_log_level(args[cur_arg]);
999 if (logsrv->minlvl < 0) {
1000 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1001 goto error;
1002 }
1003 cur_arg++;
1004 }
1005
1006 /* Too many args */
1007 if (*(args[cur_arg])) {
1008 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1009 goto error;
1010 }
1011
1012 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001013 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001014 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001015 logsrv->addr.ss_family = AF_UNSPEC;
1016 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001017 logsrv->sink = NULL;
1018 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001019 goto done;
1020 }
1021
Willy Tarreau5fc93282020-09-16 18:25:03 +02001022 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, NULL,
Willy Tarreaua93e5c72020-09-15 14:01:16 +02001023 err, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001024 if (!sk)
1025 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001026
1027 if (fd != -1)
1028 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001029 logsrv->addr = *sk;
1030
1031 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001032 logsrv->addr = *sk;
1033 if (!port1)
1034 set_host_port(&logsrv->addr, SYSLOG_PORT);
1035 }
Willy Tarreauc046d162019-08-30 15:24:59 +02001036 done:
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001037 LIST_ADDQ(logsrvs, &logsrv->list);
1038 return 1;
1039
1040 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001041 free(smp_rgs);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001042 if (logsrv)
1043 free(logsrv->ring_name);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001044 free(logsrv);
1045 return 0;
1046}
1047
1048
Christopher Fauletd4696382017-10-24 11:44:05 +02001049/* Generic function to display messages prefixed by a label */
1050static void print_message(const char *label, const char *fmt, va_list argp)
1051{
1052 struct tm tm;
1053 char *head, *msg;
1054
1055 head = msg = NULL;
1056
1057 get_localtime(date.tv_sec, &tm);
1058 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
1059 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
1060 memvprintf(&msg, fmt, argp);
1061
Willy Tarreau869efd52019-11-15 15:16:57 +01001062 if (global.mode & MODE_STARTING) {
1063 if (unlikely(!startup_logs))
1064 startup_logs = ring_new(STARTUP_LOG_SIZE);
1065
1066 if (likely(startup_logs)) {
1067 struct ist m[2];
1068
1069 m[0] = ist(head);
1070 m[1] = ist(msg);
1071 /* trim the trailing '\n' */
1072 if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
1073 m[1].len--;
1074 ring_write(startup_logs, ~0, 0, 0, m, 2);
1075 }
1076 }
Christopher Fauletd4696382017-10-24 11:44:05 +02001077
1078 fprintf(stderr, "%s%s", head, msg);
1079 fflush(stderr);
1080
1081 free(head);
1082 free(msg);
1083}
1084
Willy Tarreaubaaee002006-06-26 02:48:02 +02001085/*
1086 * Displays the message on stderr with the date and pid. Overrides the quiet
1087 * mode during startup.
1088 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001089void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001090{
1091 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001092
1093 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaubb869862020-04-16 10:52:41 +02001094 if (!(warned & WARN_EXEC_PATH)) {
1095 const char *path = get_exec_path();
1096
1097 warned |= WARN_EXEC_PATH;
1098 ha_notice("haproxy version is %s\n", haproxy_version);
1099 if (path)
1100 ha_notice("path to executable is %s\n", path);
1101 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001102 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001103 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104 va_end(argp);
1105 }
1106}
1107
1108
1109/*
1110 * Displays the message on stderr with the date and pid.
1111 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001112void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113{
1114 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001115
Willy Tarreaubebd2122020-04-15 16:06:11 +02001116 warned |= WARN_ANY;
1117
Willy Tarreaubaaee002006-06-26 02:48:02 +02001118 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1119 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001120 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001121 va_end(argp);
1122 }
1123}
1124
1125/*
William Lallemand9c56a222018-11-21 18:04:52 +01001126 * Displays the message on stderr with the date and pid.
1127 */
1128void ha_notice(const char *fmt, ...)
1129{
1130 va_list argp;
1131
1132 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1133 va_start(argp, fmt);
1134 print_message("NOTICE", fmt, argp);
1135 va_end(argp);
1136 }
1137}
1138
1139/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001140 * Displays the message on <out> only if quiet mode is not set.
1141 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001142void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001143{
1144 va_list argp;
1145
1146 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1147 va_start(argp, fmt);
1148 vfprintf(out, fmt, argp);
1149 fflush(out);
1150 va_end(argp);
1151 }
1152}
1153
1154/*
Emeric Brun54648852020-07-06 15:54:06 +02001155 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001156 */
Emeric Brun54648852020-07-06 15:54:06 +02001157enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001158{
Emeric Brun54648852020-07-06 15:54:06 +02001159 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001160
1161 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001162 while (format > 0 && log_formats[format].name
1163 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +02001164 format--;
1165
Emeric Brun54648852020-07-06 15:54:06 +02001166 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001167 return format;
1168}
1169
1170/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001171 * returns log level for <lev> or -1 if not found.
1172 */
1173int get_log_level(const char *lev)
1174{
1175 int level;
1176
1177 level = NB_LOG_LEVELS - 1;
1178 while (level >= 0 && strcmp(log_levels[level], lev))
1179 level--;
1180
1181 return level;
1182}
1183
Willy Tarreaubaaee002006-06-26 02:48:02 +02001184/*
1185 * returns log facility for <fac> or -1 if not found.
1186 */
1187int get_log_facility(const char *fac)
1188{
1189 int facility;
1190
1191 facility = NB_LOG_FACILITIES - 1;
1192 while (facility >= 0 && strcmp(log_facilities[facility], fac))
1193 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001194
Willy Tarreaubaaee002006-06-26 02:48:02 +02001195 return facility;
1196}
1197
William Lallemanda1cc3812012-02-08 16:38:44 +01001198/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001199 * Encode the string.
1200 *
1201 * When using the +E log format option, it will try to escape '"\]'
1202 * characters with '\' as prefix. The same prefix should not be used as
1203 * <escape>.
1204 */
1205static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001206 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001207 const char *string,
1208 struct logformat_node *node)
1209{
1210 if (node->options & LOG_OPT_ESC) {
1211 if (start < stop) {
1212 stop--; /* reserve one byte for the final '\0' */
1213 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001214 if (!ha_bit_test((unsigned char)(*string), map)) {
1215 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001216 *start++ = *string;
1217 else {
1218 if (start + 2 >= stop)
1219 break;
1220 *start++ = '\\';
1221 *start++ = *string;
1222 }
1223 }
1224 else {
1225 if (start + 3 >= stop)
1226 break;
1227 *start++ = escape;
1228 *start++ = hextab[(*string >> 4) & 15];
1229 *start++ = hextab[*string & 15];
1230 }
1231 string++;
1232 }
1233 *start = '\0';
1234 }
1235 }
1236 else {
1237 return encode_string(start, stop, escape, map, string);
1238 }
1239
1240 return start;
1241}
1242
1243/*
1244 * Encode the chunk.
1245 *
1246 * When using the +E log format option, it will try to escape '"\]'
1247 * characters with '\' as prefix. The same prefix should not be used as
1248 * <escape>.
1249 */
1250static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001251 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001252 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001253 struct logformat_node *node)
1254{
1255 char *str, *end;
1256
1257 if (node->options & LOG_OPT_ESC) {
1258 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001259 str = chunk->area;
1260 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001261
1262 stop--; /* reserve one byte for the final '\0' */
1263 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001264 if (!ha_bit_test((unsigned char)(*str), map)) {
1265 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001266 *start++ = *str;
1267 else {
1268 if (start + 2 >= stop)
1269 break;
1270 *start++ = '\\';
1271 *start++ = *str;
1272 }
1273 }
1274 else {
1275 if (start + 3 >= stop)
1276 break;
1277 *start++ = escape;
1278 *start++ = hextab[(*str >> 4) & 15];
1279 *start++ = hextab[*str & 15];
1280 }
1281 str++;
1282 }
1283 *start = '\0';
1284 }
1285 }
1286 else {
1287 return encode_chunk(start, stop, escape, map, chunk);
1288 }
1289
1290 return start;
1291}
1292
1293/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001294 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001295 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001296 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001297 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001298 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001299char *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 +01001300{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001301 if (size < 2)
1302 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001303
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001304 if (node->options & LOG_OPT_QUOTE) {
1305 *(dst++) = '"';
1306 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001307 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001308
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001309 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001310 if (++len > size)
1311 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001312 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001313 char *ret;
1314
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001315 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001316 if (ret == NULL || *ret != '\0')
1317 return NULL;
1318 len = ret - dst;
1319 }
1320 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001321 len = strlcpy2(dst, src, len);
1322 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001323
1324 size -= len;
1325 dst += len;
1326 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001327 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1328 if (size < 2)
1329 return NULL;
1330 *(dst++) = '-';
1331 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001332
1333 if (node->options & LOG_OPT_QUOTE) {
1334 if (size < 2)
1335 return NULL;
1336 *(dst++) = '"';
1337 }
1338
1339 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001340 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001341}
1342
Willy Tarreau26ffa852018-09-05 15:23:10 +02001343static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001344{
1345 return lf_text_len(dst, src, size, size, node);
1346}
1347
William Lallemand5f232402012-04-05 18:02:55 +02001348/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001349 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001350 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001351 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001352char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001353{
1354 char *ret = dst;
1355 int iret;
1356 char pn[INET6_ADDRSTRLEN];
1357
1358 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001359 unsigned char *addr = NULL;
1360 switch (sockaddr->sa_family) {
1361 case AF_INET:
1362 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1363 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1364 break;
1365 case AF_INET6:
1366 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1367 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1368 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1369 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1370 break;
1371 default:
1372 return NULL;
1373 }
William Lallemand5f232402012-04-05 18:02:55 +02001374 if (iret < 0 || iret > size)
1375 return NULL;
1376 ret += iret;
1377 } else {
1378 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1379 ret = lf_text(dst, pn, size, node);
1380 if (ret == NULL)
1381 return NULL;
1382 }
1383 return ret;
1384}
1385
1386/*
1387 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001388 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001389 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001390char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001391{
1392 char *ret = dst;
1393 int iret;
1394
1395 if (node->options & LOG_OPT_HEXA) {
1396 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1397 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1398 if (iret < 0 || iret > size)
1399 return NULL;
1400 ret += iret;
1401 } else {
1402 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1403 if (ret == NULL)
1404 return NULL;
1405 }
1406 return ret;
1407}
1408
Emeric Brun54648852020-07-06 15:54:06 +02001409
1410/*
1411 * This function sends the syslog message using a printf format string. It
1412 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001413 */
Emeric Brun54648852020-07-06 15:54:06 +02001414void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001415{
Emeric Brun54648852020-07-06 15:54:06 +02001416 va_list argp;
1417 int data_len;
1418
1419 if (level < 0 || format == NULL || logline == NULL)
1420 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001421
Emeric Brun54648852020-07-06 15:54:06 +02001422 va_start(argp, format);
1423 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1424 if (data_len < 0 || data_len > global.max_syslog_len)
1425 data_len = global.max_syslog_len;
1426 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001427
Emeric Brun54648852020-07-06 15:54:06 +02001428 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1429 logline, data_len, default_rfc5424_sd_log_format, 2);
1430}
1431/*
1432 * This function builds a log header of given format using given
1433 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1434 * to determine format based on given metadas. It is useful
1435 * for log-forwarding to be able to forward any format without
1436 * settings.
1437 * This function returns a struct ist array of elements of the header
1438 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001439 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001440 * elements.
1441 */
1442struct ist *build_log_header(enum log_fmt format, int level, int facility,
1443 struct ist *metadata, size_t *nbelem)
1444{
1445 static THREAD_LOCAL struct {
1446 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1447 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1448 time_t cur_legacy_time;
1449 char priority_buffer[6];
1450 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001451
Emeric Brun54648852020-07-06 15:54:06 +02001452 struct tm logtime;
1453 int len;
1454 int fac_level = 0;
1455 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001456
Emeric Brun54648852020-07-06 15:54:06 +02001457 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001458
Emeric Brun54648852020-07-06 15:54:06 +02001459
1460 if (format == LOG_FORMAT_UNSPEC) {
1461 format = LOG_FORMAT_RAW;
1462 if (metadata) {
1463 /* If a hostname is set, it appears we want to perform syslog
1464 * because only rfc5427 or rfc3164 support an hostname.
1465 */
1466 if (metadata[LOG_META_HOST].len) {
1467 /* If a rfc5424 compliant timestamp is used we consider
1468 * that output format is rfc5424, else legacy format
1469 * is used as specified default for local logs
1470 * in documentation.
1471 */
1472 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1473 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1474 format = LOG_FORMAT_RFC5424;
1475 else
1476 format = LOG_FORMAT_RFC3164;
1477 }
1478 else if (metadata[LOG_META_PRIO].len) {
1479 /* the source seems a parsed message
1480 * offering a valid level/prio prefix
1481 * so we consider this format.
1482 */
1483 format = LOG_FORMAT_PRIO;
1484 }
1485 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001486 }
1487
Emeric Brun54648852020-07-06 15:54:06 +02001488 /* prepare priority, stored into 1 single elem */
1489 switch (format) {
1490 case LOG_FORMAT_RFC3164:
1491 case LOG_FORMAT_RFC5424:
1492 case LOG_FORMAT_PRIO:
1493 fac_level = facility << 3;
1494 /* further format ignore the facility */
1495 /* fall through */
1496 case LOG_FORMAT_TIMED:
1497 case LOG_FORMAT_SHORT:
1498 fac_level += level;
1499 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1500 do {
1501 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1502 fac_level /= 10;
1503 hdr_ctx.ist_vector[*nbelem].ptr--;
1504 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1505 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1506 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1507 break;
1508 case LOG_FORMAT_ISO:
1509 case LOG_FORMAT_RAW:
1510 break;
1511 case LOG_FORMAT_UNSPEC:
1512 case LOG_FORMATS:
1513 ABORT_NOW();
1514 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001515
William Lallemand2a4a44f2012-02-06 16:00:33 +01001516
Emeric Brun54648852020-07-06 15:54:06 +02001517 /* prepare timestamp, stored into a max of 4 elems */
1518 switch (format) {
1519 case LOG_FORMAT_RFC3164:
1520 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1521 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1522 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1523 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001524 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001525 break;
1526 }
1527 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1528 int month;
1529 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001530
Emeric Brun54648852020-07-06 15:54:06 +02001531 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001532
Emeric Brun54648852020-07-06 15:54:06 +02001533 /* compute month */
1534 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1535 if (month)
1536 month--;
1537 if (month <= 11) {
1538 /* builds log prefix ex: 'Jan 1 ' */
1539 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1540 "%s %c%c ", monthname[month],
1541 timestamp[8] != '0' ? timestamp[8] : ' ',
1542 timestamp[9]);
1543 /* we reused the timestamp_buffer, signal that it does not
1544 * contain local time anymore
1545 */
1546 hdr_ctx.cur_legacy_time = 0;
1547 if (len == 7) {
1548 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1549 /* adds 'HH:MM:SS' from iso time */
1550 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1551 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1552 /* we successfully reuse iso time, we can break */
1553 break;
1554 }
1555 }
1556 /* Failed to reuse isotime time, fallback to local legacy time */
1557 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001558
Emeric Brun54648852020-07-06 15:54:06 +02001559 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1560 /* re-builds timestamp from the current local time */
1561 get_localtime(time, &logtime);
1562
1563 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1564 "%s %2d %02d:%02d:%02d ",
1565 monthname[logtime.tm_mon],
1566 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1567 if (len != LOG_LEGACYTIME_LEN+1)
1568 hdr_ctx.cur_legacy_time = 0;
1569 else
1570 hdr_ctx.cur_legacy_time = time;
1571 }
1572 if (likely(hdr_ctx.cur_legacy_time))
1573 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1574 else
1575 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1576 break;
1577 case LOG_FORMAT_RFC5424:
1578 /* adds rfc5425 version prefix */
1579 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1580 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001581 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001582 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1583 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1584 break;
1585 }
1586 /* let continue as 'timed' and 'iso' format for usual timestamp */
1587 /* fall through */
1588 case LOG_FORMAT_TIMED:
1589 case LOG_FORMAT_ISO:
1590 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1591 * '1900:01:01T14:00:00+02:00'
1592 * '1900:01:01T10:00:00.123456-02:00'
1593 */
1594 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1595 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1596 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001597 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001598 break;
1599 }
1600 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1601 int month;
1602 char *timestamp = metadata[LOG_META_TIME].ptr;
1603
1604 for (month = 0; month < 12; month++)
1605 if (!memcmp(monthname[month], timestamp, 3))
1606 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001607
Emeric Brun54648852020-07-06 15:54:06 +02001608 if (month < 12) {
1609
1610 /* get local time to retrieve year */
1611 get_localtime(time, &logtime);
1612
1613 /* year seems changed since log */
1614 if (logtime.tm_mon < month)
1615 logtime.tm_year--;
1616
1617 /* builds rfc5424 prefix ex: '1900-01-01T' */
1618 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1619 "%4d-%02d-%c%cT",
1620 logtime.tm_year+1900, month+1,
1621 timestamp[4] != ' ' ? timestamp[4] : '0',
1622 timestamp[5]);
1623
1624 /* we reused the timestamp_buffer, signal that it does not
1625 * contain local time anymore
1626 */
1627 hdr_ctx.cur_legacy_time = 0;
1628 if (len == 11) {
1629 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1630 /* adds HH:MM:SS from legacy timestamp */
1631 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001632 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001633 /* according to rfc: -00:00 means we don't know the timezone */
1634 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1635 /* we successfully reuse legacy time, we can break */
1636 break;
1637 }
1638 }
1639 /* Failed to reuse legacy time, fallback to local iso time */
1640 }
1641 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1642 break;
1643 case LOG_FORMAT_PRIO:
1644 case LOG_FORMAT_SHORT:
1645 case LOG_FORMAT_RAW:
1646 break;
1647 case LOG_FORMAT_UNSPEC:
1648 case LOG_FORMATS:
1649 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001650 }
1651
Emeric Brun54648852020-07-06 15:54:06 +02001652 /* prepare other meta data, stored into a max of 10 elems */
1653 switch (format) {
1654 case LOG_FORMAT_RFC3164:
1655 if (metadata && metadata[LOG_META_HOST].len) {
1656 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1657 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1658 }
1659 else /* the caller MUST fill the hostname */
1660 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun9f9b22c2020-07-02 16:16:59 +02001661
Emeric Brun54648852020-07-06 15:54:06 +02001662 if (!metadata || !metadata[LOG_META_TAG].len)
1663 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001664
Emeric Brun54648852020-07-06 15:54:06 +02001665 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1666 if (metadata[LOG_META_PID].len) {
1667 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1668 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1669 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1670 }
1671 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1672 break;
1673 case LOG_FORMAT_RFC5424:
1674 if (metadata && metadata[LOG_META_HOST].len) {
1675 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1676 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1677 }
1678 else
1679 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001680
Emeric Brun54648852020-07-06 15:54:06 +02001681 if (metadata && metadata[LOG_META_TAG].len) {
1682 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1683 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1684 }
1685 else
1686 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001687
Emeric Brun54648852020-07-06 15:54:06 +02001688 if (metadata && metadata[LOG_META_PID].len) {
1689 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1690 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1691 }
1692 else
1693 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001694
Emeric Brun54648852020-07-06 15:54:06 +02001695 if (metadata && metadata[LOG_META_MSGID].len) {
1696 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
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_STDATA].len) {
1703 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1704 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1705 }
1706 else
1707 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1708 break;
1709 case LOG_FORMAT_PRIO:
1710 case LOG_FORMAT_SHORT:
1711 case LOG_FORMAT_TIMED:
1712 case LOG_FORMAT_ISO:
1713 case LOG_FORMAT_RAW:
1714 break;
1715 case LOG_FORMAT_UNSPEC:
1716 case LOG_FORMATS:
1717 ABORT_NOW();
1718 }
1719
1720 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001721}
1722
1723/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001724 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001725 * The argument <metadata> MUST be an array of size
1726 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001727 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001728 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001729 */
Emeric Brun54648852020-07-06 15:54:06 +02001730static 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 +01001731{
Emeric Brun54648852020-07-06 15:54:06 +02001732 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001733 static THREAD_LOCAL struct msghdr msghdr = {
1734 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001735 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001736 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001737 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1738 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001739 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001740 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001741 size_t nbelem;
1742 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001743
1744 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001745
Emeric Brunfa9d7802020-05-28 14:21:33 +02001746 /* historically some messages used to already contain the trailing LF
1747 * or Zero. Let's remove all trailing LF or Zero
1748 */
Emeric Brun54648852020-07-06 15:54:06 +02001749 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001750 size--;
1751
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001752 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001753 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001754 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001755 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001756 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1757 /* the socket's address is a file descriptor */
1758 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1759 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001760 else if (logsrv->addr.ss_family == AF_UNIX)
1761 plogfd = &logfdunix;
1762 else
1763 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001764
Willy Tarreauc046d162019-08-30 15:24:59 +02001765 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001766 /* socket not successfully initialized yet */
1767 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1768 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1769 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001770
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001771 if (!once) {
1772 once = 1; /* note: no need for atomic ops here */
1773 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1774 nblogger, strerror(errno), errno);
1775 }
1776 return;
1777 } else {
1778 /* we don't want to receive anything on this socket */
1779 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1780 /* does nothing under Linux, maybe needed for others */
1781 shutdown(*plogfd, SHUT_RD);
1782 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1783 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001784 }
1785
Emeric Brun54648852020-07-06 15:54:06 +02001786 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1787 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001788 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001789 struct ist msg;
1790
1791 msg = ist2(message, size);
1792 if (msg.len > logsrv->maxlen)
1793 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001794
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001795 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1796 }
1797 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1798 struct ist msg;
1799
1800 msg = ist2(message, size);
1801 if (msg.len > logsrv->maxlen)
1802 msg.len = logsrv->maxlen;
1803
1804 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001805 }
1806 else {
Emeric Brun54648852020-07-06 15:54:06 +02001807 int i = 0;
1808 int totlen = logsrv->maxlen;
1809
1810 for (i = 0 ; i < nbelem ; i++ ) {
1811 iovec[i].iov_base = msg_header[i].ptr;
1812 iovec[i].iov_len = msg_header[i].len;
1813 if (totlen <= iovec[i].iov_len) {
1814 iovec[i].iov_len = totlen;
1815 totlen = 0;
1816 break;
1817 }
1818 totlen -= iovec[i].iov_len;
1819 }
1820 if (totlen) {
1821 iovec[i].iov_base = message;
1822 iovec[i].iov_len = size;
1823 if (totlen <= iovec[i].iov_len)
1824 iovec[i].iov_len = totlen;
1825 i++;
1826 }
1827 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1828 iovec[i].iov_len = 1;
1829 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001830
Emeric Brun54648852020-07-06 15:54:06 +02001831 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001832 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1833 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001834
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001835 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1836 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001837
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001838 if (sent < 0) {
1839 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001840
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001841 if (errno == EAGAIN)
1842 _HA_ATOMIC_ADD(&dropped_logs, 1);
1843 else if (!once) {
1844 once = 1; /* note: no need for atomic ops here */
1845 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1846 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001847 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001848 }
1849}
Dragan Dosen59cee972015-09-19 22:09:02 +02001850
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001851/*
1852 * This function sends a syslog message.
1853 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001854 * The argument <metadata> MUST be an array of size
1855 * LOG_META_FIELDS*sizeof(struct ist) containing
1856 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001857 */
Emeric Brun54648852020-07-06 15:54:06 +02001858void process_send_log(struct list *logsrvs, int level, int facility,
1859 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001860{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001861 struct logsrv *logsrv;
1862 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001863
1864 /* Send log messages to syslog server. */
1865 nblogger = 0;
1866 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001867 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001868
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001869 /* we can filter the level of the messages that are sent to each logger */
1870 if (level > logsrv->level)
1871 continue;
1872
Frédéric Lécailled803e472019-04-25 07:42:09 +02001873 if (logsrv->lb.smp_rgs) {
1874 struct smp_log_range *curr_rg;
1875
1876 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1877 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1878 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1879 if (in_range) {
1880 /* Let's consume this range. */
1881 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1882 if (!curr_rg->curr_idx) {
1883 /* If consumed, let's select the next range. */
1884 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1885 }
1886 }
1887 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1888 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1889 }
1890 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001891 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1892 (facility == -1) ? logsrv->facility : facility,
1893 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001894 }
1895}
1896
Emeric Brun54648852020-07-06 15:54:06 +02001897/*
1898 * This function sends a syslog message.
1899 * It doesn't care about errors nor does it report them.
1900 * The arguments <sd> and <sd_size> are used for the structured-data part
1901 * in RFC5424 formatted syslog messages.
1902 */
1903void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1904 char *message, size_t size, char *sd, size_t sd_size)
1905{
1906 static THREAD_LOCAL pid_t curr_pid;
1907 static THREAD_LOCAL char pidstr[16];
1908 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1909
1910 if (logsrvs == NULL) {
1911 if (!LIST_ISEMPTY(&global.logsrvs)) {
1912 logsrvs = &global.logsrvs;
1913 }
1914 }
1915 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1916 return;
1917
1918 if (!metadata[LOG_META_HOST].len) {
1919 if (global.log_send_hostname)
1920 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
1921 else
1922 metadata[LOG_META_HOST] = ist2(hostname, strlen(hostname));
1923 }
1924
1925 if (!tagb || !tagb->area)
1926 tagb = &global.log_tag;
1927
1928 if (tagb)
1929 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1930
1931 if (unlikely(curr_pid != getpid()))
1932 metadata[LOG_META_PID].len = 0;
1933
1934 if (!metadata[LOG_META_PID].len) {
1935 curr_pid = getpid();
1936 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1937 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1938 }
1939
1940 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1941
1942 /* Remove trailing space of structured data */
1943 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1944 metadata[LOG_META_STDATA].len--;
1945
1946 return process_send_log(logsrvs, level, -1, metadata, message, size);
1947}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001948
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001949const 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 +01001950const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1951 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1952 Set-cookie Updated, unknown, unknown */
1953
William Lallemand1d705562012-03-12 12:46:41 +01001954/*
1955 * try to write a character if there is enough space, or goto out
1956 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001957#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001958 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001959 *(tmplog++) = (x); \
1960 } else { \
1961 goto out; \
1962 } \
1963 } while(0)
1964
Dragan Dosen835b9212016-02-12 13:23:03 +01001965
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001966/* Initializes some log data at boot */
1967static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001968{
1969 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001970 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001971
1972 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1973 * inside PARAM-VALUE should be escaped with '\' as prefix.
1974 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1975 * details.
1976 */
1977 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1978
1979 tmp = "\"\\]";
1980 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001981 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001982 tmp++;
1983 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001984
1985 /* initialize the log header encoding map : '{|}"#' should be encoded with
1986 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1987 * URL encoding only requires '"', '#' to be encoded as well as non-
1988 * printable characters above.
1989 */
1990 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1991 memset(url_encode_map, 0, sizeof(url_encode_map));
1992 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001993 ha_bit_set(i, hdr_encode_map);
1994 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001995 }
1996 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001997 ha_bit_set(i, hdr_encode_map);
1998 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001999 }
2000
2001 tmp = "\"#{|}";
2002 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002003 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002004 tmp++;
2005 }
2006
2007 tmp = "\"#";
2008 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002009 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002010 tmp++;
2011 }
2012
2013 /* initialize the http header encoding map. The draft httpbis define the
2014 * header content as:
2015 *
2016 * HTTP-message = start-line
2017 * *( header-field CRLF )
2018 * CRLF
2019 * [ message-body ]
2020 * header-field = field-name ":" OWS field-value OWS
2021 * field-value = *( field-content / obs-fold )
2022 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2023 * obs-fold = CRLF 1*( SP / HTAB )
2024 * field-vchar = VCHAR / obs-text
2025 * VCHAR = %x21-7E
2026 * obs-text = %x80-FF
2027 *
2028 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
2029 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08002030 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02002031 */
2032 memset(http_encode_map, 0, sizeof(http_encode_map));
2033 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002034 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002035 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002036 ha_bit_set(i, http_encode_map);
2037 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002038}
William Lallemand1d705562012-03-12 12:46:41 +01002039
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002040INITCALL0(STG_PREPARE, init_log);
2041
Christopher Faulet0132d062017-07-26 15:33:35 +02002042/* Initialize log buffers used for syslog messages */
2043int init_log_buffers()
2044{
Christopher Faulet0132d062017-07-26 15:33:35 +02002045 logline = my_realloc2(logline, global.max_syslog_len + 1);
2046 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02002047 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02002048 return 0;
2049 return 1;
2050}
2051
2052/* Deinitialize log buffers used for syslog messages */
2053void deinit_log_buffers()
2054{
Christopher Faulet0132d062017-07-26 15:33:35 +02002055 free(logline);
2056 free(logline_rfc5424);
Willy Tarreau869efd52019-11-15 15:16:57 +01002057 ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
Christopher Faulet0132d062017-07-26 15:33:35 +02002058 logline = NULL;
2059 logline_rfc5424 = NULL;
2060}
2061
Willy Tarreaudf974472012-12-28 02:44:01 +01002062/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2063 * <maxsize> characters. Returns the size of the output string in characters,
2064 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002065 * is not zero. It requires a valid session and optionally a stream. If the
2066 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002067 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002068int 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 +02002069{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002070 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002071 struct proxy *be;
2072 struct http_txn *txn;
2073 const struct strm_logs *logs;
Willy Tarreau8fa99842019-07-17 11:47:11 +02002074 struct connection *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002075 unsigned int s_flags;
2076 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002077 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002078 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002079 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002080 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002081 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002082 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002083 int t_request;
2084 int hdr;
2085 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002086 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002087 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002088 char *ret;
2089 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002090 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002091 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002092 struct strm_logs tmp_strm_log;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002093
William Lallemandbddd4fd2012-02-27 11:23:10 +01002094 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002095
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002096 if (likely(s)) {
2097 be = s->be;
2098 txn = s->txn;
2099 be_conn = cs_conn(objt_cs(s->si[1].end));
2100 s_flags = s->flags;
2101 uniq_id = s->uniq_id;
2102 logs = &s->logs;
2103 } else {
2104 /* we have no stream so we first need to initialize a few
2105 * things that are needed later. We do increment the request
2106 * ID so that it's uniquely assigned to this request just as
2107 * if the request had reached the point of being processed.
2108 * A request error is reported as it's the only element we have
2109 * here and which justifies emitting such a log.
2110 */
2111 be = fe;
2112 txn = NULL;
2113 be_conn = NULL;
2114 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01002115 uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002116
2117 /* prepare a valid log structure */
2118 tmp_strm_log.tv_accept = sess->tv_accept;
2119 tmp_strm_log.accept_date = sess->accept_date;
2120 tmp_strm_log.t_handshake = sess->t_handshake;
2121 tmp_strm_log.t_idle = tv_ms_elapsed(&sess->tv_accept, &now) - sess->t_handshake;
2122 tv_zero(&tmp_strm_log.tv_request);
2123 tmp_strm_log.t_queue = -1;
2124 tmp_strm_log.t_connect = -1;
2125 tmp_strm_log.t_data = -1;
2126 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2127 tmp_strm_log.bytes_in = 0;
2128 tmp_strm_log.bytes_out = 0;
2129 tmp_strm_log.prx_queue_pos = 0;
2130 tmp_strm_log.srv_queue_pos = 0;
2131
2132 logs = &tmp_strm_log;
2133 }
2134
William Lallemandbddd4fd2012-02-27 11:23:10 +01002135 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002136 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2137 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002138
William Lallemand1d705562012-03-12 12:46:41 +01002139 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002140
William Lallemandbddd4fd2012-02-27 11:23:10 +01002141 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002142 if (LIST_ISEMPTY(list_format))
2143 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002144
William Lallemand1d705562012-03-12 12:46:41 +01002145 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002146 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002147 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002148 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002149 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002150
Willy Tarreauc8368452012-12-21 00:09:23 +01002151 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002152 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002153 if (!last_isspace) {
2154 LOGCHAR(' ');
2155 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002156 }
2157 break;
2158
William Lallemand1d705562012-03-12 12:46:41 +01002159 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002160 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002161 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002162 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002163 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002164 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002165 last_isspace = 0;
2166 break;
2167
Willy Tarreauc8368452012-12-21 00:09:23 +01002168 case LOG_FMT_EXPR: // sample expression, may be request or response
2169 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002170 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002171 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 +02002172 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002173 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 +01002174 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002175 ret = lf_encode_chunk(tmplog, dst + maxsize,
2176 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002177 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002178 ret = lf_text_len(tmplog,
2179 key ? key->data.u.str.area : NULL,
2180 key ? key->data.u.str.data : 0,
2181 dst + maxsize - tmplog,
2182 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002183 if (ret == 0)
2184 goto out;
2185 tmplog = ret;
2186 last_isspace = 0;
2187 break;
2188
Willy Tarreau2beef582012-12-20 17:22:52 +01002189 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002190 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002191 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002192 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002193 else
2194 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002195 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002196 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002197 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002198 last_isspace = 0;
2199 break;
2200
Willy Tarreau2beef582012-12-20 17:22:52 +01002201 case LOG_FMT_CLIENTPORT: // %cp
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 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002205 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002206 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002207 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002208 dst + maxsize - tmplog, tmp);
2209 }
William Lallemand5f232402012-04-05 18:02:55 +02002210 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002211 else
2212 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2213
William Lallemand5f232402012-04-05 18:02:55 +02002214 if (ret == NULL)
2215 goto out;
2216 tmplog = ret;
2217 last_isspace = 0;
2218 break;
2219
Willy Tarreau2beef582012-12-20 17:22:52 +01002220 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002221 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002222 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002223 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002224 }
2225 else
2226 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2227
William Lallemand1d705562012-03-12 12:46:41 +01002228 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002229 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002230 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002231 last_isspace = 0;
2232 break;
2233
Willy Tarreau2beef582012-12-20 17:22:52 +01002234 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002235 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002236 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002237 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002238 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002239 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002240 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002241 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002242 else
2243 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2244
William Lallemand5f232402012-04-05 18:02:55 +02002245 if (ret == NULL)
2246 goto out;
2247 tmplog = ret;
2248 last_isspace = 0;
2249 break;
2250
Willy Tarreau2beef582012-12-20 17:22:52 +01002251 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002252 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002253 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002254 else
2255 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2256
William Lallemand1d705562012-03-12 12:46:41 +01002257 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002258 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002259 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002260 last_isspace = 0;
2261 break;
2262
Willy Tarreau2beef582012-12-20 17:22:52 +01002263 case LOG_FMT_BACKENDPORT: // %bp
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_port(tmplog, (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 Lallemand5f232402012-04-05 18:02:55 +02002269 if (ret == NULL)
2270 goto out;
2271 tmplog = ret;
2272 last_isspace = 0;
2273 break;
2274
Willy Tarreau2beef582012-12-20 17:22:52 +01002275 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002276 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002277 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, 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_SERVERPORT: // %sp
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_port(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 Lallemand1d705562012-03-12 12:46:41 +01002293 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002294 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002295 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002296 last_isspace = 0;
2297 break;
2298
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002299 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002300 get_localtime(logs->accept_date.tv_sec, &tm);
2301 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002302 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002303 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002304 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002305 last_isspace = 0;
2306 break;
2307
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002308 case LOG_FMT_tr: // %tr = start of request date
2309 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002310 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 +02002311 get_localtime(tv.tv_sec, &tm);
2312 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2313 if (ret == NULL)
2314 goto out;
2315 tmplog = ret;
2316 last_isspace = 0;
2317 break;
2318
2319 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002320 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002321 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002322 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002323 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002324 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002325 last_isspace = 0;
2326 break;
2327
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002328 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002329 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 +02002330 get_gmtime(tv.tv_sec, &tm);
2331 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2332 if (ret == NULL)
2333 goto out;
2334 tmplog = ret;
2335 last_isspace = 0;
2336 break;
2337
2338 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002339 get_localtime(logs->accept_date.tv_sec, &tm);
2340 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002341 if (ret == NULL)
2342 goto out;
2343 tmplog = ret;
2344 last_isspace = 0;
2345 break;
2346
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002347 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002348 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 +02002349 get_localtime(tv.tv_sec, &tm);
2350 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2351 if (ret == NULL)
2352 goto out;
2353 tmplog = ret;
2354 last_isspace = 0;
2355 break;
2356
William Lallemand5f232402012-04-05 18:02:55 +02002357 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002358 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002359 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002360 if (iret < 0 || iret > dst + maxsize - tmplog)
2361 goto out;
2362 last_isspace = 0;
2363 tmplog += iret;
2364 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002365 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002366 if (ret == NULL)
2367 goto out;
2368 tmplog = ret;
2369 last_isspace = 0;
2370 }
2371 break;
2372
William Lallemand1d705562012-03-12 12:46:41 +01002373 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002374 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002375 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002376 if (iret < 0 || iret > dst + maxsize - tmplog)
2377 goto out;
2378 last_isspace = 0;
2379 tmplog += iret;
2380 } else {
2381 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002382 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002383 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002384 tmplog, 4);
2385 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002386 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002387 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002388 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002389 }
2390 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002391
William Lallemand1d705562012-03-12 12:46:41 +01002392 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002393 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002394 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002395 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002396 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002397 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002398 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002399 break;
2400
Willy Tarreau773d65f2012-10-12 14:56:11 +02002401 case LOG_FMT_FRONTEND_XPRT: // %ft
2402 src = fe->id;
2403 if (tmp->options & LOG_OPT_QUOTE)
2404 LOGCHAR('"');
2405 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2406 if (iret == 0)
2407 goto out;
2408 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002409 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002410 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002411 if (tmp->options & LOG_OPT_QUOTE)
2412 LOGCHAR('"');
2413 last_isspace = 0;
2414 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002415#ifdef USE_OPENSSL
2416 case LOG_FMT_SSL_CIPHER: // %sslc
2417 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002418 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002419 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002420 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002421 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002422 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2423 if (ret == NULL)
2424 goto out;
2425 tmplog = ret;
2426 last_isspace = 0;
2427 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002428
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002429 case LOG_FMT_SSL_VERSION: // %sslv
2430 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002431 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002432 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002433 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002434 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002435 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2436 if (ret == NULL)
2437 goto out;
2438 tmplog = ret;
2439 last_isspace = 0;
2440 break;
2441#endif
William Lallemand1d705562012-03-12 12:46:41 +01002442 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002443 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002444 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002445 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002446 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002447 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002448 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002449 break;
2450
William Lallemand1d705562012-03-12 12:46:41 +01002451 case LOG_FMT_SERVER: // %s
Willy Tarreaue1809df2018-09-05 15:30:16 +02002452 switch (obj_type(s ? s->target : NULL)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002453 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002454 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002455 break;
2456 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002457 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002458 break;
2459 default:
2460 src = "<NOSRV>";
2461 break;
2462 }
William Lallemand5f232402012-04-05 18:02:55 +02002463 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002464 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002465 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002466 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002467 last_isspace = 0;
2468 break;
2469
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002470 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002471 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002472 if (ret == NULL)
2473 goto out;
2474 tmplog = ret;
2475 last_isspace = 0;
2476 break;
2477
2478 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002479 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002480 if (ret == NULL)
2481 goto out;
2482 tmplog = ret;
2483 last_isspace = 0;
2484 break;
2485
2486 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002487 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002488 tmplog, dst + maxsize - tmplog);
2489 if (ret == NULL)
2490 goto out;
2491 tmplog = ret;
2492 last_isspace = 0;
2493 break;
2494
2495 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002496 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002497 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002498 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002499 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002500 last_isspace = 0;
2501 break;
2502
William Lallemand1d705562012-03-12 12:46:41 +01002503 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002504 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002505 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002506 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002507 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002508 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002509 last_isspace = 0;
2510 break;
2511
William Lallemand1d705562012-03-12 12:46:41 +01002512 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002513 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002514 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002515 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002516 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002517 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002518 last_isspace = 0;
2519 break;
2520
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002521 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002522 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002523 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002524 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002525 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002526 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002527 last_isspace = 0;
2528 break;
2529
Willy Tarreau27b639d2016-05-17 17:55:27 +02002530 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002531 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002532 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002533 tmplog, dst + maxsize - tmplog);
2534 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002535 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002536 tmplog, dst + maxsize - tmplog);
2537 if (ret == NULL)
2538 goto out;
2539 tmplog = ret;
2540 last_isspace = 0;
2541 break;
2542
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002543 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2544 if (!(fe->to_log & LW_BYTES))
2545 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002546 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 +02002547 tmplog, dst + maxsize - tmplog);
2548 if (ret == NULL)
2549 goto out;
2550 tmplog = ret;
2551 last_isspace = 0;
2552 break;
2553
2554 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002555 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002556 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002557 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002558 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002559 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002560 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002561 last_isspace = 0;
2562 break;
2563
Damien Claisse57c8eb92020-04-28 12:09:19 +00002564 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2565 if (!(fe->to_log & LW_BYTES))
2566 LOGCHAR('+');
2567 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2568 tmplog, dst + maxsize - tmplog);
2569 if (ret == NULL)
2570 goto out;
2571 tmplog = ret;
2572 last_isspace = 0;
2573 break;
2574
Willy Tarreau2beef582012-12-20 17:22:52 +01002575 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02002576 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002577 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002578 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002579 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002580 last_isspace = 0;
2581 break;
2582
William Lallemand1d705562012-03-12 12:46:41 +01002583 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002584 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002585 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002586 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002587 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002588 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002589 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002590 last_isspace = 0;
2591 break;
2592
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002593 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002594 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002595 if (ret == NULL)
2596 goto out;
2597 tmplog = ret;
2598 last_isspace = 0;
2599 break;
2600
Willy Tarreau2beef582012-12-20 17:22:52 +01002601 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002602 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002603 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002604 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002605 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002606 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002607 last_isspace = 0;
2608 break;
2609
Willy Tarreau2beef582012-12-20 17:22:52 +01002610 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002611 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002612 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002613 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002614 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002615 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002616 last_isspace = 0;
2617 break;
2618
William Lallemand1d705562012-03-12 12:46:41 +01002619 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002620 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2621 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002622 *tmplog = '\0';
2623 last_isspace = 0;
2624 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002625
William Lallemand1d705562012-03-12 12:46:41 +01002626 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002627 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2628 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002629 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2630 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 +01002631 last_isspace = 0;
2632 break;
2633
William Lallemand1d705562012-03-12 12:46:41 +01002634 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002635 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002636 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002637 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002638 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002639 last_isspace = 0;
2640 break;
2641
William Lallemand1d705562012-03-12 12:46:41 +01002642 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002643 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002644 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002645 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002646 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002647 last_isspace = 0;
2648 break;
2649
William Lallemand1d705562012-03-12 12:46:41 +01002650 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002651 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002652 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002653 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002654 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002655 last_isspace = 0;
2656 break;
2657
William Lallemand1d705562012-03-12 12:46:41 +01002658 case LOG_FMT_SRVCONN: // %sc
Willy Tarreaue1809df2018-09-05 15:30:16 +02002659 ret = ultoa_o(objt_server(s ? s->target : NULL) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002660 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002661 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002662 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002663 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002664 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002665 last_isspace = 0;
2666 break;
2667
William Lallemand1d705562012-03-12 12:46:41 +01002668 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002669 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002670 LOGCHAR('+');
Willy Tarreauabd71a52018-09-04 19:21:44 +02002671 ret = ltoa_o((s && s->si[1].conn_retries > 0) ?
Willy Tarreau350f4872014-11-28 14:42:25 +01002672 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002673 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002674 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002675 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_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002681 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002682 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002683 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002684 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002685 last_isspace = 0;
2686 break;
2687
William Lallemand1d705562012-03-12 12:46:41 +01002688 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002689 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002690 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002691 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002692 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002693 last_isspace = 0;
2694 break;
2695
William Lallemand1d705562012-03-12 12:46:41 +01002696 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002697 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002698 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002699 if (tmp->options & LOG_OPT_QUOTE)
2700 LOGCHAR('"');
2701 LOGCHAR('{');
2702 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2703 if (hdr)
2704 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002705 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002706 ret = lf_encode_string(tmplog, dst + maxsize,
2707 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002708 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002709 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002710 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002711 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002712 }
2713 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002714 if (tmp->options & LOG_OPT_QUOTE)
2715 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002716 last_isspace = 0;
2717 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002718 break;
2719
William Lallemand1d705562012-03-12 12:46:41 +01002720 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002721 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002722 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002723 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2724 if (hdr > 0)
2725 LOGCHAR(' ');
2726 if (tmp->options & LOG_OPT_QUOTE)
2727 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002728 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002729 ret = lf_encode_string(tmplog, dst + maxsize,
2730 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002731 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002732 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002733 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002734 } else if (!(tmp->options & LOG_OPT_QUOTE))
2735 LOGCHAR('-');
2736 if (tmp->options & LOG_OPT_QUOTE)
2737 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002738 last_isspace = 0;
2739 }
2740 }
2741 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002742
William Lallemand1d705562012-03-12 12:46:41 +01002743
2744 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002745 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002746 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002747 if (tmp->options & LOG_OPT_QUOTE)
2748 LOGCHAR('"');
2749 LOGCHAR('{');
2750 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2751 if (hdr)
2752 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002753 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002754 ret = lf_encode_string(tmplog, dst + maxsize,
2755 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002756 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002757 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002758 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002759 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002760 }
2761 LOGCHAR('}');
2762 last_isspace = 0;
2763 if (tmp->options & LOG_OPT_QUOTE)
2764 LOGCHAR('"');
2765 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002766 break;
2767
William Lallemand1d705562012-03-12 12:46:41 +01002768 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002769 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002770 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002771 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2772 if (hdr > 0)
2773 LOGCHAR(' ');
2774 if (tmp->options & LOG_OPT_QUOTE)
2775 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002776 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002777 ret = lf_encode_string(tmplog, dst + maxsize,
2778 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002779 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002780 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002781 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002782 } else if (!(tmp->options & LOG_OPT_QUOTE))
2783 LOGCHAR('-');
2784 if (tmp->options & LOG_OPT_QUOTE)
2785 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002786 last_isspace = 0;
2787 }
2788 }
2789 break;
2790
William Lallemand1d705562012-03-12 12:46:41 +01002791 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002792 /* Request */
2793 if (tmp->options & LOG_OPT_QUOTE)
2794 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002795 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002796 ret = lf_encode_string(tmplog, dst + maxsize,
2797 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002798 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002799 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002800 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002801 if (tmp->options & LOG_OPT_QUOTE)
2802 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002803 last_isspace = 0;
2804 break;
William Lallemand5f232402012-04-05 18:02:55 +02002805
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002806 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002807 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002808
Willy Tarreaub7636d12015-06-17 19:58:02 +02002809 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002810 LOGCHAR('"');
2811
2812 end = uri + strlen(uri);
2813 // look for the first whitespace character
2814 while (uri < end && !HTTP_IS_SPHT(*uri))
2815 uri++;
2816
2817 // keep advancing past multiple spaces
2818 while (uri < end && HTTP_IS_SPHT(*uri)) {
2819 uri++; nspaces++;
2820 }
2821
2822 // look for first space or question mark after url
2823 spc = uri;
2824 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2825 spc++;
2826
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002827 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002828 chunk.area = "<BADREQ>";
2829 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002830 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002831 chunk.area = uri;
2832 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002833 }
2834
Dragan Dosen835b9212016-02-12 13:23:03 +01002835 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002836 if (ret == NULL || *ret != '\0')
2837 goto out;
2838
2839 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002840 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002841 LOGCHAR('"');
2842
2843 last_isspace = 0;
2844 break;
2845
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002846 case LOG_FMT_HTTP_QUERY: // %HQ
2847 if (tmp->options & LOG_OPT_QUOTE)
2848 LOGCHAR('"');
2849
Willy Tarreau57bc8912016-04-25 17:09:40 +02002850 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002851 chunk.area = "<BADREQ>";
2852 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002853 } else {
2854 uri = txn->uri;
2855 end = uri + strlen(uri);
2856 // look for the first question mark
2857 while (uri < end && *uri != '?')
2858 uri++;
2859
2860 qmark = uri;
2861 // look for first space or question mark after url
2862 while (uri < end && !HTTP_IS_SPHT(*uri))
2863 uri++;
2864
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002865 chunk.area = qmark;
2866 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002867 }
2868
Dragan Dosen835b9212016-02-12 13:23:03 +01002869 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002870 if (ret == NULL || *ret != '\0')
2871 goto out;
2872
2873 tmplog = ret;
2874 if (tmp->options & LOG_OPT_QUOTE)
2875 LOGCHAR('"');
2876
2877 last_isspace = 0;
2878 break;
2879
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002880 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002881 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002882
Willy Tarreaub7636d12015-06-17 19:58:02 +02002883 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002884 LOGCHAR('"');
2885
2886 end = uri + strlen(uri);
2887 // look for the first whitespace character
2888 while (uri < end && !HTTP_IS_SPHT(*uri))
2889 uri++;
2890
2891 // keep advancing past multiple spaces
2892 while (uri < end && HTTP_IS_SPHT(*uri)) {
2893 uri++; nspaces++;
2894 }
2895
2896 // look for first space after url
2897 spc = uri;
2898 while (spc < end && !HTTP_IS_SPHT(*spc))
2899 spc++;
2900
Willy Tarreau57bc8912016-04-25 17:09:40 +02002901 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002902 chunk.area = "<BADREQ>";
2903 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002904 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002905 chunk.area = uri;
2906 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002907 }
2908
Dragan Dosen835b9212016-02-12 13:23:03 +01002909 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002910 if (ret == NULL || *ret != '\0')
2911 goto out;
2912
2913 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002914 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002915 LOGCHAR('"');
2916
2917 last_isspace = 0;
2918 break;
2919
2920 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002921 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002922 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002923 LOGCHAR('"');
2924
2925 end = uri + strlen(uri);
2926 // look for the first whitespace character
2927 spc = uri;
2928 while (spc < end && !HTTP_IS_SPHT(*spc))
2929 spc++;
2930
2931 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002932 chunk.area = "<BADREQ>";
2933 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002934 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002935 chunk.area = uri;
2936 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002937 }
2938
Dragan Dosen835b9212016-02-12 13:23:03 +01002939 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002940 if (ret == NULL || *ret != '\0')
2941 goto out;
2942
2943 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002944 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002945 LOGCHAR('"');
2946
2947 last_isspace = 0;
2948 break;
2949
2950 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002951 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002952 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002953 LOGCHAR('"');
2954
2955 end = uri + strlen(uri);
2956 // look for the first whitespace character
2957 while (uri < end && !HTTP_IS_SPHT(*uri))
2958 uri++;
2959
2960 // keep advancing past multiple spaces
2961 while (uri < end && HTTP_IS_SPHT(*uri)) {
2962 uri++; nspaces++;
2963 }
2964
2965 // look for the next whitespace character
2966 while (uri < end && !HTTP_IS_SPHT(*uri))
2967 uri++;
2968
2969 // keep advancing past multiple spaces
2970 while (uri < end && HTTP_IS_SPHT(*uri))
2971 uri++;
2972
Willy Tarreau57bc8912016-04-25 17:09:40 +02002973 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002974 chunk.area = "<BADREQ>";
2975 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002976 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002977 chunk.area = "HTTP/0.9";
2978 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002979 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002980 chunk.area = uri;
2981 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002982 }
2983
Dragan Dosen835b9212016-02-12 13:23:03 +01002984 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002985 if (ret == NULL || *ret != '\0')
2986 goto out;
2987
2988 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002989 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002990 LOGCHAR('"');
2991
2992 last_isspace = 0;
2993 break;
2994
William Lallemand5f232402012-04-05 18:02:55 +02002995 case LOG_FMT_COUNTER: // %rt
2996 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002997 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002998 if (iret < 0 || iret > dst + maxsize - tmplog)
2999 goto out;
3000 last_isspace = 0;
3001 tmplog += iret;
3002 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003003 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003004 if (ret == NULL)
3005 goto out;
3006 tmplog = ret;
3007 last_isspace = 0;
3008 }
3009 break;
3010
Willy Tarreau7346acb2014-08-28 15:03:15 +02003011 case LOG_FMT_LOGCNT: // %lc
3012 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003013 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003014 if (iret < 0 || iret > dst + maxsize - tmplog)
3015 goto out;
3016 last_isspace = 0;
3017 tmplog += iret;
3018 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003019 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003020 if (ret == NULL)
3021 goto out;
3022 tmplog = ret;
3023 last_isspace = 0;
3024 }
3025 break;
3026
William Lallemand5f232402012-04-05 18:02:55 +02003027 case LOG_FMT_HOSTNAME: // %H
3028 src = hostname;
3029 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3030 if (ret == NULL)
3031 goto out;
3032 tmplog = ret;
3033 last_isspace = 0;
3034 break;
3035
3036 case LOG_FMT_PID: // %pid
3037 if (tmp->options & LOG_OPT_HEXA) {
3038 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3039 if (iret < 0 || iret > dst + maxsize - tmplog)
3040 goto out;
3041 last_isspace = 0;
3042 tmplog += iret;
3043 } else {
3044 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3045 if (ret == NULL)
3046 goto out;
3047 tmplog = ret;
3048 last_isspace = 0;
3049 }
3050 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003051
3052 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003053 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003054 if (s)
3055 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3056 else
3057 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003058 if (ret == NULL)
3059 goto out;
3060 tmplog = ret;
3061 last_isspace = 0;
3062 break;
3063
William Lallemandbddd4fd2012-02-27 11:23:10 +01003064 }
3065 }
3066
3067out:
William Lallemand1d705562012-03-12 12:46:41 +01003068 /* *tmplog is a unused character */
3069 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003070 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003071
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072}
3073
William Lallemand1d705562012-03-12 12:46:41 +01003074/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003075 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003076 * Will not log if the frontend has no log defined.
3077 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003078void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003079{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003080 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003081 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003082 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003083
3084 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003085 err = (s->flags & SF_REDISP) ||
3086 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3087 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003088 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003089 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003090
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003091 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003092 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003093
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003094 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003095 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003096
Willy Tarreauabcd5142013-06-11 17:18:02 +02003097 if (s->logs.level) { /* loglevel was overridden */
3098 if (s->logs.level == -1) {
3099 s->logs.logwait = 0; /* logs disabled */
3100 return;
3101 }
3102 level = s->logs.level - 1;
3103 }
3104 else {
3105 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003106 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003107 level = LOG_ERR;
3108 }
William Lallemand1d705562012-03-12 12:46:41 +01003109
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003110 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003111 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003112 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003113 }
3114
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003115 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3116 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3117 &sess->fe->logformat_sd);
3118 }
3119
Dragan Dosen59cee972015-09-19 22:09:02 +02003120 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003121 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003122 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003123 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3124 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003125 s->logs.logwait = 0;
3126 }
3127}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003128
Willy Tarreau53839352018-09-05 19:51:10 +02003129/*
3130 * send a minimalist log for the session. Will not log if the frontend has no
3131 * log defined. It is assumed that this is only used to report anomalies that
3132 * cannot lead to the creation of a regular stream. Because of this the log
3133 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3134 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003135 * function to report unimportant events. It is safe to call this function with
3136 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003137 */
3138void sess_log(struct session *sess)
3139{
3140 int size, level;
3141 int sd_size = 0;
3142
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003143 if (!sess)
3144 return;
3145
Willy Tarreau53839352018-09-05 19:51:10 +02003146 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3147 return;
3148
3149 level = LOG_INFO;
3150 if (sess->fe->options2 & PR_O2_LOGERRORS)
3151 level = LOG_ERR;
3152
3153 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3154 sd_size = sess_build_logline(sess, NULL,
3155 logline_rfc5424, global.max_syslog_len,
3156 &sess->fe->logformat_sd);
3157 }
3158
3159 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3160 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003161 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003162 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3163 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003164 }
3165}
3166
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003167void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3168{
3169 va_list argp;
3170 int data_len;
3171
3172 if (level < 0 || format == NULL || logline == NULL)
3173 return;
3174
3175 va_start(argp, format);
3176 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3177 if (data_len < 0 || data_len > global.max_syslog_len)
3178 data_len = global.max_syslog_len;
3179 va_end(argp);
3180
3181 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3182}
Emeric Brun54932b42020-07-07 09:43:24 +02003183/*
3184 * This function parse a received log message <buf>, of size <buflen>
3185 * it fills <level>, <facility> and <metadata> depending of the detected
3186 * header format and message will point on remaining payload of <size>
3187 *
3188 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3189 * struct ist len will be set to 0 if field is not found
3190 * <level> and <facility> will be set to -1 if not found.
3191 */
3192void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3193 struct ist *metadata, char **message, size_t *size)
3194{
3195
3196 char *p;
3197 int fac_level = 0;
3198
3199 *level = *facility = -1;
3200
3201 *message = buf;
3202 *size = buflen;
3203
3204 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3205
3206 p = buf;
3207 if (*size < 2 || *p != '<')
3208 return;
3209
3210 p++;
3211 while (*p != '>') {
3212 if (*p > '9' || *p < '0')
3213 return;
3214 fac_level = 10*fac_level + (*p - '0');
3215 p++;
3216 if ((p - buf) > buflen)
3217 return;
3218 }
3219
3220 *facility = fac_level >> 3;
3221 *level = fac_level & 0x7;
3222 p++;
3223
3224 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3225
3226 buflen -= p - buf;
3227 buf = p;
3228
3229 *size = buflen;
3230 *message = buf;
3231
3232 /* for rfc5424, prio is always followed by '1' and ' ' */
3233 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3234 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3235 * followed by message.
3236 * Each header field can present NILVALUE: '-'
3237 */
3238
3239 p += 2;
3240 /* timestamp is NILVALUE '-' */
3241 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3242 metadata[LOG_META_TIME] = ist2(p, 1);
3243 p++;
3244 }
3245 else if (*size > LOG_ISOTIME_MINLEN) {
3246 metadata[LOG_META_TIME].ptr = p;
3247
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003248 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003249 * in timestamp.
3250 * possible format are:
3251 * ex: '1970-01-01T00:00:00.000000Z'
3252 * '1970-01-01T00:00:00.000000+00:00'
3253 * '1970-01-01T00:00:00.000000-00:00'
3254 * '1970-01-01T00:00:00Z'
3255 * '1970-01-01T00:00:00+00:00'
3256 * '1970-01-01T00:00:00-00:00'
3257 */
3258 p += 19;
3259 if (*p == '.') {
3260 p++;
3261 if ((p - buf) >= buflen)
3262 goto bad_format;
3263 while (*p != 'Z' && *p != '+' && *p != '-') {
3264 if ((unsigned char)(*p - '0') > 9)
3265 goto bad_format;
3266
3267 p++;
3268 if ((p - buf) >= buflen)
3269 goto bad_format;
3270 }
3271 }
3272
3273 if (*p == 'Z')
3274 p++;
3275 else
3276 p += 6; /* case of '+00:00 or '-00:00' */
3277
3278 if ((p - buf) >= buflen || *p != ' ')
3279 goto bad_format;
3280 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3281 }
3282 else
3283 goto bad_format;
3284
3285
3286 p++;
3287 if ((p - buf) >= buflen || *p == ' ')
3288 goto bad_format;
3289
3290 metadata[LOG_META_HOST].ptr = p;
3291 while (*p != ' ') {
3292 p++;
3293 if ((p - buf) >= buflen)
3294 goto bad_format;
3295 }
3296 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3297 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3298 metadata[LOG_META_HOST].len = 0;
3299
3300 p++;
3301 if ((p - buf) >= buflen || *p == ' ')
3302 goto bad_format;
3303
3304 metadata[LOG_META_TAG].ptr = p;
3305 while (*p != ' ') {
3306 p++;
3307 if ((p - buf) >= buflen)
3308 goto bad_format;
3309 }
3310 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3311 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3312 metadata[LOG_META_TAG].len = 0;
3313
3314 p++;
3315 if ((p - buf) >= buflen || *p == ' ')
3316 goto bad_format;
3317
3318 metadata[LOG_META_PID].ptr = p;
3319 while (*p != ' ') {
3320 p++;
3321 if ((p - buf) >= buflen)
3322 goto bad_format;
3323 }
3324 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3325 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3326 metadata[LOG_META_PID].len = 0;
3327
3328 p++;
3329 if ((p - buf) >= buflen || *p == ' ')
3330 goto bad_format;
3331
3332 metadata[LOG_META_MSGID].ptr = p;
3333 while (*p != ' ') {
3334 p++;
3335 if ((p - buf) >= buflen)
3336 goto bad_format;
3337 }
3338 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3339 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3340 metadata[LOG_META_MSGID].len = 0;
3341
3342 p++;
3343 if ((p - buf) >= buflen || *p == ' ')
3344 goto bad_format;
3345
3346 /* structured data format is:
3347 * ex:
3348 * '[key1=value1 key2=value2][key3=value3]'
3349 *
3350 * space is invalid outside [] because
3351 * considered as the end of structured data field
3352 */
3353 metadata[LOG_META_STDATA].ptr = p;
3354 if (*p == '[') {
3355 int elem = 0;
3356
3357 while (1) {
3358 if (elem) {
3359 /* according to rfc this char is escaped in param values */
3360 if (*p == ']' && *(p-1) != '\\')
3361 elem = 0;
3362 }
3363 else {
3364 if (*p == '[')
3365 elem = 1;
3366 else if (*p == ' ')
3367 break;
3368 else
3369 goto bad_format;
3370 }
3371 p++;
3372 if ((p - buf) >= buflen)
3373 goto bad_format;
3374 }
3375 }
3376 else if (*p == '-') {
3377 /* case of NILVALUE */
3378 p++;
3379 if ((p - buf) >= buflen || *p != ' ')
3380 goto bad_format;
3381 }
3382 else
3383 goto bad_format;
3384
3385 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3386 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3387 metadata[LOG_META_STDATA].len = 0;
3388
3389 p++;
3390
3391 buflen -= p - buf;
3392 buf = p;
3393
3394 *size = buflen;
3395 *message = p;
3396 }
3397 else if (*size > LOG_LEGACYTIME_LEN) {
3398 int m;
3399
3400 /* supported header format according to rfc3164.
3401 * ex:
3402 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3403 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3404 * or 'Jan 1 00:00:00 HOSTNAME '
3405 * Note: HOSTNAME is mandatory, and day
3406 * of month uses a single space prefix if
3407 * less than 10 to ensure hour offset is
3408 * always the same.
3409 */
3410
3411 /* Check month to see if it correspond to a rfc3164
3412 * header ex 'Jan 1 00:00:00' */
3413 for (m = 0; m < 12; m++)
3414 if (!memcmp(monthname[m], p, 3))
3415 break;
3416 /* Month not found */
3417 if (m == 12)
3418 goto bad_format;
3419
3420 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3421
3422 p += LOG_LEGACYTIME_LEN;
3423 if ((p - buf) >= buflen || *p != ' ')
3424 goto bad_format;
3425
3426 p++;
3427 if ((p - buf) >= buflen || *p == ' ')
3428 goto bad_format;
3429
3430 metadata[LOG_META_HOST].ptr = p;
3431 while (*p != ' ') {
3432 p++;
3433 if ((p - buf) >= buflen)
3434 goto bad_format;
3435 }
3436 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3437
3438 /* TAG seems to no be mandatory */
3439 p++;
3440
3441 buflen -= p - buf;
3442 buf = p;
3443
3444 *size = buflen;
3445 *message = buf;
3446
3447 if (!buflen)
3448 return;
3449
3450 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3451 p++;
3452
3453 /* a tag must present a trailing ':' */
3454 if (((p - buf) >= buflen) || *p != ':')
3455 return;
3456 p++;
3457 /* followed by a space */
3458 if (((p - buf) >= buflen) || *p != ' ')
3459 return;
3460
3461 /* rewind to parse tag and pid */
3462 p = buf;
3463 metadata[LOG_META_TAG].ptr = p;
3464 /* we have the guarantee that ':' will be reach before size limit */
3465 while (*p != ':') {
3466 if (*p == '[') {
3467 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3468 metadata[LOG_META_PID].ptr = p + 1;
3469 }
3470 else if (*p == ']' && metadata[LOG_META_PID].ptr) {
3471 if (p[1] != ':')
3472 return;
3473 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3474 }
3475 p++;
3476 }
3477 if (!metadata[LOG_META_TAG].len)
3478 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3479
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003480 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003481 p += 2;
3482
3483 buflen -= p - buf;
3484 buf = p;
3485
3486 *size = buflen;
3487 *message = buf;
3488 }
3489
3490 return;
3491
3492bad_format:
3493 /* bad syslog format, we reset all parsed syslog fields
3494 * but priority is kept because we are able to re-build
3495 * this message using LOF_FORMAT_PRIO.
3496 */
3497 metadata[LOG_META_TIME].len = 0;
3498 metadata[LOG_META_HOST].len = 0;
3499 metadata[LOG_META_TAG].len = 0;
3500 metadata[LOG_META_PID].len = 0;
3501 metadata[LOG_META_MSGID].len = 0;
3502 metadata[LOG_META_STDATA].len = 0;
3503
3504 return;
3505}
3506
3507/*
3508 * UDP syslog fd handler
3509 */
3510void syslog_fd_handler(int fd)
3511{
3512 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3513 ssize_t ret = 0;
3514 struct buffer *buf = get_trash_chunk();
3515 size_t size;
3516 char *message;
3517 int level;
3518 int facility;
3519 struct listener *l = objt_listener(fdtab[fd].owner);
3520 int max_accept;
3521
3522 if(!l)
3523 ABORT_NOW();
3524
3525 if (fdtab[fd].ev & FD_POLL_IN) {
3526
3527 if (!fd_recv_ready(fd))
3528 return;
3529
3530 max_accept = l->maxaccept ? l->maxaccept : 1;
3531
3532 do {
3533 /* Source address */
3534 struct sockaddr_storage saddr = {0};
3535 socklen_t saddrlen;
3536
3537 saddrlen = sizeof(saddr);
3538
3539 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3540 if (ret < 0) {
3541 if (errno == EINTR)
3542 continue;
3543 if (errno == EAGAIN)
3544 fd_cant_recv(fd);
3545 goto out;
3546 }
3547 buf->data = ret;
3548
Emeric Brun45c457a2020-07-09 23:23:34 +02003549 /* update counters */
3550 _HA_ATOMIC_ADD(&cum_log_messages, 1);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003551 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003552
Emeric Brun54932b42020-07-07 09:43:24 +02003553 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3554
3555 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3556
3557 } while (--max_accept);
3558 }
3559
3560out:
3561 return;
3562}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003563
Emeric Brun12941c82020-07-07 14:19:42 +02003564/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003565 * IO Handler to handle message exchange with a syslog tcp client
3566 */
3567static void syslog_io_handler(struct appctx *appctx)
3568{
3569 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3570 struct stream_interface *si = appctx->owner;
3571 struct stream *s = si_strm(si);
3572 struct proxy *frontend = strm_fe(s);
3573 struct listener *l = strm_li(s);
3574 struct buffer *buf = get_trash_chunk();
3575 int max_accept;
3576 int to_skip;
3577 int facility;
3578 int level;
3579 char *message;
3580 size_t size;
3581
3582 max_accept = l->maxaccept ? l->maxaccept : 1;
3583 while (co_data(si_oc(si))) {
3584 char c;
3585
3586 if (max_accept <= 0)
3587 goto missing_budget;
3588 max_accept--;
3589
3590 to_skip = co_getchar(si_oc(si), &c);
3591 if (!to_skip)
3592 goto missing_data;
3593 else if (to_skip < 0)
3594 goto cli_abort;
3595
3596 if (c == '<') {
3597 /* rfc-6587, Non-Transparent-Framing: messages separated by
3598 * a trailing LF or CR LF
3599 */
3600 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3601 if (!to_skip)
3602 goto missing_data;
3603 else if (to_skip < 0)
3604 goto cli_abort;
3605
3606 if (buf->area[to_skip - 1] != '\n')
3607 goto parse_error;
3608
3609 buf->data = to_skip - 1;
3610
3611 /* according to rfc-6587, some devices adds CR before LF */
3612 if (buf->data && buf->area[buf->data - 1] == '\r')
3613 buf->data--;
3614
3615 }
3616 else if ((unsigned char)(c - '1') <= 8) {
3617 /* rfc-6587, Octet-Counting: message length in ASCII
3618 * (first digit can not be ZERO), followed by a space
3619 * and message length
3620 */
3621 char *p = NULL;
3622 int msglen;
3623
3624 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3625 if (!to_skip)
3626 goto missing_data;
3627 else if (to_skip < 0)
3628 goto cli_abort;
3629
3630 if (buf->area[to_skip - 1] != ' ')
3631 goto parse_error;
3632
3633 msglen = strtol(trash.area, &p, 10);
3634 if (!msglen || p != &buf->area[to_skip - 1])
3635 goto parse_error;
3636
3637 /* message seems too large */
3638 if (msglen > buf->size)
3639 goto parse_error;
3640
3641 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3642 if (!msglen)
3643 goto missing_data;
3644 else if (msglen < 0)
3645 goto cli_abort;
3646
3647
3648 buf->data = msglen;
3649 to_skip += msglen;
3650 }
3651 else
3652 goto parse_error;
3653
3654 co_skip(si_oc(si), to_skip);
3655
3656 /* update counters */
3657 _HA_ATOMIC_ADD(&cum_log_messages, 1);
3658 proxy_inc_fe_req_ctr(l, frontend);
3659
3660 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3661
3662 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3663
3664 }
3665
3666missing_data:
3667 /* we need more data to read */
3668 si_oc(si)->flags |= CF_READ_DONTWAIT;
3669
3670 return;
3671
3672missing_budget:
3673 /* it may remain some stuff to do, let's retry later */
3674 appctx_wakeup(appctx);
3675
3676 return;
3677
3678parse_error:
3679 if (l->counters)
3680 _HA_ATOMIC_ADD(&l->counters->failed_req, 1);
3681 _HA_ATOMIC_ADD(&frontend->fe_counters.failed_req, 1);
3682
3683 goto close;
3684
3685cli_abort:
3686 if (l->counters)
3687 _HA_ATOMIC_ADD(&l->counters->cli_aborts, 1);
3688 _HA_ATOMIC_ADD(&frontend->fe_counters.cli_aborts, 1);
3689
3690close:
3691 si_shutw(si);
3692 si_shutr(si);
3693
3694 si_ic(si)->flags |= CF_READ_NULL;
3695
3696 return;
3697}
3698
3699static struct applet syslog_applet = {
3700 .obj_type = OBJ_TYPE_APPLET,
3701 .name = "<SYSLOG>", /* used for logging */
3702 .fct = syslog_io_handler,
3703 .release = NULL,
3704};
3705
3706/*
Emeric Brun12941c82020-07-07 14:19:42 +02003707 * Parse "log-forward" section and create corresponding sink buffer.
3708 *
3709 * The function returns 0 in success case, otherwise, it returns error
3710 * flags.
3711 */
3712int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3713{
3714 int err_code = 0;
3715 struct proxy *px;
3716 char *errmsg = NULL;
3717 const char *err = NULL;
3718
3719 if (strcmp(args[0], "log-forward") == 0) {
3720 if (!*args[1]) {
3721 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3722 err_code |= ERR_ALERT | ERR_ABORT;
3723 goto out;
3724 }
3725
3726 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3727 goto out;
3728
3729 err = invalid_char(args[1]);
3730 if (err) {
3731 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3732 file, linenum, *err, args[0], args[1]);
3733 err_code |= ERR_ALERT | ERR_ABORT;
3734 goto out;
3735 }
3736
Emeric Brunb0c331f2020-10-07 17:05:59 +02003737 px = log_forward_by_name(args[1]);
3738 if (px) {
3739 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3740 file, linenum, args[1], px->conf.file, px->conf.line);
3741 err_code |= ERR_ALERT | ERR_FATAL;
3742 }
3743
3744 px = proxy_find_by_name(args[1], 0, 0);
3745 if (px) {
3746 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3747 file, linenum, args[1], proxy_type_str(px),
3748 px->id, px->conf.file, px->conf.line);
3749 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003750 }
3751
3752 px = calloc(1, sizeof *px);
3753 if (!px) {
3754 err_code |= ERR_ALERT | ERR_FATAL;
3755 goto out;
3756 }
3757
3758 px->next = cfg_log_forward;
3759 cfg_log_forward = px;
3760
3761 init_new_proxy(px);
3762 px->conf.file = strdup(file);
3763 px->conf.line = linenum;
3764 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003765 px->last_change = now.tv_sec;
3766 px->cap = PR_CAP_FE;
3767 px->maxconn = 10;
3768 px->timeout.client = TICK_ETERNITY;
3769 px->accept = frontend_accept;
3770 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003771 px->id = strdup(args[1]);
3772
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003773 }
3774 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
3775 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3776 err_code |= ERR_WARN;
3777
3778 if (*(args[1]) == 0) {
3779 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3780 err_code |= ERR_ALERT | ERR_FATAL;
3781 goto out;
3782 }
3783 cfg_log_forward->maxconn = atol(args[1]);
3784 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3785 goto out;
3786 }
3787 else if (!strcmp(args[0], "backlog")) { /* backlog */
3788 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3789 err_code |= ERR_WARN;
3790
3791 if (*(args[1]) == 0) {
3792 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3793 err_code |= ERR_ALERT | ERR_FATAL;
3794 goto out;
3795 }
3796 cfg_log_forward->backlog = atol(args[1]);
3797 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3798 goto out;
3799 }
3800 else if (strcmp(args[0], "bind") == 0) {
3801 int cur_arg;
3802 static int kws_dumped;
3803 struct bind_conf *bind_conf;
3804 struct bind_kw *kw;
3805 struct listener *l;
3806
3807 cur_arg = 1;
3808
3809 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3810 NULL, xprt_get(XPRT_RAW));
3811 if (!bind_conf) {
3812 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3813 err_code |= ERR_ALERT | ERR_FATAL;
3814 goto out;
3815 }
3816
3817 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3818 if (errmsg && *errmsg) {
3819 indent_msg(&errmsg, 2);
3820 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3821 }
3822 else {
3823 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3824 file, linenum, args[0], args[1], args[2]);
3825 err_code |= ERR_ALERT | ERR_FATAL;
3826 goto out;
3827 }
3828 }
3829 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
3830 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : 64;
3831 l->accept = session_accept_fd;
3832 l->analysers |= cfg_log_forward->fe_req_ana;
3833 l->default_target = cfg_log_forward->default_target;
3834 global.maxsock++;
3835 }
3836 cur_arg++;
3837
3838 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3839 int ret;
3840
3841 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3842 err_code |= ret;
3843 if (ret) {
3844 if (errmsg && *errmsg) {
3845 indent_msg(&errmsg, 2);
3846 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3847 }
3848 else
3849 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3850 file, linenum, args[cur_arg]);
3851 if (ret & ERR_FATAL)
3852 goto out;
3853 }
3854 cur_arg += 1 + kw->skip;
3855 }
3856 if (*args[cur_arg] != 0) {
3857 char *kws = NULL;
3858
3859 if (!kws_dumped) {
3860 kws_dumped = 1;
3861 bind_dump_kws(&kws);
3862 indent_msg(&kws, 4);
3863 }
3864 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.%s%s\n",
3865 file, linenum, args[cur_arg], cursection,
3866 kws ? " Registered keywords :" : "", kws ? kws: "");
3867 free(kws);
3868 err_code |= ERR_ALERT | ERR_FATAL;
3869 goto out;
3870 }
Emeric Brun12941c82020-07-07 14:19:42 +02003871 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003872 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003873 int cur_arg;
3874 static int kws_dumped;
3875 struct bind_conf *bind_conf;
3876 struct bind_kw *kw;
3877 struct listener *l;
3878
3879 cur_arg = 1;
3880
3881 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3882 NULL, xprt_get(XPRT_RAW));
3883
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003884 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003885 if (errmsg && *errmsg) {
3886 indent_msg(&errmsg, 2);
3887 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3888 }
3889 else {
3890 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3891 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003892 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003893 err_code |= ERR_ALERT | ERR_FATAL;
3894 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003895 }
3896 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003897 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Emeric Brun12941c82020-07-07 14:19:42 +02003898 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : 64;
Willy Tarreaue140a692020-10-15 21:25:32 +02003899 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003900 global.maxsock++;
3901 }
3902 cur_arg++;
3903
3904 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3905 int ret;
3906
3907 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3908 err_code |= ret;
3909 if (ret) {
3910 if (errmsg && *errmsg) {
3911 indent_msg(&errmsg, 2);
3912 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3913 }
3914 else
3915 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3916 file, linenum, args[cur_arg]);
3917 if (ret & ERR_FATAL)
3918 goto out;
3919 }
3920 cur_arg += 1 + kw->skip;
3921 }
3922 if (*args[cur_arg] != 0) {
3923 char *kws = NULL;
3924
3925 if (!kws_dumped) {
3926 kws_dumped = 1;
3927 bind_dump_kws(&kws);
3928 indent_msg(&kws, 4);
3929 }
3930 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.%s%s\n",
3931 file, linenum, args[cur_arg], cursection,
3932 kws ? " Registered keywords :" : "", kws ? kws: "");
3933 free(kws);
3934 err_code |= ERR_ALERT | ERR_FATAL;
3935 goto out;
3936 }
3937 }
3938 else if (strcmp(args[0], "log") == 0) {
3939 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), &errmsg)) {
3940 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3941 err_code |= ERR_ALERT | ERR_FATAL;
3942 goto out;
3943 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003944 }
3945 else if (strcmp(args[0], "timeout") == 0) {
3946 const char *res;
3947 unsigned timeout;
3948
3949 if (strcmp(args[1], "client") != 0) {
3950 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
3951 err_code |= ERR_ALERT | ERR_FATAL;
3952 goto out;
3953 }
3954
3955 if (*args[2] == 0) {
3956 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
3957 err_code |= ERR_ALERT | ERR_FATAL;
3958 goto out;
3959 }
3960 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3961 if (res == PARSE_TIME_OVER) {
3962 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
3963 }
3964 else if (res == PARSE_TIME_UNDER) {
3965 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
3966 }
3967 else if (res) {
3968 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
3969 return -1;
3970 }
3971
3972 if (res) {
3973 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3974 err_code |= ERR_ALERT | ERR_FATAL;
3975 goto out;
3976 }
3977 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02003978 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02003979 else {
3980 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
3981 err_code |= ERR_ALERT | ERR_ABORT;
3982 goto out;
3983 }
Emeric Brun12941c82020-07-07 14:19:42 +02003984out:
3985 return err_code;
3986}
3987
3988
Willy Tarreau869efd52019-11-15 15:16:57 +01003989/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
3990static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
Christopher Fauletc1b730a2017-10-24 12:00:51 +02003991{
Willy Tarreau869efd52019-11-15 15:16:57 +01003992 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3993 return 1;
Christopher Fauletc1b730a2017-10-24 12:00:51 +02003994
Willy Tarreau869efd52019-11-15 15:16:57 +01003995 if (!startup_logs)
3996 return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
3997
3998 return ring_attach_cli(startup_logs, appctx);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02003999}
4000
4001/* register cli keywords */
4002static struct cli_kw_list cli_kws = {{ },{
4003 { { "show", "startup-logs", NULL },
Willy Tarreau869efd52019-11-15 15:16:57 +01004004 "show startup-logs : report logs emitted during HAProxy startup", cli_parse_show_startup_logs, NULL, NULL },
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004005 {{},}
4006}};
4007
Willy Tarreau0108d902018-11-25 19:14:37 +01004008INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4009
Emeric Brun12941c82020-07-07 14:19:42 +02004010/* config parsers for this section */
4011REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4012
Willy Tarreau082b6282019-05-22 14:42:12 +02004013REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4014REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004015
Willy Tarreaubaaee002006-06-26 02:48:02 +02004016/*
4017 * Local variables:
4018 * c-indent-level: 8
4019 * c-basic-offset: 8
4020 * End:
4021 */