blob: a1de7e57a2efd8585b4b540ee65cd6b6dd02020b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
Willy Tarreau077edcb2016-08-10 18:30:56 +020025#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/applet-t.h>
Emeric Brun12941c82020-07-07 14:19:42 +020029#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020030#include <haproxy/cli.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020032#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020034#include <haproxy/http.h>
Emeric Brun12941c82020-07-07 14:19:42 +020035#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036#include <haproxy/log.h>
Emeric Brun12941c82020-07-07 14:19:42 +020037#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/ring.h>
39#include <haproxy/sample.h>
40#include <haproxy/sink.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020041#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020043#include <haproxy/stream_interface.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020044#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020045#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020046#include <haproxy/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Emeric Brun45c457a2020-07-09 23:23:34 +020048/* global recv logs counter */
49int cum_log_messages;
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Emeric Brun12941c82020-07-07 14:19:42 +020051/* log forward proxy list */
52struct proxy *cfg_log_forward;
53
Emeric Brun54648852020-07-06 15:54:06 +020054struct log_fmt_st {
Dragan Dosen43885c72015-10-01 13:18:13 +020055 char *name;
Dragan Dosen43885c72015-10-01 13:18:13 +020056};
57
Emeric Brun54648852020-07-06 15:54:06 +020058static const struct log_fmt_st log_formats[LOG_FORMATS] = {
Emeric Brun0237c4e2020-11-27 16:24:34 +010059 [LOG_FORMAT_LOCAL] = {
60 .name = "local",
61 },
Dragan Dosen43885c72015-10-01 13:18:13 +020062 [LOG_FORMAT_RFC3164] = {
63 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020064 },
65 [LOG_FORMAT_RFC5424] = {
66 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020067 },
68 [LOG_FORMAT_PRIO] = {
69 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010070 },
71 [LOG_FORMAT_SHORT] = {
72 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020073 },
74 [LOG_FORMAT_TIMED] = {
75 .name = "timed",
76 },
77 [LOG_FORMAT_ISO] = {
78 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010079 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010080 [LOG_FORMAT_RAW] = {
81 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010082 },
Dragan Dosen1322d092015-09-22 16:05:32 +020083};
84
Dragan Dosen835b9212016-02-12 13:23:03 +010085/*
86 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020087 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
88 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
89 * that the byte should be escaped. Be careful to always pass bytes from 0 to
90 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010091 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020092long rfc5424_escape_map[(256/8) / sizeof(long)];
93long hdr_encode_map[(256/8) / sizeof(long)];
94long url_encode_map[(256/8) / sizeof(long)];
95long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010096
Dragan Dosen835b9212016-02-12 13:23:03 +010097
Willy Tarreaubaaee002006-06-26 02:48:02 +020098const char *log_facilities[NB_LOG_FACILITIES] = {
99 "kern", "user", "mail", "daemon",
100 "auth", "syslog", "lpr", "news",
101 "uucp", "cron", "auth2", "ftp",
102 "ntp", "audit", "alert", "cron2",
103 "local0", "local1", "local2", "local3",
104 "local4", "local5", "local6", "local7"
105};
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107const char *log_levels[NB_LOG_LEVELS] = {
108 "emerg", "alert", "crit", "err",
109 "warning", "notice", "info", "debug"
110};
111
Willy Tarreau570f2212013-06-10 16:42:09 +0200112const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200113const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
William Lallemand723b73a2012-02-08 16:37:49 +0100115
116/* log_format */
117struct logformat_type {
118 char *name;
119 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100120 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200121 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100122 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100123 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100124};
125
William Lallemandb7ff6a32012-03-02 14:35:21 +0100126int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
127
William Lallemand723b73a2012-02-08 16:37:49 +0100128/* log_format variable names */
129static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200130 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100131
132 /* please keep these lines sorted ! */
133 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
134 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
135 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
136 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100137 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200138 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200139 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200140 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100141 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200142 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
143 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
144 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
145 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
146 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
147 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200148 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100149 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200150 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000151 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100152 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
153 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200154 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100155 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200156 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100157 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
158 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200159 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200160 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
161 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100162 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
163 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200164 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
165 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100166 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200167 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
168 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
169 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
170 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000171 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
Maciej Zdebfcdfd852020-11-30 18:27:47 +0000172 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP relative or absolute path */
173 { "HPO", LOG_FMT_HTTP_PATH_ONLY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path only (without host nor query string) */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000174 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000175 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
176 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200177 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100178 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200179 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100180 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
181 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100182 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100183 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
184 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
185 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
186 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
187 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200188 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
189 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100190 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200191 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
192 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
193 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100194 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
195 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
196
197 /* The following tags are deprecated and will be removed soon */
198 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
199 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200200 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
201 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
202 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
203 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100204 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
205 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
206 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
207 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
208 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200209 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100210};
211
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200212char default_http_log_format[] = "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
213char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%trg] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl";
Willy Tarreau2beef582012-12-20 17:22:52 +0100214char default_tcp_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq";
William Lallemand723b73a2012-02-08 16:37:49 +0100215char *log_format = NULL;
216
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200217/* Default string used for structured-data part in RFC5424 formatted
218 * syslog messages.
219 */
220char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200221
Willy Tarreau13ef7732018-11-12 07:25:28 +0100222/* total number of dropped logs */
223unsigned int dropped_logs = 0;
224
Dragan Dosen59cee972015-09-19 22:09:02 +0200225/* This is a global syslog message buffer, common to all outgoing
226 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100227 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200228THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100229
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200230/* A global syslog message buffer, common to all RFC5424 syslog messages.
231 * Currently, it is used for generating the structured-data part.
232 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200233THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200234
Christopher Fauletd4696382017-10-24 11:44:05 +0200235/* A global buffer used to store all startup alerts/warnings. It will then be
236 * retrieve on the CLI. */
Willy Tarreau869efd52019-11-15 15:16:57 +0100237static struct ring *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200238
William Lallemand723b73a2012-02-08 16:37:49 +0100239struct logformat_var_args {
240 char *name;
241 int mask;
242};
243
244struct logformat_var_args var_args_list[] = {
245// global
246 { "M", LOG_OPT_MANDATORY },
247 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200248 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100249 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100250 { 0, 0 }
251};
252
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200253/* return the name of the directive used in the current proxy for which we're
254 * currently parsing a header, when it is known.
255 */
256static inline const char *fmt_directive(const struct proxy *curproxy)
257{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100258 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200259 case ARGC_ACL:
260 return "acl";
261 case ARGC_STK:
262 return "stick";
263 case ARGC_TRK:
264 return "track-sc";
265 case ARGC_LOG:
266 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200267 case ARGC_LOGSD:
268 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100269 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100270 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100271 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100272 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200273 case ARGC_UIF:
274 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100275 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200276 return "redirect";
277 case ARGC_CAP:
278 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200279 case ARGC_SRV:
280 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200281 case ARGC_SPOE:
282 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100283 case ARGC_UBK:
284 return "use_backend";
Christopher Faulet3b967c12020-05-15 15:47:44 +0200285 case ARGC_HERR:
286 return "http-error";
Miroslav Zagorac7f8314c2020-12-09 16:31:48 +0100287 case ARGC_OT:
288 return "ot-scope";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100289 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200290 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100291 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200292}
293
William Lallemand723b73a2012-02-08 16:37:49 +0100294/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100295 * callback used to configure addr source retrieval
296 */
297int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
298{
299 curproxy->options2 |= PR_O2_SRC_ADDR;
300
301 return 0;
302}
303
304
305/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100306 * Parse args in a logformat_var. Returns 0 in error
307 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100308 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100309int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100310{
311 int i = 0;
312 int end = 0;
313 int flags = 0; // 1 = + 2 = -
314 char *sp = NULL; // start pointer
315
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100316 if (args == NULL) {
317 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100318 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100319 }
William Lallemand723b73a2012-02-08 16:37:49 +0100320
321 while (1) {
322 if (*args == '\0')
323 end = 1;
324
325 if (*args == '+') {
326 // add flag
327 sp = args + 1;
328 flags = 1;
329 }
330 if (*args == '-') {
331 // delete flag
332 sp = args + 1;
333 flags = 2;
334 }
335
336 if (*args == '\0' || *args == ',') {
337 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100338 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100339 if (strcmp(sp, var_args_list[i].name) == 0) {
340 if (flags == 1) {
341 node->options |= var_args_list[i].mask;
342 break;
343 } else if (flags == 2) {
344 node->options &= ~var_args_list[i].mask;
345 break;
346 }
347 }
348 }
349 sp = NULL;
350 if (end)
351 break;
352 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100353 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100354 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100355 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100356}
357
358/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100359 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
360 * must pass the args part in the <arg> pointer with its length in <arg_len>,
361 * and varname with its length in <var> and <var_len> respectively. <arg> is
362 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100363 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100364 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100365int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100366{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100367 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200368 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100369
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100370 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
371 if (strlen(logformat_keywords[j].name) == var_len &&
372 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
373 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200374 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100375 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100376 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200377 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100378 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100379 node->type = logformat_keywords[j].type;
380 node->options = *defoptions;
381 if (arg_len) {
382 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100383 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200384 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100385 }
386 if (node->type == LOG_FMT_GLOBAL) {
387 *defoptions = node->options;
388 free(node->arg);
389 free(node);
390 } else {
391 if (logformat_keywords[j].config_callback &&
392 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200393 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100394 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100395 curproxy->to_log |= logformat_keywords[j].lw;
396 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100397 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100398 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100399 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
400 curproxy->conf.args.file, curproxy->conf.args.line,
401 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100402 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100403 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100404 memprintf(err, "format variable '%s' is reserved for HTTP mode",
405 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200406 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100407 }
William Lallemand723b73a2012-02-08 16:37:49 +0100408 }
409 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100410
411 j = var[var_len];
412 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100413 memprintf(err, "no such format variable '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%'", var);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100414 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200415
416 error_free:
417 if (node) {
418 free(node->arg);
419 free(node);
420 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100421 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100422}
423
424/*
425 * push to the logformat linked list
426 *
427 * start: start pointer
428 * end: end text pointer
429 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100430 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100431 *
432 * LOG_TEXT: copy chars from start to end excluding end.
433 *
434*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100435int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100436{
437 char *str;
438
Willy Tarreaua3571662012-12-20 21:59:12 +0100439 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200440 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100441 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100442 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100443 return 0;
444 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200445 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100446 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100447 str[end - start] = '\0';
448 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100449 node->type = LOG_FMT_TEXT; // type string
450 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100451 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200452 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100453 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100454 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100455 return 0;
456 }
William Lallemand1d705562012-03-12 12:46:41 +0100457 node->type = LOG_FMT_SEPARATOR;
458 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100459 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100460 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100461}
462
463/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100464 * Parse the sample fetch expression <text> and add a node to <list_format> upon
465 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100466 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
467 * is passed in <endptr>, it will be updated with the pointer to the first character
468 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100469 *
470 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100471 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100472int add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, char **err, char **endptr)
Willy Tarreauc8368452012-12-21 00:09:23 +0100473{
474 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200475 struct sample_expr *expr = NULL;
476 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100477 int cmd_arg;
478
479 cmd[0] = text;
480 cmd[1] = "";
481 cmd_arg = 0;
482
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100483 expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err, &curpx->conf.args, endptr);
Willy Tarreauc8368452012-12-21 00:09:23 +0100484 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100485 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200486 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100487 }
488
Vincent Bernat02779b62016-04-03 13:48:43 +0200489 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100490 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100491 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200492 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100493 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100494 node->type = LOG_FMT_EXPR;
495 node->expr = expr;
496 node->options = options;
497
498 if (arg_len) {
499 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100500 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200501 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100502 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100503 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100504 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
505
Willy Tarreau434c57c2013-01-08 01:10:24 +0100506 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100507 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
508
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100509 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100510 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
511 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200512 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100513 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100514
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200515 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100516 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100517 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100518
William Lallemand65ad6e12014-01-31 15:08:02 +0100519 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
520 * needed with some sample fetches (eg: ssl*). We always set it for
521 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100522 */
523 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200524 if (curpx->http_needed)
525 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100526 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100527 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200528
529 error_free:
530 release_sample_expr(expr);
531 if (node) {
532 free(node->arg);
533 free(node);
534 }
535 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100536}
537
538/*
William Lallemand723b73a2012-02-08 16:37:49 +0100539 * Parse the log_format string and fill a linked list.
540 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200541 * You can set arguments using { } : %{many arguments}varname.
542 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100543 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500544 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100545 * curproxy: the proxy affected
546 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100547 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100548 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100549 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100550 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100551 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100552int 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 +0100553{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100554 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100555 char *arg = NULL; /* start pointer for args */
556 char *var = NULL; /* start pointer for vars */
557 int arg_len = 0;
558 int var_len = 0;
559 int cformat; /* current token format */
560 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100561 struct logformat_node *tmplf, *back;
562
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100563 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100564 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100565 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100566 return 0;
567 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200568 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200569
William Lallemand723b73a2012-02-08 16:37:49 +0100570 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100571 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100572 LIST_DEL(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200573 release_sample_expr(tmplf->expr);
574 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100575 free(tmplf);
576 }
577
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100578 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100579 pformat = cformat;
580
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100581 if (!*str)
582 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100583
Joseph Herlant85b40592018-11-15 12:10:04 -0800584 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100585 * second have all common paths processed at one place. The common paths are the ones
586 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
587 * We use the common LF_INIT state to dispatch to the different final states.
588 */
589 switch (pformat) {
590 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100591 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100592 arg_len = var_len = 0;
593 if (*str == '{') { // optional argument
594 cformat = LF_STARG;
595 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100596 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100597 else if (*str == '[') {
598 cformat = LF_STEXPR;
599 var = str + 1; // store expr in variable name
600 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100601 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100602 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100603 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100604 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100605 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500606 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100607 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100608 /* single '%' followed by blank or digit, send them both */
609 cformat = LF_TEXT;
610 pformat = LF_TEXT; /* finally we include the previous char as well */
611 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600612 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 +0100613 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100614 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100615
616 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100617 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500618 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100619 break;
620
621 case LF_STARG: // text immediately following '%{'
622 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100623 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100624 arg_len = str - arg;
625 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100626 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100627 break;
628
629 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100630 if (*str == '[') {
631 cformat = LF_STEXPR;
632 var = str + 1; // store expr in variable name
633 break;
634 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100635 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100636 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100637 var = str;
638 break;
639 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100640 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100641 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100642
Willy Tarreauc8368452012-12-21 00:09:23 +0100643 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100644 /* the whole sample expression is parsed at once,
645 * returning the pointer to the first character not
646 * part of the expression, which MUST be the trailing
647 * angle bracket.
648 */
649 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
650 goto fail;
651
652 if (*str == ']') {
653 // end of arg, go on with next state
654 cformat = pformat = LF_EDEXPR;
655 sp = str;
656 }
657 else {
658 char c = *str;
659 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100660 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100661 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
662 else
663 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200664 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100665 }
666 break;
667
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100668 case LF_VAR: // text part of a variable name
669 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100670 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100671 cformat = LF_INIT; // not variable name anymore
672 break;
673
Willy Tarreauc8368452012-12-21 00:09:23 +0100674 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100675 cformat = LF_INIT;
676 }
677
678 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
679 switch (*str) {
680 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100681 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200682 case ' ':
683 if (options & LOG_OPT_MERGE_SPACES) {
684 cformat = LF_SEPARATOR;
685 break;
686 }
687 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100688 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100689 }
690 }
691
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100692 if (cformat != pformat || pformat == LF_SEPARATOR) {
693 switch (pformat) {
694 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100695 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100696 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100697 break;
698 case LF_TEXT:
699 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100700 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100701 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100702 break;
703 }
704 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100705 }
706 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100707
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100708 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100709 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100710 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100711 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100712 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100713
714 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100715 fail:
716 free(backfmt);
717 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100718}
719
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200720/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500721 * 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 +0200722 * ranges of indexes. Note that an index may be considered as a particular range
723 * with a high limit to the low limit.
724 */
725int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
726{
727 char *end, *p;
728
729 *low = *high = 0;
730
731 p = *arg;
732 end = strchr(p, ',');
733 if (!end)
734 end = p + strlen(p);
735
736 *high = *low = read_uint((const char **)&p, end);
737 if (!*low || (p != end && *p != '-'))
738 goto err;
739
740 if (p == end)
741 goto done;
742
743 p++;
744 *high = read_uint((const char **)&p, end);
745 if (!*high || *high <= *low || p != end)
746 goto err;
747
748 done:
749 if (*end == ',')
750 end++;
751 *arg = end;
752 return 1;
753
754 err:
755 memprintf(err, "wrong sample range '%s'", *arg);
756 return 0;
757}
758
759/*
760 * Returns 1 if the range defined by <low> and <high> overlaps
761 * one of them in <rgs> array of ranges with <sz> the size of this
762 * array, 0 if not.
763 */
764int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
765 unsigned int low, unsigned int high, char **err)
766{
767 size_t i;
768
769 for (i = 0; i < sz; i++) {
770 if ((low >= rgs[i].low && low <= rgs[i].high) ||
771 (high >= rgs[i].low && high <= rgs[i].high)) {
772 memprintf(err, "ranges are overlapping");
773 return 1;
774 }
775 }
776
777 return 0;
778}
779
780int smp_log_range_cmp(const void *a, const void *b)
781{
782 const struct smp_log_range *rg_a = a;
783 const struct smp_log_range *rg_b = b;
784
785 if (rg_a->high < rg_b->low)
786 return -1;
787 else if (rg_a->low > rg_b->high)
788 return 1;
789
790 return 0;
791}
792
793/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200794 * Parse "log" keyword and update <logsrvs> list accordingly.
795 *
796 * When <do_del> is set, it means the "no log" line was parsed, so all log
797 * servers in <logsrvs> are released.
798 *
799 * Otherwise, we try to parse the "log" line. First of all, when the list is not
800 * the global one, we look for the parameter "global". If we find it,
801 * global.logsrvs is copied. Else we parse each arguments.
802 *
803 * The function returns 1 in success case, otherwise, it returns 0 and err is
804 * filled.
805 */
806int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
807{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100808 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200809 struct sockaddr_storage *sk;
810 struct logsrv *logsrv = NULL;
811 int port1, port2;
812 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200813 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200814
815 /*
816 * "no log": delete previous herited or defined syslog
817 * servers.
818 */
819 if (do_del) {
820 struct logsrv *back;
821
822 if (*(args[1]) != 0) {
823 memprintf(err, "'no log' does not expect arguments");
824 goto error;
825 }
826
827 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
828 LIST_DEL(&logsrv->list);
829 free(logsrv);
830 }
831 return 1;
832 }
833
834 /*
835 * "log global": copy global.logrsvs linked list to the end of logsrvs
836 * list. But first, we check (logsrvs != global.logsrvs).
837 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100838 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200839 if (logsrvs == &global.logsrvs) {
840 memprintf(err, "'global' is not supported for a global syslog server");
841 goto error;
842 }
843 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200844 struct logsrv *node;
845
846 list_for_each_entry(node, logsrvs, list) {
847 if (node->ref == logsrv)
848 goto skip_logsrv;
849 }
850
851 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200852 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200853 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200854 LIST_INIT(&node->list);
855 LIST_ADDQ(logsrvs, &node->list);
Christopher Faulet28ac0992018-03-26 16:09:19 +0200856
857 skip_logsrv:
858 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200859 }
860 return 1;
861 }
862
863 /*
864 * "log <address> ...: parse a syslog server line
865 */
866 if (*(args[1]) == 0 || *(args[2]) == 0) {
867 memprintf(err, "expects <address> and <facility> %s as arguments",
868 ((logsrvs == &global.logsrvs) ? "" : "or global"));
869 goto error;
870 }
871
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100872 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
873 if (strcmp(args[1], "stdout") == 0)
874 args[1] = "fd@1";
875 else if (strcmp(args[1], "stderr") == 0)
876 args[1] = "fd@2";
877
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200878 logsrv = calloc(1, sizeof(*logsrv));
879 if (!logsrv) {
880 memprintf(err, "out of memory");
881 goto error;
882 }
883
884 /* skip address for now, it will be parsed at the end */
885 cur_arg = 2;
886
887 /* just after the address, a length may be specified */
888 logsrv->maxlen = MAX_SYSLOG_LEN;
889 if (strcmp(args[cur_arg], "len") == 0) {
890 int len = atoi(args[cur_arg+1]);
891 if (len < 80 || len > 65535) {
892 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
893 args[cur_arg+1]);
894 goto error;
895 }
896 logsrv->maxlen = len;
897 cur_arg += 2;
898 }
899 if (logsrv->maxlen > global.max_syslog_len)
900 global.max_syslog_len = logsrv->maxlen;
901
902 /* after the length, a format may be specified */
903 if (strcmp(args[cur_arg], "format") == 0) {
904 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200905 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200906 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
907 goto error;
908 }
909 cur_arg += 2;
910 }
911
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200912 if (strcmp(args[cur_arg], "sample") == 0) {
913 unsigned low, high;
914 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200915 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
916
917 p = args[cur_arg+1];
918 smp_sz_str = strchr(p, ':');
919 if (!smp_sz_str) {
920 memprintf(err, "Missing sample size");
921 goto error;
922 }
923
924 *smp_sz_str++ = '\0';
925
926 end = p + strlen(p);
927
928 while (p != end) {
929 if (!get_logsrv_smp_range(&low, &high, &p, err))
930 goto error;
931
932 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
933 goto error;
934
935 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
936 if (!smp_rgs) {
937 memprintf(err, "out of memory error");
938 goto error;
939 }
940
941 smp_rgs[smp_rgs_sz].low = low;
942 smp_rgs[smp_rgs_sz].high = high;
943 smp_rgs[smp_rgs_sz].sz = high - low + 1;
944 smp_rgs[smp_rgs_sz].curr_idx = 0;
945 if (smp_rgs[smp_rgs_sz].high > smp_sz)
946 smp_sz = smp_rgs[smp_rgs_sz].high;
947 smp_rgs_sz++;
948 }
949
Tim Duesterhus21648002019-06-23 22:10:10 +0200950 if (smp_rgs == NULL) {
951 memprintf(err, "no sampling ranges given");
952 goto error;
953 }
954
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200955 beg = smp_sz_str;
956 end = beg + strlen(beg);
957 new_smp_sz = read_uint((const char **)&beg, end);
958 if (!new_smp_sz || beg != end) {
959 memprintf(err, "wrong sample size '%s' for sample range '%s'",
960 smp_sz_str, args[cur_arg+1]);
961 goto error;
962 }
963
964 if (new_smp_sz < smp_sz) {
965 memprintf(err, "sample size %zu should be greater or equal to "
966 "%zu the maximum of the high ranges limits",
967 new_smp_sz, smp_sz);
968 goto error;
969 }
970 smp_sz = new_smp_sz;
971
972 /* Let's order <smp_rgs> array. */
973 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
974
975 logsrv->lb.smp_rgs = smp_rgs;
976 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
977 logsrv->lb.smp_sz = smp_sz;
978
979 cur_arg += 2;
980 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200981 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200982 /* parse the facility */
983 logsrv->facility = get_log_facility(args[cur_arg]);
984 if (logsrv->facility < 0) {
985 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
986 goto error;
987 }
988 cur_arg++;
989
990 /* parse the max syslog level (default: debug) */
991 logsrv->level = 7;
992 if (*(args[cur_arg])) {
993 logsrv->level = get_log_level(args[cur_arg]);
994 if (logsrv->level < 0) {
995 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
996 goto error;
997 }
998 cur_arg++;
999 }
1000
1001 /* parse the limit syslog level (default: emerg) */
1002 logsrv->minlvl = 0;
1003 if (*(args[cur_arg])) {
1004 logsrv->minlvl = get_log_level(args[cur_arg]);
1005 if (logsrv->minlvl < 0) {
1006 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
1007 goto error;
1008 }
1009 cur_arg++;
1010 }
1011
1012 /* Too many args */
1013 if (*(args[cur_arg])) {
1014 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1015 goto error;
1016 }
1017
1018 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +02001019 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +02001020 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001021 logsrv->addr.ss_family = AF_UNSPEC;
1022 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +02001023 logsrv->sink = NULL;
1024 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +02001025 goto done;
1026 }
1027
Willy Tarreau5fc93282020-09-16 18:25:03 +02001028 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, NULL,
Willy Tarreaua93e5c72020-09-15 14:01:16 +02001029 err, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001030 if (!sk)
1031 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +02001032
1033 if (fd != -1)
1034 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001035 logsrv->addr = *sk;
1036
1037 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001038 logsrv->addr = *sk;
1039 if (!port1)
1040 set_host_port(&logsrv->addr, SYSLOG_PORT);
1041 }
Willy Tarreauc046d162019-08-30 15:24:59 +02001042 done:
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001043 LIST_ADDQ(logsrvs, &logsrv->list);
1044 return 1;
1045
1046 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001047 free(smp_rgs);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001048 if (logsrv)
1049 free(logsrv->ring_name);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001050 free(logsrv);
1051 return 0;
1052}
1053
1054
Christopher Fauletd4696382017-10-24 11:44:05 +02001055/* Generic function to display messages prefixed by a label */
1056static void print_message(const char *label, const char *fmt, va_list argp)
1057{
1058 struct tm tm;
1059 char *head, *msg;
1060
1061 head = msg = NULL;
1062
1063 get_localtime(date.tv_sec, &tm);
1064 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
1065 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
1066 memvprintf(&msg, fmt, argp);
1067
Willy Tarreau869efd52019-11-15 15:16:57 +01001068 if (global.mode & MODE_STARTING) {
1069 if (unlikely(!startup_logs))
1070 startup_logs = ring_new(STARTUP_LOG_SIZE);
1071
1072 if (likely(startup_logs)) {
1073 struct ist m[2];
1074
1075 m[0] = ist(head);
1076 m[1] = ist(msg);
1077 /* trim the trailing '\n' */
1078 if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
1079 m[1].len--;
1080 ring_write(startup_logs, ~0, 0, 0, m, 2);
1081 }
1082 }
Christopher Fauletd4696382017-10-24 11:44:05 +02001083
1084 fprintf(stderr, "%s%s", head, msg);
1085 fflush(stderr);
1086
1087 free(head);
1088 free(msg);
1089}
1090
Willy Tarreaubaaee002006-06-26 02:48:02 +02001091/*
1092 * Displays the message on stderr with the date and pid. Overrides the quiet
1093 * mode during startup.
1094 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001095void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001096{
1097 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098
1099 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaubb869862020-04-16 10:52:41 +02001100 if (!(warned & WARN_EXEC_PATH)) {
1101 const char *path = get_exec_path();
1102
1103 warned |= WARN_EXEC_PATH;
1104 ha_notice("haproxy version is %s\n", haproxy_version);
1105 if (path)
1106 ha_notice("path to executable is %s\n", path);
1107 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001108 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001109 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001110 va_end(argp);
1111 }
1112}
1113
1114
1115/*
1116 * Displays the message on stderr with the date and pid.
1117 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001118void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001119{
1120 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001121
Willy Tarreaubebd2122020-04-15 16:06:11 +02001122 warned |= WARN_ANY;
1123
Willy Tarreaubaaee002006-06-26 02:48:02 +02001124 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1125 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001126 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001127 va_end(argp);
1128 }
1129}
1130
1131/*
William Lallemand9c56a222018-11-21 18:04:52 +01001132 * Displays the message on stderr with the date and pid.
1133 */
1134void ha_notice(const char *fmt, ...)
1135{
1136 va_list argp;
1137
1138 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1139 va_start(argp, fmt);
1140 print_message("NOTICE", fmt, argp);
1141 va_end(argp);
1142 }
1143}
1144
1145/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001146 * Displays the message on <out> only if quiet mode is not set.
1147 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001148void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001149{
1150 va_list argp;
1151
1152 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1153 va_start(argp, fmt);
1154 vfprintf(out, fmt, argp);
1155 fflush(out);
1156 va_end(argp);
1157 }
1158}
1159
1160/*
Emeric Brun54648852020-07-06 15:54:06 +02001161 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001162 */
Emeric Brun54648852020-07-06 15:54:06 +02001163enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001164{
Emeric Brun54648852020-07-06 15:54:06 +02001165 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001166
1167 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001168 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001169 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001170 format--;
1171
Emeric Brun54648852020-07-06 15:54:06 +02001172 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001173 return format;
1174}
1175
1176/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001177 * returns log level for <lev> or -1 if not found.
1178 */
1179int get_log_level(const char *lev)
1180{
1181 int level;
1182
1183 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001184 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001185 level--;
1186
1187 return level;
1188}
1189
Willy Tarreaubaaee002006-06-26 02:48:02 +02001190/*
1191 * returns log facility for <fac> or -1 if not found.
1192 */
1193int get_log_facility(const char *fac)
1194{
1195 int facility;
1196
1197 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001198 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001199 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001200
Willy Tarreaubaaee002006-06-26 02:48:02 +02001201 return facility;
1202}
1203
William Lallemanda1cc3812012-02-08 16:38:44 +01001204/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001205 * Encode the string.
1206 *
1207 * When using the +E log format option, it will try to escape '"\]'
1208 * characters with '\' as prefix. The same prefix should not be used as
1209 * <escape>.
1210 */
1211static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001212 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001213 const char *string,
1214 struct logformat_node *node)
1215{
1216 if (node->options & LOG_OPT_ESC) {
1217 if (start < stop) {
1218 stop--; /* reserve one byte for the final '\0' */
1219 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001220 if (!ha_bit_test((unsigned char)(*string), map)) {
1221 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001222 *start++ = *string;
1223 else {
1224 if (start + 2 >= stop)
1225 break;
1226 *start++ = '\\';
1227 *start++ = *string;
1228 }
1229 }
1230 else {
1231 if (start + 3 >= stop)
1232 break;
1233 *start++ = escape;
1234 *start++ = hextab[(*string >> 4) & 15];
1235 *start++ = hextab[*string & 15];
1236 }
1237 string++;
1238 }
1239 *start = '\0';
1240 }
1241 }
1242 else {
1243 return encode_string(start, stop, escape, map, string);
1244 }
1245
1246 return start;
1247}
1248
1249/*
1250 * Encode the chunk.
1251 *
1252 * When using the +E log format option, it will try to escape '"\]'
1253 * characters with '\' as prefix. The same prefix should not be used as
1254 * <escape>.
1255 */
1256static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001257 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001258 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001259 struct logformat_node *node)
1260{
1261 char *str, *end;
1262
1263 if (node->options & LOG_OPT_ESC) {
1264 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001265 str = chunk->area;
1266 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001267
1268 stop--; /* reserve one byte for the final '\0' */
1269 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001270 if (!ha_bit_test((unsigned char)(*str), map)) {
1271 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001272 *start++ = *str;
1273 else {
1274 if (start + 2 >= stop)
1275 break;
1276 *start++ = '\\';
1277 *start++ = *str;
1278 }
1279 }
1280 else {
1281 if (start + 3 >= stop)
1282 break;
1283 *start++ = escape;
1284 *start++ = hextab[(*str >> 4) & 15];
1285 *start++ = hextab[*str & 15];
1286 }
1287 str++;
1288 }
1289 *start = '\0';
1290 }
1291 }
1292 else {
1293 return encode_chunk(start, stop, escape, map, chunk);
1294 }
1295
1296 return start;
1297}
1298
1299/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001300 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001301 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001302 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001303 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001304 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001305char *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 +01001306{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001307 if (size < 2)
1308 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001309
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001310 if (node->options & LOG_OPT_QUOTE) {
1311 *(dst++) = '"';
1312 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001313 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001314
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001315 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001316 if (++len > size)
1317 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001318 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001319 char *ret;
1320
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001321 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001322 if (ret == NULL || *ret != '\0')
1323 return NULL;
1324 len = ret - dst;
1325 }
1326 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001327 len = strlcpy2(dst, src, len);
1328 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001329
1330 size -= len;
1331 dst += len;
1332 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001333 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1334 if (size < 2)
1335 return NULL;
1336 *(dst++) = '-';
1337 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001338
1339 if (node->options & LOG_OPT_QUOTE) {
1340 if (size < 2)
1341 return NULL;
1342 *(dst++) = '"';
1343 }
1344
1345 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001346 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001347}
1348
Willy Tarreau26ffa852018-09-05 15:23:10 +02001349static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001350{
1351 return lf_text_len(dst, src, size, size, node);
1352}
1353
William Lallemand5f232402012-04-05 18:02:55 +02001354/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001355 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001356 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001357 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001358char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001359{
1360 char *ret = dst;
1361 int iret;
1362 char pn[INET6_ADDRSTRLEN];
1363
1364 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001365 unsigned char *addr = NULL;
1366 switch (sockaddr->sa_family) {
1367 case AF_INET:
1368 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1369 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1370 break;
1371 case AF_INET6:
1372 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1373 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1374 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1375 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1376 break;
1377 default:
1378 return NULL;
1379 }
William Lallemand5f232402012-04-05 18:02:55 +02001380 if (iret < 0 || iret > size)
1381 return NULL;
1382 ret += iret;
1383 } else {
1384 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1385 ret = lf_text(dst, pn, size, node);
1386 if (ret == NULL)
1387 return NULL;
1388 }
1389 return ret;
1390}
1391
1392/*
1393 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001394 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001395 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001396char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001397{
1398 char *ret = dst;
1399 int iret;
1400
1401 if (node->options & LOG_OPT_HEXA) {
1402 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1403 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1404 if (iret < 0 || iret > size)
1405 return NULL;
1406 ret += iret;
1407 } else {
1408 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1409 if (ret == NULL)
1410 return NULL;
1411 }
1412 return ret;
1413}
1414
Emeric Brun54648852020-07-06 15:54:06 +02001415
1416/*
1417 * This function sends the syslog message using a printf format string. It
1418 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001419 */
Emeric Brun54648852020-07-06 15:54:06 +02001420void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001421{
Emeric Brun54648852020-07-06 15:54:06 +02001422 va_list argp;
1423 int data_len;
1424
1425 if (level < 0 || format == NULL || logline == NULL)
1426 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001427
Emeric Brun54648852020-07-06 15:54:06 +02001428 va_start(argp, format);
1429 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1430 if (data_len < 0 || data_len > global.max_syslog_len)
1431 data_len = global.max_syslog_len;
1432 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001433
Emeric Brun54648852020-07-06 15:54:06 +02001434 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1435 logline, data_len, default_rfc5424_sd_log_format, 2);
1436}
1437/*
1438 * This function builds a log header of given format using given
1439 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1440 * to determine format based on given metadas. It is useful
1441 * for log-forwarding to be able to forward any format without
1442 * settings.
1443 * This function returns a struct ist array of elements of the header
1444 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001445 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001446 * elements.
1447 */
1448struct ist *build_log_header(enum log_fmt format, int level, int facility,
1449 struct ist *metadata, size_t *nbelem)
1450{
1451 static THREAD_LOCAL struct {
1452 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1453 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1454 time_t cur_legacy_time;
1455 char priority_buffer[6];
1456 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001457
Emeric Brun54648852020-07-06 15:54:06 +02001458 struct tm logtime;
1459 int len;
1460 int fac_level = 0;
1461 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001462
Emeric Brun54648852020-07-06 15:54:06 +02001463 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001464
Emeric Brun54648852020-07-06 15:54:06 +02001465
1466 if (format == LOG_FORMAT_UNSPEC) {
1467 format = LOG_FORMAT_RAW;
1468 if (metadata) {
1469 /* If a hostname is set, it appears we want to perform syslog
1470 * because only rfc5427 or rfc3164 support an hostname.
1471 */
1472 if (metadata[LOG_META_HOST].len) {
1473 /* If a rfc5424 compliant timestamp is used we consider
1474 * that output format is rfc5424, else legacy format
1475 * is used as specified default for local logs
1476 * in documentation.
1477 */
1478 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1479 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1480 format = LOG_FORMAT_RFC5424;
1481 else
1482 format = LOG_FORMAT_RFC3164;
1483 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001484 else if (metadata[LOG_META_TAG].len) {
1485 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001486 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001487 * in legacy format (analog to RFC3164 but
1488 * with stripped hostname).
1489 */
1490 format = LOG_FORMAT_LOCAL;
1491 }
Emeric Brun54648852020-07-06 15:54:06 +02001492 else if (metadata[LOG_META_PRIO].len) {
1493 /* the source seems a parsed message
1494 * offering a valid level/prio prefix
1495 * so we consider this format.
1496 */
1497 format = LOG_FORMAT_PRIO;
1498 }
1499 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001500 }
1501
Emeric Brun54648852020-07-06 15:54:06 +02001502 /* prepare priority, stored into 1 single elem */
1503 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001504 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001505 case LOG_FORMAT_RFC3164:
1506 case LOG_FORMAT_RFC5424:
1507 case LOG_FORMAT_PRIO:
1508 fac_level = facility << 3;
1509 /* further format ignore the facility */
1510 /* fall through */
1511 case LOG_FORMAT_TIMED:
1512 case LOG_FORMAT_SHORT:
1513 fac_level += level;
1514 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1515 do {
1516 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1517 fac_level /= 10;
1518 hdr_ctx.ist_vector[*nbelem].ptr--;
1519 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1520 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1521 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1522 break;
1523 case LOG_FORMAT_ISO:
1524 case LOG_FORMAT_RAW:
1525 break;
1526 case LOG_FORMAT_UNSPEC:
1527 case LOG_FORMATS:
1528 ABORT_NOW();
1529 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001530
William Lallemand2a4a44f2012-02-06 16:00:33 +01001531
Emeric Brun54648852020-07-06 15:54:06 +02001532 /* prepare timestamp, stored into a max of 4 elems */
1533 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001534 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001535 case LOG_FORMAT_RFC3164:
1536 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1537 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1538 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1539 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001540 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001541 break;
1542 }
1543 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1544 int month;
1545 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001546
Emeric Brun54648852020-07-06 15:54:06 +02001547 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001548
Emeric Brun54648852020-07-06 15:54:06 +02001549 /* compute month */
1550 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1551 if (month)
1552 month--;
1553 if (month <= 11) {
1554 /* builds log prefix ex: 'Jan 1 ' */
1555 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1556 "%s %c%c ", monthname[month],
1557 timestamp[8] != '0' ? timestamp[8] : ' ',
1558 timestamp[9]);
1559 /* we reused the timestamp_buffer, signal that it does not
1560 * contain local time anymore
1561 */
1562 hdr_ctx.cur_legacy_time = 0;
1563 if (len == 7) {
1564 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1565 /* adds 'HH:MM:SS' from iso time */
1566 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1567 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1568 /* we successfully reuse iso time, we can break */
1569 break;
1570 }
1571 }
1572 /* Failed to reuse isotime time, fallback to local legacy time */
1573 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001574
Emeric Brun54648852020-07-06 15:54:06 +02001575 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1576 /* re-builds timestamp from the current local time */
1577 get_localtime(time, &logtime);
1578
1579 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1580 "%s %2d %02d:%02d:%02d ",
1581 monthname[logtime.tm_mon],
1582 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1583 if (len != LOG_LEGACYTIME_LEN+1)
1584 hdr_ctx.cur_legacy_time = 0;
1585 else
1586 hdr_ctx.cur_legacy_time = time;
1587 }
1588 if (likely(hdr_ctx.cur_legacy_time))
1589 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1590 else
1591 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1592 break;
1593 case LOG_FORMAT_RFC5424:
1594 /* adds rfc5425 version prefix */
1595 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1596 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001597 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001598 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1599 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1600 break;
1601 }
1602 /* let continue as 'timed' and 'iso' format for usual timestamp */
1603 /* fall through */
1604 case LOG_FORMAT_TIMED:
1605 case LOG_FORMAT_ISO:
1606 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1607 * '1900:01:01T14:00:00+02:00'
1608 * '1900:01:01T10:00:00.123456-02:00'
1609 */
1610 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1611 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1612 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001613 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001614 break;
1615 }
1616 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1617 int month;
1618 char *timestamp = metadata[LOG_META_TIME].ptr;
1619
1620 for (month = 0; month < 12; month++)
1621 if (!memcmp(monthname[month], timestamp, 3))
1622 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001623
Emeric Brun54648852020-07-06 15:54:06 +02001624 if (month < 12) {
1625
1626 /* get local time to retrieve year */
1627 get_localtime(time, &logtime);
1628
1629 /* year seems changed since log */
1630 if (logtime.tm_mon < month)
1631 logtime.tm_year--;
1632
1633 /* builds rfc5424 prefix ex: '1900-01-01T' */
1634 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1635 "%4d-%02d-%c%cT",
1636 logtime.tm_year+1900, month+1,
1637 timestamp[4] != ' ' ? timestamp[4] : '0',
1638 timestamp[5]);
1639
1640 /* we reused the timestamp_buffer, signal that it does not
1641 * contain local time anymore
1642 */
1643 hdr_ctx.cur_legacy_time = 0;
1644 if (len == 11) {
1645 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1646 /* adds HH:MM:SS from legacy timestamp */
1647 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001648 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001649 /* according to rfc: -00:00 means we don't know the timezone */
1650 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1651 /* we successfully reuse legacy time, we can break */
1652 break;
1653 }
1654 }
1655 /* Failed to reuse legacy time, fallback to local iso time */
1656 }
1657 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1658 break;
1659 case LOG_FORMAT_PRIO:
1660 case LOG_FORMAT_SHORT:
1661 case LOG_FORMAT_RAW:
1662 break;
1663 case LOG_FORMAT_UNSPEC:
1664 case LOG_FORMATS:
1665 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001666 }
1667
Emeric Brun54648852020-07-06 15:54:06 +02001668 /* prepare other meta data, stored into a max of 10 elems */
1669 switch (format) {
1670 case LOG_FORMAT_RFC3164:
1671 if (metadata && metadata[LOG_META_HOST].len) {
1672 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1673 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1674 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001675 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001676 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001677 /* fall through */
1678 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001679 if (!metadata || !metadata[LOG_META_TAG].len)
1680 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001681
Emeric Brun54648852020-07-06 15:54:06 +02001682 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1683 if (metadata[LOG_META_PID].len) {
1684 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1685 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1686 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1687 }
1688 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1689 break;
1690 case LOG_FORMAT_RFC5424:
1691 if (metadata && metadata[LOG_META_HOST].len) {
1692 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1693 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1694 }
1695 else
1696 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001697
Emeric Brun54648852020-07-06 15:54:06 +02001698 if (metadata && metadata[LOG_META_TAG].len) {
1699 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1700 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1701 }
1702 else
1703 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001704
Emeric Brun54648852020-07-06 15:54:06 +02001705 if (metadata && metadata[LOG_META_PID].len) {
1706 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1707 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1708 }
1709 else
1710 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001711
Emeric Brun54648852020-07-06 15:54:06 +02001712 if (metadata && metadata[LOG_META_MSGID].len) {
1713 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1714 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1715 }
1716 else
1717 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001718
Emeric Brun54648852020-07-06 15:54:06 +02001719 if (metadata && metadata[LOG_META_STDATA].len) {
1720 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1721 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1722 }
1723 else
1724 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1725 break;
1726 case LOG_FORMAT_PRIO:
1727 case LOG_FORMAT_SHORT:
1728 case LOG_FORMAT_TIMED:
1729 case LOG_FORMAT_ISO:
1730 case LOG_FORMAT_RAW:
1731 break;
1732 case LOG_FORMAT_UNSPEC:
1733 case LOG_FORMATS:
1734 ABORT_NOW();
1735 }
1736
1737 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001738}
1739
1740/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001741 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001742 * The argument <metadata> MUST be an array of size
1743 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001744 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001745 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001746 */
Emeric Brun54648852020-07-06 15:54:06 +02001747static 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 +01001748{
Emeric Brun54648852020-07-06 15:54:06 +02001749 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001750 static THREAD_LOCAL struct msghdr msghdr = {
1751 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001752 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001753 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001754 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1755 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001756 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001757 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001758 size_t nbelem;
1759 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001760
1761 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001762
Emeric Brunfa9d7802020-05-28 14:21:33 +02001763 /* historically some messages used to already contain the trailing LF
1764 * or Zero. Let's remove all trailing LF or Zero
1765 */
Emeric Brun54648852020-07-06 15:54:06 +02001766 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001767 size--;
1768
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001769 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001770 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001771 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001772 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001773 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1774 /* the socket's address is a file descriptor */
1775 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1776 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001777 else if (logsrv->addr.ss_family == AF_UNIX)
1778 plogfd = &logfdunix;
1779 else
1780 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001781
Willy Tarreauc046d162019-08-30 15:24:59 +02001782 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001783 /* socket not successfully initialized yet */
1784 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1785 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1786 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001787
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001788 if (!once) {
1789 once = 1; /* note: no need for atomic ops here */
1790 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1791 nblogger, strerror(errno), errno);
1792 }
1793 return;
1794 } else {
1795 /* we don't want to receive anything on this socket */
1796 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1797 /* does nothing under Linux, maybe needed for others */
1798 shutdown(*plogfd, SHUT_RD);
1799 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1800 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001801 }
1802
Emeric Brun54648852020-07-06 15:54:06 +02001803 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1804 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001805 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001806 struct ist msg;
1807
1808 msg = ist2(message, size);
1809 if (msg.len > logsrv->maxlen)
1810 msg.len = logsrv->maxlen;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001811
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001812 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1813 }
1814 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1815 struct ist msg;
1816
1817 msg = ist2(message, size);
1818 if (msg.len > logsrv->maxlen)
1819 msg.len = logsrv->maxlen;
1820
1821 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001822 }
1823 else {
Emeric Brun54648852020-07-06 15:54:06 +02001824 int i = 0;
1825 int totlen = logsrv->maxlen;
1826
1827 for (i = 0 ; i < nbelem ; i++ ) {
1828 iovec[i].iov_base = msg_header[i].ptr;
1829 iovec[i].iov_len = msg_header[i].len;
1830 if (totlen <= iovec[i].iov_len) {
1831 iovec[i].iov_len = totlen;
1832 totlen = 0;
1833 break;
1834 }
1835 totlen -= iovec[i].iov_len;
1836 }
1837 if (totlen) {
1838 iovec[i].iov_base = message;
1839 iovec[i].iov_len = size;
1840 if (totlen <= iovec[i].iov_len)
1841 iovec[i].iov_len = totlen;
1842 i++;
1843 }
1844 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1845 iovec[i].iov_len = 1;
1846 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001847
Emeric Brun54648852020-07-06 15:54:06 +02001848 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001849 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1850 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001851
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001852 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1853 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001854
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001855 if (sent < 0) {
1856 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001857
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001858 if (errno == EAGAIN)
1859 _HA_ATOMIC_ADD(&dropped_logs, 1);
1860 else if (!once) {
1861 once = 1; /* note: no need for atomic ops here */
1862 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1863 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001864 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001865 }
1866}
Dragan Dosen59cee972015-09-19 22:09:02 +02001867
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001868/*
1869 * This function sends a syslog message.
1870 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001871 * The argument <metadata> MUST be an array of size
1872 * LOG_META_FIELDS*sizeof(struct ist) containing
1873 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001874 */
Emeric Brun54648852020-07-06 15:54:06 +02001875void process_send_log(struct list *logsrvs, int level, int facility,
1876 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001877{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001878 struct logsrv *logsrv;
1879 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001880
1881 /* Send log messages to syslog server. */
1882 nblogger = 0;
1883 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001884 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001885
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001886 /* we can filter the level of the messages that are sent to each logger */
1887 if (level > logsrv->level)
1888 continue;
1889
Frédéric Lécailled803e472019-04-25 07:42:09 +02001890 if (logsrv->lb.smp_rgs) {
1891 struct smp_log_range *curr_rg;
1892
1893 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1894 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1895 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1896 if (in_range) {
1897 /* Let's consume this range. */
1898 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1899 if (!curr_rg->curr_idx) {
1900 /* If consumed, let's select the next range. */
1901 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1902 }
1903 }
1904 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1905 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1906 }
1907 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001908 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1909 (facility == -1) ? logsrv->facility : facility,
1910 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001911 }
1912}
1913
Emeric Brun54648852020-07-06 15:54:06 +02001914/*
1915 * This function sends a syslog message.
1916 * It doesn't care about errors nor does it report them.
1917 * The arguments <sd> and <sd_size> are used for the structured-data part
1918 * in RFC5424 formatted syslog messages.
1919 */
1920void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1921 char *message, size_t size, char *sd, size_t sd_size)
1922{
1923 static THREAD_LOCAL pid_t curr_pid;
1924 static THREAD_LOCAL char pidstr[16];
1925 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1926
1927 if (logsrvs == NULL) {
1928 if (!LIST_ISEMPTY(&global.logsrvs)) {
1929 logsrvs = &global.logsrvs;
1930 }
1931 }
1932 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1933 return;
1934
1935 if (!metadata[LOG_META_HOST].len) {
1936 if (global.log_send_hostname)
1937 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02001938 }
1939
1940 if (!tagb || !tagb->area)
1941 tagb = &global.log_tag;
1942
1943 if (tagb)
1944 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1945
1946 if (unlikely(curr_pid != getpid()))
1947 metadata[LOG_META_PID].len = 0;
1948
1949 if (!metadata[LOG_META_PID].len) {
1950 curr_pid = getpid();
1951 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1952 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1953 }
1954
1955 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1956
1957 /* Remove trailing space of structured data */
1958 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1959 metadata[LOG_META_STDATA].len--;
1960
1961 return process_send_log(logsrvs, level, -1, metadata, message, size);
1962}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001963
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001964const 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 +01001965const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1966 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1967 Set-cookie Updated, unknown, unknown */
1968
William Lallemand1d705562012-03-12 12:46:41 +01001969/*
1970 * try to write a character if there is enough space, or goto out
1971 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001972#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001973 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001974 *(tmplog++) = (x); \
1975 } else { \
1976 goto out; \
1977 } \
1978 } while(0)
1979
Dragan Dosen835b9212016-02-12 13:23:03 +01001980
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001981/* Initializes some log data at boot */
1982static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001983{
1984 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001985 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001986
1987 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1988 * inside PARAM-VALUE should be escaped with '\' as prefix.
1989 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1990 * details.
1991 */
1992 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1993
1994 tmp = "\"\\]";
1995 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001996 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001997 tmp++;
1998 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001999
2000 /* initialize the log header encoding map : '{|}"#' should be encoded with
2001 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
2002 * URL encoding only requires '"', '#' to be encoded as well as non-
2003 * printable characters above.
2004 */
2005 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
2006 memset(url_encode_map, 0, sizeof(url_encode_map));
2007 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002008 ha_bit_set(i, hdr_encode_map);
2009 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002010 }
2011 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002012 ha_bit_set(i, hdr_encode_map);
2013 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002014 }
2015
2016 tmp = "\"#{|}";
2017 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002018 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002019 tmp++;
2020 }
2021
2022 tmp = "\"#";
2023 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002024 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002025 tmp++;
2026 }
2027
2028 /* initialize the http header encoding map. The draft httpbis define the
2029 * header content as:
2030 *
2031 * HTTP-message = start-line
2032 * *( header-field CRLF )
2033 * CRLF
2034 * [ message-body ]
2035 * header-field = field-name ":" OWS field-value OWS
2036 * field-value = *( field-content / obs-fold )
2037 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2038 * obs-fold = CRLF 1*( SP / HTAB )
2039 * field-vchar = VCHAR / obs-text
2040 * VCHAR = %x21-7E
2041 * obs-text = %x80-FF
2042 *
2043 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
2044 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08002045 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02002046 */
2047 memset(http_encode_map, 0, sizeof(http_encode_map));
2048 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002049 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02002050 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02002051 ha_bit_set(i, http_encode_map);
2052 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01002053}
William Lallemand1d705562012-03-12 12:46:41 +01002054
Willy Tarreaub6b3df32018-11-26 16:31:20 +01002055INITCALL0(STG_PREPARE, init_log);
2056
Christopher Faulet0132d062017-07-26 15:33:35 +02002057/* Initialize log buffers used for syslog messages */
2058int init_log_buffers()
2059{
Christopher Faulet0132d062017-07-26 15:33:35 +02002060 logline = my_realloc2(logline, global.max_syslog_len + 1);
2061 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02002062 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02002063 return 0;
2064 return 1;
2065}
2066
2067/* Deinitialize log buffers used for syslog messages */
2068void deinit_log_buffers()
2069{
Christopher Faulet0132d062017-07-26 15:33:35 +02002070 free(logline);
2071 free(logline_rfc5424);
Willy Tarreau869efd52019-11-15 15:16:57 +01002072 ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
Christopher Faulet0132d062017-07-26 15:33:35 +02002073 logline = NULL;
2074 logline_rfc5424 = NULL;
2075}
2076
Willy Tarreaudf974472012-12-28 02:44:01 +01002077/* Builds a log line in <dst> based on <list_format>, and stops before reaching
2078 * <maxsize> characters. Returns the size of the output string in characters,
2079 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002080 * is not zero. It requires a valid session and optionally a stream. If the
2081 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01002082 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02002083int 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 +02002084{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002085 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002086 struct proxy *be;
2087 struct http_txn *txn;
2088 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002089 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002090 unsigned int s_flags;
2091 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02002092 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002093 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002094 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002095 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002096 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02002097 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002098 int t_request;
2099 int hdr;
2100 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002101 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01002102 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01002103 char *ret;
2104 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002105 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002106 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002107 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002108 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002109 struct ist path;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002110
William Lallemandbddd4fd2012-02-27 11:23:10 +01002111 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002112
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002113 if (likely(s)) {
2114 be = s->be;
2115 txn = s->txn;
2116 be_conn = cs_conn(objt_cs(s->si[1].end));
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002117 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002118 s_flags = s->flags;
2119 uniq_id = s->uniq_id;
2120 logs = &s->logs;
2121 } else {
2122 /* we have no stream so we first need to initialize a few
2123 * things that are needed later. We do increment the request
2124 * ID so that it's uniquely assigned to this request just as
2125 * if the request had reached the point of being processed.
2126 * A request error is reported as it's the only element we have
2127 * here and which justifies emitting such a log.
2128 */
2129 be = fe;
2130 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002131 fe_conn = objt_conn(sess->origin);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002132 be_conn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002133 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002134 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01002135 uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002136
2137 /* prepare a valid log structure */
2138 tmp_strm_log.tv_accept = sess->tv_accept;
2139 tmp_strm_log.accept_date = sess->accept_date;
2140 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02002141 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002142 tv_zero(&tmp_strm_log.tv_request);
2143 tmp_strm_log.t_queue = -1;
2144 tmp_strm_log.t_connect = -1;
2145 tmp_strm_log.t_data = -1;
2146 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2147 tmp_strm_log.bytes_in = 0;
2148 tmp_strm_log.bytes_out = 0;
2149 tmp_strm_log.prx_queue_pos = 0;
2150 tmp_strm_log.srv_queue_pos = 0;
2151
2152 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002153
2154 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
2155 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, NULL);
2156
2157 switch (es) {
2158 case MUX_ES_SUCCESS:
2159 break;
2160 case MUX_ES_INVALID_ERR:
2161 status = 400;
2162 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2163 s_flags = SF_ERR_CLICL | SF_FINST_R;
2164 else
2165 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2166 break;
2167 case MUX_ES_TOUT_ERR:
2168 status = 408;
2169 s_flags = SF_ERR_CLITO | SF_FINST_R;
2170 break;
2171 case MUX_ES_INTERNAL_ERR:
2172 status = 500;
2173 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2174 break;
2175 default:
2176 break;
2177 }
2178 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002179 }
2180
William Lallemandbddd4fd2012-02-27 11:23:10 +01002181 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002182 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2183 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002184
William Lallemand1d705562012-03-12 12:46:41 +01002185 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002186
William Lallemandbddd4fd2012-02-27 11:23:10 +01002187 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002188 if (LIST_ISEMPTY(list_format))
2189 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002190
William Lallemand1d705562012-03-12 12:46:41 +01002191 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002192 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002193 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002194 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002195 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002196
Willy Tarreauc8368452012-12-21 00:09:23 +01002197 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002198 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002199 if (!last_isspace) {
2200 LOGCHAR(' ');
2201 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002202 }
2203 break;
2204
William Lallemand1d705562012-03-12 12:46:41 +01002205 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002206 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002207 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002208 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002209 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002210 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002211 last_isspace = 0;
2212 break;
2213
Willy Tarreauc8368452012-12-21 00:09:23 +01002214 case LOG_FMT_EXPR: // sample expression, may be request or response
2215 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002216 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002217 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 +02002218 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002219 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 +01002220 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002221 ret = lf_encode_chunk(tmplog, dst + maxsize,
2222 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002223 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002224 ret = lf_text_len(tmplog,
2225 key ? key->data.u.str.area : NULL,
2226 key ? key->data.u.str.data : 0,
2227 dst + maxsize - tmplog,
2228 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002229 if (ret == 0)
2230 goto out;
2231 tmplog = ret;
2232 last_isspace = 0;
2233 break;
2234
Willy Tarreau2beef582012-12-20 17:22:52 +01002235 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002236 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002237 if (conn && conn_get_src(conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002238 ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002239 else
2240 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002241 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002242 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002243 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002244 last_isspace = 0;
2245 break;
2246
Willy Tarreau2beef582012-12-20 17:22:52 +01002247 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002248 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002249 if (conn && conn_get_src(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002250 if (conn->src->ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002251 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002252 } else {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002253 ret = lf_port(tmplog, (struct sockaddr *)conn->src,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002254 dst + maxsize - tmplog, tmp);
2255 }
William Lallemand5f232402012-04-05 18:02:55 +02002256 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002257 else
2258 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2259
William Lallemand5f232402012-04-05 18:02:55 +02002260 if (ret == NULL)
2261 goto out;
2262 tmplog = ret;
2263 last_isspace = 0;
2264 break;
2265
Willy Tarreau2beef582012-12-20 17:22:52 +01002266 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002267 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002268 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002269 ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002270 }
2271 else
2272 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2273
William Lallemand1d705562012-03-12 12:46:41 +01002274 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002275 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002276 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002277 last_isspace = 0;
2278 break;
2279
Willy Tarreau2beef582012-12-20 17:22:52 +01002280 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002281 conn = objt_conn(sess->origin);
Willy Tarreau8fa99842019-07-17 11:47:11 +02002282 if (conn && conn_get_dst(conn)) {
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002283 if (conn->dst->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002284 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002285 else
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002286 ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002287 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002288 else
2289 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2290
William Lallemand5f232402012-04-05 18:02:55 +02002291 if (ret == NULL)
2292 goto out;
2293 tmplog = ret;
2294 last_isspace = 0;
2295 break;
2296
Willy Tarreau2beef582012-12-20 17:22:52 +01002297 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002298 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002299 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002300 else
2301 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2302
William Lallemand1d705562012-03-12 12:46:41 +01002303 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002304 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002305 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002306 last_isspace = 0;
2307 break;
2308
Willy Tarreau2beef582012-12-20 17:22:52 +01002309 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002310 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002311 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002312 else
2313 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2314
William Lallemand5f232402012-04-05 18:02:55 +02002315 if (ret == NULL)
2316 goto out;
2317 tmplog = ret;
2318 last_isspace = 0;
2319 break;
2320
Willy Tarreau2beef582012-12-20 17:22:52 +01002321 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002322 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002323 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002324 else
2325 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2326
William Lallemand5f232402012-04-05 18:02:55 +02002327 if (ret == NULL)
2328 goto out;
2329 tmplog = ret;
2330 last_isspace = 0;
2331 break;
2332
Willy Tarreau2beef582012-12-20 17:22:52 +01002333 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002334 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002335 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002336 else
2337 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2338
William Lallemand1d705562012-03-12 12:46:41 +01002339 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002340 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002341 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002342 last_isspace = 0;
2343 break;
2344
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002345 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002346 get_localtime(logs->accept_date.tv_sec, &tm);
2347 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002348 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002349 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002350 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002351 last_isspace = 0;
2352 break;
2353
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002354 case LOG_FMT_tr: // %tr = start of request date
2355 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002356 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 +02002357 get_localtime(tv.tv_sec, &tm);
2358 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2359 if (ret == NULL)
2360 goto out;
2361 tmplog = ret;
2362 last_isspace = 0;
2363 break;
2364
2365 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002366 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002367 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002368 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002369 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002370 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002371 last_isspace = 0;
2372 break;
2373
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002374 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002375 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 +02002376 get_gmtime(tv.tv_sec, &tm);
2377 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2378 if (ret == NULL)
2379 goto out;
2380 tmplog = ret;
2381 last_isspace = 0;
2382 break;
2383
2384 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002385 get_localtime(logs->accept_date.tv_sec, &tm);
2386 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002387 if (ret == NULL)
2388 goto out;
2389 tmplog = ret;
2390 last_isspace = 0;
2391 break;
2392
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002393 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002394 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 +02002395 get_localtime(tv.tv_sec, &tm);
2396 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2397 if (ret == NULL)
2398 goto out;
2399 tmplog = ret;
2400 last_isspace = 0;
2401 break;
2402
William Lallemand5f232402012-04-05 18:02:55 +02002403 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002404 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002405 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002406 if (iret < 0 || iret > dst + maxsize - tmplog)
2407 goto out;
2408 last_isspace = 0;
2409 tmplog += iret;
2410 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002411 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002412 if (ret == NULL)
2413 goto out;
2414 tmplog = ret;
2415 last_isspace = 0;
2416 }
2417 break;
2418
William Lallemand1d705562012-03-12 12:46:41 +01002419 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002420 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002421 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002422 if (iret < 0 || iret > dst + maxsize - tmplog)
2423 goto out;
2424 last_isspace = 0;
2425 tmplog += iret;
2426 } else {
2427 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002428 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002429 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002430 tmplog, 4);
2431 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002432 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002433 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002434 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002435 }
2436 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002437
William Lallemand1d705562012-03-12 12:46:41 +01002438 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002439 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002440 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002441 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002442 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002443 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002444 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002445 break;
2446
Willy Tarreau773d65f2012-10-12 14:56:11 +02002447 case LOG_FMT_FRONTEND_XPRT: // %ft
2448 src = fe->id;
2449 if (tmp->options & LOG_OPT_QUOTE)
2450 LOGCHAR('"');
2451 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2452 if (iret == 0)
2453 goto out;
2454 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002455 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002456 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002457 if (tmp->options & LOG_OPT_QUOTE)
2458 LOGCHAR('"');
2459 last_isspace = 0;
2460 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002461#ifdef USE_OPENSSL
2462 case LOG_FMT_SSL_CIPHER: // %sslc
2463 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002464 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002465 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002466 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002467 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002468 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2469 if (ret == NULL)
2470 goto out;
2471 tmplog = ret;
2472 last_isspace = 0;
2473 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002474
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002475 case LOG_FMT_SSL_VERSION: // %sslv
2476 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002477 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002478 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002479 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002480 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002481 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2482 if (ret == NULL)
2483 goto out;
2484 tmplog = ret;
2485 last_isspace = 0;
2486 break;
2487#endif
William Lallemand1d705562012-03-12 12:46:41 +01002488 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002489 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002490 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002491 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002492 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002493 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002494 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002495 break;
2496
William Lallemand1d705562012-03-12 12:46:41 +01002497 case LOG_FMT_SERVER: // %s
Willy Tarreaue1809df2018-09-05 15:30:16 +02002498 switch (obj_type(s ? s->target : NULL)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002499 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002500 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002501 break;
2502 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002503 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002504 break;
2505 default:
2506 src = "<NOSRV>";
2507 break;
2508 }
William Lallemand5f232402012-04-05 18:02:55 +02002509 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002510 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002511 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002512 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002513 last_isspace = 0;
2514 break;
2515
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002516 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002517 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002518 if (ret == NULL)
2519 goto out;
2520 tmplog = ret;
2521 last_isspace = 0;
2522 break;
2523
2524 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002525 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002526 if (ret == NULL)
2527 goto out;
2528 tmplog = ret;
2529 last_isspace = 0;
2530 break;
2531
2532 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002533 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002534 tmplog, dst + maxsize - tmplog);
2535 if (ret == NULL)
2536 goto out;
2537 tmplog = ret;
2538 last_isspace = 0;
2539 break;
2540
2541 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002542 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002543 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002544 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002545 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002546 last_isspace = 0;
2547 break;
2548
William Lallemand1d705562012-03-12 12:46:41 +01002549 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002550 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002551 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002552 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002553 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002554 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002555 last_isspace = 0;
2556 break;
2557
William Lallemand1d705562012-03-12 12:46:41 +01002558 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002559 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002560 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002561 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002562 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002563 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002564 last_isspace = 0;
2565 break;
2566
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002567 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002568 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002569 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002570 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002571 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002572 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002573 last_isspace = 0;
2574 break;
2575
Willy Tarreau27b639d2016-05-17 17:55:27 +02002576 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002577 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002578 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002579 tmplog, dst + maxsize - tmplog);
2580 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002581 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002582 tmplog, dst + maxsize - tmplog);
2583 if (ret == NULL)
2584 goto out;
2585 tmplog = ret;
2586 last_isspace = 0;
2587 break;
2588
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002589 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2590 if (!(fe->to_log & LW_BYTES))
2591 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002592 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 +02002593 tmplog, dst + maxsize - tmplog);
2594 if (ret == NULL)
2595 goto out;
2596 tmplog = ret;
2597 last_isspace = 0;
2598 break;
2599
2600 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002601 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002602 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002603 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002604 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002605 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002606 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002607 last_isspace = 0;
2608 break;
2609
Damien Claisse57c8eb92020-04-28 12:09:19 +00002610 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2611 if (!(fe->to_log & LW_BYTES))
2612 LOGCHAR('+');
2613 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2614 tmplog, dst + maxsize - tmplog);
2615 if (ret == NULL)
2616 goto out;
2617 tmplog = ret;
2618 last_isspace = 0;
2619 break;
2620
Willy Tarreau2beef582012-12-20 17:22:52 +01002621 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002622 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002623 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002624 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002625 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002626 last_isspace = 0;
2627 break;
2628
William Lallemand1d705562012-03-12 12:46:41 +01002629 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002630 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002631 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002632 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002633 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002634 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002635 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002636 last_isspace = 0;
2637 break;
2638
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002639 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002640 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002641 if (ret == NULL)
2642 goto out;
2643 tmplog = ret;
2644 last_isspace = 0;
2645 break;
2646
Willy Tarreau2beef582012-12-20 17:22:52 +01002647 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002648 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002649 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002650 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002651 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002652 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002653 last_isspace = 0;
2654 break;
2655
Willy Tarreau2beef582012-12-20 17:22:52 +01002656 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002657 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002658 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002659 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002660 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002661 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002662 last_isspace = 0;
2663 break;
2664
William Lallemand1d705562012-03-12 12:46:41 +01002665 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002666 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2667 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002668 *tmplog = '\0';
2669 last_isspace = 0;
2670 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002671
William Lallemand1d705562012-03-12 12:46:41 +01002672 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002673 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2674 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002675 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2676 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 +01002677 last_isspace = 0;
2678 break;
2679
William Lallemand1d705562012-03-12 12:46:41 +01002680 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002681 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002682 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002683 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002684 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002685 last_isspace = 0;
2686 break;
2687
William Lallemand1d705562012-03-12 12:46:41 +01002688 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002689 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002690 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002691 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002692 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002693 last_isspace = 0;
2694 break;
2695
William Lallemand1d705562012-03-12 12:46:41 +01002696 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002697 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002698 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002699 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002700 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002701 last_isspace = 0;
2702 break;
2703
William Lallemand1d705562012-03-12 12:46:41 +01002704 case LOG_FMT_SRVCONN: // %sc
Willy Tarreaue1809df2018-09-05 15:30:16 +02002705 ret = ultoa_o(objt_server(s ? s->target : NULL) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002706 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002707 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002708 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002709 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002710 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002711 last_isspace = 0;
2712 break;
2713
William Lallemand1d705562012-03-12 12:46:41 +01002714 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002715 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002716 LOGCHAR('+');
Willy Tarreauabd71a52018-09-04 19:21:44 +02002717 ret = ltoa_o((s && s->si[1].conn_retries > 0) ?
Willy Tarreau350f4872014-11-28 14:42:25 +01002718 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002719 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002720 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002721 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002722 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002723 last_isspace = 0;
2724 break;
2725
William Lallemand1d705562012-03-12 12:46:41 +01002726 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002727 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002728 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002729 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002730 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002731 last_isspace = 0;
2732 break;
2733
William Lallemand1d705562012-03-12 12:46:41 +01002734 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002735 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002736 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002737 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002738 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002739 last_isspace = 0;
2740 break;
2741
William Lallemand1d705562012-03-12 12:46:41 +01002742 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002743 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002744 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002745 if (tmp->options & LOG_OPT_QUOTE)
2746 LOGCHAR('"');
2747 LOGCHAR('{');
2748 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2749 if (hdr)
2750 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002751 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002752 ret = lf_encode_string(tmplog, dst + maxsize,
2753 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002754 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002755 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002756 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002757 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002758 }
2759 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002760 if (tmp->options & LOG_OPT_QUOTE)
2761 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002762 last_isspace = 0;
2763 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002764 break;
2765
William Lallemand1d705562012-03-12 12:46:41 +01002766 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002767 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002768 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002769 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2770 if (hdr > 0)
2771 LOGCHAR(' ');
2772 if (tmp->options & LOG_OPT_QUOTE)
2773 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002774 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002775 ret = lf_encode_string(tmplog, dst + maxsize,
2776 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002777 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002778 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002779 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002780 } else if (!(tmp->options & LOG_OPT_QUOTE))
2781 LOGCHAR('-');
2782 if (tmp->options & LOG_OPT_QUOTE)
2783 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002784 last_isspace = 0;
2785 }
2786 }
2787 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002788
William Lallemand1d705562012-03-12 12:46:41 +01002789
2790 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002791 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002792 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002793 if (tmp->options & LOG_OPT_QUOTE)
2794 LOGCHAR('"');
2795 LOGCHAR('{');
2796 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2797 if (hdr)
2798 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002799 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002800 ret = lf_encode_string(tmplog, dst + maxsize,
2801 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002802 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002803 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002804 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002805 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002806 }
2807 LOGCHAR('}');
2808 last_isspace = 0;
2809 if (tmp->options & LOG_OPT_QUOTE)
2810 LOGCHAR('"');
2811 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002812 break;
2813
William Lallemand1d705562012-03-12 12:46:41 +01002814 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002815 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002816 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002817 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2818 if (hdr > 0)
2819 LOGCHAR(' ');
2820 if (tmp->options & LOG_OPT_QUOTE)
2821 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002822 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002823 ret = lf_encode_string(tmplog, dst + maxsize,
2824 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002825 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002826 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002827 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002828 } else if (!(tmp->options & LOG_OPT_QUOTE))
2829 LOGCHAR('-');
2830 if (tmp->options & LOG_OPT_QUOTE)
2831 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002832 last_isspace = 0;
2833 }
2834 }
2835 break;
2836
William Lallemand1d705562012-03-12 12:46:41 +01002837 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002838 /* Request */
2839 if (tmp->options & LOG_OPT_QUOTE)
2840 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002841 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002842 ret = lf_encode_string(tmplog, dst + maxsize,
2843 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002844 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002845 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002846 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002847 if (tmp->options & LOG_OPT_QUOTE)
2848 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002849 last_isspace = 0;
2850 break;
William Lallemand5f232402012-04-05 18:02:55 +02002851
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002852 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002853 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002854
Willy Tarreaub7636d12015-06-17 19:58:02 +02002855 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002856 LOGCHAR('"');
2857
2858 end = uri + strlen(uri);
2859 // look for the first whitespace character
2860 while (uri < end && !HTTP_IS_SPHT(*uri))
2861 uri++;
2862
2863 // keep advancing past multiple spaces
2864 while (uri < end && HTTP_IS_SPHT(*uri)) {
2865 uri++; nspaces++;
2866 }
2867
2868 // look for first space or question mark after url
2869 spc = uri;
2870 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2871 spc++;
2872
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002873 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002874 chunk.area = "<BADREQ>";
2875 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002876 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002877 chunk.area = uri;
2878 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002879 }
2880
Dragan Dosen835b9212016-02-12 13:23:03 +01002881 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002882 if (ret == NULL || *ret != '\0')
2883 goto out;
2884
2885 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002886 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002887 LOGCHAR('"');
2888
2889 last_isspace = 0;
2890 break;
2891
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002892 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2893 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2894
2895 if (tmp->options & LOG_OPT_QUOTE)
2896 LOGCHAR('"');
2897
2898 end = uri + strlen(uri);
2899
2900 // look for the first whitespace character
2901 while (uri < end && !HTTP_IS_SPHT(*uri))
2902 uri++;
2903
2904 // keep advancing past multiple spaces
2905 while (uri < end && HTTP_IS_SPHT(*uri)) {
2906 uri++; nspaces++;
2907 }
2908
2909 // look for first space after url
2910 spc = uri;
2911 while (spc < end && !HTTP_IS_SPHT(*spc))
2912 spc++;
2913
2914 path.ptr = uri;
2915 path.len = spc - uri;
2916
2917 // extract relative path without query params from url
2918 path = iststop(http_get_path(path), '?');
2919 if (!txn || !txn->uri || nspaces == 0) {
2920 chunk.area = "<BADREQ>";
2921 chunk.data = strlen("<BADREQ>");
2922 } else {
2923 chunk.area = path.ptr;
2924 chunk.data = path.len;
2925 }
2926
2927 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2928 if (ret == NULL || *ret != '\0')
2929 goto out;
2930
2931 tmplog = ret;
2932 if (tmp->options & LOG_OPT_QUOTE)
2933 LOGCHAR('"');
2934
2935 last_isspace = 0;
2936 break;
2937
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002938 case LOG_FMT_HTTP_QUERY: // %HQ
2939 if (tmp->options & LOG_OPT_QUOTE)
2940 LOGCHAR('"');
2941
Willy Tarreau57bc8912016-04-25 17:09:40 +02002942 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002943 chunk.area = "<BADREQ>";
2944 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002945 } else {
2946 uri = txn->uri;
2947 end = uri + strlen(uri);
2948 // look for the first question mark
2949 while (uri < end && *uri != '?')
2950 uri++;
2951
2952 qmark = uri;
2953 // look for first space or question mark after url
2954 while (uri < end && !HTTP_IS_SPHT(*uri))
2955 uri++;
2956
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002957 chunk.area = qmark;
2958 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002959 }
2960
Dragan Dosen835b9212016-02-12 13:23:03 +01002961 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002962 if (ret == NULL || *ret != '\0')
2963 goto out;
2964
2965 tmplog = ret;
2966 if (tmp->options & LOG_OPT_QUOTE)
2967 LOGCHAR('"');
2968
2969 last_isspace = 0;
2970 break;
2971
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002972 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002973 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002974
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 end = uri + strlen(uri);
2979 // look for the first whitespace character
2980 while (uri < end && !HTTP_IS_SPHT(*uri))
2981 uri++;
2982
2983 // keep advancing past multiple spaces
2984 while (uri < end && HTTP_IS_SPHT(*uri)) {
2985 uri++; nspaces++;
2986 }
2987
2988 // look for first space after url
2989 spc = uri;
2990 while (spc < end && !HTTP_IS_SPHT(*spc))
2991 spc++;
2992
Willy Tarreau57bc8912016-04-25 17:09:40 +02002993 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002994 chunk.area = "<BADREQ>";
2995 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002996 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002997 chunk.area = uri;
2998 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002999 }
3000
Dragan Dosen835b9212016-02-12 13:23:03 +01003001 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003002 if (ret == NULL || *ret != '\0')
3003 goto out;
3004
3005 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003006 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003007 LOGCHAR('"');
3008
3009 last_isspace = 0;
3010 break;
3011
3012 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02003013 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003014 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003015 LOGCHAR('"');
3016
3017 end = uri + strlen(uri);
3018 // look for the first whitespace character
3019 spc = uri;
3020 while (spc < end && !HTTP_IS_SPHT(*spc))
3021 spc++;
3022
3023 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003024 chunk.area = "<BADREQ>";
3025 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003026 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003027 chunk.area = uri;
3028 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003029 }
3030
Dragan Dosen835b9212016-02-12 13:23:03 +01003031 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003032 if (ret == NULL || *ret != '\0')
3033 goto out;
3034
3035 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003036 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003037 LOGCHAR('"');
3038
3039 last_isspace = 0;
3040 break;
3041
3042 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02003043 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02003044 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003045 LOGCHAR('"');
3046
3047 end = uri + strlen(uri);
3048 // look for the first whitespace character
3049 while (uri < end && !HTTP_IS_SPHT(*uri))
3050 uri++;
3051
3052 // keep advancing past multiple spaces
3053 while (uri < end && HTTP_IS_SPHT(*uri)) {
3054 uri++; nspaces++;
3055 }
3056
3057 // look for the next whitespace character
3058 while (uri < end && !HTTP_IS_SPHT(*uri))
3059 uri++;
3060
3061 // keep advancing past multiple spaces
3062 while (uri < end && HTTP_IS_SPHT(*uri))
3063 uri++;
3064
Willy Tarreau57bc8912016-04-25 17:09:40 +02003065 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003066 chunk.area = "<BADREQ>";
3067 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003068 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003069 chunk.area = "HTTP/0.9";
3070 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003071 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003072 chunk.area = uri;
3073 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003074 }
3075
Dragan Dosen835b9212016-02-12 13:23:03 +01003076 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003077 if (ret == NULL || *ret != '\0')
3078 goto out;
3079
3080 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02003081 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00003082 LOGCHAR('"');
3083
3084 last_isspace = 0;
3085 break;
3086
William Lallemand5f232402012-04-05 18:02:55 +02003087 case LOG_FMT_COUNTER: // %rt
3088 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003089 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02003090 if (iret < 0 || iret > dst + maxsize - tmplog)
3091 goto out;
3092 last_isspace = 0;
3093 tmplog += iret;
3094 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02003095 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02003096 if (ret == NULL)
3097 goto out;
3098 tmplog = ret;
3099 last_isspace = 0;
3100 }
3101 break;
3102
Willy Tarreau7346acb2014-08-28 15:03:15 +02003103 case LOG_FMT_LOGCNT: // %lc
3104 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003105 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003106 if (iret < 0 || iret > dst + maxsize - tmplog)
3107 goto out;
3108 last_isspace = 0;
3109 tmplog += iret;
3110 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003111 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003112 if (ret == NULL)
3113 goto out;
3114 tmplog = ret;
3115 last_isspace = 0;
3116 }
3117 break;
3118
William Lallemand5f232402012-04-05 18:02:55 +02003119 case LOG_FMT_HOSTNAME: // %H
3120 src = hostname;
3121 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3122 if (ret == NULL)
3123 goto out;
3124 tmplog = ret;
3125 last_isspace = 0;
3126 break;
3127
3128 case LOG_FMT_PID: // %pid
3129 if (tmp->options & LOG_OPT_HEXA) {
3130 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3131 if (iret < 0 || iret > dst + maxsize - tmplog)
3132 goto out;
3133 last_isspace = 0;
3134 tmplog += iret;
3135 } else {
3136 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3137 if (ret == NULL)
3138 goto out;
3139 tmplog = ret;
3140 last_isspace = 0;
3141 }
3142 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003143
3144 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003145 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003146 if (s)
3147 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3148 else
3149 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003150 if (ret == NULL)
3151 goto out;
3152 tmplog = ret;
3153 last_isspace = 0;
3154 break;
3155
William Lallemandbddd4fd2012-02-27 11:23:10 +01003156 }
3157 }
3158
3159out:
William Lallemand1d705562012-03-12 12:46:41 +01003160 /* *tmplog is a unused character */
3161 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003162 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003163
Willy Tarreaubaaee002006-06-26 02:48:02 +02003164}
3165
William Lallemand1d705562012-03-12 12:46:41 +01003166/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003167 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003168 * Will not log if the frontend has no log defined.
3169 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003170void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003171{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003172 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003173 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003174 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003175
3176 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003177 err = (s->flags & SF_REDISP) ||
3178 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3179 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003180 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003181 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003182
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003183 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003184 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003185
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003186 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003187 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003188
Willy Tarreauabcd5142013-06-11 17:18:02 +02003189 if (s->logs.level) { /* loglevel was overridden */
3190 if (s->logs.level == -1) {
3191 s->logs.logwait = 0; /* logs disabled */
3192 return;
3193 }
3194 level = s->logs.level - 1;
3195 }
3196 else {
3197 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003198 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003199 level = LOG_ERR;
3200 }
William Lallemand1d705562012-03-12 12:46:41 +01003201
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003202 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003203 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003204 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003205 }
3206
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003207 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3208 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3209 &sess->fe->logformat_sd);
3210 }
3211
Dragan Dosen59cee972015-09-19 22:09:02 +02003212 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003213 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003214 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003215 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3216 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003217 s->logs.logwait = 0;
3218 }
3219}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003220
Willy Tarreau53839352018-09-05 19:51:10 +02003221/*
3222 * send a minimalist log for the session. Will not log if the frontend has no
3223 * log defined. It is assumed that this is only used to report anomalies that
3224 * cannot lead to the creation of a regular stream. Because of this the log
3225 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3226 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003227 * function to report unimportant events. It is safe to call this function with
3228 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003229 */
3230void sess_log(struct session *sess)
3231{
3232 int size, level;
3233 int sd_size = 0;
3234
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003235 if (!sess)
3236 return;
3237
Willy Tarreau53839352018-09-05 19:51:10 +02003238 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3239 return;
3240
3241 level = LOG_INFO;
3242 if (sess->fe->options2 & PR_O2_LOGERRORS)
3243 level = LOG_ERR;
3244
3245 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3246 sd_size = sess_build_logline(sess, NULL,
3247 logline_rfc5424, global.max_syslog_len,
3248 &sess->fe->logformat_sd);
3249 }
3250
3251 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3252 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003253 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003254 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3255 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003256 }
3257}
3258
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003259void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3260{
3261 va_list argp;
3262 int data_len;
3263
3264 if (level < 0 || format == NULL || logline == NULL)
3265 return;
3266
3267 va_start(argp, format);
3268 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3269 if (data_len < 0 || data_len > global.max_syslog_len)
3270 data_len = global.max_syslog_len;
3271 va_end(argp);
3272
3273 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3274}
Emeric Brun54932b42020-07-07 09:43:24 +02003275/*
3276 * This function parse a received log message <buf>, of size <buflen>
3277 * it fills <level>, <facility> and <metadata> depending of the detected
3278 * header format and message will point on remaining payload of <size>
3279 *
3280 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3281 * struct ist len will be set to 0 if field is not found
3282 * <level> and <facility> will be set to -1 if not found.
3283 */
3284void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3285 struct ist *metadata, char **message, size_t *size)
3286{
3287
3288 char *p;
3289 int fac_level = 0;
3290
3291 *level = *facility = -1;
3292
3293 *message = buf;
3294 *size = buflen;
3295
3296 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3297
3298 p = buf;
3299 if (*size < 2 || *p != '<')
3300 return;
3301
3302 p++;
3303 while (*p != '>') {
3304 if (*p > '9' || *p < '0')
3305 return;
3306 fac_level = 10*fac_level + (*p - '0');
3307 p++;
3308 if ((p - buf) > buflen)
3309 return;
3310 }
3311
3312 *facility = fac_level >> 3;
3313 *level = fac_level & 0x7;
3314 p++;
3315
3316 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3317
3318 buflen -= p - buf;
3319 buf = p;
3320
3321 *size = buflen;
3322 *message = buf;
3323
3324 /* for rfc5424, prio is always followed by '1' and ' ' */
3325 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3326 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3327 * followed by message.
3328 * Each header field can present NILVALUE: '-'
3329 */
3330
3331 p += 2;
3332 /* timestamp is NILVALUE '-' */
3333 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3334 metadata[LOG_META_TIME] = ist2(p, 1);
3335 p++;
3336 }
3337 else if (*size > LOG_ISOTIME_MINLEN) {
3338 metadata[LOG_META_TIME].ptr = p;
3339
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003340 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003341 * in timestamp.
3342 * possible format are:
3343 * ex: '1970-01-01T00:00:00.000000Z'
3344 * '1970-01-01T00:00:00.000000+00:00'
3345 * '1970-01-01T00:00:00.000000-00:00'
3346 * '1970-01-01T00:00:00Z'
3347 * '1970-01-01T00:00:00+00:00'
3348 * '1970-01-01T00:00:00-00:00'
3349 */
3350 p += 19;
3351 if (*p == '.') {
3352 p++;
3353 if ((p - buf) >= buflen)
3354 goto bad_format;
3355 while (*p != 'Z' && *p != '+' && *p != '-') {
3356 if ((unsigned char)(*p - '0') > 9)
3357 goto bad_format;
3358
3359 p++;
3360 if ((p - buf) >= buflen)
3361 goto bad_format;
3362 }
3363 }
3364
3365 if (*p == 'Z')
3366 p++;
3367 else
3368 p += 6; /* case of '+00:00 or '-00:00' */
3369
3370 if ((p - buf) >= buflen || *p != ' ')
3371 goto bad_format;
3372 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3373 }
3374 else
3375 goto bad_format;
3376
3377
3378 p++;
3379 if ((p - buf) >= buflen || *p == ' ')
3380 goto bad_format;
3381
3382 metadata[LOG_META_HOST].ptr = p;
3383 while (*p != ' ') {
3384 p++;
3385 if ((p - buf) >= buflen)
3386 goto bad_format;
3387 }
3388 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3389 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3390 metadata[LOG_META_HOST].len = 0;
3391
3392 p++;
3393 if ((p - buf) >= buflen || *p == ' ')
3394 goto bad_format;
3395
3396 metadata[LOG_META_TAG].ptr = p;
3397 while (*p != ' ') {
3398 p++;
3399 if ((p - buf) >= buflen)
3400 goto bad_format;
3401 }
3402 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3403 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3404 metadata[LOG_META_TAG].len = 0;
3405
3406 p++;
3407 if ((p - buf) >= buflen || *p == ' ')
3408 goto bad_format;
3409
3410 metadata[LOG_META_PID].ptr = p;
3411 while (*p != ' ') {
3412 p++;
3413 if ((p - buf) >= buflen)
3414 goto bad_format;
3415 }
3416 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3417 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3418 metadata[LOG_META_PID].len = 0;
3419
3420 p++;
3421 if ((p - buf) >= buflen || *p == ' ')
3422 goto bad_format;
3423
3424 metadata[LOG_META_MSGID].ptr = p;
3425 while (*p != ' ') {
3426 p++;
3427 if ((p - buf) >= buflen)
3428 goto bad_format;
3429 }
3430 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3431 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3432 metadata[LOG_META_MSGID].len = 0;
3433
3434 p++;
3435 if ((p - buf) >= buflen || *p == ' ')
3436 goto bad_format;
3437
3438 /* structured data format is:
3439 * ex:
3440 * '[key1=value1 key2=value2][key3=value3]'
3441 *
3442 * space is invalid outside [] because
3443 * considered as the end of structured data field
3444 */
3445 metadata[LOG_META_STDATA].ptr = p;
3446 if (*p == '[') {
3447 int elem = 0;
3448
3449 while (1) {
3450 if (elem) {
3451 /* according to rfc this char is escaped in param values */
3452 if (*p == ']' && *(p-1) != '\\')
3453 elem = 0;
3454 }
3455 else {
3456 if (*p == '[')
3457 elem = 1;
3458 else if (*p == ' ')
3459 break;
3460 else
3461 goto bad_format;
3462 }
3463 p++;
3464 if ((p - buf) >= buflen)
3465 goto bad_format;
3466 }
3467 }
3468 else if (*p == '-') {
3469 /* case of NILVALUE */
3470 p++;
3471 if ((p - buf) >= buflen || *p != ' ')
3472 goto bad_format;
3473 }
3474 else
3475 goto bad_format;
3476
3477 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3478 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3479 metadata[LOG_META_STDATA].len = 0;
3480
3481 p++;
3482
3483 buflen -= p - buf;
3484 buf = p;
3485
3486 *size = buflen;
3487 *message = p;
3488 }
3489 else if (*size > LOG_LEGACYTIME_LEN) {
3490 int m;
3491
3492 /* supported header format according to rfc3164.
3493 * ex:
3494 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3495 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3496 * or 'Jan 1 00:00:00 HOSTNAME '
3497 * Note: HOSTNAME is mandatory, and day
3498 * of month uses a single space prefix if
3499 * less than 10 to ensure hour offset is
3500 * always the same.
3501 */
3502
3503 /* Check month to see if it correspond to a rfc3164
3504 * header ex 'Jan 1 00:00:00' */
3505 for (m = 0; m < 12; m++)
3506 if (!memcmp(monthname[m], p, 3))
3507 break;
3508 /* Month not found */
3509 if (m == 12)
3510 goto bad_format;
3511
3512 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3513
3514 p += LOG_LEGACYTIME_LEN;
3515 if ((p - buf) >= buflen || *p != ' ')
3516 goto bad_format;
3517
3518 p++;
3519 if ((p - buf) >= buflen || *p == ' ')
3520 goto bad_format;
3521
3522 metadata[LOG_META_HOST].ptr = p;
3523 while (*p != ' ') {
3524 p++;
3525 if ((p - buf) >= buflen)
3526 goto bad_format;
3527 }
3528 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3529
3530 /* TAG seems to no be mandatory */
3531 p++;
3532
3533 buflen -= p - buf;
3534 buf = p;
3535
3536 *size = buflen;
3537 *message = buf;
3538
3539 if (!buflen)
3540 return;
3541
3542 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3543 p++;
3544
3545 /* a tag must present a trailing ':' */
3546 if (((p - buf) >= buflen) || *p != ':')
3547 return;
3548 p++;
3549 /* followed by a space */
3550 if (((p - buf) >= buflen) || *p != ' ')
3551 return;
3552
3553 /* rewind to parse tag and pid */
3554 p = buf;
3555 metadata[LOG_META_TAG].ptr = p;
3556 /* we have the guarantee that ':' will be reach before size limit */
3557 while (*p != ':') {
3558 if (*p == '[') {
3559 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3560 metadata[LOG_META_PID].ptr = p + 1;
3561 }
3562 else if (*p == ']' && metadata[LOG_META_PID].ptr) {
3563 if (p[1] != ':')
3564 return;
3565 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3566 }
3567 p++;
3568 }
3569 if (!metadata[LOG_META_TAG].len)
3570 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3571
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003572 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003573 p += 2;
3574
3575 buflen -= p - buf;
3576 buf = p;
3577
3578 *size = buflen;
3579 *message = buf;
3580 }
3581
3582 return;
3583
3584bad_format:
3585 /* bad syslog format, we reset all parsed syslog fields
3586 * but priority is kept because we are able to re-build
3587 * this message using LOF_FORMAT_PRIO.
3588 */
3589 metadata[LOG_META_TIME].len = 0;
3590 metadata[LOG_META_HOST].len = 0;
3591 metadata[LOG_META_TAG].len = 0;
3592 metadata[LOG_META_PID].len = 0;
3593 metadata[LOG_META_MSGID].len = 0;
3594 metadata[LOG_META_STDATA].len = 0;
3595
3596 return;
3597}
3598
3599/*
3600 * UDP syslog fd handler
3601 */
3602void syslog_fd_handler(int fd)
3603{
3604 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3605 ssize_t ret = 0;
3606 struct buffer *buf = get_trash_chunk();
3607 size_t size;
3608 char *message;
3609 int level;
3610 int facility;
3611 struct listener *l = objt_listener(fdtab[fd].owner);
3612 int max_accept;
3613
3614 if(!l)
3615 ABORT_NOW();
3616
3617 if (fdtab[fd].ev & FD_POLL_IN) {
3618
3619 if (!fd_recv_ready(fd))
3620 return;
3621
3622 max_accept = l->maxaccept ? l->maxaccept : 1;
3623
3624 do {
3625 /* Source address */
3626 struct sockaddr_storage saddr = {0};
3627 socklen_t saddrlen;
3628
3629 saddrlen = sizeof(saddr);
3630
3631 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3632 if (ret < 0) {
3633 if (errno == EINTR)
3634 continue;
3635 if (errno == EAGAIN)
3636 fd_cant_recv(fd);
3637 goto out;
3638 }
3639 buf->data = ret;
3640
Emeric Brun45c457a2020-07-09 23:23:34 +02003641 /* update counters */
3642 _HA_ATOMIC_ADD(&cum_log_messages, 1);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003643 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003644
Emeric Brun54932b42020-07-07 09:43:24 +02003645 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3646
3647 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3648
3649 } while (--max_accept);
3650 }
3651
3652out:
3653 return;
3654}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003655
Emeric Brun12941c82020-07-07 14:19:42 +02003656/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003657 * IO Handler to handle message exchange with a syslog tcp client
3658 */
3659static void syslog_io_handler(struct appctx *appctx)
3660{
3661 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3662 struct stream_interface *si = appctx->owner;
3663 struct stream *s = si_strm(si);
3664 struct proxy *frontend = strm_fe(s);
3665 struct listener *l = strm_li(s);
3666 struct buffer *buf = get_trash_chunk();
3667 int max_accept;
3668 int to_skip;
3669 int facility;
3670 int level;
3671 char *message;
3672 size_t size;
3673
3674 max_accept = l->maxaccept ? l->maxaccept : 1;
3675 while (co_data(si_oc(si))) {
3676 char c;
3677
3678 if (max_accept <= 0)
3679 goto missing_budget;
3680 max_accept--;
3681
3682 to_skip = co_getchar(si_oc(si), &c);
3683 if (!to_skip)
3684 goto missing_data;
3685 else if (to_skip < 0)
3686 goto cli_abort;
3687
3688 if (c == '<') {
3689 /* rfc-6587, Non-Transparent-Framing: messages separated by
3690 * a trailing LF or CR LF
3691 */
3692 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3693 if (!to_skip)
3694 goto missing_data;
3695 else if (to_skip < 0)
3696 goto cli_abort;
3697
3698 if (buf->area[to_skip - 1] != '\n')
3699 goto parse_error;
3700
3701 buf->data = to_skip - 1;
3702
3703 /* according to rfc-6587, some devices adds CR before LF */
3704 if (buf->data && buf->area[buf->data - 1] == '\r')
3705 buf->data--;
3706
3707 }
3708 else if ((unsigned char)(c - '1') <= 8) {
3709 /* rfc-6587, Octet-Counting: message length in ASCII
3710 * (first digit can not be ZERO), followed by a space
3711 * and message length
3712 */
3713 char *p = NULL;
3714 int msglen;
3715
3716 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3717 if (!to_skip)
3718 goto missing_data;
3719 else if (to_skip < 0)
3720 goto cli_abort;
3721
3722 if (buf->area[to_skip - 1] != ' ')
3723 goto parse_error;
3724
3725 msglen = strtol(trash.area, &p, 10);
3726 if (!msglen || p != &buf->area[to_skip - 1])
3727 goto parse_error;
3728
3729 /* message seems too large */
3730 if (msglen > buf->size)
3731 goto parse_error;
3732
3733 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3734 if (!msglen)
3735 goto missing_data;
3736 else if (msglen < 0)
3737 goto cli_abort;
3738
3739
3740 buf->data = msglen;
3741 to_skip += msglen;
3742 }
3743 else
3744 goto parse_error;
3745
3746 co_skip(si_oc(si), to_skip);
3747
3748 /* update counters */
3749 _HA_ATOMIC_ADD(&cum_log_messages, 1);
3750 proxy_inc_fe_req_ctr(l, frontend);
3751
3752 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3753
3754 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3755
3756 }
3757
3758missing_data:
3759 /* we need more data to read */
3760 si_oc(si)->flags |= CF_READ_DONTWAIT;
3761
3762 return;
3763
3764missing_budget:
3765 /* it may remain some stuff to do, let's retry later */
3766 appctx_wakeup(appctx);
3767
3768 return;
3769
3770parse_error:
3771 if (l->counters)
3772 _HA_ATOMIC_ADD(&l->counters->failed_req, 1);
3773 _HA_ATOMIC_ADD(&frontend->fe_counters.failed_req, 1);
3774
3775 goto close;
3776
3777cli_abort:
3778 if (l->counters)
3779 _HA_ATOMIC_ADD(&l->counters->cli_aborts, 1);
3780 _HA_ATOMIC_ADD(&frontend->fe_counters.cli_aborts, 1);
3781
3782close:
3783 si_shutw(si);
3784 si_shutr(si);
3785
3786 si_ic(si)->flags |= CF_READ_NULL;
3787
3788 return;
3789}
3790
3791static struct applet syslog_applet = {
3792 .obj_type = OBJ_TYPE_APPLET,
3793 .name = "<SYSLOG>", /* used for logging */
3794 .fct = syslog_io_handler,
3795 .release = NULL,
3796};
3797
3798/*
Emeric Brun12941c82020-07-07 14:19:42 +02003799 * Parse "log-forward" section and create corresponding sink buffer.
3800 *
3801 * The function returns 0 in success case, otherwise, it returns error
3802 * flags.
3803 */
3804int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3805{
3806 int err_code = 0;
3807 struct proxy *px;
3808 char *errmsg = NULL;
3809 const char *err = NULL;
3810
3811 if (strcmp(args[0], "log-forward") == 0) {
3812 if (!*args[1]) {
3813 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3814 err_code |= ERR_ALERT | ERR_ABORT;
3815 goto out;
3816 }
3817
3818 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3819 goto out;
3820
3821 err = invalid_char(args[1]);
3822 if (err) {
3823 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3824 file, linenum, *err, args[0], args[1]);
3825 err_code |= ERR_ALERT | ERR_ABORT;
3826 goto out;
3827 }
3828
Emeric Brunb0c331f2020-10-07 17:05:59 +02003829 px = log_forward_by_name(args[1]);
3830 if (px) {
3831 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3832 file, linenum, args[1], px->conf.file, px->conf.line);
3833 err_code |= ERR_ALERT | ERR_FATAL;
3834 }
3835
3836 px = proxy_find_by_name(args[1], 0, 0);
3837 if (px) {
3838 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3839 file, linenum, args[1], proxy_type_str(px),
3840 px->id, px->conf.file, px->conf.line);
3841 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003842 }
3843
3844 px = calloc(1, sizeof *px);
3845 if (!px) {
3846 err_code |= ERR_ALERT | ERR_FATAL;
3847 goto out;
3848 }
3849
3850 px->next = cfg_log_forward;
3851 cfg_log_forward = px;
3852
3853 init_new_proxy(px);
3854 px->conf.file = strdup(file);
3855 px->conf.line = linenum;
3856 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003857 px->last_change = now.tv_sec;
3858 px->cap = PR_CAP_FE;
3859 px->maxconn = 10;
3860 px->timeout.client = TICK_ETERNITY;
3861 px->accept = frontend_accept;
3862 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003863 px->id = strdup(args[1]);
3864
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003865 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003866 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003867 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3868 err_code |= ERR_WARN;
3869
3870 if (*(args[1]) == 0) {
3871 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3872 err_code |= ERR_ALERT | ERR_FATAL;
3873 goto out;
3874 }
3875 cfg_log_forward->maxconn = atol(args[1]);
3876 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3877 goto out;
3878 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003879 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003880 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3881 err_code |= ERR_WARN;
3882
3883 if (*(args[1]) == 0) {
3884 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3885 err_code |= ERR_ALERT | ERR_FATAL;
3886 goto out;
3887 }
3888 cfg_log_forward->backlog = atol(args[1]);
3889 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3890 goto out;
3891 }
3892 else if (strcmp(args[0], "bind") == 0) {
3893 int cur_arg;
3894 static int kws_dumped;
3895 struct bind_conf *bind_conf;
3896 struct bind_kw *kw;
3897 struct listener *l;
3898
3899 cur_arg = 1;
3900
3901 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3902 NULL, xprt_get(XPRT_RAW));
3903 if (!bind_conf) {
3904 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3905 err_code |= ERR_ALERT | ERR_FATAL;
3906 goto out;
3907 }
3908
3909 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3910 if (errmsg && *errmsg) {
3911 indent_msg(&errmsg, 2);
3912 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3913 }
3914 else {
3915 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3916 file, linenum, args[0], args[1], args[2]);
3917 err_code |= ERR_ALERT | ERR_FATAL;
3918 goto out;
3919 }
3920 }
3921 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
3922 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : 64;
3923 l->accept = session_accept_fd;
3924 l->analysers |= cfg_log_forward->fe_req_ana;
3925 l->default_target = cfg_log_forward->default_target;
3926 global.maxsock++;
3927 }
3928 cur_arg++;
3929
3930 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3931 int ret;
3932
3933 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3934 err_code |= ret;
3935 if (ret) {
3936 if (errmsg && *errmsg) {
3937 indent_msg(&errmsg, 2);
3938 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3939 }
3940 else
3941 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3942 file, linenum, args[cur_arg]);
3943 if (ret & ERR_FATAL)
3944 goto out;
3945 }
3946 cur_arg += 1 + kw->skip;
3947 }
3948 if (*args[cur_arg] != 0) {
3949 char *kws = NULL;
3950
3951 if (!kws_dumped) {
3952 kws_dumped = 1;
3953 bind_dump_kws(&kws);
3954 indent_msg(&kws, 4);
3955 }
3956 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.%s%s\n",
3957 file, linenum, args[cur_arg], cursection,
3958 kws ? " Registered keywords :" : "", kws ? kws: "");
3959 free(kws);
3960 err_code |= ERR_ALERT | ERR_FATAL;
3961 goto out;
3962 }
Emeric Brun12941c82020-07-07 14:19:42 +02003963 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003964 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003965 int cur_arg;
3966 static int kws_dumped;
3967 struct bind_conf *bind_conf;
3968 struct bind_kw *kw;
3969 struct listener *l;
3970
3971 cur_arg = 1;
3972
3973 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3974 NULL, xprt_get(XPRT_RAW));
3975
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003976 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003977 if (errmsg && *errmsg) {
3978 indent_msg(&errmsg, 2);
3979 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3980 }
3981 else {
3982 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3983 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003984 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003985 err_code |= ERR_ALERT | ERR_FATAL;
3986 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003987 }
3988 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003989 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Emeric Brun12941c82020-07-07 14:19:42 +02003990 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : 64;
Willy Tarreaue140a692020-10-15 21:25:32 +02003991 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003992 global.maxsock++;
3993 }
3994 cur_arg++;
3995
3996 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3997 int ret;
3998
3999 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
4000 err_code |= ret;
4001 if (ret) {
4002 if (errmsg && *errmsg) {
4003 indent_msg(&errmsg, 2);
4004 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
4005 }
4006 else
4007 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
4008 file, linenum, args[cur_arg]);
4009 if (ret & ERR_FATAL)
4010 goto out;
4011 }
4012 cur_arg += 1 + kw->skip;
4013 }
4014 if (*args[cur_arg] != 0) {
4015 char *kws = NULL;
4016
4017 if (!kws_dumped) {
4018 kws_dumped = 1;
4019 bind_dump_kws(&kws);
4020 indent_msg(&kws, 4);
4021 }
4022 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.%s%s\n",
4023 file, linenum, args[cur_arg], cursection,
4024 kws ? " Registered keywords :" : "", kws ? kws: "");
4025 free(kws);
4026 err_code |= ERR_ALERT | ERR_FATAL;
4027 goto out;
4028 }
4029 }
4030 else if (strcmp(args[0], "log") == 0) {
4031 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), &errmsg)) {
4032 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4033 err_code |= ERR_ALERT | ERR_FATAL;
4034 goto out;
4035 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02004036 }
4037 else if (strcmp(args[0], "timeout") == 0) {
4038 const char *res;
4039 unsigned timeout;
4040
4041 if (strcmp(args[1], "client") != 0) {
4042 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
4043 err_code |= ERR_ALERT | ERR_FATAL;
4044 goto out;
4045 }
4046
4047 if (*args[2] == 0) {
4048 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
4049 err_code |= ERR_ALERT | ERR_FATAL;
4050 goto out;
4051 }
4052 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
4053 if (res == PARSE_TIME_OVER) {
4054 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
4055 }
4056 else if (res == PARSE_TIME_UNDER) {
4057 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
4058 }
4059 else if (res) {
4060 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
4061 return -1;
4062 }
4063
4064 if (res) {
4065 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
4066 err_code |= ERR_ALERT | ERR_FATAL;
4067 goto out;
4068 }
4069 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02004070 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02004071 else {
4072 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
4073 err_code |= ERR_ALERT | ERR_ABORT;
4074 goto out;
4075 }
Emeric Brun12941c82020-07-07 14:19:42 +02004076out:
4077 return err_code;
4078}
4079
4080
Willy Tarreau869efd52019-11-15 15:16:57 +01004081/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
4082static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004083{
Willy Tarreau869efd52019-11-15 15:16:57 +01004084 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4085 return 1;
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004086
Willy Tarreau869efd52019-11-15 15:16:57 +01004087 if (!startup_logs)
4088 return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
4089
4090 return ring_attach_cli(startup_logs, appctx);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004091}
4092
4093/* register cli keywords */
4094static struct cli_kw_list cli_kws = {{ },{
4095 { { "show", "startup-logs", NULL },
Willy Tarreau869efd52019-11-15 15:16:57 +01004096 "show startup-logs : report logs emitted during HAProxy startup", cli_parse_show_startup_logs, NULL, NULL },
Christopher Fauletc1b730a2017-10-24 12:00:51 +02004097 {{},}
4098}};
4099
Willy Tarreau0108d902018-11-25 19:14:37 +01004100INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4101
Emeric Brun12941c82020-07-07 14:19:42 +02004102/* config parsers for this section */
4103REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
4104
Willy Tarreau082b6282019-05-22 14:42:12 +02004105REGISTER_PER_THREAD_ALLOC(init_log_buffers);
4106REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01004107
Willy Tarreaubaaee002006-06-26 02:48:02 +02004108/*
4109 * Local variables:
4110 * c-indent-level: 8
4111 * c-basic-offset: 8
4112 * End:
4113 */