blob: 8bd45ae3fbe51f077051ea3f7ada51f8fe8bdc55 [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);
Willy Tarreau1a9f27f2019-12-11 12:05:39 +0100618 goto fail;
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);
Willy Tarreau1a9f27f2019-12-11 12:05:39 +0100645 goto fail;
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))
Willy Tarreau1a9f27f2019-12-11 12:05:39 +0100678 goto fail;
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))
Willy Tarreau1a9f27f2019-12-11 12:05:39 +0100682 goto fail;
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))
Willy Tarreau1a9f27f2019-12-11 12:05:39 +0100687 goto fail;
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 : "%");
Willy Tarreau1a9f27f2019-12-11 12:05:39 +0100696 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100697 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100698 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100699
700 return 1;
Willy Tarreau1a9f27f2019-12-11 12:05:39 +0100701 fail:
702 free(backfmt);
703 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100704}
705
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200706/*
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200707 * Parse the first range of indexes from a string made of a list of comma seperated
708 * ranges of indexes. Note that an index may be considered as a particular range
709 * with a high limit to the low limit.
710 */
711int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
712{
713 char *end, *p;
714
715 *low = *high = 0;
716
717 p = *arg;
718 end = strchr(p, ',');
719 if (!end)
720 end = p + strlen(p);
721
722 *high = *low = read_uint((const char **)&p, end);
723 if (!*low || (p != end && *p != '-'))
724 goto err;
725
726 if (p == end)
727 goto done;
728
729 p++;
730 *high = read_uint((const char **)&p, end);
731 if (!*high || *high <= *low || p != end)
732 goto err;
733
734 done:
735 if (*end == ',')
736 end++;
737 *arg = end;
738 return 1;
739
740 err:
741 memprintf(err, "wrong sample range '%s'", *arg);
742 return 0;
743}
744
745/*
746 * Returns 1 if the range defined by <low> and <high> overlaps
747 * one of them in <rgs> array of ranges with <sz> the size of this
748 * array, 0 if not.
749 */
750int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
751 unsigned int low, unsigned int high, char **err)
752{
753 size_t i;
754
755 for (i = 0; i < sz; i++) {
756 if ((low >= rgs[i].low && low <= rgs[i].high) ||
757 (high >= rgs[i].low && high <= rgs[i].high)) {
758 memprintf(err, "ranges are overlapping");
759 return 1;
760 }
761 }
762
763 return 0;
764}
765
766int smp_log_range_cmp(const void *a, const void *b)
767{
768 const struct smp_log_range *rg_a = a;
769 const struct smp_log_range *rg_b = b;
770
771 if (rg_a->high < rg_b->low)
772 return -1;
773 else if (rg_a->low > rg_b->high)
774 return 1;
775
776 return 0;
777}
778
779/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200780 * Parse "log" keyword and update <logsrvs> list accordingly.
781 *
782 * When <do_del> is set, it means the "no log" line was parsed, so all log
783 * servers in <logsrvs> are released.
784 *
785 * Otherwise, we try to parse the "log" line. First of all, when the list is not
786 * the global one, we look for the parameter "global". If we find it,
787 * global.logsrvs is copied. Else we parse each arguments.
788 *
789 * The function returns 1 in success case, otherwise, it returns 0 and err is
790 * filled.
791 */
792int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
793{
Willy Tarreau06274b92020-10-27 09:51:37 +0100794 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200795 struct sockaddr_storage *sk;
796 struct logsrv *logsrv = NULL;
797 int port1, port2;
798 int cur_arg;
799
800 /*
801 * "no log": delete previous herited or defined syslog
802 * servers.
803 */
804 if (do_del) {
805 struct logsrv *back;
806
807 if (*(args[1]) != 0) {
808 memprintf(err, "'no log' does not expect arguments");
809 goto error;
810 }
811
812 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
813 LIST_DEL(&logsrv->list);
814 free(logsrv);
815 }
816 return 1;
817 }
818
819 /*
820 * "log global": copy global.logrsvs linked list to the end of logsrvs
821 * list. But first, we check (logsrvs != global.logsrvs).
822 */
823 if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) {
824 if (logsrvs == &global.logsrvs) {
825 memprintf(err, "'global' is not supported for a global syslog server");
826 goto error;
827 }
828 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200829 struct logsrv *node;
830
831 list_for_each_entry(node, logsrvs, list) {
832 if (node->ref == logsrv)
833 goto skip_logsrv;
834 }
835
836 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200837 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200838 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200839 LIST_INIT(&node->list);
840 LIST_ADDQ(logsrvs, &node->list);
Christopher Faulet28ac0992018-03-26 16:09:19 +0200841
842 skip_logsrv:
843 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200844 }
845 return 1;
846 }
847
848 /*
849 * "log <address> ...: parse a syslog server line
850 */
851 if (*(args[1]) == 0 || *(args[2]) == 0) {
852 memprintf(err, "expects <address> and <facility> %s as arguments",
853 ((logsrvs == &global.logsrvs) ? "" : "or global"));
854 goto error;
855 }
856
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100857 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
858 if (strcmp(args[1], "stdout") == 0)
859 args[1] = "fd@1";
860 else if (strcmp(args[1], "stderr") == 0)
861 args[1] = "fd@2";
862
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200863 logsrv = calloc(1, sizeof(*logsrv));
864 if (!logsrv) {
865 memprintf(err, "out of memory");
866 goto error;
867 }
868
869 /* skip address for now, it will be parsed at the end */
870 cur_arg = 2;
871
872 /* just after the address, a length may be specified */
873 logsrv->maxlen = MAX_SYSLOG_LEN;
874 if (strcmp(args[cur_arg], "len") == 0) {
875 int len = atoi(args[cur_arg+1]);
876 if (len < 80 || len > 65535) {
877 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
878 args[cur_arg+1]);
879 goto error;
880 }
881 logsrv->maxlen = len;
882 cur_arg += 2;
883 }
884 if (logsrv->maxlen > global.max_syslog_len)
885 global.max_syslog_len = logsrv->maxlen;
886
887 /* after the length, a format may be specified */
888 if (strcmp(args[cur_arg], "format") == 0) {
889 logsrv->format = get_log_format(args[cur_arg+1]);
890 if (logsrv->format < 0) {
891 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
892 goto error;
893 }
894 cur_arg += 2;
895 }
896
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200897 if (strcmp(args[cur_arg], "sample") == 0) {
898 unsigned low, high;
899 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200900 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
901
902 p = args[cur_arg+1];
903 smp_sz_str = strchr(p, ':');
904 if (!smp_sz_str) {
905 memprintf(err, "Missing sample size");
906 goto error;
907 }
908
909 *smp_sz_str++ = '\0';
910
911 end = p + strlen(p);
912
913 while (p != end) {
914 if (!get_logsrv_smp_range(&low, &high, &p, err))
915 goto error;
916
917 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
918 goto error;
919
920 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
921 if (!smp_rgs) {
922 memprintf(err, "out of memory error");
923 goto error;
924 }
925
926 smp_rgs[smp_rgs_sz].low = low;
927 smp_rgs[smp_rgs_sz].high = high;
928 smp_rgs[smp_rgs_sz].sz = high - low + 1;
929 smp_rgs[smp_rgs_sz].curr_idx = 0;
930 if (smp_rgs[smp_rgs_sz].high > smp_sz)
931 smp_sz = smp_rgs[smp_rgs_sz].high;
932 smp_rgs_sz++;
933 }
934
Tim Duesterhusf52039f2019-06-23 22:10:10 +0200935 if (smp_rgs == NULL) {
936 memprintf(err, "no sampling ranges given");
937 goto error;
938 }
939
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200940 beg = smp_sz_str;
941 end = beg + strlen(beg);
942 new_smp_sz = read_uint((const char **)&beg, end);
943 if (!new_smp_sz || beg != end) {
944 memprintf(err, "wrong sample size '%s' for sample range '%s'",
945 smp_sz_str, args[cur_arg+1]);
946 goto error;
947 }
948
949 if (new_smp_sz < smp_sz) {
950 memprintf(err, "sample size %zu should be greater or equal to "
951 "%zu the maximum of the high ranges limits",
952 new_smp_sz, smp_sz);
953 goto error;
954 }
955 smp_sz = new_smp_sz;
956
957 /* Let's order <smp_rgs> array. */
958 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
959
960 logsrv->lb.smp_rgs = smp_rgs;
961 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
962 logsrv->lb.smp_sz = smp_sz;
963
964 cur_arg += 2;
965 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200966 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200967 /* parse the facility */
968 logsrv->facility = get_log_facility(args[cur_arg]);
969 if (logsrv->facility < 0) {
970 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
971 goto error;
972 }
973 cur_arg++;
974
975 /* parse the max syslog level (default: debug) */
976 logsrv->level = 7;
977 if (*(args[cur_arg])) {
978 logsrv->level = get_log_level(args[cur_arg]);
979 if (logsrv->level < 0) {
980 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
981 goto error;
982 }
983 cur_arg++;
984 }
985
986 /* parse the limit syslog level (default: emerg) */
987 logsrv->minlvl = 0;
988 if (*(args[cur_arg])) {
989 logsrv->minlvl = get_log_level(args[cur_arg]);
990 if (logsrv->minlvl < 0) {
991 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
992 goto error;
993 }
994 cur_arg++;
995 }
996
997 /* Too many args */
998 if (*(args[cur_arg])) {
999 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
1000 goto error;
1001 }
1002
1003 /* now, back to the address */
1004 sk = str2sa_range(args[1], NULL, &port1, &port2, err, NULL, NULL, 1);
1005 if (!sk)
1006 goto error;
1007 logsrv->addr = *sk;
1008
1009 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
1010 if (port1 != port2) {
1011 memprintf(err, "port ranges and offsets are not allowed in '%s'", args[1]);
1012 goto error;
1013 }
1014 logsrv->addr = *sk;
1015 if (!port1)
1016 set_host_port(&logsrv->addr, SYSLOG_PORT);
1017 }
1018 LIST_ADDQ(logsrvs, &logsrv->list);
1019 return 1;
1020
1021 error:
Willy Tarreau06274b92020-10-27 09:51:37 +01001022 free(smp_rgs);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001023 free(logsrv);
1024 return 0;
1025}
1026
1027
Christopher Fauletd4696382017-10-24 11:44:05 +02001028/* Generic function to display messages prefixed by a label */
1029static void print_message(const char *label, const char *fmt, va_list argp)
1030{
1031 struct tm tm;
1032 char *head, *msg;
1033
1034 head = msg = NULL;
1035
1036 get_localtime(date.tv_sec, &tm);
1037 memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
1038 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
1039 memvprintf(&msg, fmt, argp);
1040
Willy Tarreaud83e71c2019-11-15 16:00:12 +01001041 if (global.mode & MODE_STARTING &&
1042 (!startup_logs || strlen(startup_logs) + strlen(head) + strlen(msg) < global.tune.bufsize))
Christopher Fauletd4696382017-10-24 11:44:05 +02001043 memprintf(&startup_logs, "%s%s%s", (startup_logs ? startup_logs : ""), head, msg);
1044
1045 fprintf(stderr, "%s%s", head, msg);
1046 fflush(stderr);
1047
1048 free(head);
1049 free(msg);
1050}
1051
Willy Tarreaubaaee002006-06-26 02:48:02 +02001052/*
1053 * Displays the message on stderr with the date and pid. Overrides the quiet
1054 * mode during startup.
1055 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001056void ha_alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001057{
1058 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001059
1060 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
1061 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001062 print_message("ALERT", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001063 va_end(argp);
1064 }
1065}
1066
1067
1068/*
1069 * Displays the message on stderr with the date and pid.
1070 */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001071void ha_warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001072{
1073 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001074
1075 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1076 va_start(argp, fmt);
Christopher Fauletd4696382017-10-24 11:44:05 +02001077 print_message("WARNING", fmt, argp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001078 va_end(argp);
1079 }
1080}
1081
1082/*
William Lallemand9c56a222018-11-21 18:04:52 +01001083 * Displays the message on stderr with the date and pid.
1084 */
1085void ha_notice(const char *fmt, ...)
1086{
1087 va_list argp;
1088
1089 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1090 va_start(argp, fmt);
1091 print_message("NOTICE", fmt, argp);
1092 va_end(argp);
1093 }
1094}
1095
1096/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001097 * Displays the message on <out> only if quiet mode is not set.
1098 */
Willy Tarreaub17916e2006-10-15 15:17:57 +02001099void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100{
1101 va_list argp;
1102
1103 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
1104 va_start(argp, fmt);
1105 vfprintf(out, fmt, argp);
1106 fflush(out);
1107 va_end(argp);
1108 }
1109}
1110
1111/*
Dragan Dosen1322d092015-09-22 16:05:32 +02001112 * returns log format for <fmt> or -1 if not found.
1113 */
1114int get_log_format(const char *fmt)
1115{
1116 int format;
1117
1118 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +02001119 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +02001120 format--;
1121
1122 return format;
1123}
1124
1125/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001126 * returns log level for <lev> or -1 if not found.
1127 */
1128int get_log_level(const char *lev)
1129{
1130 int level;
1131
1132 level = NB_LOG_LEVELS - 1;
1133 while (level >= 0 && strcmp(log_levels[level], lev))
1134 level--;
1135
1136 return level;
1137}
1138
Willy Tarreaubaaee002006-06-26 02:48:02 +02001139/*
1140 * returns log facility for <fac> or -1 if not found.
1141 */
1142int get_log_facility(const char *fac)
1143{
1144 int facility;
1145
1146 facility = NB_LOG_FACILITIES - 1;
1147 while (facility >= 0 && strcmp(log_facilities[facility], fac))
1148 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001149
Willy Tarreaubaaee002006-06-26 02:48:02 +02001150 return facility;
1151}
1152
William Lallemanda1cc3812012-02-08 16:38:44 +01001153/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001154 * Encode the string.
1155 *
1156 * When using the +E log format option, it will try to escape '"\]'
1157 * characters with '\' as prefix. The same prefix should not be used as
1158 * <escape>.
1159 */
1160static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001161 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001162 const char *string,
1163 struct logformat_node *node)
1164{
1165 if (node->options & LOG_OPT_ESC) {
1166 if (start < stop) {
1167 stop--; /* reserve one byte for the final '\0' */
1168 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001169 if (!ha_bit_test((unsigned char)(*string), map)) {
1170 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001171 *start++ = *string;
1172 else {
1173 if (start + 2 >= stop)
1174 break;
1175 *start++ = '\\';
1176 *start++ = *string;
1177 }
1178 }
1179 else {
1180 if (start + 3 >= stop)
1181 break;
1182 *start++ = escape;
1183 *start++ = hextab[(*string >> 4) & 15];
1184 *start++ = hextab[*string & 15];
1185 }
1186 string++;
1187 }
1188 *start = '\0';
1189 }
1190 }
1191 else {
1192 return encode_string(start, stop, escape, map, string);
1193 }
1194
1195 return start;
1196}
1197
1198/*
1199 * Encode the chunk.
1200 *
1201 * When using the +E log format option, it will try to escape '"\]'
1202 * characters with '\' as prefix. The same prefix should not be used as
1203 * <escape>.
1204 */
1205static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001206 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001207 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001208 struct logformat_node *node)
1209{
1210 char *str, *end;
1211
1212 if (node->options & LOG_OPT_ESC) {
1213 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001214 str = chunk->area;
1215 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001216
1217 stop--; /* reserve one byte for the final '\0' */
1218 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001219 if (!ha_bit_test((unsigned char)(*str), map)) {
1220 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001221 *start++ = *str;
1222 else {
1223 if (start + 2 >= stop)
1224 break;
1225 *start++ = '\\';
1226 *start++ = *str;
1227 }
1228 }
1229 else {
1230 if (start + 3 >= stop)
1231 break;
1232 *start++ = escape;
1233 *start++ = hextab[(*str >> 4) & 15];
1234 *start++ = hextab[*str & 15];
1235 }
1236 str++;
1237 }
1238 *start = '\0';
1239 }
1240 }
1241 else {
1242 return encode_chunk(start, stop, escape, map, chunk);
1243 }
1244
1245 return start;
1246}
1247
1248/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001249 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001250 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001251 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001252 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001253 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001254char *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 +01001255{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001256 if (size < 2)
1257 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001258
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001259 if (node->options & LOG_OPT_QUOTE) {
1260 *(dst++) = '"';
1261 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001262 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001263
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001264 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001265 if (++len > size)
1266 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001267 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001268 char *ret;
1269
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001270 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001271 if (ret == NULL || *ret != '\0')
1272 return NULL;
1273 len = ret - dst;
1274 }
1275 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001276 len = strlcpy2(dst, src, len);
1277 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001278
1279 size -= len;
1280 dst += len;
1281 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001282 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1283 if (size < 2)
1284 return NULL;
1285 *(dst++) = '-';
1286 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001287
1288 if (node->options & LOG_OPT_QUOTE) {
1289 if (size < 2)
1290 return NULL;
1291 *(dst++) = '"';
1292 }
1293
1294 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001295 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001296}
1297
Willy Tarreau26ffa852018-09-05 15:23:10 +02001298static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001299{
1300 return lf_text_len(dst, src, size, size, node);
1301}
1302
William Lallemand5f232402012-04-05 18:02:55 +02001303/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001304 * Write a IP address to the log string
William Lallemand5f232402012-04-05 18:02:55 +02001305 * +X option write in hexadecimal notation, most signifant byte on the left
1306 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001307char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001308{
1309 char *ret = dst;
1310 int iret;
1311 char pn[INET6_ADDRSTRLEN];
1312
1313 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001314 unsigned char *addr = NULL;
1315 switch (sockaddr->sa_family) {
1316 case AF_INET:
1317 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1318 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1319 break;
1320 case AF_INET6:
1321 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1322 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1323 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1324 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1325 break;
1326 default:
1327 return NULL;
1328 }
William Lallemand5f232402012-04-05 18:02:55 +02001329 if (iret < 0 || iret > size)
1330 return NULL;
1331 ret += iret;
1332 } else {
1333 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1334 ret = lf_text(dst, pn, size, node);
1335 if (ret == NULL)
1336 return NULL;
1337 }
1338 return ret;
1339}
1340
1341/*
1342 * Write a port to the log
1343 * +X option write in hexadecimal notation, most signifant byte on the left
1344 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001345char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001346{
1347 char *ret = dst;
1348 int iret;
1349
1350 if (node->options & LOG_OPT_HEXA) {
1351 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1352 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1353 if (iret < 0 || iret > size)
1354 return NULL;
1355 ret += iret;
1356 } else {
1357 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1358 if (ret == NULL)
1359 return NULL;
1360 }
1361 return ret;
1362}
1363
Dragan Dosen1322d092015-09-22 16:05:32 +02001364/* Re-generate time-based part of the syslog header in RFC3164 format at
1365 * the beginning of logheader once a second and return the pointer to the
1366 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001367 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001368static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001369{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001370 static THREAD_LOCAL long tvsec;
Willy Tarreau83061a82018-07-13 11:56:34 +02001371 static THREAD_LOCAL struct buffer host = { };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001372 static THREAD_LOCAL int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001373
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001374 if (unlikely(time != tvsec || logheader_end == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001375 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +02001376 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001377 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +02001378
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001379 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +02001380 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001381
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001382 if (unlikely(global.log_send_hostname != host.area)) {
1383 host.area = global.log_send_hostname;
1384 host.data = host.area ? strlen(host.area) : 0;
1385 sep = host.data ? 1 : 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001386 }
1387
Dragan Dosen59cee972015-09-19 22:09:02 +02001388 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +02001389 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +02001390 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +02001391 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001392 (int)host.data, host.area, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001393 /* WARNING: depending upon implementations, snprintf may return
1394 * either -1 or the number of bytes that would be needed to store
1395 * the total message. In both cases, we must adjust it.
1396 */
Willy Tarreau18324f52014-06-27 18:10:07 +02001397 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1398 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001400 logheader_end = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001401 }
1402
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001403 logheader_end[0] = 0; // ensure we get rid of any previous attempt
Willy Tarreau094af4e2015-01-07 15:03:42 +01001404
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001405 return logheader_end;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001406}
1407
Dragan Dosen1322d092015-09-22 16:05:32 +02001408/* Re-generate time-based part of the syslog header in RFC5424 format at
1409 * the beginning of logheader_rfc5424 once a second and return the pointer
1410 * to the first character after it.
1411 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001412static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +02001413{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001414 static THREAD_LOCAL long tvsec;
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001415 const char *gmt_offset;
Dragan Dosen1322d092015-09-22 16:05:32 +02001416
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001417 if (unlikely(time != tvsec || logheader_rfc5424_end == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +02001418 /* this string is rebuild only once a second */
1419 struct tm tm;
1420 int hdr_len;
1421
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001422 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +02001423 get_localtime(tvsec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001424 gmt_offset = get_gmt_offset(time, &tm);
Dragan Dosen1322d092015-09-22 16:05:32 +02001425
1426 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +02001427 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +02001428 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +02001429 tm.tm_hour, tm.tm_min, tm.tm_sec,
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001430 gmt_offset, gmt_offset+3,
Dragan Dosen43885c72015-10-01 13:18:13 +02001431 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +02001432 /* WARNING: depending upon implementations, snprintf may return
1433 * either -1 or the number of bytes that would be needed to store
1434 * the total message. In both cases, we must adjust it.
1435 */
1436 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1437 hdr_len = global.max_syslog_len;
1438
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001439 logheader_rfc5424_end = logheader_rfc5424 + hdr_len;
Dragan Dosen1322d092015-09-22 16:05:32 +02001440 }
1441
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001442 logheader_rfc5424_end[0] = 0; // ensure we get rid of any previous attempt
Dragan Dosen1322d092015-09-22 16:05:32 +02001443
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001444 return logheader_rfc5424_end;
Dragan Dosen1322d092015-09-22 16:05:32 +02001445}
1446
William Lallemand2a4a44f2012-02-06 16:00:33 +01001447/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001448 * This function sends the syslog message using a printf format string. It
1449 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001450 */
1451void send_log(struct proxy *p, int level, const char *format, ...)
1452{
1453 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001454 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001455
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001456 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001457 return;
1458
William Lallemand2a4a44f2012-02-06 16:00:33 +01001459 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001460 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001461 if (data_len < 0 || data_len > global.max_syslog_len)
1462 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001463 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001464
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001465 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001466}
1467
1468/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001469 * This function sends a syslog message to <logsrv>.
1470 * <pid_str> is the string to be used for the PID of the caller, <pid_size> is length.
1471 * Same thing for <sd> and <sd_size> which are used for the structured-data part
1472 * in RFC5424 formatted syslog messages, and <tag_str> and <tag_size> the syslog tag.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001473 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001474 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001475 */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001476static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_str, size_t pid_size,
1477 int level, char *message, size_t size, char *sd, size_t sd_size,
1478 char *tag_str, size_t tag_size)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001479{
Christopher Fauletf8188c62017-06-02 16:20:16 +02001480 static THREAD_LOCAL struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1481 static THREAD_LOCAL struct msghdr msghdr = {
1482 //.msg_iov = iovec,
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001483 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1484 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001485 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1486 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
1487 static THREAD_LOCAL char *dataptr = NULL;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001488 time_t time = date.tv_sec;
Dragan Dosen1322d092015-09-22 16:05:32 +02001489 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001490 size_t hdr_size;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001491 int fac_level;
1492 int *plogfd;
1493 char *pid_sep1 = "", *pid_sep2 = "";
1494 char logheader_short[3];
1495 int sent;
1496 int maxlen;
1497 int hdr_max = 0;
1498 int tag_max = 0;
1499 int pid_sep1_max = 0;
Frédéric Lécaille90a10ae2019-05-14 10:57:58 +02001500 int pid_max = 0;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001501 int pid_sep2_max = 0;
1502 int sd_max = 0;
1503 int max = 0;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001504
1505 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001506
1507 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001508
Emeric Brun15613612020-05-28 14:21:33 +02001509 /* historically some messages used to already contain the trailing LF
1510 * or Zero. Let's remove all trailing LF or Zero
1511 */
1512 while (size && ((dataptr[size-1] == '\n' || (dataptr[size-1] == 0))))
Emeric Brun4ce00fc2020-05-12 19:33:15 +02001513 size--;
1514
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001515 if (logsrv->addr.ss_family == AF_UNSPEC) {
1516 /* the socket's address is a file descriptor */
1517 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1518 if (unlikely(!((struct sockaddr_in *)&logsrv->addr)->sin_port)) {
1519 /* FD not yet initialized to non-blocking mode.
1520 * DON'T DO IT ON A TERMINAL!
1521 */
1522 if (!isatty(*plogfd))
1523 fcntl(*plogfd, F_SETFL, O_NONBLOCK);
1524 ((struct sockaddr_in *)&logsrv->addr)->sin_port = 1;
Dragan Dosen43885c72015-10-01 13:18:13 +02001525 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001526 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001527 else if (logsrv->addr.ss_family == AF_UNIX)
1528 plogfd = &logfdunix;
1529 else
1530 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001531
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001532 if (unlikely(*plogfd < 0)) {
1533 /* socket not successfully initialized yet */
1534 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1535 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1536 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001537
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001538 if (!once) {
1539 once = 1; /* note: no need for atomic ops here */
1540 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1541 nblogger, strerror(errno), errno);
1542 }
1543 return;
1544 } else {
1545 /* we don't want to receive anything on this socket */
1546 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1547 /* does nothing under Linux, maybe needed for others */
1548 shutdown(*plogfd, SHUT_RD);
1549 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1550 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001551 }
1552
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001553 switch (logsrv->format) {
1554 case LOG_FORMAT_RFC3164:
1555 hdr = logheader;
1556 hdr_ptr = update_log_hdr(time);
1557 break;
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001558
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001559 case LOG_FORMAT_RFC5424:
1560 hdr = logheader_rfc5424;
1561 hdr_ptr = update_log_hdr_rfc5424(time);
1562 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
1563 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001564
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001565 case LOG_FORMAT_SHORT:
1566 /* all fields are known, skip the header generation */
1567 hdr = logheader_short;
1568 hdr[0] = '<';
1569 hdr[1] = '0' + MAX(level, logsrv->minlvl);
1570 hdr[2] = '>';
1571 hdr_ptr = hdr;
1572 hdr_max = 3;
1573 maxlen = logsrv->maxlen - hdr_max;
Emeric Brun4ce00fc2020-05-12 19:33:15 +02001574 max = MIN(size, maxlen - 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001575 goto send;
Willy Tarreau204e3f12018-12-15 15:48:48 +01001576
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001577 case LOG_FORMAT_RAW:
1578 /* all fields are known, skip the header generation */
1579 hdr_ptr = hdr = "";
1580 hdr_max = 0;
1581 maxlen = logsrv->maxlen;
Emeric Brun4ce00fc2020-05-12 19:33:15 +02001582 max = MIN(size, maxlen - 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001583 goto send;
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001584
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001585 default:
1586 return; /* must never happen */
1587 }
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001588
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001589 hdr_size = hdr_ptr - hdr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001590
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001591 /* For each target, we may have a different facility.
1592 * We can also have a different log level for each message.
1593 * This induces variations in the message header length.
1594 * Since we don't want to recompute it each time, nor copy it every
1595 * time, we only change the facility in the pre-computed header,
1596 * and we change the pointer to the header accordingly.
1597 */
1598 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
1599 hdr_ptr = hdr + 3; /* last digit of the log level */
1600 do {
1601 *hdr_ptr = '0' + fac_level % 10;
1602 fac_level /= 10;
1603 hdr_ptr--;
1604 } while (fac_level && hdr_ptr > hdr);
1605 *hdr_ptr = '<';
Dragan Dosen1322d092015-09-22 16:05:32 +02001606
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001607 hdr_max = hdr_size - (hdr_ptr - hdr);
Willy Tarreaue8746a02018-11-12 08:45:00 +01001608
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001609 /* time-based header */
1610 if (unlikely(hdr_size >= logsrv->maxlen)) {
1611 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
1612 sd_max = 0;
1613 goto send;
1614 }
Willy Tarreauc1b06452018-11-12 11:57:56 +01001615
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001616 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen1322d092015-09-22 16:05:32 +02001617
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001618 /* tag */
Frédéric Lécaille90a10ae2019-05-14 10:57:58 +02001619 tag_max = tag_size;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001620 if (unlikely(tag_max >= maxlen)) {
1621 tag_max = maxlen - 1;
1622 sd_max = 0;
1623 goto send;
1624 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001625
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001626 maxlen -= tag_max;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001627
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001628 /* first pid separator */
1629 pid_sep1_max = log_formats[logsrv->format].pid.sep1.data;
1630 if (unlikely(pid_sep1_max >= maxlen)) {
1631 pid_sep1_max = maxlen - 1;
1632 sd_max = 0;
1633 goto send;
1634 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001635
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001636 pid_sep1 = log_formats[logsrv->format].pid.sep1.area;
1637 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001638
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001639 /* pid */
Frédéric Lécaille90a10ae2019-05-14 10:57:58 +02001640 pid_max = pid_size;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001641 if (unlikely(pid_size >= maxlen)) {
1642 pid_size = maxlen - 1;
1643 sd_max = 0;
1644 goto send;
1645 }
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001646
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001647 maxlen -= pid_size;
Dragan Dosen43885c72015-10-01 13:18:13 +02001648
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001649 /* second pid separator */
1650 pid_sep2_max = log_formats[logsrv->format].pid.sep2.data;
1651 if (unlikely(pid_sep2_max >= maxlen)) {
1652 pid_sep2_max = maxlen - 1;
1653 sd_max = 0;
1654 goto send;
1655 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001656
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001657 pid_sep2 = log_formats[logsrv->format].pid.sep2.area;
1658 maxlen -= pid_sep2_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001659
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001660 /* structured-data */
1661 if (sd_max >= maxlen) {
1662 sd_max = maxlen - 1;
1663 goto send;
1664 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001665
Emeric Brun4ce00fc2020-05-12 19:33:15 +02001666 max = MIN(size, maxlen - sd_max - 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001667send:
1668 iovec[0].iov_base = hdr_ptr;
1669 iovec[0].iov_len = hdr_max;
1670 iovec[1].iov_base = tag_str;
Frédéric Lécaille90a10ae2019-05-14 10:57:58 +02001671 iovec[1].iov_len = tag_max;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001672 iovec[2].iov_base = pid_sep1;
1673 iovec[2].iov_len = pid_sep1_max;
1674 iovec[3].iov_base = pid_str;
Frédéric Lécaille90a10ae2019-05-14 10:57:58 +02001675 iovec[3].iov_len = pid_max;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001676 iovec[4].iov_base = pid_sep2;
1677 iovec[4].iov_len = pid_sep2_max;
1678 iovec[5].iov_base = sd;
1679 iovec[5].iov_len = sd_max;
1680 iovec[6].iov_base = dataptr;
1681 iovec[6].iov_len = max;
1682 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1683 iovec[7].iov_len = 1;
Dragan Dosen43885c72015-10-01 13:18:13 +02001684
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001685 if (logsrv->addr.ss_family == AF_UNSPEC) {
Willy Tarreaueca0ecf2020-06-11 14:25:47 +02001686 int attempts = 0;
1687 /* make sure we never interleave writes and we never block. This means
1688 * we prefer to fail on collision than to block. But we don't want to
1689 * lose too many logs so we just perform a few lock attempts then give
1690 * up.
1691 */
1692
1693 while (HA_SPIN_TRYLOCK(LOGSRV_LOCK, &logsrv->lock) != 0) {
1694 if (++attempts >= 200) {
1695 /* so that the caller knows the message couldn't be delivered */
1696 sent = -1;
1697 errno = EAGAIN;
1698 goto leave;
1699 }
1700 ha_thread_relax();
1701 }
1702
Willy Tarreau487b38e2019-07-26 15:10:39 +02001703 /* the target is a direct file descriptor. While writev() guarantees
1704 * to write everything, it doesn't guarantee that it will not be
1705 * interrupted while doing so. This occasionally results in interleaved
1706 * messages when the output is a tty, hence the lock. There's no real
1707 * performance concern here for such type of output.
1708 */
Willy Tarreaueca0ecf2020-06-11 14:25:47 +02001709
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001710 sent = writev(*plogfd, iovec, 8);
Willy Tarreau487b38e2019-07-26 15:10:39 +02001711 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001712 }
1713 else {
1714 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1715 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001716
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001717 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1718 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001719
Willy Tarreaueca0ecf2020-06-11 14:25:47 +02001720leave:
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001721 if (sent < 0) {
1722 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001723
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001724 if (errno == EAGAIN)
1725 _HA_ATOMIC_ADD(&dropped_logs, 1);
1726 else if (!once) {
1727 once = 1; /* note: no need for atomic ops here */
1728 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1729 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001730 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001731 }
1732}
Dragan Dosen59cee972015-09-19 22:09:02 +02001733
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001734/*
1735 * This function sends a syslog message.
1736 * It doesn't care about errors nor does it report them.
1737 * The arguments <sd> and <sd_size> are used for the structured-data part
1738 * in RFC5424 formatted syslog messages.
1739 */
1740void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size)
1741{
1742 struct list *logsrvs = NULL;
1743 struct logsrv *logsrv;
1744 int nblogger;
1745 static THREAD_LOCAL int curr_pid;
1746 static THREAD_LOCAL char pidstr[100];
1747 static THREAD_LOCAL struct buffer pid;
1748 struct buffer *tag = &global.log_tag;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001749
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001750 if (p == NULL) {
1751 if (!LIST_ISEMPTY(&global.logsrvs)) {
1752 logsrvs = &global.logsrvs;
Willy Tarreau5a32ecc2018-11-12 07:34:59 +01001753 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001754 } else {
1755 if (!LIST_ISEMPTY(&p->logsrvs)) {
1756 logsrvs = &p->logsrvs;
Willy Tarreau5a32ecc2018-11-12 07:34:59 +01001757 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001758 if (p->log_tag.area) {
1759 tag = &p->log_tag;
1760 }
1761 }
Willy Tarreau18324f52014-06-27 18:10:07 +02001762
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001763 if (!logsrvs)
1764 return;
Willy Tarreauc98aebc2018-03-20 11:17:29 +01001765
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001766 if (unlikely(curr_pid != getpid())) {
1767 curr_pid = getpid();
1768 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1769 chunk_initstr(&pid, pidstr);
1770 }
1771
1772 /* Send log messages to syslog server. */
1773 nblogger = 0;
1774 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun38eff0d2020-07-10 15:47:11 +02001775 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001776
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001777 /* we can filter the level of the messages that are sent to each logger */
1778 if (level > logsrv->level)
1779 continue;
1780
Frédéric Lécailled803e472019-04-25 07:42:09 +02001781 if (logsrv->lb.smp_rgs) {
1782 struct smp_log_range *curr_rg;
1783
1784 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1785 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1786 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1787 if (in_range) {
1788 /* Let's consume this range. */
1789 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1790 if (!curr_rg->curr_idx) {
1791 /* If consumed, let's select the next range. */
1792 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1793 }
1794 }
1795 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1796 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1797 }
1798 if (in_range)
1799 __do_send_log(logsrv, ++nblogger, pid.area, pid.data, level,
1800 message, size, sd, sd_size, tag->area, tag->data);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001801 }
1802}
1803
1804
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001805const 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 +01001806const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1807 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1808 Set-cookie Updated, unknown, unknown */
1809
William Lallemand1d705562012-03-12 12:46:41 +01001810/*
1811 * try to write a character if there is enough space, or goto out
1812 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001813#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001814 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001815 *(tmplog++) = (x); \
1816 } else { \
1817 goto out; \
1818 } \
1819 } while(0)
1820
Dragan Dosen835b9212016-02-12 13:23:03 +01001821
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001822/* Initializes some log data at boot */
1823static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001824{
1825 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001826 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001827
1828 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1829 * inside PARAM-VALUE should be escaped with '\' as prefix.
1830 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1831 * details.
1832 */
1833 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1834
1835 tmp = "\"\\]";
1836 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001837 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001838 tmp++;
1839 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001840
1841 /* initialize the log header encoding map : '{|}"#' should be encoded with
1842 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1843 * URL encoding only requires '"', '#' to be encoded as well as non-
1844 * printable characters above.
1845 */
1846 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1847 memset(url_encode_map, 0, sizeof(url_encode_map));
1848 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001849 ha_bit_set(i, hdr_encode_map);
1850 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001851 }
1852 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001853 ha_bit_set(i, hdr_encode_map);
1854 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001855 }
1856
1857 tmp = "\"#{|}";
1858 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001859 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001860 tmp++;
1861 }
1862
1863 tmp = "\"#";
1864 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001865 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001866 tmp++;
1867 }
1868
1869 /* initialize the http header encoding map. The draft httpbis define the
1870 * header content as:
1871 *
1872 * HTTP-message = start-line
1873 * *( header-field CRLF )
1874 * CRLF
1875 * [ message-body ]
1876 * header-field = field-name ":" OWS field-value OWS
1877 * field-value = *( field-content / obs-fold )
1878 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1879 * obs-fold = CRLF 1*( SP / HTAB )
1880 * field-vchar = VCHAR / obs-text
1881 * VCHAR = %x21-7E
1882 * obs-text = %x80-FF
1883 *
1884 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1885 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08001886 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02001887 */
1888 memset(http_encode_map, 0, sizeof(http_encode_map));
1889 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001890 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001891 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001892 ha_bit_set(i, http_encode_map);
1893 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001894}
William Lallemand1d705562012-03-12 12:46:41 +01001895
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001896INITCALL0(STG_PREPARE, init_log);
1897
Christopher Faulet0132d062017-07-26 15:33:35 +02001898/* Initialize log buffers used for syslog messages */
1899int init_log_buffers()
1900{
1901 logheader = my_realloc2(logheader, global.max_syslog_len + 1);
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001902 logheader_end = NULL;
Christopher Faulet0132d062017-07-26 15:33:35 +02001903 logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1);
Willy Tarreau55e2f5a2019-05-05 10:11:39 +02001904 logheader_rfc5424_end = NULL;
Christopher Faulet0132d062017-07-26 15:33:35 +02001905 logline = my_realloc2(logline, global.max_syslog_len + 1);
1906 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
1907 if (!logheader || !logline_rfc5424 || !logline || !logline_rfc5424)
1908 return 0;
1909 return 1;
1910}
1911
1912/* Deinitialize log buffers used for syslog messages */
1913void deinit_log_buffers()
1914{
Olivier Houchard7c497112019-03-07 14:19:24 +01001915 void *tmp_startup_logs;
1916
Christopher Faulet0132d062017-07-26 15:33:35 +02001917 free(logheader);
1918 free(logheader_rfc5424);
1919 free(logline);
1920 free(logline_rfc5424);
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01001921 tmp_startup_logs = _HA_ATOMIC_XCHG(&startup_logs, NULL);
Olivier Houchard7c497112019-03-07 14:19:24 +01001922 free(tmp_startup_logs);
1923
Christopher Faulet0132d062017-07-26 15:33:35 +02001924 logheader = NULL;
1925 logheader_rfc5424 = NULL;
1926 logline = NULL;
1927 logline_rfc5424 = NULL;
Christopher Fauletd4696382017-10-24 11:44:05 +02001928 startup_logs = NULL;
Christopher Faulet0132d062017-07-26 15:33:35 +02001929}
1930
Willy Tarreaudf974472012-12-28 02:44:01 +01001931/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1932 * <maxsize> characters. Returns the size of the output string in characters,
1933 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001934 * is not zero. It requires a valid session and optionally a stream. If the
1935 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01001936 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001937int 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 +02001938{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001939 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001940 struct proxy *be;
1941 struct http_txn *txn;
1942 const struct strm_logs *logs;
1943 const struct connection *be_conn;
1944 unsigned int s_flags;
1945 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02001946 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001947 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001948 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001949 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001950 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001951 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001952 int t_request;
1953 int hdr;
1954 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001955 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001956 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001957 char *ret;
1958 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001959 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001960 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001961 struct strm_logs tmp_strm_log;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001962
William Lallemandbddd4fd2012-02-27 11:23:10 +01001963 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001964
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001965 if (likely(s)) {
1966 be = s->be;
1967 txn = s->txn;
1968 be_conn = cs_conn(objt_cs(s->si[1].end));
1969 s_flags = s->flags;
1970 uniq_id = s->uniq_id;
1971 logs = &s->logs;
1972 } else {
1973 /* we have no stream so we first need to initialize a few
1974 * things that are needed later. We do increment the request
1975 * ID so that it's uniquely assigned to this request just as
1976 * if the request had reached the point of being processed.
1977 * A request error is reported as it's the only element we have
1978 * here and which justifies emitting such a log.
1979 */
1980 be = fe;
1981 txn = NULL;
1982 be_conn = NULL;
1983 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01001984 uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001985
1986 /* prepare a valid log structure */
1987 tmp_strm_log.tv_accept = sess->tv_accept;
1988 tmp_strm_log.accept_date = sess->accept_date;
1989 tmp_strm_log.t_handshake = sess->t_handshake;
1990 tmp_strm_log.t_idle = tv_ms_elapsed(&sess->tv_accept, &now) - sess->t_handshake;
1991 tv_zero(&tmp_strm_log.tv_request);
1992 tmp_strm_log.t_queue = -1;
1993 tmp_strm_log.t_connect = -1;
1994 tmp_strm_log.t_data = -1;
1995 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
1996 tmp_strm_log.bytes_in = 0;
1997 tmp_strm_log.bytes_out = 0;
1998 tmp_strm_log.prx_queue_pos = 0;
1999 tmp_strm_log.srv_queue_pos = 0;
2000
2001 logs = &tmp_strm_log;
2002 }
2003
William Lallemandbddd4fd2012-02-27 11:23:10 +01002004 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002005 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2006 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002007
William Lallemand1d705562012-03-12 12:46:41 +01002008 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002009
William Lallemandbddd4fd2012-02-27 11:23:10 +01002010 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002011 if (LIST_ISEMPTY(list_format))
2012 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002013
William Lallemand1d705562012-03-12 12:46:41 +01002014 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002015 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02002016 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002017 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002018 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002019
Willy Tarreauc8368452012-12-21 00:09:23 +01002020 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002021 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002022 if (!last_isspace) {
2023 LOGCHAR(' ');
2024 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002025 }
2026 break;
2027
William Lallemand1d705562012-03-12 12:46:41 +01002028 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002029 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002030 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002031 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002032 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002033 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002034 last_isspace = 0;
2035 break;
2036
Willy Tarreauc8368452012-12-21 00:09:23 +01002037 case LOG_FMT_EXPR: // sample expression, may be request or response
2038 key = NULL;
Olivier Houchardf90db442018-12-15 14:00:06 +01002039 if (tmp->options & LOG_OPT_REQ_CAP && s)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002040 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 +01002041 if (!key && (tmp->options & LOG_OPT_RES_CAP) && s)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002042 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 +01002043 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002044 ret = lf_encode_chunk(tmplog, dst + maxsize,
2045 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002046 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002047 ret = lf_text_len(tmplog,
2048 key ? key->data.u.str.area : NULL,
2049 key ? key->data.u.str.data : 0,
2050 dst + maxsize - tmplog,
2051 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002052 if (ret == 0)
2053 goto out;
2054 tmplog = ret;
2055 last_isspace = 0;
2056 break;
2057
Willy Tarreau2beef582012-12-20 17:22:52 +01002058 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002059 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002060 if (conn)
2061 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
2062 else
2063 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002064 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002065 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002066 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002067 last_isspace = 0;
2068 break;
2069
Willy Tarreau2beef582012-12-20 17:22:52 +01002070 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002071 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002072 if (conn) {
2073 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002074 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002075 } else {
2076 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
2077 dst + maxsize - tmplog, tmp);
2078 }
William Lallemand5f232402012-04-05 18:02:55 +02002079 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002080 else
2081 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2082
William Lallemand5f232402012-04-05 18:02:55 +02002083 if (ret == NULL)
2084 goto out;
2085 tmplog = ret;
2086 last_isspace = 0;
2087 break;
2088
Willy Tarreau2beef582012-12-20 17:22:52 +01002089 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002090 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002091 if (conn) {
2092 conn_get_to_addr(conn);
2093 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
2094 }
2095 else
2096 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2097
William Lallemand1d705562012-03-12 12:46:41 +01002098 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002099 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002100 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002101 last_isspace = 0;
2102 break;
2103
Willy Tarreau2beef582012-12-20 17:22:52 +01002104 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002105 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002106 if (conn) {
2107 conn_get_to_addr(conn);
2108 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002109 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002110 else
2111 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002112 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002113 else
2114 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2115
William Lallemand5f232402012-04-05 18:02:55 +02002116 if (ret == NULL)
2117 goto out;
2118 tmplog = ret;
2119 last_isspace = 0;
2120 break;
2121
Willy Tarreau2beef582012-12-20 17:22:52 +01002122 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau2393c5b2018-09-05 15:24:56 +02002123 if (be_conn)
2124 ret = lf_ip(tmplog, (const struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002125 else
2126 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2127
William Lallemand1d705562012-03-12 12:46:41 +01002128 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002129 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002130 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002131 last_isspace = 0;
2132 break;
2133
Willy Tarreau2beef582012-12-20 17:22:52 +01002134 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau2393c5b2018-09-05 15:24:56 +02002135 if (be_conn)
2136 ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002137 else
2138 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2139
William Lallemand5f232402012-04-05 18:02:55 +02002140 if (ret == NULL)
2141 goto out;
2142 tmplog = ret;
2143 last_isspace = 0;
2144 break;
2145
Willy Tarreau2beef582012-12-20 17:22:52 +01002146 case LOG_FMT_SERVERIP: // %si
Willy Tarreau2393c5b2018-09-05 15:24:56 +02002147 if (be_conn)
2148 ret = lf_ip(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002149 else
2150 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2151
William Lallemand5f232402012-04-05 18:02:55 +02002152 if (ret == NULL)
2153 goto out;
2154 tmplog = ret;
2155 last_isspace = 0;
2156 break;
2157
Willy Tarreau2beef582012-12-20 17:22:52 +01002158 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau2393c5b2018-09-05 15:24:56 +02002159 if (be_conn)
2160 ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002161 else
2162 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2163
William Lallemand1d705562012-03-12 12:46:41 +01002164 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002165 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002166 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002167 last_isspace = 0;
2168 break;
2169
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002170 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002171 get_localtime(logs->accept_date.tv_sec, &tm);
2172 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002173 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002174 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002175 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002176 last_isspace = 0;
2177 break;
2178
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002179 case LOG_FMT_tr: // %tr = start of request date
2180 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002181 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 +02002182 get_localtime(tv.tv_sec, &tm);
2183 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2184 if (ret == NULL)
2185 goto out;
2186 tmplog = ret;
2187 last_isspace = 0;
2188 break;
2189
2190 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002191 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002192 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002193 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002194 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002195 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002196 last_isspace = 0;
2197 break;
2198
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002199 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002200 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 +02002201 get_gmtime(tv.tv_sec, &tm);
2202 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2203 if (ret == NULL)
2204 goto out;
2205 tmplog = ret;
2206 last_isspace = 0;
2207 break;
2208
2209 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002210 get_localtime(logs->accept_date.tv_sec, &tm);
2211 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002212 if (ret == NULL)
2213 goto out;
2214 tmplog = ret;
2215 last_isspace = 0;
2216 break;
2217
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002218 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002219 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 +02002220 get_localtime(tv.tv_sec, &tm);
2221 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2222 if (ret == NULL)
2223 goto out;
2224 tmplog = ret;
2225 last_isspace = 0;
2226 break;
2227
William Lallemand5f232402012-04-05 18:02:55 +02002228 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002229 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002230 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002231 if (iret < 0 || iret > dst + maxsize - tmplog)
2232 goto out;
2233 last_isspace = 0;
2234 tmplog += iret;
2235 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002236 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002237 if (ret == NULL)
2238 goto out;
2239 tmplog = ret;
2240 last_isspace = 0;
2241 }
2242 break;
2243
William Lallemand1d705562012-03-12 12:46:41 +01002244 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002245 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002246 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002247 if (iret < 0 || iret > dst + maxsize - tmplog)
2248 goto out;
2249 last_isspace = 0;
2250 tmplog += iret;
2251 } else {
2252 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002253 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002254 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002255 tmplog, 4);
2256 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002257 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002258 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002259 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002260 }
2261 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002262
William Lallemand1d705562012-03-12 12:46:41 +01002263 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002264 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002265 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002266 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002267 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002268 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002269 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002270 break;
2271
Willy Tarreau773d65f2012-10-12 14:56:11 +02002272 case LOG_FMT_FRONTEND_XPRT: // %ft
2273 src = fe->id;
2274 if (tmp->options & LOG_OPT_QUOTE)
2275 LOGCHAR('"');
2276 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2277 if (iret == 0)
2278 goto out;
2279 tmplog += iret;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01002280 if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002281 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002282 if (tmp->options & LOG_OPT_QUOTE)
2283 LOGCHAR('"');
2284 last_isspace = 0;
2285 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002286#ifdef USE_OPENSSL
2287 case LOG_FMT_SSL_CIPHER: // %sslc
2288 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002289 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002290 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002291 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002292 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002293 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2294 if (ret == NULL)
2295 goto out;
2296 tmplog = ret;
2297 last_isspace = 0;
2298 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002299
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002300 case LOG_FMT_SSL_VERSION: // %sslv
2301 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002302 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002303 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002304 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002305 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002306 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2307 if (ret == NULL)
2308 goto out;
2309 tmplog = ret;
2310 last_isspace = 0;
2311 break;
2312#endif
William Lallemand1d705562012-03-12 12:46:41 +01002313 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002314 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002315 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002316 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002317 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002318 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002319 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002320 break;
2321
William Lallemand1d705562012-03-12 12:46:41 +01002322 case LOG_FMT_SERVER: // %s
Willy Tarreaue1809df2018-09-05 15:30:16 +02002323 switch (obj_type(s ? s->target : NULL)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002324 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002325 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002326 break;
2327 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002328 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002329 break;
2330 default:
2331 src = "<NOSRV>";
2332 break;
2333 }
William Lallemand5f232402012-04-05 18:02:55 +02002334 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002335 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002336 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002337 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002338 last_isspace = 0;
2339 break;
2340
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002341 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002342 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002343 if (ret == NULL)
2344 goto out;
2345 tmplog = ret;
2346 last_isspace = 0;
2347 break;
2348
2349 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002350 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002351 if (ret == NULL)
2352 goto out;
2353 tmplog = ret;
2354 last_isspace = 0;
2355 break;
2356
2357 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002358 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002359 tmplog, dst + maxsize - tmplog);
2360 if (ret == NULL)
2361 goto out;
2362 tmplog = ret;
2363 last_isspace = 0;
2364 break;
2365
2366 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002367 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002368 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002369 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002370 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002371 last_isspace = 0;
2372 break;
2373
William Lallemand1d705562012-03-12 12:46:41 +01002374 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002375 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002376 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002377 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002378 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002379 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002380 last_isspace = 0;
2381 break;
2382
William Lallemand1d705562012-03-12 12:46:41 +01002383 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002384 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002385 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002386 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002387 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002388 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002389 last_isspace = 0;
2390 break;
2391
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002392 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002393 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002394 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002395 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002396 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002397 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002398 last_isspace = 0;
2399 break;
2400
Willy Tarreau27b639d2016-05-17 17:55:27 +02002401 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002402 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002403 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002404 tmplog, dst + maxsize - tmplog);
2405 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002406 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002407 tmplog, dst + maxsize - tmplog);
2408 if (ret == NULL)
2409 goto out;
2410 tmplog = ret;
2411 last_isspace = 0;
2412 break;
2413
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002414 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2415 if (!(fe->to_log & LW_BYTES))
2416 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002417 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 +02002418 tmplog, dst + maxsize - tmplog);
2419 if (ret == NULL)
2420 goto out;
2421 tmplog = ret;
2422 last_isspace = 0;
2423 break;
2424
2425 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002426 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002427 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002428 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002429 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002430 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002431 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002432 last_isspace = 0;
2433 break;
2434
Willy Tarreau2beef582012-12-20 17:22:52 +01002435 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02002436 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002437 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002438 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002439 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002440 last_isspace = 0;
2441 break;
2442
William Lallemand1d705562012-03-12 12:46:41 +01002443 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002444 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002445 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002446 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002447 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002448 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002449 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002450 last_isspace = 0;
2451 break;
2452
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002453 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002454 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002455 if (ret == NULL)
2456 goto out;
2457 tmplog = ret;
2458 last_isspace = 0;
2459 break;
2460
Willy Tarreau2beef582012-12-20 17:22:52 +01002461 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002462 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002463 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002464 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002465 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002466 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002467 last_isspace = 0;
2468 break;
2469
Willy Tarreau2beef582012-12-20 17:22:52 +01002470 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002471 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002472 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002473 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002474 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002475 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002476 last_isspace = 0;
2477 break;
2478
William Lallemand1d705562012-03-12 12:46:41 +01002479 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002480 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2481 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002482 *tmplog = '\0';
2483 last_isspace = 0;
2484 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002485
William Lallemand1d705562012-03-12 12:46:41 +01002486 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002487 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2488 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002489 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2490 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 +01002491 last_isspace = 0;
2492 break;
2493
William Lallemand1d705562012-03-12 12:46:41 +01002494 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002495 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002496 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002497 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002498 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002499 last_isspace = 0;
2500 break;
2501
William Lallemand1d705562012-03-12 12:46:41 +01002502 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002503 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002504 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002505 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002506 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002507 last_isspace = 0;
2508 break;
2509
William Lallemand1d705562012-03-12 12:46:41 +01002510 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002511 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002512 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002513 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002514 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002515 last_isspace = 0;
2516 break;
2517
William Lallemand1d705562012-03-12 12:46:41 +01002518 case LOG_FMT_SRVCONN: // %sc
Willy Tarreaue1809df2018-09-05 15:30:16 +02002519 ret = ultoa_o(objt_server(s ? s->target : NULL) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002520 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02002521 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002522 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002523 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002524 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002525 last_isspace = 0;
2526 break;
2527
William Lallemand1d705562012-03-12 12:46:41 +01002528 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002529 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002530 LOGCHAR('+');
Willy Tarreauabd71a52018-09-04 19:21:44 +02002531 ret = ltoa_o((s && s->si[1].conn_retries > 0) ?
Willy Tarreau350f4872014-11-28 14:42:25 +01002532 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02002533 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002534 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002535 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002536 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002537 last_isspace = 0;
2538 break;
2539
William Lallemand1d705562012-03-12 12:46:41 +01002540 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002541 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002542 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002543 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002544 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002545 last_isspace = 0;
2546 break;
2547
William Lallemand1d705562012-03-12 12:46:41 +01002548 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002549 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002550 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002551 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002552 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002553 last_isspace = 0;
2554 break;
2555
William Lallemand1d705562012-03-12 12:46:41 +01002556 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002557 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002558 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002559 if (tmp->options & LOG_OPT_QUOTE)
2560 LOGCHAR('"');
2561 LOGCHAR('{');
2562 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2563 if (hdr)
2564 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002565 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002566 ret = lf_encode_string(tmplog, dst + maxsize,
2567 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002568 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002569 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002570 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002571 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002572 }
2573 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002574 if (tmp->options & LOG_OPT_QUOTE)
2575 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002576 last_isspace = 0;
2577 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002578 break;
2579
William Lallemand1d705562012-03-12 12:46:41 +01002580 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002581 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002582 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002583 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2584 if (hdr > 0)
2585 LOGCHAR(' ');
2586 if (tmp->options & LOG_OPT_QUOTE)
2587 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002588 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002589 ret = lf_encode_string(tmplog, dst + maxsize,
2590 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002591 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002592 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002593 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002594 } else if (!(tmp->options & LOG_OPT_QUOTE))
2595 LOGCHAR('-');
2596 if (tmp->options & LOG_OPT_QUOTE)
2597 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002598 last_isspace = 0;
2599 }
2600 }
2601 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002602
William Lallemand1d705562012-03-12 12:46:41 +01002603
2604 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002605 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002606 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002607 if (tmp->options & LOG_OPT_QUOTE)
2608 LOGCHAR('"');
2609 LOGCHAR('{');
2610 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2611 if (hdr)
2612 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002613 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002614 ret = lf_encode_string(tmplog, dst + maxsize,
2615 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002616 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002617 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002618 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002619 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002620 }
2621 LOGCHAR('}');
2622 last_isspace = 0;
2623 if (tmp->options & LOG_OPT_QUOTE)
2624 LOGCHAR('"');
2625 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002626 break;
2627
William Lallemand1d705562012-03-12 12:46:41 +01002628 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002629 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002630 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002631 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2632 if (hdr > 0)
2633 LOGCHAR(' ');
2634 if (tmp->options & LOG_OPT_QUOTE)
2635 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002636 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002637 ret = lf_encode_string(tmplog, dst + maxsize,
2638 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002639 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002640 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002641 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002642 } else if (!(tmp->options & LOG_OPT_QUOTE))
2643 LOGCHAR('-');
2644 if (tmp->options & LOG_OPT_QUOTE)
2645 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002646 last_isspace = 0;
2647 }
2648 }
2649 break;
2650
William Lallemand1d705562012-03-12 12:46:41 +01002651 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002652 /* Request */
2653 if (tmp->options & LOG_OPT_QUOTE)
2654 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002655 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002656 ret = lf_encode_string(tmplog, dst + maxsize,
2657 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002658 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002659 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002660 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002661 if (tmp->options & LOG_OPT_QUOTE)
2662 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002663 last_isspace = 0;
2664 break;
William Lallemand5f232402012-04-05 18:02:55 +02002665
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002666 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002667 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002668
Willy Tarreaub7636d12015-06-17 19:58:02 +02002669 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002670 LOGCHAR('"');
2671
2672 end = uri + strlen(uri);
2673 // look for the first whitespace character
2674 while (uri < end && !HTTP_IS_SPHT(*uri))
2675 uri++;
2676
2677 // keep advancing past multiple spaces
2678 while (uri < end && HTTP_IS_SPHT(*uri)) {
2679 uri++; nspaces++;
2680 }
2681
2682 // look for first space or question mark after url
2683 spc = uri;
2684 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2685 spc++;
2686
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002687 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002688 chunk.area = "<BADREQ>";
2689 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002690 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002691 chunk.area = uri;
2692 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002693 }
2694
Dragan Dosen835b9212016-02-12 13:23:03 +01002695 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002696 if (ret == NULL || *ret != '\0')
2697 goto out;
2698
2699 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002700 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002701 LOGCHAR('"');
2702
2703 last_isspace = 0;
2704 break;
2705
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002706 case LOG_FMT_HTTP_QUERY: // %HQ
2707 if (tmp->options & LOG_OPT_QUOTE)
2708 LOGCHAR('"');
2709
Willy Tarreau57bc8912016-04-25 17:09:40 +02002710 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002711 chunk.area = "<BADREQ>";
2712 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002713 } else {
2714 uri = txn->uri;
2715 end = uri + strlen(uri);
2716 // look for the first question mark
2717 while (uri < end && *uri != '?')
2718 uri++;
2719
2720 qmark = uri;
2721 // look for first space or question mark after url
2722 while (uri < end && !HTTP_IS_SPHT(*uri))
2723 uri++;
2724
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002725 chunk.area = qmark;
2726 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002727 }
2728
Dragan Dosen835b9212016-02-12 13:23:03 +01002729 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002730 if (ret == NULL || *ret != '\0')
2731 goto out;
2732
2733 tmplog = ret;
2734 if (tmp->options & LOG_OPT_QUOTE)
2735 LOGCHAR('"');
2736
2737 last_isspace = 0;
2738 break;
2739
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002740 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002741 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002742
Willy Tarreaub7636d12015-06-17 19:58:02 +02002743 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002744 LOGCHAR('"');
2745
2746 end = uri + strlen(uri);
2747 // look for the first whitespace character
2748 while (uri < end && !HTTP_IS_SPHT(*uri))
2749 uri++;
2750
2751 // keep advancing past multiple spaces
2752 while (uri < end && HTTP_IS_SPHT(*uri)) {
2753 uri++; nspaces++;
2754 }
2755
2756 // look for first space after url
2757 spc = uri;
2758 while (spc < end && !HTTP_IS_SPHT(*spc))
2759 spc++;
2760
Willy Tarreau57bc8912016-04-25 17:09:40 +02002761 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002762 chunk.area = "<BADREQ>";
2763 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002764 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002765 chunk.area = uri;
2766 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002767 }
2768
Dragan Dosen835b9212016-02-12 13:23:03 +01002769 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002770 if (ret == NULL || *ret != '\0')
2771 goto out;
2772
2773 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002774 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002775 LOGCHAR('"');
2776
2777 last_isspace = 0;
2778 break;
2779
2780 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002781 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002782 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002783 LOGCHAR('"');
2784
2785 end = uri + strlen(uri);
2786 // look for the first whitespace character
2787 spc = uri;
2788 while (spc < end && !HTTP_IS_SPHT(*spc))
2789 spc++;
2790
2791 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002792 chunk.area = "<BADREQ>";
2793 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002794 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002795 chunk.area = uri;
2796 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002797 }
2798
Dragan Dosen835b9212016-02-12 13:23:03 +01002799 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002800 if (ret == NULL || *ret != '\0')
2801 goto out;
2802
2803 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002804 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002805 LOGCHAR('"');
2806
2807 last_isspace = 0;
2808 break;
2809
2810 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002811 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002812 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002813 LOGCHAR('"');
2814
2815 end = uri + strlen(uri);
2816 // look for the first whitespace character
2817 while (uri < end && !HTTP_IS_SPHT(*uri))
2818 uri++;
2819
2820 // keep advancing past multiple spaces
2821 while (uri < end && HTTP_IS_SPHT(*uri)) {
2822 uri++; nspaces++;
2823 }
2824
2825 // look for the next whitespace character
2826 while (uri < end && !HTTP_IS_SPHT(*uri))
2827 uri++;
2828
2829 // keep advancing past multiple spaces
2830 while (uri < end && HTTP_IS_SPHT(*uri))
2831 uri++;
2832
Willy Tarreau57bc8912016-04-25 17:09:40 +02002833 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002834 chunk.area = "<BADREQ>";
2835 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002836 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002837 chunk.area = "HTTP/0.9";
2838 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002839 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002840 chunk.area = uri;
2841 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002842 }
2843
Dragan Dosen835b9212016-02-12 13:23:03 +01002844 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002845 if (ret == NULL || *ret != '\0')
2846 goto out;
2847
2848 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002849 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002850 LOGCHAR('"');
2851
2852 last_isspace = 0;
2853 break;
2854
William Lallemand5f232402012-04-05 18:02:55 +02002855 case LOG_FMT_COUNTER: // %rt
2856 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002857 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002858 if (iret < 0 || iret > dst + maxsize - tmplog)
2859 goto out;
2860 last_isspace = 0;
2861 tmplog += iret;
2862 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002863 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002864 if (ret == NULL)
2865 goto out;
2866 tmplog = ret;
2867 last_isspace = 0;
2868 }
2869 break;
2870
Willy Tarreau7346acb2014-08-28 15:03:15 +02002871 case LOG_FMT_LOGCNT: // %lc
2872 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002873 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002874 if (iret < 0 || iret > dst + maxsize - tmplog)
2875 goto out;
2876 last_isspace = 0;
2877 tmplog += iret;
2878 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002879 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002880 if (ret == NULL)
2881 goto out;
2882 tmplog = ret;
2883 last_isspace = 0;
2884 }
2885 break;
2886
William Lallemand5f232402012-04-05 18:02:55 +02002887 case LOG_FMT_HOSTNAME: // %H
2888 src = hostname;
2889 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2890 if (ret == NULL)
2891 goto out;
2892 tmplog = ret;
2893 last_isspace = 0;
2894 break;
2895
2896 case LOG_FMT_PID: // %pid
2897 if (tmp->options & LOG_OPT_HEXA) {
2898 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2899 if (iret < 0 || iret > dst + maxsize - tmplog)
2900 goto out;
2901 last_isspace = 0;
2902 tmplog += iret;
2903 } else {
2904 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2905 if (ret == NULL)
2906 goto out;
2907 tmplog = ret;
2908 last_isspace = 0;
2909 }
2910 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002911
2912 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002913 ret = NULL;
Willy Tarreau02fdf4f2018-09-05 15:49:01 +02002914 src = s ? s->unique_id : NULL;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002915 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002916 if (ret == NULL)
2917 goto out;
2918 tmplog = ret;
2919 last_isspace = 0;
2920 break;
2921
William Lallemandbddd4fd2012-02-27 11:23:10 +01002922 }
2923 }
2924
2925out:
William Lallemand1d705562012-03-12 12:46:41 +01002926 /* *tmplog is a unused character */
2927 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002928 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002929
Willy Tarreaubaaee002006-06-26 02:48:02 +02002930}
2931
William Lallemand1d705562012-03-12 12:46:41 +01002932/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002933 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002934 * Will not log if the frontend has no log defined.
2935 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002936void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002937{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002938 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002939 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002940 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002941
2942 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002943 err = (s->flags & SF_REDISP) ||
2944 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2945 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002946 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002947 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002948
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002949 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002950 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002951
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002952 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002953 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002954
Willy Tarreauabcd5142013-06-11 17:18:02 +02002955 if (s->logs.level) { /* loglevel was overridden */
2956 if (s->logs.level == -1) {
2957 s->logs.logwait = 0; /* logs disabled */
2958 return;
2959 }
2960 level = s->logs.level - 1;
2961 }
2962 else {
2963 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002964 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002965 level = LOG_ERR;
2966 }
William Lallemand1d705562012-03-12 12:46:41 +01002967
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002968 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002969 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002970 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002971 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002972 }
2973
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002974 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2975 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2976 &sess->fe->logformat_sd);
2977 }
2978
Dragan Dosen59cee972015-09-19 22:09:02 +02002979 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002980 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01002981 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002982 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002983 s->logs.logwait = 0;
2984 }
2985}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002986
Willy Tarreau53839352018-09-05 19:51:10 +02002987/*
2988 * send a minimalist log for the session. Will not log if the frontend has no
2989 * log defined. It is assumed that this is only used to report anomalies that
2990 * cannot lead to the creation of a regular stream. Because of this the log
2991 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
2992 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02002993 * function to report unimportant events. It is safe to call this function with
2994 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02002995 */
2996void sess_log(struct session *sess)
2997{
2998 int size, level;
2999 int sd_size = 0;
3000
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003001 if (!sess)
3002 return;
3003
Willy Tarreau53839352018-09-05 19:51:10 +02003004 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3005 return;
3006
3007 level = LOG_INFO;
3008 if (sess->fe->options2 & PR_O2_LOGERRORS)
3009 level = LOG_ERR;
3010
3011 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3012 sd_size = sess_build_logline(sess, NULL,
3013 logline_rfc5424, global.max_syslog_len,
3014 &sess->fe->logformat_sd);
3015 }
3016
3017 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
3018 if (size > 0) {
Olivier Houchardd2ee3e72019-03-08 18:53:21 +01003019 _HA_ATOMIC_ADD(&sess->fe->log_count, 1);
Willy Tarreau53839352018-09-05 19:51:10 +02003020 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
3021 }
3022}
3023
Christopher Fauletc1b730a2017-10-24 12:00:51 +02003024static int cli_io_handler_show_startup_logs(struct appctx *appctx)
3025{
3026 struct stream_interface *si = appctx->owner;
3027 const char *msg = (startup_logs ? startup_logs : "No startup alerts/warnings.\n");
3028
3029 if (ci_putstr(si_ic(si), msg) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003030 si_rx_room_blk(si);
Christopher Fauletc1b730a2017-10-24 12:00:51 +02003031 return 0;
3032 }
3033 return 1;
3034}
3035
3036/* register cli keywords */
3037static struct cli_kw_list cli_kws = {{ },{
3038 { { "show", "startup-logs", NULL },
3039 "show startup-logs : report logs emitted during HAProxy startup",
3040 NULL, cli_io_handler_show_startup_logs },
3041 {{},}
3042}};
3043
Willy Tarreau0108d902018-11-25 19:14:37 +01003044INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3045
Willy Tarreau082b6282019-05-22 14:42:12 +02003046REGISTER_PER_THREAD_ALLOC(init_log_buffers);
3047REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01003048
Willy Tarreaubaaee002006-06-26 02:48:02 +02003049/*
3050 * Local variables:
3051 * c-indent-level: 8
3052 * c-basic-offset: 8
3053 * End:
3054 */