blob: 4e6dc30084b342eb16b39f0f449c341c0a66de2a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
Willy Tarreau077edcb2016-08-10 18:30:56 +020025#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/applet-t.h>
Emeric Brun12941c82020-07-07 14:19:42 +020029#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020030#include <haproxy/cli.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020032#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020034#include <haproxy/http.h>
Emeric Brun12941c82020-07-07 14:19:42 +020035#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036#include <haproxy/log.h>
Emeric Brun12941c82020-07-07 14:19:42 +020037#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/ring.h>
39#include <haproxy/sample.h>
40#include <haproxy/sink.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020041#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020043#include <haproxy/stream_interface.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020044#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020045#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020046#include <haproxy/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Emeric Brun45c457a2020-07-09 23:23:34 +020048/* global recv logs counter */
49int cum_log_messages;
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Emeric Brun12941c82020-07-07 14:19:42 +020051/* log forward proxy list */
52struct proxy *cfg_log_forward;
53
Emeric Brun54648852020-07-06 15:54:06 +020054struct log_fmt_st {
Dragan Dosen43885c72015-10-01 13:18:13 +020055 char *name;
Dragan Dosen43885c72015-10-01 13:18:13 +020056};
57
Emeric Brun54648852020-07-06 15:54:06 +020058static const struct log_fmt_st log_formats[LOG_FORMATS] = {
Emeric Brun0237c4e2020-11-27 16:24:34 +010059 [LOG_FORMAT_LOCAL] = {
60 .name = "local",
61 },
Dragan Dosen43885c72015-10-01 13:18:13 +020062 [LOG_FORMAT_RFC3164] = {
63 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020064 },
65 [LOG_FORMAT_RFC5424] = {
66 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020067 },
68 [LOG_FORMAT_PRIO] = {
69 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010070 },
71 [LOG_FORMAT_SHORT] = {
72 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020073 },
74 [LOG_FORMAT_TIMED] = {
75 .name = "timed",
76 },
77 [LOG_FORMAT_ISO] = {
78 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010079 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010080 [LOG_FORMAT_RAW] = {
81 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010082 },
Dragan Dosen1322d092015-09-22 16:05:32 +020083};
84
Dragan Dosen835b9212016-02-12 13:23:03 +010085/*
86 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020087 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
88 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
89 * that the byte should be escaped. Be careful to always pass bytes from 0 to
90 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010091 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020092long rfc5424_escape_map[(256/8) / sizeof(long)];
93long hdr_encode_map[(256/8) / sizeof(long)];
94long url_encode_map[(256/8) / sizeof(long)];
95long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010096
Dragan Dosen835b9212016-02-12 13:23:03 +010097
Willy Tarreaubaaee002006-06-26 02:48:02 +020098const char *log_facilities[NB_LOG_FACILITIES] = {
99 "kern", "user", "mail", "daemon",
100 "auth", "syslog", "lpr", "news",
101 "uucp", "cron", "auth2", "ftp",
102 "ntp", "audit", "alert", "cron2",
103 "local0", "local1", "local2", "local3",
104 "local4", "local5", "local6", "local7"
105};
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107const char *log_levels[NB_LOG_LEVELS] = {
108 "emerg", "alert", "crit", "err",
109 "warning", "notice", "info", "debug"
110};
111
Willy Tarreau570f2212013-06-10 16:42:09 +0200112const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200113const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
William Lallemand723b73a2012-02-08 16:37:49 +0100115
116/* log_format */
117struct logformat_type {
118 char *name;
119 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100120 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200121 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100122 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100123 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100124};
125
William Lallemandb7ff6a32012-03-02 14:35:21 +0100126int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
127
William Lallemand723b73a2012-02-08 16:37:49 +0100128/* log_format variable names */
129static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200130 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100131
132 /* please keep these lines sorted ! */
133 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
134 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
135 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
136 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100137 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200138 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200139 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200140 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100141 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200142 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
143 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
144 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
145 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
146 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
147 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200148 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100149 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200150 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000151 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100152 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
153 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200154 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100155 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200156 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100157 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
158 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200159 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200160 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
161 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100162 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
163 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200164 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
165 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100166 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200167 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
168 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
169 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
170 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000171 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
Maciej Zdebfcdfd852020-11-30 18:27:47 +0000172 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP relative or absolute path */
173 { "HPO", LOG_FMT_HTTP_PATH_ONLY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path only (without host nor query string) */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000174 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000175 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
176 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200177 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100178 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200179 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100180 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
181 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100182 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100183 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
184 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
185 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
186 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
187 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200188 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
189 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100190 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200191 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
192 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
193 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100194 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
195 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
196
197 /* The following tags are deprecated and will be removed soon */
198 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
199 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200200 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
201 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
202 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
203 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100204 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
205 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
206 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
207 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
208 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200209 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100210};
211
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200212char 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
213char 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 +0100214char 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 +0100215char *log_format = NULL;
216
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200217/* Default string used for structured-data part in RFC5424 formatted
218 * syslog messages.
219 */
220char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200221
Willy Tarreau13ef7732018-11-12 07:25:28 +0100222/* total number of dropped logs */
223unsigned int dropped_logs = 0;
224
Dragan Dosen59cee972015-09-19 22:09:02 +0200225/* This is a global syslog message buffer, common to all outgoing
226 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100227 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200228THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100229
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200230/* A global syslog message buffer, common to all RFC5424 syslog messages.
231 * Currently, it is used for generating the structured-data part.
232 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200233THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200234
Christopher Fauletd4696382017-10-24 11:44:05 +0200235/* A global buffer used to store all startup alerts/warnings. It will then be
236 * retrieve on the CLI. */
Willy Tarreau869efd52019-11-15 15:16:57 +0100237static struct ring *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200238
William Lallemand723b73a2012-02-08 16:37:49 +0100239struct logformat_var_args {
240 char *name;
241 int mask;
242};
243
244struct logformat_var_args var_args_list[] = {
245// global
246 { "M", LOG_OPT_MANDATORY },
247 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200248 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100249 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100250 { 0, 0 }
251};
252
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200253/* return the name of the directive used in the current proxy for which we're
254 * currently parsing a header, when it is known.
255 */
256static inline const char *fmt_directive(const struct proxy *curproxy)
257{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100258 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200259 case ARGC_ACL:
260 return "acl";
261 case ARGC_STK:
262 return "stick";
263 case ARGC_TRK:
264 return "track-sc";
265 case ARGC_LOG:
266 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200267 case ARGC_LOGSD:
268 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100269 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100270 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100271 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100272 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200273 case ARGC_UIF:
274 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100275 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200276 return "redirect";
277 case ARGC_CAP:
278 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200279 case ARGC_SRV:
280 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200281 case ARGC_SPOE:
282 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100283 case ARGC_UBK:
284 return "use_backend";
Christopher Faulet3b967c12020-05-15 15:47:44 +0200285 case ARGC_HERR:
286 return "http-error";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100287 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200288 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100289 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200290}
291
William Lallemand723b73a2012-02-08 16:37:49 +0100292/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100293 * callback used to configure addr source retrieval
294 */
295int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
296{
297 curproxy->options2 |= PR_O2_SRC_ADDR;
298
299 return 0;
300}
301
302
303/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100304 * Parse args in a logformat_var. Returns 0 in error
305 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100306 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100307int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100308{
309 int i = 0;
310 int end = 0;
311 int flags = 0; // 1 = + 2 = -
312 char *sp = NULL; // start pointer
313
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100314 if (args == NULL) {
315 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100316 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100317 }
William Lallemand723b73a2012-02-08 16:37:49 +0100318
319 while (1) {
320 if (*args == '\0')
321 end = 1;
322
323 if (*args == '+') {
324 // add flag
325 sp = args + 1;
326 flags = 1;
327 }
328 if (*args == '-') {
329 // delete flag
330 sp = args + 1;
331 flags = 2;
332 }
333
334 if (*args == '\0' || *args == ',') {
335 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100336 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100337 if (strcmp(sp, var_args_list[i].name) == 0) {
338 if (flags == 1) {
339 node->options |= var_args_list[i].mask;
340 break;
341 } else if (flags == 2) {
342 node->options &= ~var_args_list[i].mask;
343 break;
344 }
345 }
346 }
347 sp = NULL;
348 if (end)
349 break;
350 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100351 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100352 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100353 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100354}
355
356/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100357 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
358 * must pass the args part in the <arg> pointer with its length in <arg_len>,
359 * and varname with its length in <var> and <var_len> respectively. <arg> is
360 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100361 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100362 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100363int 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 +0100364{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100365 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200366 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100367
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100368 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
369 if (strlen(logformat_keywords[j].name) == var_len &&
370 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
371 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200372 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100373 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100374 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200375 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100376 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100377 node->type = logformat_keywords[j].type;
378 node->options = *defoptions;
379 if (arg_len) {
380 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100381 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200382 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100383 }
384 if (node->type == LOG_FMT_GLOBAL) {
385 *defoptions = node->options;
386 free(node->arg);
387 free(node);
388 } else {
389 if (logformat_keywords[j].config_callback &&
390 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200391 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100392 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100393 curproxy->to_log |= logformat_keywords[j].lw;
394 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100395 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100396 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100397 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
398 curproxy->conf.args.file, curproxy->conf.args.line,
399 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100400 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100401 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100402 memprintf(err, "format variable '%s' is reserved for HTTP mode",
403 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200404 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100405 }
William Lallemand723b73a2012-02-08 16:37:49 +0100406 }
407 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100408
409 j = var[var_len];
410 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100411 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 +0100412 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200413
414 error_free:
415 if (node) {
416 free(node->arg);
417 free(node);
418 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100419 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100420}
421
422/*
423 * push to the logformat linked list
424 *
425 * start: start pointer
426 * end: end text pointer
427 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100428 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100429 *
430 * LOG_TEXT: copy chars from start to end excluding end.
431 *
432*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100433int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100434{
435 char *str;
436
Willy Tarreaua3571662012-12-20 21:59:12 +0100437 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200438 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100439 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100440 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100441 return 0;
442 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200443 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100444 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100445 str[end - start] = '\0';
446 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100447 node->type = LOG_FMT_TEXT; // type string
448 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100449 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200450 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100451 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100452 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100453 return 0;
454 }
William Lallemand1d705562012-03-12 12:46:41 +0100455 node->type = LOG_FMT_SEPARATOR;
456 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100457 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100458 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100459}
460
461/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100462 * Parse the sample fetch expression <text> and add a node to <list_format> upon
463 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100464 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
465 * is passed in <endptr>, it will be updated with the pointer to the first character
466 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100467 *
468 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100469 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100470int 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 +0100471{
472 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200473 struct sample_expr *expr = NULL;
474 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100475 int cmd_arg;
476
477 cmd[0] = text;
478 cmd[1] = "";
479 cmd_arg = 0;
480
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100481 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 +0100482 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100483 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200484 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100485 }
486
Vincent Bernat02779b62016-04-03 13:48:43 +0200487 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100488 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100489 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200490 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100491 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100492 node->type = LOG_FMT_EXPR;
493 node->expr = expr;
494 node->options = options;
495
496 if (arg_len) {
497 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100498 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200499 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100500 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100501 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100502 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
503
Willy Tarreau434c57c2013-01-08 01:10:24 +0100504 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100505 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
506
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100507 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100508 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
509 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200510 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100511 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100512
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200513 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100514 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100515 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100516
William Lallemand65ad6e12014-01-31 15:08:02 +0100517 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
518 * needed with some sample fetches (eg: ssl*). We always set it for
519 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100520 */
521 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200522 if (curpx->http_needed)
523 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100524 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100525 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200526
527 error_free:
528 release_sample_expr(expr);
529 if (node) {
530 free(node->arg);
531 free(node);
532 }
533 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100534}
535
536/*
William Lallemand723b73a2012-02-08 16:37:49 +0100537 * Parse the log_format string and fill a linked list.
538 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200539 * You can set arguments using { } : %{many arguments}varname.
540 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100541 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500542 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100543 * curproxy: the proxy affected
544 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100545 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100546 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100547 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100548 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100549 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100550int parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100551{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100552 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100553 char *arg = NULL; /* start pointer for args */
554 char *var = NULL; /* start pointer for vars */
555 int arg_len = 0;
556 int var_len = 0;
557 int cformat; /* current token format */
558 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100559 struct logformat_node *tmplf, *back;
560
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100561 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100562 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100563 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100564 return 0;
565 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200566 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200567
William Lallemand723b73a2012-02-08 16:37:49 +0100568 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100569 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100570 LIST_DEL(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200571 release_sample_expr(tmplf->expr);
572 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100573 free(tmplf);
574 }
575
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100576 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100577 pformat = cformat;
578
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100579 if (!*str)
580 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100581
Joseph Herlant85b40592018-11-15 12:10:04 -0800582 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100583 * second have all common paths processed at one place. The common paths are the ones
584 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
585 * We use the common LF_INIT state to dispatch to the different final states.
586 */
587 switch (pformat) {
588 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100589 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100590 arg_len = var_len = 0;
591 if (*str == '{') { // optional argument
592 cformat = LF_STARG;
593 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100594 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100595 else if (*str == '[') {
596 cformat = LF_STEXPR;
597 var = str + 1; // store expr in variable name
598 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100599 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100600 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100601 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100602 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100603 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500604 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100605 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100606 /* single '%' followed by blank or digit, send them both */
607 cformat = LF_TEXT;
608 pformat = LF_TEXT; /* finally we include the previous char as well */
609 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600610 memprintf(err, "unexpected variable name near '%c' at position %d line : '%s'. Maybe you want to write a single '%%', use the syntax '%%%%'",
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100611 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100612 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100613
614 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100615 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500616 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100617 break;
618
619 case LF_STARG: // text immediately following '%{'
620 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100621 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100622 arg_len = str - arg;
623 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100624 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100625 break;
626
627 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100628 if (*str == '[') {
629 cformat = LF_STEXPR;
630 var = str + 1; // store expr in variable name
631 break;
632 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100633 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100634 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100635 var = str;
636 break;
637 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100638 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100639 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100640
Willy Tarreauc8368452012-12-21 00:09:23 +0100641 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100642 /* the whole sample expression is parsed at once,
643 * returning the pointer to the first character not
644 * part of the expression, which MUST be the trailing
645 * angle bracket.
646 */
647 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
648 goto fail;
649
650 if (*str == ']') {
651 // end of arg, go on with next state
652 cformat = pformat = LF_EDEXPR;
653 sp = str;
654 }
655 else {
656 char c = *str;
657 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100658 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100659 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
660 else
661 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200662 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100663 }
664 break;
665
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100666 case LF_VAR: // text part of a variable name
667 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100668 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100669 cformat = LF_INIT; // not variable name anymore
670 break;
671
Willy Tarreauc8368452012-12-21 00:09:23 +0100672 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100673 cformat = LF_INIT;
674 }
675
676 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
677 switch (*str) {
678 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100679 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200680 case ' ':
681 if (options & LOG_OPT_MERGE_SPACES) {
682 cformat = LF_SEPARATOR;
683 break;
684 }
685 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100686 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100687 }
688 }
689
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100690 if (cformat != pformat || pformat == LF_SEPARATOR) {
691 switch (pformat) {
692 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100693 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100694 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100695 break;
696 case LF_TEXT:
697 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100698 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100699 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100700 break;
701 }
702 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100703 }
704 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100705
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100706 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100707 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100708 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100709 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100710 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100711
712 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100713 fail:
714 free(backfmt);
715 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100716}
717
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200718/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500719 * Parse the first range of indexes from a string made of a list of comma separated
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200720 * ranges of indexes. Note that an index may be considered as a particular range
721 * with a high limit to the low limit.
722 */
723int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
724{
725 char *end, *p;
726
727 *low = *high = 0;
728
729 p = *arg;
730 end = strchr(p, ',');
731 if (!end)
732 end = p + strlen(p);
733
734 *high = *low = read_uint((const char **)&p, end);
735 if (!*low || (p != end && *p != '-'))
736 goto err;
737
738 if (p == end)
739 goto done;
740
741 p++;
742 *high = read_uint((const char **)&p, end);
743 if (!*high || *high <= *low || p != end)
744 goto err;
745
746 done:
747 if (*end == ',')
748 end++;
749 *arg = end;
750 return 1;
751
752 err:
753 memprintf(err, "wrong sample range '%s'", *arg);
754 return 0;
755}
756
757/*
758 * Returns 1 if the range defined by <low> and <high> overlaps
759 * one of them in <rgs> array of ranges with <sz> the size of this
760 * array, 0 if not.
761 */
762int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
763 unsigned int low, unsigned int high, char **err)
764{
765 size_t i;
766
767 for (i = 0; i < sz; i++) {
768 if ((low >= rgs[i].low && low <= rgs[i].high) ||
769 (high >= rgs[i].low && high <= rgs[i].high)) {
770 memprintf(err, "ranges are overlapping");
771 return 1;
772 }
773 }
774
775 return 0;
776}
777
778int smp_log_range_cmp(const void *a, const void *b)
779{
780 const struct smp_log_range *rg_a = a;
781 const struct smp_log_range *rg_b = b;
782
783 if (rg_a->high < rg_b->low)
784 return -1;
785 else if (rg_a->low > rg_b->high)
786 return 1;
787
788 return 0;
789}
790
791/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200792 * Parse "log" keyword and update <logsrvs> list accordingly.
793 *
794 * When <do_del> is set, it means the "no log" line was parsed, so all log
795 * servers in <logsrvs> are released.
796 *
797 * Otherwise, we try to parse the "log" line. First of all, when the list is not
798 * the global one, we look for the parameter "global". If we find it,
799 * global.logsrvs is copied. Else we parse each arguments.
800 *
801 * The function returns 1 in success case, otherwise, it returns 0 and err is
802 * filled.
803 */
804int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
805{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100806 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200807 struct sockaddr_storage *sk;
808 struct logsrv *logsrv = NULL;
809 int port1, port2;
810 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200811 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200812
813 /*
814 * "no log": delete previous herited or defined syslog
815 * servers.
816 */
817 if (do_del) {
818 struct logsrv *back;
819
820 if (*(args[1]) != 0) {
821 memprintf(err, "'no log' does not expect arguments");
822 goto error;
823 }
824
825 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
826 LIST_DEL(&logsrv->list);
827 free(logsrv);
828 }
829 return 1;
830 }
831
832 /*
833 * "log global": copy global.logrsvs linked list to the end of logsrvs
834 * list. But first, we check (logsrvs != global.logsrvs).
835 */
836 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
837 if (logsrvs == &global.logsrvs) {
838 memprintf(err, "'global' is not supported for a global syslog server");
839 goto error;
840 }
841 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200842 struct logsrv *node;
843
844 list_for_each_entry(node, logsrvs, list) {
845 if (node->ref == logsrv)
846 goto skip_logsrv;
847 }
848
849 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200850 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200851 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200852 LIST_INIT(&node->list);
853 LIST_ADDQ(logsrvs, &node->list);
Christopher Faulet28ac0992018-03-26 16:09:19 +0200854
855 skip_logsrv:
856 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200857 }
858 return 1;
859 }
860
861 /*
862 * "log <address> ...: parse a syslog server line
863 */
864 if (*(args[1]) == 0 || *(args[2]) == 0) {
865 memprintf(err, "expects <address> and <facility> %s as arguments",
866 ((logsrvs == &global.logsrvs) ? "" : "or global"));
867 goto error;
868 }
869
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100870 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
871 if (strcmp(args[1], "stdout") == 0)
872 args[1] = "fd@1";
873 else if (strcmp(args[1], "stderr") == 0)
874 args[1] = "fd@2";
875
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200876 logsrv = calloc(1, sizeof(*logsrv));
877 if (!logsrv) {
878 memprintf(err, "out of memory");
879 goto error;
880 }
881
882 /* skip address for now, it will be parsed at the end */
883 cur_arg = 2;
884
885 /* just after the address, a length may be specified */
886 logsrv->maxlen = MAX_SYSLOG_LEN;
887 if (strcmp(args[cur_arg], "len") == 0) {
888 int len = atoi(args[cur_arg+1]);
889 if (len < 80 || len > 65535) {
890 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
891 args[cur_arg+1]);
892 goto error;
893 }
894 logsrv->maxlen = len;
895 cur_arg += 2;
896 }
897 if (logsrv->maxlen > global.max_syslog_len)
898 global.max_syslog_len = logsrv->maxlen;
899
900 /* after the length, a format may be specified */
901 if (strcmp(args[cur_arg], "format") == 0) {
902 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200903 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200904 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
905 goto error;
906 }
907 cur_arg += 2;
908 }
909
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200910 if (strcmp(args[cur_arg], "sample") == 0) {
911 unsigned low, high;
912 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200913 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
914
915 p = args[cur_arg+1];
916 smp_sz_str = strchr(p, ':');
917 if (!smp_sz_str) {
918 memprintf(err, "Missing sample size");
919 goto error;
920 }
921
922 *smp_sz_str++ = '\0';
923
924 end = p + strlen(p);
925
926 while (p != end) {
927 if (!get_logsrv_smp_range(&low, &high, &p, err))
928 goto error;
929
930 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
931 goto error;
932
933 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
934 if (!smp_rgs) {
935 memprintf(err, "out of memory error");
936 goto error;
937 }
938
939 smp_rgs[smp_rgs_sz].low = low;
940 smp_rgs[smp_rgs_sz].high = high;
941 smp_rgs[smp_rgs_sz].sz = high - low + 1;
942 smp_rgs[smp_rgs_sz].curr_idx = 0;
943 if (smp_rgs[smp_rgs_sz].high > smp_sz)
944 smp_sz = smp_rgs[smp_rgs_sz].high;
945 smp_rgs_sz++;
946 }
947
Tim Duesterhus21648002019-06-23 22:10:10 +0200948 if (smp_rgs == NULL) {
949 memprintf(err, "no sampling ranges given");
950 goto error;
951 }
952
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200953 beg = smp_sz_str;
954 end = beg + strlen(beg);
955 new_smp_sz = read_uint((const char **)&beg, end);
956 if (!new_smp_sz || beg != end) {
957 memprintf(err, "wrong sample size '%s' for sample range '%s'",
958 smp_sz_str, args[cur_arg+1]);
959 goto error;
960 }
961
962 if (new_smp_sz < smp_sz) {
963 memprintf(err, "sample size %zu should be greater or equal to "
964 "%zu the maximum of the high ranges limits",
965 new_smp_sz, smp_sz);
966 goto error;
967 }
968 smp_sz = new_smp_sz;
969
970 /* Let's order <smp_rgs> array. */
971 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
972
973 logsrv->lb.smp_rgs = smp_rgs;
974 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
975 logsrv->lb.smp_sz = smp_sz;
976
977 cur_arg += 2;
978 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200979 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200980 /* parse the facility */
981 logsrv->facility = get_log_facility(args[cur_arg]);
982 if (logsrv->facility < 0) {
983 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
984 goto error;
985 }
986 cur_arg++;
987
988 /* parse the max syslog level (default: debug) */
989 logsrv->level = 7;
990 if (*(args[cur_arg])) {
991 logsrv->level = get_log_level(args[cur_arg]);
992 if (logsrv->level < 0) {
993 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
994 goto error;
995 }
996 cur_arg++;
997 }
998
999 /* parse the limit syslog level (default: emerg) */
1000 logsrv->minlvl = 0;
1001 if (*(args[cur_arg])) {
1002 logsrv->minlvl = get_log_level(args[cur_arg]);
1003 if (logsrv->minlvl < 0) {
1004 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1005 goto error;
1006 }
1007 cur_arg++;
1008 }
1009
1010 /* Too many args */
1011 if (*(args[cur_arg])) {
1012 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1013 goto error;
1014 }
1015
1016 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001017 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001018 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001019 logsrv->addr.ss_family = AF_UNSPEC;
1020 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001021 logsrv->sink = NULL;
1022 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001023 goto done;
1024 }
1025
Willy Tarreau5fc93282020-09-16 18:25:03 +02001026 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, NULL,
Willy Tarreaua93e5c72020-09-15 14:01:16 +02001027 err, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001028 if (!sk)
1029 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001030
1031 if (fd != -1)
1032 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001033 logsrv->addr = *sk;
1034
1035 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001036 logsrv->addr = *sk;
1037 if (!port1)
1038 set_host_port(&logsrv->addr, SYSLOG_PORT);
1039 }
Willy Tarreauc046d162019-08-30 15:24:59 +02001040 done:
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001041 LIST_ADDQ(logsrvs, &logsrv->list);
1042 return 1;
1043
1044 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001045 free(smp_rgs);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001046 if (logsrv)
1047 free(logsrv->ring_name);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001048 free(logsrv);
1049 return 0;
1050}
1051
1052
Christopher Fauletd4696382017-10-24 11:44:05 +02001053/* Generic function to display messages prefixed by a label */
1054static void print_message(const char *label, const char *fmt, va_list argp)
1055{
1056 struct tm tm;
1057 char *head, *msg;
1058
1059 head = msg = NULL;
1060
1061 get_localtime(date.tv_sec, &tm);
1062 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
1063 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
1064 memvprintf(&msg, fmt, argp);
1065
Willy Tarreau869efd52019-11-15 15:16:57 +01001066 if (global.mode & MODE_STARTING) {
1067 if (unlikely(!startup_logs))
1068 startup_logs = ring_new(STARTUP_LOG_SIZE);
1069
1070 if (likely(startup_logs)) {
1071 struct ist m[2];
1072
1073 m[0] = ist(head);
1074 m[1] = ist(msg);
1075 /* trim the trailing '\n' */
1076 if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
1077 m[1].len--;
1078 ring_write(startup_logs, ~0, 0, 0, m, 2);
1079 }
1080 }
Christopher Fauletd4696382017-10-24 11:44:05 +02001081
1082 fprintf(stderr, "%s%s", head, msg);
1083 fflush(stderr);
1084
1085 free(head);
1086 free(msg);
1087}
1088
Willy Tarreaubaaee002006-06-26 02:48:02 +02001089/*
1090 * Displays the message on stderr with the date and pid. Overrides the quiet
1091 * mode during startup.
1092 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001093void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001094{
1095 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001096
1097 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaubb869862020-04-16 10:52:41 +02001098 if (!(warned & WARN_EXEC_PATH)) {
1099 const char *path = get_exec_path();
1100
1101 warned |= WARN_EXEC_PATH;
1102 ha_notice("haproxy version is %s\n", haproxy_version);
1103 if (path)
1104 ha_notice("path to executable is %s\n", path);
1105 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001106 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001107 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001108 va_end(argp);
1109 }
1110}
1111
1112
1113/*
1114 * Displays the message on stderr with the date and pid.
1115 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001116void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001117{
1118 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001119
Willy Tarreaubebd2122020-04-15 16:06:11 +02001120 warned |= WARN_ANY;
1121
Willy Tarreaubaaee002006-06-26 02:48:02 +02001122 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1123 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001124 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001125 va_end(argp);
1126 }
1127}
1128
1129/*
William Lallemand9c56a222018-11-21 18:04:52 +01001130 * Displays the message on stderr with the date and pid.
1131 */
1132void ha_notice(const char *fmt, ...)
1133{
1134 va_list argp;
1135
1136 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1137 va_start(argp, fmt);
1138 print_message("NOTICE", fmt, argp);
1139 va_end(argp);
1140 }
1141}
1142
1143/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001144 * Displays the message on <out> only if quiet mode is not set.
1145 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001146void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001147{
1148 va_list argp;
1149
1150 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1151 va_start(argp, fmt);
1152 vfprintf(out, fmt, argp);
1153 fflush(out);
1154 va_end(argp);
1155 }
1156}
1157
1158/*
Emeric Brun54648852020-07-06 15:54:06 +02001159 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001160 */
Emeric Brun54648852020-07-06 15:54:06 +02001161enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001162{
Emeric Brun54648852020-07-06 15:54:06 +02001163 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001164
1165 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001166 while (format > 0 && log_formats[format].name
1167 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +02001168 format--;
1169
Emeric Brun54648852020-07-06 15:54:06 +02001170 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001171 return format;
1172}
1173
1174/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001175 * returns log level for <lev> or -1 if not found.
1176 */
1177int get_log_level(const char *lev)
1178{
1179 int level;
1180
1181 level = NB_LOG_LEVELS - 1;
1182 while (level >= 0 && strcmp(log_levels[level], lev))
1183 level--;
1184
1185 return level;
1186}
1187
Willy Tarreaubaaee002006-06-26 02:48:02 +02001188/*
1189 * returns log facility for <fac> or -1 if not found.
1190 */
1191int get_log_facility(const char *fac)
1192{
1193 int facility;
1194
1195 facility = NB_LOG_FACILITIES - 1;
1196 while (facility >= 0 && strcmp(log_facilities[facility], fac))
1197 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001198
Willy Tarreaubaaee002006-06-26 02:48:02 +02001199 return facility;
1200}
1201
William Lallemanda1cc3812012-02-08 16:38:44 +01001202/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001203 * Encode the string.
1204 *
1205 * When using the +E log format option, it will try to escape '"\]'
1206 * characters with '\' as prefix. The same prefix should not be used as
1207 * <escape>.
1208 */
1209static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001210 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001211 const char *string,
1212 struct logformat_node *node)
1213{
1214 if (node->options & LOG_OPT_ESC) {
1215 if (start < stop) {
1216 stop--; /* reserve one byte for the final '\0' */
1217 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001218 if (!ha_bit_test((unsigned char)(*string), map)) {
1219 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001220 *start++ = *string;
1221 else {
1222 if (start + 2 >= stop)
1223 break;
1224 *start++ = '\\';
1225 *start++ = *string;
1226 }
1227 }
1228 else {
1229 if (start + 3 >= stop)
1230 break;
1231 *start++ = escape;
1232 *start++ = hextab[(*string >> 4) & 15];
1233 *start++ = hextab[*string & 15];
1234 }
1235 string++;
1236 }
1237 *start = '\0';
1238 }
1239 }
1240 else {
1241 return encode_string(start, stop, escape, map, string);
1242 }
1243
1244 return start;
1245}
1246
1247/*
1248 * Encode the chunk.
1249 *
1250 * When using the +E log format option, it will try to escape '"\]'
1251 * characters with '\' as prefix. The same prefix should not be used as
1252 * <escape>.
1253 */
1254static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001255 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001256 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001257 struct logformat_node *node)
1258{
1259 char *str, *end;
1260
1261 if (node->options & LOG_OPT_ESC) {
1262 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001263 str = chunk->area;
1264 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001265
1266 stop--; /* reserve one byte for the final '\0' */
1267 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001268 if (!ha_bit_test((unsigned char)(*str), map)) {
1269 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001270 *start++ = *str;
1271 else {
1272 if (start + 2 >= stop)
1273 break;
1274 *start++ = '\\';
1275 *start++ = *str;
1276 }
1277 }
1278 else {
1279 if (start + 3 >= stop)
1280 break;
1281 *start++ = escape;
1282 *start++ = hextab[(*str >> 4) & 15];
1283 *start++ = hextab[*str & 15];
1284 }
1285 str++;
1286 }
1287 *start = '\0';
1288 }
1289 }
1290 else {
1291 return encode_chunk(start, stop, escape, map, chunk);
1292 }
1293
1294 return start;
1295}
1296
1297/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001298 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001299 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001300 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001301 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001302 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001303char *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 +01001304{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001305 if (size < 2)
1306 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001307
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001308 if (node->options & LOG_OPT_QUOTE) {
1309 *(dst++) = '"';
1310 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001311 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001312
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001313 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001314 if (++len > size)
1315 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001316 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001317 char *ret;
1318
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001319 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001320 if (ret == NULL || *ret != '\0')
1321 return NULL;
1322 len = ret - dst;
1323 }
1324 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001325 len = strlcpy2(dst, src, len);
1326 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001327
1328 size -= len;
1329 dst += len;
1330 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001331 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1332 if (size < 2)
1333 return NULL;
1334 *(dst++) = '-';
1335 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001336
1337 if (node->options & LOG_OPT_QUOTE) {
1338 if (size < 2)
1339 return NULL;
1340 *(dst++) = '"';
1341 }
1342
1343 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001344 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001345}
1346
Willy Tarreau26ffa852018-09-05 15:23:10 +02001347static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001348{
1349 return lf_text_len(dst, src, size, size, node);
1350}
1351
William Lallemand5f232402012-04-05 18:02:55 +02001352/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001353 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001354 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001355 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001356char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001357{
1358 char *ret = dst;
1359 int iret;
1360 char pn[INET6_ADDRSTRLEN];
1361
1362 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001363 unsigned char *addr = NULL;
1364 switch (sockaddr->sa_family) {
1365 case AF_INET:
1366 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1367 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1368 break;
1369 case AF_INET6:
1370 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1371 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1372 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1373 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1374 break;
1375 default:
1376 return NULL;
1377 }
William Lallemand5f232402012-04-05 18:02:55 +02001378 if (iret < 0 || iret > size)
1379 return NULL;
1380 ret += iret;
1381 } else {
1382 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1383 ret = lf_text(dst, pn, size, node);
1384 if (ret == NULL)
1385 return NULL;
1386 }
1387 return ret;
1388}
1389
1390/*
1391 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001392 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001393 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001394char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001395{
1396 char *ret = dst;
1397 int iret;
1398
1399 if (node->options & LOG_OPT_HEXA) {
1400 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1401 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1402 if (iret < 0 || iret > size)
1403 return NULL;
1404 ret += iret;
1405 } else {
1406 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1407 if (ret == NULL)
1408 return NULL;
1409 }
1410 return ret;
1411}
1412
Emeric Brun54648852020-07-06 15:54:06 +02001413
1414/*
1415 * This function sends the syslog message using a printf format string. It
1416 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001417 */
Emeric Brun54648852020-07-06 15:54:06 +02001418void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001419{
Emeric Brun54648852020-07-06 15:54:06 +02001420 va_list argp;
1421 int data_len;
1422
1423 if (level < 0 || format == NULL || logline == NULL)
1424 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001425
Emeric Brun54648852020-07-06 15:54:06 +02001426 va_start(argp, format);
1427 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1428 if (data_len < 0 || data_len > global.max_syslog_len)
1429 data_len = global.max_syslog_len;
1430 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001431
Emeric Brun54648852020-07-06 15:54:06 +02001432 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1433 logline, data_len, default_rfc5424_sd_log_format, 2);
1434}
1435/*
1436 * This function builds a log header of given format using given
1437 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1438 * to determine format based on given metadas. It is useful
1439 * for log-forwarding to be able to forward any format without
1440 * settings.
1441 * This function returns a struct ist array of elements of the header
1442 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001443 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001444 * elements.
1445 */
1446struct ist *build_log_header(enum log_fmt format, int level, int facility,
1447 struct ist *metadata, size_t *nbelem)
1448{
1449 static THREAD_LOCAL struct {
1450 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1451 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1452 time_t cur_legacy_time;
1453 char priority_buffer[6];
1454 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001455
Emeric Brun54648852020-07-06 15:54:06 +02001456 struct tm logtime;
1457 int len;
1458 int fac_level = 0;
1459 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001460
Emeric Brun54648852020-07-06 15:54:06 +02001461 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001462
Emeric Brun54648852020-07-06 15:54:06 +02001463
1464 if (format == LOG_FORMAT_UNSPEC) {
1465 format = LOG_FORMAT_RAW;
1466 if (metadata) {
1467 /* If a hostname is set, it appears we want to perform syslog
1468 * because only rfc5427 or rfc3164 support an hostname.
1469 */
1470 if (metadata[LOG_META_HOST].len) {
1471 /* If a rfc5424 compliant timestamp is used we consider
1472 * that output format is rfc5424, else legacy format
1473 * is used as specified default for local logs
1474 * in documentation.
1475 */
1476 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1477 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1478 format = LOG_FORMAT_RFC5424;
1479 else
1480 format = LOG_FORMAT_RFC3164;
1481 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001482 else if (metadata[LOG_META_TAG].len) {
1483 /* Tag is present but no hostname, we should
1484 * consider we try to emmit a local log
1485 * in legacy format (analog to RFC3164 but
1486 * with stripped hostname).
1487 */
1488 format = LOG_FORMAT_LOCAL;
1489 }
Emeric Brun54648852020-07-06 15:54:06 +02001490 else if (metadata[LOG_META_PRIO].len) {
1491 /* the source seems a parsed message
1492 * offering a valid level/prio prefix
1493 * so we consider this format.
1494 */
1495 format = LOG_FORMAT_PRIO;
1496 }
1497 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001498 }
1499
Emeric Brun54648852020-07-06 15:54:06 +02001500 /* prepare priority, stored into 1 single elem */
1501 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001502 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001503 case LOG_FORMAT_RFC3164:
1504 case LOG_FORMAT_RFC5424:
1505 case LOG_FORMAT_PRIO:
1506 fac_level = facility << 3;
1507 /* further format ignore the facility */
1508 /* fall through */
1509 case LOG_FORMAT_TIMED:
1510 case LOG_FORMAT_SHORT:
1511 fac_level += level;
1512 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1513 do {
1514 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1515 fac_level /= 10;
1516 hdr_ctx.ist_vector[*nbelem].ptr--;
1517 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1518 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1519 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1520 break;
1521 case LOG_FORMAT_ISO:
1522 case LOG_FORMAT_RAW:
1523 break;
1524 case LOG_FORMAT_UNSPEC:
1525 case LOG_FORMATS:
1526 ABORT_NOW();
1527 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001528
William Lallemand2a4a44f2012-02-06 16:00:33 +01001529
Emeric Brun54648852020-07-06 15:54:06 +02001530 /* prepare timestamp, stored into a max of 4 elems */
1531 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001532 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001533 case LOG_FORMAT_RFC3164:
1534 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1535 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1536 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1537 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001538 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001539 break;
1540 }
1541 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1542 int month;
1543 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001544
Emeric Brun54648852020-07-06 15:54:06 +02001545 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001546
Emeric Brun54648852020-07-06 15:54:06 +02001547 /* compute month */
1548 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1549 if (month)
1550 month--;
1551 if (month <= 11) {
1552 /* builds log prefix ex: 'Jan 1 ' */
1553 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1554 "%s %c%c ", monthname[month],
1555 timestamp[8] != '0' ? timestamp[8] : ' ',
1556 timestamp[9]);
1557 /* we reused the timestamp_buffer, signal that it does not
1558 * contain local time anymore
1559 */
1560 hdr_ctx.cur_legacy_time = 0;
1561 if (len == 7) {
1562 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1563 /* adds 'HH:MM:SS' from iso time */
1564 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1565 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1566 /* we successfully reuse iso time, we can break */
1567 break;
1568 }
1569 }
1570 /* Failed to reuse isotime time, fallback to local legacy time */
1571 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001572
Emeric Brun54648852020-07-06 15:54:06 +02001573 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1574 /* re-builds timestamp from the current local time */
1575 get_localtime(time, &logtime);
1576
1577 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1578 "%s %2d %02d:%02d:%02d ",
1579 monthname[logtime.tm_mon],
1580 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1581 if (len != LOG_LEGACYTIME_LEN+1)
1582 hdr_ctx.cur_legacy_time = 0;
1583 else
1584 hdr_ctx.cur_legacy_time = time;
1585 }
1586 if (likely(hdr_ctx.cur_legacy_time))
1587 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1588 else
1589 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1590 break;
1591 case LOG_FORMAT_RFC5424:
1592 /* adds rfc5425 version prefix */
1593 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1594 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001595 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001596 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1597 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1598 break;
1599 }
1600 /* let continue as 'timed' and 'iso' format for usual timestamp */
1601 /* fall through */
1602 case LOG_FORMAT_TIMED:
1603 case LOG_FORMAT_ISO:
1604 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1605 * '1900:01:01T14:00:00+02:00'
1606 * '1900:01:01T10:00:00.123456-02:00'
1607 */
1608 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1609 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1610 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001611 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001612 break;
1613 }
1614 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1615 int month;
1616 char *timestamp = metadata[LOG_META_TIME].ptr;
1617
1618 for (month = 0; month < 12; month++)
1619 if (!memcmp(monthname[month], timestamp, 3))
1620 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001621
Emeric Brun54648852020-07-06 15:54:06 +02001622 if (month < 12) {
1623
1624 /* get local time to retrieve year */
1625 get_localtime(time, &logtime);
1626
1627 /* year seems changed since log */
1628 if (logtime.tm_mon < month)
1629 logtime.tm_year--;
1630
1631 /* builds rfc5424 prefix ex: '1900-01-01T' */
1632 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1633 "%4d-%02d-%c%cT",
1634 logtime.tm_year+1900, month+1,
1635 timestamp[4] != ' ' ? timestamp[4] : '0',
1636 timestamp[5]);
1637
1638 /* we reused the timestamp_buffer, signal that it does not
1639 * contain local time anymore
1640 */
1641 hdr_ctx.cur_legacy_time = 0;
1642 if (len == 11) {
1643 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1644 /* adds HH:MM:SS from legacy timestamp */
1645 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001646 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001647 /* according to rfc: -00:00 means we don't know the timezone */
1648 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1649 /* we successfully reuse legacy time, we can break */
1650 break;
1651 }
1652 }
1653 /* Failed to reuse legacy time, fallback to local iso time */
1654 }
1655 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1656 break;
1657 case LOG_FORMAT_PRIO:
1658 case LOG_FORMAT_SHORT:
1659 case LOG_FORMAT_RAW:
1660 break;
1661 case LOG_FORMAT_UNSPEC:
1662 case LOG_FORMATS:
1663 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001664 }
1665
Emeric Brun54648852020-07-06 15:54:06 +02001666 /* prepare other meta data, stored into a max of 10 elems */
1667 switch (format) {
1668 case LOG_FORMAT_RFC3164:
1669 if (metadata && metadata[LOG_META_HOST].len) {
1670 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1671 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1672 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001673 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001674 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001675 /* fall through */
1676 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001677 if (!metadata || !metadata[LOG_META_TAG].len)
1678 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001679
Emeric Brun54648852020-07-06 15:54:06 +02001680 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1681 if (metadata[LOG_META_PID].len) {
1682 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1683 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1684 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1685 }
1686 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1687 break;
1688 case LOG_FORMAT_RFC5424:
1689 if (metadata && metadata[LOG_META_HOST].len) {
1690 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1691 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1692 }
1693 else
1694 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001695
Emeric Brun54648852020-07-06 15:54:06 +02001696 if (metadata && metadata[LOG_META_TAG].len) {
1697 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1698 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1699 }
1700 else
1701 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001702
Emeric Brun54648852020-07-06 15:54:06 +02001703 if (metadata && metadata[LOG_META_PID].len) {
1704 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1705 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1706 }
1707 else
1708 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001709
Emeric Brun54648852020-07-06 15:54:06 +02001710 if (metadata && metadata[LOG_META_MSGID].len) {
1711 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1712 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1713 }
1714 else
1715 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001716
Emeric Brun54648852020-07-06 15:54:06 +02001717 if (metadata && metadata[LOG_META_STDATA].len) {
1718 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1719 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1720 }
1721 else
1722 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1723 break;
1724 case LOG_FORMAT_PRIO:
1725 case LOG_FORMAT_SHORT:
1726 case LOG_FORMAT_TIMED:
1727 case LOG_FORMAT_ISO:
1728 case LOG_FORMAT_RAW:
1729 break;
1730 case LOG_FORMAT_UNSPEC:
1731 case LOG_FORMATS:
1732 ABORT_NOW();
1733 }
1734
1735 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001736}
1737
1738/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001739 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001740 * The argument <metadata> MUST be an array of size
1741 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001742 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001743 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001744 */
Emeric Brun54648852020-07-06 15:54:06 +02001745static 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 +01001746{
Emeric Brun54648852020-07-06 15:54:06 +02001747 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001748 static THREAD_LOCAL struct msghdr msghdr = {
1749 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001750 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001751 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001752 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1753 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001754 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001755 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001756 size_t nbelem;
1757 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001758
1759 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001760
Emeric Brunfa9d7802020-05-28 14:21:33 +02001761 /* historically some messages used to already contain the trailing LF
1762 * or Zero. Let's remove all trailing LF or Zero
1763 */
Emeric Brun54648852020-07-06 15:54:06 +02001764 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001765 size--;
1766
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001767 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001768 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001769 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001770 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001771 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1772 /* the socket's address is a file descriptor */
1773 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1774 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001775 else if (logsrv->addr.ss_family == AF_UNIX)
1776 plogfd = &logfdunix;
1777 else
1778 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001779
Willy Tarreauc046d162019-08-30 15:24:59 +02001780 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001781 /* socket not successfully initialized yet */
1782 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1783 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1784 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001785
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001786 if (!once) {
1787 once = 1; /* note: no need for atomic ops here */
1788 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1789 nblogger, strerror(errno), errno);
1790 }
1791 return;
1792 } else {
1793 /* we don't want to receive anything on this socket */
1794 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1795 /* does nothing under Linux, maybe needed for others */
1796 shutdown(*plogfd, SHUT_RD);
1797 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1798 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001799 }
1800
Emeric Brun54648852020-07-06 15:54:06 +02001801 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1802 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001803 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001804 struct ist msg;
1805
1806 msg = ist2(message, size);
1807 if (msg.len > logsrv->maxlen)
1808 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001809
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001810 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1811 }
1812 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1813 struct ist msg;
1814
1815 msg = ist2(message, size);
1816 if (msg.len > logsrv->maxlen)
1817 msg.len = logsrv->maxlen;
1818
1819 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001820 }
1821 else {
Emeric Brun54648852020-07-06 15:54:06 +02001822 int i = 0;
1823 int totlen = logsrv->maxlen;
1824
1825 for (i = 0 ; i < nbelem ; i++ ) {
1826 iovec[i].iov_base = msg_header[i].ptr;
1827 iovec[i].iov_len = msg_header[i].len;
1828 if (totlen <= iovec[i].iov_len) {
1829 iovec[i].iov_len = totlen;
1830 totlen = 0;
1831 break;
1832 }
1833 totlen -= iovec[i].iov_len;
1834 }
1835 if (totlen) {
1836 iovec[i].iov_base = message;
1837 iovec[i].iov_len = size;
1838 if (totlen <= iovec[i].iov_len)
1839 iovec[i].iov_len = totlen;
1840 i++;
1841 }
1842 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1843 iovec[i].iov_len = 1;
1844 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001845
Emeric Brun54648852020-07-06 15:54:06 +02001846 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001847 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1848 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001849
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001850 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1851 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001852
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001853 if (sent < 0) {
1854 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001855
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001856 if (errno == EAGAIN)
1857 _HA_ATOMIC_ADD(&dropped_logs, 1);
1858 else if (!once) {
1859 once = 1; /* note: no need for atomic ops here */
1860 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1861 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001862 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001863 }
1864}
Dragan Dosen59cee972015-09-19 22:09:02 +02001865
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001866/*
1867 * This function sends a syslog message.
1868 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001869 * The argument <metadata> MUST be an array of size
1870 * LOG_META_FIELDS*sizeof(struct ist) containing
1871 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001872 */
Emeric Brun54648852020-07-06 15:54:06 +02001873void process_send_log(struct list *logsrvs, int level, int facility,
1874 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001875{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001876 struct logsrv *logsrv;
1877 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001878
1879 /* Send log messages to syslog server. */
1880 nblogger = 0;
1881 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001882 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001883
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001884 /* we can filter the level of the messages that are sent to each logger */
1885 if (level > logsrv->level)
1886 continue;
1887
Frédéric Lécailled803e472019-04-25 07:42:09 +02001888 if (logsrv->lb.smp_rgs) {
1889 struct smp_log_range *curr_rg;
1890
1891 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1892 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1893 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1894 if (in_range) {
1895 /* Let's consume this range. */
1896 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1897 if (!curr_rg->curr_idx) {
1898 /* If consumed, let's select the next range. */
1899 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1900 }
1901 }
1902 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1903 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1904 }
1905 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001906 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1907 (facility == -1) ? logsrv->facility : facility,
1908 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001909 }
1910}
1911
Emeric Brun54648852020-07-06 15:54:06 +02001912/*
1913 * This function sends a syslog message.
1914 * It doesn't care about errors nor does it report them.
1915 * The arguments <sd> and <sd_size> are used for the structured-data part
1916 * in RFC5424 formatted syslog messages.
1917 */
1918void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1919 char *message, size_t size, char *sd, size_t sd_size)
1920{
1921 static THREAD_LOCAL pid_t curr_pid;
1922 static THREAD_LOCAL char pidstr[16];
1923 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1924
1925 if (logsrvs == NULL) {
1926 if (!LIST_ISEMPTY(&global.logsrvs)) {
1927 logsrvs = &global.logsrvs;
1928 }
1929 }
1930 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1931 return;
1932
1933 if (!metadata[LOG_META_HOST].len) {
1934 if (global.log_send_hostname)
1935 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02001936 }
1937
1938 if (!tagb || !tagb->area)
1939 tagb = &global.log_tag;
1940
1941 if (tagb)
1942 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1943
1944 if (unlikely(curr_pid != getpid()))
1945 metadata[LOG_META_PID].len = 0;
1946
1947 if (!metadata[LOG_META_PID].len) {
1948 curr_pid = getpid();
1949 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1950 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1951 }
1952
1953 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1954
1955 /* Remove trailing space of structured data */
1956 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1957 metadata[LOG_META_STDATA].len--;
1958
1959 return process_send_log(logsrvs, level, -1, metadata, message, size);
1960}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001961
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001962const 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 +01001963const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1964 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1965 Set-cookie Updated, unknown, unknown */
1966
William Lallemand1d705562012-03-12 12:46:41 +01001967/*
1968 * try to write a character if there is enough space, or goto out
1969 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001970#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001971 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001972 *(tmplog++) = (x); \
1973 } else { \
1974 goto out; \
1975 } \
1976 } while(0)
1977
Dragan Dosen835b9212016-02-12 13:23:03 +01001978
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001979/* Initializes some log data at boot */
1980static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001981{
1982 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001983 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001984
1985 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1986 * inside PARAM-VALUE should be escaped with '\' as prefix.
1987 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1988 * details.
1989 */
1990 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1991
1992 tmp = "\"\\]";
1993 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001994 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001995 tmp++;
1996 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001997
1998 /* initialize the log header encoding map : '{|}"#' should be encoded with
1999 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
2000 * URL encoding only requires '"', '#' to be encoded as well as non-
2001 * printable characters above.
2002 */
2003 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
2004 memset(url_encode_map, 0, sizeof(url_encode_map));
2005 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002006 ha_bit_set(i, hdr_encode_map);
2007 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002008 }
2009 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002010 ha_bit_set(i, hdr_encode_map);
2011 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002012 }
2013
2014 tmp = "\"#{|}";
2015 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002016 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002017 tmp++;
2018 }
2019
2020 tmp = "\"#";
2021 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002022 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002023 tmp++;
2024 }
2025
2026 /* initialize the http header encoding map. The draft httpbis define the
2027 * header content as:
2028 *
2029 * HTTP-message = start-line
2030 * *( header-field CRLF )
2031 * CRLF
2032 * [ message-body ]
2033 * header-field = field-name ":" OWS field-value OWS
2034 * field-value = *( field-content / obs-fold )
2035 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2036 * obs-fold = CRLF 1*( SP / HTAB )
2037 * field-vchar = VCHAR / obs-text
2038 * VCHAR = %x21-7E
2039 * obs-text = %x80-FF
2040 *
2041 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
2042 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08002043 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02002044 */
2045 memset(http_encode_map, 0, sizeof(http_encode_map));
2046 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002047 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002048 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002049 ha_bit_set(i, http_encode_map);
2050 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002051}
William Lallemand1d705562012-03-12 12:46:41 +01002052
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002053INITCALL0(STG_PREPARE, init_log);
2054
Christopher Faulet0132d062017-07-26 15:33:35 +02002055/* Initialize log buffers used for syslog messages */
2056int init_log_buffers()
2057{
Christopher Faulet0132d062017-07-26 15:33:35 +02002058 logline = my_realloc2(logline, global.max_syslog_len + 1);
2059 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02002060 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02002061 return 0;
2062 return 1;
2063}
2064
2065/* Deinitialize log buffers used for syslog messages */
2066void deinit_log_buffers()
2067{
Christopher Faulet0132d062017-07-26 15:33:35 +02002068 free(logline);
2069 free(logline_rfc5424);
Willy Tarreau869efd52019-11-15 15:16:57 +01002070 ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
Christopher Faulet0132d062017-07-26 15:33:35 +02002071 logline = NULL;
2072 logline_rfc5424 = NULL;
2073}
2074
Willy Tarreaudf974472012-12-28 02:44:01 +01002075/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2076 * <maxsize> characters. Returns the size of the output string in characters,
2077 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002078 * is not zero. It requires a valid session and optionally a stream. If the
2079 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002080 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002081int 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 +02002082{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002083 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002084 struct proxy *be;
2085 struct http_txn *txn;
2086 const struct strm_logs *logs;
Willy Tarreau8fa99842019-07-17 11:47:11 +02002087 struct connection *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002088 unsigned int s_flags;
2089 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002090 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002091 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002092 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002093 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002094 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002095 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002096 int t_request;
2097 int hdr;
2098 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002099 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002100 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002101 char *ret;
2102 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002103 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002104 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002105 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002106 struct ist path;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002107
William Lallemandbddd4fd2012-02-27 11:23:10 +01002108 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002109
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002110 if (likely(s)) {
2111 be = s->be;
2112 txn = s->txn;
2113 be_conn = cs_conn(objt_cs(s->si[1].end));
2114 s_flags = s->flags;
2115 uniq_id = s->uniq_id;
2116 logs = &s->logs;
2117 } else {
2118 /* we have no stream so we first need to initialize a few
2119 * things that are needed later. We do increment the request
2120 * ID so that it's uniquely assigned to this request just as
2121 * if the request had reached the point of being processed.
2122 * A request error is reported as it's the only element we have
2123 * here and which justifies emitting such a log.
2124 */
2125 be = fe;
2126 txn = NULL;
2127 be_conn = NULL;
2128 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01002129 uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002130
2131 /* prepare a valid log structure */
2132 tmp_strm_log.tv_accept = sess->tv_accept;
2133 tmp_strm_log.accept_date = sess->accept_date;
2134 tmp_strm_log.t_handshake = sess->t_handshake;
2135 tmp_strm_log.t_idle = tv_ms_elapsed(&sess->tv_accept, &now) - sess->t_handshake;
2136 tv_zero(&tmp_strm_log.tv_request);
2137 tmp_strm_log.t_queue = -1;
2138 tmp_strm_log.t_connect = -1;
2139 tmp_strm_log.t_data = -1;
2140 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2141 tmp_strm_log.bytes_in = 0;
2142 tmp_strm_log.bytes_out = 0;
2143 tmp_strm_log.prx_queue_pos = 0;
2144 tmp_strm_log.srv_queue_pos = 0;
2145
2146 logs = &tmp_strm_log;
2147 }
2148
William Lallemandbddd4fd2012-02-27 11:23:10 +01002149 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002150 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2151 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002152
William Lallemand1d705562012-03-12 12:46:41 +01002153 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002154
William Lallemandbddd4fd2012-02-27 11:23:10 +01002155 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002156 if (LIST_ISEMPTY(list_format))
2157 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002158
William Lallemand1d705562012-03-12 12:46:41 +01002159 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002160 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002161 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002162 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002163 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002164
Willy Tarreauc8368452012-12-21 00:09:23 +01002165 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002166 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002167 if (!last_isspace) {
2168 LOGCHAR(' ');
2169 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002170 }
2171 break;
2172
William Lallemand1d705562012-03-12 12:46:41 +01002173 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002174 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002175 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002176 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002177 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002178 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002179 last_isspace = 0;
2180 break;
2181
Willy Tarreauc8368452012-12-21 00:09:23 +01002182 case LOG_FMT_EXPR: // sample expression, may be request or response
2183 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002184 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002185 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 +02002186 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002187 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 +01002188 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002189 ret = lf_encode_chunk(tmplog, dst + maxsize,
2190 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002191 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002192 ret = lf_text_len(tmplog,
2193 key ? key->data.u.str.area : NULL,
2194 key ? key->data.u.str.data : 0,
2195 dst + maxsize - tmplog,
2196 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002197 if (ret == 0)
2198 goto out;
2199 tmplog = ret;
2200 last_isspace = 0;
2201 break;
2202
Willy Tarreau2beef582012-12-20 17:22:52 +01002203 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002204 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002205 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002206 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002207 else
2208 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002209 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002210 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002211 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002212 last_isspace = 0;
2213 break;
2214
Willy Tarreau2beef582012-12-20 17:22:52 +01002215 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002216 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002217 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002218 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002219 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002220 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002221 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002222 dst + maxsize - tmplog, tmp);
2223 }
William Lallemand5f232402012-04-05 18:02:55 +02002224 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002225 else
2226 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2227
William Lallemand5f232402012-04-05 18:02:55 +02002228 if (ret == NULL)
2229 goto out;
2230 tmplog = ret;
2231 last_isspace = 0;
2232 break;
2233
Willy Tarreau2beef582012-12-20 17:22:52 +01002234 case LOG_FMT_FRONTENDIP: // %fi
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 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002238 }
2239 else
2240 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2241
William Lallemand1d705562012-03-12 12:46:41 +01002242 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002243 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002244 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002245 last_isspace = 0;
2246 break;
2247
Willy Tarreau2beef582012-12-20 17:22:52 +01002248 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002249 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002250 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002251 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002252 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002253 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002254 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002255 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002256 else
2257 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2258
William Lallemand5f232402012-04-05 18:02:55 +02002259 if (ret == NULL)
2260 goto out;
2261 tmplog = ret;
2262 last_isspace = 0;
2263 break;
2264
Willy Tarreau2beef582012-12-20 17:22:52 +01002265 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002266 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002267 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002268 else
2269 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2270
William Lallemand1d705562012-03-12 12:46:41 +01002271 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002272 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002273 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002274 last_isspace = 0;
2275 break;
2276
Willy Tarreau2beef582012-12-20 17:22:52 +01002277 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002278 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002279 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002280 else
2281 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2282
William Lallemand5f232402012-04-05 18:02:55 +02002283 if (ret == NULL)
2284 goto out;
2285 tmplog = ret;
2286 last_isspace = 0;
2287 break;
2288
Willy Tarreau2beef582012-12-20 17:22:52 +01002289 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002290 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002291 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002292 else
2293 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2294
William Lallemand5f232402012-04-05 18:02:55 +02002295 if (ret == NULL)
2296 goto out;
2297 tmplog = ret;
2298 last_isspace = 0;
2299 break;
2300
Willy Tarreau2beef582012-12-20 17:22:52 +01002301 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002302 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002303 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002304 else
2305 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2306
William Lallemand1d705562012-03-12 12:46:41 +01002307 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002308 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002309 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002310 last_isspace = 0;
2311 break;
2312
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002313 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002314 get_localtime(logs->accept_date.tv_sec, &tm);
2315 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002316 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002317 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002318 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002319 last_isspace = 0;
2320 break;
2321
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002322 case LOG_FMT_tr: // %tr = start of request date
2323 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002324 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 +02002325 get_localtime(tv.tv_sec, &tm);
2326 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2327 if (ret == NULL)
2328 goto out;
2329 tmplog = ret;
2330 last_isspace = 0;
2331 break;
2332
2333 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002334 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002335 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002336 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002337 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002338 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002339 last_isspace = 0;
2340 break;
2341
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002342 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002343 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 +02002344 get_gmtime(tv.tv_sec, &tm);
2345 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2346 if (ret == NULL)
2347 goto out;
2348 tmplog = ret;
2349 last_isspace = 0;
2350 break;
2351
2352 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002353 get_localtime(logs->accept_date.tv_sec, &tm);
2354 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002355 if (ret == NULL)
2356 goto out;
2357 tmplog = ret;
2358 last_isspace = 0;
2359 break;
2360
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002361 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002362 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 +02002363 get_localtime(tv.tv_sec, &tm);
2364 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2365 if (ret == NULL)
2366 goto out;
2367 tmplog = ret;
2368 last_isspace = 0;
2369 break;
2370
William Lallemand5f232402012-04-05 18:02:55 +02002371 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002372 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002373 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002374 if (iret < 0 || iret > dst + maxsize - tmplog)
2375 goto out;
2376 last_isspace = 0;
2377 tmplog += iret;
2378 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002379 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002380 if (ret == NULL)
2381 goto out;
2382 tmplog = ret;
2383 last_isspace = 0;
2384 }
2385 break;
2386
William Lallemand1d705562012-03-12 12:46:41 +01002387 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002388 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002389 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002390 if (iret < 0 || iret > dst + maxsize - tmplog)
2391 goto out;
2392 last_isspace = 0;
2393 tmplog += iret;
2394 } else {
2395 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002396 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002397 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002398 tmplog, 4);
2399 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002400 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002401 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002402 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002403 }
2404 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002405
William Lallemand1d705562012-03-12 12:46:41 +01002406 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002407 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002408 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002409 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002410 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002411 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002412 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002413 break;
2414
Willy Tarreau773d65f2012-10-12 14:56:11 +02002415 case LOG_FMT_FRONTEND_XPRT: // %ft
2416 src = fe->id;
2417 if (tmp->options & LOG_OPT_QUOTE)
2418 LOGCHAR('"');
2419 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2420 if (iret == 0)
2421 goto out;
2422 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002423 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002424 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002425 if (tmp->options & LOG_OPT_QUOTE)
2426 LOGCHAR('"');
2427 last_isspace = 0;
2428 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002429#ifdef USE_OPENSSL
2430 case LOG_FMT_SSL_CIPHER: // %sslc
2431 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002432 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002433 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002434 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002435 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002436 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2437 if (ret == NULL)
2438 goto out;
2439 tmplog = ret;
2440 last_isspace = 0;
2441 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002442
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002443 case LOG_FMT_SSL_VERSION: // %sslv
2444 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002445 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002446 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002447 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002448 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002449 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2450 if (ret == NULL)
2451 goto out;
2452 tmplog = ret;
2453 last_isspace = 0;
2454 break;
2455#endif
William Lallemand1d705562012-03-12 12:46:41 +01002456 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002457 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002458 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002459 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002460 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002461 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002462 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002463 break;
2464
William Lallemand1d705562012-03-12 12:46:41 +01002465 case LOG_FMT_SERVER: // %s
Willy Tarreaue1809df2018-09-05 15:30:16 +02002466 switch (obj_type(s ? s->target : NULL)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002467 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002468 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002469 break;
2470 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002471 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002472 break;
2473 default:
2474 src = "<NOSRV>";
2475 break;
2476 }
William Lallemand5f232402012-04-05 18:02:55 +02002477 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002478 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002479 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002480 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002481 last_isspace = 0;
2482 break;
2483
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002484 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002485 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002486 if (ret == NULL)
2487 goto out;
2488 tmplog = ret;
2489 last_isspace = 0;
2490 break;
2491
2492 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002493 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002494 if (ret == NULL)
2495 goto out;
2496 tmplog = ret;
2497 last_isspace = 0;
2498 break;
2499
2500 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002501 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002502 tmplog, dst + maxsize - tmplog);
2503 if (ret == NULL)
2504 goto out;
2505 tmplog = ret;
2506 last_isspace = 0;
2507 break;
2508
2509 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002510 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002511 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002512 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002513 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002514 last_isspace = 0;
2515 break;
2516
William Lallemand1d705562012-03-12 12:46:41 +01002517 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002518 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002519 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002520 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002521 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002522 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002523 last_isspace = 0;
2524 break;
2525
William Lallemand1d705562012-03-12 12:46:41 +01002526 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002527 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002528 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002529 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002530 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002531 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002532 last_isspace = 0;
2533 break;
2534
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002535 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002536 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002537 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002538 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002539 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002540 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002541 last_isspace = 0;
2542 break;
2543
Willy Tarreau27b639d2016-05-17 17:55:27 +02002544 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002545 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002546 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002547 tmplog, dst + maxsize - tmplog);
2548 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002549 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002550 tmplog, dst + maxsize - tmplog);
2551 if (ret == NULL)
2552 goto out;
2553 tmplog = ret;
2554 last_isspace = 0;
2555 break;
2556
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002557 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2558 if (!(fe->to_log & LW_BYTES))
2559 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002560 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 +02002561 tmplog, dst + maxsize - tmplog);
2562 if (ret == NULL)
2563 goto out;
2564 tmplog = ret;
2565 last_isspace = 0;
2566 break;
2567
2568 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002569 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002570 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002571 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002572 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002573 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002574 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002575 last_isspace = 0;
2576 break;
2577
Damien Claisse57c8eb92020-04-28 12:09:19 +00002578 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2579 if (!(fe->to_log & LW_BYTES))
2580 LOGCHAR('+');
2581 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2582 tmplog, dst + maxsize - tmplog);
2583 if (ret == NULL)
2584 goto out;
2585 tmplog = ret;
2586 last_isspace = 0;
2587 break;
2588
Willy Tarreau2beef582012-12-20 17:22:52 +01002589 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02002590 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002591 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002592 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002593 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002594 last_isspace = 0;
2595 break;
2596
William Lallemand1d705562012-03-12 12:46:41 +01002597 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002598 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002599 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002600 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002601 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002602 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002603 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002604 last_isspace = 0;
2605 break;
2606
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002607 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002608 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002609 if (ret == NULL)
2610 goto out;
2611 tmplog = ret;
2612 last_isspace = 0;
2613 break;
2614
Willy Tarreau2beef582012-12-20 17:22:52 +01002615 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002616 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002617 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002618 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002619 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002620 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002621 last_isspace = 0;
2622 break;
2623
Willy Tarreau2beef582012-12-20 17:22:52 +01002624 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002625 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002626 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002627 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002628 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002629 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002630 last_isspace = 0;
2631 break;
2632
William Lallemand1d705562012-03-12 12:46:41 +01002633 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002634 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2635 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002636 *tmplog = '\0';
2637 last_isspace = 0;
2638 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002639
William Lallemand1d705562012-03-12 12:46:41 +01002640 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002641 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2642 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002643 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2644 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 +01002645 last_isspace = 0;
2646 break;
2647
William Lallemand1d705562012-03-12 12:46:41 +01002648 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002649 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002650 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002651 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002652 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002653 last_isspace = 0;
2654 break;
2655
William Lallemand1d705562012-03-12 12:46:41 +01002656 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002657 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002658 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002659 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002660 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002661 last_isspace = 0;
2662 break;
2663
William Lallemand1d705562012-03-12 12:46:41 +01002664 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002665 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002666 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002667 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002668 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002669 last_isspace = 0;
2670 break;
2671
William Lallemand1d705562012-03-12 12:46:41 +01002672 case LOG_FMT_SRVCONN: // %sc
Willy Tarreaue1809df2018-09-05 15:30:16 +02002673 ret = ultoa_o(objt_server(s ? s->target : NULL) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002674 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002675 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002676 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002677 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002678 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002679 last_isspace = 0;
2680 break;
2681
William Lallemand1d705562012-03-12 12:46:41 +01002682 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002683 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002684 LOGCHAR('+');
Willy Tarreauabd71a52018-09-04 19:21:44 +02002685 ret = ltoa_o((s && s->si[1].conn_retries > 0) ?
Willy Tarreau350f4872014-11-28 14:42:25 +01002686 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002687 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002688 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002689 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002690 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002691 last_isspace = 0;
2692 break;
2693
William Lallemand1d705562012-03-12 12:46:41 +01002694 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002695 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002696 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002697 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002698 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002699 last_isspace = 0;
2700 break;
2701
William Lallemand1d705562012-03-12 12:46:41 +01002702 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002703 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002704 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002705 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002706 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002707 last_isspace = 0;
2708 break;
2709
William Lallemand1d705562012-03-12 12:46:41 +01002710 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002711 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002712 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002713 if (tmp->options & LOG_OPT_QUOTE)
2714 LOGCHAR('"');
2715 LOGCHAR('{');
2716 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2717 if (hdr)
2718 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002719 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002720 ret = lf_encode_string(tmplog, dst + maxsize,
2721 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002722 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002723 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002724 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002725 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002726 }
2727 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002728 if (tmp->options & LOG_OPT_QUOTE)
2729 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002730 last_isspace = 0;
2731 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002732 break;
2733
William Lallemand1d705562012-03-12 12:46:41 +01002734 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002735 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002736 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002737 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2738 if (hdr > 0)
2739 LOGCHAR(' ');
2740 if (tmp->options & LOG_OPT_QUOTE)
2741 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002742 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002743 ret = lf_encode_string(tmplog, dst + maxsize,
2744 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002745 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002746 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002747 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002748 } else if (!(tmp->options & LOG_OPT_QUOTE))
2749 LOGCHAR('-');
2750 if (tmp->options & LOG_OPT_QUOTE)
2751 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002752 last_isspace = 0;
2753 }
2754 }
2755 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002756
William Lallemand1d705562012-03-12 12:46:41 +01002757
2758 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002759 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002760 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002761 if (tmp->options & LOG_OPT_QUOTE)
2762 LOGCHAR('"');
2763 LOGCHAR('{');
2764 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2765 if (hdr)
2766 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002767 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002768 ret = lf_encode_string(tmplog, dst + maxsize,
2769 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002770 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002771 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002772 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002773 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002774 }
2775 LOGCHAR('}');
2776 last_isspace = 0;
2777 if (tmp->options & LOG_OPT_QUOTE)
2778 LOGCHAR('"');
2779 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002780 break;
2781
William Lallemand1d705562012-03-12 12:46:41 +01002782 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002783 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002784 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002785 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2786 if (hdr > 0)
2787 LOGCHAR(' ');
2788 if (tmp->options & LOG_OPT_QUOTE)
2789 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002790 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002791 ret = lf_encode_string(tmplog, dst + maxsize,
2792 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002793 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002794 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002795 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002796 } else if (!(tmp->options & LOG_OPT_QUOTE))
2797 LOGCHAR('-');
2798 if (tmp->options & LOG_OPT_QUOTE)
2799 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002800 last_isspace = 0;
2801 }
2802 }
2803 break;
2804
William Lallemand1d705562012-03-12 12:46:41 +01002805 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002806 /* Request */
2807 if (tmp->options & LOG_OPT_QUOTE)
2808 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002809 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002810 ret = lf_encode_string(tmplog, dst + maxsize,
2811 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002812 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002813 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002814 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002815 if (tmp->options & LOG_OPT_QUOTE)
2816 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002817 last_isspace = 0;
2818 break;
William Lallemand5f232402012-04-05 18:02:55 +02002819
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002820 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002821 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002822
Willy Tarreaub7636d12015-06-17 19:58:02 +02002823 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002824 LOGCHAR('"');
2825
2826 end = uri + strlen(uri);
2827 // look for the first whitespace character
2828 while (uri < end && !HTTP_IS_SPHT(*uri))
2829 uri++;
2830
2831 // keep advancing past multiple spaces
2832 while (uri < end && HTTP_IS_SPHT(*uri)) {
2833 uri++; nspaces++;
2834 }
2835
2836 // look for first space or question mark after url
2837 spc = uri;
2838 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2839 spc++;
2840
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002841 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002842 chunk.area = "<BADREQ>";
2843 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002844 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002845 chunk.area = uri;
2846 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002847 }
2848
Dragan Dosen835b9212016-02-12 13:23:03 +01002849 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002850 if (ret == NULL || *ret != '\0')
2851 goto out;
2852
2853 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002854 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002855 LOGCHAR('"');
2856
2857 last_isspace = 0;
2858 break;
2859
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002860 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2861 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2862
2863 if (tmp->options & LOG_OPT_QUOTE)
2864 LOGCHAR('"');
2865
2866 end = uri + strlen(uri);
2867
2868 // look for the first whitespace character
2869 while (uri < end && !HTTP_IS_SPHT(*uri))
2870 uri++;
2871
2872 // keep advancing past multiple spaces
2873 while (uri < end && HTTP_IS_SPHT(*uri)) {
2874 uri++; nspaces++;
2875 }
2876
2877 // look for first space after url
2878 spc = uri;
2879 while (spc < end && !HTTP_IS_SPHT(*spc))
2880 spc++;
2881
2882 path.ptr = uri;
2883 path.len = spc - uri;
2884
2885 // extract relative path without query params from url
2886 path = iststop(http_get_path(path), '?');
2887 if (!txn || !txn->uri || nspaces == 0) {
2888 chunk.area = "<BADREQ>";
2889 chunk.data = strlen("<BADREQ>");
2890 } else {
2891 chunk.area = path.ptr;
2892 chunk.data = path.len;
2893 }
2894
2895 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2896 if (ret == NULL || *ret != '\0')
2897 goto out;
2898
2899 tmplog = ret;
2900 if (tmp->options & LOG_OPT_QUOTE)
2901 LOGCHAR('"');
2902
2903 last_isspace = 0;
2904 break;
2905
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002906 case LOG_FMT_HTTP_QUERY: // %HQ
2907 if (tmp->options & LOG_OPT_QUOTE)
2908 LOGCHAR('"');
2909
Willy Tarreau57bc8912016-04-25 17:09:40 +02002910 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002911 chunk.area = "<BADREQ>";
2912 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002913 } else {
2914 uri = txn->uri;
2915 end = uri + strlen(uri);
2916 // look for the first question mark
2917 while (uri < end && *uri != '?')
2918 uri++;
2919
2920 qmark = uri;
2921 // look for first space or question mark after url
2922 while (uri < end && !HTTP_IS_SPHT(*uri))
2923 uri++;
2924
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002925 chunk.area = qmark;
2926 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002927 }
2928
Dragan Dosen835b9212016-02-12 13:23:03 +01002929 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002930 if (ret == NULL || *ret != '\0')
2931 goto out;
2932
2933 tmplog = ret;
2934 if (tmp->options & LOG_OPT_QUOTE)
2935 LOGCHAR('"');
2936
2937 last_isspace = 0;
2938 break;
2939
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002940 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002941 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002942
Willy Tarreaub7636d12015-06-17 19:58:02 +02002943 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002944 LOGCHAR('"');
2945
2946 end = uri + strlen(uri);
2947 // look for the first whitespace character
2948 while (uri < end && !HTTP_IS_SPHT(*uri))
2949 uri++;
2950
2951 // keep advancing past multiple spaces
2952 while (uri < end && HTTP_IS_SPHT(*uri)) {
2953 uri++; nspaces++;
2954 }
2955
2956 // look for first space after url
2957 spc = uri;
2958 while (spc < end && !HTTP_IS_SPHT(*spc))
2959 spc++;
2960
Willy Tarreau57bc8912016-04-25 17:09:40 +02002961 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002962 chunk.area = "<BADREQ>";
2963 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002964 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002965 chunk.area = uri;
2966 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002967 }
2968
Dragan Dosen835b9212016-02-12 13:23:03 +01002969 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002970 if (ret == NULL || *ret != '\0')
2971 goto out;
2972
2973 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002974 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002975 LOGCHAR('"');
2976
2977 last_isspace = 0;
2978 break;
2979
2980 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002981 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002982 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002983 LOGCHAR('"');
2984
2985 end = uri + strlen(uri);
2986 // look for the first whitespace character
2987 spc = uri;
2988 while (spc < end && !HTTP_IS_SPHT(*spc))
2989 spc++;
2990
2991 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002992 chunk.area = "<BADREQ>";
2993 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002994 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002995 chunk.area = uri;
2996 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002997 }
2998
Dragan Dosen835b9212016-02-12 13:23:03 +01002999 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003000 if (ret == NULL || *ret != '\0')
3001 goto out;
3002
3003 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003004 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003005 LOGCHAR('"');
3006
3007 last_isspace = 0;
3008 break;
3009
3010 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02003011 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003012 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003013 LOGCHAR('"');
3014
3015 end = uri + strlen(uri);
3016 // look for the first whitespace character
3017 while (uri < end && !HTTP_IS_SPHT(*uri))
3018 uri++;
3019
3020 // keep advancing past multiple spaces
3021 while (uri < end && HTTP_IS_SPHT(*uri)) {
3022 uri++; nspaces++;
3023 }
3024
3025 // look for the next whitespace character
3026 while (uri < end && !HTTP_IS_SPHT(*uri))
3027 uri++;
3028
3029 // keep advancing past multiple spaces
3030 while (uri < end && HTTP_IS_SPHT(*uri))
3031 uri++;
3032
Willy Tarreau57bc8912016-04-25 17:09:40 +02003033 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003034 chunk.area = "<BADREQ>";
3035 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003036 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003037 chunk.area = "HTTP/0.9";
3038 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003039 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003040 chunk.area = uri;
3041 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003042 }
3043
Dragan Dosen835b9212016-02-12 13:23:03 +01003044 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003045 if (ret == NULL || *ret != '\0')
3046 goto out;
3047
3048 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003049 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003050 LOGCHAR('"');
3051
3052 last_isspace = 0;
3053 break;
3054
William Lallemand5f232402012-04-05 18:02:55 +02003055 case LOG_FMT_COUNTER: // %rt
3056 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003057 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02003058 if (iret < 0 || iret > dst + maxsize - tmplog)
3059 goto out;
3060 last_isspace = 0;
3061 tmplog += iret;
3062 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003063 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003064 if (ret == NULL)
3065 goto out;
3066 tmplog = ret;
3067 last_isspace = 0;
3068 }
3069 break;
3070
Willy Tarreau7346acb2014-08-28 15:03:15 +02003071 case LOG_FMT_LOGCNT: // %lc
3072 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003073 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003074 if (iret < 0 || iret > dst + maxsize - tmplog)
3075 goto out;
3076 last_isspace = 0;
3077 tmplog += iret;
3078 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003079 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003080 if (ret == NULL)
3081 goto out;
3082 tmplog = ret;
3083 last_isspace = 0;
3084 }
3085 break;
3086
William Lallemand5f232402012-04-05 18:02:55 +02003087 case LOG_FMT_HOSTNAME: // %H
3088 src = hostname;
3089 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3090 if (ret == NULL)
3091 goto out;
3092 tmplog = ret;
3093 last_isspace = 0;
3094 break;
3095
3096 case LOG_FMT_PID: // %pid
3097 if (tmp->options & LOG_OPT_HEXA) {
3098 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3099 if (iret < 0 || iret > dst + maxsize - tmplog)
3100 goto out;
3101 last_isspace = 0;
3102 tmplog += iret;
3103 } else {
3104 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3105 if (ret == NULL)
3106 goto out;
3107 tmplog = ret;
3108 last_isspace = 0;
3109 }
3110 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003111
3112 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003113 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003114 if (s)
3115 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3116 else
3117 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003118 if (ret == NULL)
3119 goto out;
3120 tmplog = ret;
3121 last_isspace = 0;
3122 break;
3123
William Lallemandbddd4fd2012-02-27 11:23:10 +01003124 }
3125 }
3126
3127out:
William Lallemand1d705562012-03-12 12:46:41 +01003128 /* *tmplog is a unused character */
3129 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003130 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003131
Willy Tarreaubaaee002006-06-26 02:48:02 +02003132}
3133
William Lallemand1d705562012-03-12 12:46:41 +01003134/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003135 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003136 * Will not log if the frontend has no log defined.
3137 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003138void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003139{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003140 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003141 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003142 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003143
3144 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003145 err = (s->flags & SF_REDISP) ||
3146 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3147 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003148 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003149 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003150
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003151 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003152 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003153
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003154 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003155 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003156
Willy Tarreauabcd5142013-06-11 17:18:02 +02003157 if (s->logs.level) { /* loglevel was overridden */
3158 if (s->logs.level == -1) {
3159 s->logs.logwait = 0; /* logs disabled */
3160 return;
3161 }
3162 level = s->logs.level - 1;
3163 }
3164 else {
3165 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003166 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003167 level = LOG_ERR;
3168 }
William Lallemand1d705562012-03-12 12:46:41 +01003169
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003170 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003171 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003172 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003173 }
3174
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003175 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3176 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3177 &sess->fe->logformat_sd);
3178 }
3179
Dragan Dosen59cee972015-09-19 22:09:02 +02003180 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003181 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003182 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003183 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3184 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003185 s->logs.logwait = 0;
3186 }
3187}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003188
Willy Tarreau53839352018-09-05 19:51:10 +02003189/*
3190 * send a minimalist log for the session. Will not log if the frontend has no
3191 * log defined. It is assumed that this is only used to report anomalies that
3192 * cannot lead to the creation of a regular stream. Because of this the log
3193 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3194 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003195 * function to report unimportant events. It is safe to call this function with
3196 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003197 */
3198void sess_log(struct session *sess)
3199{
3200 int size, level;
3201 int sd_size = 0;
3202
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003203 if (!sess)
3204 return;
3205
Willy Tarreau53839352018-09-05 19:51:10 +02003206 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3207 return;
3208
3209 level = LOG_INFO;
3210 if (sess->fe->options2 & PR_O2_LOGERRORS)
3211 level = LOG_ERR;
3212
3213 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3214 sd_size = sess_build_logline(sess, NULL,
3215 logline_rfc5424, global.max_syslog_len,
3216 &sess->fe->logformat_sd);
3217 }
3218
3219 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3220 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003221 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003222 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3223 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003224 }
3225}
3226
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003227void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3228{
3229 va_list argp;
3230 int data_len;
3231
3232 if (level < 0 || format == NULL || logline == NULL)
3233 return;
3234
3235 va_start(argp, format);
3236 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3237 if (data_len < 0 || data_len > global.max_syslog_len)
3238 data_len = global.max_syslog_len;
3239 va_end(argp);
3240
3241 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3242}
Emeric Brun54932b42020-07-07 09:43:24 +02003243/*
3244 * This function parse a received log message <buf>, of size <buflen>
3245 * it fills <level>, <facility> and <metadata> depending of the detected
3246 * header format and message will point on remaining payload of <size>
3247 *
3248 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3249 * struct ist len will be set to 0 if field is not found
3250 * <level> and <facility> will be set to -1 if not found.
3251 */
3252void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3253 struct ist *metadata, char **message, size_t *size)
3254{
3255
3256 char *p;
3257 int fac_level = 0;
3258
3259 *level = *facility = -1;
3260
3261 *message = buf;
3262 *size = buflen;
3263
3264 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3265
3266 p = buf;
3267 if (*size < 2 || *p != '<')
3268 return;
3269
3270 p++;
3271 while (*p != '>') {
3272 if (*p > '9' || *p < '0')
3273 return;
3274 fac_level = 10*fac_level + (*p - '0');
3275 p++;
3276 if ((p - buf) > buflen)
3277 return;
3278 }
3279
3280 *facility = fac_level >> 3;
3281 *level = fac_level & 0x7;
3282 p++;
3283
3284 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3285
3286 buflen -= p - buf;
3287 buf = p;
3288
3289 *size = buflen;
3290 *message = buf;
3291
3292 /* for rfc5424, prio is always followed by '1' and ' ' */
3293 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3294 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3295 * followed by message.
3296 * Each header field can present NILVALUE: '-'
3297 */
3298
3299 p += 2;
3300 /* timestamp is NILVALUE '-' */
3301 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3302 metadata[LOG_META_TIME] = ist2(p, 1);
3303 p++;
3304 }
3305 else if (*size > LOG_ISOTIME_MINLEN) {
3306 metadata[LOG_META_TIME].ptr = p;
3307
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003308 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003309 * in timestamp.
3310 * possible format are:
3311 * ex: '1970-01-01T00:00:00.000000Z'
3312 * '1970-01-01T00:00:00.000000+00:00'
3313 * '1970-01-01T00:00:00.000000-00:00'
3314 * '1970-01-01T00:00:00Z'
3315 * '1970-01-01T00:00:00+00:00'
3316 * '1970-01-01T00:00:00-00:00'
3317 */
3318 p += 19;
3319 if (*p == '.') {
3320 p++;
3321 if ((p - buf) >= buflen)
3322 goto bad_format;
3323 while (*p != 'Z' && *p != '+' && *p != '-') {
3324 if ((unsigned char)(*p - '0') > 9)
3325 goto bad_format;
3326
3327 p++;
3328 if ((p - buf) >= buflen)
3329 goto bad_format;
3330 }
3331 }
3332
3333 if (*p == 'Z')
3334 p++;
3335 else
3336 p += 6; /* case of '+00:00 or '-00:00' */
3337
3338 if ((p - buf) >= buflen || *p != ' ')
3339 goto bad_format;
3340 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3341 }
3342 else
3343 goto bad_format;
3344
3345
3346 p++;
3347 if ((p - buf) >= buflen || *p == ' ')
3348 goto bad_format;
3349
3350 metadata[LOG_META_HOST].ptr = p;
3351 while (*p != ' ') {
3352 p++;
3353 if ((p - buf) >= buflen)
3354 goto bad_format;
3355 }
3356 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3357 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3358 metadata[LOG_META_HOST].len = 0;
3359
3360 p++;
3361 if ((p - buf) >= buflen || *p == ' ')
3362 goto bad_format;
3363
3364 metadata[LOG_META_TAG].ptr = p;
3365 while (*p != ' ') {
3366 p++;
3367 if ((p - buf) >= buflen)
3368 goto bad_format;
3369 }
3370 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3371 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3372 metadata[LOG_META_TAG].len = 0;
3373
3374 p++;
3375 if ((p - buf) >= buflen || *p == ' ')
3376 goto bad_format;
3377
3378 metadata[LOG_META_PID].ptr = p;
3379 while (*p != ' ') {
3380 p++;
3381 if ((p - buf) >= buflen)
3382 goto bad_format;
3383 }
3384 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3385 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3386 metadata[LOG_META_PID].len = 0;
3387
3388 p++;
3389 if ((p - buf) >= buflen || *p == ' ')
3390 goto bad_format;
3391
3392 metadata[LOG_META_MSGID].ptr = p;
3393 while (*p != ' ') {
3394 p++;
3395 if ((p - buf) >= buflen)
3396 goto bad_format;
3397 }
3398 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3399 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3400 metadata[LOG_META_MSGID].len = 0;
3401
3402 p++;
3403 if ((p - buf) >= buflen || *p == ' ')
3404 goto bad_format;
3405
3406 /* structured data format is:
3407 * ex:
3408 * '[key1=value1 key2=value2][key3=value3]'
3409 *
3410 * space is invalid outside [] because
3411 * considered as the end of structured data field
3412 */
3413 metadata[LOG_META_STDATA].ptr = p;
3414 if (*p == '[') {
3415 int elem = 0;
3416
3417 while (1) {
3418 if (elem) {
3419 /* according to rfc this char is escaped in param values */
3420 if (*p == ']' && *(p-1) != '\\')
3421 elem = 0;
3422 }
3423 else {
3424 if (*p == '[')
3425 elem = 1;
3426 else if (*p == ' ')
3427 break;
3428 else
3429 goto bad_format;
3430 }
3431 p++;
3432 if ((p - buf) >= buflen)
3433 goto bad_format;
3434 }
3435 }
3436 else if (*p == '-') {
3437 /* case of NILVALUE */
3438 p++;
3439 if ((p - buf) >= buflen || *p != ' ')
3440 goto bad_format;
3441 }
3442 else
3443 goto bad_format;
3444
3445 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3446 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3447 metadata[LOG_META_STDATA].len = 0;
3448
3449 p++;
3450
3451 buflen -= p - buf;
3452 buf = p;
3453
3454 *size = buflen;
3455 *message = p;
3456 }
3457 else if (*size > LOG_LEGACYTIME_LEN) {
3458 int m;
3459
3460 /* supported header format according to rfc3164.
3461 * ex:
3462 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3463 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3464 * or 'Jan 1 00:00:00 HOSTNAME '
3465 * Note: HOSTNAME is mandatory, and day
3466 * of month uses a single space prefix if
3467 * less than 10 to ensure hour offset is
3468 * always the same.
3469 */
3470
3471 /* Check month to see if it correspond to a rfc3164
3472 * header ex 'Jan 1 00:00:00' */
3473 for (m = 0; m < 12; m++)
3474 if (!memcmp(monthname[m], p, 3))
3475 break;
3476 /* Month not found */
3477 if (m == 12)
3478 goto bad_format;
3479
3480 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3481
3482 p += LOG_LEGACYTIME_LEN;
3483 if ((p - buf) >= buflen || *p != ' ')
3484 goto bad_format;
3485
3486 p++;
3487 if ((p - buf) >= buflen || *p == ' ')
3488 goto bad_format;
3489
3490 metadata[LOG_META_HOST].ptr = p;
3491 while (*p != ' ') {
3492 p++;
3493 if ((p - buf) >= buflen)
3494 goto bad_format;
3495 }
3496 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3497
3498 /* TAG seems to no be mandatory */
3499 p++;
3500
3501 buflen -= p - buf;
3502 buf = p;
3503
3504 *size = buflen;
3505 *message = buf;
3506
3507 if (!buflen)
3508 return;
3509
3510 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3511 p++;
3512
3513 /* a tag must present a trailing ':' */
3514 if (((p - buf) >= buflen) || *p != ':')
3515 return;
3516 p++;
3517 /* followed by a space */
3518 if (((p - buf) >= buflen) || *p != ' ')
3519 return;
3520
3521 /* rewind to parse tag and pid */
3522 p = buf;
3523 metadata[LOG_META_TAG].ptr = p;
3524 /* we have the guarantee that ':' will be reach before size limit */
3525 while (*p != ':') {
3526 if (*p == '[') {
3527 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3528 metadata[LOG_META_PID].ptr = p + 1;
3529 }
3530 else if (*p == ']' && metadata[LOG_META_PID].ptr) {
3531 if (p[1] != ':')
3532 return;
3533 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3534 }
3535 p++;
3536 }
3537 if (!metadata[LOG_META_TAG].len)
3538 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3539
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003540 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003541 p += 2;
3542
3543 buflen -= p - buf;
3544 buf = p;
3545
3546 *size = buflen;
3547 *message = buf;
3548 }
3549
3550 return;
3551
3552bad_format:
3553 /* bad syslog format, we reset all parsed syslog fields
3554 * but priority is kept because we are able to re-build
3555 * this message using LOF_FORMAT_PRIO.
3556 */
3557 metadata[LOG_META_TIME].len = 0;
3558 metadata[LOG_META_HOST].len = 0;
3559 metadata[LOG_META_TAG].len = 0;
3560 metadata[LOG_META_PID].len = 0;
3561 metadata[LOG_META_MSGID].len = 0;
3562 metadata[LOG_META_STDATA].len = 0;
3563
3564 return;
3565}
3566
3567/*
3568 * UDP syslog fd handler
3569 */
3570void syslog_fd_handler(int fd)
3571{
3572 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3573 ssize_t ret = 0;
3574 struct buffer *buf = get_trash_chunk();
3575 size_t size;
3576 char *message;
3577 int level;
3578 int facility;
3579 struct listener *l = objt_listener(fdtab[fd].owner);
3580 int max_accept;
3581
3582 if(!l)
3583 ABORT_NOW();
3584
3585 if (fdtab[fd].ev & FD_POLL_IN) {
3586
3587 if (!fd_recv_ready(fd))
3588 return;
3589
3590 max_accept = l->maxaccept ? l->maxaccept : 1;
3591
3592 do {
3593 /* Source address */
3594 struct sockaddr_storage saddr = {0};
3595 socklen_t saddrlen;
3596
3597 saddrlen = sizeof(saddr);
3598
3599 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3600 if (ret < 0) {
3601 if (errno == EINTR)
3602 continue;
3603 if (errno == EAGAIN)
3604 fd_cant_recv(fd);
3605 goto out;
3606 }
3607 buf->data = ret;
3608
Emeric Brun45c457a2020-07-09 23:23:34 +02003609 /* update counters */
3610 _HA_ATOMIC_ADD(&cum_log_messages, 1);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003611 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003612
Emeric Brun54932b42020-07-07 09:43:24 +02003613 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3614
3615 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3616
3617 } while (--max_accept);
3618 }
3619
3620out:
3621 return;
3622}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003623
Emeric Brun12941c82020-07-07 14:19:42 +02003624/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003625 * IO Handler to handle message exchange with a syslog tcp client
3626 */
3627static void syslog_io_handler(struct appctx *appctx)
3628{
3629 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3630 struct stream_interface *si = appctx->owner;
3631 struct stream *s = si_strm(si);
3632 struct proxy *frontend = strm_fe(s);
3633 struct listener *l = strm_li(s);
3634 struct buffer *buf = get_trash_chunk();
3635 int max_accept;
3636 int to_skip;
3637 int facility;
3638 int level;
3639 char *message;
3640 size_t size;
3641
3642 max_accept = l->maxaccept ? l->maxaccept : 1;
3643 while (co_data(si_oc(si))) {
3644 char c;
3645
3646 if (max_accept <= 0)
3647 goto missing_budget;
3648 max_accept--;
3649
3650 to_skip = co_getchar(si_oc(si), &c);
3651 if (!to_skip)
3652 goto missing_data;
3653 else if (to_skip < 0)
3654 goto cli_abort;
3655
3656 if (c == '<') {
3657 /* rfc-6587, Non-Transparent-Framing: messages separated by
3658 * a trailing LF or CR LF
3659 */
3660 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3661 if (!to_skip)
3662 goto missing_data;
3663 else if (to_skip < 0)
3664 goto cli_abort;
3665
3666 if (buf->area[to_skip - 1] != '\n')
3667 goto parse_error;
3668
3669 buf->data = to_skip - 1;
3670
3671 /* according to rfc-6587, some devices adds CR before LF */
3672 if (buf->data && buf->area[buf->data - 1] == '\r')
3673 buf->data--;
3674
3675 }
3676 else if ((unsigned char)(c - '1') <= 8) {
3677 /* rfc-6587, Octet-Counting: message length in ASCII
3678 * (first digit can not be ZERO), followed by a space
3679 * and message length
3680 */
3681 char *p = NULL;
3682 int msglen;
3683
3684 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3685 if (!to_skip)
3686 goto missing_data;
3687 else if (to_skip < 0)
3688 goto cli_abort;
3689
3690 if (buf->area[to_skip - 1] != ' ')
3691 goto parse_error;
3692
3693 msglen = strtol(trash.area, &p, 10);
3694 if (!msglen || p != &buf->area[to_skip - 1])
3695 goto parse_error;
3696
3697 /* message seems too large */
3698 if (msglen > buf->size)
3699 goto parse_error;
3700
3701 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3702 if (!msglen)
3703 goto missing_data;
3704 else if (msglen < 0)
3705 goto cli_abort;
3706
3707
3708 buf->data = msglen;
3709 to_skip += msglen;
3710 }
3711 else
3712 goto parse_error;
3713
3714 co_skip(si_oc(si), to_skip);
3715
3716 /* update counters */
3717 _HA_ATOMIC_ADD(&cum_log_messages, 1);
3718 proxy_inc_fe_req_ctr(l, frontend);
3719
3720 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3721
3722 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3723
3724 }
3725
3726missing_data:
3727 /* we need more data to read */
3728 si_oc(si)->flags |= CF_READ_DONTWAIT;
3729
3730 return;
3731
3732missing_budget:
3733 /* it may remain some stuff to do, let's retry later */
3734 appctx_wakeup(appctx);
3735
3736 return;
3737
3738parse_error:
3739 if (l->counters)
3740 _HA_ATOMIC_ADD(&l->counters->failed_req, 1);
3741 _HA_ATOMIC_ADD(&frontend->fe_counters.failed_req, 1);
3742
3743 goto close;
3744
3745cli_abort:
3746 if (l->counters)
3747 _HA_ATOMIC_ADD(&l->counters->cli_aborts, 1);
3748 _HA_ATOMIC_ADD(&frontend->fe_counters.cli_aborts, 1);
3749
3750close:
3751 si_shutw(si);
3752 si_shutr(si);
3753
3754 si_ic(si)->flags |= CF_READ_NULL;
3755
3756 return;
3757}
3758
3759static struct applet syslog_applet = {
3760 .obj_type = OBJ_TYPE_APPLET,
3761 .name = "<SYSLOG>", /* used for logging */
3762 .fct = syslog_io_handler,
3763 .release = NULL,
3764};
3765
3766/*
Emeric Brun12941c82020-07-07 14:19:42 +02003767 * Parse "log-forward" section and create corresponding sink buffer.
3768 *
3769 * The function returns 0 in success case, otherwise, it returns error
3770 * flags.
3771 */
3772int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3773{
3774 int err_code = 0;
3775 struct proxy *px;
3776 char *errmsg = NULL;
3777 const char *err = NULL;
3778
3779 if (strcmp(args[0], "log-forward") == 0) {
3780 if (!*args[1]) {
3781 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3782 err_code |= ERR_ALERT | ERR_ABORT;
3783 goto out;
3784 }
3785
3786 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3787 goto out;
3788
3789 err = invalid_char(args[1]);
3790 if (err) {
3791 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3792 file, linenum, *err, args[0], args[1]);
3793 err_code |= ERR_ALERT | ERR_ABORT;
3794 goto out;
3795 }
3796
Emeric Brunb0c331f2020-10-07 17:05:59 +02003797 px = log_forward_by_name(args[1]);
3798 if (px) {
3799 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3800 file, linenum, args[1], px->conf.file, px->conf.line);
3801 err_code |= ERR_ALERT | ERR_FATAL;
3802 }
3803
3804 px = proxy_find_by_name(args[1], 0, 0);
3805 if (px) {
3806 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3807 file, linenum, args[1], proxy_type_str(px),
3808 px->id, px->conf.file, px->conf.line);
3809 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003810 }
3811
3812 px = calloc(1, sizeof *px);
3813 if (!px) {
3814 err_code |= ERR_ALERT | ERR_FATAL;
3815 goto out;
3816 }
3817
3818 px->next = cfg_log_forward;
3819 cfg_log_forward = px;
3820
3821 init_new_proxy(px);
3822 px->conf.file = strdup(file);
3823 px->conf.line = linenum;
3824 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003825 px->last_change = now.tv_sec;
3826 px->cap = PR_CAP_FE;
3827 px->maxconn = 10;
3828 px->timeout.client = TICK_ETERNITY;
3829 px->accept = frontend_accept;
3830 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003831 px->id = strdup(args[1]);
3832
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003833 }
3834 else if (!strcmp(args[0], "maxconn")) { /* maxconn */
3835 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3836 err_code |= ERR_WARN;
3837
3838 if (*(args[1]) == 0) {
3839 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3840 err_code |= ERR_ALERT | ERR_FATAL;
3841 goto out;
3842 }
3843 cfg_log_forward->maxconn = atol(args[1]);
3844 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3845 goto out;
3846 }
3847 else if (!strcmp(args[0], "backlog")) { /* backlog */
3848 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3849 err_code |= ERR_WARN;
3850
3851 if (*(args[1]) == 0) {
3852 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3853 err_code |= ERR_ALERT | ERR_FATAL;
3854 goto out;
3855 }
3856 cfg_log_forward->backlog = atol(args[1]);
3857 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3858 goto out;
3859 }
3860 else if (strcmp(args[0], "bind") == 0) {
3861 int cur_arg;
3862 static int kws_dumped;
3863 struct bind_conf *bind_conf;
3864 struct bind_kw *kw;
3865 struct listener *l;
3866
3867 cur_arg = 1;
3868
3869 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3870 NULL, xprt_get(XPRT_RAW));
3871 if (!bind_conf) {
3872 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3873 err_code |= ERR_ALERT | ERR_FATAL;
3874 goto out;
3875 }
3876
3877 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3878 if (errmsg && *errmsg) {
3879 indent_msg(&errmsg, 2);
3880 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3881 }
3882 else {
3883 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3884 file, linenum, args[0], args[1], args[2]);
3885 err_code |= ERR_ALERT | ERR_FATAL;
3886 goto out;
3887 }
3888 }
3889 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
3890 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : 64;
3891 l->accept = session_accept_fd;
3892 l->analysers |= cfg_log_forward->fe_req_ana;
3893 l->default_target = cfg_log_forward->default_target;
3894 global.maxsock++;
3895 }
3896 cur_arg++;
3897
3898 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3899 int ret;
3900
3901 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3902 err_code |= ret;
3903 if (ret) {
3904 if (errmsg && *errmsg) {
3905 indent_msg(&errmsg, 2);
3906 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3907 }
3908 else
3909 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3910 file, linenum, args[cur_arg]);
3911 if (ret & ERR_FATAL)
3912 goto out;
3913 }
3914 cur_arg += 1 + kw->skip;
3915 }
3916 if (*args[cur_arg] != 0) {
3917 char *kws = NULL;
3918
3919 if (!kws_dumped) {
3920 kws_dumped = 1;
3921 bind_dump_kws(&kws);
3922 indent_msg(&kws, 4);
3923 }
3924 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.%s%s\n",
3925 file, linenum, args[cur_arg], cursection,
3926 kws ? " Registered keywords :" : "", kws ? kws: "");
3927 free(kws);
3928 err_code |= ERR_ALERT | ERR_FATAL;
3929 goto out;
3930 }
Emeric Brun12941c82020-07-07 14:19:42 +02003931 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003932 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003933 int cur_arg;
3934 static int kws_dumped;
3935 struct bind_conf *bind_conf;
3936 struct bind_kw *kw;
3937 struct listener *l;
3938
3939 cur_arg = 1;
3940
3941 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3942 NULL, xprt_get(XPRT_RAW));
3943
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003944 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003945 if (errmsg && *errmsg) {
3946 indent_msg(&errmsg, 2);
3947 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3948 }
3949 else {
3950 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3951 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003952 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003953 err_code |= ERR_ALERT | ERR_FATAL;
3954 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003955 }
3956 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003957 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Emeric Brun12941c82020-07-07 14:19:42 +02003958 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : 64;
Willy Tarreaue140a692020-10-15 21:25:32 +02003959 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003960 global.maxsock++;
3961 }
3962 cur_arg++;
3963
3964 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3965 int ret;
3966
3967 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3968 err_code |= ret;
3969 if (ret) {
3970 if (errmsg && *errmsg) {
3971 indent_msg(&errmsg, 2);
3972 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3973 }
3974 else
3975 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3976 file, linenum, args[cur_arg]);
3977 if (ret & ERR_FATAL)
3978 goto out;
3979 }
3980 cur_arg += 1 + kw->skip;
3981 }
3982 if (*args[cur_arg] != 0) {
3983 char *kws = NULL;
3984
3985 if (!kws_dumped) {
3986 kws_dumped = 1;
3987 bind_dump_kws(&kws);
3988 indent_msg(&kws, 4);
3989 }
3990 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.%s%s\n",
3991 file, linenum, args[cur_arg], cursection,
3992 kws ? " Registered keywords :" : "", kws ? kws: "");
3993 free(kws);
3994 err_code |= ERR_ALERT | ERR_FATAL;
3995 goto out;
3996 }
3997 }
3998 else if (strcmp(args[0], "log") == 0) {
3999 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), &errmsg)) {
4000 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4001 err_code |= ERR_ALERT | ERR_FATAL;
4002 goto out;
4003 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004004 }
4005 else if (strcmp(args[0], "timeout") == 0) {
4006 const char *res;
4007 unsigned timeout;
4008
4009 if (strcmp(args[1], "client") != 0) {
4010 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
4011 err_code |= ERR_ALERT | ERR_FATAL;
4012 goto out;
4013 }
4014
4015 if (*args[2] == 0) {
4016 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
4017 err_code |= ERR_ALERT | ERR_FATAL;
4018 goto out;
4019 }
4020 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
4021 if (res == PARSE_TIME_OVER) {
4022 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
4023 }
4024 else if (res == PARSE_TIME_UNDER) {
4025 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
4026 }
4027 else if (res) {
4028 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
4029 return -1;
4030 }
4031
4032 if (res) {
4033 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4034 err_code |= ERR_ALERT | ERR_FATAL;
4035 goto out;
4036 }
4037 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02004038 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02004039 else {
4040 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
4041 err_code |= ERR_ALERT | ERR_ABORT;
4042 goto out;
4043 }
Emeric Brun12941c82020-07-07 14:19:42 +02004044out:
4045 return err_code;
4046}
4047
4048
Willy Tarreau869efd52019-11-15 15:16:57 +01004049/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
4050static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004051{
Willy Tarreau869efd52019-11-15 15:16:57 +01004052 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4053 return 1;
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004054
Willy Tarreau869efd52019-11-15 15:16:57 +01004055 if (!startup_logs)
4056 return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
4057
4058 return ring_attach_cli(startup_logs, appctx);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004059}
4060
4061/* register cli keywords */
4062static struct cli_kw_list cli_kws = {{ },{
4063 { { "show", "startup-logs", NULL },
Willy Tarreau869efd52019-11-15 15:16:57 +01004064 "show startup-logs : report logs emitted during HAProxy startup", cli_parse_show_startup_logs, NULL, NULL },
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004065 {{},}
4066}};
4067
Willy Tarreau0108d902018-11-25 19:14:37 +01004068INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4069
Emeric Brun12941c82020-07-07 14:19:42 +02004070/* config parsers for this section */
4071REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4072
Willy Tarreau082b6282019-05-22 14:42:12 +02004073REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4074REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004075
Willy Tarreaubaaee002006-06-26 02:48:02 +02004076/*
4077 * Local variables:
4078 * c-indent-level: 8
4079 * c-basic-offset: 8
4080 * End:
4081 */