blob: b6fc58498c0a54a47524d19feeb2658ff33b6670 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
Willy Tarreau077edcb2016-08-10 18:30:56 +020025#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/applet-t.h>
Emeric Brun12941c82020-07-07 14:19:42 +020029#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020030#include <haproxy/cli.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020032#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020034#include <haproxy/http.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/ring.h>
39#include <haproxy/sample.h>
40#include <haproxy/sink.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020041#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020043#include <haproxy/stream_interface.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 Tarreaud6788052020-05-27 15:59:00 +020046#include <haproxy/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Emeric Brun45c457a2020-07-09 23:23:34 +020048/* global recv logs counter */
49int cum_log_messages;
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Emeric Brun12941c82020-07-07 14:19:42 +020051/* log forward proxy list */
52struct proxy *cfg_log_forward;
53
Emeric Brun54648852020-07-06 15:54:06 +020054struct log_fmt_st {
Dragan Dosen43885c72015-10-01 13:18:13 +020055 char *name;
Dragan Dosen43885c72015-10-01 13:18:13 +020056};
57
Emeric Brun54648852020-07-06 15:54:06 +020058static const struct log_fmt_st log_formats[LOG_FORMATS] = {
Emeric Brun0237c4e2020-11-27 16:24:34 +010059 [LOG_FORMAT_LOCAL] = {
60 .name = "local",
61 },
Dragan Dosen43885c72015-10-01 13:18:13 +020062 [LOG_FORMAT_RFC3164] = {
63 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020064 },
65 [LOG_FORMAT_RFC5424] = {
66 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020067 },
68 [LOG_FORMAT_PRIO] = {
69 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010070 },
71 [LOG_FORMAT_SHORT] = {
72 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020073 },
74 [LOG_FORMAT_TIMED] = {
75 .name = "timed",
76 },
77 [LOG_FORMAT_ISO] = {
78 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010079 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010080 [LOG_FORMAT_RAW] = {
81 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010082 },
Dragan Dosen1322d092015-09-22 16:05:32 +020083};
84
Dragan Dosen835b9212016-02-12 13:23:03 +010085/*
86 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020087 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
88 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
89 * that the byte should be escaped. Be careful to always pass bytes from 0 to
90 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010091 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020092long rfc5424_escape_map[(256/8) / sizeof(long)];
93long hdr_encode_map[(256/8) / sizeof(long)];
94long url_encode_map[(256/8) / sizeof(long)];
95long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010096
Dragan Dosen835b9212016-02-12 13:23:03 +010097
Willy Tarreaubaaee002006-06-26 02:48:02 +020098const char *log_facilities[NB_LOG_FACILITIES] = {
99 "kern", "user", "mail", "daemon",
100 "auth", "syslog", "lpr", "news",
101 "uucp", "cron", "auth2", "ftp",
102 "ntp", "audit", "alert", "cron2",
103 "local0", "local1", "local2", "local3",
104 "local4", "local5", "local6", "local7"
105};
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107const char *log_levels[NB_LOG_LEVELS] = {
108 "emerg", "alert", "crit", "err",
109 "warning", "notice", "info", "debug"
110};
111
Willy Tarreau570f2212013-06-10 16:42:09 +0200112const 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 +0200113const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
William Lallemand723b73a2012-02-08 16:37:49 +0100115
116/* log_format */
117struct logformat_type {
118 char *name;
119 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100120 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200121 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100122 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100123 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100124};
125
William Lallemandb7ff6a32012-03-02 14:35:21 +0100126int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
127
William Lallemand723b73a2012-02-08 16:37:49 +0100128/* log_format variable names */
129static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200130 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100131
132 /* please keep these lines sorted ! */
133 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
134 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
135 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
136 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100137 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200138 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200139 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200140 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100141 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200142 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
143 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
144 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
145 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
146 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
147 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200148 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100149 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200150 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000151 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100152 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
153 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200154 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100155 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200156 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100157 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
158 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200159 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200160 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
161 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100162 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
163 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200164 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
165 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100166 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200167 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
168 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
169 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
170 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000171 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
Maciej Zdebfcdfd852020-11-30 18:27:47 +0000172 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP relative or absolute path */
173 { "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 +0000174 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000175 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
176 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200177 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100178 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200179 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100180 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
181 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100182 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100183 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
184 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
185 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
186 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
187 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200188 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
189 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100190 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200191 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
192 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
193 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100194 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
195 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
196
197 /* The following tags are deprecated and will be removed soon */
198 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
199 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200200 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
201 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
202 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
203 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100204 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
205 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
206 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
207 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
208 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200209 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100210};
211
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200212char 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
213char 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 +0100214char 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 +0100215char *log_format = NULL;
216
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200217/* Default string used for structured-data part in RFC5424 formatted
218 * syslog messages.
219 */
220char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200221
Willy Tarreau13ef7732018-11-12 07:25:28 +0100222/* total number of dropped logs */
223unsigned int dropped_logs = 0;
224
Dragan Dosen59cee972015-09-19 22:09:02 +0200225/* This is a global syslog message buffer, common to all outgoing
226 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100227 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200228THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100229
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200230/* A global syslog message buffer, common to all RFC5424 syslog messages.
231 * Currently, it is used for generating the structured-data part.
232 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200233THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200234
Christopher Fauletd4696382017-10-24 11:44:05 +0200235/* A global buffer used to store all startup alerts/warnings. It will then be
236 * retrieve on the CLI. */
Willy Tarreau869efd52019-11-15 15:16:57 +0100237static struct ring *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200238
William Lallemand723b73a2012-02-08 16:37:49 +0100239struct logformat_var_args {
240 char *name;
241 int mask;
242};
243
244struct logformat_var_args var_args_list[] = {
245// global
246 { "M", LOG_OPT_MANDATORY },
247 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200248 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100249 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100250 { 0, 0 }
251};
252
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200253/* return the name of the directive used in the current proxy for which we're
254 * currently parsing a header, when it is known.
255 */
256static inline const char *fmt_directive(const struct proxy *curproxy)
257{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100258 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200259 case ARGC_ACL:
260 return "acl";
261 case ARGC_STK:
262 return "stick";
263 case ARGC_TRK:
264 return "track-sc";
265 case ARGC_LOG:
266 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200267 case ARGC_LOGSD:
268 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100269 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100270 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100271 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100272 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200273 case ARGC_UIF:
274 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100275 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200276 return "redirect";
277 case ARGC_CAP:
278 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200279 case ARGC_SRV:
280 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200281 case ARGC_SPOE:
282 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100283 case ARGC_UBK:
284 return "use_backend";
Christopher Faulet3b967c12020-05-15 15:47:44 +0200285 case ARGC_HERR:
286 return "http-error";
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +0100287 case ARGC_OT:
288 return "ot-scope";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100289 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200290 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100291 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200292}
293
William Lallemand723b73a2012-02-08 16:37:49 +0100294/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100295 * callback used to configure addr source retrieval
296 */
297int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
298{
299 curproxy->options2 |= PR_O2_SRC_ADDR;
300
301 return 0;
302}
303
304
305/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100306 * Parse args in a logformat_var. Returns 0 in error
307 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100308 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100309int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100310{
311 int i = 0;
312 int end = 0;
313 int flags = 0; // 1 = + 2 = -
314 char *sp = NULL; // start pointer
315
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100316 if (args == NULL) {
317 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100318 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100319 }
William Lallemand723b73a2012-02-08 16:37:49 +0100320
321 while (1) {
322 if (*args == '\0')
323 end = 1;
324
325 if (*args == '+') {
326 // add flag
327 sp = args + 1;
328 flags = 1;
329 }
330 if (*args == '-') {
331 // delete flag
332 sp = args + 1;
333 flags = 2;
334 }
335
336 if (*args == '\0' || *args == ',') {
337 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100338 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100339 if (strcmp(sp, var_args_list[i].name) == 0) {
340 if (flags == 1) {
341 node->options |= var_args_list[i].mask;
342 break;
343 } else if (flags == 2) {
344 node->options &= ~var_args_list[i].mask;
345 break;
346 }
347 }
348 }
349 sp = NULL;
350 if (end)
351 break;
352 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100353 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100354 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100355 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100356}
357
358/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100359 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
360 * must pass the args part in the <arg> pointer with its length in <arg_len>,
361 * and varname with its length in <var> and <var_len> respectively. <arg> is
362 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100363 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100364 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100365int 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 +0100366{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100367 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200368 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100369
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100370 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
371 if (strlen(logformat_keywords[j].name) == var_len &&
372 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
373 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200374 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100375 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100376 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200377 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100378 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100379 node->type = logformat_keywords[j].type;
380 node->options = *defoptions;
381 if (arg_len) {
382 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100383 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200384 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100385 }
386 if (node->type == LOG_FMT_GLOBAL) {
387 *defoptions = node->options;
388 free(node->arg);
389 free(node);
390 } else {
391 if (logformat_keywords[j].config_callback &&
392 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200393 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100394 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100395 curproxy->to_log |= logformat_keywords[j].lw;
Willy Tarreau2b718102021-04-21 07:32:39 +0200396 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100397 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100398 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100399 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
400 curproxy->conf.args.file, curproxy->conf.args.line,
401 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100402 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100403 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100404 memprintf(err, "format variable '%s' is reserved for HTTP mode",
405 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200406 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100407 }
William Lallemand723b73a2012-02-08 16:37:49 +0100408 }
409 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100410
411 j = var[var_len];
412 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100413 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 +0100414 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200415
416 error_free:
417 if (node) {
418 free(node->arg);
419 free(node);
420 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100421 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100422}
423
424/*
425 * push to the logformat linked list
426 *
427 * start: start pointer
428 * end: end text pointer
429 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100430 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100431 *
432 * LOG_TEXT: copy chars from start to end excluding end.
433 *
434*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100435int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100436{
437 char *str;
438
Willy Tarreaua3571662012-12-20 21:59:12 +0100439 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200440 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100441 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100442 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100443 return 0;
444 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200445 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100446 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100447 str[end - start] = '\0';
448 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100449 node->type = LOG_FMT_TEXT; // type string
Willy Tarreau2b718102021-04-21 07:32:39 +0200450 LIST_APPEND(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100451 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200452 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100453 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100454 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100455 return 0;
456 }
William Lallemand1d705562012-03-12 12:46:41 +0100457 node->type = LOG_FMT_SEPARATOR;
Willy Tarreau2b718102021-04-21 07:32:39 +0200458 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100459 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100460 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100461}
462
463/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100464 * Parse the sample fetch expression <text> and add a node to <list_format> upon
465 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100466 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
467 * is passed in <endptr>, it will be updated with the pointer to the first character
468 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100469 *
470 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100471 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100472int 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 +0100473{
474 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200475 struct sample_expr *expr = NULL;
476 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100477 int cmd_arg;
478
479 cmd[0] = text;
480 cmd[1] = "";
481 cmd_arg = 0;
482
Christopher Faulet8551c342021-09-30 16:22:51 +0200483 expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err,
484 (curpx->cap & PR_CAP_DEF) ? NULL: &curpx->conf.args, endptr);
Willy Tarreauc8368452012-12-21 00:09:23 +0100485 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100486 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200487 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100488 }
489
Vincent Bernat02779b62016-04-03 13:48:43 +0200490 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100491 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100492 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200493 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100494 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100495 node->type = LOG_FMT_EXPR;
496 node->expr = expr;
497 node->options = options;
498
499 if (arg_len) {
500 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100501 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200502 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100503 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100504 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100505 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
506
Willy Tarreau434c57c2013-01-08 01:10:24 +0100507 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100508 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
509
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100510 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100511 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
512 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200513 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100514 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100515
Christopher Faulet581db2b2021-03-26 10:02:46 +0100516 if ((options & LOG_OPT_HTTP) && (expr->fetch->use & (SMP_USE_L6REQ|SMP_USE_L6RES))) {
517 ha_warning("parsing [%s:%d] : L6 sample fetch <%s> ignored in HTTP log-format string.\n",
518 curpx->conf.args.file, curpx->conf.args.line, text);
519 }
520
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200521 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100522 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100523 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100524
William Lallemand65ad6e12014-01-31 15:08:02 +0100525 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
526 * needed with some sample fetches (eg: ssl*). We always set it for
527 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100528 */
529 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200530 if (curpx->http_needed)
531 curpx->to_log |= LW_REQ;
Willy Tarreau2b718102021-04-21 07:32:39 +0200532 LIST_APPEND(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100533 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200534
535 error_free:
536 release_sample_expr(expr);
537 if (node) {
538 free(node->arg);
539 free(node);
540 }
541 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100542}
543
544/*
William Lallemand723b73a2012-02-08 16:37:49 +0100545 * Parse the log_format string and fill a linked list.
546 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200547 * You can set arguments using { } : %{many arguments}varname.
548 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100549 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500550 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100551 * curproxy: the proxy affected
552 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100553 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100554 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100555 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100556 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100557 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100558int 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 +0100559{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100560 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100561 char *arg = NULL; /* start pointer for args */
562 char *var = NULL; /* start pointer for vars */
563 int arg_len = 0;
564 int var_len = 0;
565 int cformat; /* current token format */
566 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100567 struct logformat_node *tmplf, *back;
568
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100569 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100570 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100571 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100572 return 0;
573 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200574 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200575
William Lallemand723b73a2012-02-08 16:37:49 +0100576 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100577 list_for_each_entry_safe(tmplf, back, list_format, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200578 LIST_DELETE(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200579 release_sample_expr(tmplf->expr);
580 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100581 free(tmplf);
582 }
583
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100584 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100585 pformat = cformat;
586
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100587 if (!*str)
588 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100589
Joseph Herlant85b40592018-11-15 12:10:04 -0800590 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100591 * second have all common paths processed at one place. The common paths are the ones
592 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
593 * We use the common LF_INIT state to dispatch to the different final states.
594 */
595 switch (pformat) {
596 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100597 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100598 arg_len = var_len = 0;
599 if (*str == '{') { // optional argument
600 cformat = LF_STARG;
601 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100602 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100603 else if (*str == '[') {
604 cformat = LF_STEXPR;
605 var = str + 1; // store expr in variable name
606 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100607 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100608 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100609 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100610 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100611 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500612 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100613 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100614 /* single '%' followed by blank or digit, send them both */
615 cformat = LF_TEXT;
616 pformat = LF_TEXT; /* finally we include the previous char as well */
617 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600618 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 +0100619 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100620 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100621
622 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100623 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500624 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100625 break;
626
627 case LF_STARG: // text immediately following '%{'
628 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100629 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100630 arg_len = str - arg;
631 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100632 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100633 break;
634
635 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100636 if (*str == '[') {
637 cformat = LF_STEXPR;
638 var = str + 1; // store expr in variable name
639 break;
640 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100641 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100642 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100643 var = str;
644 break;
645 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100646 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100647 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100648
Willy Tarreauc8368452012-12-21 00:09:23 +0100649 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100650 /* the whole sample expression is parsed at once,
651 * returning the pointer to the first character not
652 * part of the expression, which MUST be the trailing
653 * angle bracket.
654 */
655 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
656 goto fail;
657
658 if (*str == ']') {
659 // end of arg, go on with next state
660 cformat = pformat = LF_EDEXPR;
661 sp = str;
662 }
663 else {
664 char c = *str;
665 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100666 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100667 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
668 else
669 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200670 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100671 }
672 break;
673
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100674 case LF_VAR: // text part of a variable name
675 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100676 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100677 cformat = LF_INIT; // not variable name anymore
678 break;
679
Willy Tarreauc8368452012-12-21 00:09:23 +0100680 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100681 cformat = LF_INIT;
682 }
683
684 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
685 switch (*str) {
686 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100687 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200688 case ' ':
689 if (options & LOG_OPT_MERGE_SPACES) {
690 cformat = LF_SEPARATOR;
691 break;
692 }
693 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100694 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100695 }
696 }
697
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100698 if (cformat != pformat || pformat == LF_SEPARATOR) {
699 switch (pformat) {
700 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100701 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100702 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100703 break;
704 case LF_TEXT:
705 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100706 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100707 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100708 break;
709 }
710 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100711 }
712 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100713
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100714 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100715 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100716 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100717 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100718 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100719
720 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100721 fail:
722 free(backfmt);
723 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100724}
725
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200726/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500727 * 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 +0200728 * ranges of indexes. Note that an index may be considered as a particular range
729 * with a high limit to the low limit.
730 */
731int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
732{
733 char *end, *p;
734
735 *low = *high = 0;
736
737 p = *arg;
738 end = strchr(p, ',');
739 if (!end)
740 end = p + strlen(p);
741
742 *high = *low = read_uint((const char **)&p, end);
743 if (!*low || (p != end && *p != '-'))
744 goto err;
745
746 if (p == end)
747 goto done;
748
749 p++;
750 *high = read_uint((const char **)&p, end);
751 if (!*high || *high <= *low || p != end)
752 goto err;
753
754 done:
755 if (*end == ',')
756 end++;
757 *arg = end;
758 return 1;
759
760 err:
761 memprintf(err, "wrong sample range '%s'", *arg);
762 return 0;
763}
764
765/*
766 * Returns 1 if the range defined by <low> and <high> overlaps
767 * one of them in <rgs> array of ranges with <sz> the size of this
768 * array, 0 if not.
769 */
770int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
771 unsigned int low, unsigned int high, char **err)
772{
773 size_t i;
774
775 for (i = 0; i < sz; i++) {
776 if ((low >= rgs[i].low && low <= rgs[i].high) ||
777 (high >= rgs[i].low && high <= rgs[i].high)) {
778 memprintf(err, "ranges are overlapping");
779 return 1;
780 }
781 }
782
783 return 0;
784}
785
786int smp_log_range_cmp(const void *a, const void *b)
787{
788 const struct smp_log_range *rg_a = a;
789 const struct smp_log_range *rg_b = b;
790
791 if (rg_a->high < rg_b->low)
792 return -1;
793 else if (rg_a->low > rg_b->high)
794 return 1;
795
796 return 0;
797}
798
799/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200800 * Parse "log" keyword and update <logsrvs> list accordingly.
801 *
802 * When <do_del> is set, it means the "no log" line was parsed, so all log
803 * servers in <logsrvs> are released.
804 *
805 * Otherwise, we try to parse the "log" line. First of all, when the list is not
806 * the global one, we look for the parameter "global". If we find it,
807 * global.logsrvs is copied. Else we parse each arguments.
808 *
809 * The function returns 1 in success case, otherwise, it returns 0 and err is
810 * filled.
811 */
Emeric Brun9533a702021-04-02 10:13:43 +0200812int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200813{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100814 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200815 struct sockaddr_storage *sk;
Emeric Brun94aab062021-04-02 10:41:36 +0200816 struct protocol *proto;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200817 struct logsrv *logsrv = NULL;
818 int port1, port2;
819 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200820 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200821
822 /*
823 * "no log": delete previous herited or defined syslog
824 * servers.
825 */
826 if (do_del) {
827 struct logsrv *back;
828
829 if (*(args[1]) != 0) {
830 memprintf(err, "'no log' does not expect arguments");
831 goto error;
832 }
833
834 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200835 LIST_DELETE(&logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200836 free(logsrv);
837 }
838 return 1;
839 }
840
841 /*
842 * "log global": copy global.logrsvs linked list to the end of logsrvs
843 * list. But first, we check (logsrvs != global.logsrvs).
844 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100845 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200846 if (logsrvs == &global.logsrvs) {
847 memprintf(err, "'global' is not supported for a global syslog server");
848 goto error;
849 }
850 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200851 struct logsrv *node;
852
853 list_for_each_entry(node, logsrvs, list) {
854 if (node->ref == logsrv)
855 goto skip_logsrv;
856 }
857
858 node = malloc(sizeof(*node));
Aurelien DARRAGON4ba4a212023-05-11 18:57:23 +0200859 if (!node) {
860 memprintf(err, "out of memory error");
861 goto error;
862 }
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200863 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200864 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200865 LIST_INIT(&node->list);
Willy Tarreau2b718102021-04-21 07:32:39 +0200866 LIST_APPEND(logsrvs, &node->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200867 node->conf.file = strdup(file);
868 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200869
870 skip_logsrv:
871 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200872 }
873 return 1;
874 }
875
876 /*
877 * "log <address> ...: parse a syslog server line
878 */
879 if (*(args[1]) == 0 || *(args[2]) == 0) {
880 memprintf(err, "expects <address> and <facility> %s as arguments",
881 ((logsrvs == &global.logsrvs) ? "" : "or global"));
882 goto error;
883 }
884
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100885 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
886 if (strcmp(args[1], "stdout") == 0)
887 args[1] = "fd@1";
888 else if (strcmp(args[1], "stderr") == 0)
889 args[1] = "fd@2";
890
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200891 logsrv = calloc(1, sizeof(*logsrv));
892 if (!logsrv) {
893 memprintf(err, "out of memory");
894 goto error;
895 }
896
Emeric Brun9533a702021-04-02 10:13:43 +0200897 logsrv->conf.file = strdup(file);
898 logsrv->conf.line = linenum;
899
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200900 /* skip address for now, it will be parsed at the end */
901 cur_arg = 2;
902
903 /* just after the address, a length may be specified */
904 logsrv->maxlen = MAX_SYSLOG_LEN;
905 if (strcmp(args[cur_arg], "len") == 0) {
906 int len = atoi(args[cur_arg+1]);
907 if (len < 80 || len > 65535) {
908 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
909 args[cur_arg+1]);
910 goto error;
911 }
912 logsrv->maxlen = len;
913 cur_arg += 2;
914 }
915 if (logsrv->maxlen > global.max_syslog_len)
916 global.max_syslog_len = logsrv->maxlen;
917
918 /* after the length, a format may be specified */
919 if (strcmp(args[cur_arg], "format") == 0) {
920 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200921 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200922 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
923 goto error;
924 }
925 cur_arg += 2;
926 }
927
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200928 if (strcmp(args[cur_arg], "sample") == 0) {
929 unsigned low, high;
930 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200931 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
932
933 p = args[cur_arg+1];
934 smp_sz_str = strchr(p, ':');
935 if (!smp_sz_str) {
936 memprintf(err, "Missing sample size");
937 goto error;
938 }
939
940 *smp_sz_str++ = '\0';
941
942 end = p + strlen(p);
943
944 while (p != end) {
945 if (!get_logsrv_smp_range(&low, &high, &p, err))
946 goto error;
947
948 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
949 goto error;
950
951 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
952 if (!smp_rgs) {
953 memprintf(err, "out of memory error");
954 goto error;
955 }
956
957 smp_rgs[smp_rgs_sz].low = low;
958 smp_rgs[smp_rgs_sz].high = high;
959 smp_rgs[smp_rgs_sz].sz = high - low + 1;
960 smp_rgs[smp_rgs_sz].curr_idx = 0;
961 if (smp_rgs[smp_rgs_sz].high > smp_sz)
962 smp_sz = smp_rgs[smp_rgs_sz].high;
963 smp_rgs_sz++;
964 }
965
Tim Duesterhus21648002019-06-23 22:10:10 +0200966 if (smp_rgs == NULL) {
967 memprintf(err, "no sampling ranges given");
968 goto error;
969 }
970
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200971 beg = smp_sz_str;
972 end = beg + strlen(beg);
973 new_smp_sz = read_uint((const char **)&beg, end);
974 if (!new_smp_sz || beg != end) {
975 memprintf(err, "wrong sample size '%s' for sample range '%s'",
976 smp_sz_str, args[cur_arg+1]);
977 goto error;
978 }
979
980 if (new_smp_sz < smp_sz) {
981 memprintf(err, "sample size %zu should be greater or equal to "
982 "%zu the maximum of the high ranges limits",
983 new_smp_sz, smp_sz);
984 goto error;
985 }
986 smp_sz = new_smp_sz;
987
988 /* Let's order <smp_rgs> array. */
989 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
990
991 logsrv->lb.smp_rgs = smp_rgs;
992 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
993 logsrv->lb.smp_sz = smp_sz;
994
995 cur_arg += 2;
996 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200997 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200998 /* parse the facility */
999 logsrv->facility = get_log_facility(args[cur_arg]);
1000 if (logsrv->facility < 0) {
1001 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
1002 goto error;
1003 }
1004 cur_arg++;
1005
1006 /* parse the max syslog level (default: debug) */
1007 logsrv->level = 7;
1008 if (*(args[cur_arg])) {
1009 logsrv->level = get_log_level(args[cur_arg]);
1010 if (logsrv->level < 0) {
1011 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
1012 goto error;
1013 }
1014 cur_arg++;
1015 }
1016
1017 /* parse the limit syslog level (default: emerg) */
1018 logsrv->minlvl = 0;
1019 if (*(args[cur_arg])) {
1020 logsrv->minlvl = get_log_level(args[cur_arg]);
1021 if (logsrv->minlvl < 0) {
1022 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1023 goto error;
1024 }
1025 cur_arg++;
1026 }
1027
1028 /* Too many args */
1029 if (*(args[cur_arg])) {
1030 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1031 goto error;
1032 }
1033
1034 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001035 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001036 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001037 logsrv->addr.ss_family = AF_UNSPEC;
1038 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001039 logsrv->sink = NULL;
1040 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001041 goto done;
1042 }
1043
Emeric Brun94aab062021-04-02 10:41:36 +02001044 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
1045 err, NULL, NULL,
1046 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 +02001047 if (!sk)
1048 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001049
1050 if (fd != -1)
1051 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001052 logsrv->addr = *sk;
1053
1054 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001055 if (!port1)
1056 set_host_port(&logsrv->addr, SYSLOG_PORT);
1057 }
Emeric Brun9533a702021-04-02 10:13:43 +02001058
Emeric Brun26754902021-04-07 14:26:44 +02001059 if (proto && proto->ctrl_type == SOCK_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +02001060 static unsigned long ring_ids;
1061
1062 /* Implicit sink buffer will be
1063 * initialized in post_check
1064 */
1065 logsrv->type = LOG_TARGET_BUFFER;
1066 logsrv->sink = NULL;
1067 /* compute uniq name for the ring */
1068 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
1069 }
1070
Willy Tarreauc046d162019-08-30 15:24:59 +02001071 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001072 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001073 return 1;
1074
1075 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001076 free(smp_rgs);
Emeric Brun9533a702021-04-02 10:13:43 +02001077 if (logsrv) {
1078 free(logsrv->conf.file);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001079 free(logsrv->ring_name);
Emeric Brun9533a702021-04-02 10:13:43 +02001080 }
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001081 free(logsrv);
1082 return 0;
1083}
1084
1085
Christopher Fauletd4696382017-10-24 11:44:05 +02001086/* Generic function to display messages prefixed by a label */
1087static void print_message(const char *label, const char *fmt, va_list argp)
1088{
Christopher Fauletd4696382017-10-24 11:44:05 +02001089 char *head, *msg;
Willy Tarreau9f903af2021-05-07 08:42:39 +02001090 char prefix[11]; // '[' + 8 chars + ']' + 0.
Christopher Fauletd4696382017-10-24 11:44:05 +02001091
Willy Tarreau9f903af2021-05-07 08:42:39 +02001092 *prefix = '[';
1093 strncpy(prefix + 1, label, sizeof(prefix) - 2);
1094 msg = prefix + strlen(prefix);
1095 *msg++ = ']';
1096 while (msg < prefix + sizeof(prefix) - 1)
1097 *msg++ = ' ';
1098 *msg = 0;
Christopher Fauletd4696382017-10-24 11:44:05 +02001099
Willy Tarreau9f903af2021-05-07 08:42:39 +02001100 head = msg = NULL;
1101 memprintf(&head, "%s (%u) : ", prefix, (uint)getpid());
Christopher Fauletd4696382017-10-24 11:44:05 +02001102 memvprintf(&msg, fmt, argp);
1103
Willy Tarreau869efd52019-11-15 15:16:57 +01001104 if (global.mode & MODE_STARTING) {
1105 if (unlikely(!startup_logs))
1106 startup_logs = ring_new(STARTUP_LOG_SIZE);
1107
1108 if (likely(startup_logs)) {
1109 struct ist m[2];
1110
1111 m[0] = ist(head);
1112 m[1] = ist(msg);
1113 /* trim the trailing '\n' */
1114 if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
1115 m[1].len--;
1116 ring_write(startup_logs, ~0, 0, 0, m, 2);
1117 }
1118 }
Christopher Fauletd4696382017-10-24 11:44:05 +02001119
1120 fprintf(stderr, "%s%s", head, msg);
1121 fflush(stderr);
1122
1123 free(head);
1124 free(msg);
1125}
1126
Willy Tarreaubaaee002006-06-26 02:48:02 +02001127/*
1128 * Displays the message on stderr with the date and pid. Overrides the quiet
1129 * mode during startup.
1130 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001131void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001132{
1133 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001134
1135 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaubb869862020-04-16 10:52:41 +02001136 if (!(warned & WARN_EXEC_PATH)) {
1137 const char *path = get_exec_path();
1138
1139 warned |= WARN_EXEC_PATH;
1140 ha_notice("haproxy version is %s\n", haproxy_version);
1141 if (path)
1142 ha_notice("path to executable is %s\n", path);
1143 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001144 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001145 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001146 va_end(argp);
1147 }
1148}
1149
1150
1151/*
1152 * Displays the message on stderr with the date and pid.
1153 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001154void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001155{
1156 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001157
Willy Tarreaubebd2122020-04-15 16:06:11 +02001158 warned |= WARN_ANY;
1159
Willy Tarreaubaaee002006-06-26 02:48:02 +02001160 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1161 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001162 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001163 va_end(argp);
1164 }
1165}
1166
1167/*
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001168 * Variant of _ha_diag_warning with va_list.
1169 * Use it only if MODE_DIAG has been previously checked.
1170 */
1171void _ha_vdiag_warning(const char *fmt, va_list argp)
1172{
Willy Tarreau9f903af2021-05-07 08:42:39 +02001173 print_message("DIAG", fmt, argp);
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001174}
1175
1176/*
1177 * Output a diagnostic warning.
1178 * Use it only if MODE_DIAG has been previously checked.
1179 */
1180void _ha_diag_warning(const char *fmt, ...)
1181{
1182 va_list argp;
1183
1184 va_start(argp, fmt);
1185 _ha_vdiag_warning(fmt, argp);
1186 va_end(argp);
1187}
1188
1189/*
1190 * Output a diagnostic warning. Do nothing of MODE_DIAG is not on.
1191 */
1192void ha_diag_warning(const char *fmt, ...)
1193{
1194 va_list argp;
1195
1196 if (global.mode & MODE_DIAG) {
1197 va_start(argp, fmt);
1198 _ha_vdiag_warning(fmt, argp);
1199 va_end(argp);
1200 }
1201}
1202
1203/*
William Lallemand9c56a222018-11-21 18:04:52 +01001204 * Displays the message on stderr with the date and pid.
1205 */
1206void ha_notice(const char *fmt, ...)
1207{
1208 va_list argp;
1209
1210 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1211 va_start(argp, fmt);
1212 print_message("NOTICE", fmt, argp);
1213 va_end(argp);
1214 }
1215}
1216
1217/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001218 * Displays the message on <out> only if quiet mode is not set.
1219 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001220void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001221{
1222 va_list argp;
1223
1224 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1225 va_start(argp, fmt);
1226 vfprintf(out, fmt, argp);
1227 fflush(out);
1228 va_end(argp);
1229 }
1230}
1231
1232/*
Emeric Brun54648852020-07-06 15:54:06 +02001233 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001234 */
Emeric Brun54648852020-07-06 15:54:06 +02001235enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001236{
Emeric Brun54648852020-07-06 15:54:06 +02001237 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001238
1239 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001240 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001241 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001242 format--;
1243
Emeric Brun54648852020-07-06 15:54:06 +02001244 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001245 return format;
1246}
1247
1248/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001249 * returns log level for <lev> or -1 if not found.
1250 */
1251int get_log_level(const char *lev)
1252{
1253 int level;
1254
1255 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001256 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001257 level--;
1258
1259 return level;
1260}
1261
Willy Tarreaubaaee002006-06-26 02:48:02 +02001262/*
1263 * returns log facility for <fac> or -1 if not found.
1264 */
1265int get_log_facility(const char *fac)
1266{
1267 int facility;
1268
1269 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001270 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001271 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001272
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273 return facility;
1274}
1275
William Lallemanda1cc3812012-02-08 16:38:44 +01001276/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001277 * Encode the string.
1278 *
1279 * When using the +E log format option, it will try to escape '"\]'
1280 * characters with '\' as prefix. The same prefix should not be used as
1281 * <escape>.
1282 */
1283static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001284 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001285 const char *string,
1286 struct logformat_node *node)
1287{
1288 if (node->options & LOG_OPT_ESC) {
1289 if (start < stop) {
1290 stop--; /* reserve one byte for the final '\0' */
1291 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001292 if (!ha_bit_test((unsigned char)(*string), map)) {
1293 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001294 *start++ = *string;
1295 else {
1296 if (start + 2 >= stop)
1297 break;
1298 *start++ = '\\';
1299 *start++ = *string;
1300 }
1301 }
1302 else {
1303 if (start + 3 >= stop)
1304 break;
1305 *start++ = escape;
1306 *start++ = hextab[(*string >> 4) & 15];
1307 *start++ = hextab[*string & 15];
1308 }
1309 string++;
1310 }
1311 *start = '\0';
1312 }
1313 }
1314 else {
1315 return encode_string(start, stop, escape, map, string);
1316 }
1317
1318 return start;
1319}
1320
1321/*
1322 * Encode the chunk.
1323 *
1324 * When using the +E log format option, it will try to escape '"\]'
1325 * characters with '\' as prefix. The same prefix should not be used as
1326 * <escape>.
1327 */
1328static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001329 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001330 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001331 struct logformat_node *node)
1332{
1333 char *str, *end;
1334
1335 if (node->options & LOG_OPT_ESC) {
1336 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001337 str = chunk->area;
1338 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001339
1340 stop--; /* reserve one byte for the final '\0' */
1341 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001342 if (!ha_bit_test((unsigned char)(*str), map)) {
1343 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001344 *start++ = *str;
1345 else {
1346 if (start + 2 >= stop)
1347 break;
1348 *start++ = '\\';
1349 *start++ = *str;
1350 }
1351 }
1352 else {
1353 if (start + 3 >= stop)
1354 break;
1355 *start++ = escape;
1356 *start++ = hextab[(*str >> 4) & 15];
1357 *start++ = hextab[*str & 15];
1358 }
1359 str++;
1360 }
1361 *start = '\0';
1362 }
1363 }
1364 else {
1365 return encode_chunk(start, stop, escape, map, chunk);
1366 }
1367
1368 return start;
1369}
1370
1371/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001372 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001373 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001374 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001375 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001376 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001377char *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 +01001378{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001379 if (size < 2)
1380 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001381
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001382 if (node->options & LOG_OPT_QUOTE) {
1383 *(dst++) = '"';
1384 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001385 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001386
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001387 if (src && len) {
Aurelien DARRAGONa5c621a2022-09-20 14:33:18 +02001388 /* escape_string and strlcpy2 will both try to add terminating NULL-byte
Aurelien DARRAGON70804572024-04-09 11:15:23 +02001389 * to dst
Aurelien DARRAGONa5c621a2022-09-20 14:33:18 +02001390 */
Dragan Dosen835b9212016-02-12 13:23:03 +01001391 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001392 char *ret;
1393
Aurelien DARRAGON70804572024-04-09 11:15:23 +02001394 ret = escape_string(dst, dst + size, '\\', rfc5424_escape_map, src, src + len);
Dragan Dosen835b9212016-02-12 13:23:03 +01001395 if (ret == NULL || *ret != '\0')
1396 return NULL;
1397 len = ret - dst;
1398 }
1399 else {
Aurelien DARRAGONa5c621a2022-09-20 14:33:18 +02001400 if (++len > size)
1401 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001402 len = strlcpy2(dst, src, len);
1403 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001404
1405 size -= len;
1406 dst += len;
1407 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001408 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1409 if (size < 2)
1410 return NULL;
1411 *(dst++) = '-';
Aurelien DARRAGONa5c621a2022-09-20 14:33:18 +02001412 size -= 1;
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001413 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001414
1415 if (node->options & LOG_OPT_QUOTE) {
1416 if (size < 2)
1417 return NULL;
1418 *(dst++) = '"';
1419 }
1420
1421 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001422 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001423}
1424
Willy Tarreau26ffa852018-09-05 15:23:10 +02001425static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001426{
1427 return lf_text_len(dst, src, size, size, node);
1428}
1429
William Lallemand5f232402012-04-05 18:02:55 +02001430/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001431 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001432 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001433 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001434char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001435{
1436 char *ret = dst;
1437 int iret;
1438 char pn[INET6_ADDRSTRLEN];
1439
1440 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001441 unsigned char *addr = NULL;
1442 switch (sockaddr->sa_family) {
1443 case AF_INET:
1444 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1445 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1446 break;
1447 case AF_INET6:
1448 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1449 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1450 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1451 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1452 break;
1453 default:
1454 return NULL;
1455 }
William Lallemand5f232402012-04-05 18:02:55 +02001456 if (iret < 0 || iret > size)
1457 return NULL;
1458 ret += iret;
1459 } else {
1460 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1461 ret = lf_text(dst, pn, size, node);
1462 if (ret == NULL)
1463 return NULL;
1464 }
1465 return ret;
1466}
1467
1468/*
1469 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001470 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001471 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001472char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001473{
1474 char *ret = dst;
1475 int iret;
1476
1477 if (node->options & LOG_OPT_HEXA) {
1478 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1479 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1480 if (iret < 0 || iret > size)
1481 return NULL;
1482 ret += iret;
1483 } else {
1484 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1485 if (ret == NULL)
1486 return NULL;
1487 }
1488 return ret;
1489}
1490
Emeric Brun54648852020-07-06 15:54:06 +02001491
1492/*
1493 * This function sends the syslog message using a printf format string. It
1494 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001495 */
Emeric Brun54648852020-07-06 15:54:06 +02001496void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001497{
Emeric Brun54648852020-07-06 15:54:06 +02001498 va_list argp;
1499 int data_len;
1500
1501 if (level < 0 || format == NULL || logline == NULL)
1502 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001503
Emeric Brun54648852020-07-06 15:54:06 +02001504 va_start(argp, format);
1505 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1506 if (data_len < 0 || data_len > global.max_syslog_len)
1507 data_len = global.max_syslog_len;
1508 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001509
Emeric Brun54648852020-07-06 15:54:06 +02001510 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1511 logline, data_len, default_rfc5424_sd_log_format, 2);
1512}
1513/*
1514 * This function builds a log header of given format using given
1515 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1516 * to determine format based on given metadas. It is useful
1517 * for log-forwarding to be able to forward any format without
1518 * settings.
1519 * This function returns a struct ist array of elements of the header
1520 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001521 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001522 * elements.
1523 */
1524struct ist *build_log_header(enum log_fmt format, int level, int facility,
1525 struct ist *metadata, size_t *nbelem)
1526{
1527 static THREAD_LOCAL struct {
1528 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1529 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1530 time_t cur_legacy_time;
1531 char priority_buffer[6];
1532 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001533
Emeric Brun54648852020-07-06 15:54:06 +02001534 struct tm logtime;
1535 int len;
1536 int fac_level = 0;
1537 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001538
Emeric Brun54648852020-07-06 15:54:06 +02001539 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001540
Emeric Brun54648852020-07-06 15:54:06 +02001541
1542 if (format == LOG_FORMAT_UNSPEC) {
1543 format = LOG_FORMAT_RAW;
1544 if (metadata) {
1545 /* If a hostname is set, it appears we want to perform syslog
1546 * because only rfc5427 or rfc3164 support an hostname.
1547 */
1548 if (metadata[LOG_META_HOST].len) {
1549 /* If a rfc5424 compliant timestamp is used we consider
1550 * that output format is rfc5424, else legacy format
1551 * is used as specified default for local logs
1552 * in documentation.
1553 */
1554 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1555 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1556 format = LOG_FORMAT_RFC5424;
1557 else
1558 format = LOG_FORMAT_RFC3164;
1559 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001560 else if (metadata[LOG_META_TAG].len) {
1561 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001562 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001563 * in legacy format (analog to RFC3164 but
1564 * with stripped hostname).
1565 */
1566 format = LOG_FORMAT_LOCAL;
1567 }
Emeric Brun54648852020-07-06 15:54:06 +02001568 else if (metadata[LOG_META_PRIO].len) {
1569 /* the source seems a parsed message
1570 * offering a valid level/prio prefix
1571 * so we consider this format.
1572 */
1573 format = LOG_FORMAT_PRIO;
1574 }
1575 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001576 }
1577
Emeric Brun54648852020-07-06 15:54:06 +02001578 /* prepare priority, stored into 1 single elem */
1579 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001580 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001581 case LOG_FORMAT_RFC3164:
1582 case LOG_FORMAT_RFC5424:
1583 case LOG_FORMAT_PRIO:
1584 fac_level = facility << 3;
1585 /* further format ignore the facility */
1586 /* fall through */
1587 case LOG_FORMAT_TIMED:
1588 case LOG_FORMAT_SHORT:
1589 fac_level += level;
1590 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1591 do {
1592 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1593 fac_level /= 10;
1594 hdr_ctx.ist_vector[*nbelem].ptr--;
1595 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1596 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1597 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1598 break;
1599 case LOG_FORMAT_ISO:
1600 case LOG_FORMAT_RAW:
1601 break;
1602 case LOG_FORMAT_UNSPEC:
1603 case LOG_FORMATS:
1604 ABORT_NOW();
1605 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001606
William Lallemand2a4a44f2012-02-06 16:00:33 +01001607
Emeric Brun54648852020-07-06 15:54:06 +02001608 /* prepare timestamp, stored into a max of 4 elems */
1609 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001610 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001611 case LOG_FORMAT_RFC3164:
1612 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1613 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1614 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1615 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001616 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001617 break;
1618 }
1619 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1620 int month;
1621 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001622
Emeric Brun54648852020-07-06 15:54:06 +02001623 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001624
Emeric Brun54648852020-07-06 15:54:06 +02001625 /* compute month */
1626 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1627 if (month)
1628 month--;
1629 if (month <= 11) {
1630 /* builds log prefix ex: 'Jan 1 ' */
1631 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1632 "%s %c%c ", monthname[month],
1633 timestamp[8] != '0' ? timestamp[8] : ' ',
1634 timestamp[9]);
1635 /* we reused the timestamp_buffer, signal that it does not
1636 * contain local time anymore
1637 */
1638 hdr_ctx.cur_legacy_time = 0;
1639 if (len == 7) {
1640 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1641 /* adds 'HH:MM:SS' from iso time */
1642 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1643 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1644 /* we successfully reuse iso time, we can break */
1645 break;
1646 }
1647 }
1648 /* Failed to reuse isotime time, fallback to local legacy time */
1649 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001650
Emeric Brun54648852020-07-06 15:54:06 +02001651 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1652 /* re-builds timestamp from the current local time */
1653 get_localtime(time, &logtime);
1654
1655 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1656 "%s %2d %02d:%02d:%02d ",
1657 monthname[logtime.tm_mon],
1658 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1659 if (len != LOG_LEGACYTIME_LEN+1)
1660 hdr_ctx.cur_legacy_time = 0;
1661 else
1662 hdr_ctx.cur_legacy_time = time;
1663 }
1664 if (likely(hdr_ctx.cur_legacy_time))
1665 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1666 else
1667 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1668 break;
1669 case LOG_FORMAT_RFC5424:
1670 /* adds rfc5425 version prefix */
1671 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1672 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001673 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001674 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1675 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1676 break;
1677 }
1678 /* let continue as 'timed' and 'iso' format for usual timestamp */
1679 /* fall through */
1680 case LOG_FORMAT_TIMED:
1681 case LOG_FORMAT_ISO:
1682 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1683 * '1900:01:01T14:00:00+02:00'
1684 * '1900:01:01T10:00:00.123456-02:00'
1685 */
1686 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1687 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1688 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001689 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001690 break;
1691 }
1692 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1693 int month;
1694 char *timestamp = metadata[LOG_META_TIME].ptr;
1695
1696 for (month = 0; month < 12; month++)
1697 if (!memcmp(monthname[month], timestamp, 3))
1698 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001699
Emeric Brun54648852020-07-06 15:54:06 +02001700 if (month < 12) {
1701
1702 /* get local time to retrieve year */
1703 get_localtime(time, &logtime);
1704
1705 /* year seems changed since log */
1706 if (logtime.tm_mon < month)
1707 logtime.tm_year--;
1708
1709 /* builds rfc5424 prefix ex: '1900-01-01T' */
1710 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1711 "%4d-%02d-%c%cT",
1712 logtime.tm_year+1900, month+1,
1713 timestamp[4] != ' ' ? timestamp[4] : '0',
1714 timestamp[5]);
1715
1716 /* we reused the timestamp_buffer, signal that it does not
1717 * contain local time anymore
1718 */
1719 hdr_ctx.cur_legacy_time = 0;
1720 if (len == 11) {
1721 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1722 /* adds HH:MM:SS from legacy timestamp */
1723 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001724 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001725 /* according to rfc: -00:00 means we don't know the timezone */
1726 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1727 /* we successfully reuse legacy time, we can break */
1728 break;
1729 }
1730 }
1731 /* Failed to reuse legacy time, fallback to local iso time */
1732 }
1733 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1734 break;
1735 case LOG_FORMAT_PRIO:
1736 case LOG_FORMAT_SHORT:
1737 case LOG_FORMAT_RAW:
1738 break;
1739 case LOG_FORMAT_UNSPEC:
1740 case LOG_FORMATS:
1741 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001742 }
1743
Emeric Brun54648852020-07-06 15:54:06 +02001744 /* prepare other meta data, stored into a max of 10 elems */
1745 switch (format) {
1746 case LOG_FORMAT_RFC3164:
1747 if (metadata && metadata[LOG_META_HOST].len) {
1748 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1749 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1750 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001751 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001752 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001753 /* fall through */
1754 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001755 if (!metadata || !metadata[LOG_META_TAG].len)
1756 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001757
Emeric Brun54648852020-07-06 15:54:06 +02001758 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1759 if (metadata[LOG_META_PID].len) {
1760 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1761 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1762 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1763 }
1764 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1765 break;
1766 case LOG_FORMAT_RFC5424:
1767 if (metadata && metadata[LOG_META_HOST].len) {
1768 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1769 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1770 }
1771 else
1772 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001773
Emeric Brun54648852020-07-06 15:54:06 +02001774 if (metadata && metadata[LOG_META_TAG].len) {
1775 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1776 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1777 }
1778 else
1779 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001780
Emeric Brun54648852020-07-06 15:54:06 +02001781 if (metadata && metadata[LOG_META_PID].len) {
1782 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1783 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1784 }
1785 else
1786 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001787
Emeric Brun54648852020-07-06 15:54:06 +02001788 if (metadata && metadata[LOG_META_MSGID].len) {
1789 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1790 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1791 }
1792 else
1793 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001794
Emeric Brun54648852020-07-06 15:54:06 +02001795 if (metadata && metadata[LOG_META_STDATA].len) {
1796 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1797 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1798 }
1799 else
1800 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1801 break;
1802 case LOG_FORMAT_PRIO:
1803 case LOG_FORMAT_SHORT:
1804 case LOG_FORMAT_TIMED:
1805 case LOG_FORMAT_ISO:
1806 case LOG_FORMAT_RAW:
1807 break;
1808 case LOG_FORMAT_UNSPEC:
1809 case LOG_FORMATS:
1810 ABORT_NOW();
1811 }
1812
1813 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001814}
1815
1816/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001817 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001818 * The argument <metadata> MUST be an array of size
1819 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001820 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001821 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001822 */
Emeric Brun54648852020-07-06 15:54:06 +02001823static 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 +01001824{
Emeric Brun54648852020-07-06 15:54:06 +02001825 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001826 static THREAD_LOCAL struct msghdr msghdr = {
1827 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001828 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001829 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001830 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1831 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001832 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001833 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001834 size_t nbelem;
1835 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001836
1837 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001838
Emeric Brunfa9d7802020-05-28 14:21:33 +02001839 /* historically some messages used to already contain the trailing LF
1840 * or Zero. Let's remove all trailing LF or Zero
1841 */
Emeric Brun54648852020-07-06 15:54:06 +02001842 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001843 size--;
1844
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001845 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001846 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001847 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001848 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001849 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1850 /* the socket's address is a file descriptor */
1851 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1852 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001853 else if (logsrv->addr.ss_family == AF_UNIX)
1854 plogfd = &logfdunix;
1855 else
1856 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001857
Willy Tarreauc046d162019-08-30 15:24:59 +02001858 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001859 /* socket not successfully initialized yet */
1860 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1861 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1862 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001863
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001864 if (!once) {
1865 once = 1; /* note: no need for atomic ops here */
1866 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1867 nblogger, strerror(errno), errno);
1868 }
1869 return;
1870 } else {
1871 /* we don't want to receive anything on this socket */
1872 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1873 /* does nothing under Linux, maybe needed for others */
1874 shutdown(*plogfd, SHUT_RD);
1875 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1876 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001877 }
1878
Emeric Brun54648852020-07-06 15:54:06 +02001879 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1880 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001881 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001882 struct ist msg;
Aurelien DARRAGON55446a42023-06-26 17:15:48 +02001883 size_t maxlen = logsrv->maxlen;
Emeric Brun54648852020-07-06 15:54:06 +02001884
1885 msg = ist2(message, size);
1886 if (msg.len > logsrv->maxlen)
1887 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001888
Aurelien DARRAGON55446a42023-06-26 17:15:48 +02001889 /* make room for the final '\n' which may be forcefully inserted
1890 * by tcp forwarder applet (sink_forward_io_handler)
1891 */
1892 maxlen -= 1;
1893
1894 sent = sink_write(logsrv->sink, maxlen, &msg, 1, level, facility, metadata);
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001895 }
1896 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1897 struct ist msg;
1898
1899 msg = ist2(message, size);
1900 if (msg.len > logsrv->maxlen)
1901 msg.len = logsrv->maxlen;
1902
1903 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001904 }
1905 else {
Emeric Brun54648852020-07-06 15:54:06 +02001906 int i = 0;
Aurelien DARRAGONa92de3f2023-06-27 11:32:06 +02001907 int totlen = logsrv->maxlen - 1; /* save space for the final '\n' */
Emeric Brun54648852020-07-06 15:54:06 +02001908
1909 for (i = 0 ; i < nbelem ; i++ ) {
1910 iovec[i].iov_base = msg_header[i].ptr;
1911 iovec[i].iov_len = msg_header[i].len;
1912 if (totlen <= iovec[i].iov_len) {
1913 iovec[i].iov_len = totlen;
1914 totlen = 0;
1915 break;
1916 }
1917 totlen -= iovec[i].iov_len;
1918 }
1919 if (totlen) {
1920 iovec[i].iov_base = message;
1921 iovec[i].iov_len = size;
1922 if (totlen <= iovec[i].iov_len)
1923 iovec[i].iov_len = totlen;
1924 i++;
1925 }
1926 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1927 iovec[i].iov_len = 1;
1928 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001929
Emeric Brun54648852020-07-06 15:54:06 +02001930 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001931 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1932 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001933
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001934 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1935 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001936
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001937 if (sent < 0) {
1938 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001939
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001940 if (errno == EAGAIN)
Willy Tarreau4781b152021-04-06 13:53:36 +02001941 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001942 else if (!once) {
1943 once = 1; /* note: no need for atomic ops here */
1944 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1945 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001946 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001947 }
1948}
Dragan Dosen59cee972015-09-19 22:09:02 +02001949
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001950/*
1951 * This function sends a syslog message.
1952 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001953 * The argument <metadata> MUST be an array of size
1954 * LOG_META_FIELDS*sizeof(struct ist) containing
1955 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001956 */
Emeric Brun54648852020-07-06 15:54:06 +02001957void process_send_log(struct list *logsrvs, int level, int facility,
1958 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001959{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001960 struct logsrv *logsrv;
1961 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001962
1963 /* Send log messages to syslog server. */
1964 nblogger = 0;
1965 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001966 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001967
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001968 /* we can filter the level of the messages that are sent to each logger */
1969 if (level > logsrv->level)
1970 continue;
1971
Frédéric Lécailled803e472019-04-25 07:42:09 +02001972 if (logsrv->lb.smp_rgs) {
1973 struct smp_log_range *curr_rg;
1974
1975 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1976 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1977 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1978 if (in_range) {
1979 /* Let's consume this range. */
1980 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1981 if (!curr_rg->curr_idx) {
1982 /* If consumed, let's select the next range. */
1983 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1984 }
1985 }
1986 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1987 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1988 }
1989 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001990 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1991 (facility == -1) ? logsrv->facility : facility,
1992 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001993 }
1994}
1995
Emeric Brun54648852020-07-06 15:54:06 +02001996/*
1997 * This function sends a syslog message.
1998 * It doesn't care about errors nor does it report them.
1999 * The arguments <sd> and <sd_size> are used for the structured-data part
2000 * in RFC5424 formatted syslog messages.
2001 */
2002void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
2003 char *message, size_t size, char *sd, size_t sd_size)
2004{
2005 static THREAD_LOCAL pid_t curr_pid;
2006 static THREAD_LOCAL char pidstr[16];
2007 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
2008
2009 if (logsrvs == NULL) {
2010 if (!LIST_ISEMPTY(&global.logsrvs)) {
2011 logsrvs = &global.logsrvs;
2012 }
2013 }
2014 if (!logsrvs || LIST_ISEMPTY(logsrvs))
2015 return;
2016
2017 if (!metadata[LOG_META_HOST].len) {
2018 if (global.log_send_hostname)
2019 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02002020 }
2021
2022 if (!tagb || !tagb->area)
2023 tagb = &global.log_tag;
2024
2025 if (tagb)
2026 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
2027
2028 if (unlikely(curr_pid != getpid()))
2029 metadata[LOG_META_PID].len = 0;
2030
2031 if (!metadata[LOG_META_PID].len) {
2032 curr_pid = getpid();
2033 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
2034 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
2035 }
2036
2037 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
2038
2039 /* Remove trailing space of structured data */
2040 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
2041 metadata[LOG_META_STDATA].len--;
2042
2043 return process_send_log(logsrvs, level, -1, metadata, message, size);
2044}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002045
Willy Tarreauc89ccb62012-04-05 21:18:22 +02002046const 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 +01002047const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
2048 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
2049 Set-cookie Updated, unknown, unknown */
2050
William Lallemand1d705562012-03-12 12:46:41 +01002051/*
2052 * try to write a character if there is enough space, or goto out
2053 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01002054#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01002055 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01002056 *(tmplog++) = (x); \
2057 } else { \
2058 goto out; \
2059 } \
2060 } while(0)
2061
Dragan Dosen835b9212016-02-12 13:23:03 +01002062
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002063/* Initializes some log data at boot */
2064static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01002065{
2066 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02002067 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01002068
2069 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
2070 * inside PARAM-VALUE should be escaped with '\' as prefix.
2071 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
2072 * details.
2073 */
2074 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
2075
2076 tmp = "\"\\]";
2077 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002078 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002079 tmp++;
2080 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02002081
2082 /* initialize the log header encoding map : '{|}"#' should be encoded with
2083 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
2084 * URL encoding only requires '"', '#' to be encoded as well as non-
2085 * printable characters above.
2086 */
2087 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
2088 memset(url_encode_map, 0, sizeof(url_encode_map));
2089 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002090 ha_bit_set(i, hdr_encode_map);
2091 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002092 }
2093 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002094 ha_bit_set(i, hdr_encode_map);
2095 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002096 }
2097
2098 tmp = "\"#{|}";
2099 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002100 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002101 tmp++;
2102 }
2103
2104 tmp = "\"#";
2105 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002106 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002107 tmp++;
2108 }
2109
2110 /* initialize the http header encoding map. The draft httpbis define the
2111 * header content as:
2112 *
2113 * HTTP-message = start-line
2114 * *( header-field CRLF )
2115 * CRLF
2116 * [ message-body ]
2117 * header-field = field-name ":" OWS field-value OWS
2118 * field-value = *( field-content / obs-fold )
2119 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2120 * obs-fold = CRLF 1*( SP / HTAB )
2121 * field-vchar = VCHAR / obs-text
2122 * VCHAR = %x21-7E
2123 * obs-text = %x80-FF
2124 *
2125 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
2126 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08002127 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02002128 */
2129 memset(http_encode_map, 0, sizeof(http_encode_map));
2130 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002131 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002132 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002133 ha_bit_set(i, http_encode_map);
2134 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002135}
William Lallemand1d705562012-03-12 12:46:41 +01002136
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002137INITCALL0(STG_PREPARE, init_log);
2138
Christopher Faulet0132d062017-07-26 15:33:35 +02002139/* Initialize log buffers used for syslog messages */
2140int init_log_buffers()
2141{
Christopher Faulet0132d062017-07-26 15:33:35 +02002142 logline = my_realloc2(logline, global.max_syslog_len + 1);
2143 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02002144 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02002145 return 0;
2146 return 1;
2147}
2148
2149/* Deinitialize log buffers used for syslog messages */
2150void deinit_log_buffers()
2151{
Christopher Faulet0132d062017-07-26 15:33:35 +02002152 free(logline);
2153 free(logline_rfc5424);
Willy Tarreau869efd52019-11-15 15:16:57 +01002154 ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
Christopher Faulet0132d062017-07-26 15:33:35 +02002155 logline = NULL;
2156 logline_rfc5424 = NULL;
2157}
2158
Willy Tarreaudf974472012-12-28 02:44:01 +01002159/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2160 * <maxsize> characters. Returns the size of the output string in characters,
2161 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002162 * is not zero. It requires a valid session and optionally a stream. If the
2163 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002164 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002165int 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 +02002166{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002167 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002168 struct proxy *be;
2169 struct http_txn *txn;
2170 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002171 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002172 unsigned int s_flags;
2173 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002174 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002175 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002176 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002177 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002178 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002179 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002180 int t_request;
2181 int hdr;
2182 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002183 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002184 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002185 char *ret;
2186 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002187 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002188 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002189 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002190 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002191 struct ist path;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002192
William Lallemandbddd4fd2012-02-27 11:23:10 +01002193 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002194
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002195 if (likely(s)) {
2196 be = s->be;
2197 txn = s->txn;
2198 be_conn = cs_conn(objt_cs(s->si[1].end));
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002199 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002200 s_flags = s->flags;
2201 uniq_id = s->uniq_id;
2202 logs = &s->logs;
2203 } else {
2204 /* we have no stream so we first need to initialize a few
2205 * things that are needed later. We do increment the request
2206 * ID so that it's uniquely assigned to this request just as
2207 * if the request had reached the point of being processed.
2208 * A request error is reported as it's the only element we have
2209 * here and which justifies emitting such a log.
2210 */
Christopher Fauletfd818482021-04-14 14:01:41 +02002211 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002212 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002213 fe_conn = objt_conn(sess->origin);
Christopher Fauletfd818482021-04-14 14:01:41 +02002214 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? cs_conn(__objt_check(sess->origin)->cs) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002215 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002216 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02002217 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002218
2219 /* prepare a valid log structure */
2220 tmp_strm_log.tv_accept = sess->tv_accept;
2221 tmp_strm_log.accept_date = sess->accept_date;
2222 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02002223 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002224 tv_zero(&tmp_strm_log.tv_request);
2225 tmp_strm_log.t_queue = -1;
2226 tmp_strm_log.t_connect = -1;
2227 tmp_strm_log.t_data = -1;
2228 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2229 tmp_strm_log.bytes_in = 0;
2230 tmp_strm_log.bytes_out = 0;
2231 tmp_strm_log.prx_queue_pos = 0;
2232 tmp_strm_log.srv_queue_pos = 0;
2233
2234 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002235
2236 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
2237 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, NULL);
2238
2239 switch (es) {
2240 case MUX_ES_SUCCESS:
2241 break;
2242 case MUX_ES_INVALID_ERR:
2243 status = 400;
2244 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2245 s_flags = SF_ERR_CLICL | SF_FINST_R;
2246 else
2247 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2248 break;
2249 case MUX_ES_TOUT_ERR:
2250 status = 408;
2251 s_flags = SF_ERR_CLITO | SF_FINST_R;
2252 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002253 case MUX_ES_NOTIMPL_ERR:
2254 status = 501;
2255 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2256 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002257 case MUX_ES_INTERNAL_ERR:
2258 status = 500;
2259 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2260 break;
2261 default:
2262 break;
2263 }
2264 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002265 }
2266
William Lallemandbddd4fd2012-02-27 11:23:10 +01002267 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002268 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2269 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002270
William Lallemand1d705562012-03-12 12:46:41 +01002271 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002272
William Lallemandbddd4fd2012-02-27 11:23:10 +01002273 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002274 if (LIST_ISEMPTY(list_format))
2275 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002276
William Lallemand1d705562012-03-12 12:46:41 +01002277 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002278 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002279 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002280 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002281 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002282
Willy Tarreauc8368452012-12-21 00:09:23 +01002283 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002284 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002285 if (!last_isspace) {
2286 LOGCHAR(' ');
2287 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002288 }
2289 break;
2290
William Lallemand1d705562012-03-12 12:46:41 +01002291 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002292 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002293 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002294 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002295 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002296 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002297 last_isspace = 0;
2298 break;
2299
Willy Tarreauc8368452012-12-21 00:09:23 +01002300 case LOG_FMT_EXPR: // sample expression, may be request or response
2301 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002302 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002303 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Christopher Faulet5f940702020-04-06 10:40:02 +02002304 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002305 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002306 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002307 ret = lf_encode_chunk(tmplog, dst + maxsize,
2308 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002309 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002310 ret = lf_text_len(tmplog,
2311 key ? key->data.u.str.area : NULL,
2312 key ? key->data.u.str.data : 0,
2313 dst + maxsize - tmplog,
2314 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002315 if (ret == 0)
2316 goto out;
2317 tmplog = ret;
2318 last_isspace = 0;
2319 break;
2320
Willy Tarreau2beef582012-12-20 17:22:52 +01002321 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002322 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002323 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002324 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002325 else
2326 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002327 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002328 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002329 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002330 last_isspace = 0;
2331 break;
2332
Willy Tarreau2beef582012-12-20 17:22:52 +01002333 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002334 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002335 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002336 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002337 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002338 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002339 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002340 dst + maxsize - tmplog, tmp);
2341 }
William Lallemand5f232402012-04-05 18:02:55 +02002342 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002343 else
2344 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2345
William Lallemand5f232402012-04-05 18:02:55 +02002346 if (ret == NULL)
2347 goto out;
2348 tmplog = ret;
2349 last_isspace = 0;
2350 break;
2351
Willy Tarreau2beef582012-12-20 17:22:52 +01002352 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002353 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002354 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002355 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002356 }
2357 else
2358 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2359
William Lallemand1d705562012-03-12 12:46:41 +01002360 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002361 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002362 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002363 last_isspace = 0;
2364 break;
2365
Willy Tarreau2beef582012-12-20 17:22:52 +01002366 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002367 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002368 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002369 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002370 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002371 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002372 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002373 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002374 else
2375 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2376
William Lallemand5f232402012-04-05 18:02:55 +02002377 if (ret == NULL)
2378 goto out;
2379 tmplog = ret;
2380 last_isspace = 0;
2381 break;
2382
Willy Tarreau2beef582012-12-20 17:22:52 +01002383 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002384 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002385 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002386 else
2387 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2388
William Lallemand1d705562012-03-12 12:46:41 +01002389 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002390 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002391 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002392 last_isspace = 0;
2393 break;
2394
Willy Tarreau2beef582012-12-20 17:22:52 +01002395 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002396 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002397 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002398 else
2399 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2400
William Lallemand5f232402012-04-05 18:02:55 +02002401 if (ret == NULL)
2402 goto out;
2403 tmplog = ret;
2404 last_isspace = 0;
2405 break;
2406
Willy Tarreau2beef582012-12-20 17:22:52 +01002407 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002408 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002409 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002410 else
2411 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2412
William Lallemand5f232402012-04-05 18:02:55 +02002413 if (ret == NULL)
2414 goto out;
2415 tmplog = ret;
2416 last_isspace = 0;
2417 break;
2418
Willy Tarreau2beef582012-12-20 17:22:52 +01002419 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002420 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002421 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002422 else
2423 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2424
William Lallemand1d705562012-03-12 12:46:41 +01002425 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002426 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002427 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002428 last_isspace = 0;
2429 break;
2430
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002431 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002432 get_localtime(logs->accept_date.tv_sec, &tm);
2433 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002434 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002435 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002436 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002437 last_isspace = 0;
2438 break;
2439
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002440 case LOG_FMT_tr: // %tr = start of request date
2441 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002442 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 +02002443 get_localtime(tv.tv_sec, &tm);
2444 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2445 if (ret == NULL)
2446 goto out;
2447 tmplog = ret;
2448 last_isspace = 0;
2449 break;
2450
2451 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002452 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002453 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002454 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002455 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002456 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002457 last_isspace = 0;
2458 break;
2459
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002460 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002461 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 +02002462 get_gmtime(tv.tv_sec, &tm);
2463 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2464 if (ret == NULL)
2465 goto out;
2466 tmplog = ret;
2467 last_isspace = 0;
2468 break;
2469
2470 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002471 get_localtime(logs->accept_date.tv_sec, &tm);
2472 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002473 if (ret == NULL)
2474 goto out;
2475 tmplog = ret;
2476 last_isspace = 0;
2477 break;
2478
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002479 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002480 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 +02002481 get_localtime(tv.tv_sec, &tm);
2482 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2483 if (ret == NULL)
2484 goto out;
2485 tmplog = ret;
2486 last_isspace = 0;
2487 break;
2488
William Lallemand5f232402012-04-05 18:02:55 +02002489 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002490 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002491 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002492 if (iret < 0 || iret > dst + maxsize - tmplog)
2493 goto out;
2494 last_isspace = 0;
2495 tmplog += iret;
2496 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002497 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002498 if (ret == NULL)
2499 goto out;
2500 tmplog = ret;
2501 last_isspace = 0;
2502 }
2503 break;
2504
William Lallemand1d705562012-03-12 12:46:41 +01002505 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002506 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002507 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002508 if (iret < 0 || iret > dst + maxsize - tmplog)
2509 goto out;
2510 last_isspace = 0;
2511 tmplog += iret;
2512 } else {
2513 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002514 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002515 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002516 tmplog, 4);
2517 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002518 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002519 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002520 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002521 }
2522 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002523
William Lallemand1d705562012-03-12 12:46:41 +01002524 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002525 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002526 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002527 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002528 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002529 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002530 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002531 break;
2532
Willy Tarreau773d65f2012-10-12 14:56:11 +02002533 case LOG_FMT_FRONTEND_XPRT: // %ft
2534 src = fe->id;
2535 if (tmp->options & LOG_OPT_QUOTE)
2536 LOGCHAR('"');
2537 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2538 if (iret == 0)
2539 goto out;
2540 tmplog += iret;
Christopher Fauletfd818482021-04-14 14:01:41 +02002541 if (sess->listener && sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002542 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002543 if (tmp->options & LOG_OPT_QUOTE)
2544 LOGCHAR('"');
2545 last_isspace = 0;
2546 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002547#ifdef USE_OPENSSL
2548 case LOG_FMT_SSL_CIPHER: // %sslc
2549 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002550 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002551 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002552 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002553 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002554 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2555 if (ret == NULL)
2556 goto out;
2557 tmplog = ret;
2558 last_isspace = 0;
2559 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002560
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002561 case LOG_FMT_SSL_VERSION: // %sslv
2562 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002563 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002564 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002565 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002566 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002567 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2568 if (ret == NULL)
2569 goto out;
2570 tmplog = ret;
2571 last_isspace = 0;
2572 break;
2573#endif
William Lallemand1d705562012-03-12 12:46:41 +01002574 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002575 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002576 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002577 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002578 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002579 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002580 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002581 break;
2582
William Lallemand1d705562012-03-12 12:46:41 +01002583 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002584 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002585 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002586 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002587 break;
2588 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002589 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002590 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002591 case OBJ_TYPE_CHECK:
2592 src = (__objt_check(sess->origin)->server
2593 ? __objt_check(sess->origin)->server->id
2594 : "<NOSRV>");
2595 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002596 default:
2597 src = "<NOSRV>";
2598 break;
2599 }
William Lallemand5f232402012-04-05 18:02:55 +02002600 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002601 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002602 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002603 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002604 last_isspace = 0;
2605 break;
2606
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002607 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002608 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002609 if (ret == NULL)
2610 goto out;
2611 tmplog = ret;
2612 last_isspace = 0;
2613 break;
2614
2615 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002616 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002617 if (ret == NULL)
2618 goto out;
2619 tmplog = ret;
2620 last_isspace = 0;
2621 break;
2622
2623 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002624 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002625 tmplog, dst + maxsize - tmplog);
2626 if (ret == NULL)
2627 goto out;
2628 tmplog = ret;
2629 last_isspace = 0;
2630 break;
2631
2632 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002633 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002634 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002635 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002636 tmplog = ret;
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_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002641 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002642 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002643 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002644 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002645 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002646 last_isspace = 0;
2647 break;
2648
William Lallemand1d705562012-03-12 12:46:41 +01002649 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002650 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002651 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002652 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002653 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002654 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002655 last_isspace = 0;
2656 break;
2657
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002658 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002659 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002660 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002661 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002662 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002663 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002664 last_isspace = 0;
2665 break;
2666
Willy Tarreau27b639d2016-05-17 17:55:27 +02002667 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002668 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002669 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002670 tmplog, dst + maxsize - tmplog);
2671 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002672 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002673 tmplog, dst + maxsize - tmplog);
2674 if (ret == NULL)
2675 goto out;
2676 tmplog = ret;
2677 last_isspace = 0;
2678 break;
2679
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002680 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2681 if (!(fe->to_log & LW_BYTES))
2682 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002683 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 +02002684 tmplog, dst + maxsize - tmplog);
2685 if (ret == NULL)
2686 goto out;
2687 tmplog = ret;
2688 last_isspace = 0;
2689 break;
2690
2691 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002692 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002693 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002694 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002695 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002696 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002697 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002698 last_isspace = 0;
2699 break;
2700
Damien Claisse57c8eb92020-04-28 12:09:19 +00002701 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2702 if (!(fe->to_log & LW_BYTES))
2703 LOGCHAR('+');
2704 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2705 tmplog, dst + maxsize - tmplog);
2706 if (ret == NULL)
2707 goto out;
2708 tmplog = ret;
2709 last_isspace = 0;
2710 break;
2711
Willy Tarreau2beef582012-12-20 17:22:52 +01002712 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002713 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002714 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002715 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002716 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002717 last_isspace = 0;
2718 break;
2719
William Lallemand1d705562012-03-12 12:46:41 +01002720 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002721 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002722 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002723 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002724 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002725 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002726 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002727 last_isspace = 0;
2728 break;
2729
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002730 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002731 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002732 if (ret == NULL)
2733 goto out;
2734 tmplog = ret;
2735 last_isspace = 0;
2736 break;
2737
Willy Tarreau2beef582012-12-20 17:22:52 +01002738 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002739 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002740 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002741 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002742 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002743 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002744 last_isspace = 0;
2745 break;
2746
Willy Tarreau2beef582012-12-20 17:22:52 +01002747 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002748 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002749 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002750 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002751 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002752 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002753 last_isspace = 0;
2754 break;
2755
William Lallemand1d705562012-03-12 12:46:41 +01002756 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002757 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2758 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002759 *tmplog = '\0';
2760 last_isspace = 0;
2761 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002762
William Lallemand1d705562012-03-12 12:46:41 +01002763 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002764 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2765 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002766 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2767 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 +01002768 last_isspace = 0;
2769 break;
2770
William Lallemand1d705562012-03-12 12:46:41 +01002771 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002772 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002773 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002774 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002775 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002776 last_isspace = 0;
2777 break;
2778
William Lallemand1d705562012-03-12 12:46:41 +01002779 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002780 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002781 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002782 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002783 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002784 last_isspace = 0;
2785 break;
2786
William Lallemand1d705562012-03-12 12:46:41 +01002787 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002788 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002789 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002790 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002791 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002792 last_isspace = 0;
2793 break;
2794
William Lallemand1d705562012-03-12 12:46:41 +01002795 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002796 switch (obj_type(s ? s->target : sess->origin)) {
2797 case OBJ_TYPE_SERVER:
2798 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2799 tmplog, dst + maxsize - tmplog);
2800 break;
2801 case OBJ_TYPE_CHECK:
2802 ret = ultoa_o(__objt_check(sess->origin)->server
2803 ? __objt_check(sess->origin)->server->cur_sess
2804 : 0, tmplog, dst + maxsize - tmplog);
2805 break;
2806 default:
2807 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2808 break;
2809 }
2810
William Lallemand1d705562012-03-12 12:46:41 +01002811 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002812 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002813 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002814 last_isspace = 0;
2815 break;
2816
William Lallemand1d705562012-03-12 12:46:41 +01002817 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002818 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002819 LOGCHAR('+');
Christopher Faulet1d26f222021-04-16 11:24:20 +02002820 ret = ltoa_o(((s && s->si[1].conn_retries > 0)
2821 ? (be->conn_retries - s->si[1].conn_retries)
2822 : ((s && s->si[1].state != SI_ST_INI) ? be->conn_retries : 0)),
2823 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002824 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002825 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002826 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002827 last_isspace = 0;
2828 break;
2829
William Lallemand1d705562012-03-12 12:46:41 +01002830 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002831 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002832 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002833 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002834 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002835 last_isspace = 0;
2836 break;
2837
William Lallemand1d705562012-03-12 12:46:41 +01002838 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002839 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002840 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002841 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002842 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002843 last_isspace = 0;
2844 break;
2845
William Lallemand1d705562012-03-12 12:46:41 +01002846 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002847 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002848 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002849 if (tmp->options & LOG_OPT_QUOTE)
2850 LOGCHAR('"');
2851 LOGCHAR('{');
2852 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2853 if (hdr)
2854 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002855 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002856 ret = lf_encode_string(tmplog, dst + maxsize,
2857 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002858 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002859 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002860 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002861 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002862 }
2863 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002864 if (tmp->options & LOG_OPT_QUOTE)
2865 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002866 last_isspace = 0;
2867 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002868 break;
2869
William Lallemand1d705562012-03-12 12:46:41 +01002870 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002871 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002872 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002873 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2874 if (hdr > 0)
2875 LOGCHAR(' ');
2876 if (tmp->options & LOG_OPT_QUOTE)
2877 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002878 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002879 ret = lf_encode_string(tmplog, dst + maxsize,
2880 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002881 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002882 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002883 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002884 } else if (!(tmp->options & LOG_OPT_QUOTE))
2885 LOGCHAR('-');
2886 if (tmp->options & LOG_OPT_QUOTE)
2887 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002888 last_isspace = 0;
2889 }
2890 }
2891 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002892
William Lallemand1d705562012-03-12 12:46:41 +01002893
2894 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002895 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002896 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002897 if (tmp->options & LOG_OPT_QUOTE)
2898 LOGCHAR('"');
2899 LOGCHAR('{');
2900 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2901 if (hdr)
2902 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002903 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002904 ret = lf_encode_string(tmplog, dst + maxsize,
2905 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002906 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002907 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002908 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002909 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002910 }
2911 LOGCHAR('}');
2912 last_isspace = 0;
2913 if (tmp->options & LOG_OPT_QUOTE)
2914 LOGCHAR('"');
2915 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002916 break;
2917
William Lallemand1d705562012-03-12 12:46:41 +01002918 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002919 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002920 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002921 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2922 if (hdr > 0)
2923 LOGCHAR(' ');
2924 if (tmp->options & LOG_OPT_QUOTE)
2925 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002926 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002927 ret = lf_encode_string(tmplog, dst + maxsize,
2928 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002929 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002930 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002931 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002932 } else if (!(tmp->options & LOG_OPT_QUOTE))
2933 LOGCHAR('-');
2934 if (tmp->options & LOG_OPT_QUOTE)
2935 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002936 last_isspace = 0;
2937 }
2938 }
2939 break;
2940
William Lallemand1d705562012-03-12 12:46:41 +01002941 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002942 /* Request */
2943 if (tmp->options & LOG_OPT_QUOTE)
2944 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002945 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002946 ret = lf_encode_string(tmplog, dst + maxsize,
2947 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002948 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002949 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002950 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002951 if (tmp->options & LOG_OPT_QUOTE)
2952 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002953 last_isspace = 0;
2954 break;
William Lallemand5f232402012-04-05 18:02:55 +02002955
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002956 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002957 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002958
Willy Tarreaub7636d12015-06-17 19:58:02 +02002959 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002960 LOGCHAR('"');
2961
2962 end = uri + strlen(uri);
2963 // look for the first whitespace character
2964 while (uri < end && !HTTP_IS_SPHT(*uri))
2965 uri++;
2966
2967 // keep advancing past multiple spaces
2968 while (uri < end && HTTP_IS_SPHT(*uri)) {
2969 uri++; nspaces++;
2970 }
2971
2972 // look for first space or question mark after url
2973 spc = uri;
2974 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2975 spc++;
2976
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002977 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002978 chunk.area = "<BADREQ>";
2979 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002980 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002981 chunk.area = uri;
2982 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002983 }
2984
Dragan Dosen835b9212016-02-12 13:23:03 +01002985 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002986 if (ret == NULL || *ret != '\0')
2987 goto out;
2988
2989 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002990 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002991 LOGCHAR('"');
2992
2993 last_isspace = 0;
2994 break;
2995
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002996 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2997 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2998
2999 if (tmp->options & LOG_OPT_QUOTE)
3000 LOGCHAR('"');
3001
3002 end = uri + strlen(uri);
3003
3004 // look for the first whitespace character
3005 while (uri < end && !HTTP_IS_SPHT(*uri))
3006 uri++;
3007
3008 // keep advancing past multiple spaces
3009 while (uri < end && HTTP_IS_SPHT(*uri)) {
3010 uri++; nspaces++;
3011 }
3012
3013 // look for first space after url
3014 spc = uri;
3015 while (spc < end && !HTTP_IS_SPHT(*spc))
3016 spc++;
3017
Tim Duesterhus92c696e2021-02-28 16:11:36 +01003018 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00003019
3020 // extract relative path without query params from url
3021 path = iststop(http_get_path(path), '?');
3022 if (!txn || !txn->uri || nspaces == 0) {
3023 chunk.area = "<BADREQ>";
3024 chunk.data = strlen("<BADREQ>");
3025 } else {
3026 chunk.area = path.ptr;
3027 chunk.data = path.len;
3028 }
3029
3030 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
3031 if (ret == NULL || *ret != '\0')
3032 goto out;
3033
3034 tmplog = ret;
3035 if (tmp->options & LOG_OPT_QUOTE)
3036 LOGCHAR('"');
3037
3038 last_isspace = 0;
3039 break;
3040
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003041 case LOG_FMT_HTTP_QUERY: // %HQ
3042 if (tmp->options & LOG_OPT_QUOTE)
3043 LOGCHAR('"');
3044
Willy Tarreau57bc8912016-04-25 17:09:40 +02003045 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003046 chunk.area = "<BADREQ>";
3047 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003048 } else {
3049 uri = txn->uri;
3050 end = uri + strlen(uri);
3051 // look for the first question mark
3052 while (uri < end && *uri != '?')
3053 uri++;
3054
3055 qmark = uri;
3056 // look for first space or question mark after url
3057 while (uri < end && !HTTP_IS_SPHT(*uri))
3058 uri++;
3059
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003060 chunk.area = qmark;
3061 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003062 }
3063
Dragan Dosen835b9212016-02-12 13:23:03 +01003064 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003065 if (ret == NULL || *ret != '\0')
3066 goto out;
3067
3068 tmplog = ret;
3069 if (tmp->options & LOG_OPT_QUOTE)
3070 LOGCHAR('"');
3071
3072 last_isspace = 0;
3073 break;
3074
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003075 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02003076 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003077
Willy Tarreaub7636d12015-06-17 19:58:02 +02003078 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003079 LOGCHAR('"');
3080
3081 end = uri + strlen(uri);
3082 // look for the first whitespace character
3083 while (uri < end && !HTTP_IS_SPHT(*uri))
3084 uri++;
3085
3086 // keep advancing past multiple spaces
3087 while (uri < end && HTTP_IS_SPHT(*uri)) {
3088 uri++; nspaces++;
3089 }
3090
3091 // look for first space after url
3092 spc = uri;
3093 while (spc < end && !HTTP_IS_SPHT(*spc))
3094 spc++;
3095
Willy Tarreau57bc8912016-04-25 17:09:40 +02003096 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003097 chunk.area = "<BADREQ>";
3098 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003099 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003100 chunk.area = uri;
3101 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003102 }
3103
Dragan Dosen835b9212016-02-12 13:23:03 +01003104 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003105 if (ret == NULL || *ret != '\0')
3106 goto out;
3107
3108 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003109 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003110 LOGCHAR('"');
3111
3112 last_isspace = 0;
3113 break;
3114
3115 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02003116 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003117 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003118 LOGCHAR('"');
3119
3120 end = uri + strlen(uri);
3121 // look for the first whitespace character
3122 spc = uri;
3123 while (spc < end && !HTTP_IS_SPHT(*spc))
3124 spc++;
3125
3126 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003127 chunk.area = "<BADREQ>";
3128 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003129 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003130 chunk.area = uri;
3131 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003132 }
3133
Dragan Dosen835b9212016-02-12 13:23:03 +01003134 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003135 if (ret == NULL || *ret != '\0')
3136 goto out;
3137
3138 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003139 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003140 LOGCHAR('"');
3141
3142 last_isspace = 0;
3143 break;
3144
3145 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02003146 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003147 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003148 LOGCHAR('"');
3149
3150 end = uri + strlen(uri);
3151 // look for the first whitespace character
3152 while (uri < end && !HTTP_IS_SPHT(*uri))
3153 uri++;
3154
3155 // keep advancing past multiple spaces
3156 while (uri < end && HTTP_IS_SPHT(*uri)) {
3157 uri++; nspaces++;
3158 }
3159
3160 // look for the next whitespace character
3161 while (uri < end && !HTTP_IS_SPHT(*uri))
3162 uri++;
3163
3164 // keep advancing past multiple spaces
3165 while (uri < end && HTTP_IS_SPHT(*uri))
3166 uri++;
3167
Willy Tarreau57bc8912016-04-25 17:09:40 +02003168 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003169 chunk.area = "<BADREQ>";
3170 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003171 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003172 chunk.area = "HTTP/0.9";
3173 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003174 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003175 chunk.area = uri;
3176 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003177 }
3178
Dragan Dosen835b9212016-02-12 13:23:03 +01003179 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003180 if (ret == NULL || *ret != '\0')
3181 goto out;
3182
3183 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003184 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003185 LOGCHAR('"');
3186
3187 last_isspace = 0;
3188 break;
3189
William Lallemand5f232402012-04-05 18:02:55 +02003190 case LOG_FMT_COUNTER: // %rt
3191 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003192 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02003193 if (iret < 0 || iret > dst + maxsize - tmplog)
3194 goto out;
3195 last_isspace = 0;
3196 tmplog += iret;
3197 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003198 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003199 if (ret == NULL)
3200 goto out;
3201 tmplog = ret;
3202 last_isspace = 0;
3203 }
3204 break;
3205
Willy Tarreau7346acb2014-08-28 15:03:15 +02003206 case LOG_FMT_LOGCNT: // %lc
3207 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003208 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003209 if (iret < 0 || iret > dst + maxsize - tmplog)
3210 goto out;
3211 last_isspace = 0;
3212 tmplog += iret;
3213 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003214 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003215 if (ret == NULL)
3216 goto out;
3217 tmplog = ret;
3218 last_isspace = 0;
3219 }
3220 break;
3221
William Lallemand5f232402012-04-05 18:02:55 +02003222 case LOG_FMT_HOSTNAME: // %H
3223 src = hostname;
3224 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3225 if (ret == NULL)
3226 goto out;
3227 tmplog = ret;
3228 last_isspace = 0;
3229 break;
3230
3231 case LOG_FMT_PID: // %pid
3232 if (tmp->options & LOG_OPT_HEXA) {
3233 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3234 if (iret < 0 || iret > dst + maxsize - tmplog)
3235 goto out;
3236 last_isspace = 0;
3237 tmplog += iret;
3238 } else {
3239 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3240 if (ret == NULL)
3241 goto out;
3242 tmplog = ret;
3243 last_isspace = 0;
3244 }
3245 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003246
3247 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003248 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003249 if (s)
3250 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3251 else
3252 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003253 if (ret == NULL)
3254 goto out;
3255 tmplog = ret;
3256 last_isspace = 0;
3257 break;
3258
William Lallemandbddd4fd2012-02-27 11:23:10 +01003259 }
3260 }
3261
3262out:
William Lallemand1d705562012-03-12 12:46:41 +01003263 /* *tmplog is a unused character */
3264 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003265 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003266
Willy Tarreaubaaee002006-06-26 02:48:02 +02003267}
3268
William Lallemand1d705562012-03-12 12:46:41 +01003269/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003270 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003271 * Will not log if the frontend has no log defined.
3272 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003273void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003274{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003275 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003276 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003277 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003278
3279 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003280 err = (s->flags & SF_REDISP) ||
3281 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3282 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003283 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003284 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003285
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003286 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003287 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003288
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003289 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003290 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003291
Willy Tarreauabcd5142013-06-11 17:18:02 +02003292 if (s->logs.level) { /* loglevel was overridden */
3293 if (s->logs.level == -1) {
3294 s->logs.logwait = 0; /* logs disabled */
3295 return;
3296 }
3297 level = s->logs.level - 1;
3298 }
3299 else {
3300 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003301 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003302 level = LOG_ERR;
3303 }
William Lallemand1d705562012-03-12 12:46:41 +01003304
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003305 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003306 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003307 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003308 }
3309
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003310 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3311 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3312 &sess->fe->logformat_sd);
3313 }
3314
Dragan Dosen59cee972015-09-19 22:09:02 +02003315 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003316 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003317 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003318 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3319 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003320 s->logs.logwait = 0;
3321 }
3322}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003323
Willy Tarreau53839352018-09-05 19:51:10 +02003324/*
3325 * send a minimalist log for the session. Will not log if the frontend has no
3326 * log defined. It is assumed that this is only used to report anomalies that
3327 * cannot lead to the creation of a regular stream. Because of this the log
3328 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3329 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003330 * function to report unimportant events. It is safe to call this function with
3331 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003332 */
3333void sess_log(struct session *sess)
3334{
3335 int size, level;
3336 int sd_size = 0;
3337
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003338 if (!sess)
3339 return;
3340
Willy Tarreau53839352018-09-05 19:51:10 +02003341 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3342 return;
3343
3344 level = LOG_INFO;
3345 if (sess->fe->options2 & PR_O2_LOGERRORS)
3346 level = LOG_ERR;
3347
3348 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3349 sd_size = sess_build_logline(sess, NULL,
3350 logline_rfc5424, global.max_syslog_len,
3351 &sess->fe->logformat_sd);
3352 }
3353
3354 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3355 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003356 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003357 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3358 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003359 }
3360}
3361
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003362void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3363{
3364 va_list argp;
3365 int data_len;
3366
3367 if (level < 0 || format == NULL || logline == NULL)
3368 return;
3369
3370 va_start(argp, format);
3371 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3372 if (data_len < 0 || data_len > global.max_syslog_len)
3373 data_len = global.max_syslog_len;
3374 va_end(argp);
3375
3376 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3377}
Emeric Brun54932b42020-07-07 09:43:24 +02003378/*
3379 * This function parse a received log message <buf>, of size <buflen>
3380 * it fills <level>, <facility> and <metadata> depending of the detected
3381 * header format and message will point on remaining payload of <size>
3382 *
3383 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3384 * struct ist len will be set to 0 if field is not found
3385 * <level> and <facility> will be set to -1 if not found.
3386 */
3387void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3388 struct ist *metadata, char **message, size_t *size)
3389{
3390
3391 char *p;
3392 int fac_level = 0;
3393
3394 *level = *facility = -1;
3395
3396 *message = buf;
3397 *size = buflen;
3398
3399 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3400
3401 p = buf;
3402 if (*size < 2 || *p != '<')
3403 return;
3404
3405 p++;
3406 while (*p != '>') {
3407 if (*p > '9' || *p < '0')
3408 return;
3409 fac_level = 10*fac_level + (*p - '0');
3410 p++;
3411 if ((p - buf) > buflen)
3412 return;
3413 }
3414
3415 *facility = fac_level >> 3;
3416 *level = fac_level & 0x7;
3417 p++;
3418
3419 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3420
3421 buflen -= p - buf;
3422 buf = p;
3423
3424 *size = buflen;
3425 *message = buf;
3426
3427 /* for rfc5424, prio is always followed by '1' and ' ' */
3428 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3429 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3430 * followed by message.
3431 * Each header field can present NILVALUE: '-'
3432 */
3433
3434 p += 2;
Aurelien DARRAGON27272f22022-11-22 11:17:11 +01003435 *size -= 2;
Emeric Brun54932b42020-07-07 09:43:24 +02003436 /* timestamp is NILVALUE '-' */
3437 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3438 metadata[LOG_META_TIME] = ist2(p, 1);
3439 p++;
3440 }
3441 else if (*size > LOG_ISOTIME_MINLEN) {
3442 metadata[LOG_META_TIME].ptr = p;
3443
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003444 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003445 * in timestamp.
3446 * possible format are:
3447 * ex: '1970-01-01T00:00:00.000000Z'
3448 * '1970-01-01T00:00:00.000000+00:00'
3449 * '1970-01-01T00:00:00.000000-00:00'
3450 * '1970-01-01T00:00:00Z'
3451 * '1970-01-01T00:00:00+00:00'
3452 * '1970-01-01T00:00:00-00:00'
3453 */
3454 p += 19;
3455 if (*p == '.') {
3456 p++;
3457 if ((p - buf) >= buflen)
3458 goto bad_format;
3459 while (*p != 'Z' && *p != '+' && *p != '-') {
3460 if ((unsigned char)(*p - '0') > 9)
3461 goto bad_format;
3462
3463 p++;
3464 if ((p - buf) >= buflen)
3465 goto bad_format;
3466 }
3467 }
3468
3469 if (*p == 'Z')
3470 p++;
3471 else
3472 p += 6; /* case of '+00:00 or '-00:00' */
3473
3474 if ((p - buf) >= buflen || *p != ' ')
3475 goto bad_format;
3476 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3477 }
3478 else
3479 goto bad_format;
3480
3481
3482 p++;
3483 if ((p - buf) >= buflen || *p == ' ')
3484 goto bad_format;
3485
3486 metadata[LOG_META_HOST].ptr = p;
3487 while (*p != ' ') {
3488 p++;
3489 if ((p - buf) >= buflen)
3490 goto bad_format;
3491 }
3492 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3493 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3494 metadata[LOG_META_HOST].len = 0;
3495
3496 p++;
3497 if ((p - buf) >= buflen || *p == ' ')
3498 goto bad_format;
3499
3500 metadata[LOG_META_TAG].ptr = p;
3501 while (*p != ' ') {
3502 p++;
3503 if ((p - buf) >= buflen)
3504 goto bad_format;
3505 }
3506 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3507 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3508 metadata[LOG_META_TAG].len = 0;
3509
3510 p++;
3511 if ((p - buf) >= buflen || *p == ' ')
3512 goto bad_format;
3513
3514 metadata[LOG_META_PID].ptr = p;
3515 while (*p != ' ') {
3516 p++;
3517 if ((p - buf) >= buflen)
3518 goto bad_format;
3519 }
3520 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3521 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3522 metadata[LOG_META_PID].len = 0;
3523
3524 p++;
3525 if ((p - buf) >= buflen || *p == ' ')
3526 goto bad_format;
3527
3528 metadata[LOG_META_MSGID].ptr = p;
3529 while (*p != ' ') {
3530 p++;
3531 if ((p - buf) >= buflen)
3532 goto bad_format;
3533 }
3534 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3535 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3536 metadata[LOG_META_MSGID].len = 0;
3537
3538 p++;
3539 if ((p - buf) >= buflen || *p == ' ')
3540 goto bad_format;
3541
3542 /* structured data format is:
3543 * ex:
3544 * '[key1=value1 key2=value2][key3=value3]'
3545 *
3546 * space is invalid outside [] because
3547 * considered as the end of structured data field
3548 */
3549 metadata[LOG_META_STDATA].ptr = p;
3550 if (*p == '[') {
3551 int elem = 0;
3552
3553 while (1) {
3554 if (elem) {
3555 /* according to rfc this char is escaped in param values */
3556 if (*p == ']' && *(p-1) != '\\')
3557 elem = 0;
3558 }
3559 else {
3560 if (*p == '[')
3561 elem = 1;
3562 else if (*p == ' ')
3563 break;
3564 else
3565 goto bad_format;
3566 }
3567 p++;
3568 if ((p - buf) >= buflen)
3569 goto bad_format;
3570 }
3571 }
3572 else if (*p == '-') {
3573 /* case of NILVALUE */
3574 p++;
3575 if ((p - buf) >= buflen || *p != ' ')
3576 goto bad_format;
3577 }
3578 else
3579 goto bad_format;
3580
3581 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3582 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3583 metadata[LOG_META_STDATA].len = 0;
3584
3585 p++;
3586
3587 buflen -= p - buf;
3588 buf = p;
3589
3590 *size = buflen;
3591 *message = p;
3592 }
3593 else if (*size > LOG_LEGACYTIME_LEN) {
3594 int m;
3595
3596 /* supported header format according to rfc3164.
3597 * ex:
3598 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3599 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3600 * or 'Jan 1 00:00:00 HOSTNAME '
3601 * Note: HOSTNAME is mandatory, and day
3602 * of month uses a single space prefix if
3603 * less than 10 to ensure hour offset is
3604 * always the same.
3605 */
3606
3607 /* Check month to see if it correspond to a rfc3164
3608 * header ex 'Jan 1 00:00:00' */
3609 for (m = 0; m < 12; m++)
3610 if (!memcmp(monthname[m], p, 3))
3611 break;
3612 /* Month not found */
3613 if (m == 12)
3614 goto bad_format;
3615
3616 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3617
3618 p += LOG_LEGACYTIME_LEN;
3619 if ((p - buf) >= buflen || *p != ' ')
3620 goto bad_format;
3621
3622 p++;
3623 if ((p - buf) >= buflen || *p == ' ')
3624 goto bad_format;
3625
3626 metadata[LOG_META_HOST].ptr = p;
3627 while (*p != ' ') {
3628 p++;
3629 if ((p - buf) >= buflen)
3630 goto bad_format;
3631 }
3632 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3633
3634 /* TAG seems to no be mandatory */
3635 p++;
3636
3637 buflen -= p - buf;
3638 buf = p;
3639
3640 *size = buflen;
3641 *message = buf;
3642
3643 if (!buflen)
3644 return;
3645
3646 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3647 p++;
3648
3649 /* a tag must present a trailing ':' */
3650 if (((p - buf) >= buflen) || *p != ':')
3651 return;
3652 p++;
3653 /* followed by a space */
3654 if (((p - buf) >= buflen) || *p != ' ')
3655 return;
3656
3657 /* rewind to parse tag and pid */
3658 p = buf;
3659 metadata[LOG_META_TAG].ptr = p;
3660 /* we have the guarantee that ':' will be reach before size limit */
3661 while (*p != ':') {
3662 if (*p == '[') {
3663 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3664 metadata[LOG_META_PID].ptr = p + 1;
3665 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003666 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003667 if (p[1] != ':')
3668 return;
3669 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3670 }
3671 p++;
3672 }
3673 if (!metadata[LOG_META_TAG].len)
3674 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3675
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003676 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003677 p += 2;
3678
3679 buflen -= p - buf;
3680 buf = p;
3681
3682 *size = buflen;
3683 *message = buf;
3684 }
3685
3686 return;
3687
3688bad_format:
3689 /* bad syslog format, we reset all parsed syslog fields
3690 * but priority is kept because we are able to re-build
3691 * this message using LOF_FORMAT_PRIO.
3692 */
3693 metadata[LOG_META_TIME].len = 0;
3694 metadata[LOG_META_HOST].len = 0;
3695 metadata[LOG_META_TAG].len = 0;
3696 metadata[LOG_META_PID].len = 0;
3697 metadata[LOG_META_MSGID].len = 0;
3698 metadata[LOG_META_STDATA].len = 0;
3699
3700 return;
3701}
3702
3703/*
3704 * UDP syslog fd handler
3705 */
3706void syslog_fd_handler(int fd)
3707{
3708 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3709 ssize_t ret = 0;
3710 struct buffer *buf = get_trash_chunk();
3711 size_t size;
3712 char *message;
3713 int level;
3714 int facility;
3715 struct listener *l = objt_listener(fdtab[fd].owner);
3716 int max_accept;
3717
3718 if(!l)
3719 ABORT_NOW();
3720
Willy Tarreauf5090652021-04-06 17:23:40 +02003721 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003722
3723 if (!fd_recv_ready(fd))
3724 return;
3725
3726 max_accept = l->maxaccept ? l->maxaccept : 1;
3727
3728 do {
3729 /* Source address */
3730 struct sockaddr_storage saddr = {0};
3731 socklen_t saddrlen;
3732
3733 saddrlen = sizeof(saddr);
3734
3735 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3736 if (ret < 0) {
3737 if (errno == EINTR)
3738 continue;
3739 if (errno == EAGAIN)
3740 fd_cant_recv(fd);
3741 goto out;
3742 }
3743 buf->data = ret;
3744
Emeric Brun45c457a2020-07-09 23:23:34 +02003745 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003746 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003747 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003748
Emeric Brun54932b42020-07-07 09:43:24 +02003749 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3750
3751 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3752
3753 } while (--max_accept);
3754 }
3755
3756out:
3757 return;
3758}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003759
Emeric Brun12941c82020-07-07 14:19:42 +02003760/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003761 * IO Handler to handle message exchange with a syslog tcp client
3762 */
3763static void syslog_io_handler(struct appctx *appctx)
3764{
3765 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3766 struct stream_interface *si = appctx->owner;
3767 struct stream *s = si_strm(si);
3768 struct proxy *frontend = strm_fe(s);
3769 struct listener *l = strm_li(s);
3770 struct buffer *buf = get_trash_chunk();
3771 int max_accept;
3772 int to_skip;
3773 int facility;
3774 int level;
3775 char *message;
3776 size_t size;
3777
3778 max_accept = l->maxaccept ? l->maxaccept : 1;
Christopher Faulet1ce396c2023-04-17 16:34:29 +02003779 while (1) {
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003780 char c;
3781
3782 if (max_accept <= 0)
3783 goto missing_budget;
3784 max_accept--;
3785
3786 to_skip = co_getchar(si_oc(si), &c);
3787 if (!to_skip)
3788 goto missing_data;
3789 else if (to_skip < 0)
3790 goto cli_abort;
3791
3792 if (c == '<') {
3793 /* rfc-6587, Non-Transparent-Framing: messages separated by
3794 * a trailing LF or CR LF
3795 */
3796 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3797 if (!to_skip)
3798 goto missing_data;
3799 else if (to_skip < 0)
3800 goto cli_abort;
3801
3802 if (buf->area[to_skip - 1] != '\n')
3803 goto parse_error;
3804
3805 buf->data = to_skip - 1;
3806
3807 /* according to rfc-6587, some devices adds CR before LF */
3808 if (buf->data && buf->area[buf->data - 1] == '\r')
3809 buf->data--;
3810
3811 }
3812 else if ((unsigned char)(c - '1') <= 8) {
3813 /* rfc-6587, Octet-Counting: message length in ASCII
3814 * (first digit can not be ZERO), followed by a space
3815 * and message length
3816 */
3817 char *p = NULL;
3818 int msglen;
3819
3820 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3821 if (!to_skip)
3822 goto missing_data;
3823 else if (to_skip < 0)
3824 goto cli_abort;
3825
3826 if (buf->area[to_skip - 1] != ' ')
3827 goto parse_error;
3828
Aurelien DARRAGON5569aa92022-10-26 23:40:08 +02003829 msglen = strtol(buf->area, &p, 10);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003830 if (!msglen || p != &buf->area[to_skip - 1])
3831 goto parse_error;
3832
3833 /* message seems too large */
3834 if (msglen > buf->size)
3835 goto parse_error;
3836
3837 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3838 if (!msglen)
3839 goto missing_data;
3840 else if (msglen < 0)
3841 goto cli_abort;
3842
3843
3844 buf->data = msglen;
3845 to_skip += msglen;
3846 }
3847 else
3848 goto parse_error;
3849
3850 co_skip(si_oc(si), to_skip);
3851
3852 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003853 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003854 proxy_inc_fe_req_ctr(l, frontend);
3855
3856 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3857
3858 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3859
3860 }
3861
3862missing_data:
3863 /* we need more data to read */
3864 si_oc(si)->flags |= CF_READ_DONTWAIT;
3865
3866 return;
3867
3868missing_budget:
3869 /* it may remain some stuff to do, let's retry later */
3870 appctx_wakeup(appctx);
3871
3872 return;
3873
3874parse_error:
3875 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003876 _HA_ATOMIC_INC(&l->counters->failed_req);
3877 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003878
3879 goto close;
3880
3881cli_abort:
3882 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003883 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3884 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003885
3886close:
3887 si_shutw(si);
3888 si_shutr(si);
3889
3890 si_ic(si)->flags |= CF_READ_NULL;
3891
3892 return;
3893}
3894
3895static struct applet syslog_applet = {
3896 .obj_type = OBJ_TYPE_APPLET,
3897 .name = "<SYSLOG>", /* used for logging */
3898 .fct = syslog_io_handler,
3899 .release = NULL,
3900};
3901
3902/*
Emeric Brun12941c82020-07-07 14:19:42 +02003903 * Parse "log-forward" section and create corresponding sink buffer.
3904 *
3905 * The function returns 0 in success case, otherwise, it returns error
3906 * flags.
3907 */
3908int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3909{
Aurelien DARRAGON1a39bd22023-07-03 18:33:18 +02003910 int err_code = ERR_NONE;
Emeric Brun12941c82020-07-07 14:19:42 +02003911 struct proxy *px;
3912 char *errmsg = NULL;
3913 const char *err = NULL;
3914
3915 if (strcmp(args[0], "log-forward") == 0) {
3916 if (!*args[1]) {
Aurelien DARRAGONab89d8b2023-07-03 17:35:54 +02003917 ha_alert("parsing [%s:%d] : missing name for log-forward section.\n", file, linenum);
Emeric Brun12941c82020-07-07 14:19:42 +02003918 err_code |= ERR_ALERT | ERR_ABORT;
3919 goto out;
3920 }
3921
3922 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3923 goto out;
3924
3925 err = invalid_char(args[1]);
3926 if (err) {
3927 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3928 file, linenum, *err, args[0], args[1]);
3929 err_code |= ERR_ALERT | ERR_ABORT;
3930 goto out;
3931 }
3932
Emeric Brunb0c331f2020-10-07 17:05:59 +02003933 px = log_forward_by_name(args[1]);
3934 if (px) {
3935 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3936 file, linenum, args[1], px->conf.file, px->conf.line);
3937 err_code |= ERR_ALERT | ERR_FATAL;
Aurelien DARRAGON1a39bd22023-07-03 18:33:18 +02003938 goto out;
Emeric Brunb0c331f2020-10-07 17:05:59 +02003939 }
3940
3941 px = proxy_find_by_name(args[1], 0, 0);
3942 if (px) {
3943 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3944 file, linenum, args[1], proxy_type_str(px),
3945 px->id, px->conf.file, px->conf.line);
3946 err_code |= ERR_ALERT | ERR_FATAL;
Aurelien DARRAGON1a39bd22023-07-03 18:33:18 +02003947 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003948 }
3949
3950 px = calloc(1, sizeof *px);
3951 if (!px) {
3952 err_code |= ERR_ALERT | ERR_FATAL;
3953 goto out;
3954 }
3955
Emeric Brun9ecb3bc2021-12-01 12:08:42 +01003956 init_new_proxy(px);
Emeric Brun12941c82020-07-07 14:19:42 +02003957 px->next = cfg_log_forward;
3958 cfg_log_forward = px;
Emeric Brun12941c82020-07-07 14:19:42 +02003959 px->conf.file = strdup(file);
3960 px->conf.line = linenum;
3961 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003962 px->last_change = now.tv_sec;
3963 px->cap = PR_CAP_FE;
3964 px->maxconn = 10;
3965 px->timeout.client = TICK_ETERNITY;
3966 px->accept = frontend_accept;
3967 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003968 px->id = strdup(args[1]);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003969 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003970 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003971 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3972 err_code |= ERR_WARN;
3973
3974 if (*(args[1]) == 0) {
3975 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3976 err_code |= ERR_ALERT | ERR_FATAL;
3977 goto out;
3978 }
3979 cfg_log_forward->maxconn = atol(args[1]);
3980 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3981 goto out;
3982 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003983 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003984 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3985 err_code |= ERR_WARN;
3986
3987 if (*(args[1]) == 0) {
3988 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3989 err_code |= ERR_ALERT | ERR_FATAL;
3990 goto out;
3991 }
3992 cfg_log_forward->backlog = atol(args[1]);
3993 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3994 goto out;
3995 }
3996 else if (strcmp(args[0], "bind") == 0) {
3997 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003998 struct bind_conf *bind_conf;
3999 struct bind_kw *kw;
4000 struct listener *l;
4001
4002 cur_arg = 1;
4003
4004 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
4005 NULL, xprt_get(XPRT_RAW));
4006 if (!bind_conf) {
4007 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
4008 err_code |= ERR_ALERT | ERR_FATAL;
4009 goto out;
4010 }
4011
4012 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
4013 if (errmsg && *errmsg) {
4014 indent_msg(&errmsg, 2);
4015 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
4016 }
4017 else {
4018 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
4019 file, linenum, args[0], args[1], args[2]);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004020 }
Aurelien DARRAGON1a39bd22023-07-03 18:33:18 +02004021 err_code |= ERR_ALERT | ERR_FATAL;
4022 goto out;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004023 }
4024 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau66161322021-02-19 15:50:27 +01004025 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004026 l->accept = session_accept_fd;
4027 l->analysers |= cfg_log_forward->fe_req_ana;
4028 l->default_target = cfg_log_forward->default_target;
4029 global.maxsock++;
4030 }
4031 cur_arg++;
4032
4033 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
4034 int ret;
4035
4036 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
4037 err_code |= ret;
4038 if (ret) {
4039 if (errmsg && *errmsg) {
4040 indent_msg(&errmsg, 2);
4041 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
4042 }
4043 else
4044 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
4045 file, linenum, args[cur_arg]);
4046 if (ret & ERR_FATAL)
4047 goto out;
4048 }
4049 cur_arg += 1 + kw->skip;
4050 }
4051 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01004052 const char *best = bind_find_best_kw(args[cur_arg]);
4053 if (best)
4054 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
4055 file, linenum, args[cur_arg], cursection, best);
4056 else
4057 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
4058 file, linenum, args[cur_arg], cursection);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004059 err_code |= ERR_ALERT | ERR_FATAL;
4060 goto out;
4061 }
Emeric Brun12941c82020-07-07 14:19:42 +02004062 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02004063 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02004064 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02004065 struct bind_conf *bind_conf;
4066 struct bind_kw *kw;
4067 struct listener *l;
4068
4069 cur_arg = 1;
4070
4071 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
4072 NULL, xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +02004073 if (!bind_conf) {
4074 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
4075 err_code |= ERR_ALERT | ERR_FATAL;
4076 goto out;
4077 }
Emeric Brun12941c82020-07-07 14:19:42 +02004078
Willy Tarreau26ff5da2020-09-16 15:22:19 +02004079 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02004080 if (errmsg && *errmsg) {
4081 indent_msg(&errmsg, 2);
4082 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
4083 }
4084 else {
4085 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
4086 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02004087 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02004088 err_code |= ERR_ALERT | ERR_FATAL;
4089 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02004090 }
4091 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02004092 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreau66161322021-02-19 15:50:27 +01004093 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreaue140a692020-10-15 21:25:32 +02004094 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02004095 global.maxsock++;
4096 }
4097 cur_arg++;
4098
4099 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
4100 int ret;
4101
4102 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
4103 err_code |= ret;
4104 if (ret) {
4105 if (errmsg && *errmsg) {
4106 indent_msg(&errmsg, 2);
4107 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
4108 }
4109 else
4110 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
4111 file, linenum, args[cur_arg]);
4112 if (ret & ERR_FATAL)
4113 goto out;
4114 }
4115 cur_arg += 1 + kw->skip;
4116 }
4117 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01004118 const char *best = bind_find_best_kw(args[cur_arg]);
4119 if (best)
4120 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
4121 file, linenum, args[cur_arg], cursection, best);
4122 else
4123 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
4124 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02004125 err_code |= ERR_ALERT | ERR_FATAL;
4126 goto out;
4127 }
4128 }
4129 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02004130 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02004131 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4132 err_code |= ERR_ALERT | ERR_FATAL;
4133 goto out;
4134 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004135 }
4136 else if (strcmp(args[0], "timeout") == 0) {
4137 const char *res;
4138 unsigned timeout;
4139
4140 if (strcmp(args[1], "client") != 0) {
4141 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
4142 err_code |= ERR_ALERT | ERR_FATAL;
4143 goto out;
4144 }
4145
4146 if (*args[2] == 0) {
4147 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
4148 err_code |= ERR_ALERT | ERR_FATAL;
4149 goto out;
4150 }
4151 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
4152 if (res == PARSE_TIME_OVER) {
4153 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
4154 }
4155 else if (res == PARSE_TIME_UNDER) {
4156 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
4157 }
4158 else if (res) {
4159 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004160 }
4161
4162 if (res) {
4163 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4164 err_code |= ERR_ALERT | ERR_FATAL;
4165 goto out;
4166 }
4167 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02004168 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02004169 else {
4170 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
4171 err_code |= ERR_ALERT | ERR_ABORT;
4172 goto out;
4173 }
Emeric Brun12941c82020-07-07 14:19:42 +02004174out:
Aurelien DARRAGON91e28752023-07-04 15:36:45 +02004175 ha_free(&errmsg);
Emeric Brun12941c82020-07-07 14:19:42 +02004176 return err_code;
4177}
4178
4179
Willy Tarreau869efd52019-11-15 15:16:57 +01004180/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
4181static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004182{
Willy Tarreau869efd52019-11-15 15:16:57 +01004183 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4184 return 1;
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004185
Willy Tarreau869efd52019-11-15 15:16:57 +01004186 if (!startup_logs)
4187 return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
4188
4189 return ring_attach_cli(startup_logs, appctx);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004190}
4191
4192/* register cli keywords */
4193static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004194 { { "show", "startup-logs", NULL }, "show startup-logs : report logs emitted during HAProxy startup", cli_parse_show_startup_logs, NULL, NULL },
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004195 {{},}
4196}};
4197
Willy Tarreau0108d902018-11-25 19:14:37 +01004198INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4199
Emeric Brun12941c82020-07-07 14:19:42 +02004200/* config parsers for this section */
4201REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4202
Willy Tarreau082b6282019-05-22 14:42:12 +02004203REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4204REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004205
Willy Tarreaubaaee002006-06-26 02:48:02 +02004206/*
4207 * Local variables:
4208 * c-indent-level: 8
4209 * c-basic-offset: 8
4210 * End:
4211 */