blob: f760bacba7e7778339940b2406795ed266e69a58 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
Willy Tarreau077edcb2016-08-10 18:30:56 +020025#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/applet-t.h>
Emeric Brun12941c82020-07-07 14:19:42 +020029#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020030#include <haproxy/cli.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020032#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020034#include <haproxy/http.h>
Emeric Brun12941c82020-07-07 14:19:42 +020035#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036#include <haproxy/log.h>
Emeric Brun12941c82020-07-07 14:19:42 +020037#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/ring.h>
39#include <haproxy/sample.h>
40#include <haproxy/sink.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020041#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020043#include <haproxy/stream_interface.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020044#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020045#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020046#include <haproxy/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Emeric Brun45c457a2020-07-09 23:23:34 +020048/* global recv logs counter */
49int cum_log_messages;
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Emeric Brun12941c82020-07-07 14:19:42 +020051/* log forward proxy list */
52struct proxy *cfg_log_forward;
53
Emeric Brun54648852020-07-06 15:54:06 +020054struct log_fmt_st {
Dragan Dosen43885c72015-10-01 13:18:13 +020055 char *name;
Dragan Dosen43885c72015-10-01 13:18:13 +020056};
57
Emeric Brun54648852020-07-06 15:54:06 +020058static const struct log_fmt_st log_formats[LOG_FORMATS] = {
Emeric Brun0237c4e2020-11-27 16:24:34 +010059 [LOG_FORMAT_LOCAL] = {
60 .name = "local",
61 },
Dragan Dosen43885c72015-10-01 13:18:13 +020062 [LOG_FORMAT_RFC3164] = {
63 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020064 },
65 [LOG_FORMAT_RFC5424] = {
66 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020067 },
68 [LOG_FORMAT_PRIO] = {
69 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010070 },
71 [LOG_FORMAT_SHORT] = {
72 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020073 },
74 [LOG_FORMAT_TIMED] = {
75 .name = "timed",
76 },
77 [LOG_FORMAT_ISO] = {
78 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010079 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010080 [LOG_FORMAT_RAW] = {
81 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010082 },
Dragan Dosen1322d092015-09-22 16:05:32 +020083};
84
Dragan Dosen835b9212016-02-12 13:23:03 +010085/*
86 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020087 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
88 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
89 * that the byte should be escaped. Be careful to always pass bytes from 0 to
90 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010091 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020092long rfc5424_escape_map[(256/8) / sizeof(long)];
93long hdr_encode_map[(256/8) / sizeof(long)];
94long url_encode_map[(256/8) / sizeof(long)];
95long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010096
Dragan Dosen835b9212016-02-12 13:23:03 +010097
Willy Tarreaubaaee002006-06-26 02:48:02 +020098const char *log_facilities[NB_LOG_FACILITIES] = {
99 "kern", "user", "mail", "daemon",
100 "auth", "syslog", "lpr", "news",
101 "uucp", "cron", "auth2", "ftp",
102 "ntp", "audit", "alert", "cron2",
103 "local0", "local1", "local2", "local3",
104 "local4", "local5", "local6", "local7"
105};
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107const char *log_levels[NB_LOG_LEVELS] = {
108 "emerg", "alert", "crit", "err",
109 "warning", "notice", "info", "debug"
110};
111
Willy Tarreau570f2212013-06-10 16:42:09 +0200112const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200113const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
William Lallemand723b73a2012-02-08 16:37:49 +0100115
116/* log_format */
117struct logformat_type {
118 char *name;
119 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100120 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200121 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100122 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100123 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100124};
125
William Lallemandb7ff6a32012-03-02 14:35:21 +0100126int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
127
William Lallemand723b73a2012-02-08 16:37:49 +0100128/* log_format variable names */
129static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200130 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100131
132 /* please keep these lines sorted ! */
133 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
134 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
135 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
136 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100137 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200138 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200139 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200140 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100141 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200142 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
143 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
144 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
145 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
146 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
147 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200148 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100149 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200150 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000151 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100152 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
153 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200154 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100155 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200156 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100157 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
158 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200159 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200160 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
161 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100162 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
163 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200164 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
165 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100166 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200167 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
168 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
169 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
170 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000171 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
Maciej Zdebfcdfd852020-11-30 18:27:47 +0000172 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP relative or absolute path */
173 { "HPO", LOG_FMT_HTTP_PATH_ONLY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path only (without host nor query string) */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000174 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000175 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
176 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200177 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100178 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200179 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100180 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
181 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100182 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100183 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
184 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
185 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
186 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
187 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200188 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
189 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100190 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200191 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
192 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
193 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100194 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
195 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
196
197 /* The following tags are deprecated and will be removed soon */
198 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
199 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200200 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
201 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
202 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
203 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100204 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
205 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
206 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
207 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
208 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200209 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100210};
211
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200212char default_http_log_format[] = "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
213char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%trg] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl";
Willy Tarreau2beef582012-12-20 17:22:52 +0100214char default_tcp_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq";
William Lallemand723b73a2012-02-08 16:37:49 +0100215char *log_format = NULL;
216
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200217/* Default string used for structured-data part in RFC5424 formatted
218 * syslog messages.
219 */
220char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200221
Willy Tarreau13ef7732018-11-12 07:25:28 +0100222/* total number of dropped logs */
223unsigned int dropped_logs = 0;
224
Dragan Dosen59cee972015-09-19 22:09:02 +0200225/* This is a global syslog message buffer, common to all outgoing
226 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100227 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200228THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100229
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200230/* A global syslog message buffer, common to all RFC5424 syslog messages.
231 * Currently, it is used for generating the structured-data part.
232 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200233THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200234
Christopher Fauletd4696382017-10-24 11:44:05 +0200235/* A global buffer used to store all startup alerts/warnings. It will then be
236 * retrieve on the CLI. */
Willy Tarreau869efd52019-11-15 15:16:57 +0100237static struct ring *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200238
William Lallemand723b73a2012-02-08 16:37:49 +0100239struct logformat_var_args {
240 char *name;
241 int mask;
242};
243
244struct logformat_var_args var_args_list[] = {
245// global
246 { "M", LOG_OPT_MANDATORY },
247 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200248 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100249 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100250 { 0, 0 }
251};
252
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200253/* return the name of the directive used in the current proxy for which we're
254 * currently parsing a header, when it is known.
255 */
256static inline const char *fmt_directive(const struct proxy *curproxy)
257{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100258 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200259 case ARGC_ACL:
260 return "acl";
261 case ARGC_STK:
262 return "stick";
263 case ARGC_TRK:
264 return "track-sc";
265 case ARGC_LOG:
266 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200267 case ARGC_LOGSD:
268 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100269 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100270 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100271 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100272 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200273 case ARGC_UIF:
274 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100275 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200276 return "redirect";
277 case ARGC_CAP:
278 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200279 case ARGC_SRV:
280 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200281 case ARGC_SPOE:
282 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100283 case ARGC_UBK:
284 return "use_backend";
Christopher Faulet3b967c12020-05-15 15:47:44 +0200285 case ARGC_HERR:
286 return "http-error";
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +0100287 case ARGC_OT:
288 return "ot-scope";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100289 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200290 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100291 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200292}
293
William Lallemand723b73a2012-02-08 16:37:49 +0100294/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100295 * callback used to configure addr source retrieval
296 */
297int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
298{
299 curproxy->options2 |= PR_O2_SRC_ADDR;
300
301 return 0;
302}
303
304
305/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100306 * Parse args in a logformat_var. Returns 0 in error
307 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100308 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100309int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100310{
311 int i = 0;
312 int end = 0;
313 int flags = 0; // 1 = + 2 = -
314 char *sp = NULL; // start pointer
315
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100316 if (args == NULL) {
317 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100318 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100319 }
William Lallemand723b73a2012-02-08 16:37:49 +0100320
321 while (1) {
322 if (*args == '\0')
323 end = 1;
324
325 if (*args == '+') {
326 // add flag
327 sp = args + 1;
328 flags = 1;
329 }
330 if (*args == '-') {
331 // delete flag
332 sp = args + 1;
333 flags = 2;
334 }
335
336 if (*args == '\0' || *args == ',') {
337 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100338 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100339 if (strcmp(sp, var_args_list[i].name) == 0) {
340 if (flags == 1) {
341 node->options |= var_args_list[i].mask;
342 break;
343 } else if (flags == 2) {
344 node->options &= ~var_args_list[i].mask;
345 break;
346 }
347 }
348 }
349 sp = NULL;
350 if (end)
351 break;
352 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100353 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100354 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100355 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100356}
357
358/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100359 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
360 * must pass the args part in the <arg> pointer with its length in <arg_len>,
361 * and varname with its length in <var> and <var_len> respectively. <arg> is
362 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100363 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100364 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100365int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100366{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100367 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200368 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100369
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100370 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
371 if (strlen(logformat_keywords[j].name) == var_len &&
372 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
373 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200374 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100375 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100376 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200377 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100378 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100379 node->type = logformat_keywords[j].type;
380 node->options = *defoptions;
381 if (arg_len) {
382 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100383 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200384 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100385 }
386 if (node->type == LOG_FMT_GLOBAL) {
387 *defoptions = node->options;
388 free(node->arg);
389 free(node);
390 } else {
391 if (logformat_keywords[j].config_callback &&
392 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200393 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100394 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100395 curproxy->to_log |= logformat_keywords[j].lw;
Willy Tarreau2b718102021-04-21 07:32:39 +0200396 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100397 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100398 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100399 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
400 curproxy->conf.args.file, curproxy->conf.args.line,
401 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100402 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100403 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100404 memprintf(err, "format variable '%s' is reserved for HTTP mode",
405 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200406 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100407 }
William Lallemand723b73a2012-02-08 16:37:49 +0100408 }
409 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100410
411 j = var[var_len];
412 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100413 memprintf(err, "no such format variable '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%'", var);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100414 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200415
416 error_free:
417 if (node) {
418 free(node->arg);
419 free(node);
420 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100421 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100422}
423
424/*
425 * push to the logformat linked list
426 *
427 * start: start pointer
428 * end: end text pointer
429 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100430 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100431 *
432 * LOG_TEXT: copy chars from start to end excluding end.
433 *
434*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100435int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100436{
437 char *str;
438
Willy Tarreaua3571662012-12-20 21:59:12 +0100439 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200440 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100441 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100442 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100443 return 0;
444 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200445 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100446 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100447 str[end - start] = '\0';
448 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100449 node->type = LOG_FMT_TEXT; // type string
Willy Tarreau2b718102021-04-21 07:32:39 +0200450 LIST_APPEND(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100451 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200452 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100453 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100454 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100455 return 0;
456 }
William Lallemand1d705562012-03-12 12:46:41 +0100457 node->type = LOG_FMT_SEPARATOR;
Willy Tarreau2b718102021-04-21 07:32:39 +0200458 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100459 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100460 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100461}
462
463/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100464 * Parse the sample fetch expression <text> and add a node to <list_format> upon
465 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100466 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
467 * is passed in <endptr>, it will be updated with the pointer to the first character
468 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100469 *
470 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100471 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100472int add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, char **err, char **endptr)
Willy Tarreauc8368452012-12-21 00:09:23 +0100473{
474 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200475 struct sample_expr *expr = NULL;
476 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100477 int cmd_arg;
478
479 cmd[0] = text;
480 cmd[1] = "";
481 cmd_arg = 0;
482
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100483 expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err, &curpx->conf.args, endptr);
Willy Tarreauc8368452012-12-21 00:09:23 +0100484 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100485 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200486 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100487 }
488
Vincent Bernat02779b62016-04-03 13:48:43 +0200489 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100490 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100491 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200492 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100493 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100494 node->type = LOG_FMT_EXPR;
495 node->expr = expr;
496 node->options = options;
497
498 if (arg_len) {
499 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100500 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200501 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100502 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100503 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100504 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
505
Willy Tarreau434c57c2013-01-08 01:10:24 +0100506 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100507 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
508
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100509 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100510 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
511 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200512 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100513 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100514
Christopher Faulet581db2b2021-03-26 10:02:46 +0100515 if ((options & LOG_OPT_HTTP) && (expr->fetch->use & (SMP_USE_L6REQ|SMP_USE_L6RES))) {
516 ha_warning("parsing [%s:%d] : L6 sample fetch <%s> ignored in HTTP log-format string.\n",
517 curpx->conf.args.file, curpx->conf.args.line, text);
518 }
519
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200520 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100521 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100522 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100523
William Lallemand65ad6e12014-01-31 15:08:02 +0100524 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
525 * needed with some sample fetches (eg: ssl*). We always set it for
526 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100527 */
528 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200529 if (curpx->http_needed)
530 curpx->to_log |= LW_REQ;
Willy Tarreau2b718102021-04-21 07:32:39 +0200531 LIST_APPEND(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100532 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200533
534 error_free:
535 release_sample_expr(expr);
536 if (node) {
537 free(node->arg);
538 free(node);
539 }
540 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100541}
542
543/*
William Lallemand723b73a2012-02-08 16:37:49 +0100544 * Parse the log_format string and fill a linked list.
545 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200546 * You can set arguments using { } : %{many arguments}varname.
547 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100548 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500549 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100550 * curproxy: the proxy affected
551 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100552 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100553 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100554 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100555 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100556 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100557int parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100558{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100559 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100560 char *arg = NULL; /* start pointer for args */
561 char *var = NULL; /* start pointer for vars */
562 int arg_len = 0;
563 int var_len = 0;
564 int cformat; /* current token format */
565 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100566 struct logformat_node *tmplf, *back;
567
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100568 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100569 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100570 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100571 return 0;
572 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200573 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200574
William Lallemand723b73a2012-02-08 16:37:49 +0100575 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100576 list_for_each_entry_safe(tmplf, back, list_format, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200577 LIST_DELETE(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200578 release_sample_expr(tmplf->expr);
579 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100580 free(tmplf);
581 }
582
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100583 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100584 pformat = cformat;
585
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100586 if (!*str)
587 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100588
Joseph Herlant85b40592018-11-15 12:10:04 -0800589 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100590 * second have all common paths processed at one place. The common paths are the ones
591 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
592 * We use the common LF_INIT state to dispatch to the different final states.
593 */
594 switch (pformat) {
595 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100596 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100597 arg_len = var_len = 0;
598 if (*str == '{') { // optional argument
599 cformat = LF_STARG;
600 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100601 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100602 else if (*str == '[') {
603 cformat = LF_STEXPR;
604 var = str + 1; // store expr in variable name
605 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100606 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100607 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100608 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100609 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100610 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500611 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100612 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100613 /* single '%' followed by blank or digit, send them both */
614 cformat = LF_TEXT;
615 pformat = LF_TEXT; /* finally we include the previous char as well */
616 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600617 memprintf(err, "unexpected variable name near '%c' at position %d line : '%s'. Maybe you want to write a single '%%', use the syntax '%%%%'",
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100618 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100619 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100620
621 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100622 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500623 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100624 break;
625
626 case LF_STARG: // text immediately following '%{'
627 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100628 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100629 arg_len = str - arg;
630 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100631 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100632 break;
633
634 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100635 if (*str == '[') {
636 cformat = LF_STEXPR;
637 var = str + 1; // store expr in variable name
638 break;
639 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100640 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100641 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100642 var = str;
643 break;
644 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100645 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100646 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100647
Willy Tarreauc8368452012-12-21 00:09:23 +0100648 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100649 /* the whole sample expression is parsed at once,
650 * returning the pointer to the first character not
651 * part of the expression, which MUST be the trailing
652 * angle bracket.
653 */
654 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
655 goto fail;
656
657 if (*str == ']') {
658 // end of arg, go on with next state
659 cformat = pformat = LF_EDEXPR;
660 sp = str;
661 }
662 else {
663 char c = *str;
664 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100665 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100666 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
667 else
668 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200669 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100670 }
671 break;
672
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100673 case LF_VAR: // text part of a variable name
674 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100675 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100676 cformat = LF_INIT; // not variable name anymore
677 break;
678
Willy Tarreauc8368452012-12-21 00:09:23 +0100679 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100680 cformat = LF_INIT;
681 }
682
683 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
684 switch (*str) {
685 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100686 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200687 case ' ':
688 if (options & LOG_OPT_MERGE_SPACES) {
689 cformat = LF_SEPARATOR;
690 break;
691 }
692 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100693 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100694 }
695 }
696
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100697 if (cformat != pformat || pformat == LF_SEPARATOR) {
698 switch (pformat) {
699 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100700 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100701 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100702 break;
703 case LF_TEXT:
704 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100705 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100706 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100707 break;
708 }
709 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100710 }
711 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100712
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100713 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100714 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100715 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100716 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100717 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100718
719 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100720 fail:
721 free(backfmt);
722 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100723}
724
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200725/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500726 * Parse the first range of indexes from a string made of a list of comma separated
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200727 * ranges of indexes. Note that an index may be considered as a particular range
728 * with a high limit to the low limit.
729 */
730int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
731{
732 char *end, *p;
733
734 *low = *high = 0;
735
736 p = *arg;
737 end = strchr(p, ',');
738 if (!end)
739 end = p + strlen(p);
740
741 *high = *low = read_uint((const char **)&p, end);
742 if (!*low || (p != end && *p != '-'))
743 goto err;
744
745 if (p == end)
746 goto done;
747
748 p++;
749 *high = read_uint((const char **)&p, end);
750 if (!*high || *high <= *low || p != end)
751 goto err;
752
753 done:
754 if (*end == ',')
755 end++;
756 *arg = end;
757 return 1;
758
759 err:
760 memprintf(err, "wrong sample range '%s'", *arg);
761 return 0;
762}
763
764/*
765 * Returns 1 if the range defined by <low> and <high> overlaps
766 * one of them in <rgs> array of ranges with <sz> the size of this
767 * array, 0 if not.
768 */
769int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
770 unsigned int low, unsigned int high, char **err)
771{
772 size_t i;
773
774 for (i = 0; i < sz; i++) {
775 if ((low >= rgs[i].low && low <= rgs[i].high) ||
776 (high >= rgs[i].low && high <= rgs[i].high)) {
777 memprintf(err, "ranges are overlapping");
778 return 1;
779 }
780 }
781
782 return 0;
783}
784
785int smp_log_range_cmp(const void *a, const void *b)
786{
787 const struct smp_log_range *rg_a = a;
788 const struct smp_log_range *rg_b = b;
789
790 if (rg_a->high < rg_b->low)
791 return -1;
792 else if (rg_a->low > rg_b->high)
793 return 1;
794
795 return 0;
796}
797
798/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200799 * Parse "log" keyword and update <logsrvs> list accordingly.
800 *
801 * When <do_del> is set, it means the "no log" line was parsed, so all log
802 * servers in <logsrvs> are released.
803 *
804 * Otherwise, we try to parse the "log" line. First of all, when the list is not
805 * the global one, we look for the parameter "global". If we find it,
806 * global.logsrvs is copied. Else we parse each arguments.
807 *
808 * The function returns 1 in success case, otherwise, it returns 0 and err is
809 * filled.
810 */
Emeric Brun9533a702021-04-02 10:13:43 +0200811int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200812{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100813 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200814 struct sockaddr_storage *sk;
Emeric Brun94aab062021-04-02 10:41:36 +0200815 struct protocol *proto;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200816 struct logsrv *logsrv = NULL;
817 int port1, port2;
818 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200819 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200820
821 /*
822 * "no log": delete previous herited or defined syslog
823 * servers.
824 */
825 if (do_del) {
826 struct logsrv *back;
827
828 if (*(args[1]) != 0) {
829 memprintf(err, "'no log' does not expect arguments");
830 goto error;
831 }
832
833 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200834 LIST_DELETE(&logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200835 free(logsrv);
836 }
837 return 1;
838 }
839
840 /*
841 * "log global": copy global.logrsvs linked list to the end of logsrvs
842 * list. But first, we check (logsrvs != global.logsrvs).
843 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100844 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200845 if (logsrvs == &global.logsrvs) {
846 memprintf(err, "'global' is not supported for a global syslog server");
847 goto error;
848 }
849 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200850 struct logsrv *node;
851
852 list_for_each_entry(node, logsrvs, list) {
853 if (node->ref == logsrv)
854 goto skip_logsrv;
855 }
856
857 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200858 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200859 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200860 LIST_INIT(&node->list);
Willy Tarreau2b718102021-04-21 07:32:39 +0200861 LIST_APPEND(logsrvs, &node->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200862 node->conf.file = strdup(file);
863 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200864
865 skip_logsrv:
866 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200867 }
868 return 1;
869 }
870
871 /*
872 * "log <address> ...: parse a syslog server line
873 */
874 if (*(args[1]) == 0 || *(args[2]) == 0) {
875 memprintf(err, "expects <address> and <facility> %s as arguments",
876 ((logsrvs == &global.logsrvs) ? "" : "or global"));
877 goto error;
878 }
879
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100880 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
881 if (strcmp(args[1], "stdout") == 0)
882 args[1] = "fd@1";
883 else if (strcmp(args[1], "stderr") == 0)
884 args[1] = "fd@2";
885
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200886 logsrv = calloc(1, sizeof(*logsrv));
887 if (!logsrv) {
888 memprintf(err, "out of memory");
889 goto error;
890 }
891
Emeric Brun9533a702021-04-02 10:13:43 +0200892 logsrv->conf.file = strdup(file);
893 logsrv->conf.line = linenum;
894
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200895 /* skip address for now, it will be parsed at the end */
896 cur_arg = 2;
897
898 /* just after the address, a length may be specified */
899 logsrv->maxlen = MAX_SYSLOG_LEN;
900 if (strcmp(args[cur_arg], "len") == 0) {
901 int len = atoi(args[cur_arg+1]);
902 if (len < 80 || len > 65535) {
903 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
904 args[cur_arg+1]);
905 goto error;
906 }
907 logsrv->maxlen = len;
908 cur_arg += 2;
909 }
910 if (logsrv->maxlen > global.max_syslog_len)
911 global.max_syslog_len = logsrv->maxlen;
912
913 /* after the length, a format may be specified */
914 if (strcmp(args[cur_arg], "format") == 0) {
915 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200916 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200917 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
918 goto error;
919 }
920 cur_arg += 2;
921 }
922
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200923 if (strcmp(args[cur_arg], "sample") == 0) {
924 unsigned low, high;
925 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200926 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
927
928 p = args[cur_arg+1];
929 smp_sz_str = strchr(p, ':');
930 if (!smp_sz_str) {
931 memprintf(err, "Missing sample size");
932 goto error;
933 }
934
935 *smp_sz_str++ = '\0';
936
937 end = p + strlen(p);
938
939 while (p != end) {
940 if (!get_logsrv_smp_range(&low, &high, &p, err))
941 goto error;
942
943 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
944 goto error;
945
946 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
947 if (!smp_rgs) {
948 memprintf(err, "out of memory error");
949 goto error;
950 }
951
952 smp_rgs[smp_rgs_sz].low = low;
953 smp_rgs[smp_rgs_sz].high = high;
954 smp_rgs[smp_rgs_sz].sz = high - low + 1;
955 smp_rgs[smp_rgs_sz].curr_idx = 0;
956 if (smp_rgs[smp_rgs_sz].high > smp_sz)
957 smp_sz = smp_rgs[smp_rgs_sz].high;
958 smp_rgs_sz++;
959 }
960
Tim Duesterhus21648002019-06-23 22:10:10 +0200961 if (smp_rgs == NULL) {
962 memprintf(err, "no sampling ranges given");
963 goto error;
964 }
965
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200966 beg = smp_sz_str;
967 end = beg + strlen(beg);
968 new_smp_sz = read_uint((const char **)&beg, end);
969 if (!new_smp_sz || beg != end) {
970 memprintf(err, "wrong sample size '%s' for sample range '%s'",
971 smp_sz_str, args[cur_arg+1]);
972 goto error;
973 }
974
975 if (new_smp_sz < smp_sz) {
976 memprintf(err, "sample size %zu should be greater or equal to "
977 "%zu the maximum of the high ranges limits",
978 new_smp_sz, smp_sz);
979 goto error;
980 }
981 smp_sz = new_smp_sz;
982
983 /* Let's order <smp_rgs> array. */
984 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
985
986 logsrv->lb.smp_rgs = smp_rgs;
987 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
988 logsrv->lb.smp_sz = smp_sz;
989
990 cur_arg += 2;
991 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200992 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200993 /* parse the facility */
994 logsrv->facility = get_log_facility(args[cur_arg]);
995 if (logsrv->facility < 0) {
996 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
997 goto error;
998 }
999 cur_arg++;
1000
1001 /* parse the max syslog level (default: debug) */
1002 logsrv->level = 7;
1003 if (*(args[cur_arg])) {
1004 logsrv->level = get_log_level(args[cur_arg]);
1005 if (logsrv->level < 0) {
1006 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
1007 goto error;
1008 }
1009 cur_arg++;
1010 }
1011
1012 /* parse the limit syslog level (default: emerg) */
1013 logsrv->minlvl = 0;
1014 if (*(args[cur_arg])) {
1015 logsrv->minlvl = get_log_level(args[cur_arg]);
1016 if (logsrv->minlvl < 0) {
1017 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1018 goto error;
1019 }
1020 cur_arg++;
1021 }
1022
1023 /* Too many args */
1024 if (*(args[cur_arg])) {
1025 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1026 goto error;
1027 }
1028
1029 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001030 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001031 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001032 logsrv->addr.ss_family = AF_UNSPEC;
1033 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001034 logsrv->sink = NULL;
1035 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001036 goto done;
1037 }
1038
Emeric Brun94aab062021-04-02 10:41:36 +02001039 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
1040 err, NULL, NULL,
1041 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM | PA_O_STREAM | PA_O_DEFAULT_DGRAM);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001042 if (!sk)
1043 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001044
1045 if (fd != -1)
1046 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001047 logsrv->addr = *sk;
1048
1049 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001050 if (!port1)
1051 set_host_port(&logsrv->addr, SYSLOG_PORT);
1052 }
Emeric Brun9533a702021-04-02 10:13:43 +02001053
Emeric Brun26754902021-04-07 14:26:44 +02001054 if (proto && proto->ctrl_type == SOCK_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +02001055 static unsigned long ring_ids;
1056
1057 /* Implicit sink buffer will be
1058 * initialized in post_check
1059 */
1060 logsrv->type = LOG_TARGET_BUFFER;
1061 logsrv->sink = NULL;
1062 /* compute uniq name for the ring */
1063 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
1064 }
1065
Willy Tarreauc046d162019-08-30 15:24:59 +02001066 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001067 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001068 return 1;
1069
1070 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001071 free(smp_rgs);
Emeric Brun9533a702021-04-02 10:13:43 +02001072 if (logsrv) {
1073 free(logsrv->conf.file);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001074 free(logsrv->ring_name);
Emeric Brun9533a702021-04-02 10:13:43 +02001075 }
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001076 free(logsrv);
1077 return 0;
1078}
1079
1080
Christopher Fauletd4696382017-10-24 11:44:05 +02001081/* Generic function to display messages prefixed by a label */
1082static void print_message(const char *label, const char *fmt, va_list argp)
1083{
Christopher Fauletd4696382017-10-24 11:44:05 +02001084 char *head, *msg;
Willy Tarreau9f903af2021-05-07 08:42:39 +02001085 char prefix[11]; // '[' + 8 chars + ']' + 0.
Christopher Fauletd4696382017-10-24 11:44:05 +02001086
Willy Tarreau9f903af2021-05-07 08:42:39 +02001087 *prefix = '[';
1088 strncpy(prefix + 1, label, sizeof(prefix) - 2);
1089 msg = prefix + strlen(prefix);
1090 *msg++ = ']';
1091 while (msg < prefix + sizeof(prefix) - 1)
1092 *msg++ = ' ';
1093 *msg = 0;
Christopher Fauletd4696382017-10-24 11:44:05 +02001094
Willy Tarreau9f903af2021-05-07 08:42:39 +02001095 head = msg = NULL;
1096 memprintf(&head, "%s (%u) : ", prefix, (uint)getpid());
Christopher Fauletd4696382017-10-24 11:44:05 +02001097 memvprintf(&msg, fmt, argp);
1098
Willy Tarreau869efd52019-11-15 15:16:57 +01001099 if (global.mode & MODE_STARTING) {
1100 if (unlikely(!startup_logs))
1101 startup_logs = ring_new(STARTUP_LOG_SIZE);
1102
1103 if (likely(startup_logs)) {
1104 struct ist m[2];
1105
1106 m[0] = ist(head);
1107 m[1] = ist(msg);
1108 /* trim the trailing '\n' */
1109 if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
1110 m[1].len--;
1111 ring_write(startup_logs, ~0, 0, 0, m, 2);
1112 }
1113 }
Christopher Fauletd4696382017-10-24 11:44:05 +02001114
1115 fprintf(stderr, "%s%s", head, msg);
1116 fflush(stderr);
1117
1118 free(head);
1119 free(msg);
1120}
1121
Willy Tarreaubaaee002006-06-26 02:48:02 +02001122/*
1123 * Displays the message on stderr with the date and pid. Overrides the quiet
1124 * mode during startup.
1125 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001126void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001127{
1128 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001129
1130 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaubb869862020-04-16 10:52:41 +02001131 if (!(warned & WARN_EXEC_PATH)) {
1132 const char *path = get_exec_path();
1133
1134 warned |= WARN_EXEC_PATH;
1135 ha_notice("haproxy version is %s\n", haproxy_version);
1136 if (path)
1137 ha_notice("path to executable is %s\n", path);
1138 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001139 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001140 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001141 va_end(argp);
1142 }
1143}
1144
1145
1146/*
1147 * Displays the message on stderr with the date and pid.
1148 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001149void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001150{
1151 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001152
Willy Tarreaubebd2122020-04-15 16:06:11 +02001153 warned |= WARN_ANY;
1154
Willy Tarreaubaaee002006-06-26 02:48:02 +02001155 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1156 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001157 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001158 va_end(argp);
1159 }
1160}
1161
1162/*
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001163 * Variant of _ha_diag_warning with va_list.
1164 * Use it only if MODE_DIAG has been previously checked.
1165 */
1166void _ha_vdiag_warning(const char *fmt, va_list argp)
1167{
Willy Tarreau9f903af2021-05-07 08:42:39 +02001168 print_message("DIAG", fmt, argp);
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001169}
1170
1171/*
1172 * Output a diagnostic warning.
1173 * Use it only if MODE_DIAG has been previously checked.
1174 */
1175void _ha_diag_warning(const char *fmt, ...)
1176{
1177 va_list argp;
1178
1179 va_start(argp, fmt);
1180 _ha_vdiag_warning(fmt, argp);
1181 va_end(argp);
1182}
1183
1184/*
1185 * Output a diagnostic warning. Do nothing of MODE_DIAG is not on.
1186 */
1187void ha_diag_warning(const char *fmt, ...)
1188{
1189 va_list argp;
1190
1191 if (global.mode & MODE_DIAG) {
1192 va_start(argp, fmt);
1193 _ha_vdiag_warning(fmt, argp);
1194 va_end(argp);
1195 }
1196}
1197
1198/*
William Lallemand9c56a222018-11-21 18:04:52 +01001199 * Displays the message on stderr with the date and pid.
1200 */
1201void ha_notice(const char *fmt, ...)
1202{
1203 va_list argp;
1204
1205 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1206 va_start(argp, fmt);
1207 print_message("NOTICE", fmt, argp);
1208 va_end(argp);
1209 }
1210}
1211
1212/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001213 * Displays the message on <out> only if quiet mode is not set.
1214 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001215void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001216{
1217 va_list argp;
1218
1219 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1220 va_start(argp, fmt);
1221 vfprintf(out, fmt, argp);
1222 fflush(out);
1223 va_end(argp);
1224 }
1225}
1226
1227/*
Emeric Brun54648852020-07-06 15:54:06 +02001228 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001229 */
Emeric Brun54648852020-07-06 15:54:06 +02001230enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001231{
Emeric Brun54648852020-07-06 15:54:06 +02001232 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001233
1234 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001235 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001236 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001237 format--;
1238
Emeric Brun54648852020-07-06 15:54:06 +02001239 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001240 return format;
1241}
1242
1243/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244 * returns log level for <lev> or -1 if not found.
1245 */
1246int get_log_level(const char *lev)
1247{
1248 int level;
1249
1250 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001251 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001252 level--;
1253
1254 return level;
1255}
1256
Willy Tarreaubaaee002006-06-26 02:48:02 +02001257/*
1258 * returns log facility for <fac> or -1 if not found.
1259 */
1260int get_log_facility(const char *fac)
1261{
1262 int facility;
1263
1264 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001265 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001267
Willy Tarreaubaaee002006-06-26 02:48:02 +02001268 return facility;
1269}
1270
William Lallemanda1cc3812012-02-08 16:38:44 +01001271/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001272 * Encode the string.
1273 *
1274 * When using the +E log format option, it will try to escape '"\]'
1275 * characters with '\' as prefix. The same prefix should not be used as
1276 * <escape>.
1277 */
1278static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001279 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001280 const char *string,
1281 struct logformat_node *node)
1282{
1283 if (node->options & LOG_OPT_ESC) {
1284 if (start < stop) {
1285 stop--; /* reserve one byte for the final '\0' */
1286 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001287 if (!ha_bit_test((unsigned char)(*string), map)) {
1288 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001289 *start++ = *string;
1290 else {
1291 if (start + 2 >= stop)
1292 break;
1293 *start++ = '\\';
1294 *start++ = *string;
1295 }
1296 }
1297 else {
1298 if (start + 3 >= stop)
1299 break;
1300 *start++ = escape;
1301 *start++ = hextab[(*string >> 4) & 15];
1302 *start++ = hextab[*string & 15];
1303 }
1304 string++;
1305 }
1306 *start = '\0';
1307 }
1308 }
1309 else {
1310 return encode_string(start, stop, escape, map, string);
1311 }
1312
1313 return start;
1314}
1315
1316/*
1317 * Encode the chunk.
1318 *
1319 * When using the +E log format option, it will try to escape '"\]'
1320 * characters with '\' as prefix. The same prefix should not be used as
1321 * <escape>.
1322 */
1323static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001324 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001325 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001326 struct logformat_node *node)
1327{
1328 char *str, *end;
1329
1330 if (node->options & LOG_OPT_ESC) {
1331 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001332 str = chunk->area;
1333 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001334
1335 stop--; /* reserve one byte for the final '\0' */
1336 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001337 if (!ha_bit_test((unsigned char)(*str), map)) {
1338 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001339 *start++ = *str;
1340 else {
1341 if (start + 2 >= stop)
1342 break;
1343 *start++ = '\\';
1344 *start++ = *str;
1345 }
1346 }
1347 else {
1348 if (start + 3 >= stop)
1349 break;
1350 *start++ = escape;
1351 *start++ = hextab[(*str >> 4) & 15];
1352 *start++ = hextab[*str & 15];
1353 }
1354 str++;
1355 }
1356 *start = '\0';
1357 }
1358 }
1359 else {
1360 return encode_chunk(start, stop, escape, map, chunk);
1361 }
1362
1363 return start;
1364}
1365
1366/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001367 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001368 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001369 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001370 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001371 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001372char *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 +01001373{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001374 if (size < 2)
1375 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001376
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001377 if (node->options & LOG_OPT_QUOTE) {
1378 *(dst++) = '"';
1379 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001380 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001381
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001382 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001383 if (++len > size)
1384 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001385 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001386 char *ret;
1387
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001388 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001389 if (ret == NULL || *ret != '\0')
1390 return NULL;
1391 len = ret - dst;
1392 }
1393 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001394 len = strlcpy2(dst, src, len);
1395 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001396
1397 size -= len;
1398 dst += len;
1399 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001400 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1401 if (size < 2)
1402 return NULL;
1403 *(dst++) = '-';
1404 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001405
1406 if (node->options & LOG_OPT_QUOTE) {
1407 if (size < 2)
1408 return NULL;
1409 *(dst++) = '"';
1410 }
1411
1412 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001413 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001414}
1415
Willy Tarreau26ffa852018-09-05 15:23:10 +02001416static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001417{
1418 return lf_text_len(dst, src, size, size, node);
1419}
1420
William Lallemand5f232402012-04-05 18:02:55 +02001421/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001422 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001423 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001424 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001425char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001426{
1427 char *ret = dst;
1428 int iret;
1429 char pn[INET6_ADDRSTRLEN];
1430
1431 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001432 unsigned char *addr = NULL;
1433 switch (sockaddr->sa_family) {
1434 case AF_INET:
1435 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1436 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1437 break;
1438 case AF_INET6:
1439 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1440 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1441 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1442 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1443 break;
1444 default:
1445 return NULL;
1446 }
William Lallemand5f232402012-04-05 18:02:55 +02001447 if (iret < 0 || iret > size)
1448 return NULL;
1449 ret += iret;
1450 } else {
1451 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1452 ret = lf_text(dst, pn, size, node);
1453 if (ret == NULL)
1454 return NULL;
1455 }
1456 return ret;
1457}
1458
1459/*
1460 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001461 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001462 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001463char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001464{
1465 char *ret = dst;
1466 int iret;
1467
1468 if (node->options & LOG_OPT_HEXA) {
1469 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1470 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1471 if (iret < 0 || iret > size)
1472 return NULL;
1473 ret += iret;
1474 } else {
1475 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1476 if (ret == NULL)
1477 return NULL;
1478 }
1479 return ret;
1480}
1481
Emeric Brun54648852020-07-06 15:54:06 +02001482
1483/*
1484 * This function sends the syslog message using a printf format string. It
1485 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001486 */
Emeric Brun54648852020-07-06 15:54:06 +02001487void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001488{
Emeric Brun54648852020-07-06 15:54:06 +02001489 va_list argp;
1490 int data_len;
1491
1492 if (level < 0 || format == NULL || logline == NULL)
1493 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001494
Emeric Brun54648852020-07-06 15:54:06 +02001495 va_start(argp, format);
1496 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1497 if (data_len < 0 || data_len > global.max_syslog_len)
1498 data_len = global.max_syslog_len;
1499 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001500
Emeric Brun54648852020-07-06 15:54:06 +02001501 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1502 logline, data_len, default_rfc5424_sd_log_format, 2);
1503}
1504/*
1505 * This function builds a log header of given format using given
1506 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1507 * to determine format based on given metadas. It is useful
1508 * for log-forwarding to be able to forward any format without
1509 * settings.
1510 * This function returns a struct ist array of elements of the header
1511 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001512 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001513 * elements.
1514 */
1515struct ist *build_log_header(enum log_fmt format, int level, int facility,
1516 struct ist *metadata, size_t *nbelem)
1517{
1518 static THREAD_LOCAL struct {
1519 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1520 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1521 time_t cur_legacy_time;
1522 char priority_buffer[6];
1523 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001524
Emeric Brun54648852020-07-06 15:54:06 +02001525 struct tm logtime;
1526 int len;
1527 int fac_level = 0;
1528 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001529
Emeric Brun54648852020-07-06 15:54:06 +02001530 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001531
Emeric Brun54648852020-07-06 15:54:06 +02001532
1533 if (format == LOG_FORMAT_UNSPEC) {
1534 format = LOG_FORMAT_RAW;
1535 if (metadata) {
1536 /* If a hostname is set, it appears we want to perform syslog
1537 * because only rfc5427 or rfc3164 support an hostname.
1538 */
1539 if (metadata[LOG_META_HOST].len) {
1540 /* If a rfc5424 compliant timestamp is used we consider
1541 * that output format is rfc5424, else legacy format
1542 * is used as specified default for local logs
1543 * in documentation.
1544 */
1545 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1546 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1547 format = LOG_FORMAT_RFC5424;
1548 else
1549 format = LOG_FORMAT_RFC3164;
1550 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001551 else if (metadata[LOG_META_TAG].len) {
1552 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001553 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001554 * in legacy format (analog to RFC3164 but
1555 * with stripped hostname).
1556 */
1557 format = LOG_FORMAT_LOCAL;
1558 }
Emeric Brun54648852020-07-06 15:54:06 +02001559 else if (metadata[LOG_META_PRIO].len) {
1560 /* the source seems a parsed message
1561 * offering a valid level/prio prefix
1562 * so we consider this format.
1563 */
1564 format = LOG_FORMAT_PRIO;
1565 }
1566 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001567 }
1568
Emeric Brun54648852020-07-06 15:54:06 +02001569 /* prepare priority, stored into 1 single elem */
1570 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001571 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001572 case LOG_FORMAT_RFC3164:
1573 case LOG_FORMAT_RFC5424:
1574 case LOG_FORMAT_PRIO:
1575 fac_level = facility << 3;
1576 /* further format ignore the facility */
1577 /* fall through */
1578 case LOG_FORMAT_TIMED:
1579 case LOG_FORMAT_SHORT:
1580 fac_level += level;
1581 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1582 do {
1583 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1584 fac_level /= 10;
1585 hdr_ctx.ist_vector[*nbelem].ptr--;
1586 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1587 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1588 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1589 break;
1590 case LOG_FORMAT_ISO:
1591 case LOG_FORMAT_RAW:
1592 break;
1593 case LOG_FORMAT_UNSPEC:
1594 case LOG_FORMATS:
1595 ABORT_NOW();
1596 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001597
William Lallemand2a4a44f2012-02-06 16:00:33 +01001598
Emeric Brun54648852020-07-06 15:54:06 +02001599 /* prepare timestamp, stored into a max of 4 elems */
1600 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001601 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001602 case LOG_FORMAT_RFC3164:
1603 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1604 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1605 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1606 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001607 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001608 break;
1609 }
1610 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1611 int month;
1612 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001613
Emeric Brun54648852020-07-06 15:54:06 +02001614 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001615
Emeric Brun54648852020-07-06 15:54:06 +02001616 /* compute month */
1617 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1618 if (month)
1619 month--;
1620 if (month <= 11) {
1621 /* builds log prefix ex: 'Jan 1 ' */
1622 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1623 "%s %c%c ", monthname[month],
1624 timestamp[8] != '0' ? timestamp[8] : ' ',
1625 timestamp[9]);
1626 /* we reused the timestamp_buffer, signal that it does not
1627 * contain local time anymore
1628 */
1629 hdr_ctx.cur_legacy_time = 0;
1630 if (len == 7) {
1631 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1632 /* adds 'HH:MM:SS' from iso time */
1633 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1634 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1635 /* we successfully reuse iso time, we can break */
1636 break;
1637 }
1638 }
1639 /* Failed to reuse isotime time, fallback to local legacy time */
1640 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001641
Emeric Brun54648852020-07-06 15:54:06 +02001642 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1643 /* re-builds timestamp from the current local time */
1644 get_localtime(time, &logtime);
1645
1646 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1647 "%s %2d %02d:%02d:%02d ",
1648 monthname[logtime.tm_mon],
1649 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1650 if (len != LOG_LEGACYTIME_LEN+1)
1651 hdr_ctx.cur_legacy_time = 0;
1652 else
1653 hdr_ctx.cur_legacy_time = time;
1654 }
1655 if (likely(hdr_ctx.cur_legacy_time))
1656 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1657 else
1658 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1659 break;
1660 case LOG_FORMAT_RFC5424:
1661 /* adds rfc5425 version prefix */
1662 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1663 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001664 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001665 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1666 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1667 break;
1668 }
1669 /* let continue as 'timed' and 'iso' format for usual timestamp */
1670 /* fall through */
1671 case LOG_FORMAT_TIMED:
1672 case LOG_FORMAT_ISO:
1673 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1674 * '1900:01:01T14:00:00+02:00'
1675 * '1900:01:01T10:00:00.123456-02:00'
1676 */
1677 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1678 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1679 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001680 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001681 break;
1682 }
1683 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1684 int month;
1685 char *timestamp = metadata[LOG_META_TIME].ptr;
1686
1687 for (month = 0; month < 12; month++)
1688 if (!memcmp(monthname[month], timestamp, 3))
1689 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001690
Emeric Brun54648852020-07-06 15:54:06 +02001691 if (month < 12) {
1692
1693 /* get local time to retrieve year */
1694 get_localtime(time, &logtime);
1695
1696 /* year seems changed since log */
1697 if (logtime.tm_mon < month)
1698 logtime.tm_year--;
1699
1700 /* builds rfc5424 prefix ex: '1900-01-01T' */
1701 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1702 "%4d-%02d-%c%cT",
1703 logtime.tm_year+1900, month+1,
1704 timestamp[4] != ' ' ? timestamp[4] : '0',
1705 timestamp[5]);
1706
1707 /* we reused the timestamp_buffer, signal that it does not
1708 * contain local time anymore
1709 */
1710 hdr_ctx.cur_legacy_time = 0;
1711 if (len == 11) {
1712 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1713 /* adds HH:MM:SS from legacy timestamp */
1714 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001715 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001716 /* according to rfc: -00:00 means we don't know the timezone */
1717 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1718 /* we successfully reuse legacy time, we can break */
1719 break;
1720 }
1721 }
1722 /* Failed to reuse legacy time, fallback to local iso time */
1723 }
1724 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1725 break;
1726 case LOG_FORMAT_PRIO:
1727 case LOG_FORMAT_SHORT:
1728 case LOG_FORMAT_RAW:
1729 break;
1730 case LOG_FORMAT_UNSPEC:
1731 case LOG_FORMATS:
1732 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001733 }
1734
Emeric Brun54648852020-07-06 15:54:06 +02001735 /* prepare other meta data, stored into a max of 10 elems */
1736 switch (format) {
1737 case LOG_FORMAT_RFC3164:
1738 if (metadata && metadata[LOG_META_HOST].len) {
1739 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1740 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1741 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001742 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001743 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001744 /* fall through */
1745 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001746 if (!metadata || !metadata[LOG_META_TAG].len)
1747 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001748
Emeric Brun54648852020-07-06 15:54:06 +02001749 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1750 if (metadata[LOG_META_PID].len) {
1751 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1752 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1753 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1754 }
1755 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1756 break;
1757 case LOG_FORMAT_RFC5424:
1758 if (metadata && metadata[LOG_META_HOST].len) {
1759 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1760 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1761 }
1762 else
1763 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001764
Emeric Brun54648852020-07-06 15:54:06 +02001765 if (metadata && metadata[LOG_META_TAG].len) {
1766 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1767 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1768 }
1769 else
1770 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001771
Emeric Brun54648852020-07-06 15:54:06 +02001772 if (metadata && metadata[LOG_META_PID].len) {
1773 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1774 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1775 }
1776 else
1777 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001778
Emeric Brun54648852020-07-06 15:54:06 +02001779 if (metadata && metadata[LOG_META_MSGID].len) {
1780 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1781 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1782 }
1783 else
1784 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001785
Emeric Brun54648852020-07-06 15:54:06 +02001786 if (metadata && metadata[LOG_META_STDATA].len) {
1787 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1788 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1789 }
1790 else
1791 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1792 break;
1793 case LOG_FORMAT_PRIO:
1794 case LOG_FORMAT_SHORT:
1795 case LOG_FORMAT_TIMED:
1796 case LOG_FORMAT_ISO:
1797 case LOG_FORMAT_RAW:
1798 break;
1799 case LOG_FORMAT_UNSPEC:
1800 case LOG_FORMATS:
1801 ABORT_NOW();
1802 }
1803
1804 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001805}
1806
1807/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001808 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001809 * The argument <metadata> MUST be an array of size
1810 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001811 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001812 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001813 */
Emeric Brun54648852020-07-06 15:54:06 +02001814static 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 +01001815{
Emeric Brun54648852020-07-06 15:54:06 +02001816 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001817 static THREAD_LOCAL struct msghdr msghdr = {
1818 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001819 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001820 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001821 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1822 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001823 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001824 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001825 size_t nbelem;
1826 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001827
1828 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001829
Emeric Brunfa9d7802020-05-28 14:21:33 +02001830 /* historically some messages used to already contain the trailing LF
1831 * or Zero. Let's remove all trailing LF or Zero
1832 */
Emeric Brun54648852020-07-06 15:54:06 +02001833 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001834 size--;
1835
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001836 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001837 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001838 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001839 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001840 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1841 /* the socket's address is a file descriptor */
1842 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1843 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001844 else if (logsrv->addr.ss_family == AF_UNIX)
1845 plogfd = &logfdunix;
1846 else
1847 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001848
Willy Tarreauc046d162019-08-30 15:24:59 +02001849 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001850 /* socket not successfully initialized yet */
1851 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1852 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1853 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001854
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001855 if (!once) {
1856 once = 1; /* note: no need for atomic ops here */
1857 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1858 nblogger, strerror(errno), errno);
1859 }
1860 return;
1861 } else {
1862 /* we don't want to receive anything on this socket */
1863 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1864 /* does nothing under Linux, maybe needed for others */
1865 shutdown(*plogfd, SHUT_RD);
1866 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1867 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001868 }
1869
Emeric Brun54648852020-07-06 15:54:06 +02001870 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1871 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001872 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001873 struct ist msg;
1874
1875 msg = ist2(message, size);
1876 if (msg.len > logsrv->maxlen)
1877 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001878
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001879 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1880 }
1881 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1882 struct ist msg;
1883
1884 msg = ist2(message, size);
1885 if (msg.len > logsrv->maxlen)
1886 msg.len = logsrv->maxlen;
1887
1888 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001889 }
1890 else {
Emeric Brun54648852020-07-06 15:54:06 +02001891 int i = 0;
1892 int totlen = logsrv->maxlen;
1893
1894 for (i = 0 ; i < nbelem ; i++ ) {
1895 iovec[i].iov_base = msg_header[i].ptr;
1896 iovec[i].iov_len = msg_header[i].len;
1897 if (totlen <= iovec[i].iov_len) {
1898 iovec[i].iov_len = totlen;
1899 totlen = 0;
1900 break;
1901 }
1902 totlen -= iovec[i].iov_len;
1903 }
1904 if (totlen) {
1905 iovec[i].iov_base = message;
1906 iovec[i].iov_len = size;
1907 if (totlen <= iovec[i].iov_len)
1908 iovec[i].iov_len = totlen;
1909 i++;
1910 }
1911 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1912 iovec[i].iov_len = 1;
1913 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001914
Emeric Brun54648852020-07-06 15:54:06 +02001915 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001916 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1917 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001918
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001919 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1920 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001921
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001922 if (sent < 0) {
1923 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001924
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001925 if (errno == EAGAIN)
Willy Tarreau4781b152021-04-06 13:53:36 +02001926 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001927 else if (!once) {
1928 once = 1; /* note: no need for atomic ops here */
1929 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1930 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001931 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001932 }
1933}
Dragan Dosen59cee972015-09-19 22:09:02 +02001934
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001935/*
1936 * This function sends a syslog message.
1937 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001938 * The argument <metadata> MUST be an array of size
1939 * LOG_META_FIELDS*sizeof(struct ist) containing
1940 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001941 */
Emeric Brun54648852020-07-06 15:54:06 +02001942void process_send_log(struct list *logsrvs, int level, int facility,
1943 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001944{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001945 struct logsrv *logsrv;
1946 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001947
1948 /* Send log messages to syslog server. */
1949 nblogger = 0;
1950 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001951 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001952
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001953 /* we can filter the level of the messages that are sent to each logger */
1954 if (level > logsrv->level)
1955 continue;
1956
Frédéric Lécailled803e472019-04-25 07:42:09 +02001957 if (logsrv->lb.smp_rgs) {
1958 struct smp_log_range *curr_rg;
1959
1960 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1961 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1962 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1963 if (in_range) {
1964 /* Let's consume this range. */
1965 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1966 if (!curr_rg->curr_idx) {
1967 /* If consumed, let's select the next range. */
1968 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1969 }
1970 }
1971 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1972 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1973 }
1974 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001975 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1976 (facility == -1) ? logsrv->facility : facility,
1977 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001978 }
1979}
1980
Emeric Brun54648852020-07-06 15:54:06 +02001981/*
1982 * This function sends a syslog message.
1983 * It doesn't care about errors nor does it report them.
1984 * The arguments <sd> and <sd_size> are used for the structured-data part
1985 * in RFC5424 formatted syslog messages.
1986 */
1987void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1988 char *message, size_t size, char *sd, size_t sd_size)
1989{
1990 static THREAD_LOCAL pid_t curr_pid;
1991 static THREAD_LOCAL char pidstr[16];
1992 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1993
1994 if (logsrvs == NULL) {
1995 if (!LIST_ISEMPTY(&global.logsrvs)) {
1996 logsrvs = &global.logsrvs;
1997 }
1998 }
1999 if (!logsrvs || LIST_ISEMPTY(logsrvs))
2000 return;
2001
2002 if (!metadata[LOG_META_HOST].len) {
2003 if (global.log_send_hostname)
2004 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02002005 }
2006
2007 if (!tagb || !tagb->area)
2008 tagb = &global.log_tag;
2009
2010 if (tagb)
2011 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
2012
2013 if (unlikely(curr_pid != getpid()))
2014 metadata[LOG_META_PID].len = 0;
2015
2016 if (!metadata[LOG_META_PID].len) {
2017 curr_pid = getpid();
2018 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
2019 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
2020 }
2021
2022 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
2023
2024 /* Remove trailing space of structured data */
2025 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
2026 metadata[LOG_META_STDATA].len--;
2027
2028 return process_send_log(logsrvs, level, -1, metadata, message, size);
2029}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002030
Willy Tarreauc89ccb62012-04-05 21:18:22 +02002031const 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 +01002032const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
2033 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
2034 Set-cookie Updated, unknown, unknown */
2035
William Lallemand1d705562012-03-12 12:46:41 +01002036/*
2037 * try to write a character if there is enough space, or goto out
2038 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01002039#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01002040 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01002041 *(tmplog++) = (x); \
2042 } else { \
2043 goto out; \
2044 } \
2045 } while(0)
2046
Dragan Dosen835b9212016-02-12 13:23:03 +01002047
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002048/* Initializes some log data at boot */
2049static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01002050{
2051 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02002052 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01002053
2054 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
2055 * inside PARAM-VALUE should be escaped with '\' as prefix.
2056 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
2057 * details.
2058 */
2059 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
2060
2061 tmp = "\"\\]";
2062 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002063 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002064 tmp++;
2065 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02002066
2067 /* initialize the log header encoding map : '{|}"#' should be encoded with
2068 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
2069 * URL encoding only requires '"', '#' to be encoded as well as non-
2070 * printable characters above.
2071 */
2072 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
2073 memset(url_encode_map, 0, sizeof(url_encode_map));
2074 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002075 ha_bit_set(i, hdr_encode_map);
2076 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002077 }
2078 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002079 ha_bit_set(i, hdr_encode_map);
2080 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002081 }
2082
2083 tmp = "\"#{|}";
2084 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002085 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002086 tmp++;
2087 }
2088
2089 tmp = "\"#";
2090 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002091 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002092 tmp++;
2093 }
2094
2095 /* initialize the http header encoding map. The draft httpbis define the
2096 * header content as:
2097 *
2098 * HTTP-message = start-line
2099 * *( header-field CRLF )
2100 * CRLF
2101 * [ message-body ]
2102 * header-field = field-name ":" OWS field-value OWS
2103 * field-value = *( field-content / obs-fold )
2104 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2105 * obs-fold = CRLF 1*( SP / HTAB )
2106 * field-vchar = VCHAR / obs-text
2107 * VCHAR = %x21-7E
2108 * obs-text = %x80-FF
2109 *
2110 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
2111 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08002112 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02002113 */
2114 memset(http_encode_map, 0, sizeof(http_encode_map));
2115 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002116 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002117 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002118 ha_bit_set(i, http_encode_map);
2119 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002120}
William Lallemand1d705562012-03-12 12:46:41 +01002121
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002122INITCALL0(STG_PREPARE, init_log);
2123
Christopher Faulet0132d062017-07-26 15:33:35 +02002124/* Initialize log buffers used for syslog messages */
2125int init_log_buffers()
2126{
Christopher Faulet0132d062017-07-26 15:33:35 +02002127 logline = my_realloc2(logline, global.max_syslog_len + 1);
2128 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02002129 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02002130 return 0;
2131 return 1;
2132}
2133
2134/* Deinitialize log buffers used for syslog messages */
2135void deinit_log_buffers()
2136{
Christopher Faulet0132d062017-07-26 15:33:35 +02002137 free(logline);
2138 free(logline_rfc5424);
Willy Tarreau869efd52019-11-15 15:16:57 +01002139 ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
Christopher Faulet0132d062017-07-26 15:33:35 +02002140 logline = NULL;
2141 logline_rfc5424 = NULL;
2142}
2143
Willy Tarreaudf974472012-12-28 02:44:01 +01002144/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2145 * <maxsize> characters. Returns the size of the output string in characters,
2146 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002147 * is not zero. It requires a valid session and optionally a stream. If the
2148 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002149 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002150int 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 +02002151{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002152 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002153 struct proxy *be;
2154 struct http_txn *txn;
2155 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002156 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002157 unsigned int s_flags;
2158 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002159 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002160 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002161 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002162 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002163 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002164 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002165 int t_request;
2166 int hdr;
2167 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002168 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002169 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002170 char *ret;
2171 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002172 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002173 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002174 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002175 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002176 struct ist path;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002177
William Lallemandbddd4fd2012-02-27 11:23:10 +01002178 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002179
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002180 if (likely(s)) {
2181 be = s->be;
2182 txn = s->txn;
2183 be_conn = cs_conn(objt_cs(s->si[1].end));
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002184 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002185 s_flags = s->flags;
2186 uniq_id = s->uniq_id;
2187 logs = &s->logs;
2188 } else {
2189 /* we have no stream so we first need to initialize a few
2190 * things that are needed later. We do increment the request
2191 * ID so that it's uniquely assigned to this request just as
2192 * if the request had reached the point of being processed.
2193 * A request error is reported as it's the only element we have
2194 * here and which justifies emitting such a log.
2195 */
Christopher Fauletfd818482021-04-14 14:01:41 +02002196 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002197 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002198 fe_conn = objt_conn(sess->origin);
Christopher Fauletfd818482021-04-14 14:01:41 +02002199 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? cs_conn(__objt_check(sess->origin)->cs) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002200 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002201 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02002202 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002203
2204 /* prepare a valid log structure */
2205 tmp_strm_log.tv_accept = sess->tv_accept;
2206 tmp_strm_log.accept_date = sess->accept_date;
2207 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02002208 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002209 tv_zero(&tmp_strm_log.tv_request);
2210 tmp_strm_log.t_queue = -1;
2211 tmp_strm_log.t_connect = -1;
2212 tmp_strm_log.t_data = -1;
2213 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2214 tmp_strm_log.bytes_in = 0;
2215 tmp_strm_log.bytes_out = 0;
2216 tmp_strm_log.prx_queue_pos = 0;
2217 tmp_strm_log.srv_queue_pos = 0;
2218
2219 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002220
2221 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
2222 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, NULL);
2223
2224 switch (es) {
2225 case MUX_ES_SUCCESS:
2226 break;
2227 case MUX_ES_INVALID_ERR:
2228 status = 400;
2229 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2230 s_flags = SF_ERR_CLICL | SF_FINST_R;
2231 else
2232 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2233 break;
2234 case MUX_ES_TOUT_ERR:
2235 status = 408;
2236 s_flags = SF_ERR_CLITO | SF_FINST_R;
2237 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002238 case MUX_ES_NOTIMPL_ERR:
2239 status = 501;
2240 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2241 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002242 case MUX_ES_INTERNAL_ERR:
2243 status = 500;
2244 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2245 break;
2246 default:
2247 break;
2248 }
2249 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002250 }
2251
William Lallemandbddd4fd2012-02-27 11:23:10 +01002252 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002253 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2254 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002255
William Lallemand1d705562012-03-12 12:46:41 +01002256 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002257
William Lallemandbddd4fd2012-02-27 11:23:10 +01002258 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002259 if (LIST_ISEMPTY(list_format))
2260 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002261
William Lallemand1d705562012-03-12 12:46:41 +01002262 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002263 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002264 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002265 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002266 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002267
Willy Tarreauc8368452012-12-21 00:09:23 +01002268 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002269 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002270 if (!last_isspace) {
2271 LOGCHAR(' ');
2272 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002273 }
2274 break;
2275
William Lallemand1d705562012-03-12 12:46:41 +01002276 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002277 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002278 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002279 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002280 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002281 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002282 last_isspace = 0;
2283 break;
2284
Willy Tarreauc8368452012-12-21 00:09:23 +01002285 case LOG_FMT_EXPR: // sample expression, may be request or response
2286 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002287 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002288 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 +02002289 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002290 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 +01002291 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002292 ret = lf_encode_chunk(tmplog, dst + maxsize,
2293 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002294 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002295 ret = lf_text_len(tmplog,
2296 key ? key->data.u.str.area : NULL,
2297 key ? key->data.u.str.data : 0,
2298 dst + maxsize - tmplog,
2299 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002300 if (ret == 0)
2301 goto out;
2302 tmplog = ret;
2303 last_isspace = 0;
2304 break;
2305
Willy Tarreau2beef582012-12-20 17:22:52 +01002306 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002307 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002308 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002309 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002310 else
2311 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002312 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002313 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002314 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002315 last_isspace = 0;
2316 break;
2317
Willy Tarreau2beef582012-12-20 17:22:52 +01002318 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002319 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002320 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002321 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002322 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002323 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002324 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002325 dst + maxsize - tmplog, tmp);
2326 }
William Lallemand5f232402012-04-05 18:02:55 +02002327 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002328 else
2329 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2330
William Lallemand5f232402012-04-05 18:02:55 +02002331 if (ret == NULL)
2332 goto out;
2333 tmplog = ret;
2334 last_isspace = 0;
2335 break;
2336
Willy Tarreau2beef582012-12-20 17:22:52 +01002337 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002338 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002339 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002340 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002341 }
2342 else
2343 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2344
William Lallemand1d705562012-03-12 12:46:41 +01002345 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002346 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002347 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002348 last_isspace = 0;
2349 break;
2350
Willy Tarreau2beef582012-12-20 17:22:52 +01002351 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002352 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002353 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002354 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002355 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002356 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002357 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002358 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002359 else
2360 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2361
William Lallemand5f232402012-04-05 18:02:55 +02002362 if (ret == NULL)
2363 goto out;
2364 tmplog = ret;
2365 last_isspace = 0;
2366 break;
2367
Willy Tarreau2beef582012-12-20 17:22:52 +01002368 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002369 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002370 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002371 else
2372 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2373
William Lallemand1d705562012-03-12 12:46:41 +01002374 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002375 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002376 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002377 last_isspace = 0;
2378 break;
2379
Willy Tarreau2beef582012-12-20 17:22:52 +01002380 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002381 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002382 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002383 else
2384 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2385
William Lallemand5f232402012-04-05 18:02:55 +02002386 if (ret == NULL)
2387 goto out;
2388 tmplog = ret;
2389 last_isspace = 0;
2390 break;
2391
Willy Tarreau2beef582012-12-20 17:22:52 +01002392 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002393 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002394 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002395 else
2396 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2397
William Lallemand5f232402012-04-05 18:02:55 +02002398 if (ret == NULL)
2399 goto out;
2400 tmplog = ret;
2401 last_isspace = 0;
2402 break;
2403
Willy Tarreau2beef582012-12-20 17:22:52 +01002404 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002405 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002406 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002407 else
2408 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2409
William Lallemand1d705562012-03-12 12:46:41 +01002410 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002411 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002412 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002413 last_isspace = 0;
2414 break;
2415
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002416 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002417 get_localtime(logs->accept_date.tv_sec, &tm);
2418 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002419 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002420 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002421 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002422 last_isspace = 0;
2423 break;
2424
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002425 case LOG_FMT_tr: // %tr = start of request date
2426 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002427 tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002428 get_localtime(tv.tv_sec, &tm);
2429 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2430 if (ret == NULL)
2431 goto out;
2432 tmplog = ret;
2433 last_isspace = 0;
2434 break;
2435
2436 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002437 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002438 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002439 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002440 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002441 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002442 last_isspace = 0;
2443 break;
2444
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002445 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002446 tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002447 get_gmtime(tv.tv_sec, &tm);
2448 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2449 if (ret == NULL)
2450 goto out;
2451 tmplog = ret;
2452 last_isspace = 0;
2453 break;
2454
2455 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002456 get_localtime(logs->accept_date.tv_sec, &tm);
2457 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002458 if (ret == NULL)
2459 goto out;
2460 tmplog = ret;
2461 last_isspace = 0;
2462 break;
2463
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002464 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002465 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 +02002466 get_localtime(tv.tv_sec, &tm);
2467 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2468 if (ret == NULL)
2469 goto out;
2470 tmplog = ret;
2471 last_isspace = 0;
2472 break;
2473
William Lallemand5f232402012-04-05 18:02:55 +02002474 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002475 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002476 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002477 if (iret < 0 || iret > dst + maxsize - tmplog)
2478 goto out;
2479 last_isspace = 0;
2480 tmplog += iret;
2481 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002482 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002483 if (ret == NULL)
2484 goto out;
2485 tmplog = ret;
2486 last_isspace = 0;
2487 }
2488 break;
2489
William Lallemand1d705562012-03-12 12:46:41 +01002490 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002491 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002492 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002493 if (iret < 0 || iret > dst + maxsize - tmplog)
2494 goto out;
2495 last_isspace = 0;
2496 tmplog += iret;
2497 } else {
2498 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002499 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002500 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002501 tmplog, 4);
2502 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002503 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002504 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002505 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002506 }
2507 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002508
William Lallemand1d705562012-03-12 12:46:41 +01002509 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002510 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002511 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002512 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002513 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002514 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002515 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002516 break;
2517
Willy Tarreau773d65f2012-10-12 14:56:11 +02002518 case LOG_FMT_FRONTEND_XPRT: // %ft
2519 src = fe->id;
2520 if (tmp->options & LOG_OPT_QUOTE)
2521 LOGCHAR('"');
2522 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2523 if (iret == 0)
2524 goto out;
2525 tmplog += iret;
Christopher Fauletfd818482021-04-14 14:01:41 +02002526 if (sess->listener && sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002527 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002528 if (tmp->options & LOG_OPT_QUOTE)
2529 LOGCHAR('"');
2530 last_isspace = 0;
2531 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002532#ifdef USE_OPENSSL
2533 case LOG_FMT_SSL_CIPHER: // %sslc
2534 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002535 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002536 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002537 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002538 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002539 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2540 if (ret == NULL)
2541 goto out;
2542 tmplog = ret;
2543 last_isspace = 0;
2544 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002545
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002546 case LOG_FMT_SSL_VERSION: // %sslv
2547 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002548 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002549 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002550 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002551 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002552 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2553 if (ret == NULL)
2554 goto out;
2555 tmplog = ret;
2556 last_isspace = 0;
2557 break;
2558#endif
William Lallemand1d705562012-03-12 12:46:41 +01002559 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002560 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002561 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002562 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002563 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002564 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002565 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002566 break;
2567
William Lallemand1d705562012-03-12 12:46:41 +01002568 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002569 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002570 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002571 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002572 break;
2573 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002574 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002575 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002576 case OBJ_TYPE_CHECK:
2577 src = (__objt_check(sess->origin)->server
2578 ? __objt_check(sess->origin)->server->id
2579 : "<NOSRV>");
2580 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002581 default:
2582 src = "<NOSRV>";
2583 break;
2584 }
William Lallemand5f232402012-04-05 18:02:55 +02002585 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002586 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002587 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002588 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002589 last_isspace = 0;
2590 break;
2591
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002592 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002593 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002594 if (ret == NULL)
2595 goto out;
2596 tmplog = ret;
2597 last_isspace = 0;
2598 break;
2599
2600 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002601 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002602 if (ret == NULL)
2603 goto out;
2604 tmplog = ret;
2605 last_isspace = 0;
2606 break;
2607
2608 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002609 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002610 tmplog, dst + maxsize - tmplog);
2611 if (ret == NULL)
2612 goto out;
2613 tmplog = ret;
2614 last_isspace = 0;
2615 break;
2616
2617 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002618 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002619 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002620 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002621 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002622 last_isspace = 0;
2623 break;
2624
William Lallemand1d705562012-03-12 12:46:41 +01002625 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002626 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002627 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002628 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002629 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002630 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002631 last_isspace = 0;
2632 break;
2633
William Lallemand1d705562012-03-12 12:46:41 +01002634 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002635 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002636 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
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002643 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002644 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002645 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002646 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002647 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002648 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002649 last_isspace = 0;
2650 break;
2651
Willy Tarreau27b639d2016-05-17 17:55:27 +02002652 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002653 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002654 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002655 tmplog, dst + maxsize - tmplog);
2656 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002657 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002658 tmplog, dst + maxsize - tmplog);
2659 if (ret == NULL)
2660 goto out;
2661 tmplog = ret;
2662 last_isspace = 0;
2663 break;
2664
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002665 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2666 if (!(fe->to_log & LW_BYTES))
2667 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002668 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 +02002669 tmplog, dst + maxsize - tmplog);
2670 if (ret == NULL)
2671 goto out;
2672 tmplog = ret;
2673 last_isspace = 0;
2674 break;
2675
2676 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002677 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002678 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002679 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002680 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002681 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002682 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002683 last_isspace = 0;
2684 break;
2685
Damien Claisse57c8eb92020-04-28 12:09:19 +00002686 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2687 if (!(fe->to_log & LW_BYTES))
2688 LOGCHAR('+');
2689 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2690 tmplog, dst + maxsize - tmplog);
2691 if (ret == NULL)
2692 goto out;
2693 tmplog = ret;
2694 last_isspace = 0;
2695 break;
2696
Willy Tarreau2beef582012-12-20 17:22:52 +01002697 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002698 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002699 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002700 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002701 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002702 last_isspace = 0;
2703 break;
2704
William Lallemand1d705562012-03-12 12:46:41 +01002705 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002706 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002707 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002708 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002709 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002710 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002711 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002712 last_isspace = 0;
2713 break;
2714
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002715 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002716 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002717 if (ret == NULL)
2718 goto out;
2719 tmplog = ret;
2720 last_isspace = 0;
2721 break;
2722
Willy Tarreau2beef582012-12-20 17:22:52 +01002723 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002724 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002725 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002726 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002727 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002728 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002729 last_isspace = 0;
2730 break;
2731
Willy Tarreau2beef582012-12-20 17:22:52 +01002732 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002733 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002734 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002735 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002736 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002737 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002738 last_isspace = 0;
2739 break;
2740
William Lallemand1d705562012-03-12 12:46:41 +01002741 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002742 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2743 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002744 *tmplog = '\0';
2745 last_isspace = 0;
2746 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002747
William Lallemand1d705562012-03-12 12:46:41 +01002748 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002749 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2750 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002751 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2752 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 +01002753 last_isspace = 0;
2754 break;
2755
William Lallemand1d705562012-03-12 12:46:41 +01002756 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002757 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002758 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002759 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002760 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002761 last_isspace = 0;
2762 break;
2763
William Lallemand1d705562012-03-12 12:46:41 +01002764 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002765 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002766 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002767 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002768 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002769 last_isspace = 0;
2770 break;
2771
William Lallemand1d705562012-03-12 12:46:41 +01002772 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002773 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002774 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002775 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002776 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002777 last_isspace = 0;
2778 break;
2779
William Lallemand1d705562012-03-12 12:46:41 +01002780 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002781 switch (obj_type(s ? s->target : sess->origin)) {
2782 case OBJ_TYPE_SERVER:
2783 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2784 tmplog, dst + maxsize - tmplog);
2785 break;
2786 case OBJ_TYPE_CHECK:
2787 ret = ultoa_o(__objt_check(sess->origin)->server
2788 ? __objt_check(sess->origin)->server->cur_sess
2789 : 0, tmplog, dst + maxsize - tmplog);
2790 break;
2791 default:
2792 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2793 break;
2794 }
2795
William Lallemand1d705562012-03-12 12:46:41 +01002796 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002797 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002798 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002799 last_isspace = 0;
2800 break;
2801
William Lallemand1d705562012-03-12 12:46:41 +01002802 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002803 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002804 LOGCHAR('+');
Christopher Faulet1d26f222021-04-16 11:24:20 +02002805 ret = ltoa_o(((s && s->si[1].conn_retries > 0)
2806 ? (be->conn_retries - s->si[1].conn_retries)
2807 : ((s && s->si[1].state != SI_ST_INI) ? be->conn_retries : 0)),
2808 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002809 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002810 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002811 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002812 last_isspace = 0;
2813 break;
2814
William Lallemand1d705562012-03-12 12:46:41 +01002815 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002816 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002817 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002818 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002819 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002820 last_isspace = 0;
2821 break;
2822
William Lallemand1d705562012-03-12 12:46:41 +01002823 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002824 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002825 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002826 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002827 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002828 last_isspace = 0;
2829 break;
2830
William Lallemand1d705562012-03-12 12:46:41 +01002831 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002832 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002833 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002834 if (tmp->options & LOG_OPT_QUOTE)
2835 LOGCHAR('"');
2836 LOGCHAR('{');
2837 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2838 if (hdr)
2839 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002840 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002841 ret = lf_encode_string(tmplog, dst + maxsize,
2842 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002843 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002844 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002845 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002846 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002847 }
2848 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002849 if (tmp->options & LOG_OPT_QUOTE)
2850 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002851 last_isspace = 0;
2852 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002853 break;
2854
William Lallemand1d705562012-03-12 12:46:41 +01002855 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002856 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002857 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002858 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2859 if (hdr > 0)
2860 LOGCHAR(' ');
2861 if (tmp->options & LOG_OPT_QUOTE)
2862 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002863 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002864 ret = lf_encode_string(tmplog, dst + maxsize,
2865 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002866 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002867 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002868 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002869 } else if (!(tmp->options & LOG_OPT_QUOTE))
2870 LOGCHAR('-');
2871 if (tmp->options & LOG_OPT_QUOTE)
2872 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002873 last_isspace = 0;
2874 }
2875 }
2876 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002877
William Lallemand1d705562012-03-12 12:46:41 +01002878
2879 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002880 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002881 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002882 if (tmp->options & LOG_OPT_QUOTE)
2883 LOGCHAR('"');
2884 LOGCHAR('{');
2885 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2886 if (hdr)
2887 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002888 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002889 ret = lf_encode_string(tmplog, dst + maxsize,
2890 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002891 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002892 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002893 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002894 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002895 }
2896 LOGCHAR('}');
2897 last_isspace = 0;
2898 if (tmp->options & LOG_OPT_QUOTE)
2899 LOGCHAR('"');
2900 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002901 break;
2902
William Lallemand1d705562012-03-12 12:46:41 +01002903 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002904 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002905 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002906 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2907 if (hdr > 0)
2908 LOGCHAR(' ');
2909 if (tmp->options & LOG_OPT_QUOTE)
2910 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002911 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002912 ret = lf_encode_string(tmplog, dst + maxsize,
2913 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002914 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002915 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002916 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002917 } else if (!(tmp->options & LOG_OPT_QUOTE))
2918 LOGCHAR('-');
2919 if (tmp->options & LOG_OPT_QUOTE)
2920 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002921 last_isspace = 0;
2922 }
2923 }
2924 break;
2925
William Lallemand1d705562012-03-12 12:46:41 +01002926 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002927 /* Request */
2928 if (tmp->options & LOG_OPT_QUOTE)
2929 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002930 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002931 ret = lf_encode_string(tmplog, dst + maxsize,
2932 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002933 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002934 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002935 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002936 if (tmp->options & LOG_OPT_QUOTE)
2937 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002938 last_isspace = 0;
2939 break;
William Lallemand5f232402012-04-05 18:02:55 +02002940
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002941 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002942 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002943
Willy Tarreaub7636d12015-06-17 19:58:02 +02002944 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002945 LOGCHAR('"');
2946
2947 end = uri + strlen(uri);
2948 // look for the first whitespace character
2949 while (uri < end && !HTTP_IS_SPHT(*uri))
2950 uri++;
2951
2952 // keep advancing past multiple spaces
2953 while (uri < end && HTTP_IS_SPHT(*uri)) {
2954 uri++; nspaces++;
2955 }
2956
2957 // look for first space or question mark after url
2958 spc = uri;
2959 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2960 spc++;
2961
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002962 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002963 chunk.area = "<BADREQ>";
2964 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002965 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002966 chunk.area = uri;
2967 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002968 }
2969
Dragan Dosen835b9212016-02-12 13:23:03 +01002970 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002971 if (ret == NULL || *ret != '\0')
2972 goto out;
2973
2974 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002975 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002976 LOGCHAR('"');
2977
2978 last_isspace = 0;
2979 break;
2980
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002981 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2982 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2983
2984 if (tmp->options & LOG_OPT_QUOTE)
2985 LOGCHAR('"');
2986
2987 end = uri + strlen(uri);
2988
2989 // look for the first whitespace character
2990 while (uri < end && !HTTP_IS_SPHT(*uri))
2991 uri++;
2992
2993 // keep advancing past multiple spaces
2994 while (uri < end && HTTP_IS_SPHT(*uri)) {
2995 uri++; nspaces++;
2996 }
2997
2998 // look for first space after url
2999 spc = uri;
3000 while (spc < end && !HTTP_IS_SPHT(*spc))
3001 spc++;
3002
Tim Duesterhus92c696e2021-02-28 16:11:36 +01003003 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00003004
3005 // extract relative path without query params from url
3006 path = iststop(http_get_path(path), '?');
3007 if (!txn || !txn->uri || nspaces == 0) {
3008 chunk.area = "<BADREQ>";
3009 chunk.data = strlen("<BADREQ>");
3010 } else {
3011 chunk.area = path.ptr;
3012 chunk.data = path.len;
3013 }
3014
3015 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
3016 if (ret == NULL || *ret != '\0')
3017 goto out;
3018
3019 tmplog = ret;
3020 if (tmp->options & LOG_OPT_QUOTE)
3021 LOGCHAR('"');
3022
3023 last_isspace = 0;
3024 break;
3025
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003026 case LOG_FMT_HTTP_QUERY: // %HQ
3027 if (tmp->options & LOG_OPT_QUOTE)
3028 LOGCHAR('"');
3029
Willy Tarreau57bc8912016-04-25 17:09:40 +02003030 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003031 chunk.area = "<BADREQ>";
3032 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003033 } else {
3034 uri = txn->uri;
3035 end = uri + strlen(uri);
3036 // look for the first question mark
3037 while (uri < end && *uri != '?')
3038 uri++;
3039
3040 qmark = uri;
3041 // look for first space or question mark after url
3042 while (uri < end && !HTTP_IS_SPHT(*uri))
3043 uri++;
3044
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003045 chunk.area = qmark;
3046 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003047 }
3048
Dragan Dosen835b9212016-02-12 13:23:03 +01003049 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00003050 if (ret == NULL || *ret != '\0')
3051 goto out;
3052
3053 tmplog = ret;
3054 if (tmp->options & LOG_OPT_QUOTE)
3055 LOGCHAR('"');
3056
3057 last_isspace = 0;
3058 break;
3059
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003060 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02003061 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003062
Willy Tarreaub7636d12015-06-17 19:58:02 +02003063 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003064 LOGCHAR('"');
3065
3066 end = uri + strlen(uri);
3067 // look for the first whitespace character
3068 while (uri < end && !HTTP_IS_SPHT(*uri))
3069 uri++;
3070
3071 // keep advancing past multiple spaces
3072 while (uri < end && HTTP_IS_SPHT(*uri)) {
3073 uri++; nspaces++;
3074 }
3075
3076 // look for first space after url
3077 spc = uri;
3078 while (spc < end && !HTTP_IS_SPHT(*spc))
3079 spc++;
3080
Willy Tarreau57bc8912016-04-25 17:09:40 +02003081 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003082 chunk.area = "<BADREQ>";
3083 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003084 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003085 chunk.area = uri;
3086 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003087 }
3088
Dragan Dosen835b9212016-02-12 13:23:03 +01003089 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003090 if (ret == NULL || *ret != '\0')
3091 goto out;
3092
3093 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003094 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003095 LOGCHAR('"');
3096
3097 last_isspace = 0;
3098 break;
3099
3100 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02003101 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003102 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003103 LOGCHAR('"');
3104
3105 end = uri + strlen(uri);
3106 // look for the first whitespace character
3107 spc = uri;
3108 while (spc < end && !HTTP_IS_SPHT(*spc))
3109 spc++;
3110
3111 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003112 chunk.area = "<BADREQ>";
3113 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003114 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003115 chunk.area = uri;
3116 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003117 }
3118
Dragan Dosen835b9212016-02-12 13:23:03 +01003119 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003120 if (ret == NULL || *ret != '\0')
3121 goto out;
3122
3123 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003124 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003125 LOGCHAR('"');
3126
3127 last_isspace = 0;
3128 break;
3129
3130 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02003131 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003132 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003133 LOGCHAR('"');
3134
3135 end = uri + strlen(uri);
3136 // look for the first whitespace character
3137 while (uri < end && !HTTP_IS_SPHT(*uri))
3138 uri++;
3139
3140 // keep advancing past multiple spaces
3141 while (uri < end && HTTP_IS_SPHT(*uri)) {
3142 uri++; nspaces++;
3143 }
3144
3145 // look for the next whitespace character
3146 while (uri < end && !HTTP_IS_SPHT(*uri))
3147 uri++;
3148
3149 // keep advancing past multiple spaces
3150 while (uri < end && HTTP_IS_SPHT(*uri))
3151 uri++;
3152
Willy Tarreau57bc8912016-04-25 17:09:40 +02003153 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003154 chunk.area = "<BADREQ>";
3155 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003156 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003157 chunk.area = "HTTP/0.9";
3158 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003159 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003160 chunk.area = uri;
3161 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003162 }
3163
Dragan Dosen835b9212016-02-12 13:23:03 +01003164 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003165 if (ret == NULL || *ret != '\0')
3166 goto out;
3167
3168 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003169 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003170 LOGCHAR('"');
3171
3172 last_isspace = 0;
3173 break;
3174
William Lallemand5f232402012-04-05 18:02:55 +02003175 case LOG_FMT_COUNTER: // %rt
3176 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003177 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02003178 if (iret < 0 || iret > dst + maxsize - tmplog)
3179 goto out;
3180 last_isspace = 0;
3181 tmplog += iret;
3182 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003183 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003184 if (ret == NULL)
3185 goto out;
3186 tmplog = ret;
3187 last_isspace = 0;
3188 }
3189 break;
3190
Willy Tarreau7346acb2014-08-28 15:03:15 +02003191 case LOG_FMT_LOGCNT: // %lc
3192 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003193 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003194 if (iret < 0 || iret > dst + maxsize - tmplog)
3195 goto out;
3196 last_isspace = 0;
3197 tmplog += iret;
3198 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003199 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003200 if (ret == NULL)
3201 goto out;
3202 tmplog = ret;
3203 last_isspace = 0;
3204 }
3205 break;
3206
William Lallemand5f232402012-04-05 18:02:55 +02003207 case LOG_FMT_HOSTNAME: // %H
3208 src = hostname;
3209 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3210 if (ret == NULL)
3211 goto out;
3212 tmplog = ret;
3213 last_isspace = 0;
3214 break;
3215
3216 case LOG_FMT_PID: // %pid
3217 if (tmp->options & LOG_OPT_HEXA) {
3218 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3219 if (iret < 0 || iret > dst + maxsize - tmplog)
3220 goto out;
3221 last_isspace = 0;
3222 tmplog += iret;
3223 } else {
3224 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3225 if (ret == NULL)
3226 goto out;
3227 tmplog = ret;
3228 last_isspace = 0;
3229 }
3230 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003231
3232 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003233 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003234 if (s)
3235 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3236 else
3237 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003238 if (ret == NULL)
3239 goto out;
3240 tmplog = ret;
3241 last_isspace = 0;
3242 break;
3243
William Lallemandbddd4fd2012-02-27 11:23:10 +01003244 }
3245 }
3246
3247out:
William Lallemand1d705562012-03-12 12:46:41 +01003248 /* *tmplog is a unused character */
3249 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003250 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003251
Willy Tarreaubaaee002006-06-26 02:48:02 +02003252}
3253
William Lallemand1d705562012-03-12 12:46:41 +01003254/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003255 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003256 * Will not log if the frontend has no log defined.
3257 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003258void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003259{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003260 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003261 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003262 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003263
3264 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003265 err = (s->flags & SF_REDISP) ||
3266 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3267 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003268 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003269 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003270
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003271 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003272 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003273
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003274 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003275 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003276
Willy Tarreauabcd5142013-06-11 17:18:02 +02003277 if (s->logs.level) { /* loglevel was overridden */
3278 if (s->logs.level == -1) {
3279 s->logs.logwait = 0; /* logs disabled */
3280 return;
3281 }
3282 level = s->logs.level - 1;
3283 }
3284 else {
3285 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003286 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003287 level = LOG_ERR;
3288 }
William Lallemand1d705562012-03-12 12:46:41 +01003289
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003290 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003291 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003292 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003293 }
3294
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003295 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3296 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3297 &sess->fe->logformat_sd);
3298 }
3299
Dragan Dosen59cee972015-09-19 22:09:02 +02003300 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003301 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003302 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003303 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3304 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003305 s->logs.logwait = 0;
3306 }
3307}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003308
Willy Tarreau53839352018-09-05 19:51:10 +02003309/*
3310 * send a minimalist log for the session. Will not log if the frontend has no
3311 * log defined. It is assumed that this is only used to report anomalies that
3312 * cannot lead to the creation of a regular stream. Because of this the log
3313 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3314 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003315 * function to report unimportant events. It is safe to call this function with
3316 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003317 */
3318void sess_log(struct session *sess)
3319{
3320 int size, level;
3321 int sd_size = 0;
3322
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003323 if (!sess)
3324 return;
3325
Willy Tarreau53839352018-09-05 19:51:10 +02003326 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3327 return;
3328
3329 level = LOG_INFO;
3330 if (sess->fe->options2 & PR_O2_LOGERRORS)
3331 level = LOG_ERR;
3332
3333 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3334 sd_size = sess_build_logline(sess, NULL,
3335 logline_rfc5424, global.max_syslog_len,
3336 &sess->fe->logformat_sd);
3337 }
3338
3339 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3340 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003341 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003342 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3343 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003344 }
3345}
3346
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003347void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3348{
3349 va_list argp;
3350 int data_len;
3351
3352 if (level < 0 || format == NULL || logline == NULL)
3353 return;
3354
3355 va_start(argp, format);
3356 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3357 if (data_len < 0 || data_len > global.max_syslog_len)
3358 data_len = global.max_syslog_len;
3359 va_end(argp);
3360
3361 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3362}
Emeric Brun54932b42020-07-07 09:43:24 +02003363/*
3364 * This function parse a received log message <buf>, of size <buflen>
3365 * it fills <level>, <facility> and <metadata> depending of the detected
3366 * header format and message will point on remaining payload of <size>
3367 *
3368 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3369 * struct ist len will be set to 0 if field is not found
3370 * <level> and <facility> will be set to -1 if not found.
3371 */
3372void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3373 struct ist *metadata, char **message, size_t *size)
3374{
3375
3376 char *p;
3377 int fac_level = 0;
3378
3379 *level = *facility = -1;
3380
3381 *message = buf;
3382 *size = buflen;
3383
3384 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3385
3386 p = buf;
3387 if (*size < 2 || *p != '<')
3388 return;
3389
3390 p++;
3391 while (*p != '>') {
3392 if (*p > '9' || *p < '0')
3393 return;
3394 fac_level = 10*fac_level + (*p - '0');
3395 p++;
3396 if ((p - buf) > buflen)
3397 return;
3398 }
3399
3400 *facility = fac_level >> 3;
3401 *level = fac_level & 0x7;
3402 p++;
3403
3404 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3405
3406 buflen -= p - buf;
3407 buf = p;
3408
3409 *size = buflen;
3410 *message = buf;
3411
3412 /* for rfc5424, prio is always followed by '1' and ' ' */
3413 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3414 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3415 * followed by message.
3416 * Each header field can present NILVALUE: '-'
3417 */
3418
3419 p += 2;
3420 /* timestamp is NILVALUE '-' */
3421 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3422 metadata[LOG_META_TIME] = ist2(p, 1);
3423 p++;
3424 }
3425 else if (*size > LOG_ISOTIME_MINLEN) {
3426 metadata[LOG_META_TIME].ptr = p;
3427
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003428 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003429 * in timestamp.
3430 * possible format are:
3431 * ex: '1970-01-01T00:00:00.000000Z'
3432 * '1970-01-01T00:00:00.000000+00:00'
3433 * '1970-01-01T00:00:00.000000-00:00'
3434 * '1970-01-01T00:00:00Z'
3435 * '1970-01-01T00:00:00+00:00'
3436 * '1970-01-01T00:00:00-00:00'
3437 */
3438 p += 19;
3439 if (*p == '.') {
3440 p++;
3441 if ((p - buf) >= buflen)
3442 goto bad_format;
3443 while (*p != 'Z' && *p != '+' && *p != '-') {
3444 if ((unsigned char)(*p - '0') > 9)
3445 goto bad_format;
3446
3447 p++;
3448 if ((p - buf) >= buflen)
3449 goto bad_format;
3450 }
3451 }
3452
3453 if (*p == 'Z')
3454 p++;
3455 else
3456 p += 6; /* case of '+00:00 or '-00:00' */
3457
3458 if ((p - buf) >= buflen || *p != ' ')
3459 goto bad_format;
3460 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3461 }
3462 else
3463 goto bad_format;
3464
3465
3466 p++;
3467 if ((p - buf) >= buflen || *p == ' ')
3468 goto bad_format;
3469
3470 metadata[LOG_META_HOST].ptr = p;
3471 while (*p != ' ') {
3472 p++;
3473 if ((p - buf) >= buflen)
3474 goto bad_format;
3475 }
3476 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3477 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3478 metadata[LOG_META_HOST].len = 0;
3479
3480 p++;
3481 if ((p - buf) >= buflen || *p == ' ')
3482 goto bad_format;
3483
3484 metadata[LOG_META_TAG].ptr = p;
3485 while (*p != ' ') {
3486 p++;
3487 if ((p - buf) >= buflen)
3488 goto bad_format;
3489 }
3490 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3491 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3492 metadata[LOG_META_TAG].len = 0;
3493
3494 p++;
3495 if ((p - buf) >= buflen || *p == ' ')
3496 goto bad_format;
3497
3498 metadata[LOG_META_PID].ptr = p;
3499 while (*p != ' ') {
3500 p++;
3501 if ((p - buf) >= buflen)
3502 goto bad_format;
3503 }
3504 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3505 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3506 metadata[LOG_META_PID].len = 0;
3507
3508 p++;
3509 if ((p - buf) >= buflen || *p == ' ')
3510 goto bad_format;
3511
3512 metadata[LOG_META_MSGID].ptr = p;
3513 while (*p != ' ') {
3514 p++;
3515 if ((p - buf) >= buflen)
3516 goto bad_format;
3517 }
3518 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3519 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3520 metadata[LOG_META_MSGID].len = 0;
3521
3522 p++;
3523 if ((p - buf) >= buflen || *p == ' ')
3524 goto bad_format;
3525
3526 /* structured data format is:
3527 * ex:
3528 * '[key1=value1 key2=value2][key3=value3]'
3529 *
3530 * space is invalid outside [] because
3531 * considered as the end of structured data field
3532 */
3533 metadata[LOG_META_STDATA].ptr = p;
3534 if (*p == '[') {
3535 int elem = 0;
3536
3537 while (1) {
3538 if (elem) {
3539 /* according to rfc this char is escaped in param values */
3540 if (*p == ']' && *(p-1) != '\\')
3541 elem = 0;
3542 }
3543 else {
3544 if (*p == '[')
3545 elem = 1;
3546 else if (*p == ' ')
3547 break;
3548 else
3549 goto bad_format;
3550 }
3551 p++;
3552 if ((p - buf) >= buflen)
3553 goto bad_format;
3554 }
3555 }
3556 else if (*p == '-') {
3557 /* case of NILVALUE */
3558 p++;
3559 if ((p - buf) >= buflen || *p != ' ')
3560 goto bad_format;
3561 }
3562 else
3563 goto bad_format;
3564
3565 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3566 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3567 metadata[LOG_META_STDATA].len = 0;
3568
3569 p++;
3570
3571 buflen -= p - buf;
3572 buf = p;
3573
3574 *size = buflen;
3575 *message = p;
3576 }
3577 else if (*size > LOG_LEGACYTIME_LEN) {
3578 int m;
3579
3580 /* supported header format according to rfc3164.
3581 * ex:
3582 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3583 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3584 * or 'Jan 1 00:00:00 HOSTNAME '
3585 * Note: HOSTNAME is mandatory, and day
3586 * of month uses a single space prefix if
3587 * less than 10 to ensure hour offset is
3588 * always the same.
3589 */
3590
3591 /* Check month to see if it correspond to a rfc3164
3592 * header ex 'Jan 1 00:00:00' */
3593 for (m = 0; m < 12; m++)
3594 if (!memcmp(monthname[m], p, 3))
3595 break;
3596 /* Month not found */
3597 if (m == 12)
3598 goto bad_format;
3599
3600 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3601
3602 p += LOG_LEGACYTIME_LEN;
3603 if ((p - buf) >= buflen || *p != ' ')
3604 goto bad_format;
3605
3606 p++;
3607 if ((p - buf) >= buflen || *p == ' ')
3608 goto bad_format;
3609
3610 metadata[LOG_META_HOST].ptr = p;
3611 while (*p != ' ') {
3612 p++;
3613 if ((p - buf) >= buflen)
3614 goto bad_format;
3615 }
3616 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3617
3618 /* TAG seems to no be mandatory */
3619 p++;
3620
3621 buflen -= p - buf;
3622 buf = p;
3623
3624 *size = buflen;
3625 *message = buf;
3626
3627 if (!buflen)
3628 return;
3629
3630 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3631 p++;
3632
3633 /* a tag must present a trailing ':' */
3634 if (((p - buf) >= buflen) || *p != ':')
3635 return;
3636 p++;
3637 /* followed by a space */
3638 if (((p - buf) >= buflen) || *p != ' ')
3639 return;
3640
3641 /* rewind to parse tag and pid */
3642 p = buf;
3643 metadata[LOG_META_TAG].ptr = p;
3644 /* we have the guarantee that ':' will be reach before size limit */
3645 while (*p != ':') {
3646 if (*p == '[') {
3647 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3648 metadata[LOG_META_PID].ptr = p + 1;
3649 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003650 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003651 if (p[1] != ':')
3652 return;
3653 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3654 }
3655 p++;
3656 }
3657 if (!metadata[LOG_META_TAG].len)
3658 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3659
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003660 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003661 p += 2;
3662
3663 buflen -= p - buf;
3664 buf = p;
3665
3666 *size = buflen;
3667 *message = buf;
3668 }
3669
3670 return;
3671
3672bad_format:
3673 /* bad syslog format, we reset all parsed syslog fields
3674 * but priority is kept because we are able to re-build
3675 * this message using LOF_FORMAT_PRIO.
3676 */
3677 metadata[LOG_META_TIME].len = 0;
3678 metadata[LOG_META_HOST].len = 0;
3679 metadata[LOG_META_TAG].len = 0;
3680 metadata[LOG_META_PID].len = 0;
3681 metadata[LOG_META_MSGID].len = 0;
3682 metadata[LOG_META_STDATA].len = 0;
3683
3684 return;
3685}
3686
3687/*
3688 * UDP syslog fd handler
3689 */
3690void syslog_fd_handler(int fd)
3691{
3692 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3693 ssize_t ret = 0;
3694 struct buffer *buf = get_trash_chunk();
3695 size_t size;
3696 char *message;
3697 int level;
3698 int facility;
3699 struct listener *l = objt_listener(fdtab[fd].owner);
3700 int max_accept;
3701
3702 if(!l)
3703 ABORT_NOW();
3704
Willy Tarreauf5090652021-04-06 17:23:40 +02003705 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003706
3707 if (!fd_recv_ready(fd))
3708 return;
3709
3710 max_accept = l->maxaccept ? l->maxaccept : 1;
3711
3712 do {
3713 /* Source address */
3714 struct sockaddr_storage saddr = {0};
3715 socklen_t saddrlen;
3716
3717 saddrlen = sizeof(saddr);
3718
3719 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3720 if (ret < 0) {
3721 if (errno == EINTR)
3722 continue;
3723 if (errno == EAGAIN)
3724 fd_cant_recv(fd);
3725 goto out;
3726 }
3727 buf->data = ret;
3728
Emeric Brun45c457a2020-07-09 23:23:34 +02003729 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003730 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003731 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003732
Emeric Brun54932b42020-07-07 09:43:24 +02003733 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3734
3735 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3736
3737 } while (--max_accept);
3738 }
3739
3740out:
3741 return;
3742}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003743
Emeric Brun12941c82020-07-07 14:19:42 +02003744/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003745 * IO Handler to handle message exchange with a syslog tcp client
3746 */
3747static void syslog_io_handler(struct appctx *appctx)
3748{
3749 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3750 struct stream_interface *si = appctx->owner;
3751 struct stream *s = si_strm(si);
3752 struct proxy *frontend = strm_fe(s);
3753 struct listener *l = strm_li(s);
3754 struct buffer *buf = get_trash_chunk();
3755 int max_accept;
3756 int to_skip;
3757 int facility;
3758 int level;
3759 char *message;
3760 size_t size;
3761
3762 max_accept = l->maxaccept ? l->maxaccept : 1;
3763 while (co_data(si_oc(si))) {
3764 char c;
3765
3766 if (max_accept <= 0)
3767 goto missing_budget;
3768 max_accept--;
3769
3770 to_skip = co_getchar(si_oc(si), &c);
3771 if (!to_skip)
3772 goto missing_data;
3773 else if (to_skip < 0)
3774 goto cli_abort;
3775
3776 if (c == '<') {
3777 /* rfc-6587, Non-Transparent-Framing: messages separated by
3778 * a trailing LF or CR LF
3779 */
3780 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3781 if (!to_skip)
3782 goto missing_data;
3783 else if (to_skip < 0)
3784 goto cli_abort;
3785
3786 if (buf->area[to_skip - 1] != '\n')
3787 goto parse_error;
3788
3789 buf->data = to_skip - 1;
3790
3791 /* according to rfc-6587, some devices adds CR before LF */
3792 if (buf->data && buf->area[buf->data - 1] == '\r')
3793 buf->data--;
3794
3795 }
3796 else if ((unsigned char)(c - '1') <= 8) {
3797 /* rfc-6587, Octet-Counting: message length in ASCII
3798 * (first digit can not be ZERO), followed by a space
3799 * and message length
3800 */
3801 char *p = NULL;
3802 int msglen;
3803
3804 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3805 if (!to_skip)
3806 goto missing_data;
3807 else if (to_skip < 0)
3808 goto cli_abort;
3809
3810 if (buf->area[to_skip - 1] != ' ')
3811 goto parse_error;
3812
3813 msglen = strtol(trash.area, &p, 10);
3814 if (!msglen || p != &buf->area[to_skip - 1])
3815 goto parse_error;
3816
3817 /* message seems too large */
3818 if (msglen > buf->size)
3819 goto parse_error;
3820
3821 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3822 if (!msglen)
3823 goto missing_data;
3824 else if (msglen < 0)
3825 goto cli_abort;
3826
3827
3828 buf->data = msglen;
3829 to_skip += msglen;
3830 }
3831 else
3832 goto parse_error;
3833
3834 co_skip(si_oc(si), to_skip);
3835
3836 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003837 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003838 proxy_inc_fe_req_ctr(l, frontend);
3839
3840 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3841
3842 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3843
3844 }
3845
3846missing_data:
3847 /* we need more data to read */
3848 si_oc(si)->flags |= CF_READ_DONTWAIT;
3849
3850 return;
3851
3852missing_budget:
3853 /* it may remain some stuff to do, let's retry later */
3854 appctx_wakeup(appctx);
3855
3856 return;
3857
3858parse_error:
3859 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003860 _HA_ATOMIC_INC(&l->counters->failed_req);
3861 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003862
3863 goto close;
3864
3865cli_abort:
3866 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003867 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3868 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003869
3870close:
3871 si_shutw(si);
3872 si_shutr(si);
3873
3874 si_ic(si)->flags |= CF_READ_NULL;
3875
3876 return;
3877}
3878
3879static struct applet syslog_applet = {
3880 .obj_type = OBJ_TYPE_APPLET,
3881 .name = "<SYSLOG>", /* used for logging */
3882 .fct = syslog_io_handler,
3883 .release = NULL,
3884};
3885
3886/*
Emeric Brun12941c82020-07-07 14:19:42 +02003887 * Parse "log-forward" section and create corresponding sink buffer.
3888 *
3889 * The function returns 0 in success case, otherwise, it returns error
3890 * flags.
3891 */
3892int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3893{
3894 int err_code = 0;
3895 struct proxy *px;
3896 char *errmsg = NULL;
3897 const char *err = NULL;
3898
3899 if (strcmp(args[0], "log-forward") == 0) {
3900 if (!*args[1]) {
3901 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3902 err_code |= ERR_ALERT | ERR_ABORT;
3903 goto out;
3904 }
3905
3906 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3907 goto out;
3908
3909 err = invalid_char(args[1]);
3910 if (err) {
3911 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3912 file, linenum, *err, args[0], args[1]);
3913 err_code |= ERR_ALERT | ERR_ABORT;
3914 goto out;
3915 }
3916
Emeric Brunb0c331f2020-10-07 17:05:59 +02003917 px = log_forward_by_name(args[1]);
3918 if (px) {
3919 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3920 file, linenum, args[1], px->conf.file, px->conf.line);
3921 err_code |= ERR_ALERT | ERR_FATAL;
3922 }
3923
3924 px = proxy_find_by_name(args[1], 0, 0);
3925 if (px) {
3926 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3927 file, linenum, args[1], proxy_type_str(px),
3928 px->id, px->conf.file, px->conf.line);
3929 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003930 }
3931
3932 px = calloc(1, sizeof *px);
3933 if (!px) {
3934 err_code |= ERR_ALERT | ERR_FATAL;
3935 goto out;
3936 }
3937
3938 px->next = cfg_log_forward;
3939 cfg_log_forward = px;
3940
3941 init_new_proxy(px);
3942 px->conf.file = strdup(file);
3943 px->conf.line = linenum;
3944 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003945 px->last_change = now.tv_sec;
3946 px->cap = PR_CAP_FE;
3947 px->maxconn = 10;
3948 px->timeout.client = TICK_ETERNITY;
3949 px->accept = frontend_accept;
3950 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003951 px->id = strdup(args[1]);
3952
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003953 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003954 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003955 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3956 err_code |= ERR_WARN;
3957
3958 if (*(args[1]) == 0) {
3959 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3960 err_code |= ERR_ALERT | ERR_FATAL;
3961 goto out;
3962 }
3963 cfg_log_forward->maxconn = atol(args[1]);
3964 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3965 goto out;
3966 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003967 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003968 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3969 err_code |= ERR_WARN;
3970
3971 if (*(args[1]) == 0) {
3972 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3973 err_code |= ERR_ALERT | ERR_FATAL;
3974 goto out;
3975 }
3976 cfg_log_forward->backlog = atol(args[1]);
3977 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3978 goto out;
3979 }
3980 else if (strcmp(args[0], "bind") == 0) {
3981 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003982 struct bind_conf *bind_conf;
3983 struct bind_kw *kw;
3984 struct listener *l;
3985
3986 cur_arg = 1;
3987
3988 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3989 NULL, xprt_get(XPRT_RAW));
3990 if (!bind_conf) {
3991 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3992 err_code |= ERR_ALERT | ERR_FATAL;
3993 goto out;
3994 }
3995
3996 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3997 if (errmsg && *errmsg) {
3998 indent_msg(&errmsg, 2);
3999 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
4000 }
4001 else {
4002 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
4003 file, linenum, args[0], args[1], args[2]);
4004 err_code |= ERR_ALERT | ERR_FATAL;
4005 goto out;
4006 }
4007 }
4008 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau66161322021-02-19 15:50:27 +01004009 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004010 l->accept = session_accept_fd;
4011 l->analysers |= cfg_log_forward->fe_req_ana;
4012 l->default_target = cfg_log_forward->default_target;
4013 global.maxsock++;
4014 }
4015 cur_arg++;
4016
4017 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
4018 int ret;
4019
4020 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
4021 err_code |= ret;
4022 if (ret) {
4023 if (errmsg && *errmsg) {
4024 indent_msg(&errmsg, 2);
4025 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
4026 }
4027 else
4028 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
4029 file, linenum, args[cur_arg]);
4030 if (ret & ERR_FATAL)
4031 goto out;
4032 }
4033 cur_arg += 1 + kw->skip;
4034 }
4035 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01004036 const char *best = bind_find_best_kw(args[cur_arg]);
4037 if (best)
4038 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
4039 file, linenum, args[cur_arg], cursection, best);
4040 else
4041 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
4042 file, linenum, args[cur_arg], cursection);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004043 err_code |= ERR_ALERT | ERR_FATAL;
4044 goto out;
4045 }
Emeric Brun12941c82020-07-07 14:19:42 +02004046 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02004047 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02004048 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02004049 struct bind_conf *bind_conf;
4050 struct bind_kw *kw;
4051 struct listener *l;
4052
4053 cur_arg = 1;
4054
4055 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
4056 NULL, xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +02004057 if (!bind_conf) {
4058 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
4059 err_code |= ERR_ALERT | ERR_FATAL;
4060 goto out;
4061 }
Emeric Brun12941c82020-07-07 14:19:42 +02004062
Willy Tarreau26ff5da2020-09-16 15:22:19 +02004063 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02004064 if (errmsg && *errmsg) {
4065 indent_msg(&errmsg, 2);
4066 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
4067 }
4068 else {
4069 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
4070 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02004071 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02004072 err_code |= ERR_ALERT | ERR_FATAL;
4073 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02004074 }
4075 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02004076 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreau66161322021-02-19 15:50:27 +01004077 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreaue140a692020-10-15 21:25:32 +02004078 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02004079 global.maxsock++;
4080 }
4081 cur_arg++;
4082
4083 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
4084 int ret;
4085
4086 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
4087 err_code |= ret;
4088 if (ret) {
4089 if (errmsg && *errmsg) {
4090 indent_msg(&errmsg, 2);
4091 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
4092 }
4093 else
4094 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
4095 file, linenum, args[cur_arg]);
4096 if (ret & ERR_FATAL)
4097 goto out;
4098 }
4099 cur_arg += 1 + kw->skip;
4100 }
4101 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01004102 const char *best = bind_find_best_kw(args[cur_arg]);
4103 if (best)
4104 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
4105 file, linenum, args[cur_arg], cursection, best);
4106 else
4107 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
4108 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02004109 err_code |= ERR_ALERT | ERR_FATAL;
4110 goto out;
4111 }
4112 }
4113 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02004114 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02004115 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4116 err_code |= ERR_ALERT | ERR_FATAL;
4117 goto out;
4118 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004119 }
4120 else if (strcmp(args[0], "timeout") == 0) {
4121 const char *res;
4122 unsigned timeout;
4123
4124 if (strcmp(args[1], "client") != 0) {
4125 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
4126 err_code |= ERR_ALERT | ERR_FATAL;
4127 goto out;
4128 }
4129
4130 if (*args[2] == 0) {
4131 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
4132 err_code |= ERR_ALERT | ERR_FATAL;
4133 goto out;
4134 }
4135 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
4136 if (res == PARSE_TIME_OVER) {
4137 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
4138 }
4139 else if (res == PARSE_TIME_UNDER) {
4140 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
4141 }
4142 else if (res) {
4143 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
4144 return -1;
4145 }
4146
4147 if (res) {
4148 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4149 err_code |= ERR_ALERT | ERR_FATAL;
4150 goto out;
4151 }
4152 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02004153 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02004154 else {
4155 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
4156 err_code |= ERR_ALERT | ERR_ABORT;
4157 goto out;
4158 }
Emeric Brun12941c82020-07-07 14:19:42 +02004159out:
4160 return err_code;
4161}
4162
4163
Willy Tarreau869efd52019-11-15 15:16:57 +01004164/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
4165static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004166{
Willy Tarreau869efd52019-11-15 15:16:57 +01004167 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4168 return 1;
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004169
Willy Tarreau869efd52019-11-15 15:16:57 +01004170 if (!startup_logs)
4171 return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
4172
4173 return ring_attach_cli(startup_logs, appctx);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004174}
4175
4176/* register cli keywords */
4177static struct cli_kw_list cli_kws = {{ },{
4178 { { "show", "startup-logs", NULL },
Willy Tarreau869efd52019-11-15 15:16:57 +01004179 "show startup-logs : report logs emitted during HAProxy startup", cli_parse_show_startup_logs, NULL, NULL },
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004180 {{},}
4181}};
4182
Willy Tarreau0108d902018-11-25 19:14:37 +01004183INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4184
Emeric Brun12941c82020-07-07 14:19:42 +02004185/* config parsers for this section */
4186REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4187
Willy Tarreau082b6282019-05-22 14:42:12 +02004188REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4189REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004190
Willy Tarreaubaaee002006-06-26 02:48:02 +02004191/*
4192 * Local variables:
4193 * c-indent-level: 8
4194 * c-basic-offset: 8
4195 * End:
4196 */