blob: 711cdd2df417225cc6915865c112fdd0058b20e8 [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;
Aurelien DARRAGONe628ce02024-05-14 10:22:19 +0200752 cpy->lb.smp_rgs = NULL;
Aurelien DARRAGON77679312023-07-05 15:52:19 +0200753 LIST_INIT(&cpy->list);
754 HA_SPIN_INIT(&cpy->lock);
755
756 /* special members */
757 if (def->ring_name) {
758 cpy->ring_name = strdup(def->ring_name);
759 if (!cpy->ring_name)
760 goto error;
761 }
762 if (def->conf.file) {
763 cpy->conf.file = strdup(def->conf.file);
764 if (!cpy->conf.file)
765 goto error;
766 }
Aurelien DARRAGONe628ce02024-05-14 10:22:19 +0200767 if (def->lb.smp_rgs) {
768 cpy->lb.smp_rgs = malloc(sizeof(*cpy->lb.smp_rgs) * def->lb.smp_rgs_sz);
769 if (!cpy->lb.smp_rgs)
770 goto error;
771 memcpy(cpy->lb.smp_rgs, def->lb.smp_rgs,
772 sizeof(*cpy->lb.smp_rgs) * def->lb.smp_rgs_sz);
773 }
Aurelien DARRAGON090f9ec2023-11-13 10:19:12 +0100774
775 /* inherit from original reference if set */
776 cpy->ref = (def->ref) ? def->ref : def;
777
Aurelien DARRAGON77679312023-07-05 15:52:19 +0200778 return cpy;
779
780 error:
781 free_logsrv(cpy);
782 return NULL;
783}
784
Willy Tarreau211ea252022-03-17 19:47:33 +0100785/* frees log server <logsrv> after freeing all of its allocated fields. The
786 * server must not belong to a list anymore. Logsrv may be NULL, which is
787 * silently ignored.
788 */
789void free_logsrv(struct logsrv *logsrv)
790{
791 if (!logsrv)
792 return;
793
794 BUG_ON(LIST_INLIST(&logsrv->list));
795 ha_free(&logsrv->conf.file);
796 ha_free(&logsrv->ring_name);
Aurelien DARRAGONe628ce02024-05-14 10:22:19 +0200797 free(logsrv->lb.smp_rgs);
Willy Tarreau211ea252022-03-17 19:47:33 +0100798 free(logsrv);
799}
800
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200801/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200802 * Parse "log" keyword and update <logsrvs> list accordingly.
803 *
804 * When <do_del> is set, it means the "no log" line was parsed, so all log
805 * servers in <logsrvs> are released.
806 *
807 * Otherwise, we try to parse the "log" line. First of all, when the list is not
808 * the global one, we look for the parameter "global". If we find it,
809 * global.logsrvs is copied. Else we parse each arguments.
810 *
811 * The function returns 1 in success case, otherwise, it returns 0 and err is
812 * filled.
813 */
Emeric Brun9533a702021-04-02 10:13:43 +0200814int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200815{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100816 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200817 struct sockaddr_storage *sk;
Emeric Brun94aab062021-04-02 10:41:36 +0200818 struct protocol *proto;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200819 struct logsrv *logsrv = NULL;
820 int port1, port2;
821 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200822 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200823
824 /*
825 * "no log": delete previous herited or defined syslog
826 * servers.
827 */
828 if (do_del) {
829 struct logsrv *back;
830
831 if (*(args[1]) != 0) {
832 memprintf(err, "'no log' does not expect arguments");
833 goto error;
834 }
835
836 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
Willy Tarreau211ea252022-03-17 19:47:33 +0100837 LIST_DEL_INIT(&logsrv->list);
838 free_logsrv(logsrv);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200839 }
840 return 1;
841 }
842
843 /*
844 * "log global": copy global.logrsvs linked list to the end of logsrvs
845 * list. But first, we check (logsrvs != global.logsrvs).
846 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100847 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200848 if (logsrvs == &global.logsrvs) {
849 memprintf(err, "'global' is not supported for a global syslog server");
850 goto error;
851 }
852 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200853 struct logsrv *node;
854
855 list_for_each_entry(node, logsrvs, list) {
856 if (node->ref == logsrv)
857 goto skip_logsrv;
858 }
859
Aurelien DARRAGON77679312023-07-05 15:52:19 +0200860 /* duplicate logsrv from global */
861 node = dup_logsrv(logsrv);
Aurelien DARRAGON256d5812023-05-11 18:57:23 +0200862 if (!node) {
863 memprintf(err, "out of memory error");
864 goto error;
865 }
Aurelien DARRAGON77679312023-07-05 15:52:19 +0200866
867 /* manually override some values */
868 ha_free(&node->conf.file);
Emeric Brun9533a702021-04-02 10:13:43 +0200869 node->conf.file = strdup(file);
870 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200871
Aurelien DARRAGON77679312023-07-05 15:52:19 +0200872 /* add to list */
873 LIST_APPEND(logsrvs, &node->list);
874
Christopher Faulet28ac0992018-03-26 16:09:19 +0200875 skip_logsrv:
876 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200877 }
878 return 1;
879 }
880
881 /*
882 * "log <address> ...: parse a syslog server line
883 */
884 if (*(args[1]) == 0 || *(args[2]) == 0) {
885 memprintf(err, "expects <address> and <facility> %s as arguments",
886 ((logsrvs == &global.logsrvs) ? "" : "or global"));
887 goto error;
888 }
889
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100890 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
891 if (strcmp(args[1], "stdout") == 0)
892 args[1] = "fd@1";
893 else if (strcmp(args[1], "stderr") == 0)
894 args[1] = "fd@2";
895
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200896 logsrv = calloc(1, sizeof(*logsrv));
897 if (!logsrv) {
898 memprintf(err, "out of memory");
899 goto error;
900 }
Christopher Fauletb4f96ed2022-03-29 14:17:09 +0200901 LIST_INIT(&logsrv->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200902 logsrv->conf.file = strdup(file);
903 logsrv->conf.line = linenum;
904
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200905 /* skip address for now, it will be parsed at the end */
906 cur_arg = 2;
907
908 /* just after the address, a length may be specified */
909 logsrv->maxlen = MAX_SYSLOG_LEN;
910 if (strcmp(args[cur_arg], "len") == 0) {
911 int len = atoi(args[cur_arg+1]);
912 if (len < 80 || len > 65535) {
913 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
914 args[cur_arg+1]);
915 goto error;
916 }
917 logsrv->maxlen = len;
918 cur_arg += 2;
919 }
920 if (logsrv->maxlen > global.max_syslog_len)
921 global.max_syslog_len = logsrv->maxlen;
922
923 /* after the length, a format may be specified */
924 if (strcmp(args[cur_arg], "format") == 0) {
925 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200926 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200927 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
928 goto error;
929 }
930 cur_arg += 2;
931 }
932
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200933 if (strcmp(args[cur_arg], "sample") == 0) {
934 unsigned low, high;
935 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200936 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
937
938 p = args[cur_arg+1];
939 smp_sz_str = strchr(p, ':');
940 if (!smp_sz_str) {
941 memprintf(err, "Missing sample size");
942 goto error;
943 }
944
945 *smp_sz_str++ = '\0';
946
947 end = p + strlen(p);
948
949 while (p != end) {
950 if (!get_logsrv_smp_range(&low, &high, &p, err))
951 goto error;
952
953 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
954 goto error;
955
956 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
957 if (!smp_rgs) {
958 memprintf(err, "out of memory error");
959 goto error;
960 }
961
962 smp_rgs[smp_rgs_sz].low = low;
963 smp_rgs[smp_rgs_sz].high = high;
964 smp_rgs[smp_rgs_sz].sz = high - low + 1;
965 smp_rgs[smp_rgs_sz].curr_idx = 0;
966 if (smp_rgs[smp_rgs_sz].high > smp_sz)
967 smp_sz = smp_rgs[smp_rgs_sz].high;
968 smp_rgs_sz++;
969 }
970
Tim Duesterhus21648002019-06-23 22:10:10 +0200971 if (smp_rgs == NULL) {
972 memprintf(err, "no sampling ranges given");
973 goto error;
974 }
975
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200976 beg = smp_sz_str;
977 end = beg + strlen(beg);
978 new_smp_sz = read_uint((const char **)&beg, end);
979 if (!new_smp_sz || beg != end) {
980 memprintf(err, "wrong sample size '%s' for sample range '%s'",
981 smp_sz_str, args[cur_arg+1]);
982 goto error;
983 }
984
985 if (new_smp_sz < smp_sz) {
986 memprintf(err, "sample size %zu should be greater or equal to "
987 "%zu the maximum of the high ranges limits",
988 new_smp_sz, smp_sz);
989 goto error;
990 }
991 smp_sz = new_smp_sz;
992
993 /* Let's order <smp_rgs> array. */
994 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
995
996 logsrv->lb.smp_rgs = smp_rgs;
997 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
998 logsrv->lb.smp_sz = smp_sz;
999
1000 cur_arg += 2;
1001 }
Frédéric Lécailled803e472019-04-25 07:42:09 +02001002 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001003 /* parse the facility */
1004 logsrv->facility = get_log_facility(args[cur_arg]);
1005 if (logsrv->facility < 0) {
1006 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
1007 goto error;
1008 }
1009 cur_arg++;
1010
1011 /* parse the max syslog level (default: debug) */
1012 logsrv->level = 7;
1013 if (*(args[cur_arg])) {
1014 logsrv->level = get_log_level(args[cur_arg]);
1015 if (logsrv->level < 0) {
1016 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
1017 goto error;
1018 }
1019 cur_arg++;
1020 }
1021
1022 /* parse the limit syslog level (default: emerg) */
1023 logsrv->minlvl = 0;
1024 if (*(args[cur_arg])) {
1025 logsrv->minlvl = get_log_level(args[cur_arg]);
1026 if (logsrv->minlvl < 0) {
1027 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1028 goto error;
1029 }
1030 cur_arg++;
1031 }
1032
1033 /* Too many args */
1034 if (*(args[cur_arg])) {
1035 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1036 goto error;
1037 }
1038
1039 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001040 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001041 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001042 logsrv->addr.ss_family = AF_UNSPEC;
1043 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001044 logsrv->sink = NULL;
1045 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001046 goto done;
1047 }
1048
Emeric Brun94aab062021-04-02 10:41:36 +02001049 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
1050 err, NULL, NULL,
1051 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 +02001052 if (!sk)
1053 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001054
1055 if (fd != -1)
1056 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001057 logsrv->addr = *sk;
1058
1059 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001060 if (!port1)
1061 set_host_port(&logsrv->addr, SYSLOG_PORT);
1062 }
Emeric Brun9533a702021-04-02 10:13:43 +02001063
Willy Tarreau91b47262022-05-20 16:36:46 +02001064 if (proto && proto->xprt_type == PROTO_TYPE_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +02001065 static unsigned long ring_ids;
1066
1067 /* Implicit sink buffer will be
1068 * initialized in post_check
1069 */
1070 logsrv->type = LOG_TARGET_BUFFER;
1071 logsrv->sink = NULL;
1072 /* compute uniq name for the ring */
1073 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
1074 }
1075
Willy Tarreauc046d162019-08-30 15:24:59 +02001076 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001077 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001078 return 1;
1079
1080 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001081 free(smp_rgs);
Willy Tarreau211ea252022-03-17 19:47:33 +01001082 free_logsrv(logsrv);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001083 return 0;
1084}
1085
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001086
1087/*
Emeric Brun54648852020-07-06 15:54:06 +02001088 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001089 */
Emeric Brun54648852020-07-06 15:54:06 +02001090enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001091{
Emeric Brun54648852020-07-06 15:54:06 +02001092 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001093
1094 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001095 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001096 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001097 format--;
1098
Emeric Brun54648852020-07-06 15:54:06 +02001099 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001100 return format;
1101}
1102
1103/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104 * returns log level for <lev> or -1 if not found.
1105 */
1106int get_log_level(const char *lev)
1107{
1108 int level;
1109
1110 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001111 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001112 level--;
1113
1114 return level;
1115}
1116
Willy Tarreaubaaee002006-06-26 02:48:02 +02001117/*
1118 * returns log facility for <fac> or -1 if not found.
1119 */
1120int get_log_facility(const char *fac)
1121{
1122 int facility;
1123
1124 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001125 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001126 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001127
Willy Tarreaubaaee002006-06-26 02:48:02 +02001128 return facility;
1129}
1130
William Lallemanda1cc3812012-02-08 16:38:44 +01001131/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001132 * Encode the string.
1133 *
1134 * When using the +E log format option, it will try to escape '"\]'
1135 * characters with '\' as prefix. The same prefix should not be used as
1136 * <escape>.
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001137 *
1138 * Return the address of the \0 character, or NULL on error
Dragan Dosen835b9212016-02-12 13:23:03 +01001139 */
1140static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001141 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001142 const char *string,
1143 struct logformat_node *node)
1144{
1145 if (node->options & LOG_OPT_ESC) {
1146 if (start < stop) {
1147 stop--; /* reserve one byte for the final '\0' */
1148 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001149 if (!ha_bit_test((unsigned char)(*string), map)) {
1150 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001151 *start++ = *string;
1152 else {
1153 if (start + 2 >= stop)
1154 break;
1155 *start++ = '\\';
1156 *start++ = *string;
1157 }
1158 }
1159 else {
1160 if (start + 3 >= stop)
1161 break;
1162 *start++ = escape;
1163 *start++ = hextab[(*string >> 4) & 15];
1164 *start++ = hextab[*string & 15];
1165 }
1166 string++;
1167 }
1168 *start = '\0';
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001169 return start;
Dragan Dosen835b9212016-02-12 13:23:03 +01001170 }
1171 }
1172 else {
1173 return encode_string(start, stop, escape, map, string);
1174 }
1175
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001176 return NULL;
Dragan Dosen835b9212016-02-12 13:23:03 +01001177}
1178
1179/*
1180 * Encode the chunk.
1181 *
1182 * When using the +E log format option, it will try to escape '"\]'
1183 * characters with '\' as prefix. The same prefix should not be used as
1184 * <escape>.
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001185 *
1186 * Return the address of the \0 character, or NULL on error
Dragan Dosen835b9212016-02-12 13:23:03 +01001187 */
1188static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001189 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001190 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001191 struct logformat_node *node)
1192{
1193 char *str, *end;
1194
1195 if (node->options & LOG_OPT_ESC) {
1196 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001197 str = chunk->area;
1198 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001199
1200 stop--; /* reserve one byte for the final '\0' */
1201 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001202 if (!ha_bit_test((unsigned char)(*str), map)) {
1203 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001204 *start++ = *str;
1205 else {
1206 if (start + 2 >= stop)
1207 break;
1208 *start++ = '\\';
1209 *start++ = *str;
1210 }
1211 }
1212 else {
1213 if (start + 3 >= stop)
1214 break;
1215 *start++ = escape;
1216 *start++ = hextab[(*str >> 4) & 15];
1217 *start++ = hextab[*str & 15];
1218 }
1219 str++;
1220 }
1221 *start = '\0';
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001222 return start;
Dragan Dosen835b9212016-02-12 13:23:03 +01001223 }
1224 }
1225 else {
1226 return encode_chunk(start, stop, escape, map, chunk);
1227 }
1228
Aurelien DARRAGONebcab992024-04-09 11:44:54 +02001229 return NULL;
Dragan Dosen835b9212016-02-12 13:23:03 +01001230}
1231
1232/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001233 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001234 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001235 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001236 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001237 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001238char *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 +01001239{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001240 if (size < 2)
1241 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001242
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001243 if (node->options & LOG_OPT_QUOTE) {
1244 *(dst++) = '"';
1245 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001246 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001247
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001248 if (src && len) {
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001249 /* escape_string and strlcpy2 will both try to add terminating NULL-byte
Aurelien DARRAGON1acf8c02024-04-09 11:15:23 +02001250 * to dst
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001251 */
Dragan Dosen835b9212016-02-12 13:23:03 +01001252 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001253 char *ret;
1254
Aurelien DARRAGON1acf8c02024-04-09 11:15:23 +02001255 ret = escape_string(dst, dst + size, '\\', rfc5424_escape_map, src, src + len);
Dragan Dosen835b9212016-02-12 13:23:03 +01001256 if (ret == NULL || *ret != '\0')
1257 return NULL;
1258 len = ret - dst;
1259 }
1260 else {
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001261 if (++len > size)
1262 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001263 len = strlcpy2(dst, src, len);
1264 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001265
1266 size -= len;
1267 dst += len;
1268 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001269 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1270 if (size < 2)
1271 return NULL;
1272 *(dst++) = '-';
Aurelien DARRAGONc5bff8e2022-09-20 14:33:18 +02001273 size -= 1;
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001274 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001275
1276 if (node->options & LOG_OPT_QUOTE) {
1277 if (size < 2)
1278 return NULL;
1279 *(dst++) = '"';
1280 }
1281
1282 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001283 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001284}
1285
Willy Tarreau26ffa852018-09-05 15:23:10 +02001286static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001287{
1288 return lf_text_len(dst, src, size, size, node);
1289}
1290
William Lallemand5f232402012-04-05 18:02:55 +02001291/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001292 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001293 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001294 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001295char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001296{
1297 char *ret = dst;
1298 int iret;
1299 char pn[INET6_ADDRSTRLEN];
1300
1301 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001302 unsigned char *addr = NULL;
1303 switch (sockaddr->sa_family) {
1304 case AF_INET:
1305 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1306 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1307 break;
1308 case AF_INET6:
1309 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1310 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1311 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1312 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1313 break;
1314 default:
1315 return NULL;
1316 }
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02001317 if (iret < 0 || iret >= size)
William Lallemand5f232402012-04-05 18:02:55 +02001318 return NULL;
1319 ret += iret;
1320 } else {
1321 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1322 ret = lf_text(dst, pn, size, node);
1323 if (ret == NULL)
1324 return NULL;
1325 }
1326 return ret;
1327}
1328
1329/*
1330 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001331 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001332 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001333char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001334{
1335 char *ret = dst;
1336 int iret;
1337
1338 if (node->options & LOG_OPT_HEXA) {
1339 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1340 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02001341 if (iret < 0 || iret >= size)
William Lallemand5f232402012-04-05 18:02:55 +02001342 return NULL;
1343 ret += iret;
1344 } else {
1345 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1346 if (ret == NULL)
1347 return NULL;
1348 }
1349 return ret;
1350}
1351
Emeric Brun54648852020-07-06 15:54:06 +02001352
1353/*
1354 * This function sends the syslog message using a printf format string. It
1355 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001356 */
Emeric Brun54648852020-07-06 15:54:06 +02001357void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001358{
Emeric Brun54648852020-07-06 15:54:06 +02001359 va_list argp;
1360 int data_len;
1361
1362 if (level < 0 || format == NULL || logline == NULL)
1363 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001364
Emeric Brun54648852020-07-06 15:54:06 +02001365 va_start(argp, format);
1366 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1367 if (data_len < 0 || data_len > global.max_syslog_len)
1368 data_len = global.max_syslog_len;
1369 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001370
Emeric Brun54648852020-07-06 15:54:06 +02001371 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1372 logline, data_len, default_rfc5424_sd_log_format, 2);
1373}
1374/*
1375 * This function builds a log header of given format using given
1376 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1377 * to determine format based on given metadas. It is useful
1378 * for log-forwarding to be able to forward any format without
1379 * settings.
1380 * This function returns a struct ist array of elements of the header
1381 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001382 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001383 * elements.
1384 */
1385struct ist *build_log_header(enum log_fmt format, int level, int facility,
1386 struct ist *metadata, size_t *nbelem)
1387{
1388 static THREAD_LOCAL struct {
1389 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1390 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1391 time_t cur_legacy_time;
1392 char priority_buffer[6];
1393 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001394
Emeric Brun54648852020-07-06 15:54:06 +02001395 struct tm logtime;
1396 int len;
1397 int fac_level = 0;
1398 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001399
Emeric Brun54648852020-07-06 15:54:06 +02001400 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001401
Emeric Brun54648852020-07-06 15:54:06 +02001402
1403 if (format == LOG_FORMAT_UNSPEC) {
1404 format = LOG_FORMAT_RAW;
1405 if (metadata) {
1406 /* If a hostname is set, it appears we want to perform syslog
1407 * because only rfc5427 or rfc3164 support an hostname.
1408 */
1409 if (metadata[LOG_META_HOST].len) {
1410 /* If a rfc5424 compliant timestamp is used we consider
1411 * that output format is rfc5424, else legacy format
1412 * is used as specified default for local logs
1413 * in documentation.
1414 */
1415 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1416 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1417 format = LOG_FORMAT_RFC5424;
1418 else
1419 format = LOG_FORMAT_RFC3164;
1420 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001421 else if (metadata[LOG_META_TAG].len) {
1422 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001423 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001424 * in legacy format (analog to RFC3164 but
1425 * with stripped hostname).
1426 */
1427 format = LOG_FORMAT_LOCAL;
1428 }
Emeric Brun54648852020-07-06 15:54:06 +02001429 else if (metadata[LOG_META_PRIO].len) {
1430 /* the source seems a parsed message
1431 * offering a valid level/prio prefix
1432 * so we consider this format.
1433 */
1434 format = LOG_FORMAT_PRIO;
1435 }
1436 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001437 }
1438
Emeric Brun54648852020-07-06 15:54:06 +02001439 /* prepare priority, stored into 1 single elem */
1440 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001441 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001442 case LOG_FORMAT_RFC3164:
1443 case LOG_FORMAT_RFC5424:
1444 case LOG_FORMAT_PRIO:
1445 fac_level = facility << 3;
1446 /* further format ignore the facility */
Willy Tarreau80f9a632022-11-14 07:22:57 +01001447 __fallthrough;
Emeric Brun54648852020-07-06 15:54:06 +02001448 case LOG_FORMAT_TIMED:
1449 case LOG_FORMAT_SHORT:
1450 fac_level += level;
1451 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1452 do {
1453 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1454 fac_level /= 10;
1455 hdr_ctx.ist_vector[*nbelem].ptr--;
1456 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1457 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1458 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1459 break;
1460 case LOG_FORMAT_ISO:
1461 case LOG_FORMAT_RAW:
1462 break;
1463 case LOG_FORMAT_UNSPEC:
1464 case LOG_FORMATS:
1465 ABORT_NOW();
1466 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001467
William Lallemand2a4a44f2012-02-06 16:00:33 +01001468
Emeric Brun54648852020-07-06 15:54:06 +02001469 /* prepare timestamp, stored into a max of 4 elems */
1470 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001471 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001472 case LOG_FORMAT_RFC3164:
1473 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1474 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1475 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1476 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001477 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001478 break;
1479 }
1480 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1481 int month;
1482 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001483
Emeric Brun54648852020-07-06 15:54:06 +02001484 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001485
Emeric Brun54648852020-07-06 15:54:06 +02001486 /* compute month */
1487 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1488 if (month)
1489 month--;
1490 if (month <= 11) {
1491 /* builds log prefix ex: 'Jan 1 ' */
1492 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1493 "%s %c%c ", monthname[month],
1494 timestamp[8] != '0' ? timestamp[8] : ' ',
1495 timestamp[9]);
1496 /* we reused the timestamp_buffer, signal that it does not
1497 * contain local time anymore
1498 */
1499 hdr_ctx.cur_legacy_time = 0;
1500 if (len == 7) {
1501 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1502 /* adds 'HH:MM:SS' from iso time */
1503 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1504 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1505 /* we successfully reuse iso time, we can break */
1506 break;
1507 }
1508 }
1509 /* Failed to reuse isotime time, fallback to local legacy time */
1510 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001511
Emeric Brun54648852020-07-06 15:54:06 +02001512 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1513 /* re-builds timestamp from the current local time */
1514 get_localtime(time, &logtime);
1515
1516 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1517 "%s %2d %02d:%02d:%02d ",
1518 monthname[logtime.tm_mon],
1519 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1520 if (len != LOG_LEGACYTIME_LEN+1)
1521 hdr_ctx.cur_legacy_time = 0;
1522 else
1523 hdr_ctx.cur_legacy_time = time;
1524 }
1525 if (likely(hdr_ctx.cur_legacy_time))
1526 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1527 else
1528 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1529 break;
1530 case LOG_FORMAT_RFC5424:
1531 /* adds rfc5425 version prefix */
1532 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1533 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001534 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001535 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1536 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1537 break;
1538 }
1539 /* let continue as 'timed' and 'iso' format for usual timestamp */
Willy Tarreau80f9a632022-11-14 07:22:57 +01001540 __fallthrough;
Emeric Brun54648852020-07-06 15:54:06 +02001541 case LOG_FORMAT_TIMED:
1542 case LOG_FORMAT_ISO:
1543 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1544 * '1900:01:01T14:00:00+02:00'
1545 * '1900:01:01T10:00:00.123456-02:00'
1546 */
1547 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1548 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1549 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001550 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001551 break;
1552 }
1553 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1554 int month;
1555 char *timestamp = metadata[LOG_META_TIME].ptr;
1556
1557 for (month = 0; month < 12; month++)
1558 if (!memcmp(monthname[month], timestamp, 3))
1559 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001560
Emeric Brun54648852020-07-06 15:54:06 +02001561 if (month < 12) {
1562
1563 /* get local time to retrieve year */
1564 get_localtime(time, &logtime);
1565
1566 /* year seems changed since log */
1567 if (logtime.tm_mon < month)
1568 logtime.tm_year--;
1569
1570 /* builds rfc5424 prefix ex: '1900-01-01T' */
1571 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1572 "%4d-%02d-%c%cT",
1573 logtime.tm_year+1900, month+1,
1574 timestamp[4] != ' ' ? timestamp[4] : '0',
1575 timestamp[5]);
1576
1577 /* we reused the timestamp_buffer, signal that it does not
1578 * contain local time anymore
1579 */
1580 hdr_ctx.cur_legacy_time = 0;
1581 if (len == 11) {
1582 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1583 /* adds HH:MM:SS from legacy timestamp */
1584 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001585 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001586 /* according to rfc: -00:00 means we don't know the timezone */
1587 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1588 /* we successfully reuse legacy time, we can break */
1589 break;
1590 }
1591 }
1592 /* Failed to reuse legacy time, fallback to local iso time */
1593 }
1594 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1595 break;
1596 case LOG_FORMAT_PRIO:
1597 case LOG_FORMAT_SHORT:
1598 case LOG_FORMAT_RAW:
1599 break;
1600 case LOG_FORMAT_UNSPEC:
1601 case LOG_FORMATS:
1602 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001603 }
1604
Emeric Brun54648852020-07-06 15:54:06 +02001605 /* prepare other meta data, stored into a max of 10 elems */
1606 switch (format) {
1607 case LOG_FORMAT_RFC3164:
1608 if (metadata && metadata[LOG_META_HOST].len) {
1609 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1610 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1611 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001612 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001613 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Willy Tarreau80f9a632022-11-14 07:22:57 +01001614 __fallthrough;
Emeric Brun0237c4e2020-11-27 16:24:34 +01001615 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001616 if (!metadata || !metadata[LOG_META_TAG].len)
1617 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001618
Emeric Brun54648852020-07-06 15:54:06 +02001619 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1620 if (metadata[LOG_META_PID].len) {
1621 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1622 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1623 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1624 }
1625 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1626 break;
1627 case LOG_FORMAT_RFC5424:
1628 if (metadata && metadata[LOG_META_HOST].len) {
1629 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1630 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1631 }
1632 else
1633 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001634
Emeric Brun54648852020-07-06 15:54:06 +02001635 if (metadata && metadata[LOG_META_TAG].len) {
1636 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1637 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1638 }
1639 else
1640 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001641
Emeric Brun54648852020-07-06 15:54:06 +02001642 if (metadata && metadata[LOG_META_PID].len) {
1643 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1644 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1645 }
1646 else
1647 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001648
Emeric Brun54648852020-07-06 15:54:06 +02001649 if (metadata && metadata[LOG_META_MSGID].len) {
1650 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1651 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1652 }
1653 else
1654 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001655
Emeric Brun54648852020-07-06 15:54:06 +02001656 if (metadata && metadata[LOG_META_STDATA].len) {
1657 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1658 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1659 }
1660 else
1661 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1662 break;
1663 case LOG_FORMAT_PRIO:
1664 case LOG_FORMAT_SHORT:
1665 case LOG_FORMAT_TIMED:
1666 case LOG_FORMAT_ISO:
1667 case LOG_FORMAT_RAW:
1668 break;
1669 case LOG_FORMAT_UNSPEC:
1670 case LOG_FORMATS:
1671 ABORT_NOW();
1672 }
1673
1674 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001675}
1676
1677/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001678 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001679 * The argument <metadata> MUST be an array of size
1680 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001681 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001682 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001683 */
Emeric Brun54648852020-07-06 15:54:06 +02001684static 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 +01001685{
Emeric Brun54648852020-07-06 15:54:06 +02001686 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001687 static THREAD_LOCAL struct msghdr msghdr = {
1688 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001689 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001690 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001691 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1692 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001693 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001694 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001695 size_t nbelem;
1696 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001697
1698 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001699
Emeric Brunfa9d7802020-05-28 14:21:33 +02001700 /* historically some messages used to already contain the trailing LF
1701 * or Zero. Let's remove all trailing LF or Zero
1702 */
Emeric Brun54648852020-07-06 15:54:06 +02001703 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001704 size--;
1705
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001706 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001707 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001708 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001709 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001710 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1711 /* the socket's address is a file descriptor */
1712 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1713 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001714 else if (logsrv->addr.ss_family == AF_UNIX)
1715 plogfd = &logfdunix;
1716 else
1717 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001718
Willy Tarreauc046d162019-08-30 15:24:59 +02001719 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001720 /* socket not successfully initialized yet */
1721 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1722 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1723 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001724
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001725 if (!once) {
1726 once = 1; /* note: no need for atomic ops here */
1727 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1728 nblogger, strerror(errno), errno);
1729 }
1730 return;
1731 } else {
1732 /* we don't want to receive anything on this socket */
1733 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1734 /* does nothing under Linux, maybe needed for others */
1735 shutdown(*plogfd, SHUT_RD);
Willy Tarreau38247432022-04-26 10:24:14 +02001736 fd_set_cloexec(*plogfd);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001737 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001738 }
1739
Emeric Brun54648852020-07-06 15:54:06 +02001740 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1741 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001742 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001743 struct ist msg;
Aurelien DARRAGON7d4e87b2023-06-26 17:15:48 +02001744 size_t maxlen = logsrv->maxlen;
Emeric Brun54648852020-07-06 15:54:06 +02001745
1746 msg = ist2(message, size);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01001747 msg = isttrim(msg, logsrv->maxlen);
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001748
Aurelien DARRAGON7d4e87b2023-06-26 17:15:48 +02001749 /* make room for the final '\n' which may be forcefully inserted
1750 * by tcp forwarder applet (sink_forward_io_handler)
1751 */
1752 maxlen -= 1;
1753
1754 sent = sink_write(logsrv->sink, maxlen, &msg, 1, level, facility, metadata);
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001755 }
1756 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1757 struct ist msg;
1758
1759 msg = ist2(message, size);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01001760 msg = isttrim(msg, logsrv->maxlen);
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001761
1762 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001763 }
1764 else {
Emeric Brun54648852020-07-06 15:54:06 +02001765 int i = 0;
Aurelien DARRAGONa0211342023-06-27 11:32:06 +02001766 int totlen = logsrv->maxlen - 1; /* save space for the final '\n' */
Emeric Brun54648852020-07-06 15:54:06 +02001767
1768 for (i = 0 ; i < nbelem ; i++ ) {
1769 iovec[i].iov_base = msg_header[i].ptr;
1770 iovec[i].iov_len = msg_header[i].len;
1771 if (totlen <= iovec[i].iov_len) {
1772 iovec[i].iov_len = totlen;
1773 totlen = 0;
1774 break;
1775 }
1776 totlen -= iovec[i].iov_len;
1777 }
1778 if (totlen) {
1779 iovec[i].iov_base = message;
1780 iovec[i].iov_len = size;
1781 if (totlen <= iovec[i].iov_len)
1782 iovec[i].iov_len = totlen;
1783 i++;
1784 }
1785 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1786 iovec[i].iov_len = 1;
1787 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001788
Emeric Brun54648852020-07-06 15:54:06 +02001789 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001790 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1791 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001792
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001793 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1794 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001795
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001796 if (sent < 0) {
1797 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001798
Willy Tarreauacef5e22022-04-25 20:32:15 +02001799 if (errno == EAGAIN || errno == EWOULDBLOCK)
Willy Tarreau4781b152021-04-06 13:53:36 +02001800 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001801 else if (!once) {
1802 once = 1; /* note: no need for atomic ops here */
1803 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1804 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001805 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001806 }
1807}
Dragan Dosen59cee972015-09-19 22:09:02 +02001808
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001809/*
1810 * This function sends a syslog message.
1811 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001812 * The argument <metadata> MUST be an array of size
1813 * LOG_META_FIELDS*sizeof(struct ist) containing
1814 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001815 */
Emeric Brun54648852020-07-06 15:54:06 +02001816void process_send_log(struct list *logsrvs, int level, int facility,
1817 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001818{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001819 struct logsrv *logsrv;
1820 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001821
1822 /* Send log messages to syslog server. */
1823 nblogger = 0;
1824 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001825 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001826
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001827 /* we can filter the level of the messages that are sent to each logger */
1828 if (level > logsrv->level)
1829 continue;
1830
Frédéric Lécailled803e472019-04-25 07:42:09 +02001831 if (logsrv->lb.smp_rgs) {
1832 struct smp_log_range *curr_rg;
1833
1834 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1835 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1836 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1837 if (in_range) {
1838 /* Let's consume this range. */
1839 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1840 if (!curr_rg->curr_idx) {
1841 /* If consumed, let's select the next range. */
1842 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1843 }
1844 }
1845 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1846 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1847 }
1848 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001849 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1850 (facility == -1) ? logsrv->facility : facility,
1851 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001852 }
1853}
1854
Emeric Brun54648852020-07-06 15:54:06 +02001855/*
1856 * This function sends a syslog message.
1857 * It doesn't care about errors nor does it report them.
1858 * The arguments <sd> and <sd_size> are used for the structured-data part
1859 * in RFC5424 formatted syslog messages.
1860 */
1861void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1862 char *message, size_t size, char *sd, size_t sd_size)
1863{
1864 static THREAD_LOCAL pid_t curr_pid;
1865 static THREAD_LOCAL char pidstr[16];
1866 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1867
1868 if (logsrvs == NULL) {
1869 if (!LIST_ISEMPTY(&global.logsrvs)) {
1870 logsrvs = &global.logsrvs;
1871 }
1872 }
1873 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1874 return;
1875
1876 if (!metadata[LOG_META_HOST].len) {
1877 if (global.log_send_hostname)
Tim Duesterhus77508502022-03-15 13:11:06 +01001878 metadata[LOG_META_HOST] = ist(global.log_send_hostname);
Emeric Brun54648852020-07-06 15:54:06 +02001879 }
1880
1881 if (!tagb || !tagb->area)
1882 tagb = &global.log_tag;
1883
1884 if (tagb)
1885 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1886
1887 if (unlikely(curr_pid != getpid()))
1888 metadata[LOG_META_PID].len = 0;
1889
1890 if (!metadata[LOG_META_PID].len) {
1891 curr_pid = getpid();
1892 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1893 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1894 }
1895
1896 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1897
1898 /* Remove trailing space of structured data */
1899 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1900 metadata[LOG_META_STDATA].len--;
1901
1902 return process_send_log(logsrvs, level, -1, metadata, message, size);
1903}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001904
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001905const 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 +01001906const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1907 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1908 Set-cookie Updated, unknown, unknown */
1909
William Lallemand1d705562012-03-12 12:46:41 +01001910/*
1911 * try to write a character if there is enough space, or goto out
1912 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001913#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001914 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001915 *(tmplog++) = (x); \
1916 } else { \
1917 goto out; \
1918 } \
1919 } while(0)
1920
Dragan Dosen835b9212016-02-12 13:23:03 +01001921
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001922/* Initializes some log data at boot */
1923static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001924{
1925 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001926 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001927
1928 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1929 * inside PARAM-VALUE should be escaped with '\' as prefix.
1930 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1931 * details.
1932 */
1933 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1934
1935 tmp = "\"\\]";
1936 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001937 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001938 tmp++;
1939 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001940
1941 /* initialize the log header encoding map : '{|}"#' should be encoded with
1942 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1943 * URL encoding only requires '"', '#' to be encoded as well as non-
1944 * printable characters above.
1945 */
1946 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1947 memset(url_encode_map, 0, sizeof(url_encode_map));
1948 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001949 ha_bit_set(i, hdr_encode_map);
1950 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001951 }
1952 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001953 ha_bit_set(i, hdr_encode_map);
1954 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001955 }
1956
1957 tmp = "\"#{|}";
1958 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001959 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001960 tmp++;
1961 }
1962
1963 tmp = "\"#";
1964 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001965 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001966 tmp++;
1967 }
1968
1969 /* initialize the http header encoding map. The draft httpbis define the
1970 * header content as:
1971 *
1972 * HTTP-message = start-line
1973 * *( header-field CRLF )
1974 * CRLF
1975 * [ message-body ]
1976 * header-field = field-name ":" OWS field-value OWS
1977 * field-value = *( field-content / obs-fold )
1978 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1979 * obs-fold = CRLF 1*( SP / HTAB )
1980 * field-vchar = VCHAR / obs-text
1981 * VCHAR = %x21-7E
1982 * obs-text = %x80-FF
1983 *
1984 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1985 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08001986 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02001987 */
1988 memset(http_encode_map, 0, sizeof(http_encode_map));
1989 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001990 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001991 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001992 ha_bit_set(i, http_encode_map);
1993 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001994}
William Lallemand1d705562012-03-12 12:46:41 +01001995
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001996INITCALL0(STG_PREPARE, init_log);
1997
Christopher Faulet0132d062017-07-26 15:33:35 +02001998/* Initialize log buffers used for syslog messages */
1999int init_log_buffers()
2000{
Christopher Faulet0132d062017-07-26 15:33:35 +02002001 logline = my_realloc2(logline, global.max_syslog_len + 1);
2002 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02002003 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02002004 return 0;
2005 return 1;
2006}
2007
2008/* Deinitialize log buffers used for syslog messages */
2009void deinit_log_buffers()
2010{
Christopher Faulet0132d062017-07-26 15:33:35 +02002011 free(logline);
2012 free(logline_rfc5424);
Christopher Faulet0132d062017-07-26 15:33:35 +02002013 logline = NULL;
2014 logline_rfc5424 = NULL;
2015}
2016
Willy Tarreaudf974472012-12-28 02:44:01 +01002017/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2018 * <maxsize> characters. Returns the size of the output string in characters,
2019 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002020 * is not zero. It requires a valid session and optionally a stream. If the
2021 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002022 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002023int 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 +02002024{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002025 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002026 struct proxy *be;
2027 struct http_txn *txn;
2028 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002029 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002030 unsigned int s_flags;
2031 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002032 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002033 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002034 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002035 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002036 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002037 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002038 int t_request;
2039 int hdr;
2040 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002041 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002042 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002043 char *ret;
2044 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002045 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002046 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002047 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002048 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002049 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002050 struct http_uri_parser parser;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002051
William Lallemandbddd4fd2012-02-27 11:23:10 +01002052 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002053
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002054 if (likely(s)) {
2055 be = s->be;
2056 txn = s->txn;
Willy Tarreaufd9417b2022-05-18 16:23:22 +02002057 be_conn = sc_conn(s->scb);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002058 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002059 s_flags = s->flags;
2060 uniq_id = s->uniq_id;
2061 logs = &s->logs;
2062 } else {
2063 /* we have no stream so we first need to initialize a few
2064 * things that are needed later. We do increment the request
2065 * ID so that it's uniquely assigned to this request just as
2066 * if the request had reached the point of being processed.
2067 * A request error is reported as it's the only element we have
2068 * here and which justifies emitting such a log.
2069 */
Christopher Fauletfd818482021-04-14 14:01:41 +02002070 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002071 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002072 fe_conn = objt_conn(sess->origin);
Willy Tarreaubde14ad2022-05-27 10:04:04 +02002073 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? sc_conn(__objt_check(sess->origin)->sc) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002074 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002075 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02002076 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002077
2078 /* prepare a valid log structure */
Willy Tarreauad5a5f62023-04-27 09:46:02 +02002079 tmp_strm_log.accept_ts = sess->accept_ts;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002080 tmp_strm_log.accept_date = sess->accept_date;
2081 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02002082 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreauad5a5f62023-04-27 09:46:02 +02002083 tmp_strm_log.request_ts = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002084 tmp_strm_log.t_queue = -1;
2085 tmp_strm_log.t_connect = -1;
2086 tmp_strm_log.t_data = -1;
Willy Tarreau69530f52023-04-28 09:16:15 +02002087 tmp_strm_log.t_close = ns_to_ms(now_ns - sess->accept_ts);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002088 tmp_strm_log.bytes_in = 0;
2089 tmp_strm_log.bytes_out = 0;
2090 tmp_strm_log.prx_queue_pos = 0;
2091 tmp_strm_log.srv_queue_pos = 0;
2092
2093 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002094
2095 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002096 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, &status);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002097
2098 switch (es) {
2099 case MUX_ES_SUCCESS:
2100 break;
2101 case MUX_ES_INVALID_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002102 status = (status ? status : 400);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002103 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2104 s_flags = SF_ERR_CLICL | SF_FINST_R;
2105 else
2106 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2107 break;
2108 case MUX_ES_TOUT_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002109 status = (status ? status : 408);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002110 s_flags = SF_ERR_CLITO | SF_FINST_R;
2111 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002112 case MUX_ES_NOTIMPL_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002113 status = (status ? status : 501);
Christopher Faulet142dd332020-12-07 11:24:37 +01002114 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2115 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002116 case MUX_ES_INTERNAL_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002117 status = (status ? status : 500);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002118 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2119 break;
2120 default:
2121 break;
2122 }
2123 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002124 }
2125
William Lallemandbddd4fd2012-02-27 11:23:10 +01002126 t_request = -1;
Willy Tarreauad5a5f62023-04-27 09:46:02 +02002127 if ((llong)(logs->request_ts - logs->accept_ts) >= 0)
2128 t_request = ns_to_ms(logs->request_ts - logs->accept_ts);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002129
William Lallemand1d705562012-03-12 12:46:41 +01002130 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002131
William Lallemandbddd4fd2012-02-27 11:23:10 +01002132 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002133 if (LIST_ISEMPTY(list_format))
2134 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002135
William Lallemand1d705562012-03-12 12:46:41 +01002136 list_for_each_entry(tmp, list_format, list) {
Christopher Faulet52b28d22021-10-27 11:58:05 +02002137#ifdef USE_OPENSSL
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002138 struct connection *conn;
Christopher Faulet52b28d22021-10-27 11:58:05 +02002139#endif
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002140 const struct sockaddr_storage *addr;
Willy Tarreau4f653562012-10-12 19:48:16 +02002141 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002142 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002143 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002144
Willy Tarreauc8368452012-12-21 00:09:23 +01002145 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002146 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002147 if (!last_isspace) {
2148 LOGCHAR(' ');
2149 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002150 }
2151 break;
2152
William Lallemand1d705562012-03-12 12:46:41 +01002153 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002154 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002155 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002156 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002157 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002158 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002159 last_isspace = 0;
2160 break;
2161
Willy Tarreauc8368452012-12-21 00:09:23 +01002162 case LOG_FMT_EXPR: // sample expression, may be request or response
2163 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002164 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002165 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 +02002166
Christopher Faulet5f940702020-04-06 10:40:02 +02002167 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002168 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 +02002169
2170 if (!key && !(tmp->options & (LOG_OPT_REQ_CAP|LOG_OPT_RES_CAP))) // cfg, cli
2171 key = sample_fetch_as_type(be, sess, s, SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
2172
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002173 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002174 ret = lf_encode_chunk(tmplog, dst + maxsize,
2175 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002176 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002177 ret = lf_text_len(tmplog,
2178 key ? key->data.u.str.area : NULL,
2179 key ? key->data.u.str.data : 0,
2180 dst + maxsize - tmplog,
2181 tmp);
Aurelien DARRAGON9c5dd212024-04-09 15:28:00 +02002182 if (ret == NULL)
Willy Tarreauc8368452012-12-21 00:09:23 +01002183 goto out;
2184 tmplog = ret;
2185 last_isspace = 0;
2186 break;
2187
Willy Tarreau2beef582012-12-20 17:22:52 +01002188 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreaud68ff012022-05-27 08:57:21 +02002189 addr = (s ? sc_src(s->scf) : sess_src(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002190 if (addr)
2191 ret = lf_ip(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002192 else
2193 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002194
William Lallemand1d705562012-03-12 12:46:41 +01002195 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002196 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002197 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002198 last_isspace = 0;
2199 break;
2200
Willy Tarreau2beef582012-12-20 17:22:52 +01002201 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreaud68ff012022-05-27 08:57:21 +02002202 addr = (s ? sc_src(s->scf) : sess_src(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002203 if (addr) {
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002204 /* sess->listener is always defined when the session's owner is an inbound connections */
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002205 if (addr->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002206 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002207 else
2208 ret = lf_port(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002209 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002210 else
2211 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2212
William Lallemand5f232402012-04-05 18:02:55 +02002213 if (ret == NULL)
2214 goto out;
2215 tmplog = ret;
2216 last_isspace = 0;
2217 break;
2218
Willy Tarreau2beef582012-12-20 17:22:52 +01002219 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreaud68ff012022-05-27 08:57:21 +02002220 addr = (s ? sc_dst(s->scf) : sess_dst(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002221 if (addr)
2222 ret = lf_ip(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002223 else
2224 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2225
William Lallemand1d705562012-03-12 12:46:41 +01002226 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002227 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002228 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002229 last_isspace = 0;
2230 break;
2231
Willy Tarreau2beef582012-12-20 17:22:52 +01002232 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreaud68ff012022-05-27 08:57:21 +02002233 addr = (s ? sc_dst(s->scf) : sess_dst(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002234 if (addr) {
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002235 /* sess->listener is always defined when the session's owner is an inbound connections */
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002236 if (addr->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002237 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002238 else
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002239 ret = lf_port(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002240 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002241 else
2242 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2243
William Lallemand5f232402012-04-05 18:02:55 +02002244 if (ret == NULL)
2245 goto out;
2246 tmplog = ret;
2247 last_isspace = 0;
2248 break;
2249
Willy Tarreau2beef582012-12-20 17:22:52 +01002250 case LOG_FMT_BACKENDIP: // %bi
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_ip(tmplog, (const 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 Lallemand1d705562012-03-12 12:46:41 +01002256 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002257 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002258 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002259 last_isspace = 0;
2260 break;
2261
Willy Tarreau2beef582012-12-20 17:22:52 +01002262 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002263 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002264 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, 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_SERVERIP: // %si
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_ip(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 Lallemand5f232402012-04-05 18:02:55 +02002280 if (ret == NULL)
2281 goto out;
2282 tmplog = ret;
2283 last_isspace = 0;
2284 break;
2285
Willy Tarreau2beef582012-12-20 17:22:52 +01002286 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002287 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002288 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002289 else
2290 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2291
William Lallemand1d705562012-03-12 12:46:41 +01002292 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002293 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002294 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002295 last_isspace = 0;
2296 break;
2297
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002298 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002299 get_localtime(logs->accept_date.tv_sec, &tm);
2300 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002301 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002302 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002303 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002304 last_isspace = 0;
2305 break;
2306
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002307 case LOG_FMT_tr: // %tr = start of request date
2308 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002309 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 +02002310 get_localtime(tv.tv_sec, &tm);
2311 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2312 if (ret == NULL)
2313 goto out;
2314 tmplog = ret;
2315 last_isspace = 0;
2316 break;
2317
2318 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002319 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002320 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002321 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002322 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002323 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002324 last_isspace = 0;
2325 break;
2326
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002327 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002328 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 +02002329 get_gmtime(tv.tv_sec, &tm);
2330 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2331 if (ret == NULL)
2332 goto out;
2333 tmplog = ret;
2334 last_isspace = 0;
2335 break;
2336
2337 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002338 get_localtime(logs->accept_date.tv_sec, &tm);
2339 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002340 if (ret == NULL)
2341 goto out;
2342 tmplog = ret;
2343 last_isspace = 0;
2344 break;
2345
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002346 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002347 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 +02002348 get_localtime(tv.tv_sec, &tm);
2349 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2350 if (ret == NULL)
2351 goto out;
2352 tmplog = ret;
2353 last_isspace = 0;
2354 break;
2355
William Lallemand5f232402012-04-05 18:02:55 +02002356 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002357 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002358 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02002359 if (iret < 0 || iret >= dst + maxsize - tmplog)
William Lallemand5f232402012-04-05 18:02:55 +02002360 goto out;
2361 last_isspace = 0;
2362 tmplog += iret;
2363 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002364 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002365 if (ret == NULL)
2366 goto out;
2367 tmplog = ret;
2368 last_isspace = 0;
2369 }
2370 break;
2371
William Lallemand1d705562012-03-12 12:46:41 +01002372 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002373 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002374 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02002375 if (iret < 0 || iret >= dst + maxsize - tmplog)
William Lallemand5f232402012-04-05 18:02:55 +02002376 goto out;
2377 last_isspace = 0;
2378 tmplog += iret;
2379 } else {
2380 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002381 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002382 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002383 tmplog, 4);
2384 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002385 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002386 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002387 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002388 }
2389 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002390
William Lallemand1d705562012-03-12 12:46:41 +01002391 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002392 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002393 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002394 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002395 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002396 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002397 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002398 break;
2399
Willy Tarreau773d65f2012-10-12 14:56:11 +02002400 case LOG_FMT_FRONTEND_XPRT: // %ft
2401 src = fe->id;
2402 if (tmp->options & LOG_OPT_QUOTE)
2403 LOGCHAR('"');
2404 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2405 if (iret == 0)
2406 goto out;
2407 tmplog += iret;
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002408
2409 /* sess->listener may be undefined if the session's owner is a health-check */
Willy Tarreau807a3a52022-04-12 08:08:33 +02002410 if (sess->listener && sess->listener->bind_conf->xprt->get_ssl_sock_ctx)
Willy Tarreau773d65f2012-10-12 14:56:11 +02002411 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002412 if (tmp->options & LOG_OPT_QUOTE)
2413 LOGCHAR('"');
2414 last_isspace = 0;
2415 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002416#ifdef USE_OPENSSL
2417 case LOG_FMT_SSL_CIPHER: // %sslc
2418 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002419 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002420 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002421 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002422 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002423 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2424 if (ret == NULL)
2425 goto out;
2426 tmplog = ret;
2427 last_isspace = 0;
2428 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002429
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002430 case LOG_FMT_SSL_VERSION: // %sslv
2431 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002432 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002433 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002434 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002435 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002436 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2437 if (ret == NULL)
2438 goto out;
2439 tmplog = ret;
2440 last_isspace = 0;
2441 break;
2442#endif
William Lallemand1d705562012-03-12 12:46:41 +01002443 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002444 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002445 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002446 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002447 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002448 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002449 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002450 break;
2451
William Lallemand1d705562012-03-12 12:46:41 +01002452 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002453 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002454 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002455 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002456 break;
2457 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002458 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002459 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002460 case OBJ_TYPE_CHECK:
2461 src = (__objt_check(sess->origin)->server
2462 ? __objt_check(sess->origin)->server->id
2463 : "<NOSRV>");
2464 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002465 default:
2466 src = "<NOSRV>";
2467 break;
2468 }
William Lallemand5f232402012-04-05 18:02:55 +02002469 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002470 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002471 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002472 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002473 last_isspace = 0;
2474 break;
2475
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002476 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002477 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002478 if (ret == NULL)
2479 goto out;
2480 tmplog = ret;
2481 last_isspace = 0;
2482 break;
2483
2484 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002485 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002486 if (ret == NULL)
2487 goto out;
2488 tmplog = ret;
2489 last_isspace = 0;
2490 break;
2491
2492 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002493 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002494 tmplog, dst + maxsize - tmplog);
2495 if (ret == NULL)
2496 goto out;
2497 tmplog = ret;
2498 last_isspace = 0;
2499 break;
2500
2501 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002502 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002503 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002504 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002505 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002506 last_isspace = 0;
2507 break;
2508
William Lallemand1d705562012-03-12 12:46:41 +01002509 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002510 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002511 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002512 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002513 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002514 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002515 last_isspace = 0;
2516 break;
2517
William Lallemand1d705562012-03-12 12:46:41 +01002518 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002519 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002520 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002521 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002522 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002523 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002524 last_isspace = 0;
2525 break;
2526
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002527 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002528 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002529 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002530 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002531 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002532 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002533 last_isspace = 0;
2534 break;
2535
Willy Tarreau27b639d2016-05-17 17:55:27 +02002536 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002537 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002538 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002539 tmplog, dst + maxsize - tmplog);
2540 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002541 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002542 tmplog, dst + maxsize - tmplog);
2543 if (ret == NULL)
2544 goto out;
2545 tmplog = ret;
2546 last_isspace = 0;
2547 break;
2548
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002549 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2550 if (!(fe->to_log & LW_BYTES))
2551 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002552 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 +02002553 tmplog, dst + maxsize - tmplog);
2554 if (ret == NULL)
2555 goto out;
2556 tmplog = ret;
2557 last_isspace = 0;
2558 break;
2559
2560 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002561 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002562 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002563 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002564 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002565 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002566 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002567 last_isspace = 0;
2568 break;
2569
Damien Claisse57c8eb92020-04-28 12:09:19 +00002570 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2571 if (!(fe->to_log & LW_BYTES))
2572 LOGCHAR('+');
2573 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2574 tmplog, dst + maxsize - tmplog);
2575 if (ret == NULL)
2576 goto out;
2577 tmplog = ret;
2578 last_isspace = 0;
2579 break;
2580
Willy Tarreau2beef582012-12-20 17:22:52 +01002581 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002582 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002583 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002584 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002585 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002586 last_isspace = 0;
2587 break;
2588
William Lallemand1d705562012-03-12 12:46:41 +01002589 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002590 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002591 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002592 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002593 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002594 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002595 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002596 last_isspace = 0;
2597 break;
2598
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002599 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002600 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002601 if (ret == NULL)
2602 goto out;
2603 tmplog = ret;
2604 last_isspace = 0;
2605 break;
2606
Willy Tarreau2beef582012-12-20 17:22:52 +01002607 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002608 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002609 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002610 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002611 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002612 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002613 last_isspace = 0;
2614 break;
2615
Willy Tarreau2beef582012-12-20 17:22:52 +01002616 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002617 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002618 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002619 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002620 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002621 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002622 last_isspace = 0;
2623 break;
2624
William Lallemand1d705562012-03-12 12:46:41 +01002625 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002626 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2627 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002628 *tmplog = '\0';
2629 last_isspace = 0;
2630 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002631
William Lallemand1d705562012-03-12 12:46:41 +01002632 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002633 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2634 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002635 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2636 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 +01002637 last_isspace = 0;
2638 break;
2639
William Lallemand1d705562012-03-12 12:46:41 +01002640 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002641 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002642 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002643 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002644 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002645 last_isspace = 0;
2646 break;
2647
William Lallemand1d705562012-03-12 12:46:41 +01002648 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002649 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002650 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002651 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002652 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002653 last_isspace = 0;
2654 break;
2655
William Lallemand1d705562012-03-12 12:46:41 +01002656 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002657 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002658 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002659 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002660 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002661 last_isspace = 0;
2662 break;
2663
William Lallemand1d705562012-03-12 12:46:41 +01002664 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002665 switch (obj_type(s ? s->target : sess->origin)) {
2666 case OBJ_TYPE_SERVER:
2667 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2668 tmplog, dst + maxsize - tmplog);
2669 break;
2670 case OBJ_TYPE_CHECK:
2671 ret = ultoa_o(__objt_check(sess->origin)->server
2672 ? __objt_check(sess->origin)->server->cur_sess
2673 : 0, tmplog, dst + maxsize - tmplog);
2674 break;
2675 default:
2676 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2677 break;
2678 }
2679
William Lallemand1d705562012-03-12 12:46:41 +01002680 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002681 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002682 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002683 last_isspace = 0;
2684 break;
2685
William Lallemand1d705562012-03-12 12:46:41 +01002686 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002687 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002688 LOGCHAR('+');
Christopher Faulet731c8e62022-03-29 16:08:44 +02002689 ret = ltoa_o((s ? s->conn_retries : 0), tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002690 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002691 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002692 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002693 last_isspace = 0;
2694 break;
2695
William Lallemand1d705562012-03-12 12:46:41 +01002696 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002697 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002698 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002699 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002700 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002701 last_isspace = 0;
2702 break;
2703
William Lallemand1d705562012-03-12 12:46:41 +01002704 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002705 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002706 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002707 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002708 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002709 last_isspace = 0;
2710 break;
2711
William Lallemand1d705562012-03-12 12:46:41 +01002712 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002713 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002714 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002715 if (tmp->options & LOG_OPT_QUOTE)
2716 LOGCHAR('"');
2717 LOGCHAR('{');
2718 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2719 if (hdr)
2720 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002721 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002722 ret = lf_encode_string(tmplog, dst + maxsize,
2723 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002724 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002725 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002726 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002727 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002728 }
2729 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002730 if (tmp->options & LOG_OPT_QUOTE)
2731 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002732 last_isspace = 0;
2733 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002734 break;
2735
William Lallemand1d705562012-03-12 12:46:41 +01002736 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002737 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002738 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002739 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2740 if (hdr > 0)
2741 LOGCHAR(' ');
2742 if (tmp->options & LOG_OPT_QUOTE)
2743 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002744 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002745 ret = lf_encode_string(tmplog, dst + maxsize,
2746 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002747 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002748 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002749 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002750 } else if (!(tmp->options & LOG_OPT_QUOTE))
2751 LOGCHAR('-');
2752 if (tmp->options & LOG_OPT_QUOTE)
2753 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002754 last_isspace = 0;
2755 }
2756 }
2757 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002758
William Lallemand1d705562012-03-12 12:46:41 +01002759
2760 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002761 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002762 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002763 if (tmp->options & LOG_OPT_QUOTE)
2764 LOGCHAR('"');
2765 LOGCHAR('{');
2766 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2767 if (hdr)
2768 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002769 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002770 ret = lf_encode_string(tmplog, dst + maxsize,
2771 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002772 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002773 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002774 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002775 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002776 }
2777 LOGCHAR('}');
2778 last_isspace = 0;
2779 if (tmp->options & LOG_OPT_QUOTE)
2780 LOGCHAR('"');
2781 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002782 break;
2783
William Lallemand1d705562012-03-12 12:46:41 +01002784 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002785 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002786 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002787 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2788 if (hdr > 0)
2789 LOGCHAR(' ');
2790 if (tmp->options & LOG_OPT_QUOTE)
2791 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002792 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002793 ret = lf_encode_string(tmplog, dst + maxsize,
2794 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002795 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002796 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002797 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002798 } else if (!(tmp->options & LOG_OPT_QUOTE))
2799 LOGCHAR('-');
2800 if (tmp->options & LOG_OPT_QUOTE)
2801 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002802 last_isspace = 0;
2803 }
2804 }
2805 break;
2806
William Lallemand1d705562012-03-12 12:46:41 +01002807 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002808 /* Request */
2809 if (tmp->options & LOG_OPT_QUOTE)
2810 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002811 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002812 ret = lf_encode_string(tmplog, dst + maxsize,
2813 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002814 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002815 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002816 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002817 if (tmp->options & LOG_OPT_QUOTE)
2818 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002819 last_isspace = 0;
2820 break;
William Lallemand5f232402012-04-05 18:02:55 +02002821
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002822 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002823 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002824
Willy Tarreaub7636d12015-06-17 19:58:02 +02002825 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002826 LOGCHAR('"');
2827
2828 end = uri + strlen(uri);
2829 // look for the first whitespace character
2830 while (uri < end && !HTTP_IS_SPHT(*uri))
2831 uri++;
2832
2833 // keep advancing past multiple spaces
2834 while (uri < end && HTTP_IS_SPHT(*uri)) {
2835 uri++; nspaces++;
2836 }
2837
2838 // look for first space or question mark after url
2839 spc = uri;
2840 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2841 spc++;
2842
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002843 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002844 chunk.area = "<BADREQ>";
2845 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002846 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002847 chunk.area = uri;
2848 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002849 }
2850
Dragan Dosen835b9212016-02-12 13:23:03 +01002851 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002852 if (ret == NULL || *ret != '\0')
2853 goto out;
2854
2855 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002856 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002857 LOGCHAR('"');
2858
2859 last_isspace = 0;
2860 break;
2861
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002862 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2863 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2864
2865 if (tmp->options & LOG_OPT_QUOTE)
2866 LOGCHAR('"');
2867
2868 end = uri + strlen(uri);
2869
2870 // look for the first whitespace character
2871 while (uri < end && !HTTP_IS_SPHT(*uri))
2872 uri++;
2873
2874 // keep advancing past multiple spaces
2875 while (uri < end && HTTP_IS_SPHT(*uri)) {
2876 uri++; nspaces++;
2877 }
2878
2879 // look for first space after url
2880 spc = uri;
2881 while (spc < end && !HTTP_IS_SPHT(*spc))
2882 spc++;
2883
Tim Duesterhus92c696e2021-02-28 16:11:36 +01002884 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002885
2886 // extract relative path without query params from url
Amaury Denoyellec453f952021-07-06 11:40:12 +02002887 parser = http_uri_parser_init(path);
2888 path = iststop(http_parse_path(&parser), '?');
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002889 if (!txn || !txn->uri || nspaces == 0) {
2890 chunk.area = "<BADREQ>";
2891 chunk.data = strlen("<BADREQ>");
2892 } else {
2893 chunk.area = path.ptr;
2894 chunk.data = path.len;
2895 }
2896
2897 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2898 if (ret == NULL || *ret != '\0')
2899 goto out;
2900
2901 tmplog = ret;
2902 if (tmp->options & LOG_OPT_QUOTE)
2903 LOGCHAR('"');
2904
2905 last_isspace = 0;
2906 break;
2907
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002908 case LOG_FMT_HTTP_QUERY: // %HQ
2909 if (tmp->options & LOG_OPT_QUOTE)
2910 LOGCHAR('"');
2911
Willy Tarreau57bc8912016-04-25 17:09:40 +02002912 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002913 chunk.area = "<BADREQ>";
2914 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002915 } else {
2916 uri = txn->uri;
2917 end = uri + strlen(uri);
2918 // look for the first question mark
2919 while (uri < end && *uri != '?')
2920 uri++;
2921
2922 qmark = uri;
2923 // look for first space or question mark after url
2924 while (uri < end && !HTTP_IS_SPHT(*uri))
2925 uri++;
2926
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002927 chunk.area = qmark;
2928 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002929 }
2930
Dragan Dosen835b9212016-02-12 13:23:03 +01002931 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002932 if (ret == NULL || *ret != '\0')
2933 goto out;
2934
2935 tmplog = ret;
2936 if (tmp->options & LOG_OPT_QUOTE)
2937 LOGCHAR('"');
2938
2939 last_isspace = 0;
2940 break;
2941
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002942 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002943 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002944
Willy Tarreaub7636d12015-06-17 19:58:02 +02002945 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002946 LOGCHAR('"');
2947
2948 end = uri + strlen(uri);
2949 // look for the first whitespace character
2950 while (uri < end && !HTTP_IS_SPHT(*uri))
2951 uri++;
2952
2953 // keep advancing past multiple spaces
2954 while (uri < end && HTTP_IS_SPHT(*uri)) {
2955 uri++; nspaces++;
2956 }
2957
2958 // look for first space after url
2959 spc = uri;
2960 while (spc < end && !HTTP_IS_SPHT(*spc))
2961 spc++;
2962
Willy Tarreau57bc8912016-04-25 17:09:40 +02002963 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002964 chunk.area = "<BADREQ>";
2965 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002966 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002967 chunk.area = uri;
2968 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002969 }
2970
Dragan Dosen835b9212016-02-12 13:23:03 +01002971 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002972 if (ret == NULL || *ret != '\0')
2973 goto out;
2974
2975 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002976 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002977 LOGCHAR('"');
2978
2979 last_isspace = 0;
2980 break;
2981
2982 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002983 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002984 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002985 LOGCHAR('"');
2986
2987 end = uri + strlen(uri);
2988 // look for the first whitespace character
2989 spc = uri;
2990 while (spc < end && !HTTP_IS_SPHT(*spc))
2991 spc++;
2992
2993 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002994 chunk.area = "<BADREQ>";
2995 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002996 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002997 chunk.area = uri;
2998 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002999 }
3000
Dragan Dosen835b9212016-02-12 13:23:03 +01003001 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003002 if (ret == NULL || *ret != '\0')
3003 goto out;
3004
3005 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003006 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003007 LOGCHAR('"');
3008
3009 last_isspace = 0;
3010 break;
3011
3012 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02003013 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003014 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003015 LOGCHAR('"');
3016
3017 end = uri + strlen(uri);
3018 // look for the first whitespace character
3019 while (uri < end && !HTTP_IS_SPHT(*uri))
3020 uri++;
3021
3022 // keep advancing past multiple spaces
3023 while (uri < end && HTTP_IS_SPHT(*uri)) {
3024 uri++; nspaces++;
3025 }
3026
3027 // look for the next whitespace character
3028 while (uri < end && !HTTP_IS_SPHT(*uri))
3029 uri++;
3030
3031 // keep advancing past multiple spaces
3032 while (uri < end && HTTP_IS_SPHT(*uri))
3033 uri++;
3034
Willy Tarreau57bc8912016-04-25 17:09:40 +02003035 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003036 chunk.area = "<BADREQ>";
3037 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003038 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003039 chunk.area = "HTTP/0.9";
3040 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003041 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003042 chunk.area = uri;
3043 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003044 }
3045
Dragan Dosen835b9212016-02-12 13:23:03 +01003046 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003047 if (ret == NULL || *ret != '\0')
3048 goto out;
3049
3050 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003051 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003052 LOGCHAR('"');
3053
3054 last_isspace = 0;
3055 break;
3056
William Lallemand5f232402012-04-05 18:02:55 +02003057 case LOG_FMT_COUNTER: // %rt
3058 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003059 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02003060 if (iret < 0 || iret >= dst + maxsize - tmplog)
William Lallemand5f232402012-04-05 18:02:55 +02003061 goto out;
3062 last_isspace = 0;
3063 tmplog += iret;
3064 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003065 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003066 if (ret == NULL)
3067 goto out;
3068 tmplog = ret;
3069 last_isspace = 0;
3070 }
3071 break;
3072
Willy Tarreau7346acb2014-08-28 15:03:15 +02003073 case LOG_FMT_LOGCNT: // %lc
3074 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003075 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02003076 if (iret < 0 || iret >= dst + maxsize - tmplog)
Willy Tarreau7346acb2014-08-28 15:03:15 +02003077 goto out;
3078 last_isspace = 0;
3079 tmplog += iret;
3080 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003081 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003082 if (ret == NULL)
3083 goto out;
3084 tmplog = ret;
3085 last_isspace = 0;
3086 }
3087 break;
3088
William Lallemand5f232402012-04-05 18:02:55 +02003089 case LOG_FMT_HOSTNAME: // %H
3090 src = hostname;
3091 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3092 if (ret == NULL)
3093 goto out;
3094 tmplog = ret;
3095 last_isspace = 0;
3096 break;
3097
3098 case LOG_FMT_PID: // %pid
3099 if (tmp->options & LOG_OPT_HEXA) {
3100 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
Aurelien DARRAGON74e040b2024-04-09 15:59:42 +02003101 if (iret < 0 || iret >= dst + maxsize - tmplog)
William Lallemand5f232402012-04-05 18:02:55 +02003102 goto out;
3103 last_isspace = 0;
3104 tmplog += iret;
3105 } else {
3106 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3107 if (ret == NULL)
3108 goto out;
3109 tmplog = ret;
3110 last_isspace = 0;
3111 }
3112 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003113
3114 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003115 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003116 if (s)
3117 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3118 else
3119 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003120 if (ret == NULL)
3121 goto out;
3122 tmplog = ret;
3123 last_isspace = 0;
3124 break;
3125
William Lallemandbddd4fd2012-02-27 11:23:10 +01003126 }
3127 }
3128
3129out:
William Lallemand1d705562012-03-12 12:46:41 +01003130 /* *tmplog is a unused character */
3131 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003132 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003133
Willy Tarreaubaaee002006-06-26 02:48:02 +02003134}
3135
William Lallemand1d705562012-03-12 12:46:41 +01003136/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003137 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003138 * Will not log if the frontend has no log defined.
3139 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003140void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003141{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003142 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003143 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003144 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003145
3146 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003147 err = (s->flags & SF_REDISP) ||
3148 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
Christopher Fauleta892b7f2022-06-17 14:53:20 +02003149 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) && s->conn_retries) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003150 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003151
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003152 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003153 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003154
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003155 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003156 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003157
Willy Tarreauabcd5142013-06-11 17:18:02 +02003158 if (s->logs.level) { /* loglevel was overridden */
3159 if (s->logs.level == -1) {
3160 s->logs.logwait = 0; /* logs disabled */
3161 return;
3162 }
3163 level = s->logs.level - 1;
3164 }
3165 else {
3166 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003167 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003168 level = LOG_ERR;
3169 }
William Lallemand1d705562012-03-12 12:46:41 +01003170
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003171 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003172 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003173 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003174 }
3175
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003176 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3177 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3178 &sess->fe->logformat_sd);
3179 }
3180
Dragan Dosen59cee972015-09-19 22:09:02 +02003181 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003182 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003183 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003184 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3185 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003186 s->logs.logwait = 0;
3187 }
3188}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003189
Willy Tarreau53839352018-09-05 19:51:10 +02003190/*
3191 * send a minimalist log for the session. Will not log if the frontend has no
3192 * log defined. It is assumed that this is only used to report anomalies that
3193 * cannot lead to the creation of a regular stream. Because of this the log
3194 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3195 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003196 * function to report unimportant events. It is safe to call this function with
3197 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003198 */
3199void sess_log(struct session *sess)
3200{
3201 int size, level;
3202 int sd_size = 0;
3203
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003204 if (!sess)
3205 return;
3206
Willy Tarreau53839352018-09-05 19:51:10 +02003207 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3208 return;
3209
3210 level = LOG_INFO;
3211 if (sess->fe->options2 & PR_O2_LOGERRORS)
3212 level = LOG_ERR;
3213
3214 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3215 sd_size = sess_build_logline(sess, NULL,
3216 logline_rfc5424, global.max_syslog_len,
3217 &sess->fe->logformat_sd);
3218 }
3219
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02003220 if (!LIST_ISEMPTY(&sess->fe->logformat_error))
3221 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat_error);
3222 else
3223 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
Willy Tarreau53839352018-09-05 19:51:10 +02003224 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003225 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003226 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3227 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003228 }
3229}
3230
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003231void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3232{
3233 va_list argp;
3234 int data_len;
3235
3236 if (level < 0 || format == NULL || logline == NULL)
3237 return;
3238
3239 va_start(argp, format);
3240 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3241 if (data_len < 0 || data_len > global.max_syslog_len)
3242 data_len = global.max_syslog_len;
3243 va_end(argp);
3244
3245 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3246}
Emeric Brun54932b42020-07-07 09:43:24 +02003247/*
3248 * This function parse a received log message <buf>, of size <buflen>
3249 * it fills <level>, <facility> and <metadata> depending of the detected
3250 * header format and message will point on remaining payload of <size>
3251 *
3252 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3253 * struct ist len will be set to 0 if field is not found
3254 * <level> and <facility> will be set to -1 if not found.
3255 */
3256void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3257 struct ist *metadata, char **message, size_t *size)
3258{
3259
3260 char *p;
3261 int fac_level = 0;
3262
3263 *level = *facility = -1;
3264
3265 *message = buf;
3266 *size = buflen;
3267
3268 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3269
3270 p = buf;
3271 if (*size < 2 || *p != '<')
3272 return;
3273
3274 p++;
3275 while (*p != '>') {
3276 if (*p > '9' || *p < '0')
3277 return;
3278 fac_level = 10*fac_level + (*p - '0');
3279 p++;
3280 if ((p - buf) > buflen)
3281 return;
3282 }
3283
3284 *facility = fac_level >> 3;
3285 *level = fac_level & 0x7;
3286 p++;
3287
3288 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3289
3290 buflen -= p - buf;
3291 buf = p;
3292
3293 *size = buflen;
3294 *message = buf;
3295
3296 /* for rfc5424, prio is always followed by '1' and ' ' */
3297 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3298 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3299 * followed by message.
3300 * Each header field can present NILVALUE: '-'
3301 */
3302
3303 p += 2;
Aurelien DARRAGONab9efc22022-11-22 11:17:11 +01003304 *size -= 2;
Emeric Brun54932b42020-07-07 09:43:24 +02003305 /* timestamp is NILVALUE '-' */
3306 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3307 metadata[LOG_META_TIME] = ist2(p, 1);
3308 p++;
3309 }
3310 else if (*size > LOG_ISOTIME_MINLEN) {
3311 metadata[LOG_META_TIME].ptr = p;
3312
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003313 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003314 * in timestamp.
3315 * possible format are:
3316 * ex: '1970-01-01T00:00:00.000000Z'
3317 * '1970-01-01T00:00:00.000000+00:00'
3318 * '1970-01-01T00:00:00.000000-00:00'
3319 * '1970-01-01T00:00:00Z'
3320 * '1970-01-01T00:00:00+00:00'
3321 * '1970-01-01T00:00:00-00:00'
3322 */
3323 p += 19;
3324 if (*p == '.') {
3325 p++;
3326 if ((p - buf) >= buflen)
3327 goto bad_format;
3328 while (*p != 'Z' && *p != '+' && *p != '-') {
3329 if ((unsigned char)(*p - '0') > 9)
3330 goto bad_format;
3331
3332 p++;
3333 if ((p - buf) >= buflen)
3334 goto bad_format;
3335 }
3336 }
3337
3338 if (*p == 'Z')
3339 p++;
3340 else
3341 p += 6; /* case of '+00:00 or '-00:00' */
3342
3343 if ((p - buf) >= buflen || *p != ' ')
3344 goto bad_format;
3345 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3346 }
3347 else
3348 goto bad_format;
3349
3350
3351 p++;
3352 if ((p - buf) >= buflen || *p == ' ')
3353 goto bad_format;
3354
3355 metadata[LOG_META_HOST].ptr = p;
3356 while (*p != ' ') {
3357 p++;
3358 if ((p - buf) >= buflen)
3359 goto bad_format;
3360 }
3361 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3362 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3363 metadata[LOG_META_HOST].len = 0;
3364
3365 p++;
3366 if ((p - buf) >= buflen || *p == ' ')
3367 goto bad_format;
3368
3369 metadata[LOG_META_TAG].ptr = p;
3370 while (*p != ' ') {
3371 p++;
3372 if ((p - buf) >= buflen)
3373 goto bad_format;
3374 }
3375 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3376 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3377 metadata[LOG_META_TAG].len = 0;
3378
3379 p++;
3380 if ((p - buf) >= buflen || *p == ' ')
3381 goto bad_format;
3382
3383 metadata[LOG_META_PID].ptr = p;
3384 while (*p != ' ') {
3385 p++;
3386 if ((p - buf) >= buflen)
3387 goto bad_format;
3388 }
3389 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3390 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3391 metadata[LOG_META_PID].len = 0;
3392
3393 p++;
3394 if ((p - buf) >= buflen || *p == ' ')
3395 goto bad_format;
3396
3397 metadata[LOG_META_MSGID].ptr = p;
3398 while (*p != ' ') {
3399 p++;
3400 if ((p - buf) >= buflen)
3401 goto bad_format;
3402 }
3403 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3404 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3405 metadata[LOG_META_MSGID].len = 0;
3406
3407 p++;
3408 if ((p - buf) >= buflen || *p == ' ')
3409 goto bad_format;
3410
3411 /* structured data format is:
3412 * ex:
3413 * '[key1=value1 key2=value2][key3=value3]'
3414 *
3415 * space is invalid outside [] because
3416 * considered as the end of structured data field
3417 */
3418 metadata[LOG_META_STDATA].ptr = p;
3419 if (*p == '[') {
3420 int elem = 0;
3421
3422 while (1) {
3423 if (elem) {
3424 /* according to rfc this char is escaped in param values */
3425 if (*p == ']' && *(p-1) != '\\')
3426 elem = 0;
3427 }
3428 else {
3429 if (*p == '[')
3430 elem = 1;
3431 else if (*p == ' ')
3432 break;
3433 else
3434 goto bad_format;
3435 }
3436 p++;
3437 if ((p - buf) >= buflen)
3438 goto bad_format;
3439 }
3440 }
3441 else if (*p == '-') {
3442 /* case of NILVALUE */
3443 p++;
3444 if ((p - buf) >= buflen || *p != ' ')
3445 goto bad_format;
3446 }
3447 else
3448 goto bad_format;
3449
3450 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3451 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3452 metadata[LOG_META_STDATA].len = 0;
3453
3454 p++;
3455
3456 buflen -= p - buf;
3457 buf = p;
3458
3459 *size = buflen;
3460 *message = p;
3461 }
3462 else if (*size > LOG_LEGACYTIME_LEN) {
3463 int m;
3464
3465 /* supported header format according to rfc3164.
3466 * ex:
3467 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3468 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3469 * or 'Jan 1 00:00:00 HOSTNAME '
3470 * Note: HOSTNAME is mandatory, and day
3471 * of month uses a single space prefix if
3472 * less than 10 to ensure hour offset is
3473 * always the same.
3474 */
3475
3476 /* Check month to see if it correspond to a rfc3164
3477 * header ex 'Jan 1 00:00:00' */
3478 for (m = 0; m < 12; m++)
3479 if (!memcmp(monthname[m], p, 3))
3480 break;
3481 /* Month not found */
3482 if (m == 12)
3483 goto bad_format;
3484
3485 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3486
3487 p += LOG_LEGACYTIME_LEN;
3488 if ((p - buf) >= buflen || *p != ' ')
3489 goto bad_format;
3490
3491 p++;
3492 if ((p - buf) >= buflen || *p == ' ')
3493 goto bad_format;
3494
3495 metadata[LOG_META_HOST].ptr = p;
3496 while (*p != ' ') {
3497 p++;
3498 if ((p - buf) >= buflen)
3499 goto bad_format;
3500 }
3501 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3502
3503 /* TAG seems to no be mandatory */
3504 p++;
3505
3506 buflen -= p - buf;
3507 buf = p;
3508
3509 *size = buflen;
3510 *message = buf;
3511
3512 if (!buflen)
3513 return;
3514
3515 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3516 p++;
3517
3518 /* a tag must present a trailing ':' */
3519 if (((p - buf) >= buflen) || *p != ':')
3520 return;
3521 p++;
3522 /* followed by a space */
3523 if (((p - buf) >= buflen) || *p != ' ')
3524 return;
3525
3526 /* rewind to parse tag and pid */
3527 p = buf;
3528 metadata[LOG_META_TAG].ptr = p;
3529 /* we have the guarantee that ':' will be reach before size limit */
3530 while (*p != ':') {
3531 if (*p == '[') {
3532 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3533 metadata[LOG_META_PID].ptr = p + 1;
3534 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003535 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003536 if (p[1] != ':')
3537 return;
3538 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3539 }
3540 p++;
3541 }
3542 if (!metadata[LOG_META_TAG].len)
3543 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3544
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003545 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003546 p += 2;
3547
3548 buflen -= p - buf;
3549 buf = p;
3550
3551 *size = buflen;
3552 *message = buf;
3553 }
3554
3555 return;
3556
3557bad_format:
3558 /* bad syslog format, we reset all parsed syslog fields
3559 * but priority is kept because we are able to re-build
3560 * this message using LOF_FORMAT_PRIO.
3561 */
3562 metadata[LOG_META_TIME].len = 0;
3563 metadata[LOG_META_HOST].len = 0;
3564 metadata[LOG_META_TAG].len = 0;
3565 metadata[LOG_META_PID].len = 0;
3566 metadata[LOG_META_MSGID].len = 0;
3567 metadata[LOG_META_STDATA].len = 0;
3568
3569 return;
3570}
3571
3572/*
3573 * UDP syslog fd handler
3574 */
3575void syslog_fd_handler(int fd)
3576{
3577 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3578 ssize_t ret = 0;
3579 struct buffer *buf = get_trash_chunk();
3580 size_t size;
3581 char *message;
3582 int level;
3583 int facility;
3584 struct listener *l = objt_listener(fdtab[fd].owner);
3585 int max_accept;
3586
Tim Duesterhus16554242021-09-15 13:58:49 +02003587 BUG_ON(!l);
Emeric Brun54932b42020-07-07 09:43:24 +02003588
Willy Tarreauf5090652021-04-06 17:23:40 +02003589 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003590
3591 if (!fd_recv_ready(fd))
3592 return;
3593
Willy Tarreau882f2482023-01-12 18:52:23 +01003594 max_accept = l->bind_conf->maxaccept ? l->bind_conf->maxaccept : 1;
Emeric Brun54932b42020-07-07 09:43:24 +02003595
3596 do {
3597 /* Source address */
3598 struct sockaddr_storage saddr = {0};
3599 socklen_t saddrlen;
3600
3601 saddrlen = sizeof(saddr);
3602
3603 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3604 if (ret < 0) {
3605 if (errno == EINTR)
3606 continue;
Willy Tarreauacef5e22022-04-25 20:32:15 +02003607 if (errno == EAGAIN || errno == EWOULDBLOCK)
Emeric Brun54932b42020-07-07 09:43:24 +02003608 fd_cant_recv(fd);
3609 goto out;
3610 }
3611 buf->data = ret;
3612
Emeric Brun45c457a2020-07-09 23:23:34 +02003613 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003614 _HA_ATOMIC_INC(&cum_log_messages);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01003615 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend, 0);
Emeric Brun45c457a2020-07-09 23:23:34 +02003616
Emeric Brun54932b42020-07-07 09:43:24 +02003617 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3618
3619 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3620
3621 } while (--max_accept);
3622 }
3623
3624out:
3625 return;
3626}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003627
Emeric Brun12941c82020-07-07 14:19:42 +02003628/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003629 * IO Handler to handle message exchange with a syslog tcp client
3630 */
3631static void syslog_io_handler(struct appctx *appctx)
3632{
3633 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
Willy Tarreauc12b3212022-05-27 11:08:15 +02003634 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau270a4572022-05-27 10:36:36 +02003635 struct stream *s = __sc_strm(sc);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003636 struct proxy *frontend = strm_fe(s);
3637 struct listener *l = strm_li(s);
3638 struct buffer *buf = get_trash_chunk();
3639 int max_accept;
3640 int to_skip;
3641 int facility;
3642 int level;
3643 char *message;
3644 size_t size;
3645
Christopher Faulet211452e2023-04-11 07:56:50 +02003646 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
3647 co_skip(sc_oc(sc), co_data(sc_oc(sc)));
Christopher Faulet22a88f02023-03-31 11:17:13 +02003648 goto out;
Christopher Faulet211452e2023-04-11 07:56:50 +02003649 }
Christopher Faulet22a88f02023-03-31 11:17:13 +02003650
Willy Tarreau882f2482023-01-12 18:52:23 +01003651 max_accept = l->bind_conf->maxaccept ? l->bind_conf->maxaccept : 1;
Christopher Faulet285aa402023-04-17 16:34:29 +02003652 while (1) {
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003653 char c;
3654
3655 if (max_accept <= 0)
3656 goto missing_budget;
3657 max_accept--;
3658
Willy Tarreau270a4572022-05-27 10:36:36 +02003659 to_skip = co_getchar(sc_oc(sc), &c);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003660 if (!to_skip)
3661 goto missing_data;
3662 else if (to_skip < 0)
3663 goto cli_abort;
3664
3665 if (c == '<') {
3666 /* rfc-6587, Non-Transparent-Framing: messages separated by
3667 * a trailing LF or CR LF
3668 */
Willy Tarreau270a4572022-05-27 10:36:36 +02003669 to_skip = co_getline(sc_oc(sc), buf->area, buf->size);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003670 if (!to_skip)
3671 goto missing_data;
3672 else if (to_skip < 0)
3673 goto cli_abort;
3674
3675 if (buf->area[to_skip - 1] != '\n')
3676 goto parse_error;
3677
3678 buf->data = to_skip - 1;
3679
3680 /* according to rfc-6587, some devices adds CR before LF */
3681 if (buf->data && buf->area[buf->data - 1] == '\r')
3682 buf->data--;
3683
3684 }
3685 else if ((unsigned char)(c - '1') <= 8) {
3686 /* rfc-6587, Octet-Counting: message length in ASCII
3687 * (first digit can not be ZERO), followed by a space
3688 * and message length
3689 */
3690 char *p = NULL;
3691 int msglen;
3692
Willy Tarreau270a4572022-05-27 10:36:36 +02003693 to_skip = co_getword(sc_oc(sc), buf->area, buf->size, ' ');
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003694 if (!to_skip)
3695 goto missing_data;
3696 else if (to_skip < 0)
3697 goto cli_abort;
3698
3699 if (buf->area[to_skip - 1] != ' ')
3700 goto parse_error;
3701
Aurelien DARRAGON7faffdc2022-10-26 23:40:08 +02003702 msglen = strtol(buf->area, &p, 10);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003703 if (!msglen || p != &buf->area[to_skip - 1])
3704 goto parse_error;
3705
3706 /* message seems too large */
3707 if (msglen > buf->size)
3708 goto parse_error;
3709
Willy Tarreau270a4572022-05-27 10:36:36 +02003710 msglen = co_getblk(sc_oc(sc), buf->area, msglen, to_skip);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003711 if (!msglen)
3712 goto missing_data;
3713 else if (msglen < 0)
3714 goto cli_abort;
3715
3716
3717 buf->data = msglen;
3718 to_skip += msglen;
3719 }
3720 else
3721 goto parse_error;
3722
Willy Tarreau270a4572022-05-27 10:36:36 +02003723 co_skip(sc_oc(sc), to_skip);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003724
3725 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003726 _HA_ATOMIC_INC(&cum_log_messages);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01003727 proxy_inc_fe_req_ctr(l, frontend, 0);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003728
3729 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3730
3731 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3732
3733 }
3734
3735missing_data:
3736 /* we need more data to read */
Christopher Faulet3aeb3662023-03-16 14:27:29 +01003737 applet_need_more_data(appctx);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003738 return;
3739
3740missing_budget:
3741 /* it may remain some stuff to do, let's retry later */
3742 appctx_wakeup(appctx);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003743 return;
3744
3745parse_error:
3746 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003747 _HA_ATOMIC_INC(&l->counters->failed_req);
3748 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003749
Christopher Faulet22a88f02023-03-31 11:17:13 +02003750 goto error;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003751
3752cli_abort:
3753 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003754 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3755 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003756
Christopher Faulet22a88f02023-03-31 11:17:13 +02003757error:
3758 se_fl_set(appctx->sedesc, SE_FL_ERROR);
3759
3760out:
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003761 return;
3762}
3763
3764static struct applet syslog_applet = {
3765 .obj_type = OBJ_TYPE_APPLET,
3766 .name = "<SYSLOG>", /* used for logging */
3767 .fct = syslog_io_handler,
3768 .release = NULL,
3769};
3770
3771/*
Emeric Brun12941c82020-07-07 14:19:42 +02003772 * Parse "log-forward" section and create corresponding sink buffer.
3773 *
3774 * The function returns 0 in success case, otherwise, it returns error
3775 * flags.
3776 */
3777int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3778{
Aurelien DARRAGON2a9dfb82023-07-03 18:33:18 +02003779 int err_code = ERR_NONE;
Emeric Brun12941c82020-07-07 14:19:42 +02003780 struct proxy *px;
3781 char *errmsg = NULL;
3782 const char *err = NULL;
3783
3784 if (strcmp(args[0], "log-forward") == 0) {
3785 if (!*args[1]) {
Aurelien DARRAGON53f080d2023-07-03 17:35:54 +02003786 ha_alert("parsing [%s:%d] : missing name for log-forward section.\n", file, linenum);
Emeric Brun12941c82020-07-07 14:19:42 +02003787 err_code |= ERR_ALERT | ERR_ABORT;
3788 goto out;
3789 }
3790
3791 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3792 goto out;
3793
3794 err = invalid_char(args[1]);
3795 if (err) {
3796 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3797 file, linenum, *err, args[0], args[1]);
3798 err_code |= ERR_ALERT | ERR_ABORT;
3799 goto out;
3800 }
3801
Emeric Brunb0c331f2020-10-07 17:05:59 +02003802 px = log_forward_by_name(args[1]);
3803 if (px) {
3804 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3805 file, linenum, args[1], px->conf.file, px->conf.line);
3806 err_code |= ERR_ALERT | ERR_FATAL;
Aurelien DARRAGON2a9dfb82023-07-03 18:33:18 +02003807 goto out;
Emeric Brunb0c331f2020-10-07 17:05:59 +02003808 }
3809
3810 px = proxy_find_by_name(args[1], 0, 0);
3811 if (px) {
3812 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3813 file, linenum, args[1], proxy_type_str(px),
3814 px->id, px->conf.file, px->conf.line);
3815 err_code |= ERR_ALERT | ERR_FATAL;
Aurelien DARRAGON2a9dfb82023-07-03 18:33:18 +02003816 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003817 }
3818
3819 px = calloc(1, sizeof *px);
3820 if (!px) {
3821 err_code |= ERR_ALERT | ERR_FATAL;
3822 goto out;
3823 }
3824
Emeric Brun2ad2b1c2021-12-01 12:08:42 +01003825 init_new_proxy(px);
Emeric Brun12941c82020-07-07 14:19:42 +02003826 px->next = cfg_log_forward;
3827 cfg_log_forward = px;
Emeric Brun12941c82020-07-07 14:19:42 +02003828 px->conf.file = strdup(file);
3829 px->conf.line = linenum;
3830 px->mode = PR_MODE_SYSLOG;
Willy Tarreau69530f52023-04-28 09:16:15 +02003831 px->last_change = ns_to_sec(now_ns);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003832 px->cap = PR_CAP_FE;
3833 px->maxconn = 10;
3834 px->timeout.client = TICK_ETERNITY;
3835 px->accept = frontend_accept;
3836 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003837 px->id = strdup(args[1]);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003838 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003839 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003840 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3841 err_code |= ERR_WARN;
3842
3843 if (*(args[1]) == 0) {
3844 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3845 err_code |= ERR_ALERT | ERR_FATAL;
3846 goto out;
3847 }
3848 cfg_log_forward->maxconn = atol(args[1]);
3849 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3850 goto out;
3851 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003852 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003853 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3854 err_code |= ERR_WARN;
3855
3856 if (*(args[1]) == 0) {
3857 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3858 err_code |= ERR_ALERT | ERR_FATAL;
3859 goto out;
3860 }
3861 cfg_log_forward->backlog = atol(args[1]);
3862 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3863 goto out;
3864 }
3865 else if (strcmp(args[0], "bind") == 0) {
3866 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003867 struct bind_conf *bind_conf;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003868 struct listener *l;
Emeric Bruna8942cd2022-08-19 15:16:26 +02003869 int ret;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003870
3871 cur_arg = 1;
3872
3873 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3874 NULL, xprt_get(XPRT_RAW));
3875 if (!bind_conf) {
3876 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3877 err_code |= ERR_ALERT | ERR_FATAL;
3878 goto out;
3879 }
3880
Willy Tarreau882f2482023-01-12 18:52:23 +01003881 bind_conf->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreau30836152023-01-12 19:10:17 +01003882 bind_conf->accept = session_accept_fd;
Willy Tarreau882f2482023-01-12 18:52:23 +01003883
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003884 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3885 if (errmsg && *errmsg) {
3886 indent_msg(&errmsg, 2);
3887 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3888 }
3889 else {
3890 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3891 file, linenum, args[0], args[1], args[2]);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003892 }
Aurelien DARRAGON2a9dfb82023-07-03 18:33:18 +02003893 err_code |= ERR_ALERT | ERR_FATAL;
3894 goto out;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003895 }
3896 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003897 global.maxsock++;
3898 }
3899 cur_arg++;
3900
Emeric Bruna8942cd2022-08-19 15:16:26 +02003901 ret = bind_parse_args_list(bind_conf, args, cur_arg, cursection, file, linenum);
3902 err_code |= ret;
3903 if (ret != 0) {
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003904 err_code |= ERR_ALERT | ERR_FATAL;
3905 goto out;
3906 }
Emeric Brun12941c82020-07-07 14:19:42 +02003907 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003908 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003909 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02003910 struct bind_conf *bind_conf;
3911 struct bind_kw *kw;
3912 struct listener *l;
3913
3914 cur_arg = 1;
3915
3916 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3917 NULL, xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +02003918 if (!bind_conf) {
3919 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3920 err_code |= ERR_ALERT | ERR_FATAL;
3921 goto out;
3922 }
Emeric Brun12941c82020-07-07 14:19:42 +02003923
Willy Tarreau882f2482023-01-12 18:52:23 +01003924 bind_conf->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
3925
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003926 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003927 if (errmsg && *errmsg) {
3928 indent_msg(&errmsg, 2);
3929 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3930 }
3931 else {
3932 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3933 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003934 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003935 err_code |= ERR_ALERT | ERR_FATAL;
3936 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003937 }
3938 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003939 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreaue140a692020-10-15 21:25:32 +02003940 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003941 global.maxsock++;
3942 }
3943 cur_arg++;
3944
3945 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3946 int ret;
3947
3948 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3949 err_code |= ret;
3950 if (ret) {
3951 if (errmsg && *errmsg) {
3952 indent_msg(&errmsg, 2);
3953 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3954 }
3955 else
3956 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3957 file, linenum, args[cur_arg]);
3958 if (ret & ERR_FATAL)
3959 goto out;
3960 }
3961 cur_arg += 1 + kw->skip;
3962 }
3963 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003964 const char *best = bind_find_best_kw(args[cur_arg]);
3965 if (best)
3966 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3967 file, linenum, args[cur_arg], cursection, best);
3968 else
3969 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3970 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02003971 err_code |= ERR_ALERT | ERR_FATAL;
3972 goto out;
3973 }
3974 }
3975 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02003976 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003977 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3978 err_code |= ERR_ALERT | ERR_FATAL;
3979 goto out;
3980 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003981 }
3982 else if (strcmp(args[0], "timeout") == 0) {
3983 const char *res;
3984 unsigned timeout;
3985
3986 if (strcmp(args[1], "client") != 0) {
3987 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
3988 err_code |= ERR_ALERT | ERR_FATAL;
3989 goto out;
3990 }
3991
3992 if (*args[2] == 0) {
3993 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
3994 err_code |= ERR_ALERT | ERR_FATAL;
3995 goto out;
3996 }
3997 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3998 if (res == PARSE_TIME_OVER) {
3999 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
4000 }
4001 else if (res == PARSE_TIME_UNDER) {
4002 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
4003 }
4004 else if (res) {
4005 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004006 }
4007
4008 if (res) {
4009 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4010 err_code |= ERR_ALERT | ERR_FATAL;
4011 goto out;
4012 }
4013 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02004014 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02004015 else {
4016 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
4017 err_code |= ERR_ALERT | ERR_ABORT;
4018 goto out;
4019 }
Emeric Brun12941c82020-07-07 14:19:42 +02004020out:
Aurelien DARRAGON791433e2023-07-04 15:36:45 +02004021 ha_free(&errmsg);
Emeric Brun12941c82020-07-07 14:19:42 +02004022 return err_code;
4023}
4024
Willy Tarreau0108d902018-11-25 19:14:37 +01004025
Emeric Brun12941c82020-07-07 14:19:42 +02004026/* config parsers for this section */
4027REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4028
Willy Tarreau082b6282019-05-22 14:42:12 +02004029REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4030REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004031
Willy Tarreaubaaee002006-06-26 02:48:02 +02004032/*
4033 * Local variables:
4034 * c-indent-level: 8
4035 * c-basic-offset: 8
4036 * End:
4037 */