blob: ecbc7ab55975d4effa8726dcae8490909d40eebf [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 Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020028#include <common/compat.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010029#include <common/initcall.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020031#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Christopher Fauletc1b730a2017-10-24 12:00:51 +020033#include <types/cli.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010035#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020036
Christopher Fauletc1b730a2017-10-24 12:00:51 +020037#include <proto/applet.h>
38#include <proto/cli.h>
William Lallemand5f232402012-04-05 18:02:55 +020039#include <proto/frontend.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020040#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010041#include <proto/sample.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020042#include <proto/ssl_sock.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020043#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010044#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
Dragan Dosen43885c72015-10-01 13:18:13 +020046struct log_fmt {
47 char *name;
48 struct {
Willy Tarreau83061a82018-07-13 11:56:34 +020049 struct buffer sep1; /* first pid separator */
50 struct buffer sep2; /* second pid separator */
Dragan Dosen43885c72015-10-01 13:18:13 +020051 } pid;
52};
53
54static const struct log_fmt log_formats[LOG_FORMATS] = {
55 [LOG_FORMAT_RFC3164] = {
56 .name = "rfc3164",
57 .pid = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020058 .sep1 = { .area = "[", .data = 1 },
59 .sep2 = { .area = "]: ", .data = 3 }
Dragan Dosen43885c72015-10-01 13:18:13 +020060 }
61 },
62 [LOG_FORMAT_RFC5424] = {
63 .name = "rfc5424",
64 .pid = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020065 .sep1 = { .area = " ", .data = 1 },
66 .sep2 = { .area = " - ", .data = 3 }
Dragan Dosen43885c72015-10-01 13:18:13 +020067 }
Willy Tarreaue8746a02018-11-12 08:45:00 +010068 },
69 [LOG_FORMAT_SHORT] = {
70 .name = "short",
71 .pid = {
72 .sep1 = { .area = "", .data = 0 },
73 .sep2 = { .area = " ", .data = 1 },
74 }
75 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010076 [LOG_FORMAT_RAW] = {
77 .name = "raw",
78 .pid = {
79 .sep1 = { .area = "", .data = 0 },
80 .sep2 = { .area = "", .data = 0 },
81 }
82 },
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 */
137 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, 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 */
Willy Tarreau2beef582012-12-20 17:22:52 +0100151 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
152 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200153 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100154 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200155 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100156 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
157 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200158 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200159 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
160 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100161 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
162 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200163 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
164 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100165 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200166 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
167 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
168 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
169 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000170 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
171 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000172 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000173 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
174 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200175 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100176 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200177 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100178 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
179 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100180 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100181 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
182 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
183 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
184 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
185 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200186 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
187 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100188 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200189 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
190 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
191 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100192 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
193 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
194
195 /* The following tags are deprecated and will be removed soon */
196 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
197 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200198 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */
199 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */
200 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */
201 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100202 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
203 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
204 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
205 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
206 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200207 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100208};
209
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200210char 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
211char 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 +0100212char 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 +0100213char *log_format = NULL;
214
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200215/* Default string used for structured-data part in RFC5424 formatted
216 * syslog messages.
217 */
218char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200219
Willy Tarreau13ef7732018-11-12 07:25:28 +0100220/* total number of dropped logs */
221unsigned int dropped_logs = 0;
222
Dragan Dosen1322d092015-09-22 16:05:32 +0200223/* This is a global syslog header, common to all outgoing messages in
224 * RFC3164 format. It begins with time-based part and is updated by
225 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200226 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200227THREAD_LOCAL char *logheader = NULL;
Willy Tarreau55e2f5a2019-05-05 10:11:39 +0200228THREAD_LOCAL char *logheader_end = NULL;
Dragan Dosen59cee972015-09-19 22:09:02 +0200229
Dragan Dosen1322d092015-09-22 16:05:32 +0200230/* This is a global syslog header for messages in RFC5424 format. It is
231 * updated by update_log_hdr_rfc5424().
232 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200233THREAD_LOCAL char *logheader_rfc5424 = NULL;
Willy Tarreau55e2f5a2019-05-05 10:11:39 +0200234THREAD_LOCAL char *logheader_rfc5424_end = NULL;
Dragan Dosen1322d092015-09-22 16:05:32 +0200235
Dragan Dosen59cee972015-09-19 22:09:02 +0200236/* This is a global syslog message buffer, common to all outgoing
237 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100238 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200239THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100240
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200241/* A global syslog message buffer, common to all RFC5424 syslog messages.
242 * Currently, it is used for generating the structured-data part.
243 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200244THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200245
Christopher Fauletd4696382017-10-24 11:44:05 +0200246/* A global buffer used to store all startup alerts/warnings. It will then be
247 * retrieve on the CLI. */
Willy Tarreaua6483992018-12-15 16:55:36 +0100248static char *startup_logs = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +0200249
William Lallemand723b73a2012-02-08 16:37:49 +0100250struct logformat_var_args {
251 char *name;
252 int mask;
253};
254
255struct logformat_var_args var_args_list[] = {
256// global
257 { "M", LOG_OPT_MANDATORY },
258 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200259 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100260 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100261 { 0, 0 }
262};
263
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200264/* return the name of the directive used in the current proxy for which we're
265 * currently parsing a header, when it is known.
266 */
267static inline const char *fmt_directive(const struct proxy *curproxy)
268{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100269 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200270 case ARGC_ACL:
271 return "acl";
272 case ARGC_STK:
273 return "stick";
274 case ARGC_TRK:
275 return "track-sc";
276 case ARGC_LOG:
277 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200278 case ARGC_LOGSD:
279 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100280 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100281 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100282 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100283 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200284 case ARGC_UIF:
285 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100286 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200287 return "redirect";
288 case ARGC_CAP:
289 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200290 case ARGC_SRV:
291 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200292 case ARGC_SPOE:
293 return "spoe-message";
Thierry FOURNIER / OZON.IO4ed1c952016-11-24 23:57:54 +0100294 case ARGC_UBK:
295 return "use_backend";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100296 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200297 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100298 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200299}
300
William Lallemand723b73a2012-02-08 16:37:49 +0100301/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100302 * callback used to configure addr source retrieval
303 */
304int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
305{
306 curproxy->options2 |= PR_O2_SRC_ADDR;
307
308 return 0;
309}
310
311
312/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100313 * Parse args in a logformat_var. Returns 0 in error
314 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100315 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100316int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100317{
318 int i = 0;
319 int end = 0;
320 int flags = 0; // 1 = + 2 = -
321 char *sp = NULL; // start pointer
322
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100323 if (args == NULL) {
324 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100325 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100326 }
William Lallemand723b73a2012-02-08 16:37:49 +0100327
328 while (1) {
329 if (*args == '\0')
330 end = 1;
331
332 if (*args == '+') {
333 // add flag
334 sp = args + 1;
335 flags = 1;
336 }
337 if (*args == '-') {
338 // delete flag
339 sp = args + 1;
340 flags = 2;
341 }
342
343 if (*args == '\0' || *args == ',') {
344 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100345 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100346 if (strcmp(sp, var_args_list[i].name) == 0) {
347 if (flags == 1) {
348 node->options |= var_args_list[i].mask;
349 break;
350 } else if (flags == 2) {
351 node->options &= ~var_args_list[i].mask;
352 break;
353 }
354 }
355 }
356 sp = NULL;
357 if (end)
358 break;
359 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100360 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100361 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100362 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100363}
364
365/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100366 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
367 * must pass the args part in the <arg> pointer with its length in <arg_len>,
368 * and varname with its length in <var> and <var_len> respectively. <arg> is
369 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100370 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100371 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100372int 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 +0100373{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100374 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200375 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100376
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100377 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
378 if (strlen(logformat_keywords[j].name) == var_len &&
379 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
380 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200381 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100382 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100383 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200384 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100385 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100386 node->type = logformat_keywords[j].type;
387 node->options = *defoptions;
388 if (arg_len) {
389 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100390 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200391 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100392 }
393 if (node->type == LOG_FMT_GLOBAL) {
394 *defoptions = node->options;
395 free(node->arg);
396 free(node);
397 } else {
398 if (logformat_keywords[j].config_callback &&
399 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200400 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100401 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100402 curproxy->to_log |= logformat_keywords[j].lw;
403 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100404 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100405 if (logformat_keywords[j].replace_by)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100406 ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
407 curproxy->conf.args.file, curproxy->conf.args.line,
408 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100409 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100410 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100411 memprintf(err, "format variable '%s' is reserved for HTTP mode",
412 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200413 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100414 }
William Lallemand723b73a2012-02-08 16:37:49 +0100415 }
416 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100417
418 j = var[var_len];
419 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100420 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 +0100421 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200422
423 error_free:
424 if (node) {
425 free(node->arg);
426 free(node);
427 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100428 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100429}
430
431/*
432 * push to the logformat linked list
433 *
434 * start: start pointer
435 * end: end text pointer
436 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100437 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100438 *
439 * LOG_TEXT: copy chars from start to end excluding end.
440 *
441*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100442int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100443{
444 char *str;
445
Willy Tarreaua3571662012-12-20 21:59:12 +0100446 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200447 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100448 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100449 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100450 return 0;
451 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200452 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100453 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100454 str[end - start] = '\0';
455 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100456 node->type = LOG_FMT_TEXT; // type string
457 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100458 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200459 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100460 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100461 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100462 return 0;
463 }
William Lallemand1d705562012-03-12 12:46:41 +0100464 node->type = LOG_FMT_SEPARATOR;
465 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100466 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100467 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100468}
469
470/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100471 * Parse the sample fetch expression <text> and add a node to <list_format> upon
472 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200473 * should work. The curpx->conf.args.ctx must be set by the caller.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100474 *
475 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100476 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100477int 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)
Willy Tarreauc8368452012-12-21 00:09:23 +0100478{
479 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200480 struct sample_expr *expr = NULL;
481 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100482 int cmd_arg;
483
484 cmd[0] = text;
485 cmd[1] = "";
486 cmd_arg = 0;
487
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100488 expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100489 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100490 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200491 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100492 }
493
Vincent Bernat02779b62016-04-03 13:48:43 +0200494 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100495 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100496 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200497 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100498 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100499 node->type = LOG_FMT_EXPR;
500 node->expr = expr;
501 node->options = options;
502
503 if (arg_len) {
504 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100505 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200506 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100507 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100508 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100509 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
510
Willy Tarreau434c57c2013-01-08 01:10:24 +0100511 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100512 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
513
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100514 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100515 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
516 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200517 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100518 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100519
Willy Tarreauc8368452012-12-21 00:09:23 +0100520 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
521 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100522 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100523
William Lallemand65ad6e12014-01-31 15:08:02 +0100524 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
525 * needed with some sample fetches (eg: ssl*). We always set it for
526 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100527 */
528 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100529 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100530 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100531 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200532
533 error_free:
534 release_sample_expr(expr);
535 if (node) {
536 free(node->arg);
537 free(node);
538 }
539 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100540}
541
542/*
William Lallemand723b73a2012-02-08 16:37:49 +0100543 * Parse the log_format string and fill a linked list.
544 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200545 * You can set arguments using { } : %{many arguments}varname.
546 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100547 *
548 * str: the string to parse
549 * curproxy: the proxy affected
550 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100551 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100552 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100553 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100554 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100555 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100556int 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 +0100557{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100558 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100559 char *arg = NULL; /* start pointer for args */
560 char *var = NULL; /* start pointer for vars */
561 int arg_len = 0;
562 int var_len = 0;
563 int cformat; /* current token format */
564 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100565 struct logformat_node *tmplf, *back;
566
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100567 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100568 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100569 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100570 return 0;
571 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200572 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200573
William Lallemand723b73a2012-02-08 16:37:49 +0100574 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100575 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100576 LIST_DEL(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200577 release_sample_expr(tmplf->expr);
578 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100579 free(tmplf);
580 }
581
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100582 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100583 pformat = cformat;
584
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100585 if (!*str)
586 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100587
Joseph Herlant85b40592018-11-15 12:10:04 -0800588 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100589 * second have all common paths processed at one place. The common paths are the ones
590 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
591 * We use the common LF_INIT state to dispatch to the different final states.
592 */
593 switch (pformat) {
594 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100595 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100596 arg_len = var_len = 0;
597 if (*str == '{') { // optional argument
598 cformat = LF_STARG;
599 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100600 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100601 else if (*str == '[') {
602 cformat = LF_STEXPR;
603 var = str + 1; // store expr in variable name
604 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100605 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100606 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100607 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100608 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100609 else if (*str == '%')
610 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100611 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100612 /* single '%' followed by blank or digit, send them both */
613 cformat = LF_TEXT;
614 pformat = LF_TEXT; /* finally we include the previous char as well */
615 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600616 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 +0100617 *str, (int)(str - backfmt), fmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100618 return 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100619
620 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100621 else
622 cformat = LF_INIT; // handle other cases of litterals
623 break;
624
625 case LF_STARG: // text immediately following '%{'
626 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100627 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100628 arg_len = str - arg;
629 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100630 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100631 break;
632
633 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100634 if (*str == '[') {
635 cformat = LF_STEXPR;
636 var = str + 1; // store expr in variable name
637 break;
638 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100639 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100640 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100641 var = str;
642 break;
643 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100644 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100645 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100646
Willy Tarreauc8368452012-12-21 00:09:23 +0100647 case LF_STEXPR: // text immediately following '%['
648 if (*str == ']') { // end of arg
649 cformat = LF_EDEXPR;
650 var_len = str - var;
651 *str = 0; // needed for parsing the expression
652 }
653 break;
654
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100655 case LF_VAR: // text part of a variable name
656 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100657 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100658 cformat = LF_INIT; // not variable name anymore
659 break;
660
Willy Tarreauc8368452012-12-21 00:09:23 +0100661 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100662 cformat = LF_INIT;
663 }
664
665 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
666 switch (*str) {
667 case '%': cformat = LF_STARTVAR; break;
668 case ' ': cformat = LF_SEPARATOR; break;
669 case 0 : cformat = LF_END; break;
670 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100671 }
672 }
673
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100674 if (cformat != pformat || pformat == LF_SEPARATOR) {
675 switch (pformat) {
676 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100677 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100678 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100679 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100680 case LF_STEXPR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100681 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100682 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100683 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100684 case LF_TEXT:
685 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100686 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100687 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100688 break;
689 }
690 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100691 }
692 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100693
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100694 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100695 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100696 return 0;
697 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100698 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100699
700 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100701}
702
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200703/*
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200704 * Parse the first range of indexes from a string made of a list of comma seperated
705 * ranges of indexes. Note that an index may be considered as a particular range
706 * with a high limit to the low limit.
707 */
708int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
709{
710 char *end, *p;
711
712 *low = *high = 0;
713
714 p = *arg;
715 end = strchr(p, ',');
716 if (!end)
717 end = p + strlen(p);
718
719 *high = *low = read_uint((const char **)&p, end);
720 if (!*low || (p != end && *p != '-'))
721 goto err;
722
723 if (p == end)
724 goto done;
725
726 p++;
727 *high = read_uint((const char **)&p, end);
728 if (!*high || *high <= *low || p != end)
729 goto err;
730
731 done:
732 if (*end == ',')
733 end++;
734 *arg = end;
735 return 1;
736
737 err:
738 memprintf(err, "wrong sample range '%s'", *arg);
739 return 0;
740}
741
742/*
743 * Returns 1 if the range defined by <low> and <high> overlaps
744 * one of them in <rgs> array of ranges with <sz> the size of this
745 * array, 0 if not.
746 */
747int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
748 unsigned int low, unsigned int high, char **err)
749{
750 size_t i;
751
752 for (i = 0; i < sz; i++) {
753 if ((low >= rgs[i].low && low <= rgs[i].high) ||
754 (high >= rgs[i].low && high <= rgs[i].high)) {
755 memprintf(err, "ranges are overlapping");
756 return 1;
757 }
758 }
759
760 return 0;
761}
762
763int smp_log_range_cmp(const void *a, const void *b)
764{
765 const struct smp_log_range *rg_a = a;
766 const struct smp_log_range *rg_b = b;
767
768 if (rg_a->high < rg_b->low)
769 return -1;
770 else if (rg_a->low > rg_b->high)
771 return 1;
772
773 return 0;
774}
775
776/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200777 * Parse "log" keyword and update <logsrvs> list accordingly.
778 *
779 * When <do_del> is set, it means the "no log" line was parsed, so all log
780 * servers in <logsrvs> are released.
781 *
782 * Otherwise, we try to parse the "log" line. First of all, when the list is not
783 * the global one, we look for the parameter "global". If we find it,
784 * global.logsrvs is copied. Else we parse each arguments.
785 *
786 * The function returns 1 in success case, otherwise, it returns 0 and err is
787 * filled.
788 */
789int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
790{
791 struct sockaddr_storage *sk;
792 struct logsrv *logsrv = NULL;
793 int port1, port2;
794 int cur_arg;
795
796 /*
797 * "no log": delete previous herited or defined syslog
798 * servers.
799 */
800 if (do_del) {
801 struct logsrv *back;
802
803 if (*(args[1]) != 0) {
804 memprintf(err, "'no log' does not expect arguments");
805 goto error;
806 }
807
808 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
809 LIST_DEL(&logsrv->list);
810 free(logsrv);
811 }
812 return 1;
813 }
814
815 /*
816 * "log global": copy global.logrsvs linked list to the end of logsrvs
817 * list. But first, we check (logsrvs != global.logsrvs).
818 */
819 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
820 if (logsrvs == &global.logsrvs) {
821 memprintf(err, "'global' is not supported for a global syslog server");
822 goto error;
823 }
824 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200825 struct logsrv *node;
826
827 list_for_each_entry(node, logsrvs, list) {
828 if (node->ref == logsrv)
829 goto skip_logsrv;
830 }
831
832 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200833 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200834 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200835 LIST_INIT(&node->list);
836 LIST_ADDQ(logsrvs, &node->list);
Christopher Faulet28ac0992018-03-26 16:09:19 +0200837
838 skip_logsrv:
839 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200840 }
841 return 1;
842 }
843
844 /*
845 * "log <address> ...: parse a syslog server line
846 */
847 if (*(args[1]) == 0 || *(args[2]) == 0) {
848 memprintf(err, "expects <address> and <facility> %s as arguments",
849 ((logsrvs == &global.logsrvs) ? "" : "or global"));
850 goto error;
851 }
852
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100853 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
854 if (strcmp(args[1], "stdout") == 0)
855 args[1] = "fd@1";
856 else if (strcmp(args[1], "stderr") == 0)
857 args[1] = "fd@2";
858
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200859 logsrv = calloc(1, sizeof(*logsrv));
860 if (!logsrv) {
861 memprintf(err, "out of memory");
862 goto error;
863 }
864
865 /* skip address for now, it will be parsed at the end */
866 cur_arg = 2;
867
868 /* just after the address, a length may be specified */
869 logsrv->maxlen = MAX_SYSLOG_LEN;
870 if (strcmp(args[cur_arg], "len") == 0) {
871 int len = atoi(args[cur_arg+1]);
872 if (len < 80 || len > 65535) {
873 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
874 args[cur_arg+1]);
875 goto error;
876 }
877 logsrv->maxlen = len;
878 cur_arg += 2;
879 }
880 if (logsrv->maxlen > global.max_syslog_len)
881 global.max_syslog_len = logsrv->maxlen;
882
883 /* after the length, a format may be specified */
884 if (strcmp(args[cur_arg], "format") == 0) {
885 logsrv->format = get_log_format(args[cur_arg+1]);
886 if (logsrv->format < 0) {
887 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
888 goto error;
889 }
890 cur_arg += 2;
891 }
892
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200893 if (strcmp(args[cur_arg], "sample") == 0) {
894 unsigned low, high;
895 char *p, *beg, *end, *smp_sz_str;
896 struct smp_log_range *smp_rgs = NULL;
897 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
898
899 p = args[cur_arg+1];
900 smp_sz_str = strchr(p, ':');
901 if (!smp_sz_str) {
902 memprintf(err, "Missing sample size");
903 goto error;
904 }
905
906 *smp_sz_str++ = '\0';
907
908 end = p + strlen(p);
909
910 while (p != end) {
911 if (!get_logsrv_smp_range(&low, &high, &p, err))
912 goto error;
913
914 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
915 goto error;
916
917 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
918 if (!smp_rgs) {
919 memprintf(err, "out of memory error");
920 goto error;
921 }
922
923 smp_rgs[smp_rgs_sz].low = low;
924 smp_rgs[smp_rgs_sz].high = high;
925 smp_rgs[smp_rgs_sz].sz = high - low + 1;
926 smp_rgs[smp_rgs_sz].curr_idx = 0;
927 if (smp_rgs[smp_rgs_sz].high > smp_sz)
928 smp_sz = smp_rgs[smp_rgs_sz].high;
929 smp_rgs_sz++;
930 }
931
932 beg = smp_sz_str;
933 end = beg + strlen(beg);
934 new_smp_sz = read_uint((const char **)&beg, end);
935 if (!new_smp_sz || beg != end) {
936 memprintf(err, "wrong sample size '%s' for sample range '%s'",
937 smp_sz_str, args[cur_arg+1]);
938 goto error;
939 }
940
941 if (new_smp_sz < smp_sz) {
942 memprintf(err, "sample size %zu should be greater or equal to "
943 "%zu the maximum of the high ranges limits",
944 new_smp_sz, smp_sz);
945 goto error;
946 }
947 smp_sz = new_smp_sz;
948
949 /* Let's order <smp_rgs> array. */
950 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
951
952 logsrv->lb.smp_rgs = smp_rgs;
953 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
954 logsrv->lb.smp_sz = smp_sz;
955
956 cur_arg += 2;
957 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200958 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200959 /* parse the facility */
960 logsrv->facility = get_log_facility(args[cur_arg]);
961 if (logsrv->facility < 0) {
962 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
963 goto error;
964 }
965 cur_arg++;
966
967 /* parse the max syslog level (default: debug) */
968 logsrv->level = 7;
969 if (*(args[cur_arg])) {
970 logsrv->level = get_log_level(args[cur_arg]);
971 if (logsrv->level < 0) {
972 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
973 goto error;
974 }
975 cur_arg++;
976 }
977
978 /* parse the limit syslog level (default: emerg) */
979 logsrv->minlvl = 0;
980 if (*(args[cur_arg])) {
981 logsrv->minlvl = get_log_level(args[cur_arg]);
982 if (logsrv->minlvl < 0) {
983 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
984 goto error;
985 }
986 cur_arg++;
987 }
988
989 /* Too many args */
990 if (*(args[cur_arg])) {
991 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
992 goto error;
993 }
994
995 /* now, back to the address */
996 sk = str2sa_range(args[1], NULL, &port1, &port2, err, NULL, NULL, 1);
997 if (!sk)
998 goto error;
999 logsrv->addr = *sk;
1000
1001 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
1002 if (port1 != port2) {
1003 memprintf(err, "port ranges and offsets are not allowed in '%s'", args[1]);
1004 goto error;
1005 }
1006 logsrv->addr = *sk;
1007 if (!port1)
1008 set_host_port(&logsrv->addr, SYSLOG_PORT);
1009 }
1010 LIST_ADDQ(logsrvs, &logsrv->list);
1011 return 1;
1012
1013 error:
1014 free(logsrv);
1015 return 0;
1016}
1017
1018
Christopher Fauletd4696382017-10-24 11:44:05 +02001019/* Generic function to display messages prefixed by a label */
1020static void print_message(const char *label, const char *fmt, va_list argp)
1021{
1022 struct tm tm;
1023 char *head, *msg;
1024
1025 head = msg = NULL;
1026
1027 get_localtime(date.tv_sec, &tm);
1028 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
1029 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
1030 memvprintf(&msg, fmt, argp);
1031
1032 if (global.mode & MODE_STARTING)
1033 memprintf(&startup_logs, "%s%s%s", (startup_logs ? startup_logs : ""), head, msg);
1034
1035 fprintf(stderr, "%s%s", head, msg);
1036 fflush(stderr);
1037
1038 free(head);
1039 free(msg);
1040}
1041
Willy Tarreaubaaee002006-06-26 02:48:02 +02001042/*
1043 * Displays the message on stderr with the date and pid. Overrides the quiet
1044 * mode during startup.
1045 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001046void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001047{
1048 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001049
1050 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
1051 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001052 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001053 va_end(argp);
1054 }
1055}
1056
1057
1058/*
1059 * Displays the message on stderr with the date and pid.
1060 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001061void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062{
1063 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064
1065 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1066 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001067 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001068 va_end(argp);
1069 }
1070}
1071
1072/*
William Lallemand9c56a222018-11-21 18:04:52 +01001073 * Displays the message on stderr with the date and pid.
1074 */
1075void ha_notice(const char *fmt, ...)
1076{
1077 va_list argp;
1078
1079 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1080 va_start(argp, fmt);
1081 print_message("NOTICE", fmt, argp);
1082 va_end(argp);
1083 }
1084}
1085
1086/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001087 * Displays the message on <out> only if quiet mode is not set.
1088 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001089void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001090{
1091 va_list argp;
1092
1093 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1094 va_start(argp, fmt);
1095 vfprintf(out, fmt, argp);
1096 fflush(out);
1097 va_end(argp);
1098 }
1099}
1100
1101/*
Dragan Dosen1322d092015-09-22 16:05:32 +02001102 * returns log format for <fmt> or -1 if not found.
1103 */
1104int get_log_format(const char *fmt)
1105{
1106 int format;
1107
1108 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +02001109 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +02001110 format--;
1111
1112 return format;
1113}
1114
1115/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001116 * returns log level for <lev> or -1 if not found.
1117 */
1118int get_log_level(const char *lev)
1119{
1120 int level;
1121
1122 level = NB_LOG_LEVELS - 1;
1123 while (level >= 0 && strcmp(log_levels[level], lev))
1124 level--;
1125
1126 return level;
1127}
1128
Willy Tarreaubaaee002006-06-26 02:48:02 +02001129/*
1130 * returns log facility for <fac> or -1 if not found.
1131 */
1132int get_log_facility(const char *fac)
1133{
1134 int facility;
1135
1136 facility = NB_LOG_FACILITIES - 1;
1137 while (facility >= 0 && strcmp(log_facilities[facility], fac))
1138 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001139
Willy Tarreaubaaee002006-06-26 02:48:02 +02001140 return facility;
1141}
1142
William Lallemanda1cc3812012-02-08 16:38:44 +01001143/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001144 * Encode the string.
1145 *
1146 * When using the +E log format option, it will try to escape '"\]'
1147 * characters with '\' as prefix. The same prefix should not be used as
1148 * <escape>.
1149 */
1150static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001151 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001152 const char *string,
1153 struct logformat_node *node)
1154{
1155 if (node->options & LOG_OPT_ESC) {
1156 if (start < stop) {
1157 stop--; /* reserve one byte for the final '\0' */
1158 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001159 if (!ha_bit_test((unsigned char)(*string), map)) {
1160 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001161 *start++ = *string;
1162 else {
1163 if (start + 2 >= stop)
1164 break;
1165 *start++ = '\\';
1166 *start++ = *string;
1167 }
1168 }
1169 else {
1170 if (start + 3 >= stop)
1171 break;
1172 *start++ = escape;
1173 *start++ = hextab[(*string >> 4) & 15];
1174 *start++ = hextab[*string & 15];
1175 }
1176 string++;
1177 }
1178 *start = '\0';
1179 }
1180 }
1181 else {
1182 return encode_string(start, stop, escape, map, string);
1183 }
1184
1185 return start;
1186}
1187
1188/*
1189 * Encode the chunk.
1190 *
1191 * When using the +E log format option, it will try to escape '"\]'
1192 * characters with '\' as prefix. The same prefix should not be used as
1193 * <escape>.
1194 */
1195static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001196 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001197 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001198 struct logformat_node *node)
1199{
1200 char *str, *end;
1201
1202 if (node->options & LOG_OPT_ESC) {
1203 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001204 str = chunk->area;
1205 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001206
1207 stop--; /* reserve one byte for the final '\0' */
1208 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001209 if (!ha_bit_test((unsigned char)(*str), map)) {
1210 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001211 *start++ = *str;
1212 else {
1213 if (start + 2 >= stop)
1214 break;
1215 *start++ = '\\';
1216 *start++ = *str;
1217 }
1218 }
1219 else {
1220 if (start + 3 >= stop)
1221 break;
1222 *start++ = escape;
1223 *start++ = hextab[(*str >> 4) & 15];
1224 *start++ = hextab[*str & 15];
1225 }
1226 str++;
1227 }
1228 *start = '\0';
1229 }
1230 }
1231 else {
1232 return encode_chunk(start, stop, escape, map, chunk);
1233 }
1234
1235 return start;
1236}
1237
1238/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001239 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001240 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001241 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001242 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001243 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001244char *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 +01001245{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001246 if (size < 2)
1247 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001248
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001249 if (node->options & LOG_OPT_QUOTE) {
1250 *(dst++) = '"';
1251 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001252 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001253
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001254 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001255 if (++len > size)
1256 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001257 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001258 char *ret;
1259
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001260 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001261 if (ret == NULL || *ret != '\0')
1262 return NULL;
1263 len = ret - dst;
1264 }
1265 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001266 len = strlcpy2(dst, src, len);
1267 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001268
1269 size -= len;
1270 dst += len;
1271 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001272 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1273 if (size < 2)
1274 return NULL;
1275 *(dst++) = '-';
1276 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001277
1278 if (node->options & LOG_OPT_QUOTE) {
1279 if (size < 2)
1280 return NULL;
1281 *(dst++) = '"';
1282 }
1283
1284 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001285 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001286}
1287
Willy Tarreau26ffa852018-09-05 15:23:10 +02001288static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001289{
1290 return lf_text_len(dst, src, size, size, node);
1291}
1292
William Lallemand5f232402012-04-05 18:02:55 +02001293/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001294 * Write a IP address to the log string
William Lallemand5f232402012-04-05 18:02:55 +02001295 * +X option write in hexadecimal notation, most signifant byte on the left
1296 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001297char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001298{
1299 char *ret = dst;
1300 int iret;
1301 char pn[INET6_ADDRSTRLEN];
1302
1303 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001304 unsigned char *addr = NULL;
1305 switch (sockaddr->sa_family) {
1306 case AF_INET:
1307 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1308 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1309 break;
1310 case AF_INET6:
1311 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1312 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1313 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1314 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1315 break;
1316 default:
1317 return NULL;
1318 }
William Lallemand5f232402012-04-05 18:02:55 +02001319 if (iret < 0 || iret > size)
1320 return NULL;
1321 ret += iret;
1322 } else {
1323 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1324 ret = lf_text(dst, pn, size, node);
1325 if (ret == NULL)
1326 return NULL;
1327 }
1328 return ret;
1329}
1330
1331/*
1332 * Write a port to the log
1333 * +X option write in hexadecimal notation, most signifant byte on the left
1334 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001335char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001336{
1337 char *ret = dst;
1338 int iret;
1339
1340 if (node->options & LOG_OPT_HEXA) {
1341 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1342 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1343 if (iret < 0 || iret > size)
1344 return NULL;
1345 ret += iret;
1346 } else {
1347 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1348 if (ret == NULL)
1349 return NULL;
1350 }
1351 return ret;
1352}
1353
Dragan Dosen1322d092015-09-22 16:05:32 +02001354/* Re-generate time-based part of the syslog header in RFC3164 format at
1355 * the beginning of logheader once a second and return the pointer to the
1356 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001357 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001358static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001359{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001360 static THREAD_LOCAL long tvsec;
Willy Tarreau83061a82018-07-13 11:56:34 +02001361 static THREAD_LOCAL struct buffer host = { };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001362 static THREAD_LOCAL int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001363
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001364 if (unlikely(time != tvsec || logheader_end == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001365 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +02001366 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001367 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +02001368
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001369 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +02001370 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001371
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001372 if (unlikely(global.log_send_hostname != host.area)) {
1373 host.area = global.log_send_hostname;
1374 host.data = host.area ? strlen(host.area) : 0;
1375 sep = host.data ? 1 : 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001376 }
1377
Dragan Dosen59cee972015-09-19 22:09:02 +02001378 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +02001379 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +02001380 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +02001381 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001382 (int)host.data, host.area, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001383 /* WARNING: depending upon implementations, snprintf may return
1384 * either -1 or the number of bytes that would be needed to store
1385 * the total message. In both cases, we must adjust it.
1386 */
Willy Tarreau18324f52014-06-27 18:10:07 +02001387 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1388 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001389
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001390 logheader_end = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001391 }
1392
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001393 logheader_end[0] = 0; // ensure we get rid of any previous attempt
Willy Tarreau094af4e2015-01-07 15:03:42 +01001394
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001395 return logheader_end;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001396}
1397
Dragan Dosen1322d092015-09-22 16:05:32 +02001398/* Re-generate time-based part of the syslog header in RFC5424 format at
1399 * the beginning of logheader_rfc5424 once a second and return the pointer
1400 * to the first character after it.
1401 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001402static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +02001403{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001404 static THREAD_LOCAL long tvsec;
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001405 const char *gmt_offset;
Dragan Dosen1322d092015-09-22 16:05:32 +02001406
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001407 if (unlikely(time != tvsec || logheader_rfc5424_end == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +02001408 /* this string is rebuild only once a second */
1409 struct tm tm;
1410 int hdr_len;
1411
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001412 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +02001413 get_localtime(tvsec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001414 gmt_offset = get_gmt_offset(time, &tm);
Dragan Dosen1322d092015-09-22 16:05:32 +02001415
1416 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +02001417 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +02001418 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +02001419 tm.tm_hour, tm.tm_min, tm.tm_sec,
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001420 gmt_offset, gmt_offset+3,
Dragan Dosen43885c72015-10-01 13:18:13 +02001421 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +02001422 /* WARNING: depending upon implementations, snprintf may return
1423 * either -1 or the number of bytes that would be needed to store
1424 * the total message. In both cases, we must adjust it.
1425 */
1426 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1427 hdr_len = global.max_syslog_len;
1428
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001429 logheader_rfc5424_end = logheader_rfc5424 + hdr_len;
Dragan Dosen1322d092015-09-22 16:05:32 +02001430 }
1431
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001432 logheader_rfc5424_end[0] = 0; // ensure we get rid of any previous attempt
Dragan Dosen1322d092015-09-22 16:05:32 +02001433
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001434 return logheader_rfc5424_end;
Dragan Dosen1322d092015-09-22 16:05:32 +02001435}
1436
William Lallemand2a4a44f2012-02-06 16:00:33 +01001437/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001438 * This function sends the syslog message using a printf format string. It
1439 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001440 */
1441void send_log(struct proxy *p, int level, const char *format, ...)
1442{
1443 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001444 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001445
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001446 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001447 return;
1448
William Lallemand2a4a44f2012-02-06 16:00:33 +01001449 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001450 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001451 if (data_len < 0 || data_len > global.max_syslog_len)
1452 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001453 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001454
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001455 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001456}
1457
1458/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001459 * This function sends a syslog message to <logsrv>.
1460 * <pid_str> is the string to be used for the PID of the caller, <pid_size> is length.
1461 * Same thing for <sd> and <sd_size> which are used for the structured-data part
1462 * in RFC5424 formatted syslog messages, and <tag_str> and <tag_size> the syslog tag.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001463 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001464 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001465 */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001466static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_str, size_t pid_size,
1467 int level, char *message, size_t size, char *sd, size_t sd_size,
1468 char *tag_str, size_t tag_size)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001469{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001470 static THREAD_LOCAL struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1471 static THREAD_LOCAL struct msghdr msghdr = {
1472 //.msg_iov = iovec,
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001473 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1474 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001475 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1476 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
1477 static THREAD_LOCAL char *dataptr = NULL;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001478 time_t time = date.tv_sec;
Dragan Dosen1322d092015-09-22 16:05:32 +02001479 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001480 size_t hdr_size;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001481 int fac_level;
1482 int *plogfd;
1483 char *pid_sep1 = "", *pid_sep2 = "";
1484 char logheader_short[3];
1485 int sent;
1486 int maxlen;
1487 int hdr_max = 0;
1488 int tag_max = 0;
1489 int pid_sep1_max = 0;
Frédéric Lécaille90a10ae2019-05-14 10:57:58 +02001490 int pid_max = 0;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001491 int pid_sep2_max = 0;
1492 int sd_max = 0;
1493 int max = 0;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001494
1495 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001496
1497 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001498
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001499 if (logsrv->addr.ss_family == AF_UNSPEC) {
1500 /* the socket's address is a file descriptor */
1501 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1502 if (unlikely(!((struct sockaddr_in *)&logsrv->addr)->sin_port)) {
1503 /* FD not yet initialized to non-blocking mode.
1504 * DON'T DO IT ON A TERMINAL!
1505 */
1506 if (!isatty(*plogfd))
1507 fcntl(*plogfd, F_SETFL, O_NONBLOCK);
1508 ((struct sockaddr_in *)&logsrv->addr)->sin_port = 1;
Dragan Dosen43885c72015-10-01 13:18:13 +02001509 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001510 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001511 else if (logsrv->addr.ss_family == AF_UNIX)
1512 plogfd = &logfdunix;
1513 else
1514 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001515
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001516 if (unlikely(*plogfd < 0)) {
1517 /* socket not successfully initialized yet */
1518 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1519 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1520 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001521
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001522 if (!once) {
1523 once = 1; /* note: no need for atomic ops here */
1524 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1525 nblogger, strerror(errno), errno);
1526 }
1527 return;
1528 } else {
1529 /* we don't want to receive anything on this socket */
1530 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1531 /* does nothing under Linux, maybe needed for others */
1532 shutdown(*plogfd, SHUT_RD);
1533 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1534 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001535 }
1536
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001537 switch (logsrv->format) {
1538 case LOG_FORMAT_RFC3164:
1539 hdr = logheader;
1540 hdr_ptr = update_log_hdr(time);
1541 break;
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001542
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001543 case LOG_FORMAT_RFC5424:
1544 hdr = logheader_rfc5424;
1545 hdr_ptr = update_log_hdr_rfc5424(time);
1546 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
1547 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001548
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001549 case LOG_FORMAT_SHORT:
1550 /* all fields are known, skip the header generation */
1551 hdr = logheader_short;
1552 hdr[0] = '<';
1553 hdr[1] = '0' + MAX(level, logsrv->minlvl);
1554 hdr[2] = '>';
1555 hdr_ptr = hdr;
1556 hdr_max = 3;
1557 maxlen = logsrv->maxlen - hdr_max;
1558 max = MIN(size, maxlen) - 1;
1559 goto send;
Willy Tarreau204e3f12018-12-15 15:48:48 +01001560
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001561 case LOG_FORMAT_RAW:
1562 /* all fields are known, skip the header generation */
1563 hdr_ptr = hdr = "";
1564 hdr_max = 0;
1565 maxlen = logsrv->maxlen;
1566 max = MIN(size, maxlen) - 1;
1567 goto send;
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001568
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001569 default:
1570 return; /* must never happen */
1571 }
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001572
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001573 hdr_size = hdr_ptr - hdr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001574
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001575 /* For each target, we may have a different facility.
1576 * We can also have a different log level for each message.
1577 * This induces variations in the message header length.
1578 * Since we don't want to recompute it each time, nor copy it every
1579 * time, we only change the facility in the pre-computed header,
1580 * and we change the pointer to the header accordingly.
1581 */
1582 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
1583 hdr_ptr = hdr + 3; /* last digit of the log level */
1584 do {
1585 *hdr_ptr = '0' + fac_level % 10;
1586 fac_level /= 10;
1587 hdr_ptr--;
1588 } while (fac_level && hdr_ptr > hdr);
1589 *hdr_ptr = '<';
Dragan Dosen1322d092015-09-22 16:05:32 +02001590
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001591 hdr_max = hdr_size - (hdr_ptr - hdr);
Willy Tarreaue8746a02018-11-12 08:45:00 +01001592
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001593 /* time-based header */
1594 if (unlikely(hdr_size >= logsrv->maxlen)) {
1595 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
1596 sd_max = 0;
1597 goto send;
1598 }
Willy Tarreauc1b06452018-11-12 11:57:56 +01001599
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001600 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen1322d092015-09-22 16:05:32 +02001601
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001602 /* tag */
Frédéric Lécaille90a10ae2019-05-14 10:57:58 +02001603 tag_max = tag_size;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001604 if (unlikely(tag_max >= maxlen)) {
1605 tag_max = maxlen - 1;
1606 sd_max = 0;
1607 goto send;
1608 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001609
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001610 maxlen -= tag_max;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001611
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001612 /* first pid separator */
1613 pid_sep1_max = log_formats[logsrv->format].pid.sep1.data;
1614 if (unlikely(pid_sep1_max >= maxlen)) {
1615 pid_sep1_max = maxlen - 1;
1616 sd_max = 0;
1617 goto send;
1618 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001619
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001620 pid_sep1 = log_formats[logsrv->format].pid.sep1.area;
1621 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001622
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001623 /* pid */
Frédéric Lécaille90a10ae2019-05-14 10:57:58 +02001624 pid_max = pid_size;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001625 if (unlikely(pid_size >= maxlen)) {
1626 pid_size = maxlen - 1;
1627 sd_max = 0;
1628 goto send;
1629 }
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001630
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001631 maxlen -= pid_size;
Dragan Dosen43885c72015-10-01 13:18:13 +02001632
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001633 /* second pid separator */
1634 pid_sep2_max = log_formats[logsrv->format].pid.sep2.data;
1635 if (unlikely(pid_sep2_max >= maxlen)) {
1636 pid_sep2_max = maxlen - 1;
1637 sd_max = 0;
1638 goto send;
1639 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001640
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001641 pid_sep2 = log_formats[logsrv->format].pid.sep2.area;
1642 maxlen -= pid_sep2_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001643
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001644 /* structured-data */
1645 if (sd_max >= maxlen) {
1646 sd_max = maxlen - 1;
1647 goto send;
1648 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001649
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001650 max = MIN(size, maxlen - sd_max) - 1;
1651send:
1652 iovec[0].iov_base = hdr_ptr;
1653 iovec[0].iov_len = hdr_max;
1654 iovec[1].iov_base = tag_str;
Frédéric Lécaille90a10ae2019-05-14 10:57:58 +02001655 iovec[1].iov_len = tag_max;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001656 iovec[2].iov_base = pid_sep1;
1657 iovec[2].iov_len = pid_sep1_max;
1658 iovec[3].iov_base = pid_str;
Frédéric Lécaille90a10ae2019-05-14 10:57:58 +02001659 iovec[3].iov_len = pid_max;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001660 iovec[4].iov_base = pid_sep2;
1661 iovec[4].iov_len = pid_sep2_max;
1662 iovec[5].iov_base = sd;
1663 iovec[5].iov_len = sd_max;
1664 iovec[6].iov_base = dataptr;
1665 iovec[6].iov_len = max;
1666 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1667 iovec[7].iov_len = 1;
Dragan Dosen43885c72015-10-01 13:18:13 +02001668
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001669 if (logsrv->addr.ss_family == AF_UNSPEC) {
1670 /* the target is a direct file descriptor */
1671 sent = writev(*plogfd, iovec, 8);
1672 }
1673 else {
1674 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1675 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001676
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001677 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1678 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001679
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001680 if (sent < 0) {
1681 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001682
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001683 if (errno == EAGAIN)
1684 _HA_ATOMIC_ADD(&dropped_logs, 1);
1685 else if (!once) {
1686 once = 1; /* note: no need for atomic ops here */
1687 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1688 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001689 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001690 }
1691}
Dragan Dosen59cee972015-09-19 22:09:02 +02001692
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001693/*
1694 * This function sends a syslog message.
1695 * It doesn't care about errors nor does it report them.
1696 * The arguments <sd> and <sd_size> are used for the structured-data part
1697 * in RFC5424 formatted syslog messages.
1698 */
1699void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size)
1700{
1701 struct list *logsrvs = NULL;
1702 struct logsrv *logsrv;
1703 int nblogger;
1704 static THREAD_LOCAL int curr_pid;
1705 static THREAD_LOCAL char pidstr[100];
1706 static THREAD_LOCAL struct buffer pid;
1707 struct buffer *tag = &global.log_tag;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001708
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001709 if (p == NULL) {
1710 if (!LIST_ISEMPTY(&global.logsrvs)) {
1711 logsrvs = &global.logsrvs;
Willy Tarreau5a32ecc2018-11-12 07:34:59 +01001712 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001713 } else {
1714 if (!LIST_ISEMPTY(&p->logsrvs)) {
1715 logsrvs = &p->logsrvs;
Willy Tarreau5a32ecc2018-11-12 07:34:59 +01001716 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001717 if (p->log_tag.area) {
1718 tag = &p->log_tag;
1719 }
1720 }
Willy Tarreau18324f52014-06-27 18:10:07 +02001721
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001722 if (!logsrvs)
1723 return;
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001724
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001725 if (unlikely(curr_pid != getpid())) {
1726 curr_pid = getpid();
1727 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1728 chunk_initstr(&pid, pidstr);
1729 }
1730
1731 /* Send log messages to syslog server. */
1732 nblogger = 0;
1733 list_for_each_entry(logsrv, logsrvs, list) {
Frédéric Lécailled803e472019-04-25 07:42:09 +02001734 static THREAD_LOCAL int in_range = 1;
1735
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001736 /* we can filter the level of the messages that are sent to each logger */
1737 if (level > logsrv->level)
1738 continue;
1739
Frédéric Lécailled803e472019-04-25 07:42:09 +02001740 if (logsrv->lb.smp_rgs) {
1741 struct smp_log_range *curr_rg;
1742
1743 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1744 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1745 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1746 if (in_range) {
1747 /* Let's consume this range. */
1748 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1749 if (!curr_rg->curr_idx) {
1750 /* If consumed, let's select the next range. */
1751 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1752 }
1753 }
1754 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1755 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1756 }
1757 if (in_range)
1758 __do_send_log(logsrv, ++nblogger, pid.area, pid.data, level,
1759 message, size, sd, sd_size, tag->area, tag->data);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001760 }
1761}
1762
1763
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001764const 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 +01001765const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1766 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1767 Set-cookie Updated, unknown, unknown */
1768
William Lallemand1d705562012-03-12 12:46:41 +01001769/*
1770 * try to write a character if there is enough space, or goto out
1771 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001772#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001773 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001774 *(tmplog++) = (x); \
1775 } else { \
1776 goto out; \
1777 } \
1778 } while(0)
1779
Dragan Dosen835b9212016-02-12 13:23:03 +01001780
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001781/* Initializes some log data at boot */
1782static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001783{
1784 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001785 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001786
1787 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1788 * inside PARAM-VALUE should be escaped with '\' as prefix.
1789 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1790 * details.
1791 */
1792 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1793
1794 tmp = "\"\\]";
1795 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001796 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001797 tmp++;
1798 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001799
1800 /* initialize the log header encoding map : '{|}"#' should be encoded with
1801 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1802 * URL encoding only requires '"', '#' to be encoded as well as non-
1803 * printable characters above.
1804 */
1805 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1806 memset(url_encode_map, 0, sizeof(url_encode_map));
1807 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001808 ha_bit_set(i, hdr_encode_map);
1809 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001810 }
1811 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001812 ha_bit_set(i, hdr_encode_map);
1813 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001814 }
1815
1816 tmp = "\"#{|}";
1817 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001818 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001819 tmp++;
1820 }
1821
1822 tmp = "\"#";
1823 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001824 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001825 tmp++;
1826 }
1827
1828 /* initialize the http header encoding map. The draft httpbis define the
1829 * header content as:
1830 *
1831 * HTTP-message = start-line
1832 * *( header-field CRLF )
1833 * CRLF
1834 * [ message-body ]
1835 * header-field = field-name ":" OWS field-value OWS
1836 * field-value = *( field-content / obs-fold )
1837 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1838 * obs-fold = CRLF 1*( SP / HTAB )
1839 * field-vchar = VCHAR / obs-text
1840 * VCHAR = %x21-7E
1841 * obs-text = %x80-FF
1842 *
1843 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1844 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08001845 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02001846 */
1847 memset(http_encode_map, 0, sizeof(http_encode_map));
1848 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001849 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001850 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001851 ha_bit_set(i, http_encode_map);
1852 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001853}
William Lallemand1d705562012-03-12 12:46:41 +01001854
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001855INITCALL0(STG_PREPARE, init_log);
1856
Christopher Faulet0132d062017-07-26 15:33:35 +02001857/* Initialize log buffers used for syslog messages */
1858int init_log_buffers()
1859{
1860 logheader = my_realloc2(logheader, global.max_syslog_len + 1);
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001861 logheader_end = NULL;
Christopher Faulet0132d062017-07-26 15:33:35 +02001862 logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1);
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001863 logheader_rfc5424_end = NULL;
Christopher Faulet0132d062017-07-26 15:33:35 +02001864 logline = my_realloc2(logline, global.max_syslog_len + 1);
1865 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
1866 if (!logheader || !logline_rfc5424 || !logline || !logline_rfc5424)
1867 return 0;
1868 return 1;
1869}
1870
1871/* Deinitialize log buffers used for syslog messages */
1872void deinit_log_buffers()
1873{
Olivier Houchard7c497112019-03-07 14:19:24 +01001874 void *tmp_startup_logs;
1875
Christopher Faulet0132d062017-07-26 15:33:35 +02001876 free(logheader);
1877 free(logheader_rfc5424);
1878 free(logline);
1879 free(logline_rfc5424);
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01001880 tmp_startup_logs = _HA_ATOMIC_XCHG(&startup_logs, NULL);
Olivier Houchard7c497112019-03-07 14:19:24 +01001881 free(tmp_startup_logs);
1882
Christopher Faulet0132d062017-07-26 15:33:35 +02001883 logheader = NULL;
1884 logheader_rfc5424 = NULL;
1885 logline = NULL;
1886 logline_rfc5424 = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +02001887 startup_logs = NULL;
Christopher Faulet0132d062017-07-26 15:33:35 +02001888}
1889
Willy Tarreaudf974472012-12-28 02:44:01 +01001890/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1891 * <maxsize> characters. Returns the size of the output string in characters,
1892 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001893 * is not zero. It requires a valid session and optionally a stream. If the
1894 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01001895 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001896int 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 +02001897{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001898 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001899 struct proxy *be;
1900 struct http_txn *txn;
1901 const struct strm_logs *logs;
1902 const struct connection *be_conn;
1903 unsigned int s_flags;
1904 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02001905 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001906 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001907 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001908 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001909 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001910 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001911 int t_request;
1912 int hdr;
1913 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001914 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001915 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001916 char *ret;
1917 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001918 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001919 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001920 struct strm_logs tmp_strm_log;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001921
William Lallemandbddd4fd2012-02-27 11:23:10 +01001922 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001923
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001924 if (likely(s)) {
1925 be = s->be;
1926 txn = s->txn;
1927 be_conn = cs_conn(objt_cs(s->si[1].end));
1928 s_flags = s->flags;
1929 uniq_id = s->uniq_id;
1930 logs = &s->logs;
1931 } else {
1932 /* we have no stream so we first need to initialize a few
1933 * things that are needed later. We do increment the request
1934 * ID so that it's uniquely assigned to this request just as
1935 * if the request had reached the point of being processed.
1936 * A request error is reported as it's the only element we have
1937 * here and which justifies emitting such a log.
1938 */
1939 be = fe;
1940 txn = NULL;
1941 be_conn = NULL;
1942 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01001943 uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001944
1945 /* prepare a valid log structure */
1946 tmp_strm_log.tv_accept = sess->tv_accept;
1947 tmp_strm_log.accept_date = sess->accept_date;
1948 tmp_strm_log.t_handshake = sess->t_handshake;
1949 tmp_strm_log.t_idle = tv_ms_elapsed(&sess->tv_accept, &now) - sess->t_handshake;
1950 tv_zero(&tmp_strm_log.tv_request);
1951 tmp_strm_log.t_queue = -1;
1952 tmp_strm_log.t_connect = -1;
1953 tmp_strm_log.t_data = -1;
1954 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
1955 tmp_strm_log.bytes_in = 0;
1956 tmp_strm_log.bytes_out = 0;
1957 tmp_strm_log.prx_queue_pos = 0;
1958 tmp_strm_log.srv_queue_pos = 0;
1959
1960 logs = &tmp_strm_log;
1961 }
1962
William Lallemandbddd4fd2012-02-27 11:23:10 +01001963 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02001964 if (tv_isge(&logs->tv_request, &logs->tv_accept))
1965 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01001966
William Lallemand1d705562012-03-12 12:46:41 +01001967 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001968
William Lallemandbddd4fd2012-02-27 11:23:10 +01001969 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001970 if (LIST_ISEMPTY(list_format))
1971 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001972
William Lallemand1d705562012-03-12 12:46:41 +01001973 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001974 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001975 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001976 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02001977 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001978
Willy Tarreauc8368452012-12-21 00:09:23 +01001979 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001980 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001981 if (!last_isspace) {
1982 LOGCHAR(' ');
1983 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001984 }
1985 break;
1986
William Lallemand1d705562012-03-12 12:46:41 +01001987 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001988 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001989 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001990 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001991 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001992 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001993 last_isspace = 0;
1994 break;
1995
Willy Tarreauc8368452012-12-21 00:09:23 +01001996 case LOG_FMT_EXPR: // sample expression, may be request or response
1997 key = NULL;
Olivier Houchardf90db442018-12-15 14:00:06 +01001998 if (tmp->options & LOG_OPT_REQ_CAP && s)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001999 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Olivier Houchardf90db442018-12-15 14:00:06 +01002000 if (!key && (tmp->options & LOG_OPT_RES_CAP) && s)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002001 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 +01002002 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002003 ret = lf_encode_chunk(tmplog, dst + maxsize,
2004 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002005 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002006 ret = lf_text_len(tmplog,
2007 key ? key->data.u.str.area : NULL,
2008 key ? key->data.u.str.data : 0,
2009 dst + maxsize - tmplog,
2010 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002011 if (ret == 0)
2012 goto out;
2013 tmplog = ret;
2014 last_isspace = 0;
2015 break;
2016
Willy Tarreau2beef582012-12-20 17:22:52 +01002017 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002018 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002019 if (conn)
2020 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
2021 else
2022 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002023 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002024 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002025 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002026 last_isspace = 0;
2027 break;
2028
Willy Tarreau2beef582012-12-20 17:22:52 +01002029 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002030 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002031 if (conn) {
2032 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002033 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002034 } else {
2035 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
2036 dst + maxsize - tmplog, tmp);
2037 }
William Lallemand5f232402012-04-05 18:02:55 +02002038 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002039 else
2040 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2041
William Lallemand5f232402012-04-05 18:02:55 +02002042 if (ret == NULL)
2043 goto out;
2044 tmplog = ret;
2045 last_isspace = 0;
2046 break;
2047
Willy Tarreau2beef582012-12-20 17:22:52 +01002048 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002049 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002050 if (conn) {
2051 conn_get_to_addr(conn);
2052 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
2053 }
2054 else
2055 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2056
William Lallemand1d705562012-03-12 12:46:41 +01002057 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002058 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002059 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002060 last_isspace = 0;
2061 break;
2062
Willy Tarreau2beef582012-12-20 17:22:52 +01002063 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002064 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002065 if (conn) {
2066 conn_get_to_addr(conn);
2067 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002068 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002069 else
2070 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002071 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002072 else
2073 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2074
William Lallemand5f232402012-04-05 18:02:55 +02002075 if (ret == NULL)
2076 goto out;
2077 tmplog = ret;
2078 last_isspace = 0;
2079 break;
2080
Willy Tarreau2beef582012-12-20 17:22:52 +01002081 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau2393c5b2018-09-05 15:24:56 +02002082 if (be_conn)
2083 ret = lf_ip(tmplog, (const struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002084 else
2085 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2086
William Lallemand1d705562012-03-12 12:46:41 +01002087 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002088 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002089 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002090 last_isspace = 0;
2091 break;
2092
Willy Tarreau2beef582012-12-20 17:22:52 +01002093 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau2393c5b2018-09-05 15:24:56 +02002094 if (be_conn)
2095 ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002096 else
2097 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2098
William Lallemand5f232402012-04-05 18:02:55 +02002099 if (ret == NULL)
2100 goto out;
2101 tmplog = ret;
2102 last_isspace = 0;
2103 break;
2104
Willy Tarreau2beef582012-12-20 17:22:52 +01002105 case LOG_FMT_SERVERIP: // %si
Willy Tarreau2393c5b2018-09-05 15:24:56 +02002106 if (be_conn)
2107 ret = lf_ip(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002108 else
2109 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2110
William Lallemand5f232402012-04-05 18:02:55 +02002111 if (ret == NULL)
2112 goto out;
2113 tmplog = ret;
2114 last_isspace = 0;
2115 break;
2116
Willy Tarreau2beef582012-12-20 17:22:52 +01002117 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau2393c5b2018-09-05 15:24:56 +02002118 if (be_conn)
2119 ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002120 else
2121 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2122
William Lallemand1d705562012-03-12 12:46:41 +01002123 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002124 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002125 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002126 last_isspace = 0;
2127 break;
2128
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002129 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002130 get_localtime(logs->accept_date.tv_sec, &tm);
2131 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002132 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002133 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002134 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002135 last_isspace = 0;
2136 break;
2137
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002138 case LOG_FMT_tr: // %tr = start of request date
2139 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002140 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 +02002141 get_localtime(tv.tv_sec, &tm);
2142 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2143 if (ret == NULL)
2144 goto out;
2145 tmplog = ret;
2146 last_isspace = 0;
2147 break;
2148
2149 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002150 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002151 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002152 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002153 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002154 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002155 last_isspace = 0;
2156 break;
2157
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002158 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002159 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 +02002160 get_gmtime(tv.tv_sec, &tm);
2161 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2162 if (ret == NULL)
2163 goto out;
2164 tmplog = ret;
2165 last_isspace = 0;
2166 break;
2167
2168 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002169 get_localtime(logs->accept_date.tv_sec, &tm);
2170 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002171 if (ret == NULL)
2172 goto out;
2173 tmplog = ret;
2174 last_isspace = 0;
2175 break;
2176
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002177 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002178 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 +02002179 get_localtime(tv.tv_sec, &tm);
2180 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2181 if (ret == NULL)
2182 goto out;
2183 tmplog = ret;
2184 last_isspace = 0;
2185 break;
2186
William Lallemand5f232402012-04-05 18:02:55 +02002187 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002188 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002189 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002190 if (iret < 0 || iret > dst + maxsize - tmplog)
2191 goto out;
2192 last_isspace = 0;
2193 tmplog += iret;
2194 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002195 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002196 if (ret == NULL)
2197 goto out;
2198 tmplog = ret;
2199 last_isspace = 0;
2200 }
2201 break;
2202
William Lallemand1d705562012-03-12 12:46:41 +01002203 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002204 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002205 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002206 if (iret < 0 || iret > dst + maxsize - tmplog)
2207 goto out;
2208 last_isspace = 0;
2209 tmplog += iret;
2210 } else {
2211 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002212 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002213 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002214 tmplog, 4);
2215 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002216 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002217 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002218 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002219 }
2220 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002221
William Lallemand1d705562012-03-12 12:46:41 +01002222 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002223 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002224 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002225 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002226 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002227 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002228 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002229 break;
2230
Willy Tarreau773d65f2012-10-12 14:56:11 +02002231 case LOG_FMT_FRONTEND_XPRT: // %ft
2232 src = fe->id;
2233 if (tmp->options & LOG_OPT_QUOTE)
2234 LOGCHAR('"');
2235 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2236 if (iret == 0)
2237 goto out;
2238 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002239 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002240 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002241 if (tmp->options & LOG_OPT_QUOTE)
2242 LOGCHAR('"');
2243 last_isspace = 0;
2244 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002245#ifdef USE_OPENSSL
2246 case LOG_FMT_SSL_CIPHER: // %sslc
2247 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002248 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002249 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002250 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002251 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002252 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2253 if (ret == NULL)
2254 goto out;
2255 tmplog = ret;
2256 last_isspace = 0;
2257 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002258
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002259 case LOG_FMT_SSL_VERSION: // %sslv
2260 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002261 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002262 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002263 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002264 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002265 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2266 if (ret == NULL)
2267 goto out;
2268 tmplog = ret;
2269 last_isspace = 0;
2270 break;
2271#endif
William Lallemand1d705562012-03-12 12:46:41 +01002272 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002273 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002274 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002275 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002276 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002277 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002278 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002279 break;
2280
William Lallemand1d705562012-03-12 12:46:41 +01002281 case LOG_FMT_SERVER: // %s
Willy Tarreaue1809df2018-09-05 15:30:16 +02002282 switch (obj_type(s ? s->target : NULL)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002283 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002284 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002285 break;
2286 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002287 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002288 break;
2289 default:
2290 src = "<NOSRV>";
2291 break;
2292 }
William Lallemand5f232402012-04-05 18:02:55 +02002293 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002294 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002295 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002296 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002297 last_isspace = 0;
2298 break;
2299
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002300 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002301 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002302 if (ret == NULL)
2303 goto out;
2304 tmplog = ret;
2305 last_isspace = 0;
2306 break;
2307
2308 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002309 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002310 if (ret == NULL)
2311 goto out;
2312 tmplog = ret;
2313 last_isspace = 0;
2314 break;
2315
2316 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002317 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002318 tmplog, dst + maxsize - tmplog);
2319 if (ret == NULL)
2320 goto out;
2321 tmplog = ret;
2322 last_isspace = 0;
2323 break;
2324
2325 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002326 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002327 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002328 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002329 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002330 last_isspace = 0;
2331 break;
2332
William Lallemand1d705562012-03-12 12:46:41 +01002333 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002334 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002335 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002336 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002337 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002338 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002339 last_isspace = 0;
2340 break;
2341
William Lallemand1d705562012-03-12 12:46:41 +01002342 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002343 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002344 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002345 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002346 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002347 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002348 last_isspace = 0;
2349 break;
2350
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002351 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002352 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002353 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002354 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002355 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002356 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002357 last_isspace = 0;
2358 break;
2359
Willy Tarreau27b639d2016-05-17 17:55:27 +02002360 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002361 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002362 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002363 tmplog, dst + maxsize - tmplog);
2364 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002365 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002366 tmplog, dst + maxsize - tmplog);
2367 if (ret == NULL)
2368 goto out;
2369 tmplog = ret;
2370 last_isspace = 0;
2371 break;
2372
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002373 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2374 if (!(fe->to_log & LW_BYTES))
2375 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002376 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 +02002377 tmplog, dst + maxsize - tmplog);
2378 if (ret == NULL)
2379 goto out;
2380 tmplog = ret;
2381 last_isspace = 0;
2382 break;
2383
2384 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002385 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002386 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002387 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002388 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002389 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002390 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002391 last_isspace = 0;
2392 break;
2393
Willy Tarreau2beef582012-12-20 17:22:52 +01002394 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02002395 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002396 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002397 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002398 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002399 last_isspace = 0;
2400 break;
2401
William Lallemand1d705562012-03-12 12:46:41 +01002402 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002403 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002404 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002405 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002406 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002407 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002408 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002409 last_isspace = 0;
2410 break;
2411
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002412 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002413 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002414 if (ret == NULL)
2415 goto out;
2416 tmplog = ret;
2417 last_isspace = 0;
2418 break;
2419
Willy Tarreau2beef582012-12-20 17:22:52 +01002420 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002421 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002422 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002423 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002424 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002425 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002426 last_isspace = 0;
2427 break;
2428
Willy Tarreau2beef582012-12-20 17:22:52 +01002429 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002430 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002431 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002432 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002433 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002434 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002435 last_isspace = 0;
2436 break;
2437
William Lallemand1d705562012-03-12 12:46:41 +01002438 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002439 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2440 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002441 *tmplog = '\0';
2442 last_isspace = 0;
2443 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002444
William Lallemand1d705562012-03-12 12:46:41 +01002445 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002446 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2447 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002448 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2449 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 +01002450 last_isspace = 0;
2451 break;
2452
William Lallemand1d705562012-03-12 12:46:41 +01002453 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002454 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002455 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002456 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002457 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002458 last_isspace = 0;
2459 break;
2460
William Lallemand1d705562012-03-12 12:46:41 +01002461 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002462 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002463 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002464 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002465 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002466 last_isspace = 0;
2467 break;
2468
William Lallemand1d705562012-03-12 12:46:41 +01002469 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002470 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002471 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002472 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002473 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002474 last_isspace = 0;
2475 break;
2476
William Lallemand1d705562012-03-12 12:46:41 +01002477 case LOG_FMT_SRVCONN: // %sc
Willy Tarreaue1809df2018-09-05 15:30:16 +02002478 ret = ultoa_o(objt_server(s ? s->target : NULL) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002479 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002480 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002481 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002482 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002483 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002484 last_isspace = 0;
2485 break;
2486
William Lallemand1d705562012-03-12 12:46:41 +01002487 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002488 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002489 LOGCHAR('+');
Willy Tarreauabd71a52018-09-04 19:21:44 +02002490 ret = ltoa_o((s && s->si[1].conn_retries > 0) ?
Willy Tarreau350f4872014-11-28 14:42:25 +01002491 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002492 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002493 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002494 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002495 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002496 last_isspace = 0;
2497 break;
2498
William Lallemand1d705562012-03-12 12:46:41 +01002499 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002500 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002501 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002502 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002503 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002504 last_isspace = 0;
2505 break;
2506
William Lallemand1d705562012-03-12 12:46:41 +01002507 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002508 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002509 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002510 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002511 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002512 last_isspace = 0;
2513 break;
2514
William Lallemand1d705562012-03-12 12:46:41 +01002515 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002516 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002517 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002518 if (tmp->options & LOG_OPT_QUOTE)
2519 LOGCHAR('"');
2520 LOGCHAR('{');
2521 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2522 if (hdr)
2523 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002524 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002525 ret = lf_encode_string(tmplog, dst + maxsize,
2526 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002527 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002528 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002529 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002530 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002531 }
2532 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002533 if (tmp->options & LOG_OPT_QUOTE)
2534 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002535 last_isspace = 0;
2536 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002537 break;
2538
William Lallemand1d705562012-03-12 12:46:41 +01002539 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002540 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002541 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002542 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2543 if (hdr > 0)
2544 LOGCHAR(' ');
2545 if (tmp->options & LOG_OPT_QUOTE)
2546 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002547 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002548 ret = lf_encode_string(tmplog, dst + maxsize,
2549 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002550 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002551 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002552 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002553 } else if (!(tmp->options & LOG_OPT_QUOTE))
2554 LOGCHAR('-');
2555 if (tmp->options & LOG_OPT_QUOTE)
2556 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002557 last_isspace = 0;
2558 }
2559 }
2560 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002561
William Lallemand1d705562012-03-12 12:46:41 +01002562
2563 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002564 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002565 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002566 if (tmp->options & LOG_OPT_QUOTE)
2567 LOGCHAR('"');
2568 LOGCHAR('{');
2569 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2570 if (hdr)
2571 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002572 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002573 ret = lf_encode_string(tmplog, dst + maxsize,
2574 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002575 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002576 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002577 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002578 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002579 }
2580 LOGCHAR('}');
2581 last_isspace = 0;
2582 if (tmp->options & LOG_OPT_QUOTE)
2583 LOGCHAR('"');
2584 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002585 break;
2586
William Lallemand1d705562012-03-12 12:46:41 +01002587 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002588 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002589 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002590 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2591 if (hdr > 0)
2592 LOGCHAR(' ');
2593 if (tmp->options & LOG_OPT_QUOTE)
2594 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002595 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002596 ret = lf_encode_string(tmplog, dst + maxsize,
2597 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002598 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002599 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002600 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002601 } else if (!(tmp->options & LOG_OPT_QUOTE))
2602 LOGCHAR('-');
2603 if (tmp->options & LOG_OPT_QUOTE)
2604 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002605 last_isspace = 0;
2606 }
2607 }
2608 break;
2609
William Lallemand1d705562012-03-12 12:46:41 +01002610 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002611 /* Request */
2612 if (tmp->options & LOG_OPT_QUOTE)
2613 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002614 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002615 ret = lf_encode_string(tmplog, dst + maxsize,
2616 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002617 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002618 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002619 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002620 if (tmp->options & LOG_OPT_QUOTE)
2621 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002622 last_isspace = 0;
2623 break;
William Lallemand5f232402012-04-05 18:02:55 +02002624
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002625 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002626 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002627
Willy Tarreaub7636d12015-06-17 19:58:02 +02002628 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002629 LOGCHAR('"');
2630
2631 end = uri + strlen(uri);
2632 // look for the first whitespace character
2633 while (uri < end && !HTTP_IS_SPHT(*uri))
2634 uri++;
2635
2636 // keep advancing past multiple spaces
2637 while (uri < end && HTTP_IS_SPHT(*uri)) {
2638 uri++; nspaces++;
2639 }
2640
2641 // look for first space or question mark after url
2642 spc = uri;
2643 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2644 spc++;
2645
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002646 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002647 chunk.area = "<BADREQ>";
2648 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002649 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002650 chunk.area = uri;
2651 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002652 }
2653
Dragan Dosen835b9212016-02-12 13:23:03 +01002654 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002655 if (ret == NULL || *ret != '\0')
2656 goto out;
2657
2658 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002659 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002660 LOGCHAR('"');
2661
2662 last_isspace = 0;
2663 break;
2664
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002665 case LOG_FMT_HTTP_QUERY: // %HQ
2666 if (tmp->options & LOG_OPT_QUOTE)
2667 LOGCHAR('"');
2668
Willy Tarreau57bc8912016-04-25 17:09:40 +02002669 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 chunk.area = "<BADREQ>";
2671 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002672 } else {
2673 uri = txn->uri;
2674 end = uri + strlen(uri);
2675 // look for the first question mark
2676 while (uri < end && *uri != '?')
2677 uri++;
2678
2679 qmark = uri;
2680 // look for first space or question mark after url
2681 while (uri < end && !HTTP_IS_SPHT(*uri))
2682 uri++;
2683
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002684 chunk.area = qmark;
2685 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002686 }
2687
Dragan Dosen835b9212016-02-12 13:23:03 +01002688 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002689 if (ret == NULL || *ret != '\0')
2690 goto out;
2691
2692 tmplog = ret;
2693 if (tmp->options & LOG_OPT_QUOTE)
2694 LOGCHAR('"');
2695
2696 last_isspace = 0;
2697 break;
2698
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002699 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002700 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002701
Willy Tarreaub7636d12015-06-17 19:58:02 +02002702 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002703 LOGCHAR('"');
2704
2705 end = uri + strlen(uri);
2706 // look for the first whitespace character
2707 while (uri < end && !HTTP_IS_SPHT(*uri))
2708 uri++;
2709
2710 // keep advancing past multiple spaces
2711 while (uri < end && HTTP_IS_SPHT(*uri)) {
2712 uri++; nspaces++;
2713 }
2714
2715 // look for first space after url
2716 spc = uri;
2717 while (spc < end && !HTTP_IS_SPHT(*spc))
2718 spc++;
2719
Willy Tarreau57bc8912016-04-25 17:09:40 +02002720 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002721 chunk.area = "<BADREQ>";
2722 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002723 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002724 chunk.area = uri;
2725 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002726 }
2727
Dragan Dosen835b9212016-02-12 13:23:03 +01002728 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002729 if (ret == NULL || *ret != '\0')
2730 goto out;
2731
2732 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002733 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002734 LOGCHAR('"');
2735
2736 last_isspace = 0;
2737 break;
2738
2739 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002740 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002741 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002742 LOGCHAR('"');
2743
2744 end = uri + strlen(uri);
2745 // look for the first whitespace character
2746 spc = uri;
2747 while (spc < end && !HTTP_IS_SPHT(*spc))
2748 spc++;
2749
2750 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002751 chunk.area = "<BADREQ>";
2752 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002753 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002754 chunk.area = uri;
2755 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002756 }
2757
Dragan Dosen835b9212016-02-12 13:23:03 +01002758 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002759 if (ret == NULL || *ret != '\0')
2760 goto out;
2761
2762 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002763 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002764 LOGCHAR('"');
2765
2766 last_isspace = 0;
2767 break;
2768
2769 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002770 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002771 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002772 LOGCHAR('"');
2773
2774 end = uri + strlen(uri);
2775 // look for the first whitespace character
2776 while (uri < end && !HTTP_IS_SPHT(*uri))
2777 uri++;
2778
2779 // keep advancing past multiple spaces
2780 while (uri < end && HTTP_IS_SPHT(*uri)) {
2781 uri++; nspaces++;
2782 }
2783
2784 // look for the next whitespace character
2785 while (uri < end && !HTTP_IS_SPHT(*uri))
2786 uri++;
2787
2788 // keep advancing past multiple spaces
2789 while (uri < end && HTTP_IS_SPHT(*uri))
2790 uri++;
2791
Willy Tarreau57bc8912016-04-25 17:09:40 +02002792 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002793 chunk.area = "<BADREQ>";
2794 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002795 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002796 chunk.area = "HTTP/0.9";
2797 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002798 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002799 chunk.area = uri;
2800 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002801 }
2802
Dragan Dosen835b9212016-02-12 13:23:03 +01002803 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002804 if (ret == NULL || *ret != '\0')
2805 goto out;
2806
2807 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002808 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002809 LOGCHAR('"');
2810
2811 last_isspace = 0;
2812 break;
2813
William Lallemand5f232402012-04-05 18:02:55 +02002814 case LOG_FMT_COUNTER: // %rt
2815 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002816 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002817 if (iret < 0 || iret > dst + maxsize - tmplog)
2818 goto out;
2819 last_isspace = 0;
2820 tmplog += iret;
2821 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002822 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002823 if (ret == NULL)
2824 goto out;
2825 tmplog = ret;
2826 last_isspace = 0;
2827 }
2828 break;
2829
Willy Tarreau7346acb2014-08-28 15:03:15 +02002830 case LOG_FMT_LOGCNT: // %lc
2831 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002832 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002833 if (iret < 0 || iret > dst + maxsize - tmplog)
2834 goto out;
2835 last_isspace = 0;
2836 tmplog += iret;
2837 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002838 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002839 if (ret == NULL)
2840 goto out;
2841 tmplog = ret;
2842 last_isspace = 0;
2843 }
2844 break;
2845
William Lallemand5f232402012-04-05 18:02:55 +02002846 case LOG_FMT_HOSTNAME: // %H
2847 src = hostname;
2848 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2849 if (ret == NULL)
2850 goto out;
2851 tmplog = ret;
2852 last_isspace = 0;
2853 break;
2854
2855 case LOG_FMT_PID: // %pid
2856 if (tmp->options & LOG_OPT_HEXA) {
2857 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2858 if (iret < 0 || iret > dst + maxsize - tmplog)
2859 goto out;
2860 last_isspace = 0;
2861 tmplog += iret;
2862 } else {
2863 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2864 if (ret == NULL)
2865 goto out;
2866 tmplog = ret;
2867 last_isspace = 0;
2868 }
2869 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002870
2871 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002872 ret = NULL;
Willy Tarreau02fdf4f2018-09-05 15:49:01 +02002873 src = s ? s->unique_id : NULL;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002874 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002875 if (ret == NULL)
2876 goto out;
2877 tmplog = ret;
2878 last_isspace = 0;
2879 break;
2880
William Lallemandbddd4fd2012-02-27 11:23:10 +01002881 }
2882 }
2883
2884out:
William Lallemand1d705562012-03-12 12:46:41 +01002885 /* *tmplog is a unused character */
2886 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002887 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002888
Willy Tarreaubaaee002006-06-26 02:48:02 +02002889}
2890
William Lallemand1d705562012-03-12 12:46:41 +01002891/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002892 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002893 * Will not log if the frontend has no log defined.
2894 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002895void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002896{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002897 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002898 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002899 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002900
2901 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002902 err = (s->flags & SF_REDISP) ||
2903 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2904 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002905 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002906 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002907
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002908 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002909 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002910
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002911 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002912 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002913
Willy Tarreauabcd5142013-06-11 17:18:02 +02002914 if (s->logs.level) { /* loglevel was overridden */
2915 if (s->logs.level == -1) {
2916 s->logs.logwait = 0; /* logs disabled */
2917 return;
2918 }
2919 level = s->logs.level - 1;
2920 }
2921 else {
2922 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002923 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002924 level = LOG_ERR;
2925 }
William Lallemand1d705562012-03-12 12:46:41 +01002926
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002927 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002928 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002929 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002930 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002931 }
2932
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002933 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2934 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2935 &sess->fe->logformat_sd);
2936 }
2937
Dragan Dosen59cee972015-09-19 22:09:02 +02002938 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002939 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01002940 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002941 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002942 s->logs.logwait = 0;
2943 }
2944}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002945
Willy Tarreau53839352018-09-05 19:51:10 +02002946/*
2947 * send a minimalist log for the session. Will not log if the frontend has no
2948 * log defined. It is assumed that this is only used to report anomalies that
2949 * cannot lead to the creation of a regular stream. Because of this the log
2950 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
2951 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02002952 * function to report unimportant events. It is safe to call this function with
2953 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02002954 */
2955void sess_log(struct session *sess)
2956{
2957 int size, level;
2958 int sd_size = 0;
2959
Willy Tarreau9fa267d2018-10-05 10:22:27 +02002960 if (!sess)
2961 return;
2962
Willy Tarreau53839352018-09-05 19:51:10 +02002963 if (LIST_ISEMPTY(&sess->fe->logsrvs))
2964 return;
2965
2966 level = LOG_INFO;
2967 if (sess->fe->options2 & PR_O2_LOGERRORS)
2968 level = LOG_ERR;
2969
2970 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2971 sd_size = sess_build_logline(sess, NULL,
2972 logline_rfc5424, global.max_syslog_len,
2973 &sess->fe->logformat_sd);
2974 }
2975
2976 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
2977 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01002978 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Willy Tarreau53839352018-09-05 19:51:10 +02002979 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
2980 }
2981}
2982
Christopher Fauletc1b730a2017-10-24 12:00:51 +02002983static int cli_io_handler_show_startup_logs(struct appctx *appctx)
2984{
2985 struct stream_interface *si = appctx->owner;
2986 const char *msg = (startup_logs ? startup_logs : "No startup alerts/warnings.\n");
2987
2988 if (ci_putstr(si_ic(si), msg) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002989 si_rx_room_blk(si);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02002990 return 0;
2991 }
2992 return 1;
2993}
2994
2995/* register cli keywords */
2996static struct cli_kw_list cli_kws = {{ },{
2997 { { "show", "startup-logs", NULL },
2998 "show startup-logs : report logs emitted during HAProxy startup",
2999 NULL, cli_io_handler_show_startup_logs },
3000 {{},}
3001}};
3002
Willy Tarreau0108d902018-11-25 19:14:37 +01003003INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3004
Willy Tarreau082b6282019-05-22 14:42:12 +02003005REGISTER_PER_THREAD_ALLOC(init_log_buffers);
3006REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01003007
Willy Tarreaubaaee002006-06-26 02:48:02 +02003008/*
3009 * Local variables:
3010 * c-indent-level: 8
3011 * c-basic-offset: 8
3012 * End:
3013 */