blob: 5cae18293d9eb2e9cde871f6a7cd688d8e00b804 [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
Remi Tricot-Le Breton98b930d2021-07-29 09:45:52 +0200196char default_https_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 %[fc_conn_err]/%[ssl_fc_hsk_err,hex]/%[ssl_c_err]/%[ssl_c_ca_err] %sslv/%sslc";
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200197char 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 +0100198char 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 +0100199char *log_format = NULL;
200
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200201/* Default string used for structured-data part in RFC5424 formatted
202 * syslog messages.
203 */
204char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200205
Willy Tarreau13ef7732018-11-12 07:25:28 +0100206/* total number of dropped logs */
207unsigned int dropped_logs = 0;
208
Dragan Dosen59cee972015-09-19 22:09:02 +0200209/* This is a global syslog message buffer, common to all outgoing
210 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100211 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200212THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100213
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200214/* A global syslog message buffer, common to all RFC5424 syslog messages.
215 * Currently, it is used for generating the structured-data part.
216 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200217THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200218
William Lallemand723b73a2012-02-08 16:37:49 +0100219struct logformat_var_args {
220 char *name;
221 int mask;
222};
223
224struct logformat_var_args var_args_list[] = {
225// global
226 { "M", LOG_OPT_MANDATORY },
227 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200228 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100229 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100230 { 0, 0 }
231};
232
233/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100234 * callback used to configure addr source retrieval
235 */
236int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
237{
238 curproxy->options2 |= PR_O2_SRC_ADDR;
239
240 return 0;
241}
242
243
244/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100245 * Parse args in a logformat_var. Returns 0 in error
246 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100247 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100248int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100249{
250 int i = 0;
251 int end = 0;
252 int flags = 0; // 1 = + 2 = -
253 char *sp = NULL; // start pointer
254
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100255 if (args == NULL) {
256 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100257 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100258 }
William Lallemand723b73a2012-02-08 16:37:49 +0100259
260 while (1) {
261 if (*args == '\0')
262 end = 1;
263
264 if (*args == '+') {
265 // add flag
266 sp = args + 1;
267 flags = 1;
268 }
269 if (*args == '-') {
270 // delete flag
271 sp = args + 1;
272 flags = 2;
273 }
274
275 if (*args == '\0' || *args == ',') {
276 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100277 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100278 if (strcmp(sp, var_args_list[i].name) == 0) {
279 if (flags == 1) {
280 node->options |= var_args_list[i].mask;
281 break;
282 } else if (flags == 2) {
283 node->options &= ~var_args_list[i].mask;
284 break;
285 }
286 }
287 }
288 sp = NULL;
289 if (end)
290 break;
291 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100292 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100293 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100294 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100295}
296
297/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100298 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
299 * must pass the args part in the <arg> pointer with its length in <arg_len>,
300 * and varname with its length in <var> and <var_len> respectively. <arg> is
301 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100302 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100303 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100304int 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 +0100305{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100306 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200307 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100308
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100309 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
310 if (strlen(logformat_keywords[j].name) == var_len &&
311 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
312 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200313 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100314 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100315 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200316 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100317 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100318 node->type = logformat_keywords[j].type;
319 node->options = *defoptions;
320 if (arg_len) {
321 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100322 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200323 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100324 }
325 if (node->type == LOG_FMT_GLOBAL) {
326 *defoptions = node->options;
327 free(node->arg);
328 free(node);
329 } else {
330 if (logformat_keywords[j].config_callback &&
331 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200332 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100333 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100334 curproxy->to_log |= logformat_keywords[j].lw;
Willy Tarreau2b718102021-04-21 07:32:39 +0200335 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100336 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100337 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100338 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100339 memprintf(err, "format variable '%s' is reserved for HTTP mode",
340 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200341 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100342 }
William Lallemand723b73a2012-02-08 16:37:49 +0100343 }
344 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100345
346 j = var[var_len];
347 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100348 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 +0100349 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200350
351 error_free:
352 if (node) {
353 free(node->arg);
354 free(node);
355 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100356 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100357}
358
359/*
360 * push to the logformat linked list
361 *
362 * start: start pointer
363 * end: end text pointer
364 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100365 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100366 *
367 * LOG_TEXT: copy chars from start to end excluding end.
368 *
369*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100370int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100371{
372 char *str;
373
Willy Tarreaua3571662012-12-20 21:59:12 +0100374 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200375 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100376 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100377 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100378 return 0;
379 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200380 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100381 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100382 str[end - start] = '\0';
383 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100384 node->type = LOG_FMT_TEXT; // type string
Willy Tarreau2b718102021-04-21 07:32:39 +0200385 LIST_APPEND(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100386 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200387 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100388 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100389 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100390 return 0;
391 }
William Lallemand1d705562012-03-12 12:46:41 +0100392 node->type = LOG_FMT_SEPARATOR;
Willy Tarreau2b718102021-04-21 07:32:39 +0200393 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100394 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100395 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100396}
397
398/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100399 * Parse the sample fetch expression <text> and add a node to <list_format> upon
400 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100401 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
402 * is passed in <endptr>, it will be updated with the pointer to the first character
403 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100404 *
405 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100406 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100407int 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 +0100408{
409 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200410 struct sample_expr *expr = NULL;
411 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100412 int cmd_arg;
413
414 cmd[0] = text;
415 cmd[1] = "";
416 cmd_arg = 0;
417
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100418 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 +0100419 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100420 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200421 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100422 }
423
Vincent Bernat02779b62016-04-03 13:48:43 +0200424 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100425 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100426 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200427 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100428 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100429 node->type = LOG_FMT_EXPR;
430 node->expr = expr;
431 node->options = options;
432
433 if (arg_len) {
434 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100435 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200436 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100437 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100438 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100439 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
440
Willy Tarreau434c57c2013-01-08 01:10:24 +0100441 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100442 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
443
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100444 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100445 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
446 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200447 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100448 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100449
Christopher Faulet581db2b2021-03-26 10:02:46 +0100450 if ((options & LOG_OPT_HTTP) && (expr->fetch->use & (SMP_USE_L6REQ|SMP_USE_L6RES))) {
451 ha_warning("parsing [%s:%d] : L6 sample fetch <%s> ignored in HTTP log-format string.\n",
452 curpx->conf.args.file, curpx->conf.args.line, text);
453 }
454
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200455 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100456 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100457 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100458
William Lallemand65ad6e12014-01-31 15:08:02 +0100459 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
460 * needed with some sample fetches (eg: ssl*). We always set it for
461 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100462 */
463 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200464 if (curpx->http_needed)
465 curpx->to_log |= LW_REQ;
Willy Tarreau2b718102021-04-21 07:32:39 +0200466 LIST_APPEND(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100467 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200468
469 error_free:
470 release_sample_expr(expr);
471 if (node) {
472 free(node->arg);
473 free(node);
474 }
475 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100476}
477
478/*
William Lallemand723b73a2012-02-08 16:37:49 +0100479 * Parse the log_format string and fill a linked list.
480 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200481 * You can set arguments using { } : %{many arguments}varname.
482 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100483 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500484 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100485 * curproxy: the proxy affected
486 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100487 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100488 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100489 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100490 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100491 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100492int 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 +0100493{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100494 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100495 char *arg = NULL; /* start pointer for args */
496 char *var = NULL; /* start pointer for vars */
497 int arg_len = 0;
498 int var_len = 0;
499 int cformat; /* current token format */
500 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100501 struct logformat_node *tmplf, *back;
502
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100503 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100504 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100505 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100506 return 0;
507 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200508 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200509
William Lallemand723b73a2012-02-08 16:37:49 +0100510 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100511 list_for_each_entry_safe(tmplf, back, list_format, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200512 LIST_DELETE(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200513 release_sample_expr(tmplf->expr);
514 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100515 free(tmplf);
516 }
517
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100518 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100519 pformat = cformat;
520
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100521 if (!*str)
522 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100523
Joseph Herlant85b40592018-11-15 12:10:04 -0800524 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100525 * second have all common paths processed at one place. The common paths are the ones
526 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
527 * We use the common LF_INIT state to dispatch to the different final states.
528 */
529 switch (pformat) {
530 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100531 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100532 arg_len = var_len = 0;
533 if (*str == '{') { // optional argument
534 cformat = LF_STARG;
535 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100536 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100537 else if (*str == '[') {
538 cformat = LF_STEXPR;
539 var = str + 1; // store expr in variable name
540 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100541 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100542 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100543 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100544 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100545 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500546 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100547 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100548 /* single '%' followed by blank or digit, send them both */
549 cformat = LF_TEXT;
550 pformat = LF_TEXT; /* finally we include the previous char as well */
551 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600552 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 +0100553 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100554 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100555
556 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100557 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500558 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100559 break;
560
561 case LF_STARG: // text immediately following '%{'
562 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100563 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100564 arg_len = str - arg;
565 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100566 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100567 break;
568
569 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100570 if (*str == '[') {
571 cformat = LF_STEXPR;
572 var = str + 1; // store expr in variable name
573 break;
574 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100575 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100576 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100577 var = str;
578 break;
579 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100580 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100581 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100582
Willy Tarreauc8368452012-12-21 00:09:23 +0100583 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100584 /* the whole sample expression is parsed at once,
585 * returning the pointer to the first character not
586 * part of the expression, which MUST be the trailing
587 * angle bracket.
588 */
589 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
590 goto fail;
591
592 if (*str == ']') {
593 // end of arg, go on with next state
594 cformat = pformat = LF_EDEXPR;
595 sp = str;
596 }
597 else {
598 char c = *str;
599 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100600 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100601 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
602 else
603 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200604 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100605 }
606 break;
607
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100608 case LF_VAR: // text part of a variable name
609 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100610 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100611 cformat = LF_INIT; // not variable name anymore
612 break;
613
Willy Tarreauc8368452012-12-21 00:09:23 +0100614 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100615 cformat = LF_INIT;
616 }
617
618 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
619 switch (*str) {
620 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100621 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200622 case ' ':
623 if (options & LOG_OPT_MERGE_SPACES) {
624 cformat = LF_SEPARATOR;
625 break;
626 }
627 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100628 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100629 }
630 }
631
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100632 if (cformat != pformat || pformat == LF_SEPARATOR) {
633 switch (pformat) {
634 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100635 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100636 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100637 break;
638 case LF_TEXT:
639 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100640 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100641 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100642 break;
643 }
644 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100645 }
646 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100647
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100648 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100649 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100650 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100651 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100652 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100653
654 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100655 fail:
656 free(backfmt);
657 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100658}
659
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200660/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500661 * 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 +0200662 * ranges of indexes. Note that an index may be considered as a particular range
663 * with a high limit to the low limit.
664 */
665int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
666{
667 char *end, *p;
668
669 *low = *high = 0;
670
671 p = *arg;
672 end = strchr(p, ',');
673 if (!end)
674 end = p + strlen(p);
675
676 *high = *low = read_uint((const char **)&p, end);
677 if (!*low || (p != end && *p != '-'))
678 goto err;
679
680 if (p == end)
681 goto done;
682
683 p++;
684 *high = read_uint((const char **)&p, end);
685 if (!*high || *high <= *low || p != end)
686 goto err;
687
688 done:
689 if (*end == ',')
690 end++;
691 *arg = end;
692 return 1;
693
694 err:
695 memprintf(err, "wrong sample range '%s'", *arg);
696 return 0;
697}
698
699/*
700 * Returns 1 if the range defined by <low> and <high> overlaps
701 * one of them in <rgs> array of ranges with <sz> the size of this
702 * array, 0 if not.
703 */
704int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
705 unsigned int low, unsigned int high, char **err)
706{
707 size_t i;
708
709 for (i = 0; i < sz; i++) {
710 if ((low >= rgs[i].low && low <= rgs[i].high) ||
711 (high >= rgs[i].low && high <= rgs[i].high)) {
712 memprintf(err, "ranges are overlapping");
713 return 1;
714 }
715 }
716
717 return 0;
718}
719
720int smp_log_range_cmp(const void *a, const void *b)
721{
722 const struct smp_log_range *rg_a = a;
723 const struct smp_log_range *rg_b = b;
724
725 if (rg_a->high < rg_b->low)
726 return -1;
727 else if (rg_a->low > rg_b->high)
728 return 1;
729
730 return 0;
731}
732
733/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200734 * Parse "log" keyword and update <logsrvs> list accordingly.
735 *
736 * When <do_del> is set, it means the "no log" line was parsed, so all log
737 * servers in <logsrvs> are released.
738 *
739 * Otherwise, we try to parse the "log" line. First of all, when the list is not
740 * the global one, we look for the parameter "global". If we find it,
741 * global.logsrvs is copied. Else we parse each arguments.
742 *
743 * The function returns 1 in success case, otherwise, it returns 0 and err is
744 * filled.
745 */
Emeric Brun9533a702021-04-02 10:13:43 +0200746int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200747{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100748 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200749 struct sockaddr_storage *sk;
Emeric Brun94aab062021-04-02 10:41:36 +0200750 struct protocol *proto;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200751 struct logsrv *logsrv = NULL;
752 int port1, port2;
753 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200754 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200755
756 /*
757 * "no log": delete previous herited or defined syslog
758 * servers.
759 */
760 if (do_del) {
761 struct logsrv *back;
762
763 if (*(args[1]) != 0) {
764 memprintf(err, "'no log' does not expect arguments");
765 goto error;
766 }
767
768 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200769 LIST_DELETE(&logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200770 free(logsrv);
771 }
772 return 1;
773 }
774
775 /*
776 * "log global": copy global.logrsvs linked list to the end of logsrvs
777 * list. But first, we check (logsrvs != global.logsrvs).
778 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100779 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200780 if (logsrvs == &global.logsrvs) {
781 memprintf(err, "'global' is not supported for a global syslog server");
782 goto error;
783 }
784 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200785 struct logsrv *node;
786
787 list_for_each_entry(node, logsrvs, list) {
788 if (node->ref == logsrv)
789 goto skip_logsrv;
790 }
791
792 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200793 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200794 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200795 LIST_INIT(&node->list);
Willy Tarreau2b718102021-04-21 07:32:39 +0200796 LIST_APPEND(logsrvs, &node->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200797 node->conf.file = strdup(file);
798 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200799
800 skip_logsrv:
801 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200802 }
803 return 1;
804 }
805
806 /*
807 * "log <address> ...: parse a syslog server line
808 */
809 if (*(args[1]) == 0 || *(args[2]) == 0) {
810 memprintf(err, "expects <address> and <facility> %s as arguments",
811 ((logsrvs == &global.logsrvs) ? "" : "or global"));
812 goto error;
813 }
814
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100815 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
816 if (strcmp(args[1], "stdout") == 0)
817 args[1] = "fd@1";
818 else if (strcmp(args[1], "stderr") == 0)
819 args[1] = "fd@2";
820
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200821 logsrv = calloc(1, sizeof(*logsrv));
822 if (!logsrv) {
823 memprintf(err, "out of memory");
824 goto error;
825 }
826
Emeric Brun9533a702021-04-02 10:13:43 +0200827 logsrv->conf.file = strdup(file);
828 logsrv->conf.line = linenum;
829
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200830 /* skip address for now, it will be parsed at the end */
831 cur_arg = 2;
832
833 /* just after the address, a length may be specified */
834 logsrv->maxlen = MAX_SYSLOG_LEN;
835 if (strcmp(args[cur_arg], "len") == 0) {
836 int len = atoi(args[cur_arg+1]);
837 if (len < 80 || len > 65535) {
838 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
839 args[cur_arg+1]);
840 goto error;
841 }
842 logsrv->maxlen = len;
843 cur_arg += 2;
844 }
845 if (logsrv->maxlen > global.max_syslog_len)
846 global.max_syslog_len = logsrv->maxlen;
847
848 /* after the length, a format may be specified */
849 if (strcmp(args[cur_arg], "format") == 0) {
850 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200851 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200852 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
853 goto error;
854 }
855 cur_arg += 2;
856 }
857
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200858 if (strcmp(args[cur_arg], "sample") == 0) {
859 unsigned low, high;
860 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200861 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
862
863 p = args[cur_arg+1];
864 smp_sz_str = strchr(p, ':');
865 if (!smp_sz_str) {
866 memprintf(err, "Missing sample size");
867 goto error;
868 }
869
870 *smp_sz_str++ = '\0';
871
872 end = p + strlen(p);
873
874 while (p != end) {
875 if (!get_logsrv_smp_range(&low, &high, &p, err))
876 goto error;
877
878 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
879 goto error;
880
881 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
882 if (!smp_rgs) {
883 memprintf(err, "out of memory error");
884 goto error;
885 }
886
887 smp_rgs[smp_rgs_sz].low = low;
888 smp_rgs[smp_rgs_sz].high = high;
889 smp_rgs[smp_rgs_sz].sz = high - low + 1;
890 smp_rgs[smp_rgs_sz].curr_idx = 0;
891 if (smp_rgs[smp_rgs_sz].high > smp_sz)
892 smp_sz = smp_rgs[smp_rgs_sz].high;
893 smp_rgs_sz++;
894 }
895
Tim Duesterhus21648002019-06-23 22:10:10 +0200896 if (smp_rgs == NULL) {
897 memprintf(err, "no sampling ranges given");
898 goto error;
899 }
900
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200901 beg = smp_sz_str;
902 end = beg + strlen(beg);
903 new_smp_sz = read_uint((const char **)&beg, end);
904 if (!new_smp_sz || beg != end) {
905 memprintf(err, "wrong sample size '%s' for sample range '%s'",
906 smp_sz_str, args[cur_arg+1]);
907 goto error;
908 }
909
910 if (new_smp_sz < smp_sz) {
911 memprintf(err, "sample size %zu should be greater or equal to "
912 "%zu the maximum of the high ranges limits",
913 new_smp_sz, smp_sz);
914 goto error;
915 }
916 smp_sz = new_smp_sz;
917
918 /* Let's order <smp_rgs> array. */
919 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
920
921 logsrv->lb.smp_rgs = smp_rgs;
922 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
923 logsrv->lb.smp_sz = smp_sz;
924
925 cur_arg += 2;
926 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200927 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200928 /* parse the facility */
929 logsrv->facility = get_log_facility(args[cur_arg]);
930 if (logsrv->facility < 0) {
931 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
932 goto error;
933 }
934 cur_arg++;
935
936 /* parse the max syslog level (default: debug) */
937 logsrv->level = 7;
938 if (*(args[cur_arg])) {
939 logsrv->level = get_log_level(args[cur_arg]);
940 if (logsrv->level < 0) {
941 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
942 goto error;
943 }
944 cur_arg++;
945 }
946
947 /* parse the limit syslog level (default: emerg) */
948 logsrv->minlvl = 0;
949 if (*(args[cur_arg])) {
950 logsrv->minlvl = get_log_level(args[cur_arg]);
951 if (logsrv->minlvl < 0) {
952 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
953 goto error;
954 }
955 cur_arg++;
956 }
957
958 /* Too many args */
959 if (*(args[cur_arg])) {
960 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
961 goto error;
962 }
963
964 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +0200965 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +0200966 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +0200967 logsrv->addr.ss_family = AF_UNSPEC;
968 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +0200969 logsrv->sink = NULL;
970 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +0200971 goto done;
972 }
973
Emeric Brun94aab062021-04-02 10:41:36 +0200974 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
975 err, NULL, NULL,
976 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 +0200977 if (!sk)
978 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +0200979
980 if (fd != -1)
981 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200982 logsrv->addr = *sk;
983
984 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200985 if (!port1)
986 set_host_port(&logsrv->addr, SYSLOG_PORT);
987 }
Emeric Brun9533a702021-04-02 10:13:43 +0200988
Emeric Brun26754902021-04-07 14:26:44 +0200989 if (proto && proto->ctrl_type == SOCK_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +0200990 static unsigned long ring_ids;
991
992 /* Implicit sink buffer will be
993 * initialized in post_check
994 */
995 logsrv->type = LOG_TARGET_BUFFER;
996 logsrv->sink = NULL;
997 /* compute uniq name for the ring */
998 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
999 }
1000
Willy Tarreauc046d162019-08-30 15:24:59 +02001001 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001002 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001003 return 1;
1004
1005 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001006 free(smp_rgs);
Emeric Brun9533a702021-04-02 10:13:43 +02001007 if (logsrv) {
1008 free(logsrv->conf.file);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001009 free(logsrv->ring_name);
Emeric Brun9533a702021-04-02 10:13:43 +02001010 }
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001011 free(logsrv);
1012 return 0;
1013}
1014
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001015
1016/*
Emeric Brun54648852020-07-06 15:54:06 +02001017 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001018 */
Emeric Brun54648852020-07-06 15:54:06 +02001019enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001020{
Emeric Brun54648852020-07-06 15:54:06 +02001021 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001022
1023 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001024 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001025 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001026 format--;
1027
Emeric Brun54648852020-07-06 15:54:06 +02001028 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001029 return format;
1030}
1031
1032/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001033 * returns log level for <lev> or -1 if not found.
1034 */
1035int get_log_level(const char *lev)
1036{
1037 int level;
1038
1039 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001040 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001041 level--;
1042
1043 return level;
1044}
1045
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046/*
1047 * returns log facility for <fac> or -1 if not found.
1048 */
1049int get_log_facility(const char *fac)
1050{
1051 int facility;
1052
1053 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001054 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001055 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001056
Willy Tarreaubaaee002006-06-26 02:48:02 +02001057 return facility;
1058}
1059
William Lallemanda1cc3812012-02-08 16:38:44 +01001060/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001061 * Encode the string.
1062 *
1063 * When using the +E log format option, it will try to escape '"\]'
1064 * characters with '\' as prefix. The same prefix should not be used as
1065 * <escape>.
1066 */
1067static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001068 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001069 const char *string,
1070 struct logformat_node *node)
1071{
1072 if (node->options & LOG_OPT_ESC) {
1073 if (start < stop) {
1074 stop--; /* reserve one byte for the final '\0' */
1075 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001076 if (!ha_bit_test((unsigned char)(*string), map)) {
1077 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001078 *start++ = *string;
1079 else {
1080 if (start + 2 >= stop)
1081 break;
1082 *start++ = '\\';
1083 *start++ = *string;
1084 }
1085 }
1086 else {
1087 if (start + 3 >= stop)
1088 break;
1089 *start++ = escape;
1090 *start++ = hextab[(*string >> 4) & 15];
1091 *start++ = hextab[*string & 15];
1092 }
1093 string++;
1094 }
1095 *start = '\0';
1096 }
1097 }
1098 else {
1099 return encode_string(start, stop, escape, map, string);
1100 }
1101
1102 return start;
1103}
1104
1105/*
1106 * Encode the chunk.
1107 *
1108 * When using the +E log format option, it will try to escape '"\]'
1109 * characters with '\' as prefix. The same prefix should not be used as
1110 * <escape>.
1111 */
1112static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001113 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001114 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001115 struct logformat_node *node)
1116{
1117 char *str, *end;
1118
1119 if (node->options & LOG_OPT_ESC) {
1120 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001121 str = chunk->area;
1122 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001123
1124 stop--; /* reserve one byte for the final '\0' */
1125 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001126 if (!ha_bit_test((unsigned char)(*str), map)) {
1127 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001128 *start++ = *str;
1129 else {
1130 if (start + 2 >= stop)
1131 break;
1132 *start++ = '\\';
1133 *start++ = *str;
1134 }
1135 }
1136 else {
1137 if (start + 3 >= stop)
1138 break;
1139 *start++ = escape;
1140 *start++ = hextab[(*str >> 4) & 15];
1141 *start++ = hextab[*str & 15];
1142 }
1143 str++;
1144 }
1145 *start = '\0';
1146 }
1147 }
1148 else {
1149 return encode_chunk(start, stop, escape, map, chunk);
1150 }
1151
1152 return start;
1153}
1154
1155/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001156 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001157 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001158 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001159 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001160 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001161char *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 +01001162{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001163 if (size < 2)
1164 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001165
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001166 if (node->options & LOG_OPT_QUOTE) {
1167 *(dst++) = '"';
1168 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001169 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001170
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001171 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001172 if (++len > size)
1173 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001174 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001175 char *ret;
1176
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001177 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001178 if (ret == NULL || *ret != '\0')
1179 return NULL;
1180 len = ret - dst;
1181 }
1182 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001183 len = strlcpy2(dst, src, len);
1184 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001185
1186 size -= len;
1187 dst += len;
1188 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001189 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1190 if (size < 2)
1191 return NULL;
1192 *(dst++) = '-';
1193 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001194
1195 if (node->options & LOG_OPT_QUOTE) {
1196 if (size < 2)
1197 return NULL;
1198 *(dst++) = '"';
1199 }
1200
1201 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001202 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001203}
1204
Willy Tarreau26ffa852018-09-05 15:23:10 +02001205static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001206{
1207 return lf_text_len(dst, src, size, size, node);
1208}
1209
William Lallemand5f232402012-04-05 18:02:55 +02001210/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001211 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001212 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001213 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001214char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001215{
1216 char *ret = dst;
1217 int iret;
1218 char pn[INET6_ADDRSTRLEN];
1219
1220 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001221 unsigned char *addr = NULL;
1222 switch (sockaddr->sa_family) {
1223 case AF_INET:
1224 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1225 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1226 break;
1227 case AF_INET6:
1228 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1229 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1230 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1231 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1232 break;
1233 default:
1234 return NULL;
1235 }
William Lallemand5f232402012-04-05 18:02:55 +02001236 if (iret < 0 || iret > size)
1237 return NULL;
1238 ret += iret;
1239 } else {
1240 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1241 ret = lf_text(dst, pn, size, node);
1242 if (ret == NULL)
1243 return NULL;
1244 }
1245 return ret;
1246}
1247
1248/*
1249 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001250 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001251 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001252char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001253{
1254 char *ret = dst;
1255 int iret;
1256
1257 if (node->options & LOG_OPT_HEXA) {
1258 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1259 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1260 if (iret < 0 || iret > size)
1261 return NULL;
1262 ret += iret;
1263 } else {
1264 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1265 if (ret == NULL)
1266 return NULL;
1267 }
1268 return ret;
1269}
1270
Emeric Brun54648852020-07-06 15:54:06 +02001271
1272/*
1273 * This function sends the syslog message using a printf format string. It
1274 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001275 */
Emeric Brun54648852020-07-06 15:54:06 +02001276void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001277{
Emeric Brun54648852020-07-06 15:54:06 +02001278 va_list argp;
1279 int data_len;
1280
1281 if (level < 0 || format == NULL || logline == NULL)
1282 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001283
Emeric Brun54648852020-07-06 15:54:06 +02001284 va_start(argp, format);
1285 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1286 if (data_len < 0 || data_len > global.max_syslog_len)
1287 data_len = global.max_syslog_len;
1288 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001289
Emeric Brun54648852020-07-06 15:54:06 +02001290 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1291 logline, data_len, default_rfc5424_sd_log_format, 2);
1292}
1293/*
1294 * This function builds a log header of given format using given
1295 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1296 * to determine format based on given metadas. It is useful
1297 * for log-forwarding to be able to forward any format without
1298 * settings.
1299 * This function returns a struct ist array of elements of the header
1300 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001301 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001302 * elements.
1303 */
1304struct ist *build_log_header(enum log_fmt format, int level, int facility,
1305 struct ist *metadata, size_t *nbelem)
1306{
1307 static THREAD_LOCAL struct {
1308 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1309 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1310 time_t cur_legacy_time;
1311 char priority_buffer[6];
1312 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313
Emeric Brun54648852020-07-06 15:54:06 +02001314 struct tm logtime;
1315 int len;
1316 int fac_level = 0;
1317 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001318
Emeric Brun54648852020-07-06 15:54:06 +02001319 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001320
Emeric Brun54648852020-07-06 15:54:06 +02001321
1322 if (format == LOG_FORMAT_UNSPEC) {
1323 format = LOG_FORMAT_RAW;
1324 if (metadata) {
1325 /* If a hostname is set, it appears we want to perform syslog
1326 * because only rfc5427 or rfc3164 support an hostname.
1327 */
1328 if (metadata[LOG_META_HOST].len) {
1329 /* If a rfc5424 compliant timestamp is used we consider
1330 * that output format is rfc5424, else legacy format
1331 * is used as specified default for local logs
1332 * in documentation.
1333 */
1334 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1335 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1336 format = LOG_FORMAT_RFC5424;
1337 else
1338 format = LOG_FORMAT_RFC3164;
1339 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001340 else if (metadata[LOG_META_TAG].len) {
1341 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001342 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001343 * in legacy format (analog to RFC3164 but
1344 * with stripped hostname).
1345 */
1346 format = LOG_FORMAT_LOCAL;
1347 }
Emeric Brun54648852020-07-06 15:54:06 +02001348 else if (metadata[LOG_META_PRIO].len) {
1349 /* the source seems a parsed message
1350 * offering a valid level/prio prefix
1351 * so we consider this format.
1352 */
1353 format = LOG_FORMAT_PRIO;
1354 }
1355 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001356 }
1357
Emeric Brun54648852020-07-06 15:54:06 +02001358 /* prepare priority, stored into 1 single elem */
1359 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001360 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001361 case LOG_FORMAT_RFC3164:
1362 case LOG_FORMAT_RFC5424:
1363 case LOG_FORMAT_PRIO:
1364 fac_level = facility << 3;
1365 /* further format ignore the facility */
1366 /* fall through */
1367 case LOG_FORMAT_TIMED:
1368 case LOG_FORMAT_SHORT:
1369 fac_level += level;
1370 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1371 do {
1372 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1373 fac_level /= 10;
1374 hdr_ctx.ist_vector[*nbelem].ptr--;
1375 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1376 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1377 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1378 break;
1379 case LOG_FORMAT_ISO:
1380 case LOG_FORMAT_RAW:
1381 break;
1382 case LOG_FORMAT_UNSPEC:
1383 case LOG_FORMATS:
1384 ABORT_NOW();
1385 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001386
William Lallemand2a4a44f2012-02-06 16:00:33 +01001387
Emeric Brun54648852020-07-06 15:54:06 +02001388 /* prepare timestamp, stored into a max of 4 elems */
1389 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001390 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001391 case LOG_FORMAT_RFC3164:
1392 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1393 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1394 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1395 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001396 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001397 break;
1398 }
1399 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1400 int month;
1401 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001402
Emeric Brun54648852020-07-06 15:54:06 +02001403 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001404
Emeric Brun54648852020-07-06 15:54:06 +02001405 /* compute month */
1406 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1407 if (month)
1408 month--;
1409 if (month <= 11) {
1410 /* builds log prefix ex: 'Jan 1 ' */
1411 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1412 "%s %c%c ", monthname[month],
1413 timestamp[8] != '0' ? timestamp[8] : ' ',
1414 timestamp[9]);
1415 /* we reused the timestamp_buffer, signal that it does not
1416 * contain local time anymore
1417 */
1418 hdr_ctx.cur_legacy_time = 0;
1419 if (len == 7) {
1420 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1421 /* adds 'HH:MM:SS' from iso time */
1422 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1423 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1424 /* we successfully reuse iso time, we can break */
1425 break;
1426 }
1427 }
1428 /* Failed to reuse isotime time, fallback to local legacy time */
1429 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001430
Emeric Brun54648852020-07-06 15:54:06 +02001431 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1432 /* re-builds timestamp from the current local time */
1433 get_localtime(time, &logtime);
1434
1435 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1436 "%s %2d %02d:%02d:%02d ",
1437 monthname[logtime.tm_mon],
1438 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1439 if (len != LOG_LEGACYTIME_LEN+1)
1440 hdr_ctx.cur_legacy_time = 0;
1441 else
1442 hdr_ctx.cur_legacy_time = time;
1443 }
1444 if (likely(hdr_ctx.cur_legacy_time))
1445 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1446 else
1447 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1448 break;
1449 case LOG_FORMAT_RFC5424:
1450 /* adds rfc5425 version prefix */
1451 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1452 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001453 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001454 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1455 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1456 break;
1457 }
1458 /* let continue as 'timed' and 'iso' format for usual timestamp */
1459 /* fall through */
1460 case LOG_FORMAT_TIMED:
1461 case LOG_FORMAT_ISO:
1462 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1463 * '1900:01:01T14:00:00+02:00'
1464 * '1900:01:01T10:00:00.123456-02:00'
1465 */
1466 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1467 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1468 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001469 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001470 break;
1471 }
1472 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1473 int month;
1474 char *timestamp = metadata[LOG_META_TIME].ptr;
1475
1476 for (month = 0; month < 12; month++)
1477 if (!memcmp(monthname[month], timestamp, 3))
1478 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001479
Emeric Brun54648852020-07-06 15:54:06 +02001480 if (month < 12) {
1481
1482 /* get local time to retrieve year */
1483 get_localtime(time, &logtime);
1484
1485 /* year seems changed since log */
1486 if (logtime.tm_mon < month)
1487 logtime.tm_year--;
1488
1489 /* builds rfc5424 prefix ex: '1900-01-01T' */
1490 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1491 "%4d-%02d-%c%cT",
1492 logtime.tm_year+1900, month+1,
1493 timestamp[4] != ' ' ? timestamp[4] : '0',
1494 timestamp[5]);
1495
1496 /* we reused the timestamp_buffer, signal that it does not
1497 * contain local time anymore
1498 */
1499 hdr_ctx.cur_legacy_time = 0;
1500 if (len == 11) {
1501 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1502 /* adds HH:MM:SS from legacy timestamp */
1503 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001504 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001505 /* according to rfc: -00:00 means we don't know the timezone */
1506 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1507 /* we successfully reuse legacy time, we can break */
1508 break;
1509 }
1510 }
1511 /* Failed to reuse legacy time, fallback to local iso time */
1512 }
1513 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1514 break;
1515 case LOG_FORMAT_PRIO:
1516 case LOG_FORMAT_SHORT:
1517 case LOG_FORMAT_RAW:
1518 break;
1519 case LOG_FORMAT_UNSPEC:
1520 case LOG_FORMATS:
1521 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001522 }
1523
Emeric Brun54648852020-07-06 15:54:06 +02001524 /* prepare other meta data, stored into a max of 10 elems */
1525 switch (format) {
1526 case LOG_FORMAT_RFC3164:
1527 if (metadata && metadata[LOG_META_HOST].len) {
1528 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1529 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1530 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001531 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001532 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001533 /* fall through */
1534 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001535 if (!metadata || !metadata[LOG_META_TAG].len)
1536 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001537
Emeric Brun54648852020-07-06 15:54:06 +02001538 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1539 if (metadata[LOG_META_PID].len) {
1540 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1541 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1542 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1543 }
1544 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1545 break;
1546 case LOG_FORMAT_RFC5424:
1547 if (metadata && metadata[LOG_META_HOST].len) {
1548 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1549 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1550 }
1551 else
1552 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001553
Emeric Brun54648852020-07-06 15:54:06 +02001554 if (metadata && metadata[LOG_META_TAG].len) {
1555 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1556 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1557 }
1558 else
1559 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001560
Emeric Brun54648852020-07-06 15:54:06 +02001561 if (metadata && metadata[LOG_META_PID].len) {
1562 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1563 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1564 }
1565 else
1566 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001567
Emeric Brun54648852020-07-06 15:54:06 +02001568 if (metadata && metadata[LOG_META_MSGID].len) {
1569 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1570 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1571 }
1572 else
1573 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001574
Emeric Brun54648852020-07-06 15:54:06 +02001575 if (metadata && metadata[LOG_META_STDATA].len) {
1576 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1577 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1578 }
1579 else
1580 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1581 break;
1582 case LOG_FORMAT_PRIO:
1583 case LOG_FORMAT_SHORT:
1584 case LOG_FORMAT_TIMED:
1585 case LOG_FORMAT_ISO:
1586 case LOG_FORMAT_RAW:
1587 break;
1588 case LOG_FORMAT_UNSPEC:
1589 case LOG_FORMATS:
1590 ABORT_NOW();
1591 }
1592
1593 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001594}
1595
1596/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001597 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001598 * The argument <metadata> MUST be an array of size
1599 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001600 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001601 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001602 */
Emeric Brun54648852020-07-06 15:54:06 +02001603static 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 +01001604{
Emeric Brun54648852020-07-06 15:54:06 +02001605 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001606 static THREAD_LOCAL struct msghdr msghdr = {
1607 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001608 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001609 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001610 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1611 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001612 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001613 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001614 size_t nbelem;
1615 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001616
1617 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001618
Emeric Brunfa9d7802020-05-28 14:21:33 +02001619 /* historically some messages used to already contain the trailing LF
1620 * or Zero. Let's remove all trailing LF or Zero
1621 */
Emeric Brun54648852020-07-06 15:54:06 +02001622 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001623 size--;
1624
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001625 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001626 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001627 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001628 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001629 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1630 /* the socket's address is a file descriptor */
1631 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1632 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001633 else if (logsrv->addr.ss_family == AF_UNIX)
1634 plogfd = &logfdunix;
1635 else
1636 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001637
Willy Tarreauc046d162019-08-30 15:24:59 +02001638 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001639 /* socket not successfully initialized yet */
1640 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1641 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1642 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001643
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001644 if (!once) {
1645 once = 1; /* note: no need for atomic ops here */
1646 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1647 nblogger, strerror(errno), errno);
1648 }
1649 return;
1650 } else {
1651 /* we don't want to receive anything on this socket */
1652 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1653 /* does nothing under Linux, maybe needed for others */
1654 shutdown(*plogfd, SHUT_RD);
1655 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1656 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001657 }
1658
Emeric Brun54648852020-07-06 15:54:06 +02001659 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1660 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001661 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001662 struct ist msg;
1663
1664 msg = ist2(message, size);
1665 if (msg.len > logsrv->maxlen)
1666 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001667
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001668 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1669 }
1670 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1671 struct ist msg;
1672
1673 msg = ist2(message, size);
1674 if (msg.len > logsrv->maxlen)
1675 msg.len = logsrv->maxlen;
1676
1677 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001678 }
1679 else {
Emeric Brun54648852020-07-06 15:54:06 +02001680 int i = 0;
1681 int totlen = logsrv->maxlen;
1682
1683 for (i = 0 ; i < nbelem ; i++ ) {
1684 iovec[i].iov_base = msg_header[i].ptr;
1685 iovec[i].iov_len = msg_header[i].len;
1686 if (totlen <= iovec[i].iov_len) {
1687 iovec[i].iov_len = totlen;
1688 totlen = 0;
1689 break;
1690 }
1691 totlen -= iovec[i].iov_len;
1692 }
1693 if (totlen) {
1694 iovec[i].iov_base = message;
1695 iovec[i].iov_len = size;
1696 if (totlen <= iovec[i].iov_len)
1697 iovec[i].iov_len = totlen;
1698 i++;
1699 }
1700 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1701 iovec[i].iov_len = 1;
1702 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001703
Emeric Brun54648852020-07-06 15:54:06 +02001704 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001705 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1706 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001707
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001708 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1709 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001710
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001711 if (sent < 0) {
1712 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001713
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001714 if (errno == EAGAIN)
Willy Tarreau4781b152021-04-06 13:53:36 +02001715 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001716 else if (!once) {
1717 once = 1; /* note: no need for atomic ops here */
1718 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1719 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001720 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001721 }
1722}
Dragan Dosen59cee972015-09-19 22:09:02 +02001723
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001724/*
1725 * This function sends a syslog message.
1726 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001727 * The argument <metadata> MUST be an array of size
1728 * LOG_META_FIELDS*sizeof(struct ist) containing
1729 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001730 */
Emeric Brun54648852020-07-06 15:54:06 +02001731void process_send_log(struct list *logsrvs, int level, int facility,
1732 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001733{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001734 struct logsrv *logsrv;
1735 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001736
1737 /* Send log messages to syslog server. */
1738 nblogger = 0;
1739 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001740 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001741
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001742 /* we can filter the level of the messages that are sent to each logger */
1743 if (level > logsrv->level)
1744 continue;
1745
Frédéric Lécailled803e472019-04-25 07:42:09 +02001746 if (logsrv->lb.smp_rgs) {
1747 struct smp_log_range *curr_rg;
1748
1749 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1750 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1751 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1752 if (in_range) {
1753 /* Let's consume this range. */
1754 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1755 if (!curr_rg->curr_idx) {
1756 /* If consumed, let's select the next range. */
1757 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1758 }
1759 }
1760 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1761 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1762 }
1763 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001764 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1765 (facility == -1) ? logsrv->facility : facility,
1766 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001767 }
1768}
1769
Emeric Brun54648852020-07-06 15:54:06 +02001770/*
1771 * This function sends a syslog message.
1772 * It doesn't care about errors nor does it report them.
1773 * The arguments <sd> and <sd_size> are used for the structured-data part
1774 * in RFC5424 formatted syslog messages.
1775 */
1776void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1777 char *message, size_t size, char *sd, size_t sd_size)
1778{
1779 static THREAD_LOCAL pid_t curr_pid;
1780 static THREAD_LOCAL char pidstr[16];
1781 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1782
1783 if (logsrvs == NULL) {
1784 if (!LIST_ISEMPTY(&global.logsrvs)) {
1785 logsrvs = &global.logsrvs;
1786 }
1787 }
1788 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1789 return;
1790
1791 if (!metadata[LOG_META_HOST].len) {
1792 if (global.log_send_hostname)
1793 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02001794 }
1795
1796 if (!tagb || !tagb->area)
1797 tagb = &global.log_tag;
1798
1799 if (tagb)
1800 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1801
1802 if (unlikely(curr_pid != getpid()))
1803 metadata[LOG_META_PID].len = 0;
1804
1805 if (!metadata[LOG_META_PID].len) {
1806 curr_pid = getpid();
1807 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1808 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1809 }
1810
1811 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1812
1813 /* Remove trailing space of structured data */
1814 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1815 metadata[LOG_META_STDATA].len--;
1816
1817 return process_send_log(logsrvs, level, -1, metadata, message, size);
1818}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001819
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001820const 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 +01001821const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1822 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1823 Set-cookie Updated, unknown, unknown */
1824
William Lallemand1d705562012-03-12 12:46:41 +01001825/*
1826 * try to write a character if there is enough space, or goto out
1827 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001828#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001829 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001830 *(tmplog++) = (x); \
1831 } else { \
1832 goto out; \
1833 } \
1834 } while(0)
1835
Dragan Dosen835b9212016-02-12 13:23:03 +01001836
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001837/* Initializes some log data at boot */
1838static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001839{
1840 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001841 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001842
1843 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1844 * inside PARAM-VALUE should be escaped with '\' as prefix.
1845 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1846 * details.
1847 */
1848 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1849
1850 tmp = "\"\\]";
1851 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001852 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001853 tmp++;
1854 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001855
1856 /* initialize the log header encoding map : '{|}"#' should be encoded with
1857 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1858 * URL encoding only requires '"', '#' to be encoded as well as non-
1859 * printable characters above.
1860 */
1861 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1862 memset(url_encode_map, 0, sizeof(url_encode_map));
1863 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001864 ha_bit_set(i, hdr_encode_map);
1865 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001866 }
1867 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001868 ha_bit_set(i, hdr_encode_map);
1869 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001870 }
1871
1872 tmp = "\"#{|}";
1873 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001874 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001875 tmp++;
1876 }
1877
1878 tmp = "\"#";
1879 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001880 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001881 tmp++;
1882 }
1883
1884 /* initialize the http header encoding map. The draft httpbis define the
1885 * header content as:
1886 *
1887 * HTTP-message = start-line
1888 * *( header-field CRLF )
1889 * CRLF
1890 * [ message-body ]
1891 * header-field = field-name ":" OWS field-value OWS
1892 * field-value = *( field-content / obs-fold )
1893 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1894 * obs-fold = CRLF 1*( SP / HTAB )
1895 * field-vchar = VCHAR / obs-text
1896 * VCHAR = %x21-7E
1897 * obs-text = %x80-FF
1898 *
1899 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1900 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08001901 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02001902 */
1903 memset(http_encode_map, 0, sizeof(http_encode_map));
1904 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001905 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001906 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001907 ha_bit_set(i, http_encode_map);
1908 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001909}
William Lallemand1d705562012-03-12 12:46:41 +01001910
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001911INITCALL0(STG_PREPARE, init_log);
1912
Christopher Faulet0132d062017-07-26 15:33:35 +02001913/* Initialize log buffers used for syslog messages */
1914int init_log_buffers()
1915{
Christopher Faulet0132d062017-07-26 15:33:35 +02001916 logline = my_realloc2(logline, global.max_syslog_len + 1);
1917 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02001918 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02001919 return 0;
1920 return 1;
1921}
1922
1923/* Deinitialize log buffers used for syslog messages */
1924void deinit_log_buffers()
1925{
Christopher Faulet0132d062017-07-26 15:33:35 +02001926 free(logline);
1927 free(logline_rfc5424);
Christopher Faulet0132d062017-07-26 15:33:35 +02001928 logline = NULL;
1929 logline_rfc5424 = NULL;
1930}
1931
Willy Tarreaudf974472012-12-28 02:44:01 +01001932/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1933 * <maxsize> characters. Returns the size of the output string in characters,
1934 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001935 * is not zero. It requires a valid session and optionally a stream. If the
1936 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01001937 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001938int 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 +02001939{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001940 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001941 struct proxy *be;
1942 struct http_txn *txn;
1943 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001944 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001945 unsigned int s_flags;
1946 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02001947 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001948 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001949 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001950 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001951 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001952 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001953 int t_request;
1954 int hdr;
1955 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001956 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001957 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001958 char *ret;
1959 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001960 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001961 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001962 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001963 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00001964 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001965 struct http_uri_parser parser;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001966
William Lallemandbddd4fd2012-02-27 11:23:10 +01001967 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001968
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001969 if (likely(s)) {
1970 be = s->be;
1971 txn = s->txn;
1972 be_conn = cs_conn(objt_cs(s->si[1].end));
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001973 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001974 s_flags = s->flags;
1975 uniq_id = s->uniq_id;
1976 logs = &s->logs;
1977 } else {
1978 /* we have no stream so we first need to initialize a few
1979 * things that are needed later. We do increment the request
1980 * ID so that it's uniquely assigned to this request just as
1981 * if the request had reached the point of being processed.
1982 * A request error is reported as it's the only element we have
1983 * here and which justifies emitting such a log.
1984 */
Christopher Fauletfd818482021-04-14 14:01:41 +02001985 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001986 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001987 fe_conn = objt_conn(sess->origin);
Christopher Fauletfd818482021-04-14 14:01:41 +02001988 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? cs_conn(__objt_check(sess->origin)->cs) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001989 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001990 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02001991 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001992
1993 /* prepare a valid log structure */
1994 tmp_strm_log.tv_accept = sess->tv_accept;
1995 tmp_strm_log.accept_date = sess->accept_date;
1996 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02001997 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001998 tv_zero(&tmp_strm_log.tv_request);
1999 tmp_strm_log.t_queue = -1;
2000 tmp_strm_log.t_connect = -1;
2001 tmp_strm_log.t_data = -1;
2002 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2003 tmp_strm_log.bytes_in = 0;
2004 tmp_strm_log.bytes_out = 0;
2005 tmp_strm_log.prx_queue_pos = 0;
2006 tmp_strm_log.srv_queue_pos = 0;
2007
2008 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002009
2010 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
2011 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, NULL);
2012
2013 switch (es) {
2014 case MUX_ES_SUCCESS:
2015 break;
2016 case MUX_ES_INVALID_ERR:
2017 status = 400;
2018 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2019 s_flags = SF_ERR_CLICL | SF_FINST_R;
2020 else
2021 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2022 break;
2023 case MUX_ES_TOUT_ERR:
2024 status = 408;
2025 s_flags = SF_ERR_CLITO | SF_FINST_R;
2026 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002027 case MUX_ES_NOTIMPL_ERR:
2028 status = 501;
2029 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2030 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002031 case MUX_ES_INTERNAL_ERR:
2032 status = 500;
2033 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2034 break;
2035 default:
2036 break;
2037 }
2038 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002039 }
2040
William Lallemandbddd4fd2012-02-27 11:23:10 +01002041 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002042 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2043 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002044
William Lallemand1d705562012-03-12 12:46:41 +01002045 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002046
William Lallemandbddd4fd2012-02-27 11:23:10 +01002047 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002048 if (LIST_ISEMPTY(list_format))
2049 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002050
William Lallemand1d705562012-03-12 12:46:41 +01002051 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002052 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002053 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002054 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002055 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002056
Willy Tarreauc8368452012-12-21 00:09:23 +01002057 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002058 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002059 if (!last_isspace) {
2060 LOGCHAR(' ');
2061 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002062 }
2063 break;
2064
William Lallemand1d705562012-03-12 12:46:41 +01002065 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002066 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002067 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002068 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002069 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002070 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002071 last_isspace = 0;
2072 break;
2073
Willy Tarreauc8368452012-12-21 00:09:23 +01002074 case LOG_FMT_EXPR: // sample expression, may be request or response
2075 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002076 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002077 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Willy Tarreau20b7a0f2021-09-03 08:53:29 +02002078
Christopher Faulet5f940702020-04-06 10:40:02 +02002079 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002080 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Willy Tarreau20b7a0f2021-09-03 08:53:29 +02002081
2082 if (!key && !(tmp->options & (LOG_OPT_REQ_CAP|LOG_OPT_RES_CAP))) // cfg, cli
2083 key = sample_fetch_as_type(be, sess, s, SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
2084
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002085 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002086 ret = lf_encode_chunk(tmplog, dst + maxsize,
2087 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002088 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002089 ret = lf_text_len(tmplog,
2090 key ? key->data.u.str.area : NULL,
2091 key ? key->data.u.str.data : 0,
2092 dst + maxsize - tmplog,
2093 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002094 if (ret == 0)
2095 goto out;
2096 tmplog = ret;
2097 last_isspace = 0;
2098 break;
2099
Willy Tarreau2beef582012-12-20 17:22:52 +01002100 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002101 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002102 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002103 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002104 else
2105 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002106 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002107 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002108 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002109 last_isspace = 0;
2110 break;
2111
Willy Tarreau2beef582012-12-20 17:22:52 +01002112 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002113 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002114 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002115 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002116 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002117 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002118 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002119 dst + maxsize - tmplog, tmp);
2120 }
William Lallemand5f232402012-04-05 18:02:55 +02002121 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002122 else
2123 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2124
William Lallemand5f232402012-04-05 18:02:55 +02002125 if (ret == NULL)
2126 goto out;
2127 tmplog = ret;
2128 last_isspace = 0;
2129 break;
2130
Willy Tarreau2beef582012-12-20 17:22:52 +01002131 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002132 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002133 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002134 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002135 }
2136 else
2137 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2138
William Lallemand1d705562012-03-12 12:46:41 +01002139 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002140 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002141 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002142 last_isspace = 0;
2143 break;
2144
Willy Tarreau2beef582012-12-20 17:22:52 +01002145 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002146 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002147 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002148 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002149 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002150 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002151 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002152 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002153 else
2154 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2155
William Lallemand5f232402012-04-05 18:02:55 +02002156 if (ret == NULL)
2157 goto out;
2158 tmplog = ret;
2159 last_isspace = 0;
2160 break;
2161
Willy Tarreau2beef582012-12-20 17:22:52 +01002162 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002163 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002164 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002165 else
2166 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2167
William Lallemand1d705562012-03-12 12:46:41 +01002168 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002169 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002170 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002171 last_isspace = 0;
2172 break;
2173
Willy Tarreau2beef582012-12-20 17:22:52 +01002174 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002175 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002176 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002177 else
2178 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2179
William Lallemand5f232402012-04-05 18:02:55 +02002180 if (ret == NULL)
2181 goto out;
2182 tmplog = ret;
2183 last_isspace = 0;
2184 break;
2185
Willy Tarreau2beef582012-12-20 17:22:52 +01002186 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002187 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002188 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002189 else
2190 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2191
William Lallemand5f232402012-04-05 18:02:55 +02002192 if (ret == NULL)
2193 goto out;
2194 tmplog = ret;
2195 last_isspace = 0;
2196 break;
2197
Willy Tarreau2beef582012-12-20 17:22:52 +01002198 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002199 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002200 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002201 else
2202 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2203
William Lallemand1d705562012-03-12 12:46:41 +01002204 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002205 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002206 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002207 last_isspace = 0;
2208 break;
2209
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002210 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002211 get_localtime(logs->accept_date.tv_sec, &tm);
2212 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002213 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002214 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002215 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002216 last_isspace = 0;
2217 break;
2218
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002219 case LOG_FMT_tr: // %tr = start of request date
2220 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002221 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 +02002222 get_localtime(tv.tv_sec, &tm);
2223 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2224 if (ret == NULL)
2225 goto out;
2226 tmplog = ret;
2227 last_isspace = 0;
2228 break;
2229
2230 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002231 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002232 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002233 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002234 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002235 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002236 last_isspace = 0;
2237 break;
2238
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002239 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002240 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 +02002241 get_gmtime(tv.tv_sec, &tm);
2242 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2243 if (ret == NULL)
2244 goto out;
2245 tmplog = ret;
2246 last_isspace = 0;
2247 break;
2248
2249 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002250 get_localtime(logs->accept_date.tv_sec, &tm);
2251 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002252 if (ret == NULL)
2253 goto out;
2254 tmplog = ret;
2255 last_isspace = 0;
2256 break;
2257
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002258 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002259 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 +02002260 get_localtime(tv.tv_sec, &tm);
2261 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2262 if (ret == NULL)
2263 goto out;
2264 tmplog = ret;
2265 last_isspace = 0;
2266 break;
2267
William Lallemand5f232402012-04-05 18:02:55 +02002268 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002269 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002270 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002271 if (iret < 0 || iret > dst + maxsize - tmplog)
2272 goto out;
2273 last_isspace = 0;
2274 tmplog += iret;
2275 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002276 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002277 if (ret == NULL)
2278 goto out;
2279 tmplog = ret;
2280 last_isspace = 0;
2281 }
2282 break;
2283
William Lallemand1d705562012-03-12 12:46:41 +01002284 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002285 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002286 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002287 if (iret < 0 || iret > dst + maxsize - tmplog)
2288 goto out;
2289 last_isspace = 0;
2290 tmplog += iret;
2291 } else {
2292 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002293 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002294 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002295 tmplog, 4);
2296 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002297 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002298 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002299 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002300 }
2301 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002302
William Lallemand1d705562012-03-12 12:46:41 +01002303 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002304 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002305 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002306 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002307 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002308 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002309 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002310 break;
2311
Willy Tarreau773d65f2012-10-12 14:56:11 +02002312 case LOG_FMT_FRONTEND_XPRT: // %ft
2313 src = fe->id;
2314 if (tmp->options & LOG_OPT_QUOTE)
2315 LOGCHAR('"');
2316 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2317 if (iret == 0)
2318 goto out;
2319 tmplog += iret;
Christopher Fauletfd818482021-04-14 14:01:41 +02002320 if (sess->listener && sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002321 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002322 if (tmp->options & LOG_OPT_QUOTE)
2323 LOGCHAR('"');
2324 last_isspace = 0;
2325 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002326#ifdef USE_OPENSSL
2327 case LOG_FMT_SSL_CIPHER: // %sslc
2328 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002329 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002330 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002331 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002332 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002333 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2334 if (ret == NULL)
2335 goto out;
2336 tmplog = ret;
2337 last_isspace = 0;
2338 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002339
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002340 case LOG_FMT_SSL_VERSION: // %sslv
2341 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002342 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002343 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002344 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002345 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002346 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2347 if (ret == NULL)
2348 goto out;
2349 tmplog = ret;
2350 last_isspace = 0;
2351 break;
2352#endif
William Lallemand1d705562012-03-12 12:46:41 +01002353 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002354 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002355 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002356 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002357 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002358 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002359 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002360 break;
2361
William Lallemand1d705562012-03-12 12:46:41 +01002362 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002363 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002364 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002365 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002366 break;
2367 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002368 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002369 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002370 case OBJ_TYPE_CHECK:
2371 src = (__objt_check(sess->origin)->server
2372 ? __objt_check(sess->origin)->server->id
2373 : "<NOSRV>");
2374 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002375 default:
2376 src = "<NOSRV>";
2377 break;
2378 }
William Lallemand5f232402012-04-05 18:02:55 +02002379 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002380 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002381 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002382 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002383 last_isspace = 0;
2384 break;
2385
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002386 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002387 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002388 if (ret == NULL)
2389 goto out;
2390 tmplog = ret;
2391 last_isspace = 0;
2392 break;
2393
2394 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002395 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002396 if (ret == NULL)
2397 goto out;
2398 tmplog = ret;
2399 last_isspace = 0;
2400 break;
2401
2402 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002403 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002404 tmplog, dst + maxsize - tmplog);
2405 if (ret == NULL)
2406 goto out;
2407 tmplog = ret;
2408 last_isspace = 0;
2409 break;
2410
2411 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002412 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002413 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002414 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002415 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002416 last_isspace = 0;
2417 break;
2418
William Lallemand1d705562012-03-12 12:46:41 +01002419 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002420 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002421 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002422 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002423 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002424 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002425 last_isspace = 0;
2426 break;
2427
William Lallemand1d705562012-03-12 12:46:41 +01002428 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002429 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002430 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002431 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002432 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002433 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002434 last_isspace = 0;
2435 break;
2436
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002437 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002438 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002439 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002440 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002441 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002442 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002443 last_isspace = 0;
2444 break;
2445
Willy Tarreau27b639d2016-05-17 17:55:27 +02002446 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002447 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002448 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002449 tmplog, dst + maxsize - tmplog);
2450 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002451 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002452 tmplog, dst + maxsize - tmplog);
2453 if (ret == NULL)
2454 goto out;
2455 tmplog = ret;
2456 last_isspace = 0;
2457 break;
2458
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002459 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2460 if (!(fe->to_log & LW_BYTES))
2461 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002462 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 +02002463 tmplog, dst + maxsize - tmplog);
2464 if (ret == NULL)
2465 goto out;
2466 tmplog = ret;
2467 last_isspace = 0;
2468 break;
2469
2470 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002471 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002472 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002473 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002474 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002475 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002476 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002477 last_isspace = 0;
2478 break;
2479
Damien Claisse57c8eb92020-04-28 12:09:19 +00002480 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2481 if (!(fe->to_log & LW_BYTES))
2482 LOGCHAR('+');
2483 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2484 tmplog, dst + maxsize - tmplog);
2485 if (ret == NULL)
2486 goto out;
2487 tmplog = ret;
2488 last_isspace = 0;
2489 break;
2490
Willy Tarreau2beef582012-12-20 17:22:52 +01002491 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002492 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002493 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002494 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002495 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002496 last_isspace = 0;
2497 break;
2498
William Lallemand1d705562012-03-12 12:46:41 +01002499 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002500 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002501 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002502 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002503 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002504 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002505 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002506 last_isspace = 0;
2507 break;
2508
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002509 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002510 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002511 if (ret == NULL)
2512 goto out;
2513 tmplog = ret;
2514 last_isspace = 0;
2515 break;
2516
Willy Tarreau2beef582012-12-20 17:22:52 +01002517 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002518 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002519 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002520 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002521 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002522 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002523 last_isspace = 0;
2524 break;
2525
Willy Tarreau2beef582012-12-20 17:22:52 +01002526 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002527 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002528 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002529 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002530 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002531 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002532 last_isspace = 0;
2533 break;
2534
William Lallemand1d705562012-03-12 12:46:41 +01002535 case LOG_FMT_TERMSTATE: // %ts
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 Tarreau6580c062012-03-12 15:09:42 +01002538 *tmplog = '\0';
2539 last_isspace = 0;
2540 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002541
William Lallemand1d705562012-03-12 12:46:41 +01002542 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002543 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2544 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002545 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2546 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 +01002547 last_isspace = 0;
2548 break;
2549
William Lallemand1d705562012-03-12 12:46:41 +01002550 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002551 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002552 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002553 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002554 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002555 last_isspace = 0;
2556 break;
2557
William Lallemand1d705562012-03-12 12:46:41 +01002558 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002559 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002560 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002561 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002562 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002563 last_isspace = 0;
2564 break;
2565
William Lallemand1d705562012-03-12 12:46:41 +01002566 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002567 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002568 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002569 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002570 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002571 last_isspace = 0;
2572 break;
2573
William Lallemand1d705562012-03-12 12:46:41 +01002574 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002575 switch (obj_type(s ? s->target : sess->origin)) {
2576 case OBJ_TYPE_SERVER:
2577 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2578 tmplog, dst + maxsize - tmplog);
2579 break;
2580 case OBJ_TYPE_CHECK:
2581 ret = ultoa_o(__objt_check(sess->origin)->server
2582 ? __objt_check(sess->origin)->server->cur_sess
2583 : 0, tmplog, dst + maxsize - tmplog);
2584 break;
2585 default:
2586 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2587 break;
2588 }
2589
William Lallemand1d705562012-03-12 12:46:41 +01002590 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002591 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002592 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002593 last_isspace = 0;
2594 break;
2595
William Lallemand1d705562012-03-12 12:46:41 +01002596 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002597 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002598 LOGCHAR('+');
Christopher Faulet1d26f222021-04-16 11:24:20 +02002599 ret = ltoa_o(((s && s->si[1].conn_retries > 0)
2600 ? (be->conn_retries - s->si[1].conn_retries)
2601 : ((s && s->si[1].state != SI_ST_INI) ? be->conn_retries : 0)),
2602 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002603 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002604 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002605 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002606 last_isspace = 0;
2607 break;
2608
William Lallemand1d705562012-03-12 12:46:41 +01002609 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002610 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002611 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002612 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002613 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002614 last_isspace = 0;
2615 break;
2616
William Lallemand1d705562012-03-12 12:46:41 +01002617 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002618 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002619 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002620 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002621 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002622 last_isspace = 0;
2623 break;
2624
William Lallemand1d705562012-03-12 12:46:41 +01002625 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002626 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002627 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002628 if (tmp->options & LOG_OPT_QUOTE)
2629 LOGCHAR('"');
2630 LOGCHAR('{');
2631 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2632 if (hdr)
2633 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002634 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002635 ret = lf_encode_string(tmplog, dst + maxsize,
2636 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002637 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002638 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002639 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002640 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002641 }
2642 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002643 if (tmp->options & LOG_OPT_QUOTE)
2644 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002645 last_isspace = 0;
2646 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002647 break;
2648
William Lallemand1d705562012-03-12 12:46:41 +01002649 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002650 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002651 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002652 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2653 if (hdr > 0)
2654 LOGCHAR(' ');
2655 if (tmp->options & LOG_OPT_QUOTE)
2656 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002657 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002658 ret = lf_encode_string(tmplog, dst + maxsize,
2659 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002660 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002661 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002662 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002663 } else if (!(tmp->options & LOG_OPT_QUOTE))
2664 LOGCHAR('-');
2665 if (tmp->options & LOG_OPT_QUOTE)
2666 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002667 last_isspace = 0;
2668 }
2669 }
2670 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002671
William Lallemand1d705562012-03-12 12:46:41 +01002672
2673 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002674 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002675 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002676 if (tmp->options & LOG_OPT_QUOTE)
2677 LOGCHAR('"');
2678 LOGCHAR('{');
2679 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2680 if (hdr)
2681 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002682 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002683 ret = lf_encode_string(tmplog, dst + maxsize,
2684 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002685 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002686 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002687 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002688 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002689 }
2690 LOGCHAR('}');
2691 last_isspace = 0;
2692 if (tmp->options & LOG_OPT_QUOTE)
2693 LOGCHAR('"');
2694 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002695 break;
2696
William Lallemand1d705562012-03-12 12:46:41 +01002697 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002698 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002699 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002700 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2701 if (hdr > 0)
2702 LOGCHAR(' ');
2703 if (tmp->options & LOG_OPT_QUOTE)
2704 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002705 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002706 ret = lf_encode_string(tmplog, dst + maxsize,
2707 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002708 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002709 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002710 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002711 } else if (!(tmp->options & LOG_OPT_QUOTE))
2712 LOGCHAR('-');
2713 if (tmp->options & LOG_OPT_QUOTE)
2714 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002715 last_isspace = 0;
2716 }
2717 }
2718 break;
2719
William Lallemand1d705562012-03-12 12:46:41 +01002720 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002721 /* Request */
2722 if (tmp->options & LOG_OPT_QUOTE)
2723 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002724 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002725 ret = lf_encode_string(tmplog, dst + maxsize,
2726 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002727 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002728 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002729 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002730 if (tmp->options & LOG_OPT_QUOTE)
2731 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002732 last_isspace = 0;
2733 break;
William Lallemand5f232402012-04-05 18:02:55 +02002734
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002735 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002736 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002737
Willy Tarreaub7636d12015-06-17 19:58:02 +02002738 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002739 LOGCHAR('"');
2740
2741 end = uri + strlen(uri);
2742 // look for the first whitespace character
2743 while (uri < end && !HTTP_IS_SPHT(*uri))
2744 uri++;
2745
2746 // keep advancing past multiple spaces
2747 while (uri < end && HTTP_IS_SPHT(*uri)) {
2748 uri++; nspaces++;
2749 }
2750
2751 // look for first space or question mark after url
2752 spc = uri;
2753 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2754 spc++;
2755
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002756 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002757 chunk.area = "<BADREQ>";
2758 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002759 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002760 chunk.area = uri;
2761 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002762 }
2763
Dragan Dosen835b9212016-02-12 13:23:03 +01002764 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002765 if (ret == NULL || *ret != '\0')
2766 goto out;
2767
2768 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002769 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002770 LOGCHAR('"');
2771
2772 last_isspace = 0;
2773 break;
2774
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002775 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2776 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2777
2778 if (tmp->options & LOG_OPT_QUOTE)
2779 LOGCHAR('"');
2780
2781 end = uri + strlen(uri);
2782
2783 // look for the first whitespace character
2784 while (uri < end && !HTTP_IS_SPHT(*uri))
2785 uri++;
2786
2787 // keep advancing past multiple spaces
2788 while (uri < end && HTTP_IS_SPHT(*uri)) {
2789 uri++; nspaces++;
2790 }
2791
2792 // look for first space after url
2793 spc = uri;
2794 while (spc < end && !HTTP_IS_SPHT(*spc))
2795 spc++;
2796
Tim Duesterhus92c696e2021-02-28 16:11:36 +01002797 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002798
2799 // extract relative path without query params from url
Amaury Denoyellec453f952021-07-06 11:40:12 +02002800 parser = http_uri_parser_init(path);
2801 path = iststop(http_parse_path(&parser), '?');
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002802 if (!txn || !txn->uri || nspaces == 0) {
2803 chunk.area = "<BADREQ>";
2804 chunk.data = strlen("<BADREQ>");
2805 } else {
2806 chunk.area = path.ptr;
2807 chunk.data = path.len;
2808 }
2809
2810 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2811 if (ret == NULL || *ret != '\0')
2812 goto out;
2813
2814 tmplog = ret;
2815 if (tmp->options & LOG_OPT_QUOTE)
2816 LOGCHAR('"');
2817
2818 last_isspace = 0;
2819 break;
2820
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002821 case LOG_FMT_HTTP_QUERY: // %HQ
2822 if (tmp->options & LOG_OPT_QUOTE)
2823 LOGCHAR('"');
2824
Willy Tarreau57bc8912016-04-25 17:09:40 +02002825 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002826 chunk.area = "<BADREQ>";
2827 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002828 } else {
2829 uri = txn->uri;
2830 end = uri + strlen(uri);
2831 // look for the first question mark
2832 while (uri < end && *uri != '?')
2833 uri++;
2834
2835 qmark = uri;
2836 // look for first space or question mark after url
2837 while (uri < end && !HTTP_IS_SPHT(*uri))
2838 uri++;
2839
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002840 chunk.area = qmark;
2841 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002842 }
2843
Dragan Dosen835b9212016-02-12 13:23:03 +01002844 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002845 if (ret == NULL || *ret != '\0')
2846 goto out;
2847
2848 tmplog = ret;
2849 if (tmp->options & LOG_OPT_QUOTE)
2850 LOGCHAR('"');
2851
2852 last_isspace = 0;
2853 break;
2854
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002855 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002856 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002857
Willy Tarreaub7636d12015-06-17 19:58:02 +02002858 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002859 LOGCHAR('"');
2860
2861 end = uri + strlen(uri);
2862 // look for the first whitespace character
2863 while (uri < end && !HTTP_IS_SPHT(*uri))
2864 uri++;
2865
2866 // keep advancing past multiple spaces
2867 while (uri < end && HTTP_IS_SPHT(*uri)) {
2868 uri++; nspaces++;
2869 }
2870
2871 // look for first space after url
2872 spc = uri;
2873 while (spc < end && !HTTP_IS_SPHT(*spc))
2874 spc++;
2875
Willy Tarreau57bc8912016-04-25 17:09:40 +02002876 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002877 chunk.area = "<BADREQ>";
2878 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002879 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002880 chunk.area = uri;
2881 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002882 }
2883
Dragan Dosen835b9212016-02-12 13:23:03 +01002884 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002885 if (ret == NULL || *ret != '\0')
2886 goto out;
2887
2888 tmplog = ret;
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 last_isspace = 0;
2893 break;
2894
2895 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002896 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002897 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002898 LOGCHAR('"');
2899
2900 end = uri + strlen(uri);
2901 // look for the first whitespace character
2902 spc = uri;
2903 while (spc < end && !HTTP_IS_SPHT(*spc))
2904 spc++;
2905
2906 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002907 chunk.area = "<BADREQ>";
2908 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002909 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002910 chunk.area = uri;
2911 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002912 }
2913
Dragan Dosen835b9212016-02-12 13:23:03 +01002914 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002915 if (ret == NULL || *ret != '\0')
2916 goto out;
2917
2918 tmplog = ret;
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 last_isspace = 0;
2923 break;
2924
2925 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002926 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002927 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002928 LOGCHAR('"');
2929
2930 end = uri + strlen(uri);
2931 // look for the first whitespace character
2932 while (uri < end && !HTTP_IS_SPHT(*uri))
2933 uri++;
2934
2935 // keep advancing past multiple spaces
2936 while (uri < end && HTTP_IS_SPHT(*uri)) {
2937 uri++; nspaces++;
2938 }
2939
2940 // look for the next whitespace character
2941 while (uri < end && !HTTP_IS_SPHT(*uri))
2942 uri++;
2943
2944 // keep advancing past multiple spaces
2945 while (uri < end && HTTP_IS_SPHT(*uri))
2946 uri++;
2947
Willy Tarreau57bc8912016-04-25 17:09:40 +02002948 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002949 chunk.area = "<BADREQ>";
2950 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002951 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002952 chunk.area = "HTTP/0.9";
2953 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002954 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002955 chunk.area = uri;
2956 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002957 }
2958
Dragan Dosen835b9212016-02-12 13:23:03 +01002959 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002960 if (ret == NULL || *ret != '\0')
2961 goto out;
2962
2963 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002964 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002965 LOGCHAR('"');
2966
2967 last_isspace = 0;
2968 break;
2969
William Lallemand5f232402012-04-05 18:02:55 +02002970 case LOG_FMT_COUNTER: // %rt
2971 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002972 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002973 if (iret < 0 || iret > dst + maxsize - tmplog)
2974 goto out;
2975 last_isspace = 0;
2976 tmplog += iret;
2977 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002978 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002979 if (ret == NULL)
2980 goto out;
2981 tmplog = ret;
2982 last_isspace = 0;
2983 }
2984 break;
2985
Willy Tarreau7346acb2014-08-28 15:03:15 +02002986 case LOG_FMT_LOGCNT: // %lc
2987 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002988 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002989 if (iret < 0 || iret > dst + maxsize - tmplog)
2990 goto out;
2991 last_isspace = 0;
2992 tmplog += iret;
2993 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002994 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002995 if (ret == NULL)
2996 goto out;
2997 tmplog = ret;
2998 last_isspace = 0;
2999 }
3000 break;
3001
William Lallemand5f232402012-04-05 18:02:55 +02003002 case LOG_FMT_HOSTNAME: // %H
3003 src = hostname;
3004 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3005 if (ret == NULL)
3006 goto out;
3007 tmplog = ret;
3008 last_isspace = 0;
3009 break;
3010
3011 case LOG_FMT_PID: // %pid
3012 if (tmp->options & LOG_OPT_HEXA) {
3013 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3014 if (iret < 0 || iret > dst + maxsize - tmplog)
3015 goto out;
3016 last_isspace = 0;
3017 tmplog += iret;
3018 } else {
3019 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3020 if (ret == NULL)
3021 goto out;
3022 tmplog = ret;
3023 last_isspace = 0;
3024 }
3025 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003026
3027 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003028 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003029 if (s)
3030 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3031 else
3032 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003033 if (ret == NULL)
3034 goto out;
3035 tmplog = ret;
3036 last_isspace = 0;
3037 break;
3038
William Lallemandbddd4fd2012-02-27 11:23:10 +01003039 }
3040 }
3041
3042out:
William Lallemand1d705562012-03-12 12:46:41 +01003043 /* *tmplog is a unused character */
3044 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003045 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003046
Willy Tarreaubaaee002006-06-26 02:48:02 +02003047}
3048
William Lallemand1d705562012-03-12 12:46:41 +01003049/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003050 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003051 * Will not log if the frontend has no log defined.
3052 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003053void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003054{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003055 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003056 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003057 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003058
3059 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003060 err = (s->flags & SF_REDISP) ||
3061 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3062 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003063 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003064 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003065
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003066 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003067 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003068
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003069 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003070 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003071
Willy Tarreauabcd5142013-06-11 17:18:02 +02003072 if (s->logs.level) { /* loglevel was overridden */
3073 if (s->logs.level == -1) {
3074 s->logs.logwait = 0; /* logs disabled */
3075 return;
3076 }
3077 level = s->logs.level - 1;
3078 }
3079 else {
3080 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003081 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003082 level = LOG_ERR;
3083 }
William Lallemand1d705562012-03-12 12:46:41 +01003084
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003085 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003086 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003087 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003088 }
3089
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003090 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3091 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3092 &sess->fe->logformat_sd);
3093 }
3094
Dragan Dosen59cee972015-09-19 22:09:02 +02003095 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003096 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003097 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003098 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3099 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003100 s->logs.logwait = 0;
3101 }
3102}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003103
Willy Tarreau53839352018-09-05 19:51:10 +02003104/*
3105 * send a minimalist log for the session. Will not log if the frontend has no
3106 * log defined. It is assumed that this is only used to report anomalies that
3107 * cannot lead to the creation of a regular stream. Because of this the log
3108 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3109 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003110 * function to report unimportant events. It is safe to call this function with
3111 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003112 */
3113void sess_log(struct session *sess)
3114{
3115 int size, level;
3116 int sd_size = 0;
3117
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003118 if (!sess)
3119 return;
3120
Willy Tarreau53839352018-09-05 19:51:10 +02003121 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3122 return;
3123
3124 level = LOG_INFO;
3125 if (sess->fe->options2 & PR_O2_LOGERRORS)
3126 level = LOG_ERR;
3127
3128 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3129 sd_size = sess_build_logline(sess, NULL,
3130 logline_rfc5424, global.max_syslog_len,
3131 &sess->fe->logformat_sd);
3132 }
3133
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02003134 if (!LIST_ISEMPTY(&sess->fe->logformat_error))
3135 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat_error);
3136 else
3137 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
Willy Tarreau53839352018-09-05 19:51:10 +02003138 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003139 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003140 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3141 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003142 }
3143}
3144
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003145void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3146{
3147 va_list argp;
3148 int data_len;
3149
3150 if (level < 0 || format == NULL || logline == NULL)
3151 return;
3152
3153 va_start(argp, format);
3154 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3155 if (data_len < 0 || data_len > global.max_syslog_len)
3156 data_len = global.max_syslog_len;
3157 va_end(argp);
3158
3159 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3160}
Emeric Brun54932b42020-07-07 09:43:24 +02003161/*
3162 * This function parse a received log message <buf>, of size <buflen>
3163 * it fills <level>, <facility> and <metadata> depending of the detected
3164 * header format and message will point on remaining payload of <size>
3165 *
3166 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3167 * struct ist len will be set to 0 if field is not found
3168 * <level> and <facility> will be set to -1 if not found.
3169 */
3170void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3171 struct ist *metadata, char **message, size_t *size)
3172{
3173
3174 char *p;
3175 int fac_level = 0;
3176
3177 *level = *facility = -1;
3178
3179 *message = buf;
3180 *size = buflen;
3181
3182 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3183
3184 p = buf;
3185 if (*size < 2 || *p != '<')
3186 return;
3187
3188 p++;
3189 while (*p != '>') {
3190 if (*p > '9' || *p < '0')
3191 return;
3192 fac_level = 10*fac_level + (*p - '0');
3193 p++;
3194 if ((p - buf) > buflen)
3195 return;
3196 }
3197
3198 *facility = fac_level >> 3;
3199 *level = fac_level & 0x7;
3200 p++;
3201
3202 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3203
3204 buflen -= p - buf;
3205 buf = p;
3206
3207 *size = buflen;
3208 *message = buf;
3209
3210 /* for rfc5424, prio is always followed by '1' and ' ' */
3211 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3212 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3213 * followed by message.
3214 * Each header field can present NILVALUE: '-'
3215 */
3216
3217 p += 2;
3218 /* timestamp is NILVALUE '-' */
3219 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3220 metadata[LOG_META_TIME] = ist2(p, 1);
3221 p++;
3222 }
3223 else if (*size > LOG_ISOTIME_MINLEN) {
3224 metadata[LOG_META_TIME].ptr = p;
3225
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003226 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003227 * in timestamp.
3228 * possible format are:
3229 * ex: '1970-01-01T00:00:00.000000Z'
3230 * '1970-01-01T00:00:00.000000+00:00'
3231 * '1970-01-01T00:00:00.000000-00:00'
3232 * '1970-01-01T00:00:00Z'
3233 * '1970-01-01T00:00:00+00:00'
3234 * '1970-01-01T00:00:00-00:00'
3235 */
3236 p += 19;
3237 if (*p == '.') {
3238 p++;
3239 if ((p - buf) >= buflen)
3240 goto bad_format;
3241 while (*p != 'Z' && *p != '+' && *p != '-') {
3242 if ((unsigned char)(*p - '0') > 9)
3243 goto bad_format;
3244
3245 p++;
3246 if ((p - buf) >= buflen)
3247 goto bad_format;
3248 }
3249 }
3250
3251 if (*p == 'Z')
3252 p++;
3253 else
3254 p += 6; /* case of '+00:00 or '-00:00' */
3255
3256 if ((p - buf) >= buflen || *p != ' ')
3257 goto bad_format;
3258 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3259 }
3260 else
3261 goto bad_format;
3262
3263
3264 p++;
3265 if ((p - buf) >= buflen || *p == ' ')
3266 goto bad_format;
3267
3268 metadata[LOG_META_HOST].ptr = p;
3269 while (*p != ' ') {
3270 p++;
3271 if ((p - buf) >= buflen)
3272 goto bad_format;
3273 }
3274 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3275 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3276 metadata[LOG_META_HOST].len = 0;
3277
3278 p++;
3279 if ((p - buf) >= buflen || *p == ' ')
3280 goto bad_format;
3281
3282 metadata[LOG_META_TAG].ptr = p;
3283 while (*p != ' ') {
3284 p++;
3285 if ((p - buf) >= buflen)
3286 goto bad_format;
3287 }
3288 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3289 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3290 metadata[LOG_META_TAG].len = 0;
3291
3292 p++;
3293 if ((p - buf) >= buflen || *p == ' ')
3294 goto bad_format;
3295
3296 metadata[LOG_META_PID].ptr = p;
3297 while (*p != ' ') {
3298 p++;
3299 if ((p - buf) >= buflen)
3300 goto bad_format;
3301 }
3302 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3303 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3304 metadata[LOG_META_PID].len = 0;
3305
3306 p++;
3307 if ((p - buf) >= buflen || *p == ' ')
3308 goto bad_format;
3309
3310 metadata[LOG_META_MSGID].ptr = p;
3311 while (*p != ' ') {
3312 p++;
3313 if ((p - buf) >= buflen)
3314 goto bad_format;
3315 }
3316 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3317 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3318 metadata[LOG_META_MSGID].len = 0;
3319
3320 p++;
3321 if ((p - buf) >= buflen || *p == ' ')
3322 goto bad_format;
3323
3324 /* structured data format is:
3325 * ex:
3326 * '[key1=value1 key2=value2][key3=value3]'
3327 *
3328 * space is invalid outside [] because
3329 * considered as the end of structured data field
3330 */
3331 metadata[LOG_META_STDATA].ptr = p;
3332 if (*p == '[') {
3333 int elem = 0;
3334
3335 while (1) {
3336 if (elem) {
3337 /* according to rfc this char is escaped in param values */
3338 if (*p == ']' && *(p-1) != '\\')
3339 elem = 0;
3340 }
3341 else {
3342 if (*p == '[')
3343 elem = 1;
3344 else if (*p == ' ')
3345 break;
3346 else
3347 goto bad_format;
3348 }
3349 p++;
3350 if ((p - buf) >= buflen)
3351 goto bad_format;
3352 }
3353 }
3354 else if (*p == '-') {
3355 /* case of NILVALUE */
3356 p++;
3357 if ((p - buf) >= buflen || *p != ' ')
3358 goto bad_format;
3359 }
3360 else
3361 goto bad_format;
3362
3363 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3364 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3365 metadata[LOG_META_STDATA].len = 0;
3366
3367 p++;
3368
3369 buflen -= p - buf;
3370 buf = p;
3371
3372 *size = buflen;
3373 *message = p;
3374 }
3375 else if (*size > LOG_LEGACYTIME_LEN) {
3376 int m;
3377
3378 /* supported header format according to rfc3164.
3379 * ex:
3380 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3381 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3382 * or 'Jan 1 00:00:00 HOSTNAME '
3383 * Note: HOSTNAME is mandatory, and day
3384 * of month uses a single space prefix if
3385 * less than 10 to ensure hour offset is
3386 * always the same.
3387 */
3388
3389 /* Check month to see if it correspond to a rfc3164
3390 * header ex 'Jan 1 00:00:00' */
3391 for (m = 0; m < 12; m++)
3392 if (!memcmp(monthname[m], p, 3))
3393 break;
3394 /* Month not found */
3395 if (m == 12)
3396 goto bad_format;
3397
3398 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3399
3400 p += LOG_LEGACYTIME_LEN;
3401 if ((p - buf) >= buflen || *p != ' ')
3402 goto bad_format;
3403
3404 p++;
3405 if ((p - buf) >= buflen || *p == ' ')
3406 goto bad_format;
3407
3408 metadata[LOG_META_HOST].ptr = p;
3409 while (*p != ' ') {
3410 p++;
3411 if ((p - buf) >= buflen)
3412 goto bad_format;
3413 }
3414 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3415
3416 /* TAG seems to no be mandatory */
3417 p++;
3418
3419 buflen -= p - buf;
3420 buf = p;
3421
3422 *size = buflen;
3423 *message = buf;
3424
3425 if (!buflen)
3426 return;
3427
3428 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3429 p++;
3430
3431 /* a tag must present a trailing ':' */
3432 if (((p - buf) >= buflen) || *p != ':')
3433 return;
3434 p++;
3435 /* followed by a space */
3436 if (((p - buf) >= buflen) || *p != ' ')
3437 return;
3438
3439 /* rewind to parse tag and pid */
3440 p = buf;
3441 metadata[LOG_META_TAG].ptr = p;
3442 /* we have the guarantee that ':' will be reach before size limit */
3443 while (*p != ':') {
3444 if (*p == '[') {
3445 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3446 metadata[LOG_META_PID].ptr = p + 1;
3447 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003448 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003449 if (p[1] != ':')
3450 return;
3451 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3452 }
3453 p++;
3454 }
3455 if (!metadata[LOG_META_TAG].len)
3456 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3457
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003458 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003459 p += 2;
3460
3461 buflen -= p - buf;
3462 buf = p;
3463
3464 *size = buflen;
3465 *message = buf;
3466 }
3467
3468 return;
3469
3470bad_format:
3471 /* bad syslog format, we reset all parsed syslog fields
3472 * but priority is kept because we are able to re-build
3473 * this message using LOF_FORMAT_PRIO.
3474 */
3475 metadata[LOG_META_TIME].len = 0;
3476 metadata[LOG_META_HOST].len = 0;
3477 metadata[LOG_META_TAG].len = 0;
3478 metadata[LOG_META_PID].len = 0;
3479 metadata[LOG_META_MSGID].len = 0;
3480 metadata[LOG_META_STDATA].len = 0;
3481
3482 return;
3483}
3484
3485/*
3486 * UDP syslog fd handler
3487 */
3488void syslog_fd_handler(int fd)
3489{
3490 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3491 ssize_t ret = 0;
3492 struct buffer *buf = get_trash_chunk();
3493 size_t size;
3494 char *message;
3495 int level;
3496 int facility;
3497 struct listener *l = objt_listener(fdtab[fd].owner);
3498 int max_accept;
3499
3500 if(!l)
3501 ABORT_NOW();
3502
Willy Tarreauf5090652021-04-06 17:23:40 +02003503 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003504
3505 if (!fd_recv_ready(fd))
3506 return;
3507
3508 max_accept = l->maxaccept ? l->maxaccept : 1;
3509
3510 do {
3511 /* Source address */
3512 struct sockaddr_storage saddr = {0};
3513 socklen_t saddrlen;
3514
3515 saddrlen = sizeof(saddr);
3516
3517 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3518 if (ret < 0) {
3519 if (errno == EINTR)
3520 continue;
3521 if (errno == EAGAIN)
3522 fd_cant_recv(fd);
3523 goto out;
3524 }
3525 buf->data = ret;
3526
Emeric Brun45c457a2020-07-09 23:23:34 +02003527 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003528 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003529 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003530
Emeric Brun54932b42020-07-07 09:43:24 +02003531 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3532
3533 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3534
3535 } while (--max_accept);
3536 }
3537
3538out:
3539 return;
3540}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003541
Emeric Brun12941c82020-07-07 14:19:42 +02003542/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003543 * IO Handler to handle message exchange with a syslog tcp client
3544 */
3545static void syslog_io_handler(struct appctx *appctx)
3546{
3547 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3548 struct stream_interface *si = appctx->owner;
3549 struct stream *s = si_strm(si);
3550 struct proxy *frontend = strm_fe(s);
3551 struct listener *l = strm_li(s);
3552 struct buffer *buf = get_trash_chunk();
3553 int max_accept;
3554 int to_skip;
3555 int facility;
3556 int level;
3557 char *message;
3558 size_t size;
3559
3560 max_accept = l->maxaccept ? l->maxaccept : 1;
3561 while (co_data(si_oc(si))) {
3562 char c;
3563
3564 if (max_accept <= 0)
3565 goto missing_budget;
3566 max_accept--;
3567
3568 to_skip = co_getchar(si_oc(si), &c);
3569 if (!to_skip)
3570 goto missing_data;
3571 else if (to_skip < 0)
3572 goto cli_abort;
3573
3574 if (c == '<') {
3575 /* rfc-6587, Non-Transparent-Framing: messages separated by
3576 * a trailing LF or CR LF
3577 */
3578 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3579 if (!to_skip)
3580 goto missing_data;
3581 else if (to_skip < 0)
3582 goto cli_abort;
3583
3584 if (buf->area[to_skip - 1] != '\n')
3585 goto parse_error;
3586
3587 buf->data = to_skip - 1;
3588
3589 /* according to rfc-6587, some devices adds CR before LF */
3590 if (buf->data && buf->area[buf->data - 1] == '\r')
3591 buf->data--;
3592
3593 }
3594 else if ((unsigned char)(c - '1') <= 8) {
3595 /* rfc-6587, Octet-Counting: message length in ASCII
3596 * (first digit can not be ZERO), followed by a space
3597 * and message length
3598 */
3599 char *p = NULL;
3600 int msglen;
3601
3602 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3603 if (!to_skip)
3604 goto missing_data;
3605 else if (to_skip < 0)
3606 goto cli_abort;
3607
3608 if (buf->area[to_skip - 1] != ' ')
3609 goto parse_error;
3610
3611 msglen = strtol(trash.area, &p, 10);
3612 if (!msglen || p != &buf->area[to_skip - 1])
3613 goto parse_error;
3614
3615 /* message seems too large */
3616 if (msglen > buf->size)
3617 goto parse_error;
3618
3619 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3620 if (!msglen)
3621 goto missing_data;
3622 else if (msglen < 0)
3623 goto cli_abort;
3624
3625
3626 buf->data = msglen;
3627 to_skip += msglen;
3628 }
3629 else
3630 goto parse_error;
3631
3632 co_skip(si_oc(si), to_skip);
3633
3634 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003635 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003636 proxy_inc_fe_req_ctr(l, frontend);
3637
3638 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3639
3640 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3641
3642 }
3643
3644missing_data:
3645 /* we need more data to read */
3646 si_oc(si)->flags |= CF_READ_DONTWAIT;
3647
3648 return;
3649
3650missing_budget:
3651 /* it may remain some stuff to do, let's retry later */
3652 appctx_wakeup(appctx);
3653
3654 return;
3655
3656parse_error:
3657 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003658 _HA_ATOMIC_INC(&l->counters->failed_req);
3659 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003660
3661 goto close;
3662
3663cli_abort:
3664 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003665 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3666 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003667
3668close:
3669 si_shutw(si);
3670 si_shutr(si);
3671
3672 si_ic(si)->flags |= CF_READ_NULL;
3673
3674 return;
3675}
3676
3677static struct applet syslog_applet = {
3678 .obj_type = OBJ_TYPE_APPLET,
3679 .name = "<SYSLOG>", /* used for logging */
3680 .fct = syslog_io_handler,
3681 .release = NULL,
3682};
3683
3684/*
Emeric Brun12941c82020-07-07 14:19:42 +02003685 * Parse "log-forward" section and create corresponding sink buffer.
3686 *
3687 * The function returns 0 in success case, otherwise, it returns error
3688 * flags.
3689 */
3690int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3691{
3692 int err_code = 0;
3693 struct proxy *px;
3694 char *errmsg = NULL;
3695 const char *err = NULL;
3696
3697 if (strcmp(args[0], "log-forward") == 0) {
3698 if (!*args[1]) {
3699 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3700 err_code |= ERR_ALERT | ERR_ABORT;
3701 goto out;
3702 }
3703
3704 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3705 goto out;
3706
3707 err = invalid_char(args[1]);
3708 if (err) {
3709 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3710 file, linenum, *err, args[0], args[1]);
3711 err_code |= ERR_ALERT | ERR_ABORT;
3712 goto out;
3713 }
3714
Emeric Brunb0c331f2020-10-07 17:05:59 +02003715 px = log_forward_by_name(args[1]);
3716 if (px) {
3717 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3718 file, linenum, args[1], px->conf.file, px->conf.line);
3719 err_code |= ERR_ALERT | ERR_FATAL;
3720 }
3721
3722 px = proxy_find_by_name(args[1], 0, 0);
3723 if (px) {
3724 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3725 file, linenum, args[1], proxy_type_str(px),
3726 px->id, px->conf.file, px->conf.line);
3727 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003728 }
3729
3730 px = calloc(1, sizeof *px);
3731 if (!px) {
3732 err_code |= ERR_ALERT | ERR_FATAL;
3733 goto out;
3734 }
3735
3736 px->next = cfg_log_forward;
3737 cfg_log_forward = px;
3738
3739 init_new_proxy(px);
3740 px->conf.file = strdup(file);
3741 px->conf.line = linenum;
3742 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003743 px->last_change = now.tv_sec;
3744 px->cap = PR_CAP_FE;
3745 px->maxconn = 10;
3746 px->timeout.client = TICK_ETERNITY;
3747 px->accept = frontend_accept;
3748 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003749 px->id = strdup(args[1]);
3750
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003751 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003752 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003753 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3754 err_code |= ERR_WARN;
3755
3756 if (*(args[1]) == 0) {
3757 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3758 err_code |= ERR_ALERT | ERR_FATAL;
3759 goto out;
3760 }
3761 cfg_log_forward->maxconn = atol(args[1]);
3762 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3763 goto out;
3764 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003765 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003766 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3767 err_code |= ERR_WARN;
3768
3769 if (*(args[1]) == 0) {
3770 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3771 err_code |= ERR_ALERT | ERR_FATAL;
3772 goto out;
3773 }
3774 cfg_log_forward->backlog = atol(args[1]);
3775 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3776 goto out;
3777 }
3778 else if (strcmp(args[0], "bind") == 0) {
3779 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003780 struct bind_conf *bind_conf;
3781 struct bind_kw *kw;
3782 struct listener *l;
3783
3784 cur_arg = 1;
3785
3786 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3787 NULL, xprt_get(XPRT_RAW));
3788 if (!bind_conf) {
3789 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3790 err_code |= ERR_ALERT | ERR_FATAL;
3791 goto out;
3792 }
3793
3794 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3795 if (errmsg && *errmsg) {
3796 indent_msg(&errmsg, 2);
3797 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3798 }
3799 else {
3800 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3801 file, linenum, args[0], args[1], args[2]);
3802 err_code |= ERR_ALERT | ERR_FATAL;
3803 goto out;
3804 }
3805 }
3806 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau66161322021-02-19 15:50:27 +01003807 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003808 l->accept = session_accept_fd;
3809 l->analysers |= cfg_log_forward->fe_req_ana;
3810 l->default_target = cfg_log_forward->default_target;
3811 global.maxsock++;
3812 }
3813 cur_arg++;
3814
3815 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3816 int ret;
3817
3818 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3819 err_code |= ret;
3820 if (ret) {
3821 if (errmsg && *errmsg) {
3822 indent_msg(&errmsg, 2);
3823 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3824 }
3825 else
3826 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3827 file, linenum, args[cur_arg]);
3828 if (ret & ERR_FATAL)
3829 goto out;
3830 }
3831 cur_arg += 1 + kw->skip;
3832 }
3833 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003834 const char *best = bind_find_best_kw(args[cur_arg]);
3835 if (best)
3836 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3837 file, linenum, args[cur_arg], cursection, best);
3838 else
3839 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3840 file, linenum, args[cur_arg], cursection);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003841 err_code |= ERR_ALERT | ERR_FATAL;
3842 goto out;
3843 }
Emeric Brun12941c82020-07-07 14:19:42 +02003844 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003845 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003846 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02003847 struct bind_conf *bind_conf;
3848 struct bind_kw *kw;
3849 struct listener *l;
3850
3851 cur_arg = 1;
3852
3853 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3854 NULL, xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +02003855 if (!bind_conf) {
3856 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3857 err_code |= ERR_ALERT | ERR_FATAL;
3858 goto out;
3859 }
Emeric Brun12941c82020-07-07 14:19:42 +02003860
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003861 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003862 if (errmsg && *errmsg) {
3863 indent_msg(&errmsg, 2);
3864 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3865 }
3866 else {
3867 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3868 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003869 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003870 err_code |= ERR_ALERT | ERR_FATAL;
3871 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003872 }
3873 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003874 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreau66161322021-02-19 15:50:27 +01003875 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreaue140a692020-10-15 21:25:32 +02003876 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003877 global.maxsock++;
3878 }
3879 cur_arg++;
3880
3881 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3882 int ret;
3883
3884 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3885 err_code |= ret;
3886 if (ret) {
3887 if (errmsg && *errmsg) {
3888 indent_msg(&errmsg, 2);
3889 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3890 }
3891 else
3892 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3893 file, linenum, args[cur_arg]);
3894 if (ret & ERR_FATAL)
3895 goto out;
3896 }
3897 cur_arg += 1 + kw->skip;
3898 }
3899 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003900 const char *best = bind_find_best_kw(args[cur_arg]);
3901 if (best)
3902 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3903 file, linenum, args[cur_arg], cursection, best);
3904 else
3905 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3906 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02003907 err_code |= ERR_ALERT | ERR_FATAL;
3908 goto out;
3909 }
3910 }
3911 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02003912 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003913 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3914 err_code |= ERR_ALERT | ERR_FATAL;
3915 goto out;
3916 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003917 }
3918 else if (strcmp(args[0], "timeout") == 0) {
3919 const char *res;
3920 unsigned timeout;
3921
3922 if (strcmp(args[1], "client") != 0) {
3923 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
3924 err_code |= ERR_ALERT | ERR_FATAL;
3925 goto out;
3926 }
3927
3928 if (*args[2] == 0) {
3929 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
3930 err_code |= ERR_ALERT | ERR_FATAL;
3931 goto out;
3932 }
3933 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3934 if (res == PARSE_TIME_OVER) {
3935 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
3936 }
3937 else if (res == PARSE_TIME_UNDER) {
3938 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
3939 }
3940 else if (res) {
3941 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
3942 return -1;
3943 }
3944
3945 if (res) {
3946 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3947 err_code |= ERR_ALERT | ERR_FATAL;
3948 goto out;
3949 }
3950 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02003951 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02003952 else {
3953 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
3954 err_code |= ERR_ALERT | ERR_ABORT;
3955 goto out;
3956 }
Emeric Brun12941c82020-07-07 14:19:42 +02003957out:
3958 return err_code;
3959}
3960
Willy Tarreau0108d902018-11-25 19:14:37 +01003961
Emeric Brun12941c82020-07-07 14:19:42 +02003962/* config parsers for this section */
3963REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
3964
Willy Tarreau082b6282019-05-22 14:42:12 +02003965REGISTER_PER_THREAD_ALLOC(init_log_buffers);
3966REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01003967
Willy Tarreaubaaee002006-06-26 02:48:02 +02003968/*
3969 * Local variables:
3970 * c-indent-level: 8
3971 * c-basic-offset: 8
3972 * End:
3973 */