blob: 916212ee7e97aaa7403a6bec04f05b4db21b9d8a [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 Tarreaubaaee002006-06-26 02:48:02 +020014#include <stdarg.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <syslog.h>
19#include <time.h>
20#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010021#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022
23#include <sys/time.h>
Willy Tarreau077edcb2016-08-10 18:30:56 +020024#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020026#include <haproxy/api.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020027#include <haproxy/applet.h>
Emeric Brun12941c82020-07-07 14:19:42 +020028#include <haproxy/cfgparse.h>
Willy Tarreau55542642021-10-08 09:33:24 +020029#include <haproxy/clock.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>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020034#include <haproxy/http_ana.h>
Emeric Brun12941c82020-07-07 14:19:42 +020035#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036#include <haproxy/log.h>
Emeric Brun12941c82020-07-07 14:19:42 +020037#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020039#include <haproxy/sc_strm.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/sink.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020041#include <haproxy/ssl_sock.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020042#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020043#include <haproxy/stream.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020044#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020045#include <haproxy/tools.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Emeric Brun45c457a2020-07-09 23:23:34 +020047/* global recv logs counter */
48int cum_log_messages;
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
Emeric Brun12941c82020-07-07 14:19:42 +020050/* log forward proxy list */
51struct proxy *cfg_log_forward;
52
Emeric Brun54648852020-07-06 15:54:06 +020053struct log_fmt_st {
Dragan Dosen43885c72015-10-01 13:18:13 +020054 char *name;
Dragan Dosen43885c72015-10-01 13:18:13 +020055};
56
Emeric Brun54648852020-07-06 15:54:06 +020057static const struct log_fmt_st log_formats[LOG_FORMATS] = {
Emeric Brun0237c4e2020-11-27 16:24:34 +010058 [LOG_FORMAT_LOCAL] = {
59 .name = "local",
60 },
Dragan Dosen43885c72015-10-01 13:18:13 +020061 [LOG_FORMAT_RFC3164] = {
62 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020063 },
64 [LOG_FORMAT_RFC5424] = {
65 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020066 },
67 [LOG_FORMAT_PRIO] = {
68 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010069 },
70 [LOG_FORMAT_SHORT] = {
71 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020072 },
73 [LOG_FORMAT_TIMED] = {
74 .name = "timed",
75 },
76 [LOG_FORMAT_ISO] = {
77 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010078 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010079 [LOG_FORMAT_RAW] = {
80 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010081 },
Dragan Dosen1322d092015-09-22 16:05:32 +020082};
83
Dragan Dosen835b9212016-02-12 13:23:03 +010084/*
85 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020086 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
87 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
88 * that the byte should be escaped. Be careful to always pass bytes from 0 to
89 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010090 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020091long rfc5424_escape_map[(256/8) / sizeof(long)];
92long hdr_encode_map[(256/8) / sizeof(long)];
93long url_encode_map[(256/8) / sizeof(long)];
94long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010095
Dragan Dosen835b9212016-02-12 13:23:03 +010096
Willy Tarreaubaaee002006-06-26 02:48:02 +020097const char *log_facilities[NB_LOG_FACILITIES] = {
98 "kern", "user", "mail", "daemon",
99 "auth", "syslog", "lpr", "news",
100 "uucp", "cron", "auth2", "ftp",
101 "ntp", "audit", "alert", "cron2",
102 "local0", "local1", "local2", "local3",
103 "local4", "local5", "local6", "local7"
104};
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106const char *log_levels[NB_LOG_LEVELS] = {
107 "emerg", "alert", "crit", "err",
108 "warning", "notice", "info", "debug"
109};
110
Willy Tarreau570f2212013-06-10 16:42:09 +0200111const 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 +0200112const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113
William Lallemand723b73a2012-02-08 16:37:49 +0100114
115/* log_format */
116struct logformat_type {
117 char *name;
118 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100119 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200120 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100121 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
William Lallemand723b73a2012-02-08 16:37:49 +0100122};
123
William Lallemandb7ff6a32012-03-02 14:35:21 +0100124int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
125
William Lallemand723b73a2012-02-08 16:37:49 +0100126/* log_format variable names */
127static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200128 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100129
130 /* please keep these lines sorted ! */
131 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
132 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
133 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
134 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100135 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200136 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200137 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200138 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100139 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200140 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
141 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
142 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
143 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
144 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
145 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200146 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100147 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200148 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000149 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100150 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
151 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200152 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100153 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200154 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100155 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
156 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200157 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200158 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
159 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100160 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
161 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200162 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
163 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100164 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200165 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
166 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
167 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
168 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000169 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
Maciej Zdebfcdfd852020-11-30 18:27:47 +0000170 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP relative or absolute path */
171 { "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 +0000172 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000173 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
174 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200175 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100176 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200177 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100178 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
179 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100180 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100181 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
182 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
183 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
184 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
185 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200186 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
187 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100188 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200189 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
190 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
191 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100192 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
193 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
William Lallemand5e19a282012-04-02 16:22:10 +0200194 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100195};
196
William Lallemandbe6a8732022-12-22 15:37:01 +0100197char httpclient_log_format[] = "%ci:%cp [%tr] %ft -/- %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r";
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200198char 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
Willy Tarreau68574dd2021-11-05 19:14:55 +0100199char 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_err]/%[ssl_fc_err,hex]/%[ssl_c_err]/%[ssl_c_ca_err]/%[ssl_fc_is_resumed] %[ssl_fc_sni]/%sslv/%sslc";
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200200char 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 +0100201char 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 +0100202char *log_format = NULL;
203
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200204/* Default string used for structured-data part in RFC5424 formatted
205 * syslog messages.
206 */
207char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200208
Willy Tarreau13ef7732018-11-12 07:25:28 +0100209/* total number of dropped logs */
210unsigned int dropped_logs = 0;
211
Dragan Dosen59cee972015-09-19 22:09:02 +0200212/* This is a global syslog message buffer, common to all outgoing
213 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100214 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200215THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100216
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200217/* A global syslog message buffer, common to all RFC5424 syslog messages.
218 * Currently, it is used for generating the structured-data part.
219 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200220THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200221
William Lallemand723b73a2012-02-08 16:37:49 +0100222struct logformat_var_args {
223 char *name;
224 int mask;
225};
226
227struct logformat_var_args var_args_list[] = {
228// global
229 { "M", LOG_OPT_MANDATORY },
230 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200231 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100232 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100233 { 0, 0 }
234};
235
236/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100237 * callback used to configure addr source retrieval
238 */
239int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
240{
241 curproxy->options2 |= PR_O2_SRC_ADDR;
242
243 return 0;
244}
245
246
247/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100248 * Parse args in a logformat_var. Returns 0 in error
249 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100250 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100251int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100252{
253 int i = 0;
254 int end = 0;
255 int flags = 0; // 1 = + 2 = -
256 char *sp = NULL; // start pointer
257
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100258 if (args == NULL) {
259 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100260 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100261 }
William Lallemand723b73a2012-02-08 16:37:49 +0100262
263 while (1) {
264 if (*args == '\0')
265 end = 1;
266
267 if (*args == '+') {
268 // add flag
269 sp = args + 1;
270 flags = 1;
271 }
272 if (*args == '-') {
273 // delete flag
274 sp = args + 1;
275 flags = 2;
276 }
277
278 if (*args == '\0' || *args == ',') {
279 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100280 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100281 if (strcmp(sp, var_args_list[i].name) == 0) {
282 if (flags == 1) {
283 node->options |= var_args_list[i].mask;
284 break;
285 } else if (flags == 2) {
286 node->options &= ~var_args_list[i].mask;
287 break;
288 }
289 }
290 }
291 sp = NULL;
292 if (end)
293 break;
294 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100295 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100296 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100297 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100298}
299
300/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100301 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
302 * must pass the args part in the <arg> pointer with its length in <arg_len>,
303 * and varname with its length in <var> and <var_len> respectively. <arg> is
304 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100305 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100306 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100307int 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 +0100308{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100309 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200310 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100311
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100312 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
313 if (strlen(logformat_keywords[j].name) == var_len &&
314 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
315 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200316 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100317 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100318 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200319 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100320 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100321 node->type = logformat_keywords[j].type;
322 node->options = *defoptions;
323 if (arg_len) {
324 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100325 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200326 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100327 }
328 if (node->type == LOG_FMT_GLOBAL) {
329 *defoptions = node->options;
330 free(node->arg);
331 free(node);
332 } else {
333 if (logformat_keywords[j].config_callback &&
334 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200335 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100336 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100337 curproxy->to_log |= logformat_keywords[j].lw;
Willy Tarreau2b718102021-04-21 07:32:39 +0200338 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100339 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100340 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100341 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100342 memprintf(err, "format variable '%s' is reserved for HTTP mode",
343 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200344 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100345 }
William Lallemand723b73a2012-02-08 16:37:49 +0100346 }
347 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100348
349 j = var[var_len];
350 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100351 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 +0100352 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200353
354 error_free:
355 if (node) {
356 free(node->arg);
357 free(node);
358 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100359 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100360}
361
362/*
363 * push to the logformat linked list
364 *
365 * start: start pointer
366 * end: end text pointer
367 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100368 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100369 *
370 * LOG_TEXT: copy chars from start to end excluding end.
371 *
372*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100373int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100374{
375 char *str;
376
Willy Tarreaua3571662012-12-20 21:59:12 +0100377 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200378 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100379 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100380 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100381 return 0;
382 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200383 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100384 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100385 str[end - start] = '\0';
386 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100387 node->type = LOG_FMT_TEXT; // type string
Willy Tarreau2b718102021-04-21 07:32:39 +0200388 LIST_APPEND(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100389 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200390 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100391 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100392 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100393 return 0;
394 }
William Lallemand1d705562012-03-12 12:46:41 +0100395 node->type = LOG_FMT_SEPARATOR;
Willy Tarreau2b718102021-04-21 07:32:39 +0200396 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100397 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100398 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100399}
400
401/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100402 * Parse the sample fetch expression <text> and add a node to <list_format> upon
403 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100404 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
405 * is passed in <endptr>, it will be updated with the pointer to the first character
406 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100407 *
408 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100409 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100410int 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 +0100411{
412 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200413 struct sample_expr *expr = NULL;
414 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100415 int cmd_arg;
416
417 cmd[0] = text;
418 cmd[1] = "";
419 cmd_arg = 0;
420
Christopher Fauleteaba25d2021-09-30 16:22:51 +0200421 expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err,
Christopher Faulet6ff7de52021-10-13 15:18:36 +0200422 &curpx->conf.args, endptr);
Willy Tarreauc8368452012-12-21 00:09:23 +0100423 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100424 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200425 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100426 }
427
Vincent Bernat02779b62016-04-03 13:48:43 +0200428 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100429 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100430 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200431 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100432 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100433 node->type = LOG_FMT_EXPR;
434 node->expr = expr;
435 node->options = options;
436
437 if (arg_len) {
438 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100439 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200440 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100441 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100442 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100443 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
444
Willy Tarreau434c57c2013-01-08 01:10:24 +0100445 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100446 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
447
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100448 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100449 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
450 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200451 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100452 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100453
Christopher Faulet581db2b2021-03-26 10:02:46 +0100454 if ((options & LOG_OPT_HTTP) && (expr->fetch->use & (SMP_USE_L6REQ|SMP_USE_L6RES))) {
455 ha_warning("parsing [%s:%d] : L6 sample fetch <%s> ignored in HTTP log-format string.\n",
456 curpx->conf.args.file, curpx->conf.args.line, text);
457 }
458
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200459 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100460 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100461 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100462
William Lallemand65ad6e12014-01-31 15:08:02 +0100463 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
464 * needed with some sample fetches (eg: ssl*). We always set it for
465 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100466 */
467 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200468 if (curpx->http_needed)
469 curpx->to_log |= LW_REQ;
Willy Tarreau2b718102021-04-21 07:32:39 +0200470 LIST_APPEND(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100471 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200472
473 error_free:
474 release_sample_expr(expr);
475 if (node) {
476 free(node->arg);
477 free(node);
478 }
479 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100480}
481
482/*
William Lallemand723b73a2012-02-08 16:37:49 +0100483 * Parse the log_format string and fill a linked list.
484 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200485 * You can set arguments using { } : %{many arguments}varname.
486 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100487 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500488 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100489 * curproxy: the proxy affected
490 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100491 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100492 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100493 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100494 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100495 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100496int 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 +0100497{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100498 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100499 char *arg = NULL; /* start pointer for args */
500 char *var = NULL; /* start pointer for vars */
501 int arg_len = 0;
502 int var_len = 0;
503 int cformat; /* current token format */
504 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100505 struct logformat_node *tmplf, *back;
506
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100507 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100508 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100509 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100510 return 0;
511 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200512 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200513
William Lallemand723b73a2012-02-08 16:37:49 +0100514 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100515 list_for_each_entry_safe(tmplf, back, list_format, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200516 LIST_DELETE(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200517 release_sample_expr(tmplf->expr);
518 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100519 free(tmplf);
520 }
521
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100522 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100523 pformat = cformat;
524
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100525 if (!*str)
526 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100527
Joseph Herlant85b40592018-11-15 12:10:04 -0800528 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100529 * second have all common paths processed at one place. The common paths are the ones
530 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
531 * We use the common LF_INIT state to dispatch to the different final states.
532 */
533 switch (pformat) {
534 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100535 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100536 arg_len = var_len = 0;
537 if (*str == '{') { // optional argument
538 cformat = LF_STARG;
539 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100540 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100541 else if (*str == '[') {
542 cformat = LF_STEXPR;
543 var = str + 1; // store expr in variable name
544 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100545 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100546 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100547 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100548 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100549 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500550 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100551 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100552 /* single '%' followed by blank or digit, send them both */
553 cformat = LF_TEXT;
554 pformat = LF_TEXT; /* finally we include the previous char as well */
555 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600556 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 +0100557 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100558 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100559
560 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100561 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500562 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100563 break;
564
565 case LF_STARG: // text immediately following '%{'
566 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100567 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100568 arg_len = str - arg;
569 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100570 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100571 break;
572
573 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100574 if (*str == '[') {
575 cformat = LF_STEXPR;
576 var = str + 1; // store expr in variable name
577 break;
578 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100579 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100580 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100581 var = str;
582 break;
583 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100584 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100585 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100586
Willy Tarreauc8368452012-12-21 00:09:23 +0100587 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100588 /* the whole sample expression is parsed at once,
589 * returning the pointer to the first character not
590 * part of the expression, which MUST be the trailing
591 * angle bracket.
592 */
593 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
594 goto fail;
595
596 if (*str == ']') {
597 // end of arg, go on with next state
598 cformat = pformat = LF_EDEXPR;
599 sp = str;
600 }
601 else {
602 char c = *str;
603 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100604 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100605 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
606 else
607 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200608 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100609 }
610 break;
611
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100612 case LF_VAR: // text part of a variable name
613 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100614 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100615 cformat = LF_INIT; // not variable name anymore
616 break;
617
Willy Tarreauc8368452012-12-21 00:09:23 +0100618 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100619 cformat = LF_INIT;
620 }
621
622 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
623 switch (*str) {
624 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100625 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200626 case ' ':
627 if (options & LOG_OPT_MERGE_SPACES) {
628 cformat = LF_SEPARATOR;
629 break;
630 }
Willy Tarreau80f9a632022-11-14 07:22:57 +0100631 __fallthrough;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100632 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100633 }
634 }
635
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100636 if (cformat != pformat || pformat == LF_SEPARATOR) {
637 switch (pformat) {
638 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100639 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100640 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100641 break;
642 case LF_TEXT:
643 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100644 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100645 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100646 break;
647 }
648 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100649 }
650 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100651
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100652 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100653 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100654 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100655 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100656 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100657
658 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100659 fail:
660 free(backfmt);
661 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100662}
663
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200664/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500665 * 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 +0200666 * ranges of indexes. Note that an index may be considered as a particular range
667 * with a high limit to the low limit.
668 */
669int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
670{
671 char *end, *p;
672
673 *low = *high = 0;
674
675 p = *arg;
676 end = strchr(p, ',');
677 if (!end)
678 end = p + strlen(p);
679
680 *high = *low = read_uint((const char **)&p, end);
681 if (!*low || (p != end && *p != '-'))
682 goto err;
683
684 if (p == end)
685 goto done;
686
687 p++;
688 *high = read_uint((const char **)&p, end);
689 if (!*high || *high <= *low || p != end)
690 goto err;
691
692 done:
693 if (*end == ',')
694 end++;
695 *arg = end;
696 return 1;
697
698 err:
699 memprintf(err, "wrong sample range '%s'", *arg);
700 return 0;
701}
702
703/*
704 * Returns 1 if the range defined by <low> and <high> overlaps
705 * one of them in <rgs> array of ranges with <sz> the size of this
706 * array, 0 if not.
707 */
708int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
709 unsigned int low, unsigned int high, char **err)
710{
711 size_t i;
712
713 for (i = 0; i < sz; i++) {
714 if ((low >= rgs[i].low && low <= rgs[i].high) ||
715 (high >= rgs[i].low && high <= rgs[i].high)) {
716 memprintf(err, "ranges are overlapping");
717 return 1;
718 }
719 }
720
721 return 0;
722}
723
724int smp_log_range_cmp(const void *a, const void *b)
725{
726 const struct smp_log_range *rg_a = a;
727 const struct smp_log_range *rg_b = b;
728
729 if (rg_a->high < rg_b->low)
730 return -1;
731 else if (rg_a->low > rg_b->high)
732 return 1;
733
734 return 0;
735}
736
Willy Tarreau211ea252022-03-17 19:47:33 +0100737/* frees log server <logsrv> after freeing all of its allocated fields. The
738 * server must not belong to a list anymore. Logsrv may be NULL, which is
739 * silently ignored.
740 */
741void free_logsrv(struct logsrv *logsrv)
742{
743 if (!logsrv)
744 return;
745
746 BUG_ON(LIST_INLIST(&logsrv->list));
747 ha_free(&logsrv->conf.file);
748 ha_free(&logsrv->ring_name);
749 free(logsrv);
750}
751
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200752/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200753 * Parse "log" keyword and update <logsrvs> list accordingly.
754 *
755 * When <do_del> is set, it means the "no log" line was parsed, so all log
756 * servers in <logsrvs> are released.
757 *
758 * Otherwise, we try to parse the "log" line. First of all, when the list is not
759 * the global one, we look for the parameter "global". If we find it,
760 * global.logsrvs is copied. Else we parse each arguments.
761 *
762 * The function returns 1 in success case, otherwise, it returns 0 and err is
763 * filled.
764 */
Emeric Brun9533a702021-04-02 10:13:43 +0200765int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200766{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100767 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200768 struct sockaddr_storage *sk;
Emeric Brun94aab062021-04-02 10:41:36 +0200769 struct protocol *proto;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200770 struct logsrv *logsrv = NULL;
771 int port1, port2;
772 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200773 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200774
775 /*
776 * "no log": delete previous herited or defined syslog
777 * servers.
778 */
779 if (do_del) {
780 struct logsrv *back;
781
782 if (*(args[1]) != 0) {
783 memprintf(err, "'no log' does not expect arguments");
784 goto error;
785 }
786
787 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
Willy Tarreau211ea252022-03-17 19:47:33 +0100788 LIST_DEL_INIT(&logsrv->list);
789 free_logsrv(logsrv);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200790 }
791 return 1;
792 }
793
794 /*
795 * "log global": copy global.logrsvs linked list to the end of logsrvs
796 * list. But first, we check (logsrvs != global.logsrvs).
797 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100798 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200799 if (logsrvs == &global.logsrvs) {
800 memprintf(err, "'global' is not supported for a global syslog server");
801 goto error;
802 }
803 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200804 struct logsrv *node;
805
806 list_for_each_entry(node, logsrvs, list) {
807 if (node->ref == logsrv)
808 goto skip_logsrv;
809 }
810
811 node = malloc(sizeof(*node));
Aurelien DARRAGON256d5812023-05-11 18:57:23 +0200812 if (!node) {
813 memprintf(err, "out of memory error");
814 goto error;
815 }
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200816 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200817 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200818 LIST_INIT(&node->list);
Willy Tarreau2b718102021-04-21 07:32:39 +0200819 LIST_APPEND(logsrvs, &node->list);
Willy Tarreau211ea252022-03-17 19:47:33 +0100820 node->ring_name = logsrv->ring_name ? strdup(logsrv->ring_name) : NULL;
Emeric Brun9533a702021-04-02 10:13:43 +0200821 node->conf.file = strdup(file);
822 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200823
824 skip_logsrv:
825 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200826 }
827 return 1;
828 }
829
830 /*
831 * "log <address> ...: parse a syslog server line
832 */
833 if (*(args[1]) == 0 || *(args[2]) == 0) {
834 memprintf(err, "expects <address> and <facility> %s as arguments",
835 ((logsrvs == &global.logsrvs) ? "" : "or global"));
836 goto error;
837 }
838
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100839 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
840 if (strcmp(args[1], "stdout") == 0)
841 args[1] = "fd@1";
842 else if (strcmp(args[1], "stderr") == 0)
843 args[1] = "fd@2";
844
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200845 logsrv = calloc(1, sizeof(*logsrv));
846 if (!logsrv) {
847 memprintf(err, "out of memory");
848 goto error;
849 }
Christopher Fauletb4f96ed2022-03-29 14:17:09 +0200850 LIST_INIT(&logsrv->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200851 logsrv->conf.file = strdup(file);
852 logsrv->conf.line = linenum;
853
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200854 /* skip address for now, it will be parsed at the end */
855 cur_arg = 2;
856
857 /* just after the address, a length may be specified */
858 logsrv->maxlen = MAX_SYSLOG_LEN;
859 if (strcmp(args[cur_arg], "len") == 0) {
860 int len = atoi(args[cur_arg+1]);
861 if (len < 80 || len > 65535) {
862 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
863 args[cur_arg+1]);
864 goto error;
865 }
866 logsrv->maxlen = len;
867 cur_arg += 2;
868 }
869 if (logsrv->maxlen > global.max_syslog_len)
870 global.max_syslog_len = logsrv->maxlen;
871
872 /* after the length, a format may be specified */
873 if (strcmp(args[cur_arg], "format") == 0) {
874 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200875 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200876 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
877 goto error;
878 }
879 cur_arg += 2;
880 }
881
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200882 if (strcmp(args[cur_arg], "sample") == 0) {
883 unsigned low, high;
884 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200885 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
886
887 p = args[cur_arg+1];
888 smp_sz_str = strchr(p, ':');
889 if (!smp_sz_str) {
890 memprintf(err, "Missing sample size");
891 goto error;
892 }
893
894 *smp_sz_str++ = '\0';
895
896 end = p + strlen(p);
897
898 while (p != end) {
899 if (!get_logsrv_smp_range(&low, &high, &p, err))
900 goto error;
901
902 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
903 goto error;
904
905 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
906 if (!smp_rgs) {
907 memprintf(err, "out of memory error");
908 goto error;
909 }
910
911 smp_rgs[smp_rgs_sz].low = low;
912 smp_rgs[smp_rgs_sz].high = high;
913 smp_rgs[smp_rgs_sz].sz = high - low + 1;
914 smp_rgs[smp_rgs_sz].curr_idx = 0;
915 if (smp_rgs[smp_rgs_sz].high > smp_sz)
916 smp_sz = smp_rgs[smp_rgs_sz].high;
917 smp_rgs_sz++;
918 }
919
Tim Duesterhus21648002019-06-23 22:10:10 +0200920 if (smp_rgs == NULL) {
921 memprintf(err, "no sampling ranges given");
922 goto error;
923 }
924
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200925 beg = smp_sz_str;
926 end = beg + strlen(beg);
927 new_smp_sz = read_uint((const char **)&beg, end);
928 if (!new_smp_sz || beg != end) {
929 memprintf(err, "wrong sample size '%s' for sample range '%s'",
930 smp_sz_str, args[cur_arg+1]);
931 goto error;
932 }
933
934 if (new_smp_sz < smp_sz) {
935 memprintf(err, "sample size %zu should be greater or equal to "
936 "%zu the maximum of the high ranges limits",
937 new_smp_sz, smp_sz);
938 goto error;
939 }
940 smp_sz = new_smp_sz;
941
942 /* Let's order <smp_rgs> array. */
943 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
944
945 logsrv->lb.smp_rgs = smp_rgs;
946 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
947 logsrv->lb.smp_sz = smp_sz;
948
949 cur_arg += 2;
950 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200951 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200952 /* parse the facility */
953 logsrv->facility = get_log_facility(args[cur_arg]);
954 if (logsrv->facility < 0) {
955 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
956 goto error;
957 }
958 cur_arg++;
959
960 /* parse the max syslog level (default: debug) */
961 logsrv->level = 7;
962 if (*(args[cur_arg])) {
963 logsrv->level = get_log_level(args[cur_arg]);
964 if (logsrv->level < 0) {
965 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
966 goto error;
967 }
968 cur_arg++;
969 }
970
971 /* parse the limit syslog level (default: emerg) */
972 logsrv->minlvl = 0;
973 if (*(args[cur_arg])) {
974 logsrv->minlvl = get_log_level(args[cur_arg]);
975 if (logsrv->minlvl < 0) {
976 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
977 goto error;
978 }
979 cur_arg++;
980 }
981
982 /* Too many args */
983 if (*(args[cur_arg])) {
984 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
985 goto error;
986 }
987
988 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +0200989 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +0200990 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +0200991 logsrv->addr.ss_family = AF_UNSPEC;
992 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +0200993 logsrv->sink = NULL;
994 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +0200995 goto done;
996 }
997
Emeric Brun94aab062021-04-02 10:41:36 +0200998 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
999 err, NULL, NULL,
1000 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 +02001001 if (!sk)
1002 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001003
1004 if (fd != -1)
1005 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001006 logsrv->addr = *sk;
1007
1008 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001009 if (!port1)
1010 set_host_port(&logsrv->addr, SYSLOG_PORT);
1011 }
Emeric Brun9533a702021-04-02 10:13:43 +02001012
Willy Tarreau91b47262022-05-20 16:36:46 +02001013 if (proto && proto->xprt_type == PROTO_TYPE_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +02001014 static unsigned long ring_ids;
1015
1016 /* Implicit sink buffer will be
1017 * initialized in post_check
1018 */
1019 logsrv->type = LOG_TARGET_BUFFER;
1020 logsrv->sink = NULL;
1021 /* compute uniq name for the ring */
1022 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
1023 }
1024
Willy Tarreauc046d162019-08-30 15:24:59 +02001025 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001026 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001027 return 1;
1028
1029 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001030 free(smp_rgs);
Willy Tarreau211ea252022-03-17 19:47:33 +01001031 free_logsrv(logsrv);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001032 return 0;
1033}
1034
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001035
1036/*
Emeric Brun54648852020-07-06 15:54:06 +02001037 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001038 */
Emeric Brun54648852020-07-06 15:54:06 +02001039enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001040{
Emeric Brun54648852020-07-06 15:54:06 +02001041 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001042
1043 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001044 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001045 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001046 format--;
1047
Emeric Brun54648852020-07-06 15:54:06 +02001048 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001049 return format;
1050}
1051
1052/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001053 * returns log level for <lev> or -1 if not found.
1054 */
1055int get_log_level(const char *lev)
1056{
1057 int level;
1058
1059 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001060 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001061 level--;
1062
1063 return level;
1064}
1065
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066/*
1067 * returns log facility for <fac> or -1 if not found.
1068 */
1069int get_log_facility(const char *fac)
1070{
1071 int facility;
1072
1073 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001074 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001075 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001076
Willy Tarreaubaaee002006-06-26 02:48:02 +02001077 return facility;
1078}
1079
William Lallemanda1cc3812012-02-08 16:38:44 +01001080/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001081 * Encode the string.
1082 *
1083 * When using the +E log format option, it will try to escape '"\]'
1084 * characters with '\' as prefix. The same prefix should not be used as
1085 * <escape>.
1086 */
1087static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001088 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001089 const char *string,
1090 struct logformat_node *node)
1091{
1092 if (node->options & LOG_OPT_ESC) {
1093 if (start < stop) {
1094 stop--; /* reserve one byte for the final '\0' */
1095 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001096 if (!ha_bit_test((unsigned char)(*string), map)) {
1097 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001098 *start++ = *string;
1099 else {
1100 if (start + 2 >= stop)
1101 break;
1102 *start++ = '\\';
1103 *start++ = *string;
1104 }
1105 }
1106 else {
1107 if (start + 3 >= stop)
1108 break;
1109 *start++ = escape;
1110 *start++ = hextab[(*string >> 4) & 15];
1111 *start++ = hextab[*string & 15];
1112 }
1113 string++;
1114 }
1115 *start = '\0';
1116 }
1117 }
1118 else {
1119 return encode_string(start, stop, escape, map, string);
1120 }
1121
1122 return start;
1123}
1124
1125/*
1126 * Encode the chunk.
1127 *
1128 * When using the +E log format option, it will try to escape '"\]'
1129 * characters with '\' as prefix. The same prefix should not be used as
1130 * <escape>.
1131 */
1132static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001133 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001134 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001135 struct logformat_node *node)
1136{
1137 char *str, *end;
1138
1139 if (node->options & LOG_OPT_ESC) {
1140 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001141 str = chunk->area;
1142 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001143
1144 stop--; /* reserve one byte for the final '\0' */
1145 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001146 if (!ha_bit_test((unsigned char)(*str), map)) {
1147 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001148 *start++ = *str;
1149 else {
1150 if (start + 2 >= stop)
1151 break;
1152 *start++ = '\\';
1153 *start++ = *str;
1154 }
1155 }
1156 else {
1157 if (start + 3 >= stop)
1158 break;
1159 *start++ = escape;
1160 *start++ = hextab[(*str >> 4) & 15];
1161 *start++ = hextab[*str & 15];
1162 }
1163 str++;
1164 }
1165 *start = '\0';
1166 }
1167 }
1168 else {
1169 return encode_chunk(start, stop, escape, map, chunk);
1170 }
1171
1172 return start;
1173}
1174
1175/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001176 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001177 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001178 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001179 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001180 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001181char *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 +01001182{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001183 if (size < 2)
1184 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001185
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001186 if (node->options & LOG_OPT_QUOTE) {
1187 *(dst++) = '"';
1188 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001189 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001190
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001191 if (src && len) {
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001192 /* escape_string and strlcpy2 will both try to add terminating NULL-byte
1193 * to dst, so we need to make sure that extra byte will fit into dst
1194 * before calling them
1195 */
Dragan Dosen835b9212016-02-12 13:23:03 +01001196 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001197 char *ret;
1198
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001199 ret = escape_string(dst, (dst + size - 1), '\\', rfc5424_escape_map, src, src + len);
Dragan Dosen835b9212016-02-12 13:23:03 +01001200 if (ret == NULL || *ret != '\0')
1201 return NULL;
1202 len = ret - dst;
1203 }
1204 else {
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001205 if (++len > size)
1206 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001207 len = strlcpy2(dst, src, len);
1208 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001209
1210 size -= len;
1211 dst += len;
1212 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001213 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1214 if (size < 2)
1215 return NULL;
1216 *(dst++) = '-';
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001217 size -= 1;
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001218 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001219
1220 if (node->options & LOG_OPT_QUOTE) {
1221 if (size < 2)
1222 return NULL;
1223 *(dst++) = '"';
1224 }
1225
1226 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001227 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001228}
1229
Willy Tarreau26ffa852018-09-05 15:23:10 +02001230static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001231{
1232 return lf_text_len(dst, src, size, size, node);
1233}
1234
William Lallemand5f232402012-04-05 18:02:55 +02001235/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001236 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001237 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001238 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001239char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001240{
1241 char *ret = dst;
1242 int iret;
1243 char pn[INET6_ADDRSTRLEN];
1244
1245 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001246 unsigned char *addr = NULL;
1247 switch (sockaddr->sa_family) {
1248 case AF_INET:
1249 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1250 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1251 break;
1252 case AF_INET6:
1253 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1254 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1255 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1256 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1257 break;
1258 default:
1259 return NULL;
1260 }
William Lallemand5f232402012-04-05 18:02:55 +02001261 if (iret < 0 || iret > size)
1262 return NULL;
1263 ret += iret;
1264 } else {
1265 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1266 ret = lf_text(dst, pn, size, node);
1267 if (ret == NULL)
1268 return NULL;
1269 }
1270 return ret;
1271}
1272
1273/*
1274 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001275 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001276 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001277char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001278{
1279 char *ret = dst;
1280 int iret;
1281
1282 if (node->options & LOG_OPT_HEXA) {
1283 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1284 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1285 if (iret < 0 || iret > size)
1286 return NULL;
1287 ret += iret;
1288 } else {
1289 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1290 if (ret == NULL)
1291 return NULL;
1292 }
1293 return ret;
1294}
1295
Emeric Brun54648852020-07-06 15:54:06 +02001296
1297/*
1298 * This function sends the syslog message using a printf format string. It
1299 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001300 */
Emeric Brun54648852020-07-06 15:54:06 +02001301void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001302{
Emeric Brun54648852020-07-06 15:54:06 +02001303 va_list argp;
1304 int data_len;
1305
1306 if (level < 0 || format == NULL || logline == NULL)
1307 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001308
Emeric Brun54648852020-07-06 15:54:06 +02001309 va_start(argp, format);
1310 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1311 if (data_len < 0 || data_len > global.max_syslog_len)
1312 data_len = global.max_syslog_len;
1313 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001314
Emeric Brun54648852020-07-06 15:54:06 +02001315 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1316 logline, data_len, default_rfc5424_sd_log_format, 2);
1317}
1318/*
1319 * This function builds a log header of given format using given
1320 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1321 * to determine format based on given metadas. It is useful
1322 * for log-forwarding to be able to forward any format without
1323 * settings.
1324 * This function returns a struct ist array of elements of the header
1325 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001326 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001327 * elements.
1328 */
1329struct ist *build_log_header(enum log_fmt format, int level, int facility,
1330 struct ist *metadata, size_t *nbelem)
1331{
1332 static THREAD_LOCAL struct {
1333 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1334 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1335 time_t cur_legacy_time;
1336 char priority_buffer[6];
1337 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001338
Emeric Brun54648852020-07-06 15:54:06 +02001339 struct tm logtime;
1340 int len;
1341 int fac_level = 0;
1342 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001343
Emeric Brun54648852020-07-06 15:54:06 +02001344 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001345
Emeric Brun54648852020-07-06 15:54:06 +02001346
1347 if (format == LOG_FORMAT_UNSPEC) {
1348 format = LOG_FORMAT_RAW;
1349 if (metadata) {
1350 /* If a hostname is set, it appears we want to perform syslog
1351 * because only rfc5427 or rfc3164 support an hostname.
1352 */
1353 if (metadata[LOG_META_HOST].len) {
1354 /* If a rfc5424 compliant timestamp is used we consider
1355 * that output format is rfc5424, else legacy format
1356 * is used as specified default for local logs
1357 * in documentation.
1358 */
1359 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1360 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1361 format = LOG_FORMAT_RFC5424;
1362 else
1363 format = LOG_FORMAT_RFC3164;
1364 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001365 else if (metadata[LOG_META_TAG].len) {
1366 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001367 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001368 * in legacy format (analog to RFC3164 but
1369 * with stripped hostname).
1370 */
1371 format = LOG_FORMAT_LOCAL;
1372 }
Emeric Brun54648852020-07-06 15:54:06 +02001373 else if (metadata[LOG_META_PRIO].len) {
1374 /* the source seems a parsed message
1375 * offering a valid level/prio prefix
1376 * so we consider this format.
1377 */
1378 format = LOG_FORMAT_PRIO;
1379 }
1380 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001381 }
1382
Emeric Brun54648852020-07-06 15:54:06 +02001383 /* prepare priority, stored into 1 single elem */
1384 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001385 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001386 case LOG_FORMAT_RFC3164:
1387 case LOG_FORMAT_RFC5424:
1388 case LOG_FORMAT_PRIO:
1389 fac_level = facility << 3;
1390 /* further format ignore the facility */
Willy Tarreau80f9a632022-11-14 07:22:57 +01001391 __fallthrough;
Emeric Brun54648852020-07-06 15:54:06 +02001392 case LOG_FORMAT_TIMED:
1393 case LOG_FORMAT_SHORT:
1394 fac_level += level;
1395 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1396 do {
1397 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1398 fac_level /= 10;
1399 hdr_ctx.ist_vector[*nbelem].ptr--;
1400 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1401 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1402 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1403 break;
1404 case LOG_FORMAT_ISO:
1405 case LOG_FORMAT_RAW:
1406 break;
1407 case LOG_FORMAT_UNSPEC:
1408 case LOG_FORMATS:
1409 ABORT_NOW();
1410 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001411
William Lallemand2a4a44f2012-02-06 16:00:33 +01001412
Emeric Brun54648852020-07-06 15:54:06 +02001413 /* prepare timestamp, stored into a max of 4 elems */
1414 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001415 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001416 case LOG_FORMAT_RFC3164:
1417 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1418 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1419 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1420 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001421 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001422 break;
1423 }
1424 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1425 int month;
1426 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001427
Emeric Brun54648852020-07-06 15:54:06 +02001428 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001429
Emeric Brun54648852020-07-06 15:54:06 +02001430 /* compute month */
1431 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1432 if (month)
1433 month--;
1434 if (month <= 11) {
1435 /* builds log prefix ex: 'Jan 1 ' */
1436 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1437 "%s %c%c ", monthname[month],
1438 timestamp[8] != '0' ? timestamp[8] : ' ',
1439 timestamp[9]);
1440 /* we reused the timestamp_buffer, signal that it does not
1441 * contain local time anymore
1442 */
1443 hdr_ctx.cur_legacy_time = 0;
1444 if (len == 7) {
1445 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1446 /* adds 'HH:MM:SS' from iso time */
1447 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1448 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1449 /* we successfully reuse iso time, we can break */
1450 break;
1451 }
1452 }
1453 /* Failed to reuse isotime time, fallback to local legacy time */
1454 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001455
Emeric Brun54648852020-07-06 15:54:06 +02001456 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1457 /* re-builds timestamp from the current local time */
1458 get_localtime(time, &logtime);
1459
1460 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1461 "%s %2d %02d:%02d:%02d ",
1462 monthname[logtime.tm_mon],
1463 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1464 if (len != LOG_LEGACYTIME_LEN+1)
1465 hdr_ctx.cur_legacy_time = 0;
1466 else
1467 hdr_ctx.cur_legacy_time = time;
1468 }
1469 if (likely(hdr_ctx.cur_legacy_time))
1470 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1471 else
1472 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1473 break;
1474 case LOG_FORMAT_RFC5424:
1475 /* adds rfc5425 version prefix */
1476 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1477 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001478 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001479 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1480 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1481 break;
1482 }
1483 /* let continue as 'timed' and 'iso' format for usual timestamp */
Willy Tarreau80f9a632022-11-14 07:22:57 +01001484 __fallthrough;
Emeric Brun54648852020-07-06 15:54:06 +02001485 case LOG_FORMAT_TIMED:
1486 case LOG_FORMAT_ISO:
1487 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1488 * '1900:01:01T14:00:00+02:00'
1489 * '1900:01:01T10:00:00.123456-02:00'
1490 */
1491 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1492 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1493 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001494 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001495 break;
1496 }
1497 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1498 int month;
1499 char *timestamp = metadata[LOG_META_TIME].ptr;
1500
1501 for (month = 0; month < 12; month++)
1502 if (!memcmp(monthname[month], timestamp, 3))
1503 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001504
Emeric Brun54648852020-07-06 15:54:06 +02001505 if (month < 12) {
1506
1507 /* get local time to retrieve year */
1508 get_localtime(time, &logtime);
1509
1510 /* year seems changed since log */
1511 if (logtime.tm_mon < month)
1512 logtime.tm_year--;
1513
1514 /* builds rfc5424 prefix ex: '1900-01-01T' */
1515 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1516 "%4d-%02d-%c%cT",
1517 logtime.tm_year+1900, month+1,
1518 timestamp[4] != ' ' ? timestamp[4] : '0',
1519 timestamp[5]);
1520
1521 /* we reused the timestamp_buffer, signal that it does not
1522 * contain local time anymore
1523 */
1524 hdr_ctx.cur_legacy_time = 0;
1525 if (len == 11) {
1526 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1527 /* adds HH:MM:SS from legacy timestamp */
1528 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001529 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001530 /* according to rfc: -00:00 means we don't know the timezone */
1531 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1532 /* we successfully reuse legacy time, we can break */
1533 break;
1534 }
1535 }
1536 /* Failed to reuse legacy time, fallback to local iso time */
1537 }
1538 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1539 break;
1540 case LOG_FORMAT_PRIO:
1541 case LOG_FORMAT_SHORT:
1542 case LOG_FORMAT_RAW:
1543 break;
1544 case LOG_FORMAT_UNSPEC:
1545 case LOG_FORMATS:
1546 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001547 }
1548
Emeric Brun54648852020-07-06 15:54:06 +02001549 /* prepare other meta data, stored into a max of 10 elems */
1550 switch (format) {
1551 case LOG_FORMAT_RFC3164:
1552 if (metadata && metadata[LOG_META_HOST].len) {
1553 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1554 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1555 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001556 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001557 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Willy Tarreau80f9a632022-11-14 07:22:57 +01001558 __fallthrough;
Emeric Brun0237c4e2020-11-27 16:24:34 +01001559 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001560 if (!metadata || !metadata[LOG_META_TAG].len)
1561 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001562
Emeric Brun54648852020-07-06 15:54:06 +02001563 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1564 if (metadata[LOG_META_PID].len) {
1565 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1566 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1567 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1568 }
1569 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1570 break;
1571 case LOG_FORMAT_RFC5424:
1572 if (metadata && metadata[LOG_META_HOST].len) {
1573 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1574 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1575 }
1576 else
1577 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001578
Emeric Brun54648852020-07-06 15:54:06 +02001579 if (metadata && metadata[LOG_META_TAG].len) {
1580 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1581 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1582 }
1583 else
1584 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001585
Emeric Brun54648852020-07-06 15:54:06 +02001586 if (metadata && metadata[LOG_META_PID].len) {
1587 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1588 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1589 }
1590 else
1591 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001592
Emeric Brun54648852020-07-06 15:54:06 +02001593 if (metadata && metadata[LOG_META_MSGID].len) {
1594 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1595 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1596 }
1597 else
1598 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001599
Emeric Brun54648852020-07-06 15:54:06 +02001600 if (metadata && metadata[LOG_META_STDATA].len) {
1601 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1602 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1603 }
1604 else
1605 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1606 break;
1607 case LOG_FORMAT_PRIO:
1608 case LOG_FORMAT_SHORT:
1609 case LOG_FORMAT_TIMED:
1610 case LOG_FORMAT_ISO:
1611 case LOG_FORMAT_RAW:
1612 break;
1613 case LOG_FORMAT_UNSPEC:
1614 case LOG_FORMATS:
1615 ABORT_NOW();
1616 }
1617
1618 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001619}
1620
1621/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001622 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001623 * The argument <metadata> MUST be an array of size
1624 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001625 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001626 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001627 */
Emeric Brun54648852020-07-06 15:54:06 +02001628static 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 +01001629{
Emeric Brun54648852020-07-06 15:54:06 +02001630 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001631 static THREAD_LOCAL struct msghdr msghdr = {
1632 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001633 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001634 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001635 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1636 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001637 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001638 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001639 size_t nbelem;
1640 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001641
1642 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001643
Emeric Brunfa9d7802020-05-28 14:21:33 +02001644 /* historically some messages used to already contain the trailing LF
1645 * or Zero. Let's remove all trailing LF or Zero
1646 */
Emeric Brun54648852020-07-06 15:54:06 +02001647 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001648 size--;
1649
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001650 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001651 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001652 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001653 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001654 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1655 /* the socket's address is a file descriptor */
1656 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1657 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001658 else if (logsrv->addr.ss_family == AF_UNIX)
1659 plogfd = &logfdunix;
1660 else
1661 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001662
Willy Tarreauc046d162019-08-30 15:24:59 +02001663 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001664 /* socket not successfully initialized yet */
1665 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1666 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1667 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001668
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001669 if (!once) {
1670 once = 1; /* note: no need for atomic ops here */
1671 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1672 nblogger, strerror(errno), errno);
1673 }
1674 return;
1675 } else {
1676 /* we don't want to receive anything on this socket */
1677 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1678 /* does nothing under Linux, maybe needed for others */
1679 shutdown(*plogfd, SHUT_RD);
Willy Tarreau38247432022-04-26 10:24:14 +02001680 fd_set_cloexec(*plogfd);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001681 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001682 }
1683
Emeric Brun54648852020-07-06 15:54:06 +02001684 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1685 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001686 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001687 struct ist msg;
1688
1689 msg = ist2(message, size);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01001690 msg = isttrim(msg, logsrv->maxlen);
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001691
Christopher Fauletcc640e82022-10-19 08:16:48 +02001692 sent = sink_write(logsrv->sink, &msg, 1, level, facility, metadata);
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001693 }
1694 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1695 struct ist msg;
1696
1697 msg = ist2(message, size);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01001698 msg = isttrim(msg, logsrv->maxlen);
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001699
1700 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001701 }
1702 else {
Emeric Brun54648852020-07-06 15:54:06 +02001703 int i = 0;
Aurelien DARRAGONa0211342023-06-27 11:32:06 +02001704 int totlen = logsrv->maxlen - 1; /* save space for the final '\n' */
Emeric Brun54648852020-07-06 15:54:06 +02001705
1706 for (i = 0 ; i < nbelem ; i++ ) {
1707 iovec[i].iov_base = msg_header[i].ptr;
1708 iovec[i].iov_len = msg_header[i].len;
1709 if (totlen <= iovec[i].iov_len) {
1710 iovec[i].iov_len = totlen;
1711 totlen = 0;
1712 break;
1713 }
1714 totlen -= iovec[i].iov_len;
1715 }
1716 if (totlen) {
1717 iovec[i].iov_base = message;
1718 iovec[i].iov_len = size;
1719 if (totlen <= iovec[i].iov_len)
1720 iovec[i].iov_len = totlen;
1721 i++;
1722 }
1723 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1724 iovec[i].iov_len = 1;
1725 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001726
Emeric Brun54648852020-07-06 15:54:06 +02001727 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001728 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1729 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001730
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001731 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1732 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001733
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001734 if (sent < 0) {
1735 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001736
Willy Tarreauacef5e22022-04-25 20:32:15 +02001737 if (errno == EAGAIN || errno == EWOULDBLOCK)
Willy Tarreau4781b152021-04-06 13:53:36 +02001738 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001739 else if (!once) {
1740 once = 1; /* note: no need for atomic ops here */
1741 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1742 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001743 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001744 }
1745}
Dragan Dosen59cee972015-09-19 22:09:02 +02001746
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001747/*
1748 * This function sends a syslog message.
1749 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001750 * The argument <metadata> MUST be an array of size
1751 * LOG_META_FIELDS*sizeof(struct ist) containing
1752 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001753 */
Emeric Brun54648852020-07-06 15:54:06 +02001754void process_send_log(struct list *logsrvs, int level, int facility,
1755 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001756{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001757 struct logsrv *logsrv;
1758 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001759
1760 /* Send log messages to syslog server. */
1761 nblogger = 0;
1762 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001763 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001764
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001765 /* we can filter the level of the messages that are sent to each logger */
1766 if (level > logsrv->level)
1767 continue;
1768
Frédéric Lécailled803e472019-04-25 07:42:09 +02001769 if (logsrv->lb.smp_rgs) {
1770 struct smp_log_range *curr_rg;
1771
1772 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1773 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1774 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1775 if (in_range) {
1776 /* Let's consume this range. */
1777 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1778 if (!curr_rg->curr_idx) {
1779 /* If consumed, let's select the next range. */
1780 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1781 }
1782 }
1783 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1784 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1785 }
1786 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001787 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1788 (facility == -1) ? logsrv->facility : facility,
1789 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001790 }
1791}
1792
Emeric Brun54648852020-07-06 15:54:06 +02001793/*
1794 * This function sends a syslog message.
1795 * It doesn't care about errors nor does it report them.
1796 * The arguments <sd> and <sd_size> are used for the structured-data part
1797 * in RFC5424 formatted syslog messages.
1798 */
1799void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1800 char *message, size_t size, char *sd, size_t sd_size)
1801{
1802 static THREAD_LOCAL pid_t curr_pid;
1803 static THREAD_LOCAL char pidstr[16];
1804 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1805
1806 if (logsrvs == NULL) {
1807 if (!LIST_ISEMPTY(&global.logsrvs)) {
1808 logsrvs = &global.logsrvs;
1809 }
1810 }
1811 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1812 return;
1813
1814 if (!metadata[LOG_META_HOST].len) {
1815 if (global.log_send_hostname)
Tim Duesterhus77508502022-03-15 13:11:06 +01001816 metadata[LOG_META_HOST] = ist(global.log_send_hostname);
Emeric Brun54648852020-07-06 15:54:06 +02001817 }
1818
1819 if (!tagb || !tagb->area)
1820 tagb = &global.log_tag;
1821
1822 if (tagb)
1823 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1824
1825 if (unlikely(curr_pid != getpid()))
1826 metadata[LOG_META_PID].len = 0;
1827
1828 if (!metadata[LOG_META_PID].len) {
1829 curr_pid = getpid();
1830 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1831 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1832 }
1833
1834 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1835
1836 /* Remove trailing space of structured data */
1837 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1838 metadata[LOG_META_STDATA].len--;
1839
1840 return process_send_log(logsrvs, level, -1, metadata, message, size);
1841}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001842
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001843const 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 +01001844const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1845 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1846 Set-cookie Updated, unknown, unknown */
1847
William Lallemand1d705562012-03-12 12:46:41 +01001848/*
1849 * try to write a character if there is enough space, or goto out
1850 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001851#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001852 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001853 *(tmplog++) = (x); \
1854 } else { \
1855 goto out; \
1856 } \
1857 } while(0)
1858
Dragan Dosen835b9212016-02-12 13:23:03 +01001859
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001860/* Initializes some log data at boot */
1861static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001862{
1863 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001864 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001865
1866 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1867 * inside PARAM-VALUE should be escaped with '\' as prefix.
1868 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1869 * details.
1870 */
1871 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1872
1873 tmp = "\"\\]";
1874 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001875 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001876 tmp++;
1877 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001878
1879 /* initialize the log header encoding map : '{|}"#' should be encoded with
1880 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1881 * URL encoding only requires '"', '#' to be encoded as well as non-
1882 * printable characters above.
1883 */
1884 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1885 memset(url_encode_map, 0, sizeof(url_encode_map));
1886 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001887 ha_bit_set(i, hdr_encode_map);
1888 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001889 }
1890 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001891 ha_bit_set(i, hdr_encode_map);
1892 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001893 }
1894
1895 tmp = "\"#{|}";
1896 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001897 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001898 tmp++;
1899 }
1900
1901 tmp = "\"#";
1902 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001903 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001904 tmp++;
1905 }
1906
1907 /* initialize the http header encoding map. The draft httpbis define the
1908 * header content as:
1909 *
1910 * HTTP-message = start-line
1911 * *( header-field CRLF )
1912 * CRLF
1913 * [ message-body ]
1914 * header-field = field-name ":" OWS field-value OWS
1915 * field-value = *( field-content / obs-fold )
1916 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1917 * obs-fold = CRLF 1*( SP / HTAB )
1918 * field-vchar = VCHAR / obs-text
1919 * VCHAR = %x21-7E
1920 * obs-text = %x80-FF
1921 *
1922 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1923 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08001924 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02001925 */
1926 memset(http_encode_map, 0, sizeof(http_encode_map));
1927 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001928 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001929 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001930 ha_bit_set(i, http_encode_map);
1931 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001932}
William Lallemand1d705562012-03-12 12:46:41 +01001933
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001934INITCALL0(STG_PREPARE, init_log);
1935
Christopher Faulet0132d062017-07-26 15:33:35 +02001936/* Initialize log buffers used for syslog messages */
1937int init_log_buffers()
1938{
Christopher Faulet0132d062017-07-26 15:33:35 +02001939 logline = my_realloc2(logline, global.max_syslog_len + 1);
1940 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02001941 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02001942 return 0;
1943 return 1;
1944}
1945
1946/* Deinitialize log buffers used for syslog messages */
1947void deinit_log_buffers()
1948{
Christopher Faulet0132d062017-07-26 15:33:35 +02001949 free(logline);
1950 free(logline_rfc5424);
Christopher Faulet0132d062017-07-26 15:33:35 +02001951 logline = NULL;
1952 logline_rfc5424 = NULL;
1953}
1954
Willy Tarreaudf974472012-12-28 02:44:01 +01001955/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1956 * <maxsize> characters. Returns the size of the output string in characters,
1957 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001958 * is not zero. It requires a valid session and optionally a stream. If the
1959 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01001960 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001961int 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 +02001962{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001963 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001964 struct proxy *be;
1965 struct http_txn *txn;
1966 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001967 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001968 unsigned int s_flags;
1969 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02001970 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001971 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001972 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001973 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001974 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001975 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001976 int t_request;
1977 int hdr;
1978 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001979 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001980 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001981 char *ret;
1982 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001983 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001984 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001985 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001986 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00001987 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001988 struct http_uri_parser parser;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001989
William Lallemandbddd4fd2012-02-27 11:23:10 +01001990 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001991
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001992 if (likely(s)) {
1993 be = s->be;
1994 txn = s->txn;
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001995 be_conn = sc_conn(s->scb);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001996 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001997 s_flags = s->flags;
1998 uniq_id = s->uniq_id;
1999 logs = &s->logs;
2000 } else {
2001 /* we have no stream so we first need to initialize a few
2002 * things that are needed later. We do increment the request
2003 * ID so that it's uniquely assigned to this request just as
2004 * if the request had reached the point of being processed.
2005 * A request error is reported as it's the only element we have
2006 * here and which justifies emitting such a log.
2007 */
Christopher Fauletfd818482021-04-14 14:01:41 +02002008 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002009 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002010 fe_conn = objt_conn(sess->origin);
Willy Tarreaubde14ad2022-05-27 10:04:04 +02002011 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? sc_conn(__objt_check(sess->origin)->sc) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002012 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002013 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02002014 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002015
2016 /* prepare a valid log structure */
Willy Tarreauad5a5f62023-04-27 09:46:02 +02002017 tmp_strm_log.accept_ts = sess->accept_ts;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002018 tmp_strm_log.accept_date = sess->accept_date;
2019 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02002020 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreauad5a5f62023-04-27 09:46:02 +02002021 tmp_strm_log.request_ts = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002022 tmp_strm_log.t_queue = -1;
2023 tmp_strm_log.t_connect = -1;
2024 tmp_strm_log.t_data = -1;
Willy Tarreau69530f52023-04-28 09:16:15 +02002025 tmp_strm_log.t_close = ns_to_ms(now_ns - sess->accept_ts);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002026 tmp_strm_log.bytes_in = 0;
2027 tmp_strm_log.bytes_out = 0;
2028 tmp_strm_log.prx_queue_pos = 0;
2029 tmp_strm_log.srv_queue_pos = 0;
2030
2031 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002032
2033 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002034 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, &status);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002035
2036 switch (es) {
2037 case MUX_ES_SUCCESS:
2038 break;
2039 case MUX_ES_INVALID_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002040 status = (status ? status : 400);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002041 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2042 s_flags = SF_ERR_CLICL | SF_FINST_R;
2043 else
2044 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2045 break;
2046 case MUX_ES_TOUT_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002047 status = (status ? status : 408);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002048 s_flags = SF_ERR_CLITO | SF_FINST_R;
2049 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002050 case MUX_ES_NOTIMPL_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002051 status = (status ? status : 501);
Christopher Faulet142dd332020-12-07 11:24:37 +01002052 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2053 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002054 case MUX_ES_INTERNAL_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002055 status = (status ? status : 500);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002056 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2057 break;
2058 default:
2059 break;
2060 }
2061 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002062 }
2063
William Lallemandbddd4fd2012-02-27 11:23:10 +01002064 t_request = -1;
Willy Tarreauad5a5f62023-04-27 09:46:02 +02002065 if ((llong)(logs->request_ts - logs->accept_ts) >= 0)
2066 t_request = ns_to_ms(logs->request_ts - logs->accept_ts);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002067
William Lallemand1d705562012-03-12 12:46:41 +01002068 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002069
William Lallemandbddd4fd2012-02-27 11:23:10 +01002070 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002071 if (LIST_ISEMPTY(list_format))
2072 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002073
William Lallemand1d705562012-03-12 12:46:41 +01002074 list_for_each_entry(tmp, list_format, list) {
Christopher Faulet52b28d22021-10-27 11:58:05 +02002075#ifdef USE_OPENSSL
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002076 struct connection *conn;
Christopher Faulet52b28d22021-10-27 11:58:05 +02002077#endif
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002078 const struct sockaddr_storage *addr;
Willy Tarreau4f653562012-10-12 19:48:16 +02002079 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002080 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002081 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002082
Willy Tarreauc8368452012-12-21 00:09:23 +01002083 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002084 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002085 if (!last_isspace) {
2086 LOGCHAR(' ');
2087 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002088 }
2089 break;
2090
William Lallemand1d705562012-03-12 12:46:41 +01002091 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002092 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002093 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002094 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002095 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002096 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002097 last_isspace = 0;
2098 break;
2099
Willy Tarreauc8368452012-12-21 00:09:23 +01002100 case LOG_FMT_EXPR: // sample expression, may be request or response
2101 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002102 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002103 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 +02002104
Christopher Faulet5f940702020-04-06 10:40:02 +02002105 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002106 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 +02002107
2108 if (!key && !(tmp->options & (LOG_OPT_REQ_CAP|LOG_OPT_RES_CAP))) // cfg, cli
2109 key = sample_fetch_as_type(be, sess, s, SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
2110
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002111 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002112 ret = lf_encode_chunk(tmplog, dst + maxsize,
2113 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002114 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002115 ret = lf_text_len(tmplog,
2116 key ? key->data.u.str.area : NULL,
2117 key ? key->data.u.str.data : 0,
2118 dst + maxsize - tmplog,
2119 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002120 if (ret == 0)
2121 goto out;
2122 tmplog = ret;
2123 last_isspace = 0;
2124 break;
2125
Willy Tarreau2beef582012-12-20 17:22:52 +01002126 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreaud68ff012022-05-27 08:57:21 +02002127 addr = (s ? sc_src(s->scf) : sess_src(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002128 if (addr)
2129 ret = lf_ip(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002130 else
2131 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002132
William Lallemand1d705562012-03-12 12:46:41 +01002133 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002134 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002135 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002136 last_isspace = 0;
2137 break;
2138
Willy Tarreau2beef582012-12-20 17:22:52 +01002139 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreaud68ff012022-05-27 08:57:21 +02002140 addr = (s ? sc_src(s->scf) : sess_src(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002141 if (addr) {
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002142 /* sess->listener is always defined when the session's owner is an inbound connections */
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002143 if (addr->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002144 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002145 else
2146 ret = lf_port(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002147 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002148 else
2149 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2150
William Lallemand5f232402012-04-05 18:02:55 +02002151 if (ret == NULL)
2152 goto out;
2153 tmplog = ret;
2154 last_isspace = 0;
2155 break;
2156
Willy Tarreau2beef582012-12-20 17:22:52 +01002157 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreaud68ff012022-05-27 08:57:21 +02002158 addr = (s ? sc_dst(s->scf) : sess_dst(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002159 if (addr)
2160 ret = lf_ip(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002161 else
2162 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2163
William Lallemand1d705562012-03-12 12:46:41 +01002164 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002165 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002166 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002167 last_isspace = 0;
2168 break;
2169
Willy Tarreau2beef582012-12-20 17:22:52 +01002170 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreaud68ff012022-05-27 08:57:21 +02002171 addr = (s ? sc_dst(s->scf) : sess_dst(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002172 if (addr) {
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002173 /* sess->listener is always defined when the session's owner is an inbound connections */
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002174 if (addr->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002175 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002176 else
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002177 ret = lf_port(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002178 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002179 else
2180 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2181
William Lallemand5f232402012-04-05 18:02:55 +02002182 if (ret == NULL)
2183 goto out;
2184 tmplog = ret;
2185 last_isspace = 0;
2186 break;
2187
Willy Tarreau2beef582012-12-20 17:22:52 +01002188 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002189 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002190 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002191 else
2192 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2193
William Lallemand1d705562012-03-12 12:46:41 +01002194 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002195 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002196 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002197 last_isspace = 0;
2198 break;
2199
Willy Tarreau2beef582012-12-20 17:22:52 +01002200 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002201 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002202 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002203 else
2204 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2205
William Lallemand5f232402012-04-05 18:02:55 +02002206 if (ret == NULL)
2207 goto out;
2208 tmplog = ret;
2209 last_isspace = 0;
2210 break;
2211
Willy Tarreau2beef582012-12-20 17:22:52 +01002212 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002213 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002214 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002215 else
2216 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2217
William Lallemand5f232402012-04-05 18:02:55 +02002218 if (ret == NULL)
2219 goto out;
2220 tmplog = ret;
2221 last_isspace = 0;
2222 break;
2223
Willy Tarreau2beef582012-12-20 17:22:52 +01002224 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002225 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002226 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002227 else
2228 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2229
William Lallemand1d705562012-03-12 12:46:41 +01002230 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002231 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002232 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002233 last_isspace = 0;
2234 break;
2235
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002236 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002237 get_localtime(logs->accept_date.tv_sec, &tm);
2238 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002239 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002240 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002241 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002242 last_isspace = 0;
2243 break;
2244
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002245 case LOG_FMT_tr: // %tr = start of request date
2246 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002247 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 +02002248 get_localtime(tv.tv_sec, &tm);
2249 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2250 if (ret == NULL)
2251 goto out;
2252 tmplog = ret;
2253 last_isspace = 0;
2254 break;
2255
2256 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002257 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002258 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002259 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002260 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002261 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002262 last_isspace = 0;
2263 break;
2264
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002265 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002266 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 +02002267 get_gmtime(tv.tv_sec, &tm);
2268 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2269 if (ret == NULL)
2270 goto out;
2271 tmplog = ret;
2272 last_isspace = 0;
2273 break;
2274
2275 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002276 get_localtime(logs->accept_date.tv_sec, &tm);
2277 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002278 if (ret == NULL)
2279 goto out;
2280 tmplog = ret;
2281 last_isspace = 0;
2282 break;
2283
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002284 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002285 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 +02002286 get_localtime(tv.tv_sec, &tm);
2287 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2288 if (ret == NULL)
2289 goto out;
2290 tmplog = ret;
2291 last_isspace = 0;
2292 break;
2293
William Lallemand5f232402012-04-05 18:02:55 +02002294 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002295 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002296 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002297 if (iret < 0 || iret > dst + maxsize - tmplog)
2298 goto out;
2299 last_isspace = 0;
2300 tmplog += iret;
2301 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002302 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002303 if (ret == NULL)
2304 goto out;
2305 tmplog = ret;
2306 last_isspace = 0;
2307 }
2308 break;
2309
William Lallemand1d705562012-03-12 12:46:41 +01002310 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002311 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002312 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002313 if (iret < 0 || iret > dst + maxsize - tmplog)
2314 goto out;
2315 last_isspace = 0;
2316 tmplog += iret;
2317 } else {
2318 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002319 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002320 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002321 tmplog, 4);
2322 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002323 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002324 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002325 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002326 }
2327 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002328
William Lallemand1d705562012-03-12 12:46:41 +01002329 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002330 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002331 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002332 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002333 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002334 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002335 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002336 break;
2337
Willy Tarreau773d65f2012-10-12 14:56:11 +02002338 case LOG_FMT_FRONTEND_XPRT: // %ft
2339 src = fe->id;
2340 if (tmp->options & LOG_OPT_QUOTE)
2341 LOGCHAR('"');
2342 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2343 if (iret == 0)
2344 goto out;
2345 tmplog += iret;
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002346
2347 /* sess->listener may be undefined if the session's owner is a health-check */
Willy Tarreau807a3a52022-04-12 08:08:33 +02002348 if (sess->listener && sess->listener->bind_conf->xprt->get_ssl_sock_ctx)
Willy Tarreau773d65f2012-10-12 14:56:11 +02002349 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002350 if (tmp->options & LOG_OPT_QUOTE)
2351 LOGCHAR('"');
2352 last_isspace = 0;
2353 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002354#ifdef USE_OPENSSL
2355 case LOG_FMT_SSL_CIPHER: // %sslc
2356 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002357 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002358 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002359 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002360 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002361 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2362 if (ret == NULL)
2363 goto out;
2364 tmplog = ret;
2365 last_isspace = 0;
2366 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002367
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002368 case LOG_FMT_SSL_VERSION: // %sslv
2369 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002370 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002371 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002372 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002373 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002374 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2375 if (ret == NULL)
2376 goto out;
2377 tmplog = ret;
2378 last_isspace = 0;
2379 break;
2380#endif
William Lallemand1d705562012-03-12 12:46:41 +01002381 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002382 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002383 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002384 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002385 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002386 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002387 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002388 break;
2389
William Lallemand1d705562012-03-12 12:46:41 +01002390 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002391 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002392 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002393 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002394 break;
2395 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002396 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002397 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002398 case OBJ_TYPE_CHECK:
2399 src = (__objt_check(sess->origin)->server
2400 ? __objt_check(sess->origin)->server->id
2401 : "<NOSRV>");
2402 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002403 default:
2404 src = "<NOSRV>";
2405 break;
2406 }
William Lallemand5f232402012-04-05 18:02:55 +02002407 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002408 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002409 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002410 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002411 last_isspace = 0;
2412 break;
2413
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002414 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002415 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002416 if (ret == NULL)
2417 goto out;
2418 tmplog = ret;
2419 last_isspace = 0;
2420 break;
2421
2422 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002423 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002424 if (ret == NULL)
2425 goto out;
2426 tmplog = ret;
2427 last_isspace = 0;
2428 break;
2429
2430 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002431 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002432 tmplog, dst + maxsize - tmplog);
2433 if (ret == NULL)
2434 goto out;
2435 tmplog = ret;
2436 last_isspace = 0;
2437 break;
2438
2439 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002440 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002441 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002442 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002443 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002444 last_isspace = 0;
2445 break;
2446
William Lallemand1d705562012-03-12 12:46:41 +01002447 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002448 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002449 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002450 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002451 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002452 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002453 last_isspace = 0;
2454 break;
2455
William Lallemand1d705562012-03-12 12:46:41 +01002456 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002457 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002458 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002459 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002460 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002461 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002462 last_isspace = 0;
2463 break;
2464
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002465 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002466 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002467 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002468 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002469 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002470 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002471 last_isspace = 0;
2472 break;
2473
Willy Tarreau27b639d2016-05-17 17:55:27 +02002474 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002475 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002476 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002477 tmplog, dst + maxsize - tmplog);
2478 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002479 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002480 tmplog, dst + maxsize - tmplog);
2481 if (ret == NULL)
2482 goto out;
2483 tmplog = ret;
2484 last_isspace = 0;
2485 break;
2486
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002487 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2488 if (!(fe->to_log & LW_BYTES))
2489 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002490 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 +02002491 tmplog, dst + maxsize - tmplog);
2492 if (ret == NULL)
2493 goto out;
2494 tmplog = ret;
2495 last_isspace = 0;
2496 break;
2497
2498 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002499 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002500 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002501 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002502 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002503 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002504 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002505 last_isspace = 0;
2506 break;
2507
Damien Claisse57c8eb92020-04-28 12:09:19 +00002508 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2509 if (!(fe->to_log & LW_BYTES))
2510 LOGCHAR('+');
2511 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2512 tmplog, dst + maxsize - tmplog);
2513 if (ret == NULL)
2514 goto out;
2515 tmplog = ret;
2516 last_isspace = 0;
2517 break;
2518
Willy Tarreau2beef582012-12-20 17:22:52 +01002519 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002520 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002521 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002522 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002523 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002524 last_isspace = 0;
2525 break;
2526
William Lallemand1d705562012-03-12 12:46:41 +01002527 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002528 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002529 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002530 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002531 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002532 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002533 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002534 last_isspace = 0;
2535 break;
2536
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002537 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002538 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002539 if (ret == NULL)
2540 goto out;
2541 tmplog = ret;
2542 last_isspace = 0;
2543 break;
2544
Willy Tarreau2beef582012-12-20 17:22:52 +01002545 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002546 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002547 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002548 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002549 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002550 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002551 last_isspace = 0;
2552 break;
2553
Willy Tarreau2beef582012-12-20 17:22:52 +01002554 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002555 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002556 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002557 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002558 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002559 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002560 last_isspace = 0;
2561 break;
2562
William Lallemand1d705562012-03-12 12:46:41 +01002563 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002564 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2565 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002566 *tmplog = '\0';
2567 last_isspace = 0;
2568 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002569
William Lallemand1d705562012-03-12 12:46:41 +01002570 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002571 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2572 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002573 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2574 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 +01002575 last_isspace = 0;
2576 break;
2577
William Lallemand1d705562012-03-12 12:46:41 +01002578 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002579 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002580 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002581 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002582 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002583 last_isspace = 0;
2584 break;
2585
William Lallemand1d705562012-03-12 12:46:41 +01002586 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002587 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002588 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002589 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002590 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002591 last_isspace = 0;
2592 break;
2593
William Lallemand1d705562012-03-12 12:46:41 +01002594 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002595 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002596 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002597 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002598 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002599 last_isspace = 0;
2600 break;
2601
William Lallemand1d705562012-03-12 12:46:41 +01002602 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002603 switch (obj_type(s ? s->target : sess->origin)) {
2604 case OBJ_TYPE_SERVER:
2605 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2606 tmplog, dst + maxsize - tmplog);
2607 break;
2608 case OBJ_TYPE_CHECK:
2609 ret = ultoa_o(__objt_check(sess->origin)->server
2610 ? __objt_check(sess->origin)->server->cur_sess
2611 : 0, tmplog, dst + maxsize - tmplog);
2612 break;
2613 default:
2614 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2615 break;
2616 }
2617
William Lallemand1d705562012-03-12 12:46:41 +01002618 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002619 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002620 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002621 last_isspace = 0;
2622 break;
2623
William Lallemand1d705562012-03-12 12:46:41 +01002624 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002625 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002626 LOGCHAR('+');
Christopher Faulet731c8e62022-03-29 16:08:44 +02002627 ret = ltoa_o((s ? s->conn_retries : 0), tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002628 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002629 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002630 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002631 last_isspace = 0;
2632 break;
2633
William Lallemand1d705562012-03-12 12:46:41 +01002634 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002635 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002636 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002637 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002638 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002639 last_isspace = 0;
2640 break;
2641
William Lallemand1d705562012-03-12 12:46:41 +01002642 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002643 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002644 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002645 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002646 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002647 last_isspace = 0;
2648 break;
2649
William Lallemand1d705562012-03-12 12:46:41 +01002650 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002651 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002652 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002653 if (tmp->options & LOG_OPT_QUOTE)
2654 LOGCHAR('"');
2655 LOGCHAR('{');
2656 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2657 if (hdr)
2658 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002659 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002660 ret = lf_encode_string(tmplog, dst + maxsize,
2661 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002662 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002663 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002664 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002665 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002666 }
2667 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002668 if (tmp->options & LOG_OPT_QUOTE)
2669 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002670 last_isspace = 0;
2671 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002672 break;
2673
William Lallemand1d705562012-03-12 12:46:41 +01002674 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002675 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002676 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002677 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2678 if (hdr > 0)
2679 LOGCHAR(' ');
2680 if (tmp->options & LOG_OPT_QUOTE)
2681 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002682 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002683 ret = lf_encode_string(tmplog, dst + maxsize,
2684 '#', hdr_encode_map, s->req_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 Lallemandbddd4fd2012-02-27 11:23:10 +01002688 } else if (!(tmp->options & LOG_OPT_QUOTE))
2689 LOGCHAR('-');
2690 if (tmp->options & LOG_OPT_QUOTE)
2691 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002692 last_isspace = 0;
2693 }
2694 }
2695 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002696
William Lallemand1d705562012-03-12 12:46:41 +01002697
2698 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002699 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002700 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002701 if (tmp->options & LOG_OPT_QUOTE)
2702 LOGCHAR('"');
2703 LOGCHAR('{');
2704 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2705 if (hdr)
2706 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002707 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002708 ret = lf_encode_string(tmplog, dst + maxsize,
2709 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002710 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002711 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002712 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002713 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002714 }
2715 LOGCHAR('}');
2716 last_isspace = 0;
2717 if (tmp->options & LOG_OPT_QUOTE)
2718 LOGCHAR('"');
2719 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002720 break;
2721
William Lallemand1d705562012-03-12 12:46:41 +01002722 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002723 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002724 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002725 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2726 if (hdr > 0)
2727 LOGCHAR(' ');
2728 if (tmp->options & LOG_OPT_QUOTE)
2729 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002730 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002731 ret = lf_encode_string(tmplog, dst + maxsize,
2732 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002733 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002734 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002735 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002736 } else if (!(tmp->options & LOG_OPT_QUOTE))
2737 LOGCHAR('-');
2738 if (tmp->options & LOG_OPT_QUOTE)
2739 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002740 last_isspace = 0;
2741 }
2742 }
2743 break;
2744
William Lallemand1d705562012-03-12 12:46:41 +01002745 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002746 /* Request */
2747 if (tmp->options & LOG_OPT_QUOTE)
2748 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002749 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002750 ret = lf_encode_string(tmplog, dst + maxsize,
2751 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002752 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002753 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002754 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002755 if (tmp->options & LOG_OPT_QUOTE)
2756 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002757 last_isspace = 0;
2758 break;
William Lallemand5f232402012-04-05 18:02:55 +02002759
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002760 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002761 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002762
Willy Tarreaub7636d12015-06-17 19:58:02 +02002763 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002764 LOGCHAR('"');
2765
2766 end = uri + strlen(uri);
2767 // look for the first whitespace character
2768 while (uri < end && !HTTP_IS_SPHT(*uri))
2769 uri++;
2770
2771 // keep advancing past multiple spaces
2772 while (uri < end && HTTP_IS_SPHT(*uri)) {
2773 uri++; nspaces++;
2774 }
2775
2776 // look for first space or question mark after url
2777 spc = uri;
2778 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2779 spc++;
2780
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002781 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002782 chunk.area = "<BADREQ>";
2783 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002784 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002785 chunk.area = uri;
2786 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002787 }
2788
Dragan Dosen835b9212016-02-12 13:23:03 +01002789 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002790 if (ret == NULL || *ret != '\0')
2791 goto out;
2792
2793 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002794 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002795 LOGCHAR('"');
2796
2797 last_isspace = 0;
2798 break;
2799
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002800 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2801 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2802
2803 if (tmp->options & LOG_OPT_QUOTE)
2804 LOGCHAR('"');
2805
2806 end = uri + strlen(uri);
2807
2808 // look for the first whitespace character
2809 while (uri < end && !HTTP_IS_SPHT(*uri))
2810 uri++;
2811
2812 // keep advancing past multiple spaces
2813 while (uri < end && HTTP_IS_SPHT(*uri)) {
2814 uri++; nspaces++;
2815 }
2816
2817 // look for first space after url
2818 spc = uri;
2819 while (spc < end && !HTTP_IS_SPHT(*spc))
2820 spc++;
2821
Tim Duesterhus92c696e2021-02-28 16:11:36 +01002822 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002823
2824 // extract relative path without query params from url
Amaury Denoyellec453f952021-07-06 11:40:12 +02002825 parser = http_uri_parser_init(path);
2826 path = iststop(http_parse_path(&parser), '?');
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002827 if (!txn || !txn->uri || nspaces == 0) {
2828 chunk.area = "<BADREQ>";
2829 chunk.data = strlen("<BADREQ>");
2830 } else {
2831 chunk.area = path.ptr;
2832 chunk.data = path.len;
2833 }
2834
2835 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2836 if (ret == NULL || *ret != '\0')
2837 goto out;
2838
2839 tmplog = ret;
2840 if (tmp->options & LOG_OPT_QUOTE)
2841 LOGCHAR('"');
2842
2843 last_isspace = 0;
2844 break;
2845
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002846 case LOG_FMT_HTTP_QUERY: // %HQ
2847 if (tmp->options & LOG_OPT_QUOTE)
2848 LOGCHAR('"');
2849
Willy Tarreau57bc8912016-04-25 17:09:40 +02002850 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002851 chunk.area = "<BADREQ>";
2852 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002853 } else {
2854 uri = txn->uri;
2855 end = uri + strlen(uri);
2856 // look for the first question mark
2857 while (uri < end && *uri != '?')
2858 uri++;
2859
2860 qmark = uri;
2861 // look for first space or question mark after url
2862 while (uri < end && !HTTP_IS_SPHT(*uri))
2863 uri++;
2864
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002865 chunk.area = qmark;
2866 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002867 }
2868
Dragan Dosen835b9212016-02-12 13:23:03 +01002869 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002870 if (ret == NULL || *ret != '\0')
2871 goto out;
2872
2873 tmplog = ret;
2874 if (tmp->options & LOG_OPT_QUOTE)
2875 LOGCHAR('"');
2876
2877 last_isspace = 0;
2878 break;
2879
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002880 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002881 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002882
Willy Tarreaub7636d12015-06-17 19:58:02 +02002883 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002884 LOGCHAR('"');
2885
2886 end = uri + strlen(uri);
2887 // look for the first whitespace character
2888 while (uri < end && !HTTP_IS_SPHT(*uri))
2889 uri++;
2890
2891 // keep advancing past multiple spaces
2892 while (uri < end && HTTP_IS_SPHT(*uri)) {
2893 uri++; nspaces++;
2894 }
2895
2896 // look for first space after url
2897 spc = uri;
2898 while (spc < end && !HTTP_IS_SPHT(*spc))
2899 spc++;
2900
Willy Tarreau57bc8912016-04-25 17:09:40 +02002901 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002902 chunk.area = "<BADREQ>";
2903 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002904 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002905 chunk.area = uri;
2906 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002907 }
2908
Dragan Dosen835b9212016-02-12 13:23:03 +01002909 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002910 if (ret == NULL || *ret != '\0')
2911 goto out;
2912
2913 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002914 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002915 LOGCHAR('"');
2916
2917 last_isspace = 0;
2918 break;
2919
2920 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002921 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002922 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002923 LOGCHAR('"');
2924
2925 end = uri + strlen(uri);
2926 // look for the first whitespace character
2927 spc = uri;
2928 while (spc < end && !HTTP_IS_SPHT(*spc))
2929 spc++;
2930
2931 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002932 chunk.area = "<BADREQ>";
2933 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002934 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002935 chunk.area = uri;
2936 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002937 }
2938
Dragan Dosen835b9212016-02-12 13:23:03 +01002939 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002940 if (ret == NULL || *ret != '\0')
2941 goto out;
2942
2943 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002944 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002945 LOGCHAR('"');
2946
2947 last_isspace = 0;
2948 break;
2949
2950 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002951 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002952 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002953 LOGCHAR('"');
2954
2955 end = uri + strlen(uri);
2956 // look for the first whitespace character
2957 while (uri < end && !HTTP_IS_SPHT(*uri))
2958 uri++;
2959
2960 // keep advancing past multiple spaces
2961 while (uri < end && HTTP_IS_SPHT(*uri)) {
2962 uri++; nspaces++;
2963 }
2964
2965 // look for the next whitespace character
2966 while (uri < end && !HTTP_IS_SPHT(*uri))
2967 uri++;
2968
2969 // keep advancing past multiple spaces
2970 while (uri < end && HTTP_IS_SPHT(*uri))
2971 uri++;
2972
Willy Tarreau57bc8912016-04-25 17:09:40 +02002973 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002974 chunk.area = "<BADREQ>";
2975 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002976 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002977 chunk.area = "HTTP/0.9";
2978 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002979 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002980 chunk.area = uri;
2981 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002982 }
2983
Dragan Dosen835b9212016-02-12 13:23:03 +01002984 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002985 if (ret == NULL || *ret != '\0')
2986 goto out;
2987
2988 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002989 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002990 LOGCHAR('"');
2991
2992 last_isspace = 0;
2993 break;
2994
William Lallemand5f232402012-04-05 18:02:55 +02002995 case LOG_FMT_COUNTER: // %rt
2996 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002997 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002998 if (iret < 0 || iret > dst + maxsize - tmplog)
2999 goto out;
3000 last_isspace = 0;
3001 tmplog += iret;
3002 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003003 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003004 if (ret == NULL)
3005 goto out;
3006 tmplog = ret;
3007 last_isspace = 0;
3008 }
3009 break;
3010
Willy Tarreau7346acb2014-08-28 15:03:15 +02003011 case LOG_FMT_LOGCNT: // %lc
3012 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003013 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003014 if (iret < 0 || iret > dst + maxsize - tmplog)
3015 goto out;
3016 last_isspace = 0;
3017 tmplog += iret;
3018 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003019 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003020 if (ret == NULL)
3021 goto out;
3022 tmplog = ret;
3023 last_isspace = 0;
3024 }
3025 break;
3026
William Lallemand5f232402012-04-05 18:02:55 +02003027 case LOG_FMT_HOSTNAME: // %H
3028 src = hostname;
3029 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3030 if (ret == NULL)
3031 goto out;
3032 tmplog = ret;
3033 last_isspace = 0;
3034 break;
3035
3036 case LOG_FMT_PID: // %pid
3037 if (tmp->options & LOG_OPT_HEXA) {
3038 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3039 if (iret < 0 || iret > dst + maxsize - tmplog)
3040 goto out;
3041 last_isspace = 0;
3042 tmplog += iret;
3043 } else {
3044 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3045 if (ret == NULL)
3046 goto out;
3047 tmplog = ret;
3048 last_isspace = 0;
3049 }
3050 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003051
3052 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003053 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003054 if (s)
3055 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3056 else
3057 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003058 if (ret == NULL)
3059 goto out;
3060 tmplog = ret;
3061 last_isspace = 0;
3062 break;
3063
William Lallemandbddd4fd2012-02-27 11:23:10 +01003064 }
3065 }
3066
3067out:
William Lallemand1d705562012-03-12 12:46:41 +01003068 /* *tmplog is a unused character */
3069 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003070 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003071
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072}
3073
William Lallemand1d705562012-03-12 12:46:41 +01003074/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003075 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003076 * Will not log if the frontend has no log defined.
3077 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003078void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003079{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003080 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003081 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003082 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003083
3084 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003085 err = (s->flags & SF_REDISP) ||
3086 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
Christopher Fauleta892b7f2022-06-17 14:53:20 +02003087 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) && s->conn_retries) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003088 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003089
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003090 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003091 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003092
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003093 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003094 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003095
Willy Tarreauabcd5142013-06-11 17:18:02 +02003096 if (s->logs.level) { /* loglevel was overridden */
3097 if (s->logs.level == -1) {
3098 s->logs.logwait = 0; /* logs disabled */
3099 return;
3100 }
3101 level = s->logs.level - 1;
3102 }
3103 else {
3104 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003105 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003106 level = LOG_ERR;
3107 }
William Lallemand1d705562012-03-12 12:46:41 +01003108
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003109 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003110 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003111 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003112 }
3113
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003114 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3115 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3116 &sess->fe->logformat_sd);
3117 }
3118
Dragan Dosen59cee972015-09-19 22:09:02 +02003119 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003120 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003121 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003122 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3123 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003124 s->logs.logwait = 0;
3125 }
3126}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003127
Willy Tarreau53839352018-09-05 19:51:10 +02003128/*
3129 * send a minimalist log for the session. Will not log if the frontend has no
3130 * log defined. It is assumed that this is only used to report anomalies that
3131 * cannot lead to the creation of a regular stream. Because of this the log
3132 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3133 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003134 * function to report unimportant events. It is safe to call this function with
3135 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003136 */
3137void sess_log(struct session *sess)
3138{
3139 int size, level;
3140 int sd_size = 0;
3141
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003142 if (!sess)
3143 return;
3144
Willy Tarreau53839352018-09-05 19:51:10 +02003145 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3146 return;
3147
3148 level = LOG_INFO;
3149 if (sess->fe->options2 & PR_O2_LOGERRORS)
3150 level = LOG_ERR;
3151
3152 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3153 sd_size = sess_build_logline(sess, NULL,
3154 logline_rfc5424, global.max_syslog_len,
3155 &sess->fe->logformat_sd);
3156 }
3157
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02003158 if (!LIST_ISEMPTY(&sess->fe->logformat_error))
3159 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat_error);
3160 else
3161 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
Willy Tarreau53839352018-09-05 19:51:10 +02003162 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003163 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003164 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3165 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003166 }
3167}
3168
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003169void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3170{
3171 va_list argp;
3172 int data_len;
3173
3174 if (level < 0 || format == NULL || logline == NULL)
3175 return;
3176
3177 va_start(argp, format);
3178 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3179 if (data_len < 0 || data_len > global.max_syslog_len)
3180 data_len = global.max_syslog_len;
3181 va_end(argp);
3182
3183 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3184}
Emeric Brun54932b42020-07-07 09:43:24 +02003185/*
3186 * This function parse a received log message <buf>, of size <buflen>
3187 * it fills <level>, <facility> and <metadata> depending of the detected
3188 * header format and message will point on remaining payload of <size>
3189 *
3190 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3191 * struct ist len will be set to 0 if field is not found
3192 * <level> and <facility> will be set to -1 if not found.
3193 */
3194void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3195 struct ist *metadata, char **message, size_t *size)
3196{
3197
3198 char *p;
3199 int fac_level = 0;
3200
3201 *level = *facility = -1;
3202
3203 *message = buf;
3204 *size = buflen;
3205
3206 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3207
3208 p = buf;
3209 if (*size < 2 || *p != '<')
3210 return;
3211
3212 p++;
3213 while (*p != '>') {
3214 if (*p > '9' || *p < '0')
3215 return;
3216 fac_level = 10*fac_level + (*p - '0');
3217 p++;
3218 if ((p - buf) > buflen)
3219 return;
3220 }
3221
3222 *facility = fac_level >> 3;
3223 *level = fac_level & 0x7;
3224 p++;
3225
3226 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3227
3228 buflen -= p - buf;
3229 buf = p;
3230
3231 *size = buflen;
3232 *message = buf;
3233
3234 /* for rfc5424, prio is always followed by '1' and ' ' */
3235 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3236 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3237 * followed by message.
3238 * Each header field can present NILVALUE: '-'
3239 */
3240
3241 p += 2;
Aurelien DARRAGONab9efc22022-11-22 11:17:11 +01003242 *size -= 2;
Emeric Brun54932b42020-07-07 09:43:24 +02003243 /* timestamp is NILVALUE '-' */
3244 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3245 metadata[LOG_META_TIME] = ist2(p, 1);
3246 p++;
3247 }
3248 else if (*size > LOG_ISOTIME_MINLEN) {
3249 metadata[LOG_META_TIME].ptr = p;
3250
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003251 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003252 * in timestamp.
3253 * possible format are:
3254 * ex: '1970-01-01T00:00:00.000000Z'
3255 * '1970-01-01T00:00:00.000000+00:00'
3256 * '1970-01-01T00:00:00.000000-00:00'
3257 * '1970-01-01T00:00:00Z'
3258 * '1970-01-01T00:00:00+00:00'
3259 * '1970-01-01T00:00:00-00:00'
3260 */
3261 p += 19;
3262 if (*p == '.') {
3263 p++;
3264 if ((p - buf) >= buflen)
3265 goto bad_format;
3266 while (*p != 'Z' && *p != '+' && *p != '-') {
3267 if ((unsigned char)(*p - '0') > 9)
3268 goto bad_format;
3269
3270 p++;
3271 if ((p - buf) >= buflen)
3272 goto bad_format;
3273 }
3274 }
3275
3276 if (*p == 'Z')
3277 p++;
3278 else
3279 p += 6; /* case of '+00:00 or '-00:00' */
3280
3281 if ((p - buf) >= buflen || *p != ' ')
3282 goto bad_format;
3283 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3284 }
3285 else
3286 goto bad_format;
3287
3288
3289 p++;
3290 if ((p - buf) >= buflen || *p == ' ')
3291 goto bad_format;
3292
3293 metadata[LOG_META_HOST].ptr = p;
3294 while (*p != ' ') {
3295 p++;
3296 if ((p - buf) >= buflen)
3297 goto bad_format;
3298 }
3299 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3300 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3301 metadata[LOG_META_HOST].len = 0;
3302
3303 p++;
3304 if ((p - buf) >= buflen || *p == ' ')
3305 goto bad_format;
3306
3307 metadata[LOG_META_TAG].ptr = p;
3308 while (*p != ' ') {
3309 p++;
3310 if ((p - buf) >= buflen)
3311 goto bad_format;
3312 }
3313 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3314 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3315 metadata[LOG_META_TAG].len = 0;
3316
3317 p++;
3318 if ((p - buf) >= buflen || *p == ' ')
3319 goto bad_format;
3320
3321 metadata[LOG_META_PID].ptr = p;
3322 while (*p != ' ') {
3323 p++;
3324 if ((p - buf) >= buflen)
3325 goto bad_format;
3326 }
3327 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3328 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3329 metadata[LOG_META_PID].len = 0;
3330
3331 p++;
3332 if ((p - buf) >= buflen || *p == ' ')
3333 goto bad_format;
3334
3335 metadata[LOG_META_MSGID].ptr = p;
3336 while (*p != ' ') {
3337 p++;
3338 if ((p - buf) >= buflen)
3339 goto bad_format;
3340 }
3341 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3342 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3343 metadata[LOG_META_MSGID].len = 0;
3344
3345 p++;
3346 if ((p - buf) >= buflen || *p == ' ')
3347 goto bad_format;
3348
3349 /* structured data format is:
3350 * ex:
3351 * '[key1=value1 key2=value2][key3=value3]'
3352 *
3353 * space is invalid outside [] because
3354 * considered as the end of structured data field
3355 */
3356 metadata[LOG_META_STDATA].ptr = p;
3357 if (*p == '[') {
3358 int elem = 0;
3359
3360 while (1) {
3361 if (elem) {
3362 /* according to rfc this char is escaped in param values */
3363 if (*p == ']' && *(p-1) != '\\')
3364 elem = 0;
3365 }
3366 else {
3367 if (*p == '[')
3368 elem = 1;
3369 else if (*p == ' ')
3370 break;
3371 else
3372 goto bad_format;
3373 }
3374 p++;
3375 if ((p - buf) >= buflen)
3376 goto bad_format;
3377 }
3378 }
3379 else if (*p == '-') {
3380 /* case of NILVALUE */
3381 p++;
3382 if ((p - buf) >= buflen || *p != ' ')
3383 goto bad_format;
3384 }
3385 else
3386 goto bad_format;
3387
3388 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3389 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3390 metadata[LOG_META_STDATA].len = 0;
3391
3392 p++;
3393
3394 buflen -= p - buf;
3395 buf = p;
3396
3397 *size = buflen;
3398 *message = p;
3399 }
3400 else if (*size > LOG_LEGACYTIME_LEN) {
3401 int m;
3402
3403 /* supported header format according to rfc3164.
3404 * ex:
3405 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3406 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3407 * or 'Jan 1 00:00:00 HOSTNAME '
3408 * Note: HOSTNAME is mandatory, and day
3409 * of month uses a single space prefix if
3410 * less than 10 to ensure hour offset is
3411 * always the same.
3412 */
3413
3414 /* Check month to see if it correspond to a rfc3164
3415 * header ex 'Jan 1 00:00:00' */
3416 for (m = 0; m < 12; m++)
3417 if (!memcmp(monthname[m], p, 3))
3418 break;
3419 /* Month not found */
3420 if (m == 12)
3421 goto bad_format;
3422
3423 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3424
3425 p += LOG_LEGACYTIME_LEN;
3426 if ((p - buf) >= buflen || *p != ' ')
3427 goto bad_format;
3428
3429 p++;
3430 if ((p - buf) >= buflen || *p == ' ')
3431 goto bad_format;
3432
3433 metadata[LOG_META_HOST].ptr = p;
3434 while (*p != ' ') {
3435 p++;
3436 if ((p - buf) >= buflen)
3437 goto bad_format;
3438 }
3439 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3440
3441 /* TAG seems to no be mandatory */
3442 p++;
3443
3444 buflen -= p - buf;
3445 buf = p;
3446
3447 *size = buflen;
3448 *message = buf;
3449
3450 if (!buflen)
3451 return;
3452
3453 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3454 p++;
3455
3456 /* a tag must present a trailing ':' */
3457 if (((p - buf) >= buflen) || *p != ':')
3458 return;
3459 p++;
3460 /* followed by a space */
3461 if (((p - buf) >= buflen) || *p != ' ')
3462 return;
3463
3464 /* rewind to parse tag and pid */
3465 p = buf;
3466 metadata[LOG_META_TAG].ptr = p;
3467 /* we have the guarantee that ':' will be reach before size limit */
3468 while (*p != ':') {
3469 if (*p == '[') {
3470 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3471 metadata[LOG_META_PID].ptr = p + 1;
3472 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003473 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003474 if (p[1] != ':')
3475 return;
3476 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3477 }
3478 p++;
3479 }
3480 if (!metadata[LOG_META_TAG].len)
3481 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3482
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003483 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003484 p += 2;
3485
3486 buflen -= p - buf;
3487 buf = p;
3488
3489 *size = buflen;
3490 *message = buf;
3491 }
3492
3493 return;
3494
3495bad_format:
3496 /* bad syslog format, we reset all parsed syslog fields
3497 * but priority is kept because we are able to re-build
3498 * this message using LOF_FORMAT_PRIO.
3499 */
3500 metadata[LOG_META_TIME].len = 0;
3501 metadata[LOG_META_HOST].len = 0;
3502 metadata[LOG_META_TAG].len = 0;
3503 metadata[LOG_META_PID].len = 0;
3504 metadata[LOG_META_MSGID].len = 0;
3505 metadata[LOG_META_STDATA].len = 0;
3506
3507 return;
3508}
3509
3510/*
3511 * UDP syslog fd handler
3512 */
3513void syslog_fd_handler(int fd)
3514{
3515 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3516 ssize_t ret = 0;
3517 struct buffer *buf = get_trash_chunk();
3518 size_t size;
3519 char *message;
3520 int level;
3521 int facility;
3522 struct listener *l = objt_listener(fdtab[fd].owner);
3523 int max_accept;
3524
Tim Duesterhus16554242021-09-15 13:58:49 +02003525 BUG_ON(!l);
Emeric Brun54932b42020-07-07 09:43:24 +02003526
Willy Tarreauf5090652021-04-06 17:23:40 +02003527 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003528
3529 if (!fd_recv_ready(fd))
3530 return;
3531
Willy Tarreau882f2482023-01-12 18:52:23 +01003532 max_accept = l->bind_conf->maxaccept ? l->bind_conf->maxaccept : 1;
Emeric Brun54932b42020-07-07 09:43:24 +02003533
3534 do {
3535 /* Source address */
3536 struct sockaddr_storage saddr = {0};
3537 socklen_t saddrlen;
3538
3539 saddrlen = sizeof(saddr);
3540
3541 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3542 if (ret < 0) {
3543 if (errno == EINTR)
3544 continue;
Willy Tarreauacef5e22022-04-25 20:32:15 +02003545 if (errno == EAGAIN || errno == EWOULDBLOCK)
Emeric Brun54932b42020-07-07 09:43:24 +02003546 fd_cant_recv(fd);
3547 goto out;
3548 }
3549 buf->data = ret;
3550
Emeric Brun45c457a2020-07-09 23:23:34 +02003551 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003552 _HA_ATOMIC_INC(&cum_log_messages);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01003553 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend, 0);
Emeric Brun45c457a2020-07-09 23:23:34 +02003554
Emeric Brun54932b42020-07-07 09:43:24 +02003555 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3556
3557 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3558
3559 } while (--max_accept);
3560 }
3561
3562out:
3563 return;
3564}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003565
Emeric Brun12941c82020-07-07 14:19:42 +02003566/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003567 * IO Handler to handle message exchange with a syslog tcp client
3568 */
3569static void syslog_io_handler(struct appctx *appctx)
3570{
3571 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
Willy Tarreauc12b3212022-05-27 11:08:15 +02003572 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau270a4572022-05-27 10:36:36 +02003573 struct stream *s = __sc_strm(sc);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003574 struct proxy *frontend = strm_fe(s);
3575 struct listener *l = strm_li(s);
3576 struct buffer *buf = get_trash_chunk();
3577 int max_accept;
3578 int to_skip;
3579 int facility;
3580 int level;
3581 char *message;
3582 size_t size;
3583
Christopher Faulet211452e2023-04-11 07:56:50 +02003584 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
3585 co_skip(sc_oc(sc), co_data(sc_oc(sc)));
Christopher Faulet22a88f02023-03-31 11:17:13 +02003586 goto out;
Christopher Faulet211452e2023-04-11 07:56:50 +02003587 }
Christopher Faulet22a88f02023-03-31 11:17:13 +02003588
Willy Tarreau882f2482023-01-12 18:52:23 +01003589 max_accept = l->bind_conf->maxaccept ? l->bind_conf->maxaccept : 1;
Christopher Faulet285aa402023-04-17 16:34:29 +02003590 while (1) {
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003591 char c;
3592
3593 if (max_accept <= 0)
3594 goto missing_budget;
3595 max_accept--;
3596
Willy Tarreau270a4572022-05-27 10:36:36 +02003597 to_skip = co_getchar(sc_oc(sc), &c);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003598 if (!to_skip)
3599 goto missing_data;
3600 else if (to_skip < 0)
3601 goto cli_abort;
3602
3603 if (c == '<') {
3604 /* rfc-6587, Non-Transparent-Framing: messages separated by
3605 * a trailing LF or CR LF
3606 */
Willy Tarreau270a4572022-05-27 10:36:36 +02003607 to_skip = co_getline(sc_oc(sc), buf->area, buf->size);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003608 if (!to_skip)
3609 goto missing_data;
3610 else if (to_skip < 0)
3611 goto cli_abort;
3612
3613 if (buf->area[to_skip - 1] != '\n')
3614 goto parse_error;
3615
3616 buf->data = to_skip - 1;
3617
3618 /* according to rfc-6587, some devices adds CR before LF */
3619 if (buf->data && buf->area[buf->data - 1] == '\r')
3620 buf->data--;
3621
3622 }
3623 else if ((unsigned char)(c - '1') <= 8) {
3624 /* rfc-6587, Octet-Counting: message length in ASCII
3625 * (first digit can not be ZERO), followed by a space
3626 * and message length
3627 */
3628 char *p = NULL;
3629 int msglen;
3630
Willy Tarreau270a4572022-05-27 10:36:36 +02003631 to_skip = co_getword(sc_oc(sc), buf->area, buf->size, ' ');
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003632 if (!to_skip)
3633 goto missing_data;
3634 else if (to_skip < 0)
3635 goto cli_abort;
3636
3637 if (buf->area[to_skip - 1] != ' ')
3638 goto parse_error;
3639
Aurelien DARRAGON7faffdc2022-10-26 23:40:08 +02003640 msglen = strtol(buf->area, &p, 10);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003641 if (!msglen || p != &buf->area[to_skip - 1])
3642 goto parse_error;
3643
3644 /* message seems too large */
3645 if (msglen > buf->size)
3646 goto parse_error;
3647
Willy Tarreau270a4572022-05-27 10:36:36 +02003648 msglen = co_getblk(sc_oc(sc), buf->area, msglen, to_skip);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003649 if (!msglen)
3650 goto missing_data;
3651 else if (msglen < 0)
3652 goto cli_abort;
3653
3654
3655 buf->data = msglen;
3656 to_skip += msglen;
3657 }
3658 else
3659 goto parse_error;
3660
Willy Tarreau270a4572022-05-27 10:36:36 +02003661 co_skip(sc_oc(sc), to_skip);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003662
3663 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003664 _HA_ATOMIC_INC(&cum_log_messages);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01003665 proxy_inc_fe_req_ctr(l, frontend, 0);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003666
3667 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3668
3669 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3670
3671 }
3672
3673missing_data:
3674 /* we need more data to read */
Christopher Faulet3aeb3662023-03-16 14:27:29 +01003675 applet_need_more_data(appctx);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003676 return;
3677
3678missing_budget:
3679 /* it may remain some stuff to do, let's retry later */
3680 appctx_wakeup(appctx);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003681 return;
3682
3683parse_error:
3684 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003685 _HA_ATOMIC_INC(&l->counters->failed_req);
3686 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003687
Christopher Faulet22a88f02023-03-31 11:17:13 +02003688 goto error;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003689
3690cli_abort:
3691 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003692 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3693 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003694
Christopher Faulet22a88f02023-03-31 11:17:13 +02003695error:
3696 se_fl_set(appctx->sedesc, SE_FL_ERROR);
3697
3698out:
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003699 return;
3700}
3701
3702static struct applet syslog_applet = {
3703 .obj_type = OBJ_TYPE_APPLET,
3704 .name = "<SYSLOG>", /* used for logging */
3705 .fct = syslog_io_handler,
3706 .release = NULL,
3707};
3708
3709/*
Emeric Brun12941c82020-07-07 14:19:42 +02003710 * Parse "log-forward" section and create corresponding sink buffer.
3711 *
3712 * The function returns 0 in success case, otherwise, it returns error
3713 * flags.
3714 */
3715int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3716{
3717 int err_code = 0;
3718 struct proxy *px;
3719 char *errmsg = NULL;
3720 const char *err = NULL;
3721
3722 if (strcmp(args[0], "log-forward") == 0) {
3723 if (!*args[1]) {
3724 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3725 err_code |= ERR_ALERT | ERR_ABORT;
3726 goto out;
3727 }
3728
3729 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3730 goto out;
3731
3732 err = invalid_char(args[1]);
3733 if (err) {
3734 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3735 file, linenum, *err, args[0], args[1]);
3736 err_code |= ERR_ALERT | ERR_ABORT;
3737 goto out;
3738 }
3739
Emeric Brunb0c331f2020-10-07 17:05:59 +02003740 px = log_forward_by_name(args[1]);
3741 if (px) {
3742 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3743 file, linenum, args[1], px->conf.file, px->conf.line);
3744 err_code |= ERR_ALERT | ERR_FATAL;
3745 }
3746
3747 px = proxy_find_by_name(args[1], 0, 0);
3748 if (px) {
3749 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3750 file, linenum, args[1], proxy_type_str(px),
3751 px->id, px->conf.file, px->conf.line);
3752 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003753 }
3754
3755 px = calloc(1, sizeof *px);
3756 if (!px) {
3757 err_code |= ERR_ALERT | ERR_FATAL;
3758 goto out;
3759 }
3760
Emeric Brun2ad2b1c2021-12-01 12:08:42 +01003761 init_new_proxy(px);
Emeric Brun12941c82020-07-07 14:19:42 +02003762 px->next = cfg_log_forward;
3763 cfg_log_forward = px;
Emeric Brun12941c82020-07-07 14:19:42 +02003764 px->conf.file = strdup(file);
3765 px->conf.line = linenum;
3766 px->mode = PR_MODE_SYSLOG;
Willy Tarreau69530f52023-04-28 09:16:15 +02003767 px->last_change = ns_to_sec(now_ns);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003768 px->cap = PR_CAP_FE;
3769 px->maxconn = 10;
3770 px->timeout.client = TICK_ETERNITY;
3771 px->accept = frontend_accept;
3772 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003773 px->id = strdup(args[1]);
3774
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003775 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003776 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003777 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3778 err_code |= ERR_WARN;
3779
3780 if (*(args[1]) == 0) {
3781 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3782 err_code |= ERR_ALERT | ERR_FATAL;
3783 goto out;
3784 }
3785 cfg_log_forward->maxconn = atol(args[1]);
3786 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3787 goto out;
3788 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003789 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003790 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3791 err_code |= ERR_WARN;
3792
3793 if (*(args[1]) == 0) {
3794 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3795 err_code |= ERR_ALERT | ERR_FATAL;
3796 goto out;
3797 }
3798 cfg_log_forward->backlog = atol(args[1]);
3799 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3800 goto out;
3801 }
3802 else if (strcmp(args[0], "bind") == 0) {
3803 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003804 struct bind_conf *bind_conf;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003805 struct listener *l;
Emeric Bruna8942cd2022-08-19 15:16:26 +02003806 int ret;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003807
3808 cur_arg = 1;
3809
3810 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3811 NULL, xprt_get(XPRT_RAW));
3812 if (!bind_conf) {
3813 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3814 err_code |= ERR_ALERT | ERR_FATAL;
3815 goto out;
3816 }
3817
Willy Tarreau882f2482023-01-12 18:52:23 +01003818 bind_conf->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreau30836152023-01-12 19:10:17 +01003819 bind_conf->accept = session_accept_fd;
Willy Tarreau882f2482023-01-12 18:52:23 +01003820
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003821 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3822 if (errmsg && *errmsg) {
3823 indent_msg(&errmsg, 2);
3824 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3825 }
3826 else {
3827 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3828 file, linenum, args[0], args[1], args[2]);
3829 err_code |= ERR_ALERT | ERR_FATAL;
3830 goto out;
3831 }
3832 }
3833 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003834 global.maxsock++;
3835 }
3836 cur_arg++;
3837
Emeric Bruna8942cd2022-08-19 15:16:26 +02003838 ret = bind_parse_args_list(bind_conf, args, cur_arg, cursection, file, linenum);
3839 err_code |= ret;
3840 if (ret != 0) {
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 Tarreau882f2482023-01-12 18:52:23 +01003861 bind_conf->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
3862
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003863 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003864 if (errmsg && *errmsg) {
3865 indent_msg(&errmsg, 2);
3866 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3867 }
3868 else {
3869 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3870 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003871 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003872 err_code |= ERR_ALERT | ERR_FATAL;
3873 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003874 }
3875 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003876 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreaue140a692020-10-15 21:25:32 +02003877 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003878 global.maxsock++;
3879 }
3880 cur_arg++;
3881
3882 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3883 int ret;
3884
3885 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3886 err_code |= ret;
3887 if (ret) {
3888 if (errmsg && *errmsg) {
3889 indent_msg(&errmsg, 2);
3890 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3891 }
3892 else
3893 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3894 file, linenum, args[cur_arg]);
3895 if (ret & ERR_FATAL)
3896 goto out;
3897 }
3898 cur_arg += 1 + kw->skip;
3899 }
3900 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003901 const char *best = bind_find_best_kw(args[cur_arg]);
3902 if (best)
3903 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3904 file, linenum, args[cur_arg], cursection, best);
3905 else
3906 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3907 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02003908 err_code |= ERR_ALERT | ERR_FATAL;
3909 goto out;
3910 }
3911 }
3912 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02003913 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003914 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3915 err_code |= ERR_ALERT | ERR_FATAL;
3916 goto out;
3917 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003918 }
3919 else if (strcmp(args[0], "timeout") == 0) {
3920 const char *res;
3921 unsigned timeout;
3922
3923 if (strcmp(args[1], "client") != 0) {
3924 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
3925 err_code |= ERR_ALERT | ERR_FATAL;
3926 goto out;
3927 }
3928
3929 if (*args[2] == 0) {
3930 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
3931 err_code |= ERR_ALERT | ERR_FATAL;
3932 goto out;
3933 }
3934 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3935 if (res == PARSE_TIME_OVER) {
3936 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
3937 }
3938 else if (res == PARSE_TIME_UNDER) {
3939 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
3940 }
3941 else if (res) {
3942 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
3943 return -1;
3944 }
3945
3946 if (res) {
3947 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3948 err_code |= ERR_ALERT | ERR_FATAL;
3949 goto out;
3950 }
3951 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02003952 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02003953 else {
3954 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
3955 err_code |= ERR_ALERT | ERR_ABORT;
3956 goto out;
3957 }
Emeric Brun12941c82020-07-07 14:19:42 +02003958out:
3959 return err_code;
3960}
3961
Willy Tarreau0108d902018-11-25 19:14:37 +01003962
Emeric Brun12941c82020-07-07 14:19:42 +02003963/* config parsers for this section */
3964REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
3965
Willy Tarreau082b6282019-05-22 14:42:12 +02003966REGISTER_PER_THREAD_ALLOC(init_log_buffers);
3967REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01003968
Willy Tarreaubaaee002006-06-26 02:48:02 +02003969/*
3970 * Local variables:
3971 * c-indent-level: 8
3972 * c-basic-offset: 8
3973 * End:
3974 */