blob: 41810f574ed472b0dc5ae30aa4729570daa3c714 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
Willy Tarreau077edcb2016-08-10 18:30:56 +020025#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/applet-t.h>
Emeric Brun12941c82020-07-07 14:19:42 +020029#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020031#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020033#include <haproxy/http.h>
Emeric Brun12941c82020-07-07 14:19:42 +020034#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020035#include <haproxy/log.h>
Emeric Brun12941c82020-07-07 14:19:42 +020036#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/sample.h>
38#include <haproxy/sink.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020039#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020040#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020041#include <haproxy/stream_interface.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020042#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020043#include <haproxy/tools.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Emeric Brun45c457a2020-07-09 23:23:34 +020045/* global recv logs counter */
46int cum_log_messages;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Emeric Brun12941c82020-07-07 14:19:42 +020048/* log forward proxy list */
49struct proxy *cfg_log_forward;
50
Emeric Brun54648852020-07-06 15:54:06 +020051struct log_fmt_st {
Dragan Dosen43885c72015-10-01 13:18:13 +020052 char *name;
Dragan Dosen43885c72015-10-01 13:18:13 +020053};
54
Emeric Brun54648852020-07-06 15:54:06 +020055static const struct log_fmt_st log_formats[LOG_FORMATS] = {
Emeric Brun0237c4e2020-11-27 16:24:34 +010056 [LOG_FORMAT_LOCAL] = {
57 .name = "local",
58 },
Dragan Dosen43885c72015-10-01 13:18:13 +020059 [LOG_FORMAT_RFC3164] = {
60 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020061 },
62 [LOG_FORMAT_RFC5424] = {
63 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020064 },
65 [LOG_FORMAT_PRIO] = {
66 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010067 },
68 [LOG_FORMAT_SHORT] = {
69 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020070 },
71 [LOG_FORMAT_TIMED] = {
72 .name = "timed",
73 },
74 [LOG_FORMAT_ISO] = {
75 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010076 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010077 [LOG_FORMAT_RAW] = {
78 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010079 },
Dragan Dosen1322d092015-09-22 16:05:32 +020080};
81
Dragan Dosen835b9212016-02-12 13:23:03 +010082/*
83 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020084 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
85 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
86 * that the byte should be escaped. Be careful to always pass bytes from 0 to
87 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010088 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020089long rfc5424_escape_map[(256/8) / sizeof(long)];
90long hdr_encode_map[(256/8) / sizeof(long)];
91long url_encode_map[(256/8) / sizeof(long)];
92long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010093
Dragan Dosen835b9212016-02-12 13:23:03 +010094
Willy Tarreaubaaee002006-06-26 02:48:02 +020095const char *log_facilities[NB_LOG_FACILITIES] = {
96 "kern", "user", "mail", "daemon",
97 "auth", "syslog", "lpr", "news",
98 "uucp", "cron", "auth2", "ftp",
99 "ntp", "audit", "alert", "cron2",
100 "local0", "local1", "local2", "local3",
101 "local4", "local5", "local6", "local7"
102};
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104const char *log_levels[NB_LOG_LEVELS] = {
105 "emerg", "alert", "crit", "err",
106 "warning", "notice", "info", "debug"
107};
108
Willy Tarreau570f2212013-06-10 16:42:09 +0200109const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200110const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111
William Lallemand723b73a2012-02-08 16:37:49 +0100112
113/* log_format */
114struct logformat_type {
115 char *name;
116 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100117 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200118 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100119 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
William Lallemand723b73a2012-02-08 16:37:49 +0100120};
121
William Lallemandb7ff6a32012-03-02 14:35:21 +0100122int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
123
William Lallemand723b73a2012-02-08 16:37:49 +0100124/* log_format variable names */
125static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200126 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100127
128 /* please keep these lines sorted ! */
129 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
130 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
131 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
132 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100133 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200134 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200135 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200136 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100137 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200138 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
139 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
140 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
141 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
142 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
143 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200144 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100145 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200146 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000147 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100148 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
149 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200150 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100151 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200152 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100153 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
154 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200155 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200156 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
157 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100158 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
159 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200160 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
161 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100162 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200163 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
164 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
165 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
166 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000167 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
Maciej Zdebfcdfd852020-11-30 18:27:47 +0000168 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP relative or absolute path */
169 { "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 +0000170 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000171 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
172 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200173 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100174 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200175 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100176 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
177 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100178 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100179 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
180 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
181 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
182 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
183 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200184 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
185 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100186 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200187 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
188 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
189 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100190 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
191 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
William Lallemand5e19a282012-04-02 16:22:10 +0200192 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100193};
194
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200195char 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
196char 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 +0100197char 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 +0100198char *log_format = NULL;
199
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200200/* Default string used for structured-data part in RFC5424 formatted
201 * syslog messages.
202 */
203char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200204
Willy Tarreau13ef7732018-11-12 07:25:28 +0100205/* total number of dropped logs */
206unsigned int dropped_logs = 0;
207
Dragan Dosen59cee972015-09-19 22:09:02 +0200208/* This is a global syslog message buffer, common to all outgoing
209 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100210 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200211THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100212
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200213/* A global syslog message buffer, common to all RFC5424 syslog messages.
214 * Currently, it is used for generating the structured-data part.
215 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200216THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200217
William Lallemand723b73a2012-02-08 16:37:49 +0100218struct logformat_var_args {
219 char *name;
220 int mask;
221};
222
223struct logformat_var_args var_args_list[] = {
224// global
225 { "M", LOG_OPT_MANDATORY },
226 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200227 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100228 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100229 { 0, 0 }
230};
231
232/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100233 * callback used to configure addr source retrieval
234 */
235int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
236{
237 curproxy->options2 |= PR_O2_SRC_ADDR;
238
239 return 0;
240}
241
242
243/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100244 * Parse args in a logformat_var. Returns 0 in error
245 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100246 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100247int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100248{
249 int i = 0;
250 int end = 0;
251 int flags = 0; // 1 = + 2 = -
252 char *sp = NULL; // start pointer
253
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100254 if (args == NULL) {
255 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100256 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100257 }
William Lallemand723b73a2012-02-08 16:37:49 +0100258
259 while (1) {
260 if (*args == '\0')
261 end = 1;
262
263 if (*args == '+') {
264 // add flag
265 sp = args + 1;
266 flags = 1;
267 }
268 if (*args == '-') {
269 // delete flag
270 sp = args + 1;
271 flags = 2;
272 }
273
274 if (*args == '\0' || *args == ',') {
275 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100276 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100277 if (strcmp(sp, var_args_list[i].name) == 0) {
278 if (flags == 1) {
279 node->options |= var_args_list[i].mask;
280 break;
281 } else if (flags == 2) {
282 node->options &= ~var_args_list[i].mask;
283 break;
284 }
285 }
286 }
287 sp = NULL;
288 if (end)
289 break;
290 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100291 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100292 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100293 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100294}
295
296/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100297 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
298 * must pass the args part in the <arg> pointer with its length in <arg_len>,
299 * and varname with its length in <var> and <var_len> respectively. <arg> is
300 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100301 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100302 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100303int 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 +0100304{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100305 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200306 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100307
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100308 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
309 if (strlen(logformat_keywords[j].name) == var_len &&
310 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
311 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200312 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100313 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100314 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200315 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100316 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100317 node->type = logformat_keywords[j].type;
318 node->options = *defoptions;
319 if (arg_len) {
320 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100321 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200322 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100323 }
324 if (node->type == LOG_FMT_GLOBAL) {
325 *defoptions = node->options;
326 free(node->arg);
327 free(node);
328 } else {
329 if (logformat_keywords[j].config_callback &&
330 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200331 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100332 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100333 curproxy->to_log |= logformat_keywords[j].lw;
Willy Tarreau2b718102021-04-21 07:32:39 +0200334 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100335 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100336 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100337 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100338 memprintf(err, "format variable '%s' is reserved for HTTP mode",
339 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200340 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100341 }
William Lallemand723b73a2012-02-08 16:37:49 +0100342 }
343 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100344
345 j = var[var_len];
346 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100347 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 +0100348 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200349
350 error_free:
351 if (node) {
352 free(node->arg);
353 free(node);
354 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100355 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100356}
357
358/*
359 * push to the logformat linked list
360 *
361 * start: start pointer
362 * end: end text pointer
363 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100364 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100365 *
366 * LOG_TEXT: copy chars from start to end excluding end.
367 *
368*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100369int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100370{
371 char *str;
372
Willy Tarreaua3571662012-12-20 21:59:12 +0100373 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200374 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100375 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100376 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100377 return 0;
378 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200379 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100380 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100381 str[end - start] = '\0';
382 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100383 node->type = LOG_FMT_TEXT; // type string
Willy Tarreau2b718102021-04-21 07:32:39 +0200384 LIST_APPEND(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100385 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200386 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100387 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100388 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100389 return 0;
390 }
William Lallemand1d705562012-03-12 12:46:41 +0100391 node->type = LOG_FMT_SEPARATOR;
Willy Tarreau2b718102021-04-21 07:32:39 +0200392 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100393 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100394 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100395}
396
397/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100398 * Parse the sample fetch expression <text> and add a node to <list_format> upon
399 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100400 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
401 * is passed in <endptr>, it will be updated with the pointer to the first character
402 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100403 *
404 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100405 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100406int 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 +0100407{
408 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200409 struct sample_expr *expr = NULL;
410 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100411 int cmd_arg;
412
413 cmd[0] = text;
414 cmd[1] = "";
415 cmd_arg = 0;
416
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100417 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 +0100418 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100419 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200420 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100421 }
422
Vincent Bernat02779b62016-04-03 13:48:43 +0200423 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100424 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100425 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200426 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100427 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100428 node->type = LOG_FMT_EXPR;
429 node->expr = expr;
430 node->options = options;
431
432 if (arg_len) {
433 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100434 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200435 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100436 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100437 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100438 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
439
Willy Tarreau434c57c2013-01-08 01:10:24 +0100440 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100441 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
442
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100443 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100444 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
445 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200446 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100447 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100448
Christopher Faulet581db2b2021-03-26 10:02:46 +0100449 if ((options & LOG_OPT_HTTP) && (expr->fetch->use & (SMP_USE_L6REQ|SMP_USE_L6RES))) {
450 ha_warning("parsing [%s:%d] : L6 sample fetch <%s> ignored in HTTP log-format string.\n",
451 curpx->conf.args.file, curpx->conf.args.line, text);
452 }
453
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200454 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100455 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100456 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100457
William Lallemand65ad6e12014-01-31 15:08:02 +0100458 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
459 * needed with some sample fetches (eg: ssl*). We always set it for
460 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100461 */
462 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200463 if (curpx->http_needed)
464 curpx->to_log |= LW_REQ;
Willy Tarreau2b718102021-04-21 07:32:39 +0200465 LIST_APPEND(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100466 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200467
468 error_free:
469 release_sample_expr(expr);
470 if (node) {
471 free(node->arg);
472 free(node);
473 }
474 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100475}
476
477/*
William Lallemand723b73a2012-02-08 16:37:49 +0100478 * Parse the log_format string and fill a linked list.
479 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200480 * You can set arguments using { } : %{many arguments}varname.
481 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100482 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500483 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100484 * curproxy: the proxy affected
485 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100486 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100487 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100488 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100489 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100490 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100491int 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 +0100492{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100493 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100494 char *arg = NULL; /* start pointer for args */
495 char *var = NULL; /* start pointer for vars */
496 int arg_len = 0;
497 int var_len = 0;
498 int cformat; /* current token format */
499 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100500 struct logformat_node *tmplf, *back;
501
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100502 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100503 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100504 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100505 return 0;
506 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200507 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200508
William Lallemand723b73a2012-02-08 16:37:49 +0100509 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100510 list_for_each_entry_safe(tmplf, back, list_format, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200511 LIST_DELETE(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200512 release_sample_expr(tmplf->expr);
513 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100514 free(tmplf);
515 }
516
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100517 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100518 pformat = cformat;
519
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100520 if (!*str)
521 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100522
Joseph Herlant85b40592018-11-15 12:10:04 -0800523 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100524 * second have all common paths processed at one place. The common paths are the ones
525 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
526 * We use the common LF_INIT state to dispatch to the different final states.
527 */
528 switch (pformat) {
529 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100530 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100531 arg_len = var_len = 0;
532 if (*str == '{') { // optional argument
533 cformat = LF_STARG;
534 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100535 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100536 else if (*str == '[') {
537 cformat = LF_STEXPR;
538 var = str + 1; // store expr in variable name
539 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100540 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100541 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100542 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100543 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100544 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500545 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100546 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100547 /* single '%' followed by blank or digit, send them both */
548 cformat = LF_TEXT;
549 pformat = LF_TEXT; /* finally we include the previous char as well */
550 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600551 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 +0100552 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100553 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100554
555 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100556 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500557 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100558 break;
559
560 case LF_STARG: // text immediately following '%{'
561 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100562 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100563 arg_len = str - arg;
564 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100565 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100566 break;
567
568 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100569 if (*str == '[') {
570 cformat = LF_STEXPR;
571 var = str + 1; // store expr in variable name
572 break;
573 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100574 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100575 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100576 var = str;
577 break;
578 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100579 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100580 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100581
Willy Tarreauc8368452012-12-21 00:09:23 +0100582 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100583 /* the whole sample expression is parsed at once,
584 * returning the pointer to the first character not
585 * part of the expression, which MUST be the trailing
586 * angle bracket.
587 */
588 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
589 goto fail;
590
591 if (*str == ']') {
592 // end of arg, go on with next state
593 cformat = pformat = LF_EDEXPR;
594 sp = str;
595 }
596 else {
597 char c = *str;
598 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100599 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100600 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
601 else
602 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200603 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100604 }
605 break;
606
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100607 case LF_VAR: // text part of a variable name
608 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100609 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100610 cformat = LF_INIT; // not variable name anymore
611 break;
612
Willy Tarreauc8368452012-12-21 00:09:23 +0100613 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100614 cformat = LF_INIT;
615 }
616
617 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
618 switch (*str) {
619 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100620 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200621 case ' ':
622 if (options & LOG_OPT_MERGE_SPACES) {
623 cformat = LF_SEPARATOR;
624 break;
625 }
626 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100627 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100628 }
629 }
630
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100631 if (cformat != pformat || pformat == LF_SEPARATOR) {
632 switch (pformat) {
633 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100634 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100635 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100636 break;
637 case LF_TEXT:
638 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100639 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100640 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100641 break;
642 }
643 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100644 }
645 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100646
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100647 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100648 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100649 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100650 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100651 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100652
653 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100654 fail:
655 free(backfmt);
656 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100657}
658
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200659/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500660 * 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 +0200661 * ranges of indexes. Note that an index may be considered as a particular range
662 * with a high limit to the low limit.
663 */
664int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
665{
666 char *end, *p;
667
668 *low = *high = 0;
669
670 p = *arg;
671 end = strchr(p, ',');
672 if (!end)
673 end = p + strlen(p);
674
675 *high = *low = read_uint((const char **)&p, end);
676 if (!*low || (p != end && *p != '-'))
677 goto err;
678
679 if (p == end)
680 goto done;
681
682 p++;
683 *high = read_uint((const char **)&p, end);
684 if (!*high || *high <= *low || p != end)
685 goto err;
686
687 done:
688 if (*end == ',')
689 end++;
690 *arg = end;
691 return 1;
692
693 err:
694 memprintf(err, "wrong sample range '%s'", *arg);
695 return 0;
696}
697
698/*
699 * Returns 1 if the range defined by <low> and <high> overlaps
700 * one of them in <rgs> array of ranges with <sz> the size of this
701 * array, 0 if not.
702 */
703int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
704 unsigned int low, unsigned int high, char **err)
705{
706 size_t i;
707
708 for (i = 0; i < sz; i++) {
709 if ((low >= rgs[i].low && low <= rgs[i].high) ||
710 (high >= rgs[i].low && high <= rgs[i].high)) {
711 memprintf(err, "ranges are overlapping");
712 return 1;
713 }
714 }
715
716 return 0;
717}
718
719int smp_log_range_cmp(const void *a, const void *b)
720{
721 const struct smp_log_range *rg_a = a;
722 const struct smp_log_range *rg_b = b;
723
724 if (rg_a->high < rg_b->low)
725 return -1;
726 else if (rg_a->low > rg_b->high)
727 return 1;
728
729 return 0;
730}
731
732/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200733 * Parse "log" keyword and update <logsrvs> list accordingly.
734 *
735 * When <do_del> is set, it means the "no log" line was parsed, so all log
736 * servers in <logsrvs> are released.
737 *
738 * Otherwise, we try to parse the "log" line. First of all, when the list is not
739 * the global one, we look for the parameter "global". If we find it,
740 * global.logsrvs is copied. Else we parse each arguments.
741 *
742 * The function returns 1 in success case, otherwise, it returns 0 and err is
743 * filled.
744 */
Emeric Brun9533a702021-04-02 10:13:43 +0200745int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200746{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100747 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200748 struct sockaddr_storage *sk;
Emeric Brun94aab062021-04-02 10:41:36 +0200749 struct protocol *proto;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200750 struct logsrv *logsrv = NULL;
751 int port1, port2;
752 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200753 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200754
755 /*
756 * "no log": delete previous herited or defined syslog
757 * servers.
758 */
759 if (do_del) {
760 struct logsrv *back;
761
762 if (*(args[1]) != 0) {
763 memprintf(err, "'no log' does not expect arguments");
764 goto error;
765 }
766
767 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200768 LIST_DELETE(&logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200769 free(logsrv);
770 }
771 return 1;
772 }
773
774 /*
775 * "log global": copy global.logrsvs linked list to the end of logsrvs
776 * list. But first, we check (logsrvs != global.logsrvs).
777 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100778 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200779 if (logsrvs == &global.logsrvs) {
780 memprintf(err, "'global' is not supported for a global syslog server");
781 goto error;
782 }
783 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200784 struct logsrv *node;
785
786 list_for_each_entry(node, logsrvs, list) {
787 if (node->ref == logsrv)
788 goto skip_logsrv;
789 }
790
791 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200792 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200793 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200794 LIST_INIT(&node->list);
Willy Tarreau2b718102021-04-21 07:32:39 +0200795 LIST_APPEND(logsrvs, &node->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200796 node->conf.file = strdup(file);
797 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200798
799 skip_logsrv:
800 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200801 }
802 return 1;
803 }
804
805 /*
806 * "log <address> ...: parse a syslog server line
807 */
808 if (*(args[1]) == 0 || *(args[2]) == 0) {
809 memprintf(err, "expects <address> and <facility> %s as arguments",
810 ((logsrvs == &global.logsrvs) ? "" : "or global"));
811 goto error;
812 }
813
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100814 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
815 if (strcmp(args[1], "stdout") == 0)
816 args[1] = "fd@1";
817 else if (strcmp(args[1], "stderr") == 0)
818 args[1] = "fd@2";
819
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200820 logsrv = calloc(1, sizeof(*logsrv));
821 if (!logsrv) {
822 memprintf(err, "out of memory");
823 goto error;
824 }
825
Emeric Brun9533a702021-04-02 10:13:43 +0200826 logsrv->conf.file = strdup(file);
827 logsrv->conf.line = linenum;
828
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200829 /* skip address for now, it will be parsed at the end */
830 cur_arg = 2;
831
832 /* just after the address, a length may be specified */
833 logsrv->maxlen = MAX_SYSLOG_LEN;
834 if (strcmp(args[cur_arg], "len") == 0) {
835 int len = atoi(args[cur_arg+1]);
836 if (len < 80 || len > 65535) {
837 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
838 args[cur_arg+1]);
839 goto error;
840 }
841 logsrv->maxlen = len;
842 cur_arg += 2;
843 }
844 if (logsrv->maxlen > global.max_syslog_len)
845 global.max_syslog_len = logsrv->maxlen;
846
847 /* after the length, a format may be specified */
848 if (strcmp(args[cur_arg], "format") == 0) {
849 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200850 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200851 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
852 goto error;
853 }
854 cur_arg += 2;
855 }
856
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200857 if (strcmp(args[cur_arg], "sample") == 0) {
858 unsigned low, high;
859 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200860 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
861
862 p = args[cur_arg+1];
863 smp_sz_str = strchr(p, ':');
864 if (!smp_sz_str) {
865 memprintf(err, "Missing sample size");
866 goto error;
867 }
868
869 *smp_sz_str++ = '\0';
870
871 end = p + strlen(p);
872
873 while (p != end) {
874 if (!get_logsrv_smp_range(&low, &high, &p, err))
875 goto error;
876
877 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
878 goto error;
879
880 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
881 if (!smp_rgs) {
882 memprintf(err, "out of memory error");
883 goto error;
884 }
885
886 smp_rgs[smp_rgs_sz].low = low;
887 smp_rgs[smp_rgs_sz].high = high;
888 smp_rgs[smp_rgs_sz].sz = high - low + 1;
889 smp_rgs[smp_rgs_sz].curr_idx = 0;
890 if (smp_rgs[smp_rgs_sz].high > smp_sz)
891 smp_sz = smp_rgs[smp_rgs_sz].high;
892 smp_rgs_sz++;
893 }
894
Tim Duesterhus21648002019-06-23 22:10:10 +0200895 if (smp_rgs == NULL) {
896 memprintf(err, "no sampling ranges given");
897 goto error;
898 }
899
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200900 beg = smp_sz_str;
901 end = beg + strlen(beg);
902 new_smp_sz = read_uint((const char **)&beg, end);
903 if (!new_smp_sz || beg != end) {
904 memprintf(err, "wrong sample size '%s' for sample range '%s'",
905 smp_sz_str, args[cur_arg+1]);
906 goto error;
907 }
908
909 if (new_smp_sz < smp_sz) {
910 memprintf(err, "sample size %zu should be greater or equal to "
911 "%zu the maximum of the high ranges limits",
912 new_smp_sz, smp_sz);
913 goto error;
914 }
915 smp_sz = new_smp_sz;
916
917 /* Let's order <smp_rgs> array. */
918 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
919
920 logsrv->lb.smp_rgs = smp_rgs;
921 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
922 logsrv->lb.smp_sz = smp_sz;
923
924 cur_arg += 2;
925 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200926 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200927 /* parse the facility */
928 logsrv->facility = get_log_facility(args[cur_arg]);
929 if (logsrv->facility < 0) {
930 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
931 goto error;
932 }
933 cur_arg++;
934
935 /* parse the max syslog level (default: debug) */
936 logsrv->level = 7;
937 if (*(args[cur_arg])) {
938 logsrv->level = get_log_level(args[cur_arg]);
939 if (logsrv->level < 0) {
940 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
941 goto error;
942 }
943 cur_arg++;
944 }
945
946 /* parse the limit syslog level (default: emerg) */
947 logsrv->minlvl = 0;
948 if (*(args[cur_arg])) {
949 logsrv->minlvl = get_log_level(args[cur_arg]);
950 if (logsrv->minlvl < 0) {
951 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
952 goto error;
953 }
954 cur_arg++;
955 }
956
957 /* Too many args */
958 if (*(args[cur_arg])) {
959 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
960 goto error;
961 }
962
963 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +0200964 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +0200965 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +0200966 logsrv->addr.ss_family = AF_UNSPEC;
967 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +0200968 logsrv->sink = NULL;
969 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +0200970 goto done;
971 }
972
Emeric Brun94aab062021-04-02 10:41:36 +0200973 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
974 err, NULL, NULL,
975 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM | PA_O_STREAM | PA_O_DEFAULT_DGRAM);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200976 if (!sk)
977 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +0200978
979 if (fd != -1)
980 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200981 logsrv->addr = *sk;
982
983 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200984 if (!port1)
985 set_host_port(&logsrv->addr, SYSLOG_PORT);
986 }
Emeric Brun9533a702021-04-02 10:13:43 +0200987
Emeric Brun26754902021-04-07 14:26:44 +0200988 if (proto && proto->ctrl_type == SOCK_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +0200989 static unsigned long ring_ids;
990
991 /* Implicit sink buffer will be
992 * initialized in post_check
993 */
994 logsrv->type = LOG_TARGET_BUFFER;
995 logsrv->sink = NULL;
996 /* compute uniq name for the ring */
997 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
998 }
999
Willy Tarreauc046d162019-08-30 15:24:59 +02001000 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001001 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001002 return 1;
1003
1004 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001005 free(smp_rgs);
Emeric Brun9533a702021-04-02 10:13:43 +02001006 if (logsrv) {
1007 free(logsrv->conf.file);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001008 free(logsrv->ring_name);
Emeric Brun9533a702021-04-02 10:13:43 +02001009 }
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001010 free(logsrv);
1011 return 0;
1012}
1013
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001014
1015/*
Emeric Brun54648852020-07-06 15:54:06 +02001016 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001017 */
Emeric Brun54648852020-07-06 15:54:06 +02001018enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001019{
Emeric Brun54648852020-07-06 15:54:06 +02001020 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001021
1022 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001023 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001024 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001025 format--;
1026
Emeric Brun54648852020-07-06 15:54:06 +02001027 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001028 return format;
1029}
1030
1031/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001032 * returns log level for <lev> or -1 if not found.
1033 */
1034int get_log_level(const char *lev)
1035{
1036 int level;
1037
1038 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001039 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001040 level--;
1041
1042 return level;
1043}
1044
Willy Tarreaubaaee002006-06-26 02:48:02 +02001045/*
1046 * returns log facility for <fac> or -1 if not found.
1047 */
1048int get_log_facility(const char *fac)
1049{
1050 int facility;
1051
1052 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001053 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001054 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001055
Willy Tarreaubaaee002006-06-26 02:48:02 +02001056 return facility;
1057}
1058
William Lallemanda1cc3812012-02-08 16:38:44 +01001059/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001060 * Encode the string.
1061 *
1062 * When using the +E log format option, it will try to escape '"\]'
1063 * characters with '\' as prefix. The same prefix should not be used as
1064 * <escape>.
1065 */
1066static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001067 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001068 const char *string,
1069 struct logformat_node *node)
1070{
1071 if (node->options & LOG_OPT_ESC) {
1072 if (start < stop) {
1073 stop--; /* reserve one byte for the final '\0' */
1074 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001075 if (!ha_bit_test((unsigned char)(*string), map)) {
1076 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001077 *start++ = *string;
1078 else {
1079 if (start + 2 >= stop)
1080 break;
1081 *start++ = '\\';
1082 *start++ = *string;
1083 }
1084 }
1085 else {
1086 if (start + 3 >= stop)
1087 break;
1088 *start++ = escape;
1089 *start++ = hextab[(*string >> 4) & 15];
1090 *start++ = hextab[*string & 15];
1091 }
1092 string++;
1093 }
1094 *start = '\0';
1095 }
1096 }
1097 else {
1098 return encode_string(start, stop, escape, map, string);
1099 }
1100
1101 return start;
1102}
1103
1104/*
1105 * Encode the chunk.
1106 *
1107 * When using the +E log format option, it will try to escape '"\]'
1108 * characters with '\' as prefix. The same prefix should not be used as
1109 * <escape>.
1110 */
1111static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001112 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001113 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001114 struct logformat_node *node)
1115{
1116 char *str, *end;
1117
1118 if (node->options & LOG_OPT_ESC) {
1119 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001120 str = chunk->area;
1121 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001122
1123 stop--; /* reserve one byte for the final '\0' */
1124 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001125 if (!ha_bit_test((unsigned char)(*str), map)) {
1126 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001127 *start++ = *str;
1128 else {
1129 if (start + 2 >= stop)
1130 break;
1131 *start++ = '\\';
1132 *start++ = *str;
1133 }
1134 }
1135 else {
1136 if (start + 3 >= stop)
1137 break;
1138 *start++ = escape;
1139 *start++ = hextab[(*str >> 4) & 15];
1140 *start++ = hextab[*str & 15];
1141 }
1142 str++;
1143 }
1144 *start = '\0';
1145 }
1146 }
1147 else {
1148 return encode_chunk(start, stop, escape, map, chunk);
1149 }
1150
1151 return start;
1152}
1153
1154/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001155 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001156 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001157 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001158 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001159 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001160char *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 +01001161{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001162 if (size < 2)
1163 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001164
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001165 if (node->options & LOG_OPT_QUOTE) {
1166 *(dst++) = '"';
1167 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001168 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001169
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001170 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001171 if (++len > size)
1172 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001173 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001174 char *ret;
1175
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001176 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001177 if (ret == NULL || *ret != '\0')
1178 return NULL;
1179 len = ret - dst;
1180 }
1181 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001182 len = strlcpy2(dst, src, len);
1183 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001184
1185 size -= len;
1186 dst += len;
1187 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001188 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1189 if (size < 2)
1190 return NULL;
1191 *(dst++) = '-';
1192 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001193
1194 if (node->options & LOG_OPT_QUOTE) {
1195 if (size < 2)
1196 return NULL;
1197 *(dst++) = '"';
1198 }
1199
1200 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001201 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001202}
1203
Willy Tarreau26ffa852018-09-05 15:23:10 +02001204static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001205{
1206 return lf_text_len(dst, src, size, size, node);
1207}
1208
William Lallemand5f232402012-04-05 18:02:55 +02001209/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001210 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001211 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001212 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001213char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001214{
1215 char *ret = dst;
1216 int iret;
1217 char pn[INET6_ADDRSTRLEN];
1218
1219 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001220 unsigned char *addr = NULL;
1221 switch (sockaddr->sa_family) {
1222 case AF_INET:
1223 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1224 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1225 break;
1226 case AF_INET6:
1227 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1228 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1229 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1230 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1231 break;
1232 default:
1233 return NULL;
1234 }
William Lallemand5f232402012-04-05 18:02:55 +02001235 if (iret < 0 || iret > size)
1236 return NULL;
1237 ret += iret;
1238 } else {
1239 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1240 ret = lf_text(dst, pn, size, node);
1241 if (ret == NULL)
1242 return NULL;
1243 }
1244 return ret;
1245}
1246
1247/*
1248 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001249 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001250 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001251char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001252{
1253 char *ret = dst;
1254 int iret;
1255
1256 if (node->options & LOG_OPT_HEXA) {
1257 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1258 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1259 if (iret < 0 || iret > size)
1260 return NULL;
1261 ret += iret;
1262 } else {
1263 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1264 if (ret == NULL)
1265 return NULL;
1266 }
1267 return ret;
1268}
1269
Emeric Brun54648852020-07-06 15:54:06 +02001270
1271/*
1272 * This function sends the syslog message using a printf format string. It
1273 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001274 */
Emeric Brun54648852020-07-06 15:54:06 +02001275void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001276{
Emeric Brun54648852020-07-06 15:54:06 +02001277 va_list argp;
1278 int data_len;
1279
1280 if (level < 0 || format == NULL || logline == NULL)
1281 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001282
Emeric Brun54648852020-07-06 15:54:06 +02001283 va_start(argp, format);
1284 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1285 if (data_len < 0 || data_len > global.max_syslog_len)
1286 data_len = global.max_syslog_len;
1287 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001288
Emeric Brun54648852020-07-06 15:54:06 +02001289 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1290 logline, data_len, default_rfc5424_sd_log_format, 2);
1291}
1292/*
1293 * This function builds a log header of given format using given
1294 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1295 * to determine format based on given metadas. It is useful
1296 * for log-forwarding to be able to forward any format without
1297 * settings.
1298 * This function returns a struct ist array of elements of the header
1299 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001300 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001301 * elements.
1302 */
1303struct ist *build_log_header(enum log_fmt format, int level, int facility,
1304 struct ist *metadata, size_t *nbelem)
1305{
1306 static THREAD_LOCAL struct {
1307 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1308 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1309 time_t cur_legacy_time;
1310 char priority_buffer[6];
1311 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001312
Emeric Brun54648852020-07-06 15:54:06 +02001313 struct tm logtime;
1314 int len;
1315 int fac_level = 0;
1316 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001317
Emeric Brun54648852020-07-06 15:54:06 +02001318 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001319
Emeric Brun54648852020-07-06 15:54:06 +02001320
1321 if (format == LOG_FORMAT_UNSPEC) {
1322 format = LOG_FORMAT_RAW;
1323 if (metadata) {
1324 /* If a hostname is set, it appears we want to perform syslog
1325 * because only rfc5427 or rfc3164 support an hostname.
1326 */
1327 if (metadata[LOG_META_HOST].len) {
1328 /* If a rfc5424 compliant timestamp is used we consider
1329 * that output format is rfc5424, else legacy format
1330 * is used as specified default for local logs
1331 * in documentation.
1332 */
1333 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1334 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1335 format = LOG_FORMAT_RFC5424;
1336 else
1337 format = LOG_FORMAT_RFC3164;
1338 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001339 else if (metadata[LOG_META_TAG].len) {
1340 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001341 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001342 * in legacy format (analog to RFC3164 but
1343 * with stripped hostname).
1344 */
1345 format = LOG_FORMAT_LOCAL;
1346 }
Emeric Brun54648852020-07-06 15:54:06 +02001347 else if (metadata[LOG_META_PRIO].len) {
1348 /* the source seems a parsed message
1349 * offering a valid level/prio prefix
1350 * so we consider this format.
1351 */
1352 format = LOG_FORMAT_PRIO;
1353 }
1354 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001355 }
1356
Emeric Brun54648852020-07-06 15:54:06 +02001357 /* prepare priority, stored into 1 single elem */
1358 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001359 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001360 case LOG_FORMAT_RFC3164:
1361 case LOG_FORMAT_RFC5424:
1362 case LOG_FORMAT_PRIO:
1363 fac_level = facility << 3;
1364 /* further format ignore the facility */
1365 /* fall through */
1366 case LOG_FORMAT_TIMED:
1367 case LOG_FORMAT_SHORT:
1368 fac_level += level;
1369 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1370 do {
1371 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1372 fac_level /= 10;
1373 hdr_ctx.ist_vector[*nbelem].ptr--;
1374 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1375 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1376 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1377 break;
1378 case LOG_FORMAT_ISO:
1379 case LOG_FORMAT_RAW:
1380 break;
1381 case LOG_FORMAT_UNSPEC:
1382 case LOG_FORMATS:
1383 ABORT_NOW();
1384 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001385
William Lallemand2a4a44f2012-02-06 16:00:33 +01001386
Emeric Brun54648852020-07-06 15:54:06 +02001387 /* prepare timestamp, stored into a max of 4 elems */
1388 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001389 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001390 case LOG_FORMAT_RFC3164:
1391 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1392 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1393 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1394 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001395 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001396 break;
1397 }
1398 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1399 int month;
1400 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001401
Emeric Brun54648852020-07-06 15:54:06 +02001402 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001403
Emeric Brun54648852020-07-06 15:54:06 +02001404 /* compute month */
1405 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1406 if (month)
1407 month--;
1408 if (month <= 11) {
1409 /* builds log prefix ex: 'Jan 1 ' */
1410 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1411 "%s %c%c ", monthname[month],
1412 timestamp[8] != '0' ? timestamp[8] : ' ',
1413 timestamp[9]);
1414 /* we reused the timestamp_buffer, signal that it does not
1415 * contain local time anymore
1416 */
1417 hdr_ctx.cur_legacy_time = 0;
1418 if (len == 7) {
1419 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1420 /* adds 'HH:MM:SS' from iso time */
1421 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1422 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1423 /* we successfully reuse iso time, we can break */
1424 break;
1425 }
1426 }
1427 /* Failed to reuse isotime time, fallback to local legacy time */
1428 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001429
Emeric Brun54648852020-07-06 15:54:06 +02001430 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1431 /* re-builds timestamp from the current local time */
1432 get_localtime(time, &logtime);
1433
1434 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1435 "%s %2d %02d:%02d:%02d ",
1436 monthname[logtime.tm_mon],
1437 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1438 if (len != LOG_LEGACYTIME_LEN+1)
1439 hdr_ctx.cur_legacy_time = 0;
1440 else
1441 hdr_ctx.cur_legacy_time = time;
1442 }
1443 if (likely(hdr_ctx.cur_legacy_time))
1444 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1445 else
1446 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1447 break;
1448 case LOG_FORMAT_RFC5424:
1449 /* adds rfc5425 version prefix */
1450 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1451 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001452 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001453 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1454 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1455 break;
1456 }
1457 /* let continue as 'timed' and 'iso' format for usual timestamp */
1458 /* fall through */
1459 case LOG_FORMAT_TIMED:
1460 case LOG_FORMAT_ISO:
1461 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1462 * '1900:01:01T14:00:00+02:00'
1463 * '1900:01:01T10:00:00.123456-02:00'
1464 */
1465 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1466 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1467 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001468 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001469 break;
1470 }
1471 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1472 int month;
1473 char *timestamp = metadata[LOG_META_TIME].ptr;
1474
1475 for (month = 0; month < 12; month++)
1476 if (!memcmp(monthname[month], timestamp, 3))
1477 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001478
Emeric Brun54648852020-07-06 15:54:06 +02001479 if (month < 12) {
1480
1481 /* get local time to retrieve year */
1482 get_localtime(time, &logtime);
1483
1484 /* year seems changed since log */
1485 if (logtime.tm_mon < month)
1486 logtime.tm_year--;
1487
1488 /* builds rfc5424 prefix ex: '1900-01-01T' */
1489 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1490 "%4d-%02d-%c%cT",
1491 logtime.tm_year+1900, month+1,
1492 timestamp[4] != ' ' ? timestamp[4] : '0',
1493 timestamp[5]);
1494
1495 /* we reused the timestamp_buffer, signal that it does not
1496 * contain local time anymore
1497 */
1498 hdr_ctx.cur_legacy_time = 0;
1499 if (len == 11) {
1500 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1501 /* adds HH:MM:SS from legacy timestamp */
1502 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001503 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001504 /* according to rfc: -00:00 means we don't know the timezone */
1505 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1506 /* we successfully reuse legacy time, we can break */
1507 break;
1508 }
1509 }
1510 /* Failed to reuse legacy time, fallback to local iso time */
1511 }
1512 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1513 break;
1514 case LOG_FORMAT_PRIO:
1515 case LOG_FORMAT_SHORT:
1516 case LOG_FORMAT_RAW:
1517 break;
1518 case LOG_FORMAT_UNSPEC:
1519 case LOG_FORMATS:
1520 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001521 }
1522
Emeric Brun54648852020-07-06 15:54:06 +02001523 /* prepare other meta data, stored into a max of 10 elems */
1524 switch (format) {
1525 case LOG_FORMAT_RFC3164:
1526 if (metadata && metadata[LOG_META_HOST].len) {
1527 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1528 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1529 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001530 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001531 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001532 /* fall through */
1533 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001534 if (!metadata || !metadata[LOG_META_TAG].len)
1535 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001536
Emeric Brun54648852020-07-06 15:54:06 +02001537 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1538 if (metadata[LOG_META_PID].len) {
1539 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1540 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1541 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1542 }
1543 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1544 break;
1545 case LOG_FORMAT_RFC5424:
1546 if (metadata && metadata[LOG_META_HOST].len) {
1547 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1548 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1549 }
1550 else
1551 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001552
Emeric Brun54648852020-07-06 15:54:06 +02001553 if (metadata && metadata[LOG_META_TAG].len) {
1554 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1555 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1556 }
1557 else
1558 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001559
Emeric Brun54648852020-07-06 15:54:06 +02001560 if (metadata && metadata[LOG_META_PID].len) {
1561 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1562 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1563 }
1564 else
1565 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001566
Emeric Brun54648852020-07-06 15:54:06 +02001567 if (metadata && metadata[LOG_META_MSGID].len) {
1568 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1569 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1570 }
1571 else
1572 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001573
Emeric Brun54648852020-07-06 15:54:06 +02001574 if (metadata && metadata[LOG_META_STDATA].len) {
1575 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1576 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1577 }
1578 else
1579 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1580 break;
1581 case LOG_FORMAT_PRIO:
1582 case LOG_FORMAT_SHORT:
1583 case LOG_FORMAT_TIMED:
1584 case LOG_FORMAT_ISO:
1585 case LOG_FORMAT_RAW:
1586 break;
1587 case LOG_FORMAT_UNSPEC:
1588 case LOG_FORMATS:
1589 ABORT_NOW();
1590 }
1591
1592 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001593}
1594
1595/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001596 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001597 * The argument <metadata> MUST be an array of size
1598 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001599 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001600 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001601 */
Emeric Brun54648852020-07-06 15:54:06 +02001602static 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 +01001603{
Emeric Brun54648852020-07-06 15:54:06 +02001604 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001605 static THREAD_LOCAL struct msghdr msghdr = {
1606 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001607 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001608 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001609 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1610 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001611 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001612 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001613 size_t nbelem;
1614 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001615
1616 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001617
Emeric Brunfa9d7802020-05-28 14:21:33 +02001618 /* historically some messages used to already contain the trailing LF
1619 * or Zero. Let's remove all trailing LF or Zero
1620 */
Emeric Brun54648852020-07-06 15:54:06 +02001621 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001622 size--;
1623
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001624 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001625 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001626 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001627 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001628 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1629 /* the socket's address is a file descriptor */
1630 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1631 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001632 else if (logsrv->addr.ss_family == AF_UNIX)
1633 plogfd = &logfdunix;
1634 else
1635 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001636
Willy Tarreauc046d162019-08-30 15:24:59 +02001637 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001638 /* socket not successfully initialized yet */
1639 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1640 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1641 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001642
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001643 if (!once) {
1644 once = 1; /* note: no need for atomic ops here */
1645 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1646 nblogger, strerror(errno), errno);
1647 }
1648 return;
1649 } else {
1650 /* we don't want to receive anything on this socket */
1651 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1652 /* does nothing under Linux, maybe needed for others */
1653 shutdown(*plogfd, SHUT_RD);
1654 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1655 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001656 }
1657
Emeric Brun54648852020-07-06 15:54:06 +02001658 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1659 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001660 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001661 struct ist msg;
1662
1663 msg = ist2(message, size);
1664 if (msg.len > logsrv->maxlen)
1665 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001666
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001667 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1668 }
1669 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1670 struct ist msg;
1671
1672 msg = ist2(message, size);
1673 if (msg.len > logsrv->maxlen)
1674 msg.len = logsrv->maxlen;
1675
1676 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001677 }
1678 else {
Emeric Brun54648852020-07-06 15:54:06 +02001679 int i = 0;
1680 int totlen = logsrv->maxlen;
1681
1682 for (i = 0 ; i < nbelem ; i++ ) {
1683 iovec[i].iov_base = msg_header[i].ptr;
1684 iovec[i].iov_len = msg_header[i].len;
1685 if (totlen <= iovec[i].iov_len) {
1686 iovec[i].iov_len = totlen;
1687 totlen = 0;
1688 break;
1689 }
1690 totlen -= iovec[i].iov_len;
1691 }
1692 if (totlen) {
1693 iovec[i].iov_base = message;
1694 iovec[i].iov_len = size;
1695 if (totlen <= iovec[i].iov_len)
1696 iovec[i].iov_len = totlen;
1697 i++;
1698 }
1699 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1700 iovec[i].iov_len = 1;
1701 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001702
Emeric Brun54648852020-07-06 15:54:06 +02001703 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001704 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1705 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001706
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001707 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1708 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001709
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001710 if (sent < 0) {
1711 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001712
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001713 if (errno == EAGAIN)
Willy Tarreau4781b152021-04-06 13:53:36 +02001714 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001715 else if (!once) {
1716 once = 1; /* note: no need for atomic ops here */
1717 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1718 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001719 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001720 }
1721}
Dragan Dosen59cee972015-09-19 22:09:02 +02001722
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001723/*
1724 * This function sends a syslog message.
1725 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001726 * The argument <metadata> MUST be an array of size
1727 * LOG_META_FIELDS*sizeof(struct ist) containing
1728 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001729 */
Emeric Brun54648852020-07-06 15:54:06 +02001730void process_send_log(struct list *logsrvs, int level, int facility,
1731 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001732{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001733 struct logsrv *logsrv;
1734 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001735
1736 /* Send log messages to syslog server. */
1737 nblogger = 0;
1738 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001739 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001740
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001741 /* we can filter the level of the messages that are sent to each logger */
1742 if (level > logsrv->level)
1743 continue;
1744
Frédéric Lécailled803e472019-04-25 07:42:09 +02001745 if (logsrv->lb.smp_rgs) {
1746 struct smp_log_range *curr_rg;
1747
1748 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1749 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1750 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1751 if (in_range) {
1752 /* Let's consume this range. */
1753 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1754 if (!curr_rg->curr_idx) {
1755 /* If consumed, let's select the next range. */
1756 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1757 }
1758 }
1759 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1760 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1761 }
1762 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001763 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1764 (facility == -1) ? logsrv->facility : facility,
1765 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001766 }
1767}
1768
Emeric Brun54648852020-07-06 15:54:06 +02001769/*
1770 * This function sends a syslog message.
1771 * It doesn't care about errors nor does it report them.
1772 * The arguments <sd> and <sd_size> are used for the structured-data part
1773 * in RFC5424 formatted syslog messages.
1774 */
1775void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1776 char *message, size_t size, char *sd, size_t sd_size)
1777{
1778 static THREAD_LOCAL pid_t curr_pid;
1779 static THREAD_LOCAL char pidstr[16];
1780 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1781
1782 if (logsrvs == NULL) {
1783 if (!LIST_ISEMPTY(&global.logsrvs)) {
1784 logsrvs = &global.logsrvs;
1785 }
1786 }
1787 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1788 return;
1789
1790 if (!metadata[LOG_META_HOST].len) {
1791 if (global.log_send_hostname)
1792 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02001793 }
1794
1795 if (!tagb || !tagb->area)
1796 tagb = &global.log_tag;
1797
1798 if (tagb)
1799 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1800
1801 if (unlikely(curr_pid != getpid()))
1802 metadata[LOG_META_PID].len = 0;
1803
1804 if (!metadata[LOG_META_PID].len) {
1805 curr_pid = getpid();
1806 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1807 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1808 }
1809
1810 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1811
1812 /* Remove trailing space of structured data */
1813 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1814 metadata[LOG_META_STDATA].len--;
1815
1816 return process_send_log(logsrvs, level, -1, metadata, message, size);
1817}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001818
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001819const 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 +01001820const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1821 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1822 Set-cookie Updated, unknown, unknown */
1823
William Lallemand1d705562012-03-12 12:46:41 +01001824/*
1825 * try to write a character if there is enough space, or goto out
1826 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001827#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001828 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001829 *(tmplog++) = (x); \
1830 } else { \
1831 goto out; \
1832 } \
1833 } while(0)
1834
Dragan Dosen835b9212016-02-12 13:23:03 +01001835
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001836/* Initializes some log data at boot */
1837static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001838{
1839 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001840 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001841
1842 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1843 * inside PARAM-VALUE should be escaped with '\' as prefix.
1844 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1845 * details.
1846 */
1847 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1848
1849 tmp = "\"\\]";
1850 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001851 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001852 tmp++;
1853 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001854
1855 /* initialize the log header encoding map : '{|}"#' should be encoded with
1856 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1857 * URL encoding only requires '"', '#' to be encoded as well as non-
1858 * printable characters above.
1859 */
1860 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1861 memset(url_encode_map, 0, sizeof(url_encode_map));
1862 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001863 ha_bit_set(i, hdr_encode_map);
1864 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001865 }
1866 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001867 ha_bit_set(i, hdr_encode_map);
1868 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001869 }
1870
1871 tmp = "\"#{|}";
1872 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001873 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001874 tmp++;
1875 }
1876
1877 tmp = "\"#";
1878 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001879 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001880 tmp++;
1881 }
1882
1883 /* initialize the http header encoding map. The draft httpbis define the
1884 * header content as:
1885 *
1886 * HTTP-message = start-line
1887 * *( header-field CRLF )
1888 * CRLF
1889 * [ message-body ]
1890 * header-field = field-name ":" OWS field-value OWS
1891 * field-value = *( field-content / obs-fold )
1892 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1893 * obs-fold = CRLF 1*( SP / HTAB )
1894 * field-vchar = VCHAR / obs-text
1895 * VCHAR = %x21-7E
1896 * obs-text = %x80-FF
1897 *
1898 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1899 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08001900 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02001901 */
1902 memset(http_encode_map, 0, sizeof(http_encode_map));
1903 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001904 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001905 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001906 ha_bit_set(i, http_encode_map);
1907 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001908}
William Lallemand1d705562012-03-12 12:46:41 +01001909
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001910INITCALL0(STG_PREPARE, init_log);
1911
Christopher Faulet0132d062017-07-26 15:33:35 +02001912/* Initialize log buffers used for syslog messages */
1913int init_log_buffers()
1914{
Christopher Faulet0132d062017-07-26 15:33:35 +02001915 logline = my_realloc2(logline, global.max_syslog_len + 1);
1916 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02001917 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02001918 return 0;
1919 return 1;
1920}
1921
1922/* Deinitialize log buffers used for syslog messages */
1923void deinit_log_buffers()
1924{
Christopher Faulet0132d062017-07-26 15:33:35 +02001925 free(logline);
1926 free(logline_rfc5424);
Christopher Faulet0132d062017-07-26 15:33:35 +02001927 logline = NULL;
1928 logline_rfc5424 = NULL;
1929}
1930
Willy Tarreaudf974472012-12-28 02:44:01 +01001931/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1932 * <maxsize> characters. Returns the size of the output string in characters,
1933 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001934 * is not zero. It requires a valid session and optionally a stream. If the
1935 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01001936 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001937int 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 +02001938{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001939 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001940 struct proxy *be;
1941 struct http_txn *txn;
1942 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001943 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001944 unsigned int s_flags;
1945 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02001946 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001947 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001948 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001949 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001950 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001951 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001952 int t_request;
1953 int hdr;
1954 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001955 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001956 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001957 char *ret;
1958 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001959 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001960 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001961 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001962 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00001963 struct ist path;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001964
William Lallemandbddd4fd2012-02-27 11:23:10 +01001965 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001966
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001967 if (likely(s)) {
1968 be = s->be;
1969 txn = s->txn;
1970 be_conn = cs_conn(objt_cs(s->si[1].end));
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001971 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001972 s_flags = s->flags;
1973 uniq_id = s->uniq_id;
1974 logs = &s->logs;
1975 } else {
1976 /* we have no stream so we first need to initialize a few
1977 * things that are needed later. We do increment the request
1978 * ID so that it's uniquely assigned to this request just as
1979 * if the request had reached the point of being processed.
1980 * A request error is reported as it's the only element we have
1981 * here and which justifies emitting such a log.
1982 */
Christopher Fauletfd818482021-04-14 14:01:41 +02001983 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001984 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001985 fe_conn = objt_conn(sess->origin);
Christopher Fauletfd818482021-04-14 14:01:41 +02001986 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? cs_conn(__objt_check(sess->origin)->cs) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001987 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001988 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02001989 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001990
1991 /* prepare a valid log structure */
1992 tmp_strm_log.tv_accept = sess->tv_accept;
1993 tmp_strm_log.accept_date = sess->accept_date;
1994 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02001995 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001996 tv_zero(&tmp_strm_log.tv_request);
1997 tmp_strm_log.t_queue = -1;
1998 tmp_strm_log.t_connect = -1;
1999 tmp_strm_log.t_data = -1;
2000 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2001 tmp_strm_log.bytes_in = 0;
2002 tmp_strm_log.bytes_out = 0;
2003 tmp_strm_log.prx_queue_pos = 0;
2004 tmp_strm_log.srv_queue_pos = 0;
2005
2006 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002007
2008 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
2009 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, NULL);
2010
2011 switch (es) {
2012 case MUX_ES_SUCCESS:
2013 break;
2014 case MUX_ES_INVALID_ERR:
2015 status = 400;
2016 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2017 s_flags = SF_ERR_CLICL | SF_FINST_R;
2018 else
2019 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2020 break;
2021 case MUX_ES_TOUT_ERR:
2022 status = 408;
2023 s_flags = SF_ERR_CLITO | SF_FINST_R;
2024 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002025 case MUX_ES_NOTIMPL_ERR:
2026 status = 501;
2027 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2028 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002029 case MUX_ES_INTERNAL_ERR:
2030 status = 500;
2031 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2032 break;
2033 default:
2034 break;
2035 }
2036 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002037 }
2038
William Lallemandbddd4fd2012-02-27 11:23:10 +01002039 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002040 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2041 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002042
William Lallemand1d705562012-03-12 12:46:41 +01002043 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002044
William Lallemandbddd4fd2012-02-27 11:23:10 +01002045 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002046 if (LIST_ISEMPTY(list_format))
2047 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002048
William Lallemand1d705562012-03-12 12:46:41 +01002049 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002050 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002051 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002052 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002053 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002054
Willy Tarreauc8368452012-12-21 00:09:23 +01002055 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002056 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002057 if (!last_isspace) {
2058 LOGCHAR(' ');
2059 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002060 }
2061 break;
2062
William Lallemand1d705562012-03-12 12:46:41 +01002063 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002064 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002065 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002066 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002067 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002068 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002069 last_isspace = 0;
2070 break;
2071
Willy Tarreauc8368452012-12-21 00:09:23 +01002072 case LOG_FMT_EXPR: // sample expression, may be request or response
2073 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002074 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002075 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 +02002076 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002077 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 +01002078 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002079 ret = lf_encode_chunk(tmplog, dst + maxsize,
2080 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002081 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002082 ret = lf_text_len(tmplog,
2083 key ? key->data.u.str.area : NULL,
2084 key ? key->data.u.str.data : 0,
2085 dst + maxsize - tmplog,
2086 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002087 if (ret == 0)
2088 goto out;
2089 tmplog = ret;
2090 last_isspace = 0;
2091 break;
2092
Willy Tarreau2beef582012-12-20 17:22:52 +01002093 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002094 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002095 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002096 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002097 else
2098 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002099 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002100 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002101 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002102 last_isspace = 0;
2103 break;
2104
Willy Tarreau2beef582012-12-20 17:22:52 +01002105 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002106 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002107 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002108 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002109 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002110 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002111 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002112 dst + maxsize - tmplog, tmp);
2113 }
William Lallemand5f232402012-04-05 18:02:55 +02002114 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002115 else
2116 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2117
William Lallemand5f232402012-04-05 18:02:55 +02002118 if (ret == NULL)
2119 goto out;
2120 tmplog = ret;
2121 last_isspace = 0;
2122 break;
2123
Willy Tarreau2beef582012-12-20 17:22:52 +01002124 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002125 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002126 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002127 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002128 }
2129 else
2130 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2131
William Lallemand1d705562012-03-12 12:46:41 +01002132 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002133 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002134 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002135 last_isspace = 0;
2136 break;
2137
Willy Tarreau2beef582012-12-20 17:22:52 +01002138 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002139 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002140 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002141 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002142 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002143 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002144 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002145 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002146 else
2147 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2148
William Lallemand5f232402012-04-05 18:02:55 +02002149 if (ret == NULL)
2150 goto out;
2151 tmplog = ret;
2152 last_isspace = 0;
2153 break;
2154
Willy Tarreau2beef582012-12-20 17:22:52 +01002155 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002156 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002157 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002158 else
2159 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2160
William Lallemand1d705562012-03-12 12:46:41 +01002161 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002162 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002163 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002164 last_isspace = 0;
2165 break;
2166
Willy Tarreau2beef582012-12-20 17:22:52 +01002167 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002168 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002169 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002170 else
2171 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2172
William Lallemand5f232402012-04-05 18:02:55 +02002173 if (ret == NULL)
2174 goto out;
2175 tmplog = ret;
2176 last_isspace = 0;
2177 break;
2178
Willy Tarreau2beef582012-12-20 17:22:52 +01002179 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002180 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002181 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002182 else
2183 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2184
William Lallemand5f232402012-04-05 18:02:55 +02002185 if (ret == NULL)
2186 goto out;
2187 tmplog = ret;
2188 last_isspace = 0;
2189 break;
2190
Willy Tarreau2beef582012-12-20 17:22:52 +01002191 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002192 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002193 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002194 else
2195 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2196
William Lallemand1d705562012-03-12 12:46:41 +01002197 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002198 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002199 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002200 last_isspace = 0;
2201 break;
2202
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002203 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002204 get_localtime(logs->accept_date.tv_sec, &tm);
2205 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002206 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002207 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002208 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002209 last_isspace = 0;
2210 break;
2211
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002212 case LOG_FMT_tr: // %tr = start of request date
2213 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002214 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 +02002215 get_localtime(tv.tv_sec, &tm);
2216 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2217 if (ret == NULL)
2218 goto out;
2219 tmplog = ret;
2220 last_isspace = 0;
2221 break;
2222
2223 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002224 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002225 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002226 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002227 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002228 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002229 last_isspace = 0;
2230 break;
2231
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002232 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002233 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 +02002234 get_gmtime(tv.tv_sec, &tm);
2235 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2236 if (ret == NULL)
2237 goto out;
2238 tmplog = ret;
2239 last_isspace = 0;
2240 break;
2241
2242 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002243 get_localtime(logs->accept_date.tv_sec, &tm);
2244 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002245 if (ret == NULL)
2246 goto out;
2247 tmplog = ret;
2248 last_isspace = 0;
2249 break;
2250
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002251 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002252 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 +02002253 get_localtime(tv.tv_sec, &tm);
2254 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2255 if (ret == NULL)
2256 goto out;
2257 tmplog = ret;
2258 last_isspace = 0;
2259 break;
2260
William Lallemand5f232402012-04-05 18:02:55 +02002261 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002262 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002263 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002264 if (iret < 0 || iret > dst + maxsize - tmplog)
2265 goto out;
2266 last_isspace = 0;
2267 tmplog += iret;
2268 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002269 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002270 if (ret == NULL)
2271 goto out;
2272 tmplog = ret;
2273 last_isspace = 0;
2274 }
2275 break;
2276
William Lallemand1d705562012-03-12 12:46:41 +01002277 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002278 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002279 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002280 if (iret < 0 || iret > dst + maxsize - tmplog)
2281 goto out;
2282 last_isspace = 0;
2283 tmplog += iret;
2284 } else {
2285 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002286 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002287 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002288 tmplog, 4);
2289 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002290 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002291 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002292 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002293 }
2294 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002295
William Lallemand1d705562012-03-12 12:46:41 +01002296 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002297 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002298 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002299 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002300 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002301 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002302 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002303 break;
2304
Willy Tarreau773d65f2012-10-12 14:56:11 +02002305 case LOG_FMT_FRONTEND_XPRT: // %ft
2306 src = fe->id;
2307 if (tmp->options & LOG_OPT_QUOTE)
2308 LOGCHAR('"');
2309 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2310 if (iret == 0)
2311 goto out;
2312 tmplog += iret;
Christopher Fauletfd818482021-04-14 14:01:41 +02002313 if (sess->listener && sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002314 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002315 if (tmp->options & LOG_OPT_QUOTE)
2316 LOGCHAR('"');
2317 last_isspace = 0;
2318 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002319#ifdef USE_OPENSSL
2320 case LOG_FMT_SSL_CIPHER: // %sslc
2321 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002322 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002323 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002324 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002325 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002326 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2327 if (ret == NULL)
2328 goto out;
2329 tmplog = ret;
2330 last_isspace = 0;
2331 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002332
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002333 case LOG_FMT_SSL_VERSION: // %sslv
2334 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002335 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002336 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002337 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002338 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002339 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2340 if (ret == NULL)
2341 goto out;
2342 tmplog = ret;
2343 last_isspace = 0;
2344 break;
2345#endif
William Lallemand1d705562012-03-12 12:46:41 +01002346 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002347 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002348 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002349 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002350 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002351 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002352 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002353 break;
2354
William Lallemand1d705562012-03-12 12:46:41 +01002355 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002356 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002357 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002358 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002359 break;
2360 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002361 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002362 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002363 case OBJ_TYPE_CHECK:
2364 src = (__objt_check(sess->origin)->server
2365 ? __objt_check(sess->origin)->server->id
2366 : "<NOSRV>");
2367 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002368 default:
2369 src = "<NOSRV>";
2370 break;
2371 }
William Lallemand5f232402012-04-05 18:02:55 +02002372 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002373 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002374 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002375 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002376 last_isspace = 0;
2377 break;
2378
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002379 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002380 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002381 if (ret == NULL)
2382 goto out;
2383 tmplog = ret;
2384 last_isspace = 0;
2385 break;
2386
2387 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002388 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002389 if (ret == NULL)
2390 goto out;
2391 tmplog = ret;
2392 last_isspace = 0;
2393 break;
2394
2395 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002396 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002397 tmplog, dst + maxsize - tmplog);
2398 if (ret == NULL)
2399 goto out;
2400 tmplog = ret;
2401 last_isspace = 0;
2402 break;
2403
2404 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002405 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002406 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002407 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002408 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002409 last_isspace = 0;
2410 break;
2411
William Lallemand1d705562012-03-12 12:46:41 +01002412 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002413 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002414 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002415 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002416 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002417 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002418 last_isspace = 0;
2419 break;
2420
William Lallemand1d705562012-03-12 12:46:41 +01002421 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002422 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002423 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002424 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002425 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002426 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002427 last_isspace = 0;
2428 break;
2429
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002430 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002431 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002432 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002433 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002434 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002435 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002436 last_isspace = 0;
2437 break;
2438
Willy Tarreau27b639d2016-05-17 17:55:27 +02002439 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002440 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002441 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002442 tmplog, dst + maxsize - tmplog);
2443 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002444 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002445 tmplog, dst + maxsize - tmplog);
2446 if (ret == NULL)
2447 goto out;
2448 tmplog = ret;
2449 last_isspace = 0;
2450 break;
2451
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002452 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2453 if (!(fe->to_log & LW_BYTES))
2454 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002455 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 +02002456 tmplog, dst + maxsize - tmplog);
2457 if (ret == NULL)
2458 goto out;
2459 tmplog = ret;
2460 last_isspace = 0;
2461 break;
2462
2463 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002464 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002465 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002466 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002467 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002468 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002469 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002470 last_isspace = 0;
2471 break;
2472
Damien Claisse57c8eb92020-04-28 12:09:19 +00002473 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2474 if (!(fe->to_log & LW_BYTES))
2475 LOGCHAR('+');
2476 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2477 tmplog, dst + maxsize - tmplog);
2478 if (ret == NULL)
2479 goto out;
2480 tmplog = ret;
2481 last_isspace = 0;
2482 break;
2483
Willy Tarreau2beef582012-12-20 17:22:52 +01002484 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002485 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002486 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002487 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002488 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002489 last_isspace = 0;
2490 break;
2491
William Lallemand1d705562012-03-12 12:46:41 +01002492 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002493 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002494 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002495 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002496 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002497 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002498 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002499 last_isspace = 0;
2500 break;
2501
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002502 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002503 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002504 if (ret == NULL)
2505 goto out;
2506 tmplog = ret;
2507 last_isspace = 0;
2508 break;
2509
Willy Tarreau2beef582012-12-20 17:22:52 +01002510 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002511 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002512 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002513 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002514 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002515 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002516 last_isspace = 0;
2517 break;
2518
Willy Tarreau2beef582012-12-20 17:22:52 +01002519 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002520 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002521 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002522 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002523 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002524 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002525 last_isspace = 0;
2526 break;
2527
William Lallemand1d705562012-03-12 12:46:41 +01002528 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002529 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2530 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002531 *tmplog = '\0';
2532 last_isspace = 0;
2533 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002534
William Lallemand1d705562012-03-12 12:46:41 +01002535 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002536 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2537 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002538 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2539 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 +01002540 last_isspace = 0;
2541 break;
2542
William Lallemand1d705562012-03-12 12:46:41 +01002543 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002544 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002545 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002546 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002547 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002548 last_isspace = 0;
2549 break;
2550
William Lallemand1d705562012-03-12 12:46:41 +01002551 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002552 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002553 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002554 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002555 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002556 last_isspace = 0;
2557 break;
2558
William Lallemand1d705562012-03-12 12:46:41 +01002559 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002560 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002561 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002562 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002563 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002564 last_isspace = 0;
2565 break;
2566
William Lallemand1d705562012-03-12 12:46:41 +01002567 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002568 switch (obj_type(s ? s->target : sess->origin)) {
2569 case OBJ_TYPE_SERVER:
2570 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2571 tmplog, dst + maxsize - tmplog);
2572 break;
2573 case OBJ_TYPE_CHECK:
2574 ret = ultoa_o(__objt_check(sess->origin)->server
2575 ? __objt_check(sess->origin)->server->cur_sess
2576 : 0, tmplog, dst + maxsize - tmplog);
2577 break;
2578 default:
2579 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2580 break;
2581 }
2582
William Lallemand1d705562012-03-12 12:46:41 +01002583 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002584 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002585 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002586 last_isspace = 0;
2587 break;
2588
William Lallemand1d705562012-03-12 12:46:41 +01002589 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002590 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002591 LOGCHAR('+');
Christopher Faulet1d26f222021-04-16 11:24:20 +02002592 ret = ltoa_o(((s && s->si[1].conn_retries > 0)
2593 ? (be->conn_retries - s->si[1].conn_retries)
2594 : ((s && s->si[1].state != SI_ST_INI) ? be->conn_retries : 0)),
2595 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002596 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002597 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002598 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002599 last_isspace = 0;
2600 break;
2601
William Lallemand1d705562012-03-12 12:46:41 +01002602 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002603 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002604 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002605 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002606 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002607 last_isspace = 0;
2608 break;
2609
William Lallemand1d705562012-03-12 12:46:41 +01002610 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002611 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002612 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002613 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002614 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002615 last_isspace = 0;
2616 break;
2617
William Lallemand1d705562012-03-12 12:46:41 +01002618 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002619 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002620 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002621 if (tmp->options & LOG_OPT_QUOTE)
2622 LOGCHAR('"');
2623 LOGCHAR('{');
2624 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2625 if (hdr)
2626 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002627 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002628 ret = lf_encode_string(tmplog, dst + maxsize,
2629 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002630 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002631 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002632 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002633 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002634 }
2635 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002636 if (tmp->options & LOG_OPT_QUOTE)
2637 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002638 last_isspace = 0;
2639 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002640 break;
2641
William Lallemand1d705562012-03-12 12:46:41 +01002642 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002643 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002644 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002645 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2646 if (hdr > 0)
2647 LOGCHAR(' ');
2648 if (tmp->options & LOG_OPT_QUOTE)
2649 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002650 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002651 ret = lf_encode_string(tmplog, dst + maxsize,
2652 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002653 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002654 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002655 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002656 } else if (!(tmp->options & LOG_OPT_QUOTE))
2657 LOGCHAR('-');
2658 if (tmp->options & LOG_OPT_QUOTE)
2659 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002660 last_isspace = 0;
2661 }
2662 }
2663 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002664
William Lallemand1d705562012-03-12 12:46:41 +01002665
2666 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002667 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002668 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002669 if (tmp->options & LOG_OPT_QUOTE)
2670 LOGCHAR('"');
2671 LOGCHAR('{');
2672 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2673 if (hdr)
2674 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002675 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002676 ret = lf_encode_string(tmplog, dst + maxsize,
2677 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002678 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002679 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002680 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002681 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002682 }
2683 LOGCHAR('}');
2684 last_isspace = 0;
2685 if (tmp->options & LOG_OPT_QUOTE)
2686 LOGCHAR('"');
2687 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002688 break;
2689
William Lallemand1d705562012-03-12 12:46:41 +01002690 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002691 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002692 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002693 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2694 if (hdr > 0)
2695 LOGCHAR(' ');
2696 if (tmp->options & LOG_OPT_QUOTE)
2697 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002698 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002699 ret = lf_encode_string(tmplog, dst + maxsize,
2700 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002701 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002702 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002703 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002704 } else if (!(tmp->options & LOG_OPT_QUOTE))
2705 LOGCHAR('-');
2706 if (tmp->options & LOG_OPT_QUOTE)
2707 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002708 last_isspace = 0;
2709 }
2710 }
2711 break;
2712
William Lallemand1d705562012-03-12 12:46:41 +01002713 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002714 /* Request */
2715 if (tmp->options & LOG_OPT_QUOTE)
2716 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002717 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002718 ret = lf_encode_string(tmplog, dst + maxsize,
2719 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002720 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002721 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002722 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002723 if (tmp->options & LOG_OPT_QUOTE)
2724 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002725 last_isspace = 0;
2726 break;
William Lallemand5f232402012-04-05 18:02:55 +02002727
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002728 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002729 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002730
Willy Tarreaub7636d12015-06-17 19:58:02 +02002731 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002732 LOGCHAR('"');
2733
2734 end = uri + strlen(uri);
2735 // look for the first whitespace character
2736 while (uri < end && !HTTP_IS_SPHT(*uri))
2737 uri++;
2738
2739 // keep advancing past multiple spaces
2740 while (uri < end && HTTP_IS_SPHT(*uri)) {
2741 uri++; nspaces++;
2742 }
2743
2744 // look for first space or question mark after url
2745 spc = uri;
2746 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2747 spc++;
2748
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002749 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002750 chunk.area = "<BADREQ>";
2751 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002752 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002753 chunk.area = uri;
2754 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002755 }
2756
Dragan Dosen835b9212016-02-12 13:23:03 +01002757 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002758 if (ret == NULL || *ret != '\0')
2759 goto out;
2760
2761 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002762 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002763 LOGCHAR('"');
2764
2765 last_isspace = 0;
2766 break;
2767
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002768 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2769 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2770
2771 if (tmp->options & LOG_OPT_QUOTE)
2772 LOGCHAR('"');
2773
2774 end = uri + strlen(uri);
2775
2776 // look for the first whitespace character
2777 while (uri < end && !HTTP_IS_SPHT(*uri))
2778 uri++;
2779
2780 // keep advancing past multiple spaces
2781 while (uri < end && HTTP_IS_SPHT(*uri)) {
2782 uri++; nspaces++;
2783 }
2784
2785 // look for first space after url
2786 spc = uri;
2787 while (spc < end && !HTTP_IS_SPHT(*spc))
2788 spc++;
2789
Tim Duesterhus92c696e2021-02-28 16:11:36 +01002790 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002791
2792 // extract relative path without query params from url
2793 path = iststop(http_get_path(path), '?');
2794 if (!txn || !txn->uri || nspaces == 0) {
2795 chunk.area = "<BADREQ>";
2796 chunk.data = strlen("<BADREQ>");
2797 } else {
2798 chunk.area = path.ptr;
2799 chunk.data = path.len;
2800 }
2801
2802 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2803 if (ret == NULL || *ret != '\0')
2804 goto out;
2805
2806 tmplog = ret;
2807 if (tmp->options & LOG_OPT_QUOTE)
2808 LOGCHAR('"');
2809
2810 last_isspace = 0;
2811 break;
2812
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002813 case LOG_FMT_HTTP_QUERY: // %HQ
2814 if (tmp->options & LOG_OPT_QUOTE)
2815 LOGCHAR('"');
2816
Willy Tarreau57bc8912016-04-25 17:09:40 +02002817 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002818 chunk.area = "<BADREQ>";
2819 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002820 } else {
2821 uri = txn->uri;
2822 end = uri + strlen(uri);
2823 // look for the first question mark
2824 while (uri < end && *uri != '?')
2825 uri++;
2826
2827 qmark = uri;
2828 // look for first space or question mark after url
2829 while (uri < end && !HTTP_IS_SPHT(*uri))
2830 uri++;
2831
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002832 chunk.area = qmark;
2833 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002834 }
2835
Dragan Dosen835b9212016-02-12 13:23:03 +01002836 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002837 if (ret == NULL || *ret != '\0')
2838 goto out;
2839
2840 tmplog = ret;
2841 if (tmp->options & LOG_OPT_QUOTE)
2842 LOGCHAR('"');
2843
2844 last_isspace = 0;
2845 break;
2846
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002847 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002848 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002849
Willy Tarreaub7636d12015-06-17 19:58:02 +02002850 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002851 LOGCHAR('"');
2852
2853 end = uri + strlen(uri);
2854 // look for the first whitespace character
2855 while (uri < end && !HTTP_IS_SPHT(*uri))
2856 uri++;
2857
2858 // keep advancing past multiple spaces
2859 while (uri < end && HTTP_IS_SPHT(*uri)) {
2860 uri++; nspaces++;
2861 }
2862
2863 // look for first space after url
2864 spc = uri;
2865 while (spc < end && !HTTP_IS_SPHT(*spc))
2866 spc++;
2867
Willy Tarreau57bc8912016-04-25 17:09:40 +02002868 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002869 chunk.area = "<BADREQ>";
2870 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002871 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002872 chunk.area = uri;
2873 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002874 }
2875
Dragan Dosen835b9212016-02-12 13:23:03 +01002876 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002877 if (ret == NULL || *ret != '\0')
2878 goto out;
2879
2880 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002881 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002882 LOGCHAR('"');
2883
2884 last_isspace = 0;
2885 break;
2886
2887 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002888 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002889 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002890 LOGCHAR('"');
2891
2892 end = uri + strlen(uri);
2893 // look for the first whitespace character
2894 spc = uri;
2895 while (spc < end && !HTTP_IS_SPHT(*spc))
2896 spc++;
2897
2898 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002899 chunk.area = "<BADREQ>";
2900 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002901 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002902 chunk.area = uri;
2903 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002904 }
2905
Dragan Dosen835b9212016-02-12 13:23:03 +01002906 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002907 if (ret == NULL || *ret != '\0')
2908 goto out;
2909
2910 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002911 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002912 LOGCHAR('"');
2913
2914 last_isspace = 0;
2915 break;
2916
2917 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002918 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002919 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002920 LOGCHAR('"');
2921
2922 end = uri + strlen(uri);
2923 // look for the first whitespace character
2924 while (uri < end && !HTTP_IS_SPHT(*uri))
2925 uri++;
2926
2927 // keep advancing past multiple spaces
2928 while (uri < end && HTTP_IS_SPHT(*uri)) {
2929 uri++; nspaces++;
2930 }
2931
2932 // look for the next whitespace character
2933 while (uri < end && !HTTP_IS_SPHT(*uri))
2934 uri++;
2935
2936 // keep advancing past multiple spaces
2937 while (uri < end && HTTP_IS_SPHT(*uri))
2938 uri++;
2939
Willy Tarreau57bc8912016-04-25 17:09:40 +02002940 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002941 chunk.area = "<BADREQ>";
2942 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002943 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002944 chunk.area = "HTTP/0.9";
2945 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002946 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002947 chunk.area = uri;
2948 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002949 }
2950
Dragan Dosen835b9212016-02-12 13:23:03 +01002951 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002952 if (ret == NULL || *ret != '\0')
2953 goto out;
2954
2955 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002956 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002957 LOGCHAR('"');
2958
2959 last_isspace = 0;
2960 break;
2961
William Lallemand5f232402012-04-05 18:02:55 +02002962 case LOG_FMT_COUNTER: // %rt
2963 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002964 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002965 if (iret < 0 || iret > dst + maxsize - tmplog)
2966 goto out;
2967 last_isspace = 0;
2968 tmplog += iret;
2969 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002970 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002971 if (ret == NULL)
2972 goto out;
2973 tmplog = ret;
2974 last_isspace = 0;
2975 }
2976 break;
2977
Willy Tarreau7346acb2014-08-28 15:03:15 +02002978 case LOG_FMT_LOGCNT: // %lc
2979 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002980 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002981 if (iret < 0 || iret > dst + maxsize - tmplog)
2982 goto out;
2983 last_isspace = 0;
2984 tmplog += iret;
2985 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002986 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002987 if (ret == NULL)
2988 goto out;
2989 tmplog = ret;
2990 last_isspace = 0;
2991 }
2992 break;
2993
William Lallemand5f232402012-04-05 18:02:55 +02002994 case LOG_FMT_HOSTNAME: // %H
2995 src = hostname;
2996 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2997 if (ret == NULL)
2998 goto out;
2999 tmplog = ret;
3000 last_isspace = 0;
3001 break;
3002
3003 case LOG_FMT_PID: // %pid
3004 if (tmp->options & LOG_OPT_HEXA) {
3005 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3006 if (iret < 0 || iret > dst + maxsize - tmplog)
3007 goto out;
3008 last_isspace = 0;
3009 tmplog += iret;
3010 } else {
3011 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3012 if (ret == NULL)
3013 goto out;
3014 tmplog = ret;
3015 last_isspace = 0;
3016 }
3017 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003018
3019 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003020 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003021 if (s)
3022 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3023 else
3024 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003025 if (ret == NULL)
3026 goto out;
3027 tmplog = ret;
3028 last_isspace = 0;
3029 break;
3030
William Lallemandbddd4fd2012-02-27 11:23:10 +01003031 }
3032 }
3033
3034out:
William Lallemand1d705562012-03-12 12:46:41 +01003035 /* *tmplog is a unused character */
3036 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003037 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003038
Willy Tarreaubaaee002006-06-26 02:48:02 +02003039}
3040
William Lallemand1d705562012-03-12 12:46:41 +01003041/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003042 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003043 * Will not log if the frontend has no log defined.
3044 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003045void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003046{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003047 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003048 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003049 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003050
3051 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003052 err = (s->flags & SF_REDISP) ||
3053 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3054 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003055 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003056 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003057
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003058 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003059 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003060
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003061 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003062 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003063
Willy Tarreauabcd5142013-06-11 17:18:02 +02003064 if (s->logs.level) { /* loglevel was overridden */
3065 if (s->logs.level == -1) {
3066 s->logs.logwait = 0; /* logs disabled */
3067 return;
3068 }
3069 level = s->logs.level - 1;
3070 }
3071 else {
3072 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003073 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003074 level = LOG_ERR;
3075 }
William Lallemand1d705562012-03-12 12:46:41 +01003076
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003077 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003078 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003079 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003080 }
3081
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003082 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3083 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3084 &sess->fe->logformat_sd);
3085 }
3086
Dragan Dosen59cee972015-09-19 22:09:02 +02003087 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003088 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003089 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003090 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3091 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003092 s->logs.logwait = 0;
3093 }
3094}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003095
Willy Tarreau53839352018-09-05 19:51:10 +02003096/*
3097 * send a minimalist log for the session. Will not log if the frontend has no
3098 * log defined. It is assumed that this is only used to report anomalies that
3099 * cannot lead to the creation of a regular stream. Because of this the log
3100 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3101 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003102 * function to report unimportant events. It is safe to call this function with
3103 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003104 */
3105void sess_log(struct session *sess)
3106{
3107 int size, level;
3108 int sd_size = 0;
3109
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003110 if (!sess)
3111 return;
3112
Willy Tarreau53839352018-09-05 19:51:10 +02003113 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3114 return;
3115
3116 level = LOG_INFO;
3117 if (sess->fe->options2 & PR_O2_LOGERRORS)
3118 level = LOG_ERR;
3119
3120 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3121 sd_size = sess_build_logline(sess, NULL,
3122 logline_rfc5424, global.max_syslog_len,
3123 &sess->fe->logformat_sd);
3124 }
3125
3126 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3127 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003128 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003129 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3130 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003131 }
3132}
3133
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003134void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3135{
3136 va_list argp;
3137 int data_len;
3138
3139 if (level < 0 || format == NULL || logline == NULL)
3140 return;
3141
3142 va_start(argp, format);
3143 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3144 if (data_len < 0 || data_len > global.max_syslog_len)
3145 data_len = global.max_syslog_len;
3146 va_end(argp);
3147
3148 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3149}
Emeric Brun54932b42020-07-07 09:43:24 +02003150/*
3151 * This function parse a received log message <buf>, of size <buflen>
3152 * it fills <level>, <facility> and <metadata> depending of the detected
3153 * header format and message will point on remaining payload of <size>
3154 *
3155 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3156 * struct ist len will be set to 0 if field is not found
3157 * <level> and <facility> will be set to -1 if not found.
3158 */
3159void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3160 struct ist *metadata, char **message, size_t *size)
3161{
3162
3163 char *p;
3164 int fac_level = 0;
3165
3166 *level = *facility = -1;
3167
3168 *message = buf;
3169 *size = buflen;
3170
3171 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3172
3173 p = buf;
3174 if (*size < 2 || *p != '<')
3175 return;
3176
3177 p++;
3178 while (*p != '>') {
3179 if (*p > '9' || *p < '0')
3180 return;
3181 fac_level = 10*fac_level + (*p - '0');
3182 p++;
3183 if ((p - buf) > buflen)
3184 return;
3185 }
3186
3187 *facility = fac_level >> 3;
3188 *level = fac_level & 0x7;
3189 p++;
3190
3191 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3192
3193 buflen -= p - buf;
3194 buf = p;
3195
3196 *size = buflen;
3197 *message = buf;
3198
3199 /* for rfc5424, prio is always followed by '1' and ' ' */
3200 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3201 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3202 * followed by message.
3203 * Each header field can present NILVALUE: '-'
3204 */
3205
3206 p += 2;
3207 /* timestamp is NILVALUE '-' */
3208 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3209 metadata[LOG_META_TIME] = ist2(p, 1);
3210 p++;
3211 }
3212 else if (*size > LOG_ISOTIME_MINLEN) {
3213 metadata[LOG_META_TIME].ptr = p;
3214
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003215 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003216 * in timestamp.
3217 * possible format are:
3218 * ex: '1970-01-01T00:00:00.000000Z'
3219 * '1970-01-01T00:00:00.000000+00:00'
3220 * '1970-01-01T00:00:00.000000-00:00'
3221 * '1970-01-01T00:00:00Z'
3222 * '1970-01-01T00:00:00+00:00'
3223 * '1970-01-01T00:00:00-00:00'
3224 */
3225 p += 19;
3226 if (*p == '.') {
3227 p++;
3228 if ((p - buf) >= buflen)
3229 goto bad_format;
3230 while (*p != 'Z' && *p != '+' && *p != '-') {
3231 if ((unsigned char)(*p - '0') > 9)
3232 goto bad_format;
3233
3234 p++;
3235 if ((p - buf) >= buflen)
3236 goto bad_format;
3237 }
3238 }
3239
3240 if (*p == 'Z')
3241 p++;
3242 else
3243 p += 6; /* case of '+00:00 or '-00:00' */
3244
3245 if ((p - buf) >= buflen || *p != ' ')
3246 goto bad_format;
3247 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3248 }
3249 else
3250 goto bad_format;
3251
3252
3253 p++;
3254 if ((p - buf) >= buflen || *p == ' ')
3255 goto bad_format;
3256
3257 metadata[LOG_META_HOST].ptr = p;
3258 while (*p != ' ') {
3259 p++;
3260 if ((p - buf) >= buflen)
3261 goto bad_format;
3262 }
3263 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3264 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3265 metadata[LOG_META_HOST].len = 0;
3266
3267 p++;
3268 if ((p - buf) >= buflen || *p == ' ')
3269 goto bad_format;
3270
3271 metadata[LOG_META_TAG].ptr = p;
3272 while (*p != ' ') {
3273 p++;
3274 if ((p - buf) >= buflen)
3275 goto bad_format;
3276 }
3277 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3278 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3279 metadata[LOG_META_TAG].len = 0;
3280
3281 p++;
3282 if ((p - buf) >= buflen || *p == ' ')
3283 goto bad_format;
3284
3285 metadata[LOG_META_PID].ptr = p;
3286 while (*p != ' ') {
3287 p++;
3288 if ((p - buf) >= buflen)
3289 goto bad_format;
3290 }
3291 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3292 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3293 metadata[LOG_META_PID].len = 0;
3294
3295 p++;
3296 if ((p - buf) >= buflen || *p == ' ')
3297 goto bad_format;
3298
3299 metadata[LOG_META_MSGID].ptr = p;
3300 while (*p != ' ') {
3301 p++;
3302 if ((p - buf) >= buflen)
3303 goto bad_format;
3304 }
3305 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3306 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3307 metadata[LOG_META_MSGID].len = 0;
3308
3309 p++;
3310 if ((p - buf) >= buflen || *p == ' ')
3311 goto bad_format;
3312
3313 /* structured data format is:
3314 * ex:
3315 * '[key1=value1 key2=value2][key3=value3]'
3316 *
3317 * space is invalid outside [] because
3318 * considered as the end of structured data field
3319 */
3320 metadata[LOG_META_STDATA].ptr = p;
3321 if (*p == '[') {
3322 int elem = 0;
3323
3324 while (1) {
3325 if (elem) {
3326 /* according to rfc this char is escaped in param values */
3327 if (*p == ']' && *(p-1) != '\\')
3328 elem = 0;
3329 }
3330 else {
3331 if (*p == '[')
3332 elem = 1;
3333 else if (*p == ' ')
3334 break;
3335 else
3336 goto bad_format;
3337 }
3338 p++;
3339 if ((p - buf) >= buflen)
3340 goto bad_format;
3341 }
3342 }
3343 else if (*p == '-') {
3344 /* case of NILVALUE */
3345 p++;
3346 if ((p - buf) >= buflen || *p != ' ')
3347 goto bad_format;
3348 }
3349 else
3350 goto bad_format;
3351
3352 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3353 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3354 metadata[LOG_META_STDATA].len = 0;
3355
3356 p++;
3357
3358 buflen -= p - buf;
3359 buf = p;
3360
3361 *size = buflen;
3362 *message = p;
3363 }
3364 else if (*size > LOG_LEGACYTIME_LEN) {
3365 int m;
3366
3367 /* supported header format according to rfc3164.
3368 * ex:
3369 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3370 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3371 * or 'Jan 1 00:00:00 HOSTNAME '
3372 * Note: HOSTNAME is mandatory, and day
3373 * of month uses a single space prefix if
3374 * less than 10 to ensure hour offset is
3375 * always the same.
3376 */
3377
3378 /* Check month to see if it correspond to a rfc3164
3379 * header ex 'Jan 1 00:00:00' */
3380 for (m = 0; m < 12; m++)
3381 if (!memcmp(monthname[m], p, 3))
3382 break;
3383 /* Month not found */
3384 if (m == 12)
3385 goto bad_format;
3386
3387 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3388
3389 p += LOG_LEGACYTIME_LEN;
3390 if ((p - buf) >= buflen || *p != ' ')
3391 goto bad_format;
3392
3393 p++;
3394 if ((p - buf) >= buflen || *p == ' ')
3395 goto bad_format;
3396
3397 metadata[LOG_META_HOST].ptr = p;
3398 while (*p != ' ') {
3399 p++;
3400 if ((p - buf) >= buflen)
3401 goto bad_format;
3402 }
3403 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3404
3405 /* TAG seems to no be mandatory */
3406 p++;
3407
3408 buflen -= p - buf;
3409 buf = p;
3410
3411 *size = buflen;
3412 *message = buf;
3413
3414 if (!buflen)
3415 return;
3416
3417 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3418 p++;
3419
3420 /* a tag must present a trailing ':' */
3421 if (((p - buf) >= buflen) || *p != ':')
3422 return;
3423 p++;
3424 /* followed by a space */
3425 if (((p - buf) >= buflen) || *p != ' ')
3426 return;
3427
3428 /* rewind to parse tag and pid */
3429 p = buf;
3430 metadata[LOG_META_TAG].ptr = p;
3431 /* we have the guarantee that ':' will be reach before size limit */
3432 while (*p != ':') {
3433 if (*p == '[') {
3434 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3435 metadata[LOG_META_PID].ptr = p + 1;
3436 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003437 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003438 if (p[1] != ':')
3439 return;
3440 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3441 }
3442 p++;
3443 }
3444 if (!metadata[LOG_META_TAG].len)
3445 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3446
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003447 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003448 p += 2;
3449
3450 buflen -= p - buf;
3451 buf = p;
3452
3453 *size = buflen;
3454 *message = buf;
3455 }
3456
3457 return;
3458
3459bad_format:
3460 /* bad syslog format, we reset all parsed syslog fields
3461 * but priority is kept because we are able to re-build
3462 * this message using LOF_FORMAT_PRIO.
3463 */
3464 metadata[LOG_META_TIME].len = 0;
3465 metadata[LOG_META_HOST].len = 0;
3466 metadata[LOG_META_TAG].len = 0;
3467 metadata[LOG_META_PID].len = 0;
3468 metadata[LOG_META_MSGID].len = 0;
3469 metadata[LOG_META_STDATA].len = 0;
3470
3471 return;
3472}
3473
3474/*
3475 * UDP syslog fd handler
3476 */
3477void syslog_fd_handler(int fd)
3478{
3479 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3480 ssize_t ret = 0;
3481 struct buffer *buf = get_trash_chunk();
3482 size_t size;
3483 char *message;
3484 int level;
3485 int facility;
3486 struct listener *l = objt_listener(fdtab[fd].owner);
3487 int max_accept;
3488
3489 if(!l)
3490 ABORT_NOW();
3491
Willy Tarreauf5090652021-04-06 17:23:40 +02003492 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003493
3494 if (!fd_recv_ready(fd))
3495 return;
3496
3497 max_accept = l->maxaccept ? l->maxaccept : 1;
3498
3499 do {
3500 /* Source address */
3501 struct sockaddr_storage saddr = {0};
3502 socklen_t saddrlen;
3503
3504 saddrlen = sizeof(saddr);
3505
3506 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3507 if (ret < 0) {
3508 if (errno == EINTR)
3509 continue;
3510 if (errno == EAGAIN)
3511 fd_cant_recv(fd);
3512 goto out;
3513 }
3514 buf->data = ret;
3515
Emeric Brun45c457a2020-07-09 23:23:34 +02003516 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003517 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003518 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003519
Emeric Brun54932b42020-07-07 09:43:24 +02003520 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3521
3522 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3523
3524 } while (--max_accept);
3525 }
3526
3527out:
3528 return;
3529}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003530
Emeric Brun12941c82020-07-07 14:19:42 +02003531/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003532 * IO Handler to handle message exchange with a syslog tcp client
3533 */
3534static void syslog_io_handler(struct appctx *appctx)
3535{
3536 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3537 struct stream_interface *si = appctx->owner;
3538 struct stream *s = si_strm(si);
3539 struct proxy *frontend = strm_fe(s);
3540 struct listener *l = strm_li(s);
3541 struct buffer *buf = get_trash_chunk();
3542 int max_accept;
3543 int to_skip;
3544 int facility;
3545 int level;
3546 char *message;
3547 size_t size;
3548
3549 max_accept = l->maxaccept ? l->maxaccept : 1;
3550 while (co_data(si_oc(si))) {
3551 char c;
3552
3553 if (max_accept <= 0)
3554 goto missing_budget;
3555 max_accept--;
3556
3557 to_skip = co_getchar(si_oc(si), &c);
3558 if (!to_skip)
3559 goto missing_data;
3560 else if (to_skip < 0)
3561 goto cli_abort;
3562
3563 if (c == '<') {
3564 /* rfc-6587, Non-Transparent-Framing: messages separated by
3565 * a trailing LF or CR LF
3566 */
3567 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3568 if (!to_skip)
3569 goto missing_data;
3570 else if (to_skip < 0)
3571 goto cli_abort;
3572
3573 if (buf->area[to_skip - 1] != '\n')
3574 goto parse_error;
3575
3576 buf->data = to_skip - 1;
3577
3578 /* according to rfc-6587, some devices adds CR before LF */
3579 if (buf->data && buf->area[buf->data - 1] == '\r')
3580 buf->data--;
3581
3582 }
3583 else if ((unsigned char)(c - '1') <= 8) {
3584 /* rfc-6587, Octet-Counting: message length in ASCII
3585 * (first digit can not be ZERO), followed by a space
3586 * and message length
3587 */
3588 char *p = NULL;
3589 int msglen;
3590
3591 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3592 if (!to_skip)
3593 goto missing_data;
3594 else if (to_skip < 0)
3595 goto cli_abort;
3596
3597 if (buf->area[to_skip - 1] != ' ')
3598 goto parse_error;
3599
3600 msglen = strtol(trash.area, &p, 10);
3601 if (!msglen || p != &buf->area[to_skip - 1])
3602 goto parse_error;
3603
3604 /* message seems too large */
3605 if (msglen > buf->size)
3606 goto parse_error;
3607
3608 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3609 if (!msglen)
3610 goto missing_data;
3611 else if (msglen < 0)
3612 goto cli_abort;
3613
3614
3615 buf->data = msglen;
3616 to_skip += msglen;
3617 }
3618 else
3619 goto parse_error;
3620
3621 co_skip(si_oc(si), to_skip);
3622
3623 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003624 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003625 proxy_inc_fe_req_ctr(l, frontend);
3626
3627 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3628
3629 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3630
3631 }
3632
3633missing_data:
3634 /* we need more data to read */
3635 si_oc(si)->flags |= CF_READ_DONTWAIT;
3636
3637 return;
3638
3639missing_budget:
3640 /* it may remain some stuff to do, let's retry later */
3641 appctx_wakeup(appctx);
3642
3643 return;
3644
3645parse_error:
3646 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003647 _HA_ATOMIC_INC(&l->counters->failed_req);
3648 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003649
3650 goto close;
3651
3652cli_abort:
3653 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003654 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3655 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003656
3657close:
3658 si_shutw(si);
3659 si_shutr(si);
3660
3661 si_ic(si)->flags |= CF_READ_NULL;
3662
3663 return;
3664}
3665
3666static struct applet syslog_applet = {
3667 .obj_type = OBJ_TYPE_APPLET,
3668 .name = "<SYSLOG>", /* used for logging */
3669 .fct = syslog_io_handler,
3670 .release = NULL,
3671};
3672
3673/*
Emeric Brun12941c82020-07-07 14:19:42 +02003674 * Parse "log-forward" section and create corresponding sink buffer.
3675 *
3676 * The function returns 0 in success case, otherwise, it returns error
3677 * flags.
3678 */
3679int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3680{
3681 int err_code = 0;
3682 struct proxy *px;
3683 char *errmsg = NULL;
3684 const char *err = NULL;
3685
3686 if (strcmp(args[0], "log-forward") == 0) {
3687 if (!*args[1]) {
3688 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3689 err_code |= ERR_ALERT | ERR_ABORT;
3690 goto out;
3691 }
3692
3693 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3694 goto out;
3695
3696 err = invalid_char(args[1]);
3697 if (err) {
3698 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3699 file, linenum, *err, args[0], args[1]);
3700 err_code |= ERR_ALERT | ERR_ABORT;
3701 goto out;
3702 }
3703
Emeric Brunb0c331f2020-10-07 17:05:59 +02003704 px = log_forward_by_name(args[1]);
3705 if (px) {
3706 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3707 file, linenum, args[1], px->conf.file, px->conf.line);
3708 err_code |= ERR_ALERT | ERR_FATAL;
3709 }
3710
3711 px = proxy_find_by_name(args[1], 0, 0);
3712 if (px) {
3713 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3714 file, linenum, args[1], proxy_type_str(px),
3715 px->id, px->conf.file, px->conf.line);
3716 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003717 }
3718
3719 px = calloc(1, sizeof *px);
3720 if (!px) {
3721 err_code |= ERR_ALERT | ERR_FATAL;
3722 goto out;
3723 }
3724
3725 px->next = cfg_log_forward;
3726 cfg_log_forward = px;
3727
3728 init_new_proxy(px);
3729 px->conf.file = strdup(file);
3730 px->conf.line = linenum;
3731 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003732 px->last_change = now.tv_sec;
3733 px->cap = PR_CAP_FE;
3734 px->maxconn = 10;
3735 px->timeout.client = TICK_ETERNITY;
3736 px->accept = frontend_accept;
3737 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003738 px->id = strdup(args[1]);
3739
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003740 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003741 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003742 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3743 err_code |= ERR_WARN;
3744
3745 if (*(args[1]) == 0) {
3746 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3747 err_code |= ERR_ALERT | ERR_FATAL;
3748 goto out;
3749 }
3750 cfg_log_forward->maxconn = atol(args[1]);
3751 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3752 goto out;
3753 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003754 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003755 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3756 err_code |= ERR_WARN;
3757
3758 if (*(args[1]) == 0) {
3759 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3760 err_code |= ERR_ALERT | ERR_FATAL;
3761 goto out;
3762 }
3763 cfg_log_forward->backlog = atol(args[1]);
3764 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3765 goto out;
3766 }
3767 else if (strcmp(args[0], "bind") == 0) {
3768 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003769 struct bind_conf *bind_conf;
3770 struct bind_kw *kw;
3771 struct listener *l;
3772
3773 cur_arg = 1;
3774
3775 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3776 NULL, xprt_get(XPRT_RAW));
3777 if (!bind_conf) {
3778 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3779 err_code |= ERR_ALERT | ERR_FATAL;
3780 goto out;
3781 }
3782
3783 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3784 if (errmsg && *errmsg) {
3785 indent_msg(&errmsg, 2);
3786 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3787 }
3788 else {
3789 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3790 file, linenum, args[0], args[1], args[2]);
3791 err_code |= ERR_ALERT | ERR_FATAL;
3792 goto out;
3793 }
3794 }
3795 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau66161322021-02-19 15:50:27 +01003796 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003797 l->accept = session_accept_fd;
3798 l->analysers |= cfg_log_forward->fe_req_ana;
3799 l->default_target = cfg_log_forward->default_target;
3800 global.maxsock++;
3801 }
3802 cur_arg++;
3803
3804 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3805 int ret;
3806
3807 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3808 err_code |= ret;
3809 if (ret) {
3810 if (errmsg && *errmsg) {
3811 indent_msg(&errmsg, 2);
3812 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3813 }
3814 else
3815 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3816 file, linenum, args[cur_arg]);
3817 if (ret & ERR_FATAL)
3818 goto out;
3819 }
3820 cur_arg += 1 + kw->skip;
3821 }
3822 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003823 const char *best = bind_find_best_kw(args[cur_arg]);
3824 if (best)
3825 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3826 file, linenum, args[cur_arg], cursection, best);
3827 else
3828 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3829 file, linenum, args[cur_arg], cursection);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003830 err_code |= ERR_ALERT | ERR_FATAL;
3831 goto out;
3832 }
Emeric Brun12941c82020-07-07 14:19:42 +02003833 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003834 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003835 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02003836 struct bind_conf *bind_conf;
3837 struct bind_kw *kw;
3838 struct listener *l;
3839
3840 cur_arg = 1;
3841
3842 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3843 NULL, xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +02003844 if (!bind_conf) {
3845 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3846 err_code |= ERR_ALERT | ERR_FATAL;
3847 goto out;
3848 }
Emeric Brun12941c82020-07-07 14:19:42 +02003849
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003850 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003851 if (errmsg && *errmsg) {
3852 indent_msg(&errmsg, 2);
3853 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3854 }
3855 else {
3856 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3857 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003858 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003859 err_code |= ERR_ALERT | ERR_FATAL;
3860 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003861 }
3862 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003863 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreau66161322021-02-19 15:50:27 +01003864 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreaue140a692020-10-15 21:25:32 +02003865 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003866 global.maxsock++;
3867 }
3868 cur_arg++;
3869
3870 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3871 int ret;
3872
3873 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3874 err_code |= ret;
3875 if (ret) {
3876 if (errmsg && *errmsg) {
3877 indent_msg(&errmsg, 2);
3878 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3879 }
3880 else
3881 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3882 file, linenum, args[cur_arg]);
3883 if (ret & ERR_FATAL)
3884 goto out;
3885 }
3886 cur_arg += 1 + kw->skip;
3887 }
3888 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003889 const char *best = bind_find_best_kw(args[cur_arg]);
3890 if (best)
3891 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3892 file, linenum, args[cur_arg], cursection, best);
3893 else
3894 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3895 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02003896 err_code |= ERR_ALERT | ERR_FATAL;
3897 goto out;
3898 }
3899 }
3900 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02003901 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003902 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3903 err_code |= ERR_ALERT | ERR_FATAL;
3904 goto out;
3905 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003906 }
3907 else if (strcmp(args[0], "timeout") == 0) {
3908 const char *res;
3909 unsigned timeout;
3910
3911 if (strcmp(args[1], "client") != 0) {
3912 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
3913 err_code |= ERR_ALERT | ERR_FATAL;
3914 goto out;
3915 }
3916
3917 if (*args[2] == 0) {
3918 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
3919 err_code |= ERR_ALERT | ERR_FATAL;
3920 goto out;
3921 }
3922 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3923 if (res == PARSE_TIME_OVER) {
3924 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
3925 }
3926 else if (res == PARSE_TIME_UNDER) {
3927 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
3928 }
3929 else if (res) {
3930 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
3931 return -1;
3932 }
3933
3934 if (res) {
3935 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3936 err_code |= ERR_ALERT | ERR_FATAL;
3937 goto out;
3938 }
3939 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02003940 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02003941 else {
3942 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
3943 err_code |= ERR_ALERT | ERR_ABORT;
3944 goto out;
3945 }
Emeric Brun12941c82020-07-07 14:19:42 +02003946out:
3947 return err_code;
3948}
3949
Willy Tarreau0108d902018-11-25 19:14:37 +01003950
Emeric Brun12941c82020-07-07 14:19:42 +02003951/* config parsers for this section */
3952REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
3953
Willy Tarreau082b6282019-05-22 14:42:12 +02003954REGISTER_PER_THREAD_ALLOC(init_log_buffers);
3955REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01003956
Willy Tarreaubaaee002006-06-26 02:48:02 +02003957/*
3958 * Local variables:
3959 * c-indent-level: 8
3960 * c-basic-offset: 8
3961 * End:
3962 */