blob: 491b149f66acb013b7b888d5d365ca45891bf684 [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] = {
Dragan Dosen43885c72015-10-01 13:18:13 +020059 [LOG_FORMAT_RFC3164] = {
60 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020061 },
62 [LOG_FORMAT_RFC5424] = {
63 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020064 },
65 [LOG_FORMAT_PRIO] = {
66 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010067 },
68 [LOG_FORMAT_SHORT] = {
69 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020070 },
71 [LOG_FORMAT_TIMED] = {
72 .name = "timed",
73 },
74 [LOG_FORMAT_ISO] = {
75 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010076 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010077 [LOG_FORMAT_RAW] = {
78 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010079 },
Dragan Dosen1322d092015-09-22 16:05:32 +020080};
81
Dragan Dosen835b9212016-02-12 13:23:03 +010082/*
83 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020084 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
85 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
86 * that the byte should be escaped. Be careful to always pass bytes from 0 to
87 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010088 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020089long rfc5424_escape_map[(256/8) / sizeof(long)];
90long hdr_encode_map[(256/8) / sizeof(long)];
91long url_encode_map[(256/8) / sizeof(long)];
92long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010093
Dragan Dosen835b9212016-02-12 13:23:03 +010094
Willy Tarreaubaaee002006-06-26 02:48:02 +020095const char *log_facilities[NB_LOG_FACILITIES] = {
96 "kern", "user", "mail", "daemon",
97 "auth", "syslog", "lpr", "news",
98 "uucp", "cron", "auth2", "ftp",
99 "ntp", "audit", "alert", "cron2",
100 "local0", "local1", "local2", "local3",
101 "local4", "local5", "local6", "local7"
102};
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104const char *log_levels[NB_LOG_LEVELS] = {
105 "emerg", "alert", "crit", "err",
106 "warning", "notice", "info", "debug"
107};
108
Willy Tarreau570f2212013-06-10 16:42:09 +0200109const 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 +0200110const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111
William Lallemand723b73a2012-02-08 16:37:49 +0100112
113/* log_format */
114struct logformat_type {
115 char *name;
116 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100117 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200118 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100119 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100120 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100121};
122
William Lallemandb7ff6a32012-03-02 14:35:21 +0100123int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
124
William Lallemand723b73a2012-02-08 16:37:49 +0100125/* log_format variable names */
126static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200127 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100128
129 /* please keep these lines sorted ! */
130 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
131 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
132 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
133 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100134 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200135 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200136 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200137 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100138 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200139 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
140 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
141 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
142 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
143 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
144 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200145 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100146 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200147 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000148 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100149 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
150 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200151 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100152 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200153 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100154 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
155 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200156 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200157 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
158 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100159 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
160 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200161 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
162 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100163 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200164 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
165 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
166 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
167 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000168 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
169 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000170 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000171 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
172 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200173 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100174 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200175 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100176 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
177 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100178 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100179 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
180 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
181 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
182 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
183 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200184 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
185 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100186 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200187 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
188 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
189 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100190 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
191 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
192
193 /* The following tags are deprecated and will be removed soon */
194 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
195 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200196 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
197 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
198 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
199 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100200 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
201 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
202 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
203 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
204 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200205 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100206};
207
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200208char 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
209char 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 +0100210char 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 +0100211char *log_format = NULL;
212
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200213/* Default string used for structured-data part in RFC5424 formatted
214 * syslog messages.
215 */
216char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200217
Willy Tarreau13ef7732018-11-12 07:25:28 +0100218/* total number of dropped logs */
219unsigned int dropped_logs = 0;
220
Dragan Dosen59cee972015-09-19 22:09:02 +0200221/* This is a global syslog message buffer, common to all outgoing
222 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100223 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200224THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100225
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200226/* A global syslog message buffer, common to all RFC5424 syslog messages.
227 * Currently, it is used for generating the structured-data part.
228 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200229THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200230
Christopher Fauletd4696382017-10-24 11:44:05 +0200231/* A global buffer used to store all startup alerts/warnings. It will then be
232 * retrieve on the CLI. */
Willy Tarreau869efd52019-11-15 15:16:57 +0100233static struct ring *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200234
William Lallemand723b73a2012-02-08 16:37:49 +0100235struct logformat_var_args {
236 char *name;
237 int mask;
238};
239
240struct logformat_var_args var_args_list[] = {
241// global
242 { "M", LOG_OPT_MANDATORY },
243 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200244 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100245 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100246 { 0, 0 }
247};
248
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200249/* return the name of the directive used in the current proxy for which we're
250 * currently parsing a header, when it is known.
251 */
252static inline const char *fmt_directive(const struct proxy *curproxy)
253{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100254 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200255 case ARGC_ACL:
256 return "acl";
257 case ARGC_STK:
258 return "stick";
259 case ARGC_TRK:
260 return "track-sc";
261 case ARGC_LOG:
262 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200263 case ARGC_LOGSD:
264 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100265 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100266 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100267 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100268 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200269 case ARGC_UIF:
270 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100271 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200272 return "redirect";
273 case ARGC_CAP:
274 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200275 case ARGC_SRV:
276 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200277 case ARGC_SPOE:
278 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100279 case ARGC_UBK:
280 return "use_backend";
Christopher Faulet3b967c12020-05-15 15:47:44 +0200281 case ARGC_HERR:
282 return "http-error";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100283 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200284 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100285 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200286}
287
William Lallemand723b73a2012-02-08 16:37:49 +0100288/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100289 * callback used to configure addr source retrieval
290 */
291int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
292{
293 curproxy->options2 |= PR_O2_SRC_ADDR;
294
295 return 0;
296}
297
298
299/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100300 * Parse args in a logformat_var. Returns 0 in error
301 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100302 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100303int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100304{
305 int i = 0;
306 int end = 0;
307 int flags = 0; // 1 = + 2 = -
308 char *sp = NULL; // start pointer
309
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100310 if (args == NULL) {
311 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100312 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100313 }
William Lallemand723b73a2012-02-08 16:37:49 +0100314
315 while (1) {
316 if (*args == '\0')
317 end = 1;
318
319 if (*args == '+') {
320 // add flag
321 sp = args + 1;
322 flags = 1;
323 }
324 if (*args == '-') {
325 // delete flag
326 sp = args + 1;
327 flags = 2;
328 }
329
330 if (*args == '\0' || *args == ',') {
331 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100332 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100333 if (strcmp(sp, var_args_list[i].name) == 0) {
334 if (flags == 1) {
335 node->options |= var_args_list[i].mask;
336 break;
337 } else if (flags == 2) {
338 node->options &= ~var_args_list[i].mask;
339 break;
340 }
341 }
342 }
343 sp = NULL;
344 if (end)
345 break;
346 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100347 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100348 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100349 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100350}
351
352/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100353 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
354 * must pass the args part in the <arg> pointer with its length in <arg_len>,
355 * and varname with its length in <var> and <var_len> respectively. <arg> is
356 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100357 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100358 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100359int 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 +0100360{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100361 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200362 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100363
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100364 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
365 if (strlen(logformat_keywords[j].name) == var_len &&
366 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
367 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200368 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100369 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100370 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200371 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100372 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100373 node->type = logformat_keywords[j].type;
374 node->options = *defoptions;
375 if (arg_len) {
376 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100377 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200378 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100379 }
380 if (node->type == LOG_FMT_GLOBAL) {
381 *defoptions = node->options;
382 free(node->arg);
383 free(node);
384 } else {
385 if (logformat_keywords[j].config_callback &&
386 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200387 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100388 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100389 curproxy->to_log |= logformat_keywords[j].lw;
390 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100391 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100392 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100393 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
394 curproxy->conf.args.file, curproxy->conf.args.line,
395 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100396 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100397 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100398 memprintf(err, "format variable '%s' is reserved for HTTP mode",
399 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200400 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100401 }
William Lallemand723b73a2012-02-08 16:37:49 +0100402 }
403 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100404
405 j = var[var_len];
406 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100407 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 +0100408 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200409
410 error_free:
411 if (node) {
412 free(node->arg);
413 free(node);
414 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100415 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100416}
417
418/*
419 * push to the logformat linked list
420 *
421 * start: start pointer
422 * end: end text pointer
423 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100424 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100425 *
426 * LOG_TEXT: copy chars from start to end excluding end.
427 *
428*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100429int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100430{
431 char *str;
432
Willy Tarreaua3571662012-12-20 21:59:12 +0100433 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200434 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100435 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100436 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100437 return 0;
438 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200439 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100440 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100441 str[end - start] = '\0';
442 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100443 node->type = LOG_FMT_TEXT; // type string
444 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100445 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200446 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100447 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100448 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100449 return 0;
450 }
William Lallemand1d705562012-03-12 12:46:41 +0100451 node->type = LOG_FMT_SEPARATOR;
452 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100453 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100454 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100455}
456
457/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100458 * Parse the sample fetch expression <text> and add a node to <list_format> upon
459 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100460 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
461 * is passed in <endptr>, it will be updated with the pointer to the first character
462 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100463 *
464 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100465 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100466int 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 +0100467{
468 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200469 struct sample_expr *expr = NULL;
470 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100471 int cmd_arg;
472
473 cmd[0] = text;
474 cmd[1] = "";
475 cmd_arg = 0;
476
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100477 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 +0100478 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100479 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200480 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100481 }
482
Vincent Bernat02779b62016-04-03 13:48:43 +0200483 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100484 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100485 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200486 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100487 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100488 node->type = LOG_FMT_EXPR;
489 node->expr = expr;
490 node->options = options;
491
492 if (arg_len) {
493 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100494 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200495 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100496 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100497 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100498 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
499
Willy Tarreau434c57c2013-01-08 01:10:24 +0100500 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100501 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
502
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100503 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100504 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
505 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200506 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100507 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100508
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200509 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100510 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100511 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100512
William Lallemand65ad6e12014-01-31 15:08:02 +0100513 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
514 * needed with some sample fetches (eg: ssl*). We always set it for
515 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100516 */
517 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200518 if (curpx->http_needed)
519 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100520 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100521 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200522
523 error_free:
524 release_sample_expr(expr);
525 if (node) {
526 free(node->arg);
527 free(node);
528 }
529 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100530}
531
532/*
William Lallemand723b73a2012-02-08 16:37:49 +0100533 * Parse the log_format string and fill a linked list.
534 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200535 * You can set arguments using { } : %{many arguments}varname.
536 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100537 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500538 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100539 * curproxy: the proxy affected
540 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100541 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100542 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100543 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100544 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100545 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100546int 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 +0100547{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100548 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100549 char *arg = NULL; /* start pointer for args */
550 char *var = NULL; /* start pointer for vars */
551 int arg_len = 0;
552 int var_len = 0;
553 int cformat; /* current token format */
554 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100555 struct logformat_node *tmplf, *back;
556
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100557 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100558 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100559 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100560 return 0;
561 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200562 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200563
William Lallemand723b73a2012-02-08 16:37:49 +0100564 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100565 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100566 LIST_DEL(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200567 release_sample_expr(tmplf->expr);
568 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100569 free(tmplf);
570 }
571
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100572 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100573 pformat = cformat;
574
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100575 if (!*str)
576 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100577
Joseph Herlant85b40592018-11-15 12:10:04 -0800578 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100579 * second have all common paths processed at one place. The common paths are the ones
580 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
581 * We use the common LF_INIT state to dispatch to the different final states.
582 */
583 switch (pformat) {
584 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100585 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100586 arg_len = var_len = 0;
587 if (*str == '{') { // optional argument
588 cformat = LF_STARG;
589 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100590 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100591 else if (*str == '[') {
592 cformat = LF_STEXPR;
593 var = str + 1; // store expr in variable name
594 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100595 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100596 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100597 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100598 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100599 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500600 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100601 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100602 /* single '%' followed by blank or digit, send them both */
603 cformat = LF_TEXT;
604 pformat = LF_TEXT; /* finally we include the previous char as well */
605 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600606 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 +0100607 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100608 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100609
610 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100611 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500612 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100613 break;
614
615 case LF_STARG: // text immediately following '%{'
616 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100617 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100618 arg_len = str - arg;
619 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100620 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100621 break;
622
623 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100624 if (*str == '[') {
625 cformat = LF_STEXPR;
626 var = str + 1; // store expr in variable name
627 break;
628 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100629 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100630 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100631 var = str;
632 break;
633 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100634 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100635 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100636
Willy Tarreauc8368452012-12-21 00:09:23 +0100637 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100638 /* the whole sample expression is parsed at once,
639 * returning the pointer to the first character not
640 * part of the expression, which MUST be the trailing
641 * angle bracket.
642 */
643 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
644 goto fail;
645
646 if (*str == ']') {
647 // end of arg, go on with next state
648 cformat = pformat = LF_EDEXPR;
649 sp = str;
650 }
651 else {
652 char c = *str;
653 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100654 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100655 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
656 else
657 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200658 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100659 }
660 break;
661
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100662 case LF_VAR: // text part of a variable name
663 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100664 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100665 cformat = LF_INIT; // not variable name anymore
666 break;
667
Willy Tarreauc8368452012-12-21 00:09:23 +0100668 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100669 cformat = LF_INIT;
670 }
671
672 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
673 switch (*str) {
674 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100675 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200676 case ' ':
677 if (options & LOG_OPT_MERGE_SPACES) {
678 cformat = LF_SEPARATOR;
679 break;
680 }
681 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100682 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100683 }
684 }
685
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100686 if (cformat != pformat || pformat == LF_SEPARATOR) {
687 switch (pformat) {
688 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100689 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100690 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100691 break;
692 case LF_TEXT:
693 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100694 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100695 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100696 break;
697 }
698 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100699 }
700 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100701
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100702 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100703 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100704 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100705 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100706 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100707
708 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100709 fail:
710 free(backfmt);
711 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100712}
713
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200714/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500715 * 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 +0200716 * ranges of indexes. Note that an index may be considered as a particular range
717 * with a high limit to the low limit.
718 */
719int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
720{
721 char *end, *p;
722
723 *low = *high = 0;
724
725 p = *arg;
726 end = strchr(p, ',');
727 if (!end)
728 end = p + strlen(p);
729
730 *high = *low = read_uint((const char **)&p, end);
731 if (!*low || (p != end && *p != '-'))
732 goto err;
733
734 if (p == end)
735 goto done;
736
737 p++;
738 *high = read_uint((const char **)&p, end);
739 if (!*high || *high <= *low || p != end)
740 goto err;
741
742 done:
743 if (*end == ',')
744 end++;
745 *arg = end;
746 return 1;
747
748 err:
749 memprintf(err, "wrong sample range '%s'", *arg);
750 return 0;
751}
752
753/*
754 * Returns 1 if the range defined by <low> and <high> overlaps
755 * one of them in <rgs> array of ranges with <sz> the size of this
756 * array, 0 if not.
757 */
758int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
759 unsigned int low, unsigned int high, char **err)
760{
761 size_t i;
762
763 for (i = 0; i < sz; i++) {
764 if ((low >= rgs[i].low && low <= rgs[i].high) ||
765 (high >= rgs[i].low && high <= rgs[i].high)) {
766 memprintf(err, "ranges are overlapping");
767 return 1;
768 }
769 }
770
771 return 0;
772}
773
774int smp_log_range_cmp(const void *a, const void *b)
775{
776 const struct smp_log_range *rg_a = a;
777 const struct smp_log_range *rg_b = b;
778
779 if (rg_a->high < rg_b->low)
780 return -1;
781 else if (rg_a->low > rg_b->high)
782 return 1;
783
784 return 0;
785}
786
787/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200788 * Parse "log" keyword and update <logsrvs> list accordingly.
789 *
790 * When <do_del> is set, it means the "no log" line was parsed, so all log
791 * servers in <logsrvs> are released.
792 *
793 * Otherwise, we try to parse the "log" line. First of all, when the list is not
794 * the global one, we look for the parameter "global". If we find it,
795 * global.logsrvs is copied. Else we parse each arguments.
796 *
797 * The function returns 1 in success case, otherwise, it returns 0 and err is
798 * filled.
799 */
800int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
801{
802 struct sockaddr_storage *sk;
803 struct logsrv *logsrv = NULL;
804 int port1, port2;
805 int cur_arg;
806
807 /*
808 * "no log": delete previous herited or defined syslog
809 * servers.
810 */
811 if (do_del) {
812 struct logsrv *back;
813
814 if (*(args[1]) != 0) {
815 memprintf(err, "'no log' does not expect arguments");
816 goto error;
817 }
818
819 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
820 LIST_DEL(&logsrv->list);
821 free(logsrv);
822 }
823 return 1;
824 }
825
826 /*
827 * "log global": copy global.logrsvs linked list to the end of logsrvs
828 * list. But first, we check (logsrvs != global.logsrvs).
829 */
830 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
831 if (logsrvs == &global.logsrvs) {
832 memprintf(err, "'global' is not supported for a global syslog server");
833 goto error;
834 }
835 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200836 struct logsrv *node;
837
838 list_for_each_entry(node, logsrvs, list) {
839 if (node->ref == logsrv)
840 goto skip_logsrv;
841 }
842
843 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200844 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200845 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200846 LIST_INIT(&node->list);
847 LIST_ADDQ(logsrvs, &node->list);
Christopher Faulet28ac0992018-03-26 16:09:19 +0200848
849 skip_logsrv:
850 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200851 }
852 return 1;
853 }
854
855 /*
856 * "log <address> ...: parse a syslog server line
857 */
858 if (*(args[1]) == 0 || *(args[2]) == 0) {
859 memprintf(err, "expects <address> and <facility> %s as arguments",
860 ((logsrvs == &global.logsrvs) ? "" : "or global"));
861 goto error;
862 }
863
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100864 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
865 if (strcmp(args[1], "stdout") == 0)
866 args[1] = "fd@1";
867 else if (strcmp(args[1], "stderr") == 0)
868 args[1] = "fd@2";
869
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200870 logsrv = calloc(1, sizeof(*logsrv));
871 if (!logsrv) {
872 memprintf(err, "out of memory");
873 goto error;
874 }
875
876 /* skip address for now, it will be parsed at the end */
877 cur_arg = 2;
878
879 /* just after the address, a length may be specified */
880 logsrv->maxlen = MAX_SYSLOG_LEN;
881 if (strcmp(args[cur_arg], "len") == 0) {
882 int len = atoi(args[cur_arg+1]);
883 if (len < 80 || len > 65535) {
884 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
885 args[cur_arg+1]);
886 goto error;
887 }
888 logsrv->maxlen = len;
889 cur_arg += 2;
890 }
891 if (logsrv->maxlen > global.max_syslog_len)
892 global.max_syslog_len = logsrv->maxlen;
893
894 /* after the length, a format may be specified */
895 if (strcmp(args[cur_arg], "format") == 0) {
896 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200897 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200898 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
899 goto error;
900 }
901 cur_arg += 2;
902 }
903
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200904 if (strcmp(args[cur_arg], "sample") == 0) {
905 unsigned low, high;
906 char *p, *beg, *end, *smp_sz_str;
907 struct smp_log_range *smp_rgs = NULL;
908 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
909
910 p = args[cur_arg+1];
911 smp_sz_str = strchr(p, ':');
912 if (!smp_sz_str) {
913 memprintf(err, "Missing sample size");
914 goto error;
915 }
916
917 *smp_sz_str++ = '\0';
918
919 end = p + strlen(p);
920
921 while (p != end) {
922 if (!get_logsrv_smp_range(&low, &high, &p, err))
923 goto error;
924
925 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
926 goto error;
927
928 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
929 if (!smp_rgs) {
930 memprintf(err, "out of memory error");
931 goto error;
932 }
933
934 smp_rgs[smp_rgs_sz].low = low;
935 smp_rgs[smp_rgs_sz].high = high;
936 smp_rgs[smp_rgs_sz].sz = high - low + 1;
937 smp_rgs[smp_rgs_sz].curr_idx = 0;
938 if (smp_rgs[smp_rgs_sz].high > smp_sz)
939 smp_sz = smp_rgs[smp_rgs_sz].high;
940 smp_rgs_sz++;
941 }
942
Tim Duesterhus21648002019-06-23 22:10:10 +0200943 if (smp_rgs == NULL) {
944 memprintf(err, "no sampling ranges given");
945 goto error;
946 }
947
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200948 beg = smp_sz_str;
949 end = beg + strlen(beg);
950 new_smp_sz = read_uint((const char **)&beg, end);
951 if (!new_smp_sz || beg != end) {
952 memprintf(err, "wrong sample size '%s' for sample range '%s'",
953 smp_sz_str, args[cur_arg+1]);
954 goto error;
955 }
956
957 if (new_smp_sz < smp_sz) {
958 memprintf(err, "sample size %zu should be greater or equal to "
959 "%zu the maximum of the high ranges limits",
960 new_smp_sz, smp_sz);
961 goto error;
962 }
963 smp_sz = new_smp_sz;
964
965 /* Let's order <smp_rgs> array. */
966 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
967
968 logsrv->lb.smp_rgs = smp_rgs;
969 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
970 logsrv->lb.smp_sz = smp_sz;
971
972 cur_arg += 2;
973 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200974 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200975 /* parse the facility */
976 logsrv->facility = get_log_facility(args[cur_arg]);
977 if (logsrv->facility < 0) {
978 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
979 goto error;
980 }
981 cur_arg++;
982
983 /* parse the max syslog level (default: debug) */
984 logsrv->level = 7;
985 if (*(args[cur_arg])) {
986 logsrv->level = get_log_level(args[cur_arg]);
987 if (logsrv->level < 0) {
988 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
989 goto error;
990 }
991 cur_arg++;
992 }
993
994 /* parse the limit syslog level (default: emerg) */
995 logsrv->minlvl = 0;
996 if (*(args[cur_arg])) {
997 logsrv->minlvl = get_log_level(args[cur_arg]);
998 if (logsrv->minlvl < 0) {
999 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1000 goto error;
1001 }
1002 cur_arg++;
1003 }
1004
1005 /* Too many args */
1006 if (*(args[cur_arg])) {
1007 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1008 goto error;
1009 }
1010
1011 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001012 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001013 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001014 logsrv->addr.ss_family = AF_UNSPEC;
1015 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001016 logsrv->sink = NULL;
1017 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001018 goto done;
1019 }
1020
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001021 if (strncmp(args[1], "fd@", 3) == 0)
1022 logsrv->type = LOG_TARGET_FD;
1023
Willy Tarreau80958762020-09-15 10:30:39 +02001024 sk = str2sa_range(args[1], NULL, &port1, &port2, err, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001025 if (!sk)
1026 goto error;
1027 logsrv->addr = *sk;
1028
Willy Tarreaue1c4c802020-09-16 19:58:34 +02001029 /* handle nicely the case where "udp@" is forced */
1030 if (sk->ss_family == AF_CUST_UDP4)
1031 sk->ss_family = AF_INET;
1032 else if (sk->ss_family == AF_CUST_UDP6)
1033 sk->ss_family = AF_INET6;
1034
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001035 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001036 logsrv->addr = *sk;
1037 if (!port1)
1038 set_host_port(&logsrv->addr, SYSLOG_PORT);
1039 }
Willy Tarreauc046d162019-08-30 15:24:59 +02001040 done:
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001041 LIST_ADDQ(logsrvs, &logsrv->list);
1042 return 1;
1043
1044 error:
1045 free(logsrv);
1046 return 0;
1047}
1048
1049
Christopher Fauletd4696382017-10-24 11:44:05 +02001050/* Generic function to display messages prefixed by a label */
1051static void print_message(const char *label, const char *fmt, va_list argp)
1052{
1053 struct tm tm;
1054 char *head, *msg;
1055
1056 head = msg = NULL;
1057
1058 get_localtime(date.tv_sec, &tm);
1059 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
1060 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
1061 memvprintf(&msg, fmt, argp);
1062
Willy Tarreau869efd52019-11-15 15:16:57 +01001063 if (global.mode & MODE_STARTING) {
1064 if (unlikely(!startup_logs))
1065 startup_logs = ring_new(STARTUP_LOG_SIZE);
1066
1067 if (likely(startup_logs)) {
1068 struct ist m[2];
1069
1070 m[0] = ist(head);
1071 m[1] = ist(msg);
1072 /* trim the trailing '\n' */
1073 if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
1074 m[1].len--;
1075 ring_write(startup_logs, ~0, 0, 0, m, 2);
1076 }
1077 }
Christopher Fauletd4696382017-10-24 11:44:05 +02001078
1079 fprintf(stderr, "%s%s", head, msg);
1080 fflush(stderr);
1081
1082 free(head);
1083 free(msg);
1084}
1085
Willy Tarreaubaaee002006-06-26 02:48:02 +02001086/*
1087 * Displays the message on stderr with the date and pid. Overrides the quiet
1088 * mode during startup.
1089 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001090void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001091{
1092 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001093
1094 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaubb869862020-04-16 10:52:41 +02001095 if (!(warned & WARN_EXEC_PATH)) {
1096 const char *path = get_exec_path();
1097
1098 warned |= WARN_EXEC_PATH;
1099 ha_notice("haproxy version is %s\n", haproxy_version);
1100 if (path)
1101 ha_notice("path to executable is %s\n", path);
1102 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001103 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001104 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001105 va_end(argp);
1106 }
1107}
1108
1109
1110/*
1111 * Displays the message on stderr with the date and pid.
1112 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001113void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001114{
1115 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001116
Willy Tarreaubebd2122020-04-15 16:06:11 +02001117 warned |= WARN_ANY;
1118
Willy Tarreaubaaee002006-06-26 02:48:02 +02001119 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1120 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001121 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001122 va_end(argp);
1123 }
1124}
1125
1126/*
William Lallemand9c56a222018-11-21 18:04:52 +01001127 * Displays the message on stderr with the date and pid.
1128 */
1129void ha_notice(const char *fmt, ...)
1130{
1131 va_list argp;
1132
1133 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1134 va_start(argp, fmt);
1135 print_message("NOTICE", fmt, argp);
1136 va_end(argp);
1137 }
1138}
1139
1140/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001141 * Displays the message on <out> only if quiet mode is not set.
1142 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001143void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001144{
1145 va_list argp;
1146
1147 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1148 va_start(argp, fmt);
1149 vfprintf(out, fmt, argp);
1150 fflush(out);
1151 va_end(argp);
1152 }
1153}
1154
1155/*
Emeric Brun54648852020-07-06 15:54:06 +02001156 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001157 */
Emeric Brun54648852020-07-06 15:54:06 +02001158enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001159{
Emeric Brun54648852020-07-06 15:54:06 +02001160 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001161
1162 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001163 while (format > 0 && log_formats[format].name
1164 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +02001165 format--;
1166
Emeric Brun54648852020-07-06 15:54:06 +02001167 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001168 return format;
1169}
1170
1171/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001172 * returns log level for <lev> or -1 if not found.
1173 */
1174int get_log_level(const char *lev)
1175{
1176 int level;
1177
1178 level = NB_LOG_LEVELS - 1;
1179 while (level >= 0 && strcmp(log_levels[level], lev))
1180 level--;
1181
1182 return level;
1183}
1184
Willy Tarreaubaaee002006-06-26 02:48:02 +02001185/*
1186 * returns log facility for <fac> or -1 if not found.
1187 */
1188int get_log_facility(const char *fac)
1189{
1190 int facility;
1191
1192 facility = NB_LOG_FACILITIES - 1;
1193 while (facility >= 0 && strcmp(log_facilities[facility], fac))
1194 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001195
Willy Tarreaubaaee002006-06-26 02:48:02 +02001196 return facility;
1197}
1198
William Lallemanda1cc3812012-02-08 16:38:44 +01001199/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001200 * Encode the string.
1201 *
1202 * When using the +E log format option, it will try to escape '"\]'
1203 * characters with '\' as prefix. The same prefix should not be used as
1204 * <escape>.
1205 */
1206static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001207 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001208 const char *string,
1209 struct logformat_node *node)
1210{
1211 if (node->options & LOG_OPT_ESC) {
1212 if (start < stop) {
1213 stop--; /* reserve one byte for the final '\0' */
1214 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001215 if (!ha_bit_test((unsigned char)(*string), map)) {
1216 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001217 *start++ = *string;
1218 else {
1219 if (start + 2 >= stop)
1220 break;
1221 *start++ = '\\';
1222 *start++ = *string;
1223 }
1224 }
1225 else {
1226 if (start + 3 >= stop)
1227 break;
1228 *start++ = escape;
1229 *start++ = hextab[(*string >> 4) & 15];
1230 *start++ = hextab[*string & 15];
1231 }
1232 string++;
1233 }
1234 *start = '\0';
1235 }
1236 }
1237 else {
1238 return encode_string(start, stop, escape, map, string);
1239 }
1240
1241 return start;
1242}
1243
1244/*
1245 * Encode the chunk.
1246 *
1247 * When using the +E log format option, it will try to escape '"\]'
1248 * characters with '\' as prefix. The same prefix should not be used as
1249 * <escape>.
1250 */
1251static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001252 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001253 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001254 struct logformat_node *node)
1255{
1256 char *str, *end;
1257
1258 if (node->options & LOG_OPT_ESC) {
1259 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001260 str = chunk->area;
1261 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001262
1263 stop--; /* reserve one byte for the final '\0' */
1264 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001265 if (!ha_bit_test((unsigned char)(*str), map)) {
1266 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001267 *start++ = *str;
1268 else {
1269 if (start + 2 >= stop)
1270 break;
1271 *start++ = '\\';
1272 *start++ = *str;
1273 }
1274 }
1275 else {
1276 if (start + 3 >= stop)
1277 break;
1278 *start++ = escape;
1279 *start++ = hextab[(*str >> 4) & 15];
1280 *start++ = hextab[*str & 15];
1281 }
1282 str++;
1283 }
1284 *start = '\0';
1285 }
1286 }
1287 else {
1288 return encode_chunk(start, stop, escape, map, chunk);
1289 }
1290
1291 return start;
1292}
1293
1294/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001295 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001296 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001297 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001298 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001299 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001300char *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 +01001301{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001302 if (size < 2)
1303 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001304
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001305 if (node->options & LOG_OPT_QUOTE) {
1306 *(dst++) = '"';
1307 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001308 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001309
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001310 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001311 if (++len > size)
1312 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001313 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001314 char *ret;
1315
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001316 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001317 if (ret == NULL || *ret != '\0')
1318 return NULL;
1319 len = ret - dst;
1320 }
1321 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001322 len = strlcpy2(dst, src, len);
1323 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001324
1325 size -= len;
1326 dst += len;
1327 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001328 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1329 if (size < 2)
1330 return NULL;
1331 *(dst++) = '-';
1332 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001333
1334 if (node->options & LOG_OPT_QUOTE) {
1335 if (size < 2)
1336 return NULL;
1337 *(dst++) = '"';
1338 }
1339
1340 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001341 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001342}
1343
Willy Tarreau26ffa852018-09-05 15:23:10 +02001344static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001345{
1346 return lf_text_len(dst, src, size, size, node);
1347}
1348
William Lallemand5f232402012-04-05 18:02:55 +02001349/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001350 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001351 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001352 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001353char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001354{
1355 char *ret = dst;
1356 int iret;
1357 char pn[INET6_ADDRSTRLEN];
1358
1359 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001360 unsigned char *addr = NULL;
1361 switch (sockaddr->sa_family) {
1362 case AF_INET:
1363 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1364 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1365 break;
1366 case AF_INET6:
1367 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1368 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1369 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1370 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1371 break;
1372 default:
1373 return NULL;
1374 }
William Lallemand5f232402012-04-05 18:02:55 +02001375 if (iret < 0 || iret > size)
1376 return NULL;
1377 ret += iret;
1378 } else {
1379 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1380 ret = lf_text(dst, pn, size, node);
1381 if (ret == NULL)
1382 return NULL;
1383 }
1384 return ret;
1385}
1386
1387/*
1388 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001389 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001390 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001391char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001392{
1393 char *ret = dst;
1394 int iret;
1395
1396 if (node->options & LOG_OPT_HEXA) {
1397 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1398 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1399 if (iret < 0 || iret > size)
1400 return NULL;
1401 ret += iret;
1402 } else {
1403 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1404 if (ret == NULL)
1405 return NULL;
1406 }
1407 return ret;
1408}
1409
Emeric Brun54648852020-07-06 15:54:06 +02001410
1411/*
1412 * This function sends the syslog message using a printf format string. It
1413 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001414 */
Emeric Brun54648852020-07-06 15:54:06 +02001415void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001416{
Emeric Brun54648852020-07-06 15:54:06 +02001417 va_list argp;
1418 int data_len;
1419
1420 if (level < 0 || format == NULL || logline == NULL)
1421 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001422
Emeric Brun54648852020-07-06 15:54:06 +02001423 va_start(argp, format);
1424 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1425 if (data_len < 0 || data_len > global.max_syslog_len)
1426 data_len = global.max_syslog_len;
1427 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001428
Emeric Brun54648852020-07-06 15:54:06 +02001429 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1430 logline, data_len, default_rfc5424_sd_log_format, 2);
1431}
1432/*
1433 * This function builds a log header of given format using given
1434 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1435 * to determine format based on given metadas. It is useful
1436 * for log-forwarding to be able to forward any format without
1437 * settings.
1438 * This function returns a struct ist array of elements of the header
1439 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001440 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001441 * elements.
1442 */
1443struct ist *build_log_header(enum log_fmt format, int level, int facility,
1444 struct ist *metadata, size_t *nbelem)
1445{
1446 static THREAD_LOCAL struct {
1447 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1448 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1449 time_t cur_legacy_time;
1450 char priority_buffer[6];
1451 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001452
Emeric Brun54648852020-07-06 15:54:06 +02001453 struct tm logtime;
1454 int len;
1455 int fac_level = 0;
1456 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001457
Emeric Brun54648852020-07-06 15:54:06 +02001458 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001459
Emeric Brun54648852020-07-06 15:54:06 +02001460
1461 if (format == LOG_FORMAT_UNSPEC) {
1462 format = LOG_FORMAT_RAW;
1463 if (metadata) {
1464 /* If a hostname is set, it appears we want to perform syslog
1465 * because only rfc5427 or rfc3164 support an hostname.
1466 */
1467 if (metadata[LOG_META_HOST].len) {
1468 /* If a rfc5424 compliant timestamp is used we consider
1469 * that output format is rfc5424, else legacy format
1470 * is used as specified default for local logs
1471 * in documentation.
1472 */
1473 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1474 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1475 format = LOG_FORMAT_RFC5424;
1476 else
1477 format = LOG_FORMAT_RFC3164;
1478 }
1479 else if (metadata[LOG_META_PRIO].len) {
1480 /* the source seems a parsed message
1481 * offering a valid level/prio prefix
1482 * so we consider this format.
1483 */
1484 format = LOG_FORMAT_PRIO;
1485 }
1486 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001487 }
1488
Emeric Brun54648852020-07-06 15:54:06 +02001489 /* prepare priority, stored into 1 single elem */
1490 switch (format) {
1491 case LOG_FORMAT_RFC3164:
1492 case LOG_FORMAT_RFC5424:
1493 case LOG_FORMAT_PRIO:
1494 fac_level = facility << 3;
1495 /* further format ignore the facility */
1496 /* fall through */
1497 case LOG_FORMAT_TIMED:
1498 case LOG_FORMAT_SHORT:
1499 fac_level += level;
1500 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1501 do {
1502 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1503 fac_level /= 10;
1504 hdr_ctx.ist_vector[*nbelem].ptr--;
1505 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1506 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1507 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1508 break;
1509 case LOG_FORMAT_ISO:
1510 case LOG_FORMAT_RAW:
1511 break;
1512 case LOG_FORMAT_UNSPEC:
1513 case LOG_FORMATS:
1514 ABORT_NOW();
1515 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001516
William Lallemand2a4a44f2012-02-06 16:00:33 +01001517
Emeric Brun54648852020-07-06 15:54:06 +02001518 /* prepare timestamp, stored into a max of 4 elems */
1519 switch (format) {
1520 case LOG_FORMAT_RFC3164:
1521 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1522 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1523 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1524 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001525 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001526 break;
1527 }
1528 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1529 int month;
1530 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001531
Emeric Brun54648852020-07-06 15:54:06 +02001532 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001533
Emeric Brun54648852020-07-06 15:54:06 +02001534 /* compute month */
1535 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1536 if (month)
1537 month--;
1538 if (month <= 11) {
1539 /* builds log prefix ex: 'Jan 1 ' */
1540 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1541 "%s %c%c ", monthname[month],
1542 timestamp[8] != '0' ? timestamp[8] : ' ',
1543 timestamp[9]);
1544 /* we reused the timestamp_buffer, signal that it does not
1545 * contain local time anymore
1546 */
1547 hdr_ctx.cur_legacy_time = 0;
1548 if (len == 7) {
1549 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1550 /* adds 'HH:MM:SS' from iso time */
1551 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1552 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1553 /* we successfully reuse iso time, we can break */
1554 break;
1555 }
1556 }
1557 /* Failed to reuse isotime time, fallback to local legacy time */
1558 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001559
Emeric Brun54648852020-07-06 15:54:06 +02001560 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1561 /* re-builds timestamp from the current local time */
1562 get_localtime(time, &logtime);
1563
1564 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1565 "%s %2d %02d:%02d:%02d ",
1566 monthname[logtime.tm_mon],
1567 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1568 if (len != LOG_LEGACYTIME_LEN+1)
1569 hdr_ctx.cur_legacy_time = 0;
1570 else
1571 hdr_ctx.cur_legacy_time = time;
1572 }
1573 if (likely(hdr_ctx.cur_legacy_time))
1574 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1575 else
1576 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1577 break;
1578 case LOG_FORMAT_RFC5424:
1579 /* adds rfc5425 version prefix */
1580 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1581 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001582 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001583 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1584 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1585 break;
1586 }
1587 /* let continue as 'timed' and 'iso' format for usual timestamp */
1588 /* fall through */
1589 case LOG_FORMAT_TIMED:
1590 case LOG_FORMAT_ISO:
1591 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1592 * '1900:01:01T14:00:00+02:00'
1593 * '1900:01:01T10:00:00.123456-02:00'
1594 */
1595 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1596 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1597 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001598 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001599 break;
1600 }
1601 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1602 int month;
1603 char *timestamp = metadata[LOG_META_TIME].ptr;
1604
1605 for (month = 0; month < 12; month++)
1606 if (!memcmp(monthname[month], timestamp, 3))
1607 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001608
Emeric Brun54648852020-07-06 15:54:06 +02001609 if (month < 12) {
1610
1611 /* get local time to retrieve year */
1612 get_localtime(time, &logtime);
1613
1614 /* year seems changed since log */
1615 if (logtime.tm_mon < month)
1616 logtime.tm_year--;
1617
1618 /* builds rfc5424 prefix ex: '1900-01-01T' */
1619 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1620 "%4d-%02d-%c%cT",
1621 logtime.tm_year+1900, month+1,
1622 timestamp[4] != ' ' ? timestamp[4] : '0',
1623 timestamp[5]);
1624
1625 /* we reused the timestamp_buffer, signal that it does not
1626 * contain local time anymore
1627 */
1628 hdr_ctx.cur_legacy_time = 0;
1629 if (len == 11) {
1630 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1631 /* adds HH:MM:SS from legacy timestamp */
1632 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001633 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001634 /* according to rfc: -00:00 means we don't know the timezone */
1635 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1636 /* we successfully reuse legacy time, we can break */
1637 break;
1638 }
1639 }
1640 /* Failed to reuse legacy time, fallback to local iso time */
1641 }
1642 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1643 break;
1644 case LOG_FORMAT_PRIO:
1645 case LOG_FORMAT_SHORT:
1646 case LOG_FORMAT_RAW:
1647 break;
1648 case LOG_FORMAT_UNSPEC:
1649 case LOG_FORMATS:
1650 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001651 }
1652
Emeric Brun54648852020-07-06 15:54:06 +02001653 /* prepare other meta data, stored into a max of 10 elems */
1654 switch (format) {
1655 case LOG_FORMAT_RFC3164:
1656 if (metadata && metadata[LOG_META_HOST].len) {
1657 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1658 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1659 }
1660 else /* the caller MUST fill the hostname */
1661 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun9f9b22c2020-07-02 16:16:59 +02001662
Emeric Brun54648852020-07-06 15:54:06 +02001663 if (!metadata || !metadata[LOG_META_TAG].len)
1664 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001665
Emeric Brun54648852020-07-06 15:54:06 +02001666 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1667 if (metadata[LOG_META_PID].len) {
1668 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1669 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1670 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1671 }
1672 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1673 break;
1674 case LOG_FORMAT_RFC5424:
1675 if (metadata && metadata[LOG_META_HOST].len) {
1676 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1677 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1678 }
1679 else
1680 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001681
Emeric Brun54648852020-07-06 15:54:06 +02001682 if (metadata && metadata[LOG_META_TAG].len) {
1683 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1684 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1685 }
1686 else
1687 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001688
Emeric Brun54648852020-07-06 15:54:06 +02001689 if (metadata && metadata[LOG_META_PID].len) {
1690 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1691 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1692 }
1693 else
1694 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001695
Emeric Brun54648852020-07-06 15:54:06 +02001696 if (metadata && metadata[LOG_META_MSGID].len) {
1697 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1698 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1699 }
1700 else
1701 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001702
Emeric Brun54648852020-07-06 15:54:06 +02001703 if (metadata && metadata[LOG_META_STDATA].len) {
1704 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1705 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1706 }
1707 else
1708 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1709 break;
1710 case LOG_FORMAT_PRIO:
1711 case LOG_FORMAT_SHORT:
1712 case LOG_FORMAT_TIMED:
1713 case LOG_FORMAT_ISO:
1714 case LOG_FORMAT_RAW:
1715 break;
1716 case LOG_FORMAT_UNSPEC:
1717 case LOG_FORMATS:
1718 ABORT_NOW();
1719 }
1720
1721 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001722}
1723
1724/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001725 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001726 * The argument <metadata> MUST be an array of size
1727 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001728 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001729 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001730 */
Emeric Brun54648852020-07-06 15:54:06 +02001731static 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 +01001732{
Emeric Brun54648852020-07-06 15:54:06 +02001733 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001734 static THREAD_LOCAL struct msghdr msghdr = {
1735 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001736 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001737 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001738 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1739 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001740 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001741 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001742 size_t nbelem;
1743 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001744
1745 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001746
Emeric Brunfa9d7802020-05-28 14:21:33 +02001747 /* historically some messages used to already contain the trailing LF
1748 * or Zero. Let's remove all trailing LF or Zero
1749 */
Emeric Brun54648852020-07-06 15:54:06 +02001750 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001751 size--;
1752
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001753 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001754 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001755 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001756 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001757 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1758 /* the socket's address is a file descriptor */
1759 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1760 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001761 else if (logsrv->addr.ss_family == AF_UNIX)
1762 plogfd = &logfdunix;
1763 else
1764 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001765
Willy Tarreauc046d162019-08-30 15:24:59 +02001766 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001767 /* socket not successfully initialized yet */
1768 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1769 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1770 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001771
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001772 if (!once) {
1773 once = 1; /* note: no need for atomic ops here */
1774 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1775 nblogger, strerror(errno), errno);
1776 }
1777 return;
1778 } else {
1779 /* we don't want to receive anything on this socket */
1780 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1781 /* does nothing under Linux, maybe needed for others */
1782 shutdown(*plogfd, SHUT_RD);
1783 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1784 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001785 }
1786
Emeric Brun54648852020-07-06 15:54:06 +02001787 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1788 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001789 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001790 struct ist msg;
1791
1792 msg = ist2(message, size);
1793 if (msg.len > logsrv->maxlen)
1794 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001795
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001796 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1797 }
1798 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1799 struct ist msg;
1800
1801 msg = ist2(message, size);
1802 if (msg.len > logsrv->maxlen)
1803 msg.len = logsrv->maxlen;
1804
1805 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001806 }
1807 else {
Emeric Brun54648852020-07-06 15:54:06 +02001808 int i = 0;
1809 int totlen = logsrv->maxlen;
1810
1811 for (i = 0 ; i < nbelem ; i++ ) {
1812 iovec[i].iov_base = msg_header[i].ptr;
1813 iovec[i].iov_len = msg_header[i].len;
1814 if (totlen <= iovec[i].iov_len) {
1815 iovec[i].iov_len = totlen;
1816 totlen = 0;
1817 break;
1818 }
1819 totlen -= iovec[i].iov_len;
1820 }
1821 if (totlen) {
1822 iovec[i].iov_base = message;
1823 iovec[i].iov_len = size;
1824 if (totlen <= iovec[i].iov_len)
1825 iovec[i].iov_len = totlen;
1826 i++;
1827 }
1828 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1829 iovec[i].iov_len = 1;
1830 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001831
Emeric Brun54648852020-07-06 15:54:06 +02001832 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001833 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1834 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001835
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001836 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1837 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001838
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001839 if (sent < 0) {
1840 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001841
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001842 if (errno == EAGAIN)
1843 _HA_ATOMIC_ADD(&dropped_logs, 1);
1844 else if (!once) {
1845 once = 1; /* note: no need for atomic ops here */
1846 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1847 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001848 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001849 }
1850}
Dragan Dosen59cee972015-09-19 22:09:02 +02001851
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001852/*
1853 * This function sends a syslog message.
1854 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001855 * The argument <metadata> MUST be an array of size
1856 * LOG_META_FIELDS*sizeof(struct ist) containing
1857 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001858 */
Emeric Brun54648852020-07-06 15:54:06 +02001859void process_send_log(struct list *logsrvs, int level, int facility,
1860 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001861{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001862 struct logsrv *logsrv;
1863 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001864
1865 /* Send log messages to syslog server. */
1866 nblogger = 0;
1867 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001868 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001869
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001870 /* we can filter the level of the messages that are sent to each logger */
1871 if (level > logsrv->level)
1872 continue;
1873
Frédéric Lécailled803e472019-04-25 07:42:09 +02001874 if (logsrv->lb.smp_rgs) {
1875 struct smp_log_range *curr_rg;
1876
1877 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1878 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1879 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1880 if (in_range) {
1881 /* Let's consume this range. */
1882 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1883 if (!curr_rg->curr_idx) {
1884 /* If consumed, let's select the next range. */
1885 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1886 }
1887 }
1888 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1889 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1890 }
1891 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001892 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1893 (facility == -1) ? logsrv->facility : facility,
1894 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001895 }
1896}
1897
Emeric Brun54648852020-07-06 15:54:06 +02001898/*
1899 * This function sends a syslog message.
1900 * It doesn't care about errors nor does it report them.
1901 * The arguments <sd> and <sd_size> are used for the structured-data part
1902 * in RFC5424 formatted syslog messages.
1903 */
1904void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1905 char *message, size_t size, char *sd, size_t sd_size)
1906{
1907 static THREAD_LOCAL pid_t curr_pid;
1908 static THREAD_LOCAL char pidstr[16];
1909 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1910
1911 if (logsrvs == NULL) {
1912 if (!LIST_ISEMPTY(&global.logsrvs)) {
1913 logsrvs = &global.logsrvs;
1914 }
1915 }
1916 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1917 return;
1918
1919 if (!metadata[LOG_META_HOST].len) {
1920 if (global.log_send_hostname)
1921 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
1922 else
1923 metadata[LOG_META_HOST] = ist2(hostname, strlen(hostname));
1924 }
1925
1926 if (!tagb || !tagb->area)
1927 tagb = &global.log_tag;
1928
1929 if (tagb)
1930 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1931
1932 if (unlikely(curr_pid != getpid()))
1933 metadata[LOG_META_PID].len = 0;
1934
1935 if (!metadata[LOG_META_PID].len) {
1936 curr_pid = getpid();
1937 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1938 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1939 }
1940
1941 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1942
1943 /* Remove trailing space of structured data */
1944 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1945 metadata[LOG_META_STDATA].len--;
1946
1947 return process_send_log(logsrvs, level, -1, metadata, message, size);
1948}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001949
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001950const 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 +01001951const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1952 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1953 Set-cookie Updated, unknown, unknown */
1954
William Lallemand1d705562012-03-12 12:46:41 +01001955/*
1956 * try to write a character if there is enough space, or goto out
1957 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001958#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001959 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001960 *(tmplog++) = (x); \
1961 } else { \
1962 goto out; \
1963 } \
1964 } while(0)
1965
Dragan Dosen835b9212016-02-12 13:23:03 +01001966
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001967/* Initializes some log data at boot */
1968static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001969{
1970 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001971 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001972
1973 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1974 * inside PARAM-VALUE should be escaped with '\' as prefix.
1975 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1976 * details.
1977 */
1978 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1979
1980 tmp = "\"\\]";
1981 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001982 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001983 tmp++;
1984 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001985
1986 /* initialize the log header encoding map : '{|}"#' should be encoded with
1987 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1988 * URL encoding only requires '"', '#' to be encoded as well as non-
1989 * printable characters above.
1990 */
1991 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1992 memset(url_encode_map, 0, sizeof(url_encode_map));
1993 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001994 ha_bit_set(i, hdr_encode_map);
1995 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001996 }
1997 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001998 ha_bit_set(i, hdr_encode_map);
1999 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002000 }
2001
2002 tmp = "\"#{|}";
2003 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002004 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002005 tmp++;
2006 }
2007
2008 tmp = "\"#";
2009 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002010 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002011 tmp++;
2012 }
2013
2014 /* initialize the http header encoding map. The draft httpbis define the
2015 * header content as:
2016 *
2017 * HTTP-message = start-line
2018 * *( header-field CRLF )
2019 * CRLF
2020 * [ message-body ]
2021 * header-field = field-name ":" OWS field-value OWS
2022 * field-value = *( field-content / obs-fold )
2023 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2024 * obs-fold = CRLF 1*( SP / HTAB )
2025 * field-vchar = VCHAR / obs-text
2026 * VCHAR = %x21-7E
2027 * obs-text = %x80-FF
2028 *
2029 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
2030 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08002031 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02002032 */
2033 memset(http_encode_map, 0, sizeof(http_encode_map));
2034 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002035 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002036 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002037 ha_bit_set(i, http_encode_map);
2038 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002039}
William Lallemand1d705562012-03-12 12:46:41 +01002040
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002041INITCALL0(STG_PREPARE, init_log);
2042
Christopher Faulet0132d062017-07-26 15:33:35 +02002043/* Initialize log buffers used for syslog messages */
2044int init_log_buffers()
2045{
Christopher Faulet0132d062017-07-26 15:33:35 +02002046 logline = my_realloc2(logline, global.max_syslog_len + 1);
2047 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02002048 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02002049 return 0;
2050 return 1;
2051}
2052
2053/* Deinitialize log buffers used for syslog messages */
2054void deinit_log_buffers()
2055{
Christopher Faulet0132d062017-07-26 15:33:35 +02002056 free(logline);
2057 free(logline_rfc5424);
Willy Tarreau869efd52019-11-15 15:16:57 +01002058 ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
Christopher Faulet0132d062017-07-26 15:33:35 +02002059 logline = NULL;
2060 logline_rfc5424 = NULL;
2061}
2062
Willy Tarreaudf974472012-12-28 02:44:01 +01002063/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2064 * <maxsize> characters. Returns the size of the output string in characters,
2065 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002066 * is not zero. It requires a valid session and optionally a stream. If the
2067 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002068 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002069int 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 +02002070{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002071 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002072 struct proxy *be;
2073 struct http_txn *txn;
2074 const struct strm_logs *logs;
Willy Tarreau8fa99842019-07-17 11:47:11 +02002075 struct connection *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002076 unsigned int s_flags;
2077 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002078 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002079 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002080 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002081 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002082 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002083 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002084 int t_request;
2085 int hdr;
2086 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002087 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002088 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002089 char *ret;
2090 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002091 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002092 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002093 struct strm_logs tmp_strm_log;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002094
William Lallemandbddd4fd2012-02-27 11:23:10 +01002095 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002096
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002097 if (likely(s)) {
2098 be = s->be;
2099 txn = s->txn;
2100 be_conn = cs_conn(objt_cs(s->si[1].end));
2101 s_flags = s->flags;
2102 uniq_id = s->uniq_id;
2103 logs = &s->logs;
2104 } else {
2105 /* we have no stream so we first need to initialize a few
2106 * things that are needed later. We do increment the request
2107 * ID so that it's uniquely assigned to this request just as
2108 * if the request had reached the point of being processed.
2109 * A request error is reported as it's the only element we have
2110 * here and which justifies emitting such a log.
2111 */
2112 be = fe;
2113 txn = NULL;
2114 be_conn = NULL;
2115 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01002116 uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002117
2118 /* prepare a valid log structure */
2119 tmp_strm_log.tv_accept = sess->tv_accept;
2120 tmp_strm_log.accept_date = sess->accept_date;
2121 tmp_strm_log.t_handshake = sess->t_handshake;
2122 tmp_strm_log.t_idle = tv_ms_elapsed(&sess->tv_accept, &now) - sess->t_handshake;
2123 tv_zero(&tmp_strm_log.tv_request);
2124 tmp_strm_log.t_queue = -1;
2125 tmp_strm_log.t_connect = -1;
2126 tmp_strm_log.t_data = -1;
2127 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2128 tmp_strm_log.bytes_in = 0;
2129 tmp_strm_log.bytes_out = 0;
2130 tmp_strm_log.prx_queue_pos = 0;
2131 tmp_strm_log.srv_queue_pos = 0;
2132
2133 logs = &tmp_strm_log;
2134 }
2135
William Lallemandbddd4fd2012-02-27 11:23:10 +01002136 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002137 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2138 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002139
William Lallemand1d705562012-03-12 12:46:41 +01002140 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002141
William Lallemandbddd4fd2012-02-27 11:23:10 +01002142 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002143 if (LIST_ISEMPTY(list_format))
2144 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002145
William Lallemand1d705562012-03-12 12:46:41 +01002146 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002147 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002148 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002149 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002150 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002151
Willy Tarreauc8368452012-12-21 00:09:23 +01002152 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002153 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002154 if (!last_isspace) {
2155 LOGCHAR(' ');
2156 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002157 }
2158 break;
2159
William Lallemand1d705562012-03-12 12:46:41 +01002160 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002161 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002162 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002163 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002164 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002165 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002166 last_isspace = 0;
2167 break;
2168
Willy Tarreauc8368452012-12-21 00:09:23 +01002169 case LOG_FMT_EXPR: // sample expression, may be request or response
2170 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002171 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002172 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 +02002173 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002174 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 +01002175 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002176 ret = lf_encode_chunk(tmplog, dst + maxsize,
2177 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002178 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002179 ret = lf_text_len(tmplog,
2180 key ? key->data.u.str.area : NULL,
2181 key ? key->data.u.str.data : 0,
2182 dst + maxsize - tmplog,
2183 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002184 if (ret == 0)
2185 goto out;
2186 tmplog = ret;
2187 last_isspace = 0;
2188 break;
2189
Willy Tarreau2beef582012-12-20 17:22:52 +01002190 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002191 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002192 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002193 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002194 else
2195 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002196 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002197 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002198 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002199 last_isspace = 0;
2200 break;
2201
Willy Tarreau2beef582012-12-20 17:22:52 +01002202 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002203 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002204 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002205 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002206 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002207 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002208 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002209 dst + maxsize - tmplog, tmp);
2210 }
William Lallemand5f232402012-04-05 18:02:55 +02002211 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002212 else
2213 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2214
William Lallemand5f232402012-04-05 18:02:55 +02002215 if (ret == NULL)
2216 goto out;
2217 tmplog = ret;
2218 last_isspace = 0;
2219 break;
2220
Willy Tarreau2beef582012-12-20 17:22:52 +01002221 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002222 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002223 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002224 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002225 }
2226 else
2227 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2228
William Lallemand1d705562012-03-12 12:46:41 +01002229 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002230 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002231 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002232 last_isspace = 0;
2233 break;
2234
Willy Tarreau2beef582012-12-20 17:22:52 +01002235 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002236 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002237 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002238 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002239 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002240 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002241 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002242 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002243 else
2244 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2245
William Lallemand5f232402012-04-05 18:02:55 +02002246 if (ret == NULL)
2247 goto out;
2248 tmplog = ret;
2249 last_isspace = 0;
2250 break;
2251
Willy Tarreau2beef582012-12-20 17:22:52 +01002252 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002253 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002254 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002255 else
2256 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2257
William Lallemand1d705562012-03-12 12:46:41 +01002258 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002259 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002260 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002261 last_isspace = 0;
2262 break;
2263
Willy Tarreau2beef582012-12-20 17:22:52 +01002264 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002265 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002266 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002267 else
2268 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2269
William Lallemand5f232402012-04-05 18:02:55 +02002270 if (ret == NULL)
2271 goto out;
2272 tmplog = ret;
2273 last_isspace = 0;
2274 break;
2275
Willy Tarreau2beef582012-12-20 17:22:52 +01002276 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002277 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002278 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002279 else
2280 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2281
William Lallemand5f232402012-04-05 18:02:55 +02002282 if (ret == NULL)
2283 goto out;
2284 tmplog = ret;
2285 last_isspace = 0;
2286 break;
2287
Willy Tarreau2beef582012-12-20 17:22:52 +01002288 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002289 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002290 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, 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);
2293
William Lallemand1d705562012-03-12 12:46:41 +01002294 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002295 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002296 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002297 last_isspace = 0;
2298 break;
2299
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002300 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002301 get_localtime(logs->accept_date.tv_sec, &tm);
2302 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002303 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002304 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002305 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002306 last_isspace = 0;
2307 break;
2308
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002309 case LOG_FMT_tr: // %tr = start of request date
2310 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002311 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 +02002312 get_localtime(tv.tv_sec, &tm);
2313 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2314 if (ret == NULL)
2315 goto out;
2316 tmplog = ret;
2317 last_isspace = 0;
2318 break;
2319
2320 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002321 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002322 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002323 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002324 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002325 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002326 last_isspace = 0;
2327 break;
2328
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002329 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002330 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 +02002331 get_gmtime(tv.tv_sec, &tm);
2332 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2333 if (ret == NULL)
2334 goto out;
2335 tmplog = ret;
2336 last_isspace = 0;
2337 break;
2338
2339 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002340 get_localtime(logs->accept_date.tv_sec, &tm);
2341 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002342 if (ret == NULL)
2343 goto out;
2344 tmplog = ret;
2345 last_isspace = 0;
2346 break;
2347
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002348 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002349 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 +02002350 get_localtime(tv.tv_sec, &tm);
2351 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2352 if (ret == NULL)
2353 goto out;
2354 tmplog = ret;
2355 last_isspace = 0;
2356 break;
2357
William Lallemand5f232402012-04-05 18:02:55 +02002358 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002359 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002360 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002361 if (iret < 0 || iret > dst + maxsize - tmplog)
2362 goto out;
2363 last_isspace = 0;
2364 tmplog += iret;
2365 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002366 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002367 if (ret == NULL)
2368 goto out;
2369 tmplog = ret;
2370 last_isspace = 0;
2371 }
2372 break;
2373
William Lallemand1d705562012-03-12 12:46:41 +01002374 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002375 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002376 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002377 if (iret < 0 || iret > dst + maxsize - tmplog)
2378 goto out;
2379 last_isspace = 0;
2380 tmplog += iret;
2381 } else {
2382 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002383 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002384 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002385 tmplog, 4);
2386 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002387 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002388 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002389 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002390 }
2391 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002392
William Lallemand1d705562012-03-12 12:46:41 +01002393 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002394 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002395 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002396 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002397 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002398 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002399 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002400 break;
2401
Willy Tarreau773d65f2012-10-12 14:56:11 +02002402 case LOG_FMT_FRONTEND_XPRT: // %ft
2403 src = fe->id;
2404 if (tmp->options & LOG_OPT_QUOTE)
2405 LOGCHAR('"');
2406 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2407 if (iret == 0)
2408 goto out;
2409 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002410 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002411 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002412 if (tmp->options & LOG_OPT_QUOTE)
2413 LOGCHAR('"');
2414 last_isspace = 0;
2415 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002416#ifdef USE_OPENSSL
2417 case LOG_FMT_SSL_CIPHER: // %sslc
2418 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002419 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002420 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002421 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002422 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002423 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2424 if (ret == NULL)
2425 goto out;
2426 tmplog = ret;
2427 last_isspace = 0;
2428 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002429
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002430 case LOG_FMT_SSL_VERSION: // %sslv
2431 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002432 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002433 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002434 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002435 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002436 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2437 if (ret == NULL)
2438 goto out;
2439 tmplog = ret;
2440 last_isspace = 0;
2441 break;
2442#endif
William Lallemand1d705562012-03-12 12:46:41 +01002443 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002444 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002445 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002446 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002447 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002448 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002449 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002450 break;
2451
William Lallemand1d705562012-03-12 12:46:41 +01002452 case LOG_FMT_SERVER: // %s
Willy Tarreaue1809df2018-09-05 15:30:16 +02002453 switch (obj_type(s ? s->target : NULL)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002454 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002455 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002456 break;
2457 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002458 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002459 break;
2460 default:
2461 src = "<NOSRV>";
2462 break;
2463 }
William Lallemand5f232402012-04-05 18:02:55 +02002464 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002465 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002466 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002467 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002468 last_isspace = 0;
2469 break;
2470
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002471 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002472 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002473 if (ret == NULL)
2474 goto out;
2475 tmplog = ret;
2476 last_isspace = 0;
2477 break;
2478
2479 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002480 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002481 if (ret == NULL)
2482 goto out;
2483 tmplog = ret;
2484 last_isspace = 0;
2485 break;
2486
2487 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002488 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002489 tmplog, dst + maxsize - tmplog);
2490 if (ret == NULL)
2491 goto out;
2492 tmplog = ret;
2493 last_isspace = 0;
2494 break;
2495
2496 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002497 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002498 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002499 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002500 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002501 last_isspace = 0;
2502 break;
2503
William Lallemand1d705562012-03-12 12:46:41 +01002504 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002505 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002506 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002507 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002508 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002509 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002510 last_isspace = 0;
2511 break;
2512
William Lallemand1d705562012-03-12 12:46:41 +01002513 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002514 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002515 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002516 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002517 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002518 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002519 last_isspace = 0;
2520 break;
2521
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002522 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002523 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002524 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002525 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002526 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002527 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002528 last_isspace = 0;
2529 break;
2530
Willy Tarreau27b639d2016-05-17 17:55:27 +02002531 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002532 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002533 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002534 tmplog, dst + maxsize - tmplog);
2535 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002536 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002537 tmplog, dst + maxsize - tmplog);
2538 if (ret == NULL)
2539 goto out;
2540 tmplog = ret;
2541 last_isspace = 0;
2542 break;
2543
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002544 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2545 if (!(fe->to_log & LW_BYTES))
2546 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002547 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 +02002548 tmplog, dst + maxsize - tmplog);
2549 if (ret == NULL)
2550 goto out;
2551 tmplog = ret;
2552 last_isspace = 0;
2553 break;
2554
2555 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002556 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002557 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002558 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002559 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002560 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002561 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002562 last_isspace = 0;
2563 break;
2564
Damien Claisse57c8eb92020-04-28 12:09:19 +00002565 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2566 if (!(fe->to_log & LW_BYTES))
2567 LOGCHAR('+');
2568 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2569 tmplog, dst + maxsize - tmplog);
2570 if (ret == NULL)
2571 goto out;
2572 tmplog = ret;
2573 last_isspace = 0;
2574 break;
2575
Willy Tarreau2beef582012-12-20 17:22:52 +01002576 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02002577 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002578 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002579 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002580 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002581 last_isspace = 0;
2582 break;
2583
William Lallemand1d705562012-03-12 12:46:41 +01002584 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002585 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002586 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002587 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002588 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002589 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002590 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002591 last_isspace = 0;
2592 break;
2593
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002594 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002595 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002596 if (ret == NULL)
2597 goto out;
2598 tmplog = ret;
2599 last_isspace = 0;
2600 break;
2601
Willy Tarreau2beef582012-12-20 17:22:52 +01002602 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002603 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002604 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002605 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002606 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002607 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002608 last_isspace = 0;
2609 break;
2610
Willy Tarreau2beef582012-12-20 17:22:52 +01002611 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002612 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002613 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002614 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002615 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002616 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002617 last_isspace = 0;
2618 break;
2619
William Lallemand1d705562012-03-12 12:46:41 +01002620 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002621 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2622 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002623 *tmplog = '\0';
2624 last_isspace = 0;
2625 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002626
William Lallemand1d705562012-03-12 12:46:41 +01002627 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002628 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2629 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002630 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2631 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 +01002632 last_isspace = 0;
2633 break;
2634
William Lallemand1d705562012-03-12 12:46:41 +01002635 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002636 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002637 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002638 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002639 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002640 last_isspace = 0;
2641 break;
2642
William Lallemand1d705562012-03-12 12:46:41 +01002643 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002644 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002645 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002646 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002647 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002648 last_isspace = 0;
2649 break;
2650
William Lallemand1d705562012-03-12 12:46:41 +01002651 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002652 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002653 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002654 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002655 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002656 last_isspace = 0;
2657 break;
2658
William Lallemand1d705562012-03-12 12:46:41 +01002659 case LOG_FMT_SRVCONN: // %sc
Willy Tarreaue1809df2018-09-05 15:30:16 +02002660 ret = ultoa_o(objt_server(s ? s->target : NULL) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002661 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002662 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002663 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002664 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002665 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002666 last_isspace = 0;
2667 break;
2668
William Lallemand1d705562012-03-12 12:46:41 +01002669 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002670 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002671 LOGCHAR('+');
Willy Tarreauabd71a52018-09-04 19:21:44 +02002672 ret = ltoa_o((s && s->si[1].conn_retries > 0) ?
Willy Tarreau350f4872014-11-28 14:42:25 +01002673 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002674 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002675 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002676 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_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002682 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002683 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002684 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002685 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002686 last_isspace = 0;
2687 break;
2688
William Lallemand1d705562012-03-12 12:46:41 +01002689 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002690 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002691 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002692 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002693 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002694 last_isspace = 0;
2695 break;
2696
William Lallemand1d705562012-03-12 12:46:41 +01002697 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002698 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002699 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002700 if (tmp->options & LOG_OPT_QUOTE)
2701 LOGCHAR('"');
2702 LOGCHAR('{');
2703 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2704 if (hdr)
2705 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002706 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002707 ret = lf_encode_string(tmplog, dst + maxsize,
2708 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002709 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002710 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002711 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002712 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002713 }
2714 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002715 if (tmp->options & LOG_OPT_QUOTE)
2716 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002717 last_isspace = 0;
2718 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002719 break;
2720
William Lallemand1d705562012-03-12 12:46:41 +01002721 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002722 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002723 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002724 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2725 if (hdr > 0)
2726 LOGCHAR(' ');
2727 if (tmp->options & LOG_OPT_QUOTE)
2728 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002729 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002730 ret = lf_encode_string(tmplog, dst + maxsize,
2731 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002732 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002733 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002734 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002735 } else if (!(tmp->options & LOG_OPT_QUOTE))
2736 LOGCHAR('-');
2737 if (tmp->options & LOG_OPT_QUOTE)
2738 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002739 last_isspace = 0;
2740 }
2741 }
2742 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002743
William Lallemand1d705562012-03-12 12:46:41 +01002744
2745 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002746 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002747 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002748 if (tmp->options & LOG_OPT_QUOTE)
2749 LOGCHAR('"');
2750 LOGCHAR('{');
2751 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2752 if (hdr)
2753 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002754 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002755 ret = lf_encode_string(tmplog, dst + maxsize,
2756 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002757 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002758 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002759 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002760 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002761 }
2762 LOGCHAR('}');
2763 last_isspace = 0;
2764 if (tmp->options & LOG_OPT_QUOTE)
2765 LOGCHAR('"');
2766 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002767 break;
2768
William Lallemand1d705562012-03-12 12:46:41 +01002769 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002770 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002771 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002772 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2773 if (hdr > 0)
2774 LOGCHAR(' ');
2775 if (tmp->options & LOG_OPT_QUOTE)
2776 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002777 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002778 ret = lf_encode_string(tmplog, dst + maxsize,
2779 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002780 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002781 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002782 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002783 } else if (!(tmp->options & LOG_OPT_QUOTE))
2784 LOGCHAR('-');
2785 if (tmp->options & LOG_OPT_QUOTE)
2786 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002787 last_isspace = 0;
2788 }
2789 }
2790 break;
2791
William Lallemand1d705562012-03-12 12:46:41 +01002792 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002793 /* Request */
2794 if (tmp->options & LOG_OPT_QUOTE)
2795 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002796 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002797 ret = lf_encode_string(tmplog, dst + maxsize,
2798 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002799 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002800 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002801 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002802 if (tmp->options & LOG_OPT_QUOTE)
2803 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002804 last_isspace = 0;
2805 break;
William Lallemand5f232402012-04-05 18:02:55 +02002806
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002807 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002808 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002809
Willy Tarreaub7636d12015-06-17 19:58:02 +02002810 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002811 LOGCHAR('"');
2812
2813 end = uri + strlen(uri);
2814 // look for the first whitespace character
2815 while (uri < end && !HTTP_IS_SPHT(*uri))
2816 uri++;
2817
2818 // keep advancing past multiple spaces
2819 while (uri < end && HTTP_IS_SPHT(*uri)) {
2820 uri++; nspaces++;
2821 }
2822
2823 // look for first space or question mark after url
2824 spc = uri;
2825 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2826 spc++;
2827
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002828 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002829 chunk.area = "<BADREQ>";
2830 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002831 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002832 chunk.area = uri;
2833 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002834 }
2835
Dragan Dosen835b9212016-02-12 13:23:03 +01002836 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002837 if (ret == NULL || *ret != '\0')
2838 goto out;
2839
2840 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002841 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002842 LOGCHAR('"');
2843
2844 last_isspace = 0;
2845 break;
2846
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002847 case LOG_FMT_HTTP_QUERY: // %HQ
2848 if (tmp->options & LOG_OPT_QUOTE)
2849 LOGCHAR('"');
2850
Willy Tarreau57bc8912016-04-25 17:09:40 +02002851 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002852 chunk.area = "<BADREQ>";
2853 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002854 } else {
2855 uri = txn->uri;
2856 end = uri + strlen(uri);
2857 // look for the first question mark
2858 while (uri < end && *uri != '?')
2859 uri++;
2860
2861 qmark = uri;
2862 // look for first space or question mark after url
2863 while (uri < end && !HTTP_IS_SPHT(*uri))
2864 uri++;
2865
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002866 chunk.area = qmark;
2867 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002868 }
2869
Dragan Dosen835b9212016-02-12 13:23:03 +01002870 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002871 if (ret == NULL || *ret != '\0')
2872 goto out;
2873
2874 tmplog = ret;
2875 if (tmp->options & LOG_OPT_QUOTE)
2876 LOGCHAR('"');
2877
2878 last_isspace = 0;
2879 break;
2880
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002881 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002882 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002883
Willy Tarreaub7636d12015-06-17 19:58:02 +02002884 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002885 LOGCHAR('"');
2886
2887 end = uri + strlen(uri);
2888 // look for the first whitespace character
2889 while (uri < end && !HTTP_IS_SPHT(*uri))
2890 uri++;
2891
2892 // keep advancing past multiple spaces
2893 while (uri < end && HTTP_IS_SPHT(*uri)) {
2894 uri++; nspaces++;
2895 }
2896
2897 // look for first space after url
2898 spc = uri;
2899 while (spc < end && !HTTP_IS_SPHT(*spc))
2900 spc++;
2901
Willy Tarreau57bc8912016-04-25 17:09:40 +02002902 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002903 chunk.area = "<BADREQ>";
2904 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002905 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002906 chunk.area = uri;
2907 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002908 }
2909
Dragan Dosen835b9212016-02-12 13:23:03 +01002910 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002911 if (ret == NULL || *ret != '\0')
2912 goto out;
2913
2914 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002915 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002916 LOGCHAR('"');
2917
2918 last_isspace = 0;
2919 break;
2920
2921 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002922 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002923 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002924 LOGCHAR('"');
2925
2926 end = uri + strlen(uri);
2927 // look for the first whitespace character
2928 spc = uri;
2929 while (spc < end && !HTTP_IS_SPHT(*spc))
2930 spc++;
2931
2932 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002933 chunk.area = "<BADREQ>";
2934 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002935 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002936 chunk.area = uri;
2937 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002938 }
2939
Dragan Dosen835b9212016-02-12 13:23:03 +01002940 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002941 if (ret == NULL || *ret != '\0')
2942 goto out;
2943
2944 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002945 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002946 LOGCHAR('"');
2947
2948 last_isspace = 0;
2949 break;
2950
2951 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002952 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002953 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002954 LOGCHAR('"');
2955
2956 end = uri + strlen(uri);
2957 // look for the first whitespace character
2958 while (uri < end && !HTTP_IS_SPHT(*uri))
2959 uri++;
2960
2961 // keep advancing past multiple spaces
2962 while (uri < end && HTTP_IS_SPHT(*uri)) {
2963 uri++; nspaces++;
2964 }
2965
2966 // look for the next whitespace character
2967 while (uri < end && !HTTP_IS_SPHT(*uri))
2968 uri++;
2969
2970 // keep advancing past multiple spaces
2971 while (uri < end && HTTP_IS_SPHT(*uri))
2972 uri++;
2973
Willy Tarreau57bc8912016-04-25 17:09:40 +02002974 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002975 chunk.area = "<BADREQ>";
2976 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002977 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002978 chunk.area = "HTTP/0.9";
2979 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002980 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002981 chunk.area = uri;
2982 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002983 }
2984
Dragan Dosen835b9212016-02-12 13:23:03 +01002985 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002986 if (ret == NULL || *ret != '\0')
2987 goto out;
2988
2989 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002990 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002991 LOGCHAR('"');
2992
2993 last_isspace = 0;
2994 break;
2995
William Lallemand5f232402012-04-05 18:02:55 +02002996 case LOG_FMT_COUNTER: // %rt
2997 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002998 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002999 if (iret < 0 || iret > dst + maxsize - tmplog)
3000 goto out;
3001 last_isspace = 0;
3002 tmplog += iret;
3003 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003004 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003005 if (ret == NULL)
3006 goto out;
3007 tmplog = ret;
3008 last_isspace = 0;
3009 }
3010 break;
3011
Willy Tarreau7346acb2014-08-28 15:03:15 +02003012 case LOG_FMT_LOGCNT: // %lc
3013 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003014 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003015 if (iret < 0 || iret > dst + maxsize - tmplog)
3016 goto out;
3017 last_isspace = 0;
3018 tmplog += iret;
3019 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003020 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003021 if (ret == NULL)
3022 goto out;
3023 tmplog = ret;
3024 last_isspace = 0;
3025 }
3026 break;
3027
William Lallemand5f232402012-04-05 18:02:55 +02003028 case LOG_FMT_HOSTNAME: // %H
3029 src = hostname;
3030 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3031 if (ret == NULL)
3032 goto out;
3033 tmplog = ret;
3034 last_isspace = 0;
3035 break;
3036
3037 case LOG_FMT_PID: // %pid
3038 if (tmp->options & LOG_OPT_HEXA) {
3039 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3040 if (iret < 0 || iret > dst + maxsize - tmplog)
3041 goto out;
3042 last_isspace = 0;
3043 tmplog += iret;
3044 } else {
3045 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3046 if (ret == NULL)
3047 goto out;
3048 tmplog = ret;
3049 last_isspace = 0;
3050 }
3051 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003052
3053 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003054 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003055 if (s)
3056 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3057 else
3058 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003059 if (ret == NULL)
3060 goto out;
3061 tmplog = ret;
3062 last_isspace = 0;
3063 break;
3064
William Lallemandbddd4fd2012-02-27 11:23:10 +01003065 }
3066 }
3067
3068out:
William Lallemand1d705562012-03-12 12:46:41 +01003069 /* *tmplog is a unused character */
3070 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003071 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003072
Willy Tarreaubaaee002006-06-26 02:48:02 +02003073}
3074
William Lallemand1d705562012-03-12 12:46:41 +01003075/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003076 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003077 * Will not log if the frontend has no log defined.
3078 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003079void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003080{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003081 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003082 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003083 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003084
3085 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003086 err = (s->flags & SF_REDISP) ||
3087 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3088 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003089 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003090 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003091
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003092 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003093 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003094
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003095 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003096 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003097
Willy Tarreauabcd5142013-06-11 17:18:02 +02003098 if (s->logs.level) { /* loglevel was overridden */
3099 if (s->logs.level == -1) {
3100 s->logs.logwait = 0; /* logs disabled */
3101 return;
3102 }
3103 level = s->logs.level - 1;
3104 }
3105 else {
3106 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003107 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003108 level = LOG_ERR;
3109 }
William Lallemand1d705562012-03-12 12:46:41 +01003110
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003111 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003112 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003113 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003114 }
3115
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003116 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3117 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3118 &sess->fe->logformat_sd);
3119 }
3120
Dragan Dosen59cee972015-09-19 22:09:02 +02003121 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003122 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003123 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003124 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3125 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003126 s->logs.logwait = 0;
3127 }
3128}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003129
Willy Tarreau53839352018-09-05 19:51:10 +02003130/*
3131 * send a minimalist log for the session. Will not log if the frontend has no
3132 * log defined. It is assumed that this is only used to report anomalies that
3133 * cannot lead to the creation of a regular stream. Because of this the log
3134 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3135 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003136 * function to report unimportant events. It is safe to call this function with
3137 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003138 */
3139void sess_log(struct session *sess)
3140{
3141 int size, level;
3142 int sd_size = 0;
3143
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003144 if (!sess)
3145 return;
3146
Willy Tarreau53839352018-09-05 19:51:10 +02003147 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3148 return;
3149
3150 level = LOG_INFO;
3151 if (sess->fe->options2 & PR_O2_LOGERRORS)
3152 level = LOG_ERR;
3153
3154 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3155 sd_size = sess_build_logline(sess, NULL,
3156 logline_rfc5424, global.max_syslog_len,
3157 &sess->fe->logformat_sd);
3158 }
3159
3160 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3161 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003162 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003163 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3164 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003165 }
3166}
3167
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003168void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3169{
3170 va_list argp;
3171 int data_len;
3172
3173 if (level < 0 || format == NULL || logline == NULL)
3174 return;
3175
3176 va_start(argp, format);
3177 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3178 if (data_len < 0 || data_len > global.max_syslog_len)
3179 data_len = global.max_syslog_len;
3180 va_end(argp);
3181
3182 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3183}
Emeric Brun54932b42020-07-07 09:43:24 +02003184/*
3185 * This function parse a received log message <buf>, of size <buflen>
3186 * it fills <level>, <facility> and <metadata> depending of the detected
3187 * header format and message will point on remaining payload of <size>
3188 *
3189 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3190 * struct ist len will be set to 0 if field is not found
3191 * <level> and <facility> will be set to -1 if not found.
3192 */
3193void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3194 struct ist *metadata, char **message, size_t *size)
3195{
3196
3197 char *p;
3198 int fac_level = 0;
3199
3200 *level = *facility = -1;
3201
3202 *message = buf;
3203 *size = buflen;
3204
3205 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3206
3207 p = buf;
3208 if (*size < 2 || *p != '<')
3209 return;
3210
3211 p++;
3212 while (*p != '>') {
3213 if (*p > '9' || *p < '0')
3214 return;
3215 fac_level = 10*fac_level + (*p - '0');
3216 p++;
3217 if ((p - buf) > buflen)
3218 return;
3219 }
3220
3221 *facility = fac_level >> 3;
3222 *level = fac_level & 0x7;
3223 p++;
3224
3225 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3226
3227 buflen -= p - buf;
3228 buf = p;
3229
3230 *size = buflen;
3231 *message = buf;
3232
3233 /* for rfc5424, prio is always followed by '1' and ' ' */
3234 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3235 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3236 * followed by message.
3237 * Each header field can present NILVALUE: '-'
3238 */
3239
3240 p += 2;
3241 /* timestamp is NILVALUE '-' */
3242 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3243 metadata[LOG_META_TIME] = ist2(p, 1);
3244 p++;
3245 }
3246 else if (*size > LOG_ISOTIME_MINLEN) {
3247 metadata[LOG_META_TIME].ptr = p;
3248
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003249 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003250 * in timestamp.
3251 * possible format are:
3252 * ex: '1970-01-01T00:00:00.000000Z'
3253 * '1970-01-01T00:00:00.000000+00:00'
3254 * '1970-01-01T00:00:00.000000-00:00'
3255 * '1970-01-01T00:00:00Z'
3256 * '1970-01-01T00:00:00+00:00'
3257 * '1970-01-01T00:00:00-00:00'
3258 */
3259 p += 19;
3260 if (*p == '.') {
3261 p++;
3262 if ((p - buf) >= buflen)
3263 goto bad_format;
3264 while (*p != 'Z' && *p != '+' && *p != '-') {
3265 if ((unsigned char)(*p - '0') > 9)
3266 goto bad_format;
3267
3268 p++;
3269 if ((p - buf) >= buflen)
3270 goto bad_format;
3271 }
3272 }
3273
3274 if (*p == 'Z')
3275 p++;
3276 else
3277 p += 6; /* case of '+00:00 or '-00:00' */
3278
3279 if ((p - buf) >= buflen || *p != ' ')
3280 goto bad_format;
3281 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3282 }
3283 else
3284 goto bad_format;
3285
3286
3287 p++;
3288 if ((p - buf) >= buflen || *p == ' ')
3289 goto bad_format;
3290
3291 metadata[LOG_META_HOST].ptr = p;
3292 while (*p != ' ') {
3293 p++;
3294 if ((p - buf) >= buflen)
3295 goto bad_format;
3296 }
3297 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3298 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3299 metadata[LOG_META_HOST].len = 0;
3300
3301 p++;
3302 if ((p - buf) >= buflen || *p == ' ')
3303 goto bad_format;
3304
3305 metadata[LOG_META_TAG].ptr = p;
3306 while (*p != ' ') {
3307 p++;
3308 if ((p - buf) >= buflen)
3309 goto bad_format;
3310 }
3311 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3312 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3313 metadata[LOG_META_TAG].len = 0;
3314
3315 p++;
3316 if ((p - buf) >= buflen || *p == ' ')
3317 goto bad_format;
3318
3319 metadata[LOG_META_PID].ptr = p;
3320 while (*p != ' ') {
3321 p++;
3322 if ((p - buf) >= buflen)
3323 goto bad_format;
3324 }
3325 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3326 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3327 metadata[LOG_META_PID].len = 0;
3328
3329 p++;
3330 if ((p - buf) >= buflen || *p == ' ')
3331 goto bad_format;
3332
3333 metadata[LOG_META_MSGID].ptr = p;
3334 while (*p != ' ') {
3335 p++;
3336 if ((p - buf) >= buflen)
3337 goto bad_format;
3338 }
3339 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3340 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3341 metadata[LOG_META_MSGID].len = 0;
3342
3343 p++;
3344 if ((p - buf) >= buflen || *p == ' ')
3345 goto bad_format;
3346
3347 /* structured data format is:
3348 * ex:
3349 * '[key1=value1 key2=value2][key3=value3]'
3350 *
3351 * space is invalid outside [] because
3352 * considered as the end of structured data field
3353 */
3354 metadata[LOG_META_STDATA].ptr = p;
3355 if (*p == '[') {
3356 int elem = 0;
3357
3358 while (1) {
3359 if (elem) {
3360 /* according to rfc this char is escaped in param values */
3361 if (*p == ']' && *(p-1) != '\\')
3362 elem = 0;
3363 }
3364 else {
3365 if (*p == '[')
3366 elem = 1;
3367 else if (*p == ' ')
3368 break;
3369 else
3370 goto bad_format;
3371 }
3372 p++;
3373 if ((p - buf) >= buflen)
3374 goto bad_format;
3375 }
3376 }
3377 else if (*p == '-') {
3378 /* case of NILVALUE */
3379 p++;
3380 if ((p - buf) >= buflen || *p != ' ')
3381 goto bad_format;
3382 }
3383 else
3384 goto bad_format;
3385
3386 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3387 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3388 metadata[LOG_META_STDATA].len = 0;
3389
3390 p++;
3391
3392 buflen -= p - buf;
3393 buf = p;
3394
3395 *size = buflen;
3396 *message = p;
3397 }
3398 else if (*size > LOG_LEGACYTIME_LEN) {
3399 int m;
3400
3401 /* supported header format according to rfc3164.
3402 * ex:
3403 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3404 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3405 * or 'Jan 1 00:00:00 HOSTNAME '
3406 * Note: HOSTNAME is mandatory, and day
3407 * of month uses a single space prefix if
3408 * less than 10 to ensure hour offset is
3409 * always the same.
3410 */
3411
3412 /* Check month to see if it correspond to a rfc3164
3413 * header ex 'Jan 1 00:00:00' */
3414 for (m = 0; m < 12; m++)
3415 if (!memcmp(monthname[m], p, 3))
3416 break;
3417 /* Month not found */
3418 if (m == 12)
3419 goto bad_format;
3420
3421 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3422
3423 p += LOG_LEGACYTIME_LEN;
3424 if ((p - buf) >= buflen || *p != ' ')
3425 goto bad_format;
3426
3427 p++;
3428 if ((p - buf) >= buflen || *p == ' ')
3429 goto bad_format;
3430
3431 metadata[LOG_META_HOST].ptr = p;
3432 while (*p != ' ') {
3433 p++;
3434 if ((p - buf) >= buflen)
3435 goto bad_format;
3436 }
3437 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3438
3439 /* TAG seems to no be mandatory */
3440 p++;
3441
3442 buflen -= p - buf;
3443 buf = p;
3444
3445 *size = buflen;
3446 *message = buf;
3447
3448 if (!buflen)
3449 return;
3450
3451 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3452 p++;
3453
3454 /* a tag must present a trailing ':' */
3455 if (((p - buf) >= buflen) || *p != ':')
3456 return;
3457 p++;
3458 /* followed by a space */
3459 if (((p - buf) >= buflen) || *p != ' ')
3460 return;
3461
3462 /* rewind to parse tag and pid */
3463 p = buf;
3464 metadata[LOG_META_TAG].ptr = p;
3465 /* we have the guarantee that ':' will be reach before size limit */
3466 while (*p != ':') {
3467 if (*p == '[') {
3468 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3469 metadata[LOG_META_PID].ptr = p + 1;
3470 }
3471 else if (*p == ']' && metadata[LOG_META_PID].ptr) {
3472 if (p[1] != ':')
3473 return;
3474 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3475 }
3476 p++;
3477 }
3478 if (!metadata[LOG_META_TAG].len)
3479 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3480
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003481 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003482 p += 2;
3483
3484 buflen -= p - buf;
3485 buf = p;
3486
3487 *size = buflen;
3488 *message = buf;
3489 }
3490
3491 return;
3492
3493bad_format:
3494 /* bad syslog format, we reset all parsed syslog fields
3495 * but priority is kept because we are able to re-build
3496 * this message using LOF_FORMAT_PRIO.
3497 */
3498 metadata[LOG_META_TIME].len = 0;
3499 metadata[LOG_META_HOST].len = 0;
3500 metadata[LOG_META_TAG].len = 0;
3501 metadata[LOG_META_PID].len = 0;
3502 metadata[LOG_META_MSGID].len = 0;
3503 metadata[LOG_META_STDATA].len = 0;
3504
3505 return;
3506}
3507
3508/*
3509 * UDP syslog fd handler
3510 */
3511void syslog_fd_handler(int fd)
3512{
3513 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3514 ssize_t ret = 0;
3515 struct buffer *buf = get_trash_chunk();
3516 size_t size;
3517 char *message;
3518 int level;
3519 int facility;
3520 struct listener *l = objt_listener(fdtab[fd].owner);
3521 int max_accept;
3522
3523 if(!l)
3524 ABORT_NOW();
3525
3526 if (fdtab[fd].ev & FD_POLL_IN) {
3527
3528 if (!fd_recv_ready(fd))
3529 return;
3530
3531 max_accept = l->maxaccept ? l->maxaccept : 1;
3532
3533 do {
3534 /* Source address */
3535 struct sockaddr_storage saddr = {0};
3536 socklen_t saddrlen;
3537
3538 saddrlen = sizeof(saddr);
3539
3540 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3541 if (ret < 0) {
3542 if (errno == EINTR)
3543 continue;
3544 if (errno == EAGAIN)
3545 fd_cant_recv(fd);
3546 goto out;
3547 }
3548 buf->data = ret;
3549
Emeric Brun45c457a2020-07-09 23:23:34 +02003550 /* update counters */
3551 _HA_ATOMIC_ADD(&cum_log_messages, 1);
3552 proxy_inc_fe_conn_ctr(l, l->bind_conf->frontend);
3553
Emeric Brun54932b42020-07-07 09:43:24 +02003554 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3555
3556 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3557
3558 } while (--max_accept);
3559 }
3560
3561out:
3562 return;
3563}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003564
Emeric Brun12941c82020-07-07 14:19:42 +02003565/*
3566 * Parse "log-forward" section and create corresponding sink buffer.
3567 *
3568 * The function returns 0 in success case, otherwise, it returns error
3569 * flags.
3570 */
3571int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3572{
3573 int err_code = 0;
3574 struct proxy *px;
3575 char *errmsg = NULL;
3576 const char *err = NULL;
3577
3578 if (strcmp(args[0], "log-forward") == 0) {
3579 if (!*args[1]) {
3580 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3581 err_code |= ERR_ALERT | ERR_ABORT;
3582 goto out;
3583 }
3584
3585 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3586 goto out;
3587
3588 err = invalid_char(args[1]);
3589 if (err) {
3590 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3591 file, linenum, *err, args[0], args[1]);
3592 err_code |= ERR_ALERT | ERR_ABORT;
3593 goto out;
3594 }
3595
3596 for (px = cfg_log_forward ; px ; px = px->next) {
3597 if (strcmp(px->id, args[1]) == 0) {
3598 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3599 file, linenum, args[1], px->conf.file, px->conf.line);
3600 err_code |= ERR_ALERT | ERR_FATAL;
3601 }
3602 }
3603
3604 px = calloc(1, sizeof *px);
3605 if (!px) {
3606 err_code |= ERR_ALERT | ERR_FATAL;
3607 goto out;
3608 }
3609
3610 px->next = cfg_log_forward;
3611 cfg_log_forward = px;
3612
3613 init_new_proxy(px);
3614 px->conf.file = strdup(file);
3615 px->conf.line = linenum;
3616 px->mode = PR_MODE_SYSLOG;
3617 px->id = strdup(args[1]);
3618
3619 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003620 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003621 int cur_arg;
3622 static int kws_dumped;
3623 struct bind_conf *bind_conf;
3624 struct bind_kw *kw;
3625 struct listener *l;
3626
3627 cur_arg = 1;
3628
3629 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3630 NULL, xprt_get(XPRT_RAW));
3631
3632 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3633 if (errmsg && *errmsg) {
3634 indent_msg(&errmsg, 2);
3635 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3636 }
3637 else {
3638 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3639 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003640 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003641 err_code |= ERR_ALERT | ERR_FATAL;
3642 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003643 }
3644 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
3645 /* Currently, only UDP handlers are allowed */
Willy Tarreaub7436612020-08-28 19:51:44 +02003646 if (l->rx.proto->sock_domain != AF_CUST_UDP4 && l->rx.proto->sock_domain != AF_CUST_UDP6) {
Emeric Brun12941c82020-07-07 14:19:42 +02003647 ha_alert("parsing [%s:%d] : '%s %s' : error, listening address must be prefixed using 'udp@', 'udp4@' or 'udp6@' %s.\n",
3648 file, linenum, args[0], args[1], args[2]);
3649 err_code |= ERR_ALERT | ERR_FATAL;
3650 goto out;
3651 }
3652 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : 64;
3653 global.maxsock++;
3654 }
3655 cur_arg++;
3656
3657 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3658 int ret;
3659
3660 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3661 err_code |= ret;
3662 if (ret) {
3663 if (errmsg && *errmsg) {
3664 indent_msg(&errmsg, 2);
3665 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3666 }
3667 else
3668 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3669 file, linenum, args[cur_arg]);
3670 if (ret & ERR_FATAL)
3671 goto out;
3672 }
3673 cur_arg += 1 + kw->skip;
3674 }
3675 if (*args[cur_arg] != 0) {
3676 char *kws = NULL;
3677
3678 if (!kws_dumped) {
3679 kws_dumped = 1;
3680 bind_dump_kws(&kws);
3681 indent_msg(&kws, 4);
3682 }
3683 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.%s%s\n",
3684 file, linenum, args[cur_arg], cursection,
3685 kws ? " Registered keywords :" : "", kws ? kws: "");
3686 free(kws);
3687 err_code |= ERR_ALERT | ERR_FATAL;
3688 goto out;
3689 }
3690 }
3691 else if (strcmp(args[0], "log") == 0) {
3692 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), &errmsg)) {
3693 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3694 err_code |= ERR_ALERT | ERR_FATAL;
3695 goto out;
3696 }
3697 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02003698 else {
3699 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
3700 err_code |= ERR_ALERT | ERR_ABORT;
3701 goto out;
3702 }
Emeric Brun12941c82020-07-07 14:19:42 +02003703out:
3704 return err_code;
3705}
3706
3707
Willy Tarreau869efd52019-11-15 15:16:57 +01003708/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
3709static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
Christopher Fauletc1b730a2017-10-24 12:00:51 +02003710{
Willy Tarreau869efd52019-11-15 15:16:57 +01003711 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3712 return 1;
Christopher Fauletc1b730a2017-10-24 12:00:51 +02003713
Willy Tarreau869efd52019-11-15 15:16:57 +01003714 if (!startup_logs)
3715 return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
3716
3717 return ring_attach_cli(startup_logs, appctx);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02003718}
3719
3720/* register cli keywords */
3721static struct cli_kw_list cli_kws = {{ },{
3722 { { "show", "startup-logs", NULL },
Willy Tarreau869efd52019-11-15 15:16:57 +01003723 "show startup-logs : report logs emitted during HAProxy startup", cli_parse_show_startup_logs, NULL, NULL },
Christopher Fauletc1b730a2017-10-24 12:00:51 +02003724 {{},}
3725}};
3726
Willy Tarreau0108d902018-11-25 19:14:37 +01003727INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3728
Emeric Brun12941c82020-07-07 14:19:42 +02003729/* config parsers for this section */
3730REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
3731
Willy Tarreau082b6282019-05-22 14:42:12 +02003732REGISTER_PER_THREAD_ALLOC(init_log_buffers);
3733REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01003734
Willy Tarreaubaaee002006-06-26 02:48:02 +02003735/*
3736 * Local variables:
3737 * c-indent-level: 8
3738 * c-basic-offset: 8
3739 * End:
3740 */