blob: 92eef6ebeb5f232360df981bdfb14de63f9691ba [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
Willy Tarreau077edcb2016-08-10 18:30:56 +020025#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/applet-t.h>
Emeric Brun12941c82020-07-07 14:19:42 +020029#include <haproxy/cfgparse.h>
Willy Tarreau55542642021-10-08 09:33:24 +020030#include <haproxy/clock.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020032#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020034#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020035#include <haproxy/http_ana.h>
Emeric Brun12941c82020-07-07 14:19:42 +020036#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020037#include <haproxy/log.h>
Emeric Brun12941c82020-07-07 14:19:42 +020038#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/sample.h>
40#include <haproxy/sink.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020041#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020043#include <haproxy/stream_interface.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020044#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020045#include <haproxy/tools.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Emeric Brun45c457a2020-07-09 23:23:34 +020047/* global recv logs counter */
48int cum_log_messages;
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
Emeric Brun12941c82020-07-07 14:19:42 +020050/* log forward proxy list */
51struct proxy *cfg_log_forward;
52
Emeric Brun54648852020-07-06 15:54:06 +020053struct log_fmt_st {
Dragan Dosen43885c72015-10-01 13:18:13 +020054 char *name;
Dragan Dosen43885c72015-10-01 13:18:13 +020055};
56
Emeric Brun54648852020-07-06 15:54:06 +020057static const struct log_fmt_st log_formats[LOG_FORMATS] = {
Emeric Brun0237c4e2020-11-27 16:24:34 +010058 [LOG_FORMAT_LOCAL] = {
59 .name = "local",
60 },
Dragan Dosen43885c72015-10-01 13:18:13 +020061 [LOG_FORMAT_RFC3164] = {
62 .name = "rfc3164",
Dragan Dosen43885c72015-10-01 13:18:13 +020063 },
64 [LOG_FORMAT_RFC5424] = {
65 .name = "rfc5424",
Emeric Brun54648852020-07-06 15:54:06 +020066 },
67 [LOG_FORMAT_PRIO] = {
68 .name = "priority",
Willy Tarreaue8746a02018-11-12 08:45:00 +010069 },
70 [LOG_FORMAT_SHORT] = {
71 .name = "short",
Emeric Brun54648852020-07-06 15:54:06 +020072 },
73 [LOG_FORMAT_TIMED] = {
74 .name = "timed",
75 },
76 [LOG_FORMAT_ISO] = {
77 .name = "iso",
Willy Tarreaue8746a02018-11-12 08:45:00 +010078 },
Willy Tarreauc1b06452018-11-12 11:57:56 +010079 [LOG_FORMAT_RAW] = {
80 .name = "raw",
Willy Tarreauc1b06452018-11-12 11:57:56 +010081 },
Dragan Dosen1322d092015-09-22 16:05:32 +020082};
83
Dragan Dosen835b9212016-02-12 13:23:03 +010084/*
85 * This map is used with all the FD_* macros to check whether a particular bit
Willy Tarreau1bfd6022019-06-07 11:10:07 +020086 * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
87 * bytes which should be escaped. When ha_bit_test() returns non-zero, it means
88 * that the byte should be escaped. Be careful to always pass bytes from 0 to
89 * 255 exclusively to the macros.
Dragan Dosen835b9212016-02-12 13:23:03 +010090 */
Willy Tarreau1bfd6022019-06-07 11:10:07 +020091long rfc5424_escape_map[(256/8) / sizeof(long)];
92long hdr_encode_map[(256/8) / sizeof(long)];
93long url_encode_map[(256/8) / sizeof(long)];
94long http_encode_map[(256/8) / sizeof(long)];
Dragan Dosen835b9212016-02-12 13:23:03 +010095
Dragan Dosen835b9212016-02-12 13:23:03 +010096
Willy Tarreaubaaee002006-06-26 02:48:02 +020097const char *log_facilities[NB_LOG_FACILITIES] = {
98 "kern", "user", "mail", "daemon",
99 "auth", "syslog", "lpr", "news",
100 "uucp", "cron", "auth2", "ftp",
101 "ntp", "audit", "alert", "cron2",
102 "local0", "local1", "local2", "local3",
103 "local4", "local5", "local6", "local7"
104};
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106const char *log_levels[NB_LOG_LEVELS] = {
107 "emerg", "alert", "crit", "err",
108 "warning", "notice", "info", "debug"
109};
110
Willy Tarreau570f2212013-06-10 16:42:09 +0200111const 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 +0200112const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113
William Lallemand723b73a2012-02-08 16:37:49 +0100114
115/* log_format */
116struct logformat_type {
117 char *name;
118 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100119 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200120 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100121 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
William Lallemand723b73a2012-02-08 16:37:49 +0100122};
123
William Lallemandb7ff6a32012-03-02 14:35:21 +0100124int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
125
William Lallemand723b73a2012-02-08 16:37:49 +0100126/* log_format variable names */
127static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200128 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100129
130 /* please keep these lines sorted ! */
131 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
132 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
133 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
134 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100135 { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200136 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200137 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200138 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100139 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200140 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
141 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
142 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
143 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
144 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
145 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200146 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100147 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200148 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Damien Claisse57c8eb92020-04-28 12:09:19 +0000149 { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */
Willy Tarreau2beef582012-12-20 17:22:52 +0100150 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
151 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200152 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100153 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200154 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100155 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
156 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200157 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200158 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */
159 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100160 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
161 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
Willy Tarreaud02286d2017-06-23 11:23:43 +0200162 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */
163 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */
Willy Tarreau2beef582012-12-20 17:22:52 +0100164 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200165 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
166 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
167 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
168 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000169 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
Maciej Zdebfcdfd852020-11-30 18:27:47 +0000170 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP relative or absolute path */
171 { "HPO", LOG_FMT_HTTP_PATH_ONLY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path only (without host nor query string) */
Andrew Hayworthe63ac872015-07-31 16:14:16 +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 */
William Lallemand5e19a282012-04-02 16:22:10 +0200194 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100195};
196
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200197char 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
Willy Tarreau68574dd2021-11-05 19:14:55 +0100198char default_https_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 %[fc_err]/%[ssl_fc_err,hex]/%[ssl_c_err]/%[ssl_c_ca_err]/%[ssl_fc_is_resumed] %[ssl_fc_sni]/%sslv/%sslc";
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200199char 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 +0100200char 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 +0100201char *log_format = NULL;
202
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200203/* Default string used for structured-data part in RFC5424 formatted
204 * syslog messages.
205 */
206char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200207
Willy Tarreau13ef7732018-11-12 07:25:28 +0100208/* total number of dropped logs */
209unsigned int dropped_logs = 0;
210
Dragan Dosen59cee972015-09-19 22:09:02 +0200211/* This is a global syslog message buffer, common to all outgoing
212 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100213 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200214THREAD_LOCAL char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100215
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200216/* A global syslog message buffer, common to all RFC5424 syslog messages.
217 * Currently, it is used for generating the structured-data part.
218 */
Christopher Fauletf8188c62017-06-02 16:20:16 +0200219THREAD_LOCAL char *logline_rfc5424 = NULL;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200220
William Lallemand723b73a2012-02-08 16:37:49 +0100221struct logformat_var_args {
222 char *name;
223 int mask;
224};
225
226struct logformat_var_args var_args_list[] = {
227// global
228 { "M", LOG_OPT_MANDATORY },
229 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200230 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100231 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100232 { 0, 0 }
233};
234
235/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100236 * callback used to configure addr source retrieval
237 */
238int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
239{
240 curproxy->options2 |= PR_O2_SRC_ADDR;
241
242 return 0;
243}
244
245
246/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100247 * Parse args in a logformat_var. Returns 0 in error
248 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100249 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100250int parse_logformat_var_args(char *args, struct logformat_node *node, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100251{
252 int i = 0;
253 int end = 0;
254 int flags = 0; // 1 = + 2 = -
255 char *sp = NULL; // start pointer
256
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100257 if (args == NULL) {
258 memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'");
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100259 return 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100260 }
William Lallemand723b73a2012-02-08 16:37:49 +0100261
262 while (1) {
263 if (*args == '\0')
264 end = 1;
265
266 if (*args == '+') {
267 // add flag
268 sp = args + 1;
269 flags = 1;
270 }
271 if (*args == '-') {
272 // delete flag
273 sp = args + 1;
274 flags = 2;
275 }
276
277 if (*args == '\0' || *args == ',') {
278 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100279 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100280 if (strcmp(sp, var_args_list[i].name) == 0) {
281 if (flags == 1) {
282 node->options |= var_args_list[i].mask;
283 break;
284 } else if (flags == 2) {
285 node->options &= ~var_args_list[i].mask;
286 break;
287 }
288 }
289 }
290 sp = NULL;
291 if (end)
292 break;
293 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100294 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100295 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100296 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100297}
298
299/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100300 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
301 * must pass the args part in the <arg> pointer with its length in <arg_len>,
302 * and varname with its length in <var> and <var_len> respectively. <arg> is
303 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100304 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100305 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100306int 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 +0100307{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100308 int j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200309 struct logformat_node *node = NULL;
William Lallemand723b73a2012-02-08 16:37:49 +0100310
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100311 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
312 if (strlen(logformat_keywords[j].name) == var_len &&
313 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
314 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200315 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100316 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100317 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200318 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100319 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100320 node->type = logformat_keywords[j].type;
321 node->options = *defoptions;
322 if (arg_len) {
323 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100324 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200325 goto error_free;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100326 }
327 if (node->type == LOG_FMT_GLOBAL) {
328 *defoptions = node->options;
329 free(node->arg);
330 free(node);
331 } else {
332 if (logformat_keywords[j].config_callback &&
333 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Dragan Dosen61302da2019-04-30 00:40:02 +0200334 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100335 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100336 curproxy->to_log |= logformat_keywords[j].lw;
Willy Tarreau2b718102021-04-21 07:32:39 +0200337 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100338 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100339 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100340 } else {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100341 memprintf(err, "format variable '%s' is reserved for HTTP mode",
342 logformat_keywords[j].name);
Dragan Dosen61302da2019-04-30 00:40:02 +0200343 goto error_free;
William Lallemand723b73a2012-02-08 16:37:49 +0100344 }
William Lallemand723b73a2012-02-08 16:37:49 +0100345 }
346 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100347
348 j = var[var_len];
349 var[var_len] = 0;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100350 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 +0100351 var[var_len] = j;
Dragan Dosen61302da2019-04-30 00:40:02 +0200352
353 error_free:
354 if (node) {
355 free(node->arg);
356 free(node);
357 }
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100358 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100359}
360
361/*
362 * push to the logformat linked list
363 *
364 * start: start pointer
365 * end: end text pointer
366 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100367 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100368 *
369 * LOG_TEXT: copy chars from start to end excluding end.
370 *
371*/
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100372int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err)
William Lallemand723b73a2012-02-08 16:37:49 +0100373{
374 char *str;
375
Willy Tarreaua3571662012-12-20 21:59:12 +0100376 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200377 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100378 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100379 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100380 return 0;
381 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200382 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100383 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100384 str[end - start] = '\0';
385 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100386 node->type = LOG_FMT_TEXT; // type string
Willy Tarreau2b718102021-04-21 07:32:39 +0200387 LIST_APPEND(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100388 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200389 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100390 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100391 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100392 return 0;
393 }
William Lallemand1d705562012-03-12 12:46:41 +0100394 node->type = LOG_FMT_SEPARATOR;
Willy Tarreau2b718102021-04-21 07:32:39 +0200395 LIST_APPEND(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100396 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100397 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100398}
399
400/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100401 * Parse the sample fetch expression <text> and add a node to <list_format> upon
402 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100403 * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer
404 * is passed in <endptr>, it will be updated with the pointer to the first character
405 * not part of the sample expression.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100406 *
407 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100408 */
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100409int add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, char **err, char **endptr)
Willy Tarreauc8368452012-12-21 00:09:23 +0100410{
411 char *cmd[2];
Dragan Dosen61302da2019-04-30 00:40:02 +0200412 struct sample_expr *expr = NULL;
413 struct logformat_node *node = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100414 int cmd_arg;
415
416 cmd[0] = text;
417 cmd[1] = "";
418 cmd_arg = 0;
419
Christopher Fauleteaba25d2021-09-30 16:22:51 +0200420 expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err,
Christopher Faulet6ff7de52021-10-13 15:18:36 +0200421 &curpx->conf.args, endptr);
Willy Tarreauc8368452012-12-21 00:09:23 +0100422 if (!expr) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100423 memprintf(err, "failed to parse sample expression <%s> : %s", text, *err);
Dragan Dosen61302da2019-04-30 00:40:02 +0200424 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100425 }
426
Vincent Bernat02779b62016-04-03 13:48:43 +0200427 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100428 if (!node) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100429 memprintf(err, "out of memory error");
Dragan Dosen61302da2019-04-30 00:40:02 +0200430 goto error_free;
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100431 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100432 node->type = LOG_FMT_EXPR;
433 node->expr = expr;
434 node->options = options;
435
436 if (arg_len) {
437 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100438 if (!parse_logformat_var_args(node->arg, node, err))
Dragan Dosen61302da2019-04-30 00:40:02 +0200439 goto error_free;
Willy Tarreauc8368452012-12-21 00:09:23 +0100440 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100441 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100442 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
443
Willy Tarreau434c57c2013-01-08 01:10:24 +0100444 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100445 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
446
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100447 if (!(expr->fetch->val & cap)) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100448 memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here",
449 text, sample_src_names(expr->fetch->use));
Dragan Dosen61302da2019-04-30 00:40:02 +0200450 goto error_free;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100451 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100452
Christopher Faulet581db2b2021-03-26 10:02:46 +0100453 if ((options & LOG_OPT_HTTP) && (expr->fetch->use & (SMP_USE_L6REQ|SMP_USE_L6RES))) {
454 ha_warning("parsing [%s:%d] : L6 sample fetch <%s> ignored in HTTP log-format string.\n",
455 curpx->conf.args.file, curpx->conf.args.line, text);
456 }
457
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200458 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreauc8368452012-12-21 00:09:23 +0100459 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100460 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100461
William Lallemand65ad6e12014-01-31 15:08:02 +0100462 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
463 * needed with some sample fetches (eg: ssl*). We always set it for
464 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100465 */
466 curpx->to_log |= LW_XPRT;
Christopher Fauletd2236cd2020-04-06 18:29:14 +0200467 if (curpx->http_needed)
468 curpx->to_log |= LW_REQ;
Willy Tarreau2b718102021-04-21 07:32:39 +0200469 LIST_APPEND(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100470 return 1;
Dragan Dosen61302da2019-04-30 00:40:02 +0200471
472 error_free:
473 release_sample_expr(expr);
474 if (node) {
475 free(node->arg);
476 free(node);
477 }
478 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100479}
480
481/*
William Lallemand723b73a2012-02-08 16:37:49 +0100482 * Parse the log_format string and fill a linked list.
483 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200484 * You can set arguments using { } : %{many arguments}varname.
485 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100486 *
Ilya Shipitsinae40dbc2020-04-04 12:59:53 +0500487 * fmt: the string to parse
William Lallemand1d705562012-03-12 12:46:41 +0100488 * curproxy: the proxy affected
489 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100490 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100491 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100492 *
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100493 * The function returns 1 in success case, otherwise, it returns 0 and err is filled.
William Lallemand723b73a2012-02-08 16:37:49 +0100494 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100495int 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 +0100496{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100497 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100498 char *arg = NULL; /* start pointer for args */
499 char *var = NULL; /* start pointer for vars */
500 int arg_len = 0;
501 int var_len = 0;
502 int cformat; /* current token format */
503 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100504 struct logformat_node *tmplf, *back;
505
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100506 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100507 if (!str) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100508 memprintf(err, "out of memory error");
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100509 return 0;
510 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200511 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200512
William Lallemand723b73a2012-02-08 16:37:49 +0100513 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100514 list_for_each_entry_safe(tmplf, back, list_format, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200515 LIST_DELETE(&tmplf->list);
Dragan Dosen61302da2019-04-30 00:40:02 +0200516 release_sample_expr(tmplf->expr);
517 free(tmplf->arg);
William Lallemand723b73a2012-02-08 16:37:49 +0100518 free(tmplf);
519 }
520
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100521 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100522 pformat = cformat;
523
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100524 if (!*str)
525 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100526
Joseph Herlant85b40592018-11-15 12:10:04 -0800527 /* The principle of the two-step state machine below is to first detect a change, and
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100528 * second have all common paths processed at one place. The common paths are the ones
529 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
530 * We use the common LF_INIT state to dispatch to the different final states.
531 */
532 switch (pformat) {
533 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100534 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100535 arg_len = var_len = 0;
536 if (*str == '{') { // optional argument
537 cformat = LF_STARG;
538 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100539 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100540 else if (*str == '[') {
541 cformat = LF_STEXPR;
542 var = str + 1; // store expr in variable name
543 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100544 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100545 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100546 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100547 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100548 else if (*str == '%')
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500549 cformat = LF_TEXT; // convert this character to a literal (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100550 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100551 /* single '%' followed by blank or digit, send them both */
552 cformat = LF_TEXT;
553 pformat = LF_TEXT; /* finally we include the previous char as well */
554 sp = str - 1; /* send both the '%' and the current char */
Jim Freemana2278c82017-04-15 08:01:59 -0600555 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 +0100556 *str, (int)(str - backfmt), fmt);
Willy Tarreau51013e82019-12-11 12:05:39 +0100557 goto fail;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100558
559 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100560 else
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500561 cformat = LF_INIT; // handle other cases of literals
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100562 break;
563
564 case LF_STARG: // text immediately following '%{'
565 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100566 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100567 arg_len = str - arg;
568 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100569 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100570 break;
571
572 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100573 if (*str == '[') {
574 cformat = LF_STEXPR;
575 var = str + 1; // store expr in variable name
576 break;
577 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100578 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100579 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100580 var = str;
581 break;
582 }
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100583 memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg);
Willy Tarreau51013e82019-12-11 12:05:39 +0100584 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100585
Willy Tarreauc8368452012-12-21 00:09:23 +0100586 case LF_STEXPR: // text immediately following '%['
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100587 /* the whole sample expression is parsed at once,
588 * returning the pointer to the first character not
589 * part of the expression, which MUST be the trailing
590 * angle bracket.
591 */
592 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str))
593 goto fail;
594
595 if (*str == ']') {
596 // end of arg, go on with next state
597 cformat = pformat = LF_EDEXPR;
598 sp = str;
599 }
600 else {
601 char c = *str;
602 *str = 0;
Willy Tarreau90807112020-02-25 08:16:33 +0100603 if (isprint((unsigned char)c))
Willy Tarreaucd0d2ed2020-02-14 17:33:06 +0100604 memprintf(err, "expected ']' after '%s', but found '%c'", var, c);
605 else
606 memprintf(err, "missing ']' after '%s'", var);
Dragan Dosen2866acf2020-06-30 21:16:43 +0200607 goto fail;
Willy Tarreauc8368452012-12-21 00:09:23 +0100608 }
609 break;
610
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100611 case LF_VAR: // text part of a variable name
612 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100613 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100614 cformat = LF_INIT; // not variable name anymore
615 break;
616
Willy Tarreauc8368452012-12-21 00:09:23 +0100617 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100618 cformat = LF_INIT;
619 }
620
621 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
622 switch (*str) {
623 case '%': cformat = LF_STARTVAR; break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100624 case 0 : cformat = LF_END; break;
Dragan Dosen1e3b16f2020-06-23 18:16:44 +0200625 case ' ':
626 if (options & LOG_OPT_MERGE_SPACES) {
627 cformat = LF_SEPARATOR;
628 break;
629 }
630 /* fall through */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100631 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100632 }
633 }
634
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100635 if (cformat != pformat || pformat == LF_SEPARATOR) {
636 switch (pformat) {
637 case LF_VAR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100638 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100639 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100640 break;
641 case LF_TEXT:
642 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100643 if (!add_to_logformat_list(sp, str, pformat, list_format, err))
Willy Tarreau51013e82019-12-11 12:05:39 +0100644 goto fail;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100645 break;
646 }
647 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100648 }
649 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100650
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100651 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +0100652 memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%");
Willy Tarreau51013e82019-12-11 12:05:39 +0100653 goto fail;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100654 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100655 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100656
657 return 1;
Willy Tarreau51013e82019-12-11 12:05:39 +0100658 fail:
659 free(backfmt);
660 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100661}
662
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200663/*
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500664 * Parse the first range of indexes from a string made of a list of comma separated
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200665 * ranges of indexes. Note that an index may be considered as a particular range
666 * with a high limit to the low limit.
667 */
668int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err)
669{
670 char *end, *p;
671
672 *low = *high = 0;
673
674 p = *arg;
675 end = strchr(p, ',');
676 if (!end)
677 end = p + strlen(p);
678
679 *high = *low = read_uint((const char **)&p, end);
680 if (!*low || (p != end && *p != '-'))
681 goto err;
682
683 if (p == end)
684 goto done;
685
686 p++;
687 *high = read_uint((const char **)&p, end);
688 if (!*high || *high <= *low || p != end)
689 goto err;
690
691 done:
692 if (*end == ',')
693 end++;
694 *arg = end;
695 return 1;
696
697 err:
698 memprintf(err, "wrong sample range '%s'", *arg);
699 return 0;
700}
701
702/*
703 * Returns 1 if the range defined by <low> and <high> overlaps
704 * one of them in <rgs> array of ranges with <sz> the size of this
705 * array, 0 if not.
706 */
707int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz,
708 unsigned int low, unsigned int high, char **err)
709{
710 size_t i;
711
712 for (i = 0; i < sz; i++) {
713 if ((low >= rgs[i].low && low <= rgs[i].high) ||
714 (high >= rgs[i].low && high <= rgs[i].high)) {
715 memprintf(err, "ranges are overlapping");
716 return 1;
717 }
718 }
719
720 return 0;
721}
722
723int smp_log_range_cmp(const void *a, const void *b)
724{
725 const struct smp_log_range *rg_a = a;
726 const struct smp_log_range *rg_b = b;
727
728 if (rg_a->high < rg_b->low)
729 return -1;
730 else if (rg_a->low > rg_b->high)
731 return 1;
732
733 return 0;
734}
735
736/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200737 * Parse "log" keyword and update <logsrvs> list accordingly.
738 *
739 * When <do_del> is set, it means the "no log" line was parsed, so all log
740 * servers in <logsrvs> are released.
741 *
742 * Otherwise, we try to parse the "log" line. First of all, when the list is not
743 * the global one, we look for the parameter "global". If we find it,
744 * global.logsrvs is copied. Else we parse each arguments.
745 *
746 * The function returns 1 in success case, otherwise, it returns 0 and err is
747 * filled.
748 */
Emeric Brun9533a702021-04-02 10:13:43 +0200749int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200750{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100751 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200752 struct sockaddr_storage *sk;
Emeric Brun94aab062021-04-02 10:41:36 +0200753 struct protocol *proto;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200754 struct logsrv *logsrv = NULL;
755 int port1, port2;
756 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200757 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200758
759 /*
760 * "no log": delete previous herited or defined syslog
761 * servers.
762 */
763 if (do_del) {
764 struct logsrv *back;
765
766 if (*(args[1]) != 0) {
767 memprintf(err, "'no log' does not expect arguments");
768 goto error;
769 }
770
771 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200772 LIST_DELETE(&logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200773 free(logsrv);
774 }
775 return 1;
776 }
777
778 /*
779 * "log global": copy global.logrsvs linked list to the end of logsrvs
780 * list. But first, we check (logsrvs != global.logsrvs).
781 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100782 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200783 if (logsrvs == &global.logsrvs) {
784 memprintf(err, "'global' is not supported for a global syslog server");
785 goto error;
786 }
787 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200788 struct logsrv *node;
789
790 list_for_each_entry(node, logsrvs, list) {
791 if (node->ref == logsrv)
792 goto skip_logsrv;
793 }
794
795 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200796 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200797 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200798 LIST_INIT(&node->list);
Willy Tarreau2b718102021-04-21 07:32:39 +0200799 LIST_APPEND(logsrvs, &node->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200800 node->conf.file = strdup(file);
801 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200802
803 skip_logsrv:
804 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200805 }
806 return 1;
807 }
808
809 /*
810 * "log <address> ...: parse a syslog server line
811 */
812 if (*(args[1]) == 0 || *(args[2]) == 0) {
813 memprintf(err, "expects <address> and <facility> %s as arguments",
814 ((logsrvs == &global.logsrvs) ? "" : "or global"));
815 goto error;
816 }
817
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100818 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
819 if (strcmp(args[1], "stdout") == 0)
820 args[1] = "fd@1";
821 else if (strcmp(args[1], "stderr") == 0)
822 args[1] = "fd@2";
823
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200824 logsrv = calloc(1, sizeof(*logsrv));
825 if (!logsrv) {
826 memprintf(err, "out of memory");
827 goto error;
828 }
829
Emeric Brun9533a702021-04-02 10:13:43 +0200830 logsrv->conf.file = strdup(file);
831 logsrv->conf.line = linenum;
832
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200833 /* skip address for now, it will be parsed at the end */
834 cur_arg = 2;
835
836 /* just after the address, a length may be specified */
837 logsrv->maxlen = MAX_SYSLOG_LEN;
838 if (strcmp(args[cur_arg], "len") == 0) {
839 int len = atoi(args[cur_arg+1]);
840 if (len < 80 || len > 65535) {
841 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
842 args[cur_arg+1]);
843 goto error;
844 }
845 logsrv->maxlen = len;
846 cur_arg += 2;
847 }
848 if (logsrv->maxlen > global.max_syslog_len)
849 global.max_syslog_len = logsrv->maxlen;
850
851 /* after the length, a format may be specified */
852 if (strcmp(args[cur_arg], "format") == 0) {
853 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200854 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200855 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
856 goto error;
857 }
858 cur_arg += 2;
859 }
860
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200861 if (strcmp(args[cur_arg], "sample") == 0) {
862 unsigned low, high;
863 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200864 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
865
866 p = args[cur_arg+1];
867 smp_sz_str = strchr(p, ':');
868 if (!smp_sz_str) {
869 memprintf(err, "Missing sample size");
870 goto error;
871 }
872
873 *smp_sz_str++ = '\0';
874
875 end = p + strlen(p);
876
877 while (p != end) {
878 if (!get_logsrv_smp_range(&low, &high, &p, err))
879 goto error;
880
881 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
882 goto error;
883
884 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
885 if (!smp_rgs) {
886 memprintf(err, "out of memory error");
887 goto error;
888 }
889
890 smp_rgs[smp_rgs_sz].low = low;
891 smp_rgs[smp_rgs_sz].high = high;
892 smp_rgs[smp_rgs_sz].sz = high - low + 1;
893 smp_rgs[smp_rgs_sz].curr_idx = 0;
894 if (smp_rgs[smp_rgs_sz].high > smp_sz)
895 smp_sz = smp_rgs[smp_rgs_sz].high;
896 smp_rgs_sz++;
897 }
898
Tim Duesterhus21648002019-06-23 22:10:10 +0200899 if (smp_rgs == NULL) {
900 memprintf(err, "no sampling ranges given");
901 goto error;
902 }
903
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200904 beg = smp_sz_str;
905 end = beg + strlen(beg);
906 new_smp_sz = read_uint((const char **)&beg, end);
907 if (!new_smp_sz || beg != end) {
908 memprintf(err, "wrong sample size '%s' for sample range '%s'",
909 smp_sz_str, args[cur_arg+1]);
910 goto error;
911 }
912
913 if (new_smp_sz < smp_sz) {
914 memprintf(err, "sample size %zu should be greater or equal to "
915 "%zu the maximum of the high ranges limits",
916 new_smp_sz, smp_sz);
917 goto error;
918 }
919 smp_sz = new_smp_sz;
920
921 /* Let's order <smp_rgs> array. */
922 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
923
924 logsrv->lb.smp_rgs = smp_rgs;
925 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
926 logsrv->lb.smp_sz = smp_sz;
927
928 cur_arg += 2;
929 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200930 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200931 /* parse the facility */
932 logsrv->facility = get_log_facility(args[cur_arg]);
933 if (logsrv->facility < 0) {
934 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
935 goto error;
936 }
937 cur_arg++;
938
939 /* parse the max syslog level (default: debug) */
940 logsrv->level = 7;
941 if (*(args[cur_arg])) {
942 logsrv->level = get_log_level(args[cur_arg]);
943 if (logsrv->level < 0) {
944 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
945 goto error;
946 }
947 cur_arg++;
948 }
949
950 /* parse the limit syslog level (default: emerg) */
951 logsrv->minlvl = 0;
952 if (*(args[cur_arg])) {
953 logsrv->minlvl = get_log_level(args[cur_arg]);
954 if (logsrv->minlvl < 0) {
955 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
956 goto error;
957 }
958 cur_arg++;
959 }
960
961 /* Too many args */
962 if (*(args[cur_arg])) {
963 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
964 goto error;
965 }
966
967 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +0200968 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +0200969 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +0200970 logsrv->addr.ss_family = AF_UNSPEC;
971 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +0200972 logsrv->sink = NULL;
973 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +0200974 goto done;
975 }
976
Emeric Brun94aab062021-04-02 10:41:36 +0200977 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
978 err, NULL, NULL,
979 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM | PA_O_STREAM | PA_O_DEFAULT_DGRAM);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200980 if (!sk)
981 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +0200982
983 if (fd != -1)
984 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200985 logsrv->addr = *sk;
986
987 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200988 if (!port1)
989 set_host_port(&logsrv->addr, SYSLOG_PORT);
990 }
Emeric Brun9533a702021-04-02 10:13:43 +0200991
Emeric Brun26754902021-04-07 14:26:44 +0200992 if (proto && proto->ctrl_type == SOCK_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +0200993 static unsigned long ring_ids;
994
995 /* Implicit sink buffer will be
996 * initialized in post_check
997 */
998 logsrv->type = LOG_TARGET_BUFFER;
999 logsrv->sink = NULL;
1000 /* compute uniq name for the ring */
1001 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
1002 }
1003
Willy Tarreauc046d162019-08-30 15:24:59 +02001004 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001005 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001006 return 1;
1007
1008 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001009 free(smp_rgs);
Emeric Brun9533a702021-04-02 10:13:43 +02001010 if (logsrv) {
1011 free(logsrv->conf.file);
Willy Tarreaua0133fc2020-10-27 10:35:32 +01001012 free(logsrv->ring_name);
Emeric Brun9533a702021-04-02 10:13:43 +02001013 }
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001014 free(logsrv);
1015 return 0;
1016}
1017
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001018
1019/*
Emeric Brun54648852020-07-06 15:54:06 +02001020 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001021 */
Emeric Brun54648852020-07-06 15:54:06 +02001022enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001023{
Emeric Brun54648852020-07-06 15:54:06 +02001024 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001025
1026 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001027 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001028 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001029 format--;
1030
Emeric Brun54648852020-07-06 15:54:06 +02001031 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001032 return format;
1033}
1034
1035/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001036 * returns log level for <lev> or -1 if not found.
1037 */
1038int get_log_level(const char *lev)
1039{
1040 int level;
1041
1042 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001043 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001044 level--;
1045
1046 return level;
1047}
1048
Willy Tarreaubaaee002006-06-26 02:48:02 +02001049/*
1050 * returns log facility for <fac> or -1 if not found.
1051 */
1052int get_log_facility(const char *fac)
1053{
1054 int facility;
1055
1056 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001057 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001058 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001059
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060 return facility;
1061}
1062
William Lallemanda1cc3812012-02-08 16:38:44 +01001063/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001064 * Encode the string.
1065 *
1066 * When using the +E log format option, it will try to escape '"\]'
1067 * characters with '\' as prefix. The same prefix should not be used as
1068 * <escape>.
1069 */
1070static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001071 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001072 const char *string,
1073 struct logformat_node *node)
1074{
1075 if (node->options & LOG_OPT_ESC) {
1076 if (start < stop) {
1077 stop--; /* reserve one byte for the final '\0' */
1078 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001079 if (!ha_bit_test((unsigned char)(*string), map)) {
1080 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001081 *start++ = *string;
1082 else {
1083 if (start + 2 >= stop)
1084 break;
1085 *start++ = '\\';
1086 *start++ = *string;
1087 }
1088 }
1089 else {
1090 if (start + 3 >= stop)
1091 break;
1092 *start++ = escape;
1093 *start++ = hextab[(*string >> 4) & 15];
1094 *start++ = hextab[*string & 15];
1095 }
1096 string++;
1097 }
1098 *start = '\0';
1099 }
1100 }
1101 else {
1102 return encode_string(start, stop, escape, map, string);
1103 }
1104
1105 return start;
1106}
1107
1108/*
1109 * Encode the chunk.
1110 *
1111 * When using the +E log format option, it will try to escape '"\]'
1112 * characters with '\' as prefix. The same prefix should not be used as
1113 * <escape>.
1114 */
1115static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001116 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001117 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001118 struct logformat_node *node)
1119{
1120 char *str, *end;
1121
1122 if (node->options & LOG_OPT_ESC) {
1123 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001124 str = chunk->area;
1125 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001126
1127 stop--; /* reserve one byte for the final '\0' */
1128 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001129 if (!ha_bit_test((unsigned char)(*str), map)) {
1130 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001131 *start++ = *str;
1132 else {
1133 if (start + 2 >= stop)
1134 break;
1135 *start++ = '\\';
1136 *start++ = *str;
1137 }
1138 }
1139 else {
1140 if (start + 3 >= stop)
1141 break;
1142 *start++ = escape;
1143 *start++ = hextab[(*str >> 4) & 15];
1144 *start++ = hextab[*str & 15];
1145 }
1146 str++;
1147 }
1148 *start = '\0';
1149 }
1150 }
1151 else {
1152 return encode_chunk(start, stop, escape, map, chunk);
1153 }
1154
1155 return start;
1156}
1157
1158/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001159 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001160 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001161 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001162 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001163 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001164char *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 +01001165{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001166 if (size < 2)
1167 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001168
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001169 if (node->options & LOG_OPT_QUOTE) {
1170 *(dst++) = '"';
1171 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001172 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001173
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001174 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001175 if (++len > size)
1176 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001177 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001178 char *ret;
1179
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001180 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001181 if (ret == NULL || *ret != '\0')
1182 return NULL;
1183 len = ret - dst;
1184 }
1185 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001186 len = strlcpy2(dst, src, len);
1187 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001188
1189 size -= len;
1190 dst += len;
1191 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001192 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1193 if (size < 2)
1194 return NULL;
1195 *(dst++) = '-';
1196 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001197
1198 if (node->options & LOG_OPT_QUOTE) {
1199 if (size < 2)
1200 return NULL;
1201 *(dst++) = '"';
1202 }
1203
1204 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001205 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001206}
1207
Willy Tarreau26ffa852018-09-05 15:23:10 +02001208static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001209{
1210 return lf_text_len(dst, src, size, size, node);
1211}
1212
William Lallemand5f232402012-04-05 18:02:55 +02001213/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001214 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001215 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001216 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001217char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001218{
1219 char *ret = dst;
1220 int iret;
1221 char pn[INET6_ADDRSTRLEN];
1222
1223 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001224 unsigned char *addr = NULL;
1225 switch (sockaddr->sa_family) {
1226 case AF_INET:
1227 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1228 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1229 break;
1230 case AF_INET6:
1231 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1232 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1233 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1234 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1235 break;
1236 default:
1237 return NULL;
1238 }
William Lallemand5f232402012-04-05 18:02:55 +02001239 if (iret < 0 || iret > size)
1240 return NULL;
1241 ret += iret;
1242 } else {
1243 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1244 ret = lf_text(dst, pn, size, node);
1245 if (ret == NULL)
1246 return NULL;
1247 }
1248 return ret;
1249}
1250
1251/*
1252 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001253 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001254 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001255char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001256{
1257 char *ret = dst;
1258 int iret;
1259
1260 if (node->options & LOG_OPT_HEXA) {
1261 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1262 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1263 if (iret < 0 || iret > size)
1264 return NULL;
1265 ret += iret;
1266 } else {
1267 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1268 if (ret == NULL)
1269 return NULL;
1270 }
1271 return ret;
1272}
1273
Emeric Brun54648852020-07-06 15:54:06 +02001274
1275/*
1276 * This function sends the syslog message using a printf format string. It
1277 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001278 */
Emeric Brun54648852020-07-06 15:54:06 +02001279void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001280{
Emeric Brun54648852020-07-06 15:54:06 +02001281 va_list argp;
1282 int data_len;
1283
1284 if (level < 0 || format == NULL || logline == NULL)
1285 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001286
Emeric Brun54648852020-07-06 15:54:06 +02001287 va_start(argp, format);
1288 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1289 if (data_len < 0 || data_len > global.max_syslog_len)
1290 data_len = global.max_syslog_len;
1291 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001292
Emeric Brun54648852020-07-06 15:54:06 +02001293 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1294 logline, data_len, default_rfc5424_sd_log_format, 2);
1295}
1296/*
1297 * This function builds a log header of given format using given
1298 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1299 * to determine format based on given metadas. It is useful
1300 * for log-forwarding to be able to forward any format without
1301 * settings.
1302 * This function returns a struct ist array of elements of the header
1303 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001304 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001305 * elements.
1306 */
1307struct ist *build_log_header(enum log_fmt format, int level, int facility,
1308 struct ist *metadata, size_t *nbelem)
1309{
1310 static THREAD_LOCAL struct {
1311 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1312 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1313 time_t cur_legacy_time;
1314 char priority_buffer[6];
1315 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001316
Emeric Brun54648852020-07-06 15:54:06 +02001317 struct tm logtime;
1318 int len;
1319 int fac_level = 0;
1320 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001321
Emeric Brun54648852020-07-06 15:54:06 +02001322 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001323
Emeric Brun54648852020-07-06 15:54:06 +02001324
1325 if (format == LOG_FORMAT_UNSPEC) {
1326 format = LOG_FORMAT_RAW;
1327 if (metadata) {
1328 /* If a hostname is set, it appears we want to perform syslog
1329 * because only rfc5427 or rfc3164 support an hostname.
1330 */
1331 if (metadata[LOG_META_HOST].len) {
1332 /* If a rfc5424 compliant timestamp is used we consider
1333 * that output format is rfc5424, else legacy format
1334 * is used as specified default for local logs
1335 * in documentation.
1336 */
1337 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1338 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1339 format = LOG_FORMAT_RFC5424;
1340 else
1341 format = LOG_FORMAT_RFC3164;
1342 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001343 else if (metadata[LOG_META_TAG].len) {
1344 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001345 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001346 * in legacy format (analog to RFC3164 but
1347 * with stripped hostname).
1348 */
1349 format = LOG_FORMAT_LOCAL;
1350 }
Emeric Brun54648852020-07-06 15:54:06 +02001351 else if (metadata[LOG_META_PRIO].len) {
1352 /* the source seems a parsed message
1353 * offering a valid level/prio prefix
1354 * so we consider this format.
1355 */
1356 format = LOG_FORMAT_PRIO;
1357 }
1358 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001359 }
1360
Emeric Brun54648852020-07-06 15:54:06 +02001361 /* prepare priority, stored into 1 single elem */
1362 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001363 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001364 case LOG_FORMAT_RFC3164:
1365 case LOG_FORMAT_RFC5424:
1366 case LOG_FORMAT_PRIO:
1367 fac_level = facility << 3;
1368 /* further format ignore the facility */
1369 /* fall through */
1370 case LOG_FORMAT_TIMED:
1371 case LOG_FORMAT_SHORT:
1372 fac_level += level;
1373 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1374 do {
1375 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1376 fac_level /= 10;
1377 hdr_ctx.ist_vector[*nbelem].ptr--;
1378 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1379 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1380 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1381 break;
1382 case LOG_FORMAT_ISO:
1383 case LOG_FORMAT_RAW:
1384 break;
1385 case LOG_FORMAT_UNSPEC:
1386 case LOG_FORMATS:
1387 ABORT_NOW();
1388 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001389
William Lallemand2a4a44f2012-02-06 16:00:33 +01001390
Emeric Brun54648852020-07-06 15:54:06 +02001391 /* prepare timestamp, stored into a max of 4 elems */
1392 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001393 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001394 case LOG_FORMAT_RFC3164:
1395 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1396 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1397 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1398 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001399 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001400 break;
1401 }
1402 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1403 int month;
1404 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001405
Emeric Brun54648852020-07-06 15:54:06 +02001406 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001407
Emeric Brun54648852020-07-06 15:54:06 +02001408 /* compute month */
1409 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1410 if (month)
1411 month--;
1412 if (month <= 11) {
1413 /* builds log prefix ex: 'Jan 1 ' */
1414 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1415 "%s %c%c ", monthname[month],
1416 timestamp[8] != '0' ? timestamp[8] : ' ',
1417 timestamp[9]);
1418 /* we reused the timestamp_buffer, signal that it does not
1419 * contain local time anymore
1420 */
1421 hdr_ctx.cur_legacy_time = 0;
1422 if (len == 7) {
1423 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1424 /* adds 'HH:MM:SS' from iso time */
1425 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1426 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1427 /* we successfully reuse iso time, we can break */
1428 break;
1429 }
1430 }
1431 /* Failed to reuse isotime time, fallback to local legacy time */
1432 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001433
Emeric Brun54648852020-07-06 15:54:06 +02001434 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1435 /* re-builds timestamp from the current local time */
1436 get_localtime(time, &logtime);
1437
1438 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1439 "%s %2d %02d:%02d:%02d ",
1440 monthname[logtime.tm_mon],
1441 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1442 if (len != LOG_LEGACYTIME_LEN+1)
1443 hdr_ctx.cur_legacy_time = 0;
1444 else
1445 hdr_ctx.cur_legacy_time = time;
1446 }
1447 if (likely(hdr_ctx.cur_legacy_time))
1448 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1449 else
1450 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1451 break;
1452 case LOG_FORMAT_RFC5424:
1453 /* adds rfc5425 version prefix */
1454 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1455 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001456 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001457 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1458 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1459 break;
1460 }
1461 /* let continue as 'timed' and 'iso' format for usual timestamp */
1462 /* fall through */
1463 case LOG_FORMAT_TIMED:
1464 case LOG_FORMAT_ISO:
1465 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1466 * '1900:01:01T14:00:00+02:00'
1467 * '1900:01:01T10:00:00.123456-02:00'
1468 */
1469 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1470 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1471 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001472 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001473 break;
1474 }
1475 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1476 int month;
1477 char *timestamp = metadata[LOG_META_TIME].ptr;
1478
1479 for (month = 0; month < 12; month++)
1480 if (!memcmp(monthname[month], timestamp, 3))
1481 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001482
Emeric Brun54648852020-07-06 15:54:06 +02001483 if (month < 12) {
1484
1485 /* get local time to retrieve year */
1486 get_localtime(time, &logtime);
1487
1488 /* year seems changed since log */
1489 if (logtime.tm_mon < month)
1490 logtime.tm_year--;
1491
1492 /* builds rfc5424 prefix ex: '1900-01-01T' */
1493 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1494 "%4d-%02d-%c%cT",
1495 logtime.tm_year+1900, month+1,
1496 timestamp[4] != ' ' ? timestamp[4] : '0',
1497 timestamp[5]);
1498
1499 /* we reused the timestamp_buffer, signal that it does not
1500 * contain local time anymore
1501 */
1502 hdr_ctx.cur_legacy_time = 0;
1503 if (len == 11) {
1504 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1505 /* adds HH:MM:SS from legacy timestamp */
1506 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001507 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001508 /* according to rfc: -00:00 means we don't know the timezone */
1509 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1510 /* we successfully reuse legacy time, we can break */
1511 break;
1512 }
1513 }
1514 /* Failed to reuse legacy time, fallback to local iso time */
1515 }
1516 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1517 break;
1518 case LOG_FORMAT_PRIO:
1519 case LOG_FORMAT_SHORT:
1520 case LOG_FORMAT_RAW:
1521 break;
1522 case LOG_FORMAT_UNSPEC:
1523 case LOG_FORMATS:
1524 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001525 }
1526
Emeric Brun54648852020-07-06 15:54:06 +02001527 /* prepare other meta data, stored into a max of 10 elems */
1528 switch (format) {
1529 case LOG_FORMAT_RFC3164:
1530 if (metadata && metadata[LOG_META_HOST].len) {
1531 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1532 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1533 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001534 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001535 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001536 /* fall through */
1537 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001538 if (!metadata || !metadata[LOG_META_TAG].len)
1539 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001540
Emeric Brun54648852020-07-06 15:54:06 +02001541 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1542 if (metadata[LOG_META_PID].len) {
1543 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1544 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1545 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1546 }
1547 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1548 break;
1549 case LOG_FORMAT_RFC5424:
1550 if (metadata && metadata[LOG_META_HOST].len) {
1551 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1552 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1553 }
1554 else
1555 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001556
Emeric Brun54648852020-07-06 15:54:06 +02001557 if (metadata && metadata[LOG_META_TAG].len) {
1558 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1559 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1560 }
1561 else
1562 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001563
Emeric Brun54648852020-07-06 15:54:06 +02001564 if (metadata && metadata[LOG_META_PID].len) {
1565 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1566 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1567 }
1568 else
1569 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001570
Emeric Brun54648852020-07-06 15:54:06 +02001571 if (metadata && metadata[LOG_META_MSGID].len) {
1572 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1573 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1574 }
1575 else
1576 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001577
Emeric Brun54648852020-07-06 15:54:06 +02001578 if (metadata && metadata[LOG_META_STDATA].len) {
1579 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1580 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1581 }
1582 else
1583 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1584 break;
1585 case LOG_FORMAT_PRIO:
1586 case LOG_FORMAT_SHORT:
1587 case LOG_FORMAT_TIMED:
1588 case LOG_FORMAT_ISO:
1589 case LOG_FORMAT_RAW:
1590 break;
1591 case LOG_FORMAT_UNSPEC:
1592 case LOG_FORMATS:
1593 ABORT_NOW();
1594 }
1595
1596 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001597}
1598
1599/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001600 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001601 * The argument <metadata> MUST be an array of size
1602 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001603 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001604 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001605 */
Emeric Brun54648852020-07-06 15:54:06 +02001606static inline void __do_send_log(struct logsrv *logsrv, int nblogger, int level, int facility, struct ist *metadata, char *message, size_t size)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001607{
Emeric Brun54648852020-07-06 15:54:06 +02001608 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001609 static THREAD_LOCAL struct msghdr msghdr = {
1610 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001611 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001612 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001613 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1614 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001615 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001616 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001617 size_t nbelem;
1618 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001619
1620 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001621
Emeric Brunfa9d7802020-05-28 14:21:33 +02001622 /* historically some messages used to already contain the trailing LF
1623 * or Zero. Let's remove all trailing LF or Zero
1624 */
Emeric Brun54648852020-07-06 15:54:06 +02001625 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001626 size--;
1627
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001628 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001629 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001630 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001631 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001632 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1633 /* the socket's address is a file descriptor */
1634 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1635 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001636 else if (logsrv->addr.ss_family == AF_UNIX)
1637 plogfd = &logfdunix;
1638 else
1639 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001640
Willy Tarreauc046d162019-08-30 15:24:59 +02001641 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001642 /* socket not successfully initialized yet */
1643 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1644 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1645 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001646
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001647 if (!once) {
1648 once = 1; /* note: no need for atomic ops here */
1649 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1650 nblogger, strerror(errno), errno);
1651 }
1652 return;
1653 } else {
1654 /* we don't want to receive anything on this socket */
1655 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1656 /* does nothing under Linux, maybe needed for others */
1657 shutdown(*plogfd, SHUT_RD);
1658 fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
1659 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001660 }
1661
Emeric Brun54648852020-07-06 15:54:06 +02001662 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1663 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001664 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001665 struct ist msg;
1666
1667 msg = ist2(message, size);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01001668 msg = isttrim(msg, logsrv->maxlen);
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001669
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001670 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1671 }
1672 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1673 struct ist msg;
1674
1675 msg = ist2(message, size);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01001676 msg = isttrim(msg, logsrv->maxlen);
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001677
1678 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001679 }
1680 else {
Emeric Brun54648852020-07-06 15:54:06 +02001681 int i = 0;
1682 int totlen = logsrv->maxlen;
1683
1684 for (i = 0 ; i < nbelem ; i++ ) {
1685 iovec[i].iov_base = msg_header[i].ptr;
1686 iovec[i].iov_len = msg_header[i].len;
1687 if (totlen <= iovec[i].iov_len) {
1688 iovec[i].iov_len = totlen;
1689 totlen = 0;
1690 break;
1691 }
1692 totlen -= iovec[i].iov_len;
1693 }
1694 if (totlen) {
1695 iovec[i].iov_base = message;
1696 iovec[i].iov_len = size;
1697 if (totlen <= iovec[i].iov_len)
1698 iovec[i].iov_len = totlen;
1699 i++;
1700 }
1701 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1702 iovec[i].iov_len = 1;
1703 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001704
Emeric Brun54648852020-07-06 15:54:06 +02001705 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001706 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1707 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001708
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001709 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1710 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001711
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001712 if (sent < 0) {
1713 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001714
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001715 if (errno == EAGAIN)
Willy Tarreau4781b152021-04-06 13:53:36 +02001716 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001717 else if (!once) {
1718 once = 1; /* note: no need for atomic ops here */
1719 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1720 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001721 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001722 }
1723}
Dragan Dosen59cee972015-09-19 22:09:02 +02001724
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001725/*
1726 * This function sends a syslog message.
1727 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001728 * The argument <metadata> MUST be an array of size
1729 * LOG_META_FIELDS*sizeof(struct ist) containing
1730 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001731 */
Emeric Brun54648852020-07-06 15:54:06 +02001732void process_send_log(struct list *logsrvs, int level, int facility,
1733 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001734{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001735 struct logsrv *logsrv;
1736 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001737
1738 /* Send log messages to syslog server. */
1739 nblogger = 0;
1740 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001741 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001742
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001743 /* we can filter the level of the messages that are sent to each logger */
1744 if (level > logsrv->level)
1745 continue;
1746
Frédéric Lécailled803e472019-04-25 07:42:09 +02001747 if (logsrv->lb.smp_rgs) {
1748 struct smp_log_range *curr_rg;
1749
1750 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1751 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1752 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1753 if (in_range) {
1754 /* Let's consume this range. */
1755 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1756 if (!curr_rg->curr_idx) {
1757 /* If consumed, let's select the next range. */
1758 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1759 }
1760 }
1761 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1762 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1763 }
1764 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001765 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1766 (facility == -1) ? logsrv->facility : facility,
1767 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001768 }
1769}
1770
Emeric Brun54648852020-07-06 15:54:06 +02001771/*
1772 * This function sends a syslog message.
1773 * It doesn't care about errors nor does it report them.
1774 * The arguments <sd> and <sd_size> are used for the structured-data part
1775 * in RFC5424 formatted syslog messages.
1776 */
1777void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1778 char *message, size_t size, char *sd, size_t sd_size)
1779{
1780 static THREAD_LOCAL pid_t curr_pid;
1781 static THREAD_LOCAL char pidstr[16];
1782 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1783
1784 if (logsrvs == NULL) {
1785 if (!LIST_ISEMPTY(&global.logsrvs)) {
1786 logsrvs = &global.logsrvs;
1787 }
1788 }
1789 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1790 return;
1791
1792 if (!metadata[LOG_META_HOST].len) {
1793 if (global.log_send_hostname)
1794 metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname));
Emeric Brun54648852020-07-06 15:54:06 +02001795 }
1796
1797 if (!tagb || !tagb->area)
1798 tagb = &global.log_tag;
1799
1800 if (tagb)
1801 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1802
1803 if (unlikely(curr_pid != getpid()))
1804 metadata[LOG_META_PID].len = 0;
1805
1806 if (!metadata[LOG_META_PID].len) {
1807 curr_pid = getpid();
1808 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1809 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1810 }
1811
1812 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1813
1814 /* Remove trailing space of structured data */
1815 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1816 metadata[LOG_META_STDATA].len--;
1817
1818 return process_send_log(logsrvs, level, -1, metadata, message, size);
1819}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001820
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001821const 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 +01001822const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1823 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1824 Set-cookie Updated, unknown, unknown */
1825
William Lallemand1d705562012-03-12 12:46:41 +01001826/*
1827 * try to write a character if there is enough space, or goto out
1828 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001829#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001830 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001831 *(tmplog++) = (x); \
1832 } else { \
1833 goto out; \
1834 } \
1835 } while(0)
1836
Dragan Dosen835b9212016-02-12 13:23:03 +01001837
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001838/* Initializes some log data at boot */
1839static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001840{
1841 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001842 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001843
1844 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1845 * inside PARAM-VALUE should be escaped with '\' as prefix.
1846 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1847 * details.
1848 */
1849 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1850
1851 tmp = "\"\\]";
1852 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001853 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001854 tmp++;
1855 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001856
1857 /* initialize the log header encoding map : '{|}"#' should be encoded with
1858 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1859 * URL encoding only requires '"', '#' to be encoded as well as non-
1860 * printable characters above.
1861 */
1862 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1863 memset(url_encode_map, 0, sizeof(url_encode_map));
1864 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001865 ha_bit_set(i, hdr_encode_map);
1866 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001867 }
1868 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001869 ha_bit_set(i, hdr_encode_map);
1870 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001871 }
1872
1873 tmp = "\"#{|}";
1874 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001875 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001876 tmp++;
1877 }
1878
1879 tmp = "\"#";
1880 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001881 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001882 tmp++;
1883 }
1884
1885 /* initialize the http header encoding map. The draft httpbis define the
1886 * header content as:
1887 *
1888 * HTTP-message = start-line
1889 * *( header-field CRLF )
1890 * CRLF
1891 * [ message-body ]
1892 * header-field = field-name ":" OWS field-value OWS
1893 * field-value = *( field-content / obs-fold )
1894 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1895 * obs-fold = CRLF 1*( SP / HTAB )
1896 * field-vchar = VCHAR / obs-text
1897 * VCHAR = %x21-7E
1898 * obs-text = %x80-FF
1899 *
1900 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1901 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08001902 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02001903 */
1904 memset(http_encode_map, 0, sizeof(http_encode_map));
1905 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001906 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001907 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001908 ha_bit_set(i, http_encode_map);
1909 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001910}
William Lallemand1d705562012-03-12 12:46:41 +01001911
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001912INITCALL0(STG_PREPARE, init_log);
1913
Christopher Faulet0132d062017-07-26 15:33:35 +02001914/* Initialize log buffers used for syslog messages */
1915int init_log_buffers()
1916{
Christopher Faulet0132d062017-07-26 15:33:35 +02001917 logline = my_realloc2(logline, global.max_syslog_len + 1);
1918 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02001919 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02001920 return 0;
1921 return 1;
1922}
1923
1924/* Deinitialize log buffers used for syslog messages */
1925void deinit_log_buffers()
1926{
Christopher Faulet0132d062017-07-26 15:33:35 +02001927 free(logline);
1928 free(logline_rfc5424);
Christopher Faulet0132d062017-07-26 15:33:35 +02001929 logline = NULL;
1930 logline_rfc5424 = NULL;
1931}
1932
Willy Tarreaudf974472012-12-28 02:44:01 +01001933/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1934 * <maxsize> characters. Returns the size of the output string in characters,
1935 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001936 * is not zero. It requires a valid session and optionally a stream. If the
1937 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01001938 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001939int 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 +02001940{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001941 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001942 struct proxy *be;
1943 struct http_txn *txn;
1944 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001945 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001946 unsigned int s_flags;
1947 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02001948 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001949 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001950 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001951 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001952 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001953 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001954 int t_request;
1955 int hdr;
1956 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001957 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001958 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001959 char *ret;
1960 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001961 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001962 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001963 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001964 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00001965 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001966 struct http_uri_parser parser;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001967
William Lallemandbddd4fd2012-02-27 11:23:10 +01001968 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001969
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001970 if (likely(s)) {
1971 be = s->be;
1972 txn = s->txn;
1973 be_conn = cs_conn(objt_cs(s->si[1].end));
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001974 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001975 s_flags = s->flags;
1976 uniq_id = s->uniq_id;
1977 logs = &s->logs;
1978 } else {
1979 /* we have no stream so we first need to initialize a few
1980 * things that are needed later. We do increment the request
1981 * ID so that it's uniquely assigned to this request just as
1982 * if the request had reached the point of being processed.
1983 * A request error is reported as it's the only element we have
1984 * here and which justifies emitting such a log.
1985 */
Christopher Fauletfd818482021-04-14 14:01:41 +02001986 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001987 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001988 fe_conn = objt_conn(sess->origin);
Christopher Fauletfd818482021-04-14 14:01:41 +02001989 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? cs_conn(__objt_check(sess->origin)->cs) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001990 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001991 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02001992 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001993
1994 /* prepare a valid log structure */
1995 tmp_strm_log.tv_accept = sess->tv_accept;
1996 tmp_strm_log.accept_date = sess->accept_date;
1997 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02001998 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001999 tv_zero(&tmp_strm_log.tv_request);
2000 tmp_strm_log.t_queue = -1;
2001 tmp_strm_log.t_connect = -1;
2002 tmp_strm_log.t_data = -1;
2003 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2004 tmp_strm_log.bytes_in = 0;
2005 tmp_strm_log.bytes_out = 0;
2006 tmp_strm_log.prx_queue_pos = 0;
2007 tmp_strm_log.srv_queue_pos = 0;
2008
2009 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002010
2011 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002012 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, &status);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002013
2014 switch (es) {
2015 case MUX_ES_SUCCESS:
2016 break;
2017 case MUX_ES_INVALID_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002018 status = (status ? status : 400);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002019 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2020 s_flags = SF_ERR_CLICL | SF_FINST_R;
2021 else
2022 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2023 break;
2024 case MUX_ES_TOUT_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002025 status = (status ? status : 408);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002026 s_flags = SF_ERR_CLITO | SF_FINST_R;
2027 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002028 case MUX_ES_NOTIMPL_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002029 status = (status ? status : 501);
Christopher Faulet142dd332020-12-07 11:24:37 +01002030 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2031 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002032 case MUX_ES_INTERNAL_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002033 status = (status ? status : 500);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002034 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2035 break;
2036 default:
2037 break;
2038 }
2039 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002040 }
2041
William Lallemandbddd4fd2012-02-27 11:23:10 +01002042 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002043 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2044 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002045
William Lallemand1d705562012-03-12 12:46:41 +01002046 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002047
William Lallemandbddd4fd2012-02-27 11:23:10 +01002048 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002049 if (LIST_ISEMPTY(list_format))
2050 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002051
William Lallemand1d705562012-03-12 12:46:41 +01002052 list_for_each_entry(tmp, list_format, list) {
Christopher Faulet52b28d22021-10-27 11:58:05 +02002053#ifdef USE_OPENSSL
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002054 struct connection *conn;
Christopher Faulet52b28d22021-10-27 11:58:05 +02002055#endif
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002056 const struct sockaddr_storage *addr;
Willy Tarreau4f653562012-10-12 19:48:16 +02002057 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002058 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002059 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002060
Willy Tarreauc8368452012-12-21 00:09:23 +01002061 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002062 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002063 if (!last_isspace) {
2064 LOGCHAR(' ');
2065 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002066 }
2067 break;
2068
William Lallemand1d705562012-03-12 12:46:41 +01002069 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002070 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002071 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002072 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002073 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002074 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002075 last_isspace = 0;
2076 break;
2077
Willy Tarreauc8368452012-12-21 00:09:23 +01002078 case LOG_FMT_EXPR: // sample expression, may be request or response
2079 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002080 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002081 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Willy Tarreau20b7a0f2021-09-03 08:53:29 +02002082
Christopher Faulet5f940702020-04-06 10:40:02 +02002083 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002084 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Willy Tarreau20b7a0f2021-09-03 08:53:29 +02002085
2086 if (!key && !(tmp->options & (LOG_OPT_REQ_CAP|LOG_OPT_RES_CAP))) // cfg, cli
2087 key = sample_fetch_as_type(be, sess, s, SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
2088
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002089 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002090 ret = lf_encode_chunk(tmplog, dst + maxsize,
2091 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002092 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002093 ret = lf_text_len(tmplog,
2094 key ? key->data.u.str.area : NULL,
2095 key ? key->data.u.str.data : 0,
2096 dst + maxsize - tmplog,
2097 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002098 if (ret == 0)
2099 goto out;
2100 tmplog = ret;
2101 last_isspace = 0;
2102 break;
2103
Willy Tarreau2beef582012-12-20 17:22:52 +01002104 case LOG_FMT_CLIENTIP: // %ci
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002105 addr = (s ? si_src(&s->si[0]) : sess_src(sess));
2106 if (addr)
2107 ret = lf_ip(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002108 else
2109 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002110
William Lallemand1d705562012-03-12 12:46:41 +01002111 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002112 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002113 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002114 last_isspace = 0;
2115 break;
2116
Willy Tarreau2beef582012-12-20 17:22:52 +01002117 case LOG_FMT_CLIENTPORT: // %cp
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002118 addr = (s ? si_src(&s->si[0]) : sess_src(sess));
2119 if (addr) {
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002120 /* sess->listener is always defined when the session's owner is an inbound connections */
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002121 if (addr->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002122 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002123 else
2124 ret = lf_port(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002125 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002126 else
2127 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2128
William Lallemand5f232402012-04-05 18:02:55 +02002129 if (ret == NULL)
2130 goto out;
2131 tmplog = ret;
2132 last_isspace = 0;
2133 break;
2134
Willy Tarreau2beef582012-12-20 17:22:52 +01002135 case LOG_FMT_FRONTENDIP: // %fi
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002136 addr = (s ? si_dst(&s->si[0]) : sess_dst(sess));
2137 if (addr)
2138 ret = lf_ip(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002139 else
2140 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2141
William Lallemand1d705562012-03-12 12:46:41 +01002142 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002143 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002144 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002145 last_isspace = 0;
2146 break;
2147
Willy Tarreau2beef582012-12-20 17:22:52 +01002148 case LOG_FMT_FRONTENDPORT: // %fp
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002149 addr = (s ? si_dst(&s->si[0]) : sess_dst(sess));
2150 if (addr) {
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002151 /* sess->listener is always defined when the session's owner is an inbound connections */
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002152 if (addr->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002153 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002154 else
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002155 ret = lf_port(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002156 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002157 else
2158 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2159
William Lallemand5f232402012-04-05 18:02:55 +02002160 if (ret == NULL)
2161 goto out;
2162 tmplog = ret;
2163 last_isspace = 0;
2164 break;
2165
Willy Tarreau2beef582012-12-20 17:22:52 +01002166 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau8fa99842019-07-17 11:47:11 +02002167 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002168 ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002169 else
2170 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2171
William Lallemand1d705562012-03-12 12:46:41 +01002172 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002173 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002174 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002175 last_isspace = 0;
2176 break;
2177
Willy Tarreau2beef582012-12-20 17:22:52 +01002178 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002179 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002180 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002181 else
2182 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2183
William Lallemand5f232402012-04-05 18:02:55 +02002184 if (ret == NULL)
2185 goto out;
2186 tmplog = ret;
2187 last_isspace = 0;
2188 break;
2189
Willy Tarreau2beef582012-12-20 17:22:52 +01002190 case LOG_FMT_SERVERIP: // %si
Willy Tarreau8fa99842019-07-17 11:47:11 +02002191 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002192 ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002193 else
2194 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2195
William Lallemand5f232402012-04-05 18:02:55 +02002196 if (ret == NULL)
2197 goto out;
2198 tmplog = ret;
2199 last_isspace = 0;
2200 break;
2201
Willy Tarreau2beef582012-12-20 17:22:52 +01002202 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002203 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002204 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002205 else
2206 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2207
William Lallemand1d705562012-03-12 12:46:41 +01002208 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002209 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002210 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002211 last_isspace = 0;
2212 break;
2213
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002214 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002215 get_localtime(logs->accept_date.tv_sec, &tm);
2216 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002217 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002218 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002219 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002220 last_isspace = 0;
2221 break;
2222
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002223 case LOG_FMT_tr: // %tr = start of request date
2224 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002225 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 +02002226 get_localtime(tv.tv_sec, &tm);
2227 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2228 if (ret == NULL)
2229 goto out;
2230 tmplog = ret;
2231 last_isspace = 0;
2232 break;
2233
2234 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002235 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002236 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002237 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002238 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002239 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002240 last_isspace = 0;
2241 break;
2242
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002243 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002244 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 +02002245 get_gmtime(tv.tv_sec, &tm);
2246 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2247 if (ret == NULL)
2248 goto out;
2249 tmplog = ret;
2250 last_isspace = 0;
2251 break;
2252
2253 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002254 get_localtime(logs->accept_date.tv_sec, &tm);
2255 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002256 if (ret == NULL)
2257 goto out;
2258 tmplog = ret;
2259 last_isspace = 0;
2260 break;
2261
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002262 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002263 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 +02002264 get_localtime(tv.tv_sec, &tm);
2265 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2266 if (ret == NULL)
2267 goto out;
2268 tmplog = ret;
2269 last_isspace = 0;
2270 break;
2271
William Lallemand5f232402012-04-05 18:02:55 +02002272 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002273 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002274 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002275 if (iret < 0 || iret > dst + maxsize - tmplog)
2276 goto out;
2277 last_isspace = 0;
2278 tmplog += iret;
2279 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002280 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002281 if (ret == NULL)
2282 goto out;
2283 tmplog = ret;
2284 last_isspace = 0;
2285 }
2286 break;
2287
William Lallemand1d705562012-03-12 12:46:41 +01002288 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002289 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002290 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002291 if (iret < 0 || iret > dst + maxsize - tmplog)
2292 goto out;
2293 last_isspace = 0;
2294 tmplog += iret;
2295 } else {
2296 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002297 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002298 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002299 tmplog, 4);
2300 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002301 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002302 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002303 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002304 }
2305 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002306
William Lallemand1d705562012-03-12 12:46:41 +01002307 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002308 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002309 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002310 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002311 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002312 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002313 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002314 break;
2315
Willy Tarreau773d65f2012-10-12 14:56:11 +02002316 case LOG_FMT_FRONTEND_XPRT: // %ft
2317 src = fe->id;
2318 if (tmp->options & LOG_OPT_QUOTE)
2319 LOGCHAR('"');
2320 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2321 if (iret == 0)
2322 goto out;
2323 tmplog += iret;
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002324
2325 /* sess->listener may be undefined if the session's owner is a health-check */
Christopher Fauletfd818482021-04-14 14:01:41 +02002326 if (sess->listener && sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL))
Willy Tarreau773d65f2012-10-12 14:56:11 +02002327 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002328 if (tmp->options & LOG_OPT_QUOTE)
2329 LOGCHAR('"');
2330 last_isspace = 0;
2331 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002332#ifdef USE_OPENSSL
2333 case LOG_FMT_SSL_CIPHER: // %sslc
2334 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002335 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002336 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002337 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002338 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002339 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2340 if (ret == NULL)
2341 goto out;
2342 tmplog = ret;
2343 last_isspace = 0;
2344 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002345
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002346 case LOG_FMT_SSL_VERSION: // %sslv
2347 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002348 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002349 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002350 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002351 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002352 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2353 if (ret == NULL)
2354 goto out;
2355 tmplog = ret;
2356 last_isspace = 0;
2357 break;
2358#endif
William Lallemand1d705562012-03-12 12:46:41 +01002359 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002360 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002361 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002362 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002363 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002364 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002365 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002366 break;
2367
William Lallemand1d705562012-03-12 12:46:41 +01002368 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002369 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002370 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002371 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002372 break;
2373 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002374 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002375 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002376 case OBJ_TYPE_CHECK:
2377 src = (__objt_check(sess->origin)->server
2378 ? __objt_check(sess->origin)->server->id
2379 : "<NOSRV>");
2380 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002381 default:
2382 src = "<NOSRV>";
2383 break;
2384 }
William Lallemand5f232402012-04-05 18:02:55 +02002385 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
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_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002393 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002394 if (ret == NULL)
2395 goto out;
2396 tmplog = ret;
2397 last_isspace = 0;
2398 break;
2399
2400 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002401 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002402 if (ret == NULL)
2403 goto out;
2404 tmplog = ret;
2405 last_isspace = 0;
2406 break;
2407
2408 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002409 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002410 tmplog, dst + maxsize - tmplog);
2411 if (ret == NULL)
2412 goto out;
2413 tmplog = ret;
2414 last_isspace = 0;
2415 break;
2416
2417 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002418 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002419 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002420 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002421 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002422 last_isspace = 0;
2423 break;
2424
William Lallemand1d705562012-03-12 12:46:41 +01002425 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002426 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002427 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002428 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002429 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002430 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002431 last_isspace = 0;
2432 break;
2433
William Lallemand1d705562012-03-12 12:46:41 +01002434 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002435 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002436 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
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002443 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002444 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002445 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002446 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002447 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002448 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002449 last_isspace = 0;
2450 break;
2451
Willy Tarreau27b639d2016-05-17 17:55:27 +02002452 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002453 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002454 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002455 tmplog, dst + maxsize - tmplog);
2456 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002457 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002458 tmplog, dst + maxsize - tmplog);
2459 if (ret == NULL)
2460 goto out;
2461 tmplog = ret;
2462 last_isspace = 0;
2463 break;
2464
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002465 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2466 if (!(fe->to_log & LW_BYTES))
2467 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002468 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 +02002469 tmplog, dst + maxsize - tmplog);
2470 if (ret == NULL)
2471 goto out;
2472 tmplog = ret;
2473 last_isspace = 0;
2474 break;
2475
2476 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002477 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002478 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002479 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002480 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002481 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002482 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002483 last_isspace = 0;
2484 break;
2485
Damien Claisse57c8eb92020-04-28 12:09:19 +00002486 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2487 if (!(fe->to_log & LW_BYTES))
2488 LOGCHAR('+');
2489 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2490 tmplog, dst + maxsize - tmplog);
2491 if (ret == NULL)
2492 goto out;
2493 tmplog = ret;
2494 last_isspace = 0;
2495 break;
2496
Willy Tarreau2beef582012-12-20 17:22:52 +01002497 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002498 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002499 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002500 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002501 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002502 last_isspace = 0;
2503 break;
2504
William Lallemand1d705562012-03-12 12:46:41 +01002505 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002506 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002507 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002508 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002509 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002510 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002511 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002512 last_isspace = 0;
2513 break;
2514
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002515 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002516 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002517 if (ret == NULL)
2518 goto out;
2519 tmplog = ret;
2520 last_isspace = 0;
2521 break;
2522
Willy Tarreau2beef582012-12-20 17:22:52 +01002523 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002524 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002525 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002526 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002527 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002528 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002529 last_isspace = 0;
2530 break;
2531
Willy Tarreau2beef582012-12-20 17:22:52 +01002532 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002533 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002534 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002535 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002536 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002537 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002538 last_isspace = 0;
2539 break;
2540
William Lallemand1d705562012-03-12 12:46:41 +01002541 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002542 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2543 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002544 *tmplog = '\0';
2545 last_isspace = 0;
2546 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002547
William Lallemand1d705562012-03-12 12:46:41 +01002548 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002549 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2550 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002551 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2552 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 +01002553 last_isspace = 0;
2554 break;
2555
William Lallemand1d705562012-03-12 12:46:41 +01002556 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002557 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002558 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002559 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002560 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002561 last_isspace = 0;
2562 break;
2563
William Lallemand1d705562012-03-12 12:46:41 +01002564 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002565 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002566 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002567 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002568 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002569 last_isspace = 0;
2570 break;
2571
William Lallemand1d705562012-03-12 12:46:41 +01002572 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002573 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002574 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002575 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002576 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002577 last_isspace = 0;
2578 break;
2579
William Lallemand1d705562012-03-12 12:46:41 +01002580 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002581 switch (obj_type(s ? s->target : sess->origin)) {
2582 case OBJ_TYPE_SERVER:
2583 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2584 tmplog, dst + maxsize - tmplog);
2585 break;
2586 case OBJ_TYPE_CHECK:
2587 ret = ultoa_o(__objt_check(sess->origin)->server
2588 ? __objt_check(sess->origin)->server->cur_sess
2589 : 0, tmplog, dst + maxsize - tmplog);
2590 break;
2591 default:
2592 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2593 break;
2594 }
2595
William Lallemand1d705562012-03-12 12:46:41 +01002596 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002597 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002598 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002599 last_isspace = 0;
2600 break;
2601
William Lallemand1d705562012-03-12 12:46:41 +01002602 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002603 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002604 LOGCHAR('+');
Christopher Faulet1d26f222021-04-16 11:24:20 +02002605 ret = ltoa_o(((s && s->si[1].conn_retries > 0)
2606 ? (be->conn_retries - s->si[1].conn_retries)
2607 : ((s && s->si[1].state != SI_ST_INI) ? be->conn_retries : 0)),
2608 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002609 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002610 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002611 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002612 last_isspace = 0;
2613 break;
2614
William Lallemand1d705562012-03-12 12:46:41 +01002615 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002616 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002617 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002618 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002619 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002620 last_isspace = 0;
2621 break;
2622
William Lallemand1d705562012-03-12 12:46:41 +01002623 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002624 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002625 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002626 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002627 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002628 last_isspace = 0;
2629 break;
2630
William Lallemand1d705562012-03-12 12:46:41 +01002631 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002632 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002633 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002634 if (tmp->options & LOG_OPT_QUOTE)
2635 LOGCHAR('"');
2636 LOGCHAR('{');
2637 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2638 if (hdr)
2639 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002640 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002641 ret = lf_encode_string(tmplog, dst + maxsize,
2642 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002643 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002644 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002645 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002646 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002647 }
2648 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002649 if (tmp->options & LOG_OPT_QUOTE)
2650 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002651 last_isspace = 0;
2652 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002653 break;
2654
William Lallemand1d705562012-03-12 12:46:41 +01002655 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002656 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002657 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002658 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2659 if (hdr > 0)
2660 LOGCHAR(' ');
2661 if (tmp->options & LOG_OPT_QUOTE)
2662 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002663 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002664 ret = lf_encode_string(tmplog, dst + maxsize,
2665 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002666 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002667 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002668 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002669 } else if (!(tmp->options & LOG_OPT_QUOTE))
2670 LOGCHAR('-');
2671 if (tmp->options & LOG_OPT_QUOTE)
2672 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002673 last_isspace = 0;
2674 }
2675 }
2676 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002677
William Lallemand1d705562012-03-12 12:46:41 +01002678
2679 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002680 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002681 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002682 if (tmp->options & LOG_OPT_QUOTE)
2683 LOGCHAR('"');
2684 LOGCHAR('{');
2685 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2686 if (hdr)
2687 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002688 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002689 ret = lf_encode_string(tmplog, dst + maxsize,
2690 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002691 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002692 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002693 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002694 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002695 }
2696 LOGCHAR('}');
2697 last_isspace = 0;
2698 if (tmp->options & LOG_OPT_QUOTE)
2699 LOGCHAR('"');
2700 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002701 break;
2702
William Lallemand1d705562012-03-12 12:46:41 +01002703 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002704 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002705 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002706 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2707 if (hdr > 0)
2708 LOGCHAR(' ');
2709 if (tmp->options & LOG_OPT_QUOTE)
2710 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002711 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002712 ret = lf_encode_string(tmplog, dst + maxsize,
2713 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002714 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002715 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002716 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002717 } else if (!(tmp->options & LOG_OPT_QUOTE))
2718 LOGCHAR('-');
2719 if (tmp->options & LOG_OPT_QUOTE)
2720 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002721 last_isspace = 0;
2722 }
2723 }
2724 break;
2725
William Lallemand1d705562012-03-12 12:46:41 +01002726 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002727 /* Request */
2728 if (tmp->options & LOG_OPT_QUOTE)
2729 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002730 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002731 ret = lf_encode_string(tmplog, dst + maxsize,
2732 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002733 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002734 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002735 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002736 if (tmp->options & LOG_OPT_QUOTE)
2737 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002738 last_isspace = 0;
2739 break;
William Lallemand5f232402012-04-05 18:02:55 +02002740
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002741 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002742 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002743
Willy Tarreaub7636d12015-06-17 19:58:02 +02002744 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002745 LOGCHAR('"');
2746
2747 end = uri + strlen(uri);
2748 // look for the first whitespace character
2749 while (uri < end && !HTTP_IS_SPHT(*uri))
2750 uri++;
2751
2752 // keep advancing past multiple spaces
2753 while (uri < end && HTTP_IS_SPHT(*uri)) {
2754 uri++; nspaces++;
2755 }
2756
2757 // look for first space or question mark after url
2758 spc = uri;
2759 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2760 spc++;
2761
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002762 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002763 chunk.area = "<BADREQ>";
2764 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002765 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002766 chunk.area = uri;
2767 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002768 }
2769
Dragan Dosen835b9212016-02-12 13:23:03 +01002770 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002771 if (ret == NULL || *ret != '\0')
2772 goto out;
2773
2774 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002775 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002776 LOGCHAR('"');
2777
2778 last_isspace = 0;
2779 break;
2780
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002781 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2782 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2783
2784 if (tmp->options & LOG_OPT_QUOTE)
2785 LOGCHAR('"');
2786
2787 end = uri + strlen(uri);
2788
2789 // look for the first whitespace character
2790 while (uri < end && !HTTP_IS_SPHT(*uri))
2791 uri++;
2792
2793 // keep advancing past multiple spaces
2794 while (uri < end && HTTP_IS_SPHT(*uri)) {
2795 uri++; nspaces++;
2796 }
2797
2798 // look for first space after url
2799 spc = uri;
2800 while (spc < end && !HTTP_IS_SPHT(*spc))
2801 spc++;
2802
Tim Duesterhus92c696e2021-02-28 16:11:36 +01002803 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002804
2805 // extract relative path without query params from url
Amaury Denoyellec453f952021-07-06 11:40:12 +02002806 parser = http_uri_parser_init(path);
2807 path = iststop(http_parse_path(&parser), '?');
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002808 if (!txn || !txn->uri || nspaces == 0) {
2809 chunk.area = "<BADREQ>";
2810 chunk.data = strlen("<BADREQ>");
2811 } else {
2812 chunk.area = path.ptr;
2813 chunk.data = path.len;
2814 }
2815
2816 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2817 if (ret == NULL || *ret != '\0')
2818 goto out;
2819
2820 tmplog = ret;
2821 if (tmp->options & LOG_OPT_QUOTE)
2822 LOGCHAR('"');
2823
2824 last_isspace = 0;
2825 break;
2826
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002827 case LOG_FMT_HTTP_QUERY: // %HQ
2828 if (tmp->options & LOG_OPT_QUOTE)
2829 LOGCHAR('"');
2830
Willy Tarreau57bc8912016-04-25 17:09:40 +02002831 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002832 chunk.area = "<BADREQ>";
2833 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002834 } else {
2835 uri = txn->uri;
2836 end = uri + strlen(uri);
2837 // look for the first question mark
2838 while (uri < end && *uri != '?')
2839 uri++;
2840
2841 qmark = uri;
2842 // look for first space or question mark after url
2843 while (uri < end && !HTTP_IS_SPHT(*uri))
2844 uri++;
2845
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002846 chunk.area = qmark;
2847 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002848 }
2849
Dragan Dosen835b9212016-02-12 13:23:03 +01002850 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002851 if (ret == NULL || *ret != '\0')
2852 goto out;
2853
2854 tmplog = ret;
2855 if (tmp->options & LOG_OPT_QUOTE)
2856 LOGCHAR('"');
2857
2858 last_isspace = 0;
2859 break;
2860
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002861 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002862 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002863
Willy Tarreaub7636d12015-06-17 19:58:02 +02002864 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002865 LOGCHAR('"');
2866
2867 end = uri + strlen(uri);
2868 // look for the first whitespace character
2869 while (uri < end && !HTTP_IS_SPHT(*uri))
2870 uri++;
2871
2872 // keep advancing past multiple spaces
2873 while (uri < end && HTTP_IS_SPHT(*uri)) {
2874 uri++; nspaces++;
2875 }
2876
2877 // look for first space after url
2878 spc = uri;
2879 while (spc < end && !HTTP_IS_SPHT(*spc))
2880 spc++;
2881
Willy Tarreau57bc8912016-04-25 17:09:40 +02002882 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002883 chunk.area = "<BADREQ>";
2884 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002885 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002886 chunk.area = uri;
2887 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002888 }
2889
Dragan Dosen835b9212016-02-12 13:23:03 +01002890 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002891 if (ret == NULL || *ret != '\0')
2892 goto out;
2893
2894 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002895 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002896 LOGCHAR('"');
2897
2898 last_isspace = 0;
2899 break;
2900
2901 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002902 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002903 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002904 LOGCHAR('"');
2905
2906 end = uri + strlen(uri);
2907 // look for the first whitespace character
2908 spc = uri;
2909 while (spc < end && !HTTP_IS_SPHT(*spc))
2910 spc++;
2911
2912 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002913 chunk.area = "<BADREQ>";
2914 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002915 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002916 chunk.area = uri;
2917 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002918 }
2919
Dragan Dosen835b9212016-02-12 13:23:03 +01002920 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002921 if (ret == NULL || *ret != '\0')
2922 goto out;
2923
2924 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002925 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002926 LOGCHAR('"');
2927
2928 last_isspace = 0;
2929 break;
2930
2931 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002932 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002933 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002934 LOGCHAR('"');
2935
2936 end = uri + strlen(uri);
2937 // look for the first whitespace character
2938 while (uri < end && !HTTP_IS_SPHT(*uri))
2939 uri++;
2940
2941 // keep advancing past multiple spaces
2942 while (uri < end && HTTP_IS_SPHT(*uri)) {
2943 uri++; nspaces++;
2944 }
2945
2946 // look for the next whitespace character
2947 while (uri < end && !HTTP_IS_SPHT(*uri))
2948 uri++;
2949
2950 // keep advancing past multiple spaces
2951 while (uri < end && HTTP_IS_SPHT(*uri))
2952 uri++;
2953
Willy Tarreau57bc8912016-04-25 17:09:40 +02002954 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002955 chunk.area = "<BADREQ>";
2956 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002957 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002958 chunk.area = "HTTP/0.9";
2959 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002960 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002961 chunk.area = uri;
2962 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002963 }
2964
Dragan Dosen835b9212016-02-12 13:23:03 +01002965 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002966 if (ret == NULL || *ret != '\0')
2967 goto out;
2968
2969 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002970 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002971 LOGCHAR('"');
2972
2973 last_isspace = 0;
2974 break;
2975
William Lallemand5f232402012-04-05 18:02:55 +02002976 case LOG_FMT_COUNTER: // %rt
2977 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002978 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002979 if (iret < 0 || iret > dst + maxsize - tmplog)
2980 goto out;
2981 last_isspace = 0;
2982 tmplog += iret;
2983 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002984 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002985 if (ret == NULL)
2986 goto out;
2987 tmplog = ret;
2988 last_isspace = 0;
2989 }
2990 break;
2991
Willy Tarreau7346acb2014-08-28 15:03:15 +02002992 case LOG_FMT_LOGCNT: // %lc
2993 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002994 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002995 if (iret < 0 || iret > dst + maxsize - tmplog)
2996 goto out;
2997 last_isspace = 0;
2998 tmplog += iret;
2999 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003000 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003001 if (ret == NULL)
3002 goto out;
3003 tmplog = ret;
3004 last_isspace = 0;
3005 }
3006 break;
3007
William Lallemand5f232402012-04-05 18:02:55 +02003008 case LOG_FMT_HOSTNAME: // %H
3009 src = hostname;
3010 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3011 if (ret == NULL)
3012 goto out;
3013 tmplog = ret;
3014 last_isspace = 0;
3015 break;
3016
3017 case LOG_FMT_PID: // %pid
3018 if (tmp->options & LOG_OPT_HEXA) {
3019 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3020 if (iret < 0 || iret > dst + maxsize - tmplog)
3021 goto out;
3022 last_isspace = 0;
3023 tmplog += iret;
3024 } else {
3025 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3026 if (ret == NULL)
3027 goto out;
3028 tmplog = ret;
3029 last_isspace = 0;
3030 }
3031 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003032
3033 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003034 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003035 if (s)
3036 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3037 else
3038 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003039 if (ret == NULL)
3040 goto out;
3041 tmplog = ret;
3042 last_isspace = 0;
3043 break;
3044
William Lallemandbddd4fd2012-02-27 11:23:10 +01003045 }
3046 }
3047
3048out:
William Lallemand1d705562012-03-12 12:46:41 +01003049 /* *tmplog is a unused character */
3050 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003051 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003052
Willy Tarreaubaaee002006-06-26 02:48:02 +02003053}
3054
William Lallemand1d705562012-03-12 12:46:41 +01003055/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003056 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003057 * Will not log if the frontend has no log defined.
3058 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003059void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003060{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003061 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003062 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003063 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003064
3065 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003066 err = (s->flags & SF_REDISP) ||
3067 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
3068 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01003069 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003070 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003071
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003072 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003073 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003074
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003075 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003076 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003077
Willy Tarreauabcd5142013-06-11 17:18:02 +02003078 if (s->logs.level) { /* loglevel was overridden */
3079 if (s->logs.level == -1) {
3080 s->logs.logwait = 0; /* logs disabled */
3081 return;
3082 }
3083 level = s->logs.level - 1;
3084 }
3085 else {
3086 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003087 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003088 level = LOG_ERR;
3089 }
William Lallemand1d705562012-03-12 12:46:41 +01003090
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003091 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003092 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003093 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003094 }
3095
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003096 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3097 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3098 &sess->fe->logformat_sd);
3099 }
3100
Dragan Dosen59cee972015-09-19 22:09:02 +02003101 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003102 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003103 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003104 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3105 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003106 s->logs.logwait = 0;
3107 }
3108}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003109
Willy Tarreau53839352018-09-05 19:51:10 +02003110/*
3111 * send a minimalist log for the session. Will not log if the frontend has no
3112 * log defined. It is assumed that this is only used to report anomalies that
3113 * cannot lead to the creation of a regular stream. Because of this the log
3114 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3115 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003116 * function to report unimportant events. It is safe to call this function with
3117 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003118 */
3119void sess_log(struct session *sess)
3120{
3121 int size, level;
3122 int sd_size = 0;
3123
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003124 if (!sess)
3125 return;
3126
Willy Tarreau53839352018-09-05 19:51:10 +02003127 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3128 return;
3129
3130 level = LOG_INFO;
3131 if (sess->fe->options2 & PR_O2_LOGERRORS)
3132 level = LOG_ERR;
3133
3134 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3135 sd_size = sess_build_logline(sess, NULL,
3136 logline_rfc5424, global.max_syslog_len,
3137 &sess->fe->logformat_sd);
3138 }
3139
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02003140 if (!LIST_ISEMPTY(&sess->fe->logformat_error))
3141 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat_error);
3142 else
3143 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
Willy Tarreau53839352018-09-05 19:51:10 +02003144 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003145 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003146 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3147 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003148 }
3149}
3150
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003151void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3152{
3153 va_list argp;
3154 int data_len;
3155
3156 if (level < 0 || format == NULL || logline == NULL)
3157 return;
3158
3159 va_start(argp, format);
3160 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3161 if (data_len < 0 || data_len > global.max_syslog_len)
3162 data_len = global.max_syslog_len;
3163 va_end(argp);
3164
3165 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3166}
Emeric Brun54932b42020-07-07 09:43:24 +02003167/*
3168 * This function parse a received log message <buf>, of size <buflen>
3169 * it fills <level>, <facility> and <metadata> depending of the detected
3170 * header format and message will point on remaining payload of <size>
3171 *
3172 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3173 * struct ist len will be set to 0 if field is not found
3174 * <level> and <facility> will be set to -1 if not found.
3175 */
3176void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3177 struct ist *metadata, char **message, size_t *size)
3178{
3179
3180 char *p;
3181 int fac_level = 0;
3182
3183 *level = *facility = -1;
3184
3185 *message = buf;
3186 *size = buflen;
3187
3188 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3189
3190 p = buf;
3191 if (*size < 2 || *p != '<')
3192 return;
3193
3194 p++;
3195 while (*p != '>') {
3196 if (*p > '9' || *p < '0')
3197 return;
3198 fac_level = 10*fac_level + (*p - '0');
3199 p++;
3200 if ((p - buf) > buflen)
3201 return;
3202 }
3203
3204 *facility = fac_level >> 3;
3205 *level = fac_level & 0x7;
3206 p++;
3207
3208 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3209
3210 buflen -= p - buf;
3211 buf = p;
3212
3213 *size = buflen;
3214 *message = buf;
3215
3216 /* for rfc5424, prio is always followed by '1' and ' ' */
3217 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3218 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3219 * followed by message.
3220 * Each header field can present NILVALUE: '-'
3221 */
3222
3223 p += 2;
3224 /* timestamp is NILVALUE '-' */
3225 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3226 metadata[LOG_META_TIME] = ist2(p, 1);
3227 p++;
3228 }
3229 else if (*size > LOG_ISOTIME_MINLEN) {
3230 metadata[LOG_META_TIME].ptr = p;
3231
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003232 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003233 * in timestamp.
3234 * possible format are:
3235 * ex: '1970-01-01T00:00:00.000000Z'
3236 * '1970-01-01T00:00:00.000000+00:00'
3237 * '1970-01-01T00:00:00.000000-00:00'
3238 * '1970-01-01T00:00:00Z'
3239 * '1970-01-01T00:00:00+00:00'
3240 * '1970-01-01T00:00:00-00:00'
3241 */
3242 p += 19;
3243 if (*p == '.') {
3244 p++;
3245 if ((p - buf) >= buflen)
3246 goto bad_format;
3247 while (*p != 'Z' && *p != '+' && *p != '-') {
3248 if ((unsigned char)(*p - '0') > 9)
3249 goto bad_format;
3250
3251 p++;
3252 if ((p - buf) >= buflen)
3253 goto bad_format;
3254 }
3255 }
3256
3257 if (*p == 'Z')
3258 p++;
3259 else
3260 p += 6; /* case of '+00:00 or '-00:00' */
3261
3262 if ((p - buf) >= buflen || *p != ' ')
3263 goto bad_format;
3264 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3265 }
3266 else
3267 goto bad_format;
3268
3269
3270 p++;
3271 if ((p - buf) >= buflen || *p == ' ')
3272 goto bad_format;
3273
3274 metadata[LOG_META_HOST].ptr = p;
3275 while (*p != ' ') {
3276 p++;
3277 if ((p - buf) >= buflen)
3278 goto bad_format;
3279 }
3280 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3281 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3282 metadata[LOG_META_HOST].len = 0;
3283
3284 p++;
3285 if ((p - buf) >= buflen || *p == ' ')
3286 goto bad_format;
3287
3288 metadata[LOG_META_TAG].ptr = p;
3289 while (*p != ' ') {
3290 p++;
3291 if ((p - buf) >= buflen)
3292 goto bad_format;
3293 }
3294 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3295 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3296 metadata[LOG_META_TAG].len = 0;
3297
3298 p++;
3299 if ((p - buf) >= buflen || *p == ' ')
3300 goto bad_format;
3301
3302 metadata[LOG_META_PID].ptr = p;
3303 while (*p != ' ') {
3304 p++;
3305 if ((p - buf) >= buflen)
3306 goto bad_format;
3307 }
3308 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3309 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3310 metadata[LOG_META_PID].len = 0;
3311
3312 p++;
3313 if ((p - buf) >= buflen || *p == ' ')
3314 goto bad_format;
3315
3316 metadata[LOG_META_MSGID].ptr = p;
3317 while (*p != ' ') {
3318 p++;
3319 if ((p - buf) >= buflen)
3320 goto bad_format;
3321 }
3322 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3323 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3324 metadata[LOG_META_MSGID].len = 0;
3325
3326 p++;
3327 if ((p - buf) >= buflen || *p == ' ')
3328 goto bad_format;
3329
3330 /* structured data format is:
3331 * ex:
3332 * '[key1=value1 key2=value2][key3=value3]'
3333 *
3334 * space is invalid outside [] because
3335 * considered as the end of structured data field
3336 */
3337 metadata[LOG_META_STDATA].ptr = p;
3338 if (*p == '[') {
3339 int elem = 0;
3340
3341 while (1) {
3342 if (elem) {
3343 /* according to rfc this char is escaped in param values */
3344 if (*p == ']' && *(p-1) != '\\')
3345 elem = 0;
3346 }
3347 else {
3348 if (*p == '[')
3349 elem = 1;
3350 else if (*p == ' ')
3351 break;
3352 else
3353 goto bad_format;
3354 }
3355 p++;
3356 if ((p - buf) >= buflen)
3357 goto bad_format;
3358 }
3359 }
3360 else if (*p == '-') {
3361 /* case of NILVALUE */
3362 p++;
3363 if ((p - buf) >= buflen || *p != ' ')
3364 goto bad_format;
3365 }
3366 else
3367 goto bad_format;
3368
3369 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3370 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3371 metadata[LOG_META_STDATA].len = 0;
3372
3373 p++;
3374
3375 buflen -= p - buf;
3376 buf = p;
3377
3378 *size = buflen;
3379 *message = p;
3380 }
3381 else if (*size > LOG_LEGACYTIME_LEN) {
3382 int m;
3383
3384 /* supported header format according to rfc3164.
3385 * ex:
3386 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3387 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3388 * or 'Jan 1 00:00:00 HOSTNAME '
3389 * Note: HOSTNAME is mandatory, and day
3390 * of month uses a single space prefix if
3391 * less than 10 to ensure hour offset is
3392 * always the same.
3393 */
3394
3395 /* Check month to see if it correspond to a rfc3164
3396 * header ex 'Jan 1 00:00:00' */
3397 for (m = 0; m < 12; m++)
3398 if (!memcmp(monthname[m], p, 3))
3399 break;
3400 /* Month not found */
3401 if (m == 12)
3402 goto bad_format;
3403
3404 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3405
3406 p += LOG_LEGACYTIME_LEN;
3407 if ((p - buf) >= buflen || *p != ' ')
3408 goto bad_format;
3409
3410 p++;
3411 if ((p - buf) >= buflen || *p == ' ')
3412 goto bad_format;
3413
3414 metadata[LOG_META_HOST].ptr = p;
3415 while (*p != ' ') {
3416 p++;
3417 if ((p - buf) >= buflen)
3418 goto bad_format;
3419 }
3420 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3421
3422 /* TAG seems to no be mandatory */
3423 p++;
3424
3425 buflen -= p - buf;
3426 buf = p;
3427
3428 *size = buflen;
3429 *message = buf;
3430
3431 if (!buflen)
3432 return;
3433
3434 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3435 p++;
3436
3437 /* a tag must present a trailing ':' */
3438 if (((p - buf) >= buflen) || *p != ':')
3439 return;
3440 p++;
3441 /* followed by a space */
3442 if (((p - buf) >= buflen) || *p != ' ')
3443 return;
3444
3445 /* rewind to parse tag and pid */
3446 p = buf;
3447 metadata[LOG_META_TAG].ptr = p;
3448 /* we have the guarantee that ':' will be reach before size limit */
3449 while (*p != ':') {
3450 if (*p == '[') {
3451 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3452 metadata[LOG_META_PID].ptr = p + 1;
3453 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003454 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003455 if (p[1] != ':')
3456 return;
3457 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3458 }
3459 p++;
3460 }
3461 if (!metadata[LOG_META_TAG].len)
3462 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3463
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003464 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003465 p += 2;
3466
3467 buflen -= p - buf;
3468 buf = p;
3469
3470 *size = buflen;
3471 *message = buf;
3472 }
3473
3474 return;
3475
3476bad_format:
3477 /* bad syslog format, we reset all parsed syslog fields
3478 * but priority is kept because we are able to re-build
3479 * this message using LOF_FORMAT_PRIO.
3480 */
3481 metadata[LOG_META_TIME].len = 0;
3482 metadata[LOG_META_HOST].len = 0;
3483 metadata[LOG_META_TAG].len = 0;
3484 metadata[LOG_META_PID].len = 0;
3485 metadata[LOG_META_MSGID].len = 0;
3486 metadata[LOG_META_STDATA].len = 0;
3487
3488 return;
3489}
3490
3491/*
3492 * UDP syslog fd handler
3493 */
3494void syslog_fd_handler(int fd)
3495{
3496 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3497 ssize_t ret = 0;
3498 struct buffer *buf = get_trash_chunk();
3499 size_t size;
3500 char *message;
3501 int level;
3502 int facility;
3503 struct listener *l = objt_listener(fdtab[fd].owner);
3504 int max_accept;
3505
Tim Duesterhus16554242021-09-15 13:58:49 +02003506 BUG_ON(!l);
Emeric Brun54932b42020-07-07 09:43:24 +02003507
Willy Tarreauf5090652021-04-06 17:23:40 +02003508 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003509
3510 if (!fd_recv_ready(fd))
3511 return;
3512
3513 max_accept = l->maxaccept ? l->maxaccept : 1;
3514
3515 do {
3516 /* Source address */
3517 struct sockaddr_storage saddr = {0};
3518 socklen_t saddrlen;
3519
3520 saddrlen = sizeof(saddr);
3521
3522 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3523 if (ret < 0) {
3524 if (errno == EINTR)
3525 continue;
3526 if (errno == EAGAIN)
3527 fd_cant_recv(fd);
3528 goto out;
3529 }
3530 buf->data = ret;
3531
Emeric Brun45c457a2020-07-09 23:23:34 +02003532 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003533 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003534 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003535
Emeric Brun54932b42020-07-07 09:43:24 +02003536 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3537
3538 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3539
3540 } while (--max_accept);
3541 }
3542
3543out:
3544 return;
3545}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003546
Emeric Brun12941c82020-07-07 14:19:42 +02003547/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003548 * IO Handler to handle message exchange with a syslog tcp client
3549 */
3550static void syslog_io_handler(struct appctx *appctx)
3551{
3552 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3553 struct stream_interface *si = appctx->owner;
3554 struct stream *s = si_strm(si);
3555 struct proxy *frontend = strm_fe(s);
3556 struct listener *l = strm_li(s);
3557 struct buffer *buf = get_trash_chunk();
3558 int max_accept;
3559 int to_skip;
3560 int facility;
3561 int level;
3562 char *message;
3563 size_t size;
3564
3565 max_accept = l->maxaccept ? l->maxaccept : 1;
3566 while (co_data(si_oc(si))) {
3567 char c;
3568
3569 if (max_accept <= 0)
3570 goto missing_budget;
3571 max_accept--;
3572
3573 to_skip = co_getchar(si_oc(si), &c);
3574 if (!to_skip)
3575 goto missing_data;
3576 else if (to_skip < 0)
3577 goto cli_abort;
3578
3579 if (c == '<') {
3580 /* rfc-6587, Non-Transparent-Framing: messages separated by
3581 * a trailing LF or CR LF
3582 */
3583 to_skip = co_getline(si_oc(si), buf->area, buf->size);
3584 if (!to_skip)
3585 goto missing_data;
3586 else if (to_skip < 0)
3587 goto cli_abort;
3588
3589 if (buf->area[to_skip - 1] != '\n')
3590 goto parse_error;
3591
3592 buf->data = to_skip - 1;
3593
3594 /* according to rfc-6587, some devices adds CR before LF */
3595 if (buf->data && buf->area[buf->data - 1] == '\r')
3596 buf->data--;
3597
3598 }
3599 else if ((unsigned char)(c - '1') <= 8) {
3600 /* rfc-6587, Octet-Counting: message length in ASCII
3601 * (first digit can not be ZERO), followed by a space
3602 * and message length
3603 */
3604 char *p = NULL;
3605 int msglen;
3606
3607 to_skip = co_getword(si_oc(si), buf->area, buf->size, ' ');
3608 if (!to_skip)
3609 goto missing_data;
3610 else if (to_skip < 0)
3611 goto cli_abort;
3612
3613 if (buf->area[to_skip - 1] != ' ')
3614 goto parse_error;
3615
3616 msglen = strtol(trash.area, &p, 10);
3617 if (!msglen || p != &buf->area[to_skip - 1])
3618 goto parse_error;
3619
3620 /* message seems too large */
3621 if (msglen > buf->size)
3622 goto parse_error;
3623
3624 msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip);
3625 if (!msglen)
3626 goto missing_data;
3627 else if (msglen < 0)
3628 goto cli_abort;
3629
3630
3631 buf->data = msglen;
3632 to_skip += msglen;
3633 }
3634 else
3635 goto parse_error;
3636
3637 co_skip(si_oc(si), to_skip);
3638
3639 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003640 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003641 proxy_inc_fe_req_ctr(l, frontend);
3642
3643 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3644
3645 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3646
3647 }
3648
3649missing_data:
3650 /* we need more data to read */
3651 si_oc(si)->flags |= CF_READ_DONTWAIT;
3652
3653 return;
3654
3655missing_budget:
3656 /* it may remain some stuff to do, let's retry later */
3657 appctx_wakeup(appctx);
3658
3659 return;
3660
3661parse_error:
3662 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003663 _HA_ATOMIC_INC(&l->counters->failed_req);
3664 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003665
3666 goto close;
3667
3668cli_abort:
3669 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003670 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3671 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003672
3673close:
3674 si_shutw(si);
3675 si_shutr(si);
3676
3677 si_ic(si)->flags |= CF_READ_NULL;
3678
3679 return;
3680}
3681
3682static struct applet syslog_applet = {
3683 .obj_type = OBJ_TYPE_APPLET,
3684 .name = "<SYSLOG>", /* used for logging */
3685 .fct = syslog_io_handler,
3686 .release = NULL,
3687};
3688
3689/*
Emeric Brun12941c82020-07-07 14:19:42 +02003690 * Parse "log-forward" section and create corresponding sink buffer.
3691 *
3692 * The function returns 0 in success case, otherwise, it returns error
3693 * flags.
3694 */
3695int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3696{
3697 int err_code = 0;
3698 struct proxy *px;
3699 char *errmsg = NULL;
3700 const char *err = NULL;
3701
3702 if (strcmp(args[0], "log-forward") == 0) {
3703 if (!*args[1]) {
3704 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3705 err_code |= ERR_ALERT | ERR_ABORT;
3706 goto out;
3707 }
3708
3709 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3710 goto out;
3711
3712 err = invalid_char(args[1]);
3713 if (err) {
3714 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3715 file, linenum, *err, args[0], args[1]);
3716 err_code |= ERR_ALERT | ERR_ABORT;
3717 goto out;
3718 }
3719
Emeric Brunb0c331f2020-10-07 17:05:59 +02003720 px = log_forward_by_name(args[1]);
3721 if (px) {
3722 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3723 file, linenum, args[1], px->conf.file, px->conf.line);
3724 err_code |= ERR_ALERT | ERR_FATAL;
3725 }
3726
3727 px = proxy_find_by_name(args[1], 0, 0);
3728 if (px) {
3729 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3730 file, linenum, args[1], proxy_type_str(px),
3731 px->id, px->conf.file, px->conf.line);
3732 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003733 }
3734
3735 px = calloc(1, sizeof *px);
3736 if (!px) {
3737 err_code |= ERR_ALERT | ERR_FATAL;
3738 goto out;
3739 }
3740
Emeric Brun2ad2b1c2021-12-01 12:08:42 +01003741 init_new_proxy(px);
Emeric Brun12941c82020-07-07 14:19:42 +02003742 px->next = cfg_log_forward;
3743 cfg_log_forward = px;
Emeric Brun12941c82020-07-07 14:19:42 +02003744 px->conf.file = strdup(file);
3745 px->conf.line = linenum;
3746 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003747 px->last_change = now.tv_sec;
3748 px->cap = PR_CAP_FE;
3749 px->maxconn = 10;
3750 px->timeout.client = TICK_ETERNITY;
3751 px->accept = frontend_accept;
3752 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003753 px->id = strdup(args[1]);
3754
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003755 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003756 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003757 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3758 err_code |= ERR_WARN;
3759
3760 if (*(args[1]) == 0) {
3761 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3762 err_code |= ERR_ALERT | ERR_FATAL;
3763 goto out;
3764 }
3765 cfg_log_forward->maxconn = atol(args[1]);
3766 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3767 goto out;
3768 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003769 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003770 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3771 err_code |= ERR_WARN;
3772
3773 if (*(args[1]) == 0) {
3774 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3775 err_code |= ERR_ALERT | ERR_FATAL;
3776 goto out;
3777 }
3778 cfg_log_forward->backlog = atol(args[1]);
3779 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3780 goto out;
3781 }
3782 else if (strcmp(args[0], "bind") == 0) {
3783 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003784 struct bind_conf *bind_conf;
3785 struct bind_kw *kw;
3786 struct listener *l;
3787
3788 cur_arg = 1;
3789
3790 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3791 NULL, xprt_get(XPRT_RAW));
3792 if (!bind_conf) {
3793 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3794 err_code |= ERR_ALERT | ERR_FATAL;
3795 goto out;
3796 }
3797
3798 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3799 if (errmsg && *errmsg) {
3800 indent_msg(&errmsg, 2);
3801 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3802 }
3803 else {
3804 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3805 file, linenum, args[0], args[1], args[2]);
3806 err_code |= ERR_ALERT | ERR_FATAL;
3807 goto out;
3808 }
3809 }
3810 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau66161322021-02-19 15:50:27 +01003811 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003812 l->accept = session_accept_fd;
3813 l->analysers |= cfg_log_forward->fe_req_ana;
3814 l->default_target = cfg_log_forward->default_target;
3815 global.maxsock++;
3816 }
3817 cur_arg++;
3818
3819 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3820 int ret;
3821
3822 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3823 err_code |= ret;
3824 if (ret) {
3825 if (errmsg && *errmsg) {
3826 indent_msg(&errmsg, 2);
3827 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3828 }
3829 else
3830 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3831 file, linenum, args[cur_arg]);
3832 if (ret & ERR_FATAL)
3833 goto out;
3834 }
3835 cur_arg += 1 + kw->skip;
3836 }
3837 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003838 const char *best = bind_find_best_kw(args[cur_arg]);
3839 if (best)
3840 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3841 file, linenum, args[cur_arg], cursection, best);
3842 else
3843 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3844 file, linenum, args[cur_arg], cursection);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003845 err_code |= ERR_ALERT | ERR_FATAL;
3846 goto out;
3847 }
Emeric Brun12941c82020-07-07 14:19:42 +02003848 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003849 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003850 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02003851 struct bind_conf *bind_conf;
3852 struct bind_kw *kw;
3853 struct listener *l;
3854
3855 cur_arg = 1;
3856
3857 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3858 NULL, xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +02003859 if (!bind_conf) {
3860 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3861 err_code |= ERR_ALERT | ERR_FATAL;
3862 goto out;
3863 }
Emeric Brun12941c82020-07-07 14:19:42 +02003864
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003865 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003866 if (errmsg && *errmsg) {
3867 indent_msg(&errmsg, 2);
3868 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3869 }
3870 else {
3871 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3872 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003873 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003874 err_code |= ERR_ALERT | ERR_FATAL;
3875 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003876 }
3877 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003878 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreau66161322021-02-19 15:50:27 +01003879 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreaue140a692020-10-15 21:25:32 +02003880 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003881 global.maxsock++;
3882 }
3883 cur_arg++;
3884
3885 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3886 int ret;
3887
3888 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3889 err_code |= ret;
3890 if (ret) {
3891 if (errmsg && *errmsg) {
3892 indent_msg(&errmsg, 2);
3893 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3894 }
3895 else
3896 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3897 file, linenum, args[cur_arg]);
3898 if (ret & ERR_FATAL)
3899 goto out;
3900 }
3901 cur_arg += 1 + kw->skip;
3902 }
3903 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003904 const char *best = bind_find_best_kw(args[cur_arg]);
3905 if (best)
3906 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3907 file, linenum, args[cur_arg], cursection, best);
3908 else
3909 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3910 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02003911 err_code |= ERR_ALERT | ERR_FATAL;
3912 goto out;
3913 }
3914 }
3915 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02003916 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003917 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3918 err_code |= ERR_ALERT | ERR_FATAL;
3919 goto out;
3920 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003921 }
3922 else if (strcmp(args[0], "timeout") == 0) {
3923 const char *res;
3924 unsigned timeout;
3925
3926 if (strcmp(args[1], "client") != 0) {
3927 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
3928 err_code |= ERR_ALERT | ERR_FATAL;
3929 goto out;
3930 }
3931
3932 if (*args[2] == 0) {
3933 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
3934 err_code |= ERR_ALERT | ERR_FATAL;
3935 goto out;
3936 }
3937 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3938 if (res == PARSE_TIME_OVER) {
3939 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
3940 }
3941 else if (res == PARSE_TIME_UNDER) {
3942 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
3943 }
3944 else if (res) {
3945 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
3946 return -1;
3947 }
3948
3949 if (res) {
3950 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3951 err_code |= ERR_ALERT | ERR_FATAL;
3952 goto out;
3953 }
3954 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02003955 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02003956 else {
3957 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
3958 err_code |= ERR_ALERT | ERR_ABORT;
3959 goto out;
3960 }
Emeric Brun12941c82020-07-07 14:19:42 +02003961out:
3962 return err_code;
3963}
3964
Willy Tarreau0108d902018-11-25 19:14:37 +01003965
Emeric Brun12941c82020-07-07 14:19:42 +02003966/* config parsers for this section */
3967REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
3968
Willy Tarreau082b6282019-05-22 14:42:12 +02003969REGISTER_PER_THREAD_ALLOC(init_log_buffers);
3970REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01003971
Willy Tarreaubaaee002006-06-26 02:48:02 +02003972/*
3973 * Local variables:
3974 * c-indent-level: 8
3975 * c-basic-offset: 8
3976 * End:
3977 */