blob: 47320229e4dfc0bcc78b9a21dc293650edb35bd9 [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
Aurelien DARRAGON77679312023-07-05 15:52:19 +0200737/* tries to duplicate <def> logsrv
738 *
739 * Returns the newly allocated and duplicated logsrv or NULL
740 * in case of error.
741 */
742struct logsrv *dup_logsrv(struct logsrv *def)
743{
744 struct logsrv *cpy = malloc(sizeof(*cpy));
745
746 /* copy everything that can be easily copied */
747 memcpy(cpy, def, sizeof(*cpy));
748
749 /* default values */
750 cpy->ring_name = NULL;
751 cpy->conf.file = NULL;
752 LIST_INIT(&cpy->list);
753 HA_SPIN_INIT(&cpy->lock);
754
755 /* special members */
756 if (def->ring_name) {
757 cpy->ring_name = strdup(def->ring_name);
758 if (!cpy->ring_name)
759 goto error;
760 }
761 if (def->conf.file) {
762 cpy->conf.file = strdup(def->conf.file);
763 if (!cpy->conf.file)
764 goto error;
765 }
766 cpy->ref = def;
767 return cpy;
768
769 error:
770 free_logsrv(cpy);
771 return NULL;
772}
773
Willy Tarreau211ea252022-03-17 19:47:33 +0100774/* frees log server <logsrv> after freeing all of its allocated fields. The
775 * server must not belong to a list anymore. Logsrv may be NULL, which is
776 * silently ignored.
777 */
778void free_logsrv(struct logsrv *logsrv)
779{
780 if (!logsrv)
781 return;
782
783 BUG_ON(LIST_INLIST(&logsrv->list));
784 ha_free(&logsrv->conf.file);
785 ha_free(&logsrv->ring_name);
786 free(logsrv);
787}
788
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200789/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200790 * Parse "log" keyword and update <logsrvs> list accordingly.
791 *
792 * When <do_del> is set, it means the "no log" line was parsed, so all log
793 * servers in <logsrvs> are released.
794 *
795 * Otherwise, we try to parse the "log" line. First of all, when the list is not
796 * the global one, we look for the parameter "global". If we find it,
797 * global.logsrvs is copied. Else we parse each arguments.
798 *
799 * The function returns 1 in success case, otherwise, it returns 0 and err is
800 * filled.
801 */
Emeric Brun9533a702021-04-02 10:13:43 +0200802int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200803{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100804 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200805 struct sockaddr_storage *sk;
Emeric Brun94aab062021-04-02 10:41:36 +0200806 struct protocol *proto;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200807 struct logsrv *logsrv = NULL;
808 int port1, port2;
809 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200810 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200811
812 /*
813 * "no log": delete previous herited or defined syslog
814 * servers.
815 */
816 if (do_del) {
817 struct logsrv *back;
818
819 if (*(args[1]) != 0) {
820 memprintf(err, "'no log' does not expect arguments");
821 goto error;
822 }
823
824 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
Willy Tarreau211ea252022-03-17 19:47:33 +0100825 LIST_DEL_INIT(&logsrv->list);
826 free_logsrv(logsrv);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200827 }
828 return 1;
829 }
830
831 /*
832 * "log global": copy global.logrsvs linked list to the end of logsrvs
833 * list. But first, we check (logsrvs != global.logsrvs).
834 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100835 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200836 if (logsrvs == &global.logsrvs) {
837 memprintf(err, "'global' is not supported for a global syslog server");
838 goto error;
839 }
840 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200841 struct logsrv *node;
842
843 list_for_each_entry(node, logsrvs, list) {
844 if (node->ref == logsrv)
845 goto skip_logsrv;
846 }
847
Aurelien DARRAGON77679312023-07-05 15:52:19 +0200848 /* duplicate logsrv from global */
849 node = dup_logsrv(logsrv);
Aurelien DARRAGON256d5812023-05-11 18:57:23 +0200850 if (!node) {
851 memprintf(err, "out of memory error");
852 goto error;
853 }
Aurelien DARRAGON77679312023-07-05 15:52:19 +0200854
855 /* manually override some values */
856 ha_free(&node->conf.file);
Emeric Brun9533a702021-04-02 10:13:43 +0200857 node->conf.file = strdup(file);
858 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200859
Aurelien DARRAGON77679312023-07-05 15:52:19 +0200860 /* add to list */
861 LIST_APPEND(logsrvs, &node->list);
862
Christopher Faulet28ac0992018-03-26 16:09:19 +0200863 skip_logsrv:
864 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200865 }
866 return 1;
867 }
868
869 /*
870 * "log <address> ...: parse a syslog server line
871 */
872 if (*(args[1]) == 0 || *(args[2]) == 0) {
873 memprintf(err, "expects <address> and <facility> %s as arguments",
874 ((logsrvs == &global.logsrvs) ? "" : "or global"));
875 goto error;
876 }
877
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100878 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
879 if (strcmp(args[1], "stdout") == 0)
880 args[1] = "fd@1";
881 else if (strcmp(args[1], "stderr") == 0)
882 args[1] = "fd@2";
883
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200884 logsrv = calloc(1, sizeof(*logsrv));
885 if (!logsrv) {
886 memprintf(err, "out of memory");
887 goto error;
888 }
Christopher Fauletb4f96ed2022-03-29 14:17:09 +0200889 LIST_INIT(&logsrv->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200890 logsrv->conf.file = strdup(file);
891 logsrv->conf.line = linenum;
892
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200893 /* skip address for now, it will be parsed at the end */
894 cur_arg = 2;
895
896 /* just after the address, a length may be specified */
897 logsrv->maxlen = MAX_SYSLOG_LEN;
898 if (strcmp(args[cur_arg], "len") == 0) {
899 int len = atoi(args[cur_arg+1]);
900 if (len < 80 || len > 65535) {
901 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
902 args[cur_arg+1]);
903 goto error;
904 }
905 logsrv->maxlen = len;
906 cur_arg += 2;
907 }
908 if (logsrv->maxlen > global.max_syslog_len)
909 global.max_syslog_len = logsrv->maxlen;
910
911 /* after the length, a format may be specified */
912 if (strcmp(args[cur_arg], "format") == 0) {
913 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200914 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200915 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
916 goto error;
917 }
918 cur_arg += 2;
919 }
920
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200921 if (strcmp(args[cur_arg], "sample") == 0) {
922 unsigned low, high;
923 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200924 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
925
926 p = args[cur_arg+1];
927 smp_sz_str = strchr(p, ':');
928 if (!smp_sz_str) {
929 memprintf(err, "Missing sample size");
930 goto error;
931 }
932
933 *smp_sz_str++ = '\0';
934
935 end = p + strlen(p);
936
937 while (p != end) {
938 if (!get_logsrv_smp_range(&low, &high, &p, err))
939 goto error;
940
941 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
942 goto error;
943
944 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
945 if (!smp_rgs) {
946 memprintf(err, "out of memory error");
947 goto error;
948 }
949
950 smp_rgs[smp_rgs_sz].low = low;
951 smp_rgs[smp_rgs_sz].high = high;
952 smp_rgs[smp_rgs_sz].sz = high - low + 1;
953 smp_rgs[smp_rgs_sz].curr_idx = 0;
954 if (smp_rgs[smp_rgs_sz].high > smp_sz)
955 smp_sz = smp_rgs[smp_rgs_sz].high;
956 smp_rgs_sz++;
957 }
958
Tim Duesterhus21648002019-06-23 22:10:10 +0200959 if (smp_rgs == NULL) {
960 memprintf(err, "no sampling ranges given");
961 goto error;
962 }
963
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200964 beg = smp_sz_str;
965 end = beg + strlen(beg);
966 new_smp_sz = read_uint((const char **)&beg, end);
967 if (!new_smp_sz || beg != end) {
968 memprintf(err, "wrong sample size '%s' for sample range '%s'",
969 smp_sz_str, args[cur_arg+1]);
970 goto error;
971 }
972
973 if (new_smp_sz < smp_sz) {
974 memprintf(err, "sample size %zu should be greater or equal to "
975 "%zu the maximum of the high ranges limits",
976 new_smp_sz, smp_sz);
977 goto error;
978 }
979 smp_sz = new_smp_sz;
980
981 /* Let's order <smp_rgs> array. */
982 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
983
984 logsrv->lb.smp_rgs = smp_rgs;
985 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
986 logsrv->lb.smp_sz = smp_sz;
987
988 cur_arg += 2;
989 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200990 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200991 /* parse the facility */
992 logsrv->facility = get_log_facility(args[cur_arg]);
993 if (logsrv->facility < 0) {
994 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
995 goto error;
996 }
997 cur_arg++;
998
999 /* parse the max syslog level (default: debug) */
1000 logsrv->level = 7;
1001 if (*(args[cur_arg])) {
1002 logsrv->level = get_log_level(args[cur_arg]);
1003 if (logsrv->level < 0) {
1004 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
1005 goto error;
1006 }
1007 cur_arg++;
1008 }
1009
1010 /* parse the limit syslog level (default: emerg) */
1011 logsrv->minlvl = 0;
1012 if (*(args[cur_arg])) {
1013 logsrv->minlvl = get_log_level(args[cur_arg]);
1014 if (logsrv->minlvl < 0) {
1015 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1016 goto error;
1017 }
1018 cur_arg++;
1019 }
1020
1021 /* Too many args */
1022 if (*(args[cur_arg])) {
1023 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1024 goto error;
1025 }
1026
1027 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001028 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001029 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001030 logsrv->addr.ss_family = AF_UNSPEC;
1031 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001032 logsrv->sink = NULL;
1033 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001034 goto done;
1035 }
1036
Emeric Brun94aab062021-04-02 10:41:36 +02001037 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
1038 err, NULL, NULL,
1039 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 +02001040 if (!sk)
1041 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001042
1043 if (fd != -1)
1044 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001045 logsrv->addr = *sk;
1046
1047 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001048 if (!port1)
1049 set_host_port(&logsrv->addr, SYSLOG_PORT);
1050 }
Emeric Brun9533a702021-04-02 10:13:43 +02001051
Willy Tarreau91b47262022-05-20 16:36:46 +02001052 if (proto && proto->xprt_type == PROTO_TYPE_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +02001053 static unsigned long ring_ids;
1054
1055 /* Implicit sink buffer will be
1056 * initialized in post_check
1057 */
1058 logsrv->type = LOG_TARGET_BUFFER;
1059 logsrv->sink = NULL;
1060 /* compute uniq name for the ring */
1061 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
1062 }
1063
Willy Tarreauc046d162019-08-30 15:24:59 +02001064 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001065 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001066 return 1;
1067
1068 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001069 free(smp_rgs);
Willy Tarreau211ea252022-03-17 19:47:33 +01001070 free_logsrv(logsrv);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001071 return 0;
1072}
1073
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001074
1075/*
Emeric Brun54648852020-07-06 15:54:06 +02001076 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001077 */
Emeric Brun54648852020-07-06 15:54:06 +02001078enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001079{
Emeric Brun54648852020-07-06 15:54:06 +02001080 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001081
1082 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001083 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001084 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001085 format--;
1086
Emeric Brun54648852020-07-06 15:54:06 +02001087 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001088 return format;
1089}
1090
1091/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001092 * returns log level for <lev> or -1 if not found.
1093 */
1094int get_log_level(const char *lev)
1095{
1096 int level;
1097
1098 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001099 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100 level--;
1101
1102 return level;
1103}
1104
Willy Tarreaubaaee002006-06-26 02:48:02 +02001105/*
1106 * returns log facility for <fac> or -1 if not found.
1107 */
1108int get_log_facility(const char *fac)
1109{
1110 int facility;
1111
1112 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001113 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001114 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001115
Willy Tarreaubaaee002006-06-26 02:48:02 +02001116 return facility;
1117}
1118
William Lallemanda1cc3812012-02-08 16:38:44 +01001119/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001120 * Encode the string.
1121 *
1122 * When using the +E log format option, it will try to escape '"\]'
1123 * characters with '\' as prefix. The same prefix should not be used as
1124 * <escape>.
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001125 *
1126 * Return the address of the \0 character, or NULL on error
Dragan Dosen835b9212016-02-12 13:23:03 +01001127 */
1128static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001129 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001130 const char *string,
1131 struct logformat_node *node)
1132{
1133 if (node->options & LOG_OPT_ESC) {
1134 if (start < stop) {
1135 stop--; /* reserve one byte for the final '\0' */
1136 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001137 if (!ha_bit_test((unsigned char)(*string), map)) {
1138 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001139 *start++ = *string;
1140 else {
1141 if (start + 2 >= stop)
1142 break;
1143 *start++ = '\\';
1144 *start++ = *string;
1145 }
1146 }
1147 else {
1148 if (start + 3 >= stop)
1149 break;
1150 *start++ = escape;
1151 *start++ = hextab[(*string >> 4) & 15];
1152 *start++ = hextab[*string & 15];
1153 }
1154 string++;
1155 }
1156 *start = '\0';
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001157 return start;
Dragan Dosen835b9212016-02-12 13:23:03 +01001158 }
1159 }
1160 else {
1161 return encode_string(start, stop, escape, map, string);
1162 }
1163
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001164 return NULL;
Dragan Dosen835b9212016-02-12 13:23:03 +01001165}
1166
1167/*
1168 * Encode the chunk.
1169 *
1170 * When using the +E log format option, it will try to escape '"\]'
1171 * characters with '\' as prefix. The same prefix should not be used as
1172 * <escape>.
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001173 *
1174 * Return the address of the \0 character, or NULL on error
Dragan Dosen835b9212016-02-12 13:23:03 +01001175 */
1176static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001177 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001178 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001179 struct logformat_node *node)
1180{
1181 char *str, *end;
1182
1183 if (node->options & LOG_OPT_ESC) {
1184 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001185 str = chunk->area;
1186 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001187
1188 stop--; /* reserve one byte for the final '\0' */
1189 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001190 if (!ha_bit_test((unsigned char)(*str), map)) {
1191 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001192 *start++ = *str;
1193 else {
1194 if (start + 2 >= stop)
1195 break;
1196 *start++ = '\\';
1197 *start++ = *str;
1198 }
1199 }
1200 else {
1201 if (start + 3 >= stop)
1202 break;
1203 *start++ = escape;
1204 *start++ = hextab[(*str >> 4) & 15];
1205 *start++ = hextab[*str & 15];
1206 }
1207 str++;
1208 }
1209 *start = '\0';
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001210 return start;
Dragan Dosen835b9212016-02-12 13:23:03 +01001211 }
1212 }
1213 else {
1214 return encode_chunk(start, stop, escape, map, chunk);
1215 }
1216
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001217 return NULL;
Dragan Dosen835b9212016-02-12 13:23:03 +01001218}
1219
1220/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001221 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001222 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001223 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001224 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001225 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001226char *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 +01001227{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001228 if (size < 2)
1229 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001230
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001231 if (node->options & LOG_OPT_QUOTE) {
1232 *(dst++) = '"';
1233 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001234 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001235
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001236 if (src && len) {
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001237 /* escape_string and strlcpy2 will both try to add terminating NULL-byte
Aurelien DARRAGON1acf8c02024-04-09 11:15:23 +02001238 * to dst
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001239 */
Dragan Dosen835b9212016-02-12 13:23:03 +01001240 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001241 char *ret;
1242
Aurelien DARRAGON1acf8c02024-04-09 11:15:23 +02001243 ret = escape_string(dst, dst + size, '\\', rfc5424_escape_map, src, src + len);
Dragan Dosen835b9212016-02-12 13:23:03 +01001244 if (ret == NULL || *ret != '\0')
1245 return NULL;
1246 len = ret - dst;
1247 }
1248 else {
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001249 if (++len > size)
1250 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001251 len = strlcpy2(dst, src, len);
1252 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001253
1254 size -= len;
1255 dst += len;
1256 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001257 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1258 if (size < 2)
1259 return NULL;
1260 *(dst++) = '-';
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001261 size -= 1;
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001262 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001263
1264 if (node->options & LOG_OPT_QUOTE) {
1265 if (size < 2)
1266 return NULL;
1267 *(dst++) = '"';
1268 }
1269
1270 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001271 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001272}
1273
Willy Tarreau26ffa852018-09-05 15:23:10 +02001274static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001275{
1276 return lf_text_len(dst, src, size, size, node);
1277}
1278
William Lallemand5f232402012-04-05 18:02:55 +02001279/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001280 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001281 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001282 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001283char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001284{
1285 char *ret = dst;
1286 int iret;
1287 char pn[INET6_ADDRSTRLEN];
1288
1289 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001290 unsigned char *addr = NULL;
1291 switch (sockaddr->sa_family) {
1292 case AF_INET:
1293 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1294 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1295 break;
1296 case AF_INET6:
1297 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1298 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1299 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1300 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1301 break;
1302 default:
1303 return NULL;
1304 }
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02001305 if (iret < 0 || iret >= size)
William Lallemand5f232402012-04-05 18:02:55 +02001306 return NULL;
1307 ret += iret;
1308 } else {
1309 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1310 ret = lf_text(dst, pn, size, node);
1311 if (ret == NULL)
1312 return NULL;
1313 }
1314 return ret;
1315}
1316
1317/*
1318 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001319 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001320 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001321char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001322{
1323 char *ret = dst;
1324 int iret;
1325
1326 if (node->options & LOG_OPT_HEXA) {
1327 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1328 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02001329 if (iret < 0 || iret >= size)
William Lallemand5f232402012-04-05 18:02:55 +02001330 return NULL;
1331 ret += iret;
1332 } else {
1333 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1334 if (ret == NULL)
1335 return NULL;
1336 }
1337 return ret;
1338}
1339
Emeric Brun54648852020-07-06 15:54:06 +02001340
1341/*
1342 * This function sends the syslog message using a printf format string. It
1343 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001344 */
Emeric Brun54648852020-07-06 15:54:06 +02001345void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001346{
Emeric Brun54648852020-07-06 15:54:06 +02001347 va_list argp;
1348 int data_len;
1349
1350 if (level < 0 || format == NULL || logline == NULL)
1351 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001352
Emeric Brun54648852020-07-06 15:54:06 +02001353 va_start(argp, format);
1354 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1355 if (data_len < 0 || data_len > global.max_syslog_len)
1356 data_len = global.max_syslog_len;
1357 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001358
Emeric Brun54648852020-07-06 15:54:06 +02001359 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1360 logline, data_len, default_rfc5424_sd_log_format, 2);
1361}
1362/*
1363 * This function builds a log header of given format using given
1364 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1365 * to determine format based on given metadas. It is useful
1366 * for log-forwarding to be able to forward any format without
1367 * settings.
1368 * This function returns a struct ist array of elements of the header
1369 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001370 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001371 * elements.
1372 */
1373struct ist *build_log_header(enum log_fmt format, int level, int facility,
1374 struct ist *metadata, size_t *nbelem)
1375{
1376 static THREAD_LOCAL struct {
1377 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1378 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1379 time_t cur_legacy_time;
1380 char priority_buffer[6];
1381 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001382
Emeric Brun54648852020-07-06 15:54:06 +02001383 struct tm logtime;
1384 int len;
1385 int fac_level = 0;
1386 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001387
Emeric Brun54648852020-07-06 15:54:06 +02001388 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001389
Emeric Brun54648852020-07-06 15:54:06 +02001390
1391 if (format == LOG_FORMAT_UNSPEC) {
1392 format = LOG_FORMAT_RAW;
1393 if (metadata) {
1394 /* If a hostname is set, it appears we want to perform syslog
1395 * because only rfc5427 or rfc3164 support an hostname.
1396 */
1397 if (metadata[LOG_META_HOST].len) {
1398 /* If a rfc5424 compliant timestamp is used we consider
1399 * that output format is rfc5424, else legacy format
1400 * is used as specified default for local logs
1401 * in documentation.
1402 */
1403 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1404 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1405 format = LOG_FORMAT_RFC5424;
1406 else
1407 format = LOG_FORMAT_RFC3164;
1408 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001409 else if (metadata[LOG_META_TAG].len) {
1410 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001411 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001412 * in legacy format (analog to RFC3164 but
1413 * with stripped hostname).
1414 */
1415 format = LOG_FORMAT_LOCAL;
1416 }
Emeric Brun54648852020-07-06 15:54:06 +02001417 else if (metadata[LOG_META_PRIO].len) {
1418 /* the source seems a parsed message
1419 * offering a valid level/prio prefix
1420 * so we consider this format.
1421 */
1422 format = LOG_FORMAT_PRIO;
1423 }
1424 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001425 }
1426
Emeric Brun54648852020-07-06 15:54:06 +02001427 /* prepare priority, stored into 1 single elem */
1428 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001429 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001430 case LOG_FORMAT_RFC3164:
1431 case LOG_FORMAT_RFC5424:
1432 case LOG_FORMAT_PRIO:
1433 fac_level = facility << 3;
1434 /* further format ignore the facility */
Willy Tarreau80f9a632022-11-14 07:22:57 +01001435 __fallthrough;
Emeric Brun54648852020-07-06 15:54:06 +02001436 case LOG_FORMAT_TIMED:
1437 case LOG_FORMAT_SHORT:
1438 fac_level += level;
1439 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1440 do {
1441 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1442 fac_level /= 10;
1443 hdr_ctx.ist_vector[*nbelem].ptr--;
1444 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1445 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1446 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1447 break;
1448 case LOG_FORMAT_ISO:
1449 case LOG_FORMAT_RAW:
1450 break;
1451 case LOG_FORMAT_UNSPEC:
1452 case LOG_FORMATS:
1453 ABORT_NOW();
1454 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001455
William Lallemand2a4a44f2012-02-06 16:00:33 +01001456
Emeric Brun54648852020-07-06 15:54:06 +02001457 /* prepare timestamp, stored into a max of 4 elems */
1458 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001459 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001460 case LOG_FORMAT_RFC3164:
1461 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1462 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1463 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1464 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001465 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001466 break;
1467 }
1468 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1469 int month;
1470 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001471
Emeric Brun54648852020-07-06 15:54:06 +02001472 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001473
Emeric Brun54648852020-07-06 15:54:06 +02001474 /* compute month */
1475 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1476 if (month)
1477 month--;
1478 if (month <= 11) {
1479 /* builds log prefix ex: 'Jan 1 ' */
1480 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1481 "%s %c%c ", monthname[month],
1482 timestamp[8] != '0' ? timestamp[8] : ' ',
1483 timestamp[9]);
1484 /* we reused the timestamp_buffer, signal that it does not
1485 * contain local time anymore
1486 */
1487 hdr_ctx.cur_legacy_time = 0;
1488 if (len == 7) {
1489 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1490 /* adds 'HH:MM:SS' from iso time */
1491 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1492 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1493 /* we successfully reuse iso time, we can break */
1494 break;
1495 }
1496 }
1497 /* Failed to reuse isotime time, fallback to local legacy time */
1498 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001499
Emeric Brun54648852020-07-06 15:54:06 +02001500 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1501 /* re-builds timestamp from the current local time */
1502 get_localtime(time, &logtime);
1503
1504 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1505 "%s %2d %02d:%02d:%02d ",
1506 monthname[logtime.tm_mon],
1507 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1508 if (len != LOG_LEGACYTIME_LEN+1)
1509 hdr_ctx.cur_legacy_time = 0;
1510 else
1511 hdr_ctx.cur_legacy_time = time;
1512 }
1513 if (likely(hdr_ctx.cur_legacy_time))
1514 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1515 else
1516 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1517 break;
1518 case LOG_FORMAT_RFC5424:
1519 /* adds rfc5425 version prefix */
1520 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1521 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001522 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001523 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1524 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1525 break;
1526 }
1527 /* let continue as 'timed' and 'iso' format for usual timestamp */
Willy Tarreau80f9a632022-11-14 07:22:57 +01001528 __fallthrough;
Emeric Brun54648852020-07-06 15:54:06 +02001529 case LOG_FORMAT_TIMED:
1530 case LOG_FORMAT_ISO:
1531 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1532 * '1900:01:01T14:00:00+02:00'
1533 * '1900:01:01T10:00:00.123456-02:00'
1534 */
1535 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1536 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1537 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001538 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001539 break;
1540 }
1541 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1542 int month;
1543 char *timestamp = metadata[LOG_META_TIME].ptr;
1544
1545 for (month = 0; month < 12; month++)
1546 if (!memcmp(monthname[month], timestamp, 3))
1547 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001548
Emeric Brun54648852020-07-06 15:54:06 +02001549 if (month < 12) {
1550
1551 /* get local time to retrieve year */
1552 get_localtime(time, &logtime);
1553
1554 /* year seems changed since log */
1555 if (logtime.tm_mon < month)
1556 logtime.tm_year--;
1557
1558 /* builds rfc5424 prefix ex: '1900-01-01T' */
1559 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1560 "%4d-%02d-%c%cT",
1561 logtime.tm_year+1900, month+1,
1562 timestamp[4] != ' ' ? timestamp[4] : '0',
1563 timestamp[5]);
1564
1565 /* we reused the timestamp_buffer, signal that it does not
1566 * contain local time anymore
1567 */
1568 hdr_ctx.cur_legacy_time = 0;
1569 if (len == 11) {
1570 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1571 /* adds HH:MM:SS from legacy timestamp */
1572 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001573 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001574 /* according to rfc: -00:00 means we don't know the timezone */
1575 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1576 /* we successfully reuse legacy time, we can break */
1577 break;
1578 }
1579 }
1580 /* Failed to reuse legacy time, fallback to local iso time */
1581 }
1582 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1583 break;
1584 case LOG_FORMAT_PRIO:
1585 case LOG_FORMAT_SHORT:
1586 case LOG_FORMAT_RAW:
1587 break;
1588 case LOG_FORMAT_UNSPEC:
1589 case LOG_FORMATS:
1590 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001591 }
1592
Emeric Brun54648852020-07-06 15:54:06 +02001593 /* prepare other meta data, stored into a max of 10 elems */
1594 switch (format) {
1595 case LOG_FORMAT_RFC3164:
1596 if (metadata && metadata[LOG_META_HOST].len) {
1597 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1598 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1599 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001600 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001601 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Willy Tarreau80f9a632022-11-14 07:22:57 +01001602 __fallthrough;
Emeric Brun0237c4e2020-11-27 16:24:34 +01001603 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001604 if (!metadata || !metadata[LOG_META_TAG].len)
1605 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001606
Emeric Brun54648852020-07-06 15:54:06 +02001607 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1608 if (metadata[LOG_META_PID].len) {
1609 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1610 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1611 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1612 }
1613 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1614 break;
1615 case LOG_FORMAT_RFC5424:
1616 if (metadata && metadata[LOG_META_HOST].len) {
1617 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1618 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1619 }
1620 else
1621 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001622
Emeric Brun54648852020-07-06 15:54:06 +02001623 if (metadata && metadata[LOG_META_TAG].len) {
1624 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1625 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1626 }
1627 else
1628 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001629
Emeric Brun54648852020-07-06 15:54:06 +02001630 if (metadata && metadata[LOG_META_PID].len) {
1631 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1632 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1633 }
1634 else
1635 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001636
Emeric Brun54648852020-07-06 15:54:06 +02001637 if (metadata && metadata[LOG_META_MSGID].len) {
1638 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1639 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1640 }
1641 else
1642 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001643
Emeric Brun54648852020-07-06 15:54:06 +02001644 if (metadata && metadata[LOG_META_STDATA].len) {
1645 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1646 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1647 }
1648 else
1649 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1650 break;
1651 case LOG_FORMAT_PRIO:
1652 case LOG_FORMAT_SHORT:
1653 case LOG_FORMAT_TIMED:
1654 case LOG_FORMAT_ISO:
1655 case LOG_FORMAT_RAW:
1656 break;
1657 case LOG_FORMAT_UNSPEC:
1658 case LOG_FORMATS:
1659 ABORT_NOW();
1660 }
1661
1662 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001663}
1664
1665/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001666 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001667 * The argument <metadata> MUST be an array of size
1668 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001669 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001670 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001671 */
Emeric Brun54648852020-07-06 15:54:06 +02001672static 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 +01001673{
Emeric Brun54648852020-07-06 15:54:06 +02001674 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001675 static THREAD_LOCAL struct msghdr msghdr = {
1676 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001677 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001678 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001679 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1680 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001681 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001682 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001683 size_t nbelem;
1684 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001685
1686 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001687
Emeric Brunfa9d7802020-05-28 14:21:33 +02001688 /* historically some messages used to already contain the trailing LF
1689 * or Zero. Let's remove all trailing LF or Zero
1690 */
Emeric Brun54648852020-07-06 15:54:06 +02001691 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001692 size--;
1693
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001694 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001695 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001696 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001697 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001698 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1699 /* the socket's address is a file descriptor */
1700 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1701 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001702 else if (logsrv->addr.ss_family == AF_UNIX)
1703 plogfd = &logfdunix;
1704 else
1705 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001706
Willy Tarreauc046d162019-08-30 15:24:59 +02001707 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001708 /* socket not successfully initialized yet */
1709 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1710 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1711 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001712
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001713 if (!once) {
1714 once = 1; /* note: no need for atomic ops here */
1715 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1716 nblogger, strerror(errno), errno);
1717 }
1718 return;
1719 } else {
1720 /* we don't want to receive anything on this socket */
1721 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1722 /* does nothing under Linux, maybe needed for others */
1723 shutdown(*plogfd, SHUT_RD);
Willy Tarreau38247432022-04-26 10:24:14 +02001724 fd_set_cloexec(*plogfd);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001725 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001726 }
1727
Emeric Brun54648852020-07-06 15:54:06 +02001728 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1729 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001730 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001731 struct ist msg;
Aurelien DARRAGON7d4e87b2023-06-26 17:15:48 +02001732 size_t maxlen = logsrv->maxlen;
Emeric Brun54648852020-07-06 15:54:06 +02001733
1734 msg = ist2(message, size);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01001735 msg = isttrim(msg, logsrv->maxlen);
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001736
Aurelien DARRAGON7d4e87b2023-06-26 17:15:48 +02001737 /* make room for the final '\n' which may be forcefully inserted
1738 * by tcp forwarder applet (sink_forward_io_handler)
1739 */
1740 maxlen -= 1;
1741
1742 sent = sink_write(logsrv->sink, maxlen, &msg, 1, level, facility, metadata);
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001743 }
1744 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1745 struct ist msg;
1746
1747 msg = ist2(message, size);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01001748 msg = isttrim(msg, logsrv->maxlen);
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001749
1750 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001751 }
1752 else {
Emeric Brun54648852020-07-06 15:54:06 +02001753 int i = 0;
Aurelien DARRAGONa0211342023-06-27 11:32:06 +02001754 int totlen = logsrv->maxlen - 1; /* save space for the final '\n' */
Emeric Brun54648852020-07-06 15:54:06 +02001755
1756 for (i = 0 ; i < nbelem ; i++ ) {
1757 iovec[i].iov_base = msg_header[i].ptr;
1758 iovec[i].iov_len = msg_header[i].len;
1759 if (totlen <= iovec[i].iov_len) {
1760 iovec[i].iov_len = totlen;
1761 totlen = 0;
1762 break;
1763 }
1764 totlen -= iovec[i].iov_len;
1765 }
1766 if (totlen) {
1767 iovec[i].iov_base = message;
1768 iovec[i].iov_len = size;
1769 if (totlen <= iovec[i].iov_len)
1770 iovec[i].iov_len = totlen;
1771 i++;
1772 }
1773 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1774 iovec[i].iov_len = 1;
1775 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001776
Emeric Brun54648852020-07-06 15:54:06 +02001777 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001778 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1779 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001780
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001781 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1782 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001783
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001784 if (sent < 0) {
1785 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001786
Willy Tarreauacef5e22022-04-25 20:32:15 +02001787 if (errno == EAGAIN || errno == EWOULDBLOCK)
Willy Tarreau4781b152021-04-06 13:53:36 +02001788 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001789 else if (!once) {
1790 once = 1; /* note: no need for atomic ops here */
1791 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1792 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001793 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001794 }
1795}
Dragan Dosen59cee972015-09-19 22:09:02 +02001796
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001797/*
1798 * This function sends a syslog message.
1799 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001800 * The argument <metadata> MUST be an array of size
1801 * LOG_META_FIELDS*sizeof(struct ist) containing
1802 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001803 */
Emeric Brun54648852020-07-06 15:54:06 +02001804void process_send_log(struct list *logsrvs, int level, int facility,
1805 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001806{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001807 struct logsrv *logsrv;
1808 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001809
1810 /* Send log messages to syslog server. */
1811 nblogger = 0;
1812 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001813 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001814
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001815 /* we can filter the level of the messages that are sent to each logger */
1816 if (level > logsrv->level)
1817 continue;
1818
Frédéric Lécailled803e472019-04-25 07:42:09 +02001819 if (logsrv->lb.smp_rgs) {
1820 struct smp_log_range *curr_rg;
1821
1822 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1823 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1824 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1825 if (in_range) {
1826 /* Let's consume this range. */
1827 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1828 if (!curr_rg->curr_idx) {
1829 /* If consumed, let's select the next range. */
1830 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1831 }
1832 }
1833 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1834 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1835 }
1836 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001837 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1838 (facility == -1) ? logsrv->facility : facility,
1839 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001840 }
1841}
1842
Emeric Brun54648852020-07-06 15:54:06 +02001843/*
1844 * This function sends a syslog message.
1845 * It doesn't care about errors nor does it report them.
1846 * The arguments <sd> and <sd_size> are used for the structured-data part
1847 * in RFC5424 formatted syslog messages.
1848 */
1849void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1850 char *message, size_t size, char *sd, size_t sd_size)
1851{
1852 static THREAD_LOCAL pid_t curr_pid;
1853 static THREAD_LOCAL char pidstr[16];
1854 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1855
1856 if (logsrvs == NULL) {
1857 if (!LIST_ISEMPTY(&global.logsrvs)) {
1858 logsrvs = &global.logsrvs;
1859 }
1860 }
1861 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1862 return;
1863
1864 if (!metadata[LOG_META_HOST].len) {
1865 if (global.log_send_hostname)
Tim Duesterhus77508502022-03-15 13:11:06 +01001866 metadata[LOG_META_HOST] = ist(global.log_send_hostname);
Emeric Brun54648852020-07-06 15:54:06 +02001867 }
1868
1869 if (!tagb || !tagb->area)
1870 tagb = &global.log_tag;
1871
1872 if (tagb)
1873 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1874
1875 if (unlikely(curr_pid != getpid()))
1876 metadata[LOG_META_PID].len = 0;
1877
1878 if (!metadata[LOG_META_PID].len) {
1879 curr_pid = getpid();
1880 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1881 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1882 }
1883
1884 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1885
1886 /* Remove trailing space of structured data */
1887 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1888 metadata[LOG_META_STDATA].len--;
1889
1890 return process_send_log(logsrvs, level, -1, metadata, message, size);
1891}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001892
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001893const 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 +01001894const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1895 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1896 Set-cookie Updated, unknown, unknown */
1897
William Lallemand1d705562012-03-12 12:46:41 +01001898/*
1899 * try to write a character if there is enough space, or goto out
1900 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001901#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001902 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001903 *(tmplog++) = (x); \
1904 } else { \
1905 goto out; \
1906 } \
1907 } while(0)
1908
Dragan Dosen835b9212016-02-12 13:23:03 +01001909
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001910/* Initializes some log data at boot */
1911static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001912{
1913 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001914 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001915
1916 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1917 * inside PARAM-VALUE should be escaped with '\' as prefix.
1918 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1919 * details.
1920 */
1921 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1922
1923 tmp = "\"\\]";
1924 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001925 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001926 tmp++;
1927 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001928
1929 /* initialize the log header encoding map : '{|}"#' should be encoded with
1930 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1931 * URL encoding only requires '"', '#' to be encoded as well as non-
1932 * printable characters above.
1933 */
1934 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1935 memset(url_encode_map, 0, sizeof(url_encode_map));
1936 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001937 ha_bit_set(i, hdr_encode_map);
1938 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001939 }
1940 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001941 ha_bit_set(i, hdr_encode_map);
1942 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001943 }
1944
1945 tmp = "\"#{|}";
1946 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001947 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001948 tmp++;
1949 }
1950
1951 tmp = "\"#";
1952 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001953 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001954 tmp++;
1955 }
1956
1957 /* initialize the http header encoding map. The draft httpbis define the
1958 * header content as:
1959 *
1960 * HTTP-message = start-line
1961 * *( header-field CRLF )
1962 * CRLF
1963 * [ message-body ]
1964 * header-field = field-name ":" OWS field-value OWS
1965 * field-value = *( field-content / obs-fold )
1966 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1967 * obs-fold = CRLF 1*( SP / HTAB )
1968 * field-vchar = VCHAR / obs-text
1969 * VCHAR = %x21-7E
1970 * obs-text = %x80-FF
1971 *
1972 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1973 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08001974 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02001975 */
1976 memset(http_encode_map, 0, sizeof(http_encode_map));
1977 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001978 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001979 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001980 ha_bit_set(i, http_encode_map);
1981 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001982}
William Lallemand1d705562012-03-12 12:46:41 +01001983
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001984INITCALL0(STG_PREPARE, init_log);
1985
Christopher Faulet0132d062017-07-26 15:33:35 +02001986/* Initialize log buffers used for syslog messages */
1987int init_log_buffers()
1988{
Christopher Faulet0132d062017-07-26 15:33:35 +02001989 logline = my_realloc2(logline, global.max_syslog_len + 1);
1990 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02001991 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02001992 return 0;
1993 return 1;
1994}
1995
1996/* Deinitialize log buffers used for syslog messages */
1997void deinit_log_buffers()
1998{
Christopher Faulet0132d062017-07-26 15:33:35 +02001999 free(logline);
2000 free(logline_rfc5424);
Christopher Faulet0132d062017-07-26 15:33:35 +02002001 logline = NULL;
2002 logline_rfc5424 = NULL;
2003}
2004
Willy Tarreaudf974472012-12-28 02:44:01 +01002005/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2006 * <maxsize> characters. Returns the size of the output string in characters,
2007 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002008 * is not zero. It requires a valid session and optionally a stream. If the
2009 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002010 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002011int 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 +02002012{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002013 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002014 struct proxy *be;
2015 struct http_txn *txn;
2016 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002017 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002018 unsigned int s_flags;
2019 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002020 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002021 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002022 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002023 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002024 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002025 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002026 int t_request;
2027 int hdr;
2028 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002029 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002030 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002031 char *ret;
2032 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002033 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002034 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002035 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002036 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002037 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002038 struct http_uri_parser parser;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002039
William Lallemandbddd4fd2012-02-27 11:23:10 +01002040 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002041
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002042 if (likely(s)) {
2043 be = s->be;
2044 txn = s->txn;
Willy Tarreaufd9417b2022-05-18 16:23:22 +02002045 be_conn = sc_conn(s->scb);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002046 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002047 s_flags = s->flags;
2048 uniq_id = s->uniq_id;
2049 logs = &s->logs;
2050 } else {
2051 /* we have no stream so we first need to initialize a few
2052 * things that are needed later. We do increment the request
2053 * ID so that it's uniquely assigned to this request just as
2054 * if the request had reached the point of being processed.
2055 * A request error is reported as it's the only element we have
2056 * here and which justifies emitting such a log.
2057 */
Christopher Fauletfd818482021-04-14 14:01:41 +02002058 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002059 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002060 fe_conn = objt_conn(sess->origin);
Willy Tarreaubde14ad2022-05-27 10:04:04 +02002061 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? sc_conn(__objt_check(sess->origin)->sc) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002062 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002063 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02002064 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002065
2066 /* prepare a valid log structure */
Willy Tarreauad5a5f62023-04-27 09:46:02 +02002067 tmp_strm_log.accept_ts = sess->accept_ts;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002068 tmp_strm_log.accept_date = sess->accept_date;
2069 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02002070 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreauad5a5f62023-04-27 09:46:02 +02002071 tmp_strm_log.request_ts = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002072 tmp_strm_log.t_queue = -1;
2073 tmp_strm_log.t_connect = -1;
2074 tmp_strm_log.t_data = -1;
Willy Tarreau69530f52023-04-28 09:16:15 +02002075 tmp_strm_log.t_close = ns_to_ms(now_ns - sess->accept_ts);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002076 tmp_strm_log.bytes_in = 0;
2077 tmp_strm_log.bytes_out = 0;
2078 tmp_strm_log.prx_queue_pos = 0;
2079 tmp_strm_log.srv_queue_pos = 0;
2080
2081 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002082
2083 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002084 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, &status);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002085
2086 switch (es) {
2087 case MUX_ES_SUCCESS:
2088 break;
2089 case MUX_ES_INVALID_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002090 status = (status ? status : 400);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002091 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2092 s_flags = SF_ERR_CLICL | SF_FINST_R;
2093 else
2094 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2095 break;
2096 case MUX_ES_TOUT_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002097 status = (status ? status : 408);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002098 s_flags = SF_ERR_CLITO | SF_FINST_R;
2099 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002100 case MUX_ES_NOTIMPL_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002101 status = (status ? status : 501);
Christopher Faulet142dd332020-12-07 11:24:37 +01002102 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2103 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002104 case MUX_ES_INTERNAL_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002105 status = (status ? status : 500);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002106 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2107 break;
2108 default:
2109 break;
2110 }
2111 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002112 }
2113
William Lallemandbddd4fd2012-02-27 11:23:10 +01002114 t_request = -1;
Willy Tarreauad5a5f62023-04-27 09:46:02 +02002115 if ((llong)(logs->request_ts - logs->accept_ts) >= 0)
2116 t_request = ns_to_ms(logs->request_ts - logs->accept_ts);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002117
William Lallemand1d705562012-03-12 12:46:41 +01002118 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002119
William Lallemandbddd4fd2012-02-27 11:23:10 +01002120 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002121 if (LIST_ISEMPTY(list_format))
2122 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002123
William Lallemand1d705562012-03-12 12:46:41 +01002124 list_for_each_entry(tmp, list_format, list) {
Christopher Faulet52b28d22021-10-27 11:58:05 +02002125#ifdef USE_OPENSSL
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002126 struct connection *conn;
Christopher Faulet52b28d22021-10-27 11:58:05 +02002127#endif
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002128 const struct sockaddr_storage *addr;
Willy Tarreau4f653562012-10-12 19:48:16 +02002129 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002130 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002131 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002132
Willy Tarreauc8368452012-12-21 00:09:23 +01002133 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002134 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002135 if (!last_isspace) {
2136 LOGCHAR(' ');
2137 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002138 }
2139 break;
2140
William Lallemand1d705562012-03-12 12:46:41 +01002141 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002142 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002143 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002144 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002145 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002146 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002147 last_isspace = 0;
2148 break;
2149
Willy Tarreauc8368452012-12-21 00:09:23 +01002150 case LOG_FMT_EXPR: // sample expression, may be request or response
2151 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002152 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002153 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 +02002154
Christopher Faulet5f940702020-04-06 10:40:02 +02002155 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002156 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 +02002157
2158 if (!key && !(tmp->options & (LOG_OPT_REQ_CAP|LOG_OPT_RES_CAP))) // cfg, cli
2159 key = sample_fetch_as_type(be, sess, s, SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
2160
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002161 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002162 ret = lf_encode_chunk(tmplog, dst + maxsize,
2163 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002164 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002165 ret = lf_text_len(tmplog,
2166 key ? key->data.u.str.area : NULL,
2167 key ? key->data.u.str.data : 0,
2168 dst + maxsize - tmplog,
2169 tmp);
Aurelien DARRAGON9c5dd212024-04-09 15:28:00 +02002170 if (ret == NULL)
Willy Tarreauc8368452012-12-21 00:09:23 +01002171 goto out;
2172 tmplog = ret;
2173 last_isspace = 0;
2174 break;
2175
Willy Tarreau2beef582012-12-20 17:22:52 +01002176 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreaud68ff012022-05-27 08:57:21 +02002177 addr = (s ? sc_src(s->scf) : sess_src(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002178 if (addr)
2179 ret = lf_ip(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002180 else
2181 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002182
William Lallemand1d705562012-03-12 12:46:41 +01002183 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002184 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002185 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002186 last_isspace = 0;
2187 break;
2188
Willy Tarreau2beef582012-12-20 17:22:52 +01002189 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreaud68ff012022-05-27 08:57:21 +02002190 addr = (s ? sc_src(s->scf) : sess_src(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002191 if (addr) {
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002192 /* sess->listener is always defined when the session's owner is an inbound connections */
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002193 if (addr->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002194 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002195 else
2196 ret = lf_port(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002197 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002198 else
2199 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2200
William Lallemand5f232402012-04-05 18:02:55 +02002201 if (ret == NULL)
2202 goto out;
2203 tmplog = ret;
2204 last_isspace = 0;
2205 break;
2206
Willy Tarreau2beef582012-12-20 17:22:52 +01002207 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreaud68ff012022-05-27 08:57:21 +02002208 addr = (s ? sc_dst(s->scf) : sess_dst(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002209 if (addr)
2210 ret = lf_ip(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002211 else
2212 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2213
William Lallemand1d705562012-03-12 12:46:41 +01002214 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002215 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002216 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002217 last_isspace = 0;
2218 break;
2219
Willy Tarreau2beef582012-12-20 17:22:52 +01002220 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreaud68ff012022-05-27 08:57:21 +02002221 addr = (s ? sc_dst(s->scf) : sess_dst(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002222 if (addr) {
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002223 /* sess->listener is always defined when the session's owner is an inbound connections */
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002224 if (addr->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002225 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002226 else
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002227 ret = lf_port(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002228 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002229 else
2230 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2231
William Lallemand5f232402012-04-05 18:02:55 +02002232 if (ret == NULL)
2233 goto out;
2234 tmplog = ret;
2235 last_isspace = 0;
2236 break;
2237
Willy Tarreau2beef582012-12-20 17:22:52 +01002238 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002239 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002240 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002241 else
2242 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2243
William Lallemand1d705562012-03-12 12:46:41 +01002244 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002245 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002246 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002247 last_isspace = 0;
2248 break;
2249
Willy Tarreau2beef582012-12-20 17:22:52 +01002250 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002251 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002252 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002253 else
2254 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2255
William Lallemand5f232402012-04-05 18:02:55 +02002256 if (ret == NULL)
2257 goto out;
2258 tmplog = ret;
2259 last_isspace = 0;
2260 break;
2261
Willy Tarreau2beef582012-12-20 17:22:52 +01002262 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002263 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002264 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002265 else
2266 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2267
William Lallemand5f232402012-04-05 18:02:55 +02002268 if (ret == NULL)
2269 goto out;
2270 tmplog = ret;
2271 last_isspace = 0;
2272 break;
2273
Willy Tarreau2beef582012-12-20 17:22:52 +01002274 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002275 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002276 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002277 else
2278 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2279
William Lallemand1d705562012-03-12 12:46:41 +01002280 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002281 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002282 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002283 last_isspace = 0;
2284 break;
2285
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002286 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002287 get_localtime(logs->accept_date.tv_sec, &tm);
2288 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002289 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002290 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002291 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002292 last_isspace = 0;
2293 break;
2294
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002295 case LOG_FMT_tr: // %tr = start of request date
2296 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002297 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 +02002298 get_localtime(tv.tv_sec, &tm);
2299 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2300 if (ret == NULL)
2301 goto out;
2302 tmplog = ret;
2303 last_isspace = 0;
2304 break;
2305
2306 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002307 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002308 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002309 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002310 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002311 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002312 last_isspace = 0;
2313 break;
2314
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002315 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002316 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 +02002317 get_gmtime(tv.tv_sec, &tm);
2318 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2319 if (ret == NULL)
2320 goto out;
2321 tmplog = ret;
2322 last_isspace = 0;
2323 break;
2324
2325 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002326 get_localtime(logs->accept_date.tv_sec, &tm);
2327 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002328 if (ret == NULL)
2329 goto out;
2330 tmplog = ret;
2331 last_isspace = 0;
2332 break;
2333
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002334 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002335 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 +02002336 get_localtime(tv.tv_sec, &tm);
2337 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2338 if (ret == NULL)
2339 goto out;
2340 tmplog = ret;
2341 last_isspace = 0;
2342 break;
2343
William Lallemand5f232402012-04-05 18:02:55 +02002344 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002345 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002346 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02002347 if (iret < 0 || iret >= dst + maxsize - tmplog)
William Lallemand5f232402012-04-05 18:02:55 +02002348 goto out;
2349 last_isspace = 0;
2350 tmplog += iret;
2351 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002352 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002353 if (ret == NULL)
2354 goto out;
2355 tmplog = ret;
2356 last_isspace = 0;
2357 }
2358 break;
2359
William Lallemand1d705562012-03-12 12:46:41 +01002360 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002361 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002362 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02002363 if (iret < 0 || iret >= dst + maxsize - tmplog)
William Lallemand5f232402012-04-05 18:02:55 +02002364 goto out;
2365 last_isspace = 0;
2366 tmplog += iret;
2367 } else {
2368 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002369 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002370 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002371 tmplog, 4);
2372 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002373 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002374 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002375 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002376 }
2377 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002378
William Lallemand1d705562012-03-12 12:46:41 +01002379 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002380 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002381 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002382 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002383 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002384 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002385 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002386 break;
2387
Willy Tarreau773d65f2012-10-12 14:56:11 +02002388 case LOG_FMT_FRONTEND_XPRT: // %ft
2389 src = fe->id;
2390 if (tmp->options & LOG_OPT_QUOTE)
2391 LOGCHAR('"');
2392 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2393 if (iret == 0)
2394 goto out;
2395 tmplog += iret;
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002396
2397 /* sess->listener may be undefined if the session's owner is a health-check */
Willy Tarreau807a3a52022-04-12 08:08:33 +02002398 if (sess->listener && sess->listener->bind_conf->xprt->get_ssl_sock_ctx)
Willy Tarreau773d65f2012-10-12 14:56:11 +02002399 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002400 if (tmp->options & LOG_OPT_QUOTE)
2401 LOGCHAR('"');
2402 last_isspace = 0;
2403 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002404#ifdef USE_OPENSSL
2405 case LOG_FMT_SSL_CIPHER: // %sslc
2406 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002407 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002408 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002409 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002410 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002411 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2412 if (ret == NULL)
2413 goto out;
2414 tmplog = ret;
2415 last_isspace = 0;
2416 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002417
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002418 case LOG_FMT_SSL_VERSION: // %sslv
2419 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002420 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002421 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002422 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002423 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002424 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2425 if (ret == NULL)
2426 goto out;
2427 tmplog = ret;
2428 last_isspace = 0;
2429 break;
2430#endif
William Lallemand1d705562012-03-12 12:46:41 +01002431 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002432 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002433 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002434 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002435 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002436 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002437 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002438 break;
2439
William Lallemand1d705562012-03-12 12:46:41 +01002440 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002441 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002442 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002443 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002444 break;
2445 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002446 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002447 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002448 case OBJ_TYPE_CHECK:
2449 src = (__objt_check(sess->origin)->server
2450 ? __objt_check(sess->origin)->server->id
2451 : "<NOSRV>");
2452 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002453 default:
2454 src = "<NOSRV>";
2455 break;
2456 }
William Lallemand5f232402012-04-05 18:02:55 +02002457 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002458 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002459 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002460 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002461 last_isspace = 0;
2462 break;
2463
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002464 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002465 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002466 if (ret == NULL)
2467 goto out;
2468 tmplog = ret;
2469 last_isspace = 0;
2470 break;
2471
2472 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002473 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002474 if (ret == NULL)
2475 goto out;
2476 tmplog = ret;
2477 last_isspace = 0;
2478 break;
2479
2480 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002481 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002482 tmplog, dst + maxsize - tmplog);
2483 if (ret == NULL)
2484 goto out;
2485 tmplog = ret;
2486 last_isspace = 0;
2487 break;
2488
2489 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002490 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002491 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002492 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002493 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002494 last_isspace = 0;
2495 break;
2496
William Lallemand1d705562012-03-12 12:46:41 +01002497 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002498 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002499 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002500 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002501 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002502 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002503 last_isspace = 0;
2504 break;
2505
William Lallemand1d705562012-03-12 12:46:41 +01002506 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002507 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002508 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002509 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002510 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002511 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002512 last_isspace = 0;
2513 break;
2514
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002515 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002516 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002517 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002518 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002519 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002520 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002521 last_isspace = 0;
2522 break;
2523
Willy Tarreau27b639d2016-05-17 17:55:27 +02002524 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002525 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002526 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002527 tmplog, dst + maxsize - tmplog);
2528 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002529 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002530 tmplog, dst + maxsize - tmplog);
2531 if (ret == NULL)
2532 goto out;
2533 tmplog = ret;
2534 last_isspace = 0;
2535 break;
2536
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002537 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2538 if (!(fe->to_log & LW_BYTES))
2539 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002540 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 +02002541 tmplog, dst + maxsize - tmplog);
2542 if (ret == NULL)
2543 goto out;
2544 tmplog = ret;
2545 last_isspace = 0;
2546 break;
2547
2548 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002549 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002550 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002551 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002552 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002553 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002554 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002555 last_isspace = 0;
2556 break;
2557
Damien Claisse57c8eb92020-04-28 12:09:19 +00002558 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2559 if (!(fe->to_log & LW_BYTES))
2560 LOGCHAR('+');
2561 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2562 tmplog, dst + maxsize - tmplog);
2563 if (ret == NULL)
2564 goto out;
2565 tmplog = ret;
2566 last_isspace = 0;
2567 break;
2568
Willy Tarreau2beef582012-12-20 17:22:52 +01002569 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002570 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002571 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002572 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002573 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002574 last_isspace = 0;
2575 break;
2576
William Lallemand1d705562012-03-12 12:46:41 +01002577 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002578 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002579 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002580 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002581 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002582 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002583 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002584 last_isspace = 0;
2585 break;
2586
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002587 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002588 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002589 if (ret == NULL)
2590 goto out;
2591 tmplog = ret;
2592 last_isspace = 0;
2593 break;
2594
Willy Tarreau2beef582012-12-20 17:22:52 +01002595 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002596 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002597 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002598 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002599 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002600 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002601 last_isspace = 0;
2602 break;
2603
Willy Tarreau2beef582012-12-20 17:22:52 +01002604 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002605 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002606 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002607 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002608 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002609 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002610 last_isspace = 0;
2611 break;
2612
William Lallemand1d705562012-03-12 12:46:41 +01002613 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002614 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2615 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002616 *tmplog = '\0';
2617 last_isspace = 0;
2618 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002619
William Lallemand1d705562012-03-12 12:46:41 +01002620 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002621 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2622 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002623 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2624 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 +01002625 last_isspace = 0;
2626 break;
2627
William Lallemand1d705562012-03-12 12:46:41 +01002628 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002629 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002630 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002631 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002632 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002633 last_isspace = 0;
2634 break;
2635
William Lallemand1d705562012-03-12 12:46:41 +01002636 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002637 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002638 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002639 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002640 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002641 last_isspace = 0;
2642 break;
2643
William Lallemand1d705562012-03-12 12:46:41 +01002644 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002645 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002646 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002647 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002648 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002649 last_isspace = 0;
2650 break;
2651
William Lallemand1d705562012-03-12 12:46:41 +01002652 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002653 switch (obj_type(s ? s->target : sess->origin)) {
2654 case OBJ_TYPE_SERVER:
2655 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2656 tmplog, dst + maxsize - tmplog);
2657 break;
2658 case OBJ_TYPE_CHECK:
2659 ret = ultoa_o(__objt_check(sess->origin)->server
2660 ? __objt_check(sess->origin)->server->cur_sess
2661 : 0, tmplog, dst + maxsize - tmplog);
2662 break;
2663 default:
2664 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2665 break;
2666 }
2667
William Lallemand1d705562012-03-12 12:46:41 +01002668 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002669 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002670 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002671 last_isspace = 0;
2672 break;
2673
William Lallemand1d705562012-03-12 12:46:41 +01002674 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002675 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002676 LOGCHAR('+');
Christopher Faulet731c8e62022-03-29 16:08:44 +02002677 ret = ltoa_o((s ? s->conn_retries : 0), tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002678 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002679 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002680 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002681 last_isspace = 0;
2682 break;
2683
William Lallemand1d705562012-03-12 12:46:41 +01002684 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002685 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002686 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002687 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002688 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002689 last_isspace = 0;
2690 break;
2691
William Lallemand1d705562012-03-12 12:46:41 +01002692 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002693 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002694 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002695 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002696 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002697 last_isspace = 0;
2698 break;
2699
William Lallemand1d705562012-03-12 12:46:41 +01002700 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002701 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002702 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002703 if (tmp->options & LOG_OPT_QUOTE)
2704 LOGCHAR('"');
2705 LOGCHAR('{');
2706 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2707 if (hdr)
2708 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002709 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002710 ret = lf_encode_string(tmplog, dst + maxsize,
2711 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002712 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002713 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002714 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002715 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002716 }
2717 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002718 if (tmp->options & LOG_OPT_QUOTE)
2719 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002720 last_isspace = 0;
2721 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002722 break;
2723
William Lallemand1d705562012-03-12 12:46:41 +01002724 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002725 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002726 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002727 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2728 if (hdr > 0)
2729 LOGCHAR(' ');
2730 if (tmp->options & LOG_OPT_QUOTE)
2731 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002732 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002733 ret = lf_encode_string(tmplog, dst + maxsize,
2734 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002735 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002736 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002737 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002738 } else if (!(tmp->options & LOG_OPT_QUOTE))
2739 LOGCHAR('-');
2740 if (tmp->options & LOG_OPT_QUOTE)
2741 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002742 last_isspace = 0;
2743 }
2744 }
2745 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002746
William Lallemand1d705562012-03-12 12:46:41 +01002747
2748 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002749 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002750 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002751 if (tmp->options & LOG_OPT_QUOTE)
2752 LOGCHAR('"');
2753 LOGCHAR('{');
2754 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2755 if (hdr)
2756 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002757 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002758 ret = lf_encode_string(tmplog, dst + maxsize,
2759 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002760 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002761 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002762 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002763 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002764 }
2765 LOGCHAR('}');
2766 last_isspace = 0;
2767 if (tmp->options & LOG_OPT_QUOTE)
2768 LOGCHAR('"');
2769 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002770 break;
2771
William Lallemand1d705562012-03-12 12:46:41 +01002772 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002773 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002774 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002775 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2776 if (hdr > 0)
2777 LOGCHAR(' ');
2778 if (tmp->options & LOG_OPT_QUOTE)
2779 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002780 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002781 ret = lf_encode_string(tmplog, dst + maxsize,
2782 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002783 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002784 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002785 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002786 } else if (!(tmp->options & LOG_OPT_QUOTE))
2787 LOGCHAR('-');
2788 if (tmp->options & LOG_OPT_QUOTE)
2789 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002790 last_isspace = 0;
2791 }
2792 }
2793 break;
2794
William Lallemand1d705562012-03-12 12:46:41 +01002795 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002796 /* Request */
2797 if (tmp->options & LOG_OPT_QUOTE)
2798 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002799 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002800 ret = lf_encode_string(tmplog, dst + maxsize,
2801 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002802 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002803 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002804 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002805 if (tmp->options & LOG_OPT_QUOTE)
2806 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002807 last_isspace = 0;
2808 break;
William Lallemand5f232402012-04-05 18:02:55 +02002809
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002810 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002811 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002812
Willy Tarreaub7636d12015-06-17 19:58:02 +02002813 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002814 LOGCHAR('"');
2815
2816 end = uri + strlen(uri);
2817 // look for the first whitespace character
2818 while (uri < end && !HTTP_IS_SPHT(*uri))
2819 uri++;
2820
2821 // keep advancing past multiple spaces
2822 while (uri < end && HTTP_IS_SPHT(*uri)) {
2823 uri++; nspaces++;
2824 }
2825
2826 // look for first space or question mark after url
2827 spc = uri;
2828 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2829 spc++;
2830
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002831 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002832 chunk.area = "<BADREQ>";
2833 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002834 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002835 chunk.area = uri;
2836 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002837 }
2838
Dragan Dosen835b9212016-02-12 13:23:03 +01002839 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002840 if (ret == NULL || *ret != '\0')
2841 goto out;
2842
2843 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002844 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002845 LOGCHAR('"');
2846
2847 last_isspace = 0;
2848 break;
2849
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002850 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2851 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2852
2853 if (tmp->options & LOG_OPT_QUOTE)
2854 LOGCHAR('"');
2855
2856 end = uri + strlen(uri);
2857
2858 // look for the first whitespace character
2859 while (uri < end && !HTTP_IS_SPHT(*uri))
2860 uri++;
2861
2862 // keep advancing past multiple spaces
2863 while (uri < end && HTTP_IS_SPHT(*uri)) {
2864 uri++; nspaces++;
2865 }
2866
2867 // look for first space after url
2868 spc = uri;
2869 while (spc < end && !HTTP_IS_SPHT(*spc))
2870 spc++;
2871
Tim Duesterhus92c696e2021-02-28 16:11:36 +01002872 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002873
2874 // extract relative path without query params from url
Amaury Denoyellec453f952021-07-06 11:40:12 +02002875 parser = http_uri_parser_init(path);
2876 path = iststop(http_parse_path(&parser), '?');
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002877 if (!txn || !txn->uri || nspaces == 0) {
2878 chunk.area = "<BADREQ>";
2879 chunk.data = strlen("<BADREQ>");
2880 } else {
2881 chunk.area = path.ptr;
2882 chunk.data = path.len;
2883 }
2884
2885 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2886 if (ret == NULL || *ret != '\0')
2887 goto out;
2888
2889 tmplog = ret;
2890 if (tmp->options & LOG_OPT_QUOTE)
2891 LOGCHAR('"');
2892
2893 last_isspace = 0;
2894 break;
2895
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002896 case LOG_FMT_HTTP_QUERY: // %HQ
2897 if (tmp->options & LOG_OPT_QUOTE)
2898 LOGCHAR('"');
2899
Willy Tarreau57bc8912016-04-25 17:09:40 +02002900 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002901 chunk.area = "<BADREQ>";
2902 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002903 } else {
2904 uri = txn->uri;
2905 end = uri + strlen(uri);
2906 // look for the first question mark
2907 while (uri < end && *uri != '?')
2908 uri++;
2909
2910 qmark = uri;
2911 // look for first space or question mark after url
2912 while (uri < end && !HTTP_IS_SPHT(*uri))
2913 uri++;
2914
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002915 chunk.area = qmark;
2916 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002917 }
2918
Dragan Dosen835b9212016-02-12 13:23:03 +01002919 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002920 if (ret == NULL || *ret != '\0')
2921 goto out;
2922
2923 tmplog = ret;
2924 if (tmp->options & LOG_OPT_QUOTE)
2925 LOGCHAR('"');
2926
2927 last_isspace = 0;
2928 break;
2929
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002930 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002931 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002932
Willy Tarreaub7636d12015-06-17 19:58:02 +02002933 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002934 LOGCHAR('"');
2935
2936 end = uri + strlen(uri);
2937 // look for the first whitespace character
2938 while (uri < end && !HTTP_IS_SPHT(*uri))
2939 uri++;
2940
2941 // keep advancing past multiple spaces
2942 while (uri < end && HTTP_IS_SPHT(*uri)) {
2943 uri++; nspaces++;
2944 }
2945
2946 // look for first space after url
2947 spc = uri;
2948 while (spc < end && !HTTP_IS_SPHT(*spc))
2949 spc++;
2950
Willy Tarreau57bc8912016-04-25 17:09:40 +02002951 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002952 chunk.area = "<BADREQ>";
2953 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002954 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002955 chunk.area = uri;
2956 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002957 }
2958
Dragan Dosen835b9212016-02-12 13:23:03 +01002959 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002960 if (ret == NULL || *ret != '\0')
2961 goto out;
2962
2963 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002964 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002965 LOGCHAR('"');
2966
2967 last_isspace = 0;
2968 break;
2969
2970 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002971 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002972 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002973 LOGCHAR('"');
2974
2975 end = uri + strlen(uri);
2976 // look for the first whitespace character
2977 spc = uri;
2978 while (spc < end && !HTTP_IS_SPHT(*spc))
2979 spc++;
2980
2981 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002982 chunk.area = "<BADREQ>";
2983 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002984 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002985 chunk.area = uri;
2986 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002987 }
2988
Dragan Dosen835b9212016-02-12 13:23:03 +01002989 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002990 if (ret == NULL || *ret != '\0')
2991 goto out;
2992
2993 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002994 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002995 LOGCHAR('"');
2996
2997 last_isspace = 0;
2998 break;
2999
3000 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02003001 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003002 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003003 LOGCHAR('"');
3004
3005 end = uri + strlen(uri);
3006 // look for the first whitespace character
3007 while (uri < end && !HTTP_IS_SPHT(*uri))
3008 uri++;
3009
3010 // keep advancing past multiple spaces
3011 while (uri < end && HTTP_IS_SPHT(*uri)) {
3012 uri++; nspaces++;
3013 }
3014
3015 // look for the next whitespace character
3016 while (uri < end && !HTTP_IS_SPHT(*uri))
3017 uri++;
3018
3019 // keep advancing past multiple spaces
3020 while (uri < end && HTTP_IS_SPHT(*uri))
3021 uri++;
3022
Willy Tarreau57bc8912016-04-25 17:09:40 +02003023 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003024 chunk.area = "<BADREQ>";
3025 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003026 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003027 chunk.area = "HTTP/0.9";
3028 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003029 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003030 chunk.area = uri;
3031 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003032 }
3033
Dragan Dosen835b9212016-02-12 13:23:03 +01003034 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003035 if (ret == NULL || *ret != '\0')
3036 goto out;
3037
3038 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003039 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003040 LOGCHAR('"');
3041
3042 last_isspace = 0;
3043 break;
3044
William Lallemand5f232402012-04-05 18:02:55 +02003045 case LOG_FMT_COUNTER: // %rt
3046 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003047 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02003048 if (iret < 0 || iret >= dst + maxsize - tmplog)
William Lallemand5f232402012-04-05 18:02:55 +02003049 goto out;
3050 last_isspace = 0;
3051 tmplog += iret;
3052 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003053 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003054 if (ret == NULL)
3055 goto out;
3056 tmplog = ret;
3057 last_isspace = 0;
3058 }
3059 break;
3060
Willy Tarreau7346acb2014-08-28 15:03:15 +02003061 case LOG_FMT_LOGCNT: // %lc
3062 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003063 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02003064 if (iret < 0 || iret >= dst + maxsize - tmplog)
Willy Tarreau7346acb2014-08-28 15:03:15 +02003065 goto out;
3066 last_isspace = 0;
3067 tmplog += iret;
3068 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003069 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003070 if (ret == NULL)
3071 goto out;
3072 tmplog = ret;
3073 last_isspace = 0;
3074 }
3075 break;
3076
William Lallemand5f232402012-04-05 18:02:55 +02003077 case LOG_FMT_HOSTNAME: // %H
3078 src = hostname;
3079 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3080 if (ret == NULL)
3081 goto out;
3082 tmplog = ret;
3083 last_isspace = 0;
3084 break;
3085
3086 case LOG_FMT_PID: // %pid
3087 if (tmp->options & LOG_OPT_HEXA) {
3088 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02003089 if (iret < 0 || iret >= dst + maxsize - tmplog)
William Lallemand5f232402012-04-05 18:02:55 +02003090 goto out;
3091 last_isspace = 0;
3092 tmplog += iret;
3093 } else {
3094 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3095 if (ret == NULL)
3096 goto out;
3097 tmplog = ret;
3098 last_isspace = 0;
3099 }
3100 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003101
3102 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003103 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003104 if (s)
3105 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3106 else
3107 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003108 if (ret == NULL)
3109 goto out;
3110 tmplog = ret;
3111 last_isspace = 0;
3112 break;
3113
William Lallemandbddd4fd2012-02-27 11:23:10 +01003114 }
3115 }
3116
3117out:
William Lallemand1d705562012-03-12 12:46:41 +01003118 /* *tmplog is a unused character */
3119 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003120 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003121
Willy Tarreaubaaee002006-06-26 02:48:02 +02003122}
3123
William Lallemand1d705562012-03-12 12:46:41 +01003124/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003125 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003126 * Will not log if the frontend has no log defined.
3127 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003128void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003129{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003130 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003131 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003132 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003133
3134 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003135 err = (s->flags & SF_REDISP) ||
3136 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
Christopher Fauleta892b7f2022-06-17 14:53:20 +02003137 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) && s->conn_retries) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003138 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003139
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003140 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003141 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003142
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003143 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003144 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003145
Willy Tarreauabcd5142013-06-11 17:18:02 +02003146 if (s->logs.level) { /* loglevel was overridden */
3147 if (s->logs.level == -1) {
3148 s->logs.logwait = 0; /* logs disabled */
3149 return;
3150 }
3151 level = s->logs.level - 1;
3152 }
3153 else {
3154 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003155 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003156 level = LOG_ERR;
3157 }
William Lallemand1d705562012-03-12 12:46:41 +01003158
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003159 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003160 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003161 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003162 }
3163
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003164 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3165 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3166 &sess->fe->logformat_sd);
3167 }
3168
Dragan Dosen59cee972015-09-19 22:09:02 +02003169 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003170 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003171 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003172 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3173 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003174 s->logs.logwait = 0;
3175 }
3176}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003177
Willy Tarreau53839352018-09-05 19:51:10 +02003178/*
3179 * send a minimalist log for the session. Will not log if the frontend has no
3180 * log defined. It is assumed that this is only used to report anomalies that
3181 * cannot lead to the creation of a regular stream. Because of this the log
3182 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3183 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003184 * function to report unimportant events. It is safe to call this function with
3185 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003186 */
3187void sess_log(struct session *sess)
3188{
3189 int size, level;
3190 int sd_size = 0;
3191
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003192 if (!sess)
3193 return;
3194
Willy Tarreau53839352018-09-05 19:51:10 +02003195 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3196 return;
3197
3198 level = LOG_INFO;
3199 if (sess->fe->options2 & PR_O2_LOGERRORS)
3200 level = LOG_ERR;
3201
3202 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3203 sd_size = sess_build_logline(sess, NULL,
3204 logline_rfc5424, global.max_syslog_len,
3205 &sess->fe->logformat_sd);
3206 }
3207
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02003208 if (!LIST_ISEMPTY(&sess->fe->logformat_error))
3209 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat_error);
3210 else
3211 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
Willy Tarreau53839352018-09-05 19:51:10 +02003212 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003213 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003214 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3215 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003216 }
3217}
3218
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003219void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3220{
3221 va_list argp;
3222 int data_len;
3223
3224 if (level < 0 || format == NULL || logline == NULL)
3225 return;
3226
3227 va_start(argp, format);
3228 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3229 if (data_len < 0 || data_len > global.max_syslog_len)
3230 data_len = global.max_syslog_len;
3231 va_end(argp);
3232
3233 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3234}
Emeric Brun54932b42020-07-07 09:43:24 +02003235/*
3236 * This function parse a received log message <buf>, of size <buflen>
3237 * it fills <level>, <facility> and <metadata> depending of the detected
3238 * header format and message will point on remaining payload of <size>
3239 *
3240 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3241 * struct ist len will be set to 0 if field is not found
3242 * <level> and <facility> will be set to -1 if not found.
3243 */
3244void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3245 struct ist *metadata, char **message, size_t *size)
3246{
3247
3248 char *p;
3249 int fac_level = 0;
3250
3251 *level = *facility = -1;
3252
3253 *message = buf;
3254 *size = buflen;
3255
3256 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3257
3258 p = buf;
3259 if (*size < 2 || *p != '<')
3260 return;
3261
3262 p++;
3263 while (*p != '>') {
3264 if (*p > '9' || *p < '0')
3265 return;
3266 fac_level = 10*fac_level + (*p - '0');
3267 p++;
3268 if ((p - buf) > buflen)
3269 return;
3270 }
3271
3272 *facility = fac_level >> 3;
3273 *level = fac_level & 0x7;
3274 p++;
3275
3276 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3277
3278 buflen -= p - buf;
3279 buf = p;
3280
3281 *size = buflen;
3282 *message = buf;
3283
3284 /* for rfc5424, prio is always followed by '1' and ' ' */
3285 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3286 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3287 * followed by message.
3288 * Each header field can present NILVALUE: '-'
3289 */
3290
3291 p += 2;
Aurelien DARRAGONab9efc22022-11-22 11:17:11 +01003292 *size -= 2;
Emeric Brun54932b42020-07-07 09:43:24 +02003293 /* timestamp is NILVALUE '-' */
3294 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3295 metadata[LOG_META_TIME] = ist2(p, 1);
3296 p++;
3297 }
3298 else if (*size > LOG_ISOTIME_MINLEN) {
3299 metadata[LOG_META_TIME].ptr = p;
3300
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003301 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003302 * in timestamp.
3303 * possible format are:
3304 * ex: '1970-01-01T00:00:00.000000Z'
3305 * '1970-01-01T00:00:00.000000+00:00'
3306 * '1970-01-01T00:00:00.000000-00:00'
3307 * '1970-01-01T00:00:00Z'
3308 * '1970-01-01T00:00:00+00:00'
3309 * '1970-01-01T00:00:00-00:00'
3310 */
3311 p += 19;
3312 if (*p == '.') {
3313 p++;
3314 if ((p - buf) >= buflen)
3315 goto bad_format;
3316 while (*p != 'Z' && *p != '+' && *p != '-') {
3317 if ((unsigned char)(*p - '0') > 9)
3318 goto bad_format;
3319
3320 p++;
3321 if ((p - buf) >= buflen)
3322 goto bad_format;
3323 }
3324 }
3325
3326 if (*p == 'Z')
3327 p++;
3328 else
3329 p += 6; /* case of '+00:00 or '-00:00' */
3330
3331 if ((p - buf) >= buflen || *p != ' ')
3332 goto bad_format;
3333 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3334 }
3335 else
3336 goto bad_format;
3337
3338
3339 p++;
3340 if ((p - buf) >= buflen || *p == ' ')
3341 goto bad_format;
3342
3343 metadata[LOG_META_HOST].ptr = p;
3344 while (*p != ' ') {
3345 p++;
3346 if ((p - buf) >= buflen)
3347 goto bad_format;
3348 }
3349 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3350 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3351 metadata[LOG_META_HOST].len = 0;
3352
3353 p++;
3354 if ((p - buf) >= buflen || *p == ' ')
3355 goto bad_format;
3356
3357 metadata[LOG_META_TAG].ptr = p;
3358 while (*p != ' ') {
3359 p++;
3360 if ((p - buf) >= buflen)
3361 goto bad_format;
3362 }
3363 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3364 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3365 metadata[LOG_META_TAG].len = 0;
3366
3367 p++;
3368 if ((p - buf) >= buflen || *p == ' ')
3369 goto bad_format;
3370
3371 metadata[LOG_META_PID].ptr = p;
3372 while (*p != ' ') {
3373 p++;
3374 if ((p - buf) >= buflen)
3375 goto bad_format;
3376 }
3377 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3378 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3379 metadata[LOG_META_PID].len = 0;
3380
3381 p++;
3382 if ((p - buf) >= buflen || *p == ' ')
3383 goto bad_format;
3384
3385 metadata[LOG_META_MSGID].ptr = p;
3386 while (*p != ' ') {
3387 p++;
3388 if ((p - buf) >= buflen)
3389 goto bad_format;
3390 }
3391 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3392 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3393 metadata[LOG_META_MSGID].len = 0;
3394
3395 p++;
3396 if ((p - buf) >= buflen || *p == ' ')
3397 goto bad_format;
3398
3399 /* structured data format is:
3400 * ex:
3401 * '[key1=value1 key2=value2][key3=value3]'
3402 *
3403 * space is invalid outside [] because
3404 * considered as the end of structured data field
3405 */
3406 metadata[LOG_META_STDATA].ptr = p;
3407 if (*p == '[') {
3408 int elem = 0;
3409
3410 while (1) {
3411 if (elem) {
3412 /* according to rfc this char is escaped in param values */
3413 if (*p == ']' && *(p-1) != '\\')
3414 elem = 0;
3415 }
3416 else {
3417 if (*p == '[')
3418 elem = 1;
3419 else if (*p == ' ')
3420 break;
3421 else
3422 goto bad_format;
3423 }
3424 p++;
3425 if ((p - buf) >= buflen)
3426 goto bad_format;
3427 }
3428 }
3429 else if (*p == '-') {
3430 /* case of NILVALUE */
3431 p++;
3432 if ((p - buf) >= buflen || *p != ' ')
3433 goto bad_format;
3434 }
3435 else
3436 goto bad_format;
3437
3438 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3439 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3440 metadata[LOG_META_STDATA].len = 0;
3441
3442 p++;
3443
3444 buflen -= p - buf;
3445 buf = p;
3446
3447 *size = buflen;
3448 *message = p;
3449 }
3450 else if (*size > LOG_LEGACYTIME_LEN) {
3451 int m;
3452
3453 /* supported header format according to rfc3164.
3454 * ex:
3455 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3456 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3457 * or 'Jan 1 00:00:00 HOSTNAME '
3458 * Note: HOSTNAME is mandatory, and day
3459 * of month uses a single space prefix if
3460 * less than 10 to ensure hour offset is
3461 * always the same.
3462 */
3463
3464 /* Check month to see if it correspond to a rfc3164
3465 * header ex 'Jan 1 00:00:00' */
3466 for (m = 0; m < 12; m++)
3467 if (!memcmp(monthname[m], p, 3))
3468 break;
3469 /* Month not found */
3470 if (m == 12)
3471 goto bad_format;
3472
3473 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3474
3475 p += LOG_LEGACYTIME_LEN;
3476 if ((p - buf) >= buflen || *p != ' ')
3477 goto bad_format;
3478
3479 p++;
3480 if ((p - buf) >= buflen || *p == ' ')
3481 goto bad_format;
3482
3483 metadata[LOG_META_HOST].ptr = p;
3484 while (*p != ' ') {
3485 p++;
3486 if ((p - buf) >= buflen)
3487 goto bad_format;
3488 }
3489 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3490
3491 /* TAG seems to no be mandatory */
3492 p++;
3493
3494 buflen -= p - buf;
3495 buf = p;
3496
3497 *size = buflen;
3498 *message = buf;
3499
3500 if (!buflen)
3501 return;
3502
3503 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3504 p++;
3505
3506 /* a tag must present a trailing ':' */
3507 if (((p - buf) >= buflen) || *p != ':')
3508 return;
3509 p++;
3510 /* followed by a space */
3511 if (((p - buf) >= buflen) || *p != ' ')
3512 return;
3513
3514 /* rewind to parse tag and pid */
3515 p = buf;
3516 metadata[LOG_META_TAG].ptr = p;
3517 /* we have the guarantee that ':' will be reach before size limit */
3518 while (*p != ':') {
3519 if (*p == '[') {
3520 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3521 metadata[LOG_META_PID].ptr = p + 1;
3522 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003523 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003524 if (p[1] != ':')
3525 return;
3526 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3527 }
3528 p++;
3529 }
3530 if (!metadata[LOG_META_TAG].len)
3531 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3532
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003533 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003534 p += 2;
3535
3536 buflen -= p - buf;
3537 buf = p;
3538
3539 *size = buflen;
3540 *message = buf;
3541 }
3542
3543 return;
3544
3545bad_format:
3546 /* bad syslog format, we reset all parsed syslog fields
3547 * but priority is kept because we are able to re-build
3548 * this message using LOF_FORMAT_PRIO.
3549 */
3550 metadata[LOG_META_TIME].len = 0;
3551 metadata[LOG_META_HOST].len = 0;
3552 metadata[LOG_META_TAG].len = 0;
3553 metadata[LOG_META_PID].len = 0;
3554 metadata[LOG_META_MSGID].len = 0;
3555 metadata[LOG_META_STDATA].len = 0;
3556
3557 return;
3558}
3559
3560/*
3561 * UDP syslog fd handler
3562 */
3563void syslog_fd_handler(int fd)
3564{
3565 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3566 ssize_t ret = 0;
3567 struct buffer *buf = get_trash_chunk();
3568 size_t size;
3569 char *message;
3570 int level;
3571 int facility;
3572 struct listener *l = objt_listener(fdtab[fd].owner);
3573 int max_accept;
3574
Tim Duesterhus16554242021-09-15 13:58:49 +02003575 BUG_ON(!l);
Emeric Brun54932b42020-07-07 09:43:24 +02003576
Willy Tarreauf5090652021-04-06 17:23:40 +02003577 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003578
3579 if (!fd_recv_ready(fd))
3580 return;
3581
Willy Tarreau882f2482023-01-12 18:52:23 +01003582 max_accept = l->bind_conf->maxaccept ? l->bind_conf->maxaccept : 1;
Emeric Brun54932b42020-07-07 09:43:24 +02003583
3584 do {
3585 /* Source address */
3586 struct sockaddr_storage saddr = {0};
3587 socklen_t saddrlen;
3588
3589 saddrlen = sizeof(saddr);
3590
3591 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3592 if (ret < 0) {
3593 if (errno == EINTR)
3594 continue;
Willy Tarreauacef5e22022-04-25 20:32:15 +02003595 if (errno == EAGAIN || errno == EWOULDBLOCK)
Emeric Brun54932b42020-07-07 09:43:24 +02003596 fd_cant_recv(fd);
3597 goto out;
3598 }
3599 buf->data = ret;
3600
Emeric Brun45c457a2020-07-09 23:23:34 +02003601 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003602 _HA_ATOMIC_INC(&cum_log_messages);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01003603 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend, 0);
Emeric Brun45c457a2020-07-09 23:23:34 +02003604
Emeric Brun54932b42020-07-07 09:43:24 +02003605 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3606
3607 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3608
3609 } while (--max_accept);
3610 }
3611
3612out:
3613 return;
3614}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003615
Emeric Brun12941c82020-07-07 14:19:42 +02003616/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003617 * IO Handler to handle message exchange with a syslog tcp client
3618 */
3619static void syslog_io_handler(struct appctx *appctx)
3620{
3621 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
Willy Tarreauc12b3212022-05-27 11:08:15 +02003622 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau270a4572022-05-27 10:36:36 +02003623 struct stream *s = __sc_strm(sc);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003624 struct proxy *frontend = strm_fe(s);
3625 struct listener *l = strm_li(s);
3626 struct buffer *buf = get_trash_chunk();
3627 int max_accept;
3628 int to_skip;
3629 int facility;
3630 int level;
3631 char *message;
3632 size_t size;
3633
Christopher Faulet211452e2023-04-11 07:56:50 +02003634 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
3635 co_skip(sc_oc(sc), co_data(sc_oc(sc)));
Christopher Faulet22a88f02023-03-31 11:17:13 +02003636 goto out;
Christopher Faulet211452e2023-04-11 07:56:50 +02003637 }
Christopher Faulet22a88f02023-03-31 11:17:13 +02003638
Willy Tarreau882f2482023-01-12 18:52:23 +01003639 max_accept = l->bind_conf->maxaccept ? l->bind_conf->maxaccept : 1;
Christopher Faulet285aa402023-04-17 16:34:29 +02003640 while (1) {
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003641 char c;
3642
3643 if (max_accept <= 0)
3644 goto missing_budget;
3645 max_accept--;
3646
Willy Tarreau270a4572022-05-27 10:36:36 +02003647 to_skip = co_getchar(sc_oc(sc), &c);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003648 if (!to_skip)
3649 goto missing_data;
3650 else if (to_skip < 0)
3651 goto cli_abort;
3652
3653 if (c == '<') {
3654 /* rfc-6587, Non-Transparent-Framing: messages separated by
3655 * a trailing LF or CR LF
3656 */
Willy Tarreau270a4572022-05-27 10:36:36 +02003657 to_skip = co_getline(sc_oc(sc), buf->area, buf->size);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003658 if (!to_skip)
3659 goto missing_data;
3660 else if (to_skip < 0)
3661 goto cli_abort;
3662
3663 if (buf->area[to_skip - 1] != '\n')
3664 goto parse_error;
3665
3666 buf->data = to_skip - 1;
3667
3668 /* according to rfc-6587, some devices adds CR before LF */
3669 if (buf->data && buf->area[buf->data - 1] == '\r')
3670 buf->data--;
3671
3672 }
3673 else if ((unsigned char)(c - '1') <= 8) {
3674 /* rfc-6587, Octet-Counting: message length in ASCII
3675 * (first digit can not be ZERO), followed by a space
3676 * and message length
3677 */
3678 char *p = NULL;
3679 int msglen;
3680
Willy Tarreau270a4572022-05-27 10:36:36 +02003681 to_skip = co_getword(sc_oc(sc), buf->area, buf->size, ' ');
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003682 if (!to_skip)
3683 goto missing_data;
3684 else if (to_skip < 0)
3685 goto cli_abort;
3686
3687 if (buf->area[to_skip - 1] != ' ')
3688 goto parse_error;
3689
Aurelien DARRAGON7faffdc2022-10-26 23:40:08 +02003690 msglen = strtol(buf->area, &p, 10);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003691 if (!msglen || p != &buf->area[to_skip - 1])
3692 goto parse_error;
3693
3694 /* message seems too large */
3695 if (msglen > buf->size)
3696 goto parse_error;
3697
Willy Tarreau270a4572022-05-27 10:36:36 +02003698 msglen = co_getblk(sc_oc(sc), buf->area, msglen, to_skip);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003699 if (!msglen)
3700 goto missing_data;
3701 else if (msglen < 0)
3702 goto cli_abort;
3703
3704
3705 buf->data = msglen;
3706 to_skip += msglen;
3707 }
3708 else
3709 goto parse_error;
3710
Willy Tarreau270a4572022-05-27 10:36:36 +02003711 co_skip(sc_oc(sc), to_skip);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003712
3713 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003714 _HA_ATOMIC_INC(&cum_log_messages);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01003715 proxy_inc_fe_req_ctr(l, frontend, 0);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003716
3717 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3718
3719 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3720
3721 }
3722
3723missing_data:
3724 /* we need more data to read */
Christopher Faulet3aeb3662023-03-16 14:27:29 +01003725 applet_need_more_data(appctx);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003726 return;
3727
3728missing_budget:
3729 /* it may remain some stuff to do, let's retry later */
3730 appctx_wakeup(appctx);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003731 return;
3732
3733parse_error:
3734 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003735 _HA_ATOMIC_INC(&l->counters->failed_req);
3736 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003737
Christopher Faulet22a88f02023-03-31 11:17:13 +02003738 goto error;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003739
3740cli_abort:
3741 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003742 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3743 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003744
Christopher Faulet22a88f02023-03-31 11:17:13 +02003745error:
3746 se_fl_set(appctx->sedesc, SE_FL_ERROR);
3747
3748out:
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003749 return;
3750}
3751
3752static struct applet syslog_applet = {
3753 .obj_type = OBJ_TYPE_APPLET,
3754 .name = "<SYSLOG>", /* used for logging */
3755 .fct = syslog_io_handler,
3756 .release = NULL,
3757};
3758
3759/*
Emeric Brun12941c82020-07-07 14:19:42 +02003760 * Parse "log-forward" section and create corresponding sink buffer.
3761 *
3762 * The function returns 0 in success case, otherwise, it returns error
3763 * flags.
3764 */
3765int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3766{
Aurelien DARRAGON2a9dfb82023-07-03 18:33:18 +02003767 int err_code = ERR_NONE;
Emeric Brun12941c82020-07-07 14:19:42 +02003768 struct proxy *px;
3769 char *errmsg = NULL;
3770 const char *err = NULL;
3771
3772 if (strcmp(args[0], "log-forward") == 0) {
3773 if (!*args[1]) {
Aurelien DARRAGON53f080d2023-07-03 17:35:54 +02003774 ha_alert("parsing [%s:%d] : missing name for log-forward section.\n", file, linenum);
Emeric Brun12941c82020-07-07 14:19:42 +02003775 err_code |= ERR_ALERT | ERR_ABORT;
3776 goto out;
3777 }
3778
3779 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3780 goto out;
3781
3782 err = invalid_char(args[1]);
3783 if (err) {
3784 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3785 file, linenum, *err, args[0], args[1]);
3786 err_code |= ERR_ALERT | ERR_ABORT;
3787 goto out;
3788 }
3789
Emeric Brunb0c331f2020-10-07 17:05:59 +02003790 px = log_forward_by_name(args[1]);
3791 if (px) {
3792 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3793 file, linenum, args[1], px->conf.file, px->conf.line);
3794 err_code |= ERR_ALERT | ERR_FATAL;
Aurelien DARRAGON2a9dfb82023-07-03 18:33:18 +02003795 goto out;
Emeric Brunb0c331f2020-10-07 17:05:59 +02003796 }
3797
3798 px = proxy_find_by_name(args[1], 0, 0);
3799 if (px) {
3800 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3801 file, linenum, args[1], proxy_type_str(px),
3802 px->id, px->conf.file, px->conf.line);
3803 err_code |= ERR_ALERT | ERR_FATAL;
Aurelien DARRAGON2a9dfb82023-07-03 18:33:18 +02003804 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003805 }
3806
3807 px = calloc(1, sizeof *px);
3808 if (!px) {
3809 err_code |= ERR_ALERT | ERR_FATAL;
3810 goto out;
3811 }
3812
Emeric Brun2ad2b1c2021-12-01 12:08:42 +01003813 init_new_proxy(px);
Emeric Brun12941c82020-07-07 14:19:42 +02003814 px->next = cfg_log_forward;
3815 cfg_log_forward = px;
Emeric Brun12941c82020-07-07 14:19:42 +02003816 px->conf.file = strdup(file);
3817 px->conf.line = linenum;
3818 px->mode = PR_MODE_SYSLOG;
Willy Tarreau69530f52023-04-28 09:16:15 +02003819 px->last_change = ns_to_sec(now_ns);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003820 px->cap = PR_CAP_FE;
3821 px->maxconn = 10;
3822 px->timeout.client = TICK_ETERNITY;
3823 px->accept = frontend_accept;
3824 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003825 px->id = strdup(args[1]);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003826 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003827 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003828 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3829 err_code |= ERR_WARN;
3830
3831 if (*(args[1]) == 0) {
3832 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3833 err_code |= ERR_ALERT | ERR_FATAL;
3834 goto out;
3835 }
3836 cfg_log_forward->maxconn = atol(args[1]);
3837 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3838 goto out;
3839 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003840 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003841 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3842 err_code |= ERR_WARN;
3843
3844 if (*(args[1]) == 0) {
3845 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3846 err_code |= ERR_ALERT | ERR_FATAL;
3847 goto out;
3848 }
3849 cfg_log_forward->backlog = atol(args[1]);
3850 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3851 goto out;
3852 }
3853 else if (strcmp(args[0], "bind") == 0) {
3854 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003855 struct bind_conf *bind_conf;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003856 struct listener *l;
Emeric Bruna8942cd2022-08-19 15:16:26 +02003857 int ret;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003858
3859 cur_arg = 1;
3860
3861 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3862 NULL, xprt_get(XPRT_RAW));
3863 if (!bind_conf) {
3864 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3865 err_code |= ERR_ALERT | ERR_FATAL;
3866 goto out;
3867 }
3868
Willy Tarreau882f2482023-01-12 18:52:23 +01003869 bind_conf->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreau30836152023-01-12 19:10:17 +01003870 bind_conf->accept = session_accept_fd;
Willy Tarreau882f2482023-01-12 18:52:23 +01003871
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003872 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3873 if (errmsg && *errmsg) {
3874 indent_msg(&errmsg, 2);
3875 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3876 }
3877 else {
3878 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3879 file, linenum, args[0], args[1], args[2]);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003880 }
Aurelien DARRAGON2a9dfb82023-07-03 18:33:18 +02003881 err_code |= ERR_ALERT | ERR_FATAL;
3882 goto out;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003883 }
3884 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003885 global.maxsock++;
3886 }
3887 cur_arg++;
3888
Emeric Bruna8942cd2022-08-19 15:16:26 +02003889 ret = bind_parse_args_list(bind_conf, args, cur_arg, cursection, file, linenum);
3890 err_code |= ret;
3891 if (ret != 0) {
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003892 err_code |= ERR_ALERT | ERR_FATAL;
3893 goto out;
3894 }
Emeric Brun12941c82020-07-07 14:19:42 +02003895 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003896 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003897 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02003898 struct bind_conf *bind_conf;
3899 struct bind_kw *kw;
3900 struct listener *l;
3901
3902 cur_arg = 1;
3903
3904 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3905 NULL, xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +02003906 if (!bind_conf) {
3907 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3908 err_code |= ERR_ALERT | ERR_FATAL;
3909 goto out;
3910 }
Emeric Brun12941c82020-07-07 14:19:42 +02003911
Willy Tarreau882f2482023-01-12 18:52:23 +01003912 bind_conf->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
3913
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003914 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003915 if (errmsg && *errmsg) {
3916 indent_msg(&errmsg, 2);
3917 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3918 }
3919 else {
3920 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3921 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003922 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003923 err_code |= ERR_ALERT | ERR_FATAL;
3924 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003925 }
3926 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003927 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreaue140a692020-10-15 21:25:32 +02003928 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003929 global.maxsock++;
3930 }
3931 cur_arg++;
3932
3933 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3934 int ret;
3935
3936 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3937 err_code |= ret;
3938 if (ret) {
3939 if (errmsg && *errmsg) {
3940 indent_msg(&errmsg, 2);
3941 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3942 }
3943 else
3944 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3945 file, linenum, args[cur_arg]);
3946 if (ret & ERR_FATAL)
3947 goto out;
3948 }
3949 cur_arg += 1 + kw->skip;
3950 }
3951 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003952 const char *best = bind_find_best_kw(args[cur_arg]);
3953 if (best)
3954 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3955 file, linenum, args[cur_arg], cursection, best);
3956 else
3957 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3958 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02003959 err_code |= ERR_ALERT | ERR_FATAL;
3960 goto out;
3961 }
3962 }
3963 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02003964 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003965 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3966 err_code |= ERR_ALERT | ERR_FATAL;
3967 goto out;
3968 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003969 }
3970 else if (strcmp(args[0], "timeout") == 0) {
3971 const char *res;
3972 unsigned timeout;
3973
3974 if (strcmp(args[1], "client") != 0) {
3975 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
3976 err_code |= ERR_ALERT | ERR_FATAL;
3977 goto out;
3978 }
3979
3980 if (*args[2] == 0) {
3981 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
3982 err_code |= ERR_ALERT | ERR_FATAL;
3983 goto out;
3984 }
3985 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3986 if (res == PARSE_TIME_OVER) {
3987 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
3988 }
3989 else if (res == PARSE_TIME_UNDER) {
3990 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
3991 }
3992 else if (res) {
3993 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003994 }
3995
3996 if (res) {
3997 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3998 err_code |= ERR_ALERT | ERR_FATAL;
3999 goto out;
4000 }
4001 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02004002 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02004003 else {
4004 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
4005 err_code |= ERR_ALERT | ERR_ABORT;
4006 goto out;
4007 }
Emeric Brun12941c82020-07-07 14:19:42 +02004008out:
Aurelien DARRAGON791433e2023-07-04 15:36:45 +02004009 ha_free(&errmsg);
Emeric Brun12941c82020-07-07 14:19:42 +02004010 return err_code;
4011}
4012
Willy Tarreau0108d902018-11-25 19:14:37 +01004013
Emeric Brun12941c82020-07-07 14:19:42 +02004014/* config parsers for this section */
4015REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4016
Willy Tarreau082b6282019-05-22 14:42:12 +02004017REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4018REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004019
Willy Tarreaubaaee002006-06-26 02:48:02 +02004020/*
4021 * Local variables:
4022 * c-indent-level: 8
4023 * c-basic-offset: 8
4024 * End:
4025 */