blob: c8cdafab07603c7235189bf6bd2e32f2c7683061 [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;
396 LIST_ADDQ(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
450 LIST_ADDQ(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;
458 LIST_ADDQ(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
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100483 expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err, &curpx->conf.args, endptr);
Willy Tarreauc8368452012-12-21 00:09:23 +0100484 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100485 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200486 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100487 }
488
Vincent Bernat02779b62016-04-03 13:48:43 +0200489 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100490 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100491 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200492 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100493 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100494 node->type = LOG_FMT_EXPR;
495 node->expr = expr;
496 node->options = options;
497
498 if (arg_len) {
499 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100500 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200501 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100502 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100503 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100504 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
505
Willy Tarreau434c57c2013-01-08 01:10:24 +0100506 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100507 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
508
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100509 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100510 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
511 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200512 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100513 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100514
Christopher Faulet581db2b2021-03-26 10:02:46 +0100515 if ((options & LOG_OPT_HTTP) && (expr->fetch->use & (SMP_USE_L6REQ|SMP_USE_L6RES))) {
516 ha_warning("parsing [%s:%d] : L6 sample fetch <%s> ignored in HTTP log-format string.\n",
517 curpx->conf.args.file, curpx->conf.args.line, text);
518 }
519
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200520 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100521 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100522 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100523
William Lallemand65ad6e12014-01-31 15:08:02 +0100524 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
525 * needed with some sample fetches (eg: ssl*). We always set it for
526 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100527 */
528 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200529 if (curpx->http_needed)
530 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100531 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100532 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200533
534 error_free:
535 release_sample_expr(expr);
536 if (node) {
537 free(node->arg);
538 free(node);
539 }
540 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100541}
542
543/*
William Lallemand723b73a2012-02-08 16:37:49 +0100544 * Parse the log_format string and fill a linked list.
545 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200546 * You can set arguments using { } : %{many arguments}varname.
547 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100548 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500549 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100550 * curproxy: the proxy affected
551 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100552 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100553 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100554 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100555 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100556 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100557int 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 +0100558{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100559 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100560 char *arg = NULL; /* start pointer for args */
561 char *var = NULL; /* start pointer for vars */
562 int arg_len = 0;
563 int var_len = 0;
564 int cformat; /* current token format */
565 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100566 struct logformat_node *tmplf, *back;
567
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100568 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100569 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100570 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100571 return 0;
572 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200573 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200574
William Lallemand723b73a2012-02-08 16:37:49 +0100575 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100576 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100577 LIST_DEL(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200578 release_sample_expr(tmplf->expr);
579 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100580 free(tmplf);
581 }
582
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100583 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100584 pformat = cformat;
585
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100586 if (!*str)
587 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100588
Joseph Herlant85b40592018-11-15 12:10:04 -0800589 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100590 * second have all common paths processed at one place. The common paths are the ones
591 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
592 * We use the common LF_INIT state to dispatch to the different final states.
593 */
594 switch (pformat) {
595 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100596 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100597 arg_len = var_len = 0;
598 if (*str == '{') { // optional argument
599 cformat = LF_STARG;
600 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100601 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100602 else if (*str == '[') {
603 cformat = LF_STEXPR;
604 var = str + 1; // store expr in variable name
605 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100606 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100607 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100608 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100609 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100610 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500611 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100612 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100613 /* single '%' followed by blank or digit, send them both */
614 cformat = LF_TEXT;
615 pformat = LF_TEXT; /* finally we include the previous char as well */
616 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600617 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 +0100618 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100619 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100620
621 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100622 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500623 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100624 break;
625
626 case LF_STARG: // text immediately following '%{'
627 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100628 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100629 arg_len = str - arg;
630 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100631 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100632 break;
633
634 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100635 if (*str == '[') {
636 cformat = LF_STEXPR;
637 var = str + 1; // store expr in variable name
638 break;
639 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100640 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100641 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100642 var = str;
643 break;
644 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100645 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100646 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100647
Willy Tarreauc8368452012-12-21 00:09:23 +0100648 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100649 /* the whole sample expression is parsed at once,
650 * returning the pointer to the first character not
651 * part of the expression, which MUST be the trailing
652 * angle bracket.
653 */
654 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
655 goto fail;
656
657 if (*str == ']') {
658 // end of arg, go on with next state
659 cformat = pformat = LF_EDEXPR;
660 sp = str;
661 }
662 else {
663 char c = *str;
664 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100665 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100666 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
667 else
668 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200669 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100670 }
671 break;
672
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100673 case LF_VAR: // text part of a variable name
674 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100675 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100676 cformat = LF_INIT; // not variable name anymore
677 break;
678
Willy Tarreauc8368452012-12-21 00:09:23 +0100679 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100680 cformat = LF_INIT;
681 }
682
683 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
684 switch (*str) {
685 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100686 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200687 case ' ':
688 if (options & LOG_OPT_MERGE_SPACES) {
689 cformat = LF_SEPARATOR;
690 break;
691 }
692 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100693 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100694 }
695 }
696
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100697 if (cformat != pformat || pformat == LF_SEPARATOR) {
698 switch (pformat) {
699 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100700 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100701 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100702 break;
703 case LF_TEXT:
704 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100705 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100706 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100707 break;
708 }
709 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100710 }
711 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100712
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100713 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100714 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100715 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100716 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100717 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100718
719 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100720 fail:
721 free(backfmt);
722 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100723}
724
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200725/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500726 * 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 +0200727 * ranges of indexes. Note that an index may be considered as a particular range
728 * with a high limit to the low limit.
729 */
730int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
731{
732 char *end, *p;
733
734 *low = *high = 0;
735
736 p = *arg;
737 end = strchr(p, ',');
738 if (!end)
739 end = p + strlen(p);
740
741 *high = *low = read_uint((const char **)&p, end);
742 if (!*low || (p != end && *p != '-'))
743 goto err;
744
745 if (p == end)
746 goto done;
747
748 p++;
749 *high = read_uint((const char **)&p, end);
750 if (!*high || *high <= *low || p != end)
751 goto err;
752
753 done:
754 if (*end == ',')
755 end++;
756 *arg = end;
757 return 1;
758
759 err:
760 memprintf(err, "wrong sample range '%s'", *arg);
761 return 0;
762}
763
764/*
765 * Returns 1 if the range defined by <low> and <high> overlaps
766 * one of them in <rgs> array of ranges with <sz> the size of this
767 * array, 0 if not.
768 */
769int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
770 unsigned int low, unsigned int high, char **err)
771{
772 size_t i;
773
774 for (i = 0; i < sz; i++) {
775 if ((low >= rgs[i].low && low <= rgs[i].high) ||
776 (high >= rgs[i].low && high <= rgs[i].high)) {
777 memprintf(err, "ranges are overlapping");
778 return 1;
779 }
780 }
781
782 return 0;
783}
784
785int smp_log_range_cmp(const void *a, const void *b)
786{
787 const struct smp_log_range *rg_a = a;
788 const struct smp_log_range *rg_b = b;
789
790 if (rg_a->high < rg_b->low)
791 return -1;
792 else if (rg_a->low > rg_b->high)
793 return 1;
794
795 return 0;
796}
797
798/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200799 * Parse "log" keyword and update <logsrvs> list accordingly.
800 *
801 * When <do_del> is set, it means the "no log" line was parsed, so all log
802 * servers in <logsrvs> are released.
803 *
804 * Otherwise, we try to parse the "log" line. First of all, when the list is not
805 * the global one, we look for the parameter "global". If we find it,
806 * global.logsrvs is copied. Else we parse each arguments.
807 *
808 * The function returns 1 in success case, otherwise, it returns 0 and err is
809 * filled.
810 */
Emeric Brun9533a702021-04-02 10:13:43 +0200811int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200812{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100813 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200814 struct sockaddr_storage *sk;
815 struct logsrv *logsrv = NULL;
816 int port1, port2;
817 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200818 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200819
820 /*
821 * "no log": delete previous herited or defined syslog
822 * servers.
823 */
824 if (do_del) {
825 struct logsrv *back;
826
827 if (*(args[1]) != 0) {
828 memprintf(err, "'no log' does not expect arguments");
829 goto error;
830 }
831
832 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
833 LIST_DEL(&logsrv->list);
834 free(logsrv);
835 }
836 return 1;
837 }
838
839 /*
840 * "log global": copy global.logrsvs linked list to the end of logsrvs
841 * list. But first, we check (logsrvs != global.logsrvs).
842 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100843 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200844 if (logsrvs == &global.logsrvs) {
845 memprintf(err, "'global' is not supported for a global syslog server");
846 goto error;
847 }
848 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200849 struct logsrv *node;
850
851 list_for_each_entry(node, logsrvs, list) {
852 if (node->ref == logsrv)
853 goto skip_logsrv;
854 }
855
856 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200857 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200858 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200859 LIST_INIT(&node->list);
860 LIST_ADDQ(logsrvs, &node->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200861 node->conf.file = strdup(file);
862 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200863
864 skip_logsrv:
865 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200866 }
867 return 1;
868 }
869
870 /*
871 * "log <address> ...: parse a syslog server line
872 */
873 if (*(args[1]) == 0 || *(args[2]) == 0) {
874 memprintf(err, "expects <address> and <facility> %s as arguments",
875 ((logsrvs == &global.logsrvs) ? "" : "or global"));
876 goto error;
877 }
878
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100879 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
880 if (strcmp(args[1], "stdout") == 0)
881 args[1] = "fd@1";
882 else if (strcmp(args[1], "stderr") == 0)
883 args[1] = "fd@2";
884
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200885 logsrv = calloc(1, sizeof(*logsrv));
886 if (!logsrv) {
887 memprintf(err, "out of memory");
888 goto error;
889 }
890
Emeric Brun9533a702021-04-02 10:13:43 +0200891 logsrv->conf.file = strdup(file);
892 logsrv->conf.line = linenum;
893
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200894 /* skip address for now, it will be parsed at the end */
895 cur_arg = 2;
896
897 /* just after the address, a length may be specified */
898 logsrv->maxlen = MAX_SYSLOG_LEN;
899 if (strcmp(args[cur_arg], "len") == 0) {
900 int len = atoi(args[cur_arg+1]);
901 if (len < 80 || len > 65535) {
902 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
903 args[cur_arg+1]);
904 goto error;
905 }
906 logsrv->maxlen = len;
907 cur_arg += 2;
908 }
909 if (logsrv->maxlen > global.max_syslog_len)
910 global.max_syslog_len = logsrv->maxlen;
911
912 /* after the length, a format may be specified */
913 if (strcmp(args[cur_arg], "format") == 0) {
914 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200915 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200916 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
917 goto error;
918 }
919 cur_arg += 2;
920 }
921
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200922 if (strcmp(args[cur_arg], "sample") == 0) {
923 unsigned low, high;
924 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200925 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
926
927 p = args[cur_arg+1];
928 smp_sz_str = strchr(p, ':');
929 if (!smp_sz_str) {
930 memprintf(err, "Missing sample size");
931 goto error;
932 }
933
934 *smp_sz_str++ = '\0';
935
936 end = p + strlen(p);
937
938 while (p != end) {
939 if (!get_logsrv_smp_range(&low, &high, &p, err))
940 goto error;
941
942 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
943 goto error;
944
945 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
946 if (!smp_rgs) {
947 memprintf(err, "out of memory error");
948 goto error;
949 }
950
951 smp_rgs[smp_rgs_sz].low = low;
952 smp_rgs[smp_rgs_sz].high = high;
953 smp_rgs[smp_rgs_sz].sz = high - low + 1;
954 smp_rgs[smp_rgs_sz].curr_idx = 0;
955 if (smp_rgs[smp_rgs_sz].high > smp_sz)
956 smp_sz = smp_rgs[smp_rgs_sz].high;
957 smp_rgs_sz++;
958 }
959
Tim Duesterhus21648002019-06-23 22:10:10 +0200960 if (smp_rgs == NULL) {
961 memprintf(err, "no sampling ranges given");
962 goto error;
963 }
964
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200965 beg = smp_sz_str;
966 end = beg + strlen(beg);
967 new_smp_sz = read_uint((const char **)&beg, end);
968 if (!new_smp_sz || beg != end) {
969 memprintf(err, "wrong sample size '%s' for sample range '%s'",
970 smp_sz_str, args[cur_arg+1]);
971 goto error;
972 }
973
974 if (new_smp_sz < smp_sz) {
975 memprintf(err, "sample size %zu should be greater or equal to "
976 "%zu the maximum of the high ranges limits",
977 new_smp_sz, smp_sz);
978 goto error;
979 }
980 smp_sz = new_smp_sz;
981
982 /* Let's order <smp_rgs> array. */
983 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
984
985 logsrv->lb.smp_rgs = smp_rgs;
986 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
987 logsrv->lb.smp_sz = smp_sz;
988
989 cur_arg += 2;
990 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200991 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200992 /* parse the facility */
993 logsrv->facility = get_log_facility(args[cur_arg]);
994 if (logsrv->facility < 0) {
995 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
996 goto error;
997 }
998 cur_arg++;
999
1000 /* parse the max syslog level (default: debug) */
1001 logsrv->level = 7;
1002 if (*(args[cur_arg])) {
1003 logsrv->level = get_log_level(args[cur_arg]);
1004 if (logsrv->level < 0) {
1005 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
1006 goto error;
1007 }
1008 cur_arg++;
1009 }
1010
1011 /* parse the limit syslog level (default: emerg) */
1012 logsrv->minlvl = 0;
1013 if (*(args[cur_arg])) {
1014 logsrv->minlvl = get_log_level(args[cur_arg]);
1015 if (logsrv->minlvl < 0) {
1016 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1017 goto error;
1018 }
1019 cur_arg++;
1020 }
1021
1022 /* Too many args */
1023 if (*(args[cur_arg])) {
1024 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1025 goto error;
1026 }
1027
1028 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001029 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001030 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001031 logsrv->addr.ss_family = AF_UNSPEC;
1032 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001033 logsrv->sink = NULL;
1034 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001035 goto done;
1036 }
1037
Willy Tarreau5fc93282020-09-16 18:25:03 +02001038 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, NULL,
Willy Tarreaua93e5c72020-09-15 14:01:16 +02001039 err, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001040 if (!sk)
1041 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001042
1043 if (fd != -1)
1044 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001045 logsrv->addr = *sk;
1046
1047 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001048 if (!port1)
1049 set_host_port(&logsrv->addr, SYSLOG_PORT);
1050 }
Emeric Brun9533a702021-04-02 10:13:43 +02001051
Willy Tarreauc046d162019-08-30 15:24:59 +02001052 done:
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001053 LIST_ADDQ(logsrvs, &logsrv->list);
1054 return 1;
1055
1056 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001057 free(smp_rgs);
Emeric Brun9533a702021-04-02 10:13:43 +02001058 if (logsrv) {
1059 free(logsrv->conf.file);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001060 free(logsrv->ring_name);
Emeric Brun9533a702021-04-02 10:13:43 +02001061 }
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001062 free(logsrv);
1063 return 0;
1064}
1065
1066
Christopher Fauletd4696382017-10-24 11:44:05 +02001067/* Generic function to display messages prefixed by a label */
1068static void print_message(const char *label, const char *fmt, va_list argp)
1069{
1070 struct tm tm;
1071 char *head, *msg;
1072
1073 head = msg = NULL;
1074
1075 get_localtime(date.tv_sec, &tm);
1076 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
1077 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
1078 memvprintf(&msg, fmt, argp);
1079
Willy Tarreau869efd52019-11-15 15:16:57 +01001080 if (global.mode & MODE_STARTING) {
1081 if (unlikely(!startup_logs))
1082 startup_logs = ring_new(STARTUP_LOG_SIZE);
1083
1084 if (likely(startup_logs)) {
1085 struct ist m[2];
1086
1087 m[0] = ist(head);
1088 m[1] = ist(msg);
1089 /* trim the trailing '\n' */
1090 if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
1091 m[1].len--;
1092 ring_write(startup_logs, ~0, 0, 0, m, 2);
1093 }
1094 }
Christopher Fauletd4696382017-10-24 11:44:05 +02001095
1096 fprintf(stderr, "%s%s", head, msg);
1097 fflush(stderr);
1098
1099 free(head);
1100 free(msg);
1101}
1102
Willy Tarreaubaaee002006-06-26 02:48:02 +02001103/*
1104 * Displays the message on stderr with the date and pid. Overrides the quiet
1105 * mode during startup.
1106 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001107void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001108{
1109 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001110
1111 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaubb869862020-04-16 10:52:41 +02001112 if (!(warned & WARN_EXEC_PATH)) {
1113 const char *path = get_exec_path();
1114
1115 warned |= WARN_EXEC_PATH;
1116 ha_notice("haproxy version is %s\n", haproxy_version);
1117 if (path)
1118 ha_notice("path to executable is %s\n", path);
1119 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001120 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001121 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001122 va_end(argp);
1123 }
1124}
1125
1126
1127/*
1128 * Displays the message on stderr with the date and pid.
1129 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001130void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001131{
1132 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001133
Willy Tarreaubebd2122020-04-15 16:06:11 +02001134 warned |= WARN_ANY;
1135
Willy Tarreaubaaee002006-06-26 02:48:02 +02001136 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1137 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001138 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001139 va_end(argp);
1140 }
1141}
1142
1143/*
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001144 * Variant of _ha_diag_warning with va_list.
1145 * Use it only if MODE_DIAG has been previously checked.
1146 */
1147void _ha_vdiag_warning(const char *fmt, va_list argp)
1148{
1149 print_message("DIAG/WARNING", fmt, argp);
1150}
1151
1152/*
1153 * Output a diagnostic warning.
1154 * Use it only if MODE_DIAG has been previously checked.
1155 */
1156void _ha_diag_warning(const char *fmt, ...)
1157{
1158 va_list argp;
1159
1160 va_start(argp, fmt);
1161 _ha_vdiag_warning(fmt, argp);
1162 va_end(argp);
1163}
1164
1165/*
1166 * Output a diagnostic warning. Do nothing of MODE_DIAG is not on.
1167 */
1168void ha_diag_warning(const char *fmt, ...)
1169{
1170 va_list argp;
1171
1172 if (global.mode & MODE_DIAG) {
1173 va_start(argp, fmt);
1174 _ha_vdiag_warning(fmt, argp);
1175 va_end(argp);
1176 }
1177}
1178
1179/*
William Lallemand9c56a222018-11-21 18:04:52 +01001180 * Displays the message on stderr with the date and pid.
1181 */
1182void ha_notice(const char *fmt, ...)
1183{
1184 va_list argp;
1185
1186 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1187 va_start(argp, fmt);
1188 print_message("NOTICE", fmt, argp);
1189 va_end(argp);
1190 }
1191}
1192
1193/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001194 * Displays the message on <out> only if quiet mode is not set.
1195 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001196void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001197{
1198 va_list argp;
1199
1200 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1201 va_start(argp, fmt);
1202 vfprintf(out, fmt, argp);
1203 fflush(out);
1204 va_end(argp);
1205 }
1206}
1207
1208/*
Emeric Brun54648852020-07-06 15:54:06 +02001209 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001210 */
Emeric Brun54648852020-07-06 15:54:06 +02001211enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001212{
Emeric Brun54648852020-07-06 15:54:06 +02001213 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001214
1215 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001216 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001217 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001218 format--;
1219
Emeric Brun54648852020-07-06 15:54:06 +02001220 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001221 return format;
1222}
1223
1224/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001225 * returns log level for <lev> or -1 if not found.
1226 */
1227int get_log_level(const char *lev)
1228{
1229 int level;
1230
1231 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001232 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233 level--;
1234
1235 return level;
1236}
1237
Willy Tarreaubaaee002006-06-26 02:48:02 +02001238/*
1239 * returns log facility for <fac> or -1 if not found.
1240 */
1241int get_log_facility(const char *fac)
1242{
1243 int facility;
1244
1245 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001246 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001247 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001248
Willy Tarreaubaaee002006-06-26 02:48:02 +02001249 return facility;
1250}
1251
William Lallemanda1cc3812012-02-08 16:38:44 +01001252/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001253 * Encode the string.
1254 *
1255 * When using the +E log format option, it will try to escape '"\]'
1256 * characters with '\' as prefix. The same prefix should not be used as
1257 * <escape>.
1258 */
1259static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001260 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001261 const char *string,
1262 struct logformat_node *node)
1263{
1264 if (node->options & LOG_OPT_ESC) {
1265 if (start < stop) {
1266 stop--; /* reserve one byte for the final '\0' */
1267 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001268 if (!ha_bit_test((unsigned char)(*string), map)) {
1269 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001270 *start++ = *string;
1271 else {
1272 if (start + 2 >= stop)
1273 break;
1274 *start++ = '\\';
1275 *start++ = *string;
1276 }
1277 }
1278 else {
1279 if (start + 3 >= stop)
1280 break;
1281 *start++ = escape;
1282 *start++ = hextab[(*string >> 4) & 15];
1283 *start++ = hextab[*string & 15];
1284 }
1285 string++;
1286 }
1287 *start = '\0';
1288 }
1289 }
1290 else {
1291 return encode_string(start, stop, escape, map, string);
1292 }
1293
1294 return start;
1295}
1296
1297/*
1298 * Encode the chunk.
1299 *
1300 * When using the +E log format option, it will try to escape '"\]'
1301 * characters with '\' as prefix. The same prefix should not be used as
1302 * <escape>.
1303 */
1304static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001305 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001306 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001307 struct logformat_node *node)
1308{
1309 char *str, *end;
1310
1311 if (node->options & LOG_OPT_ESC) {
1312 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001313 str = chunk->area;
1314 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001315
1316 stop--; /* reserve one byte for the final '\0' */
1317 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001318 if (!ha_bit_test((unsigned char)(*str), map)) {
1319 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001320 *start++ = *str;
1321 else {
1322 if (start + 2 >= stop)
1323 break;
1324 *start++ = '\\';
1325 *start++ = *str;
1326 }
1327 }
1328 else {
1329 if (start + 3 >= stop)
1330 break;
1331 *start++ = escape;
1332 *start++ = hextab[(*str >> 4) & 15];
1333 *start++ = hextab[*str & 15];
1334 }
1335 str++;
1336 }
1337 *start = '\0';
1338 }
1339 }
1340 else {
1341 return encode_chunk(start, stop, escape, map, chunk);
1342 }
1343
1344 return start;
1345}
1346
1347/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001348 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001349 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001350 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001351 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001352 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001353char *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 +01001354{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001355 if (size < 2)
1356 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001357
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001358 if (node->options & LOG_OPT_QUOTE) {
1359 *(dst++) = '"';
1360 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001361 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001362
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001363 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001364 if (++len > size)
1365 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001366 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001367 char *ret;
1368
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001369 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001370 if (ret == NULL || *ret != '\0')
1371 return NULL;
1372 len = ret - dst;
1373 }
1374 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001375 len = strlcpy2(dst, src, len);
1376 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001377
1378 size -= len;
1379 dst += len;
1380 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001381 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1382 if (size < 2)
1383 return NULL;
1384 *(dst++) = '-';
1385 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001386
1387 if (node->options & LOG_OPT_QUOTE) {
1388 if (size < 2)
1389 return NULL;
1390 *(dst++) = '"';
1391 }
1392
1393 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001394 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001395}
1396
Willy Tarreau26ffa852018-09-05 15:23:10 +02001397static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001398{
1399 return lf_text_len(dst, src, size, size, node);
1400}
1401
William Lallemand5f232402012-04-05 18:02:55 +02001402/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001403 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001404 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001405 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001406char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001407{
1408 char *ret = dst;
1409 int iret;
1410 char pn[INET6_ADDRSTRLEN];
1411
1412 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001413 unsigned char *addr = NULL;
1414 switch (sockaddr->sa_family) {
1415 case AF_INET:
1416 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1417 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1418 break;
1419 case AF_INET6:
1420 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1421 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1422 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1423 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1424 break;
1425 default:
1426 return NULL;
1427 }
William Lallemand5f232402012-04-05 18:02:55 +02001428 if (iret < 0 || iret > size)
1429 return NULL;
1430 ret += iret;
1431 } else {
1432 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1433 ret = lf_text(dst, pn, size, node);
1434 if (ret == NULL)
1435 return NULL;
1436 }
1437 return ret;
1438}
1439
1440/*
1441 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001442 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001443 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001444char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001445{
1446 char *ret = dst;
1447 int iret;
1448
1449 if (node->options & LOG_OPT_HEXA) {
1450 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1451 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1452 if (iret < 0 || iret > size)
1453 return NULL;
1454 ret += iret;
1455 } else {
1456 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1457 if (ret == NULL)
1458 return NULL;
1459 }
1460 return ret;
1461}
1462
Emeric Brun54648852020-07-06 15:54:06 +02001463
1464/*
1465 * This function sends the syslog message using a printf format string. It
1466 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001467 */
Emeric Brun54648852020-07-06 15:54:06 +02001468void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001469{
Emeric Brun54648852020-07-06 15:54:06 +02001470 va_list argp;
1471 int data_len;
1472
1473 if (level < 0 || format == NULL || logline == NULL)
1474 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001475
Emeric Brun54648852020-07-06 15:54:06 +02001476 va_start(argp, format);
1477 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1478 if (data_len < 0 || data_len > global.max_syslog_len)
1479 data_len = global.max_syslog_len;
1480 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001481
Emeric Brun54648852020-07-06 15:54:06 +02001482 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1483 logline, data_len, default_rfc5424_sd_log_format, 2);
1484}
1485/*
1486 * This function builds a log header of given format using given
1487 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1488 * to determine format based on given metadas. It is useful
1489 * for log-forwarding to be able to forward any format without
1490 * settings.
1491 * This function returns a struct ist array of elements of the header
1492 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001493 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001494 * elements.
1495 */
1496struct ist *build_log_header(enum log_fmt format, int level, int facility,
1497 struct ist *metadata, size_t *nbelem)
1498{
1499 static THREAD_LOCAL struct {
1500 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1501 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1502 time_t cur_legacy_time;
1503 char priority_buffer[6];
1504 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001505
Emeric Brun54648852020-07-06 15:54:06 +02001506 struct tm logtime;
1507 int len;
1508 int fac_level = 0;
1509 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001510
Emeric Brun54648852020-07-06 15:54:06 +02001511 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001512
Emeric Brun54648852020-07-06 15:54:06 +02001513
1514 if (format == LOG_FORMAT_UNSPEC) {
1515 format = LOG_FORMAT_RAW;
1516 if (metadata) {
1517 /* If a hostname is set, it appears we want to perform syslog
1518 * because only rfc5427 or rfc3164 support an hostname.
1519 */
1520 if (metadata[LOG_META_HOST].len) {
1521 /* If a rfc5424 compliant timestamp is used we consider
1522 * that output format is rfc5424, else legacy format
1523 * is used as specified default for local logs
1524 * in documentation.
1525 */
1526 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1527 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1528 format = LOG_FORMAT_RFC5424;
1529 else
1530 format = LOG_FORMAT_RFC3164;
1531 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001532 else if (metadata[LOG_META_TAG].len) {
1533 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001534 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001535 * in legacy format (analog to RFC3164 but
1536 * with stripped hostname).
1537 */
1538 format = LOG_FORMAT_LOCAL;
1539 }
Emeric Brun54648852020-07-06 15:54:06 +02001540 else if (metadata[LOG_META_PRIO].len) {
1541 /* the source seems a parsed message
1542 * offering a valid level/prio prefix
1543 * so we consider this format.
1544 */
1545 format = LOG_FORMAT_PRIO;
1546 }
1547 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001548 }
1549
Emeric Brun54648852020-07-06 15:54:06 +02001550 /* prepare priority, stored into 1 single elem */
1551 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001552 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001553 case LOG_FORMAT_RFC3164:
1554 case LOG_FORMAT_RFC5424:
1555 case LOG_FORMAT_PRIO:
1556 fac_level = facility << 3;
1557 /* further format ignore the facility */
1558 /* fall through */
1559 case LOG_FORMAT_TIMED:
1560 case LOG_FORMAT_SHORT:
1561 fac_level += level;
1562 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1563 do {
1564 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1565 fac_level /= 10;
1566 hdr_ctx.ist_vector[*nbelem].ptr--;
1567 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1568 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1569 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1570 break;
1571 case LOG_FORMAT_ISO:
1572 case LOG_FORMAT_RAW:
1573 break;
1574 case LOG_FORMAT_UNSPEC:
1575 case LOG_FORMATS:
1576 ABORT_NOW();
1577 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001578
William Lallemand2a4a44f2012-02-06 16:00:33 +01001579
Emeric Brun54648852020-07-06 15:54:06 +02001580 /* prepare timestamp, stored into a max of 4 elems */
1581 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001582 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001583 case LOG_FORMAT_RFC3164:
1584 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1585 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1586 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1587 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001588 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001589 break;
1590 }
1591 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1592 int month;
1593 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001594
Emeric Brun54648852020-07-06 15:54:06 +02001595 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001596
Emeric Brun54648852020-07-06 15:54:06 +02001597 /* compute month */
1598 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1599 if (month)
1600 month--;
1601 if (month <= 11) {
1602 /* builds log prefix ex: 'Jan 1 ' */
1603 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1604 "%s %c%c ", monthname[month],
1605 timestamp[8] != '0' ? timestamp[8] : ' ',
1606 timestamp[9]);
1607 /* we reused the timestamp_buffer, signal that it does not
1608 * contain local time anymore
1609 */
1610 hdr_ctx.cur_legacy_time = 0;
1611 if (len == 7) {
1612 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1613 /* adds 'HH:MM:SS' from iso time */
1614 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1615 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1616 /* we successfully reuse iso time, we can break */
1617 break;
1618 }
1619 }
1620 /* Failed to reuse isotime time, fallback to local legacy time */
1621 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001622
Emeric Brun54648852020-07-06 15:54:06 +02001623 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1624 /* re-builds timestamp from the current local time */
1625 get_localtime(time, &logtime);
1626
1627 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1628 "%s %2d %02d:%02d:%02d ",
1629 monthname[logtime.tm_mon],
1630 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1631 if (len != LOG_LEGACYTIME_LEN+1)
1632 hdr_ctx.cur_legacy_time = 0;
1633 else
1634 hdr_ctx.cur_legacy_time = time;
1635 }
1636 if (likely(hdr_ctx.cur_legacy_time))
1637 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1638 else
1639 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1640 break;
1641 case LOG_FORMAT_RFC5424:
1642 /* adds rfc5425 version prefix */
1643 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1644 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001645 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001646 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1647 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1648 break;
1649 }
1650 /* let continue as 'timed' and 'iso' format for usual timestamp */
1651 /* fall through */
1652 case LOG_FORMAT_TIMED:
1653 case LOG_FORMAT_ISO:
1654 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1655 * '1900:01:01T14:00:00+02:00'
1656 * '1900:01:01T10:00:00.123456-02:00'
1657 */
1658 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1659 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1660 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001661 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001662 break;
1663 }
1664 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1665 int month;
1666 char *timestamp = metadata[LOG_META_TIME].ptr;
1667
1668 for (month = 0; month < 12; month++)
1669 if (!memcmp(monthname[month], timestamp, 3))
1670 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001671
Emeric Brun54648852020-07-06 15:54:06 +02001672 if (month < 12) {
1673
1674 /* get local time to retrieve year */
1675 get_localtime(time, &logtime);
1676
1677 /* year seems changed since log */
1678 if (logtime.tm_mon < month)
1679 logtime.tm_year--;
1680
1681 /* builds rfc5424 prefix ex: '1900-01-01T' */
1682 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1683 "%4d-%02d-%c%cT",
1684 logtime.tm_year+1900, month+1,
1685 timestamp[4] != ' ' ? timestamp[4] : '0',
1686 timestamp[5]);
1687
1688 /* we reused the timestamp_buffer, signal that it does not
1689 * contain local time anymore
1690 */
1691 hdr_ctx.cur_legacy_time = 0;
1692 if (len == 11) {
1693 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1694 /* adds HH:MM:SS from legacy timestamp */
1695 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001696 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001697 /* according to rfc: -00:00 means we don't know the timezone */
1698 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1699 /* we successfully reuse legacy time, we can break */
1700 break;
1701 }
1702 }
1703 /* Failed to reuse legacy time, fallback to local iso time */
1704 }
1705 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1706 break;
1707 case LOG_FORMAT_PRIO:
1708 case LOG_FORMAT_SHORT:
1709 case LOG_FORMAT_RAW:
1710 break;
1711 case LOG_FORMAT_UNSPEC:
1712 case LOG_FORMATS:
1713 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001714 }
1715
Emeric Brun54648852020-07-06 15:54:06 +02001716 /* prepare other meta data, stored into a max of 10 elems */
1717 switch (format) {
1718 case LOG_FORMAT_RFC3164:
1719 if (metadata && metadata[LOG_META_HOST].len) {
1720 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1721 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1722 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001723 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001724 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001725 /* fall through */
1726 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001727 if (!metadata || !metadata[LOG_META_TAG].len)
1728 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001729
Emeric Brun54648852020-07-06 15:54:06 +02001730 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1731 if (metadata[LOG_META_PID].len) {
1732 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1733 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1734 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1735 }
1736 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1737 break;
1738 case LOG_FORMAT_RFC5424:
1739 if (metadata && metadata[LOG_META_HOST].len) {
1740 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1741 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1742 }
1743 else
1744 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001745
Emeric Brun54648852020-07-06 15:54:06 +02001746 if (metadata && metadata[LOG_META_TAG].len) {
1747 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1748 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1749 }
1750 else
1751 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001752
Emeric Brun54648852020-07-06 15:54:06 +02001753 if (metadata && metadata[LOG_META_PID].len) {
1754 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1755 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1756 }
1757 else
1758 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001759
Emeric Brun54648852020-07-06 15:54:06 +02001760 if (metadata && metadata[LOG_META_MSGID].len) {
1761 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1762 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1763 }
1764 else
1765 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001766
Emeric Brun54648852020-07-06 15:54:06 +02001767 if (metadata && metadata[LOG_META_STDATA].len) {
1768 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1769 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1770 }
1771 else
1772 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1773 break;
1774 case LOG_FORMAT_PRIO:
1775 case LOG_FORMAT_SHORT:
1776 case LOG_FORMAT_TIMED:
1777 case LOG_FORMAT_ISO:
1778 case LOG_FORMAT_RAW:
1779 break;
1780 case LOG_FORMAT_UNSPEC:
1781 case LOG_FORMATS:
1782 ABORT_NOW();
1783 }
1784
1785 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001786}
1787
1788/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001789 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001790 * The argument <metadata> MUST be an array of size
1791 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001792 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001793 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001794 */
Emeric Brun54648852020-07-06 15:54:06 +02001795static 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 +01001796{
Emeric Brun54648852020-07-06 15:54:06 +02001797 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001798 static THREAD_LOCAL struct msghdr msghdr = {
1799 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001800 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001801 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001802 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1803 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001804 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001805 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001806 size_t nbelem;
1807 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001808
1809 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001810
Emeric Brunfa9d7802020-05-28 14:21:33 +02001811 /* historically some messages used to already contain the trailing LF
1812 * or Zero. Let's remove all trailing LF or Zero
1813 */
Emeric Brun54648852020-07-06 15:54:06 +02001814 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001815 size--;
1816
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001817 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001818 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001819 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001820 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001821 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1822 /* the socket's address is a file descriptor */
1823 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1824 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001825 else if (logsrv->addr.ss_family == AF_UNIX)
1826 plogfd = &logfdunix;
1827 else
1828 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001829
Willy Tarreauc046d162019-08-30 15:24:59 +02001830 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001831 /* socket not successfully initialized yet */
1832 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1833 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1834 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001835
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001836 if (!once) {
1837 once = 1; /* note: no need for atomic ops here */
1838 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1839 nblogger, strerror(errno), errno);
1840 }
1841 return;
1842 } else {
1843 /* we don't want to receive anything on this socket */
1844 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1845 /* does nothing under Linux, maybe needed for others */
1846 shutdown(*plogfd, SHUT_RD);
1847 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1848 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001849 }
1850
Emeric Brun54648852020-07-06 15:54:06 +02001851 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1852 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001853 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001854 struct ist msg;
1855
1856 msg = ist2(message, size);
1857 if (msg.len > logsrv->maxlen)
1858 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001859
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001860 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1861 }
1862 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1863 struct ist msg;
1864
1865 msg = ist2(message, size);
1866 if (msg.len > logsrv->maxlen)
1867 msg.len = logsrv->maxlen;
1868
1869 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001870 }
1871 else {
Emeric Brun54648852020-07-06 15:54:06 +02001872 int i = 0;
1873 int totlen = logsrv->maxlen;
1874
1875 for (i = 0 ; i < nbelem ; i++ ) {
1876 iovec[i].iov_base = msg_header[i].ptr;
1877 iovec[i].iov_len = msg_header[i].len;
1878 if (totlen <= iovec[i].iov_len) {
1879 iovec[i].iov_len = totlen;
1880 totlen = 0;
1881 break;
1882 }
1883 totlen -= iovec[i].iov_len;
1884 }
1885 if (totlen) {
1886 iovec[i].iov_base = message;
1887 iovec[i].iov_len = size;
1888 if (totlen <= iovec[i].iov_len)
1889 iovec[i].iov_len = totlen;
1890 i++;
1891 }
1892 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1893 iovec[i].iov_len = 1;
1894 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001895
Emeric Brun54648852020-07-06 15:54:06 +02001896 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001897 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1898 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001899
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001900 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1901 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001902
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001903 if (sent < 0) {
1904 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001905
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001906 if (errno == EAGAIN)
1907 _HA_ATOMIC_ADD(&dropped_logs, 1);
1908 else if (!once) {
1909 once = 1; /* note: no need for atomic ops here */
1910 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1911 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001912 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001913 }
1914}
Dragan Dosen59cee972015-09-19 22:09:02 +02001915
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001916/*
1917 * This function sends a syslog message.
1918 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001919 * The argument <metadata> MUST be an array of size
1920 * LOG_META_FIELDS*sizeof(struct ist) containing
1921 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001922 */
Emeric Brun54648852020-07-06 15:54:06 +02001923void process_send_log(struct list *logsrvs, int level, int facility,
1924 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001925{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001926 struct logsrv *logsrv;
1927 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001928
1929 /* Send log messages to syslog server. */
1930 nblogger = 0;
1931 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001932 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001933
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001934 /* we can filter the level of the messages that are sent to each logger */
1935 if (level > logsrv->level)
1936 continue;
1937
Frédéric Lécailled803e472019-04-25 07:42:09 +02001938 if (logsrv->lb.smp_rgs) {
1939 struct smp_log_range *curr_rg;
1940
1941 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1942 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1943 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1944 if (in_range) {
1945 /* Let's consume this range. */
1946 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1947 if (!curr_rg->curr_idx) {
1948 /* If consumed, let's select the next range. */
1949 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1950 }
1951 }
1952 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1953 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1954 }
1955 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001956 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1957 (facility == -1) ? logsrv->facility : facility,
1958 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001959 }
1960}
1961
Emeric Brun54648852020-07-06 15:54:06 +02001962/*
1963 * This function sends a syslog message.
1964 * It doesn't care about errors nor does it report them.
1965 * The arguments <sd> and <sd_size> are used for the structured-data part
1966 * in RFC5424 formatted syslog messages.
1967 */
1968void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1969 char *message, size_t size, char *sd, size_t sd_size)
1970{
1971 static THREAD_LOCAL pid_t curr_pid;
1972 static THREAD_LOCAL char pidstr[16];
1973 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1974
1975 if (logsrvs == NULL) {
1976 if (!LIST_ISEMPTY(&global.logsrvs)) {
1977 logsrvs = &global.logsrvs;
1978 }
1979 }
1980 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1981 return;
1982
1983 if (!metadata[LOG_META_HOST].len) {
1984 if (global.log_send_hostname)
1985 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02001986 }
1987
1988 if (!tagb || !tagb->area)
1989 tagb = &global.log_tag;
1990
1991 if (tagb)
1992 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1993
1994 if (unlikely(curr_pid != getpid()))
1995 metadata[LOG_META_PID].len = 0;
1996
1997 if (!metadata[LOG_META_PID].len) {
1998 curr_pid = getpid();
1999 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
2000 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
2001 }
2002
2003 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
2004
2005 /* Remove trailing space of structured data */
2006 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
2007 metadata[LOG_META_STDATA].len--;
2008
2009 return process_send_log(logsrvs, level, -1, metadata, message, size);
2010}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002011
Willy Tarreauc89ccb62012-04-05 21:18:22 +02002012const 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 +01002013const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
2014 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
2015 Set-cookie Updated, unknown, unknown */
2016
William Lallemand1d705562012-03-12 12:46:41 +01002017/*
2018 * try to write a character if there is enough space, or goto out
2019 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01002020#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01002021 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01002022 *(tmplog++) = (x); \
2023 } else { \
2024 goto out; \
2025 } \
2026 } while(0)
2027
Dragan Dosen835b9212016-02-12 13:23:03 +01002028
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002029/* Initializes some log data at boot */
2030static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01002031{
2032 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02002033 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01002034
2035 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
2036 * inside PARAM-VALUE should be escaped with '\' as prefix.
2037 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
2038 * details.
2039 */
2040 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
2041
2042 tmp = "\"\\]";
2043 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002044 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002045 tmp++;
2046 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02002047
2048 /* initialize the log header encoding map : '{|}"#' should be encoded with
2049 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
2050 * URL encoding only requires '"', '#' to be encoded as well as non-
2051 * printable characters above.
2052 */
2053 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
2054 memset(url_encode_map, 0, sizeof(url_encode_map));
2055 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002056 ha_bit_set(i, hdr_encode_map);
2057 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002058 }
2059 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002060 ha_bit_set(i, hdr_encode_map);
2061 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002062 }
2063
2064 tmp = "\"#{|}";
2065 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002066 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002067 tmp++;
2068 }
2069
2070 tmp = "\"#";
2071 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002072 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002073 tmp++;
2074 }
2075
2076 /* initialize the http header encoding map. The draft httpbis define the
2077 * header content as:
2078 *
2079 * HTTP-message = start-line
2080 * *( header-field CRLF )
2081 * CRLF
2082 * [ message-body ]
2083 * header-field = field-name ":" OWS field-value OWS
2084 * field-value = *( field-content / obs-fold )
2085 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2086 * obs-fold = CRLF 1*( SP / HTAB )
2087 * field-vchar = VCHAR / obs-text
2088 * VCHAR = %x21-7E
2089 * obs-text = %x80-FF
2090 *
2091 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
2092 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08002093 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02002094 */
2095 memset(http_encode_map, 0, sizeof(http_encode_map));
2096 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002097 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002098 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002099 ha_bit_set(i, http_encode_map);
2100 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002101}
William Lallemand1d705562012-03-12 12:46:41 +01002102
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002103INITCALL0(STG_PREPARE, init_log);
2104
Christopher Faulet0132d062017-07-26 15:33:35 +02002105/* Initialize log buffers used for syslog messages */
2106int init_log_buffers()
2107{
Christopher Faulet0132d062017-07-26 15:33:35 +02002108 logline = my_realloc2(logline, global.max_syslog_len + 1);
2109 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02002110 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02002111 return 0;
2112 return 1;
2113}
2114
2115/* Deinitialize log buffers used for syslog messages */
2116void deinit_log_buffers()
2117{
Christopher Faulet0132d062017-07-26 15:33:35 +02002118 free(logline);
2119 free(logline_rfc5424);
Willy Tarreau869efd52019-11-15 15:16:57 +01002120 ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
Christopher Faulet0132d062017-07-26 15:33:35 +02002121 logline = NULL;
2122 logline_rfc5424 = NULL;
2123}
2124
Willy Tarreaudf974472012-12-28 02:44:01 +01002125/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2126 * <maxsize> characters. Returns the size of the output string in characters,
2127 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002128 * is not zero. It requires a valid session and optionally a stream. If the
2129 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002130 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002131int 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 +02002132{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002133 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002134 struct proxy *be;
2135 struct http_txn *txn;
2136 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002137 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002138 unsigned int s_flags;
2139 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002140 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002141 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002142 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002143 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002144 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002145 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002146 int t_request;
2147 int hdr;
2148 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002149 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002150 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002151 char *ret;
2152 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002153 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002154 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002155 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002156 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002157 struct ist path;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002158
William Lallemandbddd4fd2012-02-27 11:23:10 +01002159 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002160
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002161 if (likely(s)) {
2162 be = s->be;
2163 txn = s->txn;
2164 be_conn = cs_conn(objt_cs(s->si[1].end));
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002165 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002166 s_flags = s->flags;
2167 uniq_id = s->uniq_id;
2168 logs = &s->logs;
2169 } else {
2170 /* we have no stream so we first need to initialize a few
2171 * things that are needed later. We do increment the request
2172 * ID so that it's uniquely assigned to this request just as
2173 * if the request had reached the point of being processed.
2174 * A request error is reported as it's the only element we have
2175 * here and which justifies emitting such a log.
2176 */
2177 be = fe;
2178 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002179 fe_conn = objt_conn(sess->origin);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002180 be_conn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002181 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002182 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01002183 uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002184
2185 /* prepare a valid log structure */
2186 tmp_strm_log.tv_accept = sess->tv_accept;
2187 tmp_strm_log.accept_date = sess->accept_date;
2188 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02002189 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002190 tv_zero(&tmp_strm_log.tv_request);
2191 tmp_strm_log.t_queue = -1;
2192 tmp_strm_log.t_connect = -1;
2193 tmp_strm_log.t_data = -1;
2194 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2195 tmp_strm_log.bytes_in = 0;
2196 tmp_strm_log.bytes_out = 0;
2197 tmp_strm_log.prx_queue_pos = 0;
2198 tmp_strm_log.srv_queue_pos = 0;
2199
2200 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002201
2202 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
2203 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, NULL);
2204
2205 switch (es) {
2206 case MUX_ES_SUCCESS:
2207 break;
2208 case MUX_ES_INVALID_ERR:
2209 status = 400;
2210 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2211 s_flags = SF_ERR_CLICL | SF_FINST_R;
2212 else
2213 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2214 break;
2215 case MUX_ES_TOUT_ERR:
2216 status = 408;
2217 s_flags = SF_ERR_CLITO | SF_FINST_R;
2218 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002219 case MUX_ES_NOTIMPL_ERR:
2220 status = 501;
2221 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2222 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002223 case MUX_ES_INTERNAL_ERR:
2224 status = 500;
2225 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2226 break;
2227 default:
2228 break;
2229 }
2230 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002231 }
2232
William Lallemandbddd4fd2012-02-27 11:23:10 +01002233 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002234 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2235 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002236
William Lallemand1d705562012-03-12 12:46:41 +01002237 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002238
William Lallemandbddd4fd2012-02-27 11:23:10 +01002239 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002240 if (LIST_ISEMPTY(list_format))
2241 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002242
William Lallemand1d705562012-03-12 12:46:41 +01002243 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002244 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002245 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002246 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002247 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002248
Willy Tarreauc8368452012-12-21 00:09:23 +01002249 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002250 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002251 if (!last_isspace) {
2252 LOGCHAR(' ');
2253 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002254 }
2255 break;
2256
William Lallemand1d705562012-03-12 12:46:41 +01002257 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002258 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002259 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002260 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002261 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002262 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002263 last_isspace = 0;
2264 break;
2265
Willy Tarreauc8368452012-12-21 00:09:23 +01002266 case LOG_FMT_EXPR: // sample expression, may be request or response
2267 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002268 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002269 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 +02002270 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002271 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 +01002272 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002273 ret = lf_encode_chunk(tmplog, dst + maxsize,
2274 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002275 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002276 ret = lf_text_len(tmplog,
2277 key ? key->data.u.str.area : NULL,
2278 key ? key->data.u.str.data : 0,
2279 dst + maxsize - tmplog,
2280 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002281 if (ret == 0)
2282 goto out;
2283 tmplog = ret;
2284 last_isspace = 0;
2285 break;
2286
Willy Tarreau2beef582012-12-20 17:22:52 +01002287 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002288 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002289 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002290 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002291 else
2292 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002293 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002294 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002295 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002296 last_isspace = 0;
2297 break;
2298
Willy Tarreau2beef582012-12-20 17:22:52 +01002299 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002300 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002301 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002302 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002303 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002304 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002305 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002306 dst + maxsize - tmplog, tmp);
2307 }
William Lallemand5f232402012-04-05 18:02:55 +02002308 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002309 else
2310 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2311
William Lallemand5f232402012-04-05 18:02:55 +02002312 if (ret == NULL)
2313 goto out;
2314 tmplog = ret;
2315 last_isspace = 0;
2316 break;
2317
Willy Tarreau2beef582012-12-20 17:22:52 +01002318 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002319 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002320 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002321 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002322 }
2323 else
2324 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2325
William Lallemand1d705562012-03-12 12:46:41 +01002326 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002327 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002328 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002329 last_isspace = 0;
2330 break;
2331
Willy Tarreau2beef582012-12-20 17:22:52 +01002332 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002333 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002334 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002335 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002336 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002337 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002338 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002339 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002340 else
2341 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2342
William Lallemand5f232402012-04-05 18:02:55 +02002343 if (ret == NULL)
2344 goto out;
2345 tmplog = ret;
2346 last_isspace = 0;
2347 break;
2348
Willy Tarreau2beef582012-12-20 17:22:52 +01002349 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002350 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002351 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002352 else
2353 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2354
William Lallemand1d705562012-03-12 12:46:41 +01002355 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002356 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002357 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002358 last_isspace = 0;
2359 break;
2360
Willy Tarreau2beef582012-12-20 17:22:52 +01002361 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002362 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002363 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002364 else
2365 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2366
William Lallemand5f232402012-04-05 18:02:55 +02002367 if (ret == NULL)
2368 goto out;
2369 tmplog = ret;
2370 last_isspace = 0;
2371 break;
2372
Willy Tarreau2beef582012-12-20 17:22:52 +01002373 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002374 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002375 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002376 else
2377 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2378
William Lallemand5f232402012-04-05 18:02:55 +02002379 if (ret == NULL)
2380 goto out;
2381 tmplog = ret;
2382 last_isspace = 0;
2383 break;
2384
Willy Tarreau2beef582012-12-20 17:22:52 +01002385 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002386 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002387 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002388 else
2389 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2390
William Lallemand1d705562012-03-12 12:46:41 +01002391 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002392 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002393 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002394 last_isspace = 0;
2395 break;
2396
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002397 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002398 get_localtime(logs->accept_date.tv_sec, &tm);
2399 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002400 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002401 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002402 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002403 last_isspace = 0;
2404 break;
2405
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002406 case LOG_FMT_tr: // %tr = start of request date
2407 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002408 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 +02002409 get_localtime(tv.tv_sec, &tm);
2410 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2411 if (ret == NULL)
2412 goto out;
2413 tmplog = ret;
2414 last_isspace = 0;
2415 break;
2416
2417 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002418 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002419 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002420 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002421 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002422 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002423 last_isspace = 0;
2424 break;
2425
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002426 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002427 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 +02002428 get_gmtime(tv.tv_sec, &tm);
2429 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2430 if (ret == NULL)
2431 goto out;
2432 tmplog = ret;
2433 last_isspace = 0;
2434 break;
2435
2436 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002437 get_localtime(logs->accept_date.tv_sec, &tm);
2438 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002439 if (ret == NULL)
2440 goto out;
2441 tmplog = ret;
2442 last_isspace = 0;
2443 break;
2444
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002445 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002446 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 +02002447 get_localtime(tv.tv_sec, &tm);
2448 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2449 if (ret == NULL)
2450 goto out;
2451 tmplog = ret;
2452 last_isspace = 0;
2453 break;
2454
William Lallemand5f232402012-04-05 18:02:55 +02002455 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002456 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002457 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002458 if (iret < 0 || iret > dst + maxsize - tmplog)
2459 goto out;
2460 last_isspace = 0;
2461 tmplog += iret;
2462 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002463 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002464 if (ret == NULL)
2465 goto out;
2466 tmplog = ret;
2467 last_isspace = 0;
2468 }
2469 break;
2470
William Lallemand1d705562012-03-12 12:46:41 +01002471 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002472 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002473 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002474 if (iret < 0 || iret > dst + maxsize - tmplog)
2475 goto out;
2476 last_isspace = 0;
2477 tmplog += iret;
2478 } else {
2479 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002480 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002481 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002482 tmplog, 4);
2483 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002484 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002485 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002486 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002487 }
2488 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002489
William Lallemand1d705562012-03-12 12:46:41 +01002490 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002491 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002492 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002493 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002494 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002495 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002496 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002497 break;
2498
Willy Tarreau773d65f2012-10-12 14:56:11 +02002499 case LOG_FMT_FRONTEND_XPRT: // %ft
2500 src = fe->id;
2501 if (tmp->options & LOG_OPT_QUOTE)
2502 LOGCHAR('"');
2503 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2504 if (iret == 0)
2505 goto out;
2506 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002507 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002508 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002509 if (tmp->options & LOG_OPT_QUOTE)
2510 LOGCHAR('"');
2511 last_isspace = 0;
2512 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002513#ifdef USE_OPENSSL
2514 case LOG_FMT_SSL_CIPHER: // %sslc
2515 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002516 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002517 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002518 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002519 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002520 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2521 if (ret == NULL)
2522 goto out;
2523 tmplog = ret;
2524 last_isspace = 0;
2525 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002526
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002527 case LOG_FMT_SSL_VERSION: // %sslv
2528 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002529 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002530 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002531 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002532 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002533 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2534 if (ret == NULL)
2535 goto out;
2536 tmplog = ret;
2537 last_isspace = 0;
2538 break;
2539#endif
William Lallemand1d705562012-03-12 12:46:41 +01002540 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002541 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002542 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002543 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002544 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002545 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002546 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002547 break;
2548
William Lallemand1d705562012-03-12 12:46:41 +01002549 case LOG_FMT_SERVER: // %s
Willy Tarreaue1809df2018-09-05 15:30:16 +02002550 switch (obj_type(s ? s->target : NULL)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002551 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002552 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002553 break;
2554 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002555 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002556 break;
2557 default:
2558 src = "<NOSRV>";
2559 break;
2560 }
William Lallemand5f232402012-04-05 18:02:55 +02002561 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002562 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002563 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002564 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002565 last_isspace = 0;
2566 break;
2567
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002568 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002569 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002570 if (ret == NULL)
2571 goto out;
2572 tmplog = ret;
2573 last_isspace = 0;
2574 break;
2575
2576 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002577 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002578 if (ret == NULL)
2579 goto out;
2580 tmplog = ret;
2581 last_isspace = 0;
2582 break;
2583
2584 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002585 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002586 tmplog, dst + maxsize - tmplog);
2587 if (ret == NULL)
2588 goto out;
2589 tmplog = ret;
2590 last_isspace = 0;
2591 break;
2592
2593 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002594 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002595 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002596 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002597 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002598 last_isspace = 0;
2599 break;
2600
William Lallemand1d705562012-03-12 12:46:41 +01002601 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002602 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002603 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002604 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002605 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002606 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002607 last_isspace = 0;
2608 break;
2609
William Lallemand1d705562012-03-12 12:46:41 +01002610 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002611 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002612 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002613 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002614 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002615 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002616 last_isspace = 0;
2617 break;
2618
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002619 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002620 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002621 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002622 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002623 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002624 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002625 last_isspace = 0;
2626 break;
2627
Willy Tarreau27b639d2016-05-17 17:55:27 +02002628 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002629 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002630 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002631 tmplog, dst + maxsize - tmplog);
2632 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002633 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002634 tmplog, dst + maxsize - tmplog);
2635 if (ret == NULL)
2636 goto out;
2637 tmplog = ret;
2638 last_isspace = 0;
2639 break;
2640
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002641 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2642 if (!(fe->to_log & LW_BYTES))
2643 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002644 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 +02002645 tmplog, dst + maxsize - tmplog);
2646 if (ret == NULL)
2647 goto out;
2648 tmplog = ret;
2649 last_isspace = 0;
2650 break;
2651
2652 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002653 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002654 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002655 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002656 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002657 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002658 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002659 last_isspace = 0;
2660 break;
2661
Damien Claisse57c8eb92020-04-28 12:09:19 +00002662 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2663 if (!(fe->to_log & LW_BYTES))
2664 LOGCHAR('+');
2665 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2666 tmplog, dst + maxsize - tmplog);
2667 if (ret == NULL)
2668 goto out;
2669 tmplog = ret;
2670 last_isspace = 0;
2671 break;
2672
Willy Tarreau2beef582012-12-20 17:22:52 +01002673 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002674 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002675 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002676 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002677 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002678 last_isspace = 0;
2679 break;
2680
William Lallemand1d705562012-03-12 12:46:41 +01002681 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002682 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002683 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002684 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002685 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002686 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002687 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002688 last_isspace = 0;
2689 break;
2690
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002691 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002692 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002693 if (ret == NULL)
2694 goto out;
2695 tmplog = ret;
2696 last_isspace = 0;
2697 break;
2698
Willy Tarreau2beef582012-12-20 17:22:52 +01002699 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002700 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002701 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002702 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002703 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002704 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002705 last_isspace = 0;
2706 break;
2707
Willy Tarreau2beef582012-12-20 17:22:52 +01002708 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002709 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002710 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002711 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002712 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002713 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002714 last_isspace = 0;
2715 break;
2716
William Lallemand1d705562012-03-12 12:46:41 +01002717 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002718 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2719 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002720 *tmplog = '\0';
2721 last_isspace = 0;
2722 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002723
William Lallemand1d705562012-03-12 12:46:41 +01002724 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002725 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2726 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002727 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2728 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 +01002729 last_isspace = 0;
2730 break;
2731
William Lallemand1d705562012-03-12 12:46:41 +01002732 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002733 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002734 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002735 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002736 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002737 last_isspace = 0;
2738 break;
2739
William Lallemand1d705562012-03-12 12:46:41 +01002740 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002741 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002742 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002743 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002744 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002745 last_isspace = 0;
2746 break;
2747
William Lallemand1d705562012-03-12 12:46:41 +01002748 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002749 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002750 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002751 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002752 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002753 last_isspace = 0;
2754 break;
2755
William Lallemand1d705562012-03-12 12:46:41 +01002756 case LOG_FMT_SRVCONN: // %sc
Willy Tarreaue1809df2018-09-05 15:30:16 +02002757 ret = ultoa_o(objt_server(s ? s->target : NULL) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002758 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002759 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002760 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002761 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002762 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002763 last_isspace = 0;
2764 break;
2765
William Lallemand1d705562012-03-12 12:46:41 +01002766 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002767 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002768 LOGCHAR('+');
Willy Tarreauabd71a52018-09-04 19:21:44 +02002769 ret = ltoa_o((s && s->si[1].conn_retries > 0) ?
Willy Tarreau350f4872014-11-28 14:42:25 +01002770 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002771 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002772 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002773 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_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002779 ret = ltoa_o(logs->srv_queue_pos, 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_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002787 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002788 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002789 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002790 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002791 last_isspace = 0;
2792 break;
2793
William Lallemand1d705562012-03-12 12:46:41 +01002794 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002795 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002796 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002797 if (tmp->options & LOG_OPT_QUOTE)
2798 LOGCHAR('"');
2799 LOGCHAR('{');
2800 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2801 if (hdr)
2802 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002803 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002804 ret = lf_encode_string(tmplog, dst + maxsize,
2805 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002806 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002807 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002808 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002809 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002810 }
2811 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002812 if (tmp->options & LOG_OPT_QUOTE)
2813 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002814 last_isspace = 0;
2815 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002816 break;
2817
William Lallemand1d705562012-03-12 12:46:41 +01002818 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002819 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002820 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002821 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2822 if (hdr > 0)
2823 LOGCHAR(' ');
2824 if (tmp->options & LOG_OPT_QUOTE)
2825 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002826 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002827 ret = lf_encode_string(tmplog, dst + maxsize,
2828 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002829 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002830 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002831 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002832 } else if (!(tmp->options & LOG_OPT_QUOTE))
2833 LOGCHAR('-');
2834 if (tmp->options & LOG_OPT_QUOTE)
2835 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002836 last_isspace = 0;
2837 }
2838 }
2839 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002840
William Lallemand1d705562012-03-12 12:46:41 +01002841
2842 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002843 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002844 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002845 if (tmp->options & LOG_OPT_QUOTE)
2846 LOGCHAR('"');
2847 LOGCHAR('{');
2848 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2849 if (hdr)
2850 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002851 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002852 ret = lf_encode_string(tmplog, dst + maxsize,
2853 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002854 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002855 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002856 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002857 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002858 }
2859 LOGCHAR('}');
2860 last_isspace = 0;
2861 if (tmp->options & LOG_OPT_QUOTE)
2862 LOGCHAR('"');
2863 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002864 break;
2865
William Lallemand1d705562012-03-12 12:46:41 +01002866 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002867 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002868 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002869 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2870 if (hdr > 0)
2871 LOGCHAR(' ');
2872 if (tmp->options & LOG_OPT_QUOTE)
2873 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002874 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002875 ret = lf_encode_string(tmplog, dst + maxsize,
2876 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002877 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002878 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002879 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002880 } else if (!(tmp->options & LOG_OPT_QUOTE))
2881 LOGCHAR('-');
2882 if (tmp->options & LOG_OPT_QUOTE)
2883 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002884 last_isspace = 0;
2885 }
2886 }
2887 break;
2888
William Lallemand1d705562012-03-12 12:46:41 +01002889 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002890 /* Request */
2891 if (tmp->options & LOG_OPT_QUOTE)
2892 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002893 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002894 ret = lf_encode_string(tmplog, dst + maxsize,
2895 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002896 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002897 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002898 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002899 if (tmp->options & LOG_OPT_QUOTE)
2900 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002901 last_isspace = 0;
2902 break;
William Lallemand5f232402012-04-05 18:02:55 +02002903
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002904 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002905 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002906
Willy Tarreaub7636d12015-06-17 19:58:02 +02002907 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002908 LOGCHAR('"');
2909
2910 end = uri + strlen(uri);
2911 // look for the first whitespace character
2912 while (uri < end && !HTTP_IS_SPHT(*uri))
2913 uri++;
2914
2915 // keep advancing past multiple spaces
2916 while (uri < end && HTTP_IS_SPHT(*uri)) {
2917 uri++; nspaces++;
2918 }
2919
2920 // look for first space or question mark after url
2921 spc = uri;
2922 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2923 spc++;
2924
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002925 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002926 chunk.area = "<BADREQ>";
2927 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002928 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002929 chunk.area = uri;
2930 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002931 }
2932
Dragan Dosen835b9212016-02-12 13:23:03 +01002933 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002934 if (ret == NULL || *ret != '\0')
2935 goto out;
2936
2937 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002938 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002939 LOGCHAR('"');
2940
2941 last_isspace = 0;
2942 break;
2943
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002944 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2945 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2946
2947 if (tmp->options & LOG_OPT_QUOTE)
2948 LOGCHAR('"');
2949
2950 end = uri + strlen(uri);
2951
2952 // look for the first whitespace character
2953 while (uri < end && !HTTP_IS_SPHT(*uri))
2954 uri++;
2955
2956 // keep advancing past multiple spaces
2957 while (uri < end && HTTP_IS_SPHT(*uri)) {
2958 uri++; nspaces++;
2959 }
2960
2961 // look for first space after url
2962 spc = uri;
2963 while (spc < end && !HTTP_IS_SPHT(*spc))
2964 spc++;
2965
Tim Duesterhus92c696e2021-02-28 16:11:36 +01002966 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002967
2968 // extract relative path without query params from url
2969 path = iststop(http_get_path(path), '?');
2970 if (!txn || !txn->uri || nspaces == 0) {
2971 chunk.area = "<BADREQ>";
2972 chunk.data = strlen("<BADREQ>");
2973 } else {
2974 chunk.area = path.ptr;
2975 chunk.data = path.len;
2976 }
2977
2978 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2979 if (ret == NULL || *ret != '\0')
2980 goto out;
2981
2982 tmplog = ret;
2983 if (tmp->options & LOG_OPT_QUOTE)
2984 LOGCHAR('"');
2985
2986 last_isspace = 0;
2987 break;
2988
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002989 case LOG_FMT_HTTP_QUERY: // %HQ
2990 if (tmp->options & LOG_OPT_QUOTE)
2991 LOGCHAR('"');
2992
Willy Tarreau57bc8912016-04-25 17:09:40 +02002993 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002994 chunk.area = "<BADREQ>";
2995 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002996 } else {
2997 uri = txn->uri;
2998 end = uri + strlen(uri);
2999 // look for the first question mark
3000 while (uri < end && *uri != '?')
3001 uri++;
3002
3003 qmark = uri;
3004 // look for first space or question mark after url
3005 while (uri < end && !HTTP_IS_SPHT(*uri))
3006 uri++;
3007
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003008 chunk.area = qmark;
3009 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003010 }
3011
Dragan Dosen835b9212016-02-12 13:23:03 +01003012 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003013 if (ret == NULL || *ret != '\0')
3014 goto out;
3015
3016 tmplog = ret;
3017 if (tmp->options & LOG_OPT_QUOTE)
3018 LOGCHAR('"');
3019
3020 last_isspace = 0;
3021 break;
3022
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003023 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02003024 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003025
Willy Tarreaub7636d12015-06-17 19:58:02 +02003026 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003027 LOGCHAR('"');
3028
3029 end = uri + strlen(uri);
3030 // look for the first whitespace character
3031 while (uri < end && !HTTP_IS_SPHT(*uri))
3032 uri++;
3033
3034 // keep advancing past multiple spaces
3035 while (uri < end && HTTP_IS_SPHT(*uri)) {
3036 uri++; nspaces++;
3037 }
3038
3039 // look for first space after url
3040 spc = uri;
3041 while (spc < end && !HTTP_IS_SPHT(*spc))
3042 spc++;
3043
Willy Tarreau57bc8912016-04-25 17:09:40 +02003044 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003045 chunk.area = "<BADREQ>";
3046 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003047 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003048 chunk.area = uri;
3049 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003050 }
3051
Dragan Dosen835b9212016-02-12 13:23:03 +01003052 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003053 if (ret == NULL || *ret != '\0')
3054 goto out;
3055
3056 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003057 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003058 LOGCHAR('"');
3059
3060 last_isspace = 0;
3061 break;
3062
3063 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02003064 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003065 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003066 LOGCHAR('"');
3067
3068 end = uri + strlen(uri);
3069 // look for the first whitespace character
3070 spc = uri;
3071 while (spc < end && !HTTP_IS_SPHT(*spc))
3072 spc++;
3073
3074 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003075 chunk.area = "<BADREQ>";
3076 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003077 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003078 chunk.area = uri;
3079 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003080 }
3081
Dragan Dosen835b9212016-02-12 13:23:03 +01003082 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003083 if (ret == NULL || *ret != '\0')
3084 goto out;
3085
3086 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003087 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003088 LOGCHAR('"');
3089
3090 last_isspace = 0;
3091 break;
3092
3093 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02003094 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003095 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003096 LOGCHAR('"');
3097
3098 end = uri + strlen(uri);
3099 // look for the first whitespace character
3100 while (uri < end && !HTTP_IS_SPHT(*uri))
3101 uri++;
3102
3103 // keep advancing past multiple spaces
3104 while (uri < end && HTTP_IS_SPHT(*uri)) {
3105 uri++; nspaces++;
3106 }
3107
3108 // look for the next whitespace character
3109 while (uri < end && !HTTP_IS_SPHT(*uri))
3110 uri++;
3111
3112 // keep advancing past multiple spaces
3113 while (uri < end && HTTP_IS_SPHT(*uri))
3114 uri++;
3115
Willy Tarreau57bc8912016-04-25 17:09:40 +02003116 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003117 chunk.area = "<BADREQ>";
3118 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003119 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003120 chunk.area = "HTTP/0.9";
3121 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003122 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003123 chunk.area = uri;
3124 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003125 }
3126
Dragan Dosen835b9212016-02-12 13:23:03 +01003127 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003128 if (ret == NULL || *ret != '\0')
3129 goto out;
3130
3131 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003132 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003133 LOGCHAR('"');
3134
3135 last_isspace = 0;
3136 break;
3137
William Lallemand5f232402012-04-05 18:02:55 +02003138 case LOG_FMT_COUNTER: // %rt
3139 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003140 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02003141 if (iret < 0 || iret > dst + maxsize - tmplog)
3142 goto out;
3143 last_isspace = 0;
3144 tmplog += iret;
3145 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003146 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003147 if (ret == NULL)
3148 goto out;
3149 tmplog = ret;
3150 last_isspace = 0;
3151 }
3152 break;
3153
Willy Tarreau7346acb2014-08-28 15:03:15 +02003154 case LOG_FMT_LOGCNT: // %lc
3155 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003156 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003157 if (iret < 0 || iret > dst + maxsize - tmplog)
3158 goto out;
3159 last_isspace = 0;
3160 tmplog += iret;
3161 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003162 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003163 if (ret == NULL)
3164 goto out;
3165 tmplog = ret;
3166 last_isspace = 0;
3167 }
3168 break;
3169
William Lallemand5f232402012-04-05 18:02:55 +02003170 case LOG_FMT_HOSTNAME: // %H
3171 src = hostname;
3172 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3173 if (ret == NULL)
3174 goto out;
3175 tmplog = ret;
3176 last_isspace = 0;
3177 break;
3178
3179 case LOG_FMT_PID: // %pid
3180 if (tmp->options & LOG_OPT_HEXA) {
3181 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3182 if (iret < 0 || iret > dst + maxsize - tmplog)
3183 goto out;
3184 last_isspace = 0;
3185 tmplog += iret;
3186 } else {
3187 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3188 if (ret == NULL)
3189 goto out;
3190 tmplog = ret;
3191 last_isspace = 0;
3192 }
3193 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003194
3195 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003196 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003197 if (s)
3198 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3199 else
3200 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003201 if (ret == NULL)
3202 goto out;
3203 tmplog = ret;
3204 last_isspace = 0;
3205 break;
3206
William Lallemandbddd4fd2012-02-27 11:23:10 +01003207 }
3208 }
3209
3210out:
William Lallemand1d705562012-03-12 12:46:41 +01003211 /* *tmplog is a unused character */
3212 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003213 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003214
Willy Tarreaubaaee002006-06-26 02:48:02 +02003215}
3216
William Lallemand1d705562012-03-12 12:46:41 +01003217/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003218 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003219 * Will not log if the frontend has no log defined.
3220 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003221void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003222{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003223 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003224 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003225 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003226
3227 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003228 err = (s->flags & SF_REDISP) ||
3229 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3230 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003231 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003232 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003233
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003234 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003235 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003236
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003237 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003238 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003239
Willy Tarreauabcd5142013-06-11 17:18:02 +02003240 if (s->logs.level) { /* loglevel was overridden */
3241 if (s->logs.level == -1) {
3242 s->logs.logwait = 0; /* logs disabled */
3243 return;
3244 }
3245 level = s->logs.level - 1;
3246 }
3247 else {
3248 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003249 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003250 level = LOG_ERR;
3251 }
William Lallemand1d705562012-03-12 12:46:41 +01003252
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003253 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003254 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003255 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003256 }
3257
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003258 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3259 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3260 &sess->fe->logformat_sd);
3261 }
3262
Dragan Dosen59cee972015-09-19 22:09:02 +02003263 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003264 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003265 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003266 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3267 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003268 s->logs.logwait = 0;
3269 }
3270}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003271
Willy Tarreau53839352018-09-05 19:51:10 +02003272/*
3273 * send a minimalist log for the session. Will not log if the frontend has no
3274 * log defined. It is assumed that this is only used to report anomalies that
3275 * cannot lead to the creation of a regular stream. Because of this the log
3276 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3277 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003278 * function to report unimportant events. It is safe to call this function with
3279 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003280 */
3281void sess_log(struct session *sess)
3282{
3283 int size, level;
3284 int sd_size = 0;
3285
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003286 if (!sess)
3287 return;
3288
Willy Tarreau53839352018-09-05 19:51:10 +02003289 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3290 return;
3291
3292 level = LOG_INFO;
3293 if (sess->fe->options2 & PR_O2_LOGERRORS)
3294 level = LOG_ERR;
3295
3296 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3297 sd_size = sess_build_logline(sess, NULL,
3298 logline_rfc5424, global.max_syslog_len,
3299 &sess->fe->logformat_sd);
3300 }
3301
3302 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3303 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003304 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003305 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3306 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003307 }
3308}
3309
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003310void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3311{
3312 va_list argp;
3313 int data_len;
3314
3315 if (level < 0 || format == NULL || logline == NULL)
3316 return;
3317
3318 va_start(argp, format);
3319 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3320 if (data_len < 0 || data_len > global.max_syslog_len)
3321 data_len = global.max_syslog_len;
3322 va_end(argp);
3323
3324 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3325}
Emeric Brun54932b42020-07-07 09:43:24 +02003326/*
3327 * This function parse a received log message <buf>, of size <buflen>
3328 * it fills <level>, <facility> and <metadata> depending of the detected
3329 * header format and message will point on remaining payload of <size>
3330 *
3331 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3332 * struct ist len will be set to 0 if field is not found
3333 * <level> and <facility> will be set to -1 if not found.
3334 */
3335void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3336 struct ist *metadata, char **message, size_t *size)
3337{
3338
3339 char *p;
3340 int fac_level = 0;
3341
3342 *level = *facility = -1;
3343
3344 *message = buf;
3345 *size = buflen;
3346
3347 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3348
3349 p = buf;
3350 if (*size < 2 || *p != '<')
3351 return;
3352
3353 p++;
3354 while (*p != '>') {
3355 if (*p > '9' || *p < '0')
3356 return;
3357 fac_level = 10*fac_level + (*p - '0');
3358 p++;
3359 if ((p - buf) > buflen)
3360 return;
3361 }
3362
3363 *facility = fac_level >> 3;
3364 *level = fac_level & 0x7;
3365 p++;
3366
3367 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3368
3369 buflen -= p - buf;
3370 buf = p;
3371
3372 *size = buflen;
3373 *message = buf;
3374
3375 /* for rfc5424, prio is always followed by '1' and ' ' */
3376 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3377 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3378 * followed by message.
3379 * Each header field can present NILVALUE: '-'
3380 */
3381
3382 p += 2;
3383 /* timestamp is NILVALUE '-' */
3384 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3385 metadata[LOG_META_TIME] = ist2(p, 1);
3386 p++;
3387 }
3388 else if (*size > LOG_ISOTIME_MINLEN) {
3389 metadata[LOG_META_TIME].ptr = p;
3390
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003391 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003392 * in timestamp.
3393 * possible format are:
3394 * ex: '1970-01-01T00:00:00.000000Z'
3395 * '1970-01-01T00:00:00.000000+00:00'
3396 * '1970-01-01T00:00:00.000000-00:00'
3397 * '1970-01-01T00:00:00Z'
3398 * '1970-01-01T00:00:00+00:00'
3399 * '1970-01-01T00:00:00-00:00'
3400 */
3401 p += 19;
3402 if (*p == '.') {
3403 p++;
3404 if ((p - buf) >= buflen)
3405 goto bad_format;
3406 while (*p != 'Z' && *p != '+' && *p != '-') {
3407 if ((unsigned char)(*p - '0') > 9)
3408 goto bad_format;
3409
3410 p++;
3411 if ((p - buf) >= buflen)
3412 goto bad_format;
3413 }
3414 }
3415
3416 if (*p == 'Z')
3417 p++;
3418 else
3419 p += 6; /* case of '+00:00 or '-00:00' */
3420
3421 if ((p - buf) >= buflen || *p != ' ')
3422 goto bad_format;
3423 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3424 }
3425 else
3426 goto bad_format;
3427
3428
3429 p++;
3430 if ((p - buf) >= buflen || *p == ' ')
3431 goto bad_format;
3432
3433 metadata[LOG_META_HOST].ptr = p;
3434 while (*p != ' ') {
3435 p++;
3436 if ((p - buf) >= buflen)
3437 goto bad_format;
3438 }
3439 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3440 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3441 metadata[LOG_META_HOST].len = 0;
3442
3443 p++;
3444 if ((p - buf) >= buflen || *p == ' ')
3445 goto bad_format;
3446
3447 metadata[LOG_META_TAG].ptr = p;
3448 while (*p != ' ') {
3449 p++;
3450 if ((p - buf) >= buflen)
3451 goto bad_format;
3452 }
3453 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3454 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3455 metadata[LOG_META_TAG].len = 0;
3456
3457 p++;
3458 if ((p - buf) >= buflen || *p == ' ')
3459 goto bad_format;
3460
3461 metadata[LOG_META_PID].ptr = p;
3462 while (*p != ' ') {
3463 p++;
3464 if ((p - buf) >= buflen)
3465 goto bad_format;
3466 }
3467 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3468 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3469 metadata[LOG_META_PID].len = 0;
3470
3471 p++;
3472 if ((p - buf) >= buflen || *p == ' ')
3473 goto bad_format;
3474
3475 metadata[LOG_META_MSGID].ptr = p;
3476 while (*p != ' ') {
3477 p++;
3478 if ((p - buf) >= buflen)
3479 goto bad_format;
3480 }
3481 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3482 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3483 metadata[LOG_META_MSGID].len = 0;
3484
3485 p++;
3486 if ((p - buf) >= buflen || *p == ' ')
3487 goto bad_format;
3488
3489 /* structured data format is:
3490 * ex:
3491 * '[key1=value1 key2=value2][key3=value3]'
3492 *
3493 * space is invalid outside [] because
3494 * considered as the end of structured data field
3495 */
3496 metadata[LOG_META_STDATA].ptr = p;
3497 if (*p == '[') {
3498 int elem = 0;
3499
3500 while (1) {
3501 if (elem) {
3502 /* according to rfc this char is escaped in param values */
3503 if (*p == ']' && *(p-1) != '\\')
3504 elem = 0;
3505 }
3506 else {
3507 if (*p == '[')
3508 elem = 1;
3509 else if (*p == ' ')
3510 break;
3511 else
3512 goto bad_format;
3513 }
3514 p++;
3515 if ((p - buf) >= buflen)
3516 goto bad_format;
3517 }
3518 }
3519 else if (*p == '-') {
3520 /* case of NILVALUE */
3521 p++;
3522 if ((p - buf) >= buflen || *p != ' ')
3523 goto bad_format;
3524 }
3525 else
3526 goto bad_format;
3527
3528 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3529 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3530 metadata[LOG_META_STDATA].len = 0;
3531
3532 p++;
3533
3534 buflen -= p - buf;
3535 buf = p;
3536
3537 *size = buflen;
3538 *message = p;
3539 }
3540 else if (*size > LOG_LEGACYTIME_LEN) {
3541 int m;
3542
3543 /* supported header format according to rfc3164.
3544 * ex:
3545 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3546 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3547 * or 'Jan 1 00:00:00 HOSTNAME '
3548 * Note: HOSTNAME is mandatory, and day
3549 * of month uses a single space prefix if
3550 * less than 10 to ensure hour offset is
3551 * always the same.
3552 */
3553
3554 /* Check month to see if it correspond to a rfc3164
3555 * header ex 'Jan 1 00:00:00' */
3556 for (m = 0; m < 12; m++)
3557 if (!memcmp(monthname[m], p, 3))
3558 break;
3559 /* Month not found */
3560 if (m == 12)
3561 goto bad_format;
3562
3563 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3564
3565 p += LOG_LEGACYTIME_LEN;
3566 if ((p - buf) >= buflen || *p != ' ')
3567 goto bad_format;
3568
3569 p++;
3570 if ((p - buf) >= buflen || *p == ' ')
3571 goto bad_format;
3572
3573 metadata[LOG_META_HOST].ptr = p;
3574 while (*p != ' ') {
3575 p++;
3576 if ((p - buf) >= buflen)
3577 goto bad_format;
3578 }
3579 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3580
3581 /* TAG seems to no be mandatory */
3582 p++;
3583
3584 buflen -= p - buf;
3585 buf = p;
3586
3587 *size = buflen;
3588 *message = buf;
3589
3590 if (!buflen)
3591 return;
3592
3593 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3594 p++;
3595
3596 /* a tag must present a trailing ':' */
3597 if (((p - buf) >= buflen) || *p != ':')
3598 return;
3599 p++;
3600 /* followed by a space */
3601 if (((p - buf) >= buflen) || *p != ' ')
3602 return;
3603
3604 /* rewind to parse tag and pid */
3605 p = buf;
3606 metadata[LOG_META_TAG].ptr = p;
3607 /* we have the guarantee that ':' will be reach before size limit */
3608 while (*p != ':') {
3609 if (*p == '[') {
3610 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3611 metadata[LOG_META_PID].ptr = p + 1;
3612 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003613 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003614 if (p[1] != ':')
3615 return;
3616 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3617 }
3618 p++;
3619 }
3620 if (!metadata[LOG_META_TAG].len)
3621 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3622
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003623 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003624 p += 2;
3625
3626 buflen -= p - buf;
3627 buf = p;
3628
3629 *size = buflen;
3630 *message = buf;
3631 }
3632
3633 return;
3634
3635bad_format:
3636 /* bad syslog format, we reset all parsed syslog fields
3637 * but priority is kept because we are able to re-build
3638 * this message using LOF_FORMAT_PRIO.
3639 */
3640 metadata[LOG_META_TIME].len = 0;
3641 metadata[LOG_META_HOST].len = 0;
3642 metadata[LOG_META_TAG].len = 0;
3643 metadata[LOG_META_PID].len = 0;
3644 metadata[LOG_META_MSGID].len = 0;
3645 metadata[LOG_META_STDATA].len = 0;
3646
3647 return;
3648}
3649
3650/*
3651 * UDP syslog fd handler
3652 */
3653void syslog_fd_handler(int fd)
3654{
3655 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3656 ssize_t ret = 0;
3657 struct buffer *buf = get_trash_chunk();
3658 size_t size;
3659 char *message;
3660 int level;
3661 int facility;
3662 struct listener *l = objt_listener(fdtab[fd].owner);
3663 int max_accept;
3664
3665 if(!l)
3666 ABORT_NOW();
3667
3668 if (fdtab[fd].ev & FD_POLL_IN) {
3669
3670 if (!fd_recv_ready(fd))
3671 return;
3672
3673 max_accept = l->maxaccept ? l->maxaccept : 1;
3674
3675 do {
3676 /* Source address */
3677 struct sockaddr_storage saddr = {0};
3678 socklen_t saddrlen;
3679
3680 saddrlen = sizeof(saddr);
3681
3682 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3683 if (ret < 0) {
3684 if (errno == EINTR)
3685 continue;
3686 if (errno == EAGAIN)
3687 fd_cant_recv(fd);
3688 goto out;
3689 }
3690 buf->data = ret;
3691
Emeric Brun45c457a2020-07-09 23:23:34 +02003692 /* update counters */
3693 _HA_ATOMIC_ADD(&cum_log_messages, 1);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003694 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003695
Emeric Brun54932b42020-07-07 09:43:24 +02003696 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3697
3698 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3699
3700 } while (--max_accept);
3701 }
3702
3703out:
3704 return;
3705}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003706
Emeric Brun12941c82020-07-07 14:19:42 +02003707/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003708 * IO Handler to handle message exchange with a syslog tcp client
3709 */
3710static void syslog_io_handler(struct appctx *appctx)
3711{
3712 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3713 struct stream_interface *si = appctx->owner;
3714 struct stream *s = si_strm(si);
3715 struct proxy *frontend = strm_fe(s);
3716 struct listener *l = strm_li(s);
3717 struct buffer *buf = get_trash_chunk();
3718 int max_accept;
3719 int to_skip;
3720 int facility;
3721 int level;
3722 char *message;
3723 size_t size;
3724
3725 max_accept = l->maxaccept ? l->maxaccept : 1;
3726 while (co_data(si_oc(si))) {
3727 char c;
3728
3729 if (max_accept <= 0)
3730 goto missing_budget;
3731 max_accept--;
3732
3733 to_skip = co_getchar(si_oc(si), &c);
3734 if (!to_skip)
3735 goto missing_data;
3736 else if (to_skip < 0)
3737 goto cli_abort;
3738
3739 if (c == '<') {
3740 /* rfc-6587, Non-Transparent-Framing: messages separated by
3741 * a trailing LF or CR LF
3742 */
3743 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3744 if (!to_skip)
3745 goto missing_data;
3746 else if (to_skip < 0)
3747 goto cli_abort;
3748
3749 if (buf->area[to_skip - 1] != '\n')
3750 goto parse_error;
3751
3752 buf->data = to_skip - 1;
3753
3754 /* according to rfc-6587, some devices adds CR before LF */
3755 if (buf->data && buf->area[buf->data - 1] == '\r')
3756 buf->data--;
3757
3758 }
3759 else if ((unsigned char)(c - '1') <= 8) {
3760 /* rfc-6587, Octet-Counting: message length in ASCII
3761 * (first digit can not be ZERO), followed by a space
3762 * and message length
3763 */
3764 char *p = NULL;
3765 int msglen;
3766
3767 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3768 if (!to_skip)
3769 goto missing_data;
3770 else if (to_skip < 0)
3771 goto cli_abort;
3772
3773 if (buf->area[to_skip - 1] != ' ')
3774 goto parse_error;
3775
3776 msglen = strtol(trash.area, &p, 10);
3777 if (!msglen || p != &buf->area[to_skip - 1])
3778 goto parse_error;
3779
3780 /* message seems too large */
3781 if (msglen > buf->size)
3782 goto parse_error;
3783
3784 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3785 if (!msglen)
3786 goto missing_data;
3787 else if (msglen < 0)
3788 goto cli_abort;
3789
3790
3791 buf->data = msglen;
3792 to_skip += msglen;
3793 }
3794 else
3795 goto parse_error;
3796
3797 co_skip(si_oc(si), to_skip);
3798
3799 /* update counters */
3800 _HA_ATOMIC_ADD(&cum_log_messages, 1);
3801 proxy_inc_fe_req_ctr(l, frontend);
3802
3803 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3804
3805 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3806
3807 }
3808
3809missing_data:
3810 /* we need more data to read */
3811 si_oc(si)->flags |= CF_READ_DONTWAIT;
3812
3813 return;
3814
3815missing_budget:
3816 /* it may remain some stuff to do, let's retry later */
3817 appctx_wakeup(appctx);
3818
3819 return;
3820
3821parse_error:
3822 if (l->counters)
3823 _HA_ATOMIC_ADD(&l->counters->failed_req, 1);
3824 _HA_ATOMIC_ADD(&frontend->fe_counters.failed_req, 1);
3825
3826 goto close;
3827
3828cli_abort:
3829 if (l->counters)
3830 _HA_ATOMIC_ADD(&l->counters->cli_aborts, 1);
3831 _HA_ATOMIC_ADD(&frontend->fe_counters.cli_aborts, 1);
3832
3833close:
3834 si_shutw(si);
3835 si_shutr(si);
3836
3837 si_ic(si)->flags |= CF_READ_NULL;
3838
3839 return;
3840}
3841
3842static struct applet syslog_applet = {
3843 .obj_type = OBJ_TYPE_APPLET,
3844 .name = "<SYSLOG>", /* used for logging */
3845 .fct = syslog_io_handler,
3846 .release = NULL,
3847};
3848
3849/*
Emeric Brun12941c82020-07-07 14:19:42 +02003850 * Parse "log-forward" section and create corresponding sink buffer.
3851 *
3852 * The function returns 0 in success case, otherwise, it returns error
3853 * flags.
3854 */
3855int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3856{
3857 int err_code = 0;
3858 struct proxy *px;
3859 char *errmsg = NULL;
3860 const char *err = NULL;
3861
3862 if (strcmp(args[0], "log-forward") == 0) {
3863 if (!*args[1]) {
3864 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3865 err_code |= ERR_ALERT | ERR_ABORT;
3866 goto out;
3867 }
3868
3869 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3870 goto out;
3871
3872 err = invalid_char(args[1]);
3873 if (err) {
3874 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3875 file, linenum, *err, args[0], args[1]);
3876 err_code |= ERR_ALERT | ERR_ABORT;
3877 goto out;
3878 }
3879
Emeric Brunb0c331f2020-10-07 17:05:59 +02003880 px = log_forward_by_name(args[1]);
3881 if (px) {
3882 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3883 file, linenum, args[1], px->conf.file, px->conf.line);
3884 err_code |= ERR_ALERT | ERR_FATAL;
3885 }
3886
3887 px = proxy_find_by_name(args[1], 0, 0);
3888 if (px) {
3889 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3890 file, linenum, args[1], proxy_type_str(px),
3891 px->id, px->conf.file, px->conf.line);
3892 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003893 }
3894
3895 px = calloc(1, sizeof *px);
3896 if (!px) {
3897 err_code |= ERR_ALERT | ERR_FATAL;
3898 goto out;
3899 }
3900
3901 px->next = cfg_log_forward;
3902 cfg_log_forward = px;
3903
3904 init_new_proxy(px);
3905 px->conf.file = strdup(file);
3906 px->conf.line = linenum;
3907 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003908 px->last_change = now.tv_sec;
3909 px->cap = PR_CAP_FE;
3910 px->maxconn = 10;
3911 px->timeout.client = TICK_ETERNITY;
3912 px->accept = frontend_accept;
3913 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003914 px->id = strdup(args[1]);
3915
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003916 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003917 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003918 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3919 err_code |= ERR_WARN;
3920
3921 if (*(args[1]) == 0) {
3922 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3923 err_code |= ERR_ALERT | ERR_FATAL;
3924 goto out;
3925 }
3926 cfg_log_forward->maxconn = atol(args[1]);
3927 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3928 goto out;
3929 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003930 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003931 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3932 err_code |= ERR_WARN;
3933
3934 if (*(args[1]) == 0) {
3935 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3936 err_code |= ERR_ALERT | ERR_FATAL;
3937 goto out;
3938 }
3939 cfg_log_forward->backlog = atol(args[1]);
3940 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3941 goto out;
3942 }
3943 else if (strcmp(args[0], "bind") == 0) {
3944 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003945 struct bind_conf *bind_conf;
3946 struct bind_kw *kw;
3947 struct listener *l;
3948
3949 cur_arg = 1;
3950
3951 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3952 NULL, xprt_get(XPRT_RAW));
3953 if (!bind_conf) {
3954 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3955 err_code |= ERR_ALERT | ERR_FATAL;
3956 goto out;
3957 }
3958
3959 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3960 if (errmsg && *errmsg) {
3961 indent_msg(&errmsg, 2);
3962 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3963 }
3964 else {
3965 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3966 file, linenum, args[0], args[1], args[2]);
3967 err_code |= ERR_ALERT | ERR_FATAL;
3968 goto out;
3969 }
3970 }
3971 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau66161322021-02-19 15:50:27 +01003972 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003973 l->accept = session_accept_fd;
3974 l->analysers |= cfg_log_forward->fe_req_ana;
3975 l->default_target = cfg_log_forward->default_target;
3976 global.maxsock++;
3977 }
3978 cur_arg++;
3979
3980 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3981 int ret;
3982
3983 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3984 err_code |= ret;
3985 if (ret) {
3986 if (errmsg && *errmsg) {
3987 indent_msg(&errmsg, 2);
3988 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3989 }
3990 else
3991 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3992 file, linenum, args[cur_arg]);
3993 if (ret & ERR_FATAL)
3994 goto out;
3995 }
3996 cur_arg += 1 + kw->skip;
3997 }
3998 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003999 const char *best = bind_find_best_kw(args[cur_arg]);
4000 if (best)
4001 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
4002 file, linenum, args[cur_arg], cursection, best);
4003 else
4004 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
4005 file, linenum, args[cur_arg], cursection);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004006 err_code |= ERR_ALERT | ERR_FATAL;
4007 goto out;
4008 }
Emeric Brun12941c82020-07-07 14:19:42 +02004009 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02004010 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02004011 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02004012 struct bind_conf *bind_conf;
4013 struct bind_kw *kw;
4014 struct listener *l;
4015
4016 cur_arg = 1;
4017
4018 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
4019 NULL, xprt_get(XPRT_RAW));
4020
Willy Tarreau26ff5da2020-09-16 15:22:19 +02004021 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02004022 if (errmsg && *errmsg) {
4023 indent_msg(&errmsg, 2);
4024 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
4025 }
4026 else {
4027 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
4028 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02004029 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02004030 err_code |= ERR_ALERT | ERR_FATAL;
4031 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02004032 }
4033 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02004034 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreau66161322021-02-19 15:50:27 +01004035 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreaue140a692020-10-15 21:25:32 +02004036 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02004037 global.maxsock++;
4038 }
4039 cur_arg++;
4040
4041 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
4042 int ret;
4043
4044 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
4045 err_code |= ret;
4046 if (ret) {
4047 if (errmsg && *errmsg) {
4048 indent_msg(&errmsg, 2);
4049 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
4050 }
4051 else
4052 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
4053 file, linenum, args[cur_arg]);
4054 if (ret & ERR_FATAL)
4055 goto out;
4056 }
4057 cur_arg += 1 + kw->skip;
4058 }
4059 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01004060 const char *best = bind_find_best_kw(args[cur_arg]);
4061 if (best)
4062 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
4063 file, linenum, args[cur_arg], cursection, best);
4064 else
4065 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
4066 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02004067 err_code |= ERR_ALERT | ERR_FATAL;
4068 goto out;
4069 }
4070 }
4071 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02004072 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02004073 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4074 err_code |= ERR_ALERT | ERR_FATAL;
4075 goto out;
4076 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004077 }
4078 else if (strcmp(args[0], "timeout") == 0) {
4079 const char *res;
4080 unsigned timeout;
4081
4082 if (strcmp(args[1], "client") != 0) {
4083 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
4084 err_code |= ERR_ALERT | ERR_FATAL;
4085 goto out;
4086 }
4087
4088 if (*args[2] == 0) {
4089 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
4090 err_code |= ERR_ALERT | ERR_FATAL;
4091 goto out;
4092 }
4093 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
4094 if (res == PARSE_TIME_OVER) {
4095 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
4096 }
4097 else if (res == PARSE_TIME_UNDER) {
4098 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
4099 }
4100 else if (res) {
4101 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
4102 return -1;
4103 }
4104
4105 if (res) {
4106 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4107 err_code |= ERR_ALERT | ERR_FATAL;
4108 goto out;
4109 }
4110 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02004111 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02004112 else {
4113 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
4114 err_code |= ERR_ALERT | ERR_ABORT;
4115 goto out;
4116 }
Emeric Brun12941c82020-07-07 14:19:42 +02004117out:
4118 return err_code;
4119}
4120
4121
Willy Tarreau869efd52019-11-15 15:16:57 +01004122/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
4123static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004124{
Willy Tarreau869efd52019-11-15 15:16:57 +01004125 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4126 return 1;
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004127
Willy Tarreau869efd52019-11-15 15:16:57 +01004128 if (!startup_logs)
4129 return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
4130
4131 return ring_attach_cli(startup_logs, appctx);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004132}
4133
4134/* register cli keywords */
4135static struct cli_kw_list cli_kws = {{ },{
4136 { { "show", "startup-logs", NULL },
Willy Tarreau869efd52019-11-15 15:16:57 +01004137 "show startup-logs : report logs emitted during HAProxy startup", cli_parse_show_startup_logs, NULL, NULL },
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004138 {{},}
4139}};
4140
Willy Tarreau0108d902018-11-25 19:14:37 +01004141INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4142
Emeric Brun12941c82020-07-07 14:19:42 +02004143/* config parsers for this section */
4144REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4145
Willy Tarreau082b6282019-05-22 14:42:12 +02004146REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4147REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004148
Willy Tarreaubaaee002006-06-26 02:48:02 +02004149/*
4150 * Local variables:
4151 * c-indent-level: 8
4152 * c-basic-offset: 8
4153 * End:
4154 */