blob: 781f0afc79b73c8435a497cb32cabf84a84ef7a8 [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));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200859 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200860 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200861 LIST_INIT(&node->list);
Willy Tarreau2b718102021-04-21 07:32:39 +0200862 LIST_APPEND(logsrvs, &node->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200863 node->conf.file = strdup(file);
864 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200865
866 skip_logsrv:
867 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200868 }
869 return 1;
870 }
871
872 /*
873 * "log <address> ...: parse a syslog server line
874 */
875 if (*(args[1]) == 0 || *(args[2]) == 0) {
876 memprintf(err, "expects <address> and <facility> %s as arguments",
877 ((logsrvs == &global.logsrvs) ? "" : "or global"));
878 goto error;
879 }
880
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100881 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
882 if (strcmp(args[1], "stdout") == 0)
883 args[1] = "fd@1";
884 else if (strcmp(args[1], "stderr") == 0)
885 args[1] = "fd@2";
886
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200887 logsrv = calloc(1, sizeof(*logsrv));
888 if (!logsrv) {
889 memprintf(err, "out of memory");
890 goto error;
891 }
892
Emeric Brun9533a702021-04-02 10:13:43 +0200893 logsrv->conf.file = strdup(file);
894 logsrv->conf.line = linenum;
895
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200896 /* skip address for now, it will be parsed at the end */
897 cur_arg = 2;
898
899 /* just after the address, a length may be specified */
900 logsrv->maxlen = MAX_SYSLOG_LEN;
901 if (strcmp(args[cur_arg], "len") == 0) {
902 int len = atoi(args[cur_arg+1]);
903 if (len < 80 || len > 65535) {
904 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
905 args[cur_arg+1]);
906 goto error;
907 }
908 logsrv->maxlen = len;
909 cur_arg += 2;
910 }
911 if (logsrv->maxlen > global.max_syslog_len)
912 global.max_syslog_len = logsrv->maxlen;
913
914 /* after the length, a format may be specified */
915 if (strcmp(args[cur_arg], "format") == 0) {
916 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200917 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200918 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
919 goto error;
920 }
921 cur_arg += 2;
922 }
923
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200924 if (strcmp(args[cur_arg], "sample") == 0) {
925 unsigned low, high;
926 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200927 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
928
929 p = args[cur_arg+1];
930 smp_sz_str = strchr(p, ':');
931 if (!smp_sz_str) {
932 memprintf(err, "Missing sample size");
933 goto error;
934 }
935
936 *smp_sz_str++ = '\0';
937
938 end = p + strlen(p);
939
940 while (p != end) {
941 if (!get_logsrv_smp_range(&low, &high, &p, err))
942 goto error;
943
944 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
945 goto error;
946
947 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
948 if (!smp_rgs) {
949 memprintf(err, "out of memory error");
950 goto error;
951 }
952
953 smp_rgs[smp_rgs_sz].low = low;
954 smp_rgs[smp_rgs_sz].high = high;
955 smp_rgs[smp_rgs_sz].sz = high - low + 1;
956 smp_rgs[smp_rgs_sz].curr_idx = 0;
957 if (smp_rgs[smp_rgs_sz].high > smp_sz)
958 smp_sz = smp_rgs[smp_rgs_sz].high;
959 smp_rgs_sz++;
960 }
961
Tim Duesterhus21648002019-06-23 22:10:10 +0200962 if (smp_rgs == NULL) {
963 memprintf(err, "no sampling ranges given");
964 goto error;
965 }
966
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200967 beg = smp_sz_str;
968 end = beg + strlen(beg);
969 new_smp_sz = read_uint((const char **)&beg, end);
970 if (!new_smp_sz || beg != end) {
971 memprintf(err, "wrong sample size '%s' for sample range '%s'",
972 smp_sz_str, args[cur_arg+1]);
973 goto error;
974 }
975
976 if (new_smp_sz < smp_sz) {
977 memprintf(err, "sample size %zu should be greater or equal to "
978 "%zu the maximum of the high ranges limits",
979 new_smp_sz, smp_sz);
980 goto error;
981 }
982 smp_sz = new_smp_sz;
983
984 /* Let's order <smp_rgs> array. */
985 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
986
987 logsrv->lb.smp_rgs = smp_rgs;
988 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
989 logsrv->lb.smp_sz = smp_sz;
990
991 cur_arg += 2;
992 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200993 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200994 /* parse the facility */
995 logsrv->facility = get_log_facility(args[cur_arg]);
996 if (logsrv->facility < 0) {
997 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
998 goto error;
999 }
1000 cur_arg++;
1001
1002 /* parse the max syslog level (default: debug) */
1003 logsrv->level = 7;
1004 if (*(args[cur_arg])) {
1005 logsrv->level = get_log_level(args[cur_arg]);
1006 if (logsrv->level < 0) {
1007 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
1008 goto error;
1009 }
1010 cur_arg++;
1011 }
1012
1013 /* parse the limit syslog level (default: emerg) */
1014 logsrv->minlvl = 0;
1015 if (*(args[cur_arg])) {
1016 logsrv->minlvl = get_log_level(args[cur_arg]);
1017 if (logsrv->minlvl < 0) {
1018 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1019 goto error;
1020 }
1021 cur_arg++;
1022 }
1023
1024 /* Too many args */
1025 if (*(args[cur_arg])) {
1026 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1027 goto error;
1028 }
1029
1030 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001031 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001032 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001033 logsrv->addr.ss_family = AF_UNSPEC;
1034 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001035 logsrv->sink = NULL;
1036 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001037 goto done;
1038 }
1039
Emeric Brun94aab062021-04-02 10:41:36 +02001040 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
1041 err, NULL, NULL,
1042 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 +02001043 if (!sk)
1044 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001045
1046 if (fd != -1)
1047 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001048 logsrv->addr = *sk;
1049
1050 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001051 if (!port1)
1052 set_host_port(&logsrv->addr, SYSLOG_PORT);
1053 }
Emeric Brun9533a702021-04-02 10:13:43 +02001054
Emeric Brun26754902021-04-07 14:26:44 +02001055 if (proto && proto->ctrl_type == SOCK_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +02001056 static unsigned long ring_ids;
1057
1058 /* Implicit sink buffer will be
1059 * initialized in post_check
1060 */
1061 logsrv->type = LOG_TARGET_BUFFER;
1062 logsrv->sink = NULL;
1063 /* compute uniq name for the ring */
1064 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
1065 }
1066
Willy Tarreauc046d162019-08-30 15:24:59 +02001067 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001068 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001069 return 1;
1070
1071 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001072 free(smp_rgs);
Emeric Brun9533a702021-04-02 10:13:43 +02001073 if (logsrv) {
1074 free(logsrv->conf.file);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001075 free(logsrv->ring_name);
Emeric Brun9533a702021-04-02 10:13:43 +02001076 }
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001077 free(logsrv);
1078 return 0;
1079}
1080
1081
Christopher Fauletd4696382017-10-24 11:44:05 +02001082/* Generic function to display messages prefixed by a label */
1083static void print_message(const char *label, const char *fmt, va_list argp)
1084{
Christopher Fauletd4696382017-10-24 11:44:05 +02001085 char *head, *msg;
Willy Tarreau9f903af2021-05-07 08:42:39 +02001086 char prefix[11]; // '[' + 8 chars + ']' + 0.
Christopher Fauletd4696382017-10-24 11:44:05 +02001087
Willy Tarreau9f903af2021-05-07 08:42:39 +02001088 *prefix = '[';
1089 strncpy(prefix + 1, label, sizeof(prefix) - 2);
1090 msg = prefix + strlen(prefix);
1091 *msg++ = ']';
1092 while (msg < prefix + sizeof(prefix) - 1)
1093 *msg++ = ' ';
1094 *msg = 0;
Christopher Fauletd4696382017-10-24 11:44:05 +02001095
Willy Tarreau9f903af2021-05-07 08:42:39 +02001096 head = msg = NULL;
1097 memprintf(&head, "%s (%u) : ", prefix, (uint)getpid());
Christopher Fauletd4696382017-10-24 11:44:05 +02001098 memvprintf(&msg, fmt, argp);
1099
Willy Tarreau869efd52019-11-15 15:16:57 +01001100 if (global.mode & MODE_STARTING) {
1101 if (unlikely(!startup_logs))
1102 startup_logs = ring_new(STARTUP_LOG_SIZE);
1103
1104 if (likely(startup_logs)) {
1105 struct ist m[2];
1106
1107 m[0] = ist(head);
1108 m[1] = ist(msg);
1109 /* trim the trailing '\n' */
1110 if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
1111 m[1].len--;
1112 ring_write(startup_logs, ~0, 0, 0, m, 2);
1113 }
1114 }
Christopher Fauletd4696382017-10-24 11:44:05 +02001115
1116 fprintf(stderr, "%s%s", head, msg);
1117 fflush(stderr);
1118
1119 free(head);
1120 free(msg);
1121}
1122
Willy Tarreaubaaee002006-06-26 02:48:02 +02001123/*
1124 * Displays the message on stderr with the date and pid. Overrides the quiet
1125 * mode during startup.
1126 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001127void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001128{
1129 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001130
1131 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaubb869862020-04-16 10:52:41 +02001132 if (!(warned & WARN_EXEC_PATH)) {
1133 const char *path = get_exec_path();
1134
1135 warned |= WARN_EXEC_PATH;
1136 ha_notice("haproxy version is %s\n", haproxy_version);
1137 if (path)
1138 ha_notice("path to executable is %s\n", path);
1139 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001140 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001141 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001142 va_end(argp);
1143 }
1144}
1145
1146
1147/*
1148 * Displays the message on stderr with the date and pid.
1149 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001150void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001151{
1152 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153
Willy Tarreaubebd2122020-04-15 16:06:11 +02001154 warned |= WARN_ANY;
1155
Willy Tarreaubaaee002006-06-26 02:48:02 +02001156 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1157 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001158 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001159 va_end(argp);
1160 }
1161}
1162
1163/*
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001164 * Variant of _ha_diag_warning with va_list.
1165 * Use it only if MODE_DIAG has been previously checked.
1166 */
1167void _ha_vdiag_warning(const char *fmt, va_list argp)
1168{
Willy Tarreau9f903af2021-05-07 08:42:39 +02001169 print_message("DIAG", fmt, argp);
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001170}
1171
1172/*
1173 * Output a diagnostic warning.
1174 * Use it only if MODE_DIAG has been previously checked.
1175 */
1176void _ha_diag_warning(const char *fmt, ...)
1177{
1178 va_list argp;
1179
1180 va_start(argp, fmt);
1181 _ha_vdiag_warning(fmt, argp);
1182 va_end(argp);
1183}
1184
1185/*
1186 * Output a diagnostic warning. Do nothing of MODE_DIAG is not on.
1187 */
1188void ha_diag_warning(const char *fmt, ...)
1189{
1190 va_list argp;
1191
1192 if (global.mode & MODE_DIAG) {
1193 va_start(argp, fmt);
1194 _ha_vdiag_warning(fmt, argp);
1195 va_end(argp);
1196 }
1197}
1198
1199/*
William Lallemand9c56a222018-11-21 18:04:52 +01001200 * Displays the message on stderr with the date and pid.
1201 */
1202void ha_notice(const char *fmt, ...)
1203{
1204 va_list argp;
1205
1206 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1207 va_start(argp, fmt);
1208 print_message("NOTICE", fmt, argp);
1209 va_end(argp);
1210 }
1211}
1212
1213/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001214 * Displays the message on <out> only if quiet mode is not set.
1215 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001216void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001217{
1218 va_list argp;
1219
1220 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1221 va_start(argp, fmt);
1222 vfprintf(out, fmt, argp);
1223 fflush(out);
1224 va_end(argp);
1225 }
1226}
1227
1228/*
Emeric Brun54648852020-07-06 15:54:06 +02001229 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001230 */
Emeric Brun54648852020-07-06 15:54:06 +02001231enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001232{
Emeric Brun54648852020-07-06 15:54:06 +02001233 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001234
1235 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001236 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001237 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001238 format--;
1239
Emeric Brun54648852020-07-06 15:54:06 +02001240 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001241 return format;
1242}
1243
1244/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001245 * returns log level for <lev> or -1 if not found.
1246 */
1247int get_log_level(const char *lev)
1248{
1249 int level;
1250
1251 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001252 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001253 level--;
1254
1255 return level;
1256}
1257
Willy Tarreaubaaee002006-06-26 02:48:02 +02001258/*
1259 * returns log facility for <fac> or -1 if not found.
1260 */
1261int get_log_facility(const char *fac)
1262{
1263 int facility;
1264
1265 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001266 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001267 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001268
Willy Tarreaubaaee002006-06-26 02:48:02 +02001269 return facility;
1270}
1271
William Lallemanda1cc3812012-02-08 16:38:44 +01001272/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001273 * Encode the string.
1274 *
1275 * When using the +E log format option, it will try to escape '"\]'
1276 * characters with '\' as prefix. The same prefix should not be used as
1277 * <escape>.
1278 */
1279static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001280 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001281 const char *string,
1282 struct logformat_node *node)
1283{
1284 if (node->options & LOG_OPT_ESC) {
1285 if (start < stop) {
1286 stop--; /* reserve one byte for the final '\0' */
1287 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001288 if (!ha_bit_test((unsigned char)(*string), map)) {
1289 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001290 *start++ = *string;
1291 else {
1292 if (start + 2 >= stop)
1293 break;
1294 *start++ = '\\';
1295 *start++ = *string;
1296 }
1297 }
1298 else {
1299 if (start + 3 >= stop)
1300 break;
1301 *start++ = escape;
1302 *start++ = hextab[(*string >> 4) & 15];
1303 *start++ = hextab[*string & 15];
1304 }
1305 string++;
1306 }
1307 *start = '\0';
1308 }
1309 }
1310 else {
1311 return encode_string(start, stop, escape, map, string);
1312 }
1313
1314 return start;
1315}
1316
1317/*
1318 * Encode the chunk.
1319 *
1320 * When using the +E log format option, it will try to escape '"\]'
1321 * characters with '\' as prefix. The same prefix should not be used as
1322 * <escape>.
1323 */
1324static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001325 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001326 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001327 struct logformat_node *node)
1328{
1329 char *str, *end;
1330
1331 if (node->options & LOG_OPT_ESC) {
1332 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001333 str = chunk->area;
1334 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001335
1336 stop--; /* reserve one byte for the final '\0' */
1337 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001338 if (!ha_bit_test((unsigned char)(*str), map)) {
1339 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001340 *start++ = *str;
1341 else {
1342 if (start + 2 >= stop)
1343 break;
1344 *start++ = '\\';
1345 *start++ = *str;
1346 }
1347 }
1348 else {
1349 if (start + 3 >= stop)
1350 break;
1351 *start++ = escape;
1352 *start++ = hextab[(*str >> 4) & 15];
1353 *start++ = hextab[*str & 15];
1354 }
1355 str++;
1356 }
1357 *start = '\0';
1358 }
1359 }
1360 else {
1361 return encode_chunk(start, stop, escape, map, chunk);
1362 }
1363
1364 return start;
1365}
1366
1367/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001368 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001369 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001370 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001371 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001372 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001373char *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 +01001374{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001375 if (size < 2)
1376 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001377
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001378 if (node->options & LOG_OPT_QUOTE) {
1379 *(dst++) = '"';
1380 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001381 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001382
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001383 if (src && len) {
Aurelien DARRAGONa5c621a2022-09-20 14:33:18 +02001384 /* escape_string and strlcpy2 will both try to add terminating NULL-byte
1385 * to dst, so we need to make sure that extra byte will fit into dst
1386 * before calling them
1387 */
Dragan Dosen835b9212016-02-12 13:23:03 +01001388 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001389 char *ret;
1390
Aurelien DARRAGONa5c621a2022-09-20 14:33:18 +02001391 ret = escape_string(dst, (dst + size - 1), '\\', rfc5424_escape_map, src, src + len);
Dragan Dosen835b9212016-02-12 13:23:03 +01001392 if (ret == NULL || *ret != '\0')
1393 return NULL;
1394 len = ret - dst;
1395 }
1396 else {
Aurelien DARRAGONa5c621a2022-09-20 14:33:18 +02001397 if (++len > size)
1398 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001399 len = strlcpy2(dst, src, len);
1400 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001401
1402 size -= len;
1403 dst += len;
1404 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001405 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1406 if (size < 2)
1407 return NULL;
1408 *(dst++) = '-';
Aurelien DARRAGONa5c621a2022-09-20 14:33:18 +02001409 size -= 1;
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001410 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001411
1412 if (node->options & LOG_OPT_QUOTE) {
1413 if (size < 2)
1414 return NULL;
1415 *(dst++) = '"';
1416 }
1417
1418 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001419 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001420}
1421
Willy Tarreau26ffa852018-09-05 15:23:10 +02001422static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001423{
1424 return lf_text_len(dst, src, size, size, node);
1425}
1426
William Lallemand5f232402012-04-05 18:02:55 +02001427/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001428 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001429 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001430 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001431char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001432{
1433 char *ret = dst;
1434 int iret;
1435 char pn[INET6_ADDRSTRLEN];
1436
1437 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001438 unsigned char *addr = NULL;
1439 switch (sockaddr->sa_family) {
1440 case AF_INET:
1441 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1442 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1443 break;
1444 case AF_INET6:
1445 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1446 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1447 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1448 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1449 break;
1450 default:
1451 return NULL;
1452 }
William Lallemand5f232402012-04-05 18:02:55 +02001453 if (iret < 0 || iret > size)
1454 return NULL;
1455 ret += iret;
1456 } else {
1457 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1458 ret = lf_text(dst, pn, size, node);
1459 if (ret == NULL)
1460 return NULL;
1461 }
1462 return ret;
1463}
1464
1465/*
1466 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001467 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001468 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001469char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001470{
1471 char *ret = dst;
1472 int iret;
1473
1474 if (node->options & LOG_OPT_HEXA) {
1475 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1476 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1477 if (iret < 0 || iret > size)
1478 return NULL;
1479 ret += iret;
1480 } else {
1481 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1482 if (ret == NULL)
1483 return NULL;
1484 }
1485 return ret;
1486}
1487
Emeric Brun54648852020-07-06 15:54:06 +02001488
1489/*
1490 * This function sends the syslog message using a printf format string. It
1491 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001492 */
Emeric Brun54648852020-07-06 15:54:06 +02001493void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001494{
Emeric Brun54648852020-07-06 15:54:06 +02001495 va_list argp;
1496 int data_len;
1497
1498 if (level < 0 || format == NULL || logline == NULL)
1499 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001500
Emeric Brun54648852020-07-06 15:54:06 +02001501 va_start(argp, format);
1502 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1503 if (data_len < 0 || data_len > global.max_syslog_len)
1504 data_len = global.max_syslog_len;
1505 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001506
Emeric Brun54648852020-07-06 15:54:06 +02001507 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1508 logline, data_len, default_rfc5424_sd_log_format, 2);
1509}
1510/*
1511 * This function builds a log header of given format using given
1512 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1513 * to determine format based on given metadas. It is useful
1514 * for log-forwarding to be able to forward any format without
1515 * settings.
1516 * This function returns a struct ist array of elements of the header
1517 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001518 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001519 * elements.
1520 */
1521struct ist *build_log_header(enum log_fmt format, int level, int facility,
1522 struct ist *metadata, size_t *nbelem)
1523{
1524 static THREAD_LOCAL struct {
1525 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1526 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1527 time_t cur_legacy_time;
1528 char priority_buffer[6];
1529 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001530
Emeric Brun54648852020-07-06 15:54:06 +02001531 struct tm logtime;
1532 int len;
1533 int fac_level = 0;
1534 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001535
Emeric Brun54648852020-07-06 15:54:06 +02001536 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001537
Emeric Brun54648852020-07-06 15:54:06 +02001538
1539 if (format == LOG_FORMAT_UNSPEC) {
1540 format = LOG_FORMAT_RAW;
1541 if (metadata) {
1542 /* If a hostname is set, it appears we want to perform syslog
1543 * because only rfc5427 or rfc3164 support an hostname.
1544 */
1545 if (metadata[LOG_META_HOST].len) {
1546 /* If a rfc5424 compliant timestamp is used we consider
1547 * that output format is rfc5424, else legacy format
1548 * is used as specified default for local logs
1549 * in documentation.
1550 */
1551 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1552 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1553 format = LOG_FORMAT_RFC5424;
1554 else
1555 format = LOG_FORMAT_RFC3164;
1556 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001557 else if (metadata[LOG_META_TAG].len) {
1558 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001559 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001560 * in legacy format (analog to RFC3164 but
1561 * with stripped hostname).
1562 */
1563 format = LOG_FORMAT_LOCAL;
1564 }
Emeric Brun54648852020-07-06 15:54:06 +02001565 else if (metadata[LOG_META_PRIO].len) {
1566 /* the source seems a parsed message
1567 * offering a valid level/prio prefix
1568 * so we consider this format.
1569 */
1570 format = LOG_FORMAT_PRIO;
1571 }
1572 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001573 }
1574
Emeric Brun54648852020-07-06 15:54:06 +02001575 /* prepare priority, stored into 1 single elem */
1576 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001577 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001578 case LOG_FORMAT_RFC3164:
1579 case LOG_FORMAT_RFC5424:
1580 case LOG_FORMAT_PRIO:
1581 fac_level = facility << 3;
1582 /* further format ignore the facility */
1583 /* fall through */
1584 case LOG_FORMAT_TIMED:
1585 case LOG_FORMAT_SHORT:
1586 fac_level += level;
1587 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1588 do {
1589 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1590 fac_level /= 10;
1591 hdr_ctx.ist_vector[*nbelem].ptr--;
1592 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1593 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1594 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1595 break;
1596 case LOG_FORMAT_ISO:
1597 case LOG_FORMAT_RAW:
1598 break;
1599 case LOG_FORMAT_UNSPEC:
1600 case LOG_FORMATS:
1601 ABORT_NOW();
1602 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001603
William Lallemand2a4a44f2012-02-06 16:00:33 +01001604
Emeric Brun54648852020-07-06 15:54:06 +02001605 /* prepare timestamp, stored into a max of 4 elems */
1606 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001607 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001608 case LOG_FORMAT_RFC3164:
1609 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1610 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1611 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1612 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001613 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001614 break;
1615 }
1616 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1617 int month;
1618 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001619
Emeric Brun54648852020-07-06 15:54:06 +02001620 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001621
Emeric Brun54648852020-07-06 15:54:06 +02001622 /* compute month */
1623 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1624 if (month)
1625 month--;
1626 if (month <= 11) {
1627 /* builds log prefix ex: 'Jan 1 ' */
1628 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1629 "%s %c%c ", monthname[month],
1630 timestamp[8] != '0' ? timestamp[8] : ' ',
1631 timestamp[9]);
1632 /* we reused the timestamp_buffer, signal that it does not
1633 * contain local time anymore
1634 */
1635 hdr_ctx.cur_legacy_time = 0;
1636 if (len == 7) {
1637 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1638 /* adds 'HH:MM:SS' from iso time */
1639 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1640 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1641 /* we successfully reuse iso time, we can break */
1642 break;
1643 }
1644 }
1645 /* Failed to reuse isotime time, fallback to local legacy time */
1646 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001647
Emeric Brun54648852020-07-06 15:54:06 +02001648 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1649 /* re-builds timestamp from the current local time */
1650 get_localtime(time, &logtime);
1651
1652 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1653 "%s %2d %02d:%02d:%02d ",
1654 monthname[logtime.tm_mon],
1655 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1656 if (len != LOG_LEGACYTIME_LEN+1)
1657 hdr_ctx.cur_legacy_time = 0;
1658 else
1659 hdr_ctx.cur_legacy_time = time;
1660 }
1661 if (likely(hdr_ctx.cur_legacy_time))
1662 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1663 else
1664 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1665 break;
1666 case LOG_FORMAT_RFC5424:
1667 /* adds rfc5425 version prefix */
1668 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1669 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001670 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001671 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1672 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1673 break;
1674 }
1675 /* let continue as 'timed' and 'iso' format for usual timestamp */
1676 /* fall through */
1677 case LOG_FORMAT_TIMED:
1678 case LOG_FORMAT_ISO:
1679 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1680 * '1900:01:01T14:00:00+02:00'
1681 * '1900:01:01T10:00:00.123456-02:00'
1682 */
1683 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1684 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1685 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001686 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001687 break;
1688 }
1689 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1690 int month;
1691 char *timestamp = metadata[LOG_META_TIME].ptr;
1692
1693 for (month = 0; month < 12; month++)
1694 if (!memcmp(monthname[month], timestamp, 3))
1695 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001696
Emeric Brun54648852020-07-06 15:54:06 +02001697 if (month < 12) {
1698
1699 /* get local time to retrieve year */
1700 get_localtime(time, &logtime);
1701
1702 /* year seems changed since log */
1703 if (logtime.tm_mon < month)
1704 logtime.tm_year--;
1705
1706 /* builds rfc5424 prefix ex: '1900-01-01T' */
1707 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1708 "%4d-%02d-%c%cT",
1709 logtime.tm_year+1900, month+1,
1710 timestamp[4] != ' ' ? timestamp[4] : '0',
1711 timestamp[5]);
1712
1713 /* we reused the timestamp_buffer, signal that it does not
1714 * contain local time anymore
1715 */
1716 hdr_ctx.cur_legacy_time = 0;
1717 if (len == 11) {
1718 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1719 /* adds HH:MM:SS from legacy timestamp */
1720 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001721 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001722 /* according to rfc: -00:00 means we don't know the timezone */
1723 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1724 /* we successfully reuse legacy time, we can break */
1725 break;
1726 }
1727 }
1728 /* Failed to reuse legacy time, fallback to local iso time */
1729 }
1730 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1731 break;
1732 case LOG_FORMAT_PRIO:
1733 case LOG_FORMAT_SHORT:
1734 case LOG_FORMAT_RAW:
1735 break;
1736 case LOG_FORMAT_UNSPEC:
1737 case LOG_FORMATS:
1738 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001739 }
1740
Emeric Brun54648852020-07-06 15:54:06 +02001741 /* prepare other meta data, stored into a max of 10 elems */
1742 switch (format) {
1743 case LOG_FORMAT_RFC3164:
1744 if (metadata && metadata[LOG_META_HOST].len) {
1745 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1746 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1747 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001748 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001749 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001750 /* fall through */
1751 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001752 if (!metadata || !metadata[LOG_META_TAG].len)
1753 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001754
Emeric Brun54648852020-07-06 15:54:06 +02001755 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1756 if (metadata[LOG_META_PID].len) {
1757 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1758 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1759 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1760 }
1761 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1762 break;
1763 case LOG_FORMAT_RFC5424:
1764 if (metadata && metadata[LOG_META_HOST].len) {
1765 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1766 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1767 }
1768 else
1769 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001770
Emeric Brun54648852020-07-06 15:54:06 +02001771 if (metadata && metadata[LOG_META_TAG].len) {
1772 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1773 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1774 }
1775 else
1776 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001777
Emeric Brun54648852020-07-06 15:54:06 +02001778 if (metadata && metadata[LOG_META_PID].len) {
1779 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1780 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1781 }
1782 else
1783 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001784
Emeric Brun54648852020-07-06 15:54:06 +02001785 if (metadata && metadata[LOG_META_MSGID].len) {
1786 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1787 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1788 }
1789 else
1790 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001791
Emeric Brun54648852020-07-06 15:54:06 +02001792 if (metadata && metadata[LOG_META_STDATA].len) {
1793 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1794 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1795 }
1796 else
1797 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1798 break;
1799 case LOG_FORMAT_PRIO:
1800 case LOG_FORMAT_SHORT:
1801 case LOG_FORMAT_TIMED:
1802 case LOG_FORMAT_ISO:
1803 case LOG_FORMAT_RAW:
1804 break;
1805 case LOG_FORMAT_UNSPEC:
1806 case LOG_FORMATS:
1807 ABORT_NOW();
1808 }
1809
1810 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001811}
1812
1813/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001814 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001815 * The argument <metadata> MUST be an array of size
1816 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001817 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001818 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001819 */
Emeric Brun54648852020-07-06 15:54:06 +02001820static 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 +01001821{
Emeric Brun54648852020-07-06 15:54:06 +02001822 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001823 static THREAD_LOCAL struct msghdr msghdr = {
1824 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001825 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001826 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001827 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1828 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001829 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001830 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001831 size_t nbelem;
1832 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001833
1834 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001835
Emeric Brunfa9d7802020-05-28 14:21:33 +02001836 /* historically some messages used to already contain the trailing LF
1837 * or Zero. Let's remove all trailing LF or Zero
1838 */
Emeric Brun54648852020-07-06 15:54:06 +02001839 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001840 size--;
1841
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001842 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001843 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001844 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001845 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001846 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1847 /* the socket's address is a file descriptor */
1848 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1849 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001850 else if (logsrv->addr.ss_family == AF_UNIX)
1851 plogfd = &logfdunix;
1852 else
1853 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001854
Willy Tarreauc046d162019-08-30 15:24:59 +02001855 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001856 /* socket not successfully initialized yet */
1857 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1858 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1859 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001860
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001861 if (!once) {
1862 once = 1; /* note: no need for atomic ops here */
1863 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1864 nblogger, strerror(errno), errno);
1865 }
1866 return;
1867 } else {
1868 /* we don't want to receive anything on this socket */
1869 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1870 /* does nothing under Linux, maybe needed for others */
1871 shutdown(*plogfd, SHUT_RD);
1872 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1873 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001874 }
1875
Emeric Brun54648852020-07-06 15:54:06 +02001876 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1877 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001878 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001879 struct ist msg;
1880
1881 msg = ist2(message, size);
1882 if (msg.len > logsrv->maxlen)
1883 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001884
Christopher Fauleta676b482022-10-19 08:16:48 +02001885 sent = sink_write(logsrv->sink, &msg, 1, level, facility, metadata);
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001886 }
1887 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1888 struct ist msg;
1889
1890 msg = ist2(message, size);
1891 if (msg.len > logsrv->maxlen)
1892 msg.len = logsrv->maxlen;
1893
1894 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001895 }
1896 else {
Emeric Brun54648852020-07-06 15:54:06 +02001897 int i = 0;
1898 int totlen = logsrv->maxlen;
1899
1900 for (i = 0 ; i < nbelem ; i++ ) {
1901 iovec[i].iov_base = msg_header[i].ptr;
1902 iovec[i].iov_len = msg_header[i].len;
1903 if (totlen <= iovec[i].iov_len) {
1904 iovec[i].iov_len = totlen;
1905 totlen = 0;
1906 break;
1907 }
1908 totlen -= iovec[i].iov_len;
1909 }
1910 if (totlen) {
1911 iovec[i].iov_base = message;
1912 iovec[i].iov_len = size;
1913 if (totlen <= iovec[i].iov_len)
1914 iovec[i].iov_len = totlen;
1915 i++;
1916 }
1917 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1918 iovec[i].iov_len = 1;
1919 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001920
Emeric Brun54648852020-07-06 15:54:06 +02001921 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001922 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1923 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001924
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001925 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1926 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001927
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001928 if (sent < 0) {
1929 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001930
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001931 if (errno == EAGAIN)
Willy Tarreau4781b152021-04-06 13:53:36 +02001932 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001933 else if (!once) {
1934 once = 1; /* note: no need for atomic ops here */
1935 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1936 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001937 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001938 }
1939}
Dragan Dosen59cee972015-09-19 22:09:02 +02001940
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001941/*
1942 * This function sends a syslog message.
1943 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001944 * The argument <metadata> MUST be an array of size
1945 * LOG_META_FIELDS*sizeof(struct ist) containing
1946 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001947 */
Emeric Brun54648852020-07-06 15:54:06 +02001948void process_send_log(struct list *logsrvs, int level, int facility,
1949 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001950{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001951 struct logsrv *logsrv;
1952 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001953
1954 /* Send log messages to syslog server. */
1955 nblogger = 0;
1956 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001957 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001958
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001959 /* we can filter the level of the messages that are sent to each logger */
1960 if (level > logsrv->level)
1961 continue;
1962
Frédéric Lécailled803e472019-04-25 07:42:09 +02001963 if (logsrv->lb.smp_rgs) {
1964 struct smp_log_range *curr_rg;
1965
1966 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1967 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1968 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1969 if (in_range) {
1970 /* Let's consume this range. */
1971 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1972 if (!curr_rg->curr_idx) {
1973 /* If consumed, let's select the next range. */
1974 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1975 }
1976 }
1977 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1978 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1979 }
1980 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001981 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1982 (facility == -1) ? logsrv->facility : facility,
1983 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001984 }
1985}
1986
Emeric Brun54648852020-07-06 15:54:06 +02001987/*
1988 * This function sends a syslog message.
1989 * It doesn't care about errors nor does it report them.
1990 * The arguments <sd> and <sd_size> are used for the structured-data part
1991 * in RFC5424 formatted syslog messages.
1992 */
1993void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1994 char *message, size_t size, char *sd, size_t sd_size)
1995{
1996 static THREAD_LOCAL pid_t curr_pid;
1997 static THREAD_LOCAL char pidstr[16];
1998 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1999
2000 if (logsrvs == NULL) {
2001 if (!LIST_ISEMPTY(&global.logsrvs)) {
2002 logsrvs = &global.logsrvs;
2003 }
2004 }
2005 if (!logsrvs || LIST_ISEMPTY(logsrvs))
2006 return;
2007
2008 if (!metadata[LOG_META_HOST].len) {
2009 if (global.log_send_hostname)
2010 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02002011 }
2012
2013 if (!tagb || !tagb->area)
2014 tagb = &global.log_tag;
2015
2016 if (tagb)
2017 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
2018
2019 if (unlikely(curr_pid != getpid()))
2020 metadata[LOG_META_PID].len = 0;
2021
2022 if (!metadata[LOG_META_PID].len) {
2023 curr_pid = getpid();
2024 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
2025 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
2026 }
2027
2028 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
2029
2030 /* Remove trailing space of structured data */
2031 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
2032 metadata[LOG_META_STDATA].len--;
2033
2034 return process_send_log(logsrvs, level, -1, metadata, message, size);
2035}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002036
Willy Tarreauc89ccb62012-04-05 21:18:22 +02002037const 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 +01002038const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
2039 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
2040 Set-cookie Updated, unknown, unknown */
2041
William Lallemand1d705562012-03-12 12:46:41 +01002042/*
2043 * try to write a character if there is enough space, or goto out
2044 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01002045#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01002046 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01002047 *(tmplog++) = (x); \
2048 } else { \
2049 goto out; \
2050 } \
2051 } while(0)
2052
Dragan Dosen835b9212016-02-12 13:23:03 +01002053
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002054/* Initializes some log data at boot */
2055static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01002056{
2057 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02002058 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01002059
2060 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
2061 * inside PARAM-VALUE should be escaped with '\' as prefix.
2062 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
2063 * details.
2064 */
2065 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
2066
2067 tmp = "\"\\]";
2068 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002069 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002070 tmp++;
2071 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02002072
2073 /* initialize the log header encoding map : '{|}"#' should be encoded with
2074 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
2075 * URL encoding only requires '"', '#' to be encoded as well as non-
2076 * printable characters above.
2077 */
2078 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
2079 memset(url_encode_map, 0, sizeof(url_encode_map));
2080 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002081 ha_bit_set(i, hdr_encode_map);
2082 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002083 }
2084 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002085 ha_bit_set(i, hdr_encode_map);
2086 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002087 }
2088
2089 tmp = "\"#{|}";
2090 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002091 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002092 tmp++;
2093 }
2094
2095 tmp = "\"#";
2096 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002097 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002098 tmp++;
2099 }
2100
2101 /* initialize the http header encoding map. The draft httpbis define the
2102 * header content as:
2103 *
2104 * HTTP-message = start-line
2105 * *( header-field CRLF )
2106 * CRLF
2107 * [ message-body ]
2108 * header-field = field-name ":" OWS field-value OWS
2109 * field-value = *( field-content / obs-fold )
2110 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2111 * obs-fold = CRLF 1*( SP / HTAB )
2112 * field-vchar = VCHAR / obs-text
2113 * VCHAR = %x21-7E
2114 * obs-text = %x80-FF
2115 *
2116 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
2117 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08002118 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02002119 */
2120 memset(http_encode_map, 0, sizeof(http_encode_map));
2121 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002122 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002123 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002124 ha_bit_set(i, http_encode_map);
2125 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002126}
William Lallemand1d705562012-03-12 12:46:41 +01002127
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002128INITCALL0(STG_PREPARE, init_log);
2129
Christopher Faulet0132d062017-07-26 15:33:35 +02002130/* Initialize log buffers used for syslog messages */
2131int init_log_buffers()
2132{
Christopher Faulet0132d062017-07-26 15:33:35 +02002133 logline = my_realloc2(logline, global.max_syslog_len + 1);
2134 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02002135 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02002136 return 0;
2137 return 1;
2138}
2139
2140/* Deinitialize log buffers used for syslog messages */
2141void deinit_log_buffers()
2142{
Christopher Faulet0132d062017-07-26 15:33:35 +02002143 free(logline);
2144 free(logline_rfc5424);
Willy Tarreau869efd52019-11-15 15:16:57 +01002145 ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
Christopher Faulet0132d062017-07-26 15:33:35 +02002146 logline = NULL;
2147 logline_rfc5424 = NULL;
2148}
2149
Willy Tarreaudf974472012-12-28 02:44:01 +01002150/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2151 * <maxsize> characters. Returns the size of the output string in characters,
2152 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002153 * is not zero. It requires a valid session and optionally a stream. If the
2154 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002155 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002156int 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 +02002157{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002158 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002159 struct proxy *be;
2160 struct http_txn *txn;
2161 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002162 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002163 unsigned int s_flags;
2164 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002165 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002166 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002167 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002168 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002169 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002170 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002171 int t_request;
2172 int hdr;
2173 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002174 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002175 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002176 char *ret;
2177 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002178 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002179 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002180 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002181 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002182 struct ist path;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002183
William Lallemandbddd4fd2012-02-27 11:23:10 +01002184 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002185
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002186 if (likely(s)) {
2187 be = s->be;
2188 txn = s->txn;
2189 be_conn = cs_conn(objt_cs(s->si[1].end));
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002190 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002191 s_flags = s->flags;
2192 uniq_id = s->uniq_id;
2193 logs = &s->logs;
2194 } else {
2195 /* we have no stream so we first need to initialize a few
2196 * things that are needed later. We do increment the request
2197 * ID so that it's uniquely assigned to this request just as
2198 * if the request had reached the point of being processed.
2199 * A request error is reported as it's the only element we have
2200 * here and which justifies emitting such a log.
2201 */
Christopher Fauletfd818482021-04-14 14:01:41 +02002202 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002203 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002204 fe_conn = objt_conn(sess->origin);
Christopher Fauletfd818482021-04-14 14:01:41 +02002205 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? cs_conn(__objt_check(sess->origin)->cs) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002206 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002207 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02002208 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002209
2210 /* prepare a valid log structure */
2211 tmp_strm_log.tv_accept = sess->tv_accept;
2212 tmp_strm_log.accept_date = sess->accept_date;
2213 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02002214 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002215 tv_zero(&tmp_strm_log.tv_request);
2216 tmp_strm_log.t_queue = -1;
2217 tmp_strm_log.t_connect = -1;
2218 tmp_strm_log.t_data = -1;
2219 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2220 tmp_strm_log.bytes_in = 0;
2221 tmp_strm_log.bytes_out = 0;
2222 tmp_strm_log.prx_queue_pos = 0;
2223 tmp_strm_log.srv_queue_pos = 0;
2224
2225 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002226
2227 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
2228 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, NULL);
2229
2230 switch (es) {
2231 case MUX_ES_SUCCESS:
2232 break;
2233 case MUX_ES_INVALID_ERR:
2234 status = 400;
2235 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2236 s_flags = SF_ERR_CLICL | SF_FINST_R;
2237 else
2238 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2239 break;
2240 case MUX_ES_TOUT_ERR:
2241 status = 408;
2242 s_flags = SF_ERR_CLITO | SF_FINST_R;
2243 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002244 case MUX_ES_NOTIMPL_ERR:
2245 status = 501;
2246 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2247 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002248 case MUX_ES_INTERNAL_ERR:
2249 status = 500;
2250 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2251 break;
2252 default:
2253 break;
2254 }
2255 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002256 }
2257
William Lallemandbddd4fd2012-02-27 11:23:10 +01002258 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002259 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2260 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002261
William Lallemand1d705562012-03-12 12:46:41 +01002262 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002263
William Lallemandbddd4fd2012-02-27 11:23:10 +01002264 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002265 if (LIST_ISEMPTY(list_format))
2266 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002267
William Lallemand1d705562012-03-12 12:46:41 +01002268 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002269 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002270 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002271 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002272 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002273
Willy Tarreauc8368452012-12-21 00:09:23 +01002274 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002275 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002276 if (!last_isspace) {
2277 LOGCHAR(' ');
2278 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002279 }
2280 break;
2281
William Lallemand1d705562012-03-12 12:46:41 +01002282 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002283 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002284 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002285 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002286 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002287 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002288 last_isspace = 0;
2289 break;
2290
Willy Tarreauc8368452012-12-21 00:09:23 +01002291 case LOG_FMT_EXPR: // sample expression, may be request or response
2292 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002293 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002294 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 +02002295 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002296 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 +01002297 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002298 ret = lf_encode_chunk(tmplog, dst + maxsize,
2299 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002300 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002301 ret = lf_text_len(tmplog,
2302 key ? key->data.u.str.area : NULL,
2303 key ? key->data.u.str.data : 0,
2304 dst + maxsize - tmplog,
2305 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002306 if (ret == 0)
2307 goto out;
2308 tmplog = ret;
2309 last_isspace = 0;
2310 break;
2311
Willy Tarreau2beef582012-12-20 17:22:52 +01002312 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002313 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002314 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002315 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002316 else
2317 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002318 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002319 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002320 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002321 last_isspace = 0;
2322 break;
2323
Willy Tarreau2beef582012-12-20 17:22:52 +01002324 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002325 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002326 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002327 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002328 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002329 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002330 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002331 dst + maxsize - tmplog, tmp);
2332 }
William Lallemand5f232402012-04-05 18:02:55 +02002333 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002334 else
2335 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2336
William Lallemand5f232402012-04-05 18:02:55 +02002337 if (ret == NULL)
2338 goto out;
2339 tmplog = ret;
2340 last_isspace = 0;
2341 break;
2342
Willy Tarreau2beef582012-12-20 17:22:52 +01002343 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002344 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002345 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002346 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002347 }
2348 else
2349 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2350
William Lallemand1d705562012-03-12 12:46:41 +01002351 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002352 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002353 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002354 last_isspace = 0;
2355 break;
2356
Willy Tarreau2beef582012-12-20 17:22:52 +01002357 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002358 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002359 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002360 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002361 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002362 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002363 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002364 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002365 else
2366 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2367
William Lallemand5f232402012-04-05 18:02:55 +02002368 if (ret == NULL)
2369 goto out;
2370 tmplog = ret;
2371 last_isspace = 0;
2372 break;
2373
Willy Tarreau2beef582012-12-20 17:22:52 +01002374 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002375 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002376 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002377 else
2378 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2379
William Lallemand1d705562012-03-12 12:46:41 +01002380 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002381 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002382 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002383 last_isspace = 0;
2384 break;
2385
Willy Tarreau2beef582012-12-20 17:22:52 +01002386 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002387 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002388 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002389 else
2390 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2391
William Lallemand5f232402012-04-05 18:02:55 +02002392 if (ret == NULL)
2393 goto out;
2394 tmplog = ret;
2395 last_isspace = 0;
2396 break;
2397
Willy Tarreau2beef582012-12-20 17:22:52 +01002398 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002399 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002400 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002401 else
2402 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2403
William Lallemand5f232402012-04-05 18:02:55 +02002404 if (ret == NULL)
2405 goto out;
2406 tmplog = ret;
2407 last_isspace = 0;
2408 break;
2409
Willy Tarreau2beef582012-12-20 17:22:52 +01002410 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002411 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002412 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002413 else
2414 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2415
William Lallemand1d705562012-03-12 12:46:41 +01002416 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002417 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002418 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002419 last_isspace = 0;
2420 break;
2421
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002422 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002423 get_localtime(logs->accept_date.tv_sec, &tm);
2424 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002425 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002426 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002427 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002428 last_isspace = 0;
2429 break;
2430
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002431 case LOG_FMT_tr: // %tr = start of request date
2432 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002433 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 +02002434 get_localtime(tv.tv_sec, &tm);
2435 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2436 if (ret == NULL)
2437 goto out;
2438 tmplog = ret;
2439 last_isspace = 0;
2440 break;
2441
2442 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002443 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002444 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002445 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002446 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002447 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002448 last_isspace = 0;
2449 break;
2450
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002451 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002452 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 +02002453 get_gmtime(tv.tv_sec, &tm);
2454 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2455 if (ret == NULL)
2456 goto out;
2457 tmplog = ret;
2458 last_isspace = 0;
2459 break;
2460
2461 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002462 get_localtime(logs->accept_date.tv_sec, &tm);
2463 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002464 if (ret == NULL)
2465 goto out;
2466 tmplog = ret;
2467 last_isspace = 0;
2468 break;
2469
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002470 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002471 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 +02002472 get_localtime(tv.tv_sec, &tm);
2473 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2474 if (ret == NULL)
2475 goto out;
2476 tmplog = ret;
2477 last_isspace = 0;
2478 break;
2479
William Lallemand5f232402012-04-05 18:02:55 +02002480 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002481 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002482 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002483 if (iret < 0 || iret > dst + maxsize - tmplog)
2484 goto out;
2485 last_isspace = 0;
2486 tmplog += iret;
2487 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002488 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002489 if (ret == NULL)
2490 goto out;
2491 tmplog = ret;
2492 last_isspace = 0;
2493 }
2494 break;
2495
William Lallemand1d705562012-03-12 12:46:41 +01002496 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002497 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002498 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002499 if (iret < 0 || iret > dst + maxsize - tmplog)
2500 goto out;
2501 last_isspace = 0;
2502 tmplog += iret;
2503 } else {
2504 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002505 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002506 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002507 tmplog, 4);
2508 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002509 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002510 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002511 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002512 }
2513 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002514
William Lallemand1d705562012-03-12 12:46:41 +01002515 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002516 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002517 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002518 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002519 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002520 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002521 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002522 break;
2523
Willy Tarreau773d65f2012-10-12 14:56:11 +02002524 case LOG_FMT_FRONTEND_XPRT: // %ft
2525 src = fe->id;
2526 if (tmp->options & LOG_OPT_QUOTE)
2527 LOGCHAR('"');
2528 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2529 if (iret == 0)
2530 goto out;
2531 tmplog += iret;
Christopher Fauletfd818482021-04-14 14:01:41 +02002532 if (sess->listener && sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002533 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002534 if (tmp->options & LOG_OPT_QUOTE)
2535 LOGCHAR('"');
2536 last_isspace = 0;
2537 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002538#ifdef USE_OPENSSL
2539 case LOG_FMT_SSL_CIPHER: // %sslc
2540 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002541 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002542 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002543 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002544 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002545 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2546 if (ret == NULL)
2547 goto out;
2548 tmplog = ret;
2549 last_isspace = 0;
2550 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002551
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002552 case LOG_FMT_SSL_VERSION: // %sslv
2553 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002554 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002555 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002556 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002557 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002558 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2559 if (ret == NULL)
2560 goto out;
2561 tmplog = ret;
2562 last_isspace = 0;
2563 break;
2564#endif
William Lallemand1d705562012-03-12 12:46:41 +01002565 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002566 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002567 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002568 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002569 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002570 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002571 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002572 break;
2573
William Lallemand1d705562012-03-12 12:46:41 +01002574 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002575 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002576 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002577 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002578 break;
2579 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002580 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002581 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002582 case OBJ_TYPE_CHECK:
2583 src = (__objt_check(sess->origin)->server
2584 ? __objt_check(sess->origin)->server->id
2585 : "<NOSRV>");
2586 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002587 default:
2588 src = "<NOSRV>";
2589 break;
2590 }
William Lallemand5f232402012-04-05 18:02:55 +02002591 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002592 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002593 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002594 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002595 last_isspace = 0;
2596 break;
2597
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002598 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002599 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002600 if (ret == NULL)
2601 goto out;
2602 tmplog = ret;
2603 last_isspace = 0;
2604 break;
2605
2606 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002607 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002608 if (ret == NULL)
2609 goto out;
2610 tmplog = ret;
2611 last_isspace = 0;
2612 break;
2613
2614 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002615 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002616 tmplog, dst + maxsize - tmplog);
2617 if (ret == NULL)
2618 goto out;
2619 tmplog = ret;
2620 last_isspace = 0;
2621 break;
2622
2623 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002624 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002625 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002626 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002627 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002628 last_isspace = 0;
2629 break;
2630
William Lallemand1d705562012-03-12 12:46:41 +01002631 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002632 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002633 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_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002641 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -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
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002649 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002650 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -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
Willy Tarreau27b639d2016-05-17 17:55:27 +02002658 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002659 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002660 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002661 tmplog, dst + maxsize - tmplog);
2662 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002663 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002664 tmplog, dst + maxsize - tmplog);
2665 if (ret == NULL)
2666 goto out;
2667 tmplog = ret;
2668 last_isspace = 0;
2669 break;
2670
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002671 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2672 if (!(fe->to_log & LW_BYTES))
2673 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002674 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 +02002675 tmplog, dst + maxsize - tmplog);
2676 if (ret == NULL)
2677 goto out;
2678 tmplog = ret;
2679 last_isspace = 0;
2680 break;
2681
2682 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002683 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002684 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002685 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002686 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002687 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002688 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002689 last_isspace = 0;
2690 break;
2691
Damien Claisse57c8eb92020-04-28 12:09:19 +00002692 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2693 if (!(fe->to_log & LW_BYTES))
2694 LOGCHAR('+');
2695 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2696 tmplog, dst + maxsize - tmplog);
2697 if (ret == NULL)
2698 goto out;
2699 tmplog = ret;
2700 last_isspace = 0;
2701 break;
2702
Willy Tarreau2beef582012-12-20 17:22:52 +01002703 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002704 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002705 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002706 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002707 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002708 last_isspace = 0;
2709 break;
2710
William Lallemand1d705562012-03-12 12:46:41 +01002711 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002712 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002713 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002714 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002715 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002716 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002717 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002718 last_isspace = 0;
2719 break;
2720
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002721 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002722 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002723 if (ret == NULL)
2724 goto out;
2725 tmplog = ret;
2726 last_isspace = 0;
2727 break;
2728
Willy Tarreau2beef582012-12-20 17:22:52 +01002729 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002730 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002731 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002732 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002733 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002734 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002735 last_isspace = 0;
2736 break;
2737
Willy Tarreau2beef582012-12-20 17:22:52 +01002738 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002739 src = txn ? txn->srv_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
William Lallemand1d705562012-03-12 12:46:41 +01002747 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002748 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2749 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002750 *tmplog = '\0';
2751 last_isspace = 0;
2752 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002753
William Lallemand1d705562012-03-12 12:46:41 +01002754 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002755 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2756 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002757 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2758 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 +01002759 last_isspace = 0;
2760 break;
2761
William Lallemand1d705562012-03-12 12:46:41 +01002762 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002763 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002764 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002765 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002766 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002767 last_isspace = 0;
2768 break;
2769
William Lallemand1d705562012-03-12 12:46:41 +01002770 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002771 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002772 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002773 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002774 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002775 last_isspace = 0;
2776 break;
2777
William Lallemand1d705562012-03-12 12:46:41 +01002778 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002779 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002780 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002781 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002782 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002783 last_isspace = 0;
2784 break;
2785
William Lallemand1d705562012-03-12 12:46:41 +01002786 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002787 switch (obj_type(s ? s->target : sess->origin)) {
2788 case OBJ_TYPE_SERVER:
2789 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2790 tmplog, dst + maxsize - tmplog);
2791 break;
2792 case OBJ_TYPE_CHECK:
2793 ret = ultoa_o(__objt_check(sess->origin)->server
2794 ? __objt_check(sess->origin)->server->cur_sess
2795 : 0, tmplog, dst + maxsize - tmplog);
2796 break;
2797 default:
2798 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2799 break;
2800 }
2801
William Lallemand1d705562012-03-12 12:46:41 +01002802 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002803 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002804 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002805 last_isspace = 0;
2806 break;
2807
William Lallemand1d705562012-03-12 12:46:41 +01002808 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002809 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002810 LOGCHAR('+');
Christopher Faulet1d26f222021-04-16 11:24:20 +02002811 ret = ltoa_o(((s && s->si[1].conn_retries > 0)
2812 ? (be->conn_retries - s->si[1].conn_retries)
2813 : ((s && s->si[1].state != SI_ST_INI) ? be->conn_retries : 0)),
2814 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002815 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002816 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002817 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002818 last_isspace = 0;
2819 break;
2820
William Lallemand1d705562012-03-12 12:46:41 +01002821 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002822 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002823 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002824 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002825 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002826 last_isspace = 0;
2827 break;
2828
William Lallemand1d705562012-03-12 12:46:41 +01002829 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002830 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002831 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002832 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002833 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002834 last_isspace = 0;
2835 break;
2836
William Lallemand1d705562012-03-12 12:46:41 +01002837 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002838 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002839 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002840 if (tmp->options & LOG_OPT_QUOTE)
2841 LOGCHAR('"');
2842 LOGCHAR('{');
2843 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2844 if (hdr)
2845 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002846 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002847 ret = lf_encode_string(tmplog, dst + maxsize,
2848 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002849 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002850 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002851 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002852 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002853 }
2854 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002855 if (tmp->options & LOG_OPT_QUOTE)
2856 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002857 last_isspace = 0;
2858 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002859 break;
2860
William Lallemand1d705562012-03-12 12:46:41 +01002861 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002862 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002863 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002864 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2865 if (hdr > 0)
2866 LOGCHAR(' ');
2867 if (tmp->options & LOG_OPT_QUOTE)
2868 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002869 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002870 ret = lf_encode_string(tmplog, dst + maxsize,
2871 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002872 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002873 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002874 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002875 } else if (!(tmp->options & LOG_OPT_QUOTE))
2876 LOGCHAR('-');
2877 if (tmp->options & LOG_OPT_QUOTE)
2878 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002879 last_isspace = 0;
2880 }
2881 }
2882 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002883
William Lallemand1d705562012-03-12 12:46:41 +01002884
2885 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002886 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002887 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002888 if (tmp->options & LOG_OPT_QUOTE)
2889 LOGCHAR('"');
2890 LOGCHAR('{');
2891 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2892 if (hdr)
2893 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002894 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002895 ret = lf_encode_string(tmplog, dst + maxsize,
2896 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002897 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002898 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002899 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002900 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002901 }
2902 LOGCHAR('}');
2903 last_isspace = 0;
2904 if (tmp->options & LOG_OPT_QUOTE)
2905 LOGCHAR('"');
2906 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002907 break;
2908
William Lallemand1d705562012-03-12 12:46:41 +01002909 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002910 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002911 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002912 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2913 if (hdr > 0)
2914 LOGCHAR(' ');
2915 if (tmp->options & LOG_OPT_QUOTE)
2916 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002917 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002918 ret = lf_encode_string(tmplog, dst + maxsize,
2919 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002920 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002921 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002922 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002923 } else if (!(tmp->options & LOG_OPT_QUOTE))
2924 LOGCHAR('-');
2925 if (tmp->options & LOG_OPT_QUOTE)
2926 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002927 last_isspace = 0;
2928 }
2929 }
2930 break;
2931
William Lallemand1d705562012-03-12 12:46:41 +01002932 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002933 /* Request */
2934 if (tmp->options & LOG_OPT_QUOTE)
2935 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002936 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002937 ret = lf_encode_string(tmplog, dst + maxsize,
2938 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002939 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002940 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002941 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002942 if (tmp->options & LOG_OPT_QUOTE)
2943 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002944 last_isspace = 0;
2945 break;
William Lallemand5f232402012-04-05 18:02:55 +02002946
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002947 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002948 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002949
Willy Tarreaub7636d12015-06-17 19:58:02 +02002950 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002951 LOGCHAR('"');
2952
2953 end = uri + strlen(uri);
2954 // look for the first whitespace character
2955 while (uri < end && !HTTP_IS_SPHT(*uri))
2956 uri++;
2957
2958 // keep advancing past multiple spaces
2959 while (uri < end && HTTP_IS_SPHT(*uri)) {
2960 uri++; nspaces++;
2961 }
2962
2963 // look for first space or question mark after url
2964 spc = uri;
2965 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2966 spc++;
2967
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002968 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002969 chunk.area = "<BADREQ>";
2970 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002971 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002972 chunk.area = uri;
2973 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002974 }
2975
Dragan Dosen835b9212016-02-12 13:23:03 +01002976 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002977 if (ret == NULL || *ret != '\0')
2978 goto out;
2979
2980 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002981 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002982 LOGCHAR('"');
2983
2984 last_isspace = 0;
2985 break;
2986
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002987 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2988 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2989
2990 if (tmp->options & LOG_OPT_QUOTE)
2991 LOGCHAR('"');
2992
2993 end = uri + strlen(uri);
2994
2995 // look for the first whitespace character
2996 while (uri < end && !HTTP_IS_SPHT(*uri))
2997 uri++;
2998
2999 // keep advancing past multiple spaces
3000 while (uri < end && HTTP_IS_SPHT(*uri)) {
3001 uri++; nspaces++;
3002 }
3003
3004 // look for first space after url
3005 spc = uri;
3006 while (spc < end && !HTTP_IS_SPHT(*spc))
3007 spc++;
3008
Tim Duesterhus92c696e2021-02-28 16:11:36 +01003009 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00003010
3011 // extract relative path without query params from url
3012 path = iststop(http_get_path(path), '?');
3013 if (!txn || !txn->uri || nspaces == 0) {
3014 chunk.area = "<BADREQ>";
3015 chunk.data = strlen("<BADREQ>");
3016 } else {
3017 chunk.area = path.ptr;
3018 chunk.data = path.len;
3019 }
3020
3021 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
3022 if (ret == NULL || *ret != '\0')
3023 goto out;
3024
3025 tmplog = ret;
3026 if (tmp->options & LOG_OPT_QUOTE)
3027 LOGCHAR('"');
3028
3029 last_isspace = 0;
3030 break;
3031
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003032 case LOG_FMT_HTTP_QUERY: // %HQ
3033 if (tmp->options & LOG_OPT_QUOTE)
3034 LOGCHAR('"');
3035
Willy Tarreau57bc8912016-04-25 17:09:40 +02003036 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003037 chunk.area = "<BADREQ>";
3038 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003039 } else {
3040 uri = txn->uri;
3041 end = uri + strlen(uri);
3042 // look for the first question mark
3043 while (uri < end && *uri != '?')
3044 uri++;
3045
3046 qmark = uri;
3047 // look for first space or question mark after url
3048 while (uri < end && !HTTP_IS_SPHT(*uri))
3049 uri++;
3050
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003051 chunk.area = qmark;
3052 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003053 }
3054
Dragan Dosen835b9212016-02-12 13:23:03 +01003055 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003056 if (ret == NULL || *ret != '\0')
3057 goto out;
3058
3059 tmplog = ret;
3060 if (tmp->options & LOG_OPT_QUOTE)
3061 LOGCHAR('"');
3062
3063 last_isspace = 0;
3064 break;
3065
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003066 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02003067 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003068
Willy Tarreaub7636d12015-06-17 19:58:02 +02003069 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003070 LOGCHAR('"');
3071
3072 end = uri + strlen(uri);
3073 // look for the first whitespace character
3074 while (uri < end && !HTTP_IS_SPHT(*uri))
3075 uri++;
3076
3077 // keep advancing past multiple spaces
3078 while (uri < end && HTTP_IS_SPHT(*uri)) {
3079 uri++; nspaces++;
3080 }
3081
3082 // look for first space after url
3083 spc = uri;
3084 while (spc < end && !HTTP_IS_SPHT(*spc))
3085 spc++;
3086
Willy Tarreau57bc8912016-04-25 17:09:40 +02003087 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003088 chunk.area = "<BADREQ>";
3089 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003090 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003091 chunk.area = uri;
3092 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003093 }
3094
Dragan Dosen835b9212016-02-12 13:23:03 +01003095 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003096 if (ret == NULL || *ret != '\0')
3097 goto out;
3098
3099 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003100 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003101 LOGCHAR('"');
3102
3103 last_isspace = 0;
3104 break;
3105
3106 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02003107 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003108 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003109 LOGCHAR('"');
3110
3111 end = uri + strlen(uri);
3112 // look for the first whitespace character
3113 spc = uri;
3114 while (spc < end && !HTTP_IS_SPHT(*spc))
3115 spc++;
3116
3117 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003118 chunk.area = "<BADREQ>";
3119 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003120 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003121 chunk.area = uri;
3122 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003123 }
3124
Dragan Dosen835b9212016-02-12 13:23:03 +01003125 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003126 if (ret == NULL || *ret != '\0')
3127 goto out;
3128
3129 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003130 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003131 LOGCHAR('"');
3132
3133 last_isspace = 0;
3134 break;
3135
3136 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02003137 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003138 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003139 LOGCHAR('"');
3140
3141 end = uri + strlen(uri);
3142 // look for the first whitespace character
3143 while (uri < end && !HTTP_IS_SPHT(*uri))
3144 uri++;
3145
3146 // keep advancing past multiple spaces
3147 while (uri < end && HTTP_IS_SPHT(*uri)) {
3148 uri++; nspaces++;
3149 }
3150
3151 // look for the next 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++;
3158
Willy Tarreau57bc8912016-04-25 17:09:40 +02003159 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003160 chunk.area = "<BADREQ>";
3161 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003162 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003163 chunk.area = "HTTP/0.9";
3164 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003165 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003166 chunk.area = uri;
3167 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003168 }
3169
Dragan Dosen835b9212016-02-12 13:23:03 +01003170 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003171 if (ret == NULL || *ret != '\0')
3172 goto out;
3173
3174 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003175 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003176 LOGCHAR('"');
3177
3178 last_isspace = 0;
3179 break;
3180
William Lallemand5f232402012-04-05 18:02:55 +02003181 case LOG_FMT_COUNTER: // %rt
3182 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003183 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02003184 if (iret < 0 || iret > dst + maxsize - tmplog)
3185 goto out;
3186 last_isspace = 0;
3187 tmplog += iret;
3188 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003189 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003190 if (ret == NULL)
3191 goto out;
3192 tmplog = ret;
3193 last_isspace = 0;
3194 }
3195 break;
3196
Willy Tarreau7346acb2014-08-28 15:03:15 +02003197 case LOG_FMT_LOGCNT: // %lc
3198 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003199 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003200 if (iret < 0 || iret > dst + maxsize - tmplog)
3201 goto out;
3202 last_isspace = 0;
3203 tmplog += iret;
3204 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003205 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003206 if (ret == NULL)
3207 goto out;
3208 tmplog = ret;
3209 last_isspace = 0;
3210 }
3211 break;
3212
William Lallemand5f232402012-04-05 18:02:55 +02003213 case LOG_FMT_HOSTNAME: // %H
3214 src = hostname;
3215 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3216 if (ret == NULL)
3217 goto out;
3218 tmplog = ret;
3219 last_isspace = 0;
3220 break;
3221
3222 case LOG_FMT_PID: // %pid
3223 if (tmp->options & LOG_OPT_HEXA) {
3224 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3225 if (iret < 0 || iret > dst + maxsize - tmplog)
3226 goto out;
3227 last_isspace = 0;
3228 tmplog += iret;
3229 } else {
3230 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3231 if (ret == NULL)
3232 goto out;
3233 tmplog = ret;
3234 last_isspace = 0;
3235 }
3236 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003237
3238 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003239 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003240 if (s)
3241 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3242 else
3243 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003244 if (ret == NULL)
3245 goto out;
3246 tmplog = ret;
3247 last_isspace = 0;
3248 break;
3249
William Lallemandbddd4fd2012-02-27 11:23:10 +01003250 }
3251 }
3252
3253out:
William Lallemand1d705562012-03-12 12:46:41 +01003254 /* *tmplog is a unused character */
3255 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003256 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003257
Willy Tarreaubaaee002006-06-26 02:48:02 +02003258}
3259
William Lallemand1d705562012-03-12 12:46:41 +01003260/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003261 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003262 * Will not log if the frontend has no log defined.
3263 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003264void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003265{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003266 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003267 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003268 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003269
3270 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003271 err = (s->flags & SF_REDISP) ||
3272 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3273 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003274 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003275 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003276
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003277 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003278 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003279
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003280 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003281 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003282
Willy Tarreauabcd5142013-06-11 17:18:02 +02003283 if (s->logs.level) { /* loglevel was overridden */
3284 if (s->logs.level == -1) {
3285 s->logs.logwait = 0; /* logs disabled */
3286 return;
3287 }
3288 level = s->logs.level - 1;
3289 }
3290 else {
3291 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003292 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003293 level = LOG_ERR;
3294 }
William Lallemand1d705562012-03-12 12:46:41 +01003295
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003296 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003297 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003298 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003299 }
3300
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003301 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3302 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3303 &sess->fe->logformat_sd);
3304 }
3305
Dragan Dosen59cee972015-09-19 22:09:02 +02003306 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003307 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003308 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003309 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3310 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003311 s->logs.logwait = 0;
3312 }
3313}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003314
Willy Tarreau53839352018-09-05 19:51:10 +02003315/*
3316 * send a minimalist log for the session. Will not log if the frontend has no
3317 * log defined. It is assumed that this is only used to report anomalies that
3318 * cannot lead to the creation of a regular stream. Because of this the log
3319 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3320 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003321 * function to report unimportant events. It is safe to call this function with
3322 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003323 */
3324void sess_log(struct session *sess)
3325{
3326 int size, level;
3327 int sd_size = 0;
3328
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003329 if (!sess)
3330 return;
3331
Willy Tarreau53839352018-09-05 19:51:10 +02003332 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3333 return;
3334
3335 level = LOG_INFO;
3336 if (sess->fe->options2 & PR_O2_LOGERRORS)
3337 level = LOG_ERR;
3338
3339 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3340 sd_size = sess_build_logline(sess, NULL,
3341 logline_rfc5424, global.max_syslog_len,
3342 &sess->fe->logformat_sd);
3343 }
3344
3345 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3346 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003347 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003348 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3349 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003350 }
3351}
3352
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003353void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3354{
3355 va_list argp;
3356 int data_len;
3357
3358 if (level < 0 || format == NULL || logline == NULL)
3359 return;
3360
3361 va_start(argp, format);
3362 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3363 if (data_len < 0 || data_len > global.max_syslog_len)
3364 data_len = global.max_syslog_len;
3365 va_end(argp);
3366
3367 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3368}
Emeric Brun54932b42020-07-07 09:43:24 +02003369/*
3370 * This function parse a received log message <buf>, of size <buflen>
3371 * it fills <level>, <facility> and <metadata> depending of the detected
3372 * header format and message will point on remaining payload of <size>
3373 *
3374 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3375 * struct ist len will be set to 0 if field is not found
3376 * <level> and <facility> will be set to -1 if not found.
3377 */
3378void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3379 struct ist *metadata, char **message, size_t *size)
3380{
3381
3382 char *p;
3383 int fac_level = 0;
3384
3385 *level = *facility = -1;
3386
3387 *message = buf;
3388 *size = buflen;
3389
3390 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3391
3392 p = buf;
3393 if (*size < 2 || *p != '<')
3394 return;
3395
3396 p++;
3397 while (*p != '>') {
3398 if (*p > '9' || *p < '0')
3399 return;
3400 fac_level = 10*fac_level + (*p - '0');
3401 p++;
3402 if ((p - buf) > buflen)
3403 return;
3404 }
3405
3406 *facility = fac_level >> 3;
3407 *level = fac_level & 0x7;
3408 p++;
3409
3410 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3411
3412 buflen -= p - buf;
3413 buf = p;
3414
3415 *size = buflen;
3416 *message = buf;
3417
3418 /* for rfc5424, prio is always followed by '1' and ' ' */
3419 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3420 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3421 * followed by message.
3422 * Each header field can present NILVALUE: '-'
3423 */
3424
3425 p += 2;
3426 /* timestamp is NILVALUE '-' */
3427 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3428 metadata[LOG_META_TIME] = ist2(p, 1);
3429 p++;
3430 }
3431 else if (*size > LOG_ISOTIME_MINLEN) {
3432 metadata[LOG_META_TIME].ptr = p;
3433
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003434 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003435 * in timestamp.
3436 * possible format are:
3437 * ex: '1970-01-01T00:00:00.000000Z'
3438 * '1970-01-01T00:00:00.000000+00:00'
3439 * '1970-01-01T00:00:00.000000-00:00'
3440 * '1970-01-01T00:00:00Z'
3441 * '1970-01-01T00:00:00+00:00'
3442 * '1970-01-01T00:00:00-00:00'
3443 */
3444 p += 19;
3445 if (*p == '.') {
3446 p++;
3447 if ((p - buf) >= buflen)
3448 goto bad_format;
3449 while (*p != 'Z' && *p != '+' && *p != '-') {
3450 if ((unsigned char)(*p - '0') > 9)
3451 goto bad_format;
3452
3453 p++;
3454 if ((p - buf) >= buflen)
3455 goto bad_format;
3456 }
3457 }
3458
3459 if (*p == 'Z')
3460 p++;
3461 else
3462 p += 6; /* case of '+00:00 or '-00:00' */
3463
3464 if ((p - buf) >= buflen || *p != ' ')
3465 goto bad_format;
3466 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3467 }
3468 else
3469 goto bad_format;
3470
3471
3472 p++;
3473 if ((p - buf) >= buflen || *p == ' ')
3474 goto bad_format;
3475
3476 metadata[LOG_META_HOST].ptr = p;
3477 while (*p != ' ') {
3478 p++;
3479 if ((p - buf) >= buflen)
3480 goto bad_format;
3481 }
3482 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3483 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3484 metadata[LOG_META_HOST].len = 0;
3485
3486 p++;
3487 if ((p - buf) >= buflen || *p == ' ')
3488 goto bad_format;
3489
3490 metadata[LOG_META_TAG].ptr = p;
3491 while (*p != ' ') {
3492 p++;
3493 if ((p - buf) >= buflen)
3494 goto bad_format;
3495 }
3496 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3497 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3498 metadata[LOG_META_TAG].len = 0;
3499
3500 p++;
3501 if ((p - buf) >= buflen || *p == ' ')
3502 goto bad_format;
3503
3504 metadata[LOG_META_PID].ptr = p;
3505 while (*p != ' ') {
3506 p++;
3507 if ((p - buf) >= buflen)
3508 goto bad_format;
3509 }
3510 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3511 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3512 metadata[LOG_META_PID].len = 0;
3513
3514 p++;
3515 if ((p - buf) >= buflen || *p == ' ')
3516 goto bad_format;
3517
3518 metadata[LOG_META_MSGID].ptr = p;
3519 while (*p != ' ') {
3520 p++;
3521 if ((p - buf) >= buflen)
3522 goto bad_format;
3523 }
3524 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3525 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3526 metadata[LOG_META_MSGID].len = 0;
3527
3528 p++;
3529 if ((p - buf) >= buflen || *p == ' ')
3530 goto bad_format;
3531
3532 /* structured data format is:
3533 * ex:
3534 * '[key1=value1 key2=value2][key3=value3]'
3535 *
3536 * space is invalid outside [] because
3537 * considered as the end of structured data field
3538 */
3539 metadata[LOG_META_STDATA].ptr = p;
3540 if (*p == '[') {
3541 int elem = 0;
3542
3543 while (1) {
3544 if (elem) {
3545 /* according to rfc this char is escaped in param values */
3546 if (*p == ']' && *(p-1) != '\\')
3547 elem = 0;
3548 }
3549 else {
3550 if (*p == '[')
3551 elem = 1;
3552 else if (*p == ' ')
3553 break;
3554 else
3555 goto bad_format;
3556 }
3557 p++;
3558 if ((p - buf) >= buflen)
3559 goto bad_format;
3560 }
3561 }
3562 else if (*p == '-') {
3563 /* case of NILVALUE */
3564 p++;
3565 if ((p - buf) >= buflen || *p != ' ')
3566 goto bad_format;
3567 }
3568 else
3569 goto bad_format;
3570
3571 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3572 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3573 metadata[LOG_META_STDATA].len = 0;
3574
3575 p++;
3576
3577 buflen -= p - buf;
3578 buf = p;
3579
3580 *size = buflen;
3581 *message = p;
3582 }
3583 else if (*size > LOG_LEGACYTIME_LEN) {
3584 int m;
3585
3586 /* supported header format according to rfc3164.
3587 * ex:
3588 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3589 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3590 * or 'Jan 1 00:00:00 HOSTNAME '
3591 * Note: HOSTNAME is mandatory, and day
3592 * of month uses a single space prefix if
3593 * less than 10 to ensure hour offset is
3594 * always the same.
3595 */
3596
3597 /* Check month to see if it correspond to a rfc3164
3598 * header ex 'Jan 1 00:00:00' */
3599 for (m = 0; m < 12; m++)
3600 if (!memcmp(monthname[m], p, 3))
3601 break;
3602 /* Month not found */
3603 if (m == 12)
3604 goto bad_format;
3605
3606 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3607
3608 p += LOG_LEGACYTIME_LEN;
3609 if ((p - buf) >= buflen || *p != ' ')
3610 goto bad_format;
3611
3612 p++;
3613 if ((p - buf) >= buflen || *p == ' ')
3614 goto bad_format;
3615
3616 metadata[LOG_META_HOST].ptr = p;
3617 while (*p != ' ') {
3618 p++;
3619 if ((p - buf) >= buflen)
3620 goto bad_format;
3621 }
3622 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3623
3624 /* TAG seems to no be mandatory */
3625 p++;
3626
3627 buflen -= p - buf;
3628 buf = p;
3629
3630 *size = buflen;
3631 *message = buf;
3632
3633 if (!buflen)
3634 return;
3635
3636 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3637 p++;
3638
3639 /* a tag must present a trailing ':' */
3640 if (((p - buf) >= buflen) || *p != ':')
3641 return;
3642 p++;
3643 /* followed by a space */
3644 if (((p - buf) >= buflen) || *p != ' ')
3645 return;
3646
3647 /* rewind to parse tag and pid */
3648 p = buf;
3649 metadata[LOG_META_TAG].ptr = p;
3650 /* we have the guarantee that ':' will be reach before size limit */
3651 while (*p != ':') {
3652 if (*p == '[') {
3653 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3654 metadata[LOG_META_PID].ptr = p + 1;
3655 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003656 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003657 if (p[1] != ':')
3658 return;
3659 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3660 }
3661 p++;
3662 }
3663 if (!metadata[LOG_META_TAG].len)
3664 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3665
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003666 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003667 p += 2;
3668
3669 buflen -= p - buf;
3670 buf = p;
3671
3672 *size = buflen;
3673 *message = buf;
3674 }
3675
3676 return;
3677
3678bad_format:
3679 /* bad syslog format, we reset all parsed syslog fields
3680 * but priority is kept because we are able to re-build
3681 * this message using LOF_FORMAT_PRIO.
3682 */
3683 metadata[LOG_META_TIME].len = 0;
3684 metadata[LOG_META_HOST].len = 0;
3685 metadata[LOG_META_TAG].len = 0;
3686 metadata[LOG_META_PID].len = 0;
3687 metadata[LOG_META_MSGID].len = 0;
3688 metadata[LOG_META_STDATA].len = 0;
3689
3690 return;
3691}
3692
3693/*
3694 * UDP syslog fd handler
3695 */
3696void syslog_fd_handler(int fd)
3697{
3698 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3699 ssize_t ret = 0;
3700 struct buffer *buf = get_trash_chunk();
3701 size_t size;
3702 char *message;
3703 int level;
3704 int facility;
3705 struct listener *l = objt_listener(fdtab[fd].owner);
3706 int max_accept;
3707
3708 if(!l)
3709 ABORT_NOW();
3710
Willy Tarreauf5090652021-04-06 17:23:40 +02003711 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003712
3713 if (!fd_recv_ready(fd))
3714 return;
3715
3716 max_accept = l->maxaccept ? l->maxaccept : 1;
3717
3718 do {
3719 /* Source address */
3720 struct sockaddr_storage saddr = {0};
3721 socklen_t saddrlen;
3722
3723 saddrlen = sizeof(saddr);
3724
3725 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3726 if (ret < 0) {
3727 if (errno == EINTR)
3728 continue;
3729 if (errno == EAGAIN)
3730 fd_cant_recv(fd);
3731 goto out;
3732 }
3733 buf->data = ret;
3734
Emeric Brun45c457a2020-07-09 23:23:34 +02003735 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003736 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003737 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003738
Emeric Brun54932b42020-07-07 09:43:24 +02003739 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3740
3741 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3742
3743 } while (--max_accept);
3744 }
3745
3746out:
3747 return;
3748}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003749
Emeric Brun12941c82020-07-07 14:19:42 +02003750/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003751 * IO Handler to handle message exchange with a syslog tcp client
3752 */
3753static void syslog_io_handler(struct appctx *appctx)
3754{
3755 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3756 struct stream_interface *si = appctx->owner;
3757 struct stream *s = si_strm(si);
3758 struct proxy *frontend = strm_fe(s);
3759 struct listener *l = strm_li(s);
3760 struct buffer *buf = get_trash_chunk();
3761 int max_accept;
3762 int to_skip;
3763 int facility;
3764 int level;
3765 char *message;
3766 size_t size;
3767
3768 max_accept = l->maxaccept ? l->maxaccept : 1;
3769 while (co_data(si_oc(si))) {
3770 char c;
3771
3772 if (max_accept <= 0)
3773 goto missing_budget;
3774 max_accept--;
3775
3776 to_skip = co_getchar(si_oc(si), &c);
3777 if (!to_skip)
3778 goto missing_data;
3779 else if (to_skip < 0)
3780 goto cli_abort;
3781
3782 if (c == '<') {
3783 /* rfc-6587, Non-Transparent-Framing: messages separated by
3784 * a trailing LF or CR LF
3785 */
3786 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3787 if (!to_skip)
3788 goto missing_data;
3789 else if (to_skip < 0)
3790 goto cli_abort;
3791
3792 if (buf->area[to_skip - 1] != '\n')
3793 goto parse_error;
3794
3795 buf->data = to_skip - 1;
3796
3797 /* according to rfc-6587, some devices adds CR before LF */
3798 if (buf->data && buf->area[buf->data - 1] == '\r')
3799 buf->data--;
3800
3801 }
3802 else if ((unsigned char)(c - '1') <= 8) {
3803 /* rfc-6587, Octet-Counting: message length in ASCII
3804 * (first digit can not be ZERO), followed by a space
3805 * and message length
3806 */
3807 char *p = NULL;
3808 int msglen;
3809
3810 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3811 if (!to_skip)
3812 goto missing_data;
3813 else if (to_skip < 0)
3814 goto cli_abort;
3815
3816 if (buf->area[to_skip - 1] != ' ')
3817 goto parse_error;
3818
Aurelien DARRAGON5569aa92022-10-26 23:40:08 +02003819 msglen = strtol(buf->area, &p, 10);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003820 if (!msglen || p != &buf->area[to_skip - 1])
3821 goto parse_error;
3822
3823 /* message seems too large */
3824 if (msglen > buf->size)
3825 goto parse_error;
3826
3827 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3828 if (!msglen)
3829 goto missing_data;
3830 else if (msglen < 0)
3831 goto cli_abort;
3832
3833
3834 buf->data = msglen;
3835 to_skip += msglen;
3836 }
3837 else
3838 goto parse_error;
3839
3840 co_skip(si_oc(si), to_skip);
3841
3842 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003843 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003844 proxy_inc_fe_req_ctr(l, frontend);
3845
3846 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3847
3848 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3849
3850 }
3851
3852missing_data:
3853 /* we need more data to read */
3854 si_oc(si)->flags |= CF_READ_DONTWAIT;
3855
3856 return;
3857
3858missing_budget:
3859 /* it may remain some stuff to do, let's retry later */
3860 appctx_wakeup(appctx);
3861
3862 return;
3863
3864parse_error:
3865 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003866 _HA_ATOMIC_INC(&l->counters->failed_req);
3867 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003868
3869 goto close;
3870
3871cli_abort:
3872 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003873 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3874 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003875
3876close:
3877 si_shutw(si);
3878 si_shutr(si);
3879
3880 si_ic(si)->flags |= CF_READ_NULL;
3881
3882 return;
3883}
3884
3885static struct applet syslog_applet = {
3886 .obj_type = OBJ_TYPE_APPLET,
3887 .name = "<SYSLOG>", /* used for logging */
3888 .fct = syslog_io_handler,
3889 .release = NULL,
3890};
3891
3892/*
Emeric Brun12941c82020-07-07 14:19:42 +02003893 * Parse "log-forward" section and create corresponding sink buffer.
3894 *
3895 * The function returns 0 in success case, otherwise, it returns error
3896 * flags.
3897 */
3898int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3899{
3900 int err_code = 0;
3901 struct proxy *px;
3902 char *errmsg = NULL;
3903 const char *err = NULL;
3904
3905 if (strcmp(args[0], "log-forward") == 0) {
3906 if (!*args[1]) {
3907 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3908 err_code |= ERR_ALERT | ERR_ABORT;
3909 goto out;
3910 }
3911
3912 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3913 goto out;
3914
3915 err = invalid_char(args[1]);
3916 if (err) {
3917 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3918 file, linenum, *err, args[0], args[1]);
3919 err_code |= ERR_ALERT | ERR_ABORT;
3920 goto out;
3921 }
3922
Emeric Brunb0c331f2020-10-07 17:05:59 +02003923 px = log_forward_by_name(args[1]);
3924 if (px) {
3925 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3926 file, linenum, args[1], px->conf.file, px->conf.line);
3927 err_code |= ERR_ALERT | ERR_FATAL;
3928 }
3929
3930 px = proxy_find_by_name(args[1], 0, 0);
3931 if (px) {
3932 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3933 file, linenum, args[1], proxy_type_str(px),
3934 px->id, px->conf.file, px->conf.line);
3935 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003936 }
3937
3938 px = calloc(1, sizeof *px);
3939 if (!px) {
3940 err_code |= ERR_ALERT | ERR_FATAL;
3941 goto out;
3942 }
3943
Emeric Brun9ecb3bc2021-12-01 12:08:42 +01003944 init_new_proxy(px);
Emeric Brun12941c82020-07-07 14:19:42 +02003945 px->next = cfg_log_forward;
3946 cfg_log_forward = px;
Emeric Brun12941c82020-07-07 14:19:42 +02003947 px->conf.file = strdup(file);
3948 px->conf.line = linenum;
3949 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003950 px->last_change = now.tv_sec;
3951 px->cap = PR_CAP_FE;
3952 px->maxconn = 10;
3953 px->timeout.client = TICK_ETERNITY;
3954 px->accept = frontend_accept;
3955 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003956 px->id = strdup(args[1]);
3957
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003958 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003959 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003960 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3961 err_code |= ERR_WARN;
3962
3963 if (*(args[1]) == 0) {
3964 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3965 err_code |= ERR_ALERT | ERR_FATAL;
3966 goto out;
3967 }
3968 cfg_log_forward->maxconn = atol(args[1]);
3969 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3970 goto out;
3971 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003972 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003973 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3974 err_code |= ERR_WARN;
3975
3976 if (*(args[1]) == 0) {
3977 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3978 err_code |= ERR_ALERT | ERR_FATAL;
3979 goto out;
3980 }
3981 cfg_log_forward->backlog = atol(args[1]);
3982 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3983 goto out;
3984 }
3985 else if (strcmp(args[0], "bind") == 0) {
3986 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003987 struct bind_conf *bind_conf;
3988 struct bind_kw *kw;
3989 struct listener *l;
3990
3991 cur_arg = 1;
3992
3993 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3994 NULL, xprt_get(XPRT_RAW));
3995 if (!bind_conf) {
3996 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3997 err_code |= ERR_ALERT | ERR_FATAL;
3998 goto out;
3999 }
4000
4001 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
4002 if (errmsg && *errmsg) {
4003 indent_msg(&errmsg, 2);
4004 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
4005 }
4006 else {
4007 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
4008 file, linenum, args[0], args[1], args[2]);
4009 err_code |= ERR_ALERT | ERR_FATAL;
4010 goto out;
4011 }
4012 }
4013 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau66161322021-02-19 15:50:27 +01004014 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004015 l->accept = session_accept_fd;
4016 l->analysers |= cfg_log_forward->fe_req_ana;
4017 l->default_target = cfg_log_forward->default_target;
4018 global.maxsock++;
4019 }
4020 cur_arg++;
4021
4022 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
4023 int ret;
4024
4025 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
4026 err_code |= ret;
4027 if (ret) {
4028 if (errmsg && *errmsg) {
4029 indent_msg(&errmsg, 2);
4030 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
4031 }
4032 else
4033 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
4034 file, linenum, args[cur_arg]);
4035 if (ret & ERR_FATAL)
4036 goto out;
4037 }
4038 cur_arg += 1 + kw->skip;
4039 }
4040 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01004041 const char *best = bind_find_best_kw(args[cur_arg]);
4042 if (best)
4043 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
4044 file, linenum, args[cur_arg], cursection, best);
4045 else
4046 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
4047 file, linenum, args[cur_arg], cursection);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004048 err_code |= ERR_ALERT | ERR_FATAL;
4049 goto out;
4050 }
Emeric Brun12941c82020-07-07 14:19:42 +02004051 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02004052 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02004053 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02004054 struct bind_conf *bind_conf;
4055 struct bind_kw *kw;
4056 struct listener *l;
4057
4058 cur_arg = 1;
4059
4060 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
4061 NULL, xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +02004062 if (!bind_conf) {
4063 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
4064 err_code |= ERR_ALERT | ERR_FATAL;
4065 goto out;
4066 }
Emeric Brun12941c82020-07-07 14:19:42 +02004067
Willy Tarreau26ff5da2020-09-16 15:22:19 +02004068 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02004069 if (errmsg && *errmsg) {
4070 indent_msg(&errmsg, 2);
4071 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
4072 }
4073 else {
4074 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
4075 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02004076 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02004077 err_code |= ERR_ALERT | ERR_FATAL;
4078 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02004079 }
4080 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02004081 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreau66161322021-02-19 15:50:27 +01004082 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreaue140a692020-10-15 21:25:32 +02004083 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02004084 global.maxsock++;
4085 }
4086 cur_arg++;
4087
4088 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
4089 int ret;
4090
4091 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
4092 err_code |= ret;
4093 if (ret) {
4094 if (errmsg && *errmsg) {
4095 indent_msg(&errmsg, 2);
4096 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
4097 }
4098 else
4099 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
4100 file, linenum, args[cur_arg]);
4101 if (ret & ERR_FATAL)
4102 goto out;
4103 }
4104 cur_arg += 1 + kw->skip;
4105 }
4106 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01004107 const char *best = bind_find_best_kw(args[cur_arg]);
4108 if (best)
4109 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
4110 file, linenum, args[cur_arg], cursection, best);
4111 else
4112 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
4113 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02004114 err_code |= ERR_ALERT | ERR_FATAL;
4115 goto out;
4116 }
4117 }
4118 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02004119 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02004120 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4121 err_code |= ERR_ALERT | ERR_FATAL;
4122 goto out;
4123 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004124 }
4125 else if (strcmp(args[0], "timeout") == 0) {
4126 const char *res;
4127 unsigned timeout;
4128
4129 if (strcmp(args[1], "client") != 0) {
4130 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
4131 err_code |= ERR_ALERT | ERR_FATAL;
4132 goto out;
4133 }
4134
4135 if (*args[2] == 0) {
4136 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
4137 err_code |= ERR_ALERT | ERR_FATAL;
4138 goto out;
4139 }
4140 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
4141 if (res == PARSE_TIME_OVER) {
4142 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
4143 }
4144 else if (res == PARSE_TIME_UNDER) {
4145 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
4146 }
4147 else if (res) {
4148 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
4149 return -1;
4150 }
4151
4152 if (res) {
4153 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4154 err_code |= ERR_ALERT | ERR_FATAL;
4155 goto out;
4156 }
4157 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02004158 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02004159 else {
4160 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
4161 err_code |= ERR_ALERT | ERR_ABORT;
4162 goto out;
4163 }
Emeric Brun12941c82020-07-07 14:19:42 +02004164out:
4165 return err_code;
4166}
4167
4168
Willy Tarreau869efd52019-11-15 15:16:57 +01004169/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
4170static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004171{
Willy Tarreau869efd52019-11-15 15:16:57 +01004172 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4173 return 1;
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004174
Willy Tarreau869efd52019-11-15 15:16:57 +01004175 if (!startup_logs)
4176 return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
4177
4178 return ring_attach_cli(startup_logs, appctx);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004179}
4180
4181/* register cli keywords */
4182static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004183 { { "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 +02004184 {{},}
4185}};
4186
Willy Tarreau0108d902018-11-25 19:14:37 +01004187INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4188
Emeric Brun12941c82020-07-07 14:19:42 +02004189/* config parsers for this section */
4190REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4191
Willy Tarreau082b6282019-05-22 14:42:12 +02004192REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4193REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004194
Willy Tarreaubaaee002006-06-26 02:48:02 +02004195/*
4196 * Local variables:
4197 * c-indent-level: 8
4198 * c-basic-offset: 8
4199 * End:
4200 */